1 use nihav_core::codecs::*;
2 use nihav_core::io::byteio::*;
3 use nihav_core::io::bitreader::*;
4 use nihav_core::io::codebook::*;
19 #[derive(Debug,Clone,Copy)]
30 const TM2_BLOCK_TYPES: [TM2BlockType; 7] = [
31 TM2BlockType::HiRes, TM2BlockType::MedRes, TM2BlockType::LowRes, TM2BlockType::NullRes,
32 TM2BlockType::Update, TM2BlockType::Still, TM2BlockType::Motion
36 fn read_len_esc(&mut self) -> DecoderResult<usize>;
39 const TM2_ESCAPE: usize = 0x80000000;
41 impl<'a> ReadLenEsc for ByteReader<'a> {
42 fn read_len_esc(&mut self) -> DecoderResult<usize> {
43 let len = self.read_u32le()? as usize;
44 if len == TM2_ESCAPE {
45 let len2 = self.read_u32le()? as usize;
60 fn read(&mut self, br: &mut BitReader, codes: &mut Vec<FullCodebookDesc<u8>>, prefix: u32, len: u8) -> DecoderResult<()> {
61 validate!(len <= self.max_bits);
63 validate!(codes.len() < self.nelems);
64 let sym = br.read(self.val_bits)? as u8;
65 codes.push(FullCodebookDesc { code: prefix, bits: len, sym });
67 self.read(br, codes, (prefix << 1) | 0, len + 1)?;
68 self.read(br, codes, (prefix << 1) | 1, len + 1)?;
75 cb: Option<Codebook<u8>>,
81 Self { cb: None, sym0: 0 }
85 const TM2_MAX_DELTAS: usize = 64;
89 deltas: [i32; TM2_MAX_DELTAS],
93 impl Default for TM2Stream {
94 fn default() -> Self {
97 deltas: [0; TM2_MAX_DELTAS],
104 fn read_header(&mut self, src: &[u8], br: &mut ByteReader) -> DecoderResult<()> {
105 self.tokens.truncate(0);
108 let len = br.read_u32le()? as usize;
109 let endpos = br.tell() + (len as u64) * 4;
113 let ntoks = br.read_u32le()? as usize;
114 validate!(ntoks < (1 << 24));
115 if (ntoks & 1) != 0 {
116 let dlen = br.read_len_esc()?;
117 if (dlen as i32) > 0 {
118 let rest_size = (endpos - br.tell()) as usize;
119 let skip_size = self.read_deltas(&src[br.tell() as usize..][..rest_size])?;
120 validate!(skip_size == dlen * 4);
121 br.read_skip(skip_size)?;
124 let _len = br.read_len_esc()?;
125 let _algo = br.read_u32le()?;
127 let mut htree = HuffTree::new();
128 let rest_size = (endpos - br.tell()) as usize;
129 let skip_size = self.read_huff_tree(&src[br.tell() as usize..][..rest_size], &mut htree)?;
130 br.read_skip(skip_size)?;
132 let len = br.read_u32le()? as usize;
133 validate!(br.tell() + (len as u64) * 4 <= endpos);
135 self.tokens.reserve(ntoks >> 1);
136 let rest_size = (endpos - br.tell()) as usize;
137 let skip_size = self.read_tokens(&src[br.tell() as usize..][..rest_size], &htree, ntoks >> 1)?;
138 br.read_skip(skip_size)?;
140 self.tokens.resize(ntoks >> 1, htree.sym0);
145 validate!(pos <= endpos);
146 let toskip = endpos - pos;
147 br.read_skip(toskip as usize)?;
151 fn read_deltas(&mut self, src: &[u8]) -> DecoderResult<usize> {
152 let mut br = BitReader::new(src, BitReaderMode::LE32MSB);
153 let coded_deltas = br.read(9)? as usize;
154 let bits = br.read(5)? as u8;
155 validate!((coded_deltas <= TM2_MAX_DELTAS) && (bits > 0));
156 let mask = 1 << (bits - 1);
157 let bias = 1 << bits;
158 self.deltas = [0; TM2_MAX_DELTAS];
159 for i in 0..coded_deltas {
160 let val = br.read(bits)?;
161 if (val & mask) != 0 {
162 self.deltas[i] = (val as i32) - bias;
164 self.deltas[i] = val as i32;
168 Ok(((br.tell() + 31) >> 5) << 2)
170 fn read_huff_tree(&mut self, src: &[u8], htree: &mut HuffTree) -> DecoderResult<usize> {
171 let mut br = BitReader::new(src, BitReaderMode::LE32MSB);
173 let val_bits = br.read(5)? as u8;
174 let max_bits = br.read(5)? as u8;
175 let min_bits = br.read(5)? as u8;
176 let nelems = br.read(17)? as usize;
177 validate!(val_bits > 0 && val_bits <= 6);
178 validate!(nelems > 0);
179 validate!((max_bits < 25) && (min_bits <= max_bits));
181 let mut codes: Vec<FullCodebookDesc<u8>> = Vec::with_capacity(nelems);
182 let mut hdef = HuffDef { val_bits, max_bits, nelems };
183 hdef.read(&mut br, &mut codes, 0, 0)?;
184 htree.sym0 = codes[0].sym;
186 let mut cr = FullCodebookDescReader::new(codes);
187 htree.cb = Some(Codebook::new(&mut cr, CodebookMode::MSB)?);
190 Ok(((br.tell() + 31) >> 5) << 2)
192 fn read_tokens(&mut self, src: &[u8], htree: &HuffTree, ntoks: usize) -> DecoderResult<usize> {
193 let mut br = BitReader::new(src, BitReaderMode::LE32MSB);
195 if let Some(ref cb) = htree.cb {
197 let tok = br.read_cb(cb)?;
198 self.tokens.push(tok);
202 Ok(((br.tell() + 31) >> 5) << 2)
205 fn get_block_type(&mut self) -> DecoderResult<u8> {
206 validate!(self.pos < self.tokens.len());
207 let res = self.tokens[self.pos];
211 fn get_token(&mut self) -> DecoderResult<i32> {
212 validate!(self.pos < self.tokens.len());
213 let idx = self.tokens[self.pos] as usize;
214 validate!(idx < TM2_MAX_DELTAS);
226 #[allow(clippy::erasing_op)]
228 fn apply_y(&mut self, dst: &mut [u8], mut yoff: usize, ystride: usize, ydeltas: &[i32; 16], last: &mut [i32]) {
230 let mut d = self.dy[y];
232 d += ydeltas[x + y * 4];
234 dst[yoff + x] = last[x].max(0).min(255) as u8;
240 fn apply_c(&mut self, dst: &mut [i16], mut coff: usize, cstride: usize, cdeltas: &[i32; 4], idx: usize, last: &mut [i32]) {
242 let mut d = self.dc[idx][y];
244 d += cdeltas[x + y * 2];
246 dst[coff + x] = last[x] as i16;
252 fn interpolate_y_low(&mut self, last: &mut [i32]) {
253 let dsum = self.dy[0] + self.dy[1] + self.dy[2] + self.dy[3];
254 last[1] = (last[0] - dsum + last[2]) >> 1;
255 last[3] = (last[2] + last[4]) >> 1;
257 let t0 = self.dy[0] + self.dy[1];
258 let t1 = self.dy[2] + self.dy[3];
259 self.dy[0] = t0 >> 1;
260 self.dy[1] = t0 - (t0 >> 1);
261 self.dy[2] = t1 >> 1;
262 self.dy[3] = t1 - (t1 >> 1);
264 fn interpolate_y_null(&mut self, last: &mut [i32]) {
265 let dsum = self.dy[0] + self.dy[1] + self.dy[2] + self.dy[3];
266 let left = last[0] - dsum;
268 let diff = right - left;
269 last[1] = left + (diff >> 2);
270 last[2] = left + (diff >> 1);
271 last[3] = right - (diff >> 2);
274 self.dy[0] = (left + (dsum >> 2)) - sum;
276 self.dy[1] = (left + (dsum >> 1)) - sum;
278 self.dy[2] = (left + dsum - (dsum >> 2)) - sum;
280 self.dy[3] = (left + dsum) - sum;
282 fn interpolate_c(&mut self, idx: usize, last: &mut [i32]) {
283 let dsum = self.dc[idx][0] + self.dc[idx][1];
284 let l = (last[0] + last[2] - dsum) >> 1;
285 self.dc[idx][0] = dsum >> 1;
286 self.dc[idx][1] = dsum - (dsum >> 1);
289 fn recalc_y(&mut self, dst: &[u8], yoff: usize, ystride: usize, last: &mut [i32]) {
290 let src = &dst[yoff+3..];
291 self.dy[0] = i32::from(src[ystride * 0]) - last[3];
292 self.dy[1] = i32::from(src[ystride * 1]) - i32::from(src[ystride * 0]);
293 self.dy[2] = i32::from(src[ystride * 2]) - i32::from(src[ystride * 1]);
294 self.dy[3] = i32::from(src[ystride * 3]) - i32::from(src[ystride * 2]);
295 let src = &dst[yoff + 3 * ystride..];
297 last[x] = i32::from(src[x]);
300 fn recalc_c(&mut self, dst: &[i16], coff: usize, cstride: usize, idx: usize, last: &mut [i32]) {
301 self.dc[idx][0] = i32::from(dst[coff + 1]) - last[1];
302 self.dc[idx][1] = i32::from(dst[coff + 1 + cstride]) - i32::from(dst[coff + 1]);
303 last[0] = i32::from(dst[coff + cstride + 0]);
304 last[1] = i32::from(dst[coff + cstride + 1]);
318 fn alloc(width: usize, height: usize) -> Self {
319 let ystride = (width + 3) & !3;
320 let ysize = ystride * ((height + 3) & !3);
321 let cstride = ystride >> 1;
322 let csize = cstride * (((height + 3) & !3) >> 1);
323 Self { ydata: vec![0; ysize], udata: vec![0; csize], vdata: vec![0; csize], ystride, cstride }
329 info: NACodecInfoRef,
330 streams: [TM2Stream; TM2StreamType::Num as usize],
334 prev_frame: TM2Frame,
338 fn new() -> Self { Self::default() }
339 #[allow(clippy::manual_memcpy)]
340 fn decode_blocks(&mut self) -> DecoderResult<bool> {
341 let ydst = &mut self.cur_frame.ydata;
342 let udst = &mut self.cur_frame.udata;
343 let vdst = &mut self.cur_frame.vdata;
344 let ystride = self.cur_frame.ystride;
345 let cstride = self.cur_frame.cstride;
346 let mut offs: [usize; 2] = [0; 2];
347 let mut is_intra = true;
349 let bw = self.width >> 2;
350 let bh = self.height >> 2;
351 validate!(self.streams[TM2StreamType::BlockType as usize].tokens.len() == bw * bh);
353 let mut ydeltas: [i32; 16] = [0; 16];
354 let mut cdeltas: [[i32; 4]; 2] = [[0; 4]; 2];
355 let mut lasty: Vec<i32> = vec![0; self.width + 1];
356 let mut lastu: Vec<i32> = vec![0; self.width/2 + 1];
357 let mut lastv: Vec<i32> = vec![0; self.width/2 + 1];
359 let mut dstate = DeltaState::default();
361 let bidx = self.streams[TM2StreamType::BlockType as usize].get_block_type()? as usize;
362 validate!(bidx < TM2_BLOCK_TYPES.len());
363 let btype = TM2_BLOCK_TYPES[bidx];
365 TM2BlockType::HiRes => {
367 cdeltas[0][i] = self.streams[TM2StreamType::CHigh as usize].get_token()?;
368 cdeltas[1][i] = self.streams[TM2StreamType::CHigh as usize].get_token()?;
370 dstate.apply_c(udst, offs[1] + bx * 2, cstride, &cdeltas[0], 0, &mut lastu[bx*2+1..]);
371 dstate.apply_c(vdst, offs[1] + bx * 2, cstride, &cdeltas[1], 1, &mut lastv[bx*2+1..]);
373 ydeltas[i] = self.streams[TM2StreamType::LHigh as usize].get_token()?;
375 dstate.apply_y(ydst, offs[0] + bx * 4, ystride, &ydeltas, &mut lasty[bx*4+1..]);
377 TM2BlockType::MedRes => {
378 cdeltas = [[0; 4]; 2];
379 cdeltas[0][0] = self.streams[TM2StreamType::CLow as usize].get_token()?;
380 cdeltas[1][0] = self.streams[TM2StreamType::CLow as usize].get_token()?;
381 dstate.interpolate_c(0, &mut lastu[bx*2..]);
382 dstate.apply_c(udst, offs[1] + bx * 2, cstride, &cdeltas[0], 0, &mut lastu[bx*2+1..]);
383 dstate.interpolate_c(1, &mut lastv[bx*2..]);
384 dstate.apply_c(vdst, offs[1] + bx * 2, cstride, &cdeltas[1], 1, &mut lastv[bx*2+1..]);
386 ydeltas[i] = self.streams[TM2StreamType::LHigh as usize].get_token()?;
388 dstate.apply_y(ydst, offs[0] + bx * 4, ystride, &ydeltas, &mut lasty[bx*4+1..]);
390 TM2BlockType::LowRes => {
391 cdeltas = [[0; 4]; 2];
392 cdeltas[0][0] = self.streams[TM2StreamType::CLow as usize].get_token()?;
393 cdeltas[1][0] = self.streams[TM2StreamType::CLow as usize].get_token()?;
394 dstate.interpolate_c(0, &mut lastu[bx*2..]);
395 dstate.apply_c(udst, offs[1] + bx * 2, cstride, &cdeltas[0], 0, &mut lastu[bx*2+1..]);
396 dstate.interpolate_c(1, &mut lastv[bx*2..]);
397 dstate.apply_c(vdst, offs[1] + bx * 2, cstride, &cdeltas[1], 1, &mut lastv[bx*2+1..]);
399 ydeltas[ 0] = self.streams[TM2StreamType::LLow as usize].get_token()?;
400 ydeltas[ 2] = self.streams[TM2StreamType::LLow as usize].get_token()?;
401 ydeltas[ 8] = self.streams[TM2StreamType::LLow as usize].get_token()?;
402 ydeltas[10] = self.streams[TM2StreamType::LLow as usize].get_token()?;
403 dstate.interpolate_y_low(&mut lasty[bx*4..]);
404 dstate.apply_y(ydst, offs[0] + bx * 4, ystride, &ydeltas, &mut lasty[bx*4+1..]);
406 TM2BlockType::NullRes => {
407 cdeltas = [[0; 4]; 2];
408 dstate.interpolate_c(0, &mut lastu[bx*2..]);
409 dstate.apply_c(udst, offs[1] + bx * 2, cstride, &cdeltas[0], 0, &mut lastu[bx*2+1..]);
410 dstate.interpolate_c(1, &mut lastv[bx*2..]);
411 dstate.apply_c(vdst, offs[1] + bx * 2, cstride, &cdeltas[1], 1, &mut lastv[bx*2+1..]);
413 dstate.interpolate_y_null(&mut lasty[bx*4..]);
414 dstate.apply_y(ydst, offs[0] + bx * 4, ystride, &ydeltas, &mut lasty[bx*4+1..]);
416 TM2BlockType::Update => {
419 let mut coff = offs[1] + bx * 2;
420 let usrc = &self.prev_frame.udata;
421 let vsrc = &self.prev_frame.vdata;
424 let du = self.streams[TM2StreamType::Update as usize].get_token()?;
425 let dv = self.streams[TM2StreamType::Update as usize].get_token()?;
426 udst[coff + x] = usrc[coff + x] + (du as i16);
427 vdst[coff + x] = vsrc[coff + x] + (dv as i16);
431 dstate.recalc_c(udst, offs[1] + bx * 2, cstride, 0, &mut lastu[bx*2+1..]);
432 dstate.recalc_c(vdst, offs[1] + bx * 2, cstride, 1, &mut lastv[bx*2+1..]);
433 let mut yoff = offs[0] + bx * 4;
434 let ysrc = &self.prev_frame.ydata;
437 let dy = self.streams[TM2StreamType::Update as usize].get_token()?;
438 ydst[yoff + x] = (i32::from(ysrc[yoff + x]) + dy) as u8;
442 dstate.recalc_y(ydst, offs[0] + bx * 4, ystride, &mut lasty[bx*4+1..]);
444 TM2BlockType::Still => {
447 let mut coff = offs[1] + bx * 2;
448 let usrc = &self.prev_frame.udata;
449 let vsrc = &self.prev_frame.vdata;
452 udst[coff + x] = usrc[coff + x];
453 vdst[coff + x] = vsrc[coff + x];
457 dstate.recalc_c(udst, offs[1] + bx * 2, cstride, 0, &mut lastu[bx*2+1..]);
458 dstate.recalc_c(vdst, offs[1] + bx * 2, cstride, 1, &mut lastv[bx*2+1..]);
459 let mut yoff = offs[0] + bx * 4;
460 let ysrc = &self.prev_frame.ydata;
463 ydst[yoff + x] = ysrc[yoff + x];
467 dstate.recalc_y(ydst, offs[0] + bx * 4, ystride, &mut lasty[bx*4+1..]);
469 TM2BlockType::Motion => {
472 let mx = self.streams[TM2StreamType::Motion as usize].get_token()?;
473 let my = self.streams[TM2StreamType::Motion as usize].get_token()?;
474 let xpos = (((bx as i32) * 4) + mx).max(0).min((self.width - 4) as i32) as usize;
475 let ypos = (((by as i32) * 4) + my).max(0).min((self.height - 4) as i32) as usize;
476 let mut coff = offs[1] + bx * 2;
477 let mut csoff = (xpos >> 1) + (ypos >> 1) * cstride;
478 let usrc = &self.prev_frame.udata;
479 let vsrc = &self.prev_frame.vdata;
482 udst[coff + x] = usrc[csoff + x];
483 vdst[coff + x] = vsrc[csoff + x];
488 dstate.recalc_c(udst, offs[1] + bx * 2, cstride, 0, &mut lastu[bx*2+1..]);
489 dstate.recalc_c(vdst, offs[1] + bx * 2, cstride, 1, &mut lastv[bx*2+1..]);
490 let mut yoff = offs[0] + bx * 4;
491 let mut ysoff = xpos + ypos * ystride;
492 let ysrc = &self.prev_frame.ydata;
495 ydst[yoff + x] = ysrc[ysoff + x];
500 dstate.recalc_y(ydst, offs[0] + bx * 4, ystride, &mut lasty[bx*4+1..]);
504 offs[0] += ystride * 4;
505 offs[1] += cstride * 2;
510 fn output_frame(&mut self, buf: &mut NAVideoBuffer<u8>) {
511 let fmt = buf.get_info().get_format();
512 let offs = [fmt.get_chromaton(0).unwrap().get_offset() as usize,
513 fmt.get_chromaton(1).unwrap().get_offset() as usize,
514 fmt.get_chromaton(2).unwrap().get_offset() as usize];
515 let stride = buf.get_stride(0);
516 let data = buf.get_data_mut().unwrap();
517 let dst = data.as_mut_slice();
522 for y in 0..self.height {
523 let out = &mut dst[off..];
524 for (x, pic) in out.chunks_exact_mut(3).take(self.width).enumerate() {
525 let y = i16::from(self.cur_frame.ydata[ysrc + x]);
526 let u = self.cur_frame.udata[csrc + (x >> 1)];
527 let v = self.cur_frame.vdata[csrc + (x >> 1)];
528 pic[offs[0]] = (y + u).max(0).min(255) as u8;
529 pic[offs[1]] = y.max(0).min(255) as u8;
530 pic[offs[2]] = (y + v).max(0).min(255) as u8;
533 ysrc += self.cur_frame.ystride;
535 csrc += self.cur_frame.cstride;
541 impl NADecoder for TM2Decoder {
542 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
543 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
544 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(vinfo.get_width(), vinfo.get_height(), false, YUV410_FORMAT));
545 self.width = vinfo.get_width();
546 self.height = vinfo.get_height();
547 self.cur_frame = TM2Frame::alloc(self.width, self.height);
548 self.prev_frame = TM2Frame::alloc(self.width, self.height);
549 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
552 Err(DecoderError::InvalidData)
555 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
556 let src = pkt.get_buffer();
557 validate!(src.len() >= 40 + (TM2StreamType::Num as usize) * 4 + 4);
558 let mut mr = MemoryReader::new_read(&src);
559 let mut br = ByteReader::new(&mut mr);
561 let magic = br.read_u32be()?;
562 validate!(magic == 0x100 || magic == 0x101);
564 for str in self.streams.iter_mut() {
565 str.read_header(&src, &mut br)?;
568 let myinfo = NAVideoInfo::new(self.width, self.height, false, RGB24_FORMAT);
569 let bufinfo = alloc_video_buffer(myinfo, 2)?;
570 let mut buf = bufinfo.get_vbuf().unwrap();
572 let is_intra = self.decode_blocks()?;
573 self.output_frame(&mut buf);
574 std::mem::swap(&mut self.cur_frame, &mut self.prev_frame);
576 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
577 frm.set_keyframe(is_intra);
578 frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P });
581 fn flush(&mut self) {
585 impl NAOptionHandler for TM2Decoder {
586 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
587 fn set_options(&mut self, _options: &[NAOption]) { }
588 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
591 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
592 Box::new(TM2Decoder::new())
597 use nihav_core::codecs::RegisteredDecoders;
598 use nihav_core::demuxers::RegisteredDemuxers;
599 use nihav_codec_support::test::dec_video::*;
600 use crate::duck_register_all_decoders;
601 use nihav_commonfmt::generic_register_all_demuxers;
604 let mut dmx_reg = RegisteredDemuxers::new();
605 generic_register_all_demuxers(&mut dmx_reg);
606 let mut dec_reg = RegisteredDecoders::new();
607 duck_register_all_decoders(&mut dec_reg);
609 test_decoding("avi", "truemotion2", "assets/Duck/tm20.avi", Some(16),
610 &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![
611 [0x8c336eb4, 0x10d0d934, 0x52392306, 0xc0bc6dd3],
612 [0xf168ddc2, 0x502fef17, 0xf7a5d0a2, 0xc0bf2d26],
613 [0xf33e02fa, 0x3931b691, 0xb29a0754, 0x07c0f8fa],
614 [0x2dd81034, 0x1c9f7616, 0x64eed48a, 0x3aa09cf0],
615 [0x55d18cd9, 0x0a3fd971, 0xf28fd5af, 0x9d9c3e3d],
616 [0xbf9cbbd8, 0x7b44c122, 0x1c7b1904, 0x77cc87aa],
617 [0xa6f6e79d, 0xc463a5bc, 0x5df9460c, 0xfce2e352],
618 [0x2ad22b2d, 0xd4ceaff8, 0xa4adb974, 0x37888a8d],
619 [0x4d45575f, 0x7e5b7670, 0x40cb7438, 0x9872d422],
620 [0xb35bc12b, 0x026c77c6, 0x93163784, 0xb37630b7],
621 [0xb1ec5059, 0x1fe26596, 0x4ac8d214, 0xdaf1b895],
622 [0x69dd5a5f, 0xe14a16fa, 0xa0653092, 0xb04e0739],
623 [0x979d8fe1, 0xbef29f89, 0xefae5f86, 0xa1ceb7d2],
624 [0xc6dc80d7, 0x80153c6b, 0x76d770c0, 0x8fd7cce7],
625 [0x8da96394, 0x1bd68024, 0x5feddfba, 0xd2b00660],
626 [0x53ff97c3, 0xc021a9b3, 0xabdddc10, 0xc99ab86f],
627 [0x66c877c4, 0xd8358048, 0xbca593db, 0xc6ecc4d1]]));