From: Kostya Shishkov Date: Sun, 12 Nov 2017 08:54:13 +0000 (+0100) Subject: RealVideo 1 decoder and RealVideo 2 bitstream parser X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=be91b1c382e773807fa17615934355667403e0f9;p=nihav.git RealVideo 1 decoder and RealVideo 2 bitstream parser --- diff --git a/Cargo.toml b/Cargo.toml index cbcc035..d349590 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ dsp_window = ["dsp"] all_decoders = ["all_video_decoders", "all_audio_decoders"] -all_video_decoders = ["decoder_gdvvid", "decoder_indeo2", "decoder_indeo3", "decoder_indeo4", "decoder_indeo5", "decoder_intel263", "decoder_realvideo1"] +all_video_decoders = ["decoder_gdvvid", "decoder_indeo2", "decoder_indeo3", "decoder_indeo4", "decoder_indeo5", "decoder_intel263", "decoder_realvideo1", "decoder_realvideo2"] decoder_gdvvid = ["decoders"] decoder_indeo2 = ["decoders"] decoder_indeo3 = ["decoders"] @@ -29,6 +29,7 @@ decoder_indeo4 = ["decoders"] decoder_indeo5 = ["decoders"] decoder_intel263 = ["h263", "decoders"] decoder_realvideo1 = ["h263", "decoders"] +decoder_realvideo2 = ["h263", "decoders"] all_audio_decoders = ["decoder_pcm", "decoder_imc"] decoder_pcm = ["decoders"] diff --git a/src/codecs/h263/mod.rs b/src/codecs/h263/mod.rs index 8feda44..a0d21b2 100644 --- a/src/codecs/h263/mod.rs +++ b/src/codecs/h263/mod.rs @@ -11,6 +11,8 @@ pub mod decoder; pub mod intel263; #[cfg(feature="decoder_realvideo1")] pub mod rv10; +#[cfg(feature="decoder_realvideo2")] +pub mod rv20; pub trait BlockDecoder { fn decode_pichdr(&mut self) -> DecoderResult; diff --git a/src/codecs/h263/rv10.rs b/src/codecs/h263/rv10.rs new file mode 100644 index 0000000..f3388c6 --- /dev/null +++ b/src/codecs/h263/rv10.rs @@ -0,0 +1,790 @@ +use io::bitreader::*; +use io::codebook::*; +use formats; +use super::super::*; +use super::*; +use super::decoder::*; +use super::data::*; + +#[allow(dead_code)] +struct Tables { + intra_mcbpc_cb: Codebook, + inter_mcbpc_cb: Codebook, + cbpy_cb: Codebook, + rl_cb: Codebook, + aic_rl_cb: Codebook, + mv_cb: Codebook, + luma_dc_cb: Codebook, + chroma_dc_cb: Codebook, +} + +struct RealVideo10Decoder { + info: Rc, + dec: H263BaseDecoder, + tables: Tables, + w: usize, + h: usize, + new_ver: bool, +} + +struct RealVideo10BR<'a> { + br: BitReader<'a>, + tables: &'a Tables, + num_slices: usize, + slice_no: usize, + slice_off: Vec, + w: usize, + h: usize, + mb_w: usize, + mb_h: usize, + mb_x: usize, + mb_y: usize, + mb_count: usize, + mb_end: usize, + new_ver: bool, + dc_coded: [bool; 3], + last_dc: [i16; 3], + is_intra: bool, +} + +struct RV10SliceInfo { + is_p: bool, + qscale: u8, + mb_x: usize, + mb_y: usize, + mb_c: usize, +} + +impl RV10SliceInfo { + fn new(is_p: bool, qscale: u8, mb_x: usize, mb_y: usize, mb_c: usize) -> Self { + RV10SliceInfo { is_p: is_p, qscale: qscale, mb_x: mb_x, mb_y: mb_y, mb_c: mb_c } + } +} + +impl<'a> RealVideo10BR<'a> { + fn new(src: &'a [u8], tables: &'a Tables, width: usize, height: usize, new_ver: bool) -> Self { + let nslices = (src[0] as usize) + 1; + let mut slice_offs = Vec::with_capacity(nslices); + { + let offs = &src[1..][..nslices * 8]; + let mut br = BitReader::new(offs, offs.len(), BitReaderMode::BE); + for _ in 0..nslices { + br.skip(32).unwrap(); + let off = br.read(32).unwrap(); + slice_offs.push(off); + } + } + let soff = nslices * 8 + 1; + RealVideo10BR { + br: BitReader::new(&src[soff..], src.len() - soff, BitReaderMode::BE), + tables: tables, + num_slices: nslices, + slice_no: 0, + slice_off: slice_offs, + w: width, + h: height, + mb_w: (width + 15) >> 4, + mb_h: (height + 15) >> 4, + mb_x: 0, + mb_y: 0, + mb_count: 0, + mb_end: 0, + new_ver: new_ver, + dc_coded: [false; 3], + last_dc: [0; 3], + is_intra: false, + } + } + + fn decode_block(&mut self, quant: u8, intra: bool, coded: bool, blk: &mut [i16; 64], plane_no: usize) -> DecoderResult<()> { + let mut br = &mut self.br; + let mut idx = 0; + if intra { + let mut dc; +//println!("dc prev {} @ {} of {},{} / {}x{}", self.last_dc[plane_no], br.tell() - (self.slice_off[self.slice_no - 1] as usize) * 8, self.mb_x, self.mb_y, self.mb_w, self.mb_h); + if !self.new_ver || !self.is_intra { + dc = br.read(8)? as i16; + if dc == 255 { dc = 128; } + } else { + if self.dc_coded[plane_no] { + let diff; + let bits = br.peek(14); + let ret = if plane_no == 0 { + br.read_cb(&self.tables.luma_dc_cb) + } else { + br.read_cb(&self.tables.chroma_dc_cb) + }; + if ret.is_err() { + println!("Illegal {} code {:X}", if plane_no==0{"luma"}else{"chroma"},bits); + } + let val = ret.unwrap() as i16; + + if val != 0 { + diff = val - 128; + } else { +println!("escape!!!"); + let code = br.read(2)?; + match code { + 0x0 => { diff = ((br.read(7)? + 1) as i8) as i16; }, + 0x1 => { diff = (br.read(7)? as i16) - 128; }, + 0x2 => { + if plane_no == 0 { + if br.read_bool()? { + diff = ((br.read(8)? + 1) as i8) as i16; + } else { + diff = (br.read(8)? as i8) as i16; + } + } else { + br.skip(9)?; + diff = 1; + } + }, + _ => { + if plane_no == 0 { + br.skip(4)?; + diff = 1; + } else { +println!("!!! wrong chroma esc"); + return Err(DecoderError::InvalidData); + } + }, + }; +println!("!!! diff = {} @ {}", diff, br.tell()); + } + dc = (self.last_dc[plane_no] - diff) & 0xFF; + self.last_dc[plane_no] = dc; + } else { + self.dc_coded[plane_no] = true; + dc = self.last_dc[plane_no]; + } + } +//println!("dc new {} @ {}", self.last_dc[plane_no], br.tell()); + blk[0] = dc << 3; + idx = 1; + } + if !coded { return Ok(()); } + + let rl_cb = &self.tables.rl_cb; // could be aic too + let q_add = if quant == 0 { 0i16 } else { ((quant - 1) | 1) as i16 }; + let q = (quant * 2) as i16; + while idx < 64 { + let code = br.read_cb(rl_cb)?; + let run; + let mut level; + let last; + if !code.is_escape() { + run = code.get_run(); + level = code.get_level(); + last = code.is_last(); + if br.read_bool()? { level = -level; } + level = (level * q) + q_add; + } else { + last = br.read_bool()?; + run = br.read(6)? as u8; + level = br.read_s(8)? as i16; + if level == -128 { + let low = br.read(5)? as i16; + let top = br.read_s(6)? as i16; + level = (top << 5) | low; + } + level = (level * q) + q_add; + if level < -2048 { level = -2048; } + if level > 2047 { level = 2047; } + } + idx += run; + validate!(idx < 64); + let oidx = H263_ZIGZAG[idx as usize]; + blk[oidx] = level; + idx += 1; + if last { break; } + } + Ok(()) + } +} + +fn decode_mv_component(br: &mut BitReader, mv_cb: &Codebook) -> DecoderResult { + let code = br.read_cb(mv_cb)? as i16; + if code == 0 { return Ok(0) } + if !br.read_bool()? { + Ok(code) + } else { + Ok(-code) + } +} + +fn decode_mv(br: &mut BitReader, mv_cb: &Codebook) -> DecoderResult { + let xval = decode_mv_component(br, mv_cb)?; + let yval = decode_mv_component(br, mv_cb)?; +//println!(" MV {},{} @ {}", xval, yval, br.tell()); + Ok(MV::new(xval, yval)) +} + +impl<'a> BlockDecoder for RealVideo10BR<'a> { + +#[allow(unused_variables)] + fn decode_pichdr(&mut self) -> DecoderResult { + self.slice_no = 0; +println!("decoding picture header size {}", if self.num_slices > 1 { self.slice_off[1] } else { ((self.br.tell() as u32) + (self.br.left() as u32))/8 }); + self.mb_x = 0; + self.mb_y = 0; + self.mb_count = self.mb_w * self.mb_h; + let shdr = self.read_slice_header()?; + self.slice_no += 1; + validate!((shdr.mb_x == 0) && (shdr.mb_y == 0)); + + self.mb_x = shdr.mb_x; + self.mb_y = shdr.mb_y; + self.mb_count = shdr.mb_c; + self.mb_end = self.mb_x + self.mb_y * self.mb_w + self.mb_count; + self.is_intra = !shdr.is_p; + + let ftype = if !shdr.is_p { Type::I } else { Type::P }; + let picinfo = PicInfo::new(self.w, self.h, ftype, shdr.qscale, false, MVMode::Old, 0, None, true); + Ok(picinfo) + } + + #[allow(unused_variables)] + fn decode_slice_header(&mut self, info: &PicInfo) -> DecoderResult { +//println!("read slice {} header", self.slice_no); + let shdr = self.read_slice_header()?; + let ret = Slice::new(shdr.mb_x, shdr.mb_y, shdr.qscale); + self.slice_no += 1; + self.mb_x = shdr.mb_x; + self.mb_y = shdr.mb_y; + self.mb_count = shdr.mb_c; + self.mb_end = self.mb_x + self.mb_y * self.mb_w + self.mb_count; + + Ok(ret) + } + + fn decode_block_header(&mut self, info: &PicInfo, slice: &Slice) -> DecoderResult { + let mut br = &mut self.br; + let mut q = slice.get_quant(); + match info.get_mode() { + Type::I => { + let mut cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; + while cbpc == 8 { cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; } + let cbpy = br.read_cb(&self.tables.cbpy_cb)?; + let cbp = (cbpy << 2) | (cbpc & 3); + let dquant = (cbpc & 4) != 0; + if dquant { + let idx = br.read(2)? as usize; + q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; + } + self.mb_x += 1; + if self.mb_x == self.mb_w { + self.mb_x = 0; + self.mb_y += 1; + } + Ok(BlockInfo::new(Type::I, cbp, q)) + }, + Type::P => { + if br.read_bool()? { +self.mb_x += 1; +if self.mb_x == self.mb_w { + self.mb_x = 0; + self.mb_y += 1; +} + return Ok(BlockInfo::new(Type::Skip, 0, info.get_quant())); + } + let mut cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; + while cbpc == 20 { cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; } + let is_intra = (cbpc & 0x04) != 0; + let dquant = (cbpc & 0x08) != 0; + let is_4x4 = (cbpc & 0x10) != 0; + if is_intra { + let cbpy = br.read_cb(&self.tables.cbpy_cb)?; + let cbp = (cbpy << 2) | (cbpc & 3); + if dquant { + let idx = br.read(2)? as usize; + q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; + } + let binfo = BlockInfo::new(Type::I, cbp, q); + self.mb_x += 1; + if self.mb_x == self.mb_w { + self.mb_x = 0; + self.mb_y += 1; + } + return Ok(binfo); + } + + let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?; +// if /* !aiv && */(cbpc & 3) != 3 { + cbpy ^= 0xF; +// } + let cbp = (cbpy << 2) | (cbpc & 3); + if dquant { + let idx = br.read(2)? as usize; + q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; + } +println!(" MB {}.{} cbp = {:X}", self.mb_x, self.mb_y, cbp); + let mut binfo = BlockInfo::new(Type::P, cbp, q); + if !is_4x4 { + let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?]; + binfo.set_mv(&mvec); + } else { + let mvec: [MV; 4] = [ + decode_mv(br, &self.tables.mv_cb)?, + decode_mv(br, &self.tables.mv_cb)?, + decode_mv(br, &self.tables.mv_cb)?, + decode_mv(br, &self.tables.mv_cb)? + ]; + binfo.set_mv(&mvec); + } + self.mb_x += 1; + if self.mb_x == self.mb_w { + self.mb_x = 0; + self.mb_y += 1; + } + Ok(binfo) + }, + _ => { println!("wrong info mode"); Err(DecoderError::InvalidData) }, + } + } + + #[allow(unused_variables)] + fn decode_block_intra(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { + self.decode_block(quant, true, coded, blk, if no < 4 { 0 } else { no - 3 }) + } + + #[allow(unused_variables)] + fn decode_block_inter(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { + self.decode_block(quant, false, coded, blk, if no < 4 { 0 } else { no - 3 }) + } + + fn is_slice_end(&mut self) -> bool { self.mb_x + self.mb_y * self.mb_w >= self.mb_end } + + fn filter_row(&mut self, buf: &mut NAVideoBuffer, mb_y: usize, mb_w: usize, cbpi: &CBPInfo) { + let stride = buf.get_stride(0); + let mut off = buf.get_offset(0) + mb_y * 16 * stride; + for mb_x in 0..mb_w { + let coff = off; + let coded0 = cbpi.is_coded(mb_x, 0); + let coded1 = cbpi.is_coded(mb_x, 1); + let q = cbpi.get_q(mb_w + mb_x); + if mb_y != 0 { + if coded0 && cbpi.is_coded_top(mb_x, 0) { deblock_hor(buf, 0, q, coff); } + if coded1 && cbpi.is_coded_top(mb_x, 1) { deblock_hor(buf, 0, q, coff + 8); } + } + let coff = off + 8 * stride; + if cbpi.is_coded(mb_x, 2) && coded0 { deblock_hor(buf, 0, q, coff); } + if cbpi.is_coded(mb_x, 3) && coded1 { deblock_hor(buf, 0, q, coff + 8); } + off += 16; + } + let mut leftt = false; + let mut leftc = false; + let mut off = buf.get_offset(0) + mb_y * 16 * stride; + for mb_x in 0..mb_w { + let ctop0 = cbpi.is_coded_top(mb_x, 0); + let ctop1 = cbpi.is_coded_top(mb_x, 0); + let ccur0 = cbpi.is_coded(mb_x, 0); + let ccur1 = cbpi.is_coded(mb_x, 1); + let q = cbpi.get_q(mb_w + mb_x); + if mb_y != 0 { + let coff = off - 8 * stride; + let qtop = cbpi.get_q(mb_x); + if leftt && ctop0 { deblock_ver(buf, 0, qtop, coff); } + if ctop0 && ctop1 { deblock_ver(buf, 0, qtop, coff + 8); } + } + if leftc && ccur0 { deblock_ver(buf, 0, q, off); } + if ccur0 && ccur1 { deblock_ver(buf, 0, q, off + 8); } + leftt = ctop1; + leftc = ccur1; + off += 16; + } + let strideu = buf.get_stride(1); + let stridev = buf.get_stride(2); + let offu = buf.get_offset(1) + mb_y * 8 * strideu; + let offv = buf.get_offset(2) + mb_y * 8 * stridev; + if mb_y != 0 { + for mb_x in 0..mb_w { + let ctu = cbpi.is_coded_top(mb_x, 4); + let ccu = cbpi.is_coded(mb_x, 4); + let ctv = cbpi.is_coded_top(mb_x, 5); + let ccv = cbpi.is_coded(mb_x, 5); + let q = cbpi.get_q(mb_w + mb_x); + if ctu && ccu { deblock_hor(buf, 1, q, offu + mb_x * 8); } + if ctv && ccv { deblock_hor(buf, 2, q, offv + mb_x * 8); } + } + let mut leftu = false; + let mut leftv = false; + let offu = buf.get_offset(1) + (mb_y - 1) * 8 * strideu; + let offv = buf.get_offset(2) + (mb_y - 1) * 8 * stridev; + for mb_x in 0..mb_w { + let ctu = cbpi.is_coded_top(mb_x, 4); + let ctv = cbpi.is_coded_top(mb_x, 5); + let qt = cbpi.get_q(mb_x); + if leftu && ctu { deblock_ver(buf, 1, qt, offu + mb_x * 8); } + if leftv && ctv { deblock_ver(buf, 2, qt, offv + mb_x * 8); } + leftu = ctu; + leftv = ctv; + } + } + } +} + +fn deblock_hor(buf: &mut NAVideoBuffer, comp: usize, q: u8, off: usize) { + let stride = buf.get_stride(comp); + let mut dptr = buf.get_data_mut(); + let mut buf = dptr.as_mut_slice(); + for x in 0..8 { + let a = buf[off - 2 * stride + x] as i16; + let b = buf[off - 1 * stride + x] as i16; + let c = buf[off + 0 * stride + x] as i16; + let d = buf[off + 1 * stride + x] as i16; + let diff = ((a - d) * 3 + (c - b) * 8) >> 4; + if (diff != 0) && (diff >= -32) && (diff < 32) { + let d0 = diff.abs() * 2 - (q as i16); + let d1 = if d0 < 0 { 0 } else { d0 }; + let d2 = diff.abs() - d1; + let d3 = if d2 < 0 { 0 } else { d2 }; + + let delta = if diff < 0 { -d3 } else { d3 }; + + let b1 = b + delta; + if b1 < 0 { buf[off - 1 * stride + x] = 0; } + else if b1 > 255 { buf[off - 1 * stride + x] = 0xFF; } + else { buf[off - 1 * stride + x] = b1 as u8; } + let c1 = c - delta; + if c1 < 0 { buf[off + x] = 0; } + else if c1 > 255 { buf[off + x] = 0xFF; } + else { buf[off + x] = c1 as u8; } + } + } +} + +impl<'a> RealVideo10BR<'a> { + fn read_slice_header(&mut self) -> DecoderResult { + validate!(self.slice_no < self.num_slices); + + let mut br = &mut self.br; + br.seek(self.slice_off[self.slice_no] * 8)?; +//println!(" slice at off {}", br.tell()); + + let marker = br.read(1)?; + validate!(marker == 1); + let is_p = br.read_bool()?; + let pb_frame = br.read_bool()?; + validate!(!pb_frame); + let qscale = br.read(5)? as u8; + validate!(qscale > 0); + + if !is_p && self.new_ver { + self.last_dc[0] = br.read(8)? as i16; + self.last_dc[1] = br.read(8)? as i16; + self.last_dc[2] = br.read(8)? as i16; + } else { + self.last_dc[0] = 0; + self.last_dc[1] = 0; + self.last_dc[2] = 0; + } + self.dc_coded[0] = false; + self.dc_coded[1] = false; + self.dc_coded[2] = false; + + let mb_x; + let mb_y; + let mb_count; + let mb_pos = self.mb_x + self.mb_y * self.mb_w; + if (br.peek(12) == 0) || (mb_pos > 0) { + mb_x = br.read(6)? as usize; + mb_y = br.read(6)? as usize; + mb_count = br.read(12)? as usize; + } else { + mb_x = self.mb_x; + mb_y = self.mb_y; + mb_count = self.mb_count; + } + br.skip(3)?; +println!("slice q {} mb {},{} {}", qscale, mb_x, mb_y, mb_count); + validate!(mb_x + mb_y * self.mb_w + mb_count <= self.mb_w * self.mb_h); + + Ok(RV10SliceInfo::new(is_p, qscale, mb_x, mb_y, mb_count)) + } +} + +fn deblock_ver(buf: &mut NAVideoBuffer, comp: usize, q: u8, off: usize) { + let stride = buf.get_stride(comp); + let mut dptr = buf.get_data_mut(); + let mut buf = dptr.as_mut_slice(); + for y in 0..8 { + let a = buf[off - 2 + y * stride] as i16; + let b = buf[off - 1 + y * stride] as i16; + let c = buf[off + 0 + y * stride] as i16; + let d = buf[off + 1 + y * stride] as i16; + let diff = ((a - d) * 3 + (c - b) * 8) >> 4; + if (diff != 0) && (diff >= -32) && (diff < 32) { + let d0 = diff.abs() * 2 - (q as i16); + let d1 = if d0 < 0 { 0 } else { d0 }; + let d2 = diff.abs() - d1; + let d3 = if d2 < 0 { 0 } else { d2 }; + + let delta = if diff < 0 { -d3 } else { d3 }; + + let b1 = b + delta; + if b1 < 0 { buf[off - 1 + y * stride] = 0; } + else if b1 > 255 { buf[off - 1 + y * stride] = 0xFF; } + else { buf[off - 1 + y * stride] = b1 as u8; } + let c1 = c - delta; + if c1 < 0 { buf[off + y * stride] = 0; } + else if c1 > 255 { buf[off + y * stride] = 0xFF; } + else { buf[off + y * stride] = c1 as u8; } + } + } +} + +impl RealVideo10Decoder { + fn new() -> Self { + let mut coderead = H263ShortCodeReader::new(H263_INTRA_MCBPC); + let intra_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = H263ShortCodeReader::new(H263_INTER_MCBPC); + let inter_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = H263ShortCodeReader::new(H263_CBPY); + let cbpy_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = H263RLCodeReader::new(H263_RL_CODES); + let rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = H263RLCodeReader::new(H263_RL_CODES_AIC); + let aic_rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = H263ShortCodeReader::new(H263_MV); + let mv_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = CodeReader::new(RV10_LUMA_DC_CODES, RV10_LUMA_DC_BITS); + let luma_dc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = CodeReader::new(RV10_CHROMA_DC_CODES, RV10_CHROMA_DC_BITS); + let chroma_dc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + + let tables = Tables { + intra_mcbpc_cb: intra_mcbpc_cb, + inter_mcbpc_cb: inter_mcbpc_cb, + cbpy_cb: cbpy_cb, + rl_cb: rl_cb, + aic_rl_cb: aic_rl_cb, + mv_cb: mv_cb, + luma_dc_cb: luma_dc_cb, + chroma_dc_cb: chroma_dc_cb, + }; + + RealVideo10Decoder{ + info: Rc::new(DUMMY_CODEC_INFO), + dec: H263BaseDecoder::new(), + tables: tables, + w: 0, + h: 0, + new_ver: false, + } + } +} + +impl NADecoder for RealVideo10Decoder { + fn init(&mut self, info: Rc) -> DecoderResult<()> { + if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { + let w = vinfo.get_width(); + let h = vinfo.get_height(); + let fmt = formats::YUV420_FORMAT; + let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt)); + self.info = Rc::new(NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata())); + self.w = w; + self.h = h; + + let edata = info.get_extradata().unwrap(); + let src: &[u8] = &edata; + let ver = ((src[4] as u32) << 12) | ((src[5] as u32) << 4) | ((src[6] as u32) >> 4); + let maj_ver = ver >> 16; + let mic_ver = ver & 0xFF; +println!("ver {:06X} -> {}", ver, mic_ver); + validate!(maj_ver == 1); + self.new_ver = mic_ver > 1; +{ +let mut br = BitReader::new(src, src.len(), BitReaderMode::BE); +println!("edata:"); +println!("{:08X}", br.read(32).unwrap()); +println!("{:08X}", br.read(32).unwrap()); +} + Ok(()) + } else { +println!("???"); + Err(DecoderError::InvalidData) + } + } + fn decode(&mut self, pkt: &NAPacket) -> DecoderResult { + let src = pkt.get_buffer(); + +println!(" decode frame size {}, {} slices", src.len(), src[0]+1); + let mut ibr = RealVideo10BR::new(&src, &self.tables, self.w, self.h, self.new_ver); + + let bufinfo = self.dec.parse_frame(&mut ibr)?; + + let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); + frm.set_keyframe(self.dec.is_intra()); + frm.set_frame_type(if self.dec.is_intra() { FrameType::I } else { FrameType::P }); + Ok(Rc::new(RefCell::new(frm))) + } +} + + +pub fn get_decoder() -> Box { + Box::new(RealVideo10Decoder::new()) +} + +#[cfg(test)] +mod test { + use test::dec_video::test_file_decoding; + #[test] + fn test_rv10() { + test_file_decoding("realmedia", "assets/RV/rv10_dnet_640x352_realvideo_encoder_4.0.rm", Some(1000), true, false, Some("rv10")); + } +} + +pub struct CodeReader { codes: &'static [u16], bits: &'static [u8] } + +impl CodeReader { + pub fn new(codes: &'static [u16], bits: &'static [u8]) -> Self { + CodeReader { codes: codes, bits: bits } + } +} + +impl CodebookDescReader for CodeReader { + fn bits(&mut self, idx: usize) -> u8 { self.bits[idx] } + fn code(&mut self, idx: usize) -> u32 { self.codes[idx] as u32 } + fn sym (&mut self, idx: usize) -> u8 { idx as u8 } + fn len(&mut self) -> usize { self.bits.len() } +} + +const RV10_LUMA_DC_CODES: &[u16] = &[ + 0x001f, 0x0f00, 0x0f01, 0x0f02, 0x0f03, 0x0f04, 0x0f05, 0x0f06, + 0x0f07, 0x0f08, 0x0f09, 0x0f0a, 0x0f0b, 0x0f0c, 0x0f0d, 0x0f0e, + 0x0f0f, 0x0f10, 0x0f11, 0x0f12, 0x0f13, 0x0f14, 0x0f15, 0x0f16, + 0x0f17, 0x0f18, 0x0f19, 0x0f1a, 0x0f1b, 0x0f1c, 0x0f1d, 0x0f1e, + 0x0f1f, 0x0f20, 0x0f21, 0x0f22, 0x0f23, 0x0f24, 0x0f25, 0x0f26, + 0x0f27, 0x0f28, 0x0f29, 0x0f2a, 0x0f2b, 0x0f2c, 0x0f2d, 0x0f2e, + 0x0f2f, 0x0f30, 0x0f31, 0x0f32, 0x0f33, 0x0f34, 0x0f35, 0x0f36, + 0x0f37, 0x0f38, 0x0f39, 0x0f3a, 0x0f3b, 0x0f3c, 0x0f3d, 0x0f3e, + 0x0f3f, 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0386, + 0x0387, 0x0388, 0x0389, 0x038a, 0x038b, 0x038c, 0x038d, 0x038e, + 0x038f, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, + 0x0397, 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, + 0x039f, 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, + 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, + 0x00cf, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, + 0x0057, 0x0020, 0x0021, 0x0022, 0x0023, 0x000c, 0x000d, 0x0004, + 0x0000, 0x0005, 0x000e, 0x000f, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, + 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, + 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, + 0x03a0, 0x03a1, 0x03a2, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, + 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af, + 0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, + 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf, + 0x0f40, 0x0f41, 0x0f42, 0x0f43, 0x0f44, 0x0f45, 0x0f46, 0x0f47, + 0x0f48, 0x0f49, 0x0f4a, 0x0f4b, 0x0f4c, 0x0f4d, 0x0f4e, 0x0f4f, + 0x0f50, 0x0f51, 0x0f52, 0x0f53, 0x0f54, 0x0f55, 0x0f56, 0x0f57, + 0x0f58, 0x0f59, 0x0f5a, 0x0f5b, 0x0f5c, 0x0f5d, 0x0f5e, 0x0f5f, + 0x0f60, 0x0f61, 0x0f62, 0x0f63, 0x0f64, 0x0f65, 0x0f66, 0x0f67, + 0x0f68, 0x0f69, 0x0f6a, 0x0f6b, 0x0f6c, 0x0f6d, 0x0f6e, 0x0f6f, + 0x0f70, 0x0f71, 0x0f72, 0x0f73, 0x0f74, 0x0f75, 0x0f76, 0x0f77, + 0x0f78, 0x0f79, 0x0f7a, 0x0f7b, 0x0f7c, 0x0f7d, 0x0f7e, 0x0f7f, +]; + +const RV10_LUMA_DC_BITS: &[u8] = &[ + 5, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 7, 7, 7, 7, 7, 7, 7, + 7, 6, 6, 6, 6, 5, 5, 4, + 2, 4, 5, 5, 6, 6, 6, 6, + 7, 7, 7, 7, 7, 7, 7, 7, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, +]; + +const RV10_CHROMA_DC_CODES: &[u16] = &[ + 0x003f, 0x3f00, 0x3f01, 0x3f02, 0x3f03, 0x3f04, 0x3f05, 0x3f06, + 0x3f07, 0x3f08, 0x3f09, 0x3f0a, 0x3f0b, 0x3f0c, 0x3f0d, 0x3f0e, + 0x3f0f, 0x3f10, 0x3f11, 0x3f12, 0x3f13, 0x3f14, 0x3f15, 0x3f16, + 0x3f17, 0x3f18, 0x3f19, 0x3f1a, 0x3f1b, 0x3f1c, 0x3f1d, 0x3f1e, + 0x3f1f, 0x3f20, 0x3f21, 0x3f22, 0x3f23, 0x3f24, 0x3f25, 0x3f26, + 0x3f27, 0x3f28, 0x3f29, 0x3f2a, 0x3f2b, 0x3f2c, 0x3f2d, 0x3f2e, + 0x3f2f, 0x3f30, 0x3f31, 0x3f32, 0x3f33, 0x3f34, 0x3f35, 0x3f36, + 0x3f37, 0x3f38, 0x3f39, 0x3f3a, 0x3f3b, 0x3f3c, 0x3f3d, 0x3f3e, + 0x3f3f, 0x0f80, 0x0f81, 0x0f82, 0x0f83, 0x0f84, 0x0f85, 0x0f86, + 0x0f87, 0x0f88, 0x0f89, 0x0f8a, 0x0f8b, 0x0f8c, 0x0f8d, 0x0f8e, + 0x0f8f, 0x0f90, 0x0f91, 0x0f92, 0x0f93, 0x0f94, 0x0f95, 0x0f96, + 0x0f97, 0x0f98, 0x0f99, 0x0f9a, 0x0f9b, 0x0f9c, 0x0f9d, 0x0f9e, + 0x0f9f, 0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6, + 0x03c7, 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce, + 0x03cf, 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, + 0x00e7, 0x0030, 0x0031, 0x0032, 0x0033, 0x0008, 0x0009, 0x0002, + 0x0000, 0x0003, 0x000a, 0x000b, 0x0034, 0x0035, 0x0036, 0x0037, + 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, + 0x03d0, 0x03d1, 0x03d2, 0x03d3, 0x03d4, 0x03d5, 0x03d6, 0x03d7, + 0x03d8, 0x03d9, 0x03da, 0x03db, 0x03dc, 0x03dd, 0x03de, 0x03df, + 0x0fa0, 0x0fa1, 0x0fa2, 0x0fa3, 0x0fa4, 0x0fa5, 0x0fa6, 0x0fa7, + 0x0fa8, 0x0fa9, 0x0faa, 0x0fab, 0x0fac, 0x0fad, 0x0fae, 0x0faf, + 0x0fb0, 0x0fb1, 0x0fb2, 0x0fb3, 0x0fb4, 0x0fb5, 0x0fb6, 0x0fb7, + 0x0fb8, 0x0fb9, 0x0fba, 0x0fbb, 0x0fbc, 0x0fbd, 0x0fbe, 0x0fbf, + 0x3f40, 0x3f41, 0x3f42, 0x3f43, 0x3f44, 0x3f45, 0x3f46, 0x3f47, + 0x3f48, 0x3f49, 0x3f4a, 0x3f4b, 0x3f4c, 0x3f4d, 0x3f4e, 0x3f4f, + 0x3f50, 0x3f51, 0x3f52, 0x3f53, 0x3f54, 0x3f55, 0x3f56, 0x3f57, + 0x3f58, 0x3f59, 0x3f5a, 0x3f5b, 0x3f5c, 0x3f5d, 0x3f5e, 0x3f5f, + 0x3f60, 0x3f61, 0x3f62, 0x3f63, 0x3f64, 0x3f65, 0x3f66, 0x3f67, + 0x3f68, 0x3f69, 0x3f6a, 0x3f6b, 0x3f6c, 0x3f6d, 0x3f6e, 0x3f6f, + 0x3f70, 0x3f71, 0x3f72, 0x3f73, 0x3f74, 0x3f75, 0x3f76, 0x3f77, + 0x3f78, 0x3f79, 0x3f7a, 0x3f7b, 0x3f7c, 0x3f7d, 0x3f7e, 0x3f7f, +]; + +const RV10_CHROMA_DC_BITS: &[u8] = &[ + 7, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 8, 8, 8, 8, 8, 8, 8, + 8, 6, 6, 6, 6, 4, 4, 3, + 2, 3, 4, 4, 6, 6, 6, 6, + 8, 8, 8, 8, 8, 8, 8, 8, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, +]; diff --git a/src/codecs/h263/rv20.rs b/src/codecs/h263/rv20.rs new file mode 100644 index 0000000..23e5414 --- /dev/null +++ b/src/codecs/h263/rv20.rs @@ -0,0 +1,641 @@ +use io::bitreader::*; +use io::codebook::*; +use formats; +use super::super::*; +use super::*; +use super::decoder::*; +use super::data::*; + +#[allow(dead_code)] +struct Tables { + intra_mcbpc_cb: Codebook, + inter_mcbpc_cb: Codebook, + cbpy_cb: Codebook, + rl_cb: Codebook, + aic_rl_cb: Codebook, + mv_cb: Codebook, +} + +#[derive(Clone,Copy)] +struct RPRInfo { + present: bool, + bits: u8, + widths: [usize; 8], + heights: [usize; 8], +} + +struct RealVideo20Decoder { + info: Rc, + dec: H263BaseDecoder, + tables: Tables, + w: usize, + h: usize, + minor_ver: u8, + rpr: RPRInfo, +} + +struct RealVideo20BR<'a> { + br: BitReader<'a>, + tables: &'a Tables, + num_slices: usize, + slice_no: usize, + slice_off: Vec, + w: usize, + h: usize, + mb_w: usize, + mb_h: usize, + mb_x: usize, + mb_y: usize, + mb_pos_bits: u8, + mb_count: usize, + mb_end: usize, + minor_ver: u8, + rpr: RPRInfo, + is_intra: bool, +} + +struct RV20SliceInfo { + ftype: Type, + qscale: u8, + mb_x: usize, + mb_y: usize, + mb_pos: usize, + w: usize, + h: usize, +} + +impl RV20SliceInfo { + fn new(ftype: Type, qscale: u8, mb_x: usize, mb_y: usize, mb_pos: usize, w: usize, h: usize) -> Self { + RV20SliceInfo { ftype: ftype, qscale: qscale, mb_x: mb_x, mb_y: mb_y, mb_pos: mb_pos, w: w, h: h } + } +} + +impl<'a> RealVideo20BR<'a> { + fn new(src: &'a [u8], tables: &'a Tables, width: usize, height: usize, minor_ver: u8, rpr: RPRInfo) -> Self { + let nslices = (src[0] as usize) + 1; + let mut slice_offs = Vec::with_capacity(nslices); + { + let offs = &src[1..][..nslices * 8]; + let mut br = BitReader::new(offs, offs.len(), BitReaderMode::BE); + for _ in 0..nslices { + br.skip(32).unwrap(); + let off = br.read(32).unwrap(); + slice_offs.push(off); + } + } + 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..], src.len() - soff, BitReaderMode::BE), + tables: tables, + num_slices: nslices, + slice_no: 0, + slice_off: slice_offs, + w: width, + h: height, + mb_w: mb_w, + mb_h: mb_h, + mb_pos_bits: mbpb, + mb_x: 0, + mb_y: 0, + mb_count: 0, + mb_end: 0, + minor_ver: minor_ver, + rpr: rpr, + is_intra: false, + } + } + + fn decode_block(&mut self, quant: u8, intra: bool, coded: bool, blk: &mut [i16; 64], plane_no: usize) -> DecoderResult<()> { + let mut br = &mut self.br; + let mut idx = 0; + if !self.is_intra && intra { + let mut dc = br.read(8).unwrap() as i16; + if dc == 255 { dc = 128; } + blk[0] = dc << 3; + idx = 1; + } + if !coded { return Ok(()); } + + let rl_cb = if self.is_intra { &self.tables.aic_rl_cb } else { &self.tables.rl_cb }; + let q_add = if quant == 0 { 0i16 } else { ((quant - 1) | 1) as i16 }; + let q = (quant * 2) as i16; + while idx < 64 { + let code = br.read_cb(rl_cb).unwrap(); + let run; + let mut level; + let last; + if !code.is_escape() { + run = code.get_run(); + level = code.get_level(); + last = code.is_last(); + if br.read_bool().unwrap() { level = -level; } + level = (level * q) + q_add; + } else { + last = br.read_bool().unwrap(); + run = br.read(6).unwrap() as u8; + level = br.read_s(8).unwrap() as i16; + if level == -128 { + let low = br.read(5).unwrap() as i16; + let top = br.read_s(6).unwrap() as i16; + level = (top << 5) | low; + } + level = (level * q) + q_add; + if level < -2048 { level = -2048; } + if level > 2047 { level = 2047; } + } + idx += run; + validate!(idx < 64); + let oidx = H263_ZIGZAG[idx as usize]; + blk[oidx] = level; + idx += 1; + if last { break; } + } + Ok(()) + } +} + +fn decode_mv_component(br: &mut BitReader, mv_cb: &Codebook) -> DecoderResult { + let code = br.read_cb(mv_cb).unwrap() as i16; + if code == 0 { return Ok(0) } + if !br.read_bool().unwrap() { + Ok(code) + } else { + Ok(-code) + } +} + +fn decode_mv(br: &mut BitReader, mv_cb: &Codebook) -> DecoderResult { + let xval = decode_mv_component(br, mv_cb).unwrap(); + let yval = decode_mv_component(br, mv_cb).unwrap(); +//println!(" MV {},{} @ {}", xval, yval, br.tell()); + Ok(MV::new(xval, yval)) +} + +impl<'a> BlockDecoder for RealVideo20BR<'a> { + +#[allow(unused_variables)] + fn decode_pichdr(&mut self) -> DecoderResult { + self.slice_no = 0; +println!("decoding picture header size {}", if self.num_slices > 1 { self.slice_off[1] } else { ((self.br.tell() as u32) + (self.br.left() as u32))/8 }); + self.mb_x = 0; + self.mb_y = 0; + self.mb_count = self.mb_w * self.mb_h; + let shdr = self.read_slice_header().unwrap(); +println!("slice ends @ {}\n", self.br.tell()); + self.slice_no += 1; + validate!((shdr.mb_x == 0) && (shdr.mb_y == 0)); + let mb_count; + if self.slice_no < self.num_slices { + let pos = self.br.tell(); + let shdr2 = self.read_slice_header().unwrap(); + self.br.seek(pos as u32)?; + mb_count = shdr2.mb_pos - shdr.mb_pos; + } else { + mb_count = self.mb_w * self.mb_h; + } + + self.mb_x = shdr.mb_x; + self.mb_y = shdr.mb_y; + self.mb_count = mb_count; + self.mb_end = shdr.mb_pos + mb_count; + self.is_intra = shdr.ftype == Type::I; + + let picinfo = PicInfo::new(shdr.w, shdr.h, shdr.ftype, shdr.qscale, false, MVMode::Old, 0, None, true); + Ok(picinfo) + } + + #[allow(unused_variables)] + fn decode_slice_header(&mut self, info: &PicInfo) -> DecoderResult { +//println!("read slice {} header", self.slice_no); + let shdr = self.read_slice_header().unwrap(); + self.slice_no += 1; + let mb_count; + if self.slice_no < self.num_slices { + let shdr2 = self.read_slice_header().unwrap(); + mb_count = shdr2.mb_pos - shdr.mb_pos; + } else { + mb_count = self.mb_w * self.mb_h - shdr.mb_pos; + } + let ret = Slice::new(shdr.mb_x, shdr.mb_y, shdr.qscale); + self.mb_x = shdr.mb_x; + self.mb_y = shdr.mb_y; + self.mb_count = mb_count; + self.mb_end = shdr.mb_pos + mb_count; + + Ok(ret) + } + + fn decode_block_header(&mut self, info: &PicInfo, slice: &Slice) -> DecoderResult { + let mut br = &mut self.br; + let mut q = slice.get_quant(); + match info.get_mode() { + Type::I => { + let mut cbpc = br.read_cb(&self.tables.intra_mcbpc_cb).unwrap(); + while cbpc == 8 { cbpc = br.read_cb(&self.tables.intra_mcbpc_cb).unwrap(); } +if self.is_intra { +let acpred = br.read_bool()?; +println!(" acp {} @ {}", acpred, br.tell()); +if acpred { + br.skip(1)?;//pred direction +} +} + let cbpy = br.read_cb(&self.tables.cbpy_cb).unwrap(); + let cbp = (cbpy << 2) | (cbpc & 3); + let dquant = (cbpc & 4) != 0; + if dquant { + let idx = br.read(2).unwrap() as usize; + q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; + } +println!(" MB {},{} CBP {:X} @ {}", self.mb_x, self.mb_y, cbp, br.tell()); + self.mb_x += 1; + if self.mb_x == self.mb_w { + self.mb_x = 0; + self.mb_y += 1; + } + Ok(BlockInfo::new(Type::I, cbp, q)) + }, + Type::P => { + if br.read_bool().unwrap() { +self.mb_x += 1; +if self.mb_x == self.mb_w { + self.mb_x = 0; + self.mb_y += 1; +} + return Ok(BlockInfo::new(Type::Skip, 0, info.get_quant())); + } + let mut cbpc = br.read_cb(&self.tables.inter_mcbpc_cb).unwrap(); + while cbpc == 20 { cbpc = br.read_cb(&self.tables.inter_mcbpc_cb).unwrap(); } + let is_intra = (cbpc & 0x04) != 0; + let dquant = (cbpc & 0x08) != 0; + let is_4x4 = (cbpc & 0x10) != 0; + if is_intra { + let cbpy = br.read_cb(&self.tables.cbpy_cb).unwrap(); + let cbp = (cbpy << 2) | (cbpc & 3); + if dquant { + let idx = br.read(2).unwrap() as usize; + q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; + } + let binfo = BlockInfo::new(Type::I, cbp, q); + self.mb_x += 1; + if self.mb_x == self.mb_w { + self.mb_x = 0; + self.mb_y += 1; + } + return Ok(binfo); + } + + let mut cbpy = br.read_cb(&self.tables.cbpy_cb).unwrap(); +// if /* !aiv && */(cbpc & 3) != 3 { + cbpy ^= 0xF; +// } + let cbp = (cbpy << 2) | (cbpc & 3); + if dquant { + let idx = br.read(2).unwrap() as usize; + q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; + } +println!(" MB {}.{} cbp = {:X}", self.mb_x, self.mb_y, cbp); + let mut binfo = BlockInfo::new(Type::P, cbp, q); + if !is_4x4 { + let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb).unwrap()]; + binfo.set_mv(&mvec); + } else { + let mvec: [MV; 4] = [ + decode_mv(br, &self.tables.mv_cb).unwrap(), + decode_mv(br, &self.tables.mv_cb).unwrap(), + decode_mv(br, &self.tables.mv_cb).unwrap(), + decode_mv(br, &self.tables.mv_cb).unwrap() + ]; + binfo.set_mv(&mvec); + } + self.mb_x += 1; + if self.mb_x == self.mb_w { + self.mb_x = 0; + self.mb_y += 1; + } + Ok(binfo) + }, + _ => { println!("wrong info mode"); Err(DecoderError::InvalidData) }, + } + } + + #[allow(unused_variables)] + fn decode_block_intra(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { + self.decode_block(quant, true, coded, blk, if no < 4 { 0 } else { no - 3 }) + } + + #[allow(unused_variables)] + fn decode_block_inter(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { + self.decode_block(quant, false, coded, blk, if no < 4 { 0 } else { no - 3 }) + } + + fn is_slice_end(&mut self) -> bool { self.mb_x + self.mb_y * self.mb_w >= self.mb_end } + + fn filter_row(&mut self, buf: &mut NAVideoBuffer, mb_y: usize, mb_w: usize, cbpi: &CBPInfo) { + let stride = buf.get_stride(0); + let mut off = buf.get_offset(0) + mb_y * 16 * stride; + for mb_x in 0..mb_w { + let coff = off; + let coded0 = cbpi.is_coded(mb_x, 0); + let coded1 = cbpi.is_coded(mb_x, 1); + let q = cbpi.get_q(mb_w + mb_x); + if mb_y != 0 { + if coded0 && cbpi.is_coded_top(mb_x, 0) { deblock_hor(buf, 0, q, coff); } + if coded1 && cbpi.is_coded_top(mb_x, 1) { deblock_hor(buf, 0, q, coff + 8); } + } + let coff = off + 8 * stride; + if cbpi.is_coded(mb_x, 2) && coded0 { deblock_hor(buf, 0, q, coff); } + if cbpi.is_coded(mb_x, 3) && coded1 { deblock_hor(buf, 0, q, coff + 8); } + off += 16; + } + let mut leftt = false; + let mut leftc = false; + let mut off = buf.get_offset(0) + mb_y * 16 * stride; + for mb_x in 0..mb_w { + let ctop0 = cbpi.is_coded_top(mb_x, 0); + let ctop1 = cbpi.is_coded_top(mb_x, 0); + let ccur0 = cbpi.is_coded(mb_x, 0); + let ccur1 = cbpi.is_coded(mb_x, 1); + let q = cbpi.get_q(mb_w + mb_x); + if mb_y != 0 { + let coff = off - 8 * stride; + let qtop = cbpi.get_q(mb_x); + if leftt && ctop0 { deblock_ver(buf, 0, qtop, coff); } + if ctop0 && ctop1 { deblock_ver(buf, 0, qtop, coff + 8); } + } + if leftc && ccur0 { deblock_ver(buf, 0, q, off); } + if ccur0 && ccur1 { deblock_ver(buf, 0, q, off + 8); } + leftt = ctop1; + leftc = ccur1; + off += 16; + } + let strideu = buf.get_stride(1); + let stridev = buf.get_stride(2); + let offu = buf.get_offset(1) + mb_y * 8 * strideu; + let offv = buf.get_offset(2) + mb_y * 8 * stridev; + if mb_y != 0 { + for mb_x in 0..mb_w { + let ctu = cbpi.is_coded_top(mb_x, 4); + let ccu = cbpi.is_coded(mb_x, 4); + let ctv = cbpi.is_coded_top(mb_x, 5); + let ccv = cbpi.is_coded(mb_x, 5); + let q = cbpi.get_q(mb_w + mb_x); + if ctu && ccu { deblock_hor(buf, 1, q, offu + mb_x * 8); } + if ctv && ccv { deblock_hor(buf, 2, q, offv + mb_x * 8); } + } + let mut leftu = false; + let mut leftv = false; + let offu = buf.get_offset(1) + (mb_y - 1) * 8 * strideu; + let offv = buf.get_offset(2) + (mb_y - 1) * 8 * stridev; + for mb_x in 0..mb_w { + let ctu = cbpi.is_coded_top(mb_x, 4); + let ctv = cbpi.is_coded_top(mb_x, 5); + let qt = cbpi.get_q(mb_x); + if leftu && ctu { deblock_ver(buf, 1, qt, offu + mb_x * 8); } + if leftv && ctv { deblock_ver(buf, 2, qt, offv + mb_x * 8); } + leftu = ctu; + leftv = ctv; + } + } + } +} + +fn deblock_hor(buf: &mut NAVideoBuffer, comp: usize, q: u8, off: usize) { + let stride = buf.get_stride(comp); + let mut dptr = buf.get_data_mut(); + let mut buf = dptr.as_mut_slice(); + for x in 0..8 { + let a = buf[off - 2 * stride + x] as i16; + let b = buf[off - 1 * stride + x] as i16; + let c = buf[off + 0 * stride + x] as i16; + let d = buf[off + 1 * stride + x] as i16; + let diff = ((a - d) * 3 + (c - b) * 8) >> 4; + if (diff != 0) && (diff >= -32) && (diff < 32) { + let d0 = diff.abs() * 2 - (q as i16); + let d1 = if d0 < 0 { 0 } else { d0 }; + let d2 = diff.abs() - d1; + let d3 = if d2 < 0 { 0 } else { d2 }; + + let delta = if diff < 0 { -d3 } else { d3 }; + + let b1 = b + delta; + if b1 < 0 { buf[off - 1 * stride + x] = 0; } + else if b1 > 255 { buf[off - 1 * stride + x] = 0xFF; } + else { buf[off - 1 * stride + x] = b1 as u8; } + let c1 = c - delta; + if c1 < 0 { buf[off + x] = 0; } + else if c1 > 255 { buf[off + x] = 0xFF; } + else { buf[off + x] = c1 as u8; } + } + } +} + +impl<'a> RealVideo20BR<'a> { + fn read_slice_header(&mut self) -> DecoderResult { + validate!(self.slice_no < self.num_slices); + + let mut br = &mut self.br; + br.seek(self.slice_off[self.slice_no] * 8).unwrap(); +//println!(" slice at off {}", br.tell()); + + let frm_type = br.read(2).unwrap(); + let ftype = match frm_type { + 0 | 1 => { Type::I }, + 2 => { Type::P }, + _ => { Type::Skip }, + }; + + let marker = br.read(1).unwrap(); + validate!(marker == 0); + let qscale = br.read(5).unwrap() as u8; + validate!(qscale > 0); + if self.minor_ver >= 2 { + br.skip(1).unwrap(); // loop filter + } + let seq = if self.minor_ver <= 1 { + br.read(8).unwrap() << 7 + } else { + br.read(13).unwrap() << 2 + }; + let w; + let h; + if self.rpr.present { + let rpr = br.read(self.rpr.bits).unwrap() as usize; + if rpr == 0 { + w = self.w; + h = self.h; + } else { + w = self.rpr.widths[rpr]; + h = self.rpr.heights[rpr]; + validate!((w != 0) && (h != 0)); + } + } else { + w = self.w; + h = self.h; + } + + let mb_pos = br.read(self.mb_pos_bits).unwrap() as usize; + let mb_x = mb_pos % self.mb_w; + let mb_y = mb_pos / self.mb_w; + + br.skip(1).unwrap(); // no rounding + + if (self.minor_ver <= 1) && (frm_type == 3) { + br.skip(5).unwrap(); + } +println!("slice q {} mb {},{}", qscale, mb_x, mb_y); + + Ok(RV20SliceInfo::new(ftype, qscale, mb_x, mb_y, mb_pos, w, h)) + } +} + +fn deblock_ver(buf: &mut NAVideoBuffer, comp: usize, q: u8, off: usize) { + let stride = buf.get_stride(comp); + let mut dptr = buf.get_data_mut(); + let mut buf = dptr.as_mut_slice(); + for y in 0..8 { + let a = buf[off - 2 + y * stride] as i16; + let b = buf[off - 1 + y * stride] as i16; + let c = buf[off + 0 + y * stride] as i16; + let d = buf[off + 1 + y * stride] as i16; + let diff = ((a - d) * 3 + (c - b) * 8) >> 4; + if (diff != 0) && (diff >= -32) && (diff < 32) { + let d0 = diff.abs() * 2 - (q as i16); + let d1 = if d0 < 0 { 0 } else { d0 }; + let d2 = diff.abs() - d1; + let d3 = if d2 < 0 { 0 } else { d2 }; + + let delta = if diff < 0 { -d3 } else { d3 }; + + let b1 = b + delta; + if b1 < 0 { buf[off - 1 + y * stride] = 0; } + else if b1 > 255 { buf[off - 1 + y * stride] = 0xFF; } + else { buf[off - 1 + y * stride] = b1 as u8; } + let c1 = c - delta; + if c1 < 0 { buf[off + y * stride] = 0; } + else if c1 > 255 { buf[off + y * stride] = 0xFF; } + else { buf[off + y * stride] = c1 as u8; } + } + } +} + +impl RealVideo20Decoder { + fn new() -> Self { + let mut coderead = H263ShortCodeReader::new(H263_INTRA_MCBPC); + let intra_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = H263ShortCodeReader::new(H263_INTER_MCBPC); + let inter_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = H263ShortCodeReader::new(H263_CBPY); + let cbpy_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = H263RLCodeReader::new(H263_RL_CODES); + let rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = H263RLCodeReader::new(H263_RL_CODES_AIC); + let aic_rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + let mut coderead = H263ShortCodeReader::new(H263_MV); + let mv_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); + + let tables = Tables { + intra_mcbpc_cb: intra_mcbpc_cb, + inter_mcbpc_cb: inter_mcbpc_cb, + cbpy_cb: cbpy_cb, + rl_cb: rl_cb, + aic_rl_cb: aic_rl_cb, + mv_cb: mv_cb, + }; + + RealVideo20Decoder{ + info: Rc::new(DUMMY_CODEC_INFO), + dec: H263BaseDecoder::new(), + tables: tables, + w: 0, + h: 0, + minor_ver: 0, + rpr: RPRInfo { present: false, bits: 0, widths: [0; 8], heights: [0; 8] }, + } + } +} + +impl NADecoder for RealVideo20Decoder { + fn init(&mut self, info: Rc) -> DecoderResult<()> { + if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { + let w = vinfo.get_width(); + let h = vinfo.get_height(); + let fmt = formats::YUV420_FORMAT; + let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt)); + self.info = Rc::new(NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata())); + self.w = w; + self.h = h; + + let edata = info.get_extradata().unwrap(); + let src: &[u8] = &edata; + let ver = ((src[4] as u32) << 12) | ((src[5] as u32) << 4) | ((src[6] as u32) >> 4); + let maj_ver = ver >> 16; + let min_ver = (ver >> 8) & 0xFF; + let mic_ver = ver & 0xFF; +println!("ver {:06X}", ver); + validate!(maj_ver == 2); + self.minor_ver = min_ver as u8; + let rprb = src[1] & 7; + if rprb == 0 { + self.rpr.present = false; + } else { + self.rpr.present = true; + self.rpr.bits = rprb 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; + } + } + Ok(()) + } else { +println!(".unwrap().unwrap().unwrap()"); + Err(DecoderError::InvalidData) + } + } + fn decode(&mut self, pkt: &NAPacket) -> DecoderResult { + let src = pkt.get_buffer(); + +println!(" decode frame size {}, {} slices", src.len(), src[0]+1); + 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).unwrap(); + + let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); + frm.set_keyframe(self.dec.is_intra()); + frm.set_frame_type(if self.dec.is_intra() { FrameType::I } else { FrameType::P }); + Ok(Rc::new(RefCell::new(frm))) + } +} + +struct MBB { blocks: usize, bits: u8 } +const H263_MBB: &[MBB; 7] = &[ + MBB{ blocks: 47, bits: 6 }, + MBB{ blocks: 98, bits: 7 }, + MBB{ blocks: 395, bits: 9 }, + MBB{ blocks: 1583, bits: 11 }, + MBB{ blocks: 6335, bits: 13 }, + MBB{ blocks: 9215, bits: 14 }, + MBB{ blocks: 65536, bits: 14 }, +]; + +pub fn get_decoder() -> Box { + Box::new(RealVideo20Decoder::new()) +} + +#[cfg(test)] +mod test { + use test::dec_video::test_file_decoding; + #[test] + fn test_rv20() { + test_file_decoding("realmedia", "assets/RV/rv20_cook_640x352_realproducer_plus_8.51.rm", None/*Some(160)*/, true, false, Some("rv20")); + } +} diff --git a/src/codecs/mod.rs b/src/codecs/mod.rs index a808419..6572f26 100644 --- a/src/codecs/mod.rs +++ b/src/codecs/mod.rs @@ -141,6 +141,8 @@ const DECODERS: &[DecoderInfo] = &[ DecoderInfo { name: "intel263", get_decoder: h263::intel263::get_decoder }, #[cfg(feature="decoder_realvideo1")] DecoderInfo { name: "realvideo1", get_decoder: h263::rv10::get_decoder }, +#[cfg(feature="decoder_realvideo2")] + DecoderInfo { name: "realvideo2", get_decoder: h263::rv20::get_decoder }, #[cfg(feature="decoder_pcm")] DecoderInfo { name: "pcm", get_decoder: pcm::get_decoder },