Skip to content

Commit

Permalink
codec/h264: remove generic argument from NaluReader
Browse files Browse the repository at this point in the history
NaluReaders are always short-lived and want to work with a reference, so
we can just build them with it and simplify the code.
  • Loading branch information
Gnurou committed Sep 25, 2023
1 parent 04124d1 commit 23746f7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 60 deletions.
8 changes: 4 additions & 4 deletions src/codec/h264/nalu_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use bytes::Buf;

/// A bit reader for h264 bitstreams. It properly handles emulation-prevention
/// bytes and stop bits.
pub struct NaluReader<T> {
pub(crate) struct NaluReader<'a> {
/// A reference into the next unread byte in the stream.
data: Cursor<T>,
data: Cursor<&'a [u8]>,
/// Contents of the current byte. First unread bit starting at position 8 -
/// num_remaining_bits_in_curr_bytes.
curr_byte: u32,
Expand All @@ -23,8 +23,8 @@ pub struct NaluReader<T> {
num_epb: usize,
}

impl<T: AsRef<[u8]>> NaluReader<T> {
pub fn new(data: T) -> Self {
impl<'a> NaluReader<'a> {
pub fn new(data: &'a [u8]) -> Self {
Self {
data: Cursor::new(data),
curr_byte: Default::default(),
Expand Down
35 changes: 14 additions & 21 deletions src/codec/h264/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,8 +1534,8 @@ impl Parser {
}
}

fn parse_scaling_list<T: AsRef<[u8]>, U: AsMut<[u8]>>(
r: &mut NaluReader<T>,
fn parse_scaling_list<U: AsMut<[u8]>>(
r: &mut NaluReader,
scaling_list: &mut U,
use_default: &mut bool,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -1565,10 +1565,7 @@ impl Parser {
Ok(())
}

fn parse_sps_scaling_lists<T: AsRef<[u8]>>(
r: &mut NaluReader<T>,
sps: &mut Sps,
) -> anyhow::Result<()> {
fn parse_sps_scaling_lists(r: &mut NaluReader, sps: &mut Sps) -> anyhow::Result<()> {
let scaling_lists4x4 = &mut sps.scaling_lists_4x4;
let scaling_lisst8x8 = &mut sps.scaling_lists_8x8;

Expand Down Expand Up @@ -1616,11 +1613,7 @@ impl Parser {
Ok(())
}

fn parse_pps_scaling_lists<T: AsRef<[u8]>>(
r: &mut NaluReader<T>,
pps: &mut Pps,
sps: &Sps,
) -> anyhow::Result<()> {
fn parse_pps_scaling_lists(r: &mut NaluReader, pps: &mut Pps, sps: &Sps) -> anyhow::Result<()> {
let scaling_lists4x4 = &mut pps.scaling_lists_4x4;
let scaling_lists8x8 = &mut pps.scaling_lists_8x8;

Expand Down Expand Up @@ -1689,7 +1682,7 @@ impl Parser {
Ok(())
}

fn parse_hrd<T: AsRef<[u8]>>(r: &mut NaluReader<T>, hrd: &mut HrdParams) -> anyhow::Result<()> {
fn parse_hrd(r: &mut NaluReader, hrd: &mut HrdParams) -> anyhow::Result<()> {
hrd.cpb_cnt_minus1 = r.read_ue_max(31)?;
hrd.bit_rate_scale = r.read_bits(4)?;
hrd.cpb_size_scale = r.read_bits(4)?;
Expand All @@ -1707,7 +1700,7 @@ impl Parser {
Ok(())
}

fn parse_vui<T: AsRef<[u8]>>(r: &mut NaluReader<T>, sps: &mut Sps) -> anyhow::Result<()> {
fn parse_vui(r: &mut NaluReader, sps: &mut Sps) -> anyhow::Result<()> {
let vui = &mut sps.vui_parameters;

vui.aspect_ratio_info_present_flag = r.read_bit()?;
Expand Down Expand Up @@ -2064,8 +2057,8 @@ impl Parser {
Ok(self.get_pps(key).unwrap())
}

fn parse_ref_pic_list_modification<T: AsRef<[u8]>>(
r: &mut NaluReader<T>,
fn parse_ref_pic_list_modification(
r: &mut NaluReader,
num_ref_idx_active_minus1: u8,
ref_list_mods: &mut Vec<RefPicListModification>,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -2102,8 +2095,8 @@ impl Parser {
Ok(())
}

fn parse_ref_pic_list_modifications<T: AsRef<[u8]>>(
r: &mut NaluReader<T>,
fn parse_ref_pic_list_modifications(
r: &mut NaluReader,
header: &mut SliceHeader,
) -> anyhow::Result<()> {
if !header.slice_type.is_i() && !header.slice_type.is_si() {
Expand Down Expand Up @@ -2131,8 +2124,8 @@ impl Parser {
Ok(())
}

fn parse_pred_weight_table<T: AsRef<[u8]>>(
r: &mut NaluReader<T>,
fn parse_pred_weight_table(
r: &mut NaluReader,
sps: &Sps,
header: &mut SliceHeader,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -2222,8 +2215,8 @@ impl Parser {
Ok(())
}

fn parse_dec_ref_pic_marking<T: AsRef<[u8]>, U: AsRef<[u8]>>(
r: &mut NaluReader<T>,
fn parse_dec_ref_pic_marking<U: AsRef<[u8]>>(
r: &mut NaluReader,
nalu: &Nalu<U>,
header: &mut SliceHeader,
) -> anyhow::Result<()> {
Expand Down
54 changes: 19 additions & 35 deletions src/codec/h265/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3808,9 +3808,9 @@ impl Parser {
Ok(self.get_vps(key).unwrap())
}

fn parse_profile_tier_level<T: AsRef<[u8]>>(
fn parse_profile_tier_level(
ptl: &mut ProfileTierLevel,
r: &mut NaluReader<T>,
r: &mut NaluReader,
profile_present_flag: bool,
sps_max_sub_layers_minus_1: u8,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -4053,10 +4053,7 @@ impl Parser {
}
}

fn parse_scaling_list_data<T: AsRef<[u8]>>(
sl: &mut ScalingLists,
r: &mut NaluReader<T>,
) -> anyhow::Result<()> {
fn parse_scaling_list_data(sl: &mut ScalingLists, r: &mut NaluReader) -> anyhow::Result<()> {
// 7.4.5
for size_id in 0..4 {
let mut matrix_id = 0;
Expand Down Expand Up @@ -4144,10 +4141,10 @@ impl Parser {
Ok(())
}

fn parse_short_term_ref_pic_set<T: AsRef<[u8]>>(
fn parse_short_term_ref_pic_set(
sps: &Sps,
st: &mut ShortTermRefPicSet,
r: &mut NaluReader<T>,
r: &mut NaluReader,
st_rps_idx: u8,
) -> anyhow::Result<()> {
if st_rps_idx != 0 {
Expand Down Expand Up @@ -4305,11 +4302,11 @@ impl Parser {
Ok(())
}

fn parse_sublayer_hrd_parameters<T: AsRef<[u8]>>(
fn parse_sublayer_hrd_parameters(
h: &mut SublayerHrdParameters,
cpb_cnt: u32,
sub_pic_hrd_params_present_flag: bool,
r: &mut NaluReader<T>,
r: &mut NaluReader,
) -> anyhow::Result<()> {
for i in 0..cpb_cnt as usize {
h.bit_rate_value_minus1[i] = r.read_ue_max((2u64.pow(32) - 2) as u32)?;
Expand All @@ -4325,11 +4322,11 @@ impl Parser {
Ok(())
}

fn parse_hrd_parameters<T: AsRef<[u8]>>(
fn parse_hrd_parameters(
common_inf_present_flag: bool,
max_num_sublayers_minus1: u8,
hrd: &mut HrdParams,
r: &mut NaluReader<T>,
r: &mut NaluReader,
) -> anyhow::Result<()> {
if common_inf_present_flag {
hrd.nal_hrd_parameters_present_flag = r.read_bit()?;
Expand Down Expand Up @@ -4390,10 +4387,7 @@ impl Parser {
Ok(())
}

fn parse_vui_parameters<T: AsRef<[u8]>>(
sps: &mut Sps,
r: &mut NaluReader<T>,
) -> anyhow::Result<()> {
fn parse_vui_parameters(sps: &mut Sps, r: &mut NaluReader) -> anyhow::Result<()> {
let vui = &mut sps.vui_parameters;

vui.aspect_ratio_info_present_flag = r.read_bit()?;
Expand Down Expand Up @@ -4488,10 +4482,7 @@ impl Parser {
Ok(())
}

fn parse_sps_scc_extension<T: AsRef<[u8]>>(
sps: &mut Sps,
r: &mut NaluReader<T>,
) -> anyhow::Result<()> {
fn parse_sps_scc_extension(sps: &mut Sps, r: &mut NaluReader) -> anyhow::Result<()> {
let scc = &mut sps.scc_extension;

scc.curr_pic_ref_enabled_flag = r.read_bit()?;
Expand Down Expand Up @@ -4527,10 +4518,7 @@ impl Parser {
Ok(())
}

fn parse_sps_range_extension<T: AsRef<[u8]>>(
sps: &mut Sps,
r: &mut NaluReader<T>,
) -> anyhow::Result<()> {
fn parse_sps_range_extension(sps: &mut Sps, r: &mut NaluReader) -> anyhow::Result<()> {
let ext = &mut sps.range_extension;

ext.transform_skip_rotation_enabled_flag = r.read_bit()?;
Expand Down Expand Up @@ -4765,11 +4753,7 @@ impl Parser {
Ok(self.get_sps(key).unwrap())
}

fn parse_pps_scc_extension<T: AsRef<[u8]>>(
pps: &mut Pps,
sps: &Sps,
r: &mut NaluReader<T>,
) -> anyhow::Result<()> {
fn parse_pps_scc_extension(pps: &mut Pps, sps: &Sps, r: &mut NaluReader) -> anyhow::Result<()> {
let scc = &mut pps.scc_extension;
scc.curr_pic_ref_enabled_flag = r.read_bit()?;
scc.residual_adaptive_colour_transform_enabled_flag = r.read_bit()?;
Expand Down Expand Up @@ -4815,10 +4799,10 @@ impl Parser {
Ok(())
}

fn parse_pps_range_extension<T: AsRef<[u8]>>(
fn parse_pps_range_extension(
pps: &mut Pps,
sps: &Sps,
r: &mut NaluReader<T>,
r: &mut NaluReader,
) -> anyhow::Result<()> {
let rext = &mut pps.range_extension;

Expand Down Expand Up @@ -5036,9 +5020,9 @@ impl Parser {
Ok(self.get_pps(key).unwrap())
}

pub fn parse_pred_weight_table<T: AsRef<[u8]>>(
fn parse_pred_weight_table(
hdr: &mut SliceHeader,
r: &mut NaluReader<T>,
r: &mut NaluReader,
sps: &Sps,
) -> anyhow::Result<()> {
let pwt = &mut hdr.pred_weight_table;
Expand Down Expand Up @@ -5110,9 +5094,9 @@ impl Parser {
Ok(())
}

fn parse_ref_pic_lists_modification<T: AsRef<[u8]>>(
fn parse_ref_pic_lists_modification(
hdr: &mut SliceHeader,
r: &mut NaluReader<T>,
r: &mut NaluReader,
) -> anyhow::Result<()> {
let rplm = &mut hdr.ref_pic_list_modification;

Expand Down

0 comments on commit 23746f7

Please sign in to comment.