You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1112 lines
39KB

  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. #include <string.h>
  19. #include <va/va.h>
  20. #include <va/va_enc_h264.h>
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/internal.h"
  24. #include "libavutil/opt.h"
  25. #include "avcodec.h"
  26. #include "cbs.h"
  27. #include "cbs_h264.h"
  28. #include "h264.h"
  29. #include "h264_levels.h"
  30. #include "h264_sei.h"
  31. #include "internal.h"
  32. #include "vaapi_encode.h"
  33. enum {
  34. SEI_TIMING = 0x01,
  35. SEI_IDENTIFIER = 0x02,
  36. SEI_RECOVERY_POINT = 0x04,
  37. };
  38. // Random (version 4) ISO 11578 UUID.
  39. static const uint8_t vaapi_encode_h264_sei_identifier_uuid[16] = {
  40. 0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
  41. 0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
  42. };
  43. typedef struct VAAPIEncodeH264Context {
  44. VAAPIEncodeContext common;
  45. // User options.
  46. int qp;
  47. int quality;
  48. int coder;
  49. int aud;
  50. int sei;
  51. int profile;
  52. int level;
  53. // Derived settings.
  54. int mb_width;
  55. int mb_height;
  56. int fixed_qp_idr;
  57. int fixed_qp_p;
  58. int fixed_qp_b;
  59. // Stream state.
  60. int frame_num;
  61. int pic_order_cnt;
  62. int next_frame_num;
  63. int64_t last_idr_frame;
  64. int64_t idr_pic_count;
  65. int primary_pic_type;
  66. int slice_type;
  67. int cpb_delay;
  68. int dpb_delay;
  69. // Writer structures.
  70. CodedBitstreamContext *cbc;
  71. CodedBitstreamFragment current_access_unit;
  72. H264RawAUD raw_aud;
  73. H264RawSPS raw_sps;
  74. H264RawPPS raw_pps;
  75. H264RawSEI raw_sei;
  76. H264RawSlice raw_slice;
  77. H264RawSEIBufferingPeriod sei_buffering_period;
  78. H264RawSEIPicTiming sei_pic_timing;
  79. H264RawSEIRecoveryPoint sei_recovery_point;
  80. H264RawSEIUserDataUnregistered sei_identifier;
  81. char *sei_identifier_string;
  82. int aud_needed;
  83. int sei_needed;
  84. int sei_cbr_workaround_needed;
  85. } VAAPIEncodeH264Context;
  86. static int vaapi_encode_h264_write_access_unit(AVCodecContext *avctx,
  87. char *data, size_t *data_len,
  88. CodedBitstreamFragment *au)
  89. {
  90. VAAPIEncodeH264Context *priv = avctx->priv_data;
  91. int err;
  92. err = ff_cbs_write_fragment_data(priv->cbc, au);
  93. if (err < 0) {
  94. av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
  95. return err;
  96. }
  97. if (*data_len < 8 * au->data_size - au->data_bit_padding) {
  98. av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
  99. "%zu < %zu.\n", *data_len,
  100. 8 * au->data_size - au->data_bit_padding);
  101. return AVERROR(ENOSPC);
  102. }
  103. memcpy(data, au->data, au->data_size);
  104. *data_len = 8 * au->data_size - au->data_bit_padding;
  105. return 0;
  106. }
  107. static int vaapi_encode_h264_add_nal(AVCodecContext *avctx,
  108. CodedBitstreamFragment *au,
  109. void *nal_unit)
  110. {
  111. VAAPIEncodeH264Context *priv = avctx->priv_data;
  112. H264RawNALUnitHeader *header = nal_unit;
  113. int err;
  114. err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
  115. header->nal_unit_type, nal_unit, NULL);
  116. if (err < 0) {
  117. av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
  118. "type = %d.\n", header->nal_unit_type);
  119. return err;
  120. }
  121. return 0;
  122. }
  123. static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
  124. char *data, size_t *data_len)
  125. {
  126. VAAPIEncodeH264Context *priv = avctx->priv_data;
  127. CodedBitstreamFragment *au = &priv->current_access_unit;
  128. int err;
  129. if (priv->aud_needed) {
  130. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
  131. if (err < 0)
  132. goto fail;
  133. priv->aud_needed = 0;
  134. }
  135. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_sps);
  136. if (err < 0)
  137. goto fail;
  138. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_pps);
  139. if (err < 0)
  140. goto fail;
  141. err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
  142. fail:
  143. ff_cbs_fragment_uninit(priv->cbc, au);
  144. return err;
  145. }
  146. static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
  147. VAAPIEncodePicture *pic,
  148. VAAPIEncodeSlice *slice,
  149. char *data, size_t *data_len)
  150. {
  151. VAAPIEncodeH264Context *priv = avctx->priv_data;
  152. CodedBitstreamFragment *au = &priv->current_access_unit;
  153. int err;
  154. if (priv->aud_needed) {
  155. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
  156. if (err < 0)
  157. goto fail;
  158. priv->aud_needed = 0;
  159. }
  160. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_slice);
  161. if (err < 0)
  162. goto fail;
  163. err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
  164. fail:
  165. ff_cbs_fragment_uninit(priv->cbc, au);
  166. return err;
  167. }
  168. static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
  169. VAAPIEncodePicture *pic,
  170. int index, int *type,
  171. char *data, size_t *data_len)
  172. {
  173. VAAPIEncodeH264Context *priv = avctx->priv_data;
  174. CodedBitstreamFragment *au = &priv->current_access_unit;
  175. int err, i;
  176. if (priv->sei_needed) {
  177. H264RawSEI *sei = &priv->raw_sei;
  178. if (priv->aud_needed) {
  179. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
  180. if (err < 0)
  181. goto fail;
  182. priv->aud_needed = 0;
  183. }
  184. *sei = (H264RawSEI) {
  185. .nal_unit_header = {
  186. .nal_unit_type = H264_NAL_SEI,
  187. },
  188. };
  189. i = 0;
  190. if (priv->sei_needed & SEI_IDENTIFIER) {
  191. sei->payload[i].payload_type = H264_SEI_TYPE_USER_DATA_UNREGISTERED;
  192. sei->payload[i].payload.user_data_unregistered = priv->sei_identifier;
  193. ++i;
  194. }
  195. if (priv->sei_needed & SEI_TIMING) {
  196. if (pic->type == PICTURE_TYPE_IDR) {
  197. sei->payload[i].payload_type = H264_SEI_TYPE_BUFFERING_PERIOD;
  198. sei->payload[i].payload.buffering_period = priv->sei_buffering_period;
  199. ++i;
  200. }
  201. sei->payload[i].payload_type = H264_SEI_TYPE_PIC_TIMING;
  202. sei->payload[i].payload.pic_timing = priv->sei_pic_timing;
  203. ++i;
  204. }
  205. if (priv->sei_needed & SEI_RECOVERY_POINT) {
  206. sei->payload[i].payload_type = H264_SEI_TYPE_RECOVERY_POINT;
  207. sei->payload[i].payload.recovery_point = priv->sei_recovery_point;
  208. ++i;
  209. }
  210. sei->payload_count = i;
  211. av_assert0(sei->payload_count > 0);
  212. err = vaapi_encode_h264_add_nal(avctx, au, sei);
  213. if (err < 0)
  214. goto fail;
  215. priv->sei_needed = 0;
  216. err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
  217. if (err < 0)
  218. goto fail;
  219. ff_cbs_fragment_uninit(priv->cbc, au);
  220. *type = VAEncPackedHeaderRawData;
  221. return 0;
  222. #if !CONFIG_VAAPI_1
  223. } else if (priv->sei_cbr_workaround_needed) {
  224. // Insert a zero-length header using the old SEI type. This is
  225. // required to avoid triggering broken behaviour on Intel platforms
  226. // in CBR mode where an invalid SEI message is generated by the
  227. // driver and inserted into the stream.
  228. *data_len = 0;
  229. *type = VAEncPackedHeaderH264_SEI;
  230. priv->sei_cbr_workaround_needed = 0;
  231. return 0;
  232. #endif
  233. } else {
  234. return AVERROR_EOF;
  235. }
  236. fail:
  237. ff_cbs_fragment_uninit(priv->cbc, au);
  238. return err;
  239. }
  240. static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
  241. {
  242. VAAPIEncodeContext *ctx = avctx->priv_data;
  243. VAAPIEncodeH264Context *priv = avctx->priv_data;
  244. H264RawSPS *sps = &priv->raw_sps;
  245. H264RawPPS *pps = &priv->raw_pps;
  246. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  247. VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
  248. int dpb_frames;
  249. memset(&priv->current_access_unit, 0,
  250. sizeof(priv->current_access_unit));
  251. memset(sps, 0, sizeof(*sps));
  252. memset(pps, 0, sizeof(*pps));
  253. sps->nal_unit_header.nal_ref_idc = 3;
  254. sps->nal_unit_header.nal_unit_type = H264_NAL_SPS;
  255. sps->profile_idc = avctx->profile & 0xff;
  256. if (avctx->profile == FF_PROFILE_H264_CONSTRAINED_BASELINE ||
  257. avctx->profile == FF_PROFILE_H264_MAIN)
  258. sps->constraint_set1_flag = 1;
  259. if (avctx->profile == FF_PROFILE_H264_HIGH)
  260. sps->constraint_set3_flag = ctx->gop_size == 1;
  261. if (avctx->profile == FF_PROFILE_H264_MAIN ||
  262. avctx->profile == FF_PROFILE_H264_HIGH) {
  263. sps->constraint_set4_flag = 1;
  264. sps->constraint_set5_flag = ctx->b_per_p == 0;
  265. }
  266. if (ctx->gop_size == 1)
  267. dpb_frames = 0;
  268. else
  269. dpb_frames = 1 + (ctx->b_per_p > 0);
  270. if (avctx->level != FF_LEVEL_UNKNOWN) {
  271. sps->level_idc = avctx->level;
  272. } else {
  273. const H264LevelDescriptor *level;
  274. level = ff_h264_guess_level(sps->profile_idc,
  275. avctx->bit_rate,
  276. priv->mb_width * 16,
  277. priv->mb_height * 16,
  278. dpb_frames);
  279. if (level) {
  280. av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
  281. if (level->constraint_set3_flag)
  282. sps->constraint_set3_flag = 1;
  283. sps->level_idc = level->level_idc;
  284. } else {
  285. av_log(avctx, AV_LOG_WARNING, "Stream will not conform "
  286. "to any level: using level 6.2.\n");
  287. sps->level_idc = 62;
  288. }
  289. }
  290. sps->seq_parameter_set_id = 0;
  291. sps->chroma_format_idc = 1;
  292. sps->log2_max_frame_num_minus4 = 4;
  293. sps->pic_order_cnt_type = 0;
  294. sps->log2_max_pic_order_cnt_lsb_minus4 =
  295. av_clip(av_log2(ctx->b_per_p + 1) - 2, 0, 12);
  296. sps->max_num_ref_frames = dpb_frames;
  297. sps->pic_width_in_mbs_minus1 = priv->mb_width - 1;
  298. sps->pic_height_in_map_units_minus1 = priv->mb_height - 1;
  299. sps->frame_mbs_only_flag = 1;
  300. sps->direct_8x8_inference_flag = 1;
  301. if (avctx->width != 16 * priv->mb_width ||
  302. avctx->height != 16 * priv->mb_height) {
  303. sps->frame_cropping_flag = 1;
  304. sps->frame_crop_left_offset = 0;
  305. sps->frame_crop_right_offset =
  306. (16 * priv->mb_width - avctx->width) / 2;
  307. sps->frame_crop_top_offset = 0;
  308. sps->frame_crop_bottom_offset =
  309. (16 * priv->mb_height - avctx->height) / 2;
  310. } else {
  311. sps->frame_cropping_flag = 0;
  312. }
  313. sps->vui_parameters_present_flag = 1;
  314. if (avctx->sample_aspect_ratio.num != 0 &&
  315. avctx->sample_aspect_ratio.den != 0) {
  316. static const AVRational sar_idc[] = {
  317. { 0, 0 },
  318. { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
  319. { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
  320. { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
  321. { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
  322. };
  323. int num, den, i;
  324. av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
  325. avctx->sample_aspect_ratio.den, 65535);
  326. for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
  327. if (num == sar_idc[i].num &&
  328. den == sar_idc[i].den) {
  329. sps->vui.aspect_ratio_idc = i;
  330. break;
  331. }
  332. }
  333. if (i >= FF_ARRAY_ELEMS(sar_idc)) {
  334. sps->vui.aspect_ratio_idc = 255;
  335. sps->vui.sar_width = num;
  336. sps->vui.sar_height = den;
  337. }
  338. sps->vui.aspect_ratio_info_present_flag = 1;
  339. }
  340. if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
  341. avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  342. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  343. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  344. sps->vui.video_signal_type_present_flag = 1;
  345. sps->vui.video_format = 5; // Unspecified.
  346. sps->vui.video_full_range_flag =
  347. avctx->color_range == AVCOL_RANGE_JPEG;
  348. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  349. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  350. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  351. sps->vui.colour_description_present_flag = 1;
  352. sps->vui.colour_primaries = avctx->color_primaries;
  353. sps->vui.transfer_characteristics = avctx->color_trc;
  354. sps->vui.matrix_coefficients = avctx->colorspace;
  355. }
  356. } else {
  357. sps->vui.video_format = 5;
  358. sps->vui.video_full_range_flag = 0;
  359. sps->vui.colour_primaries = avctx->color_primaries;
  360. sps->vui.transfer_characteristics = avctx->color_trc;
  361. sps->vui.matrix_coefficients = avctx->colorspace;
  362. }
  363. if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
  364. sps->vui.chroma_loc_info_present_flag = 1;
  365. sps->vui.chroma_sample_loc_type_top_field =
  366. sps->vui.chroma_sample_loc_type_bottom_field =
  367. avctx->chroma_sample_location - 1;
  368. }
  369. sps->vui.timing_info_present_flag = 1;
  370. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  371. sps->vui.num_units_in_tick = avctx->framerate.den;
  372. sps->vui.time_scale = 2 * avctx->framerate.num;
  373. sps->vui.fixed_frame_rate_flag = 1;
  374. } else {
  375. sps->vui.num_units_in_tick = avctx->time_base.num;
  376. sps->vui.time_scale = 2 * avctx->time_base.den;
  377. sps->vui.fixed_frame_rate_flag = 0;
  378. }
  379. if (priv->sei & SEI_TIMING) {
  380. H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
  381. H264RawSEIBufferingPeriod *bp = &priv->sei_buffering_period;
  382. sps->vui.nal_hrd_parameters_present_flag = 1;
  383. hrd->cpb_cnt_minus1 = 0;
  384. // Try to scale these to a sensible range so that the
  385. // golomb encode of the value is not overlong.
  386. hrd->bit_rate_scale =
  387. av_clip_uintp2(av_log2(ctx->va_bit_rate) - 15 - 6, 4);
  388. hrd->bit_rate_value_minus1[0] =
  389. (ctx->va_bit_rate >> hrd->bit_rate_scale + 6) - 1;
  390. hrd->cpb_size_scale =
  391. av_clip_uintp2(av_log2(ctx->hrd_params.hrd.buffer_size) - 15 - 4, 4);
  392. hrd->cpb_size_value_minus1[0] =
  393. (ctx->hrd_params.hrd.buffer_size >> hrd->cpb_size_scale + 4) - 1;
  394. // CBR mode as defined for the HRD cannot be achieved without filler
  395. // data, so this flag cannot be set even with VAAPI CBR modes.
  396. hrd->cbr_flag[0] = 0;
  397. hrd->initial_cpb_removal_delay_length_minus1 = 23;
  398. hrd->cpb_removal_delay_length_minus1 = 23;
  399. hrd->dpb_output_delay_length_minus1 = 7;
  400. hrd->time_offset_length = 0;
  401. bp->seq_parameter_set_id = sps->seq_parameter_set_id;
  402. // This calculation can easily overflow 32 bits.
  403. bp->nal.initial_cpb_removal_delay[0] = 90000 *
  404. (uint64_t)ctx->hrd_params.hrd.initial_buffer_fullness /
  405. ctx->hrd_params.hrd.buffer_size;
  406. bp->nal.initial_cpb_removal_delay_offset[0] = 0;
  407. } else {
  408. sps->vui.nal_hrd_parameters_present_flag = 0;
  409. sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag;
  410. }
  411. sps->vui.bitstream_restriction_flag = 1;
  412. sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
  413. sps->vui.log2_max_mv_length_horizontal = 15;
  414. sps->vui.log2_max_mv_length_vertical = 15;
  415. sps->vui.max_num_reorder_frames = (ctx->b_per_p > 0);
  416. sps->vui.max_dec_frame_buffering = sps->max_num_ref_frames;
  417. pps->nal_unit_header.nal_ref_idc = 3;
  418. pps->nal_unit_header.nal_unit_type = H264_NAL_PPS;
  419. pps->pic_parameter_set_id = 0;
  420. pps->seq_parameter_set_id = 0;
  421. pps->entropy_coding_mode_flag =
  422. !(sps->profile_idc == FF_PROFILE_H264_BASELINE ||
  423. sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
  424. sps->profile_idc == FF_PROFILE_H264_CAVLC_444);
  425. if (!priv->coder && pps->entropy_coding_mode_flag)
  426. pps->entropy_coding_mode_flag = 0;
  427. pps->num_ref_idx_l0_default_active_minus1 = 0;
  428. pps->num_ref_idx_l1_default_active_minus1 = 0;
  429. pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
  430. if (sps->profile_idc == FF_PROFILE_H264_BASELINE ||
  431. sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
  432. sps->profile_idc == FF_PROFILE_H264_MAIN) {
  433. pps->more_rbsp_data = 0;
  434. } else {
  435. pps->more_rbsp_data = 1;
  436. pps->transform_8x8_mode_flag = 1;
  437. }
  438. *vseq = (VAEncSequenceParameterBufferH264) {
  439. .seq_parameter_set_id = sps->seq_parameter_set_id,
  440. .level_idc = sps->level_idc,
  441. .intra_period = ctx->gop_size,
  442. .intra_idr_period = ctx->gop_size,
  443. .ip_period = ctx->b_per_p + 1,
  444. .bits_per_second = ctx->va_bit_rate,
  445. .max_num_ref_frames = sps->max_num_ref_frames,
  446. .picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1,
  447. .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
  448. .seq_fields.bits = {
  449. .chroma_format_idc = sps->chroma_format_idc,
  450. .frame_mbs_only_flag = sps->frame_mbs_only_flag,
  451. .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
  452. .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
  453. .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
  454. .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
  455. .pic_order_cnt_type = sps->pic_order_cnt_type,
  456. .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
  457. .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
  458. },
  459. .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
  460. .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
  461. .frame_cropping_flag = sps->frame_cropping_flag,
  462. .frame_crop_left_offset = sps->frame_crop_left_offset,
  463. .frame_crop_right_offset = sps->frame_crop_right_offset,
  464. .frame_crop_top_offset = sps->frame_crop_top_offset,
  465. .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
  466. .vui_parameters_present_flag = sps->vui_parameters_present_flag,
  467. .vui_fields.bits = {
  468. .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
  469. .timing_info_present_flag = sps->vui.timing_info_present_flag,
  470. .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
  471. .log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal,
  472. .log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical,
  473. },
  474. .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
  475. .sar_width = sps->vui.sar_width,
  476. .sar_height = sps->vui.sar_height,
  477. .num_units_in_tick = sps->vui.num_units_in_tick,
  478. .time_scale = sps->vui.time_scale,
  479. };
  480. *vpic = (VAEncPictureParameterBufferH264) {
  481. .CurrPic = {
  482. .picture_id = VA_INVALID_ID,
  483. .flags = VA_PICTURE_H264_INVALID,
  484. },
  485. .coded_buf = VA_INVALID_ID,
  486. .pic_parameter_set_id = pps->pic_parameter_set_id,
  487. .seq_parameter_set_id = pps->seq_parameter_set_id,
  488. .pic_init_qp = pps->pic_init_qp_minus26 + 26,
  489. .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
  490. .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
  491. .chroma_qp_index_offset = pps->chroma_qp_index_offset,
  492. .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
  493. .pic_fields.bits = {
  494. .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
  495. .weighted_pred_flag = pps->weighted_pred_flag,
  496. .weighted_bipred_idc = pps->weighted_bipred_idc,
  497. .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
  498. .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
  499. .deblocking_filter_control_present_flag =
  500. pps->deblocking_filter_control_present_flag,
  501. .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
  502. .pic_order_present_flag =
  503. pps->bottom_field_pic_order_in_frame_present_flag,
  504. .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
  505. },
  506. };
  507. return 0;
  508. }
  509. static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
  510. VAAPIEncodePicture *pic)
  511. {
  512. VAAPIEncodeContext *ctx = avctx->priv_data;
  513. VAAPIEncodeH264Context *priv = avctx->priv_data;
  514. H264RawSPS *sps = &priv->raw_sps;
  515. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  516. int i;
  517. memset(&priv->current_access_unit, 0,
  518. sizeof(priv->current_access_unit));
  519. if (pic->type == PICTURE_TYPE_IDR) {
  520. av_assert0(pic->display_order == pic->encode_order);
  521. priv->frame_num = 0;
  522. priv->next_frame_num = 1;
  523. priv->cpb_delay = 0;
  524. priv->last_idr_frame = pic->display_order;
  525. ++priv->idr_pic_count;
  526. priv->slice_type = 7;
  527. priv->primary_pic_type = 0;
  528. } else {
  529. priv->frame_num = priv->next_frame_num;
  530. if (pic->type != PICTURE_TYPE_B) {
  531. // Reference picture, so frame_num advances.
  532. priv->next_frame_num = (priv->frame_num + 1) &
  533. ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
  534. }
  535. ++priv->cpb_delay;
  536. if (pic->type == PICTURE_TYPE_I) {
  537. priv->slice_type = 7;
  538. priv->primary_pic_type = 0;
  539. } else if (pic->type == PICTURE_TYPE_P) {
  540. priv->slice_type = 5;
  541. priv->primary_pic_type = 1;
  542. } else {
  543. priv->slice_type = 6;
  544. priv->primary_pic_type = 2;
  545. }
  546. }
  547. priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
  548. priv->dpb_delay = pic->display_order - pic->encode_order + 1;
  549. if (priv->aud) {
  550. priv->aud_needed = 1;
  551. priv->raw_aud = (H264RawAUD) {
  552. .nal_unit_header = {
  553. .nal_unit_type = H264_NAL_AUD,
  554. },
  555. .primary_pic_type = priv->primary_pic_type,
  556. };
  557. } else {
  558. priv->aud_needed = 0;
  559. }
  560. priv->sei_needed = 0;
  561. if (priv->sei & SEI_IDENTIFIER && pic->encode_order == 0)
  562. priv->sei_needed |= SEI_IDENTIFIER;
  563. #if !CONFIG_VAAPI_1
  564. if (ctx->va_rc_mode == VA_RC_CBR)
  565. priv->sei_cbr_workaround_needed = 1;
  566. #endif
  567. if (priv->sei & SEI_TIMING) {
  568. priv->sei_pic_timing = (H264RawSEIPicTiming) {
  569. .cpb_removal_delay = 2 * priv->cpb_delay,
  570. .dpb_output_delay = 2 * priv->dpb_delay,
  571. };
  572. priv->sei_needed |= SEI_TIMING;
  573. }
  574. if (priv->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
  575. priv->sei_recovery_point = (H264RawSEIRecoveryPoint) {
  576. .recovery_frame_cnt = 0,
  577. .exact_match_flag = 1,
  578. .broken_link_flag = ctx->b_per_p > 0,
  579. };
  580. priv->sei_needed |= SEI_RECOVERY_POINT;
  581. }
  582. vpic->CurrPic = (VAPictureH264) {
  583. .picture_id = pic->recon_surface,
  584. .frame_idx = priv->frame_num,
  585. .flags = 0,
  586. .TopFieldOrderCnt = priv->pic_order_cnt,
  587. .BottomFieldOrderCnt = priv->pic_order_cnt,
  588. };
  589. for (i = 0; i < pic->nb_refs; i++) {
  590. VAAPIEncodePicture *ref = pic->refs[i];
  591. unsigned int frame_num = (ref->encode_order - priv->last_idr_frame) &
  592. ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
  593. unsigned int pic_order_cnt = ref->display_order - priv->last_idr_frame;
  594. av_assert0(ref && ref->encode_order < pic->encode_order);
  595. vpic->ReferenceFrames[i] = (VAPictureH264) {
  596. .picture_id = ref->recon_surface,
  597. .frame_idx = frame_num,
  598. .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
  599. .TopFieldOrderCnt = pic_order_cnt,
  600. .BottomFieldOrderCnt = pic_order_cnt,
  601. };
  602. }
  603. for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
  604. vpic->ReferenceFrames[i] = (VAPictureH264) {
  605. .picture_id = VA_INVALID_ID,
  606. .flags = VA_PICTURE_H264_INVALID,
  607. };
  608. }
  609. vpic->coded_buf = pic->output_buffer;
  610. vpic->frame_num = priv->frame_num;
  611. vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
  612. vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
  613. return 0;
  614. }
  615. static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
  616. VAAPIEncodePicture *pic,
  617. VAAPIEncodeSlice *slice)
  618. {
  619. VAAPIEncodeH264Context *priv = avctx->priv_data;
  620. H264RawSPS *sps = &priv->raw_sps;
  621. H264RawPPS *pps = &priv->raw_pps;
  622. H264RawSliceHeader *sh = &priv->raw_slice.header;
  623. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  624. VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
  625. int i;
  626. if (pic->type == PICTURE_TYPE_IDR) {
  627. sh->nal_unit_header.nal_unit_type = H264_NAL_IDR_SLICE;
  628. sh->nal_unit_header.nal_ref_idc = 3;
  629. } else {
  630. sh->nal_unit_header.nal_unit_type = H264_NAL_SLICE;
  631. sh->nal_unit_header.nal_ref_idc = pic->type != PICTURE_TYPE_B;
  632. }
  633. sh->first_mb_in_slice = slice->block_start;
  634. sh->slice_type = priv->slice_type;
  635. sh->pic_parameter_set_id = pps->pic_parameter_set_id;
  636. sh->frame_num = priv->frame_num;
  637. sh->idr_pic_id = priv->idr_pic_count;
  638. sh->pic_order_cnt_lsb = priv->pic_order_cnt &
  639. ((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
  640. sh->direct_spatial_mv_pred_flag = 1;
  641. if (pic->type == PICTURE_TYPE_B)
  642. sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26);
  643. else if (pic->type == PICTURE_TYPE_P)
  644. sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26);
  645. else
  646. sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26);
  647. vslice->macroblock_address = slice->block_start;
  648. vslice->num_macroblocks = slice->block_size;
  649. vslice->macroblock_info = VA_INVALID_ID;
  650. vslice->slice_type = sh->slice_type % 5;
  651. vslice->pic_parameter_set_id = sh->pic_parameter_set_id;
  652. vslice->idr_pic_id = sh->idr_pic_id;
  653. vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb;
  654. vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag;
  655. for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
  656. vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
  657. vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
  658. vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
  659. vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
  660. }
  661. av_assert0(pic->nb_refs <= 2);
  662. if (pic->nb_refs >= 1) {
  663. // Backward reference for P- or B-frame.
  664. av_assert0(pic->type == PICTURE_TYPE_P ||
  665. pic->type == PICTURE_TYPE_B);
  666. vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
  667. }
  668. if (pic->nb_refs >= 2) {
  669. // Forward reference for B-frame.
  670. av_assert0(pic->type == PICTURE_TYPE_B);
  671. vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
  672. }
  673. vslice->slice_qp_delta = sh->slice_qp_delta;
  674. return 0;
  675. }
  676. static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
  677. {
  678. VAAPIEncodeContext *ctx = avctx->priv_data;
  679. VAAPIEncodeH264Context *priv = avctx->priv_data;
  680. int err;
  681. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
  682. if (err < 0)
  683. return err;
  684. priv->mb_width = FFALIGN(avctx->width, 16) / 16;
  685. priv->mb_height = FFALIGN(avctx->height, 16) / 16;
  686. if (ctx->va_rc_mode == VA_RC_CQP) {
  687. priv->fixed_qp_p = priv->qp;
  688. if (avctx->i_quant_factor > 0.0)
  689. priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
  690. avctx->i_quant_offset) + 0.5);
  691. else
  692. priv->fixed_qp_idr = priv->fixed_qp_p;
  693. if (avctx->b_quant_factor > 0.0)
  694. priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
  695. avctx->b_quant_offset) + 0.5);
  696. else
  697. priv->fixed_qp_b = priv->fixed_qp_p;
  698. priv->sei &= ~SEI_TIMING;
  699. av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
  700. "%d / %d / %d for IDR- / P- / B-frames.\n",
  701. priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
  702. } else if (ctx->va_rc_mode == VA_RC_CBR ||
  703. ctx->va_rc_mode == VA_RC_VBR) {
  704. // These still need to be set for pic_init_qp/slice_qp_delta.
  705. priv->fixed_qp_idr = 26;
  706. priv->fixed_qp_p = 26;
  707. priv->fixed_qp_b = 26;
  708. } else {
  709. av_assert0(0 && "Invalid RC mode.");
  710. }
  711. if (priv->sei & SEI_IDENTIFIER) {
  712. const char *lavc = LIBAVCODEC_IDENT;
  713. const char *vaapi = VA_VERSION_S;
  714. const char *driver;
  715. int len;
  716. memcpy(priv->sei_identifier.uuid_iso_iec_11578,
  717. vaapi_encode_h264_sei_identifier_uuid,
  718. sizeof(priv->sei_identifier.uuid_iso_iec_11578));
  719. driver = vaQueryVendorString(ctx->hwctx->display);
  720. if (!driver)
  721. driver = "unknown driver";
  722. len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver);
  723. if (len >= 0) {
  724. priv->sei_identifier_string = av_malloc(len + 1);
  725. if (!priv->sei_identifier_string)
  726. return AVERROR(ENOMEM);
  727. snprintf(priv->sei_identifier_string, len + 1,
  728. "%s / VAAPI %s / %s", lavc, vaapi, driver);
  729. priv->sei_identifier.data = priv->sei_identifier_string;
  730. priv->sei_identifier.data_length = len + 1;
  731. }
  732. }
  733. return 0;
  734. }
  735. static const VAAPIEncodeProfile vaapi_encode_h264_profiles[] = {
  736. { FF_PROFILE_H264_HIGH, 8, 3, 1, 1, VAProfileH264High },
  737. { FF_PROFILE_H264_MAIN, 8, 3, 1, 1, VAProfileH264Main },
  738. { FF_PROFILE_H264_CONSTRAINED_BASELINE,
  739. 8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
  740. { FF_PROFILE_UNKNOWN }
  741. };
  742. static const VAAPIEncodeType vaapi_encode_type_h264 = {
  743. .profiles = vaapi_encode_h264_profiles,
  744. .flags = FLAG_SLICE_CONTROL |
  745. FLAG_B_PICTURES |
  746. FLAG_NON_IDR_KEY_PICTURES,
  747. .configure = &vaapi_encode_h264_configure,
  748. .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
  749. .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
  750. .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
  751. .init_picture_params = &vaapi_encode_h264_init_picture_params,
  752. .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
  753. .init_slice_params = &vaapi_encode_h264_init_slice_params,
  754. .sequence_header_type = VAEncPackedHeaderSequence,
  755. .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
  756. .slice_header_type = VAEncPackedHeaderH264_Slice,
  757. .write_slice_header = &vaapi_encode_h264_write_slice_header,
  758. .write_extra_header = &vaapi_encode_h264_write_extra_header,
  759. };
  760. static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
  761. {
  762. VAAPIEncodeContext *ctx = avctx->priv_data;
  763. VAAPIEncodeH264Context *priv = avctx->priv_data;
  764. ctx->codec = &vaapi_encode_type_h264;
  765. if (avctx->profile == FF_PROFILE_UNKNOWN)
  766. avctx->profile = priv->profile;
  767. if (avctx->level == FF_LEVEL_UNKNOWN)
  768. avctx->level = priv->level;
  769. if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
  770. avctx->compression_level = priv->quality;
  771. // Reject unsupported profiles.
  772. switch (avctx->profile) {
  773. case FF_PROFILE_H264_BASELINE:
  774. av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
  775. "supported, using constrained baseline profile instead.\n");
  776. avctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
  777. break;
  778. case FF_PROFILE_H264_EXTENDED:
  779. av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
  780. "is not supported.\n");
  781. return AVERROR_PATCHWELCOME;
  782. case FF_PROFILE_H264_HIGH_10:
  783. case FF_PROFILE_H264_HIGH_10_INTRA:
  784. av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
  785. "are not supported.\n");
  786. return AVERROR_PATCHWELCOME;
  787. case FF_PROFILE_H264_HIGH_422:
  788. case FF_PROFILE_H264_HIGH_422_INTRA:
  789. case FF_PROFILE_H264_HIGH_444:
  790. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  791. case FF_PROFILE_H264_HIGH_444_INTRA:
  792. case FF_PROFILE_H264_CAVLC_444:
  793. av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
  794. "are not supported.\n");
  795. return AVERROR_PATCHWELCOME;
  796. }
  797. if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
  798. av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
  799. "in 8-bit unsigned integer.\n", avctx->level);
  800. return AVERROR(EINVAL);
  801. }
  802. ctx->desired_packed_headers =
  803. VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
  804. VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
  805. VA_ENC_PACKED_HEADER_MISC; // SEI.
  806. ctx->surface_width = FFALIGN(avctx->width, 16);
  807. ctx->surface_height = FFALIGN(avctx->height, 16);
  808. ctx->slice_block_height = ctx->slice_block_width = 16;
  809. return ff_vaapi_encode_init(avctx);
  810. }
  811. static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
  812. {
  813. VAAPIEncodeH264Context *priv = avctx->priv_data;
  814. ff_cbs_close(&priv->cbc);
  815. av_freep(&priv->sei_identifier_string);
  816. return ff_vaapi_encode_close(avctx);
  817. }
  818. #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
  819. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  820. static const AVOption vaapi_encode_h264_options[] = {
  821. VAAPI_ENCODE_COMMON_OPTIONS,
  822. { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
  823. OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
  824. { "quality", "Set encode quality (trades off against speed, higher is faster)",
  825. OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  826. { "coder", "Entropy coder type",
  827. OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
  828. { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  829. { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  830. { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  831. { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  832. { "aud", "Include AUD",
  833. OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  834. { "sei", "Set SEI to include",
  835. OFFSET(sei), AV_OPT_TYPE_FLAGS,
  836. { .i64 = SEI_IDENTIFIER | SEI_TIMING | SEI_RECOVERY_POINT },
  837. 0, INT_MAX, FLAGS, "sei" },
  838. { "identifier", "Include encoder version identifier",
  839. 0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
  840. INT_MIN, INT_MAX, FLAGS, "sei" },
  841. { "timing", "Include timing parameters (buffering_period and pic_timing)",
  842. 0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
  843. INT_MIN, INT_MAX, FLAGS, "sei" },
  844. { "recovery_point", "Include recovery points where appropriate",
  845. 0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
  846. INT_MIN, INT_MAX, FLAGS, "sei" },
  847. { "profile", "Set profile (profile_idc and constraint_set*_flag)",
  848. OFFSET(profile), AV_OPT_TYPE_INT,
  849. { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xffff, FLAGS, "profile" },
  850. #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  851. { .i64 = value }, 0, 0, FLAGS, "profile"
  852. { PROFILE("constrained_baseline", FF_PROFILE_H264_CONSTRAINED_BASELINE) },
  853. { PROFILE("main", FF_PROFILE_H264_MAIN) },
  854. { PROFILE("high", FF_PROFILE_H264_HIGH) },
  855. #undef PROFILE
  856. { "level", "Set level (level_idc)",
  857. OFFSET(level), AV_OPT_TYPE_INT,
  858. { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
  859. #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  860. { .i64 = value }, 0, 0, FLAGS, "level"
  861. { LEVEL("1", 10) },
  862. { LEVEL("1.1", 11) },
  863. { LEVEL("1.2", 12) },
  864. { LEVEL("1.3", 13) },
  865. { LEVEL("2", 20) },
  866. { LEVEL("2.1", 21) },
  867. { LEVEL("2.2", 22) },
  868. { LEVEL("3", 30) },
  869. { LEVEL("3.1", 31) },
  870. { LEVEL("3.2", 32) },
  871. { LEVEL("4", 40) },
  872. { LEVEL("4.1", 41) },
  873. { LEVEL("4.2", 42) },
  874. { LEVEL("5", 50) },
  875. { LEVEL("5.1", 51) },
  876. { LEVEL("5.2", 52) },
  877. { LEVEL("6", 60) },
  878. { LEVEL("6.1", 61) },
  879. { LEVEL("6.2", 62) },
  880. #undef LEVEL
  881. { NULL },
  882. };
  883. static const AVCodecDefault vaapi_encode_h264_defaults[] = {
  884. { "b", "0" },
  885. { "bf", "2" },
  886. { "g", "120" },
  887. { "i_qfactor", "1" },
  888. { "i_qoffset", "0" },
  889. { "b_qfactor", "6/5" },
  890. { "b_qoffset", "0" },
  891. { "qmin", "-1" },
  892. { "qmax", "-1" },
  893. { NULL },
  894. };
  895. static const AVClass vaapi_encode_h264_class = {
  896. .class_name = "h264_vaapi",
  897. .item_name = av_default_item_name,
  898. .option = vaapi_encode_h264_options,
  899. .version = LIBAVUTIL_VERSION_INT,
  900. };
  901. AVCodec ff_h264_vaapi_encoder = {
  902. .name = "h264_vaapi",
  903. .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
  904. .type = AVMEDIA_TYPE_VIDEO,
  905. .id = AV_CODEC_ID_H264,
  906. .priv_data_size = sizeof(VAAPIEncodeH264Context),
  907. .init = &vaapi_encode_h264_init,
  908. .send_frame = &ff_vaapi_encode_send_frame,
  909. .receive_packet = &ff_vaapi_encode_receive_packet,
  910. .close = &vaapi_encode_h264_close,
  911. .priv_class = &vaapi_encode_h264_class,
  912. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  913. .defaults = vaapi_encode_h264_defaults,
  914. .pix_fmts = (const enum AVPixelFormat[]) {
  915. AV_PIX_FMT_VAAPI,
  916. AV_PIX_FMT_NONE,
  917. },
  918. .wrapper_name = "vaapi",
  919. };