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.

1181 lines
42KB

  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. VAAPIEncodeContext common;
  39. // User options.
  40. int qp;
  41. int aud;
  42. int profile;
  43. int level;
  44. int sei;
  45. // Derived settings.
  46. unsigned int ctu_width;
  47. unsigned int ctu_height;
  48. int fixed_qp_idr;
  49. int fixed_qp_p;
  50. int fixed_qp_b;
  51. // Stream state.
  52. int64_t last_idr_frame;
  53. int pic_order_cnt;
  54. int slice_nal_unit;
  55. int slice_type;
  56. int pic_type;
  57. // Writer structures.
  58. H265RawAUD raw_aud;
  59. H265RawVPS raw_vps;
  60. H265RawSPS raw_sps;
  61. H265RawPPS raw_pps;
  62. H265RawSEI raw_sei;
  63. H265RawSlice raw_slice;
  64. H265RawSEIMasteringDisplayColourVolume sei_mastering_display;
  65. H265RawSEIContentLightLevelInfo sei_content_light_level;
  66. CodedBitstreamContext *cbc;
  67. CodedBitstreamFragment current_access_unit;
  68. int aud_needed;
  69. int sei_needed;
  70. } VAAPIEncodeH265Context;
  71. static int vaapi_encode_h265_write_access_unit(AVCodecContext *avctx,
  72. char *data, size_t *data_len,
  73. CodedBitstreamFragment *au)
  74. {
  75. VAAPIEncodeH265Context *priv = avctx->priv_data;
  76. int err;
  77. err = ff_cbs_write_fragment_data(priv->cbc, au);
  78. if (err < 0) {
  79. av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
  80. return err;
  81. }
  82. if (*data_len < 8 * au->data_size - au->data_bit_padding) {
  83. av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
  84. "%zu < %zu.\n", *data_len,
  85. 8 * au->data_size - au->data_bit_padding);
  86. return AVERROR(ENOSPC);
  87. }
  88. memcpy(data, au->data, au->data_size);
  89. *data_len = 8 * au->data_size - au->data_bit_padding;
  90. return 0;
  91. }
  92. static int vaapi_encode_h265_add_nal(AVCodecContext *avctx,
  93. CodedBitstreamFragment *au,
  94. void *nal_unit)
  95. {
  96. VAAPIEncodeH265Context *priv = avctx->priv_data;
  97. H265RawNALUnitHeader *header = nal_unit;
  98. int err;
  99. err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
  100. header->nal_unit_type, nal_unit, NULL);
  101. if (err < 0) {
  102. av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
  103. "type = %d.\n", header->nal_unit_type);
  104. return err;
  105. }
  106. return 0;
  107. }
  108. static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
  109. char *data, size_t *data_len)
  110. {
  111. VAAPIEncodeH265Context *priv = avctx->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->raw_aud);
  116. if (err < 0)
  117. goto fail;
  118. priv->aud_needed = 0;
  119. }
  120. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_vps);
  121. if (err < 0)
  122. goto fail;
  123. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_sps);
  124. if (err < 0)
  125. goto fail;
  126. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_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. VAAPIEncodeH265Context *priv = avctx->priv_data;
  140. CodedBitstreamFragment *au = &priv->current_access_unit;
  141. int err;
  142. if (priv->aud_needed) {
  143. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
  144. if (err < 0)
  145. goto fail;
  146. priv->aud_needed = 0;
  147. }
  148. err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_slice);
  149. if (err < 0)
  150. goto fail;
  151. err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
  152. fail:
  153. ff_cbs_fragment_uninit(priv->cbc, au);
  154. return err;
  155. }
  156. static int vaapi_encode_h265_write_extra_header(AVCodecContext *avctx,
  157. VAAPIEncodePicture *pic,
  158. int index, int *type,
  159. char *data, size_t *data_len)
  160. {
  161. VAAPIEncodeH265Context *priv = avctx->priv_data;
  162. CodedBitstreamFragment *au = &priv->current_access_unit;
  163. int err, i;
  164. if (priv->sei_needed) {
  165. H265RawSEI *sei = &priv->raw_sei;
  166. if (priv->aud_needed) {
  167. err = vaapi_encode_h265_add_nal(avctx, au, &priv->aud);
  168. if (err < 0)
  169. goto fail;
  170. priv->aud_needed = 0;
  171. }
  172. *sei = (H265RawSEI) {
  173. .nal_unit_header = {
  174. .nal_unit_type = HEVC_NAL_SEI_PREFIX,
  175. .nuh_layer_id = 0,
  176. .nuh_temporal_id_plus1 = 1,
  177. },
  178. };
  179. i = 0;
  180. if (priv->sei_needed & SEI_MASTERING_DISPLAY) {
  181. sei->payload[i].payload_type = HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO;
  182. sei->payload[i].payload.mastering_display = priv->sei_mastering_display;
  183. ++i;
  184. }
  185. if (priv->sei_needed & SEI_CONTENT_LIGHT_LEVEL) {
  186. sei->payload[i].payload_type = HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO;
  187. sei->payload[i].payload.content_light_level = priv->sei_content_light_level;
  188. ++i;
  189. }
  190. sei->payload_count = i;
  191. av_assert0(sei->payload_count > 0);
  192. err = vaapi_encode_h265_add_nal(avctx, au, 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 = avctx->priv_data;
  213. H265RawVPS *vps = &priv->raw_vps;
  214. H265RawSPS *sps = &priv->raw_sps;
  215. H265RawPPS *pps = &priv->raw_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 = ctx->gop_size,
  418. .intra_idr_period = ctx->gop_size,
  419. .ip_period = ctx->b_per_p + 1,
  420. .bits_per_second = ctx->va_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. VAAPIEncodeH265Context *priv = avctx->priv_data;
  507. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  508. int i;
  509. if (pic->type == PICTURE_TYPE_IDR) {
  510. av_assert0(pic->display_order == pic->encode_order);
  511. priv->last_idr_frame = pic->display_order;
  512. priv->slice_nal_unit = HEVC_NAL_IDR_W_RADL;
  513. priv->slice_type = HEVC_SLICE_I;
  514. priv->pic_type = 0;
  515. } else {
  516. av_assert0(pic->encode_order > priv->last_idr_frame);
  517. if (pic->type == PICTURE_TYPE_I) {
  518. priv->slice_nal_unit = HEVC_NAL_CRA_NUT;
  519. priv->slice_type = HEVC_SLICE_I;
  520. priv->pic_type = 0;
  521. } else if (pic->type == PICTURE_TYPE_P) {
  522. av_assert0(pic->refs[0]);
  523. priv->slice_nal_unit = HEVC_NAL_TRAIL_R;
  524. priv->slice_type = HEVC_SLICE_P;
  525. priv->pic_type = 1;
  526. } else {
  527. av_assert0(pic->refs[0] && pic->refs[1]);
  528. if (pic->refs[1]->type == PICTURE_TYPE_I)
  529. priv->slice_nal_unit = HEVC_NAL_RASL_N;
  530. else
  531. priv->slice_nal_unit = HEVC_NAL_TRAIL_N;
  532. priv->slice_type = HEVC_SLICE_B;
  533. priv->pic_type = 2;
  534. }
  535. }
  536. priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
  537. if (priv->aud) {
  538. priv->aud_needed = 1;
  539. priv->raw_aud = (H265RawAUD) {
  540. .nal_unit_header = {
  541. .nal_unit_type = HEVC_NAL_AUD,
  542. .nuh_layer_id = 0,
  543. .nuh_temporal_id_plus1 = 1,
  544. },
  545. .pic_type = priv->pic_type,
  546. };
  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 ((priv->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. H265RawSEIMasteringDisplayColourVolume *mdcv =
  565. &priv->sei_mastering_display;
  566. const int mapping[3] = {1, 2, 0};
  567. const int chroma_den = 50000;
  568. const int luma_den = 10000;
  569. for (i = 0; i < 3; i++) {
  570. const int j = mapping[i];
  571. mdcv->display_primaries_x[i] =
  572. FFMIN(lrint(chroma_den *
  573. av_q2d(mdm->display_primaries[j][0])),
  574. chroma_den);
  575. mdcv->display_primaries_y[i] =
  576. FFMIN(lrint(chroma_den *
  577. av_q2d(mdm->display_primaries[j][1])),
  578. chroma_den);
  579. }
  580. mdcv->white_point_x =
  581. FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])),
  582. chroma_den);
  583. mdcv->white_point_y =
  584. FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])),
  585. chroma_den);
  586. mdcv->max_display_mastering_luminance =
  587. lrint(luma_den * av_q2d(mdm->max_luminance));
  588. mdcv->min_display_mastering_luminance =
  589. FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)),
  590. mdcv->max_display_mastering_luminance);
  591. priv->sei_needed |= SEI_MASTERING_DISPLAY;
  592. }
  593. }
  594. }
  595. if ((priv->sei & SEI_CONTENT_LIGHT_LEVEL) &&
  596. (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
  597. AVFrameSideData *sd =
  598. av_frame_get_side_data(pic->input_image,
  599. AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
  600. if (sd) {
  601. AVContentLightMetadata *clm =
  602. (AVContentLightMetadata *)sd->data;
  603. H265RawSEIContentLightLevelInfo *clli =
  604. &priv->sei_content_light_level;
  605. clli->max_content_light_level = FFMIN(clm->MaxCLL, 65535);
  606. clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535);
  607. priv->sei_needed |= SEI_CONTENT_LIGHT_LEVEL;
  608. }
  609. }
  610. vpic->decoded_curr_pic = (VAPictureHEVC) {
  611. .picture_id = pic->recon_surface,
  612. .pic_order_cnt = priv->pic_order_cnt,
  613. .flags = 0,
  614. };
  615. for (i = 0; i < pic->nb_refs; i++) {
  616. VAAPIEncodePicture *ref = pic->refs[i];
  617. av_assert0(ref && ref->encode_order < pic->encode_order);
  618. vpic->reference_frames[i] = (VAPictureHEVC) {
  619. .picture_id = ref->recon_surface,
  620. .pic_order_cnt = ref->display_order - priv->last_idr_frame,
  621. .flags = (ref->display_order < pic->display_order ?
  622. VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
  623. (ref->display_order > pic->display_order ?
  624. VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
  625. };
  626. }
  627. for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
  628. vpic->reference_frames[i] = (VAPictureHEVC) {
  629. .picture_id = VA_INVALID_ID,
  630. .flags = VA_PICTURE_HEVC_INVALID,
  631. };
  632. }
  633. vpic->coded_buf = pic->output_buffer;
  634. vpic->nal_unit_type = priv->slice_nal_unit;
  635. switch (pic->type) {
  636. case PICTURE_TYPE_IDR:
  637. vpic->pic_fields.bits.idr_pic_flag = 1;
  638. vpic->pic_fields.bits.coding_type = 1;
  639. vpic->pic_fields.bits.reference_pic_flag = 1;
  640. break;
  641. case PICTURE_TYPE_I:
  642. vpic->pic_fields.bits.idr_pic_flag = 0;
  643. vpic->pic_fields.bits.coding_type = 1;
  644. vpic->pic_fields.bits.reference_pic_flag = 1;
  645. break;
  646. case PICTURE_TYPE_P:
  647. vpic->pic_fields.bits.idr_pic_flag = 0;
  648. vpic->pic_fields.bits.coding_type = 2;
  649. vpic->pic_fields.bits.reference_pic_flag = 1;
  650. break;
  651. case PICTURE_TYPE_B:
  652. vpic->pic_fields.bits.idr_pic_flag = 0;
  653. vpic->pic_fields.bits.coding_type = 3;
  654. vpic->pic_fields.bits.reference_pic_flag = 0;
  655. break;
  656. default:
  657. av_assert0(0 && "invalid picture type");
  658. }
  659. pic->nb_slices = 1;
  660. return 0;
  661. }
  662. static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
  663. VAAPIEncodePicture *pic,
  664. VAAPIEncodeSlice *slice)
  665. {
  666. VAAPIEncodeContext *ctx = avctx->priv_data;
  667. VAAPIEncodeH265Context *priv = avctx->priv_data;
  668. const H265RawSPS *sps = &priv->raw_sps;
  669. const H265RawPPS *pps = &priv->raw_pps;
  670. H265RawSliceHeader *sh = &priv->raw_slice.header;
  671. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  672. VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
  673. int i;
  674. sh->nal_unit_header = (H265RawNALUnitHeader) {
  675. .nal_unit_type = priv->slice_nal_unit,
  676. .nuh_layer_id = 0,
  677. .nuh_temporal_id_plus1 = 1,
  678. };
  679. sh->slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id;
  680. // Currently we only support one slice per frame.
  681. sh->first_slice_segment_in_pic_flag = 1;
  682. sh->slice_segment_address = 0;
  683. sh->slice_type = priv->slice_type;
  684. sh->slice_pic_order_cnt_lsb = priv->pic_order_cnt &
  685. (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
  686. if (pic->type != PICTURE_TYPE_IDR) {
  687. H265RawSTRefPicSet *rps;
  688. VAAPIEncodePicture *st;
  689. int used;
  690. sh->short_term_ref_pic_set_sps_flag = 0;
  691. rps = &sh->short_term_ref_pic_set;
  692. memset(rps, 0, sizeof(*rps));
  693. for (st = ctx->pic_start; st; st = st->next) {
  694. if (st->encode_order >= pic->encode_order) {
  695. // Not yet in DPB.
  696. continue;
  697. }
  698. used = 0;
  699. for (i = 0; i < pic->nb_refs; i++) {
  700. if (pic->refs[i] == st)
  701. used = 1;
  702. }
  703. if (!used) {
  704. // Usually each picture always uses all of the others in the
  705. // DPB as references. The one case we have to treat here is
  706. // a non-IDR IRAP picture, which may need to hold unused
  707. // references across itself to be used for the decoding of
  708. // following RASL pictures. This looks for such an RASL
  709. // picture, and keeps the reference if there is one.
  710. VAAPIEncodePicture *rp;
  711. for (rp = ctx->pic_start; rp; rp = rp->next) {
  712. if (rp->encode_order < pic->encode_order)
  713. continue;
  714. if (rp->type != PICTURE_TYPE_B)
  715. continue;
  716. if (rp->refs[0] == st && rp->refs[1] == pic)
  717. break;
  718. }
  719. if (!rp)
  720. continue;
  721. }
  722. // This only works for one instance of each (delta_poc_sN_minus1
  723. // is relative to the previous frame in the list, not relative to
  724. // the current frame directly).
  725. if (st->display_order < pic->display_order) {
  726. rps->delta_poc_s0_minus1[rps->num_negative_pics] =
  727. pic->display_order - st->display_order - 1;
  728. rps->used_by_curr_pic_s0_flag[rps->num_negative_pics] = used;
  729. ++rps->num_negative_pics;
  730. } else {
  731. rps->delta_poc_s1_minus1[rps->num_positive_pics] =
  732. st->display_order - pic->display_order - 1;
  733. rps->used_by_curr_pic_s1_flag[rps->num_positive_pics] = used;
  734. ++rps->num_positive_pics;
  735. }
  736. }
  737. sh->num_long_term_sps = 0;
  738. sh->num_long_term_pics = 0;
  739. sh->slice_temporal_mvp_enabled_flag =
  740. sps->sps_temporal_mvp_enabled_flag;
  741. if (sh->slice_temporal_mvp_enabled_flag) {
  742. sh->collocated_from_l0_flag = sh->slice_type == HEVC_SLICE_B;
  743. sh->collocated_ref_idx = 0;
  744. }
  745. sh->num_ref_idx_active_override_flag = 0;
  746. sh->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
  747. sh->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
  748. }
  749. sh->slice_sao_luma_flag = sh->slice_sao_chroma_flag =
  750. sps->sample_adaptive_offset_enabled_flag;
  751. if (pic->type == PICTURE_TYPE_B)
  752. sh->slice_qp_delta = priv->fixed_qp_b - (pps->init_qp_minus26 + 26);
  753. else if (pic->type == PICTURE_TYPE_P)
  754. sh->slice_qp_delta = priv->fixed_qp_p - (pps->init_qp_minus26 + 26);
  755. else
  756. sh->slice_qp_delta = priv->fixed_qp_idr - (pps->init_qp_minus26 + 26);
  757. *vslice = (VAEncSliceParameterBufferHEVC) {
  758. .slice_segment_address = sh->slice_segment_address,
  759. .num_ctu_in_slice = priv->ctu_width * priv->ctu_height,
  760. .slice_type = sh->slice_type,
  761. .slice_pic_parameter_set_id = sh->slice_pic_parameter_set_id,
  762. .num_ref_idx_l0_active_minus1 = sh->num_ref_idx_l0_active_minus1,
  763. .num_ref_idx_l1_active_minus1 = sh->num_ref_idx_l1_active_minus1,
  764. .luma_log2_weight_denom = sh->luma_log2_weight_denom,
  765. .delta_chroma_log2_weight_denom = sh->delta_chroma_log2_weight_denom,
  766. .max_num_merge_cand = 5 - sh->five_minus_max_num_merge_cand,
  767. .slice_qp_delta = sh->slice_qp_delta,
  768. .slice_cb_qp_offset = sh->slice_cb_qp_offset,
  769. .slice_cr_qp_offset = sh->slice_cr_qp_offset,
  770. .slice_beta_offset_div2 = sh->slice_beta_offset_div2,
  771. .slice_tc_offset_div2 = sh->slice_tc_offset_div2,
  772. .slice_fields.bits = {
  773. .last_slice_of_pic_flag = 1,
  774. .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
  775. .colour_plane_id = sh->colour_plane_id,
  776. .slice_temporal_mvp_enabled_flag =
  777. sh->slice_temporal_mvp_enabled_flag,
  778. .slice_sao_luma_flag = sh->slice_sao_luma_flag,
  779. .slice_sao_chroma_flag = sh->slice_sao_chroma_flag,
  780. .num_ref_idx_active_override_flag =
  781. sh->num_ref_idx_active_override_flag,
  782. .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
  783. .cabac_init_flag = sh->cabac_init_flag,
  784. .slice_deblocking_filter_disabled_flag =
  785. sh->slice_deblocking_filter_disabled_flag,
  786. .slice_loop_filter_across_slices_enabled_flag =
  787. sh->slice_loop_filter_across_slices_enabled_flag,
  788. .collocated_from_l0_flag = sh->collocated_from_l0_flag,
  789. },
  790. };
  791. for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
  792. vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
  793. vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
  794. vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
  795. vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
  796. }
  797. av_assert0(pic->nb_refs <= 2);
  798. if (pic->nb_refs >= 1) {
  799. // Backward reference for P- or B-frame.
  800. av_assert0(pic->type == PICTURE_TYPE_P ||
  801. pic->type == PICTURE_TYPE_B);
  802. vslice->ref_pic_list0[0] = vpic->reference_frames[0];
  803. }
  804. if (pic->nb_refs >= 2) {
  805. // Forward reference for B-frame.
  806. av_assert0(pic->type == PICTURE_TYPE_B);
  807. vslice->ref_pic_list1[0] = vpic->reference_frames[1];
  808. }
  809. return 0;
  810. }
  811. static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
  812. {
  813. VAAPIEncodeContext *ctx = avctx->priv_data;
  814. VAAPIEncodeH265Context *priv = avctx->priv_data;
  815. int err;
  816. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
  817. if (err < 0)
  818. return err;
  819. priv->ctu_width = FFALIGN(ctx->surface_width, 32) / 32;
  820. priv->ctu_height = FFALIGN(ctx->surface_height, 32) / 32;
  821. av_log(avctx, AV_LOG_VERBOSE, "Input %ux%u -> Surface %ux%u -> CTU %ux%u.\n",
  822. avctx->width, avctx->height, ctx->surface_width,
  823. ctx->surface_height, priv->ctu_width, priv->ctu_height);
  824. if (ctx->va_rc_mode == VA_RC_CQP) {
  825. priv->fixed_qp_p = priv->qp;
  826. if (avctx->i_quant_factor > 0.0)
  827. priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
  828. avctx->i_quant_offset) + 0.5);
  829. else
  830. priv->fixed_qp_idr = priv->fixed_qp_p;
  831. if (avctx->b_quant_factor > 0.0)
  832. priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
  833. avctx->b_quant_offset) + 0.5);
  834. else
  835. priv->fixed_qp_b = priv->fixed_qp_p;
  836. av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
  837. "%d / %d / %d for IDR- / P- / B-frames.\n",
  838. priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
  839. } else if (ctx->va_rc_mode == VA_RC_CBR ||
  840. ctx->va_rc_mode == VA_RC_VBR) {
  841. // These still need to be set for pic_init_qp/slice_qp_delta.
  842. priv->fixed_qp_idr = 30;
  843. priv->fixed_qp_p = 30;
  844. priv->fixed_qp_b = 30;
  845. } else {
  846. av_assert0(0 && "Invalid RC mode.");
  847. }
  848. return 0;
  849. }
  850. static const VAAPIEncodeProfile vaapi_encode_h265_profiles[] = {
  851. { FF_PROFILE_HEVC_MAIN, 8, 3, 1, 1, VAProfileHEVCMain },
  852. #if VA_CHECK_VERSION(0, 37, 0)
  853. { FF_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, VAProfileHEVCMain10 },
  854. #endif
  855. { FF_PROFILE_UNKNOWN }
  856. };
  857. static const VAAPIEncodeType vaapi_encode_type_h265 = {
  858. .profiles = vaapi_encode_h265_profiles,
  859. .configure = &vaapi_encode_h265_configure,
  860. .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
  861. .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
  862. .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
  863. .init_picture_params = &vaapi_encode_h265_init_picture_params,
  864. .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
  865. .init_slice_params = &vaapi_encode_h265_init_slice_params,
  866. .sequence_header_type = VAEncPackedHeaderSequence,
  867. .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
  868. .slice_header_type = VAEncPackedHeaderHEVC_Slice,
  869. .write_slice_header = &vaapi_encode_h265_write_slice_header,
  870. .write_extra_header = &vaapi_encode_h265_write_extra_header,
  871. };
  872. static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
  873. {
  874. VAAPIEncodeContext *ctx = avctx->priv_data;
  875. VAAPIEncodeH265Context *priv = avctx->priv_data;
  876. ctx->codec = &vaapi_encode_type_h265;
  877. if (avctx->profile == FF_PROFILE_UNKNOWN)
  878. avctx->profile = priv->profile;
  879. if (avctx->level == FF_LEVEL_UNKNOWN)
  880. avctx->level = priv->level;
  881. ctx->va_packed_headers =
  882. VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
  883. VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
  884. VA_ENC_PACKED_HEADER_MISC; // SEI
  885. ctx->surface_width = FFALIGN(avctx->width, 16);
  886. ctx->surface_height = FFALIGN(avctx->height, 16);
  887. return ff_vaapi_encode_init(avctx);
  888. }
  889. static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
  890. {
  891. VAAPIEncodeH265Context *priv = avctx->priv_data;
  892. ff_cbs_close(&priv->cbc);
  893. return ff_vaapi_encode_close(avctx);
  894. }
  895. #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
  896. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  897. static const AVOption vaapi_encode_h265_options[] = {
  898. VAAPI_ENCODE_COMMON_OPTIONS,
  899. { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
  900. OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, 52, FLAGS },
  901. { "aud", "Include AUD",
  902. OFFSET(aud), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  903. { "profile", "Set profile (general_profile_idc)",
  904. OFFSET(profile), AV_OPT_TYPE_INT,
  905. { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
  906. #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  907. { .i64 = value }, 0, 0, FLAGS, "profile"
  908. { PROFILE("main", FF_PROFILE_HEVC_MAIN) },
  909. { PROFILE("main10", FF_PROFILE_HEVC_MAIN_10) },
  910. #undef PROFILE
  911. { "level", "Set level (general_level_idc)",
  912. OFFSET(level), AV_OPT_TYPE_INT,
  913. { .i64 = 153 }, 0x00, 0xff, FLAGS, "level" },
  914. #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  915. { .i64 = value }, 0, 0, FLAGS, "level"
  916. { LEVEL("1", 30) },
  917. { LEVEL("2", 60) },
  918. { LEVEL("2.1", 63) },
  919. { LEVEL("3", 90) },
  920. { LEVEL("3.1", 93) },
  921. { LEVEL("4", 120) },
  922. { LEVEL("4.1", 123) },
  923. { LEVEL("5", 150) },
  924. { LEVEL("5.1", 153) },
  925. { LEVEL("5.2", 156) },
  926. { LEVEL("6", 180) },
  927. { LEVEL("6.1", 183) },
  928. { LEVEL("6.2", 186) },
  929. #undef LEVEL
  930. { "sei", "Set SEI to include",
  931. OFFSET(sei), AV_OPT_TYPE_FLAGS,
  932. { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
  933. 0, INT_MAX, FLAGS, "sei" },
  934. { "hdr",
  935. "Include HDR metadata for mastering display colour volume "
  936. "and content light level information",
  937. 0, AV_OPT_TYPE_CONST,
  938. { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
  939. INT_MIN, INT_MAX, FLAGS, "sei" },
  940. { NULL },
  941. };
  942. static const AVCodecDefault vaapi_encode_h265_defaults[] = {
  943. { "b", "0" },
  944. { "bf", "2" },
  945. { "g", "120" },
  946. { "i_qfactor", "1" },
  947. { "i_qoffset", "0" },
  948. { "b_qfactor", "6/5" },
  949. { "b_qoffset", "0" },
  950. { "qmin", "-1" },
  951. { "qmax", "-1" },
  952. { NULL },
  953. };
  954. static const AVClass vaapi_encode_h265_class = {
  955. .class_name = "h265_vaapi",
  956. .item_name = av_default_item_name,
  957. .option = vaapi_encode_h265_options,
  958. .version = LIBAVUTIL_VERSION_INT,
  959. };
  960. AVCodec ff_hevc_vaapi_encoder = {
  961. .name = "hevc_vaapi",
  962. .long_name = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
  963. .type = AVMEDIA_TYPE_VIDEO,
  964. .id = AV_CODEC_ID_HEVC,
  965. .priv_data_size = sizeof(VAAPIEncodeH265Context),
  966. .init = &vaapi_encode_h265_init,
  967. .encode2 = &ff_vaapi_encode2,
  968. .close = &vaapi_encode_h265_close,
  969. .priv_class = &vaapi_encode_h265_class,
  970. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  971. .defaults = vaapi_encode_h265_defaults,
  972. .pix_fmts = (const enum AVPixelFormat[]) {
  973. AV_PIX_FMT_VAAPI,
  974. AV_PIX_FMT_NONE,
  975. },
  976. .wrapper_name = "vaapi",
  977. };