Skip to content

Commit 950b0e7

Browse files
committed
h26x: replace Nalu's bitstream storage with Cow
Addresses partially chromeos#49
1 parent a14e6e5 commit 950b0e7

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

examples/ccdec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ fn main() {
344344
let display = libva::Display::open().expect("failed to open libva display");
345345
let (mut decoder, frame_iter) = match args.input_format {
346346
EncodedFormat::H264 => {
347-
let frame_iter = Box::new(NalIterator::<H264Nalu>::new(&input).map(Cow::Borrowed))
347+
let frame_iter = Box::new(NalIterator::<H264Nalu>::new(&input))
348348
as Box<dyn Iterator<Item = Cow<[u8]>>>;
349349

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

381381
let decoder = Box::new(StatelessDecoder::<H265, _>::new_vaapi(

src/codec/h264/nalu.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use anyhow::anyhow;
66
use bytes::Buf;
7+
use std::borrow::Cow;
78
use std::fmt::Debug;
89
use std::io::Cursor;
910

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

2728
pub size: usize,
2829
pub offset: usize,
@@ -80,7 +81,7 @@ where
8081

8182
Ok(Nalu {
8283
header: hdr,
83-
data: bitstream,
84+
data: Cow::from(bitstream),
8485
size: nal_size,
8586
offset: nalu_offset,
8687
sc_offset: start_code_offset,

src/utils.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! This module is for anything that doesn't fit into the other top-level modules. Try not to add
88
//! new code here unless it really doesn't belong anywhere else.
99
10+
use std::borrow::Cow;
1011
use std::io::Cursor;
1112
use std::io::Seek;
1213
use std::marker::PhantomData;
@@ -164,28 +165,40 @@ impl<'a, Nalu> NalIterator<'a, Nalu> {
164165
}
165166

166167
impl<'a> Iterator for NalIterator<'a, H264Nalu<'a>> {
167-
type Item = &'a [u8];
168+
type Item = Cow<'a, [u8]>;
168169

169170
fn next(&mut self) -> Option<Self::Item> {
170171
H264Nalu::next(&mut self.0)
171172
.map(|n| {
172173
let start = n.sc_offset;
173174
let end = n.offset + n.size;
174-
&n.data[start..end]
175+
match n.data {
176+
Cow::Borrowed(data) => Cow::Borrowed(&data[start..end]),
177+
Cow::Owned(mut data) => {
178+
data.truncate(end);
179+
Cow::Owned(data.split_off(start))
180+
}
181+
}
175182
})
176183
.ok()
177184
}
178185
}
179186

180187
impl<'a> Iterator for NalIterator<'a, H265Nalu<'a>> {
181-
type Item = &'a [u8];
188+
type Item = Cow<'a, [u8]>;
182189

183190
fn next(&mut self) -> Option<Self::Item> {
184191
H265Nalu::next(&mut self.0)
185192
.map(|n| {
186193
let start = n.sc_offset;
187194
let end = n.offset + n.size;
188-
&n.data[start..end]
195+
match n.data {
196+
Cow::Borrowed(data) => Cow::Borrowed(&data[start..end]),
197+
Cow::Owned(mut data) => {
198+
data.truncate(end);
199+
Cow::Owned(data.split_off(start))
200+
}
201+
}
189202
})
190203
.ok()
191204
}

0 commit comments

Comments
 (0)