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.

1347 lines
48KB

  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 VAAPIEncodeH264Picture {
  44. int frame_num;
  45. int pic_order_cnt;
  46. int64_t last_idr_frame;
  47. uint16_t idr_pic_id;
  48. int primary_pic_type;
  49. int slice_type;
  50. int cpb_delay;
  51. int dpb_delay;
  52. } VAAPIEncodeH264Picture;
  53. typedef struct VAAPIEncodeH264Context {
  54. VAAPIEncodeContext common;
  55. // User options.
  56. int qp;
  57. int quality;
  58. int coder;
  59. int aud;
  60. int sei;
  61. int profile;
  62. int level;
  63. // Derived settings.
  64. int mb_width;
  65. int mb_height;
  66. int fixed_qp_idr;
  67. int fixed_qp_p;
  68. int fixed_qp_b;
  69. int dpb_frames;
  70. // Writer structures.
  71. CodedBitstreamContext *cbc;
  72. CodedBitstreamFragment current_access_unit;
  73. H264RawAUD raw_aud;
  74. H264RawSPS raw_sps;
  75. H264RawPPS raw_pps;
  76. H264RawSlice raw_slice;
  77. H264RawSEIBufferingPeriod sei_buffering_period;
  78. H264RawSEIPicTiming sei_pic_timing;
  79. H264RawSEIRecoveryPoint sei_recovery_point;
  80. SEIRawUserDataUnregistered 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. H264RawNALUnitHeader *header = nal_unit;
  112. int err;
  113. err = ff_cbs_insert_unit_content(au, -1,
  114. header->nal_unit_type, nal_unit, NULL);
  115. if (err < 0) {
  116. av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
  117. "type = %d.\n", header->nal_unit_type);
  118. return err;
  119. }
  120. return 0;
  121. }
  122. static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
  123. char *data, size_t *data_len)
  124. {
  125. VAAPIEncodeH264Context *priv = avctx->priv_data;
  126. CodedBitstreamFragment *au = &priv->current_access_unit;
  127. int err;
  128. if (priv->aud_needed) {
  129. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
  130. if (err < 0)
  131. goto fail;
  132. priv->aud_needed = 0;
  133. }
  134. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_sps);
  135. if (err < 0)
  136. goto fail;
  137. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_pps);
  138. if (err < 0)
  139. goto fail;
  140. err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
  141. fail:
  142. ff_cbs_fragment_reset(au);
  143. return err;
  144. }
  145. static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
  146. VAAPIEncodePicture *pic,
  147. VAAPIEncodeSlice *slice,
  148. char *data, size_t *data_len)
  149. {
  150. VAAPIEncodeH264Context *priv = avctx->priv_data;
  151. CodedBitstreamFragment *au = &priv->current_access_unit;
  152. int err;
  153. if (priv->aud_needed) {
  154. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
  155. if (err < 0)
  156. goto fail;
  157. priv->aud_needed = 0;
  158. }
  159. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_slice);
  160. if (err < 0)
  161. goto fail;
  162. err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
  163. fail:
  164. ff_cbs_fragment_reset(au);
  165. return err;
  166. }
  167. static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
  168. VAAPIEncodePicture *pic,
  169. int index, int *type,
  170. char *data, size_t *data_len)
  171. {
  172. VAAPIEncodeH264Context *priv = avctx->priv_data;
  173. CodedBitstreamFragment *au = &priv->current_access_unit;
  174. int err;
  175. if (priv->sei_needed) {
  176. if (priv->aud_needed) {
  177. err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
  178. if (err < 0)
  179. goto fail;
  180. priv->aud_needed = 0;
  181. }
  182. if (priv->sei_needed & SEI_IDENTIFIER) {
  183. err = ff_cbs_sei_add_message(priv->cbc, au, 1,
  184. SEI_TYPE_USER_DATA_UNREGISTERED,
  185. &priv->sei_identifier, NULL);
  186. if (err < 0)
  187. goto fail;
  188. }
  189. if (priv->sei_needed & SEI_TIMING) {
  190. if (pic->type == PICTURE_TYPE_IDR) {
  191. err = ff_cbs_sei_add_message(priv->cbc, au, 1,
  192. SEI_TYPE_BUFFERING_PERIOD,
  193. &priv->sei_buffering_period, NULL);
  194. if (err < 0)
  195. goto fail;
  196. }
  197. err = ff_cbs_sei_add_message(priv->cbc, au, 1,
  198. SEI_TYPE_PIC_TIMING,
  199. &priv->sei_pic_timing, NULL);
  200. if (err < 0)
  201. goto fail;
  202. }
  203. if (priv->sei_needed & SEI_RECOVERY_POINT) {
  204. err = ff_cbs_sei_add_message(priv->cbc, au, 1,
  205. SEI_TYPE_RECOVERY_POINT,
  206. &priv->sei_recovery_point, NULL);
  207. if (err < 0)
  208. goto fail;
  209. }
  210. priv->sei_needed = 0;
  211. err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
  212. if (err < 0)
  213. goto fail;
  214. ff_cbs_fragment_reset(au);
  215. *type = VAEncPackedHeaderRawData;
  216. return 0;
  217. #if !CONFIG_VAAPI_1
  218. } else if (priv->sei_cbr_workaround_needed) {
  219. // Insert a zero-length header using the old SEI type. This is
  220. // required to avoid triggering broken behaviour on Intel platforms
  221. // in CBR mode where an invalid SEI message is generated by the
  222. // driver and inserted into the stream.
  223. *data_len = 0;
  224. *type = VAEncPackedHeaderH264_SEI;
  225. priv->sei_cbr_workaround_needed = 0;
  226. return 0;
  227. #endif
  228. } else {
  229. return AVERROR_EOF;
  230. }
  231. fail:
  232. ff_cbs_fragment_reset(au);
  233. return err;
  234. }
  235. static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
  236. {
  237. VAAPIEncodeContext *ctx = avctx->priv_data;
  238. VAAPIEncodeH264Context *priv = avctx->priv_data;
  239. H264RawSPS *sps = &priv->raw_sps;
  240. H264RawPPS *pps = &priv->raw_pps;
  241. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  242. VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
  243. memset(sps, 0, sizeof(*sps));
  244. memset(pps, 0, sizeof(*pps));
  245. sps->nal_unit_header.nal_ref_idc = 3;
  246. sps->nal_unit_header.nal_unit_type = H264_NAL_SPS;
  247. sps->profile_idc = avctx->profile & 0xff;
  248. if (avctx->profile == FF_PROFILE_H264_CONSTRAINED_BASELINE ||
  249. avctx->profile == FF_PROFILE_H264_MAIN)
  250. sps->constraint_set1_flag = 1;
  251. if (avctx->profile == FF_PROFILE_H264_HIGH)
  252. sps->constraint_set3_flag = ctx->gop_size == 1;
  253. if (avctx->profile == FF_PROFILE_H264_MAIN ||
  254. avctx->profile == FF_PROFILE_H264_HIGH) {
  255. sps->constraint_set4_flag = 1;
  256. sps->constraint_set5_flag = ctx->b_per_p == 0;
  257. }
  258. if (ctx->gop_size == 1)
  259. priv->dpb_frames = 0;
  260. else
  261. priv->dpb_frames = 1 + ctx->max_b_depth;
  262. if (avctx->level != FF_LEVEL_UNKNOWN) {
  263. sps->level_idc = avctx->level;
  264. } else {
  265. const H264LevelDescriptor *level;
  266. int framerate;
  267. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  268. framerate = avctx->framerate.num / avctx->framerate.den;
  269. else
  270. framerate = 0;
  271. level = ff_h264_guess_level(sps->profile_idc,
  272. avctx->bit_rate,
  273. framerate,
  274. priv->mb_width * 16,
  275. priv->mb_height * 16,
  276. priv->dpb_frames);
  277. if (level) {
  278. av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
  279. if (level->constraint_set3_flag)
  280. sps->constraint_set3_flag = 1;
  281. sps->level_idc = level->level_idc;
  282. } else {
  283. av_log(avctx, AV_LOG_WARNING, "Stream will not conform "
  284. "to any level: using level 6.2.\n");
  285. sps->level_idc = 62;
  286. }
  287. }
  288. sps->seq_parameter_set_id = 0;
  289. sps->chroma_format_idc = 1;
  290. sps->log2_max_frame_num_minus4 = 4;
  291. sps->pic_order_cnt_type = 0;
  292. sps->log2_max_pic_order_cnt_lsb_minus4 = 4;
  293. sps->max_num_ref_frames = priv->dpb_frames;
  294. sps->pic_width_in_mbs_minus1 = priv->mb_width - 1;
  295. sps->pic_height_in_map_units_minus1 = priv->mb_height - 1;
  296. sps->frame_mbs_only_flag = 1;
  297. sps->direct_8x8_inference_flag = 1;
  298. if (avctx->width != 16 * priv->mb_width ||
  299. avctx->height != 16 * priv->mb_height) {
  300. sps->frame_cropping_flag = 1;
  301. sps->frame_crop_left_offset = 0;
  302. sps->frame_crop_right_offset =
  303. (16 * priv->mb_width - avctx->width) / 2;
  304. sps->frame_crop_top_offset = 0;
  305. sps->frame_crop_bottom_offset =
  306. (16 * priv->mb_height - avctx->height) / 2;
  307. } else {
  308. sps->frame_cropping_flag = 0;
  309. }
  310. sps->vui_parameters_present_flag = 1;
  311. if (avctx->sample_aspect_ratio.num != 0 &&
  312. avctx->sample_aspect_ratio.den != 0) {
  313. static const AVRational sar_idc[] = {
  314. { 0, 0 },
  315. { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
  316. { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
  317. { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
  318. { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
  319. };
  320. int num, den, i;
  321. av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
  322. avctx->sample_aspect_ratio.den, 65535);
  323. for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
  324. if (num == sar_idc[i].num &&
  325. den == sar_idc[i].den) {
  326. sps->vui.aspect_ratio_idc = i;
  327. break;
  328. }
  329. }
  330. if (i >= FF_ARRAY_ELEMS(sar_idc)) {
  331. sps->vui.aspect_ratio_idc = 255;
  332. sps->vui.sar_width = num;
  333. sps->vui.sar_height = den;
  334. }
  335. sps->vui.aspect_ratio_info_present_flag = 1;
  336. }
  337. // Unspecified video format, from table E-2.
  338. sps->vui.video_format = 5;
  339. sps->vui.video_full_range_flag =
  340. avctx->color_range == AVCOL_RANGE_JPEG;
  341. sps->vui.colour_primaries = avctx->color_primaries;
  342. sps->vui.transfer_characteristics = avctx->color_trc;
  343. sps->vui.matrix_coefficients = avctx->colorspace;
  344. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  345. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  346. avctx->colorspace != AVCOL_SPC_UNSPECIFIED)
  347. sps->vui.colour_description_present_flag = 1;
  348. if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
  349. sps->vui.colour_description_present_flag)
  350. sps->vui.video_signal_type_present_flag = 1;
  351. if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
  352. sps->vui.chroma_loc_info_present_flag = 1;
  353. sps->vui.chroma_sample_loc_type_top_field =
  354. sps->vui.chroma_sample_loc_type_bottom_field =
  355. avctx->chroma_sample_location - 1;
  356. }
  357. sps->vui.timing_info_present_flag = 1;
  358. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  359. sps->vui.num_units_in_tick = avctx->framerate.den;
  360. sps->vui.time_scale = 2 * avctx->framerate.num;
  361. sps->vui.fixed_frame_rate_flag = 1;
  362. } else {
  363. sps->vui.num_units_in_tick = avctx->time_base.num;
  364. sps->vui.time_scale = 2 * avctx->time_base.den;
  365. sps->vui.fixed_frame_rate_flag = 0;
  366. }
  367. if (priv->sei & SEI_TIMING) {
  368. H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
  369. H264RawSEIBufferingPeriod *bp = &priv->sei_buffering_period;
  370. sps->vui.nal_hrd_parameters_present_flag = 1;
  371. hrd->cpb_cnt_minus1 = 0;
  372. // Try to scale these to a sensible range so that the
  373. // golomb encode of the value is not overlong.
  374. hrd->bit_rate_scale =
  375. av_clip_uintp2(av_log2(ctx->va_bit_rate) - 15 - 6, 4);
  376. hrd->bit_rate_value_minus1[0] =
  377. (ctx->va_bit_rate >> hrd->bit_rate_scale + 6) - 1;
  378. hrd->cpb_size_scale =
  379. av_clip_uintp2(av_log2(ctx->hrd_params.buffer_size) - 15 - 4, 4);
  380. hrd->cpb_size_value_minus1[0] =
  381. (ctx->hrd_params.buffer_size >> hrd->cpb_size_scale + 4) - 1;
  382. // CBR mode as defined for the HRD cannot be achieved without filler
  383. // data, so this flag cannot be set even with VAAPI CBR modes.
  384. hrd->cbr_flag[0] = 0;
  385. hrd->initial_cpb_removal_delay_length_minus1 = 23;
  386. hrd->cpb_removal_delay_length_minus1 = 23;
  387. hrd->dpb_output_delay_length_minus1 = 7;
  388. hrd->time_offset_length = 0;
  389. bp->seq_parameter_set_id = sps->seq_parameter_set_id;
  390. // This calculation can easily overflow 32 bits.
  391. bp->nal.initial_cpb_removal_delay[0] = 90000 *
  392. (uint64_t)ctx->hrd_params.initial_buffer_fullness /
  393. ctx->hrd_params.buffer_size;
  394. bp->nal.initial_cpb_removal_delay_offset[0] = 0;
  395. } else {
  396. sps->vui.nal_hrd_parameters_present_flag = 0;
  397. sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag;
  398. }
  399. sps->vui.bitstream_restriction_flag = 1;
  400. sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
  401. sps->vui.log2_max_mv_length_horizontal = 15;
  402. sps->vui.log2_max_mv_length_vertical = 15;
  403. sps->vui.max_num_reorder_frames = ctx->max_b_depth;
  404. sps->vui.max_dec_frame_buffering = ctx->max_b_depth + 1;
  405. pps->nal_unit_header.nal_ref_idc = 3;
  406. pps->nal_unit_header.nal_unit_type = H264_NAL_PPS;
  407. pps->pic_parameter_set_id = 0;
  408. pps->seq_parameter_set_id = 0;
  409. pps->entropy_coding_mode_flag =
  410. !(sps->profile_idc == FF_PROFILE_H264_BASELINE ||
  411. sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
  412. sps->profile_idc == FF_PROFILE_H264_CAVLC_444);
  413. if (!priv->coder && pps->entropy_coding_mode_flag)
  414. pps->entropy_coding_mode_flag = 0;
  415. pps->num_ref_idx_l0_default_active_minus1 = 0;
  416. pps->num_ref_idx_l1_default_active_minus1 = 0;
  417. pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
  418. if (sps->profile_idc == FF_PROFILE_H264_BASELINE ||
  419. sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
  420. sps->profile_idc == FF_PROFILE_H264_MAIN) {
  421. pps->more_rbsp_data = 0;
  422. } else {
  423. pps->more_rbsp_data = 1;
  424. pps->transform_8x8_mode_flag = 1;
  425. }
  426. *vseq = (VAEncSequenceParameterBufferH264) {
  427. .seq_parameter_set_id = sps->seq_parameter_set_id,
  428. .level_idc = sps->level_idc,
  429. .intra_period = ctx->gop_size,
  430. .intra_idr_period = ctx->gop_size,
  431. .ip_period = ctx->b_per_p + 1,
  432. .bits_per_second = ctx->va_bit_rate,
  433. .max_num_ref_frames = sps->max_num_ref_frames,
  434. .picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1,
  435. .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
  436. .seq_fields.bits = {
  437. .chroma_format_idc = sps->chroma_format_idc,
  438. .frame_mbs_only_flag = sps->frame_mbs_only_flag,
  439. .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
  440. .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
  441. .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
  442. .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
  443. .pic_order_cnt_type = sps->pic_order_cnt_type,
  444. .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
  445. .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
  446. },
  447. .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
  448. .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
  449. .frame_cropping_flag = sps->frame_cropping_flag,
  450. .frame_crop_left_offset = sps->frame_crop_left_offset,
  451. .frame_crop_right_offset = sps->frame_crop_right_offset,
  452. .frame_crop_top_offset = sps->frame_crop_top_offset,
  453. .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
  454. .vui_parameters_present_flag = sps->vui_parameters_present_flag,
  455. .vui_fields.bits = {
  456. .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
  457. .timing_info_present_flag = sps->vui.timing_info_present_flag,
  458. .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
  459. .log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal,
  460. .log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical,
  461. },
  462. .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
  463. .sar_width = sps->vui.sar_width,
  464. .sar_height = sps->vui.sar_height,
  465. .num_units_in_tick = sps->vui.num_units_in_tick,
  466. .time_scale = sps->vui.time_scale,
  467. };
  468. *vpic = (VAEncPictureParameterBufferH264) {
  469. .CurrPic = {
  470. .picture_id = VA_INVALID_ID,
  471. .flags = VA_PICTURE_H264_INVALID,
  472. },
  473. .coded_buf = VA_INVALID_ID,
  474. .pic_parameter_set_id = pps->pic_parameter_set_id,
  475. .seq_parameter_set_id = pps->seq_parameter_set_id,
  476. .pic_init_qp = pps->pic_init_qp_minus26 + 26,
  477. .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
  478. .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
  479. .chroma_qp_index_offset = pps->chroma_qp_index_offset,
  480. .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
  481. .pic_fields.bits = {
  482. .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
  483. .weighted_pred_flag = pps->weighted_pred_flag,
  484. .weighted_bipred_idc = pps->weighted_bipred_idc,
  485. .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
  486. .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
  487. .deblocking_filter_control_present_flag =
  488. pps->deblocking_filter_control_present_flag,
  489. .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
  490. .pic_order_present_flag =
  491. pps->bottom_field_pic_order_in_frame_present_flag,
  492. .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
  493. },
  494. };
  495. return 0;
  496. }
  497. static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
  498. VAAPIEncodePicture *pic)
  499. {
  500. VAAPIEncodeContext *ctx = avctx->priv_data;
  501. VAAPIEncodeH264Context *priv = avctx->priv_data;
  502. VAAPIEncodeH264Picture *hpic = pic->priv_data;
  503. VAAPIEncodePicture *prev = pic->prev;
  504. VAAPIEncodeH264Picture *hprev = prev ? prev->priv_data : NULL;
  505. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  506. int i;
  507. if (pic->type == PICTURE_TYPE_IDR) {
  508. av_assert0(pic->display_order == pic->encode_order);
  509. hpic->frame_num = 0;
  510. hpic->last_idr_frame = pic->display_order;
  511. hpic->idr_pic_id = hprev ? hprev->idr_pic_id + 1 : 0;
  512. hpic->primary_pic_type = 0;
  513. hpic->slice_type = 7;
  514. } else {
  515. av_assert0(prev);
  516. hpic->frame_num = hprev->frame_num + prev->is_reference;
  517. hpic->last_idr_frame = hprev->last_idr_frame;
  518. hpic->idr_pic_id = hprev->idr_pic_id;
  519. if (pic->type == PICTURE_TYPE_I) {
  520. hpic->slice_type = 7;
  521. hpic->primary_pic_type = 0;
  522. } else if (pic->type == PICTURE_TYPE_P) {
  523. hpic->slice_type = 5;
  524. hpic->primary_pic_type = 1;
  525. } else {
  526. hpic->slice_type = 6;
  527. hpic->primary_pic_type = 2;
  528. }
  529. }
  530. hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame;
  531. hpic->dpb_delay = pic->display_order - pic->encode_order + ctx->max_b_depth;
  532. hpic->cpb_delay = pic->encode_order - hpic->last_idr_frame;
  533. if (priv->aud) {
  534. priv->aud_needed = 1;
  535. priv->raw_aud = (H264RawAUD) {
  536. .nal_unit_header = {
  537. .nal_unit_type = H264_NAL_AUD,
  538. },
  539. .primary_pic_type = hpic->primary_pic_type,
  540. };
  541. } else {
  542. priv->aud_needed = 0;
  543. }
  544. priv->sei_needed = 0;
  545. if (priv->sei & SEI_IDENTIFIER && pic->encode_order == 0)
  546. priv->sei_needed |= SEI_IDENTIFIER;
  547. #if !CONFIG_VAAPI_1
  548. if (ctx->va_rc_mode == VA_RC_CBR)
  549. priv->sei_cbr_workaround_needed = 1;
  550. #endif
  551. if (priv->sei & SEI_TIMING) {
  552. priv->sei_pic_timing = (H264RawSEIPicTiming) {
  553. .cpb_removal_delay = 2 * hpic->cpb_delay,
  554. .dpb_output_delay = 2 * hpic->dpb_delay,
  555. };
  556. priv->sei_needed |= SEI_TIMING;
  557. }
  558. if (priv->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
  559. priv->sei_recovery_point = (H264RawSEIRecoveryPoint) {
  560. .recovery_frame_cnt = 0,
  561. .exact_match_flag = 1,
  562. .broken_link_flag = ctx->b_per_p > 0,
  563. };
  564. priv->sei_needed |= SEI_RECOVERY_POINT;
  565. }
  566. vpic->CurrPic = (VAPictureH264) {
  567. .picture_id = pic->recon_surface,
  568. .frame_idx = hpic->frame_num,
  569. .flags = 0,
  570. .TopFieldOrderCnt = hpic->pic_order_cnt,
  571. .BottomFieldOrderCnt = hpic->pic_order_cnt,
  572. };
  573. for (i = 0; i < pic->nb_refs; i++) {
  574. VAAPIEncodePicture *ref = pic->refs[i];
  575. VAAPIEncodeH264Picture *href;
  576. av_assert0(ref && ref->encode_order < pic->encode_order);
  577. href = ref->priv_data;
  578. vpic->ReferenceFrames[i] = (VAPictureH264) {
  579. .picture_id = ref->recon_surface,
  580. .frame_idx = href->frame_num,
  581. .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
  582. .TopFieldOrderCnt = href->pic_order_cnt,
  583. .BottomFieldOrderCnt = href->pic_order_cnt,
  584. };
  585. }
  586. for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
  587. vpic->ReferenceFrames[i] = (VAPictureH264) {
  588. .picture_id = VA_INVALID_ID,
  589. .flags = VA_PICTURE_H264_INVALID,
  590. };
  591. }
  592. vpic->coded_buf = pic->output_buffer;
  593. vpic->frame_num = hpic->frame_num;
  594. vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
  595. vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
  596. return 0;
  597. }
  598. static void vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx,
  599. VAAPIEncodePicture *pic,
  600. VAAPIEncodePicture **rpl0,
  601. VAAPIEncodePicture **rpl1,
  602. int *rpl_size)
  603. {
  604. VAAPIEncodePicture *prev;
  605. VAAPIEncodeH264Picture *hp, *hn, *hc;
  606. int i, j, n = 0;
  607. prev = pic->prev;
  608. av_assert0(prev);
  609. hp = pic->priv_data;
  610. for (i = 0; i < pic->prev->nb_dpb_pics; i++) {
  611. hn = prev->dpb[i]->priv_data;
  612. av_assert0(hn->frame_num < hp->frame_num);
  613. if (pic->type == PICTURE_TYPE_P) {
  614. for (j = n; j > 0; j--) {
  615. hc = rpl0[j - 1]->priv_data;
  616. av_assert0(hc->frame_num != hn->frame_num);
  617. if (hc->frame_num > hn->frame_num)
  618. break;
  619. rpl0[j] = rpl0[j - 1];
  620. }
  621. rpl0[j] = prev->dpb[i];
  622. } else if (pic->type == PICTURE_TYPE_B) {
  623. for (j = n; j > 0; j--) {
  624. hc = rpl0[j - 1]->priv_data;
  625. av_assert0(hc->pic_order_cnt != hp->pic_order_cnt);
  626. if (hc->pic_order_cnt < hp->pic_order_cnt) {
  627. if (hn->pic_order_cnt > hp->pic_order_cnt ||
  628. hn->pic_order_cnt < hc->pic_order_cnt)
  629. break;
  630. } else {
  631. if (hn->pic_order_cnt > hc->pic_order_cnt)
  632. break;
  633. }
  634. rpl0[j] = rpl0[j - 1];
  635. }
  636. rpl0[j] = prev->dpb[i];
  637. for (j = n; j > 0; j--) {
  638. hc = rpl1[j - 1]->priv_data;
  639. av_assert0(hc->pic_order_cnt != hp->pic_order_cnt);
  640. if (hc->pic_order_cnt > hp->pic_order_cnt) {
  641. if (hn->pic_order_cnt < hp->pic_order_cnt ||
  642. hn->pic_order_cnt > hc->pic_order_cnt)
  643. break;
  644. } else {
  645. if (hn->pic_order_cnt < hc->pic_order_cnt)
  646. break;
  647. }
  648. rpl1[j] = rpl1[j - 1];
  649. }
  650. rpl1[j] = prev->dpb[i];
  651. }
  652. ++n;
  653. }
  654. if (pic->type == PICTURE_TYPE_B) {
  655. for (i = 0; i < n; i++) {
  656. if (rpl0[i] != rpl1[i])
  657. break;
  658. }
  659. if (i == n)
  660. FFSWAP(VAAPIEncodePicture*, rpl1[0], rpl1[1]);
  661. }
  662. if (pic->type == PICTURE_TYPE_P ||
  663. pic->type == PICTURE_TYPE_B) {
  664. av_log(avctx, AV_LOG_DEBUG, "Default RefPicList0 for fn=%d/poc=%d:",
  665. hp->frame_num, hp->pic_order_cnt);
  666. for (i = 0; i < n; i++) {
  667. hn = rpl0[i]->priv_data;
  668. av_log(avctx, AV_LOG_DEBUG, " fn=%d/poc=%d",
  669. hn->frame_num, hn->pic_order_cnt);
  670. }
  671. av_log(avctx, AV_LOG_DEBUG, "\n");
  672. }
  673. if (pic->type == PICTURE_TYPE_B) {
  674. av_log(avctx, AV_LOG_DEBUG, "Default RefPicList1 for fn=%d/poc=%d:",
  675. hp->frame_num, hp->pic_order_cnt);
  676. for (i = 0; i < n; i++) {
  677. hn = rpl1[i]->priv_data;
  678. av_log(avctx, AV_LOG_DEBUG, " fn=%d/poc=%d",
  679. hn->frame_num, hn->pic_order_cnt);
  680. }
  681. av_log(avctx, AV_LOG_DEBUG, "\n");
  682. }
  683. *rpl_size = n;
  684. }
  685. static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
  686. VAAPIEncodePicture *pic,
  687. VAAPIEncodeSlice *slice)
  688. {
  689. VAAPIEncodeH264Context *priv = avctx->priv_data;
  690. VAAPIEncodeH264Picture *hpic = pic->priv_data;
  691. VAAPIEncodePicture *prev = pic->prev;
  692. H264RawSPS *sps = &priv->raw_sps;
  693. H264RawPPS *pps = &priv->raw_pps;
  694. H264RawSliceHeader *sh = &priv->raw_slice.header;
  695. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  696. VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
  697. int i, j;
  698. if (pic->type == PICTURE_TYPE_IDR) {
  699. sh->nal_unit_header.nal_unit_type = H264_NAL_IDR_SLICE;
  700. sh->nal_unit_header.nal_ref_idc = 3;
  701. } else {
  702. sh->nal_unit_header.nal_unit_type = H264_NAL_SLICE;
  703. sh->nal_unit_header.nal_ref_idc = pic->is_reference;
  704. }
  705. sh->first_mb_in_slice = slice->block_start;
  706. sh->slice_type = hpic->slice_type;
  707. sh->pic_parameter_set_id = pps->pic_parameter_set_id;
  708. sh->frame_num = hpic->frame_num &
  709. ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
  710. sh->idr_pic_id = hpic->idr_pic_id;
  711. sh->pic_order_cnt_lsb = hpic->pic_order_cnt &
  712. ((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
  713. sh->direct_spatial_mv_pred_flag = 1;
  714. if (pic->type == PICTURE_TYPE_B)
  715. sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26);
  716. else if (pic->type == PICTURE_TYPE_P)
  717. sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26);
  718. else
  719. sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26);
  720. if (pic->is_reference && pic->type != PICTURE_TYPE_IDR) {
  721. VAAPIEncodePicture *discard_list[MAX_DPB_SIZE];
  722. int discard = 0, keep = 0;
  723. // Discard everything which is in the DPB of the previous frame but
  724. // not in the DPB of this one.
  725. for (i = 0; i < prev->nb_dpb_pics; i++) {
  726. for (j = 0; j < pic->nb_dpb_pics; j++) {
  727. if (prev->dpb[i] == pic->dpb[j])
  728. break;
  729. }
  730. if (j == pic->nb_dpb_pics) {
  731. discard_list[discard] = prev->dpb[i];
  732. ++discard;
  733. } else {
  734. ++keep;
  735. }
  736. }
  737. av_assert0(keep <= priv->dpb_frames);
  738. if (discard == 0) {
  739. sh->adaptive_ref_pic_marking_mode_flag = 0;
  740. } else {
  741. sh->adaptive_ref_pic_marking_mode_flag = 1;
  742. for (i = 0; i < discard; i++) {
  743. VAAPIEncodeH264Picture *old = discard_list[i]->priv_data;
  744. av_assert0(old->frame_num < hpic->frame_num);
  745. sh->mmco[i].memory_management_control_operation = 1;
  746. sh->mmco[i].difference_of_pic_nums_minus1 =
  747. hpic->frame_num - old->frame_num - 1;
  748. }
  749. sh->mmco[i].memory_management_control_operation = 0;
  750. }
  751. }
  752. // If the intended references are not the first entries of RefPicListN
  753. // by default, use ref-pic-list-modification to move them there.
  754. if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) {
  755. VAAPIEncodePicture *def_l0[MAX_DPB_SIZE], *def_l1[MAX_DPB_SIZE];
  756. VAAPIEncodeH264Picture *href;
  757. int n;
  758. vaapi_encode_h264_default_ref_pic_list(avctx, pic,
  759. def_l0, def_l1, &n);
  760. if (pic->type == PICTURE_TYPE_P) {
  761. int need_rplm = 0;
  762. for (i = 0; i < pic->nb_refs; i++) {
  763. av_assert0(pic->refs[i]);
  764. if (pic->refs[i] != def_l0[i])
  765. need_rplm = 1;
  766. }
  767. sh->ref_pic_list_modification_flag_l0 = need_rplm;
  768. if (need_rplm) {
  769. int pic_num = hpic->frame_num;
  770. for (i = 0; i < pic->nb_refs; i++) {
  771. href = pic->refs[i]->priv_data;
  772. av_assert0(href->frame_num != pic_num);
  773. if (href->frame_num < pic_num) {
  774. sh->rplm_l0[i].modification_of_pic_nums_idc = 0;
  775. sh->rplm_l0[i].abs_diff_pic_num_minus1 =
  776. pic_num - href->frame_num - 1;
  777. } else {
  778. sh->rplm_l0[i].modification_of_pic_nums_idc = 1;
  779. sh->rplm_l0[i].abs_diff_pic_num_minus1 =
  780. href->frame_num - pic_num - 1;
  781. }
  782. pic_num = href->frame_num;
  783. }
  784. sh->rplm_l0[i].modification_of_pic_nums_idc = 3;
  785. }
  786. } else {
  787. int need_rplm_l0 = 0, need_rplm_l1 = 0;
  788. int n0 = 0, n1 = 0;
  789. for (i = 0; i < pic->nb_refs; i++) {
  790. av_assert0(pic->refs[i]);
  791. href = pic->refs[i]->priv_data;
  792. av_assert0(href->pic_order_cnt != hpic->pic_order_cnt);
  793. if (href->pic_order_cnt < hpic->pic_order_cnt) {
  794. if (pic->refs[i] != def_l0[n0])
  795. need_rplm_l0 = 1;
  796. ++n0;
  797. } else {
  798. if (pic->refs[i] != def_l1[n1])
  799. need_rplm_l1 = 1;
  800. ++n1;
  801. }
  802. }
  803. sh->ref_pic_list_modification_flag_l0 = need_rplm_l0;
  804. if (need_rplm_l0) {
  805. int pic_num = hpic->frame_num;
  806. for (i = j = 0; i < pic->nb_refs; i++) {
  807. href = pic->refs[i]->priv_data;
  808. if (href->pic_order_cnt > hpic->pic_order_cnt)
  809. continue;
  810. av_assert0(href->frame_num != pic_num);
  811. if (href->frame_num < pic_num) {
  812. sh->rplm_l0[j].modification_of_pic_nums_idc = 0;
  813. sh->rplm_l0[j].abs_diff_pic_num_minus1 =
  814. pic_num - href->frame_num - 1;
  815. } else {
  816. sh->rplm_l0[j].modification_of_pic_nums_idc = 1;
  817. sh->rplm_l0[j].abs_diff_pic_num_minus1 =
  818. href->frame_num - pic_num - 1;
  819. }
  820. pic_num = href->frame_num;
  821. ++j;
  822. }
  823. av_assert0(j == n0);
  824. sh->rplm_l0[j].modification_of_pic_nums_idc = 3;
  825. }
  826. sh->ref_pic_list_modification_flag_l1 = need_rplm_l1;
  827. if (need_rplm_l1) {
  828. int pic_num = hpic->frame_num;
  829. for (i = j = 0; i < pic->nb_refs; i++) {
  830. href = pic->refs[i]->priv_data;
  831. if (href->pic_order_cnt < hpic->pic_order_cnt)
  832. continue;
  833. av_assert0(href->frame_num != pic_num);
  834. if (href->frame_num < pic_num) {
  835. sh->rplm_l1[j].modification_of_pic_nums_idc = 0;
  836. sh->rplm_l1[j].abs_diff_pic_num_minus1 =
  837. pic_num - href->frame_num - 1;
  838. } else {
  839. sh->rplm_l1[j].modification_of_pic_nums_idc = 1;
  840. sh->rplm_l1[j].abs_diff_pic_num_minus1 =
  841. href->frame_num - pic_num - 1;
  842. }
  843. pic_num = href->frame_num;
  844. ++j;
  845. }
  846. av_assert0(j == n1);
  847. sh->rplm_l1[j].modification_of_pic_nums_idc = 3;
  848. }
  849. }
  850. }
  851. vslice->macroblock_address = slice->block_start;
  852. vslice->num_macroblocks = slice->block_size;
  853. vslice->macroblock_info = VA_INVALID_ID;
  854. vslice->slice_type = sh->slice_type % 5;
  855. vslice->pic_parameter_set_id = sh->pic_parameter_set_id;
  856. vslice->idr_pic_id = sh->idr_pic_id;
  857. vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb;
  858. vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag;
  859. for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
  860. vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
  861. vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
  862. vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
  863. vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
  864. }
  865. av_assert0(pic->nb_refs <= 2);
  866. if (pic->nb_refs >= 1) {
  867. // Backward reference for P- or B-frame.
  868. av_assert0(pic->type == PICTURE_TYPE_P ||
  869. pic->type == PICTURE_TYPE_B);
  870. vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
  871. }
  872. if (pic->nb_refs >= 2) {
  873. // Forward reference for B-frame.
  874. av_assert0(pic->type == PICTURE_TYPE_B);
  875. vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
  876. }
  877. vslice->slice_qp_delta = sh->slice_qp_delta;
  878. return 0;
  879. }
  880. static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
  881. {
  882. VAAPIEncodeContext *ctx = avctx->priv_data;
  883. VAAPIEncodeH264Context *priv = avctx->priv_data;
  884. int err;
  885. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
  886. if (err < 0)
  887. return err;
  888. priv->mb_width = FFALIGN(avctx->width, 16) / 16;
  889. priv->mb_height = FFALIGN(avctx->height, 16) / 16;
  890. if (ctx->va_rc_mode == VA_RC_CQP) {
  891. priv->fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
  892. if (avctx->i_quant_factor > 0.0)
  893. priv->fixed_qp_idr =
  894. av_clip((avctx->i_quant_factor * priv->fixed_qp_p +
  895. avctx->i_quant_offset) + 0.5, 1, 51);
  896. else
  897. priv->fixed_qp_idr = priv->fixed_qp_p;
  898. if (avctx->b_quant_factor > 0.0)
  899. priv->fixed_qp_b =
  900. av_clip((avctx->b_quant_factor * priv->fixed_qp_p +
  901. avctx->b_quant_offset) + 0.5, 1, 51);
  902. else
  903. priv->fixed_qp_b = priv->fixed_qp_p;
  904. av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
  905. "%d / %d / %d for IDR- / P- / B-frames.\n",
  906. priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
  907. } else {
  908. // These still need to be set for pic_init_qp/slice_qp_delta.
  909. priv->fixed_qp_idr = 26;
  910. priv->fixed_qp_p = 26;
  911. priv->fixed_qp_b = 26;
  912. }
  913. if (!ctx->rc_mode->hrd) {
  914. // Timing SEI requires a mode respecting HRD parameters.
  915. priv->sei &= ~SEI_TIMING;
  916. }
  917. if (priv->sei & SEI_IDENTIFIER) {
  918. const char *lavc = LIBAVCODEC_IDENT;
  919. const char *vaapi = VA_VERSION_S;
  920. const char *driver;
  921. int len;
  922. memcpy(priv->sei_identifier.uuid_iso_iec_11578,
  923. vaapi_encode_h264_sei_identifier_uuid,
  924. sizeof(priv->sei_identifier.uuid_iso_iec_11578));
  925. driver = vaQueryVendorString(ctx->hwctx->display);
  926. if (!driver)
  927. driver = "unknown driver";
  928. len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver);
  929. if (len >= 0) {
  930. priv->sei_identifier_string = av_malloc(len + 1);
  931. if (!priv->sei_identifier_string)
  932. return AVERROR(ENOMEM);
  933. snprintf(priv->sei_identifier_string, len + 1,
  934. "%s / VAAPI %s / %s", lavc, vaapi, driver);
  935. priv->sei_identifier.data = priv->sei_identifier_string;
  936. priv->sei_identifier.data_length = len + 1;
  937. }
  938. }
  939. ctx->roi_quant_range = 51 + 6 * (ctx->profile->depth - 8);
  940. return 0;
  941. }
  942. static const VAAPIEncodeProfile vaapi_encode_h264_profiles[] = {
  943. { FF_PROFILE_H264_HIGH, 8, 3, 1, 1, VAProfileH264High },
  944. { FF_PROFILE_H264_MAIN, 8, 3, 1, 1, VAProfileH264Main },
  945. { FF_PROFILE_H264_CONSTRAINED_BASELINE,
  946. 8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
  947. { FF_PROFILE_UNKNOWN }
  948. };
  949. static const VAAPIEncodeType vaapi_encode_type_h264 = {
  950. .profiles = vaapi_encode_h264_profiles,
  951. .flags = FLAG_SLICE_CONTROL |
  952. FLAG_B_PICTURES |
  953. FLAG_B_PICTURE_REFERENCES |
  954. FLAG_NON_IDR_KEY_PICTURES,
  955. .default_quality = 20,
  956. .configure = &vaapi_encode_h264_configure,
  957. .picture_priv_data_size = sizeof(VAAPIEncodeH264Picture),
  958. .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
  959. .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
  960. .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
  961. .init_picture_params = &vaapi_encode_h264_init_picture_params,
  962. .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
  963. .init_slice_params = &vaapi_encode_h264_init_slice_params,
  964. .sequence_header_type = VAEncPackedHeaderSequence,
  965. .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
  966. .slice_header_type = VAEncPackedHeaderH264_Slice,
  967. .write_slice_header = &vaapi_encode_h264_write_slice_header,
  968. .write_extra_header = &vaapi_encode_h264_write_extra_header,
  969. };
  970. static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
  971. {
  972. VAAPIEncodeContext *ctx = avctx->priv_data;
  973. VAAPIEncodeH264Context *priv = avctx->priv_data;
  974. ctx->codec = &vaapi_encode_type_h264;
  975. if (avctx->profile == FF_PROFILE_UNKNOWN)
  976. avctx->profile = priv->profile;
  977. if (avctx->level == FF_LEVEL_UNKNOWN)
  978. avctx->level = priv->level;
  979. if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
  980. avctx->compression_level = priv->quality;
  981. // Reject unsupported profiles.
  982. switch (avctx->profile) {
  983. case FF_PROFILE_H264_BASELINE:
  984. av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
  985. "supported, using constrained baseline profile instead.\n");
  986. avctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
  987. break;
  988. case FF_PROFILE_H264_EXTENDED:
  989. av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
  990. "is not supported.\n");
  991. return AVERROR_PATCHWELCOME;
  992. case FF_PROFILE_H264_HIGH_10:
  993. case FF_PROFILE_H264_HIGH_10_INTRA:
  994. av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
  995. "are not supported.\n");
  996. return AVERROR_PATCHWELCOME;
  997. case FF_PROFILE_H264_HIGH_422:
  998. case FF_PROFILE_H264_HIGH_422_INTRA:
  999. case FF_PROFILE_H264_HIGH_444:
  1000. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  1001. case FF_PROFILE_H264_HIGH_444_INTRA:
  1002. case FF_PROFILE_H264_CAVLC_444:
  1003. av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
  1004. "are not supported.\n");
  1005. return AVERROR_PATCHWELCOME;
  1006. }
  1007. if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
  1008. av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
  1009. "in 8-bit unsigned integer.\n", avctx->level);
  1010. return AVERROR(EINVAL);
  1011. }
  1012. ctx->desired_packed_headers =
  1013. VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
  1014. VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
  1015. VA_ENC_PACKED_HEADER_MISC; // SEI.
  1016. ctx->surface_width = FFALIGN(avctx->width, 16);
  1017. ctx->surface_height = FFALIGN(avctx->height, 16);
  1018. ctx->slice_block_height = ctx->slice_block_width = 16;
  1019. if (priv->qp > 0)
  1020. ctx->explicit_qp = priv->qp;
  1021. return ff_vaapi_encode_init(avctx);
  1022. }
  1023. static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
  1024. {
  1025. VAAPIEncodeH264Context *priv = avctx->priv_data;
  1026. ff_cbs_fragment_free(&priv->current_access_unit);
  1027. ff_cbs_close(&priv->cbc);
  1028. av_freep(&priv->sei_identifier_string);
  1029. return ff_vaapi_encode_close(avctx);
  1030. }
  1031. #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
  1032. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  1033. static const AVOption vaapi_encode_h264_options[] = {
  1034. VAAPI_ENCODE_COMMON_OPTIONS,
  1035. VAAPI_ENCODE_RC_OPTIONS,
  1036. { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
  1037. OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
  1038. { "quality", "Set encode quality (trades off against speed, higher is faster)",
  1039. OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  1040. { "coder", "Entropy coder type",
  1041. OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
  1042. { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  1043. { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  1044. { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  1045. { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
  1046. { "aud", "Include AUD",
  1047. OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  1048. { "sei", "Set SEI to include",
  1049. OFFSET(sei), AV_OPT_TYPE_FLAGS,
  1050. { .i64 = SEI_IDENTIFIER | SEI_TIMING | SEI_RECOVERY_POINT },
  1051. 0, INT_MAX, FLAGS, "sei" },
  1052. { "identifier", "Include encoder version identifier",
  1053. 0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
  1054. INT_MIN, INT_MAX, FLAGS, "sei" },
  1055. { "timing", "Include timing parameters (buffering_period and pic_timing)",
  1056. 0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
  1057. INT_MIN, INT_MAX, FLAGS, "sei" },
  1058. { "recovery_point", "Include recovery points where appropriate",
  1059. 0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
  1060. INT_MIN, INT_MAX, FLAGS, "sei" },
  1061. { "profile", "Set profile (profile_idc and constraint_set*_flag)",
  1062. OFFSET(profile), AV_OPT_TYPE_INT,
  1063. { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xffff, FLAGS, "profile" },
  1064. #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  1065. { .i64 = value }, 0, 0, FLAGS, "profile"
  1066. { PROFILE("constrained_baseline", FF_PROFILE_H264_CONSTRAINED_BASELINE) },
  1067. { PROFILE("main", FF_PROFILE_H264_MAIN) },
  1068. { PROFILE("high", FF_PROFILE_H264_HIGH) },
  1069. #undef PROFILE
  1070. { "level", "Set level (level_idc)",
  1071. OFFSET(level), AV_OPT_TYPE_INT,
  1072. { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
  1073. #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  1074. { .i64 = value }, 0, 0, FLAGS, "level"
  1075. { LEVEL("1", 10) },
  1076. { LEVEL("1.1", 11) },
  1077. { LEVEL("1.2", 12) },
  1078. { LEVEL("1.3", 13) },
  1079. { LEVEL("2", 20) },
  1080. { LEVEL("2.1", 21) },
  1081. { LEVEL("2.2", 22) },
  1082. { LEVEL("3", 30) },
  1083. { LEVEL("3.1", 31) },
  1084. { LEVEL("3.2", 32) },
  1085. { LEVEL("4", 40) },
  1086. { LEVEL("4.1", 41) },
  1087. { LEVEL("4.2", 42) },
  1088. { LEVEL("5", 50) },
  1089. { LEVEL("5.1", 51) },
  1090. { LEVEL("5.2", 52) },
  1091. { LEVEL("6", 60) },
  1092. { LEVEL("6.1", 61) },
  1093. { LEVEL("6.2", 62) },
  1094. #undef LEVEL
  1095. { NULL },
  1096. };
  1097. static const AVCodecDefault vaapi_encode_h264_defaults[] = {
  1098. { "b", "0" },
  1099. { "bf", "2" },
  1100. { "g", "120" },
  1101. { "i_qfactor", "1" },
  1102. { "i_qoffset", "0" },
  1103. { "b_qfactor", "6/5" },
  1104. { "b_qoffset", "0" },
  1105. { "qmin", "-1" },
  1106. { "qmax", "-1" },
  1107. { NULL },
  1108. };
  1109. static const AVClass vaapi_encode_h264_class = {
  1110. .class_name = "h264_vaapi",
  1111. .item_name = av_default_item_name,
  1112. .option = vaapi_encode_h264_options,
  1113. .version = LIBAVUTIL_VERSION_INT,
  1114. };
  1115. AVCodec ff_h264_vaapi_encoder = {
  1116. .name = "h264_vaapi",
  1117. .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
  1118. .type = AVMEDIA_TYPE_VIDEO,
  1119. .id = AV_CODEC_ID_H264,
  1120. .priv_data_size = sizeof(VAAPIEncodeH264Context),
  1121. .init = &vaapi_encode_h264_init,
  1122. .receive_packet = &ff_vaapi_encode_receive_packet,
  1123. .close = &vaapi_encode_h264_close,
  1124. .priv_class = &vaapi_encode_h264_class,
  1125. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  1126. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  1127. .defaults = vaapi_encode_h264_defaults,
  1128. .pix_fmts = (const enum AVPixelFormat[]) {
  1129. AV_PIX_FMT_VAAPI,
  1130. AV_PIX_FMT_NONE,
  1131. },
  1132. .hw_configs = ff_vaapi_encode_hw_configs,
  1133. .wrapper_name = "vaapi",
  1134. };