forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

video_profile.rs326 lines, 12.4 KiBHistoryRawDownload
1//! The `profile` a video stream states, spelled as ffprobe spells its
2//! `stream=profile` field ([`crate::VideoFacts::profile`]). Each answer is read
3//! from the stream's own configuration: the H.264 SPS, the HEVC `hvcC`
4//! `general_profile_idc`, the AV1 sequence header's `seq_profile`, or the VP9
5//! frame header's profile bits, or the VP8 frame tag's version bits. Anything
6//! else is `None`.
7
8use ec_h264_syntax::{AnnexBIter, Sps, unescape_rbsp};
9
10/// The H.264 profile the first SPS in `sets` states, where `sets` is Annex-B
11/// framed the way both demuxers keep their parameter sets.
12pub(crate) fn h264(sets: &[u8]) -> Option<&'static str> {
13 let nal = AnnexBIter::new(sets).find(|n| n.first().is_some_and(|b| b & 0x1f == 7))?;
14 let mut rbsp = Vec::new();
15 unescape_rbsp(&nal[1..], &mut rbsp);
16 let sps = Sps::parse(&rbsp).ok()?;
17 h264_name(sps.profile_idc, sps.constraint_flags)
18}
19
20/// Whether the first SPS in `sets` has `frame_mbs_only_flag` 0.
21pub(crate) fn h264_interlaced(sets: &[u8]) -> bool {
22 let Some(nal) = AnnexBIter::new(sets).find(|n| n.first().is_some_and(|b| b & 0x1f == 7)) else {
23 return false;
24 };
25 let mut rbsp = Vec::new();
26 unescape_rbsp(&nal[1..], &mut rbsp);
27 Sps::parse(&rbsp).is_ok_and(|sps| !sps.frame_mbs_only)
28}
29
30/// The name of an H.264 `profile_idc` under its `constraint_setX_flag` bits
31/// (set0 is bit 7). Constrained Baseline is set1 on a Baseline stream, and the
32/// intra variants are set3 on the High 10, 4:2:2 and 4:4:4 Predictive profiles.
33fn h264_name(idc: u8, flags: u8) -> Option<&'static str> {
34 let (set1, set3) = (flags & 0x40 != 0, flags & 0x10 != 0);
35 Some(match idc {
36 66 if set1 => "Constrained Baseline",
37 66 => "Baseline",
38 77 => "Main",
39 88 => "Extended",
40 100 => "High",
41 110 if set3 => "High 10 Intra",
42 110 => "High 10",
43 122 if set3 => "High 4:2:2 Intra",
44 122 => "High 4:2:2",
45 244 if set3 => "High 4:4:4 Intra",
46 244 => "High 4:4:4 Predictive",
47 44 => "CAVLC 4:4:4",
48 _ => return None,
49 })
50}
51
52/// The HEVC profile of an `hvcC` record: its `general_profile_idc`, the low five
53/// bits of the byte after the configuration version.
54pub(crate) fn hevc_record(hvcc: &[u8]) -> Option<&'static str> {
55 Some(match hvcc.get(1)? & 0x1f {
56 1 => "Main",
57 2 => "Main 10",
58 3 => "Main Still Picture",
59 4 => "Rext",
60 _ => return None,
61 })
62}
63
64/// The AV1 profile the sequence header OBU in `obus` states (`seq_profile`, the
65/// top three bits of its first payload byte). `obus` is the `av1C` configuration
66/// OBUs, or the sequence header as a bitstream carries it.
67pub(crate) fn av1(obus: &[u8]) -> Option<&'static str> {
68 let mut at = 0;
69 while let Some(&header) = obus.get(at) {
70 let kind = (header >> 3) & 0xf;
71 at += 1 + usize::from((header >> 2) & 1);
72 let len = if (header >> 1) & 1 == 1 {
73 let (len, used) = leb128(obus.get(at..)?)?;
74 at += used;
75 len
76 } else {
77 obus.len().saturating_sub(at)
78 };
79 let payload = obus.get(at..at.checked_add(len)?)?;
80 if kind == 1 {
81 return Some(match payload.first()? >> 5 {
82 0 => "Main",
83 1 => "High",
84 2 => "Professional",
85 _ => return None,
86 });
87 }
88 at += len;
89 }
90 None
91}
92
93/// The VP9 profile of a frame: `profile_low_bit` and `profile_high_bit`, the two
94/// bits after the two-bit frame marker, read most significant first.
95pub(crate) fn vp9(frame: &[u8]) -> Option<&'static str> {
96 let first = *frame.first()?;
97 Some(match ((first >> 4) & 1) << 1 | (first >> 5) & 1 {
98 0 => "Profile 0",
99 1 => "Profile 1",
100 2 => "Profile 2",
101 _ => "Profile 3",
102 })
103}
104
105/// The VP8 profile of a frame: the `version` field of its frame tag, the first
106/// three bytes read as a little-endian 24-bit value, whose bits 1 to 3 hold it.
107/// Versions above 3 name no profile ffprobe knows, so they are `None`.
108pub(crate) fn vp8(frame: &[u8]) -> Option<&'static str> {
109 if frame.len() < 3 {
110 return None;
111 }
112 match (frame[0] >> 1) & 7 {
113 0 => Some("0"),
114 1 => Some("1"),
115 2 => Some("2"),
116 3 => Some("3"),
117 _ => None,
118 }
119}
120
121/// The ProRes flavour a sample entry's fourcc names, in Apple's own words (the name the
122/// ProRes white paper and every NLE use, not ffprobe's `Standard` / `4444 XQ`): `422 Proxy`
123/// (`apco`), `422 LT` (`apcs`), `422` (`apcn`), `422 HQ` (`apch`), `4444` (`ap4h`) and
124/// `4444 XQ` (`ap4x`). The frame header states none of it, so a track whose fourcc is something
125/// else (or that has none, as in a Matroska or AVI wrapper) states no profile.
126pub(crate) fn prores(fourcc: &[u8; 4]) -> Option<&'static str> {
127 Some(match fourcc {
128 b"apco" => "422 Proxy",
129 b"apcs" => "422 LT",
130 b"apcn" => "422",
131 b"apch" => "422 HQ",
132 b"ap4h" => "4444",
133 b"ap4x" => "4444 XQ",
134 _ => return None,
135 })
136}
137
138/// The Avid profile of a DNxHD / DNxHR frame. The DNxHR compression ids (1270 to 1274) are
139/// the profiles themselves: `DNxHR 444`, `DNxHR HQX`, `DNxHR HQ`, `DNxHR SQ`, `DNxHR LB`. A
140/// DNxHD one is named by its nominal data rate, which is constant: a frame's bytes over its
141/// duration (`bytes` of the first sample at `fps`), snapped to the nearest of Avid's rates
142/// within 6 %, with an `x` for the 10-bit ones (`DNxHD 145`, `DNxHD 220x`). `None` for a rate
143/// that is none of Avid's, or an unknown frame rate.
144pub(crate) fn dnxhd(cid: u32, bit_depth: u32, bytes: usize, fps: f64) -> Option<&'static str> {
145 match cid {
146 1270 => return Some("DNxHR 444"),
147 1271 => return Some("DNxHR HQX"),
148 1272 => return Some("DNxHR HQ"),
149 1273 => return Some("DNxHR SQ"),
150 1274 => return Some("DNxHR LB"),
151 _ => {}
152 }
153 if fps <= 0.0 {
154 return None;
155 }
156 let mbps = bytes as f64 * 8.0 * fps / 1e6;
157 const RATES: [u32; 12] = [36, 45, 60, 75, 90, 100, 110, 115, 120, 145, 175, 185];
158 let nearest = RATES
159 .iter()
160 .chain([&220u32])
161 .copied()
162 .min_by(|&a, &b| (f64::from(a) - mbps).abs().total_cmp(&(f64::from(b) - mbps).abs()))?;
163 if (f64::from(nearest) - mbps).abs() > f64::from(nearest) * 0.06 {
164 return None;
165 }
166 let ten = bit_depth >= 10;
167 Some(match (nearest, ten) {
168 (36, _) => "DNxHD 36",
169 (45, _) => "DNxHD 45",
170 (60, _) => "DNxHD 60",
171 (75, _) => "DNxHD 75",
172 (90, false) => "DNxHD 90",
173 (90, true) => "DNxHD 90x",
174 (100, _) => "DNxHD 100",
175 (110, _) => "DNxHD 110x",
176 (115, _) => "DNxHD 115",
177 (120, _) => "DNxHD 120",
178 (145, _) => "DNxHD 145",
179 (175, false) => "DNxHD 175",
180 (175, true) => "DNxHD 175x",
181 (185, false) => "DNxHD 185",
182 (185, true) => "DNxHD 185x",
183 (220, false) => "DNxHD 220",
184 (220, true) => "DNxHD 220x",
185 _ => return None,
186 })
187}
188
189/// The MPEG-4 Part 2 profile of a track: `H.263` for a 3GPP / QuickTime H.263 sample entry, else
190/// the `profile_and_level_indication` of the Visual Object Sequence header (`00 00 01 B0`) in
191/// `vol` -- `Simple` (levels 0 to 3), `Advanced Simple` (`0xF0` to `0xF5`) or the other names
192/// ISO/IEC 14496-2 Annex G gives. `None` for a configuration with no sequence header, which
193/// states none.
194pub(crate) fn mpeg4(entry: Option<&[u8; 4]>, vol: &[u8]) -> Option<&'static str> {
195 if matches!(entry, Some(b"s263" | b"h263")) {
196 return Some("H.263");
197 }
198 let at = vol.windows(4).position(|w| w == [0, 0, 1, 0xB0])?;
199 Some(match *vol.get(at + 4)? {
200 0x01..=0x03 | 0x08 | 0x09 => "Simple",
201 0x10..=0x12 => "Simple Scalable",
202 0x21 | 0x22 => "Core",
203 0x32..=0x34 => "Main",
204 0x42 => "N-bit",
205 0x51..=0x55 => "Scalable Texture",
206 0x61 | 0x62 => "Simple Face",
207 0x63 | 0x64 => "Simple FBA",
208 0x71..=0x74 => "Basic Animated Texture",
209 0x81..=0x85 => "Hybrid",
210 0x91..=0x95 => "Advanced Real Time Simple",
211 0xA1..=0xA3 => "Core Scalable",
212 0xB1..=0xB3 => "Advanced Coding Efficiency",
213 0xC1..=0xC3 => "Advanced Core",
214 0xD1..=0xD3 => "Advanced Scalable Texture",
215 0xF0..=0xF5 => "Advanced Simple",
216 0xF7..=0xFF => "Fine Granularity Scalability",
217 _ => return None,
218 })
219}
220
221/// The DV flavour of a frame's system, in the broadcast names: `DV25` (consumer DV, DVCAM and
222/// DVCPRO25 -- one bit rate, one stream type), `DVCPRO50` and `DVCPRO HD`.
223pub(crate) fn dv(profile: &ec_dv::Profile) -> &'static str {
224 if profile.is_hd() {
225 "DVCPRO HD"
226 } else if profile.video_stype == 4 {
227 "DVCPRO50"
228 } else {
229 "DV25"
230 }
231}
232
233/// The layout of uncompressed video, from the `BITMAPINFOHEADER` an AVI keeps or the one the mov
234/// demuxer synthesizes (marked `mov ` where `biSize` sits): the FourCC's usual name (`YUY2`,
235/// `UYVY`, `YVYU`, `I420`, `YV12`, `NV12`, `Y800`, `v210`), else by depth for a DIB (`BGR24`,
236/// `BGRA`, `RGB555`, `PAL8`). A mov entry is named by its own FourCC and depth: `raw ` is `RGB24`,
237/// `ARGB` or `RGB555`, `2vuy` is `UYVY`, `yuvs` `YUY2`, `yuv2` `yuv2`, `v210`, `v410`, `b16g` `Gray16`.
238pub(crate) fn raw(bih: &[u8]) -> &'static str {
239 let bits = bih.get(14..16).map_or(0, |b| u16::from_le_bytes([b[0], b[1]]));
240 if bih.get(..4) == Some(&b"mov "[..]) {
241 return match (bih.get(16..20), bits) {
242 (Some(b"raw "), 32) => "ARGB",
243 (Some(b"raw "), 16) => "RGB555",
244 (Some(b"raw "), _) => "RGB24",
245 (Some(b"2vuy"), _) => "UYVY",
246 (Some(b"yuvs"), _) => "YUY2",
247 (Some(b"yuv2"), _) => "yuv2",
248 (Some(b"v210"), _) => "v210",
249 (Some(b"v410"), _) => "v410",
250 (Some(b"b16g"), _) => "Gray16",
251 _ => "raw",
252 };
253 }
254 let tag = bih.get(16..20).map(|t| t.to_ascii_uppercase());
255 match tag.as_deref() {
256 Some(b"YUY2" | b"YUYV" | b"YUV2" | b"V422") => "YUY2",
257 Some(b"UYVY" | b"2VUY") => "UYVY",
258 Some(b"YVYU") => "YVYU",
259 Some(b"I420" | b"IYUV") => "I420",
260 Some(b"YV12") => "YV12",
261 Some(b"NV12") => "NV12",
262 Some(b"Y800" | b"Y8 " | b"GREY") => "Y800",
263 Some(b"V210") => "v210",
264 _ if bits == 8 => "PAL8",
265 _ if bits == 15 || bits == 16 => "RGB555",
266 _ if bits == 32 => "BGRA",
267 _ => "BGR24",
268 }
269}
270
271/// An unsigned LEB128 value and how many bytes it took.
272fn leb128(bytes: &[u8]) -> Option<(usize, usize)> {
273 let mut value = 0usize;
274 for (i, &b) in bytes.iter().take(8).enumerate() {
275 value |= usize::from(b & 0x7f) << (7 * i);
276 if b & 0x80 == 0 {
277 return Some((value, i + 1));
278 }
279 }
280 None
281}
282
283#[cfg(test)]
284mod tests {
285 use super::*;
286
287 #[test]
288 fn h264_names_follow_the_constraint_flags() {
289 assert_eq!(h264_name(66, 0), Some("Baseline"));
290 assert_eq!(h264_name(66, 0x40), Some("Constrained Baseline"));
291 assert_eq!(h264_name(100, 0x10), Some("High"));
292 assert_eq!(h264_name(110, 0), Some("High 10"));
293 assert_eq!(h264_name(110, 0x10), Some("High 10 Intra"));
294 assert_eq!(h264_name(122, 0), Some("High 4:2:2"));
295 assert_eq!(h264_name(244, 0), Some("High 4:4:4 Predictive"));
296 assert_eq!(h264_name(200, 0), None);
297 }
298
299 #[test]
300 fn av1_reads_seq_profile_from_a_sized_obu() {
301 // A sequence header OBU (type 1, size field) of two bytes, seq_profile 2.
302 assert_eq!(av1(&[0x0a, 0x02, 0x40, 0x00]), Some("Professional"));
303 // An av1C-style record: a metadata OBU first, then the sequence header.
304 assert_eq!(av1(&[0x22, 0x01, 0x00, 0x0a, 0x01, 0x20]), Some("High"));
305 }
306
307 #[test]
308 fn vp9_reads_the_profile_bits() {
309 assert_eq!(vp9(&[0x82]), Some("Profile 0"));
310 assert_eq!(vp9(&[0xa2]), Some("Profile 1"));
311 assert_eq!(vp9(&[0x92]), Some("Profile 2"));
312 assert_eq!(vp9(&[0xb2]), Some("Profile 3"));
313 }
314
315 #[test]
316 fn vp8_reads_the_version_bits_of_the_frame_tag() {
317 // Bit 0 is !key_frame, bits 1..=3 the version.
318 assert_eq!(vp8(&[0x00, 0x00, 0x00]), Some("0"));
319 assert_eq!(vp8(&[0x01, 0x00, 0x00]), Some("0"));
320 assert_eq!(vp8(&[0x02, 0x00, 0x00]), Some("1"));
321 assert_eq!(vp8(&[0x04, 0x00, 0x00]), Some("2"));
322 assert_eq!(vp8(&[0x07, 0x00, 0x00]), Some("3"));
323 assert_eq!(vp8(&[0x08, 0x00, 0x00]), None);
324 assert_eq!(vp8(&[0x00, 0x00]), None);
325 }
326}