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.

1254 lines
45KB

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