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