FFmpeg  4.0
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_sei.h"
34 #include "internal.h"
35 #include "vaapi_encode.h"
36 
37 enum {
38  SEI_TIMING = 0x01,
41 };
42 
43 // Random (version 4) ISO 11578 UUID.
45  0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
46  0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
47 };
48 
49 typedef struct VAAPIEncodeH264Context {
50  int mb_width;
51  int mb_height;
52 
56 
62 
68 
69  int frame_num;
72  int64_t last_idr_frame;
73  int64_t idr_pic_count;
74 
77 
78  int cpb_delay;
79  int dpb_delay;
80 
87 
88 typedef struct VAAPIEncodeH264Options {
89  int qp;
90  int quality;
91  int low_power;
92  // Entropy encoder type.
93  int coder;
94  int aud;
95  int sei;
96  int profile;
97  int level;
99 
100 
102  char *data, size_t *data_len,
104 {
105  VAAPIEncodeContext *ctx = avctx->priv_data;
106  VAAPIEncodeH264Context *priv = ctx->priv_data;
107  int err;
108 
109  err = ff_cbs_write_fragment_data(priv->cbc, au);
110  if (err < 0) {
111  av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
112  return err;
113  }
114 
115  if (*data_len < 8 * au->data_size - au->data_bit_padding) {
116  av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
117  "%zu < %zu.\n", *data_len,
118  8 * au->data_size - au->data_bit_padding);
119  return AVERROR(ENOSPC);
120  }
121 
122  memcpy(data, au->data, au->data_size);
123  *data_len = 8 * au->data_size - au->data_bit_padding;
124 
125  return 0;
126 }
127 
130  void *nal_unit)
131 {
132  VAAPIEncodeContext *ctx = avctx->priv_data;
133  VAAPIEncodeH264Context *priv = ctx->priv_data;
134  H264RawNALUnitHeader *header = nal_unit;
135  int err;
136 
137  err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
138  header->nal_unit_type, nal_unit, NULL);
139  if (err < 0) {
140  av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
141  "type = %d.\n", header->nal_unit_type);
142  return err;
143  }
144 
145  return 0;
146 }
147 
149  char *data, size_t *data_len)
150 {
151  VAAPIEncodeContext *ctx = avctx->priv_data;
152  VAAPIEncodeH264Context *priv = ctx->priv_data;
154  int err;
155 
156  if (priv->aud_needed) {
157  err = vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
158  if (err < 0)
159  goto fail;
160  priv->aud_needed = 0;
161  }
162 
163  err = vaapi_encode_h264_add_nal(avctx, au, &priv->sps);
164  if (err < 0)
165  goto fail;
166 
167  err = vaapi_encode_h264_add_nal(avctx, au, &priv->pps);
168  if (err < 0)
169  goto fail;
170 
171  err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
172 fail:
173  ff_cbs_fragment_uninit(priv->cbc, au);
174  return err;
175 }
176 
178  VAAPIEncodePicture *pic,
180  char *data, size_t *data_len)
181 {
182  VAAPIEncodeContext *ctx = avctx->priv_data;
183  VAAPIEncodeH264Context *priv = ctx->priv_data;
185  int err;
186 
187  if (priv->aud_needed) {
188  err = vaapi_encode_h264_add_nal(avctx, au, &priv->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->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  VAAPIEncodeContext *ctx = avctx->priv_data;
210  VAAPIEncodeH264Context *priv = ctx->priv_data;
213  int err, i;
214 
215  if (priv->sei_needed) {
216  if (priv->aud_needed) {
217  err = vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
218  if (err < 0)
219  goto fail;
220  priv->aud_needed = 0;
221  }
222 
223  memset(&priv->sei, 0, sizeof(priv->sei));
225 
226  i = 0;
227  if (pic->encode_order == 0 && opt->sei & SEI_IDENTIFIER) {
230  ++i;
231  }
232  if (opt->sei & SEI_TIMING) {
233  if (pic->type == PICTURE_TYPE_IDR) {
236  ++i;
237  }
239  priv->sei.payload[i].payload.pic_timing = priv->pic_timing;
240  ++i;
241  }
242  if (opt->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
245  ++i;
246  }
247 
248  priv->sei.payload_count = i;
249  av_assert0(priv->sei.payload_count > 0);
250 
251  err = vaapi_encode_h264_add_nal(avctx, au, &priv->sei);
252  if (err < 0)
253  goto fail;
254  priv->sei_needed = 0;
255 
256  err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
257  if (err < 0)
258  goto fail;
259 
260  ff_cbs_fragment_uninit(priv->cbc, au);
261 
262  *type = VAEncPackedHeaderRawData;
263  return 0;
264 
265 #if !CONFIG_VAAPI_1
266  } else if (priv->sei_cbr_workaround_needed) {
267  // Insert a zero-length header using the old SEI type. This is
268  // required to avoid triggering broken behaviour on Intel platforms
269  // in CBR mode where an invalid SEI message is generated by the
270  // driver and inserted into the stream.
271  *data_len = 0;
272  *type = VAEncPackedHeaderH264_SEI;
273  priv->sei_cbr_workaround_needed = 0;
274  return 0;
275 #endif
276 
277  } else {
278  return AVERROR_EOF;
279  }
280 
281 fail:
282  ff_cbs_fragment_uninit(priv->cbc, au);
283  return err;
284 }
285 
287 {
288  VAAPIEncodeContext *ctx = avctx->priv_data;
289  VAAPIEncodeH264Context *priv = ctx->priv_data;
291  H264RawSPS *sps = &priv->sps;
292  H264RawPPS *pps = &priv->pps;
293  VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
294  VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
295 
296  memset(&priv->current_access_unit, 0,
297  sizeof(priv->current_access_unit));
298 
299  memset(sps, 0, sizeof(*sps));
300  memset(pps, 0, sizeof(*pps));
301 
302  sps->nal_unit_header.nal_ref_idc = 3;
304 
305  sps->profile_idc = avctx->profile & 0xff;
306  sps->constraint_set1_flag =
308  sps->constraint_set3_flag =
309  !!(avctx->profile & FF_PROFILE_H264_INTRA);
310 
311  sps->level_idc = avctx->level;
312 
313  sps->seq_parameter_set_id = 0;
314  sps->chroma_format_idc = 1;
315 
316  sps->log2_max_frame_num_minus4 = 4;
317  sps->pic_order_cnt_type = 0;
319  av_clip(av_log2(ctx->b_per_p + 1) - 2, 0, 12);
320 
321  sps->max_num_ref_frames =
322  (avctx->profile & FF_PROFILE_H264_INTRA) ? 0 :
323  1 + (ctx->b_per_p > 0);
324 
325  sps->pic_width_in_mbs_minus1 = priv->mb_width - 1;
326  sps->pic_height_in_map_units_minus1 = priv->mb_height - 1;
327 
328  sps->frame_mbs_only_flag = 1;
329  sps->direct_8x8_inference_flag = 1;
330 
331  if (avctx->width != 16 * priv->mb_width ||
332  avctx->height != 16 * priv->mb_height) {
333  sps->frame_cropping_flag = 1;
334 
335  sps->frame_crop_left_offset = 0;
337  (16 * priv->mb_width - avctx->width) / 2;
338  sps->frame_crop_top_offset = 0;
340  (16 * priv->mb_height - avctx->height) / 2;
341  } else {
342  sps->frame_cropping_flag = 0;
343  }
344 
346 
347  if (avctx->sample_aspect_ratio.num != 0 &&
348  avctx->sample_aspect_ratio.den != 0) {
349  static const AVRational sar_idc[] = {
350  { 0, 0 },
351  { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
352  { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
353  { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
354  { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
355  };
356  int i;
357  for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
358  if (avctx->sample_aspect_ratio.num == sar_idc[i].num &&
359  avctx->sample_aspect_ratio.den == sar_idc[i].den) {
360  sps->vui.aspect_ratio_idc = i;
361  break;
362  }
363  }
364  if (i >= FF_ARRAY_ELEMS(sar_idc)) {
365  sps->vui.aspect_ratio_idc = 255;
366  sps->vui.sar_width = avctx->sample_aspect_ratio.num;
367  sps->vui.sar_height = avctx->sample_aspect_ratio.den;
368  }
370  }
371 
372  if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
374  avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
375  avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
377  sps->vui.video_format = 5; // Unspecified.
379  avctx->color_range == AVCOL_RANGE_JPEG;
380 
381  if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
382  avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
383  avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
385  sps->vui.colour_primaries = avctx->color_primaries;
386  sps->vui.transfer_characteristics = avctx->color_trc;
387  sps->vui.matrix_coefficients = avctx->colorspace;
388  }
389  } else {
390  sps->vui.video_format = 5;
391  sps->vui.video_full_range_flag = 0;
392  sps->vui.colour_primaries = avctx->color_primaries;
393  sps->vui.transfer_characteristics = avctx->color_trc;
394  sps->vui.matrix_coefficients = avctx->colorspace;
395  }
396 
401  avctx->chroma_sample_location - 1;
402  }
403 
404  sps->vui.timing_info_present_flag = 1;
405  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
406  sps->vui.num_units_in_tick = avctx->framerate.den;
407  sps->vui.time_scale = 2 * avctx->framerate.num;
408  sps->vui.fixed_frame_rate_flag = 1;
409  } else {
410  sps->vui.num_units_in_tick = avctx->time_base.num;
411  sps->vui.time_scale = 2 * avctx->time_base.den;
412  sps->vui.fixed_frame_rate_flag = 0;
413  }
414 
415  if (opt->sei & SEI_TIMING) {
416  H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
417 
419 
420  hrd->cpb_cnt_minus1 = 0;
421 
422  // Try to scale these to a sensible range so that the
423  // golomb encode of the value is not overlong.
424  hrd->bit_rate_scale =
425  av_clip_uintp2(av_log2(avctx->bit_rate) - 15 - 6, 4);
426  hrd->bit_rate_value_minus1[0] =
427  (avctx->bit_rate >> hrd->bit_rate_scale + 6) - 1;
428 
429  hrd->cpb_size_scale =
430  av_clip_uintp2(av_log2(ctx->hrd_params.hrd.buffer_size) - 15 - 4, 4);
431  hrd->cpb_size_value_minus1[0] =
432  (ctx->hrd_params.hrd.buffer_size >> hrd->cpb_size_scale + 4) - 1;
433 
434  // CBR mode as defined for the HRD cannot be achieved without filler
435  // data, so this flag cannot be set even with VAAPI CBR modes.
436  hrd->cbr_flag[0] = 0;
437 
441  hrd->time_offset_length = 0;
442 
444 
445  // This calculation can easily overflow 32 bits.
447  (uint64_t)ctx->hrd_params.hrd.initial_buffer_fullness /
448  ctx->hrd_params.hrd.buffer_size;
450  } else {
453  }
454 
459  sps->vui.max_num_reorder_frames = (ctx->b_per_p > 0);
461 
462  pps->nal_unit_header.nal_ref_idc = 3;
464 
465  pps->pic_parameter_set_id = 0;
466  pps->seq_parameter_set_id = 0;
467 
472  if (!opt->coder && pps->entropy_coding_mode_flag)
473  pps->entropy_coding_mode_flag = 0;
474 
477 
478  pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
479 
483  pps->more_rbsp_data = 0;
484  } else {
485  pps->more_rbsp_data = 1;
486 
487  pps->transform_8x8_mode_flag = 1;
488  }
489 
490  *vseq = (VAEncSequenceParameterBufferH264) {
491  .seq_parameter_set_id = sps->seq_parameter_set_id,
492  .level_idc = sps->level_idc,
493  .intra_period = avctx->gop_size,
494  .intra_idr_period = avctx->gop_size,
495  .ip_period = ctx->b_per_p + 1,
496 
497  .bits_per_second = avctx->bit_rate,
498  .max_num_ref_frames = sps->max_num_ref_frames,
499  .picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1,
500  .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
501 
502  .seq_fields.bits = {
503  .chroma_format_idc = sps->chroma_format_idc,
504  .frame_mbs_only_flag = sps->frame_mbs_only_flag,
505  .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
506  .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
507  .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
508  .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
509  .pic_order_cnt_type = sps->pic_order_cnt_type,
510  .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
511  .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
512  },
513 
514  .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
515  .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
516 
517  .frame_cropping_flag = sps->frame_cropping_flag,
518  .frame_crop_left_offset = sps->frame_crop_left_offset,
519  .frame_crop_right_offset = sps->frame_crop_right_offset,
520  .frame_crop_top_offset = sps->frame_crop_top_offset,
521  .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
522 
523  .vui_parameters_present_flag = sps->vui_parameters_present_flag,
524 
525  .vui_fields.bits = {
526  .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
527  .timing_info_present_flag = sps->vui.timing_info_present_flag,
528  .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
529  .log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal,
530  .log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical,
531  },
532 
533  .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
534  .sar_width = sps->vui.sar_width,
535  .sar_height = sps->vui.sar_height,
536  .num_units_in_tick = sps->vui.num_units_in_tick,
537  .time_scale = sps->vui.time_scale,
538  };
539 
540  *vpic = (VAEncPictureParameterBufferH264) {
541  .CurrPic = {
542  .picture_id = VA_INVALID_ID,
543  .flags = VA_PICTURE_H264_INVALID,
544  },
545 
546  .coded_buf = VA_INVALID_ID,
547 
548  .pic_parameter_set_id = pps->pic_parameter_set_id,
549  .seq_parameter_set_id = pps->seq_parameter_set_id,
550 
551  .pic_init_qp = pps->pic_init_qp_minus26 + 26,
552  .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
553  .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
554 
555  .chroma_qp_index_offset = pps->chroma_qp_index_offset,
556  .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
557 
558  .pic_fields.bits = {
559  .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
560  .weighted_pred_flag = pps->weighted_pred_flag,
561  .weighted_bipred_idc = pps->weighted_bipred_idc,
562  .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
563  .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
564  .deblocking_filter_control_present_flag =
566  .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
567  .pic_order_present_flag =
569  .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
570  },
571  };
572 
573  return 0;
574 }
575 
577  VAAPIEncodePicture *pic)
578 {
579  VAAPIEncodeContext *ctx = avctx->priv_data;
580  VAAPIEncodeH264Context *priv = ctx->priv_data;
582  H264RawSPS *sps = &priv->sps;
583  VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
584  int i;
585 
586  memset(&priv->current_access_unit, 0,
587  sizeof(priv->current_access_unit));
588 
589  if (pic->type == PICTURE_TYPE_IDR) {
590  av_assert0(pic->display_order == pic->encode_order);
591  priv->frame_num = 0;
592  priv->next_frame_num = 1;
593  priv->cpb_delay = 0;
594  priv->last_idr_frame = pic->display_order;
595  ++priv->idr_pic_count;
596 
597  priv->slice_type = 7;
598  priv->primary_pic_type = 0;
599  } else {
600  priv->frame_num = priv->next_frame_num;
601 
602  if (pic->type != PICTURE_TYPE_B) {
603  // Reference picture, so frame_num advances.
604  priv->next_frame_num = (priv->frame_num + 1) &
605  ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
606  }
607  ++priv->cpb_delay;
608 
609  if (pic->type == PICTURE_TYPE_I) {
610  priv->slice_type = 7;
611  priv->primary_pic_type = 0;
612  } else if (pic->type == PICTURE_TYPE_P) {
613  priv->slice_type = 5;
614  priv->primary_pic_type = 1;
615  } else {
616  priv->slice_type = 6;
617  priv->primary_pic_type = 2;
618  }
619  }
620  priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
621  priv->dpb_delay = pic->display_order - pic->encode_order + 1;
622 
623  if (opt->aud) {
624  priv->aud_needed = 1;
626  priv->aud.primary_pic_type = priv->primary_pic_type;
627  } else {
628  priv->aud_needed = 0;
629  }
630 
631  if (opt->sei & SEI_IDENTIFIER && pic->encode_order == 0)
632  priv->sei_needed = 1;
633 #if !CONFIG_VAAPI_1
634  if (ctx->va_rc_mode == VA_RC_CBR)
635  priv->sei_cbr_workaround_needed = 1;
636 #endif
637 
638  if (opt->sei & SEI_TIMING) {
639  memset(&priv->pic_timing, 0, sizeof(priv->pic_timing));
640 
641  priv->pic_timing.cpb_removal_delay = 2 * priv->cpb_delay;
642  priv->pic_timing.dpb_output_delay = 2 * priv->dpb_delay;
643 
644  priv->sei_needed = 1;
645  }
646 
647  if (opt->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
650  priv->recovery_point.broken_link_flag = ctx->b_per_p > 0;
651 
652  priv->sei_needed = 1;
653  }
654 
655  vpic->CurrPic = (VAPictureH264) {
656  .picture_id = pic->recon_surface,
657  .frame_idx = priv->frame_num,
658  .flags = 0,
659  .TopFieldOrderCnt = priv->pic_order_cnt,
660  .BottomFieldOrderCnt = priv->pic_order_cnt,
661  };
662 
663  for (i = 0; i < pic->nb_refs; i++) {
664  VAAPIEncodePicture *ref = pic->refs[i];
665  unsigned int frame_num = (ref->encode_order - priv->last_idr_frame) &
666  ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
667  unsigned int pic_order_cnt = ref->display_order - priv->last_idr_frame;
668 
669  av_assert0(ref && ref->encode_order < pic->encode_order);
670  vpic->ReferenceFrames[i] = (VAPictureH264) {
671  .picture_id = ref->recon_surface,
672  .frame_idx = frame_num,
673  .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
674  .TopFieldOrderCnt = pic_order_cnt,
675  .BottomFieldOrderCnt = pic_order_cnt,
676  };
677  }
678  for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
679  vpic->ReferenceFrames[i] = (VAPictureH264) {
680  .picture_id = VA_INVALID_ID,
681  .flags = VA_PICTURE_H264_INVALID,
682  };
683  }
684 
685  vpic->coded_buf = pic->output_buffer;
686 
687  vpic->frame_num = priv->frame_num;
688 
689  vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
690  vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
691 
692  pic->nb_slices = 1;
693 
694  return 0;
695 }
696 
698  VAAPIEncodePicture *pic,
700 {
701  VAAPIEncodeContext *ctx = avctx->priv_data;
702  VAAPIEncodeH264Context *priv = ctx->priv_data;
703  H264RawSPS *sps = &priv->sps;
704  H264RawPPS *pps = &priv->pps;
705  H264RawSliceHeader *sh = &priv->slice.header;
706  VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
707  VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
708  int i;
709 
710  if (pic->type == PICTURE_TYPE_IDR) {
713  } else {
716  }
717 
718  // Only one slice per frame.
719  sh->first_mb_in_slice = 0;
720  sh->slice_type = priv->slice_type;
721 
723 
724  sh->frame_num = priv->frame_num;
725  sh->idr_pic_id = priv->idr_pic_count;
726 
727  sh->pic_order_cnt_lsb = priv->pic_order_cnt &
728  ((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
729 
731 
732  if (pic->type == PICTURE_TYPE_B)
733  sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26);
734  else if (pic->type == PICTURE_TYPE_P)
735  sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26);
736  else
737  sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26);
738 
739 
740  vslice->macroblock_address = sh->first_mb_in_slice;
741  vslice->num_macroblocks = priv->mb_width * priv->mb_height;
742 
743  vslice->macroblock_info = VA_INVALID_ID;
744 
745  vslice->slice_type = sh->slice_type % 5;
746  vslice->pic_parameter_set_id = sh->pic_parameter_set_id;
747  vslice->idr_pic_id = sh->idr_pic_id;
748 
749  vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb;
750 
751  vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag;
752 
753  for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
754  vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
755  vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
756  vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
757  vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
758  }
759 
760  av_assert0(pic->nb_refs <= 2);
761  if (pic->nb_refs >= 1) {
762  // Backward reference for P- or B-frame.
763  av_assert0(pic->type == PICTURE_TYPE_P ||
764  pic->type == PICTURE_TYPE_B);
765  vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
766  }
767  if (pic->nb_refs >= 2) {
768  // Forward reference for B-frame.
769  av_assert0(pic->type == PICTURE_TYPE_B);
770  vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
771  }
772 
773  vslice->slice_qp_delta = sh->slice_qp_delta;
774 
775  return 0;
776 }
777 
779 {
780  VAAPIEncodeContext *ctx = avctx->priv_data;
781  VAAPIEncodeH264Context *priv = ctx->priv_data;
783  int err;
784 
785  err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
786  if (err < 0)
787  return err;
788 
789  priv->mb_width = FFALIGN(avctx->width, 16) / 16;
790  priv->mb_height = FFALIGN(avctx->height, 16) / 16;
791 
792  if (ctx->va_rc_mode == VA_RC_CQP) {
793  priv->fixed_qp_p = opt->qp;
794  if (avctx->i_quant_factor > 0.0)
795  priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
796  avctx->i_quant_offset) + 0.5);
797  else
798  priv->fixed_qp_idr = priv->fixed_qp_p;
799  if (avctx->b_quant_factor > 0.0)
800  priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
801  avctx->b_quant_offset) + 0.5);
802  else
803  priv->fixed_qp_b = priv->fixed_qp_p;
804 
805  opt->sei &= ~SEI_TIMING;
806 
807  av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
808  "%d / %d / %d for IDR- / P- / B-frames.\n",
809  priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
810 
811  } else if (ctx->va_rc_mode == VA_RC_CBR ||
812  ctx->va_rc_mode == VA_RC_VBR) {
813  // These still need to be set for pic_init_qp/slice_qp_delta.
814  priv->fixed_qp_idr = 26;
815  priv->fixed_qp_p = 26;
816  priv->fixed_qp_b = 26;
817 
818  av_log(avctx, AV_LOG_DEBUG, "Using %s-bitrate = %"PRId64" bps.\n",
819  ctx->va_rc_mode == VA_RC_CBR ? "constant" : "variable",
820  avctx->bit_rate);
821 
822  } else {
823  av_assert0(0 && "Invalid RC mode.");
824  }
825 
827  avctx->compression_level = opt->quality;
828 
829  if (opt->sei & SEI_IDENTIFIER) {
830  const char *lavc = LIBAVCODEC_IDENT;
831  const char *vaapi = VA_VERSION_S;
832  const char *driver;
833  int len;
834 
835  memcpy(priv->identifier.uuid_iso_iec_11578,
837  sizeof(priv->identifier.uuid_iso_iec_11578));
838 
839  driver = vaQueryVendorString(ctx->hwctx->display);
840  if (!driver)
841  driver = "unknown driver";
842 
843  len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver);
844  if (len >= 0) {
845  priv->identifier_string = av_malloc(len + 1);
846  if (!priv->identifier_string)
847  return AVERROR(ENOMEM);
848 
849  snprintf(priv->identifier_string, len + 1,
850  "%s / VAAPI %s / %s", lavc, vaapi, driver);
851 
852  priv->identifier.data = priv->identifier_string;
853  priv->identifier.data_length = len + 1;
854  }
855  }
856 
857  return 0;
858 }
859 
862 
863  .configure = &vaapi_encode_h264_configure,
864 
865  .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
866  .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
867 
868  .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
869  .init_picture_params = &vaapi_encode_h264_init_picture_params,
870 
871  .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
872  .init_slice_params = &vaapi_encode_h264_init_slice_params,
873 
874  .sequence_header_type = VAEncPackedHeaderSequence,
875  .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
876 
877  .slice_header_type = VAEncPackedHeaderH264_Slice,
878  .write_slice_header = &vaapi_encode_h264_write_slice_header,
879 
880  .write_extra_header = &vaapi_encode_h264_write_extra_header,
881 };
882 
884 {
885  VAAPIEncodeContext *ctx = avctx->priv_data;
888 
889  ctx->codec = &vaapi_encode_type_h264;
890 
891  if (avctx->profile == FF_PROFILE_UNKNOWN)
892  avctx->profile = opt->profile;
893  if (avctx->level == FF_LEVEL_UNKNOWN)
894  avctx->level = opt->level;
895 
896  switch (avctx->profile) {
898  av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
899  "supported, using constrained baseline profile instead.\n");
902  ctx->va_profile = VAProfileH264ConstrainedBaseline;
903  if (avctx->max_b_frames != 0) {
904  avctx->max_b_frames = 0;
905  av_log(avctx, AV_LOG_WARNING, "H.264 constrained baseline profile "
906  "doesn't support encoding with B frames, disabling them.\n");
907  }
908  break;
910  ctx->va_profile = VAProfileH264Main;
911  break;
913  av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
914  "is not supported.\n");
915  return AVERROR_PATCHWELCOME;
916  case FF_PROFILE_UNKNOWN:
918  ctx->va_profile = VAProfileH264High;
919  break;
922  av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
923  "are not supported.\n");
924  return AVERROR_PATCHWELCOME;
931  av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
932  "are not supported.\n");
933  return AVERROR_PATCHWELCOME;
934  default:
935  av_log(avctx, AV_LOG_ERROR, "Unknown H.264 profile %d.\n",
936  avctx->profile);
937  return AVERROR(EINVAL);
938  }
939  if (opt->low_power) {
940 #if VA_CHECK_VERSION(0, 39, 2)
941  ctx->va_entrypoint = VAEntrypointEncSliceLP;
942 #else
943  av_log(avctx, AV_LOG_ERROR, "Low-power encoding is not "
944  "supported with this VAAPI version.\n");
945  return AVERROR(EINVAL);
946 #endif
947  } else {
948  ctx->va_entrypoint = VAEntrypointEncSlice;
949  }
950 
951  // Only 8-bit encode is supported.
952  ctx->va_rt_format = VA_RT_FORMAT_YUV420;
953 
954  if (avctx->bit_rate > 0) {
955  if (avctx->rc_max_rate == avctx->bit_rate)
956  ctx->va_rc_mode = VA_RC_CBR;
957  else
958  ctx->va_rc_mode = VA_RC_VBR;
959  } else
960  ctx->va_rc_mode = VA_RC_CQP;
961 
962  ctx->va_packed_headers =
963  VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
964  VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
965  VA_ENC_PACKED_HEADER_MISC; // SEI.
966 
967  ctx->surface_width = FFALIGN(avctx->width, 16);
968  ctx->surface_height = FFALIGN(avctx->height, 16);
969 
970  return ff_vaapi_encode_init(avctx);
971 }
972 
974 {
975  VAAPIEncodeContext *ctx = avctx->priv_data;
976  VAAPIEncodeH264Context *priv = ctx->priv_data;
977 
978  if (priv) {
979  ff_cbs_close(&priv->cbc);
980  av_freep(&priv->identifier_string);
981  }
982 
983  return ff_vaapi_encode_close(avctx);
984 }
985 
986 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
987  offsetof(VAAPIEncodeH264Options, x))
988 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
990  { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
991  OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
992  { "quality", "Set encode quality (trades off against speed, higher is faster)",
993  OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8, FLAGS },
994  { "low_power", "Use low-power encoding mode (experimental: only supported "
995  "on some platforms, does not support all features)",
996  OFFSET(low_power), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
997  { "coder", "Entropy coder type",
998  OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
999  { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1000  { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1001  { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1002  { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1003 
1004  { "aud", "Include AUD",
1005  OFFSET(aud), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
1006 
1007  { "sei", "Set SEI to include",
1010  0, INT_MAX, FLAGS, "sei" },
1011  { "identifier", "Include encoder version identifier",
1012  0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
1013  INT_MIN, INT_MAX, FLAGS, "sei" },
1014  { "timing", "Include timing parameters (buffering_period and pic_timing)",
1015  0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
1016  INT_MIN, INT_MAX, FLAGS, "sei" },
1017  { "recovery_point", "Include recovery points where appropriate",
1018  0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
1019  INT_MIN, INT_MAX, FLAGS, "sei" },
1020 
1021  { "profile", "Set profile (profile_idc and constraint_set*_flag)",
1023  { .i64 = FF_PROFILE_H264_HIGH }, 0x0000, 0xffff, FLAGS, "profile" },
1024 
1025 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1026  { .i64 = value }, 0, 0, FLAGS, "profile"
1027  { PROFILE("constrained_baseline", FF_PROFILE_H264_CONSTRAINED_BASELINE) },
1028  { PROFILE("main", FF_PROFILE_H264_MAIN) },
1029  { PROFILE("high", FF_PROFILE_H264_HIGH) },
1030 #undef PROFILE
1031 
1032  { "level", "Set level (level_idc)",
1034  { .i64 = 51 }, 0x00, 0xff, FLAGS, "level" },
1035 
1036 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1037  { .i64 = value }, 0, 0, FLAGS, "level"
1038  { LEVEL("1", 10) },
1039  { LEVEL("1.1", 11) },
1040  { LEVEL("1.2", 12) },
1041  { LEVEL("1.3", 13) },
1042  { LEVEL("2", 20) },
1043  { LEVEL("2.1", 21) },
1044  { LEVEL("2.2", 22) },
1045  { LEVEL("3", 30) },
1046  { LEVEL("3.1", 31) },
1047  { LEVEL("3.2", 32) },
1048  { LEVEL("4", 40) },
1049  { LEVEL("4.1", 41) },
1050  { LEVEL("4.2", 42) },
1051  { LEVEL("5", 50) },
1052  { LEVEL("5.1", 51) },
1053  { LEVEL("5.2", 52) },
1054  { LEVEL("6", 60) },
1055  { LEVEL("6.1", 61) },
1056  { LEVEL("6.2", 62) },
1057 #undef LEVEL
1058 
1059  { NULL },
1060 };
1061 
1063  { "b", "0" },
1064  { "bf", "2" },
1065  { "g", "120" },
1066  { "i_qfactor", "1" },
1067  { "i_qoffset", "0" },
1068  { "b_qfactor", "6/5" },
1069  { "b_qoffset", "0" },
1070  { "qmin", "0" },
1071  { NULL },
1072 };
1073 
1075  .class_name = "h264_vaapi",
1076  .item_name = av_default_item_name,
1077  .option = vaapi_encode_h264_options,
1078  .version = LIBAVUTIL_VERSION_INT,
1079 };
1080 
1082  .name = "h264_vaapi",
1083  .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
1084  .type = AVMEDIA_TYPE_VIDEO,
1085  .id = AV_CODEC_ID_H264,
1086  .priv_data_size = (sizeof(VAAPIEncodeContext) +
1087  sizeof(VAAPIEncodeH264Options)),
1089  .encode2 = &ff_vaapi_encode2,
1090  .close = &vaapi_encode_h264_close,
1091  .priv_class = &vaapi_encode_h264_class,
1092  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
1093  .defaults = vaapi_encode_h264_defaults,
1094  .pix_fmts = (const enum AVPixelFormat[]) {
1097  },
1098  .wrapper_name = "vaapi",
1099 };
#define FF_PROFILE_H264_MAIN
Definition: avcodec.h:2884
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:299
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3040
static const VAAPIEncodeType vaapi_encode_type_h264
#define FF_PROFILE_H264_CAVLC_444
Definition: avcodec.h:2896
uint8_t bit_depth_chroma_minus8
Definition: cbs_h264.h:130
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:1591
uint8_t aspect_ratio_idc
Definition: cbs_h264.h:70
VAProfile va_profile
Definition: vaapi_encode.h:96
static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
VAEntrypoint va_entrypoint
Definition: vaapi_encode.h:98
uint8_t video_format
Definition: cbs_h264.h:78
AVOption.
Definition: opt.h:246
uint8_t bit_rate_scale
Definition: cbs_h264.h:55
uint16_t recovery_frame_cnt
Definition: cbs_h264.h:283
uint8_t dpb_output_delay_length_minus1
Definition: cbs_h264.h:64
uint8_t initial_cpb_removal_delay_length_minus1
Definition: cbs_h264.h:62
char codec_options_data[0]
Definition: vaapi_encode.h:213
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1568
#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
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:1777
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition: avcodec.h:1056
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:56
struct H264RawSEIBufferingPeriod::@41 nal
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:318
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2148
H264RawSEIRecoveryPoint recovery_point
Definition: cbs_h264.h:307
#define FF_PROFILE_H264_INTRA
Definition: avcodec.h:2880
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
size_t priv_data_size
Definition: vaapi_encode.h:218
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:518
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:1896
uint16_t pic_order_cnt_lsb
Definition: cbs_h264.h:340
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:168
VAEncMiscParameterHRD hrd
Definition: vaapi_encode.h:154
#define FLAGS
#define OFFSET(x)
uint8_t max_dec_frame_buffering
Definition: cbs_h264.h:109
int profile
profile
Definition: avcodec.h:2843
uint8_t fixed_frame_rate_flag
Definition: cbs_h264.h:92
H264RawVUI vui
Definition: cbs_h264.h:164
AVCodec.
Definition: avcodec.h:3408
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1829
uint32_t first_mb_in_slice
Definition: cbs_h264.h:327
static const AVClass vaapi_encode_h264_class
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: avcodec.h:2894
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1640
unsigned int va_packed_headers
Definition: vaapi_encode.h:105
uint8_t weighted_pred_flag
Definition: cbs_h264.h:205
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:2954
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:984
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:390
static const AVOption vaapi_encode_h264_options[]
#define FF_PROFILE_H264_BASELINE
Definition: avcodec.h:2882
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
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:1786
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:325
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:32
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)
const char data[16]
Definition: mxf.c:90
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:2885
VASurfaceID recon_surface
Definition: vaapi_encode.h:71
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:338
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2155
#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:127
uint8_t primary_pic_type
Definition: cbs_h264.h:231
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:102
uint8_t chroma_sample_loc_type_top_field
Definition: cbs_h264.h:86
union H264RawSEIPayload::@42 payload
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:121
#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
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:2890
#define FF_PROFILE_H264_HIGH
Definition: avcodec.h:2886
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:328
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:3415
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:1822
static const AVCodecDefault defaults[]
Definition: amfenc_h264.c:361
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:80
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:131
H264RawSEIPicTiming pic_timing
Definition: cbs_h264.h:303
#define fail()
Definition: checkasm.h:116
uint8_t timing_info_present_flag
Definition: cbs_h264.h:89
uint8_t more_rbsp_data
Definition: cbs_h264.h:215
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:306
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:1690
uint8_t vui_parameters_present_flag
Definition: cbs_h264.h:163
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2844
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
H264RawSEIPicTiming pic_timing
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:2127
unsigned int va_rt_format
Definition: vaapi_encode.h:100
uint32_t dpb_output_delay
Definition: cbs_h264.h:261
uint8_t video_signal_type_present_flag
Definition: cbs_h264.h:77
int level
level
Definition: avcodec.h:2953
void * codec_picture_params
Definition: vaapi_encode.h:172
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
if(ret< 0)
Definition: vf_mcdeint.c:279
uint8_t pic_parameter_set_id
Definition: cbs_h264.h:330
uint8_t chroma_format_idc
Definition: cbs_h264.h:127
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:120
#define FF_ARRAY_ELEMS(a)
uint8_t profile_idc
Definition: cbs_h264.h:115
#define av_log2
Definition: intmath.h:83
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:499
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:261
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:83
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:93
Libavcodec external API header.
#define FF_PROFILE_H264_HIGH_422_INTRA
Definition: avcodec.h:2891
int compression_level
Definition: avcodec.h:1590
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:114
main external API structure.
Definition: avcodec.h:1518
#define FF_PROFILE_H264_HIGH_10_INTRA
Definition: avcodec.h:2888
uint8_t pic_order_cnt_type
Definition: cbs_h264.h:139
Describe the class of an AVClass context structure.
Definition: log.h:67
Context structure for coded bitstream operations.
Definition: cbs.h:156
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:2141
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2134
uint16_t frame_num
Definition: cbs_h264.h:334
cl_device_type type
recovery point (frame # to decoder sync)
Definition: h264_sei.h:33
#define FF_PROFILE_H264_HIGH_444
Definition: avcodec.h:2893
uint8_t log2_max_mv_length_horizontal
Definition: cbs_h264.h:106
uint8_t direct_spatial_mv_pred_flag
Definition: cbs_h264.h:345
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:95
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1799
#define snprintf
Definition: snprintf.h:34
H264RawSEIBufferingPeriod buffering_period
Definition: cbs_h264.h:302
H264RawSEIPayload payload[H264_MAX_SEI_PAYLOADS]
Definition: cbs_h264.h:320
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
mfxU16 profile
Definition: qsvenc.c:44
CodedBitstreamFragment current_access_unit
uint8_t level
Definition: svq3.c:207
uint8_t direct_8x8_inference_flag
Definition: cbs_h264.h:155
struct VAAPIEncodeContext::@136 hrd_params
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1712
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
H264RawSEIUserDataUnregistered identifier
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:853
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
void * priv_data
Definition: avcodec.h:1545
#define PROFILE(name, value)
uint8_t log2_max_mv_length_vertical
Definition: cbs_h264.h:107
H264RawSliceHeader header
Definition: cbs_h264.h:403
H264RawSEIBufferingPeriod buffering_period
uint8_t log2_max_pic_order_cnt_lsb_minus4
Definition: cbs_h264.h:140
int len
static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
void * codec_slice_params
Definition: vaapi_encode.h:52
uint8_t delta_pic_order_always_zero_flag
Definition: cbs_h264.h:141
#define FF_PROFILE_H264_HIGH_444_INTRA
Definition: avcodec.h:2895
int8_t pic_init_qp_minus26
Definition: cbs_h264.h:208
#define LIBAVCODEC_IDENT
Definition: version.h:42
uint8_t nal_ref_idc
Definition: cbs_h264.h:42
#define av_freep(p)
uint8_t constraint_set1_flag
Definition: cbs_h264.h:117
#define FF_PROFILE_H264_CONSTRAINED
Definition: avcodec.h:2879
uint16_t pic_height_in_map_units_minus1
Definition: cbs_h264.h:151
#define FF_PROFILE_H264_CONSTRAINED_BASELINE
Definition: avcodec.h:2883
VABufferID output_buffer
Definition: vaapi_encode.h:77
#define FF_PROFILE_H264_HIGH_10
Definition: avcodec.h:2887
uint8_t nal_unit_type
Definition: cbs_h264.h:43
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
uint8_t payload_count
Definition: cbs_h264.h:321
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
H264RawSEIRecoveryPoint recovery_point
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2391
AVVAAPIDeviceContext * hwctx
Definition: vaapi_encode.h:128
uint8_t constraint_set3_flag
Definition: cbs_h264.h:119