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