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.

1325 lines
48KB

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