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