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 fn decode_cb_tree(&mut self, buf: &mut NASimpleVideoFrame<u8>, hdr: &FrameHeader, br: &mut BitReader, xpos: usize, ypos: usize, log_size: u8) -> DecoderResult<()> {
732 if (xpos >= hdr.width) || (ypos >= hdr.height) { return Ok(()); }
734 let size = 1 << log_size;
735 let split = (xpos + size > hdr.width) || (ypos + size > hdr.height) || (size > 8 && br.read_bool()?);
736 self.cu_splits.push(split);
738 let hsize = size >> 1;
739 self.decode_cb_tree(buf, hdr, br, xpos, ypos, log_size - 1)?;
740 self.decode_cb_tree(buf, hdr, br, xpos + hsize, ypos, log_size - 1)?;
741 self.decode_cb_tree(buf, hdr, br, xpos, ypos + hsize, log_size - 1)?;
742 self.decode_cb_tree(buf, hdr, br, xpos + hsize, ypos + hsize, log_size - 1)?;
744 let cbh = CBHeader::read(br, hdr.ftype, hdr.two_f_refs, size)?;
745 self.pu_pos = (xpos >> 3) + (ypos >> 3) * self.pu_stride;
746 self.blk_pos = (xpos >> 2) + (ypos >> 2) * self.blk_stride;
749 self.reconstruct_info(hdr, &cbh, size)?;
751 let split_i4x4 = (cbh.cu_type == CUType::Intra) && (size == 8) && (cbh.pu_type == PUType::Quarters);
754 let itype = self.blk_info[self.blk_pos].imode;
756 let dstride = buf.stride[0];
757 let off = xpos + ypos * dstride;
758 let dst = &mut buf.data;
759 self.populate_ipred(hdr, dst, 0, dstride, 0, 0, size, true);
760 self.ipred.pred_angle(dst, off, dstride, size, itype as usize, true);
763 let dstride = buf.stride[comp];
764 let soff = buf.offset[comp];
765 let off = soff + (xpos >> 1) + (ypos >> 1) * dstride;
766 let mut dst = &mut buf.data;
767 self.populate_ipred(hdr, dst, soff, dstride, 0, 0, size >> 1, false);
768 self.ipred.pred_angle(&mut dst, off, dstride, size >> 1, itype as usize, false);
772 let mut mv_x = xpos >> 2;
773 let mut mv_y = ypos >> 2;
774 let mut mv_pos = mv_x + mv_y * self.blk_stride;
775 for part_no in 0..cbh.pu_type.get_num_mvs() {
776 let (mv_w, mv_h) = cbh.pu_type.get_mv_size(part_no, size);
777 let mv = self.blk_info[mv_pos].mv;
784 if hdr.ftype != FrameType::B {
785 if let Some(ref prevbuf) = self.ipbs.get_lastref() {
786 self.dsp.do_mc(buf, prevbuf, bx, by, bw, bh, mv.f_mv, false);
789 if let Some(ref prevbuf) = self.ipbs.get_b_fwdref() {
790 self.dsp.do_mc(buf, prevbuf, bx, by, bw, bh, mv.f_mv, false);
795 if let Some(ref prevbuf) = self.ipbs.get_nextref() {
796 self.dsp.do_mc(buf, prevbuf, bx, by, bw, bh, mv.f_mv, false);
800 validate!(hdr.ftype == FrameType::B);
801 if let Some(ref prevbuf) = self.ipbs.get_b_bwdref() {
802 self.dsp.do_mc(buf, prevbuf, bx, by, bw, bh, mv.b_mv, false);
805 MVRef::Ref0AndBRef => {
806 validate!(hdr.ftype == FrameType::B);
807 if let (Some(ref prevbuf), Some(ref nextbuf)) = (self.ipbs.get_b_fwdref(), self.ipbs.get_b_bwdref()) {
808 self.dsp.do_mc(buf, prevbuf, bx, by, bw, bh, mv.f_mv, false);
810 let mut avg_buf = NASimpleVideoFrame::from_video_buf(&mut self.avg_buf).unwrap();
811 self.dsp.do_mc(&mut avg_buf, nextbuf, bx, by, bw, bh, mv.b_mv, true);
813 self.dsp.do_avg(buf, &self.avg_buf, bx, by, bw, bh);
818 if cbh.pu_type == PUType::Quarters {
823 mv_pos += mv_h * self.blk_stride - mv_w;
827 } else if cbh.pu_type.has_hor_split() {
828 mv_pos += mv_h * self.blk_stride;
830 } else if cbh.pu_type.has_ver_split() {
837 if cbh.ttype != TransformType::None {
838 self.y_coeffs = [0; 16 * 16];
839 self.u_coeffs = [0; 8 * 8];
840 self.v_coeffs = [0; 8 * 8];
842 let is_intra = cbh.cu_type == CUType::Intra;
843 let cb_pos = ((xpos & 63) >> 3) + ((ypos & 63) >> 3) * 8;
845 TransformType::T4X4 => {
846 let subset = if is_intra { 0 } else { 2 };
850 cbp16 = rv6_decode_cbp16(br, &self.cbs, subset, self.sel_qp)?;
855 self.coded_blk[cb_pos + 0] = true;
856 self.coded_blk[cb_pos + 1] = true;
857 self.coded_blk[cb_pos + 8] = true;
858 self.coded_blk[cb_pos + 9] = true;
859 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)?;
863 if ((cbp16 >> i) & 1) != 0 {
864 self.dsp.transform4x4(&mut self.y_coeffs[i * 16..][..16]);
865 let dstride = buf.stride[0];
866 let off = xpos + x * 4 + (ypos + y * 4) * dstride;
867 let mut dst = &mut buf.data;
868 self.dsp.add_block(&mut dst, off, dstride, &self.y_coeffs[i*16..][..16], 4);
875 let xoff = (xpos >> 1) + x * 4;
876 let yoff = (ypos >> 1) + y * 4;
877 if ((cbp16 >> (16 + i)) & 1) != 0 {
878 self.dsp.transform4x4(&mut self.u_coeffs[i * 16..][..16]);
879 let dstride = buf.stride[1];
880 let off = buf.offset[1] + xoff + yoff * dstride;
881 let mut dst = &mut buf.data;
882 self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs[i * 16..][..16], 4);
884 if ((cbp16 >> (20 + i)) & 1) != 0 {
885 self.dsp.transform4x4(&mut self.v_coeffs[i * 16..][..16]);
886 let dstride = buf.stride[2];
887 let off = buf.offset[2] + xoff + yoff * dstride;
888 let mut dst = &mut buf.data;
889 self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs[i * 16..][..16], 4);
895 let cbp8 = rv6_decode_cbp8(br, &self.cbs, subset, self.sel_qp)?;
897 self.coded_blk[cb_pos] = true;
898 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)?;
901 let xoff = (i & 1) * 4;
902 let yoff = (i & 2) * 2;
904 let dstride = buf.stride[0];
905 let off = xpos + xoff + (ypos + yoff) * dstride;
906 let mut dst = &mut buf.data;
907 self.populate_ipred(hdr, dst, 0, dstride, xoff, yoff, 4, true);
908 let itype = self.blk_info[self.blk_pos + (i & 1) + (i >> 1) * self.blk_stride].imode;
909 self.ipred.pred_angle(&mut dst, off, dstride, 4, itype as usize, false);
911 if ((cbp8 >> i) & 1) != 0 {
912 let blk = &mut self.y_coeffs[i * 16..][..16];
913 self.dsp.transform4x4(blk);
914 let dstride = buf.stride[0];
915 let soff = buf.offset[0];
916 let off = soff + xpos + xoff + (ypos + yoff) * dstride;
917 let mut dst = &mut buf.data;
918 self.dsp.add_block(&mut dst, off, dstride, blk, 4);
921 if ((cbp8 >> 4) & 1) != 0 {
922 self.dsp.transform4x4(&mut self.u_coeffs);
923 let dstride = buf.stride[1];
924 let soff = buf.offset[1];
925 let off = soff + (xpos >> 1) + (ypos >> 1) * dstride;
926 let mut dst = &mut buf.data;
927 self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs, 4);
929 if ((cbp8 >> 5) & 1) != 0 {
930 self.dsp.transform4x4(&mut self.v_coeffs);
931 let dstride = buf.stride[2];
932 let soff = buf.offset[2];
933 let off = soff + (xpos >> 1) + (ypos >> 1) * dstride;
934 let mut dst = &mut buf.data;
935 self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs, 4);
939 TransformType::T8X8 => {
940 let subset = if is_intra { 1 } else { 3 };
941 let cbp8 = rv6_decode_cbp8(br, &self.cbs, subset, self.sel_qp)?;
943 self.coded_blk[cb_pos] = true;
944 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)?;
945 if (cbp8 & 0xF) != 0 {
946 self.dsp.transform8x8(&mut self.y_coeffs);
947 let dstride = buf.stride[0];
948 let off = xpos + ypos * dstride;
949 let mut dst = &mut buf.data;
950 self.dsp.add_block(&mut dst, off, dstride, &self.y_coeffs, 8);
952 if ((cbp8 >> 4) & 1) != 0 {
953 self.dsp.transform4x4(&mut self.u_coeffs);
954 let dstride = buf.stride[1];
955 let soff = buf.offset[1];
956 let off = soff + (xpos >> 1) + (ypos >> 1) * dstride;
957 let mut dst = &mut buf.data;
958 self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs, 4);
960 if ((cbp8 >> 5) & 1) != 0 {
961 self.dsp.transform4x4(&mut self.v_coeffs);
962 let dstride = buf.stride[2];
963 let soff = buf.offset[2];
964 let off = soff + (xpos >> 1) + (ypos >> 1) * dstride;
965 let mut dst = &mut buf.data;
966 self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs, 4);
970 TransformType::T16X16 => {
971 let subset = if is_intra { 1 } else { 3 };
972 let num_clusters = size >> 4;
973 let cl_cbp = br.read((num_clusters * num_clusters) as u8)?;
974 for y in 0..num_clusters {
975 for x in 0..num_clusters {
976 if ((cl_cbp >> (x + y * num_clusters)) & 1) == 0 { continue; }
977 self.coded_blk[cb_pos + x * 2 + y * 2 * 8 + 0] = true;
978 self.coded_blk[cb_pos + x * 2 + y * 2 * 8 + 1] = true;
979 self.coded_blk[cb_pos + x * 2 + y * 2 * 8 + 8] = true;
980 self.coded_blk[cb_pos + x * 2 + y * 2 * 8 + 9] = true;
981 let super_cbp = rv6_decode_cbp16(br, &self.cbs, subset, self.sel_qp)?;
983 self.y_coeffs = [0; 16 * 16];
984 self.u_coeffs = [0; 8 * 8];
985 self.v_coeffs = [0; 8 * 8];
986 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)?;
987 if (super_cbp & 0xFFFF) != 0 {
988 self.dsp.transform16x16(&mut self.y_coeffs);
989 let dstride = buf.stride[0];
990 let off = xpos + x * 16 + (ypos + y * 16) * dstride;
991 let mut dst = &mut buf.data;
992 self.dsp.add_block(&mut dst, off, dstride, &self.y_coeffs, 16);
994 if ((super_cbp >> 16) & 0xF) != 0 {
995 self.dsp.transform8x8(&mut self.u_coeffs);
996 let dstride = buf.stride[1];
997 let soff = buf.offset[1];
998 let off = soff + (xpos >> 1) + x * 8 + ((ypos >> 1) + y * 8) * dstride;
999 let mut dst = &mut buf.data;
1000 self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs, 8);
1002 if ((super_cbp >> 20) & 0xF) != 0 {
1003 self.dsp.transform8x8(&mut self.v_coeffs);
1004 let dstride = buf.stride[2];
1005 let soff = buf.offset[2];
1006 let off = soff + (xpos >> 1) + x * 8 + ((ypos >> 1) + y * 8) * dstride;
1007 let mut dst = &mut buf.data;
1008 self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs, 8);
1019 fn reconstruct_info(&mut self, hdr: &FrameHeader, cbh: &CBHeader, size: usize) -> DecoderResult<()>{
1020 let mut pui = PUInfo::default();
1021 let pu_size = size >> 3;
1022 pui.cu_type = cbh.cu_type;
1023 pui.ttype = cbh.ttype;
1024 if (cbh.cu_type == CUType::Intra) && (cbh.pu_type == PUType::Quarters) { // very special case
1025 self.pu_info[self.pu_pos] = pui;
1028 let imode = self.reconstruct_intra(hdr, cbh, 4, x + y * 2);
1029 validate!(imode <= MAX_IMODE);
1030 self.blk_info[self.blk_pos + x + y * self.blk_stride].imode = imode;
1031 self.blk_info[self.blk_pos + x + y * self.blk_stride].mv = MVInfo::default();
1038 self.pu_info[self.pu_pos] = pui;
1039 let imode = self.reconstruct_intra(hdr, cbh, size, 0);
1040 validate!(imode <= MAX_IMODE);
1041 for y in 0..(size >> 2) {
1042 for x in 0..(size >> 2) {
1043 self.blk_info[self.blk_pos + x + y * self.blk_stride].imode = imode;
1047 CUType::InterMV => {
1048 let mut mv_x = self.xpos >> 2;
1049 let mut mv_y = self.ypos >> 2;
1050 let mut mv_pos = self.blk_pos;
1051 let pu_type = cbh.pu_type;
1052 for part_no in 0..pu_type.get_num_mvs() {
1053 let (mv_w, mv_h) = pu_type.get_mv_size(part_no, size);
1054 let mv = self.predict_mv(hdr, mv_x, mv_y, mv_w, &cbh.mv[part_no]);
1057 self.blk_info[mv_pos + x + y * self.blk_stride].mv = mv;
1060 if pu_type == PUType::Quarters {
1065 mv_pos += mv_h * self.blk_stride - mv_w;
1069 } else if pu_type.has_hor_split() {
1070 mv_pos += mv_h * self.blk_stride;
1072 } else if pu_type.has_ver_split() {
1079 let skip_idx = cbh.mv[0].mvref.get_skip_mv_num();
1080 let mut skip_cand: UniqueList<MVInfo> = UniqueList::new(4);
1081 self.fill_skip_cand(hdr, &mut skip_cand, size);
1082 let mv = skip_cand.list[skip_idx];
1084 let mv_size = size >> 2;
1085 for y in 0..mv_size {
1086 for x in 0..mv_size {
1087 self.blk_info[self.blk_pos + x + y * self.blk_stride].mv = mv;
1092 for y in 0..pu_size {
1093 for x in 0..pu_size {
1094 self.pu_info[self.pu_pos + x + y * self.pu_stride] = pui;
1099 fn reconstruct_intra(&self, hdr: &FrameHeader, cbh: &CBHeader, size: usize, sub: usize) -> u8 {
1100 match cbh.imode[0] {
1101 IntraMode::DC64 => { return 1; },
1102 IntraMode::Plane64 => { return 0; },
1105 // form list of predictors
1106 let blk_pos = self.blk_pos + (sub & 1) + (sub >> 1) * self.blk_stride;
1107 let mut ipm_cand: UniqueList<u8> = UniqueList::new(3);
1108 if hdr.has_top_block(self.xpos, self.ypos, (sub & 1) * 4, 0, size) {
1109 let pu = &self.pu_info[self.pu_pos - self.pu_stride];
1111 ipm_cand.add(self.blk_info[self.blk_pos + (sub & 1) - self.blk_stride].imode);
1114 if hdr.has_left_block(self.xpos, self.ypos, 0, (sub & 2) * 2, size) {
1115 let pu = &self.pu_info[self.pu_pos - 1];
1117 ipm_cand.add(self.blk_info[blk_pos - 1 - (sub & 1)].imode);
1120 let tl_x = if (sub & 2) == 0 { self.xpos + (sub & 1) * 4 } else { self.xpos };
1121 let tl_y = self.ypos + (sub & 2) * 4;
1122 if (tl_x > 0) && (tl_y > 0) {
1123 let pu = match sub {
1124 0 => &self.pu_info[self.pu_pos - self.pu_stride - 1],
1125 1 => &self.pu_info[self.pu_pos - self.pu_stride],
1126 2 => &self.pu_info[self.pu_pos - 1],
1127 _ => &self.pu_info[self.pu_pos - 1],
1131 ipm_cand.add(self.blk_info[blk_pos - self.blk_stride - 1].imode);
1133 ipm_cand.add(self.blk_info[blk_pos - self.blk_stride - 2].imode);
1137 for el in RV60_CANDIDATE_INTRA_ANGLES.iter() {
1140 // actually decode prediction mode
1141 match cbh.imode[sub] {
1142 IntraMode::Index(idx) => {
1143 ipm_cand.list[idx as usize]
1145 IntraMode::Mode(mode) => {
1146 let mut imode = mode;
1147 let mut ipm_cs: [u8; 3] = [ipm_cand.list[0], ipm_cand.list[1], ipm_cand.list[2]];
1149 for ic in ipm_cs.iter() {
1156 _ => unreachable!(),
1159 fn populate_ipred(&mut self, hdr: &FrameHeader, src: &[u8], soff: usize, stride: usize, xoff: usize, yoff: usize, size: usize, is_luma: bool) {
1160 let src_off = if is_luma {
1161 soff + self.xpos + xoff + (self.ypos + yoff) * stride
1163 soff + (self.xpos >> 1) + (self.ypos >> 1) * stride
1165 self.ipred = IntraPredContext::new();
1166 if (self.ypos + yoff) > 0 {
1167 self.ipred.has_t = true;
1169 self.ipred.t[x + 1] = src[src_off - stride + x];
1171 if (is_luma && hdr.has_top_right_block(self.xpos, self.ypos, xoff, yoff, size)) ||
1172 (!is_luma && hdr.has_top_right_block(self.xpos, self.ypos, 0, 0, size << 1)) {
1173 self.ipred.has_tr = true;
1174 for x in size..size*2 {
1175 self.ipred.t[x + 1] = src[src_off - stride + x];
1179 self.ipred.t[size + i + 1] = self.ipred.t[size];
1182 if (self.xpos + xoff) > 0 {
1183 self.ipred.t[0] = src[src_off - stride - 1];
1186 if (self.xpos + xoff) > 0 {
1187 self.ipred.has_l = true;
1189 self.ipred.l[y + 1] = src[src_off - 1 + y * stride];
1191 if (is_luma && hdr.has_left_down_block(self.xpos, self.ypos, xoff, yoff, size)) ||
1192 (!is_luma && hdr.has_left_down_block(self.xpos, self.ypos, 0, 0, size << 1)) {
1193 self.ipred.has_ld = true;
1194 for y in size..size*2 {
1195 self.ipred.l[y + 1] = src[src_off - 1 + y * stride];
1199 self.ipred.l[size + i + 1] = self.ipred.l[size];
1202 if (self.ypos + yoff) > 0 {
1203 self.ipred.l[0] = src[src_off - stride - 1];
1207 fn predict_mv(&self, hdr: &FrameHeader, mv_x: usize, mv_y: usize, mv_w: usize, mvi: &MVInfo) -> MVInfo {
1208 let mv_pos = mv_x + mv_y * self.blk_stride;
1211 if mvi.mvref.is_fwd() {
1212 let mut mv_cand: [MV; 3] = [ZERO_MV; 3];
1213 let mut mv_cand_size: usize = 0;
1215 let ref_mv = &self.blk_info[mv_pos - 1].mv;
1216 if ref_mv.matches_fwd(mvi.mvref) {
1217 mv_cand[mv_cand_size] = ref_mv.f_mv;
1222 let ref_mv = &self.blk_info[mv_pos - self.blk_stride].mv;
1223 if ref_mv.matches_fwd(mvi.mvref) {
1224 mv_cand[mv_cand_size] = ref_mv.f_mv;
1228 if hdr.has_top_block(mv_x << 2, mv_y << 2, mv_w << 2, 0, 4) {
1229 let ref_mv = &self.blk_info[mv_pos - self.blk_stride + mv_w].mv;
1230 if ref_mv.matches_fwd(mvi.mvref) {
1231 mv_cand[mv_cand_size] = ref_mv.f_mv;
1235 f_mv = match mv_cand_size {
1237 let x = mv_cand[0].x + mv_cand[1].x + mv_cand[2].x;
1238 let y = mv_cand[0].y + mv_cand[1].y + mv_cand[2].y;
1239 if mv_cand_size == 1 {
1242 MV { x: x >> 1, y: y >> 1 }
1245 3 => MV::pred(mv_cand[0], mv_cand[1], mv_cand[2]),
1251 if mvi.mvref.is_bwd() {
1252 let mut mv_cand: [MV; 3] = [ZERO_MV; 3];
1253 let mut mv_cand_size: usize = 0;
1255 let ref_mv = &self.blk_info[mv_pos - 1].mv;
1256 if ref_mv.matches_bwd(mvi.mvref) {
1257 mv_cand[mv_cand_size] = ref_mv.b_mv;
1262 let ref_mv = &self.blk_info[mv_pos - self.blk_stride].mv;
1263 if ref_mv.matches_bwd(mvi.mvref) {
1264 mv_cand[mv_cand_size] = ref_mv.b_mv;
1268 if hdr.has_top_block(mv_x << 2, mv_y << 2, mv_w << 2, 0, 4) {
1269 let ref_mv = &self.blk_info[mv_pos - self.blk_stride + mv_w].mv;
1270 if ref_mv.matches_bwd(mvi.mvref) {
1271 mv_cand[mv_cand_size] = ref_mv.b_mv;
1275 b_mv = match mv_cand_size {
1277 let x = mv_cand[0].x + mv_cand[1].x + mv_cand[2].x;
1278 let y = mv_cand[0].y + mv_cand[1].y + mv_cand[2].y;
1279 if mv_cand_size == 1 {
1282 MV { x: x >> 1, y: y >> 1 }
1285 3 => MV::pred(mv_cand[0], mv_cand[1], mv_cand[2]),
1292 MVInfo { f_mv: mvi.f_mv + f_mv, b_mv: mvi.b_mv + b_mv, mvref: mvi.mvref }
1294 fn fill_skip_cand(&mut self, hdr: &FrameHeader, skip_cand: &mut UniqueList<MVInfo>, size: usize) {
1295 let mv_size = size >> 2;
1298 let mv = &self.blk_info[self.blk_pos - 1].mv;
1304 let mv = &self.blk_info[self.blk_pos - self.blk_stride].mv;
1309 if hdr.has_top_right_block(self.xpos, self.ypos, 0, 0, size) {
1310 let mv = &self.blk_info[self.blk_pos - self.blk_stride + mv_size].mv;
1315 if hdr.has_left_down_block(self.xpos, self.ypos, 0, 0, size) {
1316 let mv = &self.blk_info[self.blk_pos + self.blk_stride * mv_size - 1].mv;
1321 if hdr.has_left_block(self.xpos, self.ypos, 0, 0, size) {
1322 let mv = &self.blk_info[self.blk_pos + self.blk_stride * (mv_size - 1) - 1].mv;
1327 if hdr.has_top_block(self.xpos, self.ypos, 0, 0, size) {
1328 let mv = &self.blk_info[self.blk_pos - self.blk_stride + mv_size - 1].mv;
1333 if (self.xpos > 0) && (self.ypos > 0) {
1334 let mv = &self.blk_info[self.blk_pos - self.blk_stride - 1].mv;
1339 for i in skip_cand.fill..4 {
1340 skip_cand.list[i] = MVInfo { f_mv: ZERO_MV, b_mv: ZERO_MV, mvref: MVRef::Ref0 };
1343 fn deblock_cb_tree(&mut self, buf: &mut NASimpleVideoFrame<u8>, hdr: &FrameHeader, xpos: usize, ypos: usize, log_size: u8) {
1344 if (xpos >= hdr.width) || (ypos >= hdr.height) { return; }
1345 let split = (log_size > 3) && self.cu_splits.pop().unwrap();
1347 let hsize = 1 << (log_size - 1);
1348 self.deblock_cb_tree(buf, hdr, xpos, ypos, log_size - 1);
1349 self.deblock_cb_tree(buf, hdr, xpos + hsize, ypos, log_size - 1);
1350 self.deblock_cb_tree(buf, hdr, xpos, ypos + hsize, log_size - 1);
1351 self.deblock_cb_tree(buf, hdr, xpos + hsize, ypos + hsize, log_size - 1);
1353 let pu_pos = (xpos >> 3) + (ypos >> 3) * self.pu_stride;
1354 let cu_type = self.pu_info[pu_pos].cu_type;
1355 let tsize = if self.pu_info[pu_pos].ttype == TransformType::T16X16 { 4 } else { 3 };
1356 let ntiles = 1 << (log_size - tsize);
1357 let dparams = RV60DeblockParams {
1358 deblock_chroma: hdr.deblock_chroma,
1361 dblkstride: self.dblk.stride,
1363 for ty in 0..ntiles {
1364 for tx in 0..ntiles {
1365 let x = xpos + (tx << tsize);
1366 let y = ypos + (ty << tsize);
1367 let cb_pos = ((x & 63) >> 3) + ((y & 63) >> 3) * 8;
1368 if cu_type == CUType::Intra {
1369 self.dblk.set_strength(x, y, 1 << tsize, self.qp, 2);
1370 } else if (cu_type != CUType::Skip) && self.coded_blk[cb_pos] {
1371 self.dblk.set_strength(x, y, 1 << tsize, self.qp, 1);
1373 self.dblk.set_strength(x, y, 1 << tsize, self.qp, 0);
1374 self.derive_deblock_strength(x, y, 1 << (tsize - 2));
1376 self.dsp.do_deblock(&dparams, buf, x, y, 1 << tsize,
1377 self.dblk.top_str.as_slice(),
1378 self.dblk.left_str.as_slice(),
1379 self.dblk.get_pos(x, y));
1384 fn derive_deblock_strength(&mut self, xpos: usize, ypos: usize, size4: usize) {
1385 let blk_pos = (xpos >> 2) + (ypos >> 2) * self.blk_stride;
1386 let mut dblk_pos = self.dblk.get_pos(xpos, ypos);
1388 let top_blk_pos = blk_pos - self.blk_stride;
1390 if self.dblk.get_top_strength(dblk_pos + i) == 0 {
1391 if self.blk_info[blk_pos + i].mv.is_deblock_cand(&self.blk_info[top_blk_pos + i].mv) {
1392 self.dblk.set_top_strength(dblk_pos + i, 1);
1399 if self.dblk.get_left_strength(dblk_pos) == 0 {
1400 if self.blk_info[blk_pos + i].mv.is_deblock_cand(&self.blk_info[blk_pos + i - 1].mv) {
1401 self.dblk.set_left_strength(dblk_pos, 1);
1404 dblk_pos += self.dblk.stride;
1410 impl NADecoder for RealVideo60Decoder {
1411 fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
1412 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
1413 let fmt = YUV420_FORMAT;
1414 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, fmt));
1415 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
1417 let edata = info.get_extradata().unwrap();
1418 let src: &[u8] = &edata;
1420 if src.len() < 8 { return Err(DecoderError::InvalidData); }
1421 let mut mr = MemoryReader::new_read(src);
1422 let mut br = ByteReader::new(&mut mr);
1423 let _flags = br.read_u32be()?;
1424 let version = br.read_u32be()?;
1425 let _unk = br.read_u16be()?;
1426 validate!((version >> 28) == 4);
1427 // then width and height again as 16be
1429 //self.bd.width = vinfo.get_width();
1430 //self.bd.height = vinfo.get_height();
1431 //self.frmmgr.clear();
1433 supp.pool_u8.set_dec_bufs(3);
1434 supp.pool_u8.prealloc_video(NAVideoInfo::new(vinfo.get_width(), vinfo.get_height(), false, fmt), 6)?;
1439 Err(DecoderError::InvalidData)
1442 fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
1443 let src = pkt.get_buffer();
1445 validate!(src.len() > 9);
1446 let hsize = (src[0] as usize) * 8 + 9;
1447 let mut br = BitReader::new(&src[hsize..], BitReaderMode::BE);
1448 let hdr = FrameHeader::read(&mut br)?;
1449 match self.skip_mode {
1450 FrameSkipMode::None => {},
1451 FrameSkipMode::KeyframesOnly => {
1452 if hdr.ftype == FrameType::B {
1453 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
1454 frm.set_frame_type(FrameType::Skip);
1455 return Ok(frm.into_ref());
1458 FrameSkipMode::IntraOnly => {
1459 if hdr.ftype != FrameType::I {
1460 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
1461 frm.set_frame_type(FrameType::Skip);
1462 return Ok(frm.into_ref());
1467 let mut slices: Vec<usize> = Vec::new();
1468 hdr.parse_slice_sizes(&mut br, &mut slices)?;
1471 if self.ipbs.get_lastref().is_none() {
1472 return Err(DecoderError::MissingReference);
1476 if self.ipbs.get_lastref().is_none() {
1477 return Err(DecoderError::MissingReference);
1479 if self.ipbs.get_nextref().is_none() {
1480 return Err(DecoderError::MissingReference);
1486 let tmp_vinfo = NAVideoInfo::new(hdr.width, hdr.height, false, YUV420_FORMAT);
1487 let ret = supp.pool_u8.get_free();
1489 return Err(DecoderError::AllocError);
1491 let mut buf = ret.unwrap();
1492 if buf.get_info() != tmp_vinfo {
1494 supp.pool_u8.reset();
1495 supp.pool_u8.prealloc_video(tmp_vinfo, 6)?;
1496 let ret = supp.pool_u8.get_free();
1498 return Err(DecoderError::AllocError);
1503 let cu_w = hdr.get_width_cu();
1504 let cu_h = hdr.get_height_cu();
1505 self.pu_stride = cu_w << 3;
1506 self.pu_info.resize(self.pu_stride * (cu_h << 3), PUInfo::default());
1507 self.blk_stride = cu_w << 4;
1508 self.blk_info.truncate(0);
1509 self.blk_info.resize(self.blk_stride * (cu_h << 4), BlockInfo::default());
1511 self.dblk.reinit(hdr.width, hdr.height);
1513 let mut off = hsize + ((br.tell() >> 3) as usize);
1514 let mut dframe = NASimpleVideoFrame::from_video_buf(&mut buf).unwrap();
1515 for (cu_y, size) in slices.into_iter().enumerate() {
1516 self.decode_cu_line(&mut dframe, &hdr, &src[off..][..size], cu_y)?;
1519 if (hdr.ftype == FrameType::I) || (hdr.ftype == FrameType::P) {
1520 self.ipbs.add_frame(buf.clone());
1523 if hdr.ftype != FrameType::B {
1524 self.ref0_pts = self.ref1_pts;
1525 self.ref1_pts = pkt.get_pts().unwrap_or(0);
1526 self.ref0_ts = self.ref1_ts;
1527 self.ref1_ts = hdr.ts as u64;
1528 if (self.ref1_pts > self.ref0_pts) && (self.ref1_ts > self.ref0_ts) {
1529 self.ts_scale = (self.ref1_pts - self.ref0_pts) / (self.ref1_ts - self.ref0_ts);
1532 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::Video(buf));
1533 frm.set_keyframe(hdr.ftype == FrameType::I);
1534 if hdr.ftype == FrameType::B {
1535 let pts = self.ref0_pts + ((hdr.ts as u64) - self.ref0_ts) * self.ts_scale;
1536 frm.set_pts(Some(pts));
1538 frm.set_frame_type(hdr.ftype);
1541 fn flush(&mut self) {
1546 const DECODER_OPTIONS: &[NAOptionDefinition] = &[
1547 NAOptionDefinition {
1548 name: FRAME_SKIP_OPTION, description: FRAME_SKIP_OPTION_DESC,
1549 opt_type: NAOptionDefinitionType::String(Some(&[
1550 FRAME_SKIP_OPTION_VAL_NONE,
1551 FRAME_SKIP_OPTION_VAL_KEYFRAME,
1552 FRAME_SKIP_OPTION_VAL_INTRA
1556 impl NAOptionHandler for RealVideo60Decoder {
1557 fn get_supported_options(&self) -> &[NAOptionDefinition] { DECODER_OPTIONS }
1558 fn set_options(&mut self, options: &[NAOption]) {
1559 for option in options.iter() {
1560 for opt_def in DECODER_OPTIONS.iter() {
1561 if opt_def.check(option).is_ok() {
1562 match (option.name, &option.value) {
1563 (FRAME_SKIP_OPTION, NAValue::String(ref str)) => {
1564 if let Ok(smode) = FrameSkipMode::from_str(str) {
1565 self.skip_mode = smode;
1574 fn query_option_value(&self, name: &str) -> Option<NAValue> {
1576 FRAME_SKIP_OPTION => Some(NAValue::String(self.skip_mode.to_string())),
1582 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
1583 Box::new(RealVideo60Decoder::new())
1588 use nihav_core::codecs::RegisteredDecoders;
1589 use nihav_core::demuxers::RegisteredDemuxers;
1590 use nihav_codec_support::test::dec_video::*;
1591 use crate::realmedia_register_all_codecs;
1592 use crate::realmedia_register_all_demuxers;
1595 let mut dmx_reg = RegisteredDemuxers::new();
1596 realmedia_register_all_demuxers(&mut dmx_reg);
1597 let mut dec_reg = RegisteredDecoders::new();
1598 realmedia_register_all_codecs(&mut dec_reg);
1600 test_file_decoding("realmedia", "assets/RV/RV60.rmhd", Some(4000), true, false, None/*Some("rv60")*/, &dmx_reg, &dec_reg);