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.

1364 lines
49KB

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