X-Git-Url: https://git.nihav.org/?p=nihav.git;a=blobdiff_plain;f=nihav-realmedia%2Fsrc%2Fcodecs%2Frv20.rs;h=88b1a600a265cb348d8f0264317ece12703acb6a;hp=b3f1e560e0e2e1ee64ccc6c3f4239dfcea264b1d;hb=78fb6560c73965d834b215fb0b49505ae5443288;hpb=e64739f87a35f29be0bbbce366876180ba3eb57e diff --git a/nihav-realmedia/src/codecs/rv20.rs b/nihav-realmedia/src/codecs/rv20.rs index b3f1e56..88b1a60 100644 --- a/nihav-realmedia/src/codecs/rv20.rs +++ b/nihav-realmedia/src/codecs/rv20.rs @@ -3,10 +3,12 @@ use nihav_core::io::codebook::*; use nihav_core::formats; use nihav_core::frame::*; use nihav_core::codecs::*; -use nihav_core::codecs::h263::*; -use nihav_core::codecs::h263::code::H263BlockDSP; -use nihav_core::codecs::h263::decoder::*; -use nihav_core::codecs::h263::data::*; +use nihav_codec_support::codecs::{MV, ZIGZAG}; +use nihav_codec_support::codecs::blockdsp; +use nihav_codec_support::codecs::h263::*; +use nihav_codec_support::codecs::h263::code::{H263_INTERP_FUNCS, H263_INTERP_AVG_FUNCS, h263_filter_row}; +use nihav_codec_support::codecs::h263::decoder::*; +use nihav_codec_support::codecs::h263::data::*; #[allow(dead_code)] @@ -37,7 +39,10 @@ struct RealVideo20Decoder { h: usize, minor_ver: u8, rpr: RPRInfo, - bdsp: H263BlockDSP, + bdsp: Box, + base_ts: u64, + last_ts: u16, + next_ts: u16, } struct RealVideo20BR<'a> { @@ -53,6 +58,7 @@ struct RealVideo20BR<'a> { mb_pos_bits: u8, minor_ver: u8, rpr: RPRInfo, + pts: u16, } struct RV20SliceInfo { @@ -64,12 +70,139 @@ struct RV20SliceInfo { mb_pos: usize, w: usize, h: usize, + loop_filter: bool, } +#[derive(Default)] +struct RV20BlockDSP {} + impl RV20SliceInfo { - fn new(ftype: Type, seq: u32, qscale: u8, mb_x: usize, mb_y: usize, mb_pos: usize, w: usize, h: usize) -> Self { - RV20SliceInfo { ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h } + fn new(ftype: Type, seq: u32, qscale: u8, mb_x: usize, mb_y: usize, mb_pos: usize, w: usize, h: usize, loop_filter: bool) -> Self { + RV20SliceInfo { ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h, loop_filter } + } +} + +macro_rules! idct { + ($src: expr, $sstep: expr, $dst: expr, $dstep: expr, $bias: expr, $shift: expr, $dtype: tt) => { + let s0 = $src[0] as i32; + let s1 = $src[$sstep] as i32; + let s2 = $src[$sstep * 2] as i32; + let s3 = $src[$sstep * 3] as i32; + let s4 = $src[$sstep * 4] as i32; + let s5 = $src[$sstep * 5] as i32; + let s6 = $src[$sstep * 6] as i32; + let s7 = $src[$sstep * 7] as i32; + + let t0 = (s0 + s4).wrapping_mul(1448); + let t1 = (s0 - s4).wrapping_mul(1448); + let t2 = s2.wrapping_mul(1892) + s6.wrapping_mul(784); + let t3 = s2.wrapping_mul(784) - s6.wrapping_mul(1892); + let t4 = s1.wrapping_mul(2009) + s3.wrapping_mul(1703) + + s5.wrapping_mul(1138) + s7.wrapping_mul(400); + let t5 = s1.wrapping_mul(1703) - s3.wrapping_mul(400) + - s5.wrapping_mul(2009) - s7.wrapping_mul(1138); + let t6 = s1.wrapping_mul(1138) - s3.wrapping_mul(2009) + + s5.wrapping_mul(400) + s7.wrapping_mul(1703); + let t7 = s1.wrapping_mul(400) - s3.wrapping_mul(1138) + + s5.wrapping_mul(1703) - s7.wrapping_mul(2009); + + let t8 = t0 + t2; + let t9 = t0 - t2; + let ta = t1 + t3; + let tb = t1 - t3; + + $dst[0] = ((t8 + t4 + $bias) >> $shift) as $dtype; + $dst[$dstep] = ((ta + t5 + $bias) >> $shift) as $dtype; + $dst[$dstep * 2] = ((tb + t6 + $bias) >> $shift) as $dtype; + $dst[$dstep * 3] = ((t9 + t7 + $bias) >> $shift) as $dtype; + $dst[$dstep * 4] = ((t9 - t7 + $bias) >> $shift) as $dtype; + $dst[$dstep * 5] = ((tb - t6 + $bias) >> $shift) as $dtype; + $dst[$dstep * 6] = ((ta - t5 + $bias) >> $shift) as $dtype; + $dst[$dstep * 7] = ((t8 - t4 + $bias) >> $shift) as $dtype; + } +} + +impl BlockDSP for RV20BlockDSP { + fn idct(&self, blk: &mut [i16; 64]) { + let mut tmp = [0i32; 64]; + for (dst, src) in tmp.chunks_mut(8).zip(blk.chunks(8)) { + idct!(src, 1, dst, 1, 0, 4, i32); + } + for i in 0..8 { + idct!(&tmp[i..], 8, &mut blk[i..], 8, 1 << 19, 20, i16); + } + } + fn copy_blocks(&self, dst: &mut NAVideoBuffer, src: NAVideoBufferRef, xpos: usize, ypos: usize, mv: MV) { + let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize; + let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 }); + + let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); + + blockdsp::copy_block(&mut dst, src.clone(), 0, xpos, ypos, mv.x >> 1, mv.y >> 1, 16, 16, 0, 1, mode, H263_INTERP_FUNCS); + blockdsp::copy_block(&mut dst, src.clone(), 1, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_FUNCS); + blockdsp::copy_block(&mut dst, src.clone(), 2, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_FUNCS); + } + fn copy_blocks8x8(&self, dst: &mut NAVideoBuffer, src: NAVideoBufferRef, xpos: usize, ypos: usize, mvs: &[MV; 4]) { + let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); + + for i in 0..4 { + let xadd = (i & 1) * 8; + let yadd = (i & 2) * 4; + let mode = ((mvs[i].x & 1) + (mvs[i].y & 1) * 2) as usize; + + blockdsp::copy_block(&mut dst, src.clone(), 0, xpos + xadd, ypos + yadd, mvs[i].x >> 1, mvs[i].y >> 1, 8, 8, 0, 1, mode, H263_INTERP_FUNCS); + } + + let sum_mv = mvs[0] + mvs[1] + mvs[2] + mvs[3]; + let cmx = (sum_mv.x >> 3) + H263_CHROMA_ROUND[(sum_mv.x & 0xF) as usize]; + let cmy = (sum_mv.y >> 3) + H263_CHROMA_ROUND[(sum_mv.y & 0xF) as usize]; + let mode = ((cmx & 1) + (cmy & 1) * 2) as usize; + for plane in 1..3 { + blockdsp::copy_block(&mut dst, src.clone(), plane, xpos >> 1, ypos >> 1, cmx >> 1, cmy >> 1, 8, 8, 0, 1, mode, H263_INTERP_FUNCS); + } + } + fn avg_blocks(&self, dst: &mut NAVideoBuffer, src: NAVideoBufferRef, xpos: usize, ypos: usize, mv: MV) { + let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize; + let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 }); + + let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); + + blockdsp::copy_block(&mut dst, src.clone(), 0, xpos, ypos, mv.x >> 1, mv.y >> 1, 16, 16, 0, 1, mode, H263_INTERP_AVG_FUNCS); + blockdsp::copy_block(&mut dst, src.clone(), 1, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_AVG_FUNCS); + blockdsp::copy_block(&mut dst, src.clone(), 2, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_AVG_FUNCS); } + fn avg_blocks8x8(&self, dst: &mut NAVideoBuffer, src: NAVideoBufferRef, xpos: usize, ypos: usize, mvs: &[MV; 4]) { + let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); + + for i in 0..4 { + let xadd = (i & 1) * 8; + let yadd = (i & 2) * 4; + let mode = ((mvs[i].x & 1) + (mvs[i].y & 1) * 2) as usize; + + blockdsp::copy_block(&mut dst, src.clone(), 0, xpos + xadd, ypos + yadd, mvs[i].x >> 1, mvs[i].y >> 1, 8, 8, 0, 1, mode, H263_INTERP_AVG_FUNCS); + } + + let sum_mv = mvs[0] + mvs[1] + mvs[2] + mvs[3]; + let cmx = (sum_mv.x >> 3) + H263_CHROMA_ROUND[(sum_mv.x & 0xF) as usize]; + let cmy = (sum_mv.y >> 3) + H263_CHROMA_ROUND[(sum_mv.y & 0xF) as usize]; + let mode = ((cmx & 1) + (cmy & 1) * 2) as usize; + for plane in 1..3 { + blockdsp::copy_block(&mut dst, src.clone(), plane, xpos >> 1, ypos >> 1, cmx >> 1, cmy >> 1, 8, 8, 0, 1, mode, H263_INTERP_AVG_FUNCS); + } + } + fn filter_row(&self, buf: &mut NAVideoBuffer, mb_y: usize, mb_w: usize, cbpi: &CBPInfo) { + h263_filter_row(buf, mb_y, mb_w, cbpi) + } +} + +fn get_mb_pos_bits(mb_w: usize, mb_h: usize) -> u8 { + let max_pos = mb_w * mb_h - 1; + for i in 0..H263_MBB.len() { + if max_pos <= H263_MBB[i].blocks { + return H263_MBB[i].bits; + } + } + 0 } impl<'a> RealVideo20BR<'a> { @@ -88,14 +221,6 @@ impl<'a> RealVideo20BR<'a> { let soff = nslices * 8 + 1; let mb_w = (width + 15) >> 4; let mb_h = (height + 15) >> 4; - let max_pos = mb_w * mb_h - 1; - let mut mbpb = 0; - for i in 0..H263_MBB.len() { - if max_pos <= H263_MBB[i].blocks { - mbpb = H263_MBB[i].bits; - break; - } - } RealVideo20BR { br: BitReader::new(&src[soff..], BitReaderMode::BE), tables, @@ -106,9 +231,10 @@ impl<'a> RealVideo20BR<'a> { h: height, mb_w, mb_h, - mb_pos_bits: mbpb, + mb_pos_bits: get_mb_pos_bits(mb_w, mb_h), minor_ver, rpr, + pts: 0, } } @@ -130,8 +256,8 @@ impl<'a> RealVideo20BR<'a> { }; let rl_cb = if sstate.is_iframe { &self.tables.aic_rl_cb } else { &self.tables.rl_cb }; - let q_add = if quant == 0 || sstate.is_iframe { 0i16 } else { ((quant - 1) | 1) as i16 }; - let q = if plane_no == 0 { (quant * 2) as i16 } else { H263_CHROMA_QUANT[quant as usize] as i16 }; + let q = if plane_no == 0 { (quant * 2) as i16 } else { (H263_CHROMA_QUANT[quant as usize] * 2) as i16 }; + let q_add = if q == 0 || sstate.is_iframe { 0i16 } else { (((q >> 1) - 1) | 1) as i16 }; while idx < 64 { let code = br.read_cb(rl_cb)?; let run; @@ -142,7 +268,11 @@ impl<'a> RealVideo20BR<'a> { level = code.get_level(); last = code.is_last(); if br.read_bool()? { level = -level; } - level = (level * q) + q_add; + if level >= 0 { + level = (level * q) + q_add; + } else { + level = (level * q) - q_add; + } } else { last = br.read_bool()?; run = br.read(6)? as u8; @@ -152,7 +282,11 @@ impl<'a> RealVideo20BR<'a> { let top = br.read_s(6)? as i16; level = (top << 5) | low; } - level = (level * q) + q_add; + if level >= 0 { + level = (level * q) + q_add; + } else { + level = (level * q) - q_add; + } if level < -2048 { level = -2048; } if level > 2047 { level = 2047; } } @@ -209,7 +343,7 @@ impl<'a> BlockDecoder for RealVideo20BR<'a> { mb_count = self.mb_w * self.mb_h; }*/ - let plusinfo = Some(PlusInfo::new(shdr.ftype == Type::I, false, false, false)); + let plusinfo = Some(PlusInfo::new(shdr.ftype == Type::I, shdr.loop_filter, false, false)); let picinfo = PicInfo::new(shdr.w, shdr.h, shdr.ftype, MVMode::Long, false, false, shdr.qscale, shdr.seq as u16, None, plusinfo); Ok(picinfo) } @@ -222,6 +356,8 @@ impl<'a> BlockDecoder for RealVideo20BR<'a> { if self.slice_no < self.num_slices { let pos = self.br.tell(); let shdr2 = self.read_slice_header()?; + validate!(shdr2.mb_pos > shdr.mb_pos); + validate!(shdr2.w == shdr.w && shdr2.h == shdr.h); mb_count = shdr2.mb_pos - shdr.mb_pos; self.br.seek(pos as u32)?; } else { @@ -373,14 +509,18 @@ impl<'a> RealVideo20BR<'a> { validate!(marker == 0); let qscale = br.read(5)? as u8; validate!(qscale > 0); + let loop_filter; if self.minor_ver >= 2 { - br.skip(1)?; // loop filter + loop_filter = br.read_bool()?; + } else { + loop_filter = false; } let seq = if self.minor_ver <= 1 { br.read(8)? << 8 } else { br.read(13)? << 3 }; + self.pts = seq as u16; let w; let h; if self.rpr.present { @@ -393,6 +533,9 @@ impl<'a> RealVideo20BR<'a> { h = self.rpr.heights[rpr - 1]; validate!((w != 0) && (h != 0)); } + self.mb_w = (w + 15) >> 4; + self.mb_h = (h + 15) >> 4; + self.mb_pos_bits = get_mb_pos_bits(self.mb_w, self.mb_h); } else { w = self.w; h = self.h; @@ -408,7 +551,7 @@ impl<'a> RealVideo20BR<'a> { br.skip(5)?; } - Ok(RV20SliceInfo::new(ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h)) + Ok(RV20SliceInfo::new(ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h, loop_filter)) } } @@ -450,7 +593,10 @@ impl RealVideo20Decoder { h: 0, minor_ver: 0, rpr: RPRInfo { present: false, bits: 0, widths: [0; 8], heights: [0; 8] }, - bdsp: H263BlockDSP::new(), + bdsp: Box::new(RV20BlockDSP::default()), + base_ts: 0, + last_ts: 0, + next_ts: 0, } } } @@ -480,10 +626,11 @@ impl NADecoder for RealVideo20Decoder { self.rpr.present = false; } else { self.rpr.present = true; - self.rpr.bits = ((rprb >> 1) + 1) as u8; - for i in 4..(src.len()/2) { - self.rpr.widths [i - 4] = (src[i * 2] as usize) * 4; - self.rpr.heights[i - 4] = (src[i * 2 + 1] as usize) * 4; + self.rpr.bits = ((rprb >> 1) + 1).min(3) as u8; + let num_dim = ((src.len() - 8) / 2).min(self.rpr.widths.len() - 1); + for i in 0..num_dim { + self.rpr.widths [i] = (src[i * 2 + 8] as usize) * 4; + self.rpr.heights[i] = (src[i * 2 + 9] as usize) * 4; } } Ok(()) @@ -496,11 +643,23 @@ impl NADecoder for RealVideo20Decoder { let mut ibr = RealVideo20BR::new(&src, &self.tables, self.w, self.h, self.minor_ver, self.rpr); - let bufinfo = self.dec.parse_frame(&mut ibr, &self.bdsp)?; + let bufinfo = self.dec.parse_frame(&mut ibr, self.bdsp.as_ref())?; let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); + let ftype = self.dec.get_frame_type(); + let pts = ibr.pts; + if ftype != FrameType::B { + self.last_ts = self.next_ts; + self.next_ts = pts; + if self.last_ts > self.next_ts { + self.base_ts += 1 << 16; + } + } + let ts_diff = self.next_ts.wrapping_sub(pts); + let ts = self.base_ts + (self.next_ts as u64) - (ts_diff as u64); frm.set_keyframe(self.dec.is_intra()); - frm.set_frame_type(self.dec.get_frame_type()); + frm.set_frame_type(ftype); + frm.set_pts(Some(ts >> 3)); Ok(frm.into_ref()) } fn flush(&mut self) { @@ -508,6 +667,12 @@ impl NADecoder for RealVideo20Decoder { } } +impl NAOptionHandler for RealVideo20Decoder { + fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } + fn set_options(&mut self, _options: &[NAOption]) { } + fn query_option_value(&self, _name: &str) -> Option { None } +} + struct MBB { blocks: usize, bits: u8 } const H263_MBB: &[MBB; 7] = &[ MBB{ blocks: 47, bits: 6 }, @@ -527,17 +692,39 @@ pub fn get_decoder() -> Box { mod test { use nihav_core::codecs::RegisteredDecoders; use nihav_core::demuxers::RegisteredDemuxers; - use nihav_core::test::dec_video::*; - use crate::realmedia_register_all_codecs; + use nihav_codec_support::test::dec_video::*; + use crate::realmedia_register_all_decoders; use crate::realmedia_register_all_demuxers; #[test] fn test_rv20() { let mut dmx_reg = RegisteredDemuxers::new(); realmedia_register_all_demuxers(&mut dmx_reg); let mut dec_reg = RegisteredDecoders::new(); - realmedia_register_all_codecs(&mut dec_reg); - - test_file_decoding("realmedia", "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm", /*None*/Some(3000), true, false, None/*Some("rv20")*/, &dmx_reg, &dec_reg); -// test_file_decoding("realmedia", "assets/RV/rv20_cook_640x352_realproducer_plus_8.51.rm", /*None*/Some(1000), true, false, Some("rv20")); + realmedia_register_all_decoders(&mut dec_reg); + + test_decoding("realmedia", "realvideo2", + "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm", + Some(1000), &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![ + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x8ce88686, 0x03ca3bb9, 0x0d18347b, 0xccdb0bc5], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], + [0xa2008d4c, 0xf4684b3a, 0xecd0526c, 0xf0742a77], + [0x5eef6cfd, 0x4eb49f19, 0x4d760b7a, 0x741ccd0c], + [0x0e0529df, 0xf1cc3f03, 0x03986b0d, 0xd2033c08], + [0xdb9aa091, 0xf2c6345c, 0xde9deae8, 0x71f51a67], + [0x22c978cf, 0x6887a9ba, 0xe74c9316, 0x8cbdd29b], + [0x7c83c784, 0x15b20881, 0x74798f24, 0x2096573c], + [0x84a68aed, 0x4cfcefb1, 0x78d1b66b, 0x21b1860a], + [0x78ed094a, 0x8df72434, 0x58bcd64d, 0x8d725dfd]])); } }