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.

1206 lines
43KB

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