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