]> git.nihav.org Git - nihav.git/blame - nihav-vivo/src/codecs/vivo.rs
add VivoActive (imperfect) format family support
[nihav.git] / nihav-vivo / src / codecs / vivo.rs
CommitLineData
31cf33ac
KS
1use nihav_core::io::bitreader::*;
2use nihav_core::io::codebook::*;
3use nihav_core::formats;
4use nihav_core::frame::*;
5use nihav_core::codecs::*;
6use nihav_codec_support::codecs::{MV, ZIGZAG};
7use nihav_codec_support::codecs::h263::*;
8use nihav_codec_support::codecs::h263::code::H263BlockDSP;
9use nihav_codec_support::codecs::h263::decoder::*;
10use nihav_codec_support::codecs::h263::data::*;
11
12#[allow(dead_code)]
13struct 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
22struct VivoDecoder {
23 info: NACodecInfoRef,
24 dec: H263BaseDecoder,
25 tables: Tables,
26 bdsp: H263BlockDSP,
27 lastframe: Option<NABufferType>,
28 lastpts: Option<u64>,
29 width: usize,
30 height: usize,
31}
32
33struct VivoBR<'a> {
34 br: BitReader<'a>,
35 tables: &'a Tables,
36 gob_no: usize,
37 mb_w: usize,
38 is_pb: bool,
39 is_ipb: bool,
40 ref_w: usize,
41 ref_h: usize,
42 aic: bool,
43}
44
45fn check_marker<'a>(br: &mut BitReader<'a>) -> DecoderResult<()> {
46 let mark = br.read(1)?;
47 validate!(mark == 1);
48 Ok(())
49}
50
51impl<'a> VivoBR<'a> {
52 fn new(src: &'a [u8], tables: &'a Tables, ref_w: usize, ref_h: usize) -> Self {
53 VivoBR {
54 br: BitReader::new(src, BitReaderMode::BE),
55 tables,
56 gob_no: 0,
57 mb_w: 0,
58 is_pb: false,
59 is_ipb: false,
60 ref_w, ref_h,
61 aic: false,
62 }
63 }
64
65 fn decode_block(&mut self, quant: u8, intra: bool, coded: bool, blk: &mut [i16; 64], plane_no: usize, acpred: ACPredMode) -> DecoderResult<()> {
66 let br = &mut self.br;
67 let mut idx = 0;
68 if !self.aic && intra {
69 let mut dc = br.read(8)? as i16;
70 if dc == 255 { dc = 128; }
71 blk[0] = dc << 3;
72 idx = 1;
73 }
74 if !coded { return Ok(()); }
75 let scan = match acpred {
76 ACPredMode::Hor => H263_SCAN_V,
77 ACPredMode::Ver => H263_SCAN_H,
78 _ => &ZIGZAG,
79 };
80
81 let rl_cb = if self.aic && intra { &self.tables.aic_rl_cb } else { &self.tables.rl_cb };
82 let q = if plane_no == 0 { (quant * 2) as i16 } else { (H263_CHROMA_QUANT[quant as usize] * 2) as i16 };
83 let q_add = if q == 0 || self.aic { 0i16 } else { (((q >> 1) - 1) | 1) as i16 };
84 while idx < 64 {
85 let code = br.read_cb(rl_cb)?;
86 let run;
87 let mut level;
88 let last;
89 if !code.is_escape() {
90 run = code.get_run();
91 level = code.get_level();
92 last = code.is_last();
93 if br.read_bool()? { level = -level; }
94 if level >= 0 {
95 level = (level * q) + q_add;
96 } else {
97 level = (level * q) - q_add;
98 }
99 } else {
100 last = br.read_bool()?;
101 run = br.read(6)? as u8;
102 level = br.read_s(8)? as i16;
103 if level == -128 {
104 let low = br.read(5)? as i16;
105 let top = br.read_s(6)? as i16;
106 level = (top << 5) | low;
107 }
108 if level >= 0 {
109 level = (level * q) + q_add;
110 } else {
111 level = (level * q) - q_add;
112 }
113 if level < -2048 { level = -2048; }
114 if level > 2047 { level = 2047; }
115 }
116 idx += run;
117 validate!(idx < 64);
118 let oidx = scan[idx as usize];
119 blk[oidx] = level;
120 idx += 1;
121 if last { break; }
122 }
123 Ok(())
124 }
125}
126
127fn decode_mv_component(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<i16> {
128 let code = i16::from(br.read_cb(mv_cb)?);
129 if code == 0 { return Ok(0) }
130 if !br.read_bool()? {
131 Ok(code)
132 } else {
133 Ok(-code)
134 }
135}
136
137fn decode_mv(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<MV> {
138 let xval = decode_mv_component(br, mv_cb)?;
139 let yval = decode_mv_component(br, mv_cb)?;
140 Ok(MV::new(xval, yval))
141}
142
143fn decode_b_info(br: &mut BitReader, is_pb: bool, is_ipb: bool, is_intra: bool) -> DecoderResult<BBlockInfo> {
144 if is_pb { // as improved pb
145 if is_ipb {
146 let pb_mv_add = if is_intra { 1 } else { 0 };
147 if br.read_bool()?{
148 if br.read_bool()? {
149 let pb_mv_count = 1 - (br.read(1)? as usize);
150 let cbpb = br.read(6)? as u8;
151 Ok(BBlockInfo::new(true, cbpb, pb_mv_count + pb_mv_add, pb_mv_count == 1))
152 } else {
153 Ok(BBlockInfo::new(true, 0, 1 + pb_mv_add, true))
154 }
155 } else {
156 Ok(BBlockInfo::new(true, 0, pb_mv_add, false))
157 }
158 } else {
159 let mvdb = br.read_bool()?;
160 let cbpb = if mvdb && br.read_bool()? { br.read(6)? as u8 } else { 0 };
161 Ok(BBlockInfo::new(true, cbpb, if mvdb { 1 } else { 0 }, false))
162 }
163 } else {
164 Ok(BBlockInfo::new(false, 0, 0, false))
165 }
166}
167
168impl<'a> BlockDecoder for VivoBR<'a> {
169
170#[allow(unused_variables)]
171 fn decode_pichdr(&mut self) -> DecoderResult<PicInfo> {
172 let br = &mut self.br;
173 let syncw = br.read(22)?;
174 validate!(syncw == 0x000020);
175 let tr = (br.read(8)? << 8) as u16;
176 check_marker(br)?;
177 let id = br.read(1)?;
178 validate!(id == 0);
179 br.read(1)?; // split screen indicator
180 br.read(1)?; // document camera indicator
181 br.read(1)?; // freeze picture release
182 let mut sfmt = br.read(3)?;
183 validate!(sfmt != 0b000);
184 let is_intra = !br.read_bool()?;
185 let umv = br.read_bool()?;
186 br.read(1)?; // syntax arithmetic coding
187 let apm = br.read_bool()?;
188 self.is_pb = br.read_bool()?;
189 let deblock;
190 let pbplus;
191 let aic;
192 if sfmt == 0b110 {
193 sfmt = br.read(3)?;
194 validate!(sfmt != 0b000 && sfmt != 0b110);
195 aic = br.read_bool()?;
196 br.read(1)?; // umv mode
197 deblock = br.read_bool()?;
198 br.read(3)?; // unknown flags
199 pbplus = br.read_bool()?;
200 br.read(4)?; // unknown flags
201 } else {
202 aic = false;
203 deblock = false;
204 pbplus = false;
205 }
206 self.is_ipb = pbplus;
207 let (w, h) = match sfmt {
208 0b001 => ( 64, 48),
209 0b011 => ( 88, 72),
210 0b010 => (176, 144),
211 0b100 => (352, 288),
212 0b101 => (704, 576),
213 0b111 => {
214 validate!((self.ref_w != 0) && (self.ref_h != 0));
215 ((self.ref_w + 15) & !15, (self.ref_h + 15) & !15)
216 },
217 _ => return Err(DecoderError::InvalidData),
218 };
219 let quant = br.read(5)?;
220 let cpm = br.read_bool()?;
221 validate!(!cpm);
222
223 let pbinfo;
224 if self.is_pb {
225 let trb = br.read(3)?;
226 let dbquant = br.read(2)?;
227 pbinfo = Some(PBInfo::new(trb as u8, dbquant as u8, pbplus));
228 } else {
229 pbinfo = None;
230 }
231 while br.read_bool()? { // skip PEI
232 br.read(8)?;
233 }
234 self.gob_no = 0;
235 self.mb_w = (w + 15) >> 4;
236 self.aic = aic;
237
238 let ftype = if is_intra { Type::I } else { Type::P };
239 let plusinfo = Some(PlusInfo::new(aic, deblock, false, false));
240 let mvmode = if umv { MVMode::UMV } else { MVMode::Old };
241 let picinfo = PicInfo::new(w, h, ftype, mvmode, umv, apm, quant as u8, tr, pbinfo, plusinfo);
242 Ok(picinfo)
243 }
244
245 #[allow(unused_variables)]
246 fn decode_slice_header(&mut self, info: &PicInfo) -> DecoderResult<SliceInfo> {
247 let br = &mut self.br;
248 let gbsc = br.read(17)?;
249 validate!(gbsc == 1);
250 let gn = br.read(5)?;
251 let gfid = br.read(2)?;
252 let gquant = br.read(5)?;
253 let ret = SliceInfo::new_gob(0, self.gob_no, gquant as u8);
254 self.gob_no += 1;
255 Ok(ret)
256 }
257
258 #[allow(unused_variables)]
259 fn decode_block_header(&mut self, info: &PicInfo, slice: &SliceInfo, sstate: &SliceState) -> DecoderResult<BlockInfo> {
260 let br = &mut self.br;
261 let mut q = slice.get_quant();
262 match info.get_mode() {
263 Type::I => {
264 let mut cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?;
265 while cbpc == 8 { cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; }
266 let mut acpred = ACPredMode::None;
267 if let Some(ref pi) = info.plusinfo {
268 if pi.aic {
269 let acpp = br.read_bool()?;
270 acpred = ACPredMode::DC;
271 if acpp {
272 acpred = if !br.read_bool()? { ACPredMode::Hor } else { ACPredMode::Ver };
273 }
274 }
275 }
276 let cbpy = br.read_cb(&self.tables.cbpy_cb)?;
277 let cbp = (cbpy << 2) | (cbpc & 3);
278 let dquant = (cbpc & 4) != 0;
279 if dquant {
280 let idx = br.read(2)? as usize;
281 q = (i16::from(q) + i16::from(H263_DQUANT_TAB[idx])) as u8;
282 }
283 let mut binfo = BlockInfo::new(Type::I, cbp, q);
284 binfo.set_acpred(acpred);
285 Ok(binfo)
286 },
287 Type::P => {
288 if br.read_bool()? { return Ok(BlockInfo::new(Type::Skip, 0, info.get_quant())); }
289 let mut cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?;
290 while cbpc == 20 { cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; }
291 let is_intra = (cbpc & 0x04) != 0;
292 let dquant = (cbpc & 0x08) != 0;
293 let is_4x4 = (cbpc & 0x10) != 0;
294 if is_intra {
295 let mut acpred = ACPredMode::None;
296 if let Some(ref pi) = info.plusinfo {
297 if pi.aic {
298 let acpp = br.read_bool()?;
299 acpred = ACPredMode::DC;
300 if acpp {
301 acpred = if !br.read_bool()? { ACPredMode::Hor } else { ACPredMode::Ver };
302 }
303 }
304 }
305 let mut mvec: Vec<MV> = Vec::new();
306 let bbinfo = decode_b_info(br, self.is_pb, self.is_ipb, true)?;
307 let cbpy = br.read_cb(&self.tables.cbpy_cb)?;
308 let cbp = (cbpy << 2) | (cbpc & 3);
309 if dquant {
310 let idx = br.read(2)? as usize;
311 q = (i16::from(q) + i16::from(H263_DQUANT_TAB[idx])) as u8;
312 }
313 let mut binfo = BlockInfo::new(Type::I, cbp, q);
314 binfo.set_bpart(bbinfo);
315 binfo.set_acpred(acpred);
316 if self.is_pb {
317 for _ in 0..bbinfo.get_num_mv() {
318 mvec.push(decode_mv(br, &self.tables.mv_cb)?);
319 }
320 binfo.set_b_mv(mvec.as_slice());
321 }
322 return Ok(binfo);
323 }
324
325 let bbinfo = decode_b_info(br, self.is_pb, self.is_ipb, false)?;
326 let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?;
327// if /* !aiv && */(cbpc & 3) != 3 {
328 cbpy ^= 0xF;
329// }
330 let cbp = (cbpy << 2) | (cbpc & 3);
331 if dquant {
332 let idx = br.read(2)? as usize;
333 q = (i16::from(q) + i16::from(H263_DQUANT_TAB[idx])) as u8;
334 }
335 let mut binfo = BlockInfo::new(Type::P, cbp, q);
336 binfo.set_bpart(bbinfo);
337 if !is_4x4 {
338 let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?];
339 binfo.set_mv(&mvec);
340 } else {
341 let mvec: [MV; 4] = [
342 decode_mv(br, &self.tables.mv_cb)?,
343 decode_mv(br, &self.tables.mv_cb)?,
344 decode_mv(br, &self.tables.mv_cb)?,
345 decode_mv(br, &self.tables.mv_cb)?
346 ];
347 binfo.set_mv(&mvec);
348 }
349 if self.is_pb {
350 let mut mvec: Vec<MV> = Vec::with_capacity(bbinfo.get_num_mv());
351 for _ in 0..bbinfo.get_num_mv() {
352 let mv = decode_mv(br, &self.tables.mv_cb)?;
353 mvec.push(mv);
354 }
355 binfo.set_b_mv(mvec.as_slice());
356 }
357 Ok(binfo)
358 },
359 _ => Err(DecoderError::InvalidData),
360 }
361 }
362
363 #[allow(unused_variables)]
364 fn decode_block_intra(&mut self, info: &BlockInfo, _sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> {
365 self.decode_block(quant, true, coded, blk, if no < 4 { 0 } else { no - 3 }, info.get_acpred())
366 }
367
368 #[allow(unused_variables)]
369 fn decode_block_inter(&mut self, info: &BlockInfo, _sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> {
370 self.decode_block(quant, false, coded, blk, if no < 4 { 0 } else { no - 3 }, ACPredMode::None)
371 }
372
373 fn is_slice_end(&mut self) -> bool { self.br.peek(16) == 0 }
374}
375
376impl VivoDecoder {
377 fn new() -> Self {
378 let mut coderead = H263ShortCodeReader::new(H263_INTRA_MCBPC);
379 let intra_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
380 let mut coderead = H263ShortCodeReader::new(H263_INTER_MCBPC);
381 let inter_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
382 let mut coderead = H263ShortCodeReader::new(H263_CBPY);
383 let cbpy_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
384 let mut coderead = H263RLCodeReader::new(H263_RL_CODES);
385 let rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
386 let mut coderead = H263RLCodeReader::new(H263_RL_CODES_AIC);
387 let aic_rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
388 let mut coderead = H263ShortCodeReader::new(H263_MV);
389 let mv_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
390 let tables = Tables {
391 intra_mcbpc_cb,
392 inter_mcbpc_cb,
393 cbpy_cb,
394 rl_cb,
395 aic_rl_cb,
396 mv_cb,
397 };
398
399 VivoDecoder{
400 info: NACodecInfo::new_dummy(),
401 dec: H263BaseDecoder::new(true),
402 tables,
403 bdsp: H263BlockDSP::new(),
404 lastframe: None,
405 lastpts: None,
406 width: 0,
407 height: 0,
408 }
409 }
410}
411
412impl NADecoder for VivoDecoder {
413 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
414 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
415 let w = vinfo.get_width();
416 let h = vinfo.get_height();
417 let fmt = formats::YUV420_FORMAT;
418 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
419 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
420 self.width = w;
421 self.height = h;
422 Ok(())
423 } else {
424 Err(DecoderError::InvalidData)
425 }
426 }
427 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
428 let src = pkt.get_buffer();
429
430 if src.len() == 0 {
431 let buftype;
432 let ftype;
433 if self.lastframe.is_none() {
434 buftype = NABufferType::None;
435 ftype = FrameType::Skip;
436 } else {
437 let mut buf = None;
438 std::mem::swap(&mut self.lastframe, &mut buf);
439 buftype = buf.unwrap();
440 ftype = FrameType::B;
441 }
442 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), buftype);
443 frm.set_keyframe(false);
444 frm.set_frame_type(ftype);
445 if self.lastpts.is_some() {
446 frm.set_pts(self.lastpts);
447 self.lastpts = None;
448 }
449 return Ok(frm.into_ref());
450 }
451 let mut ibr = VivoBR::new(&src, &self.tables, self.width, self.height);
452
453 let bufinfo = self.dec.parse_frame(&mut ibr, &self.bdsp)?;
454
455 let mut cur_pts = pkt.get_pts();
456 if !self.dec.is_intra() {
457 let bret = self.dec.get_bframe(&self.bdsp);
458 if let Ok(b_buf) = bret {
459 self.lastframe = Some(b_buf);
460 self.lastpts = pkt.get_pts();
461 if let Some(pts) = pkt.get_pts() {
462 cur_pts = Some(pts + 1);
463 }
464 }
465 }
466
467 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
468 frm.set_keyframe(self.dec.is_intra());
469 frm.set_frame_type(if self.dec.is_intra() { FrameType::I } else { FrameType::P });
470 frm.set_pts(cur_pts);
471 Ok(frm.into_ref())
472 }
473 fn flush(&mut self) {
474 self.dec.flush();
475 }
476}
477
478
479pub fn get_decoder() -> Box<dyn NADecoder + Send> {
480 Box::new(VivoDecoder::new())
481}
482
483#[cfg(test)]
484mod test {
485 use nihav_core::codecs::RegisteredDecoders;
486 use nihav_core::demuxers::RegisteredDemuxers;
487 use nihav_codec_support::test::dec_video::*;
488 use crate::vivo_register_all_codecs;
489 use crate::vivo_register_all_demuxers;
490 #[test]
491 fn test_vivo1() {
492 let mut dmx_reg = RegisteredDemuxers::new();
493 vivo_register_all_demuxers(&mut dmx_reg);
494 let mut dec_reg = RegisteredDecoders::new();
495 vivo_register_all_codecs(&mut dec_reg);
496
497test_file_decoding("vivo", "assets/Misc/gr_al.viv", Some(16), true, false, Some("viv1"), &dmx_reg, &dec_reg);
498// test_decoding("vivo", "vivo1", "assets/Misc/gr_al.viv", Some(16),
499// &dmx_reg, &dec_reg, ExpectedTestResult::GenerateMD5Frames));
500 }
501 #[test]
502 fn test_vivo2() {
503 let mut dmx_reg = RegisteredDemuxers::new();
504 vivo_register_all_demuxers(&mut dmx_reg);
505 let mut dec_reg = RegisteredDecoders::new();
506 vivo_register_all_codecs(&mut dec_reg);
507
508test_file_decoding("vivo", "assets/Misc/02-KimagureOrangeRoad.viv", Some(50), true, false, Some("viv2"), &dmx_reg, &dec_reg);
509panic!("end");
510// test_decoding("vivo", "vivo2", "assets/Misc/greetings.viv", Some(16),
511// &dmx_reg, &dec_reg, ExpectedTestResult::GenerateMD5Frames));
512 }
513}