1 use nihav_core::codecs::*;
2 use nihav_core::io::byteio::*;
10 fn decrypt(&mut self, buf: &mut [u8]) {
12 for el in buf.iter_mut() {
13 *el ^= self.key[(pos & 3) as usize];
14 pos = pos.wrapping_add(1);
17 fn set_state(&mut self, mut key: u32) {
19 let bit31 = (key >> 31) & 1;
20 let bit21 = (key >> 21) & 1;
21 let bit01 = (key >> 1) & 1;
23 key = (key << 1) | (bit31 ^ bit21 ^ bit01 ^ nbit0);
26 self.key[i] = (key >> (8 * (i ^ 3))) as u8;
32 tabs: [[i16; 256]; 2],
33 codebook: [[u8; 8]; 256],
34 num_elems: [usize; 256],
42 fn reset(&mut self, br: &mut ByteReader) -> DecoderResult<()> {
43 let b = br.read_byte()? as usize;
48 fn get_val(&mut self, br: &mut ByteReader) -> DecoderResult<u8> {
49 if self.vq_idx > self.codebook.len() { return Err(DecoderError::ShortData); }
50 let ret = self.codebook[self.vq_idx][self.vq_pos];
52 if self.vq_pos == self.num_elems[self.vq_idx] {
56 self.vq_idx = self.codebook.len() + 1;
61 fn remap(val: u16) -> i16 {
62 let hval = (val >> 1) as i16;
69 fn get_int(&mut self, br: &mut ByteReader) -> DecoderResult<i16> {
70 let b = self.get_val(br)?;
71 if b != self.vq_esc - 1 {
72 return Ok(Self::remap(u16::from(b)));
76 let mut pow = u16::from(self.vq_esc);
78 let b = self.get_val(br)?;
80 if b != self.vq_esc - 1 {
81 val = u16::from(b) * pow;
84 pow *= u16::from(self.vq_esc);
88 pow /= u16::from(self.vq_esc);
89 let b = u16::from(self.get_val(br)?);
94 fn get_dy(&mut self, br: &mut ByteReader) -> DecoderResult<i16> {
95 let b = self.get_val(br)?;
96 Ok(self.tabs[1][b as usize])
98 fn get_dc(&mut self, br: &mut ByteReader) -> DecoderResult<i16> {
99 let b = self.get_val(br)?;
100 Ok(self.tabs[0][b as usize])
104 impl Default for Deltas {
105 fn default() -> Self {
108 codebook: [[0; 8]; 256],
118 #[derive(Clone,Copy, Default)]
126 const NUM_CPARAMS: usize = 25;
127 const CPARAM_NONE: u8 = 42;
128 const CPARAM_MISSING: u8 = 42 * 2;
129 const CPARAM_MV: u8 = 42 * 3;
131 macro_rules! apply_delta {
132 ($buf:expr, $off:expr, $stride:expr, $hpred: expr, $delta:expr) => {
133 $hpred = $hpred.wrapping_add($delta);
134 $buf[$off] = $buf[$off - $stride].wrapping_add($hpred);
137 macro_rules! copy_line {
138 ($buf:expr, $off:expr, $stride:expr) => {
140 $buf[$off + i] = $buf[$off + i - $stride];
154 fn resize(&mut self, w: usize, h: usize) {
156 self.ydata.resize(self.stride * (h + 1), 0x80);
157 self.udata.resize(self.stride * (h + 1), 0x80);
158 self.vdata.resize(self.stride * (h + 1), 0x80);
164 info: NACodecInfoRef,
171 blk_info: Vec<BlkInfo>,
173 cparams: [[u8; 8]; NUM_CPARAMS],
179 fn new() -> Self { Self::default() }
180 fn output_frame(&mut self, buf: &mut NAVideoBuffer<u8>) {
181 let mut offs = [ buf.get_offset(0), buf.get_offset(1), buf.get_offset(2) ];
182 let strides = [ buf.get_stride(0), buf.get_stride(1), buf.get_stride(2) ];
183 let data = buf.get_data_mut().unwrap();
184 let dst = data.as_mut_slice();
186 let mut pos = self.cur_frame.stride;
187 for _y in 0..self.height {
188 for x in 0..self.width {
189 let y = self.cur_frame.ydata[pos + x];
190 let u = self.cur_frame.udata[pos + x];
191 let v = self.cur_frame.vdata[pos + x];
192 dst[offs[0] + x] = y.max(0).min(255) as u8;
193 dst[offs[1] + x] = u.max(0).min(255) as u8;
194 dst[offs[2] + x] = v.max(0).min(255) as u8;
197 offs[c] += strides[c];
199 pos += self.cur_frame.stride;
202 fn parse_init(&mut self, version: u8) -> DecoderResult<()> {
203 self.version = version;
205 let mut mr = MemoryReader::new_read(&self.dec_buf);
206 let mut br = ByteReader::new(&mut mr);
208 let _smth = br.read_u32be()?;
210 let height = br.read_u16be()? as usize;
211 let width = br.read_u16be()? as usize;
212 validate!(width == self.width && height == self.height);
214 let _smth = br.read_u32be()?;
216 let _smth = br.read_byte()?;
217 let _nfuncs = br.read_byte()? as usize;
218 let _smth = br.read_u16be()? as usize;
219 let has_mv = br.read_byte()?;
220 self.is_intra = has_mv == 0;
222 let _flags = br.read_u16be()?;
223 let id_len = br.read_byte()? as usize;
224 br.read_skip(id_len)?;
225 let _smth1 = br.read_byte()?;
226 let len = br.read_byte()? as usize;
228 let _smth = br.read_u32be()?;
233 fn parse_tabs(&mut self) -> DecoderResult<()> {
234 let mut mr = MemoryReader::new_read(&self.dec_buf);
235 let mut br = ByteReader::new(&mut mr);
237 let idx = br.read_byte()? as usize;
238 validate!(idx < self.deltas.tabs.len());
239 let len = br.read_byte()? as usize;
240 validate!(((len * 2) as i64) == br.left());
242 self.deltas.tabs[idx][i] = br.read_u16be()? as i16;
247 fn parse_cb_desc(&mut self, version: u8) -> DecoderResult<()> {
248 let mut mr = MemoryReader::new_read(&self.dec_buf);
249 let mut br = ByteReader::new(&mut mr);
252 self.deltas.vq_esc = br.read_byte()?;
253 validate!(self.deltas.vq_esc > 1);
254 let _tag = br.read_u16be()?;
256 let len = br.read_u16be()? as usize;
257 validate!(len + 3 == (br.left() as usize));
258 let num_entries = br.read_u16be()?;
259 validate!(num_entries == 256);
260 let max_elems = br.read_byte()?;
261 validate!(max_elems > 0 && max_elems <= 8);
263 while br.left() > 0 {
264 validate!(idx < self.deltas.codebook.len());
265 let num_elems = br.read_byte()? as usize;
266 validate!(num_elems <= 8);
267 self.deltas.num_elems[idx] = num_elems;
268 for i in 0..num_elems {
269 self.deltas.codebook[idx][i] = br.read_byte()?;
273 validate!(idx == 256);
274 self.deltas.num_vq = idx;
278 fn decode_frame(&mut self, src: &[u8]) -> DecoderResult<()> {
279 let mut mr = MemoryReader::new_read(src);
280 let mut br = ByteReader::new(&mut mr);
282 self.deltas.reset(&mut br)?;
283 let bw = self.width / 8;
284 let bh = self.height / 8;
285 let ntiles = (bw + self.tile_size - 1) / self.tile_size;
286 let mut pos = self.cur_frame.stride;
287 let ydata = &mut self.cur_frame.ydata;
288 let udata = &mut self.cur_frame.udata;
289 let vdata = &mut self.cur_frame.vdata;
290 let stride = self.cur_frame.stride;
292 for tile in 0..ntiles {
293 let xpos = tile * self.tile_size;
294 let len = self.tile_size.min(bw - xpos);
297 for el in self.blk_info.iter_mut().take(len) {
298 let t1 = self.deltas.get_val(&mut br)?;
299 let t2 = self.deltas.get_val(&mut br)?;
300 validate!((t1 as usize) < NUM_CPARAMS);
301 validate!(self.cparams[t1 as usize][0] != CPARAM_MISSING);
305 el.mv_x = self.deltas.get_int(&mut br)?;
306 el.mv_y = self.deltas.get_int(&mut br)?;
315 let mut ypred = 0i16;
316 let mut upred = 0i16;
317 let mut vpred = 0i16;
320 let op = self.cparams[self.blk_info[x].btype as usize][line];
321 let cur_off = pos + bx * 8 + line * stride;
325 let delta = self.deltas.get_dy(&mut br)?;
326 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
328 copy_line!(udata, cur_off, stride);
329 copy_line!(vdata, cur_off, stride);
336 let delta = self.deltas.get_dy(&mut br)?;
337 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
339 ydata[cur_off + i] = ydata[cur_off + i - 1];
342 copy_line!(udata, cur_off, stride);
343 copy_line!(vdata, cur_off, stride);
350 let delta = self.deltas.get_dy(&mut br)?;
351 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
353 ydata[cur_off + i] = ydata[cur_off + i - 1];
356 copy_line!(udata, cur_off, stride);
357 copy_line!(vdata, cur_off, stride);
362 let delta = self.deltas.get_dy(&mut br)?;
363 apply_delta!(ydata, cur_off, stride, ypred, delta);
365 ydata[cur_off + i] = ydata[cur_off];
367 copy_line!(udata, cur_off, stride);
368 copy_line!(vdata, cur_off, stride);
372 4 => { // c2y2c2y2|c2y2c2y2
373 for i in (0..8).step_by(2) {
374 let delta = self.deltas.get_dc(&mut br)?;
375 apply_delta!(udata, cur_off + i + 0, stride, upred, delta);
376 udata[cur_off + i + 1] = udata[cur_off + i];
377 let delta = self.deltas.get_dc(&mut br)?;
378 apply_delta!(vdata, cur_off + i + 0, stride, vpred, delta);
379 vdata[cur_off + i + 1] = vdata[cur_off + i];
380 let delta = self.deltas.get_dy(&mut br)?;
381 apply_delta!(ydata, cur_off + i + 0, stride, ypred, delta);
382 let delta = self.deltas.get_dy(&mut br)?;
383 apply_delta!(ydata, cur_off + i + 1, stride, ypred, delta);
389 let delta = self.deltas.get_dc(&mut br)?;
390 apply_delta!(udata, cur_off + i, stride, upred, delta);
391 let delta = self.deltas.get_dc(&mut br)?;
392 apply_delta!(vdata, cur_off + i, stride, vpred, delta);
393 let delta = self.deltas.get_dy(&mut br)?;
394 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
396 udata[cur_off + i] = udata[cur_off + i - 1];
397 vdata[cur_off + i] = vdata[cur_off + i - 1];
398 ydata[cur_off + i] = ydata[cur_off + i - 1];
402 6 | 7 => unreachable!(),
406 let delta = self.deltas.get_dc(&mut br)?;
407 apply_delta!(udata, cur_off + i, stride, upred, delta);
408 let delta = self.deltas.get_dc(&mut br)?;
409 apply_delta!(vdata, cur_off + i, stride, vpred, delta);
411 udata[cur_off + i] = udata[cur_off + i - 1];
412 vdata[cur_off + i] = vdata[cur_off + i - 1];
414 let delta = self.deltas.get_dy(&mut br)?;
415 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
421 let delta = self.deltas.get_dc(&mut br)?;
422 apply_delta!(udata, cur_off + i, stride, upred, delta);
423 let delta = self.deltas.get_dc(&mut br)?;
424 apply_delta!(vdata, cur_off + i, stride, vpred, delta);
426 udata[cur_off + i] = udata[cur_off + i - 1];
427 vdata[cur_off + i] = vdata[cur_off + i - 1];
430 let delta = self.deltas.get_dy(&mut br)?;
431 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
433 ydata[cur_off + i] = ydata[cur_off + i - 1];
440 let delta = self.deltas.get_dc(&mut br)?;
441 apply_delta!(udata, cur_off + i, stride, upred, delta);
442 let delta = self.deltas.get_dc(&mut br)?;
443 apply_delta!(vdata, cur_off + i, stride, vpred, delta);
444 let delta = self.deltas.get_dy(&mut br)?;
445 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
447 udata[cur_off + i] = udata[cur_off + i - 1];
448 vdata[cur_off + i] = vdata[cur_off + i - 1];
449 ydata[cur_off + i] = ydata[cur_off + i - 1];
453 11 => unreachable!(),
457 let delta = self.deltas.get_dc(&mut br)?;
458 apply_delta!(udata, cur_off + i, stride, upred, delta);
459 let delta = self.deltas.get_dc(&mut br)?;
460 apply_delta!(vdata, cur_off + i, stride, vpred, delta);
462 udata[cur_off + i] = udata[cur_off + i - 1];
463 vdata[cur_off + i] = vdata[cur_off + i - 1];
465 let delta = self.deltas.get_dy(&mut br)?;
466 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
472 let delta = self.deltas.get_dc(&mut br)?;
473 apply_delta!(udata, cur_off + i, stride, upred, delta);
474 let delta = self.deltas.get_dc(&mut br)?;
475 apply_delta!(vdata, cur_off + i, stride, vpred, delta);
477 udata[cur_off + i] = udata[cur_off + i - 1];
478 vdata[cur_off + i] = vdata[cur_off + i - 1];
481 let delta = self.deltas.get_dy(&mut br)?;
482 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
484 ydata[cur_off + i] = ydata[cur_off + i - 1];
491 let delta = self.deltas.get_dc(&mut br)?;
492 apply_delta!(udata, cur_off + i, stride, upred, delta);
493 let delta = self.deltas.get_dc(&mut br)?;
494 apply_delta!(vdata, cur_off + i, stride, vpred, delta);
496 udata[cur_off + i] = udata[cur_off + i - 1];
497 vdata[cur_off + i] = vdata[cur_off + i - 1];
500 let delta = self.deltas.get_dy(&mut br)?;
501 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
503 ydata[cur_off + i] = ydata[cur_off + i - 1];
510 let delta = self.deltas.get_dc(&mut br)?;
511 apply_delta!(udata, cur_off + i, stride, upred, delta);
512 let delta = self.deltas.get_dc(&mut br)?;
513 apply_delta!(vdata, cur_off + i, stride, vpred, delta);
514 let delta = self.deltas.get_dy(&mut br)?;
515 apply_delta!(ydata, cur_off + i, stride, ypred, delta);
517 udata[cur_off + i] = udata[cur_off + i - 1];
518 vdata[cur_off + i] = vdata[cur_off + i - 1];
519 ydata[cur_off + i] = ydata[cur_off + i - 1];
524 copy_line!(ydata, cur_off, stride);
525 copy_line!(udata, cur_off, stride);
526 copy_line!(vdata, cur_off, stride);
532 let src_x = (bx as i16) * 8 + self.blk_info[x].mv_x;
533 let src_y = ((by * 8 + line) as i16) + self.blk_info[x].mv_y;
534 validate!(src_x >= 0 && (src_x as usize) + 8 <= self.width);
535 validate!(src_y >= 0 && (src_y as usize) + 1 <= self.height);
536 let ref_off = (src_x as usize) + ((src_y + 1) as usize) * stride;
538 ydata[cur_off + i] = self.ref_frame.ydata[ref_off + i];
539 udata[cur_off + i] = self.ref_frame.udata[ref_off + i];
540 vdata[cur_off + i] = self.ref_frame.vdata[ref_off + i];
542 ypred = ydata[cur_off + 7] - ydata[cur_off + 7 - stride];
543 upred = udata[cur_off + 7] - udata[cur_off + 7 - stride];
544 vpred = vdata[cur_off + 7] - vdata[cur_off + 7 - stride];
558 impl NADecoder for TM2XDecoder {
559 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
560 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
561 let fmt = NAPixelFormaton::new(ColorModel::YUV(YUVSubmodel::YUVJ),
562 Some(NAPixelChromaton::new(0, 0, false, 8, 0, 0, 1)),
563 Some(NAPixelChromaton::new(0, 0, false, 8, 0, 1, 1)),
564 Some(NAPixelChromaton::new(0, 0, false, 8, 0, 2, 1)),
566 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(vinfo.get_width(), vinfo.get_height(), false, fmt));
567 self.width = vinfo.get_width();
568 self.height = vinfo.get_height();
569 self.cur_frame.resize(self.width, self.height);
570 self.ref_frame.resize(self.width, self.height);
571 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
574 Err(DecoderError::InvalidData)
577 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
578 let src = pkt.get_buffer();
579 validate!(src.len() > 8);
580 let mut mr = MemoryReader::new_read(&src);
581 let mut br = ByteReader::new(&mut mr);
582 let mut dec = Decryptor::default();
584 let mut initialised = false;
585 let mut got_key = false;
586 let mut data_size = 0;
587 self.cparams = [[CPARAM_MISSING; 8]; NUM_CPARAMS];
588 self.is_intra = false;
589 while br.left() >= 8 {
590 let magic = br.read_u24be()?;
591 let ctype = br.read_byte()?;
592 let size = br.read_u32be()? as usize;
593 validate!(magic == 0xA00001 && br.left() >= (size as i64));
595 validate!(size >= 4);
596 let key = br.read_u32be()?;
597 validate!((key as usize) == size - 4);
599 data_size = size - 4;
600 br.read_skip(size - 4)?;
604 self.dec_buf.resize(size, 0);
605 br.read_buf(&mut self.dec_buf)?;
607 dec.decrypt(&mut self.dec_buf);
610 0x08 | 0x0C | 0x10 | 0x11 | 0x15 | 0x16 => {
611 if ctype < 0x15 { // old versions are not encrypted, roll back and zero key
612 dec.decrypt(&mut self.dec_buf);
616 validate!(got_key && !initialised);
618 0x08 => self.parse_init(0)?,
619 0x0C => self.parse_init(1)?,
620 0x10 => self.parse_init(2)?,
621 0x11 => self.parse_init(3)?,
622 0x15 => self.parse_init(4)?,
623 0x16 => self.parse_init(5)?,
629 validate!(initialised);
630 validate!(self.dec_buf.len() == 3);
631 validate!(self.dec_buf[0] == 8);
632 validate!(self.dec_buf[1] > 0);
633 validate!(self.dec_buf[2] == 1);
634 self.tile_size = self.dec_buf[1] as usize;
635 self.blk_info.resize(self.tile_size, BlkInfo::default());
638 validate!(initialised);
639 validate!(self.dec_buf.len() == 4);
640 let idx = self.dec_buf[3] as usize;
641 validate!(idx < NUM_CPARAMS);
642 validate!((self.dec_buf[0] as usize) < TM2X_CODING_PARAMS.len());
643 if self.dec_buf[0] != 0 {
644 let tab = &TM2X_CODING_PARAMS[self.dec_buf[0] as usize];
645 let m0 = tab[0] as usize;
646 let m1 = tab[1] as usize;
647 let m2 = tab[2] as usize;
648 let m3 = tab[3] as usize;
649 let full_mode = (m2 * 4 + m0) as u8;
650 let lores_mode = m0 as u8;
652 if (i % m1) == 0 && (i % m3) == 0 {
653 self.cparams[idx][i] = full_mode;
654 } else if (i % m1) == 0 {
655 self.cparams[idx][i] = lores_mode;
657 self.cparams[idx][i] = CPARAM_NONE;
661 for i in 0..8 { self.cparams[idx][i] = CPARAM_MV; }
665 validate!(initialised);
669 validate!(initialised);
670 self.parse_cb_desc(0xA)?;
672 _ => { unimplemented!(); },
676 std::mem::swap(&mut self.cur_frame, &mut self.ref_frame);
678 self.decode_frame(&src[12..][..data_size])?;
680 let myinfo = self.info.get_properties().get_video_info().unwrap();
681 let bufinfo = alloc_video_buffer(myinfo, 2)?;
682 let mut buf = bufinfo.get_vbuf().unwrap();
684 self.output_frame(&mut buf);
686 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
687 frm.set_keyframe(self.is_intra);
688 frm.set_frame_type(if self.is_intra { FrameType::I } else { FrameType::P });
693 pub fn get_decoder() -> Box<dyn NADecoder> {
694 Box::new(TM2XDecoder::new())
699 use nihav_core::codecs::RegisteredDecoders;
700 use nihav_core::demuxers::RegisteredDemuxers;
701 use nihav_core::test::dec_video::*;
702 use crate::codecs::duck_register_all_codecs;
703 use nihav_commonfmt::demuxers::generic_register_all_demuxers;
706 let mut dmx_reg = RegisteredDemuxers::new();
707 generic_register_all_demuxers(&mut dmx_reg);
708 let mut dec_reg = RegisteredDecoders::new();
709 duck_register_all_codecs(&mut dec_reg);
711 test_file_decoding("avi", "assets/Duck/TM2x.avi", Some(16), true, false, None/*Some("tm2x")*/, &dmx_reg, &dec_reg);
715 const TM2X_CODING_PARAMS: [[u8; 4]; 25] = [
716 [ 0, 0, 0, 0 ], [ 0, 1, 1, 1 ], [ 0, 1, 1, 2 ], [ 0, 1, 2, 4 ], [ 1, 1, 2, 4 ],
717 [ 0, 2, 2, 4 ], [ 1, 2, 2, 4 ], [ 2, 2, 2, 4 ], [ 1, 4, 2, 4 ], [ 2, 4, 2, 4 ],
718 [ 2, 8, 3, 8 ], [ 3, 4, 3, 8 ], [ 3, 8, 3, 8 ], [ 0, 1, 1, 4 ], [ 0, 1, 2, 2 ],
719 [ 0, 2, 1, 4 ], [ 1, 1, 2, 2 ], [ 1, 4, 2, 8 ], [ 2, 2, 3, 4 ], [ 2, 4, 3, 8 ],
720 [ 0, 1, 3, 8 ], [ 1, 2, 3, 8 ], [ 2, 4, 2, 4 ], [ 2, 4, 3, 8 ], [ 3, 8, 3, 8 ]