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.

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