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.

1066 lines
37KB

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