]> git.nihav.org Git - nihav.git/commitdiff
h264: make some structures shareable
authorKostya Shishkov <kostya.shishkov@gmail.com>
Wed, 14 Jun 2023 16:47:18 +0000 (18:47 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Wed, 14 Jun 2023 16:47:18 +0000 (18:47 +0200)
This is a preparation work for an upcoming multi-threaded decoding.

nihav-itu/src/codecs/h264/decoder_st.rs
nihav-itu/src/codecs/h264/pic_ref.rs
nihav-itu/src/codecs/h264/sets.rs
nihav-itu/src/codecs/h264/slice.rs

index 3cffc6a278c04f3dae0d6e27513b3e7d9f54d353..63815f8683a490cad8cbe89940016dcf3c19e925 100644 (file)
@@ -1,3 +1,5 @@
+use std::sync::Arc;
+
 use nihav_core::codecs::*;
 use nihav_core::io::bitreader::*;
 
@@ -9,9 +11,9 @@ struct H264Decoder {
     height:     usize,
     num_mbs:    usize,
     nal_len:    u8,
-    sps:        Vec<SeqParameterSet>,
+    sps:        Vec<Arc<SeqParameterSet>>,
     cur_sps:    usize,
-    pps:        Vec<PicParameterSet>,
+    pps:        Vec<Arc<PicParameterSet>>,
     cur_pps:    usize,
 
     skip_mode:      FrameSkipMode,
@@ -104,7 +106,7 @@ impl H264Decoder {
                 let mut br = BitReader::new(&src[..(full_size + 7)/8], BitReaderMode::BE);
                                                     br.skip(8)?;
 
-                let slice_hdr = parse_slice_header(&mut br, &self.sps, &self.pps, is_idr, nal_ref_idc)?;
+                let slice_hdr = parse_slice_header(&mut br, self.sps.as_slice(), self.pps.as_slice(), is_idr, nal_ref_idc)?;
                 validate!(br.tell() < full_size);
                 let full_id;
                 if slice_hdr.first_mb_in_slice == 0 {
@@ -206,7 +208,7 @@ println!("PAFF?");
                             cur_mb: 0,
                             is_ref: nal_ref_idc != 0,
                             long_term: get_long_term_id(is_idr, &slice_hdr),
-                            mv_info: FrameMV::new(sps.pic_width_in_mbs, sps.pic_height_in_mbs),
+                            mv_info: NABufferRef::new(FrameMV::new(sps.pic_width_in_mbs, sps.pic_height_in_mbs)),
                         });
                 }
 
@@ -245,7 +247,7 @@ println!("PAFF?");
              6 => {}, //SEI
              7 => {
                 let sps = parse_sps(&src[1..])?;
-                self.sps.push(sps);
+                self.sps.push(Arc::new(sps));
             },
              8 => {
                 validate!(full_size >= 8 + 16);
@@ -253,7 +255,7 @@ println!("PAFF?");
                 let mut found = false;
                 for stored_pps in self.pps.iter_mut() {
                     if stored_pps.pic_parameter_set_id == pps.pic_parameter_set_id {
-                        *stored_pps = pps.clone();
+                        *stored_pps = Arc::clone(&pps);
                         found = true;
                         break;
                     }
index bf838be4204e1cc4bf096e8fa02edb03da1bc552..fd2ae974fa557e7ee8203cdae60858d08191b3c6 100644 (file)
@@ -1,5 +1,6 @@
 use nihav_core::codecs::DecoderResult;
 use nihav_core::frame::{FrameType, NAVideoBufferRef};
+use nihav_core::refs::*;
 use nihav_codec_support::codecs::MV;
 use super::sets::SeqParameterSet;
 use super::slice::*;
@@ -15,7 +16,7 @@ pub struct PictureInfo {
     pub is_ref:     bool,
     pub long_term:  Option<usize>,
 
-    pub mv_info:    FrameMV, //todo replace with refcounted index to a pool
+    pub mv_info:    NABufferRef<FrameMV>,
 }
 
 #[derive(Clone,Copy,Default, Debug)]
index 61cf0a8994a84f147dc0be06d8aa0498444cae54..2d879bdd7b87bc1a682e294a168939a6a3dbdae7 100644 (file)
@@ -1,3 +1,5 @@
+use std::sync::Arc;
+
 use nihav_core::codecs::{DecoderResult, DecoderError};
 use nihav_core::io::bitreader::*;
 
@@ -303,7 +305,7 @@ pub struct PicParameterSet {
     pub second_chroma_qp_index_offset:      i8,
 }
 
-pub fn parse_pps(src: &[u8], sps_arr: &[SeqParameterSet], full_size: usize) -> DecoderResult<PicParameterSet> {
+pub fn parse_pps(src: &[u8], sps_arr: &[Arc<SeqParameterSet>], full_size: usize) -> DecoderResult<Arc<PicParameterSet>> {
     let mut br = BitReader::new(src, BitReaderMode::BE);
     let mut pps: PicParameterSet = unsafe { std::mem::zeroed() };
 
@@ -417,5 +419,5 @@ println!("slice mode!");
         pps.second_chroma_qp_index_offset = pps.chroma_qp_index_offset;
     }
 
-    Ok(pps)
+    Ok(Arc::new(pps))
 }
index e5a72ad8ec785718e4dc86191bec0e2e87fd31a6..ad1d244afe5cfd84b29fbe0060697971491c0558 100644 (file)
@@ -1,3 +1,5 @@
+use std::sync::Arc;
+
 use nihav_core::codecs::{DecoderResult, DecoderError};
 use nihav_core::frame::FrameType;
 use nihav_core::io::bitreader::*;
@@ -154,7 +156,7 @@ pub fn parse_slice_header_minimal(br: &mut BitReader) -> DecoderResult<(usize, S
 
 #[allow(clippy::cognitive_complexity)]
 #[allow(clippy::manual_range_contains)]
-pub fn parse_slice_header(br: &mut BitReader, sps_arr: &[SeqParameterSet], pps_arr: &[PicParameterSet], is_idr: bool, nal_ref_idc: u8) -> DecoderResult<SliceHeader> {
+pub fn parse_slice_header(br: &mut BitReader, sps_arr: &[Arc<SeqParameterSet>], pps_arr: &[Arc<PicParameterSet>], is_idr: bool, nal_ref_idc: u8) -> DecoderResult<SliceHeader> {
     let mut hdr: SliceHeader = unsafe { std::mem::zeroed() };
 
     hdr.first_mb_in_slice                           = br.read_ue()? as usize;