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.

1302 lines
46KB

  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_hevc.h>
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/mastering_display_metadata.h"
  26. #include "avcodec.h"
  27. #include "cbs.h"
  28. #include "cbs_h265.h"
  29. #include "h265_profile_level.h"
  30. #include "hevc.h"
  31. #include "hevc_sei.h"
  32. #include "internal.h"
  33. #include "put_bits.h"
  34. #include "vaapi_encode.h"
  35. enum {
  36. SEI_MASTERING_DISPLAY = 0x08,
  37. SEI_CONTENT_LIGHT_LEVEL = 0x10,
  38. };
  39. typedef struct VAAPIEncodeH265Picture {
  40. int pic_order_cnt;
  41. int64_t last_idr_frame;
  42. int slice_nal_unit;
  43. int slice_type;
  44. int pic_type;
  45. } VAAPIEncodeH265Picture;
  46. typedef struct VAAPIEncodeH265Context {
  47. VAAPIEncodeContext common;
  48. // User options.
  49. int qp;
  50. int aud;
  51. int profile;
  52. int tier;
  53. int level;
  54. int sei;
  55. // Derived settings.
  56. int fixed_qp_idr;
  57. int fixed_qp_p;
  58. int fixed_qp_b;
  59. // Writer structures.
  60. H265RawAUD raw_aud;
  61. H265RawVPS raw_vps;
  62. H265RawSPS raw_sps;
  63. H265RawPPS raw_pps;
  64. H265RawSEI raw_sei;
  65. H265RawSlice raw_slice;
  66. H265RawSEIMasteringDisplayColourVolume sei_mastering_display;
  67. H265RawSEIContentLightLevelInfo sei_content_light_level;
  68. CodedBitstreamContext *cbc;
  69. CodedBitstreamFragment current_access_unit;
  70. int aud_needed;
  71. int sei_needed;
  72. } VAAPIEncodeH265Context;
  73. static int vaapi_encode_h265_write_access_unit(AVCodecContext *avctx,
  74. char *data, size_t *data_len,
  75. CodedBitstreamFragment *au)
  76. {
  77. VAAPIEncodeH265Context *priv = avctx->priv_data;
  78. int err;
  79. err = ff_cbs_write_fragment_data(priv->cbc, au);
  80. if (err < 0) {
  81. av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
  82. return err;
  83. }
  84. if (*data_len < 8 * au->data_size - au->data_bit_padding) {
  85. av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
  86. "%zu < %zu.\n", *data_len,
  87. 8 * au->data_size - au->data_bit_padding);
  88. return AVERROR(ENOSPC);
  89. }
  90. memcpy(data, au->data, au->data_size);
  91. *data_len = 8 * au->data_size - au->data_bit_padding;
  92. return 0;
  93. }
  94. static int vaapi_encode_h265_add_nal(AVCodecContext *avctx,
  95. CodedBitstreamFragment *au,
  96. void *nal_unit)
  97. {
  98. VAAPIEncodeH265Context *priv = avctx->priv_data;
  99. H265RawNALUnitHeader *header = nal_unit;
  100. int err;
  101. err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
  102. header->nal_unit_type, nal_unit, NULL);
  103. if (err < 0) {
  104. av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
  105. "type = %d.\n", header->nal_unit_type);
  106. return err;
  107. }
  108. return 0;
  109. }
  110. static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
  111. char *data, size_t *data_len)
  112. {
  113. VAAPIEncodeH265Context *priv = avctx->priv_data;
  114. CodedBitstreamFragment *au = &priv->current_access_unit;
  115. int err;
  116. if (priv->aud_needed) {
  117. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
  118. if (err < 0)
  119. goto fail;
  120. priv->aud_needed = 0;
  121. }
  122. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_vps);
  123. if (err < 0)
  124. goto fail;
  125. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_sps);
  126. if (err < 0)
  127. goto fail;
  128. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_pps);
  129. if (err < 0)
  130. goto fail;
  131. err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
  132. fail:
  133. ff_cbs_fragment_reset(priv->cbc, au);
  134. return err;
  135. }
  136. static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
  137. VAAPIEncodePicture *pic,
  138. VAAPIEncodeSlice *slice,
  139. char *data, size_t *data_len)
  140. {
  141. VAAPIEncodeH265Context *priv = avctx->priv_data;
  142. CodedBitstreamFragment *au = &priv->current_access_unit;
  143. int err;
  144. if (priv->aud_needed) {
  145. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
  146. if (err < 0)
  147. goto fail;
  148. priv->aud_needed = 0;
  149. }
  150. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_slice);
  151. if (err < 0)
  152. goto fail;
  153. err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
  154. fail:
  155. ff_cbs_fragment_reset(priv->cbc, au);
  156. return err;
  157. }
  158. static int vaapi_encode_h265_write_extra_header(AVCodecContext *avctx,
  159. VAAPIEncodePicture *pic,
  160. int index, int *type,
  161. char *data, size_t *data_len)
  162. {
  163. VAAPIEncodeH265Context *priv = avctx->priv_data;
  164. CodedBitstreamFragment *au = &priv->current_access_unit;
  165. int err, i;
  166. if (priv->sei_needed) {
  167. H265RawSEI *sei = &priv->raw_sei;
  168. if (priv->aud_needed) {
  169. err = vaapi_encode_h265_add_nal(avctx, au, &priv->aud);
  170. if (err < 0)
  171. goto fail;
  172. priv->aud_needed = 0;
  173. }
  174. *sei = (H265RawSEI) {
  175. .nal_unit_header = {
  176. .nal_unit_type = HEVC_NAL_SEI_PREFIX,
  177. .nuh_layer_id = 0,
  178. .nuh_temporal_id_plus1 = 1,
  179. },
  180. };
  181. i = 0;
  182. if (priv->sei_needed & SEI_MASTERING_DISPLAY) {
  183. sei->payload[i].payload_type = HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO;
  184. sei->payload[i].payload.mastering_display = priv->sei_mastering_display;
  185. ++i;
  186. }
  187. if (priv->sei_needed & SEI_CONTENT_LIGHT_LEVEL) {
  188. sei->payload[i].payload_type = HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO;
  189. sei->payload[i].payload.content_light_level = priv->sei_content_light_level;
  190. ++i;
  191. }
  192. sei->payload_count = i;
  193. av_assert0(sei->payload_count > 0);
  194. err = vaapi_encode_h265_add_nal(avctx, au, sei);
  195. if (err < 0)
  196. goto fail;
  197. priv->sei_needed = 0;
  198. err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
  199. if (err < 0)
  200. goto fail;
  201. ff_cbs_fragment_reset(priv->cbc, au);
  202. *type = VAEncPackedHeaderRawData;
  203. return 0;
  204. } else {
  205. return AVERROR_EOF;
  206. }
  207. fail:
  208. ff_cbs_fragment_reset(priv->cbc, au);
  209. return err;
  210. }
  211. static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
  212. {
  213. VAAPIEncodeContext *ctx = avctx->priv_data;
  214. VAAPIEncodeH265Context *priv = avctx->priv_data;
  215. H265RawVPS *vps = &priv->raw_vps;
  216. H265RawSPS *sps = &priv->raw_sps;
  217. H265RawPPS *pps = &priv->raw_pps;
  218. H265RawProfileTierLevel *ptl = &vps->profile_tier_level;
  219. H265RawVUI *vui = &sps->vui;
  220. VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
  221. VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
  222. const AVPixFmtDescriptor *desc;
  223. int chroma_format, bit_depth;
  224. int i;
  225. memset(vps, 0, sizeof(*vps));
  226. memset(sps, 0, sizeof(*sps));
  227. memset(pps, 0, sizeof(*pps));
  228. desc = av_pix_fmt_desc_get(priv->common.input_frames->sw_format);
  229. av_assert0(desc);
  230. if (desc->nb_components == 1) {
  231. chroma_format = 0;
  232. } else {
  233. if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1) {
  234. chroma_format = 1;
  235. } else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0) {
  236. chroma_format = 2;
  237. } else if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
  238. chroma_format = 3;
  239. } else {
  240. av_log(avctx, AV_LOG_ERROR, "Chroma format of input pixel format "
  241. "%s is not supported.\n", desc->name);
  242. return AVERROR(EINVAL);
  243. }
  244. }
  245. bit_depth = desc->comp[0].depth;
  246. // VPS
  247. vps->nal_unit_header = (H265RawNALUnitHeader) {
  248. .nal_unit_type = HEVC_NAL_VPS,
  249. .nuh_layer_id = 0,
  250. .nuh_temporal_id_plus1 = 1,
  251. };
  252. vps->vps_video_parameter_set_id = 0;
  253. vps->vps_base_layer_internal_flag = 1;
  254. vps->vps_base_layer_available_flag = 1;
  255. vps->vps_max_layers_minus1 = 0;
  256. vps->vps_max_sub_layers_minus1 = 0;
  257. vps->vps_temporal_id_nesting_flag = 1;
  258. ptl->general_profile_space = 0;
  259. ptl->general_profile_idc = avctx->profile;
  260. ptl->general_tier_flag = priv->tier;
  261. if (chroma_format == 1) {
  262. ptl->general_profile_compatibility_flag[1] = bit_depth == 8;
  263. ptl->general_profile_compatibility_flag[2] = bit_depth <= 10;
  264. }
  265. ptl->general_profile_compatibility_flag[4] = 1;
  266. ptl->general_progressive_source_flag = 1;
  267. ptl->general_interlaced_source_flag = 0;
  268. ptl->general_non_packed_constraint_flag = 1;
  269. ptl->general_frame_only_constraint_flag = 1;
  270. ptl->general_max_12bit_constraint_flag = bit_depth <= 12;
  271. ptl->general_max_10bit_constraint_flag = bit_depth <= 10;
  272. ptl->general_max_8bit_constraint_flag = bit_depth == 8;
  273. ptl->general_max_422chroma_constraint_flag = chroma_format <= 2;
  274. ptl->general_max_420chroma_constraint_flag = chroma_format <= 1;
  275. ptl->general_max_monochrome_constraint_flag = chroma_format == 0;
  276. ptl->general_intra_constraint_flag = ctx->gop_size == 1;
  277. ptl->general_lower_bit_rate_constraint_flag = 1;
  278. if (avctx->level != FF_LEVEL_UNKNOWN) {
  279. ptl->general_level_idc = avctx->level;
  280. } else {
  281. const H265LevelDescriptor *level;
  282. level = ff_h265_guess_level(ptl, avctx->bit_rate,
  283. ctx->surface_width, ctx->surface_height,
  284. ctx->nb_slices, 1, 1,
  285. (ctx->b_per_p > 0) + 1);
  286. if (level) {
  287. av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
  288. ptl->general_level_idc = level->level_idc;
  289. } else {
  290. av_log(avctx, AV_LOG_VERBOSE, "Stream will not conform to "
  291. "any normal level; using level 8.5.\n");
  292. ptl->general_level_idc = 255;
  293. // The tier flag must be set in level 8.5.
  294. ptl->general_tier_flag = 1;
  295. }
  296. }
  297. vps->vps_sub_layer_ordering_info_present_flag = 0;
  298. vps->vps_max_dec_pic_buffering_minus1[0] = ctx->max_b_depth + 1;
  299. vps->vps_max_num_reorder_pics[0] = ctx->max_b_depth;
  300. vps->vps_max_latency_increase_plus1[0] = 0;
  301. vps->vps_max_layer_id = 0;
  302. vps->vps_num_layer_sets_minus1 = 0;
  303. vps->layer_id_included_flag[0][0] = 1;
  304. vps->vps_timing_info_present_flag = 1;
  305. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  306. vps->vps_num_units_in_tick = avctx->framerate.den;
  307. vps->vps_time_scale = avctx->framerate.num;
  308. vps->vps_poc_proportional_to_timing_flag = 1;
  309. vps->vps_num_ticks_poc_diff_one_minus1 = 0;
  310. } else {
  311. vps->vps_num_units_in_tick = avctx->time_base.num;
  312. vps->vps_time_scale = avctx->time_base.den;
  313. vps->vps_poc_proportional_to_timing_flag = 0;
  314. }
  315. vps->vps_num_hrd_parameters = 0;
  316. // SPS
  317. sps->nal_unit_header = (H265RawNALUnitHeader) {
  318. .nal_unit_type = HEVC_NAL_SPS,
  319. .nuh_layer_id = 0,
  320. .nuh_temporal_id_plus1 = 1,
  321. };
  322. sps->sps_video_parameter_set_id = vps->vps_video_parameter_set_id;
  323. sps->sps_max_sub_layers_minus1 = vps->vps_max_sub_layers_minus1;
  324. sps->sps_temporal_id_nesting_flag = vps->vps_temporal_id_nesting_flag;
  325. sps->profile_tier_level = vps->profile_tier_level;
  326. sps->sps_seq_parameter_set_id = 0;
  327. sps->chroma_format_idc = chroma_format;
  328. sps->separate_colour_plane_flag = 0;
  329. sps->pic_width_in_luma_samples = ctx->surface_width;
  330. sps->pic_height_in_luma_samples = ctx->surface_height;
  331. if (avctx->width != ctx->surface_width ||
  332. avctx->height != ctx->surface_height) {
  333. sps->conformance_window_flag = 1;
  334. sps->conf_win_left_offset = 0;
  335. sps->conf_win_right_offset =
  336. (ctx->surface_width - avctx->width) >> desc->log2_chroma_w;
  337. sps->conf_win_top_offset = 0;
  338. sps->conf_win_bottom_offset =
  339. (ctx->surface_height - avctx->height) >> desc->log2_chroma_h;
  340. } else {
  341. sps->conformance_window_flag = 0;
  342. }
  343. sps->bit_depth_luma_minus8 = bit_depth - 8;
  344. sps->bit_depth_chroma_minus8 = bit_depth - 8;
  345. sps->log2_max_pic_order_cnt_lsb_minus4 = 8;
  346. sps->sps_sub_layer_ordering_info_present_flag =
  347. vps->vps_sub_layer_ordering_info_present_flag;
  348. for (i = 0; i <= sps->sps_max_sub_layers_minus1; i++) {
  349. sps->sps_max_dec_pic_buffering_minus1[i] =
  350. vps->vps_max_dec_pic_buffering_minus1[i];
  351. sps->sps_max_num_reorder_pics[i] =
  352. vps->vps_max_num_reorder_pics[i];
  353. sps->sps_max_latency_increase_plus1[i] =
  354. vps->vps_max_latency_increase_plus1[i];
  355. }
  356. // These have to come from the capabilities of the encoder. We have no
  357. // way to query them, so just hardcode parameters which work on the Intel
  358. // driver.
  359. // CTB size from 8x8 to 32x32.
  360. sps->log2_min_luma_coding_block_size_minus3 = 0;
  361. sps->log2_diff_max_min_luma_coding_block_size = 2;
  362. // Transform size from 4x4 to 32x32.
  363. sps->log2_min_luma_transform_block_size_minus2 = 0;
  364. sps->log2_diff_max_min_luma_transform_block_size = 3;
  365. // Full transform hierarchy allowed (2-5).
  366. sps->max_transform_hierarchy_depth_inter = 3;
  367. sps->max_transform_hierarchy_depth_intra = 3;
  368. // AMP works.
  369. sps->amp_enabled_flag = 1;
  370. // SAO and temporal MVP do not work.
  371. sps->sample_adaptive_offset_enabled_flag = 0;
  372. sps->sps_temporal_mvp_enabled_flag = 0;
  373. sps->pcm_enabled_flag = 0;
  374. // STRPSs should ideally be here rather than defined individually in
  375. // each slice, but the structure isn't completely fixed so for now
  376. // don't bother.
  377. sps->num_short_term_ref_pic_sets = 0;
  378. sps->long_term_ref_pics_present_flag = 0;
  379. sps->vui_parameters_present_flag = 1;
  380. if (avctx->sample_aspect_ratio.num != 0 &&
  381. avctx->sample_aspect_ratio.den != 0) {
  382. static const AVRational sar_idc[] = {
  383. { 0, 0 },
  384. { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
  385. { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
  386. { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
  387. { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
  388. };
  389. int num, den, i;
  390. av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
  391. avctx->sample_aspect_ratio.den, 65535);
  392. for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
  393. if (num == sar_idc[i].num &&
  394. den == sar_idc[i].den) {
  395. vui->aspect_ratio_idc = i;
  396. break;
  397. }
  398. }
  399. if (i >= FF_ARRAY_ELEMS(sar_idc)) {
  400. vui->aspect_ratio_idc = 255;
  401. vui->sar_width = num;
  402. vui->sar_height = den;
  403. }
  404. vui->aspect_ratio_info_present_flag = 1;
  405. }
  406. if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
  407. avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  408. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  409. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  410. vui->video_signal_type_present_flag = 1;
  411. vui->video_format = 5; // Unspecified.
  412. vui->video_full_range_flag =
  413. avctx->color_range == AVCOL_RANGE_JPEG;
  414. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  415. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  416. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  417. vui->colour_description_present_flag = 1;
  418. vui->colour_primaries = avctx->color_primaries;
  419. vui->transfer_characteristics = avctx->color_trc;
  420. vui->matrix_coefficients = avctx->colorspace;
  421. }
  422. } else {
  423. vui->video_format = 5;
  424. vui->video_full_range_flag = 0;
  425. vui->colour_primaries = avctx->color_primaries;
  426. vui->transfer_characteristics = avctx->color_trc;
  427. vui->matrix_coefficients = avctx->colorspace;
  428. }
  429. if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
  430. vui->chroma_loc_info_present_flag = 1;
  431. vui->chroma_sample_loc_type_top_field =
  432. vui->chroma_sample_loc_type_bottom_field =
  433. avctx->chroma_sample_location - 1;
  434. }
  435. vui->vui_timing_info_present_flag = 1;
  436. vui->vui_num_units_in_tick = vps->vps_num_units_in_tick;
  437. vui->vui_time_scale = vps->vps_time_scale;
  438. vui->vui_poc_proportional_to_timing_flag = vps->vps_poc_proportional_to_timing_flag;
  439. vui->vui_num_ticks_poc_diff_one_minus1 = vps->vps_num_ticks_poc_diff_one_minus1;
  440. vui->vui_hrd_parameters_present_flag = 0;
  441. vui->bitstream_restriction_flag = 1;
  442. vui->motion_vectors_over_pic_boundaries_flag = 1;
  443. vui->restricted_ref_pic_lists_flag = 1;
  444. vui->max_bytes_per_pic_denom = 0;
  445. vui->max_bits_per_min_cu_denom = 0;
  446. vui->log2_max_mv_length_horizontal = 15;
  447. vui->log2_max_mv_length_vertical = 15;
  448. // PPS
  449. pps->nal_unit_header = (H265RawNALUnitHeader) {
  450. .nal_unit_type = HEVC_NAL_PPS,
  451. .nuh_layer_id = 0,
  452. .nuh_temporal_id_plus1 = 1,
  453. };
  454. pps->pps_pic_parameter_set_id = 0;
  455. pps->pps_seq_parameter_set_id = sps->sps_seq_parameter_set_id;
  456. pps->num_ref_idx_l0_default_active_minus1 = 0;
  457. pps->num_ref_idx_l1_default_active_minus1 = 0;
  458. pps->init_qp_minus26 = priv->fixed_qp_idr - 26;
  459. pps->cu_qp_delta_enabled_flag = (ctx->va_rc_mode != VA_RC_CQP);
  460. pps->diff_cu_qp_delta_depth = 0;
  461. pps->pps_loop_filter_across_slices_enabled_flag = 1;
  462. // Fill VAAPI parameter buffers.
  463. *vseq = (VAEncSequenceParameterBufferHEVC) {
  464. .general_profile_idc = vps->profile_tier_level.general_profile_idc,
  465. .general_level_idc = vps->profile_tier_level.general_level_idc,
  466. .general_tier_flag = vps->profile_tier_level.general_tier_flag,
  467. .intra_period = ctx->gop_size,
  468. .intra_idr_period = ctx->gop_size,
  469. .ip_period = ctx->b_per_p + 1,
  470. .bits_per_second = ctx->va_bit_rate,
  471. .pic_width_in_luma_samples = sps->pic_width_in_luma_samples,
  472. .pic_height_in_luma_samples = sps->pic_height_in_luma_samples,
  473. .seq_fields.bits = {
  474. .chroma_format_idc = sps->chroma_format_idc,
  475. .separate_colour_plane_flag = sps->separate_colour_plane_flag,
  476. .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
  477. .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
  478. .scaling_list_enabled_flag = sps->scaling_list_enabled_flag,
  479. .strong_intra_smoothing_enabled_flag =
  480. sps->strong_intra_smoothing_enabled_flag,
  481. .amp_enabled_flag = sps->amp_enabled_flag,
  482. .sample_adaptive_offset_enabled_flag =
  483. sps->sample_adaptive_offset_enabled_flag,
  484. .pcm_enabled_flag = sps->pcm_enabled_flag,
  485. .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled_flag,
  486. .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
  487. },
  488. .log2_min_luma_coding_block_size_minus3 =
  489. sps->log2_min_luma_coding_block_size_minus3,
  490. .log2_diff_max_min_luma_coding_block_size =
  491. sps->log2_diff_max_min_luma_coding_block_size,
  492. .log2_min_transform_block_size_minus2 =
  493. sps->log2_min_luma_transform_block_size_minus2,
  494. .log2_diff_max_min_transform_block_size =
  495. sps->log2_diff_max_min_luma_transform_block_size,
  496. .max_transform_hierarchy_depth_inter =
  497. sps->max_transform_hierarchy_depth_inter,
  498. .max_transform_hierarchy_depth_intra =
  499. sps->max_transform_hierarchy_depth_intra,
  500. .pcm_sample_bit_depth_luma_minus1 =
  501. sps->pcm_sample_bit_depth_luma_minus1,
  502. .pcm_sample_bit_depth_chroma_minus1 =
  503. sps->pcm_sample_bit_depth_chroma_minus1,
  504. .log2_min_pcm_luma_coding_block_size_minus3 =
  505. sps->log2_min_pcm_luma_coding_block_size_minus3,
  506. .log2_max_pcm_luma_coding_block_size_minus3 =
  507. sps->log2_min_pcm_luma_coding_block_size_minus3 +
  508. sps->log2_diff_max_min_pcm_luma_coding_block_size,
  509. .vui_parameters_present_flag = 0,
  510. };
  511. *vpic = (VAEncPictureParameterBufferHEVC) {
  512. .decoded_curr_pic = {
  513. .picture_id = VA_INVALID_ID,
  514. .flags = VA_PICTURE_HEVC_INVALID,
  515. },
  516. .coded_buf = VA_INVALID_ID,
  517. .collocated_ref_pic_index = 0xff,
  518. .last_picture = 0,
  519. .pic_init_qp = pps->init_qp_minus26 + 26,
  520. .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
  521. .pps_cb_qp_offset = pps->pps_cb_qp_offset,
  522. .pps_cr_qp_offset = pps->pps_cr_qp_offset,
  523. .num_tile_columns_minus1 = pps->num_tile_columns_minus1,
  524. .num_tile_rows_minus1 = pps->num_tile_rows_minus1,
  525. .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level_minus2,
  526. .ctu_max_bitsize_allowed = 0,
  527. .num_ref_idx_l0_default_active_minus1 =
  528. pps->num_ref_idx_l0_default_active_minus1,
  529. .num_ref_idx_l1_default_active_minus1 =
  530. pps->num_ref_idx_l1_default_active_minus1,
  531. .slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id,
  532. .pic_fields.bits = {
  533. .sign_data_hiding_enabled_flag = pps->sign_data_hiding_enabled_flag,
  534. .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
  535. .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
  536. .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
  537. .weighted_pred_flag = pps->weighted_pred_flag,
  538. .weighted_bipred_flag = pps->weighted_bipred_flag,
  539. .transquant_bypass_enabled_flag = pps->transquant_bypass_enabled_flag,
  540. .tiles_enabled_flag = pps->tiles_enabled_flag,
  541. .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
  542. .loop_filter_across_tiles_enabled_flag =
  543. pps->loop_filter_across_tiles_enabled_flag,
  544. .scaling_list_data_present_flag = (sps->sps_scaling_list_data_present_flag |
  545. pps->pps_scaling_list_data_present_flag),
  546. .screen_content_flag = 0,
  547. .enable_gpu_weighted_prediction = 0,
  548. .no_output_of_prior_pics_flag = 0,
  549. },
  550. };
  551. return 0;
  552. }
  553. static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
  554. VAAPIEncodePicture *pic)
  555. {
  556. VAAPIEncodeContext *ctx = avctx->priv_data;
  557. VAAPIEncodeH265Context *priv = avctx->priv_data;
  558. VAAPIEncodeH265Picture *hpic = pic->priv_data;
  559. VAAPIEncodePicture *prev = pic->prev;
  560. VAAPIEncodeH265Picture *hprev = prev ? prev->priv_data : NULL;
  561. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  562. int i;
  563. if (pic->type == PICTURE_TYPE_IDR) {
  564. av_assert0(pic->display_order == pic->encode_order);
  565. hpic->last_idr_frame = pic->display_order;
  566. hpic->slice_nal_unit = HEVC_NAL_IDR_W_RADL;
  567. hpic->slice_type = HEVC_SLICE_I;
  568. hpic->pic_type = 0;
  569. } else {
  570. av_assert0(prev);
  571. hpic->last_idr_frame = hprev->last_idr_frame;
  572. if (pic->type == PICTURE_TYPE_I) {
  573. hpic->slice_nal_unit = HEVC_NAL_CRA_NUT;
  574. hpic->slice_type = HEVC_SLICE_I;
  575. hpic->pic_type = 0;
  576. } else if (pic->type == PICTURE_TYPE_P) {
  577. av_assert0(pic->refs[0]);
  578. hpic->slice_nal_unit = HEVC_NAL_TRAIL_R;
  579. hpic->slice_type = HEVC_SLICE_P;
  580. hpic->pic_type = 1;
  581. } else {
  582. VAAPIEncodePicture *irap_ref;
  583. av_assert0(pic->refs[0] && pic->refs[1]);
  584. for (irap_ref = pic; irap_ref; irap_ref = irap_ref->refs[1]) {
  585. if (irap_ref->type == PICTURE_TYPE_I)
  586. break;
  587. }
  588. if (pic->b_depth == ctx->max_b_depth) {
  589. hpic->slice_nal_unit = irap_ref ? HEVC_NAL_RASL_N
  590. : HEVC_NAL_TRAIL_N;
  591. } else {
  592. hpic->slice_nal_unit = irap_ref ? HEVC_NAL_RASL_R
  593. : HEVC_NAL_TRAIL_R;
  594. }
  595. hpic->slice_type = HEVC_SLICE_B;
  596. hpic->pic_type = 2;
  597. }
  598. }
  599. hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame;
  600. if (priv->aud) {
  601. priv->aud_needed = 1;
  602. priv->raw_aud = (H265RawAUD) {
  603. .nal_unit_header = {
  604. .nal_unit_type = HEVC_NAL_AUD,
  605. .nuh_layer_id = 0,
  606. .nuh_temporal_id_plus1 = 1,
  607. },
  608. .pic_type = hpic->pic_type,
  609. };
  610. } else {
  611. priv->aud_needed = 0;
  612. }
  613. priv->sei_needed = 0;
  614. // Only look for the metadata on I/IDR frame on the output. We
  615. // may force an IDR frame on the output where the medadata gets
  616. // changed on the input frame.
  617. if ((priv->sei & SEI_MASTERING_DISPLAY) &&
  618. (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
  619. AVFrameSideData *sd =
  620. av_frame_get_side_data(pic->input_image,
  621. AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
  622. if (sd) {
  623. AVMasteringDisplayMetadata *mdm =
  624. (AVMasteringDisplayMetadata *)sd->data;
  625. // SEI is needed when both the primaries and luminance are set
  626. if (mdm->has_primaries && mdm->has_luminance) {
  627. H265RawSEIMasteringDisplayColourVolume *mdcv =
  628. &priv->sei_mastering_display;
  629. const int mapping[3] = {1, 2, 0};
  630. const int chroma_den = 50000;
  631. const int luma_den = 10000;
  632. for (i = 0; i < 3; i++) {
  633. const int j = mapping[i];
  634. mdcv->display_primaries_x[i] =
  635. FFMIN(lrint(chroma_den *
  636. av_q2d(mdm->display_primaries[j][0])),
  637. chroma_den);
  638. mdcv->display_primaries_y[i] =
  639. FFMIN(lrint(chroma_den *
  640. av_q2d(mdm->display_primaries[j][1])),
  641. chroma_den);
  642. }
  643. mdcv->white_point_x =
  644. FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])),
  645. chroma_den);
  646. mdcv->white_point_y =
  647. FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])),
  648. chroma_den);
  649. mdcv->max_display_mastering_luminance =
  650. lrint(luma_den * av_q2d(mdm->max_luminance));
  651. mdcv->min_display_mastering_luminance =
  652. FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)),
  653. mdcv->max_display_mastering_luminance);
  654. priv->sei_needed |= SEI_MASTERING_DISPLAY;
  655. }
  656. }
  657. }
  658. if ((priv->sei & SEI_CONTENT_LIGHT_LEVEL) &&
  659. (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
  660. AVFrameSideData *sd =
  661. av_frame_get_side_data(pic->input_image,
  662. AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
  663. if (sd) {
  664. AVContentLightMetadata *clm =
  665. (AVContentLightMetadata *)sd->data;
  666. H265RawSEIContentLightLevelInfo *clli =
  667. &priv->sei_content_light_level;
  668. clli->max_content_light_level = FFMIN(clm->MaxCLL, 65535);
  669. clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535);
  670. priv->sei_needed |= SEI_CONTENT_LIGHT_LEVEL;
  671. }
  672. }
  673. vpic->decoded_curr_pic = (VAPictureHEVC) {
  674. .picture_id = pic->recon_surface,
  675. .pic_order_cnt = hpic->pic_order_cnt,
  676. .flags = 0,
  677. };
  678. for (i = 0; i < pic->nb_refs; i++) {
  679. VAAPIEncodePicture *ref = pic->refs[i];
  680. VAAPIEncodeH265Picture *href;
  681. av_assert0(ref && ref->encode_order < pic->encode_order);
  682. href = ref->priv_data;
  683. vpic->reference_frames[i] = (VAPictureHEVC) {
  684. .picture_id = ref->recon_surface,
  685. .pic_order_cnt = href->pic_order_cnt,
  686. .flags = (ref->display_order < pic->display_order ?
  687. VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
  688. (ref->display_order > pic->display_order ?
  689. VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
  690. };
  691. }
  692. for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
  693. vpic->reference_frames[i] = (VAPictureHEVC) {
  694. .picture_id = VA_INVALID_ID,
  695. .flags = VA_PICTURE_HEVC_INVALID,
  696. };
  697. }
  698. vpic->coded_buf = pic->output_buffer;
  699. vpic->nal_unit_type = hpic->slice_nal_unit;
  700. switch (pic->type) {
  701. case PICTURE_TYPE_IDR:
  702. vpic->pic_fields.bits.idr_pic_flag = 1;
  703. vpic->pic_fields.bits.coding_type = 1;
  704. vpic->pic_fields.bits.reference_pic_flag = 1;
  705. break;
  706. case PICTURE_TYPE_I:
  707. vpic->pic_fields.bits.idr_pic_flag = 0;
  708. vpic->pic_fields.bits.coding_type = 1;
  709. vpic->pic_fields.bits.reference_pic_flag = 1;
  710. break;
  711. case PICTURE_TYPE_P:
  712. vpic->pic_fields.bits.idr_pic_flag = 0;
  713. vpic->pic_fields.bits.coding_type = 2;
  714. vpic->pic_fields.bits.reference_pic_flag = 1;
  715. break;
  716. case PICTURE_TYPE_B:
  717. vpic->pic_fields.bits.idr_pic_flag = 0;
  718. vpic->pic_fields.bits.coding_type = 3;
  719. vpic->pic_fields.bits.reference_pic_flag = 0;
  720. break;
  721. default:
  722. av_assert0(0 && "invalid picture type");
  723. }
  724. return 0;
  725. }
  726. static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
  727. VAAPIEncodePicture *pic,
  728. VAAPIEncodeSlice *slice)
  729. {
  730. VAAPIEncodeH265Context *priv = avctx->priv_data;
  731. VAAPIEncodeH265Picture *hpic = pic->priv_data;
  732. const H265RawSPS *sps = &priv->raw_sps;
  733. const H265RawPPS *pps = &priv->raw_pps;
  734. H265RawSliceHeader *sh = &priv->raw_slice.header;
  735. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  736. VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
  737. int i;
  738. sh->nal_unit_header = (H265RawNALUnitHeader) {
  739. .nal_unit_type = hpic->slice_nal_unit,
  740. .nuh_layer_id = 0,
  741. .nuh_temporal_id_plus1 = 1,
  742. };
  743. sh->slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id;
  744. sh->first_slice_segment_in_pic_flag = slice->index == 0;
  745. sh->slice_segment_address = slice->block_start;
  746. sh->slice_type = hpic->slice_type;
  747. sh->slice_pic_order_cnt_lsb = hpic->pic_order_cnt &
  748. (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
  749. if (pic->type != PICTURE_TYPE_IDR) {
  750. H265RawSTRefPicSet *rps;
  751. const VAAPIEncodeH265Picture *strp;
  752. int rps_poc[MAX_DPB_SIZE];
  753. int rps_used[MAX_DPB_SIZE];
  754. int i, j, poc, rps_pics;
  755. sh->short_term_ref_pic_set_sps_flag = 0;
  756. rps = &sh->short_term_ref_pic_set;
  757. memset(rps, 0, sizeof(*rps));
  758. rps_pics = 0;
  759. for (i = 0; i < pic->nb_refs; i++) {
  760. strp = pic->refs[i]->priv_data;
  761. rps_poc[rps_pics] = strp->pic_order_cnt;
  762. rps_used[rps_pics] = 1;
  763. ++rps_pics;
  764. }
  765. for (i = 0; i < pic->nb_dpb_pics; i++) {
  766. if (pic->dpb[i] == pic)
  767. continue;
  768. for (j = 0; j < pic->nb_refs; j++) {
  769. if (pic->dpb[i] == pic->refs[j])
  770. break;
  771. }
  772. if (j < pic->nb_refs)
  773. continue;
  774. strp = pic->dpb[i]->priv_data;
  775. rps_poc[rps_pics] = strp->pic_order_cnt;
  776. rps_used[rps_pics] = 0;
  777. ++rps_pics;
  778. }
  779. for (i = 1; i < rps_pics; i++) {
  780. for (j = i; j > 0; j--) {
  781. if (rps_poc[j] > rps_poc[j - 1])
  782. break;
  783. av_assert0(rps_poc[j] != rps_poc[j - 1]);
  784. FFSWAP(int, rps_poc[j], rps_poc[j - 1]);
  785. FFSWAP(int, rps_used[j], rps_used[j - 1]);
  786. }
  787. }
  788. av_log(avctx, AV_LOG_DEBUG, "RPS for POC %d:",
  789. hpic->pic_order_cnt);
  790. for (i = 0; i < rps_pics; i++) {
  791. av_log(avctx, AV_LOG_DEBUG, " (%d,%d)",
  792. rps_poc[i], rps_used[i]);
  793. }
  794. av_log(avctx, AV_LOG_DEBUG, "\n");
  795. for (i = 0; i < rps_pics; i++) {
  796. av_assert0(rps_poc[i] != hpic->pic_order_cnt);
  797. if (rps_poc[i] > hpic->pic_order_cnt)
  798. break;
  799. }
  800. rps->num_negative_pics = i;
  801. poc = hpic->pic_order_cnt;
  802. for (j = i - 1; j >= 0; j--) {
  803. rps->delta_poc_s0_minus1[i - 1 - j] = poc - rps_poc[j] - 1;
  804. rps->used_by_curr_pic_s0_flag[i - 1 - j] = rps_used[j];
  805. poc = rps_poc[j];
  806. }
  807. rps->num_positive_pics = rps_pics - i;
  808. poc = hpic->pic_order_cnt;
  809. for (j = i; j < rps_pics; j++) {
  810. rps->delta_poc_s1_minus1[j - i] = rps_poc[j] - poc - 1;
  811. rps->used_by_curr_pic_s1_flag[j - i] = rps_used[j];
  812. poc = rps_poc[j];
  813. }
  814. sh->num_long_term_sps = 0;
  815. sh->num_long_term_pics = 0;
  816. sh->slice_temporal_mvp_enabled_flag =
  817. sps->sps_temporal_mvp_enabled_flag;
  818. if (sh->slice_temporal_mvp_enabled_flag) {
  819. sh->collocated_from_l0_flag = sh->slice_type == HEVC_SLICE_B;
  820. sh->collocated_ref_idx = 0;
  821. }
  822. sh->num_ref_idx_active_override_flag = 0;
  823. sh->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
  824. sh->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
  825. }
  826. sh->slice_sao_luma_flag = sh->slice_sao_chroma_flag =
  827. sps->sample_adaptive_offset_enabled_flag;
  828. if (pic->type == PICTURE_TYPE_B)
  829. sh->slice_qp_delta = priv->fixed_qp_b - (pps->init_qp_minus26 + 26);
  830. else if (pic->type == PICTURE_TYPE_P)
  831. sh->slice_qp_delta = priv->fixed_qp_p - (pps->init_qp_minus26 + 26);
  832. else
  833. sh->slice_qp_delta = priv->fixed_qp_idr - (pps->init_qp_minus26 + 26);
  834. *vslice = (VAEncSliceParameterBufferHEVC) {
  835. .slice_segment_address = sh->slice_segment_address,
  836. .num_ctu_in_slice = slice->block_size,
  837. .slice_type = sh->slice_type,
  838. .slice_pic_parameter_set_id = sh->slice_pic_parameter_set_id,
  839. .num_ref_idx_l0_active_minus1 = sh->num_ref_idx_l0_active_minus1,
  840. .num_ref_idx_l1_active_minus1 = sh->num_ref_idx_l1_active_minus1,
  841. .luma_log2_weight_denom = sh->luma_log2_weight_denom,
  842. .delta_chroma_log2_weight_denom = sh->delta_chroma_log2_weight_denom,
  843. .max_num_merge_cand = 5 - sh->five_minus_max_num_merge_cand,
  844. .slice_qp_delta = sh->slice_qp_delta,
  845. .slice_cb_qp_offset = sh->slice_cb_qp_offset,
  846. .slice_cr_qp_offset = sh->slice_cr_qp_offset,
  847. .slice_beta_offset_div2 = sh->slice_beta_offset_div2,
  848. .slice_tc_offset_div2 = sh->slice_tc_offset_div2,
  849. .slice_fields.bits = {
  850. .last_slice_of_pic_flag = slice->index == pic->nb_slices - 1,
  851. .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
  852. .colour_plane_id = sh->colour_plane_id,
  853. .slice_temporal_mvp_enabled_flag =
  854. sh->slice_temporal_mvp_enabled_flag,
  855. .slice_sao_luma_flag = sh->slice_sao_luma_flag,
  856. .slice_sao_chroma_flag = sh->slice_sao_chroma_flag,
  857. .num_ref_idx_active_override_flag =
  858. sh->num_ref_idx_active_override_flag,
  859. .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
  860. .cabac_init_flag = sh->cabac_init_flag,
  861. .slice_deblocking_filter_disabled_flag =
  862. sh->slice_deblocking_filter_disabled_flag,
  863. .slice_loop_filter_across_slices_enabled_flag =
  864. sh->slice_loop_filter_across_slices_enabled_flag,
  865. .collocated_from_l0_flag = sh->collocated_from_l0_flag,
  866. },
  867. };
  868. for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
  869. vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
  870. vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
  871. vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
  872. vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
  873. }
  874. av_assert0(pic->nb_refs <= 2);
  875. if (pic->nb_refs >= 1) {
  876. // Backward reference for P- or B-frame.
  877. av_assert0(pic->type == PICTURE_TYPE_P ||
  878. pic->type == PICTURE_TYPE_B);
  879. vslice->ref_pic_list0[0] = vpic->reference_frames[0];
  880. }
  881. if (pic->nb_refs >= 2) {
  882. // Forward reference for B-frame.
  883. av_assert0(pic->type == PICTURE_TYPE_B);
  884. vslice->ref_pic_list1[0] = vpic->reference_frames[1];
  885. }
  886. return 0;
  887. }
  888. static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
  889. {
  890. VAAPIEncodeContext *ctx = avctx->priv_data;
  891. VAAPIEncodeH265Context *priv = avctx->priv_data;
  892. int err;
  893. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
  894. if (err < 0)
  895. return err;
  896. if (ctx->va_rc_mode == VA_RC_CQP) {
  897. // Note that VAAPI only supports positive QP values - the range is
  898. // therefore always bounded below by 1, even in 10-bit mode where
  899. // it should go down to -12.
  900. priv->fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
  901. if (avctx->i_quant_factor > 0.0)
  902. priv->fixed_qp_idr =
  903. av_clip((avctx->i_quant_factor * priv->fixed_qp_p +
  904. avctx->i_quant_offset) + 0.5, 1, 51);
  905. else
  906. priv->fixed_qp_idr = priv->fixed_qp_p;
  907. if (avctx->b_quant_factor > 0.0)
  908. priv->fixed_qp_b =
  909. av_clip((avctx->b_quant_factor * priv->fixed_qp_p +
  910. avctx->b_quant_offset) + 0.5, 1, 51);
  911. else
  912. priv->fixed_qp_b = priv->fixed_qp_p;
  913. av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
  914. "%d / %d / %d for IDR- / P- / B-frames.\n",
  915. priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
  916. } else {
  917. // These still need to be set for init_qp/slice_qp_delta.
  918. priv->fixed_qp_idr = 30;
  919. priv->fixed_qp_p = 30;
  920. priv->fixed_qp_b = 30;
  921. }
  922. ctx->roi_quant_range = 51 + 6 * (ctx->profile->depth - 8);
  923. return 0;
  924. }
  925. static const VAAPIEncodeProfile vaapi_encode_h265_profiles[] = {
  926. { FF_PROFILE_HEVC_MAIN, 8, 3, 1, 1, VAProfileHEVCMain },
  927. { FF_PROFILE_HEVC_REXT, 8, 3, 1, 1, VAProfileHEVCMain },
  928. #if VA_CHECK_VERSION(0, 37, 0)
  929. { FF_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, VAProfileHEVCMain10 },
  930. { FF_PROFILE_HEVC_REXT, 10, 3, 1, 1, VAProfileHEVCMain10 },
  931. #endif
  932. { FF_PROFILE_UNKNOWN }
  933. };
  934. static const VAAPIEncodeType vaapi_encode_type_h265 = {
  935. .profiles = vaapi_encode_h265_profiles,
  936. .flags = FLAG_SLICE_CONTROL |
  937. FLAG_B_PICTURES |
  938. FLAG_B_PICTURE_REFERENCES |
  939. FLAG_NON_IDR_KEY_PICTURES,
  940. .default_quality = 25,
  941. .configure = &vaapi_encode_h265_configure,
  942. .picture_priv_data_size = sizeof(VAAPIEncodeH265Picture),
  943. .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
  944. .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
  945. .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
  946. .init_picture_params = &vaapi_encode_h265_init_picture_params,
  947. .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
  948. .init_slice_params = &vaapi_encode_h265_init_slice_params,
  949. .sequence_header_type = VAEncPackedHeaderSequence,
  950. .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
  951. .slice_header_type = VAEncPackedHeaderHEVC_Slice,
  952. .write_slice_header = &vaapi_encode_h265_write_slice_header,
  953. .write_extra_header = &vaapi_encode_h265_write_extra_header,
  954. };
  955. static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
  956. {
  957. VAAPIEncodeContext *ctx = avctx->priv_data;
  958. VAAPIEncodeH265Context *priv = avctx->priv_data;
  959. ctx->codec = &vaapi_encode_type_h265;
  960. if (avctx->profile == FF_PROFILE_UNKNOWN)
  961. avctx->profile = priv->profile;
  962. if (avctx->level == FF_LEVEL_UNKNOWN)
  963. avctx->level = priv->level;
  964. if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
  965. av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
  966. "in 8-bit unsigned integer.\n", avctx->level);
  967. return AVERROR(EINVAL);
  968. }
  969. ctx->desired_packed_headers =
  970. VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
  971. VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
  972. VA_ENC_PACKED_HEADER_MISC; // SEI
  973. ctx->surface_width = FFALIGN(avctx->width, 16);
  974. ctx->surface_height = FFALIGN(avctx->height, 16);
  975. // CTU size is currently hard-coded to 32.
  976. ctx->slice_block_width = ctx->slice_block_height = 32;
  977. if (priv->qp > 0)
  978. ctx->explicit_qp = priv->qp;
  979. return ff_vaapi_encode_init(avctx);
  980. }
  981. static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
  982. {
  983. VAAPIEncodeH265Context *priv = avctx->priv_data;
  984. ff_cbs_fragment_free(priv->cbc, &priv->current_access_unit);
  985. ff_cbs_close(&priv->cbc);
  986. return ff_vaapi_encode_close(avctx);
  987. }
  988. #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
  989. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  990. static const AVOption vaapi_encode_h265_options[] = {
  991. VAAPI_ENCODE_COMMON_OPTIONS,
  992. VAAPI_ENCODE_RC_OPTIONS,
  993. { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
  994. OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
  995. { "aud", "Include AUD",
  996. OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  997. { "profile", "Set profile (general_profile_idc)",
  998. OFFSET(profile), AV_OPT_TYPE_INT,
  999. { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
  1000. #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  1001. { .i64 = value }, 0, 0, FLAGS, "profile"
  1002. { PROFILE("main", FF_PROFILE_HEVC_MAIN) },
  1003. { PROFILE("main10", FF_PROFILE_HEVC_MAIN_10) },
  1004. { PROFILE("rext", FF_PROFILE_HEVC_REXT) },
  1005. #undef PROFILE
  1006. { "tier", "Set tier (general_tier_flag)",
  1007. OFFSET(tier), AV_OPT_TYPE_INT,
  1008. { .i64 = 0 }, 0, 1, FLAGS, "tier" },
  1009. { "main", NULL, 0, AV_OPT_TYPE_CONST,
  1010. { .i64 = 0 }, 0, 0, FLAGS, "tier" },
  1011. { "high", NULL, 0, AV_OPT_TYPE_CONST,
  1012. { .i64 = 1 }, 0, 0, FLAGS, "tier" },
  1013. { "level", "Set level (general_level_idc)",
  1014. OFFSET(level), AV_OPT_TYPE_INT,
  1015. { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
  1016. #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  1017. { .i64 = value }, 0, 0, FLAGS, "level"
  1018. { LEVEL("1", 30) },
  1019. { LEVEL("2", 60) },
  1020. { LEVEL("2.1", 63) },
  1021. { LEVEL("3", 90) },
  1022. { LEVEL("3.1", 93) },
  1023. { LEVEL("4", 120) },
  1024. { LEVEL("4.1", 123) },
  1025. { LEVEL("5", 150) },
  1026. { LEVEL("5.1", 153) },
  1027. { LEVEL("5.2", 156) },
  1028. { LEVEL("6", 180) },
  1029. { LEVEL("6.1", 183) },
  1030. { LEVEL("6.2", 186) },
  1031. #undef LEVEL
  1032. { "sei", "Set SEI to include",
  1033. OFFSET(sei), AV_OPT_TYPE_FLAGS,
  1034. { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
  1035. 0, INT_MAX, FLAGS, "sei" },
  1036. { "hdr",
  1037. "Include HDR metadata for mastering display colour volume "
  1038. "and content light level information",
  1039. 0, AV_OPT_TYPE_CONST,
  1040. { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
  1041. INT_MIN, INT_MAX, FLAGS, "sei" },
  1042. { NULL },
  1043. };
  1044. static const AVCodecDefault vaapi_encode_h265_defaults[] = {
  1045. { "b", "0" },
  1046. { "bf", "2" },
  1047. { "g", "120" },
  1048. { "i_qfactor", "1" },
  1049. { "i_qoffset", "0" },
  1050. { "b_qfactor", "6/5" },
  1051. { "b_qoffset", "0" },
  1052. { "qmin", "-1" },
  1053. { "qmax", "-1" },
  1054. { NULL },
  1055. };
  1056. static const AVClass vaapi_encode_h265_class = {
  1057. .class_name = "h265_vaapi",
  1058. .item_name = av_default_item_name,
  1059. .option = vaapi_encode_h265_options,
  1060. .version = LIBAVUTIL_VERSION_INT,
  1061. };
  1062. AVCodec ff_hevc_vaapi_encoder = {
  1063. .name = "hevc_vaapi",
  1064. .long_name = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
  1065. .type = AVMEDIA_TYPE_VIDEO,
  1066. .id = AV_CODEC_ID_HEVC,
  1067. .priv_data_size = sizeof(VAAPIEncodeH265Context),
  1068. .init = &vaapi_encode_h265_init,
  1069. .send_frame = &ff_vaapi_encode_send_frame,
  1070. .receive_packet = &ff_vaapi_encode_receive_packet,
  1071. .close = &vaapi_encode_h265_close,
  1072. .priv_class = &vaapi_encode_h265_class,
  1073. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  1074. .defaults = vaapi_encode_h265_defaults,
  1075. .pix_fmts = (const enum AVPixelFormat[]) {
  1076. AV_PIX_FMT_VAAPI,
  1077. AV_PIX_FMT_NONE,
  1078. },
  1079. .wrapper_name = "vaapi",
  1080. };