forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

decode.rs120 lines, 4.1 KiBHistoryRawDownload
1//! Decoded pictures, and the one-access-unit convenience entry point over
2//! the stateful [`crate::dec::Decoder`].
3
4use ec_core::error::{Error, Result};
5
6/// A decoded picture: three planes at the SPS's *coded* size (before the
7/// conformance window crops it). Samples are 16-bit whatever the bit depth:
8/// Main and Main 10 both store one `u16` per sample, and `bit_depth` says how
9/// many of its bits are used.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct DecodedPicture {
12 /// Coded width.
13 pub width: usize,
14 /// Coded height.
15 pub height: usize,
16 /// `BitDepthY`.
17 pub bit_depth: u32,
18 /// `BitDepthC`; equal to `bit_depth` unless the stream codes chroma at another depth.
19 pub bit_depth_c: u32,
20 /// `chroma_format_idc`: 0 (4:0:0, `cb` and `cr` empty), 1 (4:2:0), 2 (4:2:2) or 3 (4:4:4).
21 pub chroma_format_idc: u32,
22 /// Luma, `width * height`.
23 pub y: Vec<u16>,
24 /// Cb, `width/2 * height/2`.
25 pub cb: Vec<u16>,
26 /// Cr, `width/2 * height/2`.
27 pub cr: Vec<u16>,
28}
29
30/// The log2 subsampling of the chroma planes for `chroma_format_idc`, `(sub_x, sub_y)`:
31/// 4:2:0 is `(1, 1)`, 4:2:2 is `(1, 0)`, 4:4:4 is `(0, 0)`.
32pub fn chroma_shifts(chroma_format_idc: u32) -> (u32, u32) {
33 match chroma_format_idc {
34 1 => (1, 1),
35 2 => (1, 0),
36 _ => (0, 0),
37 }
38}
39
40impl DecodedPicture {
41 /// The log2 chroma subsampling of this picture, `(sub_x, sub_y)`.
42 pub fn chroma_shifts(&self) -> (u32, u32) {
43 chroma_shifts(self.chroma_format_idc)
44 }
45
46 /// The chroma plane width and height of this picture, rounded up.
47 pub fn chroma_dims(&self) -> (usize, usize) {
48 if self.chroma_format_idc == 0 {
49 return (0, 0);
50 }
51 let (sx, sy) = self.chroma_shifts();
52 (self.width.div_ceil(1 << sx), self.height.div_ceil(1 << sy))
53 }
54 /// A picture of mid-grey samples (`1 << (bit_depth - 1)`).
55 pub fn grey(width: usize, height: usize, bit_depth: u32) -> DecodedPicture {
56 Self::grey_planes(width, height, bit_depth, bit_depth)
57 }
58
59 /// A picture of mid-grey samples, each plane at its own bit depth.
60 pub fn grey_planes(
61 width: usize,
62 height: usize,
63 bit_depth: u32,
64 bit_depth_c: u32,
65 ) -> DecodedPicture {
66 Self::grey_planes_fmt(width, height, bit_depth, bit_depth_c, 1)
67 }
68
69 /// [`Self::grey_planes`] for any `chroma_format_idc`.
70 pub fn grey_planes_fmt(
71 width: usize,
72 height: usize,
73 bit_depth: u32,
74 bit_depth_c: u32,
75 chroma_format_idc: u32,
76 ) -> DecodedPicture {
77 let mid = 1u16 << (bit_depth - 1);
78 let mid_c = 1u16 << (bit_depth_c - 1);
79 let (sx, sy) = chroma_shifts(chroma_format_idc);
80 let (cw, ch) = (width.div_ceil(1 << sx), height.div_ceil(1 << sy));
81 DecodedPicture {
82 width,
83 height,
84 bit_depth,
85 bit_depth_c,
86 chroma_format_idc,
87 y: vec![mid; width * height],
88 cb: vec![mid_c; cw * ch],
89 cr: vec![mid_c; cw * ch],
90 }
91 }
92
93 /// The bit depth of component `c` (0 luma, 1 and 2 chroma).
94 pub fn bit_depth_of(&self, c: usize) -> u32 {
95 if c == 0 {
96 self.bit_depth
97 } else {
98 self.bit_depth_c
99 }
100 }
101
102 /// The luma plane as 8-bit samples. Only meaningful for 8-bit pictures.
103 pub fn y_u8(&self) -> Vec<u8> {
104 self.y.iter().map(|&v| v as u8).collect()
105 }
106}
107
108/// Decode one IDR access unit: VPS/SPS/PPS/slice NALs in Annex-B framing, one
109/// intra slice covering the whole picture. A thin wrapper over
110/// [`crate::dec::Decoder`], which owns the picture management; anything
111/// outside what that decoder can pixel-decode is refused with a named reason.
112pub fn decode_idr_au(data: &[u8]) -> Result<DecodedPicture> {
113 let mut decoder = crate::dec::Decoder::new();
114 decoder.decode_annex_b(data)?;
115 decoder.flush()?;
116 let frame = decoder.next_frame().ok_or_else(|| {
117 Error::corrupt("HEVC decode: no IDR slice found in the access unit".to_string())
118 })?;
119 Ok(std::sync::Arc::try_unwrap(frame.picture).unwrap_or_else(|pic| (*pic).clone()))
120}