FFmpeg
vulkan_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 "libavutil/opt.h"
20 #include "libavutil/mem.h"
21 
22 #include "cbs.h"
23 #include "cbs_h264.h"
24 #include "atsc_a53.h"
25 
26 #include "h264_levels.h"
27 #include "h2645data.h"
28 #include "codec_internal.h"
29 #include "version.h"
30 #include "hw_base_encode_h264.h"
31 
32 #include "vulkan_encode.h"
33 
34 enum UnitElems {
35  UNIT_AUD = 1 << 0,
36  UNIT_SEI_TIMING = 1 << 1,
39  UNIT_SEI_A53_CC = 1 << 4,
40 };
41 
44  .encode_extension = FF_VK_EXT_VIDEO_ENCODE_H264,
45  .encode_op = VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR,
46  .ext_props = {
47  .extensionName = VK_STD_VULKAN_VIDEO_CODEC_H264_ENCODE_EXTENSION_NAME,
48  .specVersion = VK_STD_VULKAN_VIDEO_CODEC_H264_ENCODE_SPEC_VERSION,
49  },
50 };
51 
52 /* Random (version 4) ISO 11578 UUID. */
53 static const uint8_t vulkan_encode_h264_sei_identifier_uuid[16] = {
54  0x03, 0xfd, 0xf2, 0x0a, 0x5d, 0x4c, 0x05, 0x48,
55  0x20, 0x98, 0xca, 0x6b, 0x0c, 0x95, 0x30, 0x1c,
56 };
57 
58 typedef struct VulkanEncodeH264Picture {
59  int frame_num;
61  uint16_t idr_pic_id;
65 
67 
68  VkVideoEncodeH264RateControlInfoKHR vkrc_info;
69  VkVideoEncodeH264RateControlLayerInfoKHR vkrc_layer_info;
70  VkVideoEncodeH264GopRemainingFrameInfoKHR vkrc_remaining;
71 
72  StdVideoEncodeH264WeightTable slice_wt;
73  StdVideoEncodeH264SliceHeader slice_hdr;
74  VkVideoEncodeH264NaluSliceInfoKHR vkslice;
75 
76  StdVideoEncodeH264PictureInfo h264pic_info;
77  VkVideoEncodeH264PictureInfoKHR vkh264pic_info;
78 
79  StdVideoEncodeH264ReferenceInfo h264dpb_info;
80  VkVideoEncodeH264DpbSlotInfoKHR vkh264dpb_info;
81 
82  StdVideoEncodeH264RefListModEntry mods[MAX_REFERENCE_LIST_NUM][H264_MAX_RPLM_COUNT];
83  StdVideoEncodeH264RefPicMarkingEntry mmco[H264_MAX_RPLM_COUNT];
84  StdVideoEncodeH264ReferenceListsInfo ref_list_info;
86 
87 typedef struct VulkanEncodeH264Context {
89 
92 
94 
95  uint8_t fixed_qp_p;
96  uint8_t fixed_qp_b;
97 
98  VkVideoEncodeH264ProfileInfoKHR profile;
99 
100  VkVideoEncodeH264CapabilitiesKHR caps;
101  VkVideoEncodeH264QualityLevelPropertiesKHR quality_props;
102 
105 
107 
115 
117  VkVideoEncodeRateControlInfoKHR *rc_info,
118  VkVideoEncodeRateControlLayerInfoKHR *rc_layer)
119 {
120  VulkanEncodeH264Context *enc = avctx->priv_data;
123 
124  hp->vkrc_info = (VkVideoEncodeH264RateControlInfoKHR) {
125  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_RATE_CONTROL_INFO_KHR,
126  .flags = VK_VIDEO_ENCODE_H264_RATE_CONTROL_REFERENCE_PATTERN_FLAT_BIT_KHR |
127  VK_VIDEO_ENCODE_H264_RATE_CONTROL_REGULAR_GOP_BIT_KHR,
128  .idrPeriod = ctx->base.gop_size,
129  .gopFrameCount = ctx->base.gop_size,
130  .consecutiveBFrameCount = FFMAX(ctx->base.b_per_p - 1, 0),
131  .temporalLayerCount = 0,
132  };
133  rc_info->pNext = &hp->vkrc_info;
134 
135  if (rc_info->rateControlMode > VK_VIDEO_ENCODE_RATE_CONTROL_MODE_DISABLED_BIT_KHR) {
136  rc_info->virtualBufferSizeInMs = (enc->unit_opts.hrd_buffer_size * 1000LL) / avctx->bit_rate;
137  rc_info->initialVirtualBufferSizeInMs = (enc->unit_opts.initial_buffer_fullness * 1000LL) / avctx->bit_rate;
138 
139  hp->vkrc_layer_info = (VkVideoEncodeH264RateControlLayerInfoKHR) {
140  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_RATE_CONTROL_LAYER_INFO_KHR,
141 
142  .useMinQp = avctx->qmin > 0,
143  .minQp.qpI = avctx->qmin > 0 ? avctx->qmin : 0,
144  .minQp.qpP = avctx->qmin > 0 ? avctx->qmin : 0,
145  .minQp.qpB = avctx->qmin > 0 ? avctx->qmin : 0,
146 
147  .useMaxQp = avctx->qmax > 0,
148  .maxQp.qpI = avctx->qmax > 0 ? avctx->qmax : 0,
149  .maxQp.qpP = avctx->qmax > 0 ? avctx->qmax : 0,
150  .maxQp.qpB = avctx->qmax > 0 ? avctx->qmax : 0,
151 
152  .useMaxFrameSize = 0,
153  };
154  rc_layer->pNext = &hp->vkrc_layer_info;
155  hp->vkrc_info.temporalLayerCount = 1;
156  }
157 
158  return 0;
159 }
160 
163 {
164  VulkanEncodeH264Context *enc = avctx->priv_data;
167  FFHWBaseEncodePicture *prev = pic->prev;
168  VulkanEncodeH264Picture *hprev = prev ? prev->codec_priv : NULL;
169 
170  if (pic->type == FF_HW_PICTURE_TYPE_IDR) {
171  av_assert0(pic->display_order == pic->encode_order);
172 
173  hp->frame_num = 0;
174  hp->last_idr_frame = pic->display_order;
175  hp->idr_pic_id = hprev ? hprev->idr_pic_id + 1 : 0;
176 
177  hp->primary_pic_type = 0;
178  hp->slice_type = STD_VIDEO_H264_SLICE_TYPE_I;
179  } else {
180  av_assert0(prev);
181 
182  hp->frame_num = hprev->frame_num + prev->is_reference;
183 
184  hp->last_idr_frame = hprev->last_idr_frame;
185  hp->idr_pic_id = hprev->idr_pic_id;
186 
187  if (pic->type == FF_HW_PICTURE_TYPE_I) {
188  hp->slice_type = STD_VIDEO_H264_SLICE_TYPE_I;
189  hp->primary_pic_type = 0;
190  } else if (pic->type == FF_HW_PICTURE_TYPE_P) {
191  hp->slice_type = STD_VIDEO_H264_SLICE_TYPE_P;
192  hp->primary_pic_type = 1;
193  } else {
194  hp->slice_type = STD_VIDEO_H264_SLICE_TYPE_B;
195  hp->primary_pic_type = 2;
196  }
197  }
198 
199  hp->pic_order_cnt = pic->display_order - hp->last_idr_frame;
200  if (enc->units.raw_sps.pic_order_cnt_type == 2)
201  hp->pic_order_cnt *= 2;
202 
203  hp->units_needed = 0;
204 
205  if (enc->unit_elems & UNIT_SEI_IDENTIFIER && pic->encode_order == 0)
207 
208  if (enc->unit_elems & UNIT_SEI_TIMING) {
210  .cpb_removal_delay = 2 * (pic->encode_order - hp->last_idr_frame),
211  .dpb_output_delay = 2 * (pic->display_order - pic->encode_order + ctx->base.max_b_depth),
212  };
213 
215  }
216 
217  if (enc->unit_elems & UNIT_SEI_RECOVERY && pic->type == FF_HW_PICTURE_TYPE_I) {
219  .recovery_frame_cnt = 0,
220  .exact_match_flag = 1,
221  .broken_link_flag = ctx->base.b_per_p > 0,
222  };
223 
225  }
226 
227  if (enc->unit_elems & UNIT_SEI_A53_CC) {
228  int err;
229  size_t sei_a53cc_len;
230  av_freep(&enc->sei_a53cc_data);
231  err = ff_alloc_a53_sei(pic->input_image, 0, &enc->sei_a53cc_data, &sei_a53cc_len);
232  if (err < 0)
233  return err;
234  if (enc->sei_a53cc_data != NULL) {
236  enc->sei_a53cc.data = (uint8_t *)enc->sei_a53cc_data + 1;
237  enc->sei_a53cc.data_length = sei_a53cc_len - 1;
238 
240  }
241  }
242 
243  return 0;
244 }
245 
246 static void setup_slices(AVCodecContext *avctx,
248 {
249  VulkanEncodeH264Context *enc = avctx->priv_data;
251 
252  hp->slice_wt = (StdVideoEncodeH264WeightTable) {
253  .flags = (StdVideoEncodeH264WeightTableFlags) {
254  .luma_weight_l0_flag = 0,
255  .chroma_weight_l0_flag = 0,
256  .luma_weight_l1_flag = 0,
257  .chroma_weight_l1_flag = 0,
258  },
259  .luma_log2_weight_denom = 0,
260  .chroma_log2_weight_denom = 0,
261  .luma_weight_l0 = { 0 },
262  .luma_offset_l0 = { 0 },
263  .chroma_weight_l0 = { { 0 } },
264  .chroma_offset_l0 = { { 0 } },
265  .luma_weight_l1 = { 0 },
266  .luma_offset_l1 = { 0 },
267  .chroma_weight_l1 = { { 0 } },
268  .chroma_offset_l1 = { { 0 } },
269  };
270 
271  hp->slice_hdr = (StdVideoEncodeH264SliceHeader) {
272  .flags = (StdVideoEncodeH264SliceHeaderFlags) {
273  .direct_spatial_mv_pred_flag = 1,
274  /* The vk_samples code does this */
275  .num_ref_idx_active_override_flag =
277  (pic->type == FF_HW_PICTURE_TYPE_B)) ? 1 : 0,
278  },
279  .first_mb_in_slice = 1,
280  .slice_type = hp->slice_type,
281  .slice_alpha_c0_offset_div2 = 0,
282  .slice_beta_offset_div2 = 0,
283  .slice_qp_delta = 0, /* Filled in below */
284  /* Reserved */
285  .cabac_init_idc = 0,
286  .disable_deblocking_filter_idc = 0,
287  .pWeightTable = NULL, // &hp->slice_wt,
288  };
289 
290  hp->vkslice = (VkVideoEncodeH264NaluSliceInfoKHR) {
291  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_NALU_SLICE_INFO_KHR,
292  .pNext = NULL,
293  .constantQp = pic->type == FF_HW_PICTURE_TYPE_B ? enc->fixed_qp_b :
294  pic->type == FF_HW_PICTURE_TYPE_P ? enc->fixed_qp_p :
295  enc->unit_opts.fixed_qp_idr,
296  .pStdSliceHeader = &hp->slice_hdr,
297  };
298 
299  if (enc->common.opts.rc_mode != VK_VIDEO_ENCODE_RATE_CONTROL_MODE_DISABLED_BIT_KHR)
300  hp->vkslice.constantQp = 0;
301 
302  hp->slice_hdr.slice_qp_delta = hp->vkslice.constantQp -
303  (enc->units.raw_pps.pic_init_qp_minus26 + 26);
304 
305  hp->vkh264pic_info.pNaluSliceEntries = &hp->vkslice;
306  hp->vkh264pic_info.naluSliceEntryCount = 1;
307 }
308 
311  FFHWBaseEncodePicture **rpl0,
312  FFHWBaseEncodePicture **rpl1,
313  int *rpl_size)
314 {
315  FFHWBaseEncodePicture *prev;
316  VulkanEncodeH264Picture *hp, *hn, *hc;
317  int i, j, n = 0;
318 
319  prev = pic->prev;
320  av_assert0(prev);
321  hp = pic->codec_priv;
322 
323  for (i = 0; i < pic->prev->nb_dpb_pics; i++) {
324  hn = prev->dpb[i]->codec_priv;
325  av_assert0(hn->frame_num < hp->frame_num);
326 
327  if (pic->type == FF_HW_PICTURE_TYPE_P) {
328  for (j = n; j > 0; j--) {
329  hc = rpl0[j - 1]->codec_priv;
330  av_assert0(hc->frame_num != hn->frame_num);
331  if (hc->frame_num > hn->frame_num)
332  break;
333  rpl0[j] = rpl0[j - 1];
334  }
335  rpl0[j] = prev->dpb[i];
336 
337  } else if (pic->type == FF_HW_PICTURE_TYPE_B) {
338  for (j = n; j > 0; j--) {
339  hc = rpl0[j - 1]->codec_priv;
341  if (hc->pic_order_cnt < hp->pic_order_cnt) {
342  if (hn->pic_order_cnt > hp->pic_order_cnt ||
343  hn->pic_order_cnt < hc->pic_order_cnt)
344  break;
345  } else {
346  if (hn->pic_order_cnt > hc->pic_order_cnt)
347  break;
348  }
349  rpl0[j] = rpl0[j - 1];
350  }
351  rpl0[j] = prev->dpb[i];
352 
353  for (j = n; j > 0; j--) {
354  hc = rpl1[j - 1]->codec_priv;
356  if (hc->pic_order_cnt > hp->pic_order_cnt) {
357  if (hn->pic_order_cnt < hp->pic_order_cnt ||
358  hn->pic_order_cnt > hc->pic_order_cnt)
359  break;
360  } else {
361  if (hn->pic_order_cnt < hc->pic_order_cnt)
362  break;
363  }
364  rpl1[j] = rpl1[j - 1];
365  }
366  rpl1[j] = prev->dpb[i];
367  }
368 
369  ++n;
370  }
371 
372  if (pic->type == FF_HW_PICTURE_TYPE_B) {
373  for (i = 0; i < n; i++) {
374  if (rpl0[i] != rpl1[i])
375  break;
376  }
377  if (i == n)
378  FFSWAP(FFHWBaseEncodePicture *, rpl1[0], rpl1[1]);
379  }
380 
381  if (pic->type == FF_HW_PICTURE_TYPE_P ||
382  pic->type == FF_HW_PICTURE_TYPE_B) {
383  av_log(avctx, AV_LOG_DEBUG, "Default RefPicList0 for fn=%d/poc=%d:",
384  hp->frame_num, hp->pic_order_cnt);
385  for (i = 0; i < n; i++) {
386  hn = rpl0[i]->codec_priv;
387  av_log(avctx, AV_LOG_DEBUG, " fn=%d/poc=%d",
388  hn->frame_num, hn->pic_order_cnt);
389  }
390  av_log(avctx, AV_LOG_DEBUG, "\n");
391  }
392  if (pic->type == FF_HW_PICTURE_TYPE_B) {
393  av_log(avctx, AV_LOG_DEBUG, "Default RefPicList1 for fn=%d/poc=%d:",
394  hp->frame_num, hp->pic_order_cnt);
395  for (i = 0; i < n; i++) {
396  hn = rpl1[i]->codec_priv;
397  av_log(avctx, AV_LOG_DEBUG, " fn=%d/poc=%d",
398  hn->frame_num, hn->pic_order_cnt);
399  }
400  av_log(avctx, AV_LOG_DEBUG, "\n");
401  }
402 
403  *rpl_size = n;
404 }
405 
406 static void setup_refs(AVCodecContext *avctx,
408  VkVideoEncodeInfoKHR *encode_info)
409 {
410  int n, i, j;
411  VulkanEncodeH264Context *enc = avctx->priv_data;
413  FFHWBaseEncodePicture *prev = pic->prev;
416 
417  hp->ref_list_info = (StdVideoEncodeH264ReferenceListsInfo) {
418  .flags = (StdVideoEncodeH264ReferenceListsInfoFlags) {
419  .ref_pic_list_modification_flag_l0 = 0,
420  .ref_pic_list_modification_flag_l1 = 0,
421  /* Reserved */
422  },
423  /* May be overridden during setup_slices() */
424  .num_ref_idx_l0_active_minus1 = pic->nb_refs[0] - 1,
425  .num_ref_idx_l1_active_minus1 = pic->nb_refs[1] - 1,
426  /* .RefPicList0 is set in vk_enc_h264_default_ref_pic_list() */
427  /* .RefPicList1 is set in vk_enc_h264_default_ref_pic_list() */
428  /* Reserved */
429  .pRefList0ModOperations = NULL, /* All set below */
430  .refList0ModOpCount = 0,
431  .pRefList1ModOperations = NULL,
432  .refList1ModOpCount = 0,
433  .pRefPicMarkingOperations = NULL,
434  .refPicMarkingOpCount = 0,
435  };
436 
437  for (i = 0; i < STD_VIDEO_H264_MAX_NUM_LIST_REF; i++)
438  hp->ref_list_info.RefPicList0[i] = hp->ref_list_info.RefPicList1[i] = -1;
439 
440  /* Note: really not sure */
441  for (int i = 0; i < pic->nb_refs[0]; i++) {
442  VkVideoReferenceSlotInfoKHR *slot_info;
443  slot_info = (VkVideoReferenceSlotInfoKHR *)&encode_info->pReferenceSlots[i];
444  hp->ref_list_info.RefPicList0[i] = slot_info->slotIndex;
445  }
446 
447  /* Note: really not sure */
448  for (int i = 0; i < pic->nb_refs[1]; i++) {
449  VkVideoReferenceSlotInfoKHR *slot_info;
450  slot_info = (VkVideoReferenceSlotInfoKHR *)&encode_info->pReferenceSlots[pic->nb_refs[0] + i];
451  hp->ref_list_info.RefPicList1[i] = slot_info->slotIndex;
452  }
453 
454  hp->h264pic_info.pRefLists = &hp->ref_list_info;
455 
456  if (pic->is_reference && pic->type != FF_HW_PICTURE_TYPE_IDR) {
457  FFHWBaseEncodePicture *discard_list[MAX_DPB_SIZE];
458  int discard = 0, keep = 0;
459 
460  // Discard everything which is in the DPB of the previous frame but
461  // not in the DPB of this one.
462  for (i = 0; i < prev->nb_dpb_pics; i++) {
463  for (j = 0; j < pic->nb_dpb_pics; j++) {
464  if (prev->dpb[i] == pic->dpb[j])
465  break;
466  }
467  if (j == pic->nb_dpb_pics) {
468  discard_list[discard] = prev->dpb[i];
469  ++discard;
470  } else {
471  ++keep;
472  }
473  }
474  av_assert0(keep <= enc->units.dpb_frames);
475 
476  if (discard == 0) {
477  hp->h264pic_info.flags.adaptive_ref_pic_marking_mode_flag = 0;
478  } else {
479  hp->h264pic_info.flags.adaptive_ref_pic_marking_mode_flag = 1;
480  for (i = 0; i < discard; i++) {
481  VulkanEncodeH264Picture *old = discard_list[i]->codec_priv;
482  av_assert0(old->frame_num < hp->frame_num);
483  hp->mmco[i] = (StdVideoEncodeH264RefPicMarkingEntry) {
484  .memory_management_control_operation = 1,
485  .difference_of_pic_nums_minus1 = hp->frame_num - old->frame_num - 1,
486  };
487  }
488  hp->mmco[i] = (StdVideoEncodeH264RefPicMarkingEntry) {
489  .memory_management_control_operation = 0,
490  };
491  hp->ref_list_info.pRefPicMarkingOperations = hp->mmco;
492  hp->ref_list_info.refPicMarkingOpCount = i + 1;
493  }
494  }
495 
496  if (pic->type == FF_HW_PICTURE_TYPE_I || pic->type == FF_HW_PICTURE_TYPE_IDR)
497  return;
498 
499  // If the intended references are not the first entries of RefPicListN
500  // by default, use ref-pic-list-modification to move them there.
502  def_l0, def_l1, &n);
503 
504  if (pic->type == FF_HW_PICTURE_TYPE_P) {
505  int need_rplm = 0;
506  for (i = 0; i < pic->nb_refs[0]; i++) {
507  av_assert0(pic->refs[0][i]);
508  if (pic->refs[0][i] != (FFHWBaseEncodePicture *)def_l0[i])
509  need_rplm = 1;
510  }
511 
512  hp->ref_list_info.flags.ref_pic_list_modification_flag_l0 = need_rplm;
513  if (need_rplm) {
514  int pic_num = hp->frame_num;
515  for (i = 0; i < pic->nb_refs[0]; i++) {
516  href = pic->refs[0][i]->codec_priv;
517  av_assert0(href->frame_num != pic_num);
518  if (href->frame_num < pic_num) {
519  hp->mods[0][i] = (StdVideoEncodeH264RefListModEntry) {
520  .modification_of_pic_nums_idc = 0,
521  .abs_diff_pic_num_minus1 = pic_num - href->frame_num - 1,
522  };
523  } else {
524  hp->mods[0][i] = (StdVideoEncodeH264RefListModEntry) {
525  .modification_of_pic_nums_idc = 1,
526  .abs_diff_pic_num_minus1 = href->frame_num - pic_num - 1,
527  };
528  }
529  pic_num = href->frame_num;
530  }
531  hp->ref_list_info.pRefList0ModOperations = hp->mods[0];
532  hp->ref_list_info.refList0ModOpCount = i - 1;
533  }
534  } else {
535  int need_rplm_l0 = 0, need_rplm_l1 = 0;
536  int n0 = 0, n1 = 0;
537  for (i = 0; i < pic->nb_refs[0]; i++) {
538  av_assert0(pic->refs[0][i]);
539  href = pic->refs[0][i]->codec_priv;
541  if (pic->refs[0][i] != (FFHWBaseEncodePicture *)def_l0[n0])
542  need_rplm_l0 = 1;
543  ++n0;
544  }
545 
546  for (int i = 0; i < pic->nb_refs[1]; i++) {
547  av_assert0(pic->refs[1][i]);
548  href = pic->refs[1][i]->codec_priv;
550  if (pic->refs[1][i] != (FFHWBaseEncodePicture *)def_l1[n1])
551  need_rplm_l1 = 1;
552  ++n1;
553  }
554 
555  hp->ref_list_info.flags.ref_pic_list_modification_flag_l0 = need_rplm_l0;
556  if (need_rplm_l0) {
557  int pic_num = hp->frame_num;
558  for (i = j = 0; i < pic->nb_refs[0]; i++) {
559  href = pic->refs[0][i]->codec_priv;
560  av_assert0(href->frame_num != pic_num);
561  if (href->frame_num < pic_num) {
562  hp->mods[0][j] = (StdVideoEncodeH264RefListModEntry) {
563  .modification_of_pic_nums_idc = 0,
564  .abs_diff_pic_num_minus1 = pic_num - href->frame_num - 1,
565  };
566  } else {
567  hp->mods[0][j] = (StdVideoEncodeH264RefListModEntry) {
568  .modification_of_pic_nums_idc = 1,
569  .abs_diff_pic_num_minus1 = href->frame_num - pic_num - 1,
570  };
571  }
572  pic_num = href->frame_num;
573  ++j;
574  }
575  hp->ref_list_info.pRefList0ModOperations = hp->mods[0];
576  hp->ref_list_info.refList0ModOpCount = j - 1;
577  }
578 
579  hp->ref_list_info.flags.ref_pic_list_modification_flag_l1 = need_rplm_l1;
580  if (need_rplm_l1) {
581  int pic_num = hp->frame_num;
582  for (i = j = 0; i < pic->nb_refs[1]; i++) {
583  href = pic->refs[1][i]->codec_priv;
584  av_assert0(href->frame_num != pic_num);
585  if (href->frame_num < pic_num) {
586  hp->mods[1][j] = (StdVideoEncodeH264RefListModEntry) {
587  .modification_of_pic_nums_idc = 0,
588  .abs_diff_pic_num_minus1 = pic_num - href->frame_num - 1,
589  };
590  } else {
591  hp->mods[1][j] = (StdVideoEncodeH264RefListModEntry) {
592  .modification_of_pic_nums_idc = 1,
593  .abs_diff_pic_num_minus1 = href->frame_num - pic_num - 1,
594  };
595  }
596  pic_num = href->frame_num;
597  ++j;
598  }
599  hp->ref_list_info.pRefList1ModOperations = hp->mods[1];
600  hp->ref_list_info.refList1ModOpCount = j - 1;
601  }
602  }
603 }
604 
606  VkVideoEncodeInfoKHR *encode_info)
607 {
608  int err;
609  FFVulkanEncodePicture *vp = pic->priv;
611  VkVideoReferenceSlotInfoKHR *ref_slot;
612 
613  err = vk_enc_h264_update_pic_info(avctx, pic);
614  if (err < 0)
615  return err;
616 
617  hp->vkh264pic_info = (VkVideoEncodeH264PictureInfoKHR) {
618  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_PICTURE_INFO_KHR,
619  .pNext = NULL,
620  .pNaluSliceEntries = NULL, // Filled in during setup_slices()
621  .naluSliceEntryCount = 0, // Filled in during setup_slices()
622  .pStdPictureInfo = &hp->h264pic_info,
623  };
624 
625  hp->h264pic_info = (StdVideoEncodeH264PictureInfo) {
626  .flags = (StdVideoEncodeH264PictureInfoFlags) {
627  .IdrPicFlag = pic->type == FF_HW_PICTURE_TYPE_IDR,
628  .is_reference = pic->is_reference,
629  .no_output_of_prior_pics_flag = 0,
630  .long_term_reference_flag = 0,
631  .adaptive_ref_pic_marking_mode_flag = 0, // Filled in during setup_refs()
632  /* Reserved */
633  },
634  .seq_parameter_set_id = 0,
635  .pic_parameter_set_id = 0,
636  .idr_pic_id = hp->idr_pic_id,
637  .primary_pic_type = pic->type == FF_HW_PICTURE_TYPE_P ? STD_VIDEO_H264_PICTURE_TYPE_P :
638  pic->type == FF_HW_PICTURE_TYPE_B ? STD_VIDEO_H264_PICTURE_TYPE_B :
639  pic->type == FF_HW_PICTURE_TYPE_I ? STD_VIDEO_H264_PICTURE_TYPE_I :
640  STD_VIDEO_H264_PICTURE_TYPE_IDR,
641  .frame_num = hp->frame_num,
642  .PicOrderCnt = hp->pic_order_cnt,
643  .temporal_id = 0, /* ? */
644  /* Reserved */
645  .pRefLists = NULL, // Filled in during setup_refs
646  };
647  encode_info->pNext = &hp->vkh264pic_info;
648 
649  hp->h264dpb_info = (StdVideoEncodeH264ReferenceInfo) {
650  .flags = (StdVideoEncodeH264ReferenceInfoFlags) {
651  .used_for_long_term_reference = 0,
652  /* Reserved */
653  },
654  .primary_pic_type = hp->h264pic_info.primary_pic_type,
655  .FrameNum = hp->h264pic_info.frame_num,
656  .PicOrderCnt = hp->h264pic_info.PicOrderCnt,
657  .long_term_pic_num = 0,
658  .long_term_frame_idx = 0,
659  .temporal_id = hp->h264pic_info.temporal_id,
660  };
661  hp->vkh264dpb_info = (VkVideoEncodeH264DpbSlotInfoKHR) {
662  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_DPB_SLOT_INFO_KHR,
663  .pStdReferenceInfo = &hp->h264dpb_info,
664  };
665 
666  vp->dpb_slot.pNext = &hp->vkh264dpb_info;
667 
668  ref_slot = (VkVideoReferenceSlotInfoKHR *)encode_info->pSetupReferenceSlot;
669  ref_slot->pNext = &hp->vkh264dpb_info;
670 
671  setup_refs(avctx, pic, encode_info);
672 
673  setup_slices(avctx, pic);
674 
675  return 0;
676 }
677 
678 static int init_profile(AVCodecContext *avctx,
679  VkVideoProfileInfoKHR *profile, void *pnext)
680 {
681  VkResult ret;
682  VulkanEncodeH264Context *enc = avctx->priv_data;
684  FFVulkanContext *s = &ctx->s;
685  FFVulkanFunctions *vk = &ctx->s.vkfn;
686  VkVideoEncodeH264CapabilitiesKHR h264_caps = {
687  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_CAPABILITIES_KHR,
688  };
689  VkVideoEncodeCapabilitiesKHR enc_caps = {
690  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_CAPABILITIES_KHR,
691  .pNext = &h264_caps,
692  };
693  VkVideoCapabilitiesKHR caps = {
694  .sType = VK_STRUCTURE_TYPE_VIDEO_CAPABILITIES_KHR,
695  .pNext = &enc_caps,
696  };
697 
698  /* In order of preference */
699  int last_supported = AV_PROFILE_UNKNOWN;
700  static const int known_profiles[] = {
705  };
706  int nb_profiles = FF_ARRAY_ELEMS(known_profiles);
707 
708  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->frames->sw_format);
709  if (!desc)
710  return AVERROR(EINVAL);
711 
712  if (desc->comp[0].depth == 8)
713  nb_profiles = 3;
714 
715  enc->profile = (VkVideoEncodeH264ProfileInfoKHR) {
716  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_PROFILE_INFO_KHR,
717  .pNext = pnext,
718  .stdProfileIdc = ff_vk_h264_profile_to_vk(avctx->profile),
719  };
720  profile->pNext = &enc->profile;
721 
722  /* Set level */
723  if (avctx->level == AV_LEVEL_UNKNOWN)
724  avctx->level = enc->common.opts.level;
725 
726  /* User has explicitly specified a profile. */
727  if (avctx->profile != AV_PROFILE_UNKNOWN)
728  return 0;
729 
730  av_log(avctx, AV_LOG_DEBUG, "Supported profiles:\n");
731  for (int i = 0; i < nb_profiles; i++) {
732  enc->profile.stdProfileIdc = ff_vk_h264_profile_to_vk(known_profiles[i]);
733  ret = vk->GetPhysicalDeviceVideoCapabilitiesKHR(s->hwctx->phys_dev,
734  profile,
735  &caps);
736  if (ret == VK_SUCCESS) {
737  av_log(avctx, AV_LOG_DEBUG, " %s\n",
738  avcodec_profile_name(avctx->codec_id, known_profiles[i]));
739  last_supported = known_profiles[i];
740  }
741  }
742 
743  if (last_supported == AV_PROFILE_UNKNOWN) {
744  av_log(avctx, AV_LOG_ERROR, "No supported profiles for given format\n");
745  return AVERROR(ENOTSUP);
746  }
747 
748  enc->profile.stdProfileIdc = ff_vk_h264_profile_to_vk(last_supported);
749  av_log(avctx, AV_LOG_VERBOSE, "Using profile %s\n",
750  avcodec_profile_name(avctx->codec_id, last_supported));
751  avctx->profile = last_supported;
752 
753  return 0;
754 }
755 
757 {
758  VulkanEncodeH264Context *enc = avctx->priv_data;
759  FFHWBaseEncodeH264Opts *unit_opts = &enc->unit_opts;
760 
761  if (avctx->rc_buffer_size)
762  unit_opts->hrd_buffer_size = avctx->rc_buffer_size;
763  else if (avctx->rc_max_rate > 0)
764  unit_opts->hrd_buffer_size = avctx->rc_max_rate;
765  else
766  unit_opts->hrd_buffer_size = avctx->bit_rate;
767 
768  if (avctx->rc_initial_buffer_occupancy) {
769  if (avctx->rc_initial_buffer_occupancy > unit_opts->hrd_buffer_size) {
770  av_log(avctx, AV_LOG_ERROR, "Invalid RC buffer settings: "
771  "must have initial buffer size (%d) <= "
772  "buffer size (%"PRId64").\n",
773  avctx->rc_initial_buffer_occupancy, unit_opts->hrd_buffer_size);
774  return AVERROR(EINVAL);
775  }
777  } else {
778  unit_opts->initial_buffer_fullness = unit_opts->hrd_buffer_size * 3 / 4;
779  }
780 
781  if (enc->common.opts.rc_mode == VK_VIDEO_ENCODE_RATE_CONTROL_MODE_DISABLED_BIT_KHR) {
782  /* HRD info is required for timing */
783  enc->unit_elems &= ~UNIT_SEI_TIMING;
784 
785  enc->fixed_qp_p = av_clip(enc->common.explicit_qp,
786  enc->caps.minQp, enc->caps.maxQp);
787  if (avctx->i_quant_factor > 0.0)
788  unit_opts->fixed_qp_idr = av_clip((avctx->i_quant_factor * enc->fixed_qp_p +
789  avctx->i_quant_offset) + 0.5,
790  enc->caps.minQp, enc->caps.maxQp);
791  else
792  unit_opts->fixed_qp_idr = enc->fixed_qp_p;
793 
794  if (avctx->b_quant_factor > 0.0)
795  enc->fixed_qp_b = av_clip((avctx->b_quant_factor * enc->fixed_qp_p +
796  avctx->b_quant_offset) + 0.5,
797  enc->caps.minQp, enc->caps.maxQp);
798  else
799  enc->fixed_qp_b = enc->fixed_qp_p;
800 
801  av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
802  "%d / %d / %d for IDR- / P- / B-frames.\n",
803  unit_opts->fixed_qp_idr, enc->fixed_qp_p, enc->fixed_qp_b);
804  } else {
805  unit_opts->fixed_qp_idr = 26;
806  enc->fixed_qp_p = 26;
807  enc->fixed_qp_b = 26;
808  }
809 
810  return 0;
811 }
812 
814 {
815  int err;
816  VulkanEncodeH264Context *enc = avctx->priv_data;
817 
818  FFHWBaseEncodeH264 *units = &enc->units;
819  FFHWBaseEncodeH264Opts *unit_opts = &enc->unit_opts;
820 
821  unit_opts->bit_rate = avctx->bit_rate;
822  unit_opts->mb_width = FFALIGN(avctx->width, 16) / 16;
823  unit_opts->mb_height = FFALIGN(avctx->height, 16) / 16;
824  unit_opts->flags = enc->unit_elems & UNIT_SEI_TIMING ? FF_HW_H264_SEI_TIMING : 0;
825 
826  /* cabac already set via an option */
827  /* fixed_qp_idr initialized in init_enc_options() */
828  /* hrd_buffer_size initialized in init_enc_options() */
829  /* initial_buffer_fullness initialized in init_enc_options() */
830 
832  units, unit_opts);
833  if (err < 0)
834  return err;
835 
837  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_SCALING_MATRIX_PRESENT_FLAG_SET_BIT_KHR);
839  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_SCALING_MATRIX_PRESENT_FLAG_SET_BIT_KHR);
841  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_TRANSFORM_8X8_MODE_FLAG_SET_BIT_KHR);
842 
843  return 0;
844 }
845 
846 typedef struct VulkanH264Units {
847  StdVideoH264SequenceParameterSet vksps;
848  StdVideoH264ScalingLists vksps_scaling;
849  StdVideoH264HrdParameters vksps_vui_header;
850  StdVideoH264SequenceParameterSetVui vksps_vui;
851 
852  StdVideoH264PictureParameterSet vkpps;
853  StdVideoH264ScalingLists vkpps_scaling;
855 
857  VulkanH264Units *vk_units)
858 {
859  VulkanEncodeH264Context *enc = avctx->priv_data;
860 
861  FFHWBaseEncodeH264 *units = &enc->units;
862 
863  H264RawSPS *sps = &units->raw_sps;
864  H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
865  StdVideoH264ScalingLists *vksps_scaling = &vk_units->vksps_scaling;
866  StdVideoH264HrdParameters *vksps_vui_header = &vk_units->vksps_vui_header;
867  StdVideoH264SequenceParameterSetVui *vksps_vui = &vk_units->vksps_vui;
868  StdVideoH264SequenceParameterSet *vksps = &vk_units->vksps;
869 
870  H264RawPPS *pps = &units->raw_pps;
871  StdVideoH264ScalingLists *vkpps_scaling = &vk_units->vkpps_scaling;
872  StdVideoH264PictureParameterSet *vkpps = &vk_units->vkpps;
873 
874  *vksps_scaling = (StdVideoH264ScalingLists) {
875  .scaling_list_present_mask = 0x0, // mask
876  .use_default_scaling_matrix_mask = 1,
877  };
878 
879  *vksps_vui_header = (StdVideoH264HrdParameters) {
880  .cpb_cnt_minus1 = hrd->cpb_cnt_minus1,
881  .bit_rate_scale = hrd->bit_rate_scale,
882  .cpb_size_scale = hrd->cpb_size_scale,
883  /* Reserved */
884  /* bit_rate/cpb_size/cbr_flag set below */
885  .initial_cpb_removal_delay_length_minus1 = hrd->initial_cpb_removal_delay_length_minus1,
886  .cpb_removal_delay_length_minus1 = hrd->cpb_removal_delay_length_minus1,
887  .dpb_output_delay_length_minus1 = hrd->dpb_output_delay_length_minus1,
888  .time_offset_length = hrd->time_offset_length,
889  };
890 
891  for (int i = 0; i < H264_MAX_CPB_CNT; i++) {
892  vksps_vui_header->bit_rate_value_minus1[i] = hrd->bit_rate_value_minus1[i];
893  vksps_vui_header->cpb_size_value_minus1[i] = hrd->cpb_size_value_minus1[i];
894  vksps_vui_header->cbr_flag[i] = hrd->cbr_flag[i];
895  }
896 
897  *vksps_vui = (StdVideoH264SequenceParameterSetVui) {
898  .flags = (StdVideoH264SpsVuiFlags) {
899  .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
900  .overscan_info_present_flag = sps->vui.overscan_info_present_flag,
901  .overscan_appropriate_flag = sps->vui.overscan_appropriate_flag,
902  .video_signal_type_present_flag = sps->vui.video_signal_type_present_flag,
903  .video_full_range_flag = sps->vui.video_full_range_flag,
904  .color_description_present_flag = sps->vui.colour_description_present_flag,
905  .chroma_loc_info_present_flag = sps->vui.chroma_loc_info_present_flag,
906  .timing_info_present_flag = sps->vui.timing_info_present_flag,
907  .fixed_frame_rate_flag = sps->vui.fixed_frame_rate_flag,
908  .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
909  .nal_hrd_parameters_present_flag = sps->vui.nal_hrd_parameters_present_flag,
910  .vcl_hrd_parameters_present_flag = sps->vui.vcl_hrd_parameters_present_flag,
911  },
912  .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
913  .sar_width = sps->vui.sar_width,
914  .sar_height = sps->vui.sar_height,
915  .video_format = sps->vui.video_format,
916  .colour_primaries = sps->vui.colour_primaries,
917  .transfer_characteristics = sps->vui.transfer_characteristics,
918  .matrix_coefficients = sps->vui.matrix_coefficients,
919  .num_units_in_tick = sps->vui.num_units_in_tick,
920  .time_scale = sps->vui.time_scale,
921  .max_num_reorder_frames = sps->vui.max_num_reorder_frames,
922  .max_dec_frame_buffering = sps->vui.max_dec_frame_buffering,
923  .chroma_sample_loc_type_top_field = sps->vui.chroma_sample_loc_type_top_field,
924  .chroma_sample_loc_type_bottom_field = sps->vui.chroma_sample_loc_type_bottom_field,
925  /* Reserved */
926  .pHrdParameters = vksps_vui_header,
927  };
928 
929  *vksps = (StdVideoH264SequenceParameterSet) {
930  .flags = (StdVideoH264SpsFlags) {
931  .constraint_set0_flag = sps->constraint_set0_flag,
932  .constraint_set1_flag = sps->constraint_set1_flag,
933  .constraint_set2_flag = sps->constraint_set2_flag,
934  .constraint_set3_flag = sps->constraint_set3_flag,
935  .constraint_set4_flag = sps->constraint_set4_flag,
936  .constraint_set5_flag = sps->constraint_set5_flag,
937  .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
938  .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
939  .frame_mbs_only_flag = sps->frame_mbs_only_flag,
940  .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
941  .separate_colour_plane_flag = sps->separate_colour_plane_flag,
942  .gaps_in_frame_num_value_allowed_flag = sps->gaps_in_frame_num_allowed_flag,
943  .qpprime_y_zero_transform_bypass_flag = sps->qpprime_y_zero_transform_bypass_flag,
944  .frame_cropping_flag = sps->frame_cropping_flag,
945  .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
946  .vui_parameters_present_flag = sps->vui_parameters_present_flag,
947  },
948  .profile_idc = ff_vk_h264_profile_to_vk(sps->profile_idc),
949  .level_idc = ff_vk_h264_level_to_vk(sps->level_idc),
950  .chroma_format_idc = sps->chroma_format_idc,
951  .seq_parameter_set_id = sps->seq_parameter_set_id,
952  .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
953  .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
954  .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
955  .pic_order_cnt_type = sps->pic_order_cnt_type,
956  .offset_for_non_ref_pic = sps->offset_for_non_ref_pic,
957  .offset_for_top_to_bottom_field = sps->offset_for_top_to_bottom_field,
958  .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
959  .num_ref_frames_in_pic_order_cnt_cycle = sps->num_ref_frames_in_pic_order_cnt_cycle,
960  .max_num_ref_frames = sps->max_num_ref_frames,
961  /* Reserved */
962  .pic_width_in_mbs_minus1 = sps->pic_width_in_mbs_minus1,
963  .pic_height_in_map_units_minus1 = sps->pic_height_in_map_units_minus1,
964  .frame_crop_left_offset = sps->frame_crop_left_offset,
965  .frame_crop_right_offset = sps->frame_crop_right_offset,
966  .frame_crop_top_offset = sps->frame_crop_top_offset,
967  .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
968  /* Reserved */
969  .pOffsetForRefFrame = sps->offset_for_ref_frame,
970  .pScalingLists = vksps_scaling,
971  .pSequenceParameterSetVui = vksps_vui,
972  };
973 
974  *vkpps_scaling = (StdVideoH264ScalingLists) {
975  .scaling_list_present_mask = 0x0, // mask
976  .use_default_scaling_matrix_mask = 1,
977  };
978 
979  *vkpps = (StdVideoH264PictureParameterSet) {
980  .flags = (StdVideoH264PpsFlags) {
981  .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
982  .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
983  .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
984  .deblocking_filter_control_present_flag = pps->deblocking_filter_control_present_flag,
985  .weighted_pred_flag = pps->weighted_pred_flag,
986  .bottom_field_pic_order_in_frame_present_flag = pps->bottom_field_pic_order_in_frame_present_flag,
987  .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
988  .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
989  },
990  .seq_parameter_set_id = pps->seq_parameter_set_id,
991  .pic_parameter_set_id = pps->pic_parameter_set_id,
992  .num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
993  .num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
994  .weighted_bipred_idc = pps->weighted_bipred_idc,
995  .pic_init_qp_minus26 = pps->pic_init_qp_minus26,
996  .pic_init_qs_minus26 = pps->pic_init_qs_minus26,
997  .chroma_qp_index_offset = pps->chroma_qp_index_offset,
998  .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
999  .pScalingLists = vkpps_scaling,
1000  };
1001 
1002  return 0;
1003 }
1004 
1006 {
1007  int err;
1008  VulkanEncodeH264Context *enc = avctx->priv_data;
1009  FFVulkanEncodeContext *ctx = &enc->common;
1010  FFVulkanContext *s = &ctx->s;
1011  FFVulkanFunctions *vk = &ctx->s.vkfn;
1012 
1013  VulkanH264Units vk_units = { 0 };
1014 
1015  VkVideoEncodeH264SessionParametersAddInfoKHR h264_params_info;
1016  VkVideoEncodeH264SessionParametersCreateInfoKHR h264_params;
1017 
1018  /* Convert it to Vulkan */
1019  err = base_unit_to_vk(avctx, &vk_units);
1020  if (err < 0) {
1021  av_log(avctx, AV_LOG_ERROR, "Unable to convert SPS/PPS units to Vulkan: %s\n",
1022  av_err2str(err));
1023  return err;
1024  }
1025 
1026  /* Destroy the session params */
1027  if (ctx->session_params)
1028  vk->DestroyVideoSessionParametersKHR(s->hwctx->act_dev,
1029  ctx->session_params,
1030  s->hwctx->alloc);
1031 
1032  h264_params_info = (VkVideoEncodeH264SessionParametersAddInfoKHR) {
1033  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_SESSION_PARAMETERS_ADD_INFO_KHR,
1034  .pStdSPSs = &vk_units.vksps,
1035  .stdSPSCount = 1,
1036  .pStdPPSs = &vk_units.vkpps,
1037  .stdPPSCount = 1,
1038  };
1039  h264_params = (VkVideoEncodeH264SessionParametersCreateInfoKHR) {
1040  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_SESSION_PARAMETERS_CREATE_INFO_KHR,
1041  .maxStdSPSCount = 1,
1042  .maxStdPPSCount = 1,
1043  .pParametersAddInfo = &h264_params_info,
1044  };
1045 
1046  return ff_vulkan_encode_create_session_params(avctx, ctx, &h264_params);
1047 }
1048 
1050  const uint8_t *data, size_t size,
1051  int sps_override, int pps_override)
1052 {
1053  int err;
1054  VulkanEncodeH264Context *enc = avctx->priv_data;
1055 
1056  CodedBitstreamContext *cbs;
1057  CodedBitstreamFragment au = { 0 };
1058 
1059  err = ff_cbs_init(&cbs, AV_CODEC_ID_H264, avctx);
1060  if (err < 0)
1061  return err;
1062 
1063  err = ff_cbs_read(cbs, &au, data, size);
1064  if (err < 0) {
1065  av_log(avctx, AV_LOG_ERROR, "Unable to parse feedback units, bad drivers: %s\n",
1066  av_err2str(err));
1067  return err;
1068  }
1069 
1070  /* If PPS has an override, just copy it entirely. */
1071  if (pps_override) {
1072  for (int i = 0; i < au.nb_units; i++) {
1073  if (au.units[i].type == H264_NAL_PPS) {
1074  H264RawPPS *pps = au.units[i].content;
1075  memcpy(&enc->units.raw_pps, pps, sizeof(*pps));
1076  break;
1077  }
1078  }
1079  }
1080 
1081  ff_cbs_fragment_free(&au);
1082  ff_cbs_close(&cbs);
1083 
1084  return 0;
1085 }
1086 
1088 {
1089  int err;
1090  VkResult ret;
1091  VulkanEncodeH264Context *enc = avctx->priv_data;
1092  FFVulkanEncodeContext *ctx = &enc->common;
1093  FFVulkanContext *s = &ctx->s;
1094  FFVulkanFunctions *vk = &ctx->s.vkfn;
1095 
1096  VkVideoEncodeH264SessionParametersGetInfoKHR h264_params_info;
1097  VkVideoEncodeSessionParametersGetInfoKHR params_info;
1098  VkVideoEncodeH264SessionParametersFeedbackInfoKHR h264_params_feedback;
1099  VkVideoEncodeSessionParametersFeedbackInfoKHR params_feedback;
1100 
1101  void *data = NULL;
1102  size_t data_size = 0;
1103 
1104  /* Generate SPS/PPS unit info */
1105  err = init_sequence_headers(avctx);
1106  if (err < 0) {
1107  av_log(avctx, AV_LOG_ERROR, "Unable to initialize SPS/PPS units: %s\n",
1108  av_err2str(err));
1109  return err;
1110  }
1111 
1112  /* Create session parameters from them */
1113  err = create_session_params(avctx);
1114  if (err < 0)
1115  return err;
1116 
1117  h264_params_info = (VkVideoEncodeH264SessionParametersGetInfoKHR) {
1118  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_SESSION_PARAMETERS_GET_INFO_KHR,
1119  .writeStdSPS = 1,
1120  .writeStdPPS = 1,
1121  .stdSPSId = enc->units.raw_sps.seq_parameter_set_id,
1122  .stdPPSId = enc->units.raw_pps.pic_parameter_set_id,
1123  };
1124  params_info = (VkVideoEncodeSessionParametersGetInfoKHR) {
1125  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_SESSION_PARAMETERS_GET_INFO_KHR,
1126  .pNext = &h264_params_info,
1127  .videoSessionParameters = ctx->session_params,
1128  };
1129 
1130  h264_params_feedback = (VkVideoEncodeH264SessionParametersFeedbackInfoKHR) {
1131  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_SESSION_PARAMETERS_FEEDBACK_INFO_KHR,
1132  };
1133  params_feedback = (VkVideoEncodeSessionParametersFeedbackInfoKHR) {
1134  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_SESSION_PARAMETERS_FEEDBACK_INFO_KHR,
1135  .pNext = &h264_params_feedback,
1136  };
1137 
1138  ret = vk->GetEncodedVideoSessionParametersKHR(s->hwctx->act_dev, &params_info,
1139  &params_feedback,
1140  &data_size, data);
1141  if (ret == VK_INCOMPLETE ||
1142  (ret == VK_SUCCESS) && (data_size > 0)) {
1143  data = av_mallocz(data_size);
1144  if (!data)
1145  return AVERROR(ENOMEM);
1146  } else {
1147  av_log(avctx, AV_LOG_ERROR, "Unable to get feedback for H.264 units = %lu\n", data_size);
1148  return err;
1149  }
1150 
1151  ret = vk->GetEncodedVideoSessionParametersKHR(s->hwctx->act_dev, &params_info,
1152  &params_feedback,
1153  &data_size, data);
1154  if (ret != VK_SUCCESS) {
1155  av_log(avctx, AV_LOG_ERROR, "Error writing feedback units\n");
1156  return err;
1157  }
1158 
1159  av_log(avctx, AV_LOG_VERBOSE, "Feedback units written, overrides: %i (SPS: %i PPS: %i)\n",
1160  params_feedback.hasOverrides,
1161  h264_params_feedback.hasStdSPSOverrides,
1162  h264_params_feedback.hasStdPPSOverrides);
1163 
1164  params_feedback.hasOverrides = 1;
1165  h264_params_feedback.hasStdPPSOverrides = 1;
1166 
1167  /* No need to sync any overrides */
1168  if (!params_feedback.hasOverrides)
1169  return 0;
1170 
1171  /* Parse back tne units and override */
1172  err = parse_feedback_units(avctx, data, data_size,
1173  h264_params_feedback.hasStdSPSOverrides,
1174  h264_params_feedback.hasStdPPSOverrides);
1175  if (err < 0)
1176  return err;
1177 
1178  /* Create final session parameters */
1179  err = create_session_params(avctx);
1180  if (err < 0)
1181  return err;
1182 
1183  return 0;
1184 }
1185 
1188  void *nal_unit)
1189 {
1190  H264RawNALUnitHeader *header = nal_unit;
1191 
1192  int err = ff_cbs_insert_unit_content(au, -1,
1193  header->nal_unit_type, nal_unit, NULL);
1194  if (err < 0)
1195  av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
1196  "type = %d.\n", header->nal_unit_type);
1197 
1198  return err;
1199 }
1200 
1202  uint8_t *data, size_t *data_len,
1204 {
1205  VulkanEncodeH264Context *enc = avctx->priv_data;
1206 
1207  int err = ff_cbs_write_fragment_data(enc->cbs, au);
1208  if (err < 0) {
1209  av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
1210  return err;
1211  }
1212 
1213  if (*data_len < au->data_size) {
1214  av_log(avctx, AV_LOG_ERROR, "Access unit too large: %zu < %zu.\n",
1215  *data_len, au->data_size);
1216  return AVERROR(ENOSPC);
1217  }
1218 
1219  memcpy(data, au->data, au->data_size);
1220  *data_len = au->data_size;
1221 
1222  return 0;
1223 }
1224 
1226  FFHWBaseEncodePicture *base_pic,
1227  uint8_t *data, size_t *data_len)
1228 {
1229  int err;
1230  VulkanEncodeH264Context *enc = avctx->priv_data;
1231  VulkanEncodeH264Picture *hp = base_pic ? base_pic->codec_priv : NULL;
1233 
1234  if (hp && hp->units_needed & UNIT_AUD) {
1235  err = vulkan_encode_h264_add_nal(avctx, au, &enc->raw_aud);
1236  if (err < 0)
1237  goto fail;
1238  }
1239 
1240  err = vulkan_encode_h264_add_nal(avctx, au, &enc->units.raw_sps);
1241  if (err < 0)
1242  goto fail;
1243 
1244  err = vulkan_encode_h264_add_nal(avctx, au, &enc->units.raw_pps);
1245  if (err < 0)
1246  goto fail;
1247 
1248  err = write_access_unit(avctx, data, data_len, au);
1249 fail:
1251  return err;
1252 }
1253 
1255  FFHWBaseEncodePicture *base_pic,
1256  uint8_t *data, size_t *data_len)
1257 {
1258  int err;
1259  VulkanEncodeH264Context *enc = avctx->priv_data;
1260  VulkanEncodeH264Picture *hp = base_pic->codec_priv;
1262 
1263  if (hp->units_needed & UNIT_AUD) {
1264  err = vulkan_encode_h264_add_nal(avctx, au, &enc->raw_aud);
1265  if (err < 0)
1266  goto fail;
1267  }
1268 
1269  if (hp->units_needed & UNIT_SEI_IDENTIFIER) {
1270  err = ff_cbs_sei_add_message(enc->cbs, au, 1,
1272  &enc->sei_identifier, NULL);
1273  if (err < 0)
1274  goto fail;
1275  }
1276 
1277  if (hp->units_needed & UNIT_SEI_TIMING) {
1278  if (base_pic->type == FF_HW_PICTURE_TYPE_IDR) {
1279  err = ff_cbs_sei_add_message(enc->cbs, au, 1,
1282  if (err < 0)
1283  goto fail;
1284  }
1285  err = ff_cbs_sei_add_message(enc->cbs, au, 1,
1287  &enc->sei_pic_timing, NULL);
1288  if (err < 0)
1289  goto fail;
1290  }
1291 
1292  if (hp->units_needed & UNIT_SEI_RECOVERY) {
1293  err = ff_cbs_sei_add_message(enc->cbs, au, 1,
1295  &enc->sei_recovery_point, NULL);
1296  if (err < 0)
1297  goto fail;
1298  }
1299 
1300  if (hp->units_needed & UNIT_SEI_A53_CC) {
1301  err = ff_cbs_sei_add_message(enc->cbs, au, 1,
1303  &enc->sei_a53cc, NULL);
1304  if (err < 0)
1305  goto fail;
1306  }
1307 
1308  if (hp->units_needed) {
1309  err = write_access_unit(avctx, data, data_len, au);
1310  if (err < 0)
1311  goto fail;
1312  } else {
1313  *data_len = 0;
1314  }
1315 
1316 fail:
1318  return err;
1319 }
1320 
1321 static int write_filler(AVCodecContext *avctx, uint32_t filler,
1322  uint8_t *data, size_t *data_len)
1323 {
1324  int err;
1325  VulkanEncodeH264Context *enc = avctx->priv_data;
1327 
1328  H264RawFiller raw_filler = {
1329  .nal_unit_header = {
1331  },
1332  .filler_size = filler,
1333  };
1334 
1335  err = vulkan_encode_h264_add_nal(avctx, au, &raw_filler);
1336  if (err < 0)
1337  goto fail;
1338 
1339  err = write_access_unit(avctx, data, data_len, au);
1340 fail:
1342  return err;
1343 }
1344 
1345 static const FFVulkanCodec enc_cb = {
1349  .picture_priv_data_size = sizeof(VulkanEncodeH264Picture),
1350  .filler_header_size = 6,
1357 };
1358 
1360 {
1361  int err, ref_l0, ref_l1;
1362  VulkanEncodeH264Context *enc = avctx->priv_data;
1363  FFVulkanEncodeContext *ctx = &enc->common;
1364  FFVulkanContext *s = &ctx->s;
1365  FFHWBaseEncodeContext *base_ctx = &ctx->base;
1366  int flags;
1367 
1368  if (avctx->profile == AV_PROFILE_UNKNOWN)
1369  avctx->profile = enc->common.opts.profile;
1370 
1371  enc->caps = (VkVideoEncodeH264CapabilitiesKHR) {
1372  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_CAPABILITIES_KHR,
1373  };
1374 
1375  enc->quality_props = (VkVideoEncodeH264QualityLevelPropertiesKHR) {
1376  .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H264_QUALITY_LEVEL_PROPERTIES_KHR,
1377  };
1378 
1379  err = ff_vulkan_encode_init(avctx, &enc->common,
1381  &enc->caps, &enc->quality_props);
1382  if (err < 0)
1383  return err;
1384 
1385  av_log(avctx, AV_LOG_VERBOSE, "H264 encoder capabilities:\n");
1386  av_log(avctx, AV_LOG_VERBOSE, " Standard capability flags:\n");
1387  av_log(avctx, AV_LOG_VERBOSE, " separate_color_plane: %i\n",
1388  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_SEPARATE_COLOR_PLANE_FLAG_SET_BIT_KHR));
1389  av_log(avctx, AV_LOG_VERBOSE, " qprime_y_zero_transform_bypass: %i\n",
1390  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_QPPRIME_Y_ZERO_TRANSFORM_BYPASS_FLAG_SET_BIT_KHR));
1391  av_log(avctx, AV_LOG_VERBOSE, " scaling_lists: %i\n",
1392  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_SCALING_MATRIX_PRESENT_FLAG_SET_BIT_KHR));
1393  av_log(avctx, AV_LOG_VERBOSE, " chroma_qp_index_offset: %i\n",
1394  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_CHROMA_QP_INDEX_OFFSET_BIT_KHR));
1395  av_log(avctx, AV_LOG_VERBOSE, " second_chroma_qp_index_offset: %i\n",
1396  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_SECOND_CHROMA_QP_INDEX_OFFSET_BIT_KHR));
1397  av_log(avctx, AV_LOG_VERBOSE, " pic_init_qp: %i\n",
1398  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_PIC_INIT_QP_MINUS26_BIT_KHR));
1399  av_log(avctx, AV_LOG_VERBOSE, " weighted:%s%s%s\n",
1400  enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_WEIGHTED_PRED_FLAG_SET_BIT_KHR ?
1401  " pred" : "",
1402  enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_WEIGHTED_BIPRED_IDC_EXPLICIT_BIT_KHR ?
1403  " bipred_explicit" : "",
1404  enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_WEIGHTED_BIPRED_IDC_IMPLICIT_BIT_KHR ?
1405  " bipred_implicit" : "");
1406  av_log(avctx, AV_LOG_VERBOSE, " 8x8_transforms: %i\n",
1407  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_TRANSFORM_8X8_MODE_FLAG_SET_BIT_KHR));
1408  av_log(avctx, AV_LOG_VERBOSE, " disable_direct_spatial_mv_pred: %i\n",
1409  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_DIRECT_SPATIAL_MV_PRED_FLAG_UNSET_BIT_KHR));
1410  av_log(avctx, AV_LOG_VERBOSE, " coder:%s%s\n",
1411  enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_ENTROPY_CODING_MODE_FLAG_UNSET_BIT_KHR ?
1412  " cabac" : "",
1413  enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_ENTROPY_CODING_MODE_FLAG_SET_BIT_KHR ?
1414  " cavlc" : "");
1415  av_log(avctx, AV_LOG_VERBOSE, " direct_8x8_inference: %i\n",
1416  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_DIRECT_8X8_INFERENCE_FLAG_UNSET_BIT_KHR));
1417  av_log(avctx, AV_LOG_VERBOSE, " constrained_intra_pred: %i\n",
1418  !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_CONSTRAINED_INTRA_PRED_FLAG_SET_BIT_KHR));
1419  av_log(avctx, AV_LOG_VERBOSE, " deblock:%s%s%s\n",
1420  enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_DEBLOCKING_FILTER_DISABLED_BIT_KHR ?
1421  " filter_disabling" : "",
1422  enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_DEBLOCKING_FILTER_ENABLED_BIT_KHR ?
1423  " filter_enabling" : "",
1424  enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H264_STD_DEBLOCKING_FILTER_PARTIAL_BIT_KHR ?
1425  " filter_partial" : "");
1426 
1427  av_log(avctx, AV_LOG_VERBOSE, " Capability flags:\n");
1428  av_log(avctx, AV_LOG_VERBOSE, " hdr_compliance: %i\n",
1429  !!(enc->caps.flags & VK_VIDEO_ENCODE_H264_CAPABILITY_HRD_COMPLIANCE_BIT_KHR));
1430  av_log(avctx, AV_LOG_VERBOSE, " pred_weight_table_generated: %i\n",
1431  !!(enc->caps.flags & VK_VIDEO_ENCODE_H264_CAPABILITY_PREDICTION_WEIGHT_TABLE_GENERATED_BIT_KHR));
1432  av_log(avctx, AV_LOG_VERBOSE, " row_unaligned_slice: %i\n",
1433  !!(enc->caps.flags & VK_VIDEO_ENCODE_H264_CAPABILITY_ROW_UNALIGNED_SLICE_BIT_KHR));
1434  av_log(avctx, AV_LOG_VERBOSE, " different_slice_type: %i\n",
1435  !!(enc->caps.flags & VK_VIDEO_ENCODE_H264_CAPABILITY_DIFFERENT_SLICE_TYPE_BIT_KHR));
1436  av_log(avctx, AV_LOG_VERBOSE, " b_frame_in_l0_list: %i\n",
1437  !!(enc->caps.flags & VK_VIDEO_ENCODE_H264_CAPABILITY_B_FRAME_IN_L0_LIST_BIT_KHR));
1438  av_log(avctx, AV_LOG_VERBOSE, " b_frame_in_l1_list: %i\n",
1439  !!(enc->caps.flags & VK_VIDEO_ENCODE_H264_CAPABILITY_B_FRAME_IN_L1_LIST_BIT_KHR));
1440  av_log(avctx, AV_LOG_VERBOSE, " per_pict_type_min_max_qp: %i\n",
1441  !!(enc->caps.flags & VK_VIDEO_ENCODE_H264_CAPABILITY_PER_PICTURE_TYPE_MIN_MAX_QP_BIT_KHR));
1442  av_log(avctx, AV_LOG_VERBOSE, " per_slice_constant_qp: %i\n",
1443  !!(enc->caps.flags & VK_VIDEO_ENCODE_H264_CAPABILITY_PER_SLICE_CONSTANT_QP_BIT_KHR));
1444  av_log(avctx, AV_LOG_VERBOSE, " generate_prefix_nalu: %i\n",
1445  !!(enc->caps.flags & VK_VIDEO_ENCODE_H264_CAPABILITY_GENERATE_PREFIX_NALU_BIT_KHR));
1446 
1447  av_log(avctx, AV_LOG_VERBOSE, " Capabilities:\n");
1448  av_log(avctx, AV_LOG_VERBOSE, " maxLevelIdc: %i\n",
1449  enc->caps.maxLevelIdc);
1450  av_log(avctx, AV_LOG_VERBOSE, " maxSliceCount: %i\n",
1451  enc->caps.maxSliceCount);
1452  av_log(avctx, AV_LOG_VERBOSE, " max(P/B)PictureL0ReferenceCount: %i P's; %i B's\n",
1453  enc->caps.maxPPictureL0ReferenceCount,
1454  enc->caps.maxBPictureL0ReferenceCount);
1455  av_log(avctx, AV_LOG_VERBOSE, " maxL1ReferenceCount: %i\n",
1456  enc->caps.maxL1ReferenceCount);
1457  av_log(avctx, AV_LOG_VERBOSE, " maxTemporalLayerCount: %i\n",
1458  enc->caps.maxTemporalLayerCount);
1459  av_log(avctx, AV_LOG_VERBOSE, " expectDyadicTemporalLayerPattern: %i\n",
1460  enc->caps.expectDyadicTemporalLayerPattern);
1461  av_log(avctx, AV_LOG_VERBOSE, " min/max Qp: [%i, %i]\n",
1462  enc->caps.minQp, enc->caps.maxQp);
1463  av_log(avctx, AV_LOG_VERBOSE, " prefersGopRemainingFrames: %i\n",
1464  enc->caps.prefersGopRemainingFrames);
1465  av_log(avctx, AV_LOG_VERBOSE, " requiresGopRemainingFrames: %i\n",
1466  enc->caps.requiresGopRemainingFrames);
1467 
1468  err = init_enc_options(avctx);
1469  if (err < 0)
1470  return err;
1471 
1472  flags = ctx->codec->flags;
1473  if (!enc->caps.maxPPictureL0ReferenceCount &&
1474  !enc->caps.maxBPictureL0ReferenceCount &&
1475  !enc->caps.maxL1ReferenceCount) {
1476  /* Intra-only */
1478  ref_l0 = ref_l1 = 0;
1479  } else if (!enc->caps.maxPPictureL0ReferenceCount) {
1480  /* No P-frames? How. */
1481  base_ctx->p_to_gpb = 1;
1482  ref_l0 = enc->caps.maxBPictureL0ReferenceCount;
1483  ref_l1 = enc->caps.maxL1ReferenceCount;
1484  } else if (!enc->caps.maxBPictureL0ReferenceCount &&
1485  !enc->caps.maxL1ReferenceCount) {
1486  /* No B-frames */
1488  ref_l0 = enc->caps.maxPPictureL0ReferenceCount;
1489  ref_l1 = 0;
1490  } else {
1491  /* P and B frames */
1492  ref_l0 = FFMIN(enc->caps.maxPPictureL0ReferenceCount,
1493  enc->caps.maxBPictureL0ReferenceCount);
1494  ref_l1 = enc->caps.maxL1ReferenceCount;
1495  }
1496 
1497  err = ff_hw_base_init_gop_structure(base_ctx, avctx, ref_l0, ref_l1,
1498  flags, 0);
1499  if (err < 0)
1500  return err;
1501 
1502  base_ctx->output_delay = base_ctx->b_per_p;
1503  base_ctx->decode_delay = base_ctx->max_b_depth;
1504 
1505  /* Prepare SEI */
1506  if (enc->unit_elems & UNIT_SEI_IDENTIFIER) {
1507  int len;
1508 
1509  memcpy(enc->sei_identifier.uuid_iso_iec_11578,
1511  sizeof(enc->sei_identifier.uuid_iso_iec_11578));
1512 
1513  len = snprintf(NULL, 0,
1514  "%s / Vulkan video %i.%i.%i / %s %i.%i.%i / %s",
1517  s->driver_props.driverName,
1518  CODEC_VER(s->props.properties.driverVersion),
1519  s->props.properties.deviceName);
1520 
1521  if (len >= 0) {
1522  enc->sei_identifier_string = av_malloc(len + 1);
1523  if (!enc->sei_identifier_string)
1524  return AVERROR(ENOMEM);
1525 
1526  len = snprintf(enc->sei_identifier_string, len + 1,
1527  "%s / Vulkan video %i.%i.%i / %s %i.%i.%i / %s",
1530  s->driver_props.driverName,
1531  CODEC_VER(s->props.properties.driverVersion),
1532  s->props.properties.deviceName);
1533 
1535  enc->sei_identifier.data_length = len + 1;
1536  }
1537  }
1538 
1539  /* Init CBS */
1540  err = ff_cbs_init(&enc->cbs, AV_CODEC_ID_H264, avctx);
1541  if (err < 0)
1542  return err;
1543 
1544  /* Create units and session parameters */
1545  err = init_base_units(avctx);
1546  if (err < 0)
1547  return err;
1548 
1549  /* Write out extradata */
1550  err = ff_vulkan_write_global_header(avctx, &enc->common);
1551  if (err < 0)
1552  return err;
1553 
1554  return 0;
1555 }
1556 
1558 {
1559  VulkanEncodeH264Context *enc = avctx->priv_data;
1561  return 0;
1562 }
1563 
1564 #define OFFSET(x) offsetof(VulkanEncodeH264Context, x)
1565 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1569 
1570  { "profile", "Set profile (profile_idc and constraint_set*_flag)",
1571  OFFSET(common.opts.profile), AV_OPT_TYPE_INT,
1572  { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xffff, FLAGS, .unit = "profile" },
1573 
1574 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1575  { .i64 = value }, 0, 0, FLAGS, .unit = "profile"
1576  { PROFILE("constrained_baseline", AV_PROFILE_H264_CONSTRAINED_BASELINE) },
1577  { PROFILE("main", AV_PROFILE_H264_MAIN) },
1578  { PROFILE("high", AV_PROFILE_H264_HIGH) },
1579  { PROFILE("high444p", AV_PROFILE_H264_HIGH_10) },
1580 #undef PROFILE
1581 
1582  { "level", "Set level (level_idc)",
1583  OFFSET(common.opts.level), AV_OPT_TYPE_INT,
1584  { .i64 = AV_LEVEL_UNKNOWN }, AV_LEVEL_UNKNOWN, 0xff, FLAGS, .unit = "level" },
1585 
1586 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1587  { .i64 = value }, 0, 0, FLAGS, .unit = "level"
1588  { LEVEL("1", 10) },
1589  { LEVEL("1.1", 11) },
1590  { LEVEL("1.2", 12) },
1591  { LEVEL("1.3", 13) },
1592  { LEVEL("2", 20) },
1593  { LEVEL("2.1", 21) },
1594  { LEVEL("2.2", 22) },
1595  { LEVEL("3", 30) },
1596  { LEVEL("3.1", 31) },
1597  { LEVEL("3.2", 32) },
1598  { LEVEL("4", 40) },
1599  { LEVEL("4.1", 41) },
1600  { LEVEL("4.2", 42) },
1601  { LEVEL("5", 50) },
1602  { LEVEL("5.1", 51) },
1603  { LEVEL("5.2", 52) },
1604  { LEVEL("6", 60) },
1605  { LEVEL("6.1", 61) },
1606  { LEVEL("6.2", 62) },
1607 #undef LEVEL
1608 
1609  { "coder", "Entropy coder type", OFFSET(unit_opts.cabac), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
1610  { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1611  { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1612 
1613  { "units", "Set units to include", OFFSET(unit_elems), AV_OPT_TYPE_FLAGS, { .i64 = UNIT_AUD | UNIT_SEI_IDENTIFIER | UNIT_SEI_RECOVERY | UNIT_SEI_TIMING | UNIT_SEI_A53_CC }, 0, INT_MAX, FLAGS, "units" },
1614  { "aud", "Include AUD units", 0, AV_OPT_TYPE_CONST, { .i64 = UNIT_AUD }, INT_MIN, INT_MAX, FLAGS, "units" },
1615  { "identifier", "Include encoder version identifier", 0, AV_OPT_TYPE_CONST, { .i64 = UNIT_SEI_IDENTIFIER }, INT_MIN, INT_MAX, FLAGS, "units" },
1616  { "timing", "Include timing parameters (buffering_period and pic_timing)", 0, AV_OPT_TYPE_CONST, { .i64 = UNIT_SEI_TIMING }, INT_MIN, INT_MAX, FLAGS, "units" },
1617  { "recovery", "Include recovery points where appropriate", 0, AV_OPT_TYPE_CONST, { .i64 = UNIT_SEI_RECOVERY }, INT_MIN, INT_MAX, FLAGS, "units" },
1618  { "a53_cc", "Include A/53 caption data", 0, AV_OPT_TYPE_CONST, { .i64 = UNIT_SEI_A53_CC }, INT_MIN, INT_MAX, FLAGS, "units" },
1619 
1620  { NULL },
1621 };
1622 
1624  { "b", "0" },
1625  { "bf", "2" },
1626  { "g", "300" },
1627  { "i_qfactor", "1" },
1628  { "i_qoffset", "0" },
1629  { "b_qfactor", "1" },
1630  { "b_qoffset", "0" },
1631  { "qmin", "-1" },
1632  { "qmax", "-1" },
1633  { NULL },
1634 };
1635 
1637  .class_name = "h264_vulkan",
1638  .item_name = av_default_item_name,
1639  .option = vulkan_encode_h264_options,
1640  .version = LIBAVUTIL_VERSION_INT,
1641 };
1642 
1644  .p.name = "h264_vulkan",
1645  CODEC_LONG_NAME("H.264/AVC (Vulkan)"),
1646  .p.type = AVMEDIA_TYPE_VIDEO,
1647  .p.id = AV_CODEC_ID_H264,
1648  .priv_data_size = sizeof(VulkanEncodeH264Context),
1651  .close = &vulkan_encode_h264_close,
1652  .p.priv_class = &vulkan_encode_h264_class,
1653  .p.capabilities = AV_CODEC_CAP_DELAY |
1658  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1659  .defaults = vulkan_encode_h264_defaults,
1660  .p.pix_fmts = (const enum AVPixelFormat[]) {
1663  },
1664  .hw_configs = ff_vulkan_encode_hw_configs,
1665  .p.wrapper_name = "vulkan",
1666 };
vk_enc_h264_default_ref_pic_list
static void vk_enc_h264_default_ref_pic_list(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, FFHWBaseEncodePicture **rpl0, FFHWBaseEncodePicture **rpl1, int *rpl_size)
Definition: vulkan_encode_h264.c:309
FFHWBaseEncodeContext::output_delay
int64_t output_delay
Definition: hw_base_encode.h:169
ff_alloc_a53_sei
int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len, void **data, size_t *sei_size)
Check AVFrame for A53 side data and allocate and fill SEI message with A53 info.
Definition: atsc_a53.c:26
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
UNIT_SEI_IDENTIFIER
@ UNIT_SEI_IDENTIFIER
Definition: vulkan_encode_h264.c:37
av_clip
#define av_clip
Definition: common.h:100
FF_HW_PICTURE_TYPE_IDR
@ FF_HW_PICTURE_TYPE_IDR
Definition: hw_base_encode.h:39
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
FFHWBaseEncodeH264Opts
Definition: hw_base_encode_h264.h:34
LIBAVCODEC_IDENT
#define LIBAVCODEC_IDENT
Definition: version.h:43
SEIRawUserDataRegistered
Definition: cbs_sei.h:33
VulkanEncodeH264Context::units
FFHWBaseEncodeH264 units
Definition: vulkan_encode_h264.c:90
FF_HW_FLAG_B_PICTURE_REFERENCES
@ FF_HW_FLAG_B_PICTURE_REFERENCES
Definition: hw_base_encode.h:55
FFHWBaseEncodeH264Opts::initial_buffer_fullness
uint64_t initial_buffer_fullness
Definition: hw_base_encode_h264.h:44
VulkanEncodeH264Context::sei_recovery_point
H264RawSEIRecoveryPoint sei_recovery_point
Definition: vulkan_encode_h264.c:110
H264_MAX_RPLM_COUNT
@ H264_MAX_RPLM_COUNT
Definition: h264.h:84
ff_vulkan_encode_hw_configs
const AVCodecHWConfigInternal *const ff_vulkan_encode_hw_configs[]
Paperwork.
Definition: vulkan_encode.c:26
base_unit_to_vk
static av_cold int base_unit_to_vk(AVCodecContext *avctx, VulkanH264Units *vk_units)
Definition: vulkan_encode_h264.c:856
FFHWBaseEncodePicture::priv
void * priv
Definition: hw_base_encode.h:63
FFHWBaseEncodePicture::codec_priv
void * codec_priv
Definition: hw_base_encode.h:65
h264_levels.h
ff_cbs_fragment_free
av_cold void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:186
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3025
AV_CODEC_CAP_HARDWARE
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition: codec.h:145
VulkanEncodeH264Picture::vkrc_layer_info
VkVideoEncodeH264RateControlLayerInfoKHR vkrc_layer_info
Definition: vulkan_encode_h264.c:69
AV_PROFILE_H264_MAIN
#define AV_PROFILE_H264_MAIN
Definition: defs.h:112
ff_cbs_sei_add_message
int ff_cbs_sei_add_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, int prefix, uint32_t payload_type, void *payload_data, void *payload_ref)
Add an SEI message to an access unit.
Definition: cbs_sei.c:267
cbs_h264.h
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:107
FFHWBaseEncodeH264Opts::flags
int flags
Definition: hw_base_encode_h264.h:35
FF_HW_FLAG_B_PICTURES
@ FF_HW_FLAG_B_PICTURES
Definition: hw_base_encode.h:53
ff_cbs_insert_unit_content
int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, void *content_ref)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:783
int64_t
long long int64_t
Definition: coverity.c:34
parse_feedback_units
static int parse_feedback_units(AVCodecContext *avctx, const uint8_t *data, size_t size, int sps_override, int pps_override)
Definition: vulkan_encode_h264.c:1049
vulkan_encode_h264_add_nal
static int vulkan_encode_h264_add_nal(AVCodecContext *avctx, CodedBitstreamFragment *au, void *nal_unit)
Definition: vulkan_encode_h264.c:1186
VulkanEncodeH264Context::sei_a53cc
SEIRawUserDataRegistered sei_a53cc
Definition: vulkan_encode_h264.c:111
VulkanEncodeH264Context::sei_identifier_string
char * sei_identifier_string
Definition: vulkan_encode_h264.c:113
create_session_params
static int create_session_params(AVCodecContext *avctx)
Definition: vulkan_encode_h264.c:1005
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
Definition: cbs.c:172
VulkanEncodeH264Picture::mods
StdVideoEncodeH264RefListModEntry mods[MAX_REFERENCE_LIST_NUM][H264_MAX_RPLM_COUNT]
Definition: vulkan_encode_h264.c:82
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:219
vulkan_encode_h264_init
static av_cold int vulkan_encode_h264_init(AVCodecContext *avctx)
Definition: vulkan_encode_h264.c:1359
H264RawHRD::dpb_output_delay_length_minus1
uint8_t dpb_output_delay_length_minus1
Definition: cbs_h264.h:54
AVOption
AVOption.
Definition: opt.h:429
ff_vulkan_write_global_header
av_cold int ff_vulkan_write_global_header(AVCodecContext *avctx, FFVulkanEncodeContext *ctx)
Write out the extradata in case its needed.
Definition: vulkan_encode.c:671
VulkanEncodeH264Context::common
FFVulkanEncodeContext common
Definition: vulkan_encode_h264.c:88
data
const char data[16]
Definition: mxf.c:148
H264RawHRD
Definition: cbs_h264.h:43
AVCodecContext::b_quant_offset
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:817
FFCodec
Definition: codec_internal.h:127
version.h
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
vulkan_encode_h264_close
static av_cold int vulkan_encode_h264_close(AVCodecContext *avctx)
Definition: vulkan_encode_h264.c:1557
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:74
ff_hw_base_encode_init_params_h264
int ff_hw_base_encode_init_params_h264(FFHWBaseEncodeContext *base_ctx, AVCodecContext *avctx, FFHWBaseEncodeH264 *common, FFHWBaseEncodeH264Opts *opts)
Definition: hw_base_encode_h264.c:26
FFVulkanEncodeContext::explicit_qp
int explicit_qp
Definition: vulkan_encode.h:171
cbs.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
avcodec_profile_name
const char * avcodec_profile_name(enum AVCodecID codec_id, int profile)
Return a name for the specified profile, if available.
Definition: utils.c:442
VulkanEncodeH264Picture::vkrc_info
VkVideoEncodeH264RateControlInfoKHR vkrc_info
Definition: vulkan_encode_h264.c:68
FFVulkanEncodeDescriptor
Definition: vulkan_encode.h:31
AVCodecContext::qmax
int qmax
maximum quantizer
Definition: avcodec.h:1273
H264RawHRD::cpb_size_scale
uint8_t cpb_size_scale
Definition: cbs_h264.h:46
VulkanH264Units::vksps_vui
StdVideoH264SequenceParameterSetVui vksps_vui
Definition: vulkan_encode_h264.c:850
AV_PIX_FMT_VULKAN
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition: pixfmt.h:379
VulkanEncodeH264Picture::units_needed
enum UnitElems units_needed
Definition: vulkan_encode_h264.c:66
ff_cbs_close
av_cold void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:142
VulkanEncodeH264Context::current_access_unit
CodedBitstreamFragment current_access_unit
Definition: vulkan_encode_h264.c:104
ff_vk_h264_profile_to_vk
StdVideoH264ProfileIdc ff_vk_h264_profile_to_vk(int profile)
Convert profile from/to AV to Vulkan.
Definition: vulkan_video.c:225
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
VulkanEncodeH264Context::fixed_qp_p
uint8_t fixed_qp_p
Definition: vulkan_encode_h264.c:95
H264_NAL_PPS
@ H264_NAL_PPS
Definition: h264.h:42
FFHWBaseEncodeContext
Definition: hw_base_encode.h:122
ff_h264_vulkan_encoder
const FFCodec ff_h264_vulkan_encoder
Definition: vulkan_encode_h264.c:1643
H264RawSEIRecoveryPoint::recovery_frame_cnt
uint16_t recovery_frame_cnt
Definition: cbs_h264.h:269
VulkanH264Units::vkpps
StdVideoH264PictureParameterSet vkpps
Definition: vulkan_encode_h264.c:852
H264RawHRD::bit_rate_scale
uint8_t bit_rate_scale
Definition: cbs_h264.h:45
H264RawSEIPicTiming
Definition: cbs_h264.h:249
AVCodecContext::i_quant_factor
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
Definition: avcodec.h:826
FFCodecDefault
Definition: codec_internal.h:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
UNIT_SEI_A53_CC
@ UNIT_SEI_A53_CC
Definition: vulkan_encode_h264.c:39
UNIT_SEI_TIMING
@ UNIT_SEI_TIMING
Definition: vulkan_encode_h264.c:36
FFHWBaseEncodePicture::type
int type
Definition: hw_base_encode.h:78
VULKAN_ENCODE_COMMON_OPTIONS
#define VULKAN_ENCODE_COMMON_OPTIONS
Definition: vulkan_encode.h:197
SEIRawUserDataUnregistered::data
uint8_t * data
RefStruct reference.
Definition: cbs_sei.h:42
FFHWBaseEncodePicture::is_reference
int is_reference
Definition: hw_base_encode.h:87
fail
#define fail()
Definition: checkasm.h:188
H264RawPPS::transform_8x8_mode_flag
uint8_t transform_8x8_mode_flag
Definition: cbs_h264.h:208
FFHWBaseEncodeH264
Definition: hw_base_encode_h264.h:25
write_extra_headers
static int write_extra_headers(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic, uint8_t *data, size_t *data_len)
Definition: vulkan_encode_h264.c:1254
H264RawHRD::cpb_size_value_minus1
uint32_t cpb_size_value_minus1[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:49
ff_cbs_read
int ff_cbs_read(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
Read a bitstream from a memory region into a fragment, then split into units and decompose.
Definition: cbs.c:325
VulkanEncodeH264Context::sei_identifier
SEIRawUserDataUnregistered sei_identifier
Definition: vulkan_encode_h264.c:108
VulkanEncodeH264Context::unit_opts
FFHWBaseEncodeH264Opts unit_opts
Definition: vulkan_encode_h264.c:91
SEIRawUserDataUnregistered
Definition: cbs_sei.h:40
FFHWBaseEncodePicture::input_image
AVFrame * input_image
Definition: hw_base_encode.h:83
VulkanEncodeH264Context::unit_elems
enum UnitElems unit_elems
Definition: vulkan_encode_h264.c:93
AV_CODEC_CAP_ENCODER_FLUSH
#define AV_CODEC_CAP_ENCODER_FLUSH
This encoder can be flushed using avcodec_flush_buffers().
Definition: codec.h:166
ff_hw_base_init_gop_structure
int ff_hw_base_init_gop_structure(FFHWBaseEncodeContext *ctx, AVCodecContext *avctx, uint32_t ref_l0, uint32_t ref_l1, int flags, int prediction_pre_only)
Definition: hw_base_encode.c:661
VulkanEncodeH264Picture::vkrc_remaining
VkVideoEncodeH264GopRemainingFrameInfoKHR vkrc_remaining
Definition: vulkan_encode_h264.c:70
H264RawPPS::pic_scaling_matrix_present_flag
uint8_t pic_scaling_matrix_present_flag
Definition: cbs_h264.h:210
FFHWBaseEncodePicture::prev
struct FFHWBaseEncodePicture * prev
Definition: hw_base_encode.h:101
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:168
init_enc_options
static int init_enc_options(AVCodecContext *avctx)
Definition: vulkan_encode_h264.c:756
SEIRawUserDataRegistered::itu_t_t35_country_code
uint8_t itu_t_t35_country_code
Definition: cbs_sei.h:34
VulkanEncodeH264Context::sei_pic_timing
H264RawSEIPicTiming sei_pic_timing
Definition: vulkan_encode_h264.c:109
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
AV_PROFILE_UNKNOWN
#define AV_PROFILE_UNKNOWN
Definition: defs.h:65
AVCodecContext::rc_initial_buffer_occupancy
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:1330
VulkanH264Units::vksps
StdVideoH264SequenceParameterSet vksps
Definition: vulkan_encode_h264.c:847
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:122
FFVulkanEncodeDescriptor::ext_props
VkExtensionProperties ext_props
Definition: vulkan_encode.h:36
FFHWBaseEncodeH264Opts::fixed_qp_idr
int fixed_qp_idr
Definition: hw_base_encode_h264.h:42
s
#define s(width, name)
Definition: cbs_vp9.c:198
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:135
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1451
H264RawNALUnitHeader::nal_unit_type
uint8_t nal_unit_type
Definition: cbs_h264.h:33
AV_PROFILE_H264_HIGH_10
#define AV_PROFILE_H264_HIGH_10
Definition: defs.h:115
FFHWBaseEncodeContext::max_b_depth
int max_b_depth
Definition: hw_base_encode.h:188
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
H264RawSPS::seq_parameter_set_id
uint8_t seq_parameter_set_id
Definition: cbs_h264.h:115
VulkanEncodeH264Picture::primary_pic_type
int primary_pic_type
Definition: vulkan_encode_h264.c:62
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
SEIRawUserDataUnregistered::data_length
size_t data_length
Definition: cbs_sei.h:43
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
FF_HW_PICTURE_TYPE_B
@ FF_HW_PICTURE_TYPE_B
Definition: hw_base_encode.h:42
init_base_units
static int init_base_units(AVCodecContext *avctx)
Definition: vulkan_encode_h264.c:1087
ctx
AVFormatContext * ctx
Definition: movenc.c:49
SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
@ SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
Definition: sei.h:34
H264RawSPS::seq_scaling_matrix_present_flag
uint8_t seq_scaling_matrix_present_flag
Definition: cbs_h264.h:123
H264_MAX_CPB_CNT
@ H264_MAX_CPB_CNT
Definition: h264.h:99
VulkanEncodeH264Picture::vkh264dpb_info
VkVideoEncodeH264DpbSlotInfoKHR vkh264dpb_info
Definition: vulkan_encode_h264.c:80
VulkanEncodeH264Context::caps
VkVideoEncodeH264CapabilitiesKHR caps
Definition: vulkan_encode_h264.c:100
AVCodecContext::rc_max_rate
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1302
h2645data.h
VulkanEncodeH264Picture::vkh264pic_info
VkVideoEncodeH264PictureInfoKHR vkh264pic_info
Definition: vulkan_encode_h264.c:77
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
VulkanEncodeH264Picture::frame_num
int frame_num
Definition: vulkan_encode_h264.c:59
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
FFHWBaseEncodeH264::raw_sps
H264RawSPS raw_sps
Definition: hw_base_encode_h264.h:26
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:461
FFHWBaseEncodeContext::b_per_p
int b_per_p
Definition: hw_base_encode.h:189
VulkanH264Units::vksps_vui_header
StdVideoH264HrdParameters vksps_vui_header
Definition: vulkan_encode_h264.c:849
AVCodecContext::rc_buffer_size
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:1287
FFHWBaseEncodePicture::dpb
struct FFHWBaseEncodePicture * dpb[MAX_DPB_SIZE]
Definition: hw_base_encode.h:93
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
hw_base_encode_h264.h
FF_HW_FLAG_INTRA_ONLY
@ FF_HW_FLAG_INTRA_ONLY
Definition: hw_base_encode.h:51
VulkanH264Units
Definition: vulkan_encode_h264.c:846
AV_LEVEL_UNKNOWN
#define AV_LEVEL_UNKNOWN
Definition: defs.h:198
FF_CODEC_RECEIVE_PACKET_CB
#define FF_CODEC_RECEIVE_PACKET_CB(func)
Definition: codec_internal.h:326
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:501
H264RawNALUnitHeader
Definition: cbs_h264.h:31
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
FFVkEncodeCommonOptions::level
int level
Definition: vulkan_encode.h:154
LEVEL
#define LEVEL(name, value)
init_sequence_headers
static av_cold int init_sequence_headers(AVCodecContext *avctx)
Definition: vulkan_encode_h264.c:813
FFVulkanContext
Definition: vulkan.h:263
FFVulkanEncodeContext::base
FFHWBaseEncodeContext base
Definition: vulkan_encode.h:168
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1794
write_access_unit
static int write_access_unit(AVCodecContext *avctx, uint8_t *data, size_t *data_len, CodedBitstreamFragment *au)
Definition: vulkan_encode_h264.c:1201
setup_refs
static void setup_refs(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, VkVideoEncodeInfoKHR *encode_info)
Definition: vulkan_encode_h264.c:406
FFVulkanEncodeContext::opts
FFVkEncodeCommonOptions opts
Definition: vulkan_encode.h:182
FFVulkanEncodeContext
Definition: vulkan_encode.h:165
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
setup_slices
static void setup_slices(AVCodecContext *avctx, FFHWBaseEncodePicture *pic)
Definition: vulkan_encode_h264.c:246
hn
static float hn(int n, EqParameter *param, float fs)
Definition: af_superequalizer.c:91
FFHWBaseEncodeH264::sei_buffering_period
H264RawSEIBufferingPeriod sei_buffering_period
Definition: hw_base_encode_h264.h:29
FFVkEncodeCommonOptions::rc_mode
VkVideoEncodeRateControlModeFlagBitsKHR rc_mode
Definition: vulkan_encode.h:161
codec_internal.h
FFHWBaseEncodePicture::nb_refs
int nb_refs[MAX_REFERENCE_LIST_NUM]
Definition: hw_base_encode.h:97
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
VulkanEncodeH264Context
Definition: vulkan_encode_h264.c:87
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
VulkanEncodeH264Picture
Definition: vulkan_encode_h264.c:58
MAX_DPB_SIZE
#define MAX_DPB_SIZE
Definition: hw_base_encode.h:26
vulkan_encode_h264_sei_identifier_uuid
static const uint8_t vulkan_encode_h264_sei_identifier_uuid[16]
Definition: vulkan_encode_h264.c:53
FFHWBaseEncodeContext::decode_delay
int64_t decode_delay
Definition: hw_base_encode.h:173
SEIRawUserDataRegistered::data
uint8_t * data
RefStruct reference.
Definition: cbs_sei.h:36
size
int size
Definition: twinvq_data.h:10344
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:128
H264_NAL_FILLER_DATA
@ H264_NAL_FILLER_DATA
Definition: h264.h:46
VulkanEncodeH264Picture::h264dpb_info
StdVideoEncodeH264ReferenceInfo h264dpb_info
Definition: vulkan_encode_h264.c:79
VulkanH264Units::vksps_scaling
StdVideoH264ScalingLists vksps_scaling
Definition: vulkan_encode_h264.c:848
vulkan_encode_h264_options
static const AVOption vulkan_encode_h264_options[]
Definition: vulkan_encode_h264.c:1566
FFHWBaseEncodeH264::raw_pps
H264RawPPS raw_pps
Definition: hw_base_encode_h264.h:27
FLAGS
#define FLAGS
Definition: vulkan_encode_h264.c:1565
VulkanEncodeH264Context::cbs
CodedBitstreamContext * cbs
Definition: vulkan_encode_h264.c:103
FFHWBaseEncodeContext::p_to_gpb
int p_to_gpb
Definition: hw_base_encode.h:194
VulkanEncodeH264Picture::slice_type
int slice_type
Definition: vulkan_encode_h264.c:63
ff_vk_h264_level_to_vk
StdVideoH264LevelIdc ff_vk_h264_level_to_vk(int level_idc)
Definition: vulkan_video.c:161
header
static const uint8_t header[24]
Definition: sdr2.c:68
FFHWBaseEncodePicture::encode_order
int64_t encode_order
Definition: hw_base_encode.h:70
vulkan_encode_h264_defaults
static const FFCodecDefault vulkan_encode_h264_defaults[]
Definition: vulkan_encode_h264.c:1623
VulkanEncodeH264Context::profile
VkVideoEncodeH264ProfileInfoKHR profile
Definition: vulkan_encode_h264.c:98
SEIRawUserDataRegistered::data_length
size_t data_length
Definition: cbs_sei.h:37
VulkanEncodeH264Picture::slice_wt
StdVideoEncodeH264WeightTable slice_wt
Definition: vulkan_encode_h264.c:72
AVCodecContext::b_quant_factor
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
Definition: avcodec.h:810
FFHWBaseEncodeH264Opts::mb_height
int mb_height
Definition: hw_base_encode_h264.h:39
FFVkEncodeCommonOptions::profile
int profile
Definition: vulkan_encode.h:153
VulkanEncodeH264Picture::slice_hdr
StdVideoEncodeH264SliceHeader slice_hdr
Definition: vulkan_encode_h264.c:73
VulkanEncodeH264Picture::h264pic_info
StdVideoEncodeH264PictureInfo h264pic_info
Definition: vulkan_encode_h264.c:76
FFHWBaseEncodeH264Opts::bit_rate
int64_t bit_rate
Definition: hw_base_encode_h264.h:40
H264RawHRD::cpb_cnt_minus1
uint8_t cpb_cnt_minus1
Definition: cbs_h264.h:44
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
FF_HW_PICTURE_TYPE_P
@ FF_HW_PICTURE_TYPE_P
Definition: hw_base_encode.h:41
ff_vulkan_encode_receive_packet
int ff_vulkan_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Encode.
Definition: vulkan_encode.c:541
H264RawHRD::cbr_flag
uint8_t cbr_flag[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:50
VulkanEncodeH264Picture::pic_order_cnt
int pic_order_cnt
Definition: vulkan_encode_h264.c:64
SEI_TYPE_PIC_TIMING
@ SEI_TYPE_PIC_TIMING
Definition: sei.h:31
MAX_REFERENCE_LIST_NUM
#define MAX_REFERENCE_LIST_NUM
Definition: hw_base_encode.h:30
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
write_sequence_headers
static int write_sequence_headers(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic, uint8_t *data, size_t *data_len)
Definition: vulkan_encode_h264.c:1225
H264RawSPS::pic_order_cnt_type
uint8_t pic_order_cnt_type
Definition: cbs_h264.h:129
FFHWBaseEncodePicture::refs
struct FFHWBaseEncodePicture * refs[MAX_REFERENCE_LIST_NUM][MAX_PICTURE_REFERENCES]
Definition: hw_base_encode.h:98
vulkan_encode.h
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
UNIT_AUD
@ UNIT_AUD
Definition: vulkan_encode_h264.c:35
profile
int profile
Definition: mxfenc.c:2228
AVCodecContext::height
int height
Definition: avcodec.h:624
FFVulkanEncodeDescriptor::codec_id
enum AVCodecID codec_id
Definition: vulkan_encode.h:32
ff_cbs_write_fragment_data
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:409
init_profile
static int init_profile(AVCodecContext *avctx, VkVideoProfileInfoKHR *profile, void *pnext)
Definition: vulkan_encode_h264.c:678
init_pic_rc
static int init_pic_rc(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, VkVideoEncodeRateControlInfoKHR *rc_info, VkVideoEncodeRateControlLayerInfoKHR *rc_layer)
Definition: vulkan_encode_h264.c:116
CODEC_VER
#define CODEC_VER(ver)
Definition: vulkan_video.h:30
vk_enc_h264_update_pic_info
static int vk_enc_h264_update_pic_info(AVCodecContext *avctx, FFHWBaseEncodePicture *pic)
Definition: vulkan_encode_h264.c:161
H264RawPPS::num_ref_idx_l0_default_active_minus1
uint8_t num_ref_idx_l0_default_active_minus1
Definition: cbs_h264.h:192
VulkanEncodeH264Context::sei_a53cc_data
void * sei_a53cc_data
Definition: vulkan_encode_h264.c:112
ret
ret
Definition: filter_design.txt:187
VulkanEncodeH264Picture::last_idr_frame
int64_t last_idr_frame
Definition: vulkan_encode_h264.c:60
FFHWBaseEncodePicture
Definition: hw_base_encode.h:61
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
atsc_a53.h
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
PROFILE
#define PROFILE(name, value)
H264RawHRD::bit_rate_value_minus1
uint32_t bit_rate_value_minus1[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:48
H264RawHRD::cpb_removal_delay_length_minus1
uint8_t cpb_removal_delay_length_minus1
Definition: cbs_h264.h:53
AVCodecContext
main external API structure.
Definition: avcodec.h:451
H264RawAUD
Definition: cbs_h264.h:218
AV_PROFILE_H264_HIGH
#define AV_PROFILE_H264_HIGH
Definition: defs.h:114
FF_HW_FLAG_NON_IDR_KEY_PICTURES
@ FF_HW_FLAG_NON_IDR_KEY_PICTURES
Definition: hw_base_encode.h:58
FF_HW_PICTURE_TYPE_I
@ FF_HW_PICTURE_TYPE_I
Definition: hw_base_encode.h:40
UNIT_SEI_RECOVERY
@ UNIT_SEI_RECOVERY
Definition: vulkan_encode_h264.c:38
VulkanH264Units::vkpps_scaling
StdVideoH264ScalingLists vkpps_scaling
Definition: vulkan_encode_h264.c:853
SEI_TYPE_BUFFERING_PERIOD
@ SEI_TYPE_BUFFERING_PERIOD
Definition: sei.h:30
SEI_TYPE_USER_DATA_UNREGISTERED
@ SEI_TYPE_USER_DATA_UNREGISTERED
Definition: sei.h:35
VulkanEncodeH264Context::quality_props
VkVideoEncodeH264QualityLevelPropertiesKHR quality_props
Definition: vulkan_encode_h264.c:101
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1266
enc_cb
static const FFVulkanCodec enc_cb
Definition: vulkan_encode_h264.c:1345
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1650
ff_vulkan_encode_init
av_cold int ff_vulkan_encode_init(AVCodecContext *avctx, FFVulkanEncodeContext *ctx, const FFVulkanEncodeDescriptor *vk_desc, const FFVulkanCodec *codec, void *codec_caps, void *quality_pnext)
Initialize encoder.
Definition: vulkan_encode.c:701
vulkan_encode_h264_class
static const AVClass vulkan_encode_h264_class
Definition: vulkan_encode_h264.c:1636
filler
int(* filler)(InterplayACMContext *s, unsigned ind, unsigned col)
Definition: interplayacm.c:421
AVCodecContext::i_quant_offset
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:833
ff_vk_enc_h264_desc
const FFVulkanEncodeDescriptor ff_vk_enc_h264_desc
Definition: vulkan_encode_h264.c:42
pps
uint64_t pps
Definition: dovi_rpuenc.c:35
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
FFHWBaseEncodeH264Opts::mb_width
int mb_width
Definition: hw_base_encode_h264.h:38
VulkanEncodeH264Context::raw_aud
H264RawAUD raw_aud
Definition: vulkan_encode_h264.c:106
AV_PROFILE_H264_CONSTRAINED_BASELINE
#define AV_PROFILE_H264_CONSTRAINED_BASELINE
Definition: defs.h:111
UnitElems
UnitElems
Definition: vulkan_encode_h264.c:34
VulkanEncodeH264Picture::mmco
StdVideoEncodeH264RefPicMarkingEntry mmco[H264_MAX_RPLM_COUNT]
Definition: vulkan_encode_h264.c:83
VulkanEncodeH264Context::fixed_qp_b
uint8_t fixed_qp_b
Definition: vulkan_encode_h264.c:96
SEI_TYPE_RECOVERY_POINT
@ SEI_TYPE_RECOVERY_POINT
Definition: sei.h:36
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
H264RawSEIRecoveryPoint
Definition: cbs_h264.h:268
mem.h
H264RawSEIPicTiming::cpb_removal_delay
uint32_t cpb_removal_delay
Definition: cbs_h264.h:250
ff_vulkan_encode_uninit
av_cold void ff_vulkan_encode_uninit(FFVulkanEncodeContext *ctx)
Uninitialize encoder.
Definition: vulkan_encode.c:31
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
ff_cbs_init
av_cold 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:90
FF_VK_EXT_VIDEO_ENCODE_H264
#define FF_VK_EXT_VIDEO_ENCODE_H264
Definition: vulkan_functions.h:62
H264RawPPS::pic_parameter_set_id
uint8_t pic_parameter_set_id
Definition: cbs_h264.h:174
OFFSET
#define OFFSET(x)
Definition: vulkan_encode_h264.c:1564
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
FFVulkanCodec::flags
int flags
Codec feature flags.
Definition: vulkan_encode.h:94
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
VulkanEncodeH264Picture::ref_list_info
StdVideoEncodeH264ReferenceListsInfo ref_list_info
Definition: vulkan_encode_h264.c:84
FF_HW_H264_SEI_TIMING
#define FF_HW_H264_SEI_TIMING
Definition: hw_base_encode_h264.h:36
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FFVulkanCodec
Definition: vulkan_encode.h:90
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
AV_OPT_TYPE_FLAGS
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition: opt.h:255
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
H264RawFiller
Definition: cbs_h264.h:417
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
H264RawFiller::nal_unit_header
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:418
SEIRawUserDataUnregistered::uuid_iso_iec_11578
uint8_t uuid_iso_iec_11578[16]
Definition: cbs_sei.h:41
H264RawHRD::initial_cpb_removal_delay_length_minus1
uint8_t initial_cpb_removal_delay_length_minus1
Definition: cbs_h264.h:52
HW_BASE_ENCODE_COMMON_OPTIONS
#define HW_BASE_ENCODE_COMMON_OPTIONS
Definition: hw_base_encode.h:239
VulkanEncodeH264Picture::vkslice
VkVideoEncodeH264NaluSliceInfoKHR vkslice
Definition: vulkan_encode_h264.c:74
write_filler
static int write_filler(AVCodecContext *avctx, uint32_t filler, uint8_t *data, size_t *data_len)
Definition: vulkan_encode_h264.c:1321
H264RawPPS::pic_init_qp_minus26
int8_t pic_init_qp_minus26
Definition: cbs_h264.h:198
FFHWBaseEncodePicture::display_order
int64_t display_order
Definition: hw_base_encode.h:69
FFVulkanEncodePicture::dpb_slot
VkVideoReferenceSlotInfoKHR dpb_slot
Definition: vulkan_encode.h:42
init_pic_params
static int init_pic_params(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, VkVideoEncodeInfoKHR *encode_info)
Definition: vulkan_encode_h264.c:605
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
snprintf
#define snprintf
Definition: snprintf.h:34
FFVulkanFunctions
Definition: vulkan_functions.h:263
ff_vulkan_encode_create_session_params
int ff_vulkan_encode_create_session_params(AVCodecContext *avctx, FFVulkanEncodeContext *ctx, void *codec_params_pnext)
Create session parameters.
Definition: vulkan_encode.c:1026
FFVulkanEncodePicture
Definition: vulkan_encode.h:39
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:153
FFHWBaseEncodePicture::nb_dpb_pics
int nb_dpb_pics
Definition: hw_base_encode.h:92
VulkanEncodeH264Picture::idr_pic_id
uint16_t idr_pic_id
Definition: vulkan_encode_h264.c:61
H264RawHRD::time_offset_length
uint8_t time_offset_length
Definition: cbs_h264.h:55
FFHWBaseEncodeH264Opts::hrd_buffer_size
uint64_t hrd_buffer_size
Definition: hw_base_encode_h264.h:43
H264RawSPS
Definition: cbs_h264.h:102
H264RawPPS
Definition: cbs_h264.h:171