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.

1221 lines
44KB

  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 "hevc.h"
  30. #include "hevc_sei.h"
  31. #include "internal.h"
  32. #include "put_bits.h"
  33. #include "vaapi_encode.h"
  34. enum {
  35. SEI_MASTERING_DISPLAY = 0x08,
  36. SEI_CONTENT_LIGHT_LEVEL = 0x10,
  37. };
  38. typedef struct VAAPIEncodeH265Context {
  39. VAAPIEncodeContext common;
  40. // User options.
  41. int qp;
  42. int aud;
  43. int profile;
  44. int level;
  45. int sei;
  46. // Derived settings.
  47. unsigned int ctu_width;
  48. unsigned int ctu_height;
  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. }
  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 = 0;
  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. ptl->general_level_idc = avctx->level;
  279. vps->vps_sub_layer_ordering_info_present_flag = 0;
  280. vps->vps_max_dec_pic_buffering_minus1[0] = (ctx->b_per_p > 0) + 1;
  281. vps->vps_max_num_reorder_pics[0] = (ctx->b_per_p > 0);
  282. vps->vps_max_latency_increase_plus1[0] = 0;
  283. vps->vps_max_layer_id = 0;
  284. vps->vps_num_layer_sets_minus1 = 0;
  285. vps->layer_id_included_flag[0][0] = 1;
  286. vps->vps_timing_info_present_flag = 1;
  287. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  288. vps->vps_num_units_in_tick = avctx->framerate.den;
  289. vps->vps_time_scale = avctx->framerate.num;
  290. vps->vps_poc_proportional_to_timing_flag = 1;
  291. vps->vps_num_ticks_poc_diff_one_minus1 = 0;
  292. } else {
  293. vps->vps_num_units_in_tick = avctx->time_base.num;
  294. vps->vps_time_scale = avctx->time_base.den;
  295. vps->vps_poc_proportional_to_timing_flag = 0;
  296. }
  297. vps->vps_num_hrd_parameters = 0;
  298. // SPS
  299. sps->nal_unit_header = (H265RawNALUnitHeader) {
  300. .nal_unit_type = HEVC_NAL_SPS,
  301. .nuh_layer_id = 0,
  302. .nuh_temporal_id_plus1 = 1,
  303. };
  304. sps->sps_video_parameter_set_id = vps->vps_video_parameter_set_id;
  305. sps->sps_max_sub_layers_minus1 = vps->vps_max_sub_layers_minus1;
  306. sps->sps_temporal_id_nesting_flag = vps->vps_temporal_id_nesting_flag;
  307. sps->profile_tier_level = vps->profile_tier_level;
  308. sps->sps_seq_parameter_set_id = 0;
  309. sps->chroma_format_idc = chroma_format;
  310. sps->separate_colour_plane_flag = 0;
  311. sps->pic_width_in_luma_samples = ctx->surface_width;
  312. sps->pic_height_in_luma_samples = ctx->surface_height;
  313. if (avctx->width != ctx->surface_width ||
  314. avctx->height != ctx->surface_height) {
  315. sps->conformance_window_flag = 1;
  316. sps->conf_win_left_offset = 0;
  317. sps->conf_win_right_offset =
  318. (ctx->surface_width - avctx->width) / 2;
  319. sps->conf_win_top_offset = 0;
  320. sps->conf_win_bottom_offset =
  321. (ctx->surface_height - avctx->height) / 2;
  322. } else {
  323. sps->conformance_window_flag = 0;
  324. }
  325. sps->bit_depth_luma_minus8 = bit_depth - 8;
  326. sps->bit_depth_chroma_minus8 = bit_depth - 8;
  327. sps->log2_max_pic_order_cnt_lsb_minus4 = 8;
  328. sps->sps_sub_layer_ordering_info_present_flag =
  329. vps->vps_sub_layer_ordering_info_present_flag;
  330. for (i = 0; i <= sps->sps_max_sub_layers_minus1; i++) {
  331. sps->sps_max_dec_pic_buffering_minus1[i] =
  332. vps->vps_max_dec_pic_buffering_minus1[i];
  333. sps->sps_max_num_reorder_pics[i] =
  334. vps->vps_max_num_reorder_pics[i];
  335. sps->sps_max_latency_increase_plus1[i] =
  336. vps->vps_max_latency_increase_plus1[i];
  337. }
  338. // These have to come from the capabilities of the encoder. We have no
  339. // way to query them, so just hardcode parameters which work on the Intel
  340. // driver.
  341. // CTB size from 8x8 to 32x32.
  342. sps->log2_min_luma_coding_block_size_minus3 = 0;
  343. sps->log2_diff_max_min_luma_coding_block_size = 2;
  344. // Transform size from 4x4 to 32x32.
  345. sps->log2_min_luma_transform_block_size_minus2 = 0;
  346. sps->log2_diff_max_min_luma_transform_block_size = 3;
  347. // Full transform hierarchy allowed (2-5).
  348. sps->max_transform_hierarchy_depth_inter = 3;
  349. sps->max_transform_hierarchy_depth_intra = 3;
  350. // AMP works.
  351. sps->amp_enabled_flag = 1;
  352. // SAO and temporal MVP do not work.
  353. sps->sample_adaptive_offset_enabled_flag = 0;
  354. sps->sps_temporal_mvp_enabled_flag = 0;
  355. sps->pcm_enabled_flag = 0;
  356. // STRPSs should ideally be here rather than defined individually in
  357. // each slice, but the structure isn't completely fixed so for now
  358. // don't bother.
  359. sps->num_short_term_ref_pic_sets = 0;
  360. sps->long_term_ref_pics_present_flag = 0;
  361. sps->vui_parameters_present_flag = 1;
  362. if (avctx->sample_aspect_ratio.num != 0 &&
  363. avctx->sample_aspect_ratio.den != 0) {
  364. static const AVRational sar_idc[] = {
  365. { 0, 0 },
  366. { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
  367. { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
  368. { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
  369. { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
  370. };
  371. int i;
  372. for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
  373. if (avctx->sample_aspect_ratio.num == sar_idc[i].num &&
  374. avctx->sample_aspect_ratio.den == sar_idc[i].den) {
  375. vui->aspect_ratio_idc = i;
  376. break;
  377. }
  378. }
  379. if (i >= FF_ARRAY_ELEMS(sar_idc)) {
  380. vui->aspect_ratio_idc = 255;
  381. vui->sar_width = avctx->sample_aspect_ratio.num;
  382. vui->sar_height = avctx->sample_aspect_ratio.den;
  383. }
  384. vui->aspect_ratio_info_present_flag = 1;
  385. }
  386. if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
  387. avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  388. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  389. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  390. vui->video_signal_type_present_flag = 1;
  391. vui->video_format = 5; // Unspecified.
  392. vui->video_full_range_flag =
  393. avctx->color_range == AVCOL_RANGE_JPEG;
  394. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  395. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  396. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  397. vui->colour_description_present_flag = 1;
  398. vui->colour_primaries = avctx->color_primaries;
  399. vui->transfer_characteristics = avctx->color_trc;
  400. vui->matrix_coefficients = avctx->colorspace;
  401. }
  402. } else {
  403. vui->video_format = 5;
  404. vui->video_full_range_flag = 0;
  405. vui->colour_primaries = avctx->color_primaries;
  406. vui->transfer_characteristics = avctx->color_trc;
  407. vui->matrix_coefficients = avctx->colorspace;
  408. }
  409. if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
  410. vui->chroma_loc_info_present_flag = 1;
  411. vui->chroma_sample_loc_type_top_field =
  412. vui->chroma_sample_loc_type_bottom_field =
  413. avctx->chroma_sample_location - 1;
  414. }
  415. vui->vui_timing_info_present_flag = 1;
  416. vui->vui_num_units_in_tick = vps->vps_num_units_in_tick;
  417. vui->vui_time_scale = vps->vps_time_scale;
  418. vui->vui_poc_proportional_to_timing_flag = vps->vps_poc_proportional_to_timing_flag;
  419. vui->vui_num_ticks_poc_diff_one_minus1 = vps->vps_num_ticks_poc_diff_one_minus1;
  420. vui->vui_hrd_parameters_present_flag = 0;
  421. vui->bitstream_restriction_flag = 1;
  422. vui->motion_vectors_over_pic_boundaries_flag = 1;
  423. vui->restricted_ref_pic_lists_flag = 1;
  424. vui->max_bytes_per_pic_denom = 0;
  425. vui->max_bits_per_min_cu_denom = 0;
  426. vui->log2_max_mv_length_horizontal = 15;
  427. vui->log2_max_mv_length_vertical = 15;
  428. // PPS
  429. pps->nal_unit_header = (H265RawNALUnitHeader) {
  430. .nal_unit_type = HEVC_NAL_PPS,
  431. .nuh_layer_id = 0,
  432. .nuh_temporal_id_plus1 = 1,
  433. };
  434. pps->pps_pic_parameter_set_id = 0;
  435. pps->pps_seq_parameter_set_id = sps->sps_seq_parameter_set_id;
  436. pps->num_ref_idx_l0_default_active_minus1 = 0;
  437. pps->num_ref_idx_l1_default_active_minus1 = 0;
  438. pps->init_qp_minus26 = priv->fixed_qp_idr - 26;
  439. pps->cu_qp_delta_enabled_flag = (ctx->va_rc_mode != VA_RC_CQP);
  440. pps->diff_cu_qp_delta_depth = 0;
  441. pps->pps_loop_filter_across_slices_enabled_flag = 1;
  442. // Fill VAAPI parameter buffers.
  443. *vseq = (VAEncSequenceParameterBufferHEVC) {
  444. .general_profile_idc = vps->profile_tier_level.general_profile_idc,
  445. .general_level_idc = vps->profile_tier_level.general_level_idc,
  446. .general_tier_flag = vps->profile_tier_level.general_tier_flag,
  447. .intra_period = ctx->gop_size,
  448. .intra_idr_period = ctx->gop_size,
  449. .ip_period = ctx->b_per_p + 1,
  450. .bits_per_second = ctx->va_bit_rate,
  451. .pic_width_in_luma_samples = sps->pic_width_in_luma_samples,
  452. .pic_height_in_luma_samples = sps->pic_height_in_luma_samples,
  453. .seq_fields.bits = {
  454. .chroma_format_idc = sps->chroma_format_idc,
  455. .separate_colour_plane_flag = sps->separate_colour_plane_flag,
  456. .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
  457. .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
  458. .scaling_list_enabled_flag = sps->scaling_list_enabled_flag,
  459. .strong_intra_smoothing_enabled_flag =
  460. sps->strong_intra_smoothing_enabled_flag,
  461. .amp_enabled_flag = sps->amp_enabled_flag,
  462. .sample_adaptive_offset_enabled_flag =
  463. sps->sample_adaptive_offset_enabled_flag,
  464. .pcm_enabled_flag = sps->pcm_enabled_flag,
  465. .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled_flag,
  466. .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
  467. },
  468. .log2_min_luma_coding_block_size_minus3 =
  469. sps->log2_min_luma_coding_block_size_minus3,
  470. .log2_diff_max_min_luma_coding_block_size =
  471. sps->log2_diff_max_min_luma_coding_block_size,
  472. .log2_min_transform_block_size_minus2 =
  473. sps->log2_min_luma_transform_block_size_minus2,
  474. .log2_diff_max_min_transform_block_size =
  475. sps->log2_diff_max_min_luma_transform_block_size,
  476. .max_transform_hierarchy_depth_inter =
  477. sps->max_transform_hierarchy_depth_inter,
  478. .max_transform_hierarchy_depth_intra =
  479. sps->max_transform_hierarchy_depth_intra,
  480. .pcm_sample_bit_depth_luma_minus1 =
  481. sps->pcm_sample_bit_depth_luma_minus1,
  482. .pcm_sample_bit_depth_chroma_minus1 =
  483. sps->pcm_sample_bit_depth_chroma_minus1,
  484. .log2_min_pcm_luma_coding_block_size_minus3 =
  485. sps->log2_min_pcm_luma_coding_block_size_minus3,
  486. .log2_max_pcm_luma_coding_block_size_minus3 =
  487. sps->log2_min_pcm_luma_coding_block_size_minus3 +
  488. sps->log2_diff_max_min_pcm_luma_coding_block_size,
  489. .vui_parameters_present_flag = 0,
  490. };
  491. *vpic = (VAEncPictureParameterBufferHEVC) {
  492. .decoded_curr_pic = {
  493. .picture_id = VA_INVALID_ID,
  494. .flags = VA_PICTURE_HEVC_INVALID,
  495. },
  496. .coded_buf = VA_INVALID_ID,
  497. .collocated_ref_pic_index = 0xff,
  498. .last_picture = 0,
  499. .pic_init_qp = pps->init_qp_minus26 + 26,
  500. .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
  501. .pps_cb_qp_offset = pps->pps_cb_qp_offset,
  502. .pps_cr_qp_offset = pps->pps_cr_qp_offset,
  503. .num_tile_columns_minus1 = pps->num_tile_columns_minus1,
  504. .num_tile_rows_minus1 = pps->num_tile_rows_minus1,
  505. .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level_minus2,
  506. .ctu_max_bitsize_allowed = 0,
  507. .num_ref_idx_l0_default_active_minus1 =
  508. pps->num_ref_idx_l0_default_active_minus1,
  509. .num_ref_idx_l1_default_active_minus1 =
  510. pps->num_ref_idx_l1_default_active_minus1,
  511. .slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id,
  512. .pic_fields.bits = {
  513. .sign_data_hiding_enabled_flag = pps->sign_data_hiding_enabled_flag,
  514. .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
  515. .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
  516. .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
  517. .weighted_pred_flag = pps->weighted_pred_flag,
  518. .weighted_bipred_flag = pps->weighted_bipred_flag,
  519. .transquant_bypass_enabled_flag = pps->transquant_bypass_enabled_flag,
  520. .tiles_enabled_flag = pps->tiles_enabled_flag,
  521. .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
  522. .loop_filter_across_tiles_enabled_flag =
  523. pps->loop_filter_across_tiles_enabled_flag,
  524. .scaling_list_data_present_flag = (sps->sps_scaling_list_data_present_flag |
  525. pps->pps_scaling_list_data_present_flag),
  526. .screen_content_flag = 0,
  527. .enable_gpu_weighted_prediction = 0,
  528. .no_output_of_prior_pics_flag = 0,
  529. },
  530. };
  531. return 0;
  532. }
  533. static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
  534. VAAPIEncodePicture *pic)
  535. {
  536. VAAPIEncodeH265Context *priv = avctx->priv_data;
  537. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  538. int i;
  539. if (pic->type == PICTURE_TYPE_IDR) {
  540. av_assert0(pic->display_order == pic->encode_order);
  541. priv->last_idr_frame = pic->display_order;
  542. priv->slice_nal_unit = HEVC_NAL_IDR_W_RADL;
  543. priv->slice_type = HEVC_SLICE_I;
  544. priv->pic_type = 0;
  545. } else {
  546. av_assert0(pic->encode_order > priv->last_idr_frame);
  547. if (pic->type == PICTURE_TYPE_I) {
  548. priv->slice_nal_unit = HEVC_NAL_CRA_NUT;
  549. priv->slice_type = HEVC_SLICE_I;
  550. priv->pic_type = 0;
  551. } else if (pic->type == PICTURE_TYPE_P) {
  552. av_assert0(pic->refs[0]);
  553. priv->slice_nal_unit = HEVC_NAL_TRAIL_R;
  554. priv->slice_type = HEVC_SLICE_P;
  555. priv->pic_type = 1;
  556. } else {
  557. av_assert0(pic->refs[0] && pic->refs[1]);
  558. if (pic->refs[1]->type == PICTURE_TYPE_I)
  559. priv->slice_nal_unit = HEVC_NAL_RASL_N;
  560. else
  561. priv->slice_nal_unit = HEVC_NAL_TRAIL_N;
  562. priv->slice_type = HEVC_SLICE_B;
  563. priv->pic_type = 2;
  564. }
  565. }
  566. priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
  567. if (priv->aud) {
  568. priv->aud_needed = 1;
  569. priv->raw_aud = (H265RawAUD) {
  570. .nal_unit_header = {
  571. .nal_unit_type = HEVC_NAL_AUD,
  572. .nuh_layer_id = 0,
  573. .nuh_temporal_id_plus1 = 1,
  574. },
  575. .pic_type = priv->pic_type,
  576. };
  577. } else {
  578. priv->aud_needed = 0;
  579. }
  580. priv->sei_needed = 0;
  581. // Only look for the metadata on I/IDR frame on the output. We
  582. // may force an IDR frame on the output where the medadata gets
  583. // changed on the input frame.
  584. if ((priv->sei & SEI_MASTERING_DISPLAY) &&
  585. (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
  586. AVFrameSideData *sd =
  587. av_frame_get_side_data(pic->input_image,
  588. AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
  589. if (sd) {
  590. AVMasteringDisplayMetadata *mdm =
  591. (AVMasteringDisplayMetadata *)sd->data;
  592. // SEI is needed when both the primaries and luminance are set
  593. if (mdm->has_primaries && mdm->has_luminance) {
  594. H265RawSEIMasteringDisplayColourVolume *mdcv =
  595. &priv->sei_mastering_display;
  596. const int mapping[3] = {1, 2, 0};
  597. const int chroma_den = 50000;
  598. const int luma_den = 10000;
  599. for (i = 0; i < 3; i++) {
  600. const int j = mapping[i];
  601. mdcv->display_primaries_x[i] =
  602. FFMIN(lrint(chroma_den *
  603. av_q2d(mdm->display_primaries[j][0])),
  604. chroma_den);
  605. mdcv->display_primaries_y[i] =
  606. FFMIN(lrint(chroma_den *
  607. av_q2d(mdm->display_primaries[j][1])),
  608. chroma_den);
  609. }
  610. mdcv->white_point_x =
  611. FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])),
  612. chroma_den);
  613. mdcv->white_point_y =
  614. FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])),
  615. chroma_den);
  616. mdcv->max_display_mastering_luminance =
  617. lrint(luma_den * av_q2d(mdm->max_luminance));
  618. mdcv->min_display_mastering_luminance =
  619. FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)),
  620. mdcv->max_display_mastering_luminance);
  621. priv->sei_needed |= SEI_MASTERING_DISPLAY;
  622. }
  623. }
  624. }
  625. if ((priv->sei & SEI_CONTENT_LIGHT_LEVEL) &&
  626. (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
  627. AVFrameSideData *sd =
  628. av_frame_get_side_data(pic->input_image,
  629. AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
  630. if (sd) {
  631. AVContentLightMetadata *clm =
  632. (AVContentLightMetadata *)sd->data;
  633. H265RawSEIContentLightLevelInfo *clli =
  634. &priv->sei_content_light_level;
  635. clli->max_content_light_level = FFMIN(clm->MaxCLL, 65535);
  636. clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535);
  637. priv->sei_needed |= SEI_CONTENT_LIGHT_LEVEL;
  638. }
  639. }
  640. vpic->decoded_curr_pic = (VAPictureHEVC) {
  641. .picture_id = pic->recon_surface,
  642. .pic_order_cnt = priv->pic_order_cnt,
  643. .flags = 0,
  644. };
  645. for (i = 0; i < pic->nb_refs; i++) {
  646. VAAPIEncodePicture *ref = pic->refs[i];
  647. av_assert0(ref && ref->encode_order < pic->encode_order);
  648. vpic->reference_frames[i] = (VAPictureHEVC) {
  649. .picture_id = ref->recon_surface,
  650. .pic_order_cnt = ref->display_order - priv->last_idr_frame,
  651. .flags = (ref->display_order < pic->display_order ?
  652. VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
  653. (ref->display_order > pic->display_order ?
  654. VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
  655. };
  656. }
  657. for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
  658. vpic->reference_frames[i] = (VAPictureHEVC) {
  659. .picture_id = VA_INVALID_ID,
  660. .flags = VA_PICTURE_HEVC_INVALID,
  661. };
  662. }
  663. vpic->coded_buf = pic->output_buffer;
  664. vpic->nal_unit_type = priv->slice_nal_unit;
  665. switch (pic->type) {
  666. case PICTURE_TYPE_IDR:
  667. vpic->pic_fields.bits.idr_pic_flag = 1;
  668. vpic->pic_fields.bits.coding_type = 1;
  669. vpic->pic_fields.bits.reference_pic_flag = 1;
  670. break;
  671. case PICTURE_TYPE_I:
  672. vpic->pic_fields.bits.idr_pic_flag = 0;
  673. vpic->pic_fields.bits.coding_type = 1;
  674. vpic->pic_fields.bits.reference_pic_flag = 1;
  675. break;
  676. case PICTURE_TYPE_P:
  677. vpic->pic_fields.bits.idr_pic_flag = 0;
  678. vpic->pic_fields.bits.coding_type = 2;
  679. vpic->pic_fields.bits.reference_pic_flag = 1;
  680. break;
  681. case PICTURE_TYPE_B:
  682. vpic->pic_fields.bits.idr_pic_flag = 0;
  683. vpic->pic_fields.bits.coding_type = 3;
  684. vpic->pic_fields.bits.reference_pic_flag = 0;
  685. break;
  686. default:
  687. av_assert0(0 && "invalid picture type");
  688. }
  689. pic->nb_slices = 1;
  690. return 0;
  691. }
  692. static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
  693. VAAPIEncodePicture *pic,
  694. VAAPIEncodeSlice *slice)
  695. {
  696. VAAPIEncodeContext *ctx = avctx->priv_data;
  697. VAAPIEncodeH265Context *priv = avctx->priv_data;
  698. const H265RawSPS *sps = &priv->raw_sps;
  699. const H265RawPPS *pps = &priv->raw_pps;
  700. H265RawSliceHeader *sh = &priv->raw_slice.header;
  701. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  702. VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
  703. int i;
  704. sh->nal_unit_header = (H265RawNALUnitHeader) {
  705. .nal_unit_type = priv->slice_nal_unit,
  706. .nuh_layer_id = 0,
  707. .nuh_temporal_id_plus1 = 1,
  708. };
  709. sh->slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id;
  710. // Currently we only support one slice per frame.
  711. sh->first_slice_segment_in_pic_flag = 1;
  712. sh->slice_segment_address = 0;
  713. sh->slice_type = priv->slice_type;
  714. sh->slice_pic_order_cnt_lsb = priv->pic_order_cnt &
  715. (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
  716. if (pic->type != PICTURE_TYPE_IDR) {
  717. H265RawSTRefPicSet *rps;
  718. VAAPIEncodePicture *st;
  719. int used;
  720. sh->short_term_ref_pic_set_sps_flag = 0;
  721. rps = &sh->short_term_ref_pic_set;
  722. memset(rps, 0, sizeof(*rps));
  723. for (st = ctx->pic_start; st; st = st->next) {
  724. if (st->encode_order >= pic->encode_order) {
  725. // Not yet in DPB.
  726. continue;
  727. }
  728. used = 0;
  729. for (i = 0; i < pic->nb_refs; i++) {
  730. if (pic->refs[i] == st)
  731. used = 1;
  732. }
  733. if (!used) {
  734. // Usually each picture always uses all of the others in the
  735. // DPB as references. The one case we have to treat here is
  736. // a non-IDR IRAP picture, which may need to hold unused
  737. // references across itself to be used for the decoding of
  738. // following RASL pictures. This looks for such an RASL
  739. // picture, and keeps the reference if there is one.
  740. VAAPIEncodePicture *rp;
  741. for (rp = ctx->pic_start; rp; rp = rp->next) {
  742. if (rp->encode_order < pic->encode_order)
  743. continue;
  744. if (rp->type != PICTURE_TYPE_B)
  745. continue;
  746. if (rp->refs[0] == st && rp->refs[1] == pic)
  747. break;
  748. }
  749. if (!rp)
  750. continue;
  751. }
  752. // This only works for one instance of each (delta_poc_sN_minus1
  753. // is relative to the previous frame in the list, not relative to
  754. // the current frame directly).
  755. if (st->display_order < pic->display_order) {
  756. rps->delta_poc_s0_minus1[rps->num_negative_pics] =
  757. pic->display_order - st->display_order - 1;
  758. rps->used_by_curr_pic_s0_flag[rps->num_negative_pics] = used;
  759. ++rps->num_negative_pics;
  760. } else {
  761. rps->delta_poc_s1_minus1[rps->num_positive_pics] =
  762. st->display_order - pic->display_order - 1;
  763. rps->used_by_curr_pic_s1_flag[rps->num_positive_pics] = used;
  764. ++rps->num_positive_pics;
  765. }
  766. }
  767. sh->num_long_term_sps = 0;
  768. sh->num_long_term_pics = 0;
  769. sh->slice_temporal_mvp_enabled_flag =
  770. sps->sps_temporal_mvp_enabled_flag;
  771. if (sh->slice_temporal_mvp_enabled_flag) {
  772. sh->collocated_from_l0_flag = sh->slice_type == HEVC_SLICE_B;
  773. sh->collocated_ref_idx = 0;
  774. }
  775. sh->num_ref_idx_active_override_flag = 0;
  776. sh->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
  777. sh->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
  778. }
  779. sh->slice_sao_luma_flag = sh->slice_sao_chroma_flag =
  780. sps->sample_adaptive_offset_enabled_flag;
  781. if (pic->type == PICTURE_TYPE_B)
  782. sh->slice_qp_delta = priv->fixed_qp_b - (pps->init_qp_minus26 + 26);
  783. else if (pic->type == PICTURE_TYPE_P)
  784. sh->slice_qp_delta = priv->fixed_qp_p - (pps->init_qp_minus26 + 26);
  785. else
  786. sh->slice_qp_delta = priv->fixed_qp_idr - (pps->init_qp_minus26 + 26);
  787. *vslice = (VAEncSliceParameterBufferHEVC) {
  788. .slice_segment_address = sh->slice_segment_address,
  789. .num_ctu_in_slice = priv->ctu_width * priv->ctu_height,
  790. .slice_type = sh->slice_type,
  791. .slice_pic_parameter_set_id = sh->slice_pic_parameter_set_id,
  792. .num_ref_idx_l0_active_minus1 = sh->num_ref_idx_l0_active_minus1,
  793. .num_ref_idx_l1_active_minus1 = sh->num_ref_idx_l1_active_minus1,
  794. .luma_log2_weight_denom = sh->luma_log2_weight_denom,
  795. .delta_chroma_log2_weight_denom = sh->delta_chroma_log2_weight_denom,
  796. .max_num_merge_cand = 5 - sh->five_minus_max_num_merge_cand,
  797. .slice_qp_delta = sh->slice_qp_delta,
  798. .slice_cb_qp_offset = sh->slice_cb_qp_offset,
  799. .slice_cr_qp_offset = sh->slice_cr_qp_offset,
  800. .slice_beta_offset_div2 = sh->slice_beta_offset_div2,
  801. .slice_tc_offset_div2 = sh->slice_tc_offset_div2,
  802. .slice_fields.bits = {
  803. .last_slice_of_pic_flag = 1,
  804. .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
  805. .colour_plane_id = sh->colour_plane_id,
  806. .slice_temporal_mvp_enabled_flag =
  807. sh->slice_temporal_mvp_enabled_flag,
  808. .slice_sao_luma_flag = sh->slice_sao_luma_flag,
  809. .slice_sao_chroma_flag = sh->slice_sao_chroma_flag,
  810. .num_ref_idx_active_override_flag =
  811. sh->num_ref_idx_active_override_flag,
  812. .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
  813. .cabac_init_flag = sh->cabac_init_flag,
  814. .slice_deblocking_filter_disabled_flag =
  815. sh->slice_deblocking_filter_disabled_flag,
  816. .slice_loop_filter_across_slices_enabled_flag =
  817. sh->slice_loop_filter_across_slices_enabled_flag,
  818. .collocated_from_l0_flag = sh->collocated_from_l0_flag,
  819. },
  820. };
  821. for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
  822. vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
  823. vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
  824. vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
  825. vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
  826. }
  827. av_assert0(pic->nb_refs <= 2);
  828. if (pic->nb_refs >= 1) {
  829. // Backward reference for P- or B-frame.
  830. av_assert0(pic->type == PICTURE_TYPE_P ||
  831. pic->type == PICTURE_TYPE_B);
  832. vslice->ref_pic_list0[0] = vpic->reference_frames[0];
  833. }
  834. if (pic->nb_refs >= 2) {
  835. // Forward reference for B-frame.
  836. av_assert0(pic->type == PICTURE_TYPE_B);
  837. vslice->ref_pic_list1[0] = vpic->reference_frames[1];
  838. }
  839. return 0;
  840. }
  841. static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
  842. {
  843. VAAPIEncodeContext *ctx = avctx->priv_data;
  844. VAAPIEncodeH265Context *priv = avctx->priv_data;
  845. int err;
  846. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
  847. if (err < 0)
  848. return err;
  849. priv->ctu_width = FFALIGN(ctx->surface_width, 32) / 32;
  850. priv->ctu_height = FFALIGN(ctx->surface_height, 32) / 32;
  851. av_log(avctx, AV_LOG_VERBOSE, "Input %ux%u -> Surface %ux%u -> CTU %ux%u.\n",
  852. avctx->width, avctx->height, ctx->surface_width,
  853. ctx->surface_height, priv->ctu_width, priv->ctu_height);
  854. if (ctx->va_rc_mode == VA_RC_CQP) {
  855. priv->fixed_qp_p = priv->qp;
  856. if (avctx->i_quant_factor > 0.0)
  857. priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
  858. avctx->i_quant_offset) + 0.5);
  859. else
  860. priv->fixed_qp_idr = priv->fixed_qp_p;
  861. if (avctx->b_quant_factor > 0.0)
  862. priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
  863. avctx->b_quant_offset) + 0.5);
  864. else
  865. priv->fixed_qp_b = priv->fixed_qp_p;
  866. av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
  867. "%d / %d / %d for IDR- / P- / B-frames.\n",
  868. priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
  869. } else if (ctx->va_rc_mode == VA_RC_CBR ||
  870. ctx->va_rc_mode == VA_RC_VBR) {
  871. // These still need to be set for pic_init_qp/slice_qp_delta.
  872. priv->fixed_qp_idr = 30;
  873. priv->fixed_qp_p = 30;
  874. priv->fixed_qp_b = 30;
  875. } else {
  876. av_assert0(0 && "Invalid RC mode.");
  877. }
  878. return 0;
  879. }
  880. static const VAAPIEncodeProfile vaapi_encode_h265_profiles[] = {
  881. { FF_PROFILE_HEVC_MAIN, 8, 3, 1, 1, VAProfileHEVCMain },
  882. { FF_PROFILE_HEVC_REXT, 8, 3, 1, 1, VAProfileHEVCMain },
  883. #if VA_CHECK_VERSION(0, 37, 0)
  884. { FF_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, VAProfileHEVCMain10 },
  885. { FF_PROFILE_HEVC_REXT, 10, 3, 1, 1, VAProfileHEVCMain10 },
  886. #endif
  887. { FF_PROFILE_UNKNOWN }
  888. };
  889. static const VAAPIEncodeType vaapi_encode_type_h265 = {
  890. .profiles = vaapi_encode_h265_profiles,
  891. .configure = &vaapi_encode_h265_configure,
  892. .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
  893. .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
  894. .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
  895. .init_picture_params = &vaapi_encode_h265_init_picture_params,
  896. .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
  897. .init_slice_params = &vaapi_encode_h265_init_slice_params,
  898. .sequence_header_type = VAEncPackedHeaderSequence,
  899. .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
  900. .slice_header_type = VAEncPackedHeaderHEVC_Slice,
  901. .write_slice_header = &vaapi_encode_h265_write_slice_header,
  902. .write_extra_header = &vaapi_encode_h265_write_extra_header,
  903. };
  904. static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
  905. {
  906. VAAPIEncodeContext *ctx = avctx->priv_data;
  907. VAAPIEncodeH265Context *priv = avctx->priv_data;
  908. ctx->codec = &vaapi_encode_type_h265;
  909. if (avctx->profile == FF_PROFILE_UNKNOWN)
  910. avctx->profile = priv->profile;
  911. if (avctx->level == FF_LEVEL_UNKNOWN)
  912. avctx->level = priv->level;
  913. ctx->desired_packed_headers =
  914. VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
  915. VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
  916. VA_ENC_PACKED_HEADER_MISC; // SEI
  917. ctx->surface_width = FFALIGN(avctx->width, 16);
  918. ctx->surface_height = FFALIGN(avctx->height, 16);
  919. return ff_vaapi_encode_init(avctx);
  920. }
  921. static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
  922. {
  923. VAAPIEncodeH265Context *priv = avctx->priv_data;
  924. ff_cbs_close(&priv->cbc);
  925. return ff_vaapi_encode_close(avctx);
  926. }
  927. #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
  928. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  929. static const AVOption vaapi_encode_h265_options[] = {
  930. VAAPI_ENCODE_COMMON_OPTIONS,
  931. { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
  932. OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, 52, FLAGS },
  933. { "aud", "Include AUD",
  934. OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  935. { "profile", "Set profile (general_profile_idc)",
  936. OFFSET(profile), AV_OPT_TYPE_INT,
  937. { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
  938. #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  939. { .i64 = value }, 0, 0, FLAGS, "profile"
  940. { PROFILE("main", FF_PROFILE_HEVC_MAIN) },
  941. { PROFILE("main10", FF_PROFILE_HEVC_MAIN_10) },
  942. { PROFILE("rext", FF_PROFILE_HEVC_REXT) },
  943. #undef PROFILE
  944. { "level", "Set level (general_level_idc)",
  945. OFFSET(level), AV_OPT_TYPE_INT,
  946. { .i64 = 153 }, 0x00, 0xff, FLAGS, "level" },
  947. #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  948. { .i64 = value }, 0, 0, FLAGS, "level"
  949. { LEVEL("1", 30) },
  950. { LEVEL("2", 60) },
  951. { LEVEL("2.1", 63) },
  952. { LEVEL("3", 90) },
  953. { LEVEL("3.1", 93) },
  954. { LEVEL("4", 120) },
  955. { LEVEL("4.1", 123) },
  956. { LEVEL("5", 150) },
  957. { LEVEL("5.1", 153) },
  958. { LEVEL("5.2", 156) },
  959. { LEVEL("6", 180) },
  960. { LEVEL("6.1", 183) },
  961. { LEVEL("6.2", 186) },
  962. #undef LEVEL
  963. { "sei", "Set SEI to include",
  964. OFFSET(sei), AV_OPT_TYPE_FLAGS,
  965. { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
  966. 0, INT_MAX, FLAGS, "sei" },
  967. { "hdr",
  968. "Include HDR metadata for mastering display colour volume "
  969. "and content light level information",
  970. 0, AV_OPT_TYPE_CONST,
  971. { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
  972. INT_MIN, INT_MAX, FLAGS, "sei" },
  973. { NULL },
  974. };
  975. static const AVCodecDefault vaapi_encode_h265_defaults[] = {
  976. { "b", "0" },
  977. { "bf", "2" },
  978. { "g", "120" },
  979. { "i_qfactor", "1" },
  980. { "i_qoffset", "0" },
  981. { "b_qfactor", "6/5" },
  982. { "b_qoffset", "0" },
  983. { "qmin", "-1" },
  984. { "qmax", "-1" },
  985. { NULL },
  986. };
  987. static const AVClass vaapi_encode_h265_class = {
  988. .class_name = "h265_vaapi",
  989. .item_name = av_default_item_name,
  990. .option = vaapi_encode_h265_options,
  991. .version = LIBAVUTIL_VERSION_INT,
  992. };
  993. AVCodec ff_hevc_vaapi_encoder = {
  994. .name = "hevc_vaapi",
  995. .long_name = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
  996. .type = AVMEDIA_TYPE_VIDEO,
  997. .id = AV_CODEC_ID_HEVC,
  998. .priv_data_size = sizeof(VAAPIEncodeH265Context),
  999. .init = &vaapi_encode_h265_init,
  1000. .encode2 = &ff_vaapi_encode2,
  1001. .close = &vaapi_encode_h265_close,
  1002. .priv_class = &vaapi_encode_h265_class,
  1003. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  1004. .defaults = vaapi_encode_h265_defaults,
  1005. .pix_fmts = (const enum AVPixelFormat[]) {
  1006. AV_PIX_FMT_VAAPI,
  1007. AV_PIX_FMT_NONE,
  1008. },
  1009. .wrapper_name = "vaapi",
  1010. };