]>
Commit | Line | Data |
---|---|---|
31cf33ac KS |
1 | use nihav_core::io::bitreader::*; |
2 | use nihav_core::io::codebook::*; | |
3 | use nihav_core::formats; | |
4 | use nihav_core::frame::*; | |
5 | use nihav_core::codecs::*; | |
6 | use nihav_codec_support::codecs::{MV, ZIGZAG}; | |
2af66d55 | 7 | use nihav_codec_support::codecs::blockdsp; |
31cf33ac | 8 | use nihav_codec_support::codecs::h263::*; |
2af66d55 | 9 | use nihav_codec_support::codecs::h263::code::*; |
31cf33ac KS |
10 | use nihav_codec_support::codecs::h263::decoder::*; |
11 | use nihav_codec_support::codecs::h263::data::*; | |
12 | ||
31cf33ac KS |
13 | struct Tables { |
14 | intra_mcbpc_cb: Codebook<u8>, | |
15 | inter_mcbpc_cb: Codebook<u8>, | |
16 | cbpy_cb: Codebook<u8>, | |
17 | rl_cb: Codebook<H263RLSym>, | |
18 | aic_rl_cb: Codebook<H263RLSym>, | |
19 | mv_cb: Codebook<u8>, | |
20 | } | |
21 | ||
e6978c9c KS |
22 | struct VivoBlockDSP { |
23 | dct_tab: [[f64; 64]; 64], | |
24 | } | |
2af66d55 | 25 | |
e6978c9c KS |
26 | fn gen_coef(i: usize, j: usize) -> f64 { |
27 | if i == 0 { | |
28 | 1.0 / 8.0f64.sqrt() | |
29 | } else { | |
30 | (((j as f64) + 0.5) * (i as f64) * std::f64::consts::PI / 8.0).cos() * 0.5 | |
31 | } | |
32 | } | |
33 | ||
34 | impl VivoBlockDSP { | |
35 | fn new() -> Self { | |
36 | let mut dct_tab = [[0.0; 64]; 64]; | |
37 | for i in 0..8 { | |
38 | for j in 0..8 { | |
39 | for k in 0..8 { | |
40 | for l in 0..8 { | |
41 | let c0 = gen_coef(i, k); | |
42 | let c1 = gen_coef(j, l); | |
43 | let c = c0 * c1 * 64.0; | |
44 | dct_tab[i * 8 + j][k * 8 + l] = c; | |
45 | } | |
46 | } | |
47 | } | |
48 | } | |
49 | Self { | |
50 | dct_tab | |
51 | } | |
52 | } | |
53 | } | |
2af66d55 KS |
54 | |
55 | #[allow(clippy::erasing_op)] | |
56 | #[allow(clippy::identity_op)] | |
e6978c9c | 57 | fn deblock_hor(buf: &mut [u8], stride: usize, off: usize, clip_tab: &[i16; 64]) { |
2af66d55 KS |
58 | for x in 0..8 { |
59 | let p1 = i16::from(buf[off - 2 * stride + x]); | |
60 | let p0 = i16::from(buf[off - 1 * stride + x]); | |
61 | let q0 = i16::from(buf[off + 0 * stride + x]); | |
62 | let q1 = i16::from(buf[off + 1 * stride + x]); | |
63 | let diff = (3 * (p1 - q1) + 8 * (q0 - p0)) >> 4; | |
64 | if (diff != 0) && (diff > -32) && (diff < 32) { | |
65 | let delta = clip_tab[(diff + 32) as usize]; | |
66 | buf[off - 1 * stride + x] = (p0 + delta).max(0).min(255) as u8; | |
67 | buf[off + 0 * stride + x] = (q0 - delta).max(0).min(255) as u8; | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | #[allow(clippy::identity_op)] | |
e6978c9c | 73 | fn deblock_ver(buf: &mut [u8], stride: usize, off: usize, clip_tab: &[i16; 64]) { |
2af66d55 KS |
74 | for y in 0..8 { |
75 | let p1 = i16::from(buf[off - 2 + y * stride]); | |
76 | let p0 = i16::from(buf[off - 1 + y * stride]); | |
77 | let q0 = i16::from(buf[off + 0 + y * stride]); | |
78 | let q1 = i16::from(buf[off + 1 + y * stride]); | |
79 | let diff = (3 * (p1 - q1) + 8 * (q0 - p0)) >> 4; | |
80 | if (diff != 0) && (diff > -32) && (diff < 32) { | |
81 | let delta = clip_tab[(diff + 32) as usize]; | |
82 | buf[off - 1 + y * stride] = (p0 + delta).max(0).min(255) as u8; | |
83 | buf[off + y * stride] = (q0 - delta).max(0).min(255) as u8; | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
88 | fn gen_clip_tab(clip_tab: &mut [i16; 64], q: u8) { | |
89 | let q = i16::from(q); | |
90 | *clip_tab = [0; 64]; | |
91 | let lim = (q + 2) >> 1; | |
92 | for i in 0..lim { | |
93 | clip_tab[(32 - i) as usize] = -i; | |
94 | clip_tab[(32 + i) as usize] = i; | |
95 | } | |
96 | for i in lim..q { | |
5181ac4d | 97 | let val = q - i; |
2af66d55 KS |
98 | clip_tab[(32 - i) as usize] = -val; |
99 | clip_tab[(32 + i) as usize] = val; | |
100 | } | |
101 | } | |
102 | ||
103 | impl BlockDSP for VivoBlockDSP { | |
104 | fn idct(&self, blk: &mut [i16; 64]) { | |
e6978c9c KS |
105 | let mut tmp = [0i32; 64]; |
106 | for (i, &el) in blk.iter().enumerate() { | |
107 | if el != 0 { | |
108 | let cmat = self.dct_tab[i]; | |
109 | for (dst, &src) in tmp.iter_mut().zip(cmat.iter()) { | |
110 | *dst += (src * (el as f64)) as i32; | |
111 | } | |
112 | } | |
113 | } | |
114 | for (dst, &src) in blk.iter_mut().zip(tmp.iter()) { | |
115 | *dst = ((src + 0x20) >> 6) as i16; | |
116 | } | |
2af66d55 KS |
117 | } |
118 | fn copy_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mv: MV) { | |
119 | let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize; | |
120 | let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 }); | |
121 | ||
122 | let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); | |
123 | ||
124 | blockdsp::copy_block(&mut dst, src.clone(), 0, xpos, ypos, mv.x >> 1, mv.y >> 1, 16, 16, 0, 1, mode, H263_INTERP_FUNCS); | |
125 | blockdsp::copy_block(&mut dst, src.clone(), 1, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_FUNCS); | |
b7c882c1 | 126 | blockdsp::copy_block(&mut dst, src, 2, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_FUNCS); |
2af66d55 KS |
127 | } |
128 | fn copy_blocks8x8(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mvs: &[MV; 4]) { | |
129 | let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); | |
130 | ||
b7c882c1 | 131 | for (i, mv) in mvs.iter().enumerate() { |
2af66d55 KS |
132 | let xadd = (i & 1) * 8; |
133 | let yadd = (i & 2) * 4; | |
b7c882c1 | 134 | let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize; |
2af66d55 | 135 | |
b7c882c1 | 136 | blockdsp::copy_block(&mut dst, src.clone(), 0, xpos + xadd, ypos + yadd, mv.x >> 1, mv.y >> 1, 8, 8, 0, 1, mode, H263_INTERP_FUNCS); |
2af66d55 KS |
137 | } |
138 | ||
139 | let sum_mv = mvs[0] + mvs[1] + mvs[2] + mvs[3]; | |
140 | let cmx = (sum_mv.x >> 3) + H263_CHROMA_ROUND[(sum_mv.x & 0xF) as usize]; | |
141 | let cmy = (sum_mv.y >> 3) + H263_CHROMA_ROUND[(sum_mv.y & 0xF) as usize]; | |
142 | let mode = ((cmx & 1) + (cmy & 1) * 2) as usize; | |
143 | for plane in 1..3 { | |
144 | blockdsp::copy_block(&mut dst, src.clone(), plane, xpos >> 1, ypos >> 1, cmx >> 1, cmy >> 1, 8, 8, 0, 1, mode, H263_INTERP_FUNCS); | |
145 | } | |
146 | } | |
147 | fn avg_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mv: MV) { | |
148 | let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize; | |
149 | let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 }); | |
150 | ||
151 | let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); | |
152 | ||
153 | blockdsp::copy_block(&mut dst, src.clone(), 0, xpos, ypos, mv.x >> 1, mv.y >> 1, 16, 16, 0, 1, mode, H263_INTERP_AVG_FUNCS); | |
154 | blockdsp::copy_block(&mut dst, src.clone(), 1, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_AVG_FUNCS); | |
b7c882c1 | 155 | blockdsp::copy_block(&mut dst, src, 2, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_AVG_FUNCS); |
2af66d55 KS |
156 | } |
157 | fn avg_blocks8x8(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mvs: &[MV; 4]) { | |
158 | let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); | |
159 | ||
b7c882c1 | 160 | for (i, mv) in mvs.iter().enumerate() { |
2af66d55 KS |
161 | let xadd = (i & 1) * 8; |
162 | let yadd = (i & 2) * 4; | |
b7c882c1 | 163 | let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize; |
2af66d55 | 164 | |
b7c882c1 | 165 | blockdsp::copy_block(&mut dst, src.clone(), 0, xpos + xadd, ypos + yadd, mv.x >> 1, mv.y >> 1, 8, 8, 0, 1, mode, H263_INTERP_AVG_FUNCS); |
2af66d55 KS |
166 | } |
167 | ||
168 | let sum_mv = mvs[0] + mvs[1] + mvs[2] + mvs[3]; | |
169 | let cmx = (sum_mv.x >> 3) + H263_CHROMA_ROUND[(sum_mv.x & 0xF) as usize]; | |
170 | let cmy = (sum_mv.y >> 3) + H263_CHROMA_ROUND[(sum_mv.y & 0xF) as usize]; | |
171 | let mode = ((cmx & 1) + (cmy & 1) * 2) as usize; | |
172 | for plane in 1..3 { | |
173 | blockdsp::copy_block(&mut dst, src.clone(), plane, xpos >> 1, ypos >> 1, cmx >> 1, cmy >> 1, 8, 8, 0, 1, mode, H263_INTERP_AVG_FUNCS); | |
174 | } | |
175 | } | |
176 | fn filter_row(&self, buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo) { | |
177 | let ystride = buf.get_stride(0); | |
178 | let ustride = buf.get_stride(1); | |
179 | let vstride = buf.get_stride(2); | |
180 | let yoff = buf.get_offset(0) + mb_y * 16 * ystride; | |
181 | let uoff = buf.get_offset(1) + mb_y * 8 * ustride; | |
182 | let voff = buf.get_offset(2) + mb_y * 8 * vstride; | |
183 | let buf = buf.get_data_mut().unwrap(); | |
184 | ||
185 | let mut clip_tab = [0i16; 64]; | |
186 | let mut last_q = 0; | |
187 | let mut off = yoff; | |
188 | for mb_x in 0..mb_w { | |
2af66d55 KS |
189 | let q = cbpi.get_q(mb_w + mb_x); |
190 | if q != last_q { | |
191 | gen_clip_tab(&mut clip_tab, q); | |
192 | last_q = q; | |
193 | } | |
194 | if mb_y != 0 { | |
e6978c9c KS |
195 | deblock_hor(buf, ystride, off, &clip_tab); |
196 | deblock_hor(buf, ystride, off + 8, &clip_tab); | |
2af66d55 | 197 | } |
e6978c9c KS |
198 | deblock_hor(buf, ystride, off + 8 * ystride, &clip_tab); |
199 | deblock_hor(buf, ystride, off + 8 * ystride + 8, &clip_tab); | |
2af66d55 KS |
200 | off += 16; |
201 | } | |
2af66d55 KS |
202 | let mut off = yoff; |
203 | for mb_x in 0..mb_w { | |
2af66d55 KS |
204 | let q = cbpi.get_q(mb_w + mb_x); |
205 | if q != last_q { | |
206 | gen_clip_tab(&mut clip_tab, q); | |
207 | last_q = q; | |
208 | } | |
209 | if mb_y != 0 { | |
2af66d55 KS |
210 | let qtop = cbpi.get_q(mb_x); |
211 | if qtop != last_q { | |
212 | gen_clip_tab(&mut clip_tab, qtop); | |
213 | last_q = qtop; | |
214 | } | |
e6978c9c KS |
215 | if mb_x != 0 { |
216 | deblock_ver(buf, ystride, off - 8 * ystride, &clip_tab); | |
2af66d55 | 217 | } |
e6978c9c | 218 | deblock_ver(buf, ystride, off - 8 * ystride + 8, &clip_tab); |
2af66d55 | 219 | } |
e6978c9c | 220 | if mb_x != 0 { |
2af66d55 | 221 | deblock_ver(buf, ystride, off, &clip_tab); |
2af66d55 KS |
222 | deblock_ver(buf, ystride, off + 8, &clip_tab); |
223 | } | |
2af66d55 KS |
224 | off += 16; |
225 | } | |
226 | if mb_y != 0 { | |
227 | for mb_x in 0..mb_w { | |
2af66d55 KS |
228 | let q = cbpi.get_q(mb_w + mb_x); |
229 | if q != last_q { | |
230 | gen_clip_tab(&mut clip_tab, q); | |
231 | last_q = q; | |
232 | } | |
e6978c9c KS |
233 | deblock_hor(buf, ustride, uoff + mb_x * 8, &clip_tab); |
234 | deblock_hor(buf, vstride, voff + mb_x * 8, &clip_tab); | |
2af66d55 | 235 | } |
2af66d55 KS |
236 | let offu = uoff - 8 * ustride; |
237 | let offv = voff - 8 * vstride; | |
e6978c9c | 238 | for mb_x in 1..mb_w { |
2af66d55 KS |
239 | let qt = cbpi.get_q(mb_x); |
240 | if qt != last_q { | |
241 | gen_clip_tab(&mut clip_tab, qt); | |
242 | last_q = qt; | |
243 | } | |
e6978c9c KS |
244 | deblock_ver(buf, ustride, offu + mb_x * 8, &clip_tab); |
245 | deblock_ver(buf, vstride, offv + mb_x * 8, &clip_tab); | |
2af66d55 KS |
246 | } |
247 | } | |
248 | } | |
249 | } | |
250 | ||
31cf33ac KS |
251 | struct VivoDecoder { |
252 | info: NACodecInfoRef, | |
253 | dec: H263BaseDecoder, | |
254 | tables: Tables, | |
2af66d55 | 255 | bdsp: VivoBlockDSP, |
31cf33ac KS |
256 | lastframe: Option<NABufferType>, |
257 | lastpts: Option<u64>, | |
258 | width: usize, | |
259 | height: usize, | |
260 | } | |
261 | ||
262 | struct VivoBR<'a> { | |
263 | br: BitReader<'a>, | |
264 | tables: &'a Tables, | |
265 | gob_no: usize, | |
266 | mb_w: usize, | |
267 | is_pb: bool, | |
268 | is_ipb: bool, | |
269 | ref_w: usize, | |
270 | ref_h: usize, | |
271 | aic: bool, | |
272 | } | |
273 | ||
6f263099 | 274 | fn check_marker(br: &mut BitReader) -> DecoderResult<()> { |
31cf33ac KS |
275 | let mark = br.read(1)?; |
276 | validate!(mark == 1); | |
277 | Ok(()) | |
278 | } | |
279 | ||
280 | impl<'a> VivoBR<'a> { | |
281 | fn new(src: &'a [u8], tables: &'a Tables, ref_w: usize, ref_h: usize) -> Self { | |
282 | VivoBR { | |
283 | br: BitReader::new(src, BitReaderMode::BE), | |
284 | tables, | |
285 | gob_no: 0, | |
286 | mb_w: 0, | |
287 | is_pb: false, | |
288 | is_ipb: false, | |
289 | ref_w, ref_h, | |
290 | aic: false, | |
291 | } | |
292 | } | |
293 | ||
2af66d55 | 294 | fn decode_block(&mut self, quant: u8, intra: bool, coded: bool, blk: &mut [i16; 64], _plane_no: usize, acpred: ACPredMode) -> DecoderResult<()> { |
31cf33ac KS |
295 | let br = &mut self.br; |
296 | let mut idx = 0; | |
297 | if !self.aic && intra { | |
298 | let mut dc = br.read(8)? as i16; | |
299 | if dc == 255 { dc = 128; } | |
300 | blk[0] = dc << 3; | |
301 | idx = 1; | |
302 | } | |
303 | if !coded { return Ok(()); } | |
304 | let scan = match acpred { | |
305 | ACPredMode::Hor => H263_SCAN_V, | |
306 | ACPredMode::Ver => H263_SCAN_H, | |
307 | _ => &ZIGZAG, | |
308 | }; | |
309 | ||
310 | let rl_cb = if self.aic && intra { &self.tables.aic_rl_cb } else { &self.tables.rl_cb }; | |
e6978c9c KS |
311 | let q = i16::from(quant) * 2; |
312 | let q_add = q >> 1; | |
31cf33ac KS |
313 | while idx < 64 { |
314 | let code = br.read_cb(rl_cb)?; | |
315 | let run; | |
316 | let mut level; | |
317 | let last; | |
318 | if !code.is_escape() { | |
319 | run = code.get_run(); | |
320 | level = code.get_level(); | |
321 | last = code.is_last(); | |
322 | if br.read_bool()? { level = -level; } | |
e6978c9c | 323 | if !intra || !self.aic { |
2af66d55 KS |
324 | if level >= 0 { |
325 | level = (level * q) + q_add; | |
326 | } else { | |
327 | level = (level * q) - q_add; | |
328 | } | |
31cf33ac KS |
329 | } |
330 | } else { | |
331 | last = br.read_bool()?; | |
332 | run = br.read(6)? as u8; | |
333 | level = br.read_s(8)? as i16; | |
334 | if level == -128 { | |
335 | let low = br.read(5)? as i16; | |
336 | let top = br.read_s(6)? as i16; | |
337 | level = (top << 5) | low; | |
338 | } | |
e6978c9c | 339 | if !intra || !self.aic { |
2af66d55 KS |
340 | if level >= 0 { |
341 | level = (level * q) + q_add; | |
342 | } else { | |
343 | level = (level * q) - q_add; | |
344 | } | |
345 | if level < -2048 { level = -2048; } | |
346 | if level > 2047 { level = 2047; } | |
31cf33ac | 347 | } |
31cf33ac KS |
348 | } |
349 | idx += run; | |
350 | validate!(idx < 64); | |
351 | let oidx = scan[idx as usize]; | |
352 | blk[oidx] = level; | |
353 | idx += 1; | |
354 | if last { break; } | |
355 | } | |
356 | Ok(()) | |
357 | } | |
358 | } | |
359 | ||
360 | fn decode_mv_component(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<i16> { | |
361 | let code = i16::from(br.read_cb(mv_cb)?); | |
362 | if code == 0 { return Ok(0) } | |
363 | if !br.read_bool()? { | |
364 | Ok(code) | |
365 | } else { | |
366 | Ok(-code) | |
367 | } | |
368 | } | |
369 | ||
370 | fn decode_mv(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<MV> { | |
371 | let xval = decode_mv_component(br, mv_cb)?; | |
372 | let yval = decode_mv_component(br, mv_cb)?; | |
373 | Ok(MV::new(xval, yval)) | |
374 | } | |
375 | ||
376 | fn decode_b_info(br: &mut BitReader, is_pb: bool, is_ipb: bool, is_intra: bool) -> DecoderResult<BBlockInfo> { | |
377 | if is_pb { // as improved pb | |
378 | if is_ipb { | |
379 | let pb_mv_add = if is_intra { 1 } else { 0 }; | |
380 | if br.read_bool()?{ | |
381 | if br.read_bool()? { | |
382 | let pb_mv_count = 1 - (br.read(1)? as usize); | |
383 | let cbpb = br.read(6)? as u8; | |
384 | Ok(BBlockInfo::new(true, cbpb, pb_mv_count + pb_mv_add, pb_mv_count == 1)) | |
385 | } else { | |
386 | Ok(BBlockInfo::new(true, 0, 1 + pb_mv_add, true)) | |
387 | } | |
388 | } else { | |
389 | Ok(BBlockInfo::new(true, 0, pb_mv_add, false)) | |
390 | } | |
391 | } else { | |
392 | let mvdb = br.read_bool()?; | |
393 | let cbpb = if mvdb && br.read_bool()? { br.read(6)? as u8 } else { 0 }; | |
394 | Ok(BBlockInfo::new(true, cbpb, if mvdb { 1 } else { 0 }, false)) | |
395 | } | |
396 | } else { | |
397 | Ok(BBlockInfo::new(false, 0, 0, false)) | |
398 | } | |
399 | } | |
400 | ||
401 | impl<'a> BlockDecoder for VivoBR<'a> { | |
402 | ||
31cf33ac KS |
403 | fn decode_pichdr(&mut self) -> DecoderResult<PicInfo> { |
404 | let br = &mut self.br; | |
405 | let syncw = br.read(22)?; | |
406 | validate!(syncw == 0x000020); | |
407 | let tr = (br.read(8)? << 8) as u16; | |
408 | check_marker(br)?; | |
409 | let id = br.read(1)?; | |
410 | validate!(id == 0); | |
411 | br.read(1)?; // split screen indicator | |
412 | br.read(1)?; // document camera indicator | |
413 | br.read(1)?; // freeze picture release | |
414 | let mut sfmt = br.read(3)?; | |
415 | validate!(sfmt != 0b000); | |
416 | let is_intra = !br.read_bool()?; | |
417 | let umv = br.read_bool()?; | |
418 | br.read(1)?; // syntax arithmetic coding | |
419 | let apm = br.read_bool()?; | |
420 | self.is_pb = br.read_bool()?; | |
421 | let deblock; | |
422 | let pbplus; | |
423 | let aic; | |
424 | if sfmt == 0b110 { | |
425 | sfmt = br.read(3)?; | |
426 | validate!(sfmt != 0b000 && sfmt != 0b110); | |
427 | aic = br.read_bool()?; | |
31cf33ac | 428 | deblock = br.read_bool()?; |
31cf33ac | 429 | br.read(4)?; // unknown flags |
e6978c9c KS |
430 | pbplus = br.read_bool()?; |
431 | br.read(1)?; // unknown flag | |
432 | let _interlaced = br.read_bool()?; | |
433 | br.read(2)?; // unknown flags | |
31cf33ac KS |
434 | } else { |
435 | aic = false; | |
436 | deblock = false; | |
437 | pbplus = false; | |
438 | } | |
439 | self.is_ipb = pbplus; | |
440 | let (w, h) = match sfmt { | |
441 | 0b001 => ( 64, 48), | |
442 | 0b011 => ( 88, 72), | |
443 | 0b010 => (176, 144), | |
444 | 0b100 => (352, 288), | |
445 | 0b101 => (704, 576), | |
446 | 0b111 => { | |
447 | validate!((self.ref_w != 0) && (self.ref_h != 0)); | |
448 | ((self.ref_w + 15) & !15, (self.ref_h + 15) & !15) | |
449 | }, | |
450 | _ => return Err(DecoderError::InvalidData), | |
451 | }; | |
452 | let quant = br.read(5)?; | |
453 | let cpm = br.read_bool()?; | |
454 | validate!(!cpm); | |
455 | ||
8dc6faae KS |
456 | let pbinfo = if self.is_pb { |
457 | let trb = br.read(3)?; | |
458 | let dbquant = br.read(2)?; | |
459 | Some(PBInfo::new(trb as u8, dbquant as u8, pbplus)) | |
460 | } else { None }; | |
31cf33ac KS |
461 | while br.read_bool()? { // skip PEI |
462 | br.read(8)?; | |
463 | } | |
464 | self.gob_no = 0; | |
465 | self.mb_w = (w + 15) >> 4; | |
466 | self.aic = aic; | |
467 | ||
468 | let ftype = if is_intra { Type::I } else { Type::P }; | |
469 | let plusinfo = Some(PlusInfo::new(aic, deblock, false, false)); | |
470 | let mvmode = if umv { MVMode::UMV } else { MVMode::Old }; | |
471 | let picinfo = PicInfo::new(w, h, ftype, mvmode, umv, apm, quant as u8, tr, pbinfo, plusinfo); | |
472 | Ok(picinfo) | |
473 | } | |
474 | ||
8dc6faae | 475 | fn decode_slice_header(&mut self, _info: &PicInfo) -> DecoderResult<SliceInfo> { |
31cf33ac KS |
476 | let br = &mut self.br; |
477 | let gbsc = br.read(17)?; | |
478 | validate!(gbsc == 1); | |
8dc6faae KS |
479 | let _gn = br.read(5)?; |
480 | let _gfid = br.read(2)?; | |
31cf33ac KS |
481 | let gquant = br.read(5)?; |
482 | let ret = SliceInfo::new_gob(0, self.gob_no, gquant as u8); | |
483 | self.gob_no += 1; | |
484 | Ok(ret) | |
485 | } | |
486 | ||
8dc6faae | 487 | fn decode_block_header(&mut self, info: &PicInfo, slice: &SliceInfo, _sstate: &SliceState) -> DecoderResult<BlockInfo> { |
31cf33ac KS |
488 | let br = &mut self.br; |
489 | let mut q = slice.get_quant(); | |
490 | match info.get_mode() { | |
491 | Type::I => { | |
492 | let mut cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; | |
493 | while cbpc == 8 { cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; } | |
494 | let mut acpred = ACPredMode::None; | |
495 | if let Some(ref pi) = info.plusinfo { | |
496 | if pi.aic { | |
497 | let acpp = br.read_bool()?; | |
498 | acpred = ACPredMode::DC; | |
499 | if acpp { | |
500 | acpred = if !br.read_bool()? { ACPredMode::Hor } else { ACPredMode::Ver }; | |
501 | } | |
502 | } | |
503 | } | |
504 | let cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
505 | let cbp = (cbpy << 2) | (cbpc & 3); | |
506 | let dquant = (cbpc & 4) != 0; | |
507 | if dquant { | |
508 | let idx = br.read(2)? as usize; | |
509 | q = (i16::from(q) + i16::from(H263_DQUANT_TAB[idx])) as u8; | |
510 | } | |
511 | let mut binfo = BlockInfo::new(Type::I, cbp, q); | |
512 | binfo.set_acpred(acpred); | |
513 | Ok(binfo) | |
514 | }, | |
515 | Type::P => { | |
516 | if br.read_bool()? { return Ok(BlockInfo::new(Type::Skip, 0, info.get_quant())); } | |
517 | let mut cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; | |
518 | while cbpc == 20 { cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; } | |
519 | let is_intra = (cbpc & 0x04) != 0; | |
520 | let dquant = (cbpc & 0x08) != 0; | |
521 | let is_4x4 = (cbpc & 0x10) != 0; | |
522 | if is_intra { | |
523 | let mut acpred = ACPredMode::None; | |
524 | if let Some(ref pi) = info.plusinfo { | |
525 | if pi.aic { | |
526 | let acpp = br.read_bool()?; | |
527 | acpred = ACPredMode::DC; | |
528 | if acpp { | |
529 | acpred = if !br.read_bool()? { ACPredMode::Hor } else { ACPredMode::Ver }; | |
530 | } | |
531 | } | |
532 | } | |
533 | let mut mvec: Vec<MV> = Vec::new(); | |
534 | let bbinfo = decode_b_info(br, self.is_pb, self.is_ipb, true)?; | |
535 | let cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
536 | let cbp = (cbpy << 2) | (cbpc & 3); | |
537 | if dquant { | |
538 | let idx = br.read(2)? as usize; | |
539 | q = (i16::from(q) + i16::from(H263_DQUANT_TAB[idx])) as u8; | |
540 | } | |
541 | let mut binfo = BlockInfo::new(Type::I, cbp, q); | |
542 | binfo.set_bpart(bbinfo); | |
543 | binfo.set_acpred(acpred); | |
544 | if self.is_pb { | |
545 | for _ in 0..bbinfo.get_num_mv() { | |
546 | mvec.push(decode_mv(br, &self.tables.mv_cb)?); | |
547 | } | |
548 | binfo.set_b_mv(mvec.as_slice()); | |
549 | } | |
550 | return Ok(binfo); | |
551 | } | |
552 | ||
553 | let bbinfo = decode_b_info(br, self.is_pb, self.is_ipb, false)?; | |
554 | let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
555 | // if /* !aiv && */(cbpc & 3) != 3 { | |
556 | cbpy ^= 0xF; | |
557 | // } | |
558 | let cbp = (cbpy << 2) | (cbpc & 3); | |
559 | if dquant { | |
560 | let idx = br.read(2)? as usize; | |
561 | q = (i16::from(q) + i16::from(H263_DQUANT_TAB[idx])) as u8; | |
562 | } | |
563 | let mut binfo = BlockInfo::new(Type::P, cbp, q); | |
564 | binfo.set_bpart(bbinfo); | |
565 | if !is_4x4 { | |
566 | let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?]; | |
567 | binfo.set_mv(&mvec); | |
568 | } else { | |
569 | let mvec: [MV; 4] = [ | |
570 | decode_mv(br, &self.tables.mv_cb)?, | |
571 | decode_mv(br, &self.tables.mv_cb)?, | |
572 | decode_mv(br, &self.tables.mv_cb)?, | |
573 | decode_mv(br, &self.tables.mv_cb)? | |
574 | ]; | |
575 | binfo.set_mv(&mvec); | |
576 | } | |
577 | if self.is_pb { | |
578 | let mut mvec: Vec<MV> = Vec::with_capacity(bbinfo.get_num_mv()); | |
579 | for _ in 0..bbinfo.get_num_mv() { | |
580 | let mv = decode_mv(br, &self.tables.mv_cb)?; | |
581 | mvec.push(mv); | |
582 | } | |
583 | binfo.set_b_mv(mvec.as_slice()); | |
584 | } | |
585 | Ok(binfo) | |
586 | }, | |
587 | _ => Err(DecoderError::InvalidData), | |
588 | } | |
589 | } | |
590 | ||
31cf33ac KS |
591 | fn decode_block_intra(&mut self, info: &BlockInfo, _sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { |
592 | self.decode_block(quant, true, coded, blk, if no < 4 { 0 } else { no - 3 }, info.get_acpred()) | |
593 | } | |
594 | ||
8dc6faae | 595 | fn decode_block_inter(&mut self, _info: &BlockInfo, _sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { |
31cf33ac KS |
596 | self.decode_block(quant, false, coded, blk, if no < 4 { 0 } else { no - 3 }, ACPredMode::None) |
597 | } | |
598 | ||
599 | fn is_slice_end(&mut self) -> bool { self.br.peek(16) == 0 } | |
600 | } | |
601 | ||
602 | impl VivoDecoder { | |
603 | fn new() -> Self { | |
604 | let mut coderead = H263ShortCodeReader::new(H263_INTRA_MCBPC); | |
605 | let intra_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
606 | let mut coderead = H263ShortCodeReader::new(H263_INTER_MCBPC); | |
607 | let inter_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
608 | let mut coderead = H263ShortCodeReader::new(H263_CBPY); | |
609 | let cbpy_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
610 | let mut coderead = H263RLCodeReader::new(H263_RL_CODES); | |
611 | let rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
612 | let mut coderead = H263RLCodeReader::new(H263_RL_CODES_AIC); | |
613 | let aic_rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
614 | let mut coderead = H263ShortCodeReader::new(H263_MV); | |
615 | let mv_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
616 | let tables = Tables { | |
617 | intra_mcbpc_cb, | |
618 | inter_mcbpc_cb, | |
619 | cbpy_cb, | |
620 | rl_cb, | |
621 | aic_rl_cb, | |
622 | mv_cb, | |
623 | }; | |
624 | ||
625 | VivoDecoder{ | |
626 | info: NACodecInfo::new_dummy(), | |
2af66d55 | 627 | dec: H263BaseDecoder::new_with_opts(H263DEC_OPT_SLICE_RESET | H263DEC_OPT_USES_GOB | H263DEC_OPT_PRED_QUANT), |
31cf33ac | 628 | tables, |
2af66d55 | 629 | bdsp: VivoBlockDSP::new(), |
31cf33ac KS |
630 | lastframe: None, |
631 | lastpts: None, | |
632 | width: 0, | |
633 | height: 0, | |
634 | } | |
635 | } | |
636 | } | |
637 | ||
638 | impl NADecoder for VivoDecoder { | |
639 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { | |
640 | if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { | |
641 | let w = vinfo.get_width(); | |
642 | let h = vinfo.get_height(); | |
643 | let fmt = formats::YUV420_FORMAT; | |
644 | let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt)); | |
645 | self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref(); | |
646 | self.width = w; | |
647 | self.height = h; | |
648 | Ok(()) | |
649 | } else { | |
650 | Err(DecoderError::InvalidData) | |
651 | } | |
652 | } | |
653 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { | |
654 | let src = pkt.get_buffer(); | |
655 | ||
656 | if src.len() == 0 { | |
657 | let buftype; | |
658 | let ftype; | |
659 | if self.lastframe.is_none() { | |
660 | buftype = NABufferType::None; | |
661 | ftype = FrameType::Skip; | |
662 | } else { | |
663 | let mut buf = None; | |
664 | std::mem::swap(&mut self.lastframe, &mut buf); | |
665 | buftype = buf.unwrap(); | |
666 | ftype = FrameType::B; | |
667 | } | |
668 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), buftype); | |
669 | frm.set_keyframe(false); | |
670 | frm.set_frame_type(ftype); | |
671 | if self.lastpts.is_some() { | |
672 | frm.set_pts(self.lastpts); | |
673 | self.lastpts = None; | |
674 | } | |
675 | return Ok(frm.into_ref()); | |
676 | } | |
677 | let mut ibr = VivoBR::new(&src, &self.tables, self.width, self.height); | |
678 | ||
679 | let bufinfo = self.dec.parse_frame(&mut ibr, &self.bdsp)?; | |
680 | ||
681 | let mut cur_pts = pkt.get_pts(); | |
682 | if !self.dec.is_intra() { | |
683 | let bret = self.dec.get_bframe(&self.bdsp); | |
684 | if let Ok(b_buf) = bret { | |
685 | self.lastframe = Some(b_buf); | |
686 | self.lastpts = pkt.get_pts(); | |
687 | if let Some(pts) = pkt.get_pts() { | |
688 | cur_pts = Some(pts + 1); | |
689 | } | |
690 | } | |
691 | } | |
692 | ||
693 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); | |
694 | frm.set_keyframe(self.dec.is_intra()); | |
695 | frm.set_frame_type(if self.dec.is_intra() { FrameType::I } else { FrameType::P }); | |
696 | frm.set_pts(cur_pts); | |
697 | Ok(frm.into_ref()) | |
698 | } | |
699 | fn flush(&mut self) { | |
700 | self.dec.flush(); | |
701 | } | |
702 | } | |
703 | ||
7d57ae2f KS |
704 | impl NAOptionHandler for VivoDecoder { |
705 | fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } | |
706 | fn set_options(&mut self, _options: &[NAOption]) { } | |
707 | fn query_option_value(&self, _name: &str) -> Option<NAValue> { None } | |
708 | } | |
709 | ||
31cf33ac KS |
710 | |
711 | pub fn get_decoder() -> Box<dyn NADecoder + Send> { | |
712 | Box::new(VivoDecoder::new()) | |
713 | } | |
714 | ||
715 | #[cfg(test)] | |
716 | mod test { | |
717 | use nihav_core::codecs::RegisteredDecoders; | |
718 | use nihav_core::demuxers::RegisteredDemuxers; | |
719 | use nihav_codec_support::test::dec_video::*; | |
78fb6560 | 720 | use crate::vivo_register_all_decoders; |
31cf33ac | 721 | use crate::vivo_register_all_demuxers; |
886cde48 | 722 | // samples from https://samples.mplayerhq.hu/vivo/ |
31cf33ac KS |
723 | #[test] |
724 | fn test_vivo1() { | |
725 | let mut dmx_reg = RegisteredDemuxers::new(); | |
726 | vivo_register_all_demuxers(&mut dmx_reg); | |
727 | let mut dec_reg = RegisteredDecoders::new(); | |
78fb6560 | 728 | vivo_register_all_decoders(&mut dec_reg); |
31cf33ac | 729 | |
6fc1b073 KS |
730 | test_decoding("vivo", "vivo1", "assets/Misc/gr_al.viv", Some(16), |
731 | &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![ | |
732 | [0x59ea0837, 0xdf9b5ca3, 0x73cd45c3, 0xaaf2fbf1], | |
733 | [0x5d21c5a0, 0xfb335c4f, 0xe30945e9, 0xec65d54f], | |
734 | [0x46916c45, 0x406f1019, 0xba0105e0, 0xfccd0aab], | |
735 | [0x11006812, 0xb8e8c959, 0x5f69950b, 0x3578db5e], | |
736 | [0xd0b84c1e, 0xad327825, 0x4a98dbbe, 0x48edc280], | |
737 | [0xe1be71a4, 0xc7649922, 0x949496a6, 0xed697d09], | |
738 | [0x0ed9deea, 0x3e9b9784, 0x4836e38b, 0x363f8108], | |
739 | [0x358f2581, 0x95e6b290, 0x35ffde28, 0x003fa23e], | |
740 | [0x482385bd, 0x43bb7cee, 0xf1900758, 0x3b5f1255], | |
741 | [0x91d7549f, 0x96976ef4, 0xb2920f71, 0x71c7a9a6], | |
742 | [0x95736d4f, 0x56748245, 0x5733bd5d, 0x20eacf6d], | |
743 | [0xb5045185, 0x55dbe063, 0xb1c96f24, 0xe5d78296], | |
744 | [0xa88cadf4, 0xedc4a0aa, 0xbb359ed8, 0x1ea28916], | |
745 | [0x4d9f9491, 0x13d29319, 0x57aef355, 0x7e0c9e0b], | |
746 | [0xc3b52284, 0xabb74a4b, 0xf03c9503, 0x834d149e], | |
747 | [0x35fc95d0, 0x0f583ef0, 0xb176f055, 0xb587839a], | |
748 | [0xd5e3b443, 0x3b11b285, 0x78ec3098, 0xe2109aaa]])); | |
31cf33ac KS |
749 | } |
750 | #[test] | |
751 | fn test_vivo2() { | |
752 | let mut dmx_reg = RegisteredDemuxers::new(); | |
753 | vivo_register_all_demuxers(&mut dmx_reg); | |
754 | let mut dec_reg = RegisteredDecoders::new(); | |
78fb6560 | 755 | vivo_register_all_decoders(&mut dec_reg); |
31cf33ac | 756 | |
6fc1b073 KS |
757 | test_decoding("vivo", "vivo2", "assets/Misc/02-KimagureOrangeRoad.viv", Some(56), |
758 | &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![ | |
759 | [0x73f2afc2, 0x1cc43762, 0xbcfa886d, 0x7e607dd1], | |
760 | [0x45652ef5, 0x7c03046c, 0xcef60a62, 0x4a1e8f24], | |
761 | [0x09f6557e, 0x852cf108, 0x13dbb6ff, 0xaa8667e9], | |
762 | [0x3668d658, 0xfdc37a56, 0x267cb690, 0xa33924ee], | |
763 | [0x0ad13447, 0x26315ca3, 0xfbf43f79, 0xc3951941], | |
764 | [0x8c870462, 0x9135c664, 0x586accfa, 0xfe6d9113], | |
765 | [0x26b08477, 0xcdef5073, 0x86899b3c, 0x0d911f1d], | |
766 | [0x7db88b9c, 0x780729bb, 0xe3195f66, 0xb4f2e7e7], | |
767 | [0x9008ed4e, 0x29b05b6c, 0x3f8a2c23, 0x6d396334], | |
768 | [0xa255e91b, 0x324b764b, 0x265a08ea, 0x0a5a6ae7], | |
769 | [0xf2110e23, 0x8fa53178, 0x675c32cb, 0x63ba9297], | |
770 | [0xbe0bb384, 0xde08646e, 0x45d3530c, 0xd5cdf571], | |
771 | [0xfa19949a, 0xb9e12931, 0xcaa98f06, 0xa284718a], | |
772 | [0x9b1472e5, 0x2d18472c, 0xd5419a58, 0xff16d619], | |
773 | [0xeecd4532, 0xa6301dcc, 0x6eed2b15, 0xa456cae0], | |
774 | [0xb58b2bb8, 0x1c07bd9a, 0x1913e1c3, 0xa1f4fdc2], | |
775 | [0x51b6bef2, 0xd155d675, 0xaba07c15, 0xcf0a673e], | |
776 | [0x3c607ce6, 0x3a203449, 0xdda5b7a0, 0x9119b738], | |
777 | [0x7e7bae10, 0x4beb488e, 0x33db98bb, 0x8b49311e], | |
778 | [0xfff04316, 0xe29afa50, 0xa6f0c921, 0x5b0d29e2], | |
779 | [0xe640595a, 0x98bb55a4, 0x50c0a1e5, 0xe4970206], | |
780 | [0x63873ec7, 0x03a5fd08, 0xe3154f25, 0xb4ed64c2], | |
781 | [0xfa792148, 0x7efebf2c, 0x88d91825, 0x9dce368d], | |
782 | [0x5ed17368, 0x0e056059, 0x2c872282, 0xe5a98237], | |
783 | [0xd7ed6bd6, 0x01414003, 0x5187d790, 0x0af5b029], | |
784 | [0x2991a9c6, 0x8e2dda24, 0xfbf79bbb, 0xbcbfc672], | |
785 | [0xf2d23c64, 0xeb7e6094, 0x70a54901, 0x6d9d0204], | |
786 | [0x54997451, 0xb4edb3c0, 0x5a873e8f, 0xe89d7aa6], | |
787 | [0x94325a5f, 0xe0feb4b3, 0x2b1860bc, 0xc6b6a1a4], | |
788 | [0x8fd428de, 0x17f14f9f, 0x94c8d0da, 0xf9feda90], | |
789 | [0xed90d455, 0x22745327, 0x0ac511f1, 0x2dc2c29e], | |
790 | [0xc50ac6c1, 0xc9eaf8ec, 0x5d9dc7f3, 0x4e55fd09], | |
791 | [0x51325c40, 0xa7c01917, 0xdc064cc6, 0x143badc1], | |
792 | [0x3fc207ee, 0x2c4b0f91, 0x66555499, 0xdf6dc148], | |
793 | [0xd6e7fa33, 0x68ba6ce9, 0x45cac834, 0x1d51b094], | |
794 | [0x1ed541a8, 0x8dd0cc41, 0x149024d4, 0x13777a2e], | |
795 | [0x7fcbd065, 0xb20e068f, 0x9a5e3ed5, 0x6c35dded], | |
796 | [0x07ea66c1, 0xbfadea3b, 0x07bb6580, 0xf40d7d2f], | |
797 | [0x2e0fe75e, 0xa03d3011, 0x539c097d, 0x95fb265c], | |
798 | [0x2f177f09, 0x63b7172a, 0xb0fdd893, 0xcab516cf], | |
799 | [0x7cc5a956, 0xcd1ecff1, 0xf927038c, 0xa0610b48], | |
800 | [0xc0518934, 0xf5c38e4b, 0x7fa255cd, 0x9b054c5b], | |
801 | [0x0551de82, 0x08253d6b, 0x9144ced8, 0xc5adda55], | |
802 | [0xf45735b5, 0x3f2e2111, 0x54d1ff16, 0xe4866157], | |
803 | [0x0aad101e, 0xd37001f4, 0xd5734080, 0xfa4cf449], | |
804 | [0x65318bdd, 0x68809e67, 0x1e27946a, 0x2ca977e5], | |
805 | [0x119cf864, 0x8834473e, 0x721a253e, 0x4e417f19], | |
806 | [0x0d887145, 0xa42a05cc, 0x64a4388e, 0xa05de81d], | |
807 | [0xc184105a, 0x71ac7a8e, 0xaab0dba5, 0x078a331e], | |
808 | [0x078827ea, 0x27670e40, 0x92be9ebb, 0x0fc1f5a0], | |
809 | [0x8c82ca58, 0xbae0a643, 0x845afc5b, 0x15052a84], | |
810 | [0x92065c13, 0x69d3f6b7, 0x7270aab9, 0xb80921fd], | |
811 | [0xa58536b5, 0x6c46d7e1, 0x5f07f282, 0x350900e9], | |
812 | [0x13bf3f52, 0x71f8aa99, 0x97e0aaa5, 0xd6c1cff5], | |
813 | [0xa760cf95, 0xeb33c24c, 0x8dd35c6a, 0x4d81c8b5], | |
814 | [0x50f68b90, 0x41b1f1a0, 0x9847b3ec, 0x1cab5ccc], | |
815 | [0xbd71de9e, 0xc097b4ac, 0x003ab1e5, 0x9394b5bd]])); | |
31cf33ac KS |
816 | } |
817 | } |