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