forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli4 days ago4021 commits

hevc.rs244 lines, 7.3 KiBHistoryRawDownload
1//! HEVC encode parameters.
2//!
3//! HEVC encoding in-tree is one of the reasons this crate exists: the incumbent
4//! stack carried it on a branch for months. It is the same IDR-then-P structure
5//! as the H.264 path, in HEVC's units — CTBs instead of macroblocks, a picture
6//! order count instead of `frame_num`, and a conformance window instead of a
7//! frame crop, which is what makes 1916x1080 (or any width that is not a
8//! multiple of 32) come out the size it went in.
9
10use std::rc::Rc;
11
12use ec_va::va::{
13 BufferType, EncPictureParameter, EncPictureParameterBufferHEVC, EncSequenceParameter,
14 EncSequenceParameterBufferHEVC, EncSliceParameter, EncSliceParameterBufferHEVC,
15 HEVCEncPicFields, HEVCEncSeqFields, HevcEncPicSccFields, HevcEncSeqSccFields,
16 HevcEncSliceFields, HevcEncVuiFields, PictureHEVC, VABufferID,
17};
18
19use super::headers::hevc as hevc_headers;
20use super::{Encoder, RateControlMode};
21use crate::error::Result;
22use crate::params::INVALID_SURFACE;
23use crate::pool::PooledSurface;
24
25/// `NAL_IDR_W_RADL`.
26const NAL_IDR_W_RADL: u8 = 19;
27/// `NAL_TRAIL_R`.
28const NAL_TRAIL_R: u8 = 1;
29/// `VA_PICTURE_HEVC_INVALID`: an unused entry of a reference list.
30const PICTURE_INVALID: u32 = 0x0000_0001;
31
32/// An unused entry of a reference list.
33fn invalid_picture() -> PictureHEVC {
34 PictureHEVC::new(INVALID_SURFACE, 0, PICTURE_INVALID)
35}
36
37pub(super) fn parameters(
38 encoder: &Encoder,
39 recon: &Rc<PooledSurface>,
40 coded_buf: VABufferID,
41 keyframe: bool,
42 out: &mut Vec<BufferType>,
43) -> Result<()> {
44 let config = *encoder.config();
45 let (coded_w, coded_h) = encoder.coded_size();
46 let poc = if keyframe {
47 0
48 } else {
49 encoder.gop_position() as i32
50 };
51 let general_level_idc = crate::level::hevc(&config);
52 let intra_period = config.gop_size.max(1);
53 let bits_per_second = match config.rate_control {
54 RateControlMode::ConstantQp { .. } => 0,
55 _ => config.bitrate,
56 };
57 let vui_num_units_in_tick = config.framerate.1.max(1);
58 let vui_time_scale = config.framerate.0.max(1);
59
60 let depth_minus8 = u32::from(config.bit_depth - 8);
61 let seq_fields =
62 HEVCEncSeqFields::new(1, 0, depth_minus8, depth_minus8, 0, 1, 1, 1, 0, 0, 0, 0, 0);
63 // vui_timing_info_present_flag.
64 let vui = HevcEncVuiFields::new(0, 0, 0, 1, 0, 0, 0, 0, 0, 0);
65 let scc = HevcEncSeqSccFields::new(0);
66 // 64x64 CTBs from 8x8 minimum CBs: log2_min = 0 (8), diff = 3 (64).
67 // Not a preference — this driver codes 64x64 CTBs whatever the sequence
68 // parameters say, and `num_ctu_in_slice` below has to count the same
69 // blocks the hardware does.
70 let seq = EncSequenceParameterBufferHEVC::new(
71 if config.bit_depth == 10 { 2 } else { 1 },
72 general_level_idc,
73 0,
74 intra_period,
75 intra_period,
76 1,
77 bits_per_second,
78 coded_w as u16,
79 coded_h as u16,
80 &seq_fields,
81 0,
82 3,
83 0,
84 3,
85 1,
86 1,
87 0,
88 0,
89 0,
90 0,
91 Some(vui),
92 0,
93 0,
94 0,
95 vui_num_units_in_tick,
96 vui_time_scale,
97 0,
98 0,
99 0,
100 &scc,
101 );
102 out.push(BufferType::EncSequenceParameter(
103 EncSequenceParameter::HEVC(seq),
104 ));
105
106 let pic_init_qp = match config.rate_control {
107 RateControlMode::ConstantQp { qp } => qp.clamp(1, 51) as u8,
108 _ => 26,
109 };
110 let nal_unit_type = if keyframe {
111 NAL_IDR_W_RADL
112 } else {
113 NAL_TRAIL_R
114 };
115 // Off, and it has to be off: this driver omits
116 // `slice_loop_filter_across_slices_enabled_flag` from the slice header
117 // it writes, so a PPS that says the flag is present describes a header
118 // that is one bit longer than the one in the stream — every P picture
119 // then decodes as `alignment_bit_equal_to_one = 0` (measured).
120 let pic_fields = HEVCEncPicFields::new(
121 u32::from(keyframe),
122 // 1 = I, 2 = P in this driver's coding_type numbering.
123 if keyframe { 1 } else { 2 },
124 1,
125 0,
126 0,
127 0,
128 1,
129 0,
130 0,
131 0,
132 0,
133 0,
134 0,
135 0,
136 0,
137 0,
138 0,
139 0,
140 0,
141 );
142 let pic_scc = HevcEncPicSccFields::new(0);
143 let mut reference_frames: [PictureHEVC; 15] = std::array::from_fn(|_| invalid_picture());
144 if let Some(reference) = encoder.reference().filter(|_| !keyframe) {
145 reference_frames[0] = PictureHEVC::new(reference.id(), poc - 1, 0);
146 }
147 let pic = EncPictureParameterBufferHEVC::new(
148 PictureHEVC::new(recon.id(), poc, 0),
149 reference_frames,
150 coded_buf,
151 0xff,
152 0,
153 pic_init_qp,
154 0,
155 0,
156 0,
157 0,
158 0,
159 [0; 19],
160 [0; 21],
161 0,
162 0,
163 0,
164 0,
165 0,
166 nal_unit_type,
167 &pic_fields,
168 0,
169 0,
170 &pic_scc,
171 );
172 out.push(BufferType::EncPictureParameter(EncPictureParameter::HEVC(
173 pic,
174 )));
175
176 // CTBs are 64x64 by the sequence parameters above.
177 let ctbs = coded_w.div_ceil(64) * coded_h.div_ceil(64);
178 let mut ref_pic_list0: [PictureHEVC; 15] = std::array::from_fn(|_| invalid_picture());
179 let ref_pic_list1: [PictureHEVC; 15] = std::array::from_fn(|_| invalid_picture());
180 if !keyframe && let Some(reference) = encoder.reference() {
181 ref_pic_list0[0] = PictureHEVC::new(reference.id(), poc - 1, 0);
182 }
183 // last_slice_of_pic_flag, slice_sao_luma/chroma, and the reference override
184 // on P pictures. The loop filter across slices stays off, matching the PPS.
185 let slice_fields =
186 HevcEncSliceFields::new(1, 0, 0, 0, 1, 1, u32::from(!keyframe), 0, 0, 0, 0, 0);
187 let slice = EncSliceParameterBufferHEVC::new(
188 0,
189 ctbs,
190 if keyframe { 2 } else { 1 }, // 2 = I, 1 = P
191 0,
192 0,
193 0,
194 ref_pic_list0,
195 ref_pic_list1,
196 0,
197 0,
198 [0; 15],
199 [0; 15],
200 [[0; 2]; 15],
201 [[0; 2]; 15],
202 [0; 15],
203 [0; 15],
204 [[0; 2]; 15],
205 [[0; 2]; 15],
206 5,
207 0,
208 0,
209 0,
210 0,
211 0,
212 &slice_fields,
213 0,
214 0,
215 );
216
217 let header_params = hevc_headers::Params {
218 width: config.width,
219 height: config.height,
220 coded_width: coded_w,
221 coded_height: coded_h,
222 level_idc: general_level_idc,
223 num_units_in_tick: vui_num_units_in_tick,
224 time_scale: vui_time_scale,
225 init_qp: pic_init_qp,
226 colour: config.colour,
227 bit_depth: config.bit_depth,
228 };
229 let (vps, sps, pps) = hevc_headers::parameter_sets(&header_params);
230 if keyframe {
231 encoder.push_packed(&hevc_headers::packed_vps(&vps), out)?;
232 encoder.push_packed(&hevc_headers::packed_sps(&sps), out)?;
233 encoder.push_packed(&hevc_headers::packed_pps(&pps), out)?;
234 }
235 // The slice header goes in too. This driver composes its own for HEVC and
236 // ignores the content of this one — but it emits the parameter sets above
237 // only when a slice header accompanies them, so dropping it costs the
238 // stream its VPS, SPS and PPS (measured 2026-08-14).
239 encoder.push_packed(&hevc_headers::packed_slice(&sps, &pps, keyframe, poc), out)?;
240 out.push(BufferType::EncSliceParameter(EncSliceParameter::HEVC(
241 slice,
242 )));
243 Ok(())
244}