1 use nihav_core::formats::YUV420_FORMAT;
2 use nihav_core::frame::*;
3 use nihav_core::codecs::{NADecoder, NADecoderSupport, DecoderError, DecoderResult, FrameSkipMode};
4 use nihav_core::options::*;
5 use nihav_codec_support::codecs::{MV, ZERO_MV, IPBShuffler};
6 use nihav_core::io::byteio::{MemoryReader,ByteReader};
7 use nihav_core::io::bitreader::{BitReader,BitReaderMode};
8 use nihav_core::io::intcode::*;
11 use super::rv60codes::*;
12 use super::rv60dsp::*;
14 struct UniqueList<A> {
20 impl<A:Copy+Default+PartialEq> UniqueList<A> {
21 fn new(max_size: usize) -> Self {
22 Self { list: [A::default(); 4], fill: 0, max_size }
24 fn add(&mut self, cand: A) {
25 if self.fill == self.max_size { return; }
26 let mut unique = true;
27 for el in self.list.iter().take(self.fill) {
34 self.list[self.fill] = cand;
40 const RV60_FRAME_TYPES: [FrameType; 4] = [ FrameType::I, FrameType::P, FrameType::B, FrameType::Other ];
41 const MAX_IMODE: u8 = 34;
43 #[derive(Clone,Copy,Debug)]
58 const RV60_CUSTOM_MSG_LENS: [u32; 4] = [ 2, 4, 16, 32 ];
60 fn read(br: &mut BitReader) -> DecoderResult<Self> {
61 let marker = br.read(2)?;
62 validate!(marker == 3);
63 let profile = br.read(2)? as u8;
64 validate!(profile == 0);
65 let _someval = br.read(4)?;
66 let ftypeid = br.read(2)? as usize;
67 let ftype = RV60_FRAME_TYPES[ftypeid];
68 let qp = br.read(6)? as u8;
69 let marker = br.read(1)?;
70 validate!(marker == 0);
71 let toolset = br.read(2)?;
72 validate!(toolset == 0);
73 let osvquant = br.read(2)? as u8;
74 let _some_flag = br.read_bool()?;
75 let _some_val = br.read(2)?;
76 let ts = br.read(24)?;
77 let width = ((br.read(11)? as usize) + 1) * 4;
78 let height = ((br.read(11)? as usize) + 0) * 4;
79 validate!(height > 0);
80 let _some_flag = br.read_bool()?;
82 if ftype == FrameType::I {
86 let flag = br.read_bool()?;
93 two_f_refs = br.read_bool()?;
95 // if byte17 { dw40 = 2; dw3C = 2; } else { dw40 = 1; dw3C = 1; }
96 let _some_val = br.read_code(UintCodeType::Unary012)?; // luma_qp_diff?
97 let chroma_qp_diff = br.read(1)?;
98 validate!(chroma_qp_diff == 0);
99 let qp_off_type = br.read_code(UintCodeType::Unary012)? as u8;
100 let deblock = br.read_bool()?;
101 let deblock_chroma = deblock && !br.read_bool()?;
103 let custom_msg_hdr_len = br.read(2)? as usize;
104 if custom_msg_hdr_len != 0 {
105 for i in 0..custom_msg_hdr_len {
106 br.skip(RV60_CUSTOM_MSG_LENS[i] * 8)?;
112 profile, ftype, qp, osvquant, ts, width, height, two_f_refs, qp_off_type,
113 deblock, deblock_chroma,
116 fn parse_slice_sizes(&self, br: &mut BitReader, sizes: &mut Vec<usize>) -> DecoderResult<()> {
117 let nslices = self.get_height_cu();
118 let nbits = (br.read(5)? as u8) + 1;
119 validate!(nbits < 32);
120 let mut signs: Vec<bool> = Vec::with_capacity(nslices);
121 for _ in 0..nslices {
122 let sign = br.read_bool()?;
128 let first_size = br.read(nbits)? as usize;
129 validate!(first_size > 0);
131 let mut lastsize = first_size;
132 sizes.push(first_size);
133 for i in 1..nslices {
134 let diff = br.read(nbits)? as isize;
137 let sum = (lastsize as isize).checked_add(diff);
138 validate!(sum.is_some());
139 size = sum.unwrap() as usize;
141 let sum = (lastsize as isize).checked_sub(diff);
142 validate!(sum.is_some());
143 size = sum.unwrap() as usize;
150 if ((br.left() >> 3) as usize) != sum {
151 println!(" left {} / {}", br.left() >> 3, sum);
153 validate!((br.left() >> 3) >= (sum as isize));
156 fn read_line_qp_offset(&self, br: &mut BitReader) -> DecoderResult<i8> {
157 match self.qp_off_type {
160 let val = br.read_code(UintCodeType::Unary012)?;
168 if br.read(1)? == 0 {
171 let val = br.read(2)? as i8;
181 fn get_width_cu(&self) -> usize {
182 (self.width + 63) >> 6
184 fn get_height_cu(&self) -> usize {
185 (self.height + 63) >> 6
187 fn has_top_block(&self, xpos: usize, ypos: usize, dx: usize, dy: usize, size: usize) -> bool {
188 if (ypos + dy) == 0 { return false; }
189 let xpos2 = xpos + dx;
190 if (xpos2 + size) > self.width { return false; }
193 fn has_top_right_block(&self, xpos: usize, ypos: usize, dx: usize, dy: usize, size: usize) -> bool {
194 if (ypos + dy) == 0 { return false; }
195 let xpos2 = xpos + dx;
196 if (xpos2 + size * 2) > self.width { return false; }
197 let cxpos = ((xpos + dx) & 63) >> RV60_BLOCK_LOG2[size];
198 let cypos = ((ypos + dy) & 63) >> RV60_BLOCK_LOG2[size];
199 ((cypos as u8) & RV60_AVAIL_MASK[cxpos]) == 0
201 fn has_left_block(&self, xpos: usize, ypos: usize, dx: usize, dy: usize, size: usize) -> bool {
202 if (xpos + dx) == 0 { return false; }
203 let ypos2 = ypos + dy;
204 if (ypos2 + size) > self.height { return false; }
207 fn has_left_down_block(&self, xpos: usize, ypos: usize, dx: usize, dy: usize, size: usize) -> bool {
208 if (xpos + dx) == 0 { return false; }
209 let ypos2 = ypos + dy;
210 if (ypos2 + size * 2) > self.height { return false; }
211 let cxpos = (!(xpos + dx) & 63) >> RV60_BLOCK_LOG2[size];
212 let cypos = (!(ypos + dy) & 63) >> RV60_BLOCK_LOG2[size];
213 ((cypos as u8) & RV60_AVAIL_MASK[cxpos]) >= 1
217 const RV60_BLOCK_LOG2: [u8; 65] = [
219 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4,
220 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
221 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6
224 const RV60_AVAIL_MASK: [u8; 64] = [
225 0, 1, 0, 3, 0, 1, 0, 7, 0, 1, 0, 3, 0, 1, 0, 0xF,
226 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
227 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
228 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
231 #[derive(Clone,Copy,PartialEq,Debug)]
239 impl Default for CUType {
240 fn default() -> Self { CUType::Intra }
243 const RV60_CU_TYPES: [CUType; 4] = [ CUType::Intra, CUType::InterMV, CUType::Skip, CUType::InterNoMV ];
245 #[derive(Clone,Copy,PartialEq,Debug)]
257 const RV60_PU_TYPES: [PUType; 8] = [
258 PUType::Full, PUType::N2Hor, PUType::N2Ver, PUType::Quarters,
259 PUType::N4Hor, PUType::N34Hor, PUType::N4Ver, PUType::N34Ver,
263 fn get_num_mvs(self) -> usize {
266 PUType::Quarters => 4,
270 fn get_mv_size(self, part_no: usize, size: usize) -> (usize, usize) {
271 let mv_size = size >> 2;
273 PUType::Full => (mv_size, mv_size),
274 PUType::N2Hor => (mv_size, mv_size >> 1),
275 PUType::N2Ver => (mv_size >> 1, mv_size),
276 PUType::Quarters => (mv_size >> 1, mv_size >> 1),
279 (mv_size, mv_size >> 2)
281 (mv_size, (3 * mv_size) >> 2)
286 (mv_size, (3 * mv_size) >> 2)
288 (mv_size, mv_size >> 2)
293 ( mv_size >> 2, mv_size)
295 ((3 * mv_size) >> 2, mv_size)
300 ((3 * mv_size) >> 2, mv_size)
302 ( mv_size >> 2, mv_size)
307 fn has_hor_split(self) -> bool {
309 PUType::N2Hor | PUType::N4Hor | PUType::N34Hor | PUType::Quarters => true,
313 fn has_ver_split(self) -> bool {
315 PUType::N2Ver | PUType::N4Ver | PUType::N34Ver | PUType::Quarters => true,
321 impl Default for PUType {
322 fn default() -> Self { PUType::Full }
325 #[derive(Clone,Copy,Debug)]
333 #[derive(Clone,Copy,PartialEq,Debug)]
341 impl Default for TransformType {
342 fn default() -> Self { TransformType::None }
345 #[derive(Clone,Copy,PartialEq,Debug)]
358 const SKIP_MV_REF: [MVRef; 4] = [ MVRef::Skip0, MVRef::Skip1, MVRef::Skip2, MVRef::Skip3 ];
361 fn get_skip_mv_num(self) -> usize {
369 fn is_ref0(self) -> bool {
371 MVRef::Ref0 | MVRef::Ref0AndBRef => true,
375 fn is_fwd(self) -> bool {
377 MVRef::Ref0 | MVRef::Ref1 | MVRef::Ref0AndBRef => true,
381 fn is_bwd(self) -> bool {
383 MVRef::BRef | MVRef::Ref0AndBRef => true,
389 #[derive(Clone,Copy,PartialEq,Debug)]
397 fn is_some(&self) -> bool { self.mvref != MVRef::None }
398 fn matches_fwd(&self, mvref: MVRef) -> bool {
399 (self.mvref == mvref) || (self.mvref.is_ref0() && mvref.is_ref0())
401 fn matches_bwd(&self, mvref: MVRef) -> bool {
402 self.mvref.is_bwd() && mvref.is_bwd()
404 fn is_deblock_cand(&self, other: &MVInfo) -> bool {
405 if self.mvref != other.mvref { return true; }
407 if self.mvref.is_fwd() {
408 let diff = self.f_mv - other.f_mv;
409 mvdiff += diff.x.abs() + diff.y.abs();
411 if self.mvref.is_bwd() {
412 let diff = self.b_mv - other.b_mv;
413 mvdiff += diff.x.abs() + diff.y.abs();
419 impl Default for MVInfo {
420 fn default() -> Self { Self { f_mv: ZERO_MV, b_mv: ZERO_MV, mvref: MVRef::None } }
423 #[derive(Clone,Copy,Debug)]
427 ttype: TransformType,
428 imode: [IntraMode; 4],
433 fn read(br: &mut BitReader, ftype: FrameType, two_f_refs: bool, size: usize) -> DecoderResult<Self> {
436 let mut imode: [IntraMode; 4] = [IntraMode::Index(0); 4];
437 let mut mv: [MVInfo; 4] = [MVInfo::default(); 4];
438 if ftype == FrameType::I {
439 cu_type = CUType::Intra;
441 cu_type = RV60_CU_TYPES[br.read(2)? as usize];
445 if (size == 8) && br.read_bool()? {
446 pu_type = PUType::Quarters;
448 pu_type = PUType::Full;
450 if pu_type == PUType::Quarters {
452 imode[i] = CBHeader::read_intra_mode(br)?;
454 } else if size <= 32 {
455 imode[0] = CBHeader::read_intra_mode(br)?;
457 if !br.read_bool()? {
458 imode[0] = IntraMode::DC64;
460 imode[0] = IntraMode::Plane64;
465 let bits = if size == 8 { 2 } else { 3 };
466 pu_type = RV60_PU_TYPES[br.read(bits)? as usize];
467 CBHeader::read_mv_data(br, ftype, two_f_refs, size, pu_type, &mut mv)?;
470 pu_type = PUType::Full;
471 let skip_mv_no = br.read_code(UintCodeType::LimitedUnary(3, 0))?;
472 mv[0].mvref = SKIP_MV_REF[skip_mv_no as usize];
476 if cu_type == CUType::Skip {
477 ttype = TransformType::None;
478 } else if size >= 32 {
479 ttype = TransformType::T16X16;
480 } else if size == 16 {
481 if (cu_type == CUType::Intra) || (pu_type == PUType::Full) {
482 ttype = TransformType::T16X16;
484 ttype = TransformType::T4X4;
487 if pu_type == PUType::Full {
488 ttype = TransformType::T8X8;
490 ttype = TransformType::T4X4;
494 cu_type, pu_type, ttype, imode, mv,
497 fn read_intra_mode(br: &mut BitReader) -> DecoderResult<IntraMode> {
499 let idx = br.read_code(UintCodeType::Unary012)? as u8;
500 Ok(IntraMode::Index(idx))
502 let mode = br.read(5)? as u8;
503 Ok(IntraMode::Mode(mode))
506 fn read_mv_data(br: &mut BitReader, ftype: FrameType, two_f_refs: bool, size: usize, pu_type: PUType, mv: &mut [MVInfo; 4]) -> DecoderResult<()> {
507 let mv_count = pu_type.get_num_mvs();
508 for i in 0..mv_count {
509 mv[i] = CBHeader::read_mv_info(br, ftype, two_f_refs, size, pu_type)?;
513 fn read_mv_info(br: &mut BitReader, ftype: FrameType, two_f_refs: bool, size: usize, pu_type: PUType) -> DecoderResult<MVInfo> {
514 let mut f_mv = ZERO_MV;
515 let mut b_mv = ZERO_MV;
517 if ftype != FrameType::B {
518 if two_f_refs && br.read_bool()? {
523 f_mv = CBHeader::read_mv(br)?;
524 Ok(MVInfo { f_mv, b_mv: ZERO_MV, mvref })
526 if ((size <= 8) && ((size != 8) || (pu_type != PUType::Full))) || br.read_bool()? {
527 if !br.read_bool()? {
529 f_mv = CBHeader::read_mv(br)?;
532 b_mv = CBHeader::read_mv(br)?;
535 mvref = MVRef::Ref0AndBRef;
536 f_mv = CBHeader::read_mv(br)?;
537 b_mv = CBHeader::read_mv(br)?;
539 Ok(MVInfo { f_mv, b_mv, mvref })
542 fn read_mv(br: &mut BitReader) -> DecoderResult<MV> {
543 let x = br.read_code_signed(IntCodeType::Gamma)? as i16;
544 let y = br.read_code_signed(IntCodeType::Gamma)? as i16;
549 #[derive(Clone,Copy,Default)]
552 ttype: TransformType,
556 fn is_intra(self) -> bool { self.cu_type == CUType::Intra }
559 const RV60_CANDIDATE_INTRA_ANGLES: [u8; 6] = [ 0, 1, 10, 26, 18, 2 ];
561 #[derive(Clone,Copy,Default)]
575 Self { left_str: Vec::new(), top_str: Vec::new(), stride: 0 }
577 fn reinit(&mut self, w: usize, h: usize) {
578 self.left_str.clear();
579 self.top_str.clear();
580 self.stride = w >> 2;
581 let size = self.stride * (h >> 2);
582 self.left_str.resize(size, 0);
583 self.top_str.resize(size, 0);
585 fn set_strength(&mut self, xpos: usize, ypos: usize, size: usize, q: u8, strength: u8) {
586 let pos = self.get_pos(xpos, ypos);
587 let dsize = size >> 2;
588 let dval = (q << 2) | strength;
590 self.top_str[pos + x] = dval;
593 self.left_str[pos + y * self.stride] = dval;
596 fn get_pos(&self, xpos: usize, ypos: usize) -> usize {
597 (xpos >> 2) + (ypos >> 2) * self.stride
599 fn get_top_strength(&self, pos: usize) -> u8 {
600 self.top_str[pos] & 3
602 fn get_left_strength(&self, pos: usize) -> u8 {
603 self.left_str[pos] & 3
605 fn set_top_strength(&mut self, pos: usize, str: u8) {
606 self.top_str[pos] |= str;
608 fn set_left_strength(&mut self, pos: usize, str: u8) {
609 self.left_str[pos] |= str;
613 struct RealVideo60Decoder {
614 info: NACodecInfoRef,
618 ipred: IntraPredContext,
619 skip_mode: FrameSkipMode,
621 avg_buf: NAVideoBufferRef<u8>,
623 y_coeffs: [i16; 16 * 16],
624 u_coeffs: [i16; 8 * 8],
625 v_coeffs: [i16; 8 * 8],
629 cu_splits: Vec<bool>,
630 coded_blk: [bool; 64],
633 pu_info: Vec<PUInfo>,
637 blk_info: Vec<BlockInfo>,
651 impl RealVideo60Decoder {
653 let tmp_vinfo = NAVideoInfo::new(64, 64, false, YUV420_FORMAT);
654 let vt = alloc_video_buffer(tmp_vinfo, 4).unwrap();
655 let vb = vt.get_vbuf();
656 let avg_buf = vb.unwrap();
658 info: NACodecInfoRef::default(),
659 cbs: RV60Codebooks::init(),
660 ipbs: IPBShuffler::new(),
661 ipred: IntraPredContext::new(),
662 skip_mode: FrameSkipMode::default(),
665 y_coeffs: [0; 16 * 16],
666 u_coeffs: [0; 8 * 8],
667 v_coeffs: [0; 8 * 8],
670 cu_splits: Vec::with_capacity(24),
671 coded_blk: [false; 64],
672 dblk: DeblockInfo::new(),
676 blk_info: Vec::new(),
689 fn decode_cu_line(&mut self, buf: &mut NASimpleVideoFrame<u8>, hdr: &FrameHeader, src: &[u8], cu_y: usize) -> DecoderResult<()> {
690 let mut br = BitReader::new(src, BitReaderMode::BE);
691 let cu_w = hdr.get_width_cu();
692 let dqp = hdr.read_line_qp_offset(&mut br)?;
693 let qps = (hdr.qp as i8) + dqp;
694 validate!((qps >= 0) && (qps < 32));
697 self.sel_qp = match hdr.osvquant {
717 for cu_x in 0..cu_w {
718 self.cu_splits.clear();
719 self.coded_blk = [false; 64];
720 self.decode_cb_tree(buf, hdr, &mut br, cu_x << 6, cu_y << 6, 6)?;
722 self.cu_splits.reverse();
723 self.deblock_cb_tree(buf, hdr, cu_x << 6, cu_y << 6, 6);
727 println!(" left {} bits", br.left());
731 #[allow(clippy::cognitive_complexity)]
732 fn decode_cb_tree(&mut self, buf: &mut NASimpleVideoFrame<u8>, hdr: &FrameHeader, br: &mut BitReader, xpos: usize, ypos: usize, log_size: u8) -> DecoderResult<()> {
733 if (xpos >= hdr.width) || (ypos >= hdr.height) { return Ok(()); }
735 let size = 1 << log_size;
736 let split = (xpos + size > hdr.width) || (ypos + size > hdr.height) || (size > 8 && br.read_bool()?);
737 self.cu_splits.push(split);
739 let hsize = size >> 1;
740 self.decode_cb_tree(buf, hdr, br, xpos, ypos, log_size - 1)?;
741 self.decode_cb_tree(buf, hdr, br, xpos + hsize, ypos, log_size - 1)?;
742 self.decode_cb_tree(buf, hdr, br, xpos, ypos + hsize, log_size - 1)?;
743 self.decode_cb_tree(buf, hdr, br, xpos + hsize, ypos + hsize, log_size - 1)?;
745 let cbh = CBHeader::read(br, hdr.ftype, hdr.two_f_refs, size)?;
746 self.pu_pos = (xpos >> 3) + (ypos >> 3) * self.pu_stride;
747 self.blk_pos = (xpos >> 2) + (ypos >> 2) * self.blk_stride;
750 self.reconstruct_info(hdr, &cbh, size)?;
752 let split_i4x4 = (cbh.cu_type == CUType::Intra) && (size == 8) && (cbh.pu_type == PUType::Quarters);
755 let itype = self.blk_info[self.blk_pos].imode;
757 let dstride = buf.stride[0];
758 let off = xpos + ypos * dstride;
759 let dst = &mut buf.data;
760 self.populate_ipred(hdr, dst, 0, dstride, 0, 0, size, true);
761 self.ipred.pred_angle(dst, off, dstride, size, itype as usize, true);
764 let dstride = buf.stride[comp];
765 let soff = buf.offset[comp];
766 let off = soff + (xpos >> 1) + (ypos >> 1) * dstride;
767 let mut dst = &mut buf.data;
768 self.populate_ipred(hdr, dst, soff, dstride, 0, 0, size >> 1, false);
769 self.ipred.pred_angle(&mut dst, off, dstride, size >> 1, itype as usize, false);
773 let mut mv_x = xpos >> 2;
774 let mut mv_y = ypos >> 2;
775 let mut mv_pos = mv_x + mv_y * self.blk_stride;
776 for part_no in 0..cbh.pu_type.get_num_mvs() {
777 let (mv_w, mv_h) = cbh.pu_type.get_mv_size(part_no, size);
778 let mv = self.blk_info[mv_pos].mv;
785 if hdr.ftype != FrameType::B {
786 if let Some(ref prevbuf) = self.ipbs.get_lastref() {
787 self.dsp.do_mc(buf, prevbuf, bx, by, bw, bh, mv.f_mv, false);
790 if let Some(ref prevbuf) = self.ipbs.get_b_fwdref() {
791 self.dsp.do_mc(buf, prevbuf, bx, by, bw, bh, mv.f_mv, false);
796 if let Some(ref prevbuf) = self.ipbs.get_nextref() {
797 self.dsp.do_mc(buf, prevbuf, bx, by, bw, bh, mv.f_mv, false);
801 validate!(hdr.ftype == FrameType::B);
802 if let Some(ref prevbuf) = self.ipbs.get_b_bwdref() {
803 self.dsp.do_mc(buf, prevbuf, bx, by, bw, bh, mv.b_mv, false);
806 MVRef::Ref0AndBRef => {
807 validate!(hdr.ftype == FrameType::B);
808 if let (Some(ref prevbuf), Some(ref nextbuf)) = (self.ipbs.get_b_fwdref(), self.ipbs.get_b_bwdref()) {
809 self.dsp.do_mc(buf, prevbuf, bx, by, bw, bh, mv.f_mv, false);
811 let mut avg_buf = NASimpleVideoFrame::from_video_buf(&mut self.avg_buf).unwrap();
812 self.dsp.do_mc(&mut avg_buf, nextbuf, bx, by, bw, bh, mv.b_mv, true);
814 self.dsp.do_avg(buf, &self.avg_buf, bx, by, bw, bh);
819 if cbh.pu_type == PUType::Quarters {
824 mv_pos += mv_h * self.blk_stride - mv_w;
828 } else if cbh.pu_type.has_hor_split() {
829 mv_pos += mv_h * self.blk_stride;
831 } else if cbh.pu_type.has_ver_split() {
838 if cbh.ttype != TransformType::None {
839 self.y_coeffs = [0; 16 * 16];
840 self.u_coeffs = [0; 8 * 8];
841 self.v_coeffs = [0; 8 * 8];
843 let is_intra = cbh.cu_type == CUType::Intra;
844 let cb_pos = ((xpos & 63) >> 3) + ((ypos & 63) >> 3) * 8;
846 TransformType::T4X4 => {
847 let subset = if is_intra { 0 } else { 2 };
851 cbp16 = rv6_decode_cbp16(br, &self.cbs, subset, self.sel_qp)?;
856 self.coded_blk[cb_pos + 0] = true;
857 self.coded_blk[cb_pos + 1] = true;
858 self.coded_blk[cb_pos + 8] = true;
859 self.coded_blk[cb_pos + 9] = true;
860 rv6_decode_cu_4x4in16x16(br, &self.cbs, is_intra, self.qp, self.sel_qp, &mut self.y_coeffs, &mut self.u_coeffs, &mut self.v_coeffs, cbp16)?;
864 if ((cbp16 >> i) & 1) != 0 {
865 self.dsp.transform4x4(&mut self.y_coeffs[i * 16..][..16]);
866 let dstride = buf.stride[0];
867 let off = xpos + x * 4 + (ypos + y * 4) * dstride;
868 let mut dst = &mut buf.data;
869 self.dsp.add_block(&mut dst, off, dstride, &self.y_coeffs[i*16..][..16], 4);
876 let xoff = (xpos >> 1) + x * 4;
877 let yoff = (ypos >> 1) + y * 4;
878 if ((cbp16 >> (16 + i)) & 1) != 0 {
879 self.dsp.transform4x4(&mut self.u_coeffs[i * 16..][..16]);
880 let dstride = buf.stride[1];
881 let off = buf.offset[1] + xoff + yoff * dstride;
882 let mut dst = &mut buf.data;
883 self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs[i * 16..][..16], 4);
885 if ((cbp16 >> (20 + i)) & 1) != 0 {
886 self.dsp.transform4x4(&mut self.v_coeffs[i * 16..][..16]);
887 let dstride = buf.stride[2];
888 let off = buf.offset[2] + xoff + yoff * dstride;
889 let mut dst = &mut buf.data;
890 self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs[i * 16..][..16], 4);
896 let cbp8 = rv6_decode_cbp8(br, &self.cbs, subset, self.sel_qp)?;
898 self.coded_blk[cb_pos] = true;
899 rv6_decode_cu_8x8(br, &self.cbs, is_intra, self.qp, self.sel_qp, &mut self.y_coeffs, &mut self.u_coeffs, &mut self.v_coeffs, cbp8, true)?;
902 let xoff = (i & 1) * 4;
903 let yoff = (i & 2) * 2;
905 let dstride = buf.stride[0];
906 let off = xpos + xoff + (ypos + yoff) * dstride;
907 let mut dst = &mut buf.data;
908 self.populate_ipred(hdr, dst, 0, dstride, xoff, yoff, 4, true);
909 let itype = self.blk_info[self.blk_pos + (i & 1) + (i >> 1) * self.blk_stride].imode;
910 self.ipred.pred_angle(&mut dst, off, dstride, 4, itype as usize, false);
912 if ((cbp8 >> i) & 1) != 0 {
913 let blk = &mut self.y_coeffs[i * 16..][..16];
914 self.dsp.transform4x4(blk);
915 let dstride = buf.stride[0];
916 let soff = buf.offset[0];
917 let off = soff + xpos + xoff + (ypos + yoff) * dstride;
918 let mut dst = &mut buf.data;
919 self.dsp.add_block(&mut dst, off, dstride, blk, 4);
922 if ((cbp8 >> 4) & 1) != 0 {
923 self.dsp.transform4x4(&mut self.u_coeffs);
924 let dstride = buf.stride[1];
925 let soff = buf.offset[1];
926 let off = soff + (xpos >> 1) + (ypos >> 1) * dstride;
927 let mut dst = &mut buf.data;
928 self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs, 4);
930 if ((cbp8 >> 5) & 1) != 0 {
931 self.dsp.transform4x4(&mut self.v_coeffs);
932 let dstride = buf.stride[2];
933 let soff = buf.offset[2];
934 let off = soff + (xpos >> 1) + (ypos >> 1) * dstride;
935 let mut dst = &mut buf.data;
936 self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs, 4);
940 TransformType::T8X8 => {
941 let subset = if is_intra { 1 } else { 3 };
942 let cbp8 = rv6_decode_cbp8(br, &self.cbs, subset, self.sel_qp)?;
944 self.coded_blk[cb_pos] = true;
945 rv6_decode_cu_8x8(br, &self.cbs, is_intra, self.qp, self.sel_qp, &mut self.y_coeffs, &mut self.u_coeffs, &mut self.v_coeffs, cbp8, false)?;
946 if (cbp8 & 0xF) != 0 {
947 self.dsp.transform8x8(&mut self.y_coeffs);
948 let dstride = buf.stride[0];
949 let off = xpos + ypos * dstride;
950 let mut dst = &mut buf.data;
951 self.dsp.add_block(&mut dst, off, dstride, &self.y_coeffs, 8);
953 if ((cbp8 >> 4) & 1) != 0 {
954 self.dsp.transform4x4(&mut self.u_coeffs);
955 let dstride = buf.stride[1];
956 let soff = buf.offset[1];
957 let off = soff + (xpos >> 1) + (ypos >> 1) * dstride;
958 let mut dst = &mut buf.data;
959 self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs, 4);
961 if ((cbp8 >> 5) & 1) != 0 {
962 self.dsp.transform4x4(&mut self.v_coeffs);
963 let dstride = buf.stride[2];
964 let soff = buf.offset[2];
965 let off = soff + (xpos >> 1) + (ypos >> 1) * dstride;
966 let mut dst = &mut buf.data;
967 self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs, 4);
971 TransformType::T16X16 => {
972 let subset = if is_intra { 1 } else { 3 };
973 let num_clusters = size >> 4;
974 let cl_cbp = br.read((num_clusters * num_clusters) as u8)?;
975 for y in 0..num_clusters {
976 for x in 0..num_clusters {
977 if ((cl_cbp >> (x + y * num_clusters)) & 1) == 0 { continue; }
978 self.coded_blk[cb_pos + x * 2 + y * 2 * 8 + 0] = true;
979 self.coded_blk[cb_pos + x * 2 + y * 2 * 8 + 1] = true;
980 self.coded_blk[cb_pos + x * 2 + y * 2 * 8 + 8] = true;
981 self.coded_blk[cb_pos + x * 2 + y * 2 * 8 + 9] = true;
982 let super_cbp = rv6_decode_cbp16(br, &self.cbs, subset, self.sel_qp)?;
984 self.y_coeffs = [0; 16 * 16];
985 self.u_coeffs = [0; 8 * 8];
986 self.v_coeffs = [0; 8 * 8];
987 rv6_decode_cu_16x16(br, &self.cbs, is_intra, self.qp, self.sel_qp, &mut self.y_coeffs, &mut self.u_coeffs, &mut self.v_coeffs, super_cbp)?;
988 if (super_cbp & 0xFFFF) != 0 {
989 self.dsp.transform16x16(&mut self.y_coeffs);
990 let dstride = buf.stride[0];
991 let off = xpos + x * 16 + (ypos + y * 16) * dstride;
992 let mut dst = &mut buf.data;
993 self.dsp.add_block(&mut dst, off, dstride, &self.y_coeffs, 16);
995 if ((super_cbp >> 16) & 0xF) != 0 {
996 self.dsp.transform8x8(&mut self.u_coeffs);
997 let dstride = buf.stride[1];
998 let soff = buf.offset[1];
999 let off = soff + (xpos >> 1) + x * 8 + ((ypos >> 1) + y * 8) * dstride;
1000 let mut dst = &mut buf.data;
1001 self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs, 8);
1003 if ((super_cbp >> 20) & 0xF) != 0 {
1004 self.dsp.transform8x8(&mut self.v_coeffs);
1005 let dstride = buf.stride[2];
1006 let soff = buf.offset[2];
1007 let off = soff + (xpos >> 1) + x * 8 + ((ypos >> 1) + y * 8) * dstride;
1008 let mut dst = &mut buf.data;
1009 self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs, 8);
1020 fn reconstruct_info(&mut self, hdr: &FrameHeader, cbh: &CBHeader, size: usize) -> DecoderResult<()>{
1021 let mut pui = PUInfo::default();
1022 let pu_size = size >> 3;
1023 pui.cu_type = cbh.cu_type;
1024 pui.ttype = cbh.ttype;
1025 if (cbh.cu_type == CUType::Intra) && (cbh.pu_type == PUType::Quarters) { // very special case
1026 self.pu_info[self.pu_pos] = pui;
1029 let imode = self.reconstruct_intra(hdr, cbh, 4, x + y * 2);
1030 validate!(imode <= MAX_IMODE);
1031 self.blk_info[self.blk_pos + x + y * self.blk_stride].imode = imode;
1032 self.blk_info[self.blk_pos + x + y * self.blk_stride].mv = MVInfo::default();
1039 self.pu_info[self.pu_pos] = pui;
1040 let imode = self.reconstruct_intra(hdr, cbh, size, 0);
1041 validate!(imode <= MAX_IMODE);
1042 for y in 0..(size >> 2) {
1043 for x in 0..(size >> 2) {
1044 self.blk_info[self.blk_pos + x + y * self.blk_stride].imode = imode;
1048 CUType::InterMV => {
1049 let mut mv_x = self.xpos >> 2;
1050 let mut mv_y = self.ypos >> 2;
1051 let mut mv_pos = self.blk_pos;
1052 let pu_type = cbh.pu_type;
1053 for part_no in 0..pu_type.get_num_mvs() {
1054 let (mv_w, mv_h) = pu_type.get_mv_size(part_no, size);
1055 let mv = self.predict_mv(hdr, mv_x, mv_y, mv_w, &cbh.mv[part_no]);
1058 self.blk_info[mv_pos + x + y * self.blk_stride].mv = mv;
1061 if pu_type == PUType::Quarters {
1066 mv_pos += mv_h * self.blk_stride - mv_w;
1070 } else if pu_type.has_hor_split() {
1071 mv_pos += mv_h * self.blk_stride;
1073 } else if pu_type.has_ver_split() {
1080 let skip_idx = cbh.mv[0].mvref.get_skip_mv_num();
1081 let mut skip_cand: UniqueList<MVInfo> = UniqueList::new(4);
1082 self.fill_skip_cand(hdr, &mut skip_cand, size);
1083 let mv = skip_cand.list[skip_idx];
1085 let mv_size = size >> 2;
1086 for y in 0..mv_size {
1087 for x in 0..mv_size {
1088 self.blk_info[self.blk_pos + x + y * self.blk_stride].mv = mv;
1093 for y in 0..pu_size {
1094 for x in 0..pu_size {
1095 self.pu_info[self.pu_pos + x + y * self.pu_stride] = pui;
1100 fn reconstruct_intra(&self, hdr: &FrameHeader, cbh: &CBHeader, size: usize, sub: usize) -> u8 {
1101 match cbh.imode[0] {
1102 IntraMode::DC64 => { return 1; },
1103 IntraMode::Plane64 => { return 0; },
1106 // form list of predictors
1107 let blk_pos = self.blk_pos + (sub & 1) + (sub >> 1) * self.blk_stride;
1108 let mut ipm_cand: UniqueList<u8> = UniqueList::new(3);
1109 if hdr.has_top_block(self.xpos, self.ypos, (sub & 1) * 4, 0, size) {
1110 let pu = &self.pu_info[self.pu_pos - self.pu_stride];
1112 ipm_cand.add(self.blk_info[self.blk_pos + (sub & 1) - self.blk_stride].imode);
1115 if hdr.has_left_block(self.xpos, self.ypos, 0, (sub & 2) * 2, size) {
1116 let pu = &self.pu_info[self.pu_pos - 1];
1118 ipm_cand.add(self.blk_info[blk_pos - 1 - (sub & 1)].imode);
1121 let tl_x = if (sub & 2) == 0 { self.xpos + (sub & 1) * 4 } else { self.xpos };
1122 let tl_y = self.ypos + (sub & 2) * 4;
1123 if (tl_x > 0) && (tl_y > 0) {
1124 let pu = match sub {
1125 0 => &self.pu_info[self.pu_pos - self.pu_stride - 1],
1126 1 => &self.pu_info[self.pu_pos - self.pu_stride],
1127 2 => &self.pu_info[self.pu_pos - 1],
1128 _ => &self.pu_info[self.pu_pos - 1],
1132 ipm_cand.add(self.blk_info[blk_pos - self.blk_stride - 1].imode);
1134 ipm_cand.add(self.blk_info[blk_pos - self.blk_stride - 2].imode);
1138 for el in RV60_CANDIDATE_INTRA_ANGLES.iter() {
1141 // actually decode prediction mode
1142 match cbh.imode[sub] {
1143 IntraMode::Index(idx) => {
1144 ipm_cand.list[idx as usize]
1146 IntraMode::Mode(mode) => {
1147 let mut imode = mode;
1148 let mut ipm_cs: [u8; 3] = [ipm_cand.list[0], ipm_cand.list[1], ipm_cand.list[2]];
1150 for ic in ipm_cs.iter() {
1157 _ => unreachable!(),
1160 fn populate_ipred(&mut self, hdr: &FrameHeader, src: &[u8], soff: usize, stride: usize, xoff: usize, yoff: usize, size: usize, is_luma: bool) {
1161 let src_off = if is_luma {
1162 soff + self.xpos + xoff + (self.ypos + yoff) * stride
1164 soff + (self.xpos >> 1) + (self.ypos >> 1) * stride
1166 self.ipred = IntraPredContext::new();
1167 if (self.ypos + yoff) > 0 {
1168 self.ipred.has_t = true;
1170 self.ipred.t[x + 1] = src[src_off - stride + x];
1172 if (is_luma && hdr.has_top_right_block(self.xpos, self.ypos, xoff, yoff, size)) ||
1173 (!is_luma && hdr.has_top_right_block(self.xpos, self.ypos, 0, 0, size << 1)) {
1174 self.ipred.has_tr = true;
1175 for x in size..size*2 {
1176 self.ipred.t[x + 1] = src[src_off - stride + x];
1180 self.ipred.t[size + i + 1] = self.ipred.t[size];
1183 if (self.xpos + xoff) > 0 {
1184 self.ipred.t[0] = src[src_off - stride - 1];
1187 if (self.xpos + xoff) > 0 {
1188 self.ipred.has_l = true;
1190 self.ipred.l[y + 1] = src[src_off - 1 + y * stride];
1192 if (is_luma && hdr.has_left_down_block(self.xpos, self.ypos, xoff, yoff, size)) ||
1193 (!is_luma && hdr.has_left_down_block(self.xpos, self.ypos, 0, 0, size << 1)) {
1194 self.ipred.has_ld = true;
1195 for y in size..size*2 {
1196 self.ipred.l[y + 1] = src[src_off - 1 + y * stride];
1200 self.ipred.l[size + i + 1] = self.ipred.l[size];
1203 if (self.ypos + yoff) > 0 {
1204 self.ipred.l[0] = src[src_off - stride - 1];
1208 fn predict_mv(&self, hdr: &FrameHeader, mv_x: usize, mv_y: usize, mv_w: usize, mvi: &MVInfo) -> MVInfo {
1209 let mv_pos = mv_x + mv_y * self.blk_stride;
1212 if mvi.mvref.is_fwd() {
1213 let mut mv_cand: [MV; 3] = [ZERO_MV; 3];
1214 let mut mv_cand_size: usize = 0;
1216 let ref_mv = &self.blk_info[mv_pos - 1].mv;
1217 if ref_mv.matches_fwd(mvi.mvref) {
1218 mv_cand[mv_cand_size] = ref_mv.f_mv;
1223 let ref_mv = &self.blk_info[mv_pos - self.blk_stride].mv;
1224 if ref_mv.matches_fwd(mvi.mvref) {
1225 mv_cand[mv_cand_size] = ref_mv.f_mv;
1229 if hdr.has_top_block(mv_x << 2, mv_y << 2, mv_w << 2, 0, 4) {
1230 let ref_mv = &self.blk_info[mv_pos - self.blk_stride + mv_w].mv;
1231 if ref_mv.matches_fwd(mvi.mvref) {
1232 mv_cand[mv_cand_size] = ref_mv.f_mv;
1236 f_mv = match mv_cand_size {
1238 let x = mv_cand[0].x + mv_cand[1].x + mv_cand[2].x;
1239 let y = mv_cand[0].y + mv_cand[1].y + mv_cand[2].y;
1240 if mv_cand_size == 1 {
1243 MV { x: x >> 1, y: y >> 1 }
1246 3 => MV::pred(mv_cand[0], mv_cand[1], mv_cand[2]),
1252 if mvi.mvref.is_bwd() {
1253 let mut mv_cand: [MV; 3] = [ZERO_MV; 3];
1254 let mut mv_cand_size: usize = 0;
1256 let ref_mv = &self.blk_info[mv_pos - 1].mv;
1257 if ref_mv.matches_bwd(mvi.mvref) {
1258 mv_cand[mv_cand_size] = ref_mv.b_mv;
1263 let ref_mv = &self.blk_info[mv_pos - self.blk_stride].mv;
1264 if ref_mv.matches_bwd(mvi.mvref) {
1265 mv_cand[mv_cand_size] = ref_mv.b_mv;
1269 if hdr.has_top_block(mv_x << 2, mv_y << 2, mv_w << 2, 0, 4) {
1270 let ref_mv = &self.blk_info[mv_pos - self.blk_stride + mv_w].mv;
1271 if ref_mv.matches_bwd(mvi.mvref) {
1272 mv_cand[mv_cand_size] = ref_mv.b_mv;
1276 b_mv = match mv_cand_size {
1278 let x = mv_cand[0].x + mv_cand[1].x + mv_cand[2].x;
1279 let y = mv_cand[0].y + mv_cand[1].y + mv_cand[2].y;
1280 if mv_cand_size == 1 {
1283 MV { x: x >> 1, y: y >> 1 }
1286 3 => MV::pred(mv_cand[0], mv_cand[1], mv_cand[2]),
1293 MVInfo { f_mv: mvi.f_mv + f_mv, b_mv: mvi.b_mv + b_mv, mvref: mvi.mvref }
1295 fn fill_skip_cand(&mut self, hdr: &FrameHeader, skip_cand: &mut UniqueList<MVInfo>, size: usize) {
1296 let mv_size = size >> 2;
1299 let mv = &self.blk_info[self.blk_pos - 1].mv;
1305 let mv = &self.blk_info[self.blk_pos - self.blk_stride].mv;
1310 if hdr.has_top_right_block(self.xpos, self.ypos, 0, 0, size) {
1311 let mv = &self.blk_info[self.blk_pos - self.blk_stride + mv_size].mv;
1316 if hdr.has_left_down_block(self.xpos, self.ypos, 0, 0, size) {
1317 let mv = &self.blk_info[self.blk_pos + self.blk_stride * mv_size - 1].mv;
1322 if hdr.has_left_block(self.xpos, self.ypos, 0, 0, size) {
1323 let mv = &self.blk_info[self.blk_pos + self.blk_stride * (mv_size - 1) - 1].mv;
1328 if hdr.has_top_block(self.xpos, self.ypos, 0, 0, size) {
1329 let mv = &self.blk_info[self.blk_pos - self.blk_stride + mv_size - 1].mv;
1334 if (self.xpos > 0) && (self.ypos > 0) {
1335 let mv = &self.blk_info[self.blk_pos - self.blk_stride - 1].mv;
1340 for i in skip_cand.fill..4 {
1341 skip_cand.list[i] = MVInfo { f_mv: ZERO_MV, b_mv: ZERO_MV, mvref: MVRef::Ref0 };
1344 fn deblock_cb_tree(&mut self, buf: &mut NASimpleVideoFrame<u8>, hdr: &FrameHeader, xpos: usize, ypos: usize, log_size: u8) {
1345 if (xpos >= hdr.width) || (ypos >= hdr.height) { return; }
1346 let split = (log_size > 3) && self.cu_splits.pop().unwrap();
1348 let hsize = 1 << (log_size - 1);
1349 self.deblock_cb_tree(buf, hdr, xpos, ypos, log_size - 1);
1350 self.deblock_cb_tree(buf, hdr, xpos + hsize, ypos, log_size - 1);
1351 self.deblock_cb_tree(buf, hdr, xpos, ypos + hsize, log_size - 1);
1352 self.deblock_cb_tree(buf, hdr, xpos + hsize, ypos + hsize, log_size - 1);
1354 let pu_pos = (xpos >> 3) + (ypos >> 3) * self.pu_stride;
1355 let cu_type = self.pu_info[pu_pos].cu_type;
1356 let tsize = if self.pu_info[pu_pos].ttype == TransformType::T16X16 { 4 } else { 3 };
1357 let ntiles = 1 << (log_size - tsize);
1358 let dparams = RV60DeblockParams {
1359 deblock_chroma: hdr.deblock_chroma,
1362 dblkstride: self.dblk.stride,
1364 for ty in 0..ntiles {
1365 for tx in 0..ntiles {
1366 let x = xpos + (tx << tsize);
1367 let y = ypos + (ty << tsize);
1368 let cb_pos = ((x & 63) >> 3) + ((y & 63) >> 3) * 8;
1369 if cu_type == CUType::Intra {
1370 self.dblk.set_strength(x, y, 1 << tsize, self.qp, 2);
1371 } else if (cu_type != CUType::Skip) && self.coded_blk[cb_pos] {
1372 self.dblk.set_strength(x, y, 1 << tsize, self.qp, 1);
1374 self.dblk.set_strength(x, y, 1 << tsize, self.qp, 0);
1375 self.derive_deblock_strength(x, y, 1 << (tsize - 2));
1377 self.dsp.do_deblock(&dparams, buf, x, y, 1 << tsize,
1378 self.dblk.top_str.as_slice(),
1379 self.dblk.left_str.as_slice(),
1380 self.dblk.get_pos(x, y));
1385 fn derive_deblock_strength(&mut self, xpos: usize, ypos: usize, size4: usize) {
1386 let blk_pos = (xpos >> 2) + (ypos >> 2) * self.blk_stride;
1387 let mut dblk_pos = self.dblk.get_pos(xpos, ypos);
1389 let top_blk_pos = blk_pos - self.blk_stride;
1391 if self.dblk.get_top_strength(dblk_pos + i) == 0 {
1392 if self.blk_info[blk_pos + i].mv.is_deblock_cand(&self.blk_info[top_blk_pos + i].mv) {
1393 self.dblk.set_top_strength(dblk_pos + i, 1);
1400 if self.dblk.get_left_strength(dblk_pos) == 0 {
1401 if self.blk_info[blk_pos + i].mv.is_deblock_cand(&self.blk_info[blk_pos + i - 1].mv) {
1402 self.dblk.set_left_strength(dblk_pos, 1);
1405 dblk_pos += self.dblk.stride;
1411 impl NADecoder for RealVideo60Decoder {
1412 fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
1413 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
1414 let fmt = YUV420_FORMAT;
1415 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, fmt));
1416 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
1418 let edata = info.get_extradata().unwrap();
1419 let src: &[u8] = &edata;
1421 if src.len() < 8 { return Err(DecoderError::InvalidData); }
1422 let mut mr = MemoryReader::new_read(src);
1423 let mut br = ByteReader::new(&mut mr);
1424 let _flags = br.read_u32be()?;
1425 let version = br.read_u32be()?;
1426 let _unk = br.read_u16be()?;
1427 validate!((version >> 28) == 4);
1428 // then width and height again as 16be
1430 //self.bd.width = vinfo.get_width();
1431 //self.bd.height = vinfo.get_height();
1432 //self.frmmgr.clear();
1434 supp.pool_u8.set_dec_bufs(3);
1435 supp.pool_u8.prealloc_video(NAVideoInfo::new(vinfo.get_width(), vinfo.get_height(), false, fmt), 6)?;
1440 Err(DecoderError::InvalidData)
1443 fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
1444 let src = pkt.get_buffer();
1446 validate!(src.len() > 9);
1447 let hsize = (src[0] as usize) * 8 + 9;
1448 let mut br = BitReader::new(&src[hsize..], BitReaderMode::BE);
1449 let hdr = FrameHeader::read(&mut br)?;
1450 match self.skip_mode {
1451 FrameSkipMode::None => {},
1452 FrameSkipMode::KeyframesOnly => {
1453 if hdr.ftype == FrameType::B {
1454 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
1455 frm.set_frame_type(FrameType::Skip);
1456 return Ok(frm.into_ref());
1459 FrameSkipMode::IntraOnly => {
1460 if hdr.ftype != FrameType::I {
1461 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
1462 frm.set_frame_type(FrameType::Skip);
1463 return Ok(frm.into_ref());
1468 let mut slices: Vec<usize> = Vec::new();
1469 hdr.parse_slice_sizes(&mut br, &mut slices)?;
1472 if self.ipbs.get_lastref().is_none() {
1473 return Err(DecoderError::MissingReference);
1477 if self.ipbs.get_lastref().is_none() {
1478 return Err(DecoderError::MissingReference);
1480 if self.ipbs.get_nextref().is_none() {
1481 return Err(DecoderError::MissingReference);
1487 let tmp_vinfo = NAVideoInfo::new(hdr.width, hdr.height, false, YUV420_FORMAT);
1488 let ret = supp.pool_u8.get_free();
1490 return Err(DecoderError::AllocError);
1492 let mut buf = ret.unwrap();
1493 if buf.get_info() != tmp_vinfo {
1495 supp.pool_u8.reset();
1496 supp.pool_u8.prealloc_video(tmp_vinfo, 6)?;
1497 let ret = supp.pool_u8.get_free();
1499 return Err(DecoderError::AllocError);
1504 let cu_w = hdr.get_width_cu();
1505 let cu_h = hdr.get_height_cu();
1506 self.pu_stride = cu_w << 3;
1507 self.pu_info.resize(self.pu_stride * (cu_h << 3), PUInfo::default());
1508 self.blk_stride = cu_w << 4;
1509 self.blk_info.clear();
1510 self.blk_info.resize(self.blk_stride * (cu_h << 4), BlockInfo::default());
1512 self.dblk.reinit(hdr.width, hdr.height);
1514 let mut off = hsize + ((br.tell() >> 3) as usize);
1515 let mut dframe = NASimpleVideoFrame::from_video_buf(&mut buf).unwrap();
1516 for (cu_y, size) in slices.into_iter().enumerate() {
1517 self.decode_cu_line(&mut dframe, &hdr, &src[off..][..size], cu_y)?;
1520 if (hdr.ftype == FrameType::I) || (hdr.ftype == FrameType::P) {
1521 self.ipbs.add_frame(buf.clone());
1524 if hdr.ftype != FrameType::B {
1525 self.ref0_pts = self.ref1_pts;
1526 self.ref1_pts = pkt.get_pts().unwrap_or(0);
1527 self.ref0_ts = self.ref1_ts;
1528 self.ref1_ts = hdr.ts as u64;
1529 if (self.ref1_pts > self.ref0_pts) && (self.ref1_ts > self.ref0_ts) {
1530 self.ts_scale = (self.ref1_pts - self.ref0_pts) / (self.ref1_ts - self.ref0_ts);
1533 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::Video(buf));
1534 frm.set_keyframe(hdr.ftype == FrameType::I);
1535 if hdr.ftype == FrameType::B {
1536 let pts = self.ref0_pts + ((hdr.ts as u64) - self.ref0_ts) * self.ts_scale;
1537 frm.set_pts(Some(pts));
1539 frm.set_frame_type(hdr.ftype);
1542 fn flush(&mut self) {
1547 const DECODER_OPTIONS: &[NAOptionDefinition] = &[
1548 NAOptionDefinition {
1549 name: FRAME_SKIP_OPTION, description: FRAME_SKIP_OPTION_DESC,
1550 opt_type: NAOptionDefinitionType::String(Some(&[
1551 FRAME_SKIP_OPTION_VAL_NONE,
1552 FRAME_SKIP_OPTION_VAL_KEYFRAME,
1553 FRAME_SKIP_OPTION_VAL_INTRA
1557 impl NAOptionHandler for RealVideo60Decoder {
1558 fn get_supported_options(&self) -> &[NAOptionDefinition] { DECODER_OPTIONS }
1559 fn set_options(&mut self, options: &[NAOption]) {
1560 for option in options.iter() {
1561 for opt_def in DECODER_OPTIONS.iter() {
1562 if opt_def.check(option).is_ok() {
1563 match (option.name, &option.value) {
1564 (FRAME_SKIP_OPTION, NAValue::String(ref str)) => {
1565 if let Ok(smode) = FrameSkipMode::from_str(str) {
1566 self.skip_mode = smode;
1575 fn query_option_value(&self, name: &str) -> Option<NAValue> {
1577 FRAME_SKIP_OPTION => Some(NAValue::String(self.skip_mode.to_string())),
1583 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
1584 Box::new(RealVideo60Decoder::new())
1589 use nihav_core::codecs::RegisteredDecoders;
1590 use nihav_core::demuxers::RegisteredDemuxers;
1591 use nihav_codec_support::test::dec_video::*;
1592 use crate::realmedia_register_all_decoders;
1593 use crate::realmedia_register_all_demuxers;
1596 let mut dmx_reg = RegisteredDemuxers::new();
1597 realmedia_register_all_demuxers(&mut dmx_reg);
1598 let mut dec_reg = RegisteredDecoders::new();
1599 realmedia_register_all_decoders(&mut dec_reg);
1601 // sample from a private collection
1602 test_decoding("realmedia", "realvideo6", "assets/RV/RV60.rmhd", Some(1000), &dmx_reg, &dec_reg,
1603 ExpectedTestResult::MD5Frames(vec![
1604 [0x2b1f1807, 0x09edef33, 0x0e6c78c1, 0x3b3c8179],
1605 [0xea406850, 0x400802b8, 0xac106fb6, 0xe1e2e766],
1606 [0x2b1f1807, 0x09edef33, 0x0e6c78c1, 0x3b3c8179],
1607 [0xb04e2626, 0x976e16f5, 0xc41a7a78, 0x2d8765da],
1608 [0xf4f30d97, 0x7f2876eb, 0x265ffad4, 0x3542a7c4],
1609 [0xa5082524, 0x38a86952, 0x35bf1fee, 0xfc830d3f],
1610 [0x75eab1a2, 0x62e2222f, 0xe96a20d9, 0x652140b4],
1611 [0x7590fa49, 0x78c83490, 0x239eeff9, 0x64282ac7],
1612 [0x70b19e9f, 0x66c1f866, 0xb8d7142a, 0xf3e424b2],
1613 [0xc2934123, 0x3bf72fc4, 0x12d8d123, 0x1f39525b],
1614 [0x13344919, 0xecd01190, 0x2f69079b, 0xbf4d7026],
1615 [0xcefb3284, 0xa9b36d4d, 0xf1aa6752, 0xaae17d44],
1616 [0x57f01275, 0xf8e883ea, 0x4865752e, 0xc760a777]]));