]>
Commit | Line | Data |
---|---|---|
5641dccf 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_core::codecs::h263::*; | |
7 | use nihav_core::codecs::h263::code::H263BlockDSP; | |
8 | use nihav_core::codecs::h263::decoder::*; | |
9 | use nihav_core::codecs::h263::data::*; | |
10 | ||
be91b1c3 KS |
11 | |
12 | #[allow(dead_code)] | |
13 | struct Tables { | |
14 | intra_mcbpc_cb: Codebook<u8>, | |
15 | inter_mcbpc_cb: Codebook<u8>, | |
507f4b11 | 16 | mbtype_b_cb: Codebook<u8>, |
be91b1c3 | 17 | cbpy_cb: Codebook<u8>, |
507f4b11 | 18 | cbpc_b_cb: Codebook<u8>, |
be91b1c3 KS |
19 | rl_cb: Codebook<H263RLSym>, |
20 | aic_rl_cb: Codebook<H263RLSym>, | |
21 | mv_cb: Codebook<u8>, | |
22 | } | |
23 | ||
24 | #[derive(Clone,Copy)] | |
25 | struct RPRInfo { | |
26 | present: bool, | |
27 | bits: u8, | |
28 | widths: [usize; 8], | |
29 | heights: [usize; 8], | |
30 | } | |
31 | ||
32 | struct RealVideo20Decoder { | |
2422d969 | 33 | info: NACodecInfoRef, |
be91b1c3 KS |
34 | dec: H263BaseDecoder, |
35 | tables: Tables, | |
36 | w: usize, | |
37 | h: usize, | |
38 | minor_ver: u8, | |
39 | rpr: RPRInfo, | |
2a2aa421 | 40 | bdsp: H263BlockDSP, |
be91b1c3 KS |
41 | } |
42 | ||
43 | struct RealVideo20BR<'a> { | |
44 | br: BitReader<'a>, | |
45 | tables: &'a Tables, | |
46 | num_slices: usize, | |
47 | slice_no: usize, | |
48 | slice_off: Vec<u32>, | |
49 | w: usize, | |
50 | h: usize, | |
51 | mb_w: usize, | |
52 | mb_h: usize, | |
be91b1c3 | 53 | mb_pos_bits: u8, |
be91b1c3 KS |
54 | minor_ver: u8, |
55 | rpr: RPRInfo, | |
be91b1c3 KS |
56 | } |
57 | ||
58 | struct RV20SliceInfo { | |
59 | ftype: Type, | |
32007ad9 | 60 | seq: u32, |
be91b1c3 KS |
61 | qscale: u8, |
62 | mb_x: usize, | |
63 | mb_y: usize, | |
64 | mb_pos: usize, | |
65 | w: usize, | |
66 | h: usize, | |
67 | } | |
68 | ||
69 | impl RV20SliceInfo { | |
32007ad9 | 70 | fn new(ftype: Type, seq: u32, qscale: u8, mb_x: usize, mb_y: usize, mb_pos: usize, w: usize, h: usize) -> Self { |
e07387c7 | 71 | RV20SliceInfo { ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h } |
be91b1c3 KS |
72 | } |
73 | } | |
74 | ||
75 | impl<'a> RealVideo20BR<'a> { | |
76 | fn new(src: &'a [u8], tables: &'a Tables, width: usize, height: usize, minor_ver: u8, rpr: RPRInfo) -> Self { | |
77 | let nslices = (src[0] as usize) + 1; | |
78 | let mut slice_offs = Vec::with_capacity(nslices); | |
79 | { | |
80 | let offs = &src[1..][..nslices * 8]; | |
81 | let mut br = BitReader::new(offs, offs.len(), BitReaderMode::BE); | |
82 | for _ in 0..nslices { | |
83 | br.skip(32).unwrap(); | |
84 | let off = br.read(32).unwrap(); | |
85 | slice_offs.push(off); | |
86 | } | |
87 | } | |
88 | let soff = nslices * 8 + 1; | |
89 | let mb_w = (width + 15) >> 4; | |
90 | let mb_h = (height + 15) >> 4; | |
91 | let max_pos = mb_w * mb_h - 1; | |
92 | let mut mbpb = 0; | |
93 | for i in 0..H263_MBB.len() { | |
94 | if max_pos <= H263_MBB[i].blocks { | |
95 | mbpb = H263_MBB[i].bits; | |
96 | break; | |
97 | } | |
98 | } | |
99 | RealVideo20BR { | |
100 | br: BitReader::new(&src[soff..], src.len() - soff, BitReaderMode::BE), | |
e07387c7 | 101 | tables, |
be91b1c3 KS |
102 | num_slices: nslices, |
103 | slice_no: 0, | |
104 | slice_off: slice_offs, | |
105 | w: width, | |
106 | h: height, | |
e07387c7 KS |
107 | mb_w, |
108 | mb_h, | |
be91b1c3 | 109 | mb_pos_bits: mbpb, |
e07387c7 KS |
110 | minor_ver, |
111 | rpr, | |
be91b1c3 KS |
112 | } |
113 | } | |
114 | ||
2a2aa421 | 115 | #[allow(unused_variables)] |
0c3b4923 | 116 | fn decode_block(&mut self, sstate: &SliceState, quant: u8, intra: bool, coded: bool, blk: &mut [i16; 64], plane_no: usize, acpred: ACPredMode) -> DecoderResult<()> { |
9037cf6b | 117 | let br = &mut self.br; |
be91b1c3 | 118 | let mut idx = 0; |
2a2aa421 | 119 | if !sstate.is_iframe && intra { |
fc26b944 | 120 | let mut dc = br.read(8)? as i16; |
be91b1c3 KS |
121 | if dc == 255 { dc = 128; } |
122 | blk[0] = dc << 3; | |
123 | idx = 1; | |
124 | } | |
125 | if !coded { return Ok(()); } | |
0c3b4923 KS |
126 | let scan = match acpred { |
127 | ACPredMode::Hor => H263_SCAN_V, | |
128 | ACPredMode::Ver => H263_SCAN_H, | |
64e8b971 | 129 | _ => &ZIGZAG, |
0c3b4923 | 130 | }; |
be91b1c3 | 131 | |
2a2aa421 | 132 | let rl_cb = if sstate.is_iframe { &self.tables.aic_rl_cb } else { &self.tables.rl_cb }; |
e3ace094 | 133 | let q_add = if quant == 0 || sstate.is_iframe { 0i16 } else { ((quant - 1) | 1) as i16 }; |
43c8b55e | 134 | let q = if plane_no == 0 { (quant * 2) as i16 } else { H263_CHROMA_QUANT[quant as usize] as i16 }; |
be91b1c3 | 135 | while idx < 64 { |
fc26b944 | 136 | let code = br.read_cb(rl_cb)?; |
be91b1c3 KS |
137 | let run; |
138 | let mut level; | |
139 | let last; | |
140 | if !code.is_escape() { | |
141 | run = code.get_run(); | |
142 | level = code.get_level(); | |
143 | last = code.is_last(); | |
fc26b944 | 144 | if br.read_bool()? { level = -level; } |
32007ad9 | 145 | level = (level * q) + q_add; |
be91b1c3 | 146 | } else { |
fc26b944 KS |
147 | last = br.read_bool()?; |
148 | run = br.read(6)? as u8; | |
149 | level = br.read_s(8)? as i16; | |
be91b1c3 | 150 | if level == -128 { |
fc26b944 KS |
151 | let low = br.read(5)? as i16; |
152 | let top = br.read_s(6)? as i16; | |
be91b1c3 KS |
153 | level = (top << 5) | low; |
154 | } | |
32007ad9 | 155 | level = (level * q) + q_add; |
be91b1c3 KS |
156 | if level < -2048 { level = -2048; } |
157 | if level > 2047 { level = 2047; } | |
158 | } | |
159 | idx += run; | |
160 | validate!(idx < 64); | |
0c3b4923 | 161 | let oidx = scan[idx as usize]; |
be91b1c3 KS |
162 | blk[oidx] = level; |
163 | idx += 1; | |
164 | if last { break; } | |
165 | } | |
166 | Ok(()) | |
167 | } | |
168 | } | |
169 | ||
170 | fn decode_mv_component(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<i16> { | |
fc26b944 | 171 | let code = br.read_cb(mv_cb)? as i16; |
be91b1c3 | 172 | if code == 0 { return Ok(0) } |
fc26b944 | 173 | if !br.read_bool()? { |
be91b1c3 KS |
174 | Ok(code) |
175 | } else { | |
176 | Ok(-code) | |
177 | } | |
178 | } | |
179 | ||
180 | fn decode_mv(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<MV> { | |
fc26b944 KS |
181 | let xval = decode_mv_component(br, mv_cb)?; |
182 | let yval = decode_mv_component(br, mv_cb)?; | |
be91b1c3 KS |
183 | Ok(MV::new(xval, yval)) |
184 | } | |
185 | ||
43c8b55e KS |
186 | fn read_dquant(br: &mut BitReader, q: u8) -> DecoderResult<u8> { |
187 | if br.read_bool()? { | |
188 | Ok(H263_MODIFIED_QUANT[br.read(1)? as usize][q as usize]) | |
189 | } else { | |
190 | Ok(br.read(5)? as u8) | |
191 | } | |
192 | } | |
193 | ||
be91b1c3 KS |
194 | impl<'a> BlockDecoder for RealVideo20BR<'a> { |
195 | ||
196 | #[allow(unused_variables)] | |
197 | fn decode_pichdr(&mut self) -> DecoderResult<PicInfo> { | |
198 | self.slice_no = 0; | |
fc26b944 | 199 | let shdr = self.read_slice_header()?; |
2a2aa421 | 200 | // self.slice_no += 1; |
be91b1c3 | 201 | validate!((shdr.mb_x == 0) && (shdr.mb_y == 0)); |
2a2aa421 | 202 | /* let mb_count; |
be91b1c3 KS |
203 | if self.slice_no < self.num_slices { |
204 | let pos = self.br.tell(); | |
fc26b944 | 205 | let shdr2 = self.read_slice_header()?; |
be91b1c3 KS |
206 | self.br.seek(pos as u32)?; |
207 | mb_count = shdr2.mb_pos - shdr.mb_pos; | |
208 | } else { | |
209 | mb_count = self.mb_w * self.mb_h; | |
2a2aa421 | 210 | }*/ |
be91b1c3 | 211 | |
0c3b4923 | 212 | let plusinfo = Some(PlusInfo::new(shdr.ftype == Type::I, false, false, false)); |
32007ad9 | 213 | let picinfo = PicInfo::new(shdr.w, shdr.h, shdr.ftype, MVMode::Long, false, false, shdr.qscale, shdr.seq as u16, None, plusinfo); |
be91b1c3 KS |
214 | Ok(picinfo) |
215 | } | |
216 | ||
217 | #[allow(unused_variables)] | |
2a2aa421 | 218 | fn decode_slice_header(&mut self, info: &PicInfo) -> DecoderResult<SliceInfo> { |
fc26b944 | 219 | let shdr = self.read_slice_header()?; |
be91b1c3 KS |
220 | self.slice_no += 1; |
221 | let mb_count; | |
222 | if self.slice_no < self.num_slices { | |
0c3b4923 | 223 | let pos = self.br.tell(); |
fc26b944 | 224 | let shdr2 = self.read_slice_header()?; |
be91b1c3 | 225 | mb_count = shdr2.mb_pos - shdr.mb_pos; |
0c3b4923 | 226 | self.br.seek(pos as u32)?; |
be91b1c3 KS |
227 | } else { |
228 | mb_count = self.mb_w * self.mb_h - shdr.mb_pos; | |
229 | } | |
2a2aa421 | 230 | let ret = SliceInfo::new(shdr.mb_x, shdr.mb_y, shdr.mb_pos + mb_count, shdr.qscale); |
be91b1c3 KS |
231 | |
232 | Ok(ret) | |
233 | } | |
234 | ||
43c8b55e | 235 | fn decode_block_header(&mut self, info: &PicInfo, _slice: &SliceInfo, sstate: &SliceState) -> DecoderResult<BlockInfo> { |
9037cf6b | 236 | let br = &mut self.br; |
43c8b55e | 237 | let mut q = sstate.quant; |
be91b1c3 KS |
238 | match info.get_mode() { |
239 | Type::I => { | |
fc26b944 KS |
240 | let mut cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; |
241 | while cbpc == 8 { cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; } | |
0c3b4923 KS |
242 | let mut acpred = ACPredMode::None; |
243 | if let Some(ref pi) = info.plusinfo { | |
244 | if pi.aic { | |
245 | let acpp = br.read_bool()?; | |
246 | acpred = ACPredMode::DC; | |
0c3b4923 KS |
247 | if acpp { |
248 | acpred = if br.read_bool()? { ACPredMode::Hor } else { ACPredMode::Ver }; | |
249 | } | |
250 | } | |
0c3b4923 | 251 | } |
fc26b944 | 252 | let cbpy = br.read_cb(&self.tables.cbpy_cb)?; |
be91b1c3 KS |
253 | let cbp = (cbpy << 2) | (cbpc & 3); |
254 | let dquant = (cbpc & 4) != 0; | |
255 | if dquant { | |
43c8b55e | 256 | q = read_dquant(br, q)?; |
be91b1c3 | 257 | } |
0c3b4923 KS |
258 | let mut binfo = BlockInfo::new(Type::I, cbp, q); |
259 | binfo.set_acpred(acpred); | |
260 | Ok(binfo) | |
be91b1c3 KS |
261 | }, |
262 | Type::P => { | |
fc26b944 | 263 | if br.read_bool()? { |
be91b1c3 KS |
264 | return Ok(BlockInfo::new(Type::Skip, 0, info.get_quant())); |
265 | } | |
fc26b944 KS |
266 | let mut cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; |
267 | while cbpc == 20 { cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; } | |
be91b1c3 KS |
268 | let is_intra = (cbpc & 0x04) != 0; |
269 | let dquant = (cbpc & 0x08) != 0; | |
270 | let is_4x4 = (cbpc & 0x10) != 0; | |
271 | if is_intra { | |
fc26b944 | 272 | let cbpy = br.read_cb(&self.tables.cbpy_cb)?; |
be91b1c3 KS |
273 | let cbp = (cbpy << 2) | (cbpc & 3); |
274 | if dquant { | |
43c8b55e | 275 | q = read_dquant(br, q)?; |
be91b1c3 KS |
276 | } |
277 | let binfo = BlockInfo::new(Type::I, cbp, q); | |
be91b1c3 KS |
278 | return Ok(binfo); |
279 | } | |
280 | ||
fc26b944 | 281 | let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?; |
be91b1c3 KS |
282 | // if /* !aiv && */(cbpc & 3) != 3 { |
283 | cbpy ^= 0xF; | |
284 | // } | |
285 | let cbp = (cbpy << 2) | (cbpc & 3); | |
286 | if dquant { | |
43c8b55e | 287 | q = read_dquant(br, q)?; |
be91b1c3 | 288 | } |
be91b1c3 KS |
289 | let mut binfo = BlockInfo::new(Type::P, cbp, q); |
290 | if !is_4x4 { | |
fc26b944 | 291 | let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?]; |
be91b1c3 KS |
292 | binfo.set_mv(&mvec); |
293 | } else { | |
294 | let mvec: [MV; 4] = [ | |
fc26b944 KS |
295 | decode_mv(br, &self.tables.mv_cb)?, |
296 | decode_mv(br, &self.tables.mv_cb)?, | |
297 | decode_mv(br, &self.tables.mv_cb)?, | |
298 | decode_mv(br, &self.tables.mv_cb)? | |
be91b1c3 KS |
299 | ]; |
300 | binfo.set_mv(&mvec); | |
301 | } | |
be91b1c3 KS |
302 | Ok(binfo) |
303 | }, | |
32007ad9 | 304 | Type::B => { // B |
507f4b11 KS |
305 | let mut mbtype = br.read_cb(&self.tables.mbtype_b_cb)? as usize; |
306 | while mbtype == 14 { mbtype = br.read_cb(&self.tables.mbtype_b_cb)? as usize; } | |
307 | ||
308 | let is_coded = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_CODED) != 0; | |
309 | let is_intra = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_INTRA) != 0; | |
310 | let dquant = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_DQUANT) != 0; | |
311 | let is_fwd = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_FORWARD) != 0; | |
312 | let is_bwd = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_BACKWARD) != 0; | |
313 | ||
314 | let cbp = if is_coded { | |
315 | let cbpc = br.read_cb(&self.tables.cbpc_b_cb)?; | |
316 | let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
317 | if !is_intra { cbpy ^= 0xF; } | |
318 | (cbpy << 2) | (cbpc & 3) | |
319 | } else { 0 }; | |
1a151e53 | 320 | |
507f4b11 | 321 | if dquant { |
43c8b55e | 322 | q = read_dquant(br, q)?; |
507f4b11 | 323 | } |
507f4b11 KS |
324 | |
325 | if is_intra { | |
326 | let binfo = BlockInfo::new(Type::I, cbp, q); | |
327 | return Ok(binfo); | |
328 | } | |
329 | ||
330 | let mut binfo = BlockInfo::new(Type::B, cbp, q); | |
331 | if is_fwd { | |
332 | let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?]; | |
333 | binfo.set_mv(&mvec); | |
334 | } | |
335 | if is_bwd { | |
336 | let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?]; | |
337 | binfo.set_b_mv(&mvec); | |
338 | } | |
339 | Ok(binfo) | |
340 | }, | |
32007ad9 | 341 | _ => { unreachable!(); }, |
be91b1c3 KS |
342 | } |
343 | } | |
344 | ||
2a2aa421 | 345 | fn decode_block_intra(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { |
0c3b4923 | 346 | self.decode_block(sstate, quant, true, coded, blk, if no < 4 { 0 } else { no - 3 }, info.get_acpred()) |
be91b1c3 KS |
347 | } |
348 | ||
349 | #[allow(unused_variables)] | |
2a2aa421 | 350 | fn decode_block_inter(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { |
0c3b4923 | 351 | self.decode_block(sstate, quant, false, coded, blk, if no < 4 { 0 } else { no - 3 }, ACPredMode::None) |
be91b1c3 KS |
352 | } |
353 | ||
2a2aa421 | 354 | fn is_slice_end(&mut self) -> bool { false } |
be91b1c3 KS |
355 | } |
356 | ||
357 | impl<'a> RealVideo20BR<'a> { | |
2a2aa421 | 358 | #[allow(unused_variables)] |
be91b1c3 KS |
359 | fn read_slice_header(&mut self) -> DecoderResult<RV20SliceInfo> { |
360 | validate!(self.slice_no < self.num_slices); | |
361 | ||
9037cf6b | 362 | let br = &mut self.br; |
fc26b944 | 363 | br.seek(self.slice_off[self.slice_no] * 8)?; |
be91b1c3 | 364 | |
fc26b944 | 365 | let frm_type = br.read(2)?; |
be91b1c3 KS |
366 | let ftype = match frm_type { |
367 | 0 | 1 => { Type::I }, | |
368 | 2 => { Type::P }, | |
507f4b11 | 369 | _ => { Type::B }, |
be91b1c3 KS |
370 | }; |
371 | ||
fc26b944 | 372 | let marker = br.read(1)?; |
be91b1c3 | 373 | validate!(marker == 0); |
fc26b944 | 374 | let qscale = br.read(5)? as u8; |
be91b1c3 KS |
375 | validate!(qscale > 0); |
376 | if self.minor_ver >= 2 { | |
fc26b944 | 377 | br.skip(1)?; // loop filter |
be91b1c3 KS |
378 | } |
379 | let seq = if self.minor_ver <= 1 { | |
32007ad9 | 380 | br.read(8)? << 8 |
be91b1c3 | 381 | } else { |
32007ad9 | 382 | br.read(13)? << 3 |
be91b1c3 KS |
383 | }; |
384 | let w; | |
385 | let h; | |
386 | if self.rpr.present { | |
fc26b944 | 387 | let rpr = br.read(self.rpr.bits)? as usize; |
be91b1c3 KS |
388 | if rpr == 0 { |
389 | w = self.w; | |
390 | h = self.h; | |
391 | } else { | |
7c37f176 KS |
392 | w = self.rpr.widths[rpr - 1]; |
393 | h = self.rpr.heights[rpr - 1]; | |
be91b1c3 KS |
394 | validate!((w != 0) && (h != 0)); |
395 | } | |
396 | } else { | |
397 | w = self.w; | |
398 | h = self.h; | |
399 | } | |
400 | ||
fc26b944 | 401 | let mb_pos = br.read(self.mb_pos_bits)? as usize; |
be91b1c3 KS |
402 | let mb_x = mb_pos % self.mb_w; |
403 | let mb_y = mb_pos / self.mb_w; | |
404 | ||
fc26b944 | 405 | br.skip(1)?; // no rounding |
be91b1c3 KS |
406 | |
407 | if (self.minor_ver <= 1) && (frm_type == 3) { | |
fc26b944 | 408 | br.skip(5)?; |
be91b1c3 | 409 | } |
be91b1c3 | 410 | |
32007ad9 | 411 | Ok(RV20SliceInfo::new(ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h)) |
be91b1c3 KS |
412 | } |
413 | } | |
414 | ||
be91b1c3 KS |
415 | impl RealVideo20Decoder { |
416 | fn new() -> Self { | |
417 | let mut coderead = H263ShortCodeReader::new(H263_INTRA_MCBPC); | |
418 | let intra_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
419 | let mut coderead = H263ShortCodeReader::new(H263_INTER_MCBPC); | |
420 | let inter_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
507f4b11 KS |
421 | let mut coderead = H263ShortCodeReader::new(H263_MBTYPE_B); |
422 | let mbtype_b_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
be91b1c3 KS |
423 | let mut coderead = H263ShortCodeReader::new(H263_CBPY); |
424 | let cbpy_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
507f4b11 KS |
425 | let mut coderead = H263ShortCodeReader::new(H263_CBPC_B); |
426 | let cbpc_b_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
be91b1c3 KS |
427 | let mut coderead = H263RLCodeReader::new(H263_RL_CODES); |
428 | let rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
429 | let mut coderead = H263RLCodeReader::new(H263_RL_CODES_AIC); | |
430 | let aic_rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
431 | let mut coderead = H263ShortCodeReader::new(H263_MV); | |
432 | let mv_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
1a151e53 | 433 | |
be91b1c3 | 434 | let tables = Tables { |
e07387c7 KS |
435 | intra_mcbpc_cb, |
436 | inter_mcbpc_cb, | |
437 | mbtype_b_cb, | |
438 | cbpy_cb, | |
439 | cbpc_b_cb, | |
440 | rl_cb, | |
441 | aic_rl_cb, | |
442 | mv_cb, | |
be91b1c3 KS |
443 | }; |
444 | ||
445 | RealVideo20Decoder{ | |
2422d969 | 446 | info: NACodecInfoRef::default(), |
32007ad9 | 447 | dec: H263BaseDecoder::new_b_frames(false), |
e07387c7 | 448 | tables, |
be91b1c3 KS |
449 | w: 0, |
450 | h: 0, | |
451 | minor_ver: 0, | |
452 | rpr: RPRInfo { present: false, bits: 0, widths: [0; 8], heights: [0; 8] }, | |
2a2aa421 | 453 | bdsp: H263BlockDSP::new(), |
be91b1c3 KS |
454 | } |
455 | } | |
456 | } | |
457 | ||
458 | impl NADecoder for RealVideo20Decoder { | |
2a2aa421 | 459 | #[allow(unused_variables)] |
01613464 | 460 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { |
be91b1c3 KS |
461 | if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { |
462 | let w = vinfo.get_width(); | |
463 | let h = vinfo.get_height(); | |
464 | let fmt = formats::YUV420_FORMAT; | |
465 | let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt)); | |
2422d969 | 466 | self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref(); |
be91b1c3 KS |
467 | self.w = w; |
468 | self.h = h; | |
469 | ||
470 | let edata = info.get_extradata().unwrap(); | |
471 | let src: &[u8] = &edata; | |
472 | let ver = ((src[4] as u32) << 12) | ((src[5] as u32) << 4) | ((src[6] as u32) >> 4); | |
473 | let maj_ver = ver >> 16; | |
474 | let min_ver = (ver >> 8) & 0xFF; | |
475 | let mic_ver = ver & 0xFF; | |
be91b1c3 KS |
476 | validate!(maj_ver == 2); |
477 | self.minor_ver = min_ver as u8; | |
478 | let rprb = src[1] & 7; | |
479 | if rprb == 0 { | |
480 | self.rpr.present = false; | |
481 | } else { | |
482 | self.rpr.present = true; | |
7c37f176 | 483 | self.rpr.bits = ((rprb >> 1) + 1) as u8; |
be91b1c3 KS |
484 | for i in 4..(src.len()/2) { |
485 | self.rpr.widths [i - 4] = (src[i * 2] as usize) * 4; | |
486 | self.rpr.heights[i - 4] = (src[i * 2 + 1] as usize) * 4; | |
487 | } | |
488 | } | |
489 | Ok(()) | |
490 | } else { | |
be91b1c3 KS |
491 | Err(DecoderError::InvalidData) |
492 | } | |
493 | } | |
01613464 | 494 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { |
be91b1c3 KS |
495 | let src = pkt.get_buffer(); |
496 | ||
be91b1c3 KS |
497 | let mut ibr = RealVideo20BR::new(&src, &self.tables, self.w, self.h, self.minor_ver, self.rpr); |
498 | ||
fc26b944 | 499 | let bufinfo = self.dec.parse_frame(&mut ibr, &self.bdsp)?; |
be91b1c3 KS |
500 | |
501 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); | |
502 | frm.set_keyframe(self.dec.is_intra()); | |
507f4b11 | 503 | frm.set_frame_type(self.dec.get_frame_type()); |
171860fc | 504 | Ok(frm.into_ref()) |
be91b1c3 | 505 | } |
f9be4e75 KS |
506 | fn flush(&mut self) { |
507 | self.dec.flush(); | |
508 | } | |
be91b1c3 KS |
509 | } |
510 | ||
511 | struct MBB { blocks: usize, bits: u8 } | |
512 | const H263_MBB: &[MBB; 7] = &[ | |
513 | MBB{ blocks: 47, bits: 6 }, | |
514 | MBB{ blocks: 98, bits: 7 }, | |
515 | MBB{ blocks: 395, bits: 9 }, | |
516 | MBB{ blocks: 1583, bits: 11 }, | |
517 | MBB{ blocks: 6335, bits: 13 }, | |
518 | MBB{ blocks: 9215, bits: 14 }, | |
519 | MBB{ blocks: 65536, bits: 14 }, | |
520 | ]; | |
521 | ||
6011e201 | 522 | pub fn get_decoder() -> Box<dyn NADecoder> { |
be91b1c3 KS |
523 | Box::new(RealVideo20Decoder::new()) |
524 | } | |
525 | ||
526 | #[cfg(test)] | |
527 | mod test { | |
3167c45c KS |
528 | use nihav_core::codecs::RegisteredDecoders; |
529 | use nihav_core::demuxers::RegisteredDemuxers; | |
530 | use nihav_core::test::dec_video::*; | |
531 | use crate::codecs::realmedia_register_all_codecs; | |
532 | use crate::demuxers::realmedia_register_all_demuxers; | |
be91b1c3 KS |
533 | #[test] |
534 | fn test_rv20() { | |
3167c45c KS |
535 | let mut dmx_reg = RegisteredDemuxers::new(); |
536 | realmedia_register_all_demuxers(&mut dmx_reg); | |
537 | let mut dec_reg = RegisteredDecoders::new(); | |
538 | realmedia_register_all_codecs(&mut dec_reg); | |
539 | ||
540 | test_file_decoding("realmedia", "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm", /*None*/Some(3000), true, false, None/*Some("rv20")*/, &dmx_reg, &dec_reg); | |
7c37f176 | 541 | // test_file_decoding("realmedia", "assets/RV/rv20_cook_640x352_realproducer_plus_8.51.rm", /*None*/Some(1000), true, false, Some("rv20")); |
be91b1c3 KS |
542 | } |
543 | } |