-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcodec.rs
More file actions
278 lines (245 loc) · 7.78 KB
/
Copy pathcodec.rs
File metadata and controls
278 lines (245 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use super::error::VideoError;
use super::frame::Rgb8Frame;
/// Supported video codec identifiers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VideoCodec {
H264,
H265,
Av1,
Raw,
}
/// Trait for video decoders that convert compressed NAL units to RGB8 frames.
pub trait VideoDecoder: Send {
fn codec(&self) -> VideoCodec;
/// Decode a single compressed access unit into an RGB8 frame.
/// Returns `None` if the decoder needs more data (e.g., initial SPS/PPS).
fn decode(
&mut self,
data: &[u8],
timestamp_us: u64,
) -> Result<Option<DecodedFrame>, VideoError>;
/// Flush any remaining buffered frames.
fn flush(&mut self) -> Result<Vec<DecodedFrame>, VideoError>;
}
/// Trait for video encoders that compress RGB8 frames.
pub trait VideoEncoder: Send {
fn codec(&self) -> VideoCodec;
/// Encode one RGB8 frame, returning zero or more compressed packets.
fn encode(&mut self, frame: &Rgb8Frame) -> Result<Vec<EncodedPacket>, VideoError>;
/// Flush remaining buffered packets.
fn flush(&mut self) -> Result<Vec<EncodedPacket>, VideoError>;
}
/// A decoded video frame with metadata.
#[derive(Debug, Clone)]
pub struct DecodedFrame {
pub width: usize,
pub height: usize,
pub rgb8_data: Vec<u8>,
pub timestamp_us: u64,
pub keyframe: bool,
pub bit_depth: u8,
pub rgb16_data: Option<Vec<u16>>,
}
impl DecodedFrame {
pub fn into_rgb8_frame(self, frame_index: u64) -> Result<Rgb8Frame, VideoError> {
Rgb8Frame::from_bytes(
frame_index,
self.timestamp_us,
self.width,
self.height,
bytes::Bytes::from(self.rgb8_data),
)
}
}
/// A compressed video packet.
#[derive(Debug, Clone)]
pub struct EncodedPacket {
pub data: Vec<u8>,
pub timestamp_us: u64,
pub keyframe: bool,
}
// ── H.264 Annex B NAL unit parser ──────────────────────────────────
/// H.264 NAL unit types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NalUnitType {
Slice,
SliceA,
SliceB,
SliceC,
Idr,
Sei,
Sps,
Pps,
Aud,
Other(u8),
}
impl NalUnitType {
pub fn from_byte(b: u8) -> Self {
match b & 0x1F {
1 => NalUnitType::Slice,
2 => NalUnitType::SliceA,
3 => NalUnitType::SliceB,
4 => NalUnitType::SliceC,
5 => NalUnitType::Idr,
6 => NalUnitType::Sei,
7 => NalUnitType::Sps,
8 => NalUnitType::Pps,
9 => NalUnitType::Aud,
other => NalUnitType::Other(other),
}
}
pub fn is_vcl(&self) -> bool {
matches!(
self,
NalUnitType::Slice
| NalUnitType::SliceA
| NalUnitType::SliceB
| NalUnitType::SliceC
| NalUnitType::Idr
)
}
}
/// A parsed NAL unit from an Annex B bitstream.
#[derive(Debug, Clone)]
pub struct NalUnit {
pub nal_type: NalUnitType,
pub nal_ref_idc: u8,
pub data: Vec<u8>,
}
/// Parses H.264 Annex B bitstream into NAL units.
/// Splits on `0x000001` or `0x00000001` start codes.
pub fn parse_annex_b(data: &[u8]) -> Vec<NalUnit> {
let mut units = Vec::new();
let mut i = 0;
let len = data.len();
while i < len {
if i + 3 <= len && data[i] == 0 && data[i + 1] == 0 {
let (start_code_len, found) = if i + 4 <= len && data[i + 2] == 0 && data[i + 3] == 1 {
(4, true)
} else if data[i + 2] == 1 {
(3, true)
} else {
(0, false)
};
if found {
let nal_start = i + start_code_len;
let mut nal_end = nal_start;
let mut j = nal_start;
while j < len {
if j + 3 <= len
&& data[j] == 0
&& data[j + 1] == 0
&& ((j + 4 <= len && data[j + 2] == 0 && data[j + 3] == 1)
|| data[j + 2] == 1)
{
nal_end = j;
break;
}
j += 1;
}
if j >= len {
nal_end = len;
}
if nal_start < nal_end {
let header = data[nal_start];
let nal_ref_idc = (header >> 5) & 0x03;
let nal_type = NalUnitType::from_byte(header);
units.push(NalUnit {
nal_type,
nal_ref_idc,
data: data[nal_start..nal_end].to_vec(),
});
}
i = nal_end;
continue;
}
}
i += 1;
}
units
}
/// Extracts SPS and PPS NAL units from an Annex B bitstream.
pub fn extract_parameter_sets(nals: &[NalUnit]) -> (Option<&NalUnit>, Option<&NalUnit>) {
let sps = nals.iter().find(|n| n.nal_type == NalUnitType::Sps);
let pps = nals.iter().find(|n| n.nal_type == NalUnitType::Pps);
(sps, pps)
}
// ── Simple MP4 box parser ──────────────────────────────────────────
/// A parsed MP4 box header.
#[derive(Debug, Clone)]
pub struct Mp4Box {
pub box_type: [u8; 4],
pub size: u64,
pub header_size: u8,
pub offset: u64,
}
impl Mp4Box {
pub fn type_str(&self) -> &str {
std::str::from_utf8(&self.box_type).unwrap_or("????")
}
}
/// Parses top-level MP4 boxes from a byte buffer.
pub fn parse_mp4_boxes(data: &[u8]) -> Result<Vec<Mp4Box>, VideoError> {
let mut boxes = Vec::new();
let mut offset = 0u64;
let len = data.len() as u64;
while offset + 8 <= len {
let o = offset as usize;
let size_32 = u32::from_be_bytes([data[o], data[o + 1], data[o + 2], data[o + 3]]);
let box_type = [data[o + 4], data[o + 5], data[o + 6], data[o + 7]];
let (size, header_size) = if size_32 == 1 {
if offset + 16 > len {
return Err(VideoError::ContainerParse(
"truncated extended box size".into(),
));
}
let extended = u64::from_be_bytes([
data[o + 8],
data[o + 9],
data[o + 10],
data[o + 11],
data[o + 12],
data[o + 13],
data[o + 14],
data[o + 15],
]);
(extended, 16u8)
} else if size_32 == 0 {
(len - offset, 8u8)
} else {
(size_32 as u64, 8u8)
};
if size < header_size as u64 {
return Err(VideoError::ContainerParse(format!(
"box size {} smaller than header at offset {offset}",
size
)));
}
boxes.push(Mp4Box {
box_type,
size,
header_size,
offset,
});
offset += size;
}
Ok(boxes)
}
/// Finds a box by 4-char type code.
pub fn find_box<'a>(boxes: &'a [Mp4Box], box_type: &[u8; 4]) -> Option<&'a Mp4Box> {
boxes.iter().find(|b| &b.box_type == box_type)
}
/// Parses child boxes inside a parent box.
pub fn parse_child_boxes(data: &[u8], parent: &Mp4Box) -> Result<Vec<Mp4Box>, VideoError> {
let start = (parent.offset + parent.header_size as u64) as usize;
let end = (parent.offset + parent.size) as usize;
if end > data.len() || start >= end {
return Ok(Vec::new());
}
let child_data = &data[start..end];
let mut children = parse_mp4_boxes(child_data)?;
for child in &mut children {
child.offset += start as u64;
}
Ok(children)
}