]>
Commit | Line | Data |
---|---|---|
92d9fb69 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}; | |
7 | use nihav_codec_support::codecs::h263::*; | |
8 | use nihav_codec_support::codecs::h263::code::*; | |
9 | use nihav_codec_support::codecs::h263::decoder::*; | |
10 | use nihav_codec_support::codecs::h263::data::*; | |
11 | ||
12 | #[allow(dead_code)] | |
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 | ||
22 | struct SparkDecoder { | |
23 | info: NACodecInfoRef, | |
24 | dec: H263BaseDecoder, | |
25 | tables: Tables, | |
26 | w: usize, | |
27 | h: usize, | |
28 | bdsp: H263BlockDSP, | |
29 | } | |
30 | ||
31 | struct SparkBR<'a> { | |
32 | br: BitReader<'a>, | |
33 | tables: &'a Tables, | |
34 | ver1: bool, | |
35 | mb_w: usize, | |
36 | mb_h: usize, | |
37 | } | |
38 | ||
39 | fn decode_mv_component(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<i16> { | |
40 | let code = i16::from(br.read_cb(mv_cb)?); | |
41 | if code == 0 { return Ok(0) } | |
42 | if !br.read_bool()? { | |
43 | Ok(code) | |
44 | } else { | |
45 | Ok(-code) | |
46 | } | |
47 | } | |
48 | ||
49 | fn decode_mv(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<MV> { | |
50 | let xval = decode_mv_component(br, mv_cb)?; | |
51 | let yval = decode_mv_component(br, mv_cb)?; | |
52 | Ok(MV::new(xval, yval)) | |
53 | } | |
54 | ||
55 | impl<'a> SparkBR<'a> { | |
56 | fn new(src: &'a [u8], tables: &'a Tables, w: usize, h: usize) -> Self { | |
57 | Self { | |
58 | br: BitReader::new(src, BitReaderMode::BE), | |
59 | tables, | |
60 | ver1: false, | |
61 | mb_w: (w + 15) >> 4, | |
62 | mb_h: (h + 15) >> 4, | |
63 | } | |
64 | } | |
65 | ||
66 | fn decode_block(&mut self, quant: u8, intra: bool, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { | |
67 | let br = &mut self.br; | |
68 | let mut idx = 0; | |
69 | if intra { | |
70 | let mut dc = br.read(8)?; | |
71 | if dc == 255 { dc = 128; } | |
72 | blk[0] = (dc as i16) << 3; | |
73 | idx = 1; | |
74 | } | |
75 | if !coded { return Ok(()); } | |
76 | ||
77 | let rl_cb = &self.tables.rl_cb; // could be aic too | |
78 | let q_add = if quant == 0 { 0i16 } else { i16::from((quant - 1) | 1) }; | |
79 | let q = i16::from(quant * 2); | |
80 | while idx < 64 { | |
81 | let code = br.read_cb(rl_cb)?; | |
82 | let run; | |
83 | let mut level; | |
84 | let last; | |
85 | if !code.is_escape() { | |
86 | run = code.get_run(); | |
87 | level = code.get_level(); | |
88 | last = code.is_last(); | |
89 | if br.read_bool()? { level = -level; } | |
90 | if level > 0 { | |
91 | level = (level * q) + q_add; | |
92 | } else { | |
93 | level = (level * q) - q_add; | |
94 | } | |
95 | } else if !self.ver1 { | |
96 | last = br.read_bool()?; | |
97 | run = br.read(6)? as u8; | |
98 | level = br.read_s(8)? as i16; | |
99 | if level == -128 { | |
100 | let low = br.read(5)? as i16; | |
101 | let top = br.read_s(6)? as i16; | |
102 | level = (top << 5) | low; | |
103 | } | |
104 | if level > 0 { | |
105 | level = (level * q) + q_add; | |
106 | } else { | |
107 | level = (level * q) - q_add; | |
108 | } | |
109 | if level < -2048 { level = -2048; } | |
110 | if level > 2047 { level = 2047; } | |
111 | } else { | |
112 | let fmt_bit = br.read_bool()?; | |
113 | last = br.read_bool()?; | |
114 | run = br.read(6)? as u8; | |
115 | level = br.read_s(if !fmt_bit { 7 } else { 11 })? as i16; | |
116 | validate!(level != 0); | |
117 | if !fmt_bit { | |
118 | validate!(level != 64); | |
119 | } else { | |
120 | validate!(level != 1024); | |
121 | } | |
122 | if level > 0 { | |
123 | level = (level * q) + q_add; | |
124 | } else { | |
125 | level = (level * q) - q_add; | |
126 | } | |
127 | if level < -2048 { level = -2048; } | |
128 | if level > 2047 { level = 2047; } | |
129 | } | |
130 | idx += run; | |
131 | validate!(idx < 64); | |
132 | let oidx = ZIGZAG[idx as usize]; | |
133 | blk[oidx] = level; | |
134 | idx += 1; | |
135 | if last { break; } | |
136 | } | |
137 | Ok(()) | |
138 | } | |
139 | } | |
140 | ||
141 | ||
142 | impl<'a> BlockDecoder for SparkBR<'a> { | |
143 | fn decode_pichdr(&mut self) -> DecoderResult<PicInfo> { | |
144 | let br = &mut self.br; | |
145 | let syncw = br.read(17)?; | |
146 | validate!(syncw == 1); | |
147 | let version = br.read(5)?; | |
148 | validate!(version == 0 || version == 1); | |
149 | self.ver1 = version == 1; | |
150 | let tr = (br.read(8)? << 8) as u16; | |
151 | let sfmt = br.read(3)?; | |
152 | validate!(sfmt != 0b111); | |
153 | let (w, h) = match sfmt { | |
154 | 0 => { | |
155 | let w = br.read(8)? as usize; | |
156 | let h = br.read(8)? as usize; | |
157 | validate!(w != 0 && h != 0); | |
158 | (w, h) | |
159 | }, | |
160 | 1 => { | |
161 | let w = br.read(16)? as usize; | |
162 | let h = br.read(16)? as usize; | |
163 | validate!(w != 0 && h != 0); | |
164 | (w, h) | |
165 | }, | |
166 | 2 => (352, 288), | |
167 | 3 => (176, 144), | |
168 | 4 => (128, 96), | |
169 | 5 => (320, 240), | |
170 | 6 => (160, 120), | |
171 | _ => unreachable!(), | |
172 | }; | |
173 | let pic_type = br.read(2)?; | |
174 | let ftype = match pic_type { | |
175 | 0 => Type::I, | |
176 | 1 => Type::P, | |
177 | 2 => Type::Special, | |
178 | _ => return Err(DecoderError::InvalidData), | |
179 | }; | |
180 | let deblock = br.read_bool()?; | |
181 | let quant = br.read(5)?; | |
182 | while br.read_bool()? { // skip PEI | |
183 | br.read(8)?; | |
184 | } | |
185 | self.mb_w = (w + 15) >> 4; | |
186 | self.mb_h = (h + 15) >> 4; | |
187 | ||
188 | let plusinfo = if deblock { Some(PlusInfo::new(false, deblock, false, false)) } else { None }; | |
189 | let picinfo = PicInfo::new(w, h, ftype, MVMode::Long, true, false, quant as u8, tr, None, plusinfo); | |
190 | Ok(picinfo) | |
191 | } | |
192 | fn decode_slice_header(&mut self, info: &PicInfo) -> DecoderResult<SliceInfo> { | |
193 | Ok(SliceInfo::new(0, 0, self.mb_w * self.mb_h, info.quant)) | |
194 | } | |
195 | fn decode_block_header(&mut self, info: &PicInfo, slice: &SliceInfo, _sstate: &SliceState) -> DecoderResult<BlockInfo> { | |
196 | let br = &mut self.br; | |
197 | let mut q = slice.get_quant(); | |
198 | match info.get_mode() { | |
199 | Type::I => { | |
200 | let mut cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; | |
201 | while cbpc == 8 { cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; } | |
202 | let cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
203 | let cbp = (cbpy << 2) | (cbpc & 3); | |
204 | let dquant = (cbpc & 4) != 0; | |
205 | if dquant { | |
206 | let idx = br.read(2)? as usize; | |
207 | q = (i16::from(q) + i16::from(H263_DQUANT_TAB[idx])) as u8; | |
208 | } | |
209 | Ok(BlockInfo::new(Type::I, cbp, q)) | |
210 | }, | |
211 | Type::P | Type::Special => { | |
212 | if br.read_bool()? { | |
213 | return Ok(BlockInfo::new(Type::Skip, 0, info.get_quant())); | |
214 | } | |
215 | let mut cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; | |
216 | while cbpc == 20 { cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; } | |
217 | let is_intra = (cbpc & 0x04) != 0; | |
218 | let dquant = (cbpc & 0x08) != 0; | |
219 | let is_4x4 = (cbpc & 0x10) != 0; | |
220 | if is_intra { | |
221 | let cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
222 | let cbp = (cbpy << 2) | (cbpc & 3); | |
223 | if dquant { | |
224 | let idx = br.read(2)? as usize; | |
225 | q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; | |
226 | validate!(q < 32); | |
227 | } | |
228 | let binfo = BlockInfo::new(Type::I, cbp, q); | |
229 | return Ok(binfo); | |
230 | } | |
231 | ||
232 | let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
233 | // if /* !aiv && */(cbpc & 3) != 3 { | |
234 | cbpy ^= 0xF; | |
235 | // } | |
236 | let cbp = (cbpy << 2) | (cbpc & 3); | |
237 | if dquant { | |
238 | let idx = br.read(2)? as usize; | |
239 | q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; | |
240 | validate!(q < 32); | |
241 | } | |
242 | let mut binfo = BlockInfo::new(Type::P, cbp, q); | |
243 | if !is_4x4 { | |
244 | let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?]; | |
245 | binfo.set_mv(&mvec); | |
246 | } else { | |
247 | let mvec: [MV; 4] = [ | |
248 | decode_mv(br, &self.tables.mv_cb)?, | |
249 | decode_mv(br, &self.tables.mv_cb)?, | |
250 | decode_mv(br, &self.tables.mv_cb)?, | |
251 | decode_mv(br, &self.tables.mv_cb)? | |
252 | ]; | |
253 | binfo.set_mv(&mvec); | |
254 | } | |
255 | Ok(binfo) | |
256 | }, | |
257 | _ => { Err(DecoderError::InvalidData) }, | |
258 | } | |
259 | } | |
260 | fn decode_block_intra(&mut self, _info: &BlockInfo, _sstate: &SliceState, quant: u8, _no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { | |
261 | self.decode_block(quant, true, coded, blk) | |
262 | } | |
263 | fn decode_block_inter(&mut self, _info: &BlockInfo, _sstate: &SliceState, quant: u8, _no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { | |
264 | self.decode_block(quant, false, coded, blk) | |
265 | } | |
266 | fn is_slice_end(&mut self) -> bool { self.br.peek(16) == 0 } | |
267 | } | |
268 | ||
269 | impl SparkDecoder { | |
270 | fn new() -> Self { | |
271 | let mut coderead = H263ShortCodeReader::new(H263_INTRA_MCBPC); | |
272 | let intra_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
273 | let mut coderead = H263ShortCodeReader::new(H263_INTER_MCBPC); | |
274 | let inter_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
275 | let mut coderead = H263ShortCodeReader::new(H263_CBPY); | |
276 | let cbpy_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
277 | let mut coderead = H263RLCodeReader::new(H263_RL_CODES); | |
278 | let rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
279 | let mut coderead = H263RLCodeReader::new(H263_RL_CODES_AIC); | |
280 | let aic_rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
281 | let mut coderead = H263ShortCodeReader::new(H263_MV); | |
282 | let mv_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
283 | let tables = Tables { | |
284 | intra_mcbpc_cb, | |
285 | inter_mcbpc_cb, | |
286 | cbpy_cb, | |
287 | rl_cb, | |
288 | aic_rl_cb, | |
289 | mv_cb, | |
290 | }; | |
291 | ||
292 | Self { | |
293 | info: NACodecInfo::new_dummy(), | |
294 | dec: H263BaseDecoder::new(true), | |
295 | tables, | |
296 | bdsp: H263BlockDSP::new(), | |
297 | w: 0, | |
298 | h: 0, | |
299 | } | |
300 | } | |
301 | } | |
302 | ||
303 | impl NADecoder for SparkDecoder { | |
304 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { | |
305 | if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { | |
306 | let w = vinfo.get_width(); | |
307 | let h = vinfo.get_height(); | |
308 | let fmt = formats::YUV420_FORMAT; | |
309 | let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt)); | |
310 | self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref(); | |
311 | self.w = w; | |
312 | self.h = h; | |
313 | ||
314 | Ok(()) | |
315 | } else { | |
316 | Err(DecoderError::InvalidData) | |
317 | } | |
318 | } | |
319 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { | |
320 | let src = pkt.get_buffer(); | |
321 | ||
322 | let mut ibr = SparkBR::new(&src, &self.tables, self.w, self.h); | |
323 | let bufinfo = self.dec.parse_frame(&mut ibr, &self.bdsp)?; | |
324 | ||
325 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); | |
326 | frm.set_keyframe(self.dec.is_intra()); | |
327 | frm.set_frame_type(if self.dec.is_intra() { FrameType::I } else { FrameType::P }); | |
328 | Ok(frm.into_ref()) | |
329 | } | |
330 | fn flush(&mut self) { | |
331 | self.dec.flush(); | |
332 | } | |
333 | } | |
334 | ||
335 | impl NAOptionHandler for SparkDecoder { | |
336 | fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } | |
337 | fn set_options(&mut self, _options: &[NAOption]) { } | |
338 | fn query_option_value(&self, _name: &str) -> Option<NAValue> { None } | |
339 | } | |
340 | ||
341 | pub fn get_decoder() -> Box<dyn NADecoder + Send> { | |
342 | Box::new(SparkDecoder::new()) | |
343 | } | |
344 | ||
345 | #[cfg(test)] | |
346 | mod test { | |
347 | use nihav_core::codecs::RegisteredDecoders; | |
348 | use nihav_core::demuxers::RegisteredDemuxers; | |
349 | use nihav_codec_support::test::dec_video::*; | |
350 | use crate::flash_register_all_decoders; | |
351 | use crate::flash_register_all_demuxers; | |
352 | #[test] | |
353 | fn test_flv263() { | |
354 | let mut dmx_reg = RegisteredDemuxers::new(); | |
355 | flash_register_all_demuxers(&mut dmx_reg); | |
356 | let mut dec_reg = RegisteredDecoders::new(); | |
357 | flash_register_all_decoders(&mut dec_reg); | |
358 | ||
886cde48 | 359 | // sample: https://samples.mplayerhq.hu/A-codecs/Nelly_Moser/input.flv |
92d9fb69 KS |
360 | test_decoding("flv", "flv263", "assets/Flash/input.flv", |
361 | Some(1000), &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![ | |
362 | [0x9110718e, 0x794e22ad, 0x3324e552, 0xf58a0449], | |
363 | [0x2f7cfcd1, 0x1d6bb63b, 0x37dcd87d, 0xb0247d9c], | |
364 | [0x317e6355, 0xc632f2d5, 0x1d6ae472, 0x45cc1ba6], | |
365 | [0x7d883ffc, 0xaa8e7c68, 0x8dec683b, 0x0e0dcdea], | |
366 | [0x79d4cece, 0x98749753, 0xfedb0fb1, 0x5398f6a0], | |
367 | [0x153f1558, 0xd98a700c, 0xdb166ebe, 0xc347fd61], | |
368 | [0x0b31c6a9, 0xcb126876, 0xd7dd8626, 0x4a6fea9f]])); | |
369 | } | |
370 | } |