1 use nihav_core::formats;
2 use nihav_core::codecs::*;
3 use nihav_core::io::byteio::*;
5 use super::indeo3data::*;
7 const DEFAULT_PIXEL: u8 = 0x40;
8 const STRIP_WIDTH: u8 = 160;
9 const FRMH_TAG: u32 = ((b'F' as u32) << 24) | ((b'R' as u32) << 16)
10 | ((b'M' as u32) << 8) | (b'H' as u32);
12 const FLAG_8BIT: u16 = 1 << 1;
13 const FLAG_KEYFRAME: u16 = 1 << 2;
14 const FLAG_BUFSEL: u16 = 1 << 9;
16 const MAX_DEPTH: u8 = 20;
18 const H_SPLIT: u8 = 0;
19 const V_SPLIT: u8 = 1;
20 const ABS_FILL: u8 = 2;
21 const REL_FILL: u8 = 3;
22 const VQ_NULL: u8 = 2;
23 const VQ_DATA: u8 = 3;
25 type RequantTab = [[u8; 128]; 8];
28 fn add_delta(&mut self, delta: i8) -> DecoderResult<()>;
31 impl AddDelta for u8 {
32 fn add_delta(&mut self, delta: i8) -> DecoderResult<()> {
33 *self = self.wrapping_add(delta as u8);
34 validate!((*self & 0x80) == 0);
39 #[derive(Clone, Copy)]
60 mvs: [MV { x: 0, y: 0 }; 256],
69 struct DataReader<'a, 'b> {
70 br: &'a mut ByteReader<'b>,
75 impl<'a, 'b> DataReader<'a, 'b> {
76 fn new(br: &'a mut ByteReader<'b>) -> Self {
83 fn read_2bits(&mut self) -> DecoderResult<u8> {
85 self.bitbuf = self.br.read_byte()?;
89 let bits = (self.bitbuf >> self.bpos) & 3;
92 fn read_byte(&mut self) -> DecoderResult<u8> {
93 Ok(self.br.read_byte()?)
97 #[derive(Debug, PartialEq)]
108 fn is_whole_block(&self) -> bool { matches!(*self, Corrector::Fill(_) | Corrector::ZeroBlock | Corrector::SkipBlock) }
111 struct QuadDecoder<'a, 'b, 'c> {
112 br: &'a mut DataReader<'b, 'c>,
113 cb: [&'static IviDeltaCB; 4],
124 impl<'a, 'b, 'c> QuadDecoder<'a, 'b, 'c> {
125 fn new(br: &'a mut DataReader<'b, 'c>, mode: u8, cb_index: [usize; 2]) -> Self {
133 cb: [IVI3_DELTA_CBS[cb_index[0]], IVI3_DELTA_CBS[cb_index[1]],
134 IVI3_DELTA_CBS[cb_index[0]], IVI3_DELTA_CBS[cb_index[1]]],
135 cb_idx: [cb_index[0], cb_index[1], cb_index[0], cb_index[1]],
138 fn get_skip_corr(&self) -> Corrector {
145 fn read(&mut self, line: u8) -> DecoderResult<Corrector> {
146 if let Some(fill) = self.fill {
148 return Ok(Corrector::Fill(fill));
150 if self.lines_run > 0 {
152 return Ok(self.get_skip_corr());
154 if self.block_run > 0 {
156 let corr = if !self.skip_flag {
163 let mut b = self.br.read_byte()?;
165 self.next_lit = false;
167 b = (self.cb[usize::from(line)].data.len() / 2) as u8;
173 let cb = self.cb[usize::from(line)];
175 let esc_val = (cb.data.len() / 2) as u8;
176 let (idx0, idx1) = if b < esc_val {
177 let idx2 = self.br.read_byte()?;
178 validate!(idx2 < esc_val);
180 } else if self.cb_idx[usize::from(line)] < 16 {
181 ((b - esc_val) % cb.quad_radix, (b - esc_val) / cb.quad_radix)
183 ((b - esc_val) / cb.quad_radix, (b - esc_val) % cb.quad_radix)
185 let idx0 = usize::from(idx0);
186 let idx1 = usize::from(idx1);
187 Ok(Corrector::Quad([cb.data[idx1 * 2], cb.data[idx1 * 2 + 1],
188 cb.data[idx0 * 2], cb.data[idx0 * 2 + 1]]))
191 validate!(line == 0);
192 let fillval = self.br.read_byte()?;
193 if (fillval & 0x80) != 0 {
194 self.fill = Some(fillval & 0x7F);
196 Ok(Corrector::Fill(fillval & 0x7F))
199 validate!(line == 0);
200 self.skip_flag = true;
202 Ok(Corrector::SkipBlock)
205 validate!(self.mode != 3 && self.mode != 10);
206 Ok(Corrector::SkipBlock)
209 let b = self.br.read_byte()?;
210 validate!((b & 0x1F) > 0);
212 self.skip_flag = (b & 0x20) != 0;
213 self.block_run = (b & 0x1F) - 1;
215 self.lines_run = 3 - line;
216 Ok(self.get_skip_corr())
218 let corr = if !self.skip_flag {
228 self.skip_flag = false;
230 self.lines_run = 3 - line;
233 Ok(Corrector::ZeroBlock)
237 self.lines_run = 3 - line;
238 self.skip_flag = false;
243 self.lines_run = 2 - line;
244 self.skip_flag = false;
248 validate!(line == 0);
250 self.skip_flag = false;
251 self.next_lit = true;
258 #[derive(Clone, Copy)]
269 fn new(width: usize, height: usize) -> Self {
273 width: (width / 4) as u8,
274 height: (height / 4) as u8,
280 fn get_x(&self) -> usize { usize::from(self.x) * 4 }
281 fn get_y(&self) -> usize { usize::from(self.y) * 4 }
282 fn get_width(&self) -> usize { usize::from(self.width) * 4 }
283 fn get_height(&self) -> usize { usize::from(self.height) * 4 }
284 fn is_intra(&self) -> bool { self.mv.is_none() }
286 fn split_h(&self) -> (Self, Self) {
287 let h1 = if self.height > 2 { ((self.height + 2) >> 2) << 1 } else { 1 };
288 let h2 = self.height - h1;
289 let mut cell1 = *self;
292 let mut cell2 = *self;
298 #[allow(clippy::collapsible_else_if)]
299 fn split_v(&self, stripw: u8) -> (Self, Self) {
300 let w1 = if self.width > stripw {
301 if self.width > stripw * 2 { stripw * 2 } else { stripw }
303 if self.width > 2 { ((self.width + 2) >> 2) << 1 } else { 1 }
305 let w2 = self.width - w1;
306 let mut cell1 = *self;
309 let mut cell2 = *self;
317 impl std::fmt::Display for Indeo3Cell {
318 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
319 let spec = if let Some(mv) = self.mv {
320 format!("mv {},{}", mv.x, mv.y)
324 write!(f, "[{}x{} @ {},{} {}]", self.get_width(), self.get_height(), self.get_x(), self.get_y(), spec)
337 fn new(stripw: u8) -> Self {
343 fn alloc(&mut self, width: usize, height: usize) {
345 self.height = height;
346 self.data.resize(width * (height + 1), 0);
347 for el in self.data[..width].iter_mut() {
351 fn reset(&mut self) {
352 for el in self.data[self.width..].iter_mut() {
356 fn output_plane(&self, dst: &mut [u8], stride: usize, is_8bit: bool) {
357 for (dline, sline) in dst.chunks_mut(stride).zip(self.data.chunks(self.width).skip(1)) {
359 for (dst, &src) in dline.iter_mut().zip(sline.iter()) {
363 let size = dline.len();
364 dline.copy_from_slice(&sline[..size]);
368 fn checksum(&self) -> u16 {
369 let mut checksum = [0; 2];
370 for pair in self.data.chunks(2) {
371 checksum[0] ^= pair[0];
372 checksum[1] ^= pair[1];
374 read_u16le(&checksum).unwrap_or(0)
376 fn decode_data(&mut self, br: &mut DataReader, ref_plane: &mut Self, header: &Header, requant_tab: &RequantTab) -> DecoderResult<()> {
377 let cell = Indeo3Cell::new(self.width, self.height);
378 self.decode_mv_tree(br, ref_plane, cell, header, requant_tab)?;
381 fn decode_mv_tree(&mut self, br: &mut DataReader, ref_plane: &mut Self, cell: Indeo3Cell, header: &Header, requant_tab: &RequantTab) -> DecoderResult<()> {
382 validate!(cell.depth < MAX_DEPTH);
383 match br.read_2bits()? {
385 let (cell1, cell2) = cell.split_h();
386 self.decode_mv_tree(br, ref_plane, cell1, header, requant_tab)?;
387 self.decode_mv_tree(br, ref_plane, cell2, header, requant_tab)?;
390 let (cell1, cell2) = cell.split_v(self.stripw);
391 self.decode_mv_tree(br, ref_plane, cell1, header, requant_tab)?;
392 self.decode_mv_tree(br, ref_plane, cell2, header, requant_tab)?;
395 self.decode_vq_tree(br, ref_plane, cell, header, requant_tab)?;
398 validate!(!header.is_intra);
399 let mv_idx = usize::from(br.read_byte()?);
400 validate!(mv_idx < header.num_mvs);
401 let mut sec_cell = cell;
402 sec_cell.mv = Some(header.mvs[mv_idx]);
403 self.decode_vq_tree(br, ref_plane, sec_cell, header, requant_tab)?;
409 fn decode_vq_tree(&mut self, br: &mut DataReader, ref_plane: &mut Self, cell: Indeo3Cell, header: &Header, requant_tab: &RequantTab) -> DecoderResult<()> {
410 validate!(cell.depth < MAX_DEPTH);
411 match br.read_2bits()? {
413 let (cell1, cell2) = cell.split_h();
414 self.decode_vq_tree(br, ref_plane, cell1, header, requant_tab)?;
415 self.decode_vq_tree(br, ref_plane, cell2, header, requant_tab)?;
418 let (cell1, cell2) = cell.split_v(self.stripw);
419 self.decode_vq_tree(br, ref_plane, cell1, header, requant_tab)?;
420 self.decode_vq_tree(br, ref_plane, cell2, header, requant_tab)?;
423 let code = br.read_2bits()?;
426 self.copy_cell(ref_plane, cell)?;
428 1 => return Err(DecoderError::NotImplemented), // skip cell
429 _ => return Err(DecoderError::InvalidData),
433 let code = br.read_byte()?;
434 let mode = code >> 4;
435 let vq_index = code & 0xF;
436 let cb_index = if matches!(mode, 1 | 4 | 12) {
437 let aq = header.alt_quant[usize::from(vq_index)];
442 let cb_index = [usize::from(cb_index[0] + header.vq_offset), usize::from(cb_index[1] + header.vq_offset)];
443 validate!(cb_index[0] < IVI3_DELTA_CBS.len());
444 validate!(cb_index[1] < IVI3_DELTA_CBS.len());
445 if cell.get_y() > 0 && matches!(mode, 0 | 3 | 10) && (8..=15).contains(&cb_index[0]) {
446 let src = if cell.is_intra() {
447 &mut self.data[cell.get_x() + cell.get_y() * self.width..]
449 &mut ref_plane.data[cell.get_x() + (cell.get_y() + 1) * ref_plane.width..]
451 let cur_requant_tab = &requant_tab[cb_index[0] - 8];
452 for el in src.iter_mut().take(cell.get_width()) {
453 *el = cur_requant_tab[usize::from(*el)];
457 let qmode = if mode != 10 || cell.is_intra() { mode } else { 20 };
458 let mut quad_decoder = QuadDecoder::new(br, qmode, cb_index);
459 if !cell.is_intra() {
460 self.copy_cell(ref_plane, cell)?;
464 self.process_cell_0_1(&mut quad_decoder, cell)?;
467 validate!(cell.is_intra());
468 validate!((cell.get_height() & 7) == 0);
469 self.process_cell_3_4(&mut quad_decoder, cell)?;
472 validate!((cell.get_width() & 7) == 0);
473 validate!((cell.get_height() & 7) == 0);
474 self.process_cell_10_intra(&mut quad_decoder, cell)?;
477 validate!((cell.get_width() & 7) == 0);
478 validate!((cell.get_height() & 7) == 0);
479 self.process_cell_10_inter(&mut quad_decoder, cell)?;
482 validate!(!cell.is_intra());
483 validate!((cell.get_height() & 7) == 0);
484 self.process_cell_11(&mut quad_decoder, cell)?;
486 2 | 12 => { return Err(DecoderError::NotImplemented)},
487 _ => return Err(DecoderError::InvalidData),
489 if matches!(qmode, 3 | 4 | 10) && cell.get_y() == 0 {
490 let line_pair = &mut self.data[cell.get_x() + (cell.get_y() + 1) * self.width..];
491 let (line0, line1) = line_pair.split_at_mut(self.width);
492 line0[..cell.get_width()].copy_from_slice(&line1[..cell.get_width()]);
499 fn process_cell_0_1(&mut self, qd: &mut QuadDecoder, cell: Indeo3Cell) -> DecoderResult<()> {
500 let stride = self.width;
501 let mut offset = cell.get_x() + (cell.get_y() + 1) * stride;
502 let cell_w = cell.get_width();
503 for _y in (0..cell.get_height()).step_by(4) {
505 for x in (0..cell_w).step_by(4) {
506 let top = &self.data[offset - stride + x..];
507 let mut top = [top[0], top[1], top[2], top[3]];
509 let corr = qd.read(line as u8)?;
510 if !cell.is_intra() && !corr.is_whole_block() {
511 let src = &self.data[offset + line * stride + x..];
512 top.copy_from_slice(&src[..4]);
515 Corrector::SkipBlock => continue 'block0,
516 Corrector::Fill(fill) => {
517 for line in self.data[offset + x..].chunks_mut(stride).take(4) {
518 for el in line[..4].iter_mut() {
523 Corrector::ZeroBlock if cell.is_intra() => {
524 let (head, cur) = self.data.split_at_mut(offset + x);
525 let prev = &head[head.len() - stride..];
526 for dline in cur.chunks_mut(stride).take(4) {
527 dline[..4].copy_from_slice(&prev[..4]);
531 Corrector::ZeroBlock => continue 'block0,
532 Corrector::Quad(quad) => {
533 for (el, &corr) in top.iter_mut().zip(quad.iter()) {
537 Corrector::Zero => {},
538 Corrector::Skip if cell.is_intra() => unimplemented!(),
539 Corrector::Skip => {},
541 let wback = match corr {
542 Corrector::Zero if cell.is_intra() => true,
543 Corrector::Quad(_) => true,
547 self.data[offset + x + line * stride..][..4].copy_from_slice(&top);
551 offset += self.width * 4;
555 fn process_cell_3_4(&mut self, qd: &mut QuadDecoder, cell: Indeo3Cell) -> DecoderResult<()> {
556 let stride = self.width;
557 let mut offset = cell.get_x() + (cell.get_y() + 1) * stride;
558 let cell_w = cell.get_width();
559 for _y in (0..cell.get_height()).step_by(8) {
561 for x in (0..cell_w).step_by(4) {
562 let top = &self.data[offset - stride + x..][..4];
563 let mut top = [top[0], top[1], top[2], top[3]];
565 let corr = qd.read(line as u8)?;
567 Corrector::SkipBlock => continue 'block3,
568 Corrector::Fill(fill) => {
569 for line in self.data[offset + x..].chunks_mut(stride).take(8) {
570 for el in line[..4].iter_mut() {
575 Corrector::ZeroBlock => {
576 for dline in self.data[offset + x..].chunks_mut(stride).take(8) {
577 dline[..4].copy_from_slice(&top);
581 Corrector::Quad(quad) => {
582 for (el, &corr) in top.iter_mut().zip(quad.iter()) {
586 Corrector::Zero => {},
587 Corrector::Skip => unimplemented!(),
590 if corr != Corrector::Skip {
591 let dst = &mut self.data[offset + x + (line * 2 + 1) * stride..][..4];
592 dst.copy_from_slice(&top);
596 let mut top_off = offset + x - stride;
599 self.data[top_off + stride + pos] = (self.data[top_off + pos] + self.data[top_off + stride * 2 + pos]) >> 1;
601 top_off += stride * 2;
604 offset += self.width * 8;
608 fn process_cell_10_intra(&mut self, qd: &mut QuadDecoder, cell: Indeo3Cell) -> DecoderResult<()> {
609 let stride = self.width;
610 let mut offset = cell.get_x() + (cell.get_y() + 1) * stride;
611 let cell_w = cell.get_width();
612 for _y in (0..cell.get_height()).step_by(8) {
614 for x in (0..cell_w).step_by(8) {
615 let top = &self.data[offset - stride + x..][..8];
616 let mut top = [top[0], top[2], top[4], top[6]];
618 let corr = qd.read(line as u8)?;
620 Corrector::SkipBlock => continue 'block10i,
621 Corrector::Fill(fill) => {
622 for line in self.data[offset + x..].chunks_mut(stride).take(8) {
623 for el in line[..8].iter_mut() {
628 Corrector::ZeroBlock => {
629 let line_pair = &mut self.data[offset - stride + x..];
630 let (top_line, dst_line) = line_pair.split_at_mut(stride);
631 for (i, (dst, &top_s)) in dst_line.iter_mut()
632 .zip(top_line.iter()).take(8).enumerate() {
633 *dst = (top_s + top[i >> 1]) >> 1;
635 for dline in self.data[offset + x..].chunks_mut(stride).take(8).skip(1) {
636 for (dst, &src) in dline.chunks_mut(2).zip(top.iter()) {
643 Corrector::Quad(quad) => {
644 for (el, &corr) in top.iter_mut().zip(quad.iter()) {
648 Corrector::Zero => {},
649 Corrector::Skip => unimplemented!(),
652 if corr != Corrector::Skip {
653 for (dst, &prev) in self.data[offset + x + (line * 2 + 1) * stride..].chunks_mut(2).zip(top.iter()).take(4) {
660 let mut top_off = offset + x - stride;
663 self.data[top_off + stride + pos] = (self.data[top_off + pos] + self.data[top_off + stride * 2 + pos]) >> 1;
665 top_off += stride * 2;
668 offset += self.width * 8;
672 fn process_cell_10_inter(&mut self, qd: &mut QuadDecoder, cell: Indeo3Cell) -> DecoderResult<()> {
673 let stride = self.width;
674 let mut offset = cell.get_x() + (cell.get_y() + 1) * stride;
675 let cell_w = cell.get_width();
676 for _y in (0..cell.get_height()).step_by(8) {
678 for x in (0..cell_w).step_by(8) {
680 let corr = qd.read(line as u8)?;
682 Corrector::SkipBlock | Corrector::ZeroBlock => continue 'block10p,
683 Corrector::Fill(fill) => {
684 for line in self.data[offset + x..].chunks_mut(stride).take(8) {
685 for el in line[..8].iter_mut() {
690 Corrector::Quad(quad) => {
691 let strip = &mut self.data[offset + x + line * 2 * stride..];
692 for (xoff, &corr) in quad.iter().enumerate() {
693 strip[xoff * 2].add_delta(corr)?;
694 strip[xoff * 2 + 1].add_delta(corr)?;
695 strip[xoff * 2 + stride].add_delta(corr)?;
696 strip[xoff * 2 + stride + 1].add_delta(corr)?;
703 offset += self.width * 8;
707 fn process_cell_11(&mut self, qd: &mut QuadDecoder, cell: Indeo3Cell) -> DecoderResult<()> {
708 let stride = self.width;
709 let mut offset = cell.get_x() + (cell.get_y() + 1) * stride;
710 let cell_w = cell.get_width();
711 for _y in (0..cell.get_height()).step_by(8) {
713 for x in (0..cell_w).step_by(4) {
715 let corr = qd.read(line as u8)?;
717 Corrector::SkipBlock | Corrector::ZeroBlock => continue 'block10p,
718 Corrector::Fill(fill) => {
719 for line in self.data[offset + x..].chunks_mut(stride).take(8) {
720 for el in line[..4].iter_mut() {
725 Corrector::Quad(quad) => {
726 let strip = &mut self.data[offset + x + line * 2 * stride..];
727 for (xoff, &corr) in quad.iter().enumerate() {
728 strip[xoff].add_delta(corr)?;
729 strip[xoff + stride].add_delta(corr)?;
736 offset += self.width * 8;
740 fn copy_cell(&mut self, ref_plane: &Self, cell: Indeo3Cell) -> DecoderResult<()> {
741 if let Some(mv) = cell.mv {
742 let xpos = (cell.get_x() as isize) + isize::from(mv.x);
743 validate!(xpos >= 0);
744 let xpos = xpos as usize;
745 validate!(xpos + cell.get_width() <= self.width);
746 let ypos = (cell.get_y() as isize) + isize::from(mv.y);
747 validate!(ypos >= 0);
748 let ypos = ypos as usize;
749 validate!(ypos + cell.get_height() <= self.height);
750 let src = &ref_plane.data[xpos + (ypos + 1) * ref_plane.width..];
751 let dst = &mut self.data[cell.get_x() + (cell.get_y() + 1) * self.width..];
753 let width = cell.get_width();
754 let height = cell.get_height();
755 for (dline, sline) in dst.chunks_mut(ref_plane.width).zip(src.chunks(self.width)).take(height) {
756 dline[..width].copy_from_slice(&sline[..width]);
760 Err(DecoderError::InvalidData)
773 Plane::new(STRIP_WIDTH),
774 Plane::new(STRIP_WIDTH >> 2),
775 Plane::new(STRIP_WIDTH >> 2),
779 fn alloc(&mut self, width: usize, height: usize) {
780 self.plane[0].alloc( width, height);
781 let chroma_w = ((width + 15) & !15) >> 2;
782 let chroma_h = ((height + 15) & !15) >> 2;
783 self.plane[1].alloc(chroma_w, chroma_h);
784 self.plane[2].alloc(chroma_w, chroma_h);
786 fn reset(&mut self) {
787 for plane in self.plane.iter_mut() {
791 fn decode_planes(&mut self, ref_frame: &mut IV3Frame, br: &mut ByteReader, header: &mut Header, requant_tab: &RequantTab) -> DecoderResult<()> {
792 let data_start = header.data_start;
793 let data_end = header.data_end;
794 for ((cur_plane, ref_plane), (&start, &end)) in self.plane.iter_mut()
795 .zip(ref_frame.plane.iter_mut())
796 .zip(data_start.iter().zip(data_end.iter())) {
797 br.seek(SeekFrom::Start(start))?;
798 let num_mvs = br.read_u32le()? as usize;
800 validate!(num_mvs == 0);
802 validate!(num_mvs <= header.mvs.len());
804 header.num_mvs = num_mvs;
805 for mv in header.mvs.iter_mut().take(num_mvs) {
806 mv.y = br.read_byte()? as i8;
807 mv.x = br.read_byte()? as i8;
809 let mut reader = DataReader::new(br);
810 cur_plane.decode_data(&mut reader, ref_plane, header, requant_tab)?;
811 validate!(br.tell() <= end);
815 fn output_frame(&self, dst: &mut NAVideoBuffer<u8>, is_8bit: bool) {
816 let dfrm = NASimpleVideoFrame::from_video_buf(dst).unwrap();
817 for (plane_no, plane) in self.plane.iter().enumerate() {
818 plane.output_plane(&mut dfrm.data[dfrm.offset[plane_no]..], dfrm.stride[plane_no], is_8bit);
821 fn checksum(&self, is_8bit: bool) -> u16 {
822 let mut checksum = 0;
823 for plane in self.plane.iter() {
824 checksum ^= plane.checksum();
833 struct Indeo3Decoder {
834 info: NACodecInfoRef,
840 requant_tab: RequantTab,
847 const REQUANT_OFF: [i32; 8] = [ 0, 1, 0, 4, 4, 1, 0, 1 ];
849 let dummy_info = NACodecInfo::new_dummy();
851 let mut requant_tab = [[0u8; 128]; 8];
853 let step = (i as i32) + 2;
854 let start = if (i == 3) || (i == 4) { -3 } else { step / 2 };
857 requant_tab[i][j] = (((j as i32) + start) / step * step + REQUANT_OFF[i]) as u8;
858 if requant_tab[i][j] < 128 {
859 last = requant_tab[i][j];
861 requant_tab[i][j] = last;
865 requant_tab[1][7] = 10;
866 requant_tab[1][119] = 118;
867 requant_tab[1][120] = 118;
868 requant_tab[4][8] = 10;
874 header: Header::new(),
875 frame0: IV3Frame::new(),
876 frame1: IV3Frame::new(),
884 impl NADecoder for Indeo3Decoder {
885 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
886 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
887 let w = vinfo.get_width();
888 let h = vinfo.get_height();
889 let fmt = formats::YUV410_FORMAT;
890 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
891 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
894 self.width = w as u16;
895 self.height = h as u16;
896 self.frame0.alloc(w, h);
897 self.frame1.alloc(w, h);
900 Err(DecoderError::InvalidData)
903 #[allow(clippy::manual_range_contains)]
904 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
905 let src = pkt.get_buffer();
906 let mut mr = MemoryReader::new_read(&src);
907 let mut br = ByteReader::new(&mut mr);
910 let frameno = br.read_u32le()?;
911 let hdr_2 = br.read_u32le()?;
912 let check = br.read_u32le()?;
913 let size = br.read_u32le()?;
915 let data_start = br.tell();
917 if (frameno ^ hdr_2 ^ size ^ FRMH_TAG) != check {
918 return Err(DecoderError::InvalidData);
920 if i64::from(size) > br.left() {
921 return Err(DecoderError::InvalidData);
924 let ver = br.read_u16le()?;
925 if ver != 32 { return Err(DecoderError::NotImplemented); }
926 let flags = br.read_u16le()?;
927 let size2 = br.read_u32le()?;
929 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
930 frm.set_keyframe(false);
931 frm.set_frame_type(FrameType::Skip);
932 return Ok(frm.into_ref());
934 validate!(((size2 + 7) >> 3) <= size);
935 self.header.vq_offset = br.read_byte()?;
937 let checksum = br.read_u16le()?;
938 let height = br.read_u16le()?;
939 let width = br.read_u16le()?;
940 validate!((width >= 16) && (width <= 640));
941 validate!((height >= 16) && (height <= 480));
942 validate!(((width & 3) == 0) && ((height & 3) == 0));
943 if !self.ign_size && (width != self.width || height != self.height) {
945 self.height = height;
946 self.frame0.alloc(width as usize, height as usize);
947 self.frame1.alloc(width as usize, height as usize);
948 let newinfo = NAVideoInfo::new(width as usize, height as usize, false, formats::YUV410_FORMAT);
949 self.info = NACodecInfo::new_ref(self.info.get_name(), NACodecTypeInfo::Video(newinfo), self.info.get_extradata()).into_ref();
952 let yoff = br.read_u32le()?;
953 let voff = br.read_u32le()?;
954 let uoff = br.read_u32le()?;
955 validate!(yoff <= size && uoff <= size && voff <= size);
958 br.read_buf(&mut self.header.alt_quant)?;
960 let mut yend = src.len() as u32;//size;
961 if (uoff < yend) && (uoff > yoff) { yend = uoff; }
962 if (voff < yend) && (voff > yoff) { yend = voff; }
964 if (yoff < uend) && (yoff > uoff) { uend = yoff; }
965 if (voff < uend) && (voff > uoff) { uend = voff; }
967 if (yoff < vend) && (yoff > voff) { vend = yoff; }
968 if (uoff < vend) && (uoff > voff) { vend = uoff; }
970 let intra_frame = (flags & FLAG_KEYFRAME) != 0;
972 let bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 4)?;
973 let mut buf = bufinfo.get_vbuf().unwrap();
975 let ystart = data_start + u64::from(yoff);
976 let ustart = data_start + u64::from(uoff);
977 let vstart = data_start + u64::from(voff);
978 let yendpos = data_start + u64::from(yend);
979 let uendpos = data_start + u64::from(uend);
980 let vendpos = data_start + u64::from(vend);
982 self.header.data_start = [ystart, ustart, vstart];
983 self.header.data_end = [yendpos, uendpos, vendpos];
984 self.header.is_intra = intra_frame;
986 let (cur_frame, ref_frame) = if (flags & FLAG_BUFSEL) != 0 {
987 (&mut self.frame0, &mut self.frame1)
989 (&mut self.frame1, &mut self.frame0)
991 cur_frame.decode_planes(ref_frame, &mut br, &mut self.header, &self.requant_tab)?;
992 cur_frame.output_frame(&mut buf, (flags & FLAG_8BIT) != 0);
993 if self.do_crc && checksum != 0 {
994 let out_checksum = cur_frame.checksum((flags & FLAG_8BIT) != 0);
995 if checksum != out_checksum && checksum.rotate_left(8) != out_checksum {
996 println!("checksum {:04X} / {:04X}", checksum, out_checksum);
997 return Err(DecoderError::ChecksumError);
1001 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
1002 frm.set_keyframe(intra_frame);
1003 frm.set_frame_type(if intra_frame { FrameType::I } else { FrameType::P });
1007 fn flush(&mut self) {
1008 self.frame0.reset();
1009 self.frame1.reset();
1013 const DO_CRC_OPTION: &str = "checksum";
1014 const IGNORE_SIZE_OPTION: &str = "ignore_size_change";
1016 const DECODER_OPTS: &[NAOptionDefinition] = &[
1017 NAOptionDefinition {
1018 name: DO_CRC_OPTION, description: "Verify frame checksum",
1019 opt_type: NAOptionDefinitionType::Bool },
1020 NAOptionDefinition {
1021 name: IGNORE_SIZE_OPTION, description: "Ignore dimensions provided in the frame header",
1022 opt_type: NAOptionDefinitionType::Bool },
1025 impl NAOptionHandler for Indeo3Decoder {
1026 fn get_supported_options(&self) -> &[NAOptionDefinition] { DECODER_OPTS }
1027 fn set_options(&mut self, options: &[NAOption]) {
1028 for option in options.iter() {
1029 for opt_def in DECODER_OPTS.iter() {
1030 if opt_def.check(option).is_ok() {
1033 if let NAValue::Bool(val) = option.value {
1037 IGNORE_SIZE_OPTION => {
1038 if let NAValue::Bool(val) = option.value {
1039 self.ign_size = val;
1048 fn query_option_value(&self, name: &str) -> Option<NAValue> {
1050 DO_CRC_OPTION => Some(NAValue::Bool(self.do_crc)),
1051 IGNORE_SIZE_OPTION => Some(NAValue::Bool(self.ign_size)),
1057 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
1058 Box::new(Indeo3Decoder::new())
1063 use nihav_core::codecs::RegisteredDecoders;
1064 use nihav_core::demuxers::RegisteredDemuxers;
1065 use nihav_codec_support::test::dec_video::*;
1066 use crate::indeo_register_all_decoders;
1067 use nihav_commonfmt::generic_register_all_demuxers;
1069 fn test_indeo3_decoder() {
1070 let mut dmx_reg = RegisteredDemuxers::new();
1071 generic_register_all_demuxers(&mut dmx_reg);
1072 let mut dec_reg = RegisteredDecoders::new();
1073 indeo_register_all_decoders(&mut dec_reg);
1075 // sample: https://samples.mplayerhq.hu/V-codecs/IV32/iv32_example.avi
1076 test_decoding("avi", "indeo3", "assets/Indeo/iv32_example.avi", Some(10),
1077 &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![
1078 [0xd710b489, 0xbeee8f99, 0xfbe34549, 0xaf5805af],
1079 [0x2b56ad73, 0x1faea777, 0xed480d09, 0x801e5185],
1080 [0x06baa992, 0x74eef5fa, 0xf9d39fb2, 0xfac872ae],
1081 [0xadceb016, 0x1fbd67f9, 0xba3e6621, 0xd822a026],
1082 [0x052244b7, 0x1e3bd7bb, 0xd5ad10cf, 0x9177dc3e],
1083 [0x84cca4bc, 0x19ac192f, 0xb9281be7, 0x7ad6193e],
1084 [0xcab74cf9, 0xd7c77c2a, 0x848cbfc9, 0x604a2718],
1085 [0xe6d65b3b, 0x3f3ea0e1, 0x383cad01, 0x0788f3ac],
1086 [0xb25d9b0c, 0xc784bf67, 0x6e86991d, 0x7c2d9a14],
1087 [0x8c70aeae, 0xf95369a1, 0x31d60201, 0xe7e4acdb],
1088 [0x5c63f1bb, 0x32ce48a4, 0x226d112e, 0x440a5bba]]));