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.

1106 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 i;
  324. for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
  325. if (avctx->sample_aspect_ratio.num == sar_idc[i].num &&
  326. avctx->sample_aspect_ratio.den == sar_idc[i].den) {
  327. sps->vui.aspect_ratio_idc = i;
  328. break;
  329. }
  330. }
  331. if (i >= FF_ARRAY_ELEMS(sar_idc)) {
  332. sps->vui.aspect_ratio_idc = 255;
  333. sps->vui.sar_width = avctx->sample_aspect_ratio.num;
  334. sps->vui.sar_height = avctx->sample_aspect_ratio.den;
  335. }
  336. sps->vui.aspect_ratio_info_present_flag = 1;
  337. }
  338. if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
  339. avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  340. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  341. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  342. sps->vui.video_signal_type_present_flag = 1;
  343. sps->vui.video_format = 5; // Unspecified.
  344. sps->vui.video_full_range_flag =
  345. avctx->color_range == AVCOL_RANGE_JPEG;
  346. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  347. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  348. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  349. sps->vui.colour_description_present_flag = 1;
  350. sps->vui.colour_primaries = avctx->color_primaries;
  351. sps->vui.transfer_characteristics = avctx->color_trc;
  352. sps->vui.matrix_coefficients = avctx->colorspace;
  353. }
  354. } else {
  355. sps->vui.video_format = 5;
  356. sps->vui.video_full_range_flag = 0;
  357. sps->vui.colour_primaries = avctx->color_primaries;
  358. sps->vui.transfer_characteristics = avctx->color_trc;
  359. sps->vui.matrix_coefficients = avctx->colorspace;
  360. }
  361. if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
  362. sps->vui.chroma_loc_info_present_flag = 1;
  363. sps->vui.chroma_sample_loc_type_top_field =
  364. sps->vui.chroma_sample_loc_type_bottom_field =
  365. avctx->chroma_sample_location - 1;
  366. }
  367. sps->vui.timing_info_present_flag = 1;
  368. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  369. sps->vui.num_units_in_tick = avctx->framerate.den;
  370. sps->vui.time_scale = 2 * avctx->framerate.num;
  371. sps->vui.fixed_frame_rate_flag = 1;
  372. } else {
  373. sps->vui.num_units_in_tick = avctx->time_base.num;
  374. sps->vui.time_scale = 2 * avctx->time_base.den;
  375. sps->vui.fixed_frame_rate_flag = 0;
  376. }
  377. if (priv->sei & SEI_TIMING) {
  378. H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
  379. H264RawSEIBufferingPeriod *bp = &priv->sei_buffering_period;
  380. sps->vui.nal_hrd_parameters_present_flag = 1;
  381. hrd->cpb_cnt_minus1 = 0;
  382. // Try to scale these to a sensible range so that the
  383. // golomb encode of the value is not overlong.
  384. hrd->bit_rate_scale =
  385. av_clip_uintp2(av_log2(ctx->va_bit_rate) - 15 - 6, 4);
  386. hrd->bit_rate_value_minus1[0] =
  387. (ctx->va_bit_rate >> hrd->bit_rate_scale + 6) - 1;
  388. hrd->cpb_size_scale =
  389. av_clip_uintp2(av_log2(ctx->hrd_params.hrd.buffer_size) - 15 - 4, 4);
  390. hrd->cpb_size_value_minus1[0] =
  391. (ctx->hrd_params.hrd.buffer_size >> hrd->cpb_size_scale + 4) - 1;
  392. // CBR mode as defined for the HRD cannot be achieved without filler
  393. // data, so this flag cannot be set even with VAAPI CBR modes.
  394. hrd->cbr_flag[0] = 0;
  395. hrd->initial_cpb_removal_delay_length_minus1 = 23;
  396. hrd->cpb_removal_delay_length_minus1 = 23;
  397. hrd->dpb_output_delay_length_minus1 = 7;
  398. hrd->time_offset_length = 0;
  399. bp->seq_parameter_set_id = sps->seq_parameter_set_id;
  400. // This calculation can easily overflow 32 bits.
  401. bp->nal.initial_cpb_removal_delay[0] = 90000 *
  402. (uint64_t)ctx->hrd_params.hrd.initial_buffer_fullness /
  403. ctx->hrd_params.hrd.buffer_size;
  404. bp->nal.initial_cpb_removal_delay_offset[0] = 0;
  405. } else {
  406. sps->vui.nal_hrd_parameters_present_flag = 0;
  407. sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag;
  408. }
  409. sps->vui.bitstream_restriction_flag = 1;
  410. sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
  411. sps->vui.log2_max_mv_length_horizontal = 15;
  412. sps->vui.log2_max_mv_length_vertical = 15;
  413. sps->vui.max_num_reorder_frames = (ctx->b_per_p > 0);
  414. sps->vui.max_dec_frame_buffering = sps->max_num_ref_frames;
  415. pps->nal_unit_header.nal_ref_idc = 3;
  416. pps->nal_unit_header.nal_unit_type = H264_NAL_PPS;
  417. pps->pic_parameter_set_id = 0;
  418. pps->seq_parameter_set_id = 0;
  419. pps->entropy_coding_mode_flag =
  420. !(sps->profile_idc == FF_PROFILE_H264_BASELINE ||
  421. sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
  422. sps->profile_idc == FF_PROFILE_H264_CAVLC_444);
  423. if (!priv->coder && pps->entropy_coding_mode_flag)
  424. pps->entropy_coding_mode_flag = 0;
  425. pps->num_ref_idx_l0_default_active_minus1 = 0;
  426. pps->num_ref_idx_l1_default_active_minus1 = 0;
  427. pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
  428. if (sps->profile_idc == FF_PROFILE_H264_BASELINE ||
  429. sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
  430. sps->profile_idc == FF_PROFILE_H264_MAIN) {
  431. pps->more_rbsp_data = 0;
  432. } else {
  433. pps->more_rbsp_data = 1;
  434. pps->transform_8x8_mode_flag = 1;
  435. }
  436. *vseq = (VAEncSequenceParameterBufferH264) {
  437. .seq_parameter_set_id = sps->seq_parameter_set_id,
  438. .level_idc = sps->level_idc,
  439. .intra_period = ctx->gop_size,
  440. .intra_idr_period = ctx->gop_size,
  441. .ip_period = ctx->b_per_p + 1,
  442. .bits_per_second = ctx->va_bit_rate,
  443. .max_num_ref_frames = sps->max_num_ref_frames,
  444. .picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1,
  445. .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
  446. .seq_fields.bits = {
  447. .chroma_format_idc = sps->chroma_format_idc,
  448. .frame_mbs_only_flag = sps->frame_mbs_only_flag,
  449. .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
  450. .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
  451. .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
  452. .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
  453. .pic_order_cnt_type = sps->pic_order_cnt_type,
  454. .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
  455. .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
  456. },
  457. .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
  458. .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
  459. .frame_cropping_flag = sps->frame_cropping_flag,
  460. .frame_crop_left_offset = sps->frame_crop_left_offset,
  461. .frame_crop_right_offset = sps->frame_crop_right_offset,
  462. .frame_crop_top_offset = sps->frame_crop_top_offset,
  463. .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
  464. .vui_parameters_present_flag = sps->vui_parameters_present_flag,
  465. .vui_fields.bits = {
  466. .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
  467. .timing_info_present_flag = sps->vui.timing_info_present_flag,
  468. .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
  469. .log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal,
  470. .log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical,
  471. },
  472. .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
  473. .sar_width = sps->vui.sar_width,
  474. .sar_height = sps->vui.sar_height,
  475. .num_units_in_tick = sps->vui.num_units_in_tick,
  476. .time_scale = sps->vui.time_scale,
  477. };
  478. *vpic = (VAEncPictureParameterBufferH264) {
  479. .CurrPic = {
  480. .picture_id = VA_INVALID_ID,
  481. .flags = VA_PICTURE_H264_INVALID,
  482. },
  483. .coded_buf = VA_INVALID_ID,
  484. .pic_parameter_set_id = pps->pic_parameter_set_id,
  485. .seq_parameter_set_id = pps->seq_parameter_set_id,
  486. .pic_init_qp = pps->pic_init_qp_minus26 + 26,
  487. .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
  488. .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
  489. .chroma_qp_index_offset = pps->chroma_qp_index_offset,
  490. .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
  491. .pic_fields.bits = {
  492. .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
  493. .weighted_pred_flag = pps->weighted_pred_flag,
  494. .weighted_bipred_idc = pps->weighted_bipred_idc,
  495. .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
  496. .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
  497. .deblocking_filter_control_present_flag =
  498. pps->deblocking_filter_control_present_flag,
  499. .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
  500. .pic_order_present_flag =
  501. pps->bottom_field_pic_order_in_frame_present_flag,
  502. .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
  503. },
  504. };
  505. return 0;
  506. }
  507. static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
  508. VAAPIEncodePicture *pic)
  509. {
  510. VAAPIEncodeContext *ctx = avctx->priv_data;
  511. VAAPIEncodeH264Context *priv = avctx->priv_data;
  512. H264RawSPS *sps = &priv->raw_sps;
  513. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  514. int i;
  515. memset(&priv->current_access_unit, 0,
  516. sizeof(priv->current_access_unit));
  517. if (pic->type == PICTURE_TYPE_IDR) {
  518. av_assert0(pic->display_order == pic->encode_order);
  519. priv->frame_num = 0;
  520. priv->next_frame_num = 1;
  521. priv->cpb_delay = 0;
  522. priv->last_idr_frame = pic->display_order;
  523. ++priv->idr_pic_count;
  524. priv->slice_type = 7;
  525. priv->primary_pic_type = 0;
  526. } else {
  527. priv->frame_num = priv->next_frame_num;
  528. if (pic->type != PICTURE_TYPE_B) {
  529. // Reference picture, so frame_num advances.
  530. priv->next_frame_num = (priv->frame_num + 1) &
  531. ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
  532. }
  533. ++priv->cpb_delay;
  534. if (pic->type == PICTURE_TYPE_I) {
  535. priv->slice_type = 7;
  536. priv->primary_pic_type = 0;
  537. } else if (pic->type == PICTURE_TYPE_P) {
  538. priv->slice_type = 5;
  539. priv->primary_pic_type = 1;
  540. } else {
  541. priv->slice_type = 6;
  542. priv->primary_pic_type = 2;
  543. }
  544. }
  545. priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
  546. priv->dpb_delay = pic->display_order - pic->encode_order + 1;
  547. if (priv->aud) {
  548. priv->aud_needed = 1;
  549. priv->raw_aud = (H264RawAUD) {
  550. .nal_unit_header = {
  551. .nal_unit_type = H264_NAL_AUD,
  552. },
  553. .primary_pic_type = priv->primary_pic_type,
  554. };
  555. } else {
  556. priv->aud_needed = 0;
  557. }
  558. priv->sei_needed = 0;
  559. if (priv->sei & SEI_IDENTIFIER && pic->encode_order == 0)
  560. priv->sei_needed |= SEI_IDENTIFIER;
  561. #if !CONFIG_VAAPI_1
  562. if (ctx->va_rc_mode == VA_RC_CBR)
  563. priv->sei_cbr_workaround_needed = 1;
  564. #endif
  565. if (priv->sei & SEI_TIMING) {
  566. priv->sei_pic_timing = (H264RawSEIPicTiming) {
  567. .cpb_removal_delay = 2 * priv->cpb_delay,
  568. .dpb_output_delay = 2 * priv->dpb_delay,
  569. };
  570. priv->sei_needed |= SEI_TIMING;
  571. }
  572. if (priv->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
  573. priv->sei_recovery_point = (H264RawSEIRecoveryPoint) {
  574. .recovery_frame_cnt = 0,
  575. .exact_match_flag = 1,
  576. .broken_link_flag = ctx->b_per_p > 0,
  577. };
  578. priv->sei_needed |= SEI_RECOVERY_POINT;
  579. }
  580. vpic->CurrPic = (VAPictureH264) {
  581. .picture_id = pic->recon_surface,
  582. .frame_idx = priv->frame_num,
  583. .flags = 0,
  584. .TopFieldOrderCnt = priv->pic_order_cnt,
  585. .BottomFieldOrderCnt = priv->pic_order_cnt,
  586. };
  587. for (i = 0; i < pic->nb_refs; i++) {
  588. VAAPIEncodePicture *ref = pic->refs[i];
  589. unsigned int frame_num = (ref->encode_order - priv->last_idr_frame) &
  590. ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
  591. unsigned int pic_order_cnt = ref->display_order - priv->last_idr_frame;
  592. av_assert0(ref && ref->encode_order < pic->encode_order);
  593. vpic->ReferenceFrames[i] = (VAPictureH264) {
  594. .picture_id = ref->recon_surface,
  595. .frame_idx = frame_num,
  596. .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
  597. .TopFieldOrderCnt = pic_order_cnt,
  598. .BottomFieldOrderCnt = pic_order_cnt,
  599. };
  600. }
  601. for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
  602. vpic->ReferenceFrames[i] = (VAPictureH264) {
  603. .picture_id = VA_INVALID_ID,
  604. .flags = VA_PICTURE_H264_INVALID,
  605. };
  606. }
  607. vpic->coded_buf = pic->output_buffer;
  608. vpic->frame_num = priv->frame_num;
  609. vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
  610. vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
  611. pic->nb_slices = 1;
  612. return 0;
  613. }
  614. static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
  615. VAAPIEncodePicture *pic,
  616. VAAPIEncodeSlice *slice)
  617. {
  618. VAAPIEncodeH264Context *priv = avctx->priv_data;
  619. H264RawSPS *sps = &priv->raw_sps;
  620. H264RawPPS *pps = &priv->raw_pps;
  621. H264RawSliceHeader *sh = &priv->raw_slice.header;
  622. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  623. VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
  624. int i;
  625. if (pic->type == PICTURE_TYPE_IDR) {
  626. sh->nal_unit_header.nal_unit_type = H264_NAL_IDR_SLICE;
  627. sh->nal_unit_header.nal_ref_idc = 3;
  628. } else {
  629. sh->nal_unit_header.nal_unit_type = H264_NAL_SLICE;
  630. sh->nal_unit_header.nal_ref_idc = pic->type != PICTURE_TYPE_B;
  631. }
  632. // Only one slice per frame.
  633. sh->first_mb_in_slice = 0;
  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 = sh->first_mb_in_slice;
  648. vslice->num_macroblocks = priv->mb_width * priv->mb_height;
  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. .configure = &vaapi_encode_h264_configure,
  745. .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
  746. .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
  747. .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
  748. .init_picture_params = &vaapi_encode_h264_init_picture_params,
  749. .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
  750. .init_slice_params = &vaapi_encode_h264_init_slice_params,
  751. .sequence_header_type = VAEncPackedHeaderSequence,
  752. .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
  753. .slice_header_type = VAEncPackedHeaderH264_Slice,
  754. .write_slice_header = &vaapi_encode_h264_write_slice_header,
  755. .write_extra_header = &vaapi_encode_h264_write_extra_header,
  756. };
  757. static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
  758. {
  759. VAAPIEncodeContext *ctx = avctx->priv_data;
  760. VAAPIEncodeH264Context *priv = avctx->priv_data;
  761. ctx->codec = &vaapi_encode_type_h264;
  762. if (avctx->profile == FF_PROFILE_UNKNOWN)
  763. avctx->profile = priv->profile;
  764. if (avctx->level == FF_LEVEL_UNKNOWN)
  765. avctx->level = priv->level;
  766. if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
  767. avctx->compression_level = priv->quality;
  768. // Reject unsupported profiles.
  769. switch (avctx->profile) {
  770. case FF_PROFILE_H264_BASELINE:
  771. av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
  772. "supported, using constrained baseline profile instead.\n");
  773. avctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
  774. break;
  775. case FF_PROFILE_H264_EXTENDED:
  776. av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
  777. "is not supported.\n");
  778. return AVERROR_PATCHWELCOME;
  779. case FF_PROFILE_H264_HIGH_10:
  780. case FF_PROFILE_H264_HIGH_10_INTRA:
  781. av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
  782. "are not supported.\n");
  783. return AVERROR_PATCHWELCOME;
  784. case FF_PROFILE_H264_HIGH_422:
  785. case FF_PROFILE_H264_HIGH_422_INTRA:
  786. case FF_PROFILE_H264_HIGH_444:
  787. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  788. case FF_PROFILE_H264_HIGH_444_INTRA:
  789. case FF_PROFILE_H264_CAVLC_444:
  790. av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
  791. "are not supported.\n");
  792. return AVERROR_PATCHWELCOME;
  793. }
  794. if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
  795. av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
  796. "in 8-bit unsigned integer.\n", avctx->level);
  797. return AVERROR(EINVAL);
  798. }
  799. ctx->desired_packed_headers =
  800. VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
  801. VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
  802. VA_ENC_PACKED_HEADER_MISC; // SEI.
  803. ctx->surface_width = FFALIGN(avctx->width, 16);
  804. ctx->surface_height = FFALIGN(avctx->height, 16);
  805. return ff_vaapi_encode_init(avctx);
  806. }
  807. static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
  808. {
  809. VAAPIEncodeH264Context *priv = avctx->priv_data;
  810. ff_cbs_close(&priv->cbc);
  811. av_freep(&priv->sei_identifier_string);
  812. return ff_vaapi_encode_close(avctx);
  813. }
  814. #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
  815. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  816. static const AVOption vaapi_encode_h264_options[] = {
  817. VAAPI_ENCODE_COMMON_OPTIONS,
  818. { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
  819. OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
  820. { "quality", "Set encode quality (trades off against speed, higher is faster)",
  821. OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  822. { "coder", "Entropy coder type",
  823. OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
  824. { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  825. { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  826. { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  827. { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  828. { "aud", "Include AUD",
  829. OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  830. { "sei", "Set SEI to include",
  831. OFFSET(sei), AV_OPT_TYPE_FLAGS,
  832. { .i64 = SEI_IDENTIFIER | SEI_TIMING | SEI_RECOVERY_POINT },
  833. 0, INT_MAX, FLAGS, "sei" },
  834. { "identifier", "Include encoder version identifier",
  835. 0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
  836. INT_MIN, INT_MAX, FLAGS, "sei" },
  837. { "timing", "Include timing parameters (buffering_period and pic_timing)",
  838. 0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
  839. INT_MIN, INT_MAX, FLAGS, "sei" },
  840. { "recovery_point", "Include recovery points where appropriate",
  841. 0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
  842. INT_MIN, INT_MAX, FLAGS, "sei" },
  843. { "profile", "Set profile (profile_idc and constraint_set*_flag)",
  844. OFFSET(profile), AV_OPT_TYPE_INT,
  845. { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xffff, FLAGS, "profile" },
  846. #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  847. { .i64 = value }, 0, 0, FLAGS, "profile"
  848. { PROFILE("constrained_baseline", FF_PROFILE_H264_CONSTRAINED_BASELINE) },
  849. { PROFILE("main", FF_PROFILE_H264_MAIN) },
  850. { PROFILE("high", FF_PROFILE_H264_HIGH) },
  851. #undef PROFILE
  852. { "level", "Set level (level_idc)",
  853. OFFSET(level), AV_OPT_TYPE_INT,
  854. { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
  855. #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  856. { .i64 = value }, 0, 0, FLAGS, "level"
  857. { LEVEL("1", 10) },
  858. { LEVEL("1.1", 11) },
  859. { LEVEL("1.2", 12) },
  860. { LEVEL("1.3", 13) },
  861. { LEVEL("2", 20) },
  862. { LEVEL("2.1", 21) },
  863. { LEVEL("2.2", 22) },
  864. { LEVEL("3", 30) },
  865. { LEVEL("3.1", 31) },
  866. { LEVEL("3.2", 32) },
  867. { LEVEL("4", 40) },
  868. { LEVEL("4.1", 41) },
  869. { LEVEL("4.2", 42) },
  870. { LEVEL("5", 50) },
  871. { LEVEL("5.1", 51) },
  872. { LEVEL("5.2", 52) },
  873. { LEVEL("6", 60) },
  874. { LEVEL("6.1", 61) },
  875. { LEVEL("6.2", 62) },
  876. #undef LEVEL
  877. { NULL },
  878. };
  879. static const AVCodecDefault vaapi_encode_h264_defaults[] = {
  880. { "b", "0" },
  881. { "bf", "2" },
  882. { "g", "120" },
  883. { "i_qfactor", "1" },
  884. { "i_qoffset", "0" },
  885. { "b_qfactor", "6/5" },
  886. { "b_qoffset", "0" },
  887. { "qmin", "-1" },
  888. { "qmax", "-1" },
  889. { NULL },
  890. };
  891. static const AVClass vaapi_encode_h264_class = {
  892. .class_name = "h264_vaapi",
  893. .item_name = av_default_item_name,
  894. .option = vaapi_encode_h264_options,
  895. .version = LIBAVUTIL_VERSION_INT,
  896. };
  897. AVCodec ff_h264_vaapi_encoder = {
  898. .name = "h264_vaapi",
  899. .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
  900. .type = AVMEDIA_TYPE_VIDEO,
  901. .id = AV_CODEC_ID_H264,
  902. .priv_data_size = sizeof(VAAPIEncodeH264Context),
  903. .init = &vaapi_encode_h264_init,
  904. .encode2 = &ff_vaapi_encode2,
  905. .close = &vaapi_encode_h264_close,
  906. .priv_class = &vaapi_encode_h264_class,
  907. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  908. .defaults = vaapi_encode_h264_defaults,
  909. .pix_fmts = (const enum AVPixelFormat[]) {
  910. AV_PIX_FMT_VAAPI,
  911. AV_PIX_FMT_NONE,
  912. },
  913. .wrapper_name = "vaapi",
  914. };