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.

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