FFmpeg
vaapi_encode_h264.c
Go to the documentation of this file.
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 <string.h>
20 
21 #include <va/va.h>
22 #include <va/va_enc_h264.h>
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
28 
29 #include "avcodec.h"
30 #include "cbs.h"
31 #include "cbs_h264.h"
32 #include "h264.h"
33 #include "h264_levels.h"
34 #include "h264_sei.h"
35 #include "internal.h"
36 #include "vaapi_encode.h"
37 
38 enum {
39  SEI_TIMING = 0x01,
42 };
43 
44 // Random (version 4) ISO 11578 UUID.
46  0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
47  0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
48 };
49 
50 typedef struct VAAPIEncodeH264Picture {
51  int frame_num;
53 
54  int64_t last_idr_frame;
55  uint16_t idr_pic_id;
56 
59 
60  int cpb_delay;
61  int dpb_delay;
63 
64 typedef struct VAAPIEncodeH264Context {
66 
67  // User options.
68  int qp;
69  int quality;
70  int coder;
71  int aud;
72  int sei;
73  int profile;
74  int level;
75 
76  // Derived settings.
77  int mb_width;
78  int mb_height;
79 
83 
85 
86  // Writer structures.
89 
94 
100 
105 
106 
108  char *data, size_t *data_len,
110 {
111  VAAPIEncodeH264Context *priv = avctx->priv_data;
112  int err;
113 
114  err = ff_cbs_write_fragment_data(priv->cbc, au);
115  if (err < 0) {
116  av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
117  return err;
118  }
119 
120  if (*data_len < 8 * au->data_size - au->data_bit_padding) {
121  av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
122  "%zu < %zu.\n", *data_len,
123  8 * au->data_size - au->data_bit_padding);
124  return AVERROR(ENOSPC);
125  }
126 
127  memcpy(data, au->data, au->data_size);
128  *data_len = 8 * au->data_size - au->data_bit_padding;
129 
130  return 0;
131 }
132 
135  void *nal_unit)
136 {
137  H264RawNALUnitHeader *header = nal_unit;
138  int err;
139 
140  err = ff_cbs_insert_unit_content(au, -1,
141  header->nal_unit_type, nal_unit, NULL);
142  if (err < 0) {
143  av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
144  "type = %d.\n", header->nal_unit_type);
145  return err;
146  }
147 
148  return 0;
149 }
150 
152  char *data, size_t *data_len)
153 {
154  VAAPIEncodeH264Context *priv = avctx->priv_data;
156  int err;
157 
158  if (priv->aud_needed) {
159  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
160  if (err < 0)
161  goto fail;
162  priv->aud_needed = 0;
163  }
164 
165  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_sps);
166  if (err < 0)
167  goto fail;
168 
169  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_pps);
170  if (err < 0)
171  goto fail;
172 
173  err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
174 fail:
176  return err;
177 }
178 
180  VAAPIEncodePicture *pic,
181  VAAPIEncodeSlice *slice,
182  char *data, size_t *data_len)
183 {
184  VAAPIEncodeH264Context *priv = avctx->priv_data;
186  int err;
187 
188  if (priv->aud_needed) {
189  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
190  if (err < 0)
191  goto fail;
192  priv->aud_needed = 0;
193  }
194 
195  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_slice);
196  if (err < 0)
197  goto fail;
198 
199  err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
200 fail:
202  return err;
203 }
204 
206  VAAPIEncodePicture *pic,
207  int index, int *type,
208  char *data, size_t *data_len)
209 {
210  VAAPIEncodeH264Context *priv = avctx->priv_data;
212  int err;
213 
214  if (priv->sei_needed) {
215  if (priv->aud_needed) {
216  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
217  if (err < 0)
218  goto fail;
219  priv->aud_needed = 0;
220  }
221 
222  if (priv->sei_needed & SEI_IDENTIFIER) {
223  err = ff_cbs_sei_add_message(priv->cbc, au, 1,
225  &priv->sei_identifier, NULL);
226  if (err < 0)
227  goto fail;
228  }
229  if (priv->sei_needed & SEI_TIMING) {
230  if (pic->type == PICTURE_TYPE_IDR) {
231  err = ff_cbs_sei_add_message(priv->cbc, au, 1,
233  &priv->sei_buffering_period, NULL);
234  if (err < 0)
235  goto fail;
236  }
237  err = ff_cbs_sei_add_message(priv->cbc, au, 1,
239  &priv->sei_pic_timing, NULL);
240  if (err < 0)
241  goto fail;
242  }
243  if (priv->sei_needed & SEI_RECOVERY_POINT) {
244  err = ff_cbs_sei_add_message(priv->cbc, au, 1,
246  &priv->sei_recovery_point, NULL);
247  if (err < 0)
248  goto fail;
249  }
250 
251  priv->sei_needed = 0;
252 
253  err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
254  if (err < 0)
255  goto fail;
256 
258 
259  *type = VAEncPackedHeaderRawData;
260  return 0;
261 
262 #if !CONFIG_VAAPI_1
263  } else if (priv->sei_cbr_workaround_needed) {
264  // Insert a zero-length header using the old SEI type. This is
265  // required to avoid triggering broken behaviour on Intel platforms
266  // in CBR mode where an invalid SEI message is generated by the
267  // driver and inserted into the stream.
268  *data_len = 0;
269  *type = VAEncPackedHeaderH264_SEI;
270  priv->sei_cbr_workaround_needed = 0;
271  return 0;
272 #endif
273 
274  } else {
275  return AVERROR_EOF;
276  }
277 
278 fail:
280  return err;
281 }
282 
284 {
285  VAAPIEncodeContext *ctx = avctx->priv_data;
286  VAAPIEncodeH264Context *priv = avctx->priv_data;
287  H264RawSPS *sps = &priv->raw_sps;
288  H264RawPPS *pps = &priv->raw_pps;
289  VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
290  VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
291 
292  memset(sps, 0, sizeof(*sps));
293  memset(pps, 0, sizeof(*pps));
294 
295  sps->nal_unit_header.nal_ref_idc = 3;
297 
298  sps->profile_idc = avctx->profile & 0xff;
299 
301  avctx->profile == FF_PROFILE_H264_MAIN)
302  sps->constraint_set1_flag = 1;
303 
304  if (avctx->profile == FF_PROFILE_H264_HIGH)
305  sps->constraint_set3_flag = ctx->gop_size == 1;
306 
307  if (avctx->profile == FF_PROFILE_H264_MAIN ||
308  avctx->profile == FF_PROFILE_H264_HIGH) {
309  sps->constraint_set4_flag = 1;
310  sps->constraint_set5_flag = ctx->b_per_p == 0;
311  }
312 
313  if (ctx->gop_size == 1)
314  priv->dpb_frames = 0;
315  else
316  priv->dpb_frames = 1 + ctx->max_b_depth;
317 
318  if (avctx->level != FF_LEVEL_UNKNOWN) {
319  sps->level_idc = avctx->level;
320  } else {
321  const H264LevelDescriptor *level;
322  int framerate;
323 
324  if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
325  framerate = avctx->framerate.num / avctx->framerate.den;
326  else
327  framerate = 0;
328 
329  level = ff_h264_guess_level(sps->profile_idc,
330  avctx->bit_rate,
331  framerate,
332  priv->mb_width * 16,
333  priv->mb_height * 16,
334  priv->dpb_frames);
335  if (level) {
336  av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
337  if (level->constraint_set3_flag)
338  sps->constraint_set3_flag = 1;
339  sps->level_idc = level->level_idc;
340  } else {
341  av_log(avctx, AV_LOG_WARNING, "Stream will not conform "
342  "to any level: using level 6.2.\n");
343  sps->level_idc = 62;
344  }
345  }
346 
347  sps->seq_parameter_set_id = 0;
348  sps->chroma_format_idc = 1;
349 
350  sps->log2_max_frame_num_minus4 = 4;
351  sps->pic_order_cnt_type = 0;
353 
354  sps->max_num_ref_frames = priv->dpb_frames;
355 
356  sps->pic_width_in_mbs_minus1 = priv->mb_width - 1;
357  sps->pic_height_in_map_units_minus1 = priv->mb_height - 1;
358 
359  sps->frame_mbs_only_flag = 1;
360  sps->direct_8x8_inference_flag = 1;
361 
362  if (avctx->width != 16 * priv->mb_width ||
363  avctx->height != 16 * priv->mb_height) {
364  sps->frame_cropping_flag = 1;
365 
366  sps->frame_crop_left_offset = 0;
368  (16 * priv->mb_width - avctx->width) / 2;
369  sps->frame_crop_top_offset = 0;
371  (16 * priv->mb_height - avctx->height) / 2;
372  } else {
373  sps->frame_cropping_flag = 0;
374  }
375 
377 
378  if (avctx->sample_aspect_ratio.num != 0 &&
379  avctx->sample_aspect_ratio.den != 0) {
380  static const AVRational sar_idc[] = {
381  { 0, 0 },
382  { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
383  { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
384  { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
385  { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
386  };
387  int num, den, i;
388  av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
389  avctx->sample_aspect_ratio.den, 65535);
390  for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
391  if (num == sar_idc[i].num &&
392  den == sar_idc[i].den) {
393  sps->vui.aspect_ratio_idc = i;
394  break;
395  }
396  }
397  if (i >= FF_ARRAY_ELEMS(sar_idc)) {
398  sps->vui.aspect_ratio_idc = 255;
399  sps->vui.sar_width = num;
400  sps->vui.sar_height = den;
401  }
403  }
404 
405  // Unspecified video format, from table E-2.
406  sps->vui.video_format = 5;
408  avctx->color_range == AVCOL_RANGE_JPEG;
409  sps->vui.colour_primaries = avctx->color_primaries;
410  sps->vui.transfer_characteristics = avctx->color_trc;
411  sps->vui.matrix_coefficients = avctx->colorspace;
412  if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
413  avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
416  if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
419 
424  avctx->chroma_sample_location - 1;
425  }
426 
427  sps->vui.timing_info_present_flag = 1;
428  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
429  sps->vui.num_units_in_tick = avctx->framerate.den;
430  sps->vui.time_scale = 2 * avctx->framerate.num;
431  sps->vui.fixed_frame_rate_flag = 1;
432  } else {
433  sps->vui.num_units_in_tick = avctx->time_base.num;
434  sps->vui.time_scale = 2 * avctx->time_base.den;
435  sps->vui.fixed_frame_rate_flag = 0;
436  }
437 
438  if (priv->sei & SEI_TIMING) {
439  H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
441 
443 
444  hrd->cpb_cnt_minus1 = 0;
445 
446  // Try to scale these to a sensible range so that the
447  // golomb encode of the value is not overlong.
448  hrd->bit_rate_scale =
449  av_clip_uintp2(av_log2(ctx->va_bit_rate) - 15 - 6, 4);
450  hrd->bit_rate_value_minus1[0] =
451  (ctx->va_bit_rate >> hrd->bit_rate_scale + 6) - 1;
452 
453  hrd->cpb_size_scale =
454  av_clip_uintp2(av_log2(ctx->hrd_params.buffer_size) - 15 - 4, 4);
455  hrd->cpb_size_value_minus1[0] =
456  (ctx->hrd_params.buffer_size >> hrd->cpb_size_scale + 4) - 1;
457 
458  // CBR mode as defined for the HRD cannot be achieved without filler
459  // data, so this flag cannot be set even with VAAPI CBR modes.
460  hrd->cbr_flag[0] = 0;
461 
465  hrd->time_offset_length = 0;
466 
468 
469  // This calculation can easily overflow 32 bits.
470  bp->nal.initial_cpb_removal_delay[0] = 90000 *
471  (uint64_t)ctx->hrd_params.initial_buffer_fullness /
472  ctx->hrd_params.buffer_size;
474  } else {
477  }
478 
484  sps->vui.max_dec_frame_buffering = ctx->max_b_depth + 1;
485 
486  pps->nal_unit_header.nal_ref_idc = 3;
488 
489  pps->pic_parameter_set_id = 0;
490  pps->seq_parameter_set_id = 0;
491 
496  if (!priv->coder && pps->entropy_coding_mode_flag)
497  pps->entropy_coding_mode_flag = 0;
498 
501 
502  pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
503 
507  pps->more_rbsp_data = 0;
508  } else {
509  pps->more_rbsp_data = 1;
510 
511  pps->transform_8x8_mode_flag = 1;
512  }
513 
514  *vseq = (VAEncSequenceParameterBufferH264) {
515  .seq_parameter_set_id = sps->seq_parameter_set_id,
516  .level_idc = sps->level_idc,
517  .intra_period = ctx->gop_size,
518  .intra_idr_period = ctx->gop_size,
519  .ip_period = ctx->b_per_p + 1,
520 
521  .bits_per_second = ctx->va_bit_rate,
522  .max_num_ref_frames = sps->max_num_ref_frames,
523  .picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1,
524  .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
525 
526  .seq_fields.bits = {
527  .chroma_format_idc = sps->chroma_format_idc,
528  .frame_mbs_only_flag = sps->frame_mbs_only_flag,
529  .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
530  .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
531  .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
532  .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
533  .pic_order_cnt_type = sps->pic_order_cnt_type,
534  .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
535  .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
536  },
537 
538  .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
539  .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
540 
541  .frame_cropping_flag = sps->frame_cropping_flag,
542  .frame_crop_left_offset = sps->frame_crop_left_offset,
543  .frame_crop_right_offset = sps->frame_crop_right_offset,
544  .frame_crop_top_offset = sps->frame_crop_top_offset,
545  .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
546 
547  .vui_parameters_present_flag = sps->vui_parameters_present_flag,
548 
549  .vui_fields.bits = {
550  .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
551  .timing_info_present_flag = sps->vui.timing_info_present_flag,
552  .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
553  .log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal,
554  .log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical,
555  },
556 
557  .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
558  .sar_width = sps->vui.sar_width,
559  .sar_height = sps->vui.sar_height,
560  .num_units_in_tick = sps->vui.num_units_in_tick,
561  .time_scale = sps->vui.time_scale,
562  };
563 
564  *vpic = (VAEncPictureParameterBufferH264) {
565  .CurrPic = {
566  .picture_id = VA_INVALID_ID,
567  .flags = VA_PICTURE_H264_INVALID,
568  },
569 
570  .coded_buf = VA_INVALID_ID,
571 
572  .pic_parameter_set_id = pps->pic_parameter_set_id,
573  .seq_parameter_set_id = pps->seq_parameter_set_id,
574 
575  .pic_init_qp = pps->pic_init_qp_minus26 + 26,
576  .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
577  .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
578 
579  .chroma_qp_index_offset = pps->chroma_qp_index_offset,
580  .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
581 
582  .pic_fields.bits = {
583  .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
584  .weighted_pred_flag = pps->weighted_pred_flag,
585  .weighted_bipred_idc = pps->weighted_bipred_idc,
586  .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
587  .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
588  .deblocking_filter_control_present_flag =
590  .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
591  .pic_order_present_flag =
593  .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
594  },
595  };
596 
597  return 0;
598 }
599 
601  VAAPIEncodePicture *pic)
602 {
603  VAAPIEncodeContext *ctx = avctx->priv_data;
604  VAAPIEncodeH264Context *priv = avctx->priv_data;
605  VAAPIEncodeH264Picture *hpic = pic->priv_data;
606  VAAPIEncodePicture *prev = pic->prev;
607  VAAPIEncodeH264Picture *hprev = prev ? prev->priv_data : NULL;
608  VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
609  int i;
610 
611  if (pic->type == PICTURE_TYPE_IDR) {
612  av_assert0(pic->display_order == pic->encode_order);
613 
614  hpic->frame_num = 0;
615  hpic->last_idr_frame = pic->display_order;
616  hpic->idr_pic_id = hprev ? hprev->idr_pic_id + 1 : 0;
617 
618  hpic->primary_pic_type = 0;
619  hpic->slice_type = 7;
620  } else {
621  av_assert0(prev);
622 
623  hpic->frame_num = hprev->frame_num + prev->is_reference;
624 
625  hpic->last_idr_frame = hprev->last_idr_frame;
626  hpic->idr_pic_id = hprev->idr_pic_id;
627 
628  if (pic->type == PICTURE_TYPE_I) {
629  hpic->slice_type = 7;
630  hpic->primary_pic_type = 0;
631  } else if (pic->type == PICTURE_TYPE_P) {
632  hpic->slice_type = 5;
633  hpic->primary_pic_type = 1;
634  } else {
635  hpic->slice_type = 6;
636  hpic->primary_pic_type = 2;
637  }
638  }
639  hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame;
640  hpic->dpb_delay = pic->display_order - pic->encode_order + ctx->max_b_depth;
641  hpic->cpb_delay = pic->encode_order - hpic->last_idr_frame;
642 
643  if (priv->aud) {
644  priv->aud_needed = 1;
645  priv->raw_aud = (H264RawAUD) {
646  .nal_unit_header = {
648  },
649  .primary_pic_type = hpic->primary_pic_type,
650  };
651  } else {
652  priv->aud_needed = 0;
653  }
654 
655  priv->sei_needed = 0;
656 
657  if (priv->sei & SEI_IDENTIFIER && pic->encode_order == 0)
658  priv->sei_needed |= SEI_IDENTIFIER;
659 #if !CONFIG_VAAPI_1
660  if (ctx->va_rc_mode == VA_RC_CBR)
661  priv->sei_cbr_workaround_needed = 1;
662 #endif
663 
664  if (priv->sei & SEI_TIMING) {
666  .cpb_removal_delay = 2 * hpic->cpb_delay,
667  .dpb_output_delay = 2 * hpic->dpb_delay,
668  };
669 
670  priv->sei_needed |= SEI_TIMING;
671  }
672 
673  if (priv->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
675  .recovery_frame_cnt = 0,
676  .exact_match_flag = 1,
677  .broken_link_flag = ctx->b_per_p > 0,
678  };
679 
681  }
682 
683  vpic->CurrPic = (VAPictureH264) {
684  .picture_id = pic->recon_surface,
685  .frame_idx = hpic->frame_num,
686  .flags = 0,
687  .TopFieldOrderCnt = hpic->pic_order_cnt,
688  .BottomFieldOrderCnt = hpic->pic_order_cnt,
689  };
690 
691  for (i = 0; i < pic->nb_refs; i++) {
692  VAAPIEncodePicture *ref = pic->refs[i];
694 
695  av_assert0(ref && ref->encode_order < pic->encode_order);
696  href = ref->priv_data;
697 
698  vpic->ReferenceFrames[i] = (VAPictureH264) {
699  .picture_id = ref->recon_surface,
700  .frame_idx = href->frame_num,
701  .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
702  .TopFieldOrderCnt = href->pic_order_cnt,
703  .BottomFieldOrderCnt = href->pic_order_cnt,
704  };
705  }
706  for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
707  vpic->ReferenceFrames[i] = (VAPictureH264) {
708  .picture_id = VA_INVALID_ID,
709  .flags = VA_PICTURE_H264_INVALID,
710  };
711  }
712 
713  vpic->coded_buf = pic->output_buffer;
714 
715  vpic->frame_num = hpic->frame_num;
716 
717  vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
718  vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
719 
720  return 0;
721 }
722 
724  VAAPIEncodePicture *pic,
725  VAAPIEncodePicture **rpl0,
726  VAAPIEncodePicture **rpl1,
727  int *rpl_size)
728 {
729  VAAPIEncodePicture *prev;
730  VAAPIEncodeH264Picture *hp, *hn, *hc;
731  int i, j, n = 0;
732 
733  prev = pic->prev;
734  av_assert0(prev);
735  hp = pic->priv_data;
736 
737  for (i = 0; i < pic->prev->nb_dpb_pics; i++) {
738  hn = prev->dpb[i]->priv_data;
739  av_assert0(hn->frame_num < hp->frame_num);
740 
741  if (pic->type == PICTURE_TYPE_P) {
742  for (j = n; j > 0; j--) {
743  hc = rpl0[j - 1]->priv_data;
744  av_assert0(hc->frame_num != hn->frame_num);
745  if (hc->frame_num > hn->frame_num)
746  break;
747  rpl0[j] = rpl0[j - 1];
748  }
749  rpl0[j] = prev->dpb[i];
750 
751  } else if (pic->type == PICTURE_TYPE_B) {
752  for (j = n; j > 0; j--) {
753  hc = rpl0[j - 1]->priv_data;
755  if (hc->pic_order_cnt < hp->pic_order_cnt) {
756  if (hn->pic_order_cnt > hp->pic_order_cnt ||
757  hn->pic_order_cnt < hc->pic_order_cnt)
758  break;
759  } else {
760  if (hn->pic_order_cnt > hc->pic_order_cnt)
761  break;
762  }
763  rpl0[j] = rpl0[j - 1];
764  }
765  rpl0[j] = prev->dpb[i];
766 
767  for (j = n; j > 0; j--) {
768  hc = rpl1[j - 1]->priv_data;
770  if (hc->pic_order_cnt > hp->pic_order_cnt) {
771  if (hn->pic_order_cnt < hp->pic_order_cnt ||
772  hn->pic_order_cnt > hc->pic_order_cnt)
773  break;
774  } else {
775  if (hn->pic_order_cnt < hc->pic_order_cnt)
776  break;
777  }
778  rpl1[j] = rpl1[j - 1];
779  }
780  rpl1[j] = prev->dpb[i];
781  }
782 
783  ++n;
784  }
785 
786  if (pic->type == PICTURE_TYPE_B) {
787  for (i = 0; i < n; i++) {
788  if (rpl0[i] != rpl1[i])
789  break;
790  }
791  if (i == n)
792  FFSWAP(VAAPIEncodePicture*, rpl1[0], rpl1[1]);
793  }
794 
795  if (pic->type == PICTURE_TYPE_P ||
796  pic->type == PICTURE_TYPE_B) {
797  av_log(avctx, AV_LOG_DEBUG, "Default RefPicList0 for fn=%d/poc=%d:",
798  hp->frame_num, hp->pic_order_cnt);
799  for (i = 0; i < n; i++) {
800  hn = rpl0[i]->priv_data;
801  av_log(avctx, AV_LOG_DEBUG, " fn=%d/poc=%d",
802  hn->frame_num, hn->pic_order_cnt);
803  }
804  av_log(avctx, AV_LOG_DEBUG, "\n");
805  }
806  if (pic->type == PICTURE_TYPE_B) {
807  av_log(avctx, AV_LOG_DEBUG, "Default RefPicList1 for fn=%d/poc=%d:",
808  hp->frame_num, hp->pic_order_cnt);
809  for (i = 0; i < n; i++) {
810  hn = rpl1[i]->priv_data;
811  av_log(avctx, AV_LOG_DEBUG, " fn=%d/poc=%d",
812  hn->frame_num, hn->pic_order_cnt);
813  }
814  av_log(avctx, AV_LOG_DEBUG, "\n");
815  }
816 
817  *rpl_size = n;
818 }
819 
821  VAAPIEncodePicture *pic,
822  VAAPIEncodeSlice *slice)
823 {
824  VAAPIEncodeH264Context *priv = avctx->priv_data;
825  VAAPIEncodeH264Picture *hpic = pic->priv_data;
826  VAAPIEncodePicture *prev = pic->prev;
827  H264RawSPS *sps = &priv->raw_sps;
828  H264RawPPS *pps = &priv->raw_pps;
829  H264RawSliceHeader *sh = &priv->raw_slice.header;
830  VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
831  VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
832  int i, j;
833 
834  if (pic->type == PICTURE_TYPE_IDR) {
837  } else {
840  }
841 
842  sh->first_mb_in_slice = slice->block_start;
843  sh->slice_type = hpic->slice_type;
844 
846 
847  sh->frame_num = hpic->frame_num &
848  ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
849  sh->idr_pic_id = hpic->idr_pic_id;
850  sh->pic_order_cnt_lsb = hpic->pic_order_cnt &
851  ((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
852 
854 
855  if (pic->type == PICTURE_TYPE_B)
856  sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26);
857  else if (pic->type == PICTURE_TYPE_P)
858  sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26);
859  else
860  sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26);
861 
862  if (pic->is_reference && pic->type != PICTURE_TYPE_IDR) {
863  VAAPIEncodePicture *discard_list[MAX_DPB_SIZE];
864  int discard = 0, keep = 0;
865 
866  // Discard everything which is in the DPB of the previous frame but
867  // not in the DPB of this one.
868  for (i = 0; i < prev->nb_dpb_pics; i++) {
869  for (j = 0; j < pic->nb_dpb_pics; j++) {
870  if (prev->dpb[i] == pic->dpb[j])
871  break;
872  }
873  if (j == pic->nb_dpb_pics) {
874  discard_list[discard] = prev->dpb[i];
875  ++discard;
876  } else {
877  ++keep;
878  }
879  }
880  av_assert0(keep <= priv->dpb_frames);
881 
882  if (discard == 0) {
884  } else {
886  for (i = 0; i < discard; i++) {
887  VAAPIEncodeH264Picture *old = discard_list[i]->priv_data;
888  av_assert0(old->frame_num < hpic->frame_num);
891  hpic->frame_num - old->frame_num - 1;
892  }
894  }
895  }
896 
897  // If the intended references are not the first entries of RefPicListN
898  // by default, use ref-pic-list-modification to move them there.
899  if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) {
900  VAAPIEncodePicture *def_l0[MAX_DPB_SIZE], *def_l1[MAX_DPB_SIZE];
902  int n;
903 
905  def_l0, def_l1, &n);
906 
907  if (pic->type == PICTURE_TYPE_P) {
908  int need_rplm = 0;
909  for (i = 0; i < pic->nb_refs; i++) {
910  av_assert0(pic->refs[i]);
911  if (pic->refs[i] != def_l0[i])
912  need_rplm = 1;
913  }
914 
915  sh->ref_pic_list_modification_flag_l0 = need_rplm;
916  if (need_rplm) {
917  int pic_num = hpic->frame_num;
918  for (i = 0; i < pic->nb_refs; i++) {
919  href = pic->refs[i]->priv_data;
920  av_assert0(href->frame_num != pic_num);
921  if (href->frame_num < pic_num) {
924  pic_num - href->frame_num - 1;
925  } else {
928  href->frame_num - pic_num - 1;
929  }
930  pic_num = href->frame_num;
931  }
933  }
934 
935  } else {
936  int need_rplm_l0 = 0, need_rplm_l1 = 0;
937  int n0 = 0, n1 = 0;
938  for (i = 0; i < pic->nb_refs; i++) {
939  av_assert0(pic->refs[i]);
940  href = pic->refs[i]->priv_data;
941  av_assert0(href->pic_order_cnt != hpic->pic_order_cnt);
942  if (href->pic_order_cnt < hpic->pic_order_cnt) {
943  if (pic->refs[i] != def_l0[n0])
944  need_rplm_l0 = 1;
945  ++n0;
946  } else {
947  if (pic->refs[i] != def_l1[n1])
948  need_rplm_l1 = 1;
949  ++n1;
950  }
951  }
952 
953  sh->ref_pic_list_modification_flag_l0 = need_rplm_l0;
954  if (need_rplm_l0) {
955  int pic_num = hpic->frame_num;
956  for (i = j = 0; i < pic->nb_refs; i++) {
957  href = pic->refs[i]->priv_data;
958  if (href->pic_order_cnt > hpic->pic_order_cnt)
959  continue;
960  av_assert0(href->frame_num != pic_num);
961  if (href->frame_num < pic_num) {
964  pic_num - href->frame_num - 1;
965  } else {
968  href->frame_num - pic_num - 1;
969  }
970  pic_num = href->frame_num;
971  ++j;
972  }
973  av_assert0(j == n0);
975  }
976 
977  sh->ref_pic_list_modification_flag_l1 = need_rplm_l1;
978  if (need_rplm_l1) {
979  int pic_num = hpic->frame_num;
980  for (i = j = 0; i < pic->nb_refs; i++) {
981  href = pic->refs[i]->priv_data;
982  if (href->pic_order_cnt < hpic->pic_order_cnt)
983  continue;
984  av_assert0(href->frame_num != pic_num);
985  if (href->frame_num < pic_num) {
988  pic_num - href->frame_num - 1;
989  } else {
992  href->frame_num - pic_num - 1;
993  }
994  pic_num = href->frame_num;
995  ++j;
996  }
997  av_assert0(j == n1);
999  }
1000  }
1001  }
1002 
1003  vslice->macroblock_address = slice->block_start;
1004  vslice->num_macroblocks = slice->block_size;
1005 
1006  vslice->macroblock_info = VA_INVALID_ID;
1007 
1008  vslice->slice_type = sh->slice_type % 5;
1009  vslice->pic_parameter_set_id = sh->pic_parameter_set_id;
1010  vslice->idr_pic_id = sh->idr_pic_id;
1011 
1012  vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb;
1013 
1014  vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag;
1015 
1016  for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
1017  vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
1018  vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1019  vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
1020  vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
1021  }
1022 
1023  av_assert0(pic->nb_refs <= 2);
1024  if (pic->nb_refs >= 1) {
1025  // Backward reference for P- or B-frame.
1026  av_assert0(pic->type == PICTURE_TYPE_P ||
1027  pic->type == PICTURE_TYPE_B);
1028  vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
1029  }
1030  if (pic->nb_refs >= 2) {
1031  // Forward reference for B-frame.
1032  av_assert0(pic->type == PICTURE_TYPE_B);
1033  vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
1034  }
1035 
1036  vslice->slice_qp_delta = sh->slice_qp_delta;
1037 
1038  return 0;
1039 }
1040 
1042 {
1043  VAAPIEncodeContext *ctx = avctx->priv_data;
1044  VAAPIEncodeH264Context *priv = avctx->priv_data;
1045  int err;
1046 
1047  err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
1048  if (err < 0)
1049  return err;
1050 
1051  priv->mb_width = FFALIGN(avctx->width, 16) / 16;
1052  priv->mb_height = FFALIGN(avctx->height, 16) / 16;
1053 
1054  if (ctx->va_rc_mode == VA_RC_CQP) {
1055  priv->fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
1056  if (avctx->i_quant_factor > 0.0)
1057  priv->fixed_qp_idr =
1058  av_clip((avctx->i_quant_factor * priv->fixed_qp_p +
1059  avctx->i_quant_offset) + 0.5, 1, 51);
1060  else
1061  priv->fixed_qp_idr = priv->fixed_qp_p;
1062  if (avctx->b_quant_factor > 0.0)
1063  priv->fixed_qp_b =
1064  av_clip((avctx->b_quant_factor * priv->fixed_qp_p +
1065  avctx->b_quant_offset) + 0.5, 1, 51);
1066  else
1067  priv->fixed_qp_b = priv->fixed_qp_p;
1068 
1069  av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
1070  "%d / %d / %d for IDR- / P- / B-frames.\n",
1071  priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1072 
1073  } else {
1074  // These still need to be set for pic_init_qp/slice_qp_delta.
1075  priv->fixed_qp_idr = 26;
1076  priv->fixed_qp_p = 26;
1077  priv->fixed_qp_b = 26;
1078  }
1079 
1080  if (!ctx->rc_mode->hrd) {
1081  // Timing SEI requires a mode respecting HRD parameters.
1082  priv->sei &= ~SEI_TIMING;
1083  }
1084 
1085  if (priv->sei & SEI_IDENTIFIER) {
1086  const char *lavc = LIBAVCODEC_IDENT;
1087  const char *vaapi = VA_VERSION_S;
1088  const char *driver;
1089  int len;
1090 
1091  memcpy(priv->sei_identifier.uuid_iso_iec_11578,
1093  sizeof(priv->sei_identifier.uuid_iso_iec_11578));
1094 
1095  driver = vaQueryVendorString(ctx->hwctx->display);
1096  if (!driver)
1097  driver = "unknown driver";
1098 
1099  len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver);
1100  if (len >= 0) {
1101  priv->sei_identifier_string = av_malloc(len + 1);
1102  if (!priv->sei_identifier_string)
1103  return AVERROR(ENOMEM);
1104 
1105  snprintf(priv->sei_identifier_string, len + 1,
1106  "%s / VAAPI %s / %s", lavc, vaapi, driver);
1107 
1109  priv->sei_identifier.data_length = len + 1;
1110  }
1111  }
1112 
1113  ctx->roi_quant_range = 51 + 6 * (ctx->profile->depth - 8);
1114 
1115  return 0;
1116 }
1117 
1119  { FF_PROFILE_H264_HIGH, 8, 3, 1, 1, VAProfileH264High },
1120  { FF_PROFILE_H264_MAIN, 8, 3, 1, 1, VAProfileH264Main },
1122  8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
1123  { FF_PROFILE_UNKNOWN }
1124 };
1125 
1128 
1129  .flags = FLAG_SLICE_CONTROL |
1130  FLAG_B_PICTURES |
1133 
1134  .default_quality = 20,
1135 
1136  .configure = &vaapi_encode_h264_configure,
1137 
1138  .picture_priv_data_size = sizeof(VAAPIEncodeH264Picture),
1139 
1140  .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
1141  .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
1142 
1143  .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
1144  .init_picture_params = &vaapi_encode_h264_init_picture_params,
1145 
1146  .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
1147  .init_slice_params = &vaapi_encode_h264_init_slice_params,
1148 
1149  .sequence_header_type = VAEncPackedHeaderSequence,
1150  .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
1151 
1152  .slice_header_type = VAEncPackedHeaderH264_Slice,
1153  .write_slice_header = &vaapi_encode_h264_write_slice_header,
1154 
1155  .write_extra_header = &vaapi_encode_h264_write_extra_header,
1156 };
1157 
1159 {
1160  VAAPIEncodeContext *ctx = avctx->priv_data;
1161  VAAPIEncodeH264Context *priv = avctx->priv_data;
1162 
1163  ctx->codec = &vaapi_encode_type_h264;
1164 
1165  if (avctx->profile == FF_PROFILE_UNKNOWN)
1166  avctx->profile = priv->profile;
1167  if (avctx->level == FF_LEVEL_UNKNOWN)
1168  avctx->level = priv->level;
1170  avctx->compression_level = priv->quality;
1171 
1172  // Reject unsupported profiles.
1173  switch (avctx->profile) {
1175  av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
1176  "supported, using constrained baseline profile instead.\n");
1178  break;
1180  av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
1181  "is not supported.\n");
1182  return AVERROR_PATCHWELCOME;
1185  av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
1186  "are not supported.\n");
1187  return AVERROR_PATCHWELCOME;
1194  av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
1195  "are not supported.\n");
1196  return AVERROR_PATCHWELCOME;
1197  }
1198 
1199  if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
1200  av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
1201  "in 8-bit unsigned integer.\n", avctx->level);
1202  return AVERROR(EINVAL);
1203  }
1204 
1205  ctx->desired_packed_headers =
1206  VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
1207  VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
1208  VA_ENC_PACKED_HEADER_MISC; // SEI.
1209 
1210  ctx->surface_width = FFALIGN(avctx->width, 16);
1211  ctx->surface_height = FFALIGN(avctx->height, 16);
1212 
1213  ctx->slice_block_height = ctx->slice_block_width = 16;
1214 
1215  if (priv->qp > 0)
1216  ctx->explicit_qp = priv->qp;
1217 
1218  return ff_vaapi_encode_init(avctx);
1219 }
1220 
1222 {
1223  VAAPIEncodeH264Context *priv = avctx->priv_data;
1224 
1226  ff_cbs_close(&priv->cbc);
1228 
1229  return ff_vaapi_encode_close(avctx);
1230 }
1231 
1232 #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
1233 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1237 
1238  { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1239  OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
1240  { "quality", "Set encode quality (trades off against speed, higher is faster)",
1241  OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
1242  { "coder", "Entropy coder type",
1243  OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
1244  { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1245  { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1246  { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1247  { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1248 
1249  { "aud", "Include AUD",
1250  OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1251 
1252  { "sei", "Set SEI to include",
1255  0, INT_MAX, FLAGS, "sei" },
1256  { "identifier", "Include encoder version identifier",
1257  0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
1258  INT_MIN, INT_MAX, FLAGS, "sei" },
1259  { "timing", "Include timing parameters (buffering_period and pic_timing)",
1260  0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
1261  INT_MIN, INT_MAX, FLAGS, "sei" },
1262  { "recovery_point", "Include recovery points where appropriate",
1263  0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
1264  INT_MIN, INT_MAX, FLAGS, "sei" },
1265 
1266  { "profile", "Set profile (profile_idc and constraint_set*_flag)",
1268  { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xffff, FLAGS, "profile" },
1269 
1270 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1271  { .i64 = value }, 0, 0, FLAGS, "profile"
1272  { PROFILE("constrained_baseline", FF_PROFILE_H264_CONSTRAINED_BASELINE) },
1273  { PROFILE("main", FF_PROFILE_H264_MAIN) },
1274  { PROFILE("high", FF_PROFILE_H264_HIGH) },
1275 #undef PROFILE
1276 
1277  { "level", "Set level (level_idc)",
1279  { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
1280 
1281 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1282  { .i64 = value }, 0, 0, FLAGS, "level"
1283  { LEVEL("1", 10) },
1284  { LEVEL("1.1", 11) },
1285  { LEVEL("1.2", 12) },
1286  { LEVEL("1.3", 13) },
1287  { LEVEL("2", 20) },
1288  { LEVEL("2.1", 21) },
1289  { LEVEL("2.2", 22) },
1290  { LEVEL("3", 30) },
1291  { LEVEL("3.1", 31) },
1292  { LEVEL("3.2", 32) },
1293  { LEVEL("4", 40) },
1294  { LEVEL("4.1", 41) },
1295  { LEVEL("4.2", 42) },
1296  { LEVEL("5", 50) },
1297  { LEVEL("5.1", 51) },
1298  { LEVEL("5.2", 52) },
1299  { LEVEL("6", 60) },
1300  { LEVEL("6.1", 61) },
1301  { LEVEL("6.2", 62) },
1302 #undef LEVEL
1303 
1304  { NULL },
1305 };
1306 
1308  { "b", "0" },
1309  { "bf", "2" },
1310  { "g", "120" },
1311  { "i_qfactor", "1" },
1312  { "i_qoffset", "0" },
1313  { "b_qfactor", "6/5" },
1314  { "b_qoffset", "0" },
1315  { "qmin", "-1" },
1316  { "qmax", "-1" },
1317  { NULL },
1318 };
1319 
1321  .class_name = "h264_vaapi",
1322  .item_name = av_default_item_name,
1323  .option = vaapi_encode_h264_options,
1324  .version = LIBAVUTIL_VERSION_INT,
1325 };
1326 
1328  .name = "h264_vaapi",
1329  .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
1330  .type = AVMEDIA_TYPE_VIDEO,
1331  .id = AV_CODEC_ID_H264,
1332  .priv_data_size = sizeof(VAAPIEncodeH264Context),
1334  .receive_packet = &ff_vaapi_encode_receive_packet,
1335  .close = &vaapi_encode_h264_close,
1336  .priv_class = &vaapi_encode_h264_class,
1337  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
1339  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1340  .defaults = vaapi_encode_h264_defaults,
1341  .pix_fmts = (const enum AVPixelFormat[]) {
1344  },
1345  .hw_configs = ff_vaapi_encode_hw_configs,
1346  .wrapper_name = "vaapi",
1347 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
#define FF_PROFILE_H264_MAIN
Definition: avcodec.h:1899
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:172
uint8_t frame_cropping_flag
Definition: cbs_h264.h:147
uint8_t deblocking_filter_control_present_flag
Definition: cbs_h264.h:202
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:2071
static const VAAPIEncodeType vaapi_encode_type_h264
#define FF_PROFILE_H264_CAVLC_444
Definition: avcodec.h:1911
uint8_t bit_depth_chroma_minus8
Definition: cbs_h264.h:120
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:609
uint8_t aspect_ratio_idc
Definition: cbs_h264.h:60
#define av_clip_uintp2
Definition: common.h:146
static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
uint8_t video_format
Definition: cbs_h264.h:68
AVOption.
Definition: opt.h:248
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodePicture **rpl0, VAAPIEncodePicture **rpl1, int *rpl_size)
uint8_t bit_rate_scale
Definition: cbs_h264.h:45
uint16_t recovery_frame_cnt
Definition: cbs_h264.h:269
uint8_t dpb_output_delay_length_minus1
Definition: cbs_h264.h:54
uint8_t initial_cpb_removal_delay_length_minus1
Definition: cbs_h264.h:52
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
int64_t bit_rate
the average bitrate
Definition: avcodec.h:586
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
uint8_t chroma_sample_loc_type_bottom_field
Definition: cbs_h264.h:77
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
uint8_t constraint_set3_flag
Definition: h264_levels.h:28
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition: codec.h:157
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:75
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1171
int num
Numerator.
Definition: rational.h:59
uint8_t log2_max_frame_num_minus4
Definition: cbs_h264.h:128
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
int av_log2(unsigned v)
Definition: intmath.c:26
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:915
uint16_t pic_order_cnt_lsb
Definition: cbs_h264.h:305
uint8_t time_offset_length
Definition: cbs_h264.h:55
GLint GLenum type
Definition: opengl_enc.c:104
uint8_t low_delay_hrd_flag
Definition: cbs_h264.h:88
#define FF_ARRAY_ELEMS(a)
void * codec_sequence_params
Definition: vaapi_encode.h:274
#define FLAGS
#define OFFSET(x)
uint8_t max_dec_frame_buffering
Definition: cbs_h264.h:99
fg index
int profile
profile
Definition: avcodec.h:1858
uint8_t fixed_frame_rate_flag
Definition: cbs_h264.h:82
H264RawVUI vui
Definition: cbs_h264.h:154
AVCodec.
Definition: codec.h:197
int framerate
Definition: h264_levels.c:65
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:848
uint32_t first_mb_in_slice
Definition: cbs_h264.h:292
int ff_cbs_sei_add_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, int prefix, uint32_t payload_type, void *payload_data, AVBufferRef *payload_buf)
Add an SEI message to an access unit.
Definition: cbs_sei.c:247
static const AVClass vaapi_encode_h264_class
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: avcodec.h:1909
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:659
uint8_t uuid_iso_iec_11578[16]
Definition: cbs_sei.h:44
uint8_t weighted_pred_flag
Definition: cbs_h264.h:195
uint8_t constraint_set4_flag
Definition: cbs_h264.h:110
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:1985
H264RawHRD nal_hrd_parameters
Definition: cbs_h264.h:85
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:77
static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, int index, int *type, char *data, size_t *data_len)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int8_t slice_qp_delta
Definition: cbs_h264.h:355
struct H264RawSliceHeader::@28 rplm_l0[H264_MAX_RPLM_COUNT]
static const AVOption vaapi_encode_h264_options[]
H264RawSEIRecoveryPoint sei_recovery_point
#define FF_PROFILE_H264_BASELINE
Definition: avcodec.h:1897
uint8_t
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
AVOptions.
uint16_t frame_crop_top_offset
Definition: cbs_h264.h:150
uint16_t pic_width_in_mbs_minus1
Definition: cbs_h264.h:140
int dpb_frames
Definition: h264_levels.c:159
static const AVCodecDefault vaapi_encode_h264_defaults[]
const VAAPIEncodeRCMode * rc_mode
Definition: vaapi_encode.h:220
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
Definition: avcodec.h:805
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:290
static const uint8_t vaapi_encode_h264_sei_identifier_uuid[16]
uint32_t num_units_in_tick
Definition: cbs_h264.h:80
uint8_t mb_adaptive_frame_field_flag
Definition: cbs_h264.h:144
uint8_t transform_8x8_mode_flag
Definition: cbs_h264.h:208
uint8_t constrained_intra_pred_flag
Definition: cbs_h264.h:203
uint32_t cpb_size_value_minus1[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:49
uint16_t frame_crop_left_offset
Definition: cbs_h264.h:148
static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)
#define av_clip
Definition: common.h:122
static float hn(int n, EqParameter *param, float fs)
static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
struct VAAPIEncodePicture * prev
Definition: vaapi_encode.h:119
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define FF_PROFILE_H264_EXTENDED
Definition: avcodec.h:1900
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
VASurfaceID recon_surface
Definition: vaapi_encode.h:94
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
static const uint8_t header[24]
Definition: sdr2.c:67
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:219
uint16_t idr_pic_id
Definition: cbs_h264.h:303
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1178
#define FFALIGN(x, a)
Definition: macros.h:48
uint8_t ref_pic_list_modification_flag_l0
Definition: cbs_h264.h:316
int8_t second_chroma_qp_index_offset
Definition: cbs_h264.h:215
#define av_log(a,...)
void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment&#39;s own data buffer, but not the units a...
Definition: cbs.c:156
uint8_t cpb_size_scale
Definition: cbs_h264.h:46
uint8_t nal_hrd_parameters_present_flag
Definition: cbs_h264.h:84
uint8_t bit_depth_luma_minus8
Definition: cbs_h264.h:119
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:131
H.264 common definitions.
uint8_t pic_scaling_matrix_present_flag
Definition: cbs_h264.h:210
unsigned int va_rc_mode
Definition: vaapi_encode.h:230
uint8_t chroma_sample_loc_type_top_field
Definition: cbs_h264.h:76
uint8_t modification_of_pic_nums_idc
Definition: cbs_h264.h:319
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
uint8_t aspect_ratio_info_present_flag
Definition: cbs_h264.h:59
uint8_t max_num_ref_frames
Definition: cbs_h264.h:137
uint8_t weighted_bipred_idc
Definition: cbs_h264.h:196
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:117
uint8_t transfer_characteristics
Definition: cbs_h264.h:72
static int vaapi_encode_h264_write_access_unit(AVCodecContext *avctx, char *data, size_t *data_len, CodedBitstreamFragment *au)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
CodedBitstreamContext * cbc
#define FF_PROFILE_H264_HIGH_422
Definition: avcodec.h:1905
#define FF_PROFILE_H264_HIGH
Definition: avcodec.h:1901
uint16_t sar_width
Definition: cbs_h264.h:61
uint32_t initial_cpb_removal_delay_offset[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:228
uint8_t slice_type
Definition: cbs_h264.h:293
uint8_t num_ref_idx_l1_default_active_minus1
Definition: cbs_h264.h:193
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: codec.h:204
uint8_t frame_mbs_only_flag
Definition: cbs_h264.h:143
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
Definition: avcodec.h:841
uint8_t ref_pic_list_modification_flag_l1
Definition: cbs_h264.h:317
static const AVCodecDefault defaults[]
Definition: amfenc_h264.c:361
unsigned int va_bit_rate
Definition: vaapi_encode.h:232
uint8_t seq_parameter_set_id
Definition: cbs_h264.h:115
uint8_t seq_scaling_matrix_present_flag
Definition: cbs_h264.h:123
static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
void * codec_picture_params
Definition: vaapi_encode.h:103
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:135
#define fail()
Definition: checkasm.h:133
uint8_t timing_info_present_flag
Definition: cbs_h264.h:79
uint8_t more_rbsp_data
Definition: cbs_h264.h:205
H264RawSEIPicTiming sei_pic_timing
uint8_t video_full_range_flag
Definition: cbs_h264.h:69
uint8_t seq_parameter_set_id
Definition: cbs_h264.h:175
#define LEVEL(name, value)
common internal API header
struct H264RawSliceHeader::@29 mmco[H264_MAX_MMCO_COUNT]
uint8_t adaptive_ref_pic_marking_mode_flag
Definition: cbs_h264.h:344
AVCodec ff_h264_vaapi_encoder
uint8_t colour_primaries
Definition: cbs_h264.h:71
static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
uint16_t sar_height
Definition: cbs_h264.h:62
uint16_t frame_crop_bottom_offset
Definition: cbs_h264.h:151
struct H264RawSliceHeader::@28 rplm_l1[H264_MAX_RPLM_COUNT]
int width
picture width / height.
Definition: avcodec.h:709
uint8_t vui_parameters_present_flag
Definition: cbs_h264.h:153
const VAAPIEncodeProfile * profile
Definition: vaapi_encode.h:217
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1859
const VAAPIEncodeProfile * profiles
Definition: vaapi_encode.h:369
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
uint32_t cpb_removal_delay
Definition: cbs_h264.h:250
AVFormatContext * ctx
Definition: movenc.c:48
uint8_t colour_description_present_flag
Definition: cbs_h264.h:70
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1150
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
static const VAAPIEncodeProfile vaapi_encode_h264_profiles[]
uint8_t video_signal_type_present_flag
Definition: cbs_h264.h:67
int level
level
Definition: avcodec.h:1984
void * codec_picture_params
Definition: vaapi_encode.h:278
static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice, char *data, size_t *data_len)
static int vaapi_encode_h264_add_nal(AVCodecContext *avctx, CodedBitstreamFragment *au, void *nal_unit)
int8_t chroma_qp_index_offset
Definition: cbs_h264.h:200
int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
uint8_t constraint_set5_flag
Definition: cbs_h264.h:111
uint8_t pic_parameter_set_id
Definition: cbs_h264.h:295
uint8_t chroma_format_idc
Definition: cbs_h264.h:117
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:124
uint8_t profile_idc
Definition: cbs_h264.h:105
int32_t abs_diff_pic_num_minus1
Definition: cbs_h264.h:320
Full range content.
Definition: pixfmt.h:586
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:358
VADisplay display
The VADisplay handle, to be filled by the user.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
Definition: vaapi_encode.h:116
uint32_t bit_rate_value_minus1[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:48
uint8_t cpb_cnt_minus1
Definition: cbs_h264.h:44
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:103
uint8_t chroma_loc_info_present_flag
Definition: cbs_h264.h:75
uint8_t cbr_flag[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:50
const struct VAAPIEncodeType * codec
Definition: vaapi_encode.h:179
Libavcodec external API header.
#define VAAPI_ENCODE_RC_OPTIONS
Definition: vaapi_encode.h:463
#define FF_PROFILE_H264_HIGH_422_INTRA
Definition: avcodec.h:1906
int compression_level
Definition: avcodec.h:608
uint8_t bottom_field_pic_order_in_frame_present_flag
Definition: cbs_h264.h:178
uint8_t entropy_coding_mode_flag
Definition: cbs_h264.h:177
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:118
VAAPIEncodeContext common
main external API structure.
Definition: avcodec.h:536
#define FF_PROFILE_H264_HIGH_10_INTRA
Definition: avcodec.h:1903
static int FUNC() aud(CodedBitstreamContext *ctx, RWContext *rw, H264RawAUD *current)
uint8_t pic_order_cnt_type
Definition: cbs_h264.h:129
Describe the class of an AVClass context structure.
Definition: log.h:67
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Context structure for coded bitstream operations.
Definition: cbs.h:170
uint8_t matrix_coefficients
Definition: cbs_h264.h:73
uint8_t num_ref_idx_l0_default_active_minus1
Definition: cbs_h264.h:192
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1164
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1157
uint16_t frame_num
Definition: cbs_h264.h:299
int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:737
SEIRawUserDataUnregistered sei_identifier
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define FF_PROFILE_H264_HIGH_444
Definition: avcodec.h:1908
uint8_t log2_max_mv_length_horizontal
Definition: cbs_h264.h:96
uint8_t direct_spatial_mv_pred_flag
Definition: cbs_h264.h:310
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:126
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:818
#define snprintf
Definition: snprintf.h:34
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
mfxU16 profile
Definition: qsvenc.c:45
CodedBitstreamFragment current_access_unit
#define VAAPI_ENCODE_COMMON_OPTIONS
Definition: vaapi_encode.h:445
uint8_t level
Definition: svq3.c:204
uint8_t direct_8x8_inference_flag
Definition: cbs_h264.h:145
static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice)
uint32_t initial_cpb_removal_delay[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:227
VAEncMiscParameterHRD hrd_params
Definition: vaapi_encode.h:267
common internal api header.
common internal and external API header
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
uint8_t redundant_pic_cnt_present_flag
Definition: cbs_h264.h:207
uint8_t memory_management_control_operation
Definition: cbs_h264.h:346
uint8_t cpb_removal_delay_length_minus1
Definition: cbs_h264.h:53
uint8_t motion_vectors_over_pic_boundaries_flag
Definition: cbs_h264.h:93
uint32_t time_scale
Definition: cbs_h264.h:81
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
uint8_t bitstream_restriction_flag
Definition: cbs_h264.h:92
uint8_t level_idc
Definition: cbs_h264.h:113
uint16_t frame_crop_right_offset
Definition: cbs_h264.h:149
int den
Denominator.
Definition: rational.h:60
uint8_t pic_parameter_set_id
Definition: cbs_h264.h:174
uint8_t max_num_reorder_frames
Definition: cbs_h264.h:98
void * priv_data
Definition: avcodec.h:563
#define PROFILE(name, value)
uint8_t log2_max_mv_length_vertical
Definition: cbs_h264.h:97
H264RawSliceHeader header
Definition: cbs_h264.h:368
uint8_t log2_max_pic_order_cnt_lsb_minus4
Definition: cbs_h264.h:130
int len
const char * name
Definition: h264_levels.h:26
struct H264RawSEIBufferingPeriod::@27 nal
static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
void * codec_slice_params
Definition: vaapi_encode.h:67
uint8_t delta_pic_order_always_zero_flag
Definition: cbs_h264.h:131
H264RawSEIBufferingPeriod sei_buffering_period
#define FF_PROFILE_H264_HIGH_444_INTRA
Definition: avcodec.h:1910
void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:170
int8_t pic_init_qp_minus26
Definition: cbs_h264.h:198
#define LIBAVCODEC_IDENT
Definition: version.h:42
unsigned int desired_packed_headers
Definition: vaapi_encode.h:201
uint8_t nal_ref_idc
Definition: cbs_h264.h:32
#define av_freep(p)
#define FFSWAP(type, a, b)
Definition: common.h:108
uint8_t constraint_set1_flag
Definition: cbs_h264.h:107
uint16_t pic_height_in_map_units_minus1
Definition: cbs_h264.h:141
#define FF_PROFILE_H264_CONSTRAINED_BASELINE
Definition: avcodec.h:1898
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
VABufferID output_buffer
Definition: vaapi_encode.h:100
#define FF_PROFILE_H264_HIGH_10
Definition: avcodec.h:1902
int32_t difference_of_pic_nums_minus1
Definition: cbs_h264.h:347
uint8_t nal_unit_type
Definition: cbs_h264.h:33
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
struct VAAPIEncodePicture * dpb[MAX_DPB_SIZE]
Definition: vaapi_encode.h:112
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators...
Definition: codec.h:52
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
int i
Definition: input.c:407
const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[]
Definition: vaapi_encode.c:32
const H264LevelDescriptor * ff_h264_guess_level(int profile_idc, int64_t bitrate, int framerate, int width, int height, int max_dec_frame_buffering)
Guess the level of a stream from some parameters.
Definition: h264_levels.c:78
AVVAAPIDeviceContext * hwctx
Definition: vaapi_encode.h:245
uint8_t constraint_set3_flag
Definition: cbs_h264.h:109