Skip to content

Commit

Permalink
h26x: replace Nalu's bitstream storage with Cow
Browse files Browse the repository at this point in the history
Addresses partially chromeos#49
  • Loading branch information
bgrzesik committed Mar 5, 2024
1 parent a14e6e5 commit c15afa5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
4 changes: 2 additions & 2 deletions examples/ccdec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ fn main() {
let display = libva::Display::open().expect("failed to open libva display");
let (mut decoder, frame_iter) = match args.input_format {
EncodedFormat::H264 => {
let frame_iter = Box::new(NalIterator::<H264Nalu>::new(&input).map(Cow::Borrowed))
let frame_iter = Box::new(NalIterator::<H264Nalu>::new(&input))
as Box<dyn Iterator<Item = Cow<[u8]>>>;

let decoder = Box::new(StatelessDecoder::<H264, _>::new_vaapi(
Expand Down Expand Up @@ -375,7 +375,7 @@ fn main() {
(decoder, frame_iter)
}
EncodedFormat::H265 => {
let frame_iter = Box::new(NalIterator::<H265Nalu>::new(&input).map(Cow::Borrowed))
let frame_iter = Box::new(NalIterator::<H265Nalu>::new(&input))
as Box<dyn Iterator<Item = Cow<[u8]>>>;

let decoder = Box::new(StatelessDecoder::<H265, _>::new_vaapi(
Expand Down
9 changes: 5 additions & 4 deletions src/codec/h264/nalu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use anyhow::anyhow;
use bytes::Buf;
use std::borrow::Cow;
use std::fmt::Debug;
use std::io::Cursor;

Expand All @@ -22,7 +23,7 @@ pub struct Nalu<'a, U> {
pub header: U,
/// The mapping that backs this NALU. Possibly shared with the other NALUs
/// in the Access Unit.
pub data: &'a [u8],
pub data: Cow<'a, [u8]>,

pub size: usize,
pub offset: usize,
Expand Down Expand Up @@ -80,10 +81,10 @@ where

Ok(Nalu {
header: hdr,
data: bitstream,
data: Cow::from(&bitstream[start_code_offset..nalu_offset + nal_size]),
size: nal_size,
offset: nalu_offset,
sc_offset: start_code_offset,
offset: nalu_offset - start_code_offset,
sc_offset: 0,
})
}
}
Expand Down
21 changes: 5 additions & 16 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! This module is for anything that doesn't fit into the other top-level modules. Try not to add
//! new code here unless it really doesn't belong anywhere else.
use std::borrow::Cow;
use std::io::Cursor;
use std::io::Seek;
use std::marker::PhantomData;
Expand Down Expand Up @@ -164,30 +165,18 @@ impl<'a, Nalu> NalIterator<'a, Nalu> {
}

impl<'a> Iterator for NalIterator<'a, H264Nalu<'a>> {
type Item = &'a [u8];
type Item = Cow<'a, [u8]>;

fn next(&mut self) -> Option<Self::Item> {
H264Nalu::next(&mut self.0)
.map(|n| {
let start = n.sc_offset;
let end = n.offset + n.size;
&n.data[start..end]
})
.ok()
H264Nalu::next(&mut self.0).map(|n| n.data).ok()
}
}

impl<'a> Iterator for NalIterator<'a, H265Nalu<'a>> {
type Item = &'a [u8];
type Item = Cow<'a, [u8]>;

fn next(&mut self) -> Option<Self::Item> {
H265Nalu::next(&mut self.0)
.map(|n| {
let start = n.sc_offset;
let end = n.offset + n.size;
&n.data[start..end]
})
.ok()
H265Nalu::next(&mut self.0).map(|n| n.data).ok()
}
}

Expand Down

0 comments on commit c15afa5

Please sign in to comment.