forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

fmo.rs133 lines, 4.8 KiBHistoryRawDownload
1//! Macroblock-to-slice-group map (clause 8.2.2), for FMO streams.
2
3use ec_h264_syntax::pps::SliceGroupMap;
4use ec_h264_syntax::{Pps, Sps};
5
6/// The slice group of every macroblock of a progressive picture, in raster
7/// order. `change_cycle` is `slice_group_change_cycle` of the slice header.
8pub fn mb_to_slice_group(sps: &Sps, pps: &Pps, change_cycle: u32) -> Option<Vec<u8>> {
9 let map = pps.slice_group_map.as_ref()?;
10 let n = pps.num_slice_groups as usize;
11 let w = sps.mb_width as usize;
12 let h = sps.mb_height as usize;
13 let size = w * h;
14 let mut m = vec![0u8; size];
15 match map {
16 SliceGroupMap::Interleaved(runs) => {
17 let mut i = 0;
18 while i < size {
19 for (g, run) in runs.iter().enumerate().take(n) {
20 let mut j = 0;
21 while j <= *run as usize && i + j < size {
22 m[i + j] = g as u8;
23 j += 1;
24 }
25 i += *run as usize + 1;
26 if i >= size {
27 break;
28 }
29 }
30 }
31 }
32 SliceGroupMap::Dispersed => {
33 for (i, v) in m.iter_mut().enumerate() {
34 *v = (((i % w) + (((i / w) * n) / 2)) % n) as u8;
35 }
36 }
37 SliceGroupMap::Foreground(rects) => {
38 m.fill((n - 1) as u8);
39 for g in (0..n - 1).rev() {
40 let (tl, br) = rects[g];
41 let (tl, br) = (tl as usize, br as usize);
42 let (y0, x0, y1, x1) = (tl / w, tl % w, br / w, br % w);
43 for y in y0..=y1.min(h - 1) {
44 for x in x0..=x1.min(w - 1) {
45 m[y * w + x] = g as u8;
46 }
47 }
48 }
49 }
50 SliceGroupMap::Changing {
51 map_type,
52 change_direction,
53 change_rate_minus1,
54 } => {
55 let dir = usize::from(*change_direction);
56 let rate = *change_rate_minus1 as usize + 1;
57 let in0 = (change_cycle as usize * rate).min(size);
58 let upper_left = if dir == 1 { size - in0 } else { in0 };
59 match map_type {
60 3 => {
61 m.fill(1);
62 let (mut x, mut y) = (((w - dir) / 2) as i64, ((h - dir) / 2) as i64);
63 let (mut lb, mut tb, mut rb, mut bb) = (x, y, x, y);
64 let d = dir as i64;
65 let (mut xd, mut yd) = (d - 1, d);
66 let mut k = 0;
67 while k < in0 {
68 let idx = y as usize * w + x as usize;
69 let vacant = m[idx] == 1;
70 if vacant {
71 m[idx] = 0;
72 }
73 if xd == -1 && x == lb {
74 lb = (lb - 1).max(0);
75 x = lb;
76 xd = 0;
77 yd = 2 * d - 1;
78 } else if xd == 1 && x == rb {
79 rb = (rb + 1).min(w as i64 - 1);
80 x = rb;
81 xd = 0;
82 yd = 1 - 2 * d;
83 } else if yd == -1 && y == tb {
84 tb = (tb - 1).max(0);
85 y = tb;
86 xd = 1 - 2 * d;
87 yd = 0;
88 } else if yd == 1 && y == bb {
89 bb = (bb + 1).min(h as i64 - 1);
90 y = bb;
91 xd = 2 * d - 1;
92 yd = 0;
93 } else {
94 x += xd;
95 y += yd;
96 }
97 k += usize::from(vacant);
98 }
99 }
100 4 => {
101 for (i, v) in m.iter_mut().enumerate() {
102 *v = if i < upper_left { dir } else { 1 - dir } as u8;
103 }
104 }
105 _ => {
106 let mut k = 0;
107 for j in 0..w {
108 for i in 0..h {
109 m[i * w + j] = if k < upper_left { dir } else { 1 - dir } as u8;
110 k += 1;
111 }
112 }
113 }
114 }
115 }
116 SliceGroupMap::Explicit(ids) => {
117 for (i, v) in m.iter_mut().enumerate() {
118 *v = ids.get(i).copied().unwrap_or(0) as u8;
119 }
120 }
121 }
122 Some(m)
123}
124
125/// The macroblock after `addr` in the same slice group, or `m.len()`.
126pub fn next_in_group(m: &[u8], addr: usize) -> usize {
127 let g = m[addr];
128 let mut a = addr + 1;
129 while a < m.len() && m[a] != g {
130 a += 1;
131 }
132 a
133}