avcodec/mpeg4_unpack_bframes_bsf: Copy packet props
[ffmpeg.git] / libavcodec / vaapi_encode_h264.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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_identifier(PutBitContext *pbc,
610                                                VAAPIEncodeContext *ctx,
611                                                VAAPIEncodePicture *pic)
612 {
613     const char *lavc   = LIBAVCODEC_IDENT;
614     const char *vaapi  = VA_VERSION_S;
615     const char *driver = vaQueryVendorString(ctx->hwctx->display);
616     char tmp[256];
617     int i;
618
619     // Random (version 4) ISO 11578 UUID.
620     uint8_t uuid[16] = {
621         0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
622         0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
623     };
624
625     for (i = 0; i < 16; i++)
626         u(8, uuid[i], uuid_iso_iec_11578);
627
628     snprintf(tmp, sizeof(tmp), "%s / VAAPI %s / %s", lavc, vaapi, driver);
629     for (i = 0; i < sizeof(tmp) && tmp[i]; i++)
630         u(8, tmp[i], user_data_payload_byte);
631 }
632
633 static void vaapi_encode_h264_write_sei(PutBitContext *pbc,
634                                         VAAPIEncodeContext *ctx,
635                                         VAAPIEncodePicture *pic)
636 {
637     VAAPIEncodeH264Context *priv = ctx->priv_data;
638     PutBitContext payload_bits;
639     char payload[256];
640     int payload_type, payload_size, i;
641     void (*write_payload)(PutBitContext *pbc,
642                           VAAPIEncodeContext *ctx,
643                           VAAPIEncodePicture *pic) = NULL;
644
645     vaapi_encode_h264_write_nal_header(pbc, NAL_SEI, 0);
646
647     for (payload_type = 0; payload_type < 64; payload_type++) {
648         switch (payload_type) {
649         case SEI_TYPE_BUFFERING_PERIOD:
650             if (!priv->send_timing_sei ||
651                 pic->type != PICTURE_TYPE_IDR)
652                 continue;
653             write_payload = &vaapi_encode_h264_write_buffering_period;
654             break;
655         case SEI_TYPE_PIC_TIMING:
656             if (!priv->send_timing_sei)
657                 continue;
658             write_payload = &vaapi_encode_h264_write_pic_timing;
659             break;
660         case SEI_TYPE_USER_DATA_UNREGISTERED:
661             if (pic->encode_order != 0)
662                 continue;
663             write_payload = &vaapi_encode_h264_write_identifier;
664             break;
665         default:
666             continue;
667         }
668
669         init_put_bits(&payload_bits, payload, sizeof(payload));
670         write_payload(&payload_bits, ctx, pic);
671         if (put_bits_count(&payload_bits) & 7) {
672             write_u(&payload_bits, 1, 1, bit_equal_to_one);
673             while (put_bits_count(&payload_bits) & 7)
674                 write_u(&payload_bits, 1, 0, bit_equal_to_zero);
675         }
676         payload_size = put_bits_count(&payload_bits) / 8;
677         flush_put_bits(&payload_bits);
678
679         u(8, payload_type, last_payload_type_byte);
680         u(8, payload_size, last_payload_size_byte);
681         for (i = 0; i < payload_size; i++)
682             u(8, payload[i] & 0xff, sei_payload);
683     }
684
685     vaapi_encode_h264_write_trailing_rbsp(pbc);
686 }
687
688 static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
689                                                    char *data, size_t *data_len)
690 {
691     VAAPIEncodeContext *ctx = avctx->priv_data;
692     PutBitContext pbc;
693     char tmp[256];
694     int err;
695     size_t nal_len, bit_len, bit_pos, next_len;
696
697     bit_len = *data_len;
698     bit_pos = 0;
699
700     init_put_bits(&pbc, tmp, sizeof(tmp));
701     vaapi_encode_h264_write_sps(&pbc, ctx);
702     nal_len = put_bits_count(&pbc);
703     flush_put_bits(&pbc);
704
705     next_len = bit_len - bit_pos;
706     err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
707                                                        &next_len,
708                                                        tmp, nal_len);
709     if (err < 0)
710         return err;
711     bit_pos += next_len;
712
713     init_put_bits(&pbc, tmp, sizeof(tmp));
714     vaapi_encode_h264_write_pps(&pbc, ctx);
715     nal_len = put_bits_count(&pbc);
716     flush_put_bits(&pbc);
717
718     next_len = bit_len - bit_pos;
719     err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
720                                                        &next_len,
721                                                        tmp, nal_len);
722     if (err < 0)
723         return err;
724     bit_pos += next_len;
725
726     *data_len = bit_pos;
727     return 0;
728 }
729
730 static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
731                                                 VAAPIEncodePicture *pic,
732                                                 VAAPIEncodeSlice *slice,
733                                                 char *data, size_t *data_len)
734 {
735     VAAPIEncodeContext *ctx = avctx->priv_data;
736     PutBitContext pbc;
737     char tmp[256];
738     size_t header_len;
739
740     init_put_bits(&pbc, tmp, sizeof(tmp));
741     vaapi_encode_h264_write_slice_header2(&pbc, ctx, pic, slice);
742     header_len = put_bits_count(&pbc);
743     flush_put_bits(&pbc);
744
745     return ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data, data_len,
746                                                         tmp, header_len);
747 }
748
749 static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
750                                                 VAAPIEncodePicture *pic,
751                                                 int index, int *type,
752                                                 char *data, size_t *data_len)
753 {
754     VAAPIEncodeContext *ctx = avctx->priv_data;
755     PutBitContext pbc;
756     char tmp[256];
757     size_t header_len;
758
759     if (index == 0 && ctx->va_rc_mode == VA_RC_CBR) {
760         *type = VAEncPackedHeaderH264_SEI;
761
762         init_put_bits(&pbc, tmp, sizeof(tmp));
763         vaapi_encode_h264_write_sei(&pbc, ctx, pic);
764         header_len = put_bits_count(&pbc);
765         flush_put_bits(&pbc);
766
767         return ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data, data_len,
768                                                             tmp, header_len);
769
770     } else {
771         return AVERROR_EOF;
772     }
773 }
774
775 static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
776 {
777     VAAPIEncodeContext                 *ctx = avctx->priv_data;
778     VAEncSequenceParameterBufferH264  *vseq = ctx->codec_sequence_params;
779     VAEncPictureParameterBufferH264   *vpic = ctx->codec_picture_params;
780     VAAPIEncodeH264Context            *priv = ctx->priv_data;
781     VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
782     int i;
783
784     {
785         vseq->seq_parameter_set_id = 0;
786
787         vseq->level_idc = avctx->level;
788
789         vseq->max_num_ref_frames = 2;
790
791         vseq->picture_width_in_mbs  = priv->mb_width;
792         vseq->picture_height_in_mbs = priv->mb_height;
793
794         vseq->seq_fields.bits.chroma_format_idc = 1;
795         vseq->seq_fields.bits.frame_mbs_only_flag = 1;
796         vseq->seq_fields.bits.direct_8x8_inference_flag = 1;
797         vseq->seq_fields.bits.log2_max_frame_num_minus4 = 4;
798         vseq->seq_fields.bits.pic_order_cnt_type = 0;
799
800         if (ctx->input_width  != ctx->aligned_width ||
801             ctx->input_height != ctx->aligned_height) {
802             vseq->frame_cropping_flag = 1;
803
804             vseq->frame_crop_left_offset   = 0;
805             vseq->frame_crop_right_offset  =
806                 (ctx->aligned_width - ctx->input_width) / 2;
807             vseq->frame_crop_top_offset    = 0;
808             vseq->frame_crop_bottom_offset =
809                 (ctx->aligned_height - ctx->input_height) / 2;
810         } else {
811             vseq->frame_cropping_flag = 0;
812         }
813
814         vseq->vui_parameters_present_flag = 1;
815         if (avctx->sample_aspect_ratio.num != 0) {
816             vseq->vui_fields.bits.aspect_ratio_info_present_flag = 1;
817             // There is a large enum of these which we could support
818             // individually rather than using the generic X/Y form?
819             if (avctx->sample_aspect_ratio.num ==
820                 avctx->sample_aspect_ratio.den) {
821                 vseq->aspect_ratio_idc = 1;
822             } else {
823                 vseq->aspect_ratio_idc = 255; // Extended SAR.
824                 vseq->sar_width  = avctx->sample_aspect_ratio.num;
825                 vseq->sar_height = avctx->sample_aspect_ratio.den;
826             }
827         }
828         if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
829             avctx->color_trc       != AVCOL_TRC_UNSPECIFIED ||
830             avctx->colorspace      != AVCOL_SPC_UNSPECIFIED) {
831             mseq->video_signal_type_present_flag = 1;
832             mseq->video_format             = 5; // Unspecified.
833             mseq->video_full_range_flag    = 0;
834             mseq->colour_description_present_flag = 1;
835             // These enums are derived from the standard and hence
836             // we can just use the values directly.
837             mseq->colour_primaries         = avctx->color_primaries;
838             mseq->transfer_characteristics = avctx->color_trc;
839             mseq->matrix_coefficients      = avctx->colorspace;
840         }
841
842         vseq->bits_per_second = avctx->bit_rate;
843
844         vseq->vui_fields.bits.timing_info_present_flag = 1;
845         if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
846             vseq->num_units_in_tick = avctx->framerate.num;
847             vseq->time_scale        = 2 * avctx->framerate.den;
848             mseq->fixed_frame_rate_flag = 1;
849         } else {
850             vseq->num_units_in_tick = avctx->time_base.num;
851             vseq->time_scale        = 2 * avctx->time_base.den;
852             mseq->fixed_frame_rate_flag = 0;
853         }
854
855         if (ctx->va_rc_mode == VA_RC_CBR) {
856             priv->send_timing_sei = 1;
857             mseq->nal_hrd_parameters_present_flag = 1;
858
859             mseq->cpb_cnt_minus1 = 0;
860
861             // Try to scale these to a sensible range so that the
862             // golomb encode of the value is not overlong.
863             mseq->bit_rate_scale =
864                 av_clip_uintp2(av_log2(avctx->bit_rate) - 15, 4);
865             mseq->bit_rate_value_minus1[0] =
866                 (avctx->bit_rate >> mseq->bit_rate_scale) - 1;
867
868             mseq->cpb_size_scale =
869                 av_clip_uintp2(av_log2(priv->hrd_params.hrd.buffer_size) - 15, 4);
870             mseq->cpb_size_value_minus1[0] =
871                 (priv->hrd_params.hrd.buffer_size >> mseq->cpb_size_scale) - 1;
872
873             // CBR mode isn't actually available here, despite naming.
874             mseq->cbr_flag[0] = 0;
875
876             mseq->initial_cpb_removal_delay_length_minus1 = 23;
877             mseq->cpb_removal_delay_length_minus1         = 23;
878             mseq->dpb_output_delay_length_minus1          = 7;
879             mseq->time_offset_length = 0;
880
881             // This calculation can easily overflow 32 bits.
882             mseq->initial_cpb_removal_delay = 90000 *
883                 (uint64_t)priv->hrd_params.hrd.initial_buffer_fullness /
884                 priv->hrd_params.hrd.buffer_size;
885
886             mseq->initial_cpb_removal_delay_offset = 0;
887         } else {
888             priv->send_timing_sei = 0;
889             mseq->nal_hrd_parameters_present_flag = 0;
890         }
891
892         vseq->intra_period     = ctx->p_per_i * (ctx->b_per_p + 1);
893         vseq->intra_idr_period = vseq->intra_period;
894         vseq->ip_period        = ctx->b_per_p + 1;
895     }
896
897     {
898         vpic->CurrPic.picture_id = VA_INVALID_ID;
899         vpic->CurrPic.flags      = VA_PICTURE_H264_INVALID;
900
901         for (i = 0; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
902             vpic->ReferenceFrames[i].picture_id = VA_INVALID_ID;
903             vpic->ReferenceFrames[i].flags      = VA_PICTURE_H264_INVALID;
904         }
905
906         vpic->coded_buf = VA_INVALID_ID;
907
908         vpic->pic_parameter_set_id = 0;
909         vpic->seq_parameter_set_id = 0;
910
911         vpic->num_ref_idx_l0_active_minus1 = 0;
912         vpic->num_ref_idx_l1_active_minus1 = 0;
913
914         vpic->pic_fields.bits.entropy_coding_mode_flag =
915             ((avctx->profile & 0xff) != 66);
916         vpic->pic_fields.bits.weighted_pred_flag = 0;
917         vpic->pic_fields.bits.weighted_bipred_idc = 0;
918         vpic->pic_fields.bits.transform_8x8_mode_flag =
919             ((avctx->profile & 0xff) >= 100);
920
921         vpic->pic_init_qp = priv->fixed_qp_idr;
922     }
923
924     {
925         mseq->profile_idc = avctx->profile & 0xff;
926
927         if (avctx->profile & FF_PROFILE_H264_CONSTRAINED)
928             mseq->constraint_set1_flag = 1;
929         if (avctx->profile & FF_PROFILE_H264_INTRA)
930             mseq->constraint_set3_flag = 1;
931     }
932
933     return 0;
934 }
935
936 static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
937                                                  VAAPIEncodePicture *pic)
938 {
939     VAAPIEncodeContext                *ctx = avctx->priv_data;
940     VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
941     VAEncPictureParameterBufferH264  *vpic = pic->codec_picture_params;
942     VAAPIEncodeH264Context           *priv = ctx->priv_data;
943     int i;
944
945     if (pic->type == PICTURE_TYPE_IDR) {
946         av_assert0(pic->display_order == pic->encode_order);
947         vpic->frame_num = 0;
948         priv->next_frame_num = 1;
949         priv->cpb_delay = 0;
950     } else {
951         vpic->frame_num = priv->next_frame_num;
952         if (pic->type != PICTURE_TYPE_B) {
953             // nal_ref_idc != 0
954             ++priv->next_frame_num;
955         }
956         ++priv->cpb_delay;
957     }
958     priv->dpb_delay = pic->display_order - pic->encode_order + 1;
959
960     vpic->frame_num = vpic->frame_num &
961         ((1 << (4 + vseq->seq_fields.bits.log2_max_frame_num_minus4)) - 1);
962
963     vpic->CurrPic.picture_id          = pic->recon_surface;
964     vpic->CurrPic.frame_idx           = vpic->frame_num;
965     vpic->CurrPic.flags               = 0;
966     vpic->CurrPic.TopFieldOrderCnt    = pic->display_order;
967     vpic->CurrPic.BottomFieldOrderCnt = pic->display_order;
968
969     for (i = 0; i < pic->nb_refs; i++) {
970         VAAPIEncodePicture *ref = pic->refs[i];
971         av_assert0(ref && ref->encode_order < pic->encode_order);
972         vpic->ReferenceFrames[i].picture_id = ref->recon_surface;
973         vpic->ReferenceFrames[i].frame_idx  = ref->encode_order;
974         vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
975         vpic->ReferenceFrames[i].TopFieldOrderCnt    = ref->display_order;
976         vpic->ReferenceFrames[i].BottomFieldOrderCnt = ref->display_order;
977     }
978     for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
979         vpic->ReferenceFrames[i].picture_id = VA_INVALID_ID;
980         vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
981     }
982
983     vpic->coded_buf = pic->output_buffer;
984
985     vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
986     vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
987
988     pic->nb_slices = 1;
989
990     return 0;
991 }
992
993 static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
994                                                VAAPIEncodePicture *pic,
995                                                VAAPIEncodeSlice *slice)
996 {
997     VAAPIEncodeContext                 *ctx = avctx->priv_data;
998     VAEncSequenceParameterBufferH264  *vseq = ctx->codec_sequence_params;
999     VAEncPictureParameterBufferH264   *vpic = pic->codec_picture_params;
1000     VAEncSliceParameterBufferH264   *vslice = slice->codec_slice_params;
1001     VAAPIEncodeH264Context            *priv = ctx->priv_data;
1002     VAAPIEncodeH264Slice            *pslice;
1003     VAAPIEncodeH264MiscSliceParams  *mslice;
1004     int i;
1005
1006     slice->priv_data = av_mallocz(sizeof(*pslice));
1007     if (!slice->priv_data)
1008         return AVERROR(ENOMEM);
1009     pslice = slice->priv_data;
1010     mslice = &pslice->misc_slice_params;
1011
1012     if (pic->type == PICTURE_TYPE_IDR)
1013         mslice->nal_unit_type = NAL_IDR_SLICE;
1014     else
1015         mslice->nal_unit_type = NAL_SLICE;
1016
1017     switch (pic->type) {
1018     case PICTURE_TYPE_IDR:
1019         vslice->slice_type  = SLICE_TYPE_I;
1020         mslice->nal_ref_idc = 3;
1021         break;
1022     case PICTURE_TYPE_I:
1023         vslice->slice_type  = SLICE_TYPE_I;
1024         mslice->nal_ref_idc = 2;
1025         break;
1026     case PICTURE_TYPE_P:
1027         vslice->slice_type  = SLICE_TYPE_P;
1028         mslice->nal_ref_idc = 1;
1029         break;
1030     case PICTURE_TYPE_B:
1031         vslice->slice_type  = SLICE_TYPE_B;
1032         mslice->nal_ref_idc = 0;
1033         break;
1034     default:
1035         av_assert0(0 && "invalid picture type");
1036     }
1037
1038     // Only one slice per frame.
1039     vslice->macroblock_address = 0;
1040     vslice->num_macroblocks = priv->mb_width * priv->mb_height;
1041
1042     vslice->macroblock_info = VA_INVALID_ID;
1043
1044     vslice->pic_parameter_set_id = vpic->pic_parameter_set_id;
1045     vslice->idr_pic_id = priv->idr_pic_count++;
1046
1047     vslice->pic_order_cnt_lsb = pic->display_order &
1048         ((1 << (4 + vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4)) - 1);
1049
1050     for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
1051         vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
1052         vslice->RefPicList0[i].flags      = VA_PICTURE_H264_INVALID;
1053         vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
1054         vslice->RefPicList1[i].flags      = VA_PICTURE_H264_INVALID;
1055     }
1056
1057     av_assert0(pic->nb_refs <= 2);
1058     if (pic->nb_refs >= 1) {
1059         // Backward reference for P- or B-frame.
1060         av_assert0(pic->type == PICTURE_TYPE_P ||
1061                    pic->type == PICTURE_TYPE_B);
1062
1063         vslice->num_ref_idx_l0_active_minus1 = 0;
1064         vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
1065     }
1066     if (pic->nb_refs >= 2) {
1067         // Forward reference for B-frame.
1068         av_assert0(pic->type == PICTURE_TYPE_B);
1069
1070         vslice->num_ref_idx_l1_active_minus1 = 0;
1071         vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
1072     }
1073
1074     if (pic->type == PICTURE_TYPE_B)
1075         vslice->slice_qp_delta = priv->fixed_qp_b - vpic->pic_init_qp;
1076     else if (pic->type == PICTURE_TYPE_P)
1077         vslice->slice_qp_delta = priv->fixed_qp_p - vpic->pic_init_qp;
1078     else
1079         vslice->slice_qp_delta = priv->fixed_qp_idr - vpic->pic_init_qp;
1080
1081     vslice->direct_spatial_mv_pred_flag = 1;
1082
1083     return 0;
1084 }
1085
1086 static av_cold int vaapi_encode_h264_init_constant_bitrate(AVCodecContext *avctx)
1087 {
1088     VAAPIEncodeContext      *ctx = avctx->priv_data;
1089     VAAPIEncodeH264Context *priv = ctx->priv_data;
1090     int hrd_buffer_size;
1091     int hrd_initial_buffer_fullness;
1092
1093     if (avctx->bit_rate > INT32_MAX) {
1094         av_log(avctx, AV_LOG_ERROR, "Target bitrate of 2^31 bps or "
1095                "higher is not supported.\n");
1096         return AVERROR(EINVAL);
1097     }
1098
1099     if (avctx->rc_buffer_size)
1100         hrd_buffer_size = avctx->rc_buffer_size;
1101     else
1102         hrd_buffer_size = avctx->bit_rate;
1103     if (avctx->rc_initial_buffer_occupancy)
1104         hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy;
1105     else
1106         hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1107
1108     priv->rc_params.misc.type = VAEncMiscParameterTypeRateControl;
1109     priv->rc_params.rc = (VAEncMiscParameterRateControl) {
1110         .bits_per_second   = avctx->bit_rate,
1111         .target_percentage = 66,
1112         .window_size       = 1000,
1113         .initial_qp        = (avctx->qmax >= 0 ? avctx->qmax : 40),
1114         .min_qp            = (avctx->qmin >= 0 ? avctx->qmin : 18),
1115         .basic_unit_size   = 0,
1116     };
1117     ctx->global_params[ctx->nb_global_params] =
1118         &priv->rc_params.misc;
1119     ctx->global_params_size[ctx->nb_global_params++] =
1120         sizeof(priv->rc_params);
1121
1122     priv->hrd_params.misc.type = VAEncMiscParameterTypeHRD;
1123     priv->hrd_params.hrd = (VAEncMiscParameterHRD) {
1124         .initial_buffer_fullness = hrd_initial_buffer_fullness,
1125         .buffer_size             = hrd_buffer_size,
1126     };
1127     ctx->global_params[ctx->nb_global_params] =
1128         &priv->hrd_params.misc;
1129     ctx->global_params_size[ctx->nb_global_params++] =
1130         sizeof(priv->hrd_params);
1131
1132     // These still need to be  set for pic_init_qp/slice_qp_delta.
1133     priv->fixed_qp_idr = 26;
1134     priv->fixed_qp_p   = 26;
1135     priv->fixed_qp_b   = 26;
1136
1137     av_log(avctx, AV_LOG_DEBUG, "Using constant-bitrate = %"PRId64" bps.\n",
1138            avctx->bit_rate);
1139     return 0;
1140 }
1141
1142 static av_cold int vaapi_encode_h264_init_fixed_qp(AVCodecContext *avctx)
1143 {
1144     VAAPIEncodeContext      *ctx = avctx->priv_data;
1145     VAAPIEncodeH264Context *priv = ctx->priv_data;
1146     VAAPIEncodeH264Options  *opt = ctx->codec_options;
1147
1148     priv->fixed_qp_p = opt->qp;
1149     if (avctx->i_quant_factor > 0.0)
1150         priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
1151                                     avctx->i_quant_offset) + 0.5);
1152     else
1153         priv->fixed_qp_idr = priv->fixed_qp_p;
1154     if (avctx->b_quant_factor > 0.0)
1155         priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
1156                                   avctx->b_quant_offset) + 0.5);
1157     else
1158         priv->fixed_qp_b = priv->fixed_qp_p;
1159
1160     av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
1161            "%d / %d / %d for IDR- / P- / B-frames.\n",
1162            priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1163     return 0;
1164 }
1165
1166 static av_cold int vaapi_encode_h264_init_internal(AVCodecContext *avctx)
1167 {
1168     static const VAConfigAttrib default_config_attributes[] = {
1169         { .type  = VAConfigAttribRTFormat,
1170           .value = VA_RT_FORMAT_YUV420 },
1171         { .type  = VAConfigAttribEncPackedHeaders,
1172           .value = (VA_ENC_PACKED_HEADER_SEQUENCE |
1173                     VA_ENC_PACKED_HEADER_SLICE) },
1174     };
1175
1176     VAAPIEncodeContext      *ctx = avctx->priv_data;
1177     VAAPIEncodeH264Context *priv = ctx->priv_data;
1178     VAAPIEncodeH264Options  *opt = ctx->codec_options;
1179     int i, err;
1180
1181     switch (avctx->profile) {
1182     case FF_PROFILE_H264_CONSTRAINED_BASELINE:
1183         ctx->va_profile = VAProfileH264ConstrainedBaseline;
1184         break;
1185     case FF_PROFILE_H264_BASELINE:
1186         ctx->va_profile = VAProfileH264Baseline;
1187         break;
1188     case FF_PROFILE_H264_MAIN:
1189         ctx->va_profile = VAProfileH264Main;
1190         break;
1191     case FF_PROFILE_H264_EXTENDED:
1192         av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
1193                "is not supported.\n");
1194         return AVERROR_PATCHWELCOME;
1195     case FF_PROFILE_UNKNOWN:
1196     case FF_PROFILE_H264_HIGH:
1197         ctx->va_profile = VAProfileH264High;
1198         break;
1199     case FF_PROFILE_H264_HIGH_10:
1200     case FF_PROFILE_H264_HIGH_10_INTRA:
1201         av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
1202                "are not supported.\n");
1203         return AVERROR_PATCHWELCOME;
1204     case FF_PROFILE_H264_HIGH_422:
1205     case FF_PROFILE_H264_HIGH_422_INTRA:
1206     case FF_PROFILE_H264_HIGH_444:
1207     case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
1208     case FF_PROFILE_H264_HIGH_444_INTRA:
1209     case FF_PROFILE_H264_CAVLC_444:
1210         av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
1211                "are not supported.\n");
1212         return AVERROR_PATCHWELCOME;
1213     default:
1214         av_log(avctx, AV_LOG_ERROR, "Unknown H.264 profile %d.\n",
1215                avctx->profile);
1216         return AVERROR(EINVAL);
1217     }
1218     if (opt->low_power) {
1219 #if VA_CHECK_VERSION(0, 39, 1)
1220         ctx->va_entrypoint = VAEntrypointEncSliceLP;
1221 #else
1222         av_log(avctx, AV_LOG_ERROR, "Low-power encoding is not "
1223                "supported with this VAAPI version.\n");
1224         return AVERROR(EINVAL);
1225 #endif
1226     } else {
1227         ctx->va_entrypoint = VAEntrypointEncSlice;
1228     }
1229
1230     ctx->input_width    = avctx->width;
1231     ctx->input_height   = avctx->height;
1232     ctx->aligned_width  = FFALIGN(ctx->input_width,  16);
1233     ctx->aligned_height = FFALIGN(ctx->input_height, 16);
1234     priv->mb_width      = ctx->aligned_width  / 16;
1235     priv->mb_height     = ctx->aligned_height / 16;
1236
1237     for (i = 0; i < FF_ARRAY_ELEMS(default_config_attributes); i++) {
1238         ctx->config_attributes[ctx->nb_config_attributes++] =
1239             default_config_attributes[i];
1240     }
1241
1242     if (avctx->bit_rate > 0) {
1243         ctx->va_rc_mode = VA_RC_CBR;
1244         err = vaapi_encode_h264_init_constant_bitrate(avctx);
1245     } else {
1246         ctx->va_rc_mode = VA_RC_CQP;
1247         err = vaapi_encode_h264_init_fixed_qp(avctx);
1248     }
1249     if (err < 0)
1250         return err;
1251
1252     ctx->config_attributes[ctx->nb_config_attributes++] = (VAConfigAttrib) {
1253         .type  = VAConfigAttribRateControl,
1254         .value = ctx->va_rc_mode,
1255     };
1256
1257     if (opt->quality > 0) {
1258 #if VA_CHECK_VERSION(0, 36, 0)
1259         priv->quality_params.misc.type =
1260             VAEncMiscParameterTypeQualityLevel;
1261         priv->quality_params.quality.quality_level = opt->quality;
1262
1263         ctx->global_params[ctx->nb_global_params] =
1264             &priv->quality_params.misc;
1265         ctx->global_params_size[ctx->nb_global_params++] =
1266             sizeof(priv->quality_params);
1267 #else
1268         av_log(avctx, AV_LOG_WARNING, "The encode quality option is not "
1269                "supported with this VAAPI version.\n");
1270 #endif
1271     }
1272
1273     ctx->nb_recon_frames = 20;
1274
1275     return 0;
1276 }
1277
1278 static VAAPIEncodeType vaapi_encode_type_h264 = {
1279     .priv_data_size        = sizeof(VAAPIEncodeH264Context),
1280
1281     .init                  = &vaapi_encode_h264_init_internal,
1282
1283     .sequence_params_size  = sizeof(VAEncSequenceParameterBufferH264),
1284     .init_sequence_params  = &vaapi_encode_h264_init_sequence_params,
1285
1286     .picture_params_size   = sizeof(VAEncPictureParameterBufferH264),
1287     .init_picture_params   = &vaapi_encode_h264_init_picture_params,
1288
1289     .slice_params_size     = sizeof(VAEncSliceParameterBufferH264),
1290     .init_slice_params     = &vaapi_encode_h264_init_slice_params,
1291
1292     .sequence_header_type  = VAEncPackedHeaderSequence,
1293     .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
1294
1295     .slice_header_type     = VAEncPackedHeaderH264_Slice,
1296     .write_slice_header    = &vaapi_encode_h264_write_slice_header,
1297
1298     .write_extra_header    = &vaapi_encode_h264_write_extra_header,
1299 };
1300
1301 static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
1302 {
1303     return ff_vaapi_encode_init(avctx, &vaapi_encode_type_h264);
1304 }
1305
1306 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
1307                    offsetof(VAAPIEncodeH264Options, x))
1308 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1309 static const AVOption vaapi_encode_h264_options[] = {
1310     { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1311       OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
1312     { "quality", "Set encode quality (trades off against speed, higher is faster)",
1313       OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8, FLAGS },
1314     { "low_power", "Use low-power encoding mode (experimental: only supported "
1315       "on some platforms, does not support all features)",
1316       OFFSET(low_power), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
1317     { NULL },
1318 };
1319
1320 static const AVCodecDefault vaapi_encode_h264_defaults[] = {
1321     { "profile",        "100" },
1322     { "level",          "51"  },
1323     { "b",              "0"   },
1324     { "bf",             "2"   },
1325     { "g",              "120" },
1326     { "i_qfactor",      "1.0" },
1327     { "i_qoffset",      "0.0" },
1328     { "b_qfactor",      "1.2" },
1329     { "b_qoffset",      "0.0" },
1330     { NULL },
1331 };
1332
1333 static const AVClass vaapi_encode_h264_class = {
1334     .class_name = "h264_vaapi",
1335     .item_name  = av_default_item_name,
1336     .option     = vaapi_encode_h264_options,
1337     .version    = LIBAVUTIL_VERSION_INT,
1338 };
1339
1340 AVCodec ff_h264_vaapi_encoder = {
1341     .name           = "h264_vaapi",
1342     .long_name      = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
1343     .type           = AVMEDIA_TYPE_VIDEO,
1344     .id             = AV_CODEC_ID_H264,
1345     .priv_data_size = (sizeof(VAAPIEncodeContext) +
1346                        sizeof(VAAPIEncodeH264Options)),
1347     .init           = &vaapi_encode_h264_init,
1348     .encode2        = &ff_vaapi_encode2,
1349     .close          = &ff_vaapi_encode_close,
1350     .priv_class     = &vaapi_encode_h264_class,
1351     .capabilities   = AV_CODEC_CAP_DELAY,
1352     .defaults       = vaapi_encode_h264_defaults,
1353     .pix_fmts = (const enum AVPixelFormat[]) {
1354         AV_PIX_FMT_VAAPI,
1355         AV_PIX_FMT_NONE,
1356     },
1357 };