X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-indeo%2Fsrc%2Fcodecs%2Fivibr.rs;h=1383f7409dec3f1b72f88fbcc41b5bd13fbe7072;hb=e6aaad5c5273cd814b5748b7faf3751835a37217;hp=8ce4d1b03d1729538c4447b6cc0417e488f45cb1;hpb=379524159c95f1c3639976ccf35f9d47cd9732ac;p=nihav.git diff --git a/nihav-indeo/src/codecs/ivibr.rs b/nihav-indeo/src/codecs/ivibr.rs index 8ce4d1b..1383f74 100644 --- a/nihav-indeo/src/codecs/ivibr.rs +++ b/nihav-indeo/src/codecs/ivibr.rs @@ -258,7 +258,7 @@ fn decode_block8x8(br: &mut BitReader, blk_cb: &IVICodebook, rvmap: &RVMap, tabl } } idx += run; - validate!((idx >= 0) && (idx < 64)); + validate!((0..64).contains(&idx)); let spos = tables.scan[idx as usize]; let q = (u32::from(quant_mat[spos]) * u32::from(quant)) >> 9; @@ -308,7 +308,7 @@ fn decode_block4x4(br: &mut BitReader, blk_cb: &IVICodebook, rvmap: &RVMap, tabl } } idx += run; - validate!((idx >= 0) && (idx < 16)); + validate!((0..16).contains(&idx)); let spos = tables.scan[idx as usize]; let q = (u32::from(quant_mat[spos]) * u32::from(quant)) >> 9; @@ -484,13 +484,16 @@ pub struct IVIDecoder { bref: Option, bands: Vec, + band_tiles: usize, tiles: Vec, num_tiles: [[usize; 4]; 4], tile_start: [[usize; 4]; 4], + + scalable: bool, } impl IVIDecoder { - pub fn new() -> Self { + pub fn new(scalable: bool) -> Self { let mut bands: Vec = Vec::with_capacity(12); bands.resize(12, BandHeader::new_empty(42, 42)); IVIDecoder { @@ -503,7 +506,10 @@ impl IVIDecoder { bref: None, bands, + band_tiles: 0, tiles: Vec::new(), tile_start: [[0; 4]; 4], num_tiles: [[0; 4]; 4], + + scalable, } } @@ -530,6 +536,9 @@ impl IVIDecoder { tile_h = (tile_h + 1) >> 1; } } + if plane == 0 { + self.band_tiles = ((band_w + tile_w - 1) / tile_w) * ((band_h + tile_h - 1) / tile_h); + } for band in 0..bands { self.tile_start[plane][band] = tstart; let band_xoff = if (band & 1) == 1 { band_w } else { 0 }; @@ -596,7 +605,7 @@ impl IVIDecoder { } br.align(); validate!(len > 0); - let tile_end = tile_start + len * 8; + let tile_end = (tile_start & !7) + len * 8; validate!(tile_end > br.tell()); validate!(tile_end <= br.tell() + (br.left() as usize)); { @@ -610,7 +619,7 @@ impl IVIDecoder { let (ref_tiles, cur_tiles) = self.tiles.split_at_mut(tile_no); let tile = &mut cur_tiles[0]; if plane_no != 0 || band_no != 0 { - let rtile = &ref_tiles[0]; + let rtile = &ref_tiles[tile_no % self.band_tiles]; if (tile.mb_w != rtile.mb_w) || (tile.mb_h != rtile.mb_h) { ref_tile = None; } else { @@ -623,6 +632,7 @@ impl IVIDecoder { } self.decode_tile(br, &band, tile_no, tr, tr_dc)?; + br.align(); let skip_part = tile_end - br.tell(); br.skip(skip_part as u32)?; } else { @@ -653,7 +663,7 @@ br.skip(skip_part as u32)?; mb.mtype = MBType::Inter; mb.cbp = 0; if band.inherit_mv { - if let Some(ref tileref) = ref_tile { + if let Some(tileref) = ref_tile { let mx = tileref.mb[mb_idx].mv_x; let my = tileref.mb[mb_idx].mv_y; mb.mv_x = scale_mv(mx, mv_scale); @@ -680,7 +690,7 @@ br.skip(skip_part as u32)?; let stride = frame.plane_stride[band.plane_no]; let mut dstidx = tile.pos_x + tile.pos_y * stride; - let mut dst = &mut frame.plane_buf[band.plane_no]; + let dst = &mut frame.plane_buf[band.plane_no]; let pos_x = tile.pos_x; let pos_y = tile.pos_y; let tile_w = (tile.w + 15) & !15; @@ -728,23 +738,23 @@ br.skip(skip_part as u32)?; if let TxType::Transform8(ref params) = band.ttype { decode_block8x8(br, &band.blk_cb, &band.rvmap, params, is_intra, band.tr.is_2d(), &mut prev_dc, mb.q, &mut blk, tr)?; if is_intra { - put_block(&mut dst, dstidx + boff, stride, &blk, 8); + put_block(dst, dstidx + boff, stride, &blk, 8); } else { - add_block(&mut dst, dstidx + boff, stride, &blk, 8); + add_block(dst, dstidx + boff, stride, &blk, 8); } } if let TxType::Transform4(ref params) = band.ttype { decode_block4x4(br, &band.blk_cb, &band.rvmap, params, is_intra, band.tr.is_2d(), &mut prev_dc, mb.q, &mut blk, tr)?; if is_intra { - put_block(&mut dst, dstidx + boff, stride, &blk, 4); + put_block(dst, dstidx + boff, stride, &blk, 4); } else { - add_block(&mut dst, dstidx + boff, stride, &blk, 4); + add_block(dst, dstidx + boff, stride, &blk, 4); } } } else { if is_intra { (transform_dc)(&mut blk, prev_dc); - put_block(&mut dst, dstidx + boff, stride, &blk, band.blk_size); + put_block(dst, dstidx + boff, stride, &blk, band.blk_size); } } cbp >>= 1; @@ -788,24 +798,24 @@ br.skip(skip_part as u32)?; } if is_intra { if band.blk_size == 8 { - put_block(&mut dst, dstidx + mb_x * band.blk_size, stride, &blk, 8); + put_block(dst, dstidx + mb_x * band.blk_size, stride, &blk, 8); } else { - put_block(&mut dst, dstidx + mb_x * band.blk_size, stride, &blk, 4); + put_block(dst, dstidx + mb_x * band.blk_size, stride, &blk, 4); } } else { if band.blk_size == 8 { - add_block(&mut dst, dstidx + mb_x * band.blk_size, stride, &blk, 8); + add_block(dst, dstidx + mb_x * band.blk_size, stride, &blk, 8); } else { - add_block(&mut dst, dstidx + mb_x * band.blk_size, stride, &blk, 4); + add_block(dst, dstidx + mb_x * band.blk_size, stride, &blk, 4); } } } else { if is_intra { (transform_dc)(&mut blk, prev_dc); if band.blk_size == 8 { - put_block(&mut dst, dstidx + mb_x * band.blk_size, stride, &blk, 8); + put_block(dst, dstidx + mb_x * band.blk_size, stride, &blk, 8); } else { - put_block(&mut dst, dstidx + mb_x * band.blk_size, stride, &blk, 4); + put_block(dst, dstidx + mb_x * band.blk_size, stride, &blk, 4); } } } @@ -814,7 +824,6 @@ br.skip(skip_part as u32)?; } dstidx += stride * band.mb_size; } - br.align(); Ok(()) } @@ -827,7 +836,7 @@ br.skip(skip_part as u32)?; unreachable!(); } - fn decode_single_frame<'a>(&mut self, dec: &mut dyn IndeoXParser, br: &mut BitReader<'a>) -> DecoderResult { + fn decode_single_frame(&mut self, dec: &mut dyn IndeoXParser, br: &mut BitReader) -> DecoderResult { let pic_hdr = dec.decode_picture_header(br)?; self.ftype = pic_hdr.ftype; if pic_hdr.ftype.is_null() { @@ -871,22 +880,77 @@ br.skip(skip_part as u32)?; self.realloc(&pic_hdr)?; self.frames[self.cur_frame].realloc(&pic_hdr)?; - for plane in 0..3 { - let num_bands = if plane == 0 { pic_hdr.luma_bands } else { pic_hdr.chroma_bands }; - for band in 0..num_bands { - self.decode_band(&pic_hdr, dec, br, plane, band)?; + if !self.scalable { + for plane in 0..3 { + let num_bands = if plane == 0 { pic_hdr.luma_bands } else { pic_hdr.chroma_bands }; + for band in 0..num_bands { + self.decode_band(&pic_hdr, dec, br, plane, band)?; + } + if let NABufferType::Video(ref mut vb) = buftype { + let mut frame = self.frames[self.cur_frame].clone(); + if num_bands == 1 { + frame.fill_plane(vb, plane); + } else { + let dplane = if (plane == 1) || (plane == 2) { plane ^ 3 } else { plane }; + let (w, h) = vb.get_dimensions(dplane); + let dstride = vb.get_stride(dplane); + let off = vb.get_offset(dplane); + let dst = vb.get_data_mut().unwrap(); + dec.recombine_plane(&frame.plane_buf[plane], frame.plane_stride[plane], &mut dst[off..], dstride, w, h); + } + } + } + } else { + let mut bands_decoded = [[false; 10]; 3]; + let mut num_decoded = 0; + + let num_bands = [ pic_hdr.luma_bands, pic_hdr.chroma_bands, pic_hdr.chroma_bands ]; + + for plane in 0..3 { + for band in 0..num_bands[plane] { + if br.peek(8) == 0x01 { // skipped scalable bands + br.skip(8)?; + continue; + } + self.decode_band(&pic_hdr, dec, br, plane, band)?; + bands_decoded[plane][band] = true; + num_decoded += 1; + } + } + if (num_decoded < num_bands[0] + num_bands[1] + num_bands[2]) && (br.left() > 0) { + validate!(br.read(8)? == 0x00); + while br.peek(8) == 0x00 { + if br.skip(8).is_err() { // happens at the end of data + break; + } + } + validate!((br.tell() & 31) == 0); + + for plane in 0..3 { + for band in 0..num_bands[plane] { + if bands_decoded[plane][band] || br.left() == 0 { + continue; + } + self.decode_band(&pic_hdr, dec, br, plane, band)?; + bands_decoded[plane][band] = true; + num_decoded += 1; + } + } } + if let NABufferType::Video(ref mut vb) = buftype { - let mut frame = self.frames[self.cur_frame].clone(); - if num_bands == 1 { - frame.fill_plane(vb, plane); - } else { - let dplane = if (plane == 1) || (plane == 2) { plane ^ 3 } else { plane }; - let (w, h) = vb.get_dimensions(dplane); - let dstride = vb.get_stride(dplane); - let off = vb.get_offset(dplane); - let dst = vb.get_data_mut().unwrap(); - dec.recombine_plane(&frame.plane_buf[plane], frame.plane_stride[plane], &mut dst[off..], dstride, w, h); + for plane in 0..3 { + let mut frame = self.frames[self.cur_frame].clone(); + if num_bands[plane] == 1 { + frame.fill_plane(vb, plane); + } else { + let dplane = if (plane == 1) || (plane == 2) { plane ^ 3 } else { plane }; + let (w, h) = vb.get_dimensions(dplane); + let dstride = vb.get_stride(dplane); + let off = vb.get_offset(dplane); + let dst = vb.get_data_mut().unwrap(); + dec.recombine_plane(&frame.plane_buf[plane], frame.plane_stride[plane], &mut dst[off..], dstride, w, h); + } } } } @@ -914,9 +978,8 @@ br.skip(skip_part as u32)?; Ok(buftype) } - pub fn decode_frame<'a>(&mut self, dec: &mut dyn IndeoXParser, br: &mut BitReader<'a>) -> DecoderResult { - let res = self.decode_single_frame(dec, br); - if res.is_err() { return res; } + pub fn decode_frame(&mut self, dec: &mut dyn IndeoXParser, br: &mut BitReader) -> DecoderResult { + let res = self.decode_single_frame(dec, br)?; if (self.ftype == IVIFrameType::Intra) && (br.left() > 16) { loop { if br.left() < 8 { break; } @@ -938,7 +1001,12 @@ br.skip(skip_part as u32)?; self.ftype = IVIFrameType::Intra; } } - if let Ok(NABufferType::None) = res { + if self.bref.is_some() && self.ftype == IVIFrameType::Inter { + let mut bref: Option = Some(res); + mem::swap(&mut bref, &mut self.bref); + return Ok(bref.unwrap()); + } + if let NABufferType::None = res { if self.bref.is_some() { let mut bref: Option = None; mem::swap(&mut bref, &mut self.bref); @@ -946,7 +1014,7 @@ br.skip(skip_part as u32)?; return Ok(bref.unwrap()); } } - res + Ok(res) } pub fn flush(&mut self) {