vaapi_h264: Add support for SEI messages
[libav.git] / libavcodec / vaapi_encode_h264.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <va/va.h>
20 #include <va/va_enc_h264.h>
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/internal.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixfmt.h"
26
27 #include "avcodec.h"
28 #include "h264.h"
29 #include "h264_sei.h"
30 #include "internal.h"
31 #include "vaapi_encode.h"
32 #include "vaapi_encode_h26x.h"
33
34 enum {
35 SLICE_TYPE_P = 0,
36 SLICE_TYPE_B = 1,
37 SLICE_TYPE_I = 2,
38 SLICE_TYPE_SP = 3,
39 SLICE_TYPE_SI = 4,
40 };
41
42 // This structure contains all possibly-useful per-sequence syntax elements
43 // which are not already contained in the various VAAPI structures.
44 typedef struct VAAPIEncodeH264MiscSequenceParams {
45 unsigned int profile_idc;
46 char constraint_set0_flag;
47 char constraint_set1_flag;
48 char constraint_set2_flag;
49 char constraint_set3_flag;
50 char constraint_set4_flag;
51 char constraint_set5_flag;
52
53 char separate_colour_plane_flag;
54 char qpprime_y_zero_transform_bypass_flag;
55
56 char gaps_in_frame_num_allowed_flag;
57 char delta_pic_order_always_zero_flag;
58 char bottom_field_pic_order_in_frame_present_flag;
59
60 unsigned int num_slice_groups_minus1;
61 unsigned int slice_group_map_type;
62
63 int pic_init_qs_minus26;
64
65 char overscan_info_present_flag;
66 char overscan_appropriate_flag;
67
68 char video_signal_type_present_flag;
69 unsigned int video_format;
70 char video_full_range_flag;
71 char colour_description_present_flag;
72 unsigned int colour_primaries;
73 unsigned int transfer_characteristics;
74 unsigned int matrix_coefficients;
75
76 char chroma_loc_info_present_flag;
77 unsigned int chroma_sample_loc_type_top_field;
78 unsigned int chroma_sample_loc_type_bottom_field;
79
80 // Some timing elements are in VAEncSequenceParameterBufferH264.
81 char fixed_frame_rate_flag;
82
83 char nal_hrd_parameters_present_flag;
84 char vcl_hrd_parameters_present_flag;
85 char low_delay_hrd_flag;
86 char pic_struct_present_flag;
87 char bitstream_restriction_flag;
88
89 unsigned int cpb_cnt_minus1;
90 unsigned int bit_rate_scale;
91 unsigned int cpb_size_scale;
92 unsigned int bit_rate_value_minus1[32];
93 unsigned int cpb_size_value_minus1[32];
94 char cbr_flag[32];
95 unsigned int initial_cpb_removal_delay_length_minus1;
96 unsigned int cpb_removal_delay_length_minus1;
97 unsigned int dpb_output_delay_length_minus1;
98 unsigned int time_offset_length;
99
100 unsigned int initial_cpb_removal_delay;
101 unsigned int initial_cpb_removal_delay_offset;
102
103 unsigned int pic_struct;
104 } VAAPIEncodeH264MiscSequenceParams;
105
106 // This structure contains all possibly-useful per-slice syntax elements
107 // which are not already contained in the various VAAPI structures.
108 typedef struct VAAPIEncodeH264MiscSliceParams {
109 unsigned int nal_unit_type;
110 unsigned int nal_ref_idc;
111
112 unsigned int colour_plane_id;
113 char field_pic_flag;
114 char bottom_field_flag;
115
116 unsigned int redundant_pic_cnt;
117
118 char sp_for_switch_flag;
119 int slice_qs_delta;
120
121 char ref_pic_list_modification_flag_l0;
122 char ref_pic_list_modification_flag_l1;
123
124 char no_output_of_prior_pics_flag;
125 char long_term_reference_flag;
126 char adaptive_ref_pic_marking_mode_flag;
127 } VAAPIEncodeH264MiscSliceParams;
128
129 typedef struct VAAPIEncodeH264Slice {
130 VAAPIEncodeH264MiscSliceParams misc_slice_params;
131 } VAAPIEncodeH264Slice;
132
133 typedef struct VAAPIEncodeH264Context {
134 VAAPIEncodeH264MiscSequenceParams misc_sequence_params;
135
136 int mb_width;
137 int mb_height;
138
139 int fixed_qp_idr;
140 int fixed_qp_p;
141 int fixed_qp_b;
142
143 int next_frame_num;
144 int64_t idr_pic_count;
145
146 int cpb_delay;
147 int dpb_delay;
148
149 // Rate control configuration.
150 int send_timing_sei;
151 struct {
152 VAEncMiscParameterBuffer misc;
153 VAEncMiscParameterRateControl rc;
154 } rc_params;
155 struct {
156 VAEncMiscParameterBuffer misc;
157 VAEncMiscParameterHRD hrd;
158 } hrd_params;
159
160 #if VA_CHECK_VERSION(0, 36, 0)
161 // Speed-quality tradeoff setting.
162 struct {
163 VAEncMiscParameterBuffer misc;
164 VAEncMiscParameterBufferQualityLevel quality;
165 } quality_params;
166 #endif
167 } VAAPIEncodeH264Context;
168
169 typedef struct VAAPIEncodeH264Options {
170 int qp;
171 int quality;
172 int low_power;
173 } VAAPIEncodeH264Options;
174
175
176 #define vseq_var(name) vseq->name, name
177 #define vseq_field(name) vseq->seq_fields.bits.name, name
178 #define vvui_field(name) vseq->vui_fields.bits.name, name
179 #define vpic_var(name) vpic->name, name
180 #define vpic_field(name) vpic->pic_fields.bits.name, name
181 #define vslice_var(name) vslice->name, name
182 #define vslice_field(name) vslice->slice_fields.bits.name, name
183 #define mseq_var(name) mseq->name, name
184 #define mslice_var(name) mslice->name, name
185
186 static void vaapi_encode_h264_write_nal_header(PutBitContext *pbc,
187 int nal_unit_type, int nal_ref_idc)
188 {
189 u(1, 0, forbidden_zero_bit);
190 u(2, nal_ref_idc, nal_ref_idc);
191 u(5, nal_unit_type, nal_unit_type);
192 }
193
194 static void vaapi_encode_h264_write_trailing_rbsp(PutBitContext *pbc)
195 {
196 u(1, 1, rbsp_stop_one_bit);
197 while (put_bits_count(pbc) & 7)
198 u(1, 0, rbsp_alignment_zero_bit);
199 }
200
201 static void vaapi_encode_h264_write_vui(PutBitContext *pbc,
202 VAAPIEncodeContext *ctx)
203 {
204 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
205 VAAPIEncodeH264Context *priv = ctx->priv_data;
206 VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
207 int i;
208
209 u(1, vvui_field(aspect_ratio_info_present_flag));
210 if (vseq->vui_fields.bits.aspect_ratio_info_present_flag) {
211 u(8, vseq_var(aspect_ratio_idc));
212 if (vseq->aspect_ratio_idc == 255) {
213 u(16, vseq_var(sar_width));
214 u(16, vseq_var(sar_height));
215 }
216 }
217
218 u(1, mseq_var(overscan_info_present_flag));
219 if (mseq->overscan_info_present_flag)
220 u(1, mseq_var(overscan_appropriate_flag));
221
222 u(1, mseq_var(video_signal_type_present_flag));
223 if (mseq->video_signal_type_present_flag) {
224 u(3, mseq_var(video_format));
225 u(1, mseq_var(video_full_range_flag));
226 u(1, mseq_var(colour_description_present_flag));
227 if (mseq->colour_description_present_flag) {
228 u(8, mseq_var(colour_primaries));
229 u(8, mseq_var(transfer_characteristics));
230 u(8, mseq_var(matrix_coefficients));
231 }
232 }
233
234 u(1, mseq_var(chroma_loc_info_present_flag));
235 if (mseq->chroma_loc_info_present_flag) {
236 ue(mseq_var(chroma_sample_loc_type_top_field));
237 ue(mseq_var(chroma_sample_loc_type_bottom_field));
238 }
239
240 u(1, vvui_field(timing_info_present_flag));
241 if (vseq->vui_fields.bits.timing_info_present_flag) {
242 u(32, vseq_var(num_units_in_tick));
243 u(32, vseq_var(time_scale));
244 u(1, mseq_var(fixed_frame_rate_flag));
245 }
246
247 u(1, mseq_var(nal_hrd_parameters_present_flag));
248 if (mseq->nal_hrd_parameters_present_flag) {
249 ue(mseq_var(cpb_cnt_minus1));
250 u(4, mseq_var(bit_rate_scale));
251 u(4, mseq_var(cpb_size_scale));
252 for (i = 0; i <= mseq->cpb_cnt_minus1; i++) {
253 ue(mseq_var(bit_rate_value_minus1[i]));
254 ue(mseq_var(cpb_size_value_minus1[i]));
255 u(1, mseq_var(cbr_flag[i]));
256 }
257 u(5, mseq_var(initial_cpb_removal_delay_length_minus1));
258 u(5, mseq_var(cpb_removal_delay_length_minus1));
259 u(5, mseq_var(dpb_output_delay_length_minus1));
260 u(5, mseq_var(time_offset_length));
261 }
262 u(1, mseq_var(vcl_hrd_parameters_present_flag));
263 if (mseq->vcl_hrd_parameters_present_flag) {
264 av_assert0(0 && "vcl hrd parameters not supported");
265 }
266
267 if (mseq->nal_hrd_parameters_present_flag ||
268 mseq->vcl_hrd_parameters_present_flag)
269 u(1, mseq_var(low_delay_hrd_flag));
270 u(1, mseq_var(pic_struct_present_flag));
271
272 u(1, vvui_field(bitstream_restriction_flag));
273 if (vseq->vui_fields.bits.bitstream_restriction_flag) {
274 av_assert0(0 && "bitstream restrictions not supported");
275 }
276 }
277
278 static void vaapi_encode_h264_write_sps(PutBitContext *pbc,
279 VAAPIEncodeContext *ctx)
280 {
281 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
282 VAAPIEncodeH264Context *priv = ctx->priv_data;
283 VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
284 int i;
285
286 vaapi_encode_h264_write_nal_header(pbc, NAL_SPS, 3);
287
288 u(8, mseq_var(profile_idc));
289 u(1, mseq_var(constraint_set0_flag));
290 u(1, mseq_var(constraint_set1_flag));
291 u(1, mseq_var(constraint_set2_flag));
292 u(1, mseq_var(constraint_set3_flag));
293 u(1, mseq_var(constraint_set4_flag));
294 u(1, mseq_var(constraint_set5_flag));
295 u(2, 0, reserved_zero_2bits);
296
297 u(8, vseq_var(level_idc));
298
299 ue(vseq_var(seq_parameter_set_id));
300
301 if (mseq->profile_idc == 100 || mseq->profile_idc == 110 ||
302 mseq->profile_idc == 122 || mseq->profile_idc == 244 ||
303 mseq->profile_idc == 44 || mseq->profile_idc == 83 ||
304 mseq->profile_idc == 86 || mseq->profile_idc == 118 ||
305 mseq->profile_idc == 128 || mseq->profile_idc == 138) {
306 ue(vseq_field(chroma_format_idc));
307
308 if (vseq->seq_fields.bits.chroma_format_idc == 3)
309 u(1, mseq_var(separate_colour_plane_flag));
310
311 ue(vseq_var(bit_depth_luma_minus8));
312 ue(vseq_var(bit_depth_chroma_minus8));
313
314 u(1, mseq_var(qpprime_y_zero_transform_bypass_flag));
315
316 u(1, vseq_field(seq_scaling_matrix_present_flag));
317 if (vseq->seq_fields.bits.seq_scaling_matrix_present_flag) {
318 av_assert0(0 && "scaling matrices not supported");
319 }
320 }
321
322 ue(vseq_field(log2_max_frame_num_minus4));
323 ue(vseq_field(pic_order_cnt_type));
324
325 if (vseq->seq_fields.bits.pic_order_cnt_type == 0) {
326 ue(vseq_field(log2_max_pic_order_cnt_lsb_minus4));
327 } else if (vseq->seq_fields.bits.pic_order_cnt_type == 1) {
328 u(1, mseq_var(delta_pic_order_always_zero_flag));
329 se(vseq_var(offset_for_non_ref_pic));
330 se(vseq_var(offset_for_top_to_bottom_field));
331 ue(vseq_var(num_ref_frames_in_pic_order_cnt_cycle));
332
333 for (i = 0; i < vseq->num_ref_frames_in_pic_order_cnt_cycle; i++)
334 se(vseq_var(offset_for_ref_frame[i]));
335 }
336
337 ue(vseq_var(max_num_ref_frames));
338 u(1, mseq_var(gaps_in_frame_num_allowed_flag));
339
340 ue(vseq->picture_width_in_mbs - 1, pic_width_in_mbs_minus1);
341 ue(vseq->picture_height_in_mbs - 1, pic_height_in_mbs_minus1);
342
343 u(1, vseq_field(frame_mbs_only_flag));
344 if (!vseq->seq_fields.bits.frame_mbs_only_flag)
345 u(1, vseq_field(mb_adaptive_frame_field_flag));
346
347 u(1, vseq_field(direct_8x8_inference_flag));
348
349 u(1, vseq_var(frame_cropping_flag));
350 if (vseq->frame_cropping_flag) {
351 ue(vseq_var(frame_crop_left_offset));
352 ue(vseq_var(frame_crop_right_offset));
353 ue(vseq_var(frame_crop_top_offset));
354 ue(vseq_var(frame_crop_bottom_offset));
355 }
356
357 u(1, vseq_var(vui_parameters_present_flag));
358 if (vseq->vui_parameters_present_flag)
359 vaapi_encode_h264_write_vui(pbc, ctx);
360
361 vaapi_encode_h264_write_trailing_rbsp(pbc);
362 }
363
364 static void vaapi_encode_h264_write_pps(PutBitContext *pbc,
365 VAAPIEncodeContext *ctx)
366 {
367 VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
368 VAAPIEncodeH264Context *priv = ctx->priv_data;
369 VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
370
371 vaapi_encode_h264_write_nal_header(pbc, NAL_PPS, 3);
372
373 ue(vpic_var(pic_parameter_set_id));
374 ue(vpic_var(seq_parameter_set_id));
375
376 u(1, vpic_field(entropy_coding_mode_flag));
377 u(1, mseq_var(bottom_field_pic_order_in_frame_present_flag));
378
379 ue(mseq_var(num_slice_groups_minus1));
380 if (mseq->num_slice_groups_minus1 > 0) {
381 ue(mseq_var(slice_group_map_type));
382 av_assert0(0 && "slice groups not supported");
383 }
384
385 ue(vpic_var(num_ref_idx_l0_active_minus1));
386 ue(vpic_var(num_ref_idx_l1_active_minus1));
387
388 u(1, vpic_field(weighted_pred_flag));
389 u(2, vpic_field(weighted_bipred_idc));
390
391 se(vpic->pic_init_qp - 26, pic_init_qp_minus26);
392 se(mseq_var(pic_init_qs_minus26));
393 se(vpic_var(chroma_qp_index_offset));
394
395 u(1, vpic_field(deblocking_filter_control_present_flag));
396 u(1, vpic_field(constrained_intra_pred_flag));
397 u(1, vpic_field(redundant_pic_cnt_present_flag));
398 u(1, vpic_field(transform_8x8_mode_flag));
399
400 u(1, vpic_field(pic_scaling_matrix_present_flag));
401 if (vpic->pic_fields.bits.pic_scaling_matrix_present_flag) {
402 av_assert0(0 && "scaling matrices not supported");
403 }
404
405 se(vpic_var(second_chroma_qp_index_offset));
406
407 vaapi_encode_h264_write_trailing_rbsp(pbc);
408 }
409
410 static void vaapi_encode_h264_write_slice_header2(PutBitContext *pbc,
411 VAAPIEncodeContext *ctx,
412 VAAPIEncodePicture *pic,
413 VAAPIEncodeSlice *slice)
414 {
415 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
416 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
417 VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
418 VAAPIEncodeH264Context *priv = ctx->priv_data;
419 VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
420 VAAPIEncodeH264Slice *pslice = slice->priv_data;
421 VAAPIEncodeH264MiscSliceParams *mslice = &pslice->misc_slice_params;
422
423 vaapi_encode_h264_write_nal_header(pbc, mslice->nal_unit_type,
424 mslice->nal_ref_idc);
425
426 ue(vslice->macroblock_address, first_mb_in_slice);
427 ue(vslice_var(slice_type));
428 ue(vpic_var(pic_parameter_set_id));
429
430 if (mseq->separate_colour_plane_flag) {
431 u(2, mslice_var(colour_plane_id));
432 }
433
434 u(4 + vseq->seq_fields.bits.log2_max_frame_num_minus4,
435 (vpic->frame_num &
436 ((1 << (4 + vseq->seq_fields.bits.log2_max_frame_num_minus4)) - 1)),
437 frame_num);
438
439 if (!vseq->seq_fields.bits.frame_mbs_only_flag) {
440 u(1, mslice_var(field_pic_flag));
441 if (mslice->field_pic_flag)
442 u(1, mslice_var(bottom_field_flag));
443 }
444
445 if (vpic->pic_fields.bits.idr_pic_flag) {
446 ue(vslice_var(idr_pic_id));
447 }
448
449 if (vseq->seq_fields.bits.pic_order_cnt_type == 0) {
450 u(4 + vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4,
451 vslice_var(pic_order_cnt_lsb));
452 if (mseq->bottom_field_pic_order_in_frame_present_flag &&
453 !mslice->field_pic_flag) {
454 se(vslice_var(delta_pic_order_cnt_bottom));
455 }
456 }
457
458 if (vseq->seq_fields.bits.pic_order_cnt_type == 1 &&
459 !vseq->seq_fields.bits.delta_pic_order_always_zero_flag) {
460 se(vslice_var(delta_pic_order_cnt[0]));
461 if (mseq->bottom_field_pic_order_in_frame_present_flag &&
462 !mslice->field_pic_flag) {
463 se(vslice_var(delta_pic_order_cnt[1]));
464 }
465 }
466
467 if (vpic->pic_fields.bits.redundant_pic_cnt_present_flag) {
468 ue(mslice_var(redundant_pic_cnt));
469 }
470
471 if (vslice->slice_type == SLICE_TYPE_B) {
472 u(1, vslice_var(direct_spatial_mv_pred_flag));
473 }
474
475 if (vslice->slice_type == SLICE_TYPE_P ||
476 vslice->slice_type == SLICE_TYPE_SP ||
477 vslice->slice_type == SLICE_TYPE_B) {
478 u(1, vslice_var(num_ref_idx_active_override_flag));
479 if (vslice->num_ref_idx_active_override_flag) {
480 ue(vslice_var(num_ref_idx_l0_active_minus1));
481 if (vslice->slice_type == SLICE_TYPE_B)
482 ue(vslice_var(num_ref_idx_l1_active_minus1));
483 }
484 }
485
486 if (mslice->nal_unit_type == 20 || mslice->nal_unit_type == 21) {
487 av_assert0(0 && "no MVC support");
488 } else {
489 if (vslice->slice_type % 5 != 2 && vslice->slice_type % 5 != 4) {
490 u(1, mslice_var(ref_pic_list_modification_flag_l0));
491 if (mslice->ref_pic_list_modification_flag_l0) {
492 av_assert0(0 && "ref pic list modification");
493 }
494 }
495 if (vslice->slice_type % 5 == 1) {
496 u(1, mslice_var(ref_pic_list_modification_flag_l1));
497 if (mslice->ref_pic_list_modification_flag_l1) {
498 av_assert0(0 && "ref pic list modification");
499 }
500 }
501 }
502
503 if ((vpic->pic_fields.bits.weighted_pred_flag &&
504 (vslice->slice_type == SLICE_TYPE_P ||
505 vslice->slice_type == SLICE_TYPE_SP)) ||
506 (vpic->pic_fields.bits.weighted_bipred_idc == 1 &&
507 vslice->slice_type == SLICE_TYPE_B)) {
508 av_assert0(0 && "prediction weights not supported");
509 }
510
511 av_assert0(mslice->nal_ref_idc > 0 ==
512 vpic->pic_fields.bits.reference_pic_flag);
513 if (mslice->nal_ref_idc != 0) {
514 if (vpic->pic_fields.bits.idr_pic_flag) {
515 u(1, mslice_var(no_output_of_prior_pics_flag));
516 u(1, mslice_var(long_term_reference_flag));
517 } else {
518 u(1, mslice_var(adaptive_ref_pic_marking_mode_flag));
519 if (mslice->adaptive_ref_pic_marking_mode_flag) {
520 av_assert0(0 && "MMCOs not supported");
521 }
522 }
523 }
524
525 if (vpic->pic_fields.bits.entropy_coding_mode_flag &&
526 vslice->slice_type != SLICE_TYPE_I &&
527 vslice->slice_type != SLICE_TYPE_SI) {
528 ue(vslice_var(cabac_init_idc));
529 }
530
531 se(vslice_var(slice_qp_delta));
532 if (vslice->slice_type == SLICE_TYPE_SP ||
533 vslice->slice_type == SLICE_TYPE_SI) {
534 if (vslice->slice_type == SLICE_TYPE_SP)
535 u(1, mslice_var(sp_for_switch_flag));
536 se(mslice_var(slice_qs_delta));
537 }
538
539 if (vpic->pic_fields.bits.deblocking_filter_control_present_flag) {
540 ue(vslice_var(disable_deblocking_filter_idc));
541 if (vslice->disable_deblocking_filter_idc != 1) {
542 se(vslice_var(slice_alpha_c0_offset_div2));
543 se(vslice_var(slice_beta_offset_div2));
544 }
545 }
546
547 if (mseq->num_slice_groups_minus1 > 0 &&
548 mseq->slice_group_map_type >= 3 && mseq->slice_group_map_type <= 5) {
549 av_assert0(0 && "slice groups not supported");
550 }
551
552 // No alignment - this need not be a byte boundary.
553 }
554
555 static void vaapi_encode_h264_write_buffering_period(PutBitContext *pbc,
556 VAAPIEncodeContext *ctx,
557 VAAPIEncodePicture *pic)
558 {
559 VAAPIEncodeH264Context *priv = ctx->priv_data;
560 VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
561 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
562 int i;
563
564 ue(vpic_var(seq_parameter_set_id));
565
566 if (mseq->nal_hrd_parameters_present_flag) {
567 for (i = 0; i <= mseq->cpb_cnt_minus1; i++) {
568 u(mseq->initial_cpb_removal_delay_length_minus1 + 1,
569 mseq_var(initial_cpb_removal_delay));
570 u(mseq->initial_cpb_removal_delay_length_minus1 + 1,
571 mseq_var(initial_cpb_removal_delay_offset));
572 }
573 }
574 if (mseq->vcl_hrd_parameters_present_flag) {
575 av_assert0(0 && "vcl hrd parameters not supported");
576 }
577 }
578
579 static void vaapi_encode_h264_write_pic_timing(PutBitContext *pbc,
580 VAAPIEncodeContext *ctx,
581 VAAPIEncodePicture *pic)
582 {
583 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
584 VAAPIEncodeH264Context *priv = ctx->priv_data;
585 VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
586 int i, num_clock_ts;
587
588 if (mseq->nal_hrd_parameters_present_flag ||
589 mseq->vcl_hrd_parameters_present_flag) {
590 u(mseq->cpb_removal_delay_length_minus1 + 1,
591 2 * vseq->num_units_in_tick * priv->cpb_delay,
592 cpb_removal_delay);
593 u(mseq->dpb_output_delay_length_minus1 + 1,
594 2 * vseq->num_units_in_tick * priv->dpb_delay,
595 dpb_output_delay);
596 }
597 if (mseq->pic_struct_present_flag) {
598 u(4, mseq_var(pic_struct));
599 num_clock_ts = (mseq->pic_struct <= 2 ? 1 :
600 mseq->pic_struct <= 4 ? 2 :
601 mseq->pic_struct <= 8 ? 3 : 0);
602 for (i = 0; i < num_clock_ts; i++) {
603 u(1, 0, clock_timestamp_flag[i]);
604 // No full timestamp information.
605 }
606 }
607 }
608
609 static void vaapi_encode_h264_write_sei(PutBitContext *pbc,
610 VAAPIEncodeContext *ctx,
611 VAAPIEncodePicture *pic)
612 {
613 VAAPIEncodeH264Context *priv = ctx->priv_data;
614 PutBitContext payload_bits;
615 char payload[256];
616 int payload_type, payload_size, i;
617 void (*write_payload)(PutBitContext *pbc,
618 VAAPIEncodeContext *ctx,
619 VAAPIEncodePicture *pic) = NULL;
620
621 vaapi_encode_h264_write_nal_header(pbc, NAL_SEI, 0);
622
623 for (payload_type = 0; payload_type < 64; payload_type++) {
624 switch (payload_type) {
625 case SEI_TYPE_BUFFERING_PERIOD:
626 if (!priv->send_timing_sei ||
627 pic->type != PICTURE_TYPE_IDR)
628 continue;
629 write_payload = &vaapi_encode_h264_write_buffering_period;
630 break;
631 case SEI_TYPE_PIC_TIMING:
632 if (!priv->send_timing_sei)
633 continue;
634 write_payload = &vaapi_encode_h264_write_pic_timing;
635 break;
636 default:
637 continue;
638 }
639
640 init_put_bits(&payload_bits, payload, sizeof(payload));
641 write_payload(&payload_bits, ctx, pic);
642 if (put_bits_count(&payload_bits) & 7) {
643 write_u(&payload_bits, 1, 1, bit_equal_to_one);
644 while (put_bits_count(&payload_bits) & 7)
645 write_u(&payload_bits, 1, 0, bit_equal_to_zero);
646 }
647 payload_size = put_bits_count(&payload_bits) / 8;
648 flush_put_bits(&payload_bits);
649
650 u(8, payload_type, last_payload_type_byte);
651 u(8, payload_size, last_payload_size_byte);
652 for (i = 0; i < payload_size; i++)
653 u(8, payload[i] & 0xff, sei_payload);
654 }
655
656 vaapi_encode_h264_write_trailing_rbsp(pbc);
657 }
658
659 static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
660 char *data, size_t *data_len)
661 {
662 VAAPIEncodeContext *ctx = avctx->priv_data;
663 PutBitContext pbc;
664 char tmp[256];
665 int err;
666 size_t nal_len, bit_len, bit_pos, next_len;
667
668 bit_len = *data_len;
669 bit_pos = 0;
670
671 init_put_bits(&pbc, tmp, sizeof(tmp));
672 vaapi_encode_h264_write_sps(&pbc, ctx);
673 nal_len = put_bits_count(&pbc);
674 flush_put_bits(&pbc);
675
676 next_len = bit_len - bit_pos;
677 err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
678 &next_len,
679 tmp, nal_len);
680 if (err < 0)
681 return err;
682 bit_pos += next_len;
683
684 init_put_bits(&pbc, tmp, sizeof(tmp));
685 vaapi_encode_h264_write_pps(&pbc, ctx);
686 nal_len = put_bits_count(&pbc);
687 flush_put_bits(&pbc);
688
689 next_len = bit_len - bit_pos;
690 err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
691 &next_len,
692 tmp, nal_len);
693 if (err < 0)
694 return err;
695 bit_pos += next_len;
696
697 *data_len = bit_pos;
698 return 0;
699 }
700
701 static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
702 VAAPIEncodePicture *pic,
703 VAAPIEncodeSlice *slice,
704 char *data, size_t *data_len)
705 {
706 VAAPIEncodeContext *ctx = avctx->priv_data;
707 PutBitContext pbc;
708 char tmp[256];
709 size_t header_len;
710
711 init_put_bits(&pbc, tmp, sizeof(tmp));
712 vaapi_encode_h264_write_slice_header2(&pbc, ctx, pic, slice);
713 header_len = put_bits_count(&pbc);
714 flush_put_bits(&pbc);
715
716 return ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data, data_len,
717 tmp, header_len);
718 }
719
720 static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
721 VAAPIEncodePicture *pic,
722 int index, int *type,
723 char *data, size_t *data_len)
724 {
725 VAAPIEncodeContext *ctx = avctx->priv_data;
726 PutBitContext pbc;
727 char tmp[256];
728 size_t header_len;
729
730 if (index == 0 && ctx->va_rc_mode == VA_RC_CBR) {
731 *type = VAEncPackedHeaderH264_SEI;
732
733 init_put_bits(&pbc, tmp, sizeof(tmp));
734 vaapi_encode_h264_write_sei(&pbc, ctx, pic);
735 header_len = put_bits_count(&pbc);
736 flush_put_bits(&pbc);
737
738 return ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data, data_len,
739 tmp, header_len);
740
741 } else {
742 return AVERROR_EOF;
743 }
744 }
745
746 static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
747 {
748 VAAPIEncodeContext *ctx = avctx->priv_data;
749 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
750 VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
751 VAAPIEncodeH264Context *priv = ctx->priv_data;
752 VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
753 int i;
754
755 {
756 vseq->seq_parameter_set_id = 0;
757
758 vseq->level_idc = avctx->level;
759
760 vseq->max_num_ref_frames = 2;
761
762 vseq->picture_width_in_mbs = priv->mb_width;
763 vseq->picture_height_in_mbs = priv->mb_height;
764
765 vseq->seq_fields.bits.chroma_format_idc = 1;
766 vseq->seq_fields.bits.frame_mbs_only_flag = 1;
767 vseq->seq_fields.bits.direct_8x8_inference_flag = 1;
768 vseq->seq_fields.bits.log2_max_frame_num_minus4 = 4;
769 vseq->seq_fields.bits.pic_order_cnt_type = 0;
770
771 if (ctx->input_width != ctx->aligned_width ||
772 ctx->input_height != ctx->aligned_height) {
773 vseq->frame_cropping_flag = 1;
774
775 vseq->frame_crop_left_offset = 0;
776 vseq->frame_crop_right_offset =
777 (ctx->aligned_width - ctx->input_width) / 2;
778 vseq->frame_crop_top_offset = 0;
779 vseq->frame_crop_bottom_offset =
780 (ctx->aligned_height - ctx->input_height) / 2;
781 } else {
782 vseq->frame_cropping_flag = 0;
783 }
784
785 vseq->vui_parameters_present_flag = 1;
786 if (avctx->sample_aspect_ratio.num != 0) {
787 vseq->vui_fields.bits.aspect_ratio_info_present_flag = 1;
788 // There is a large enum of these which we could support
789 // individually rather than using the generic X/Y form?
790 if (avctx->sample_aspect_ratio.num ==
791 avctx->sample_aspect_ratio.den) {
792 vseq->aspect_ratio_idc = 1;
793 } else {
794 vseq->aspect_ratio_idc = 255; // Extended SAR.
795 vseq->sar_width = avctx->sample_aspect_ratio.num;
796 vseq->sar_height = avctx->sample_aspect_ratio.den;
797 }
798 }
799 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
800 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
801 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
802 mseq->video_signal_type_present_flag = 1;
803 mseq->video_format = 5; // Unspecified.
804 mseq->video_full_range_flag = 0;
805 mseq->colour_description_present_flag = 1;
806 // These enums are derived from the standard and hence
807 // we can just use the values directly.
808 mseq->colour_primaries = avctx->color_primaries;
809 mseq->transfer_characteristics = avctx->color_trc;
810 mseq->matrix_coefficients = avctx->colorspace;
811 }
812
813 vseq->bits_per_second = avctx->bit_rate;
814
815 vseq->vui_fields.bits.timing_info_present_flag = 1;
816 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
817 vseq->num_units_in_tick = avctx->framerate.num;
818 vseq->time_scale = 2 * avctx->framerate.den;
819 mseq->fixed_frame_rate_flag = 1;
820 } else {
821 vseq->num_units_in_tick = avctx->time_base.num;
822 vseq->time_scale = 2 * avctx->time_base.den;
823 mseq->fixed_frame_rate_flag = 0;
824 }
825
826 if (ctx->va_rc_mode == VA_RC_CBR) {
827 priv->send_timing_sei = 1;
828 mseq->nal_hrd_parameters_present_flag = 1;
829
830 mseq->cpb_cnt_minus1 = 0;
831
832 // Try to scale these to a sensible range so that the
833 // golomb encode of the value is not overlong.
834 mseq->bit_rate_scale =
835 av_clip(av_log2(avctx->bit_rate) - 15, 0, 15);
836 mseq->bit_rate_value_minus1[0] =
837 (avctx->bit_rate >> mseq->bit_rate_scale) - 1;
838
839 mseq->cpb_size_scale =
840 av_clip(av_log2(priv->hrd_params.hrd.buffer_size) - 15, 0, 15);
841 mseq->cpb_size_value_minus1[0] =
842 (priv->hrd_params.hrd.buffer_size >> mseq->cpb_size_scale) - 1;
843
844 // CBR mode isn't actually available here, despite naming.
845 mseq->cbr_flag[0] = 0;
846
847 mseq->initial_cpb_removal_delay_length_minus1 = 23;
848 mseq->cpb_removal_delay_length_minus1 = 23;
849 mseq->dpb_output_delay_length_minus1 = 7;
850 mseq->time_offset_length = 0;
851
852 // This calculation can easily overflow 32 bits.
853 mseq->initial_cpb_removal_delay = 90000 *
854 (uint64_t)priv->hrd_params.hrd.initial_buffer_fullness /
855 priv->hrd_params.hrd.buffer_size;
856
857 mseq->initial_cpb_removal_delay_offset = 0;
858 } else {
859 priv->send_timing_sei = 0;
860 mseq->nal_hrd_parameters_present_flag = 0;
861 }
862
863 vseq->intra_period = ctx->p_per_i * (ctx->b_per_p + 1);
864 vseq->intra_idr_period = vseq->intra_period;
865 vseq->ip_period = ctx->b_per_p + 1;
866 }
867
868 {
869 vpic->CurrPic.picture_id = VA_INVALID_ID;
870 vpic->CurrPic.flags = VA_PICTURE_H264_INVALID;
871
872 for (i = 0; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
873 vpic->ReferenceFrames[i].picture_id = VA_INVALID_ID;
874 vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
875 }
876
877 vpic->coded_buf = VA_INVALID_ID;
878
879 vpic->pic_parameter_set_id = 0;
880 vpic->seq_parameter_set_id = 0;
881
882 vpic->num_ref_idx_l0_active_minus1 = 0;
883 vpic->num_ref_idx_l1_active_minus1 = 0;
884
885 vpic->pic_fields.bits.entropy_coding_mode_flag =
886 ((avctx->profile & 0xff) != 66);
887 vpic->pic_fields.bits.weighted_pred_flag = 0;
888 vpic->pic_fields.bits.weighted_bipred_idc = 0;
889 vpic->pic_fields.bits.transform_8x8_mode_flag =
890 ((avctx->profile & 0xff) >= 100);
891
892 vpic->pic_init_qp = priv->fixed_qp_idr;
893 }
894
895 {
896 mseq->profile_idc = avctx->profile & 0xff;
897
898 if (avctx->profile & FF_PROFILE_H264_CONSTRAINED)
899 mseq->constraint_set1_flag = 1;
900 if (avctx->profile & FF_PROFILE_H264_INTRA)
901 mseq->constraint_set3_flag = 1;
902 }
903
904 return 0;
905 }
906
907 static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
908 VAAPIEncodePicture *pic)
909 {
910 VAAPIEncodeContext *ctx = avctx->priv_data;
911 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
912 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
913 VAAPIEncodeH264Context *priv = ctx->priv_data;
914 int i;
915
916 if (pic->type == PICTURE_TYPE_IDR) {
917 av_assert0(pic->display_order == pic->encode_order);
918 vpic->frame_num = 0;
919 priv->next_frame_num = 1;
920 priv->cpb_delay = 0;
921 } else {
922 vpic->frame_num = priv->next_frame_num;
923 if (pic->type != PICTURE_TYPE_B) {
924 // nal_ref_idc != 0
925 ++priv->next_frame_num;
926 }
927 ++priv->cpb_delay;
928 }
929 priv->dpb_delay = pic->display_order - pic->encode_order + 1;
930
931 vpic->frame_num = vpic->frame_num &
932 ((1 << (4 + vseq->seq_fields.bits.log2_max_frame_num_minus4)) - 1);
933
934 vpic->CurrPic.picture_id = pic->recon_surface;
935 vpic->CurrPic.frame_idx = vpic->frame_num;
936 vpic->CurrPic.flags = 0;
937 vpic->CurrPic.TopFieldOrderCnt = pic->display_order;
938 vpic->CurrPic.BottomFieldOrderCnt = pic->display_order;
939
940 for (i = 0; i < pic->nb_refs; i++) {
941 VAAPIEncodePicture *ref = pic->refs[i];
942 av_assert0(ref && ref->encode_order < pic->encode_order);
943 vpic->ReferenceFrames[i].picture_id = ref->recon_surface;
944 vpic->ReferenceFrames[i].frame_idx = ref->encode_order;
945 vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
946 vpic->ReferenceFrames[i].TopFieldOrderCnt = ref->display_order;
947 vpic->ReferenceFrames[i].BottomFieldOrderCnt = ref->display_order;
948 }
949 for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
950 vpic->ReferenceFrames[i].picture_id = VA_INVALID_ID;
951 vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
952 }
953
954 vpic->coded_buf = pic->output_buffer;
955
956 vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
957 vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
958
959 pic->nb_slices = 1;
960
961 return 0;
962 }
963
964 static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
965 VAAPIEncodePicture *pic,
966 VAAPIEncodeSlice *slice)
967 {
968 VAAPIEncodeContext *ctx = avctx->priv_data;
969 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
970 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
971 VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
972 VAAPIEncodeH264Context *priv = ctx->priv_data;
973 VAAPIEncodeH264Slice *pslice;
974 VAAPIEncodeH264MiscSliceParams *mslice;
975 int i;
976
977 slice->priv_data = av_mallocz(sizeof(*pslice));
978 if (!slice->priv_data)
979 return AVERROR(ENOMEM);
980 pslice = slice->priv_data;
981 mslice = &pslice->misc_slice_params;
982
983 if (pic->type == PICTURE_TYPE_IDR)
984 mslice->nal_unit_type = NAL_IDR_SLICE;
985 else
986 mslice->nal_unit_type = NAL_SLICE;
987
988 switch (pic->type) {
989 case PICTURE_TYPE_IDR:
990 vslice->slice_type = SLICE_TYPE_I;
991 mslice->nal_ref_idc = 3;
992 break;
993 case PICTURE_TYPE_I:
994 vslice->slice_type = SLICE_TYPE_I;
995 mslice->nal_ref_idc = 2;
996 break;
997 case PICTURE_TYPE_P:
998 vslice->slice_type = SLICE_TYPE_P;
999 mslice->nal_ref_idc = 1;
1000 break;
1001 case PICTURE_TYPE_B:
1002 vslice->slice_type = SLICE_TYPE_B;
1003 mslice->nal_ref_idc = 0;
1004 break;
1005 default:
1006 av_assert0(0 && "invalid picture type");
1007 }
1008
1009 // Only one slice per frame.
1010 vslice->macroblock_address = 0;
1011 vslice->num_macroblocks = priv->mb_width * priv->mb_height;
1012
1013 vslice->macroblock_info = VA_INVALID_ID;
1014
1015 vslice->pic_parameter_set_id = vpic->pic_parameter_set_id;
1016 vslice->idr_pic_id = priv->idr_pic_count++;
1017
1018 vslice->pic_order_cnt_lsb = pic->display_order &
1019 ((1 << (4 + vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4)) - 1);
1020
1021 for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
1022 vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
1023 vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1024 vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
1025 vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
1026 }
1027
1028 av_assert0(pic->nb_refs <= 2);
1029 if (pic->nb_refs >= 1) {
1030 // Backward reference for P- or B-frame.
1031 av_assert0(pic->type == PICTURE_TYPE_P ||
1032 pic->type == PICTURE_TYPE_B);
1033
1034 vslice->num_ref_idx_l0_active_minus1 = 0;
1035 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
1036 }
1037 if (pic->nb_refs >= 2) {
1038 // Forward reference for B-frame.
1039 av_assert0(pic->type == PICTURE_TYPE_B);
1040
1041 vslice->num_ref_idx_l1_active_minus1 = 0;
1042 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
1043 }
1044
1045 if (pic->type == PICTURE_TYPE_B)
1046 vslice->slice_qp_delta = priv->fixed_qp_b - vpic->pic_init_qp;
1047 else if (pic->type == PICTURE_TYPE_P)
1048 vslice->slice_qp_delta = priv->fixed_qp_p - vpic->pic_init_qp;
1049 else
1050 vslice->slice_qp_delta = priv->fixed_qp_idr - vpic->pic_init_qp;
1051
1052 vslice->direct_spatial_mv_pred_flag = 1;
1053
1054 return 0;
1055 }
1056
1057 static av_cold int vaapi_encode_h264_init_constant_bitrate(AVCodecContext *avctx)
1058 {
1059 VAAPIEncodeContext *ctx = avctx->priv_data;
1060 VAAPIEncodeH264Context *priv = ctx->priv_data;
1061 int hrd_buffer_size;
1062 int hrd_initial_buffer_fullness;
1063
1064 if (avctx->rc_buffer_size)
1065 hrd_buffer_size = avctx->rc_buffer_size;
1066 else
1067 hrd_buffer_size = avctx->bit_rate;
1068 if (avctx->rc_initial_buffer_occupancy)
1069 hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy;
1070 else
1071 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1072
1073 priv->rc_params.misc.type = VAEncMiscParameterTypeRateControl;
1074 priv->rc_params.rc = (VAEncMiscParameterRateControl) {
1075 .bits_per_second = avctx->bit_rate,
1076 .target_percentage = 66,
1077 .window_size = 1000,
1078 .initial_qp = (avctx->qmax >= 0 ? avctx->qmax : 40),
1079 .min_qp = (avctx->qmin >= 0 ? avctx->qmin : 18),
1080 .basic_unit_size = 0,
1081 };
1082 ctx->global_params[ctx->nb_global_params] =
1083 &priv->rc_params.misc;
1084 ctx->global_params_size[ctx->nb_global_params++] =
1085 sizeof(priv->rc_params);
1086
1087 priv->hrd_params.misc.type = VAEncMiscParameterTypeHRD;
1088 priv->hrd_params.hrd = (VAEncMiscParameterHRD) {
1089 .initial_buffer_fullness = hrd_initial_buffer_fullness,
1090 .buffer_size = hrd_buffer_size,
1091 };
1092 ctx->global_params[ctx->nb_global_params] =
1093 &priv->hrd_params.misc;
1094 ctx->global_params_size[ctx->nb_global_params++] =
1095 sizeof(priv->hrd_params);
1096
1097 // These still need to be set for pic_init_qp/slice_qp_delta.
1098 priv->fixed_qp_idr = 26;
1099 priv->fixed_qp_p = 26;
1100 priv->fixed_qp_b = 26;
1101
1102 av_log(avctx, AV_LOG_DEBUG, "Using constant-bitrate = %d bps.\n",
1103 avctx->bit_rate);
1104 return 0;
1105 }
1106
1107 static av_cold int vaapi_encode_h264_init_fixed_qp(AVCodecContext *avctx)
1108 {
1109 VAAPIEncodeContext *ctx = avctx->priv_data;
1110 VAAPIEncodeH264Context *priv = ctx->priv_data;
1111 VAAPIEncodeH264Options *opt = ctx->codec_options;
1112
1113 priv->fixed_qp_p = opt->qp;
1114 if (avctx->i_quant_factor > 0.0)
1115 priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
1116 avctx->i_quant_offset) + 0.5);
1117 else
1118 priv->fixed_qp_idr = priv->fixed_qp_p;
1119 if (avctx->b_quant_factor > 0.0)
1120 priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
1121 avctx->b_quant_offset) + 0.5);
1122 else
1123 priv->fixed_qp_b = priv->fixed_qp_p;
1124
1125 av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
1126 "%d / %d / %d for IDR- / P- / B-frames.\n",
1127 priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1128 return 0;
1129 }
1130
1131 static av_cold int vaapi_encode_h264_init_internal(AVCodecContext *avctx)
1132 {
1133 static const VAConfigAttrib default_config_attributes[] = {
1134 { .type = VAConfigAttribRTFormat,
1135 .value = VA_RT_FORMAT_YUV420 },
1136 { .type = VAConfigAttribEncPackedHeaders,
1137 .value = (VA_ENC_PACKED_HEADER_SEQUENCE |
1138 VA_ENC_PACKED_HEADER_SLICE) },
1139 };
1140
1141 VAAPIEncodeContext *ctx = avctx->priv_data;
1142 VAAPIEncodeH264Context *priv = ctx->priv_data;
1143 VAAPIEncodeH264Options *opt = ctx->codec_options;
1144 int i, err;
1145
1146 switch (avctx->profile) {
1147 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
1148 ctx->va_profile = VAProfileH264ConstrainedBaseline;
1149 break;
1150 case FF_PROFILE_H264_BASELINE:
1151 ctx->va_profile = VAProfileH264Baseline;
1152 break;
1153 case FF_PROFILE_H264_MAIN:
1154 ctx->va_profile = VAProfileH264Main;
1155 break;
1156 case FF_PROFILE_H264_EXTENDED:
1157 av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
1158 "is not supported.\n");
1159 return AVERROR_PATCHWELCOME;
1160 case FF_PROFILE_UNKNOWN:
1161 case FF_PROFILE_H264_HIGH:
1162 ctx->va_profile = VAProfileH264High;
1163 break;
1164 case FF_PROFILE_H264_HIGH_10:
1165 case FF_PROFILE_H264_HIGH_10_INTRA:
1166 av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
1167 "are not supported.\n");
1168 return AVERROR_PATCHWELCOME;
1169 case FF_PROFILE_H264_HIGH_422:
1170 case FF_PROFILE_H264_HIGH_422_INTRA:
1171 case FF_PROFILE_H264_HIGH_444:
1172 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
1173 case FF_PROFILE_H264_HIGH_444_INTRA:
1174 case FF_PROFILE_H264_CAVLC_444:
1175 av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
1176 "are not supported.\n");
1177 return AVERROR_PATCHWELCOME;
1178 default:
1179 av_log(avctx, AV_LOG_ERROR, "Unknown H.264 profile %d.\n",
1180 avctx->profile);
1181 return AVERROR(EINVAL);
1182 }
1183 if (opt->low_power) {
1184 #if VA_CHECK_VERSION(0, 39, 1)
1185 ctx->va_entrypoint = VAEntrypointEncSliceLP;
1186 #else
1187 av_log(avctx, AV_LOG_ERROR, "Low-power encoding is not "
1188 "supported with this VAAPI version.\n");
1189 return AVERROR(EINVAL);
1190 #endif
1191 } else {
1192 ctx->va_entrypoint = VAEntrypointEncSlice;
1193 }
1194
1195 ctx->input_width = avctx->width;
1196 ctx->input_height = avctx->height;
1197 ctx->aligned_width = FFALIGN(ctx->input_width, 16);
1198 ctx->aligned_height = FFALIGN(ctx->input_height, 16);
1199 priv->mb_width = ctx->aligned_width / 16;
1200 priv->mb_height = ctx->aligned_height / 16;
1201
1202 for (i = 0; i < FF_ARRAY_ELEMS(default_config_attributes); i++) {
1203 ctx->config_attributes[ctx->nb_config_attributes++] =
1204 default_config_attributes[i];
1205 }
1206
1207 if (avctx->bit_rate > 0) {
1208 ctx->va_rc_mode = VA_RC_CBR;
1209 err = vaapi_encode_h264_init_constant_bitrate(avctx);
1210 } else {
1211 ctx->va_rc_mode = VA_RC_CQP;
1212 err = vaapi_encode_h264_init_fixed_qp(avctx);
1213 }
1214 if (err < 0)
1215 return err;
1216
1217 ctx->config_attributes[ctx->nb_config_attributes++] = (VAConfigAttrib) {
1218 .type = VAConfigAttribRateControl,
1219 .value = ctx->va_rc_mode,
1220 };
1221
1222 if (opt->quality > 0) {
1223 #if VA_CHECK_VERSION(0, 36, 0)
1224 priv->quality_params.misc.type =
1225 VAEncMiscParameterTypeQualityLevel;
1226 priv->quality_params.quality.quality_level = opt->quality;
1227
1228 ctx->global_params[ctx->nb_global_params] =
1229 &priv->quality_params.misc;
1230 ctx->global_params_size[ctx->nb_global_params++] =
1231 sizeof(priv->quality_params);
1232 #else
1233 av_log(avctx, AV_LOG_WARNING, "The encode quality option is not "
1234 "supported with this VAAPI version.\n");
1235 #endif
1236 }
1237
1238 ctx->nb_recon_frames = 20;
1239
1240 return 0;
1241 }
1242
1243 static VAAPIEncodeType vaapi_encode_type_h264 = {
1244 .priv_data_size = sizeof(VAAPIEncodeH264Context),
1245
1246 .init = &vaapi_encode_h264_init_internal,
1247
1248 .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
1249 .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
1250
1251 .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
1252 .init_picture_params = &vaapi_encode_h264_init_picture_params,
1253
1254 .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
1255 .init_slice_params = &vaapi_encode_h264_init_slice_params,
1256
1257 .sequence_header_type = VAEncPackedHeaderSequence,
1258 .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
1259
1260 .slice_header_type = VAEncPackedHeaderH264_Slice,
1261 .write_slice_header = &vaapi_encode_h264_write_slice_header,
1262
1263 .write_extra_header = &vaapi_encode_h264_write_extra_header,
1264 };
1265
1266 static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
1267 {
1268 return ff_vaapi_encode_init(avctx, &vaapi_encode_type_h264);
1269 }
1270
1271 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
1272 offsetof(VAAPIEncodeH264Options, x))
1273 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1274 static const AVOption vaapi_encode_h264_options[] = {
1275 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1276 OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
1277 { "quality", "Set encode quality (trades off against speed, higher is faster)",
1278 OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8, FLAGS },
1279 { "low_power", "Use low-power encoding mode (experimental: only supported "
1280 "on some platforms, does not support all features)",
1281 OFFSET(low_power), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
1282 { NULL },
1283 };
1284
1285 static const AVCodecDefault vaapi_encode_h264_defaults[] = {
1286 { "profile", "100" },
1287 { "level", "51" },
1288 { "b", "0" },
1289 { "bf", "2" },
1290 { "g", "120" },
1291 { "i_qfactor", "1.0" },
1292 { "i_qoffset", "0.0" },
1293 { "b_qfactor", "1.2" },
1294 { "b_qoffset", "0.0" },
1295 { NULL },
1296 };
1297
1298 static const AVClass vaapi_encode_h264_class = {
1299 .class_name = "h264_vaapi",
1300 .item_name = av_default_item_name,
1301 .option = vaapi_encode_h264_options,
1302 .version = LIBAVUTIL_VERSION_INT,
1303 };
1304
1305 AVCodec ff_h264_vaapi_encoder = {
1306 .name = "h264_vaapi",
1307 .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
1308 .type = AVMEDIA_TYPE_VIDEO,
1309 .id = AV_CODEC_ID_H264,
1310 .priv_data_size = (sizeof(VAAPIEncodeContext) +
1311 sizeof(VAAPIEncodeH264Options)),
1312 .init = &vaapi_encode_h264_init,
1313 .encode2 = &ff_vaapi_encode2,
1314 .close = &ff_vaapi_encode_close,
1315 .priv_class = &vaapi_encode_h264_class,
1316 .capabilities = AV_CODEC_CAP_DELAY,
1317 .defaults = vaapi_encode_h264_defaults,
1318 .pix_fmts = (const enum AVPixelFormat[]) {
1319 AV_PIX_FMT_VAAPI,
1320 AV_PIX_FMT_NONE,
1321 },
1322 };