FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 VAAPIEncodeH264Context {
52 
53  // User options.
54  int qp;
55  int quality;
56  int coder;
57  int aud;
58  int sei;
59  int profile;
60  int level;
61 
62  // Derived settings.
63  int mb_width;
64  int mb_height;
65 
69 
70  // Stream state.
71  int frame_num;
74  int64_t last_idr_frame;
75  int64_t idr_pic_count;
76 
79 
80  int cpb_delay;
81  int dpb_delay;
82 
83  // Writer structures.
86 
92 
98 
103 
104 
106  char *data, size_t *data_len,
108 {
109  VAAPIEncodeH264Context *priv = avctx->priv_data;
110  int err;
111 
112  err = ff_cbs_write_fragment_data(priv->cbc, au);
113  if (err < 0) {
114  av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
115  return err;
116  }
117 
118  if (*data_len < 8 * au->data_size - au->data_bit_padding) {
119  av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
120  "%zu < %zu.\n", *data_len,
121  8 * au->data_size - au->data_bit_padding);
122  return AVERROR(ENOSPC);
123  }
124 
125  memcpy(data, au->data, au->data_size);
126  *data_len = 8 * au->data_size - au->data_bit_padding;
127 
128  return 0;
129 }
130 
133  void *nal_unit)
134 {
135  VAAPIEncodeH264Context *priv = avctx->priv_data;
136  H264RawNALUnitHeader *header = nal_unit;
137  int err;
138 
139  err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
140  header->nal_unit_type, nal_unit, NULL);
141  if (err < 0) {
142  av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
143  "type = %d.\n", header->nal_unit_type);
144  return err;
145  }
146 
147  return 0;
148 }
149 
151  char *data, size_t *data_len)
152 {
153  VAAPIEncodeH264Context *priv = avctx->priv_data;
155  int err;
156 
157  if (priv->aud_needed) {
158  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
159  if (err < 0)
160  goto fail;
161  priv->aud_needed = 0;
162  }
163 
164  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_sps);
165  if (err < 0)
166  goto fail;
167 
168  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_pps);
169  if (err < 0)
170  goto fail;
171 
172  err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
173 fail:
174  ff_cbs_fragment_uninit(priv->cbc, au);
175  return err;
176 }
177 
179  VAAPIEncodePicture *pic,
180  VAAPIEncodeSlice *slice,
181  char *data, size_t *data_len)
182 {
183  VAAPIEncodeH264Context *priv = avctx->priv_data;
185  int err;
186 
187  if (priv->aud_needed) {
188  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
189  if (err < 0)
190  goto fail;
191  priv->aud_needed = 0;
192  }
193 
194  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_slice);
195  if (err < 0)
196  goto fail;
197 
198  err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
199 fail:
200  ff_cbs_fragment_uninit(priv->cbc, au);
201  return err;
202 }
203 
205  VAAPIEncodePicture *pic,
206  int index, int *type,
207  char *data, size_t *data_len)
208 {
209  VAAPIEncodeH264Context *priv = avctx->priv_data;
211  int err, i;
212 
213  if (priv->sei_needed) {
214  H264RawSEI *sei = &priv->raw_sei;
215 
216  if (priv->aud_needed) {
217  err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
218  if (err < 0)
219  goto fail;
220  priv->aud_needed = 0;
221  }
222 
223  *sei = (H264RawSEI) {
224  .nal_unit_header = {
226  },
227  };
228 
229  i = 0;
230 
231  if (priv->sei_needed & SEI_IDENTIFIER) {
234  ++i;
235  }
236  if (priv->sei_needed & SEI_TIMING) {
237  if (pic->type == PICTURE_TYPE_IDR) {
240  ++i;
241  }
243  sei->payload[i].payload.pic_timing = priv->sei_pic_timing;
244  ++i;
245  }
246  if (priv->sei_needed & SEI_RECOVERY_POINT) {
249  ++i;
250  }
251 
252  sei->payload_count = i;
253  av_assert0(sei->payload_count > 0);
254 
255  err = vaapi_encode_h264_add_nal(avctx, au, sei);
256  if (err < 0)
257  goto fail;
258  priv->sei_needed = 0;
259 
260  err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
261  if (err < 0)
262  goto fail;
263 
264  ff_cbs_fragment_uninit(priv->cbc, au);
265 
266  *type = VAEncPackedHeaderRawData;
267  return 0;
268 
269 #if !CONFIG_VAAPI_1
270  } else if (priv->sei_cbr_workaround_needed) {
271  // Insert a zero-length header using the old SEI type. This is
272  // required to avoid triggering broken behaviour on Intel platforms
273  // in CBR mode where an invalid SEI message is generated by the
274  // driver and inserted into the stream.
275  *data_len = 0;
276  *type = VAEncPackedHeaderH264_SEI;
277  priv->sei_cbr_workaround_needed = 0;
278  return 0;
279 #endif
280 
281  } else {
282  return AVERROR_EOF;
283  }
284 
285 fail:
286  ff_cbs_fragment_uninit(priv->cbc, au);
287  return err;
288 }
289 
291 {
292  VAAPIEncodeContext *ctx = avctx->priv_data;
293  VAAPIEncodeH264Context *priv = avctx->priv_data;
294  H264RawSPS *sps = &priv->raw_sps;
295  H264RawPPS *pps = &priv->raw_pps;
296  VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
297  VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
298  int dpb_frames;
299 
300  memset(&priv->current_access_unit, 0,
301  sizeof(priv->current_access_unit));
302 
303  memset(sps, 0, sizeof(*sps));
304  memset(pps, 0, sizeof(*pps));
305 
306  sps->nal_unit_header.nal_ref_idc = 3;
308 
309  sps->profile_idc = avctx->profile & 0xff;
310 
312  avctx->profile == FF_PROFILE_H264_MAIN)
313  sps->constraint_set1_flag = 1;
314 
315  if (avctx->profile == FF_PROFILE_H264_HIGH)
316  sps->constraint_set3_flag = ctx->gop_size == 1;
317 
318  if (avctx->profile == FF_PROFILE_H264_MAIN ||
319  avctx->profile == FF_PROFILE_H264_HIGH) {
320  sps->constraint_set4_flag = 1;
321  sps->constraint_set5_flag = ctx->b_per_p == 0;
322  }
323 
324  if (ctx->gop_size == 1)
325  dpb_frames = 0;
326  else
327  dpb_frames = 1 + (ctx->b_per_p > 0);
328 
329  if (avctx->level != FF_LEVEL_UNKNOWN) {
330  sps->level_idc = avctx->level;
331  } else {
332  const H264LevelDescriptor *level;
333 
334  level = ff_h264_guess_level(sps->profile_idc,
335  avctx->bit_rate,
336  priv->mb_width * 16,
337  priv->mb_height * 16,
338  dpb_frames);
339  if (level) {
340  av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
341  if (level->constraint_set3_flag)
342  sps->constraint_set3_flag = 1;
343  sps->level_idc = level->level_idc;
344  } else {
345  av_log(avctx, AV_LOG_WARNING, "Stream will not conform "
346  "to any level: using level 6.2.\n");
347  sps->level_idc = 62;
348  }
349  }
350 
351  sps->seq_parameter_set_id = 0;
352  sps->chroma_format_idc = 1;
353 
354  sps->log2_max_frame_num_minus4 = 4;
355  sps->pic_order_cnt_type = 0;
357  av_clip(av_log2(ctx->b_per_p + 1) - 2, 0, 12);
358 
360 
361  sps->pic_width_in_mbs_minus1 = priv->mb_width - 1;
362  sps->pic_height_in_map_units_minus1 = priv->mb_height - 1;
363 
364  sps->frame_mbs_only_flag = 1;
365  sps->direct_8x8_inference_flag = 1;
366 
367  if (avctx->width != 16 * priv->mb_width ||
368  avctx->height != 16 * priv->mb_height) {
369  sps->frame_cropping_flag = 1;
370 
371  sps->frame_crop_left_offset = 0;
373  (16 * priv->mb_width - avctx->width) / 2;
374  sps->frame_crop_top_offset = 0;
376  (16 * priv->mb_height - avctx->height) / 2;
377  } else {
378  sps->frame_cropping_flag = 0;
379  }
380 
382 
383  if (avctx->sample_aspect_ratio.num != 0 &&
384  avctx->sample_aspect_ratio.den != 0) {
385  static const AVRational sar_idc[] = {
386  { 0, 0 },
387  { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
388  { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
389  { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
390  { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
391  };
392  int i;
393  for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
394  if (avctx->sample_aspect_ratio.num == sar_idc[i].num &&
395  avctx->sample_aspect_ratio.den == sar_idc[i].den) {
396  sps->vui.aspect_ratio_idc = i;
397  break;
398  }
399  }
400  if (i >= FF_ARRAY_ELEMS(sar_idc)) {
401  sps->vui.aspect_ratio_idc = 255;
402  sps->vui.sar_width = avctx->sample_aspect_ratio.num;
403  sps->vui.sar_height = avctx->sample_aspect_ratio.den;
404  }
406  }
407 
408  if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
410  avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
411  avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
413  sps->vui.video_format = 5; // Unspecified.
415  avctx->color_range == AVCOL_RANGE_JPEG;
416 
417  if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
418  avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
419  avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
421  sps->vui.colour_primaries = avctx->color_primaries;
422  sps->vui.transfer_characteristics = avctx->color_trc;
423  sps->vui.matrix_coefficients = avctx->colorspace;
424  }
425  } else {
426  sps->vui.video_format = 5;
427  sps->vui.video_full_range_flag = 0;
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 
437  avctx->chroma_sample_location - 1;
438  }
439 
440  sps->vui.timing_info_present_flag = 1;
441  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
442  sps->vui.num_units_in_tick = avctx->framerate.den;
443  sps->vui.time_scale = 2 * avctx->framerate.num;
444  sps->vui.fixed_frame_rate_flag = 1;
445  } else {
446  sps->vui.num_units_in_tick = avctx->time_base.num;
447  sps->vui.time_scale = 2 * avctx->time_base.den;
448  sps->vui.fixed_frame_rate_flag = 0;
449  }
450 
451  if (priv->sei & SEI_TIMING) {
452  H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
454 
456 
457  hrd->cpb_cnt_minus1 = 0;
458 
459  // Try to scale these to a sensible range so that the
460  // golomb encode of the value is not overlong.
461  hrd->bit_rate_scale =
462  av_clip_uintp2(av_log2(ctx->va_bit_rate) - 15 - 6, 4);
463  hrd->bit_rate_value_minus1[0] =
464  (ctx->va_bit_rate >> hrd->bit_rate_scale + 6) - 1;
465 
466  hrd->cpb_size_scale =
467  av_clip_uintp2(av_log2(ctx->hrd_params.hrd.buffer_size) - 15 - 4, 4);
468  hrd->cpb_size_value_minus1[0] =
469  (ctx->hrd_params.hrd.buffer_size >> hrd->cpb_size_scale + 4) - 1;
470 
471  // CBR mode as defined for the HRD cannot be achieved without filler
472  // data, so this flag cannot be set even with VAAPI CBR modes.
473  hrd->cbr_flag[0] = 0;
474 
478  hrd->time_offset_length = 0;
479 
481 
482  // This calculation can easily overflow 32 bits.
483  bp->nal.initial_cpb_removal_delay[0] = 90000 *
484  (uint64_t)ctx->hrd_params.hrd.initial_buffer_fullness /
485  ctx->hrd_params.hrd.buffer_size;
487  } else {
490  }
491 
496  sps->vui.max_num_reorder_frames = (ctx->b_per_p > 0);
498 
499  pps->nal_unit_header.nal_ref_idc = 3;
501 
502  pps->pic_parameter_set_id = 0;
503  pps->seq_parameter_set_id = 0;
504 
509  if (!priv->coder && pps->entropy_coding_mode_flag)
510  pps->entropy_coding_mode_flag = 0;
511 
514 
515  pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
516 
520  pps->more_rbsp_data = 0;
521  } else {
522  pps->more_rbsp_data = 1;
523 
524  pps->transform_8x8_mode_flag = 1;
525  }
526 
527  *vseq = (VAEncSequenceParameterBufferH264) {
528  .seq_parameter_set_id = sps->seq_parameter_set_id,
529  .level_idc = sps->level_idc,
530  .intra_period = ctx->gop_size,
531  .intra_idr_period = ctx->gop_size,
532  .ip_period = ctx->b_per_p + 1,
533 
534  .bits_per_second = ctx->va_bit_rate,
535  .max_num_ref_frames = sps->max_num_ref_frames,
536  .picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1,
537  .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
538 
539  .seq_fields.bits = {
540  .chroma_format_idc = sps->chroma_format_idc,
541  .frame_mbs_only_flag = sps->frame_mbs_only_flag,
542  .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
543  .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
544  .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
545  .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
546  .pic_order_cnt_type = sps->pic_order_cnt_type,
547  .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
548  .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
549  },
550 
551  .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
552  .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
553 
554  .frame_cropping_flag = sps->frame_cropping_flag,
555  .frame_crop_left_offset = sps->frame_crop_left_offset,
556  .frame_crop_right_offset = sps->frame_crop_right_offset,
557  .frame_crop_top_offset = sps->frame_crop_top_offset,
558  .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
559 
560  .vui_parameters_present_flag = sps->vui_parameters_present_flag,
561 
562  .vui_fields.bits = {
563  .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
564  .timing_info_present_flag = sps->vui.timing_info_present_flag,
565  .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
566  .log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal,
567  .log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical,
568  },
569 
570  .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
571  .sar_width = sps->vui.sar_width,
572  .sar_height = sps->vui.sar_height,
573  .num_units_in_tick = sps->vui.num_units_in_tick,
574  .time_scale = sps->vui.time_scale,
575  };
576 
577  *vpic = (VAEncPictureParameterBufferH264) {
578  .CurrPic = {
579  .picture_id = VA_INVALID_ID,
580  .flags = VA_PICTURE_H264_INVALID,
581  },
582 
583  .coded_buf = VA_INVALID_ID,
584 
585  .pic_parameter_set_id = pps->pic_parameter_set_id,
586  .seq_parameter_set_id = pps->seq_parameter_set_id,
587 
588  .pic_init_qp = pps->pic_init_qp_minus26 + 26,
589  .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
590  .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
591 
592  .chroma_qp_index_offset = pps->chroma_qp_index_offset,
593  .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
594 
595  .pic_fields.bits = {
596  .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
597  .weighted_pred_flag = pps->weighted_pred_flag,
598  .weighted_bipred_idc = pps->weighted_bipred_idc,
599  .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
600  .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
601  .deblocking_filter_control_present_flag =
603  .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
604  .pic_order_present_flag =
606  .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
607  },
608  };
609 
610  return 0;
611 }
612 
614  VAAPIEncodePicture *pic)
615 {
616  VAAPIEncodeContext *ctx = avctx->priv_data;
617  VAAPIEncodeH264Context *priv = avctx->priv_data;
618  H264RawSPS *sps = &priv->raw_sps;
619  VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
620  int i;
621 
622  memset(&priv->current_access_unit, 0,
623  sizeof(priv->current_access_unit));
624 
625  if (pic->type == PICTURE_TYPE_IDR) {
626  av_assert0(pic->display_order == pic->encode_order);
627  priv->frame_num = 0;
628  priv->next_frame_num = 1;
629  priv->cpb_delay = 0;
630  priv->last_idr_frame = pic->display_order;
631  ++priv->idr_pic_count;
632 
633  priv->slice_type = 7;
634  priv->primary_pic_type = 0;
635  } else {
636  priv->frame_num = priv->next_frame_num;
637 
638  if (pic->type != PICTURE_TYPE_B) {
639  // Reference picture, so frame_num advances.
640  priv->next_frame_num = (priv->frame_num + 1) &
641  ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
642  }
643  ++priv->cpb_delay;
644 
645  if (pic->type == PICTURE_TYPE_I) {
646  priv->slice_type = 7;
647  priv->primary_pic_type = 0;
648  } else if (pic->type == PICTURE_TYPE_P) {
649  priv->slice_type = 5;
650  priv->primary_pic_type = 1;
651  } else {
652  priv->slice_type = 6;
653  priv->primary_pic_type = 2;
654  }
655  }
656  priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
657  priv->dpb_delay = pic->display_order - pic->encode_order + 1;
658 
659  if (priv->aud) {
660  priv->aud_needed = 1;
661  priv->raw_aud = (H264RawAUD) {
662  .nal_unit_header = {
664  },
665  .primary_pic_type = priv->primary_pic_type,
666  };
667  } else {
668  priv->aud_needed = 0;
669  }
670 
671  priv->sei_needed = 0;
672 
673  if (priv->sei & SEI_IDENTIFIER && pic->encode_order == 0)
674  priv->sei_needed |= SEI_IDENTIFIER;
675 #if !CONFIG_VAAPI_1
676  if (ctx->va_rc_mode == VA_RC_CBR)
677  priv->sei_cbr_workaround_needed = 1;
678 #endif
679 
680  if (priv->sei & SEI_TIMING) {
682  .cpb_removal_delay = 2 * priv->cpb_delay,
683  .dpb_output_delay = 2 * priv->dpb_delay,
684  };
685 
686  priv->sei_needed |= SEI_TIMING;
687  }
688 
689  if (priv->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
691  .recovery_frame_cnt = 0,
692  .exact_match_flag = 1,
693  .broken_link_flag = ctx->b_per_p > 0,
694  };
695 
697  }
698 
699  vpic->CurrPic = (VAPictureH264) {
700  .picture_id = pic->recon_surface,
701  .frame_idx = priv->frame_num,
702  .flags = 0,
703  .TopFieldOrderCnt = priv->pic_order_cnt,
704  .BottomFieldOrderCnt = priv->pic_order_cnt,
705  };
706 
707  for (i = 0; i < pic->nb_refs; i++) {
708  VAAPIEncodePicture *ref = pic->refs[i];
709  unsigned int frame_num = (ref->encode_order - priv->last_idr_frame) &
710  ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
711  unsigned int pic_order_cnt = ref->display_order - priv->last_idr_frame;
712 
713  av_assert0(ref && ref->encode_order < pic->encode_order);
714  vpic->ReferenceFrames[i] = (VAPictureH264) {
715  .picture_id = ref->recon_surface,
716  .frame_idx = frame_num,
717  .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
718  .TopFieldOrderCnt = pic_order_cnt,
719  .BottomFieldOrderCnt = pic_order_cnt,
720  };
721  }
722  for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
723  vpic->ReferenceFrames[i] = (VAPictureH264) {
724  .picture_id = VA_INVALID_ID,
725  .flags = VA_PICTURE_H264_INVALID,
726  };
727  }
728 
729  vpic->coded_buf = pic->output_buffer;
730 
731  vpic->frame_num = priv->frame_num;
732 
733  vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
734  vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
735 
736  pic->nb_slices = 1;
737 
738  return 0;
739 }
740 
742  VAAPIEncodePicture *pic,
743  VAAPIEncodeSlice *slice)
744 {
745  VAAPIEncodeH264Context *priv = avctx->priv_data;
746  H264RawSPS *sps = &priv->raw_sps;
747  H264RawPPS *pps = &priv->raw_pps;
748  H264RawSliceHeader *sh = &priv->raw_slice.header;
749  VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
750  VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
751  int i;
752 
753  if (pic->type == PICTURE_TYPE_IDR) {
756  } else {
759  }
760 
761  // Only one slice per frame.
762  sh->first_mb_in_slice = 0;
763  sh->slice_type = priv->slice_type;
764 
766 
767  sh->frame_num = priv->frame_num;
768  sh->idr_pic_id = priv->idr_pic_count;
769 
770  sh->pic_order_cnt_lsb = priv->pic_order_cnt &
771  ((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
772 
774 
775  if (pic->type == PICTURE_TYPE_B)
776  sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26);
777  else if (pic->type == PICTURE_TYPE_P)
778  sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26);
779  else
780  sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26);
781 
782 
783  vslice->macroblock_address = sh->first_mb_in_slice;
784  vslice->num_macroblocks = priv->mb_width * priv->mb_height;
785 
786  vslice->macroblock_info = VA_INVALID_ID;
787 
788  vslice->slice_type = sh->slice_type % 5;
789  vslice->pic_parameter_set_id = sh->pic_parameter_set_id;
790  vslice->idr_pic_id = sh->idr_pic_id;
791 
792  vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb;
793 
794  vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag;
795 
796  for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
797  vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
798  vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
799  vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
800  vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
801  }
802 
803  av_assert0(pic->nb_refs <= 2);
804  if (pic->nb_refs >= 1) {
805  // Backward reference for P- or B-frame.
806  av_assert0(pic->type == PICTURE_TYPE_P ||
807  pic->type == PICTURE_TYPE_B);
808  vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
809  }
810  if (pic->nb_refs >= 2) {
811  // Forward reference for B-frame.
812  av_assert0(pic->type == PICTURE_TYPE_B);
813  vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
814  }
815 
816  vslice->slice_qp_delta = sh->slice_qp_delta;
817 
818  return 0;
819 }
820 
822 {
823  VAAPIEncodeContext *ctx = avctx->priv_data;
824  VAAPIEncodeH264Context *priv = avctx->priv_data;
825  int err;
826 
827  err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
828  if (err < 0)
829  return err;
830 
831  priv->mb_width = FFALIGN(avctx->width, 16) / 16;
832  priv->mb_height = FFALIGN(avctx->height, 16) / 16;
833 
834  if (ctx->va_rc_mode == VA_RC_CQP) {
835  priv->fixed_qp_p = priv->qp;
836  if (avctx->i_quant_factor > 0.0)
837  priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
838  avctx->i_quant_offset) + 0.5);
839  else
840  priv->fixed_qp_idr = priv->fixed_qp_p;
841  if (avctx->b_quant_factor > 0.0)
842  priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
843  avctx->b_quant_offset) + 0.5);
844  else
845  priv->fixed_qp_b = priv->fixed_qp_p;
846 
847  priv->sei &= ~SEI_TIMING;
848 
849  av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
850  "%d / %d / %d for IDR- / P- / B-frames.\n",
851  priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
852 
853  } else if (ctx->va_rc_mode == VA_RC_CBR ||
854  ctx->va_rc_mode == VA_RC_VBR) {
855  // These still need to be set for pic_init_qp/slice_qp_delta.
856  priv->fixed_qp_idr = 26;
857  priv->fixed_qp_p = 26;
858  priv->fixed_qp_b = 26;
859 
860  } else {
861  av_assert0(0 && "Invalid RC mode.");
862  }
863 
864  if (priv->sei & SEI_IDENTIFIER) {
865  const char *lavc = LIBAVCODEC_IDENT;
866  const char *vaapi = VA_VERSION_S;
867  const char *driver;
868  int len;
869 
870  memcpy(priv->sei_identifier.uuid_iso_iec_11578,
872  sizeof(priv->sei_identifier.uuid_iso_iec_11578));
873 
874  driver = vaQueryVendorString(ctx->hwctx->display);
875  if (!driver)
876  driver = "unknown driver";
877 
878  len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver);
879  if (len >= 0) {
880  priv->sei_identifier_string = av_malloc(len + 1);
881  if (!priv->sei_identifier_string)
882  return AVERROR(ENOMEM);
883 
884  snprintf(priv->sei_identifier_string, len + 1,
885  "%s / VAAPI %s / %s", lavc, vaapi, driver);
886 
888  priv->sei_identifier.data_length = len + 1;
889  }
890  }
891 
892  return 0;
893 }
894 
896  { FF_PROFILE_H264_HIGH, 8, 3, 1, 1, VAProfileH264High },
897  { FF_PROFILE_H264_MAIN, 8, 3, 1, 1, VAProfileH264Main },
899  8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
901 };
902 
905 
906  .configure = &vaapi_encode_h264_configure,
907 
908  .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
909  .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
910 
911  .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
912  .init_picture_params = &vaapi_encode_h264_init_picture_params,
913 
914  .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
915  .init_slice_params = &vaapi_encode_h264_init_slice_params,
916 
917  .sequence_header_type = VAEncPackedHeaderSequence,
918  .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
919 
920  .slice_header_type = VAEncPackedHeaderH264_Slice,
921  .write_slice_header = &vaapi_encode_h264_write_slice_header,
922 
923  .write_extra_header = &vaapi_encode_h264_write_extra_header,
924 };
925 
927 {
928  VAAPIEncodeContext *ctx = avctx->priv_data;
929  VAAPIEncodeH264Context *priv = avctx->priv_data;
930 
932 
933  if (avctx->profile == FF_PROFILE_UNKNOWN)
934  avctx->profile = priv->profile;
935  if (avctx->level == FF_LEVEL_UNKNOWN)
936  avctx->level = priv->level;
938  avctx->compression_level = priv->quality;
939 
940  // Reject unsupported profiles.
941  switch (avctx->profile) {
943  av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
944  "supported, using constrained baseline profile instead.\n");
946  break;
948  av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
949  "is not supported.\n");
950  return AVERROR_PATCHWELCOME;
953  av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
954  "are not supported.\n");
955  return AVERROR_PATCHWELCOME;
962  av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
963  "are not supported.\n");
964  return AVERROR_PATCHWELCOME;
965  }
966 
967  if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
968  av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
969  "in 8-bit unsigned integer.\n", avctx->level);
970  return AVERROR(EINVAL);
971  }
972 
974  VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
975  VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
976  VA_ENC_PACKED_HEADER_MISC; // SEI.
977 
978  ctx->surface_width = FFALIGN(avctx->width, 16);
979  ctx->surface_height = FFALIGN(avctx->height, 16);
980 
981  return ff_vaapi_encode_init(avctx);
982 }
983 
985 {
986  VAAPIEncodeH264Context *priv = avctx->priv_data;
987 
988  ff_cbs_close(&priv->cbc);
990 
991  return ff_vaapi_encode_close(avctx);
992 }
993 
994 #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
995 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
998 
999  { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1000  OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
1001  { "quality", "Set encode quality (trades off against speed, higher is faster)",
1002  OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
1003  { "coder", "Entropy coder type",
1004  OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
1005  { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1006  { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1007  { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1008  { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1009 
1010  { "aud", "Include AUD",
1011  OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1012 
1013  { "sei", "Set SEI to include",
1016  0, INT_MAX, FLAGS, "sei" },
1017  { "identifier", "Include encoder version identifier",
1018  0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
1019  INT_MIN, INT_MAX, FLAGS, "sei" },
1020  { "timing", "Include timing parameters (buffering_period and pic_timing)",
1021  0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
1022  INT_MIN, INT_MAX, FLAGS, "sei" },
1023  { "recovery_point", "Include recovery points where appropriate",
1024  0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
1025  INT_MIN, INT_MAX, FLAGS, "sei" },
1026 
1027  { "profile", "Set profile (profile_idc and constraint_set*_flag)",
1029  { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xffff, FLAGS, "profile" },
1030 
1031 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1032  { .i64 = value }, 0, 0, FLAGS, "profile"
1033  { PROFILE("constrained_baseline", FF_PROFILE_H264_CONSTRAINED_BASELINE) },
1034  { PROFILE("main", FF_PROFILE_H264_MAIN) },
1035  { PROFILE("high", FF_PROFILE_H264_HIGH) },
1036 #undef PROFILE
1037 
1038  { "level", "Set level (level_idc)",
1040  { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
1041 
1042 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1043  { .i64 = value }, 0, 0, FLAGS, "level"
1044  { LEVEL("1", 10) },
1045  { LEVEL("1.1", 11) },
1046  { LEVEL("1.2", 12) },
1047  { LEVEL("1.3", 13) },
1048  { LEVEL("2", 20) },
1049  { LEVEL("2.1", 21) },
1050  { LEVEL("2.2", 22) },
1051  { LEVEL("3", 30) },
1052  { LEVEL("3.1", 31) },
1053  { LEVEL("3.2", 32) },
1054  { LEVEL("4", 40) },
1055  { LEVEL("4.1", 41) },
1056  { LEVEL("4.2", 42) },
1057  { LEVEL("5", 50) },
1058  { LEVEL("5.1", 51) },
1059  { LEVEL("5.2", 52) },
1060  { LEVEL("6", 60) },
1061  { LEVEL("6.1", 61) },
1062  { LEVEL("6.2", 62) },
1063 #undef LEVEL
1064 
1065  { NULL },
1066 };
1067 
1069  { "b", "0" },
1070  { "bf", "2" },
1071  { "g", "120" },
1072  { "i_qfactor", "1" },
1073  { "i_qoffset", "0" },
1074  { "b_qfactor", "6/5" },
1075  { "b_qoffset", "0" },
1076  { "qmin", "-1" },
1077  { "qmax", "-1" },
1078  { NULL },
1079 };
1080 
1082  .class_name = "h264_vaapi",
1083  .item_name = av_default_item_name,
1084  .option = vaapi_encode_h264_options,
1085  .version = LIBAVUTIL_VERSION_INT,
1086 };
1087 
1089  .name = "h264_vaapi",
1090  .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
1091  .type = AVMEDIA_TYPE_VIDEO,
1092  .id = AV_CODEC_ID_H264,
1093  .priv_data_size = sizeof(VAAPIEncodeH264Context),
1095  .encode2 = &ff_vaapi_encode2,
1096  .close = &vaapi_encode_h264_close,
1097  .priv_class = &vaapi_encode_h264_class,
1098  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
1099  .defaults = vaapi_encode_h264_defaults,
1100  .pix_fmts = (const enum AVPixelFormat[]) {
1103  },
1104  .wrapper_name = "vaapi",
1105 };
#define FF_PROFILE_H264_MAIN
Definition: avcodec.h:2899
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:182
uint8_t frame_cropping_flag
Definition: cbs_h264.h:157
uint8_t deblocking_filter_control_present_flag
Definition: cbs_h264.h:212
uint32_t payload_type
Definition: cbs_h264.h:319
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3055
static const VAAPIEncodeType vaapi_encode_type_h264
#define FF_PROFILE_H264_CAVLC_444
Definition: avcodec.h:2911
uint8_t bit_depth_chroma_minus8
Definition: cbs_h264.h:130
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:1605
uint8_t aspect_ratio_idc
Definition: cbs_h264.h:70
static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
uint8_t video_format
Definition: cbs_h264.h:78
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
uint8_t bit_rate_scale
Definition: cbs_h264.h:55
union H264RawSEIPayload::@50 payload
uint16_t recovery_frame_cnt
Definition: cbs_h264.h:294
uint8_t dpb_output_delay_length_minus1
Definition: cbs_h264.h:64
struct H264RawSEIBufferingPeriod::@49 nal
uint8_t initial_cpb_removal_delay_length_minus1
Definition: cbs_h264.h:62
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1582
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
uint8_t chroma_sample_loc_type_bottom_field
Definition: cbs_h264.h:87
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t constraint_set3_flag
Definition: h264_levels.h:28
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition: avcodec.h:1064
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
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:340
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2163
H264RawSEIRecoveryPoint recovery_point
Definition: cbs_h264.h:328
int num
Numerator.
Definition: rational.h:59
uint8_t log2_max_frame_num_minus4
Definition: cbs_h264.h:138
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
struct VAAPIEncodeContext::@161 hrd_params
int av_log2(unsigned v)
Definition: intmath.c:26
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:570
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:1911
uint16_t pic_order_cnt_lsb
Definition: cbs_h264.h:362
uint8_t time_offset_length
Definition: cbs_h264.h:65
uint8_t low_delay_hrd_flag
Definition: cbs_h264.h:98
void * codec_sequence_params
Definition: vaapi_encode.h:194
VAEncMiscParameterHRD hrd
Definition: vaapi_encode.h:180
#define FLAGS
#define OFFSET(x)
uint8_t max_dec_frame_buffering
Definition: cbs_h264.h:109
int profile
profile
Definition: avcodec.h:2858
uint8_t fixed_frame_rate_flag
Definition: cbs_h264.h:92
H264RawVUI vui
Definition: cbs_h264.h:164
AVCodec.
Definition: avcodec.h:3423
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1844
uint32_t first_mb_in_slice
Definition: cbs_h264.h:349
static const AVClass vaapi_encode_h264_class
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: avcodec.h:2909
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1655
uint8_t weighted_pred_flag
Definition: cbs_h264.h:205
uint8_t constraint_set4_flag
Definition: cbs_h264.h:120
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:2969
H264RawHRD nal_hrd_parameters
Definition: cbs_h264.h:95
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: avcodec.h:992
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:412
static const AVOption vaapi_encode_h264_options[]
H264RawSEIRecoveryPoint sei_recovery_point
#define FF_PROFILE_H264_BASELINE
Definition: avcodec.h:2897
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVOptions.
uint16_t frame_crop_top_offset
Definition: cbs_h264.h:160
uint16_t pic_width_in_mbs_minus1
Definition: cbs_h264.h:150
int dpb_frames
Definition: h264_levels.c:117
static const AVCodecDefault vaapi_encode_h264_defaults[]
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:1801
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:347
static const uint8_t vaapi_encode_h264_sei_identifier_uuid[16]
uint32_t num_units_in_tick
Definition: cbs_h264.h:90
unregistered user data
Definition: h264_sei.h:33
uint8_t mb_adaptive_frame_field_flag
Definition: cbs_h264.h:154
uint8_t transform_8x8_mode_flag
Definition: cbs_h264.h:218
uint8_t constrained_intra_pred_flag
Definition: cbs_h264.h:213
uint32_t cpb_size_value_minus1[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:59
uint16_t frame_crop_left_offset
Definition: cbs_h264.h:158
static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)
static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define FF_PROFILE_H264_EXTENDED
Definition: avcodec.h:2900
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
VASurfaceID recon_surface
Definition: vaapi_encode.h:75
static const uint8_t header[24]
Definition: sdr2.c:67
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:229
uint16_t idr_pic_id
Definition: cbs_h264.h:360
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2170
#define FFALIGN(x, a)
Definition: macros.h:48
int8_t second_chroma_qp_index_offset
Definition: cbs_h264.h:225
#define av_log(a,...)
uint8_t cpb_size_scale
Definition: cbs_h264.h:56
uint8_t nal_hrd_parameters_present_flag
Definition: cbs_h264.h:94
uint8_t bit_depth_luma_minus8
Definition: cbs_h264.h:129
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
H.264 common definitions.
uint8_t pic_scaling_matrix_present_flag
Definition: cbs_h264.h:220
unsigned int va_rc_mode
Definition: vaapi_encode.h:139
uint8_t chroma_sample_loc_type_top_field
Definition: cbs_h264.h:86
buffering period (H.264, D.1.1)
Definition: h264_sei.h:28
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t aspect_ratio_info_present_flag
Definition: cbs_h264.h:69
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Definition: cbs.c:139
#define AVERROR(e)
Definition: error.h:43
uint8_t max_num_ref_frames
Definition: cbs_h264.h:147
uint8_t weighted_bipred_idc
Definition: cbs_h264.h:206
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const H264LevelDescriptor * ff_h264_guess_level(int profile_idc, int64_t bitrate, int width, int height, int max_dec_frame_buffering)
Guess the level of a stream from some parameters.
Definition: h264_levels.c:90
uint8_t transfer_characteristics
Definition: cbs_h264.h:82
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:197
CodedBitstreamContext * cbc
#define FF_PROFILE_H264_HIGH_422
Definition: avcodec.h:2905
#define FF_PROFILE_H264_HIGH
Definition: avcodec.h:2901
uint16_t sar_width
Definition: cbs_h264.h:71
uint32_t initial_cpb_removal_delay_offset[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:238
uint8_t slice_type
Definition: cbs_h264.h:350
uint8_t num_ref_idx_l1_default_active_minus1
Definition: cbs_h264.h:203
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3430
uint8_t frame_mbs_only_flag
Definition: cbs_h264.h:153
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:1837
static const AVCodecDefault defaults[]
Definition: amfenc_h264.c:361
unsigned int va_bit_rate
Definition: vaapi_encode.h:141
uint8_t seq_parameter_set_id
Definition: cbs_h264.h:125
uint8_t seq_scaling_matrix_present_flag
Definition: cbs_h264.h:133
static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
void * codec_picture_params
Definition: vaapi_encode.h:84
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:133
H264RawSEIPicTiming pic_timing
Definition: cbs_h264.h:323
#define fail()
Definition: checkasm.h:117
uint8_t timing_info_present_flag
Definition: cbs_h264.h:89
uint8_t more_rbsp_data
Definition: cbs_h264.h:215
H264RawSEIPicTiming sei_pic_timing
uint8_t video_full_range_flag
Definition: cbs_h264.h:79
uint8_t seq_parameter_set_id
Definition: cbs_h264.h:185
#define LEVEL(name, value)
H264RawSEIUserDataUnregistered user_data_unregistered
Definition: cbs_h264.h:327
common internal API header
AVCodec ff_h264_vaapi_encoder
uint8_t colour_primaries
Definition: cbs_h264.h:81
picture timing
Definition: h264_sei.h:29
static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
uint16_t sar_height
Definition: cbs_h264.h:72
uint16_t frame_crop_bottom_offset
Definition: cbs_h264.h:161
int width
picture width / height.
Definition: avcodec.h:1705
uint8_t vui_parameters_present_flag
Definition: cbs_h264.h:163
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2859
const VAAPIEncodeProfile * profiles
Definition: vaapi_encode.h:240
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
uint32_t cpb_removal_delay
Definition: cbs_h264.h:260
AVFormatContext * ctx
Definition: movenc.c:48
uint8_t colour_description_present_flag
Definition: cbs_h264.h:80
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2142
static const VAAPIEncodeProfile vaapi_encode_h264_profiles[]
uint8_t video_signal_type_present_flag
Definition: cbs_h264.h:77
int level
level
Definition: avcodec.h:2968
void * codec_picture_params
Definition: vaapi_encode.h:198
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:210
uint8_t constraint_set5_flag
Definition: cbs_h264.h:121
uint8_t pic_parameter_set_id
Definition: cbs_h264.h:352
uint8_t chroma_format_idc
Definition: cbs_h264.h:127
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
#define FF_ARRAY_ELEMS(a)
uint8_t profile_idc
Definition: cbs_h264.h:115
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:512
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:282
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:87
uint32_t bit_rate_value_minus1[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:58
uint8_t cpb_cnt_minus1
Definition: cbs_h264.h:54
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:113
uint8_t chroma_loc_info_present_flag
Definition: cbs_h264.h:85
uint8_t cbr_flag[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:60
const struct VAAPIEncodeType * codec
Definition: vaapi_encode.h:112
Libavcodec external API header.
#define FF_PROFILE_H264_HIGH_422_INTRA
Definition: avcodec.h:2906
int compression_level
Definition: avcodec.h:1604
uint8_t bottom_field_pic_order_in_frame_present_flag
Definition: cbs_h264.h:188
uint8_t entropy_coding_mode_flag
Definition: cbs_h264.h:187
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
VAAPIEncodeContext common
main external API structure.
Definition: avcodec.h:1532
GLint GLenum type
Definition: opengl_enc.c:105
#define FF_PROFILE_H264_HIGH_10_INTRA
Definition: avcodec.h:2903
static int FUNC() aud(CodedBitstreamContext *ctx, RWContext *rw, H264RawAUD *current)
uint8_t pic_order_cnt_type
Definition: cbs_h264.h:139
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:159
uint8_t matrix_coefficients
Definition: cbs_h264.h:83
uint8_t num_ref_idx_l0_default_active_minus1
Definition: cbs_h264.h:202
int index
Definition: gxfenc.c:89
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2156
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2149
uint16_t frame_num
Definition: cbs_h264.h:356
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
recovery point (frame # to decoder sync)
Definition: h264_sei.h:34
#define FF_PROFILE_H264_HIGH_444
Definition: avcodec.h:2908
uint8_t log2_max_mv_length_horizontal
Definition: cbs_h264.h:106
uint8_t direct_spatial_mv_pred_flag
Definition: cbs_h264.h:367
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:113
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1814
#define snprintf
Definition: snprintf.h:34
H264RawSEIBufferingPeriod buffering_period
Definition: cbs_h264.h:322
H264RawSEIPayload payload[H264_MAX_SEI_PAYLOADS]
Definition: cbs_h264.h:342
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
mfxU16 profile
Definition: qsvenc.c:44
CodedBitstreamFragment current_access_unit
#define VAAPI_ENCODE_COMMON_OPTIONS
Definition: vaapi_encode.h:306
uint8_t level
Definition: svq3.c:207
uint8_t direct_8x8_inference_flag
Definition: cbs_h264.h:155
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:237
int
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:217
uint8_t cpb_removal_delay_length_minus1
Definition: cbs_h264.h:63
uint8_t motion_vectors_over_pic_boundaries_flag
Definition: cbs_h264.h:103
uint32_t time_scale
Definition: cbs_h264.h:91
int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *input_image, int *got_packet)
Definition: vaapi_encode.c:851
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
uint8_t bitstream_restriction_flag
Definition: cbs_h264.h:102
uint8_t level_idc
Definition: cbs_h264.h:123
uint16_t frame_crop_right_offset
Definition: cbs_h264.h:159
int den
Denominator.
Definition: rational.h:60
uint8_t pic_parameter_set_id
Definition: cbs_h264.h:184
uint8_t max_num_reorder_frames
Definition: cbs_h264.h:108
H264RawSEIUserDataUnregistered sei_identifier
void * priv_data
Definition: avcodec.h:1559
#define PROFILE(name, value)
uint8_t log2_max_mv_length_vertical
Definition: cbs_h264.h:107
H264RawSliceHeader header
Definition: cbs_h264.h:425
uint8_t log2_max_pic_order_cnt_lsb_minus4
Definition: cbs_h264.h:140
int len
const char * name
Definition: h264_levels.h:26
static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
void * codec_slice_params
Definition: vaapi_encode.h:56
uint8_t delta_pic_order_always_zero_flag
Definition: cbs_h264.h:141
H264RawSEIBufferingPeriod sei_buffering_period
#define FF_PROFILE_H264_HIGH_444_INTRA
Definition: avcodec.h:2910
int8_t pic_init_qp_minus26
Definition: cbs_h264.h:208
#define LIBAVCODEC_IDENT
Definition: version.h:42
unsigned int desired_packed_headers
Definition: vaapi_encode.h:120
uint8_t nal_ref_idc
Definition: cbs_h264.h:42
#define av_freep(p)
uint8_t constraint_set1_flag
Definition: cbs_h264.h:117
uint16_t pic_height_in_map_units_minus1
Definition: cbs_h264.h:151
#define FF_PROFILE_H264_CONSTRAINED_BASELINE
Definition: avcodec.h:2898
VABufferID output_buffer
Definition: vaapi_encode.h:81
#define FF_PROFILE_H264_HIGH_10
Definition: avcodec.h:2902
uint8_t nal_unit_type
Definition: cbs_h264.h:43
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
uint8_t payload_count
Definition: cbs_h264.h:343
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
AVVAAPIDeviceContext * hwctx
Definition: vaapi_encode.h:154
uint8_t constraint_set3_flag
Definition: cbs_h264.h:119