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.

946 lines
35KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 "avcodec.h"
  25. #include "cbs.h"
  26. #include "cbs_h265.h"
  27. #include "hevc.h"
  28. #include "internal.h"
  29. #include "put_bits.h"
  30. #include "vaapi_encode.h"
  31. typedef struct VAAPIEncodeH265Context {
  32. unsigned int ctu_width;
  33. unsigned int ctu_height;
  34. int fixed_qp_idr;
  35. int fixed_qp_p;
  36. int fixed_qp_b;
  37. H265RawVPS vps;
  38. H265RawSPS sps;
  39. H265RawPPS pps;
  40. H265RawSlice slice;
  41. int64_t last_idr_frame;
  42. int pic_order_cnt;
  43. int slice_nal_unit;
  44. int slice_type;
  45. CodedBitstreamContext cbc;
  46. CodedBitstreamFragment current_access_unit;
  47. } VAAPIEncodeH265Context;
  48. typedef struct VAAPIEncodeH265Options {
  49. int qp;
  50. } VAAPIEncodeH265Options;
  51. static int vaapi_encode_h265_write_access_unit(AVCodecContext *avctx,
  52. char *data, size_t *data_len,
  53. CodedBitstreamFragment *au)
  54. {
  55. VAAPIEncodeContext *ctx = avctx->priv_data;
  56. VAAPIEncodeH265Context *priv = ctx->priv_data;
  57. int err;
  58. err = ff_cbs_write_fragment_data(&priv->cbc, au);
  59. if (err < 0) {
  60. av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
  61. return err;
  62. }
  63. if (*data_len < 8 * au->data_size - au->data_bit_padding) {
  64. av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
  65. "%zu < %zu.\n", *data_len,
  66. 8 * au->data_size - au->data_bit_padding);
  67. return AVERROR(ENOSPC);
  68. }
  69. memcpy(data, au->data, au->data_size);
  70. *data_len = 8 * au->data_size - au->data_bit_padding;
  71. return 0;
  72. }
  73. static int vaapi_encode_h265_add_nal(AVCodecContext *avctx,
  74. CodedBitstreamFragment *au,
  75. void *nal_unit)
  76. {
  77. VAAPIEncodeContext *ctx = avctx->priv_data;
  78. VAAPIEncodeH265Context *priv = ctx->priv_data;
  79. H265RawNALUnitHeader *header = nal_unit;
  80. int err;
  81. err = ff_cbs_insert_unit_content(&priv->cbc, au, -1,
  82. header->nal_unit_type, nal_unit);
  83. if (err < 0) {
  84. av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
  85. "type = %d.\n", header->nal_unit_type);
  86. return err;
  87. }
  88. return 0;
  89. }
  90. static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
  91. char *data, size_t *data_len)
  92. {
  93. VAAPIEncodeContext *ctx = avctx->priv_data;
  94. VAAPIEncodeH265Context *priv = ctx->priv_data;
  95. CodedBitstreamFragment *au = &priv->current_access_unit;
  96. int err;
  97. err = vaapi_encode_h265_add_nal(avctx, au, &priv->vps);
  98. if (err < 0)
  99. goto fail;
  100. err = vaapi_encode_h265_add_nal(avctx, au, &priv->sps);
  101. if (err < 0)
  102. goto fail;
  103. err = vaapi_encode_h265_add_nal(avctx, au, &priv->pps);
  104. if (err < 0)
  105. goto fail;
  106. err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
  107. fail:
  108. ff_cbs_fragment_uninit(&priv->cbc, au);
  109. return err;
  110. }
  111. static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
  112. VAAPIEncodePicture *pic,
  113. VAAPIEncodeSlice *slice,
  114. char *data, size_t *data_len)
  115. {
  116. VAAPIEncodeContext *ctx = avctx->priv_data;
  117. VAAPIEncodeH265Context *priv = ctx->priv_data;
  118. CodedBitstreamFragment *au = &priv->current_access_unit;
  119. int err;
  120. err = vaapi_encode_h265_add_nal(avctx, au, &priv->slice);
  121. if (err < 0)
  122. goto fail;
  123. err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
  124. fail:
  125. ff_cbs_fragment_uninit(&priv->cbc, au);
  126. return err;
  127. }
  128. static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
  129. {
  130. VAAPIEncodeContext *ctx = avctx->priv_data;
  131. VAAPIEncodeH265Context *priv = ctx->priv_data;
  132. H265RawVPS *vps = &priv->vps;
  133. H265RawSPS *sps = &priv->sps;
  134. H265RawPPS *pps = &priv->pps;
  135. H265RawVUI *vui = &sps->vui;
  136. VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
  137. VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
  138. int i;
  139. memset(&priv->current_access_unit, 0,
  140. sizeof(priv->current_access_unit));
  141. memset(vps, 0, sizeof(*vps));
  142. memset(sps, 0, sizeof(*sps));
  143. memset(pps, 0, sizeof(*pps));
  144. // VPS
  145. vps->nal_unit_header = (H265RawNALUnitHeader) {
  146. .nal_unit_type = HEVC_NAL_VPS,
  147. .nuh_layer_id = 0,
  148. .nuh_temporal_id_plus1 = 1,
  149. };
  150. vps->vps_video_parameter_set_id = 0;
  151. vps->vps_base_layer_internal_flag = 1;
  152. vps->vps_base_layer_available_flag = 1;
  153. vps->vps_max_layers_minus1 = 0;
  154. vps->vps_max_sub_layers_minus1 = 0;
  155. vps->vps_temporal_id_nesting_flag = 1;
  156. vps->profile_tier_level = (H265RawProfileTierLevel) {
  157. .general_profile_space = 0,
  158. .general_profile_idc = avctx->profile,
  159. .general_tier_flag = 0,
  160. .general_progressive_source_flag = 1,
  161. .general_interlaced_source_flag = 0,
  162. .general_non_packed_constraint_flag = 1,
  163. .general_frame_only_constraint_flag = 1,
  164. .general_level_idc = avctx->level,
  165. };
  166. vps->profile_tier_level.general_profile_compatibility_flag[avctx->profile & 31] = 1;
  167. vps->vps_sub_layer_ordering_info_present_flag = 0;
  168. vps->vps_max_dec_pic_buffering_minus1[0] = (ctx->b_per_p > 0) + 1;
  169. vps->vps_max_num_reorder_pics[0] = (ctx->b_per_p > 0);
  170. vps->vps_max_latency_increase_plus1[0] = 0;
  171. vps->vps_max_layer_id = 0;
  172. vps->vps_num_layer_sets_minus1 = 0;
  173. vps->layer_id_included_flag[0][0] = 1;
  174. vps->vps_timing_info_present_flag = 1;
  175. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  176. vps->vps_num_units_in_tick = avctx->framerate.den;
  177. vps->vps_time_scale = avctx->framerate.num;
  178. vps->vps_poc_proportional_to_timing_flag = 1;
  179. vps->vps_num_ticks_poc_diff_one_minus1 = 0;
  180. } else {
  181. vps->vps_num_units_in_tick = avctx->time_base.num;
  182. vps->vps_time_scale = avctx->time_base.den;
  183. vps->vps_poc_proportional_to_timing_flag = 0;
  184. }
  185. vps->vps_num_hrd_parameters = 0;
  186. // SPS
  187. sps->nal_unit_header = (H265RawNALUnitHeader) {
  188. .nal_unit_type = HEVC_NAL_SPS,
  189. .nuh_layer_id = 0,
  190. .nuh_temporal_id_plus1 = 1,
  191. };
  192. sps->sps_video_parameter_set_id = vps->vps_video_parameter_set_id;
  193. sps->sps_max_sub_layers_minus1 = vps->vps_max_sub_layers_minus1;
  194. sps->sps_temporal_id_nesting_flag = vps->vps_temporal_id_nesting_flag;
  195. sps->profile_tier_level = vps->profile_tier_level;
  196. sps->sps_seq_parameter_set_id = 0;
  197. sps->chroma_format_idc = 1; // YUV 4:2:0.
  198. sps->separate_colour_plane_flag = 0;
  199. sps->pic_width_in_luma_samples = ctx->surface_width;
  200. sps->pic_height_in_luma_samples = ctx->surface_height;
  201. if (avctx->width != ctx->surface_width ||
  202. avctx->height != ctx->surface_height) {
  203. sps->conformance_window_flag = 1;
  204. sps->conf_win_left_offset = 0;
  205. sps->conf_win_right_offset =
  206. (ctx->surface_width - avctx->width) / 2;
  207. sps->conf_win_top_offset = 0;
  208. sps->conf_win_bottom_offset =
  209. (ctx->surface_height - avctx->height) / 2;
  210. } else {
  211. sps->conformance_window_flag = 0;
  212. }
  213. sps->bit_depth_luma_minus8 =
  214. avctx->profile == FF_PROFILE_HEVC_MAIN_10 ? 2 : 0;
  215. sps->bit_depth_chroma_minus8 = sps->bit_depth_luma_minus8;
  216. sps->log2_max_pic_order_cnt_lsb_minus4 = 8;
  217. sps->sps_sub_layer_ordering_info_present_flag =
  218. vps->vps_sub_layer_ordering_info_present_flag;
  219. for (i = 0; i <= sps->sps_max_sub_layers_minus1; i++) {
  220. sps->sps_max_dec_pic_buffering_minus1[i] =
  221. vps->vps_max_dec_pic_buffering_minus1[i];
  222. sps->sps_max_num_reorder_pics[i] =
  223. vps->vps_max_num_reorder_pics[i];
  224. sps->sps_max_latency_increase_plus1[i] =
  225. vps->vps_max_latency_increase_plus1[i];
  226. }
  227. // These have to come from the capabilities of the encoder. We have no
  228. // way to query them, so just hardcode parameters which work on the Intel
  229. // driver.
  230. // CTB size from 8x8 to 32x32.
  231. sps->log2_min_luma_coding_block_size_minus3 = 0;
  232. sps->log2_diff_max_min_luma_coding_block_size = 2;
  233. // Transform size from 4x4 to 32x32.
  234. sps->log2_min_luma_transform_block_size_minus2 = 0;
  235. sps->log2_diff_max_min_luma_transform_block_size = 3;
  236. // Full transform hierarchy allowed (2-5).
  237. sps->max_transform_hierarchy_depth_inter = 3;
  238. sps->max_transform_hierarchy_depth_intra = 3;
  239. // AMP works.
  240. sps->amp_enabled_flag = 1;
  241. // SAO and temporal MVP do not work.
  242. sps->sample_adaptive_offset_enabled_flag = 0;
  243. sps->sps_temporal_mvp_enabled_flag = 0;
  244. sps->pcm_enabled_flag = 0;
  245. // STRPSs should ideally be here rather than defined individually in
  246. // each slice, but the structure isn't completely fixed so for now
  247. // don't bother.
  248. sps->num_short_term_ref_pic_sets = 0;
  249. sps->long_term_ref_pics_present_flag = 0;
  250. sps->vui_parameters_present_flag = 1;
  251. if (avctx->sample_aspect_ratio.num != 0 &&
  252. avctx->sample_aspect_ratio.den != 0) {
  253. static const AVRational sar_idc[] = {
  254. { 0, 0 },
  255. { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
  256. { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
  257. { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
  258. { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
  259. };
  260. int i;
  261. for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
  262. if (avctx->sample_aspect_ratio.num == sar_idc[i].num &&
  263. avctx->sample_aspect_ratio.den == sar_idc[i].den) {
  264. vui->aspect_ratio_idc = i;
  265. break;
  266. }
  267. }
  268. if (i >= FF_ARRAY_ELEMS(sar_idc)) {
  269. vui->aspect_ratio_idc = 255;
  270. vui->sar_width = avctx->sample_aspect_ratio.num;
  271. vui->sar_height = avctx->sample_aspect_ratio.den;
  272. }
  273. vui->aspect_ratio_info_present_flag = 1;
  274. }
  275. if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
  276. avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  277. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  278. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  279. vui->video_signal_type_present_flag = 1;
  280. vui->video_format = 5; // Unspecified.
  281. vui->video_full_range_flag =
  282. avctx->color_range == AVCOL_RANGE_JPEG;
  283. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  284. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  285. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  286. vui->colour_description_present_flag = 1;
  287. vui->colour_primaries = avctx->color_primaries;
  288. vui->transfer_characteristics = avctx->color_trc;
  289. vui->matrix_coefficients = avctx->colorspace;
  290. }
  291. } else {
  292. vui->video_format = 5;
  293. vui->video_full_range_flag = 0;
  294. vui->colour_primaries = avctx->color_primaries;
  295. vui->transfer_characteristics = avctx->color_trc;
  296. vui->matrix_coefficients = avctx->colorspace;
  297. }
  298. if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
  299. vui->chroma_loc_info_present_flag = 1;
  300. vui->chroma_sample_loc_type_top_field =
  301. vui->chroma_sample_loc_type_bottom_field =
  302. avctx->chroma_sample_location - 1;
  303. }
  304. vui->vui_timing_info_present_flag = 1;
  305. vui->vui_num_units_in_tick = vps->vps_num_units_in_tick;
  306. vui->vui_time_scale = vps->vps_time_scale;
  307. vui->vui_poc_proportional_to_timing_flag = vps->vps_poc_proportional_to_timing_flag;
  308. vui->vui_num_ticks_poc_diff_one_minus1 = vps->vps_num_ticks_poc_diff_one_minus1;
  309. vui->vui_hrd_parameters_present_flag = 0;
  310. vui->bitstream_restriction_flag = 1;
  311. vui->motion_vectors_over_pic_boundaries_flag = 1;
  312. vui->restricted_ref_pic_lists_flag = 1;
  313. vui->max_bytes_per_pic_denom = 0;
  314. vui->max_bits_per_min_cu_denom = 0;
  315. vui->log2_max_mv_length_horizontal = 15;
  316. vui->log2_max_mv_length_vertical = 15;
  317. // PPS
  318. pps->nal_unit_header = (H265RawNALUnitHeader) {
  319. .nal_unit_type = HEVC_NAL_PPS,
  320. .nuh_layer_id = 0,
  321. .nuh_temporal_id_plus1 = 1,
  322. };
  323. pps->pps_pic_parameter_set_id = 0;
  324. pps->pps_seq_parameter_set_id = sps->sps_seq_parameter_set_id;
  325. pps->num_ref_idx_l0_default_active_minus1 = 0;
  326. pps->num_ref_idx_l1_default_active_minus1 = 0;
  327. pps->init_qp_minus26 = priv->fixed_qp_idr - 26;
  328. pps->cu_qp_delta_enabled_flag = (ctx->va_rc_mode != VA_RC_CQP);
  329. pps->diff_cu_qp_delta_depth = 0;
  330. pps->pps_loop_filter_across_slices_enabled_flag = 1;
  331. // Fill VAAPI parameter buffers.
  332. *vseq = (VAEncSequenceParameterBufferHEVC) {
  333. .general_profile_idc = vps->profile_tier_level.general_profile_idc,
  334. .general_level_idc = vps->profile_tier_level.general_level_idc,
  335. .general_tier_flag = vps->profile_tier_level.general_tier_flag,
  336. .intra_period = avctx->gop_size,
  337. .intra_idr_period = avctx->gop_size,
  338. .ip_period = ctx->b_per_p + 1,
  339. .bits_per_second = avctx->bit_rate,
  340. .pic_width_in_luma_samples = sps->pic_width_in_luma_samples,
  341. .pic_height_in_luma_samples = sps->pic_height_in_luma_samples,
  342. .seq_fields.bits = {
  343. .chroma_format_idc = sps->chroma_format_idc,
  344. .separate_colour_plane_flag = sps->separate_colour_plane_flag,
  345. .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
  346. .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
  347. .scaling_list_enabled_flag = sps->scaling_list_enabled_flag,
  348. .strong_intra_smoothing_enabled_flag =
  349. sps->strong_intra_smoothing_enabled_flag,
  350. .amp_enabled_flag = sps->amp_enabled_flag,
  351. .sample_adaptive_offset_enabled_flag =
  352. sps->sample_adaptive_offset_enabled_flag,
  353. .pcm_enabled_flag = sps->pcm_enabled_flag,
  354. .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled_flag,
  355. .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
  356. },
  357. .log2_min_luma_coding_block_size_minus3 =
  358. sps->log2_min_luma_coding_block_size_minus3,
  359. .log2_diff_max_min_luma_coding_block_size =
  360. sps->log2_diff_max_min_luma_coding_block_size,
  361. .log2_min_transform_block_size_minus2 =
  362. sps->log2_min_luma_transform_block_size_minus2,
  363. .log2_diff_max_min_transform_block_size =
  364. sps->log2_diff_max_min_luma_transform_block_size,
  365. .max_transform_hierarchy_depth_inter =
  366. sps->max_transform_hierarchy_depth_inter,
  367. .max_transform_hierarchy_depth_intra =
  368. sps->max_transform_hierarchy_depth_intra,
  369. .pcm_sample_bit_depth_luma_minus1 =
  370. sps->pcm_sample_bit_depth_luma_minus1,
  371. .pcm_sample_bit_depth_chroma_minus1 =
  372. sps->pcm_sample_bit_depth_chroma_minus1,
  373. .log2_min_pcm_luma_coding_block_size_minus3 =
  374. sps->log2_min_pcm_luma_coding_block_size_minus3,
  375. .log2_max_pcm_luma_coding_block_size_minus3 =
  376. sps->log2_min_pcm_luma_coding_block_size_minus3 +
  377. sps->log2_diff_max_min_pcm_luma_coding_block_size,
  378. .vui_parameters_present_flag = 0,
  379. };
  380. *vpic = (VAEncPictureParameterBufferHEVC) {
  381. .decoded_curr_pic = {
  382. .picture_id = VA_INVALID_ID,
  383. .flags = VA_PICTURE_HEVC_INVALID,
  384. },
  385. .coded_buf = VA_INVALID_ID,
  386. .collocated_ref_pic_index = 0xff,
  387. .last_picture = 0,
  388. .pic_init_qp = pps->init_qp_minus26 + 26,
  389. .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
  390. .pps_cb_qp_offset = pps->pps_cb_qp_offset,
  391. .pps_cr_qp_offset = pps->pps_cr_qp_offset,
  392. .num_tile_columns_minus1 = pps->num_tile_columns_minus1,
  393. .num_tile_rows_minus1 = pps->num_tile_rows_minus1,
  394. .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level_minus2,
  395. .ctu_max_bitsize_allowed = 0,
  396. .num_ref_idx_l0_default_active_minus1 =
  397. pps->num_ref_idx_l0_default_active_minus1,
  398. .num_ref_idx_l1_default_active_minus1 =
  399. pps->num_ref_idx_l1_default_active_minus1,
  400. .slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id,
  401. .pic_fields.bits = {
  402. .sign_data_hiding_enabled_flag = pps->sign_data_hiding_enabled_flag,
  403. .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
  404. .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
  405. .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
  406. .weighted_pred_flag = pps->weighted_pred_flag,
  407. .weighted_bipred_flag = pps->weighted_bipred_flag,
  408. .transquant_bypass_enabled_flag = pps->transquant_bypass_enabled_flag,
  409. .tiles_enabled_flag = pps->tiles_enabled_flag,
  410. .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
  411. .loop_filter_across_tiles_enabled_flag =
  412. pps->loop_filter_across_tiles_enabled_flag,
  413. .scaling_list_data_present_flag = (sps->sps_scaling_list_data_present_flag |
  414. pps->pps_scaling_list_data_present_flag),
  415. .screen_content_flag = 0,
  416. .enable_gpu_weighted_prediction = 0,
  417. .no_output_of_prior_pics_flag = 0,
  418. },
  419. };
  420. return 0;
  421. }
  422. static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
  423. VAAPIEncodePicture *pic)
  424. {
  425. VAAPIEncodeContext *ctx = avctx->priv_data;
  426. VAAPIEncodeH265Context *priv = ctx->priv_data;
  427. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  428. int i;
  429. if (pic->type == PICTURE_TYPE_IDR) {
  430. av_assert0(pic->display_order == pic->encode_order);
  431. priv->last_idr_frame = pic->display_order;
  432. priv->slice_nal_unit = HEVC_NAL_IDR_W_RADL;
  433. priv->slice_type = HEVC_SLICE_I;
  434. } else {
  435. av_assert0(pic->encode_order > priv->last_idr_frame);
  436. if (pic->type == PICTURE_TYPE_I) {
  437. priv->slice_nal_unit = HEVC_NAL_CRA_NUT;
  438. priv->slice_type = HEVC_SLICE_I;
  439. } else if (pic->type == PICTURE_TYPE_P) {
  440. av_assert0(pic->refs[0]);
  441. priv->slice_nal_unit = HEVC_NAL_TRAIL_R;
  442. priv->slice_type = HEVC_SLICE_P;
  443. } else {
  444. av_assert0(pic->refs[0] && pic->refs[1]);
  445. if (pic->refs[1]->type == PICTURE_TYPE_I)
  446. priv->slice_nal_unit = HEVC_NAL_RASL_N;
  447. else
  448. priv->slice_nal_unit = HEVC_NAL_TRAIL_N;
  449. priv->slice_type = HEVC_SLICE_B;
  450. }
  451. }
  452. priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
  453. vpic->decoded_curr_pic = (VAPictureHEVC) {
  454. .picture_id = pic->recon_surface,
  455. .pic_order_cnt = priv->pic_order_cnt,
  456. .flags = 0,
  457. };
  458. for (i = 0; i < pic->nb_refs; i++) {
  459. VAAPIEncodePicture *ref = pic->refs[i];
  460. av_assert0(ref && ref->encode_order < pic->encode_order);
  461. vpic->reference_frames[i] = (VAPictureHEVC) {
  462. .picture_id = ref->recon_surface,
  463. .pic_order_cnt = ref->display_order - priv->last_idr_frame,
  464. .flags = (ref->display_order < pic->display_order ?
  465. VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
  466. (ref->display_order > pic->display_order ?
  467. VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
  468. };
  469. }
  470. for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
  471. vpic->reference_frames[i] = (VAPictureHEVC) {
  472. .picture_id = VA_INVALID_ID,
  473. .flags = VA_PICTURE_HEVC_INVALID,
  474. };
  475. }
  476. vpic->coded_buf = pic->output_buffer;
  477. vpic->nal_unit_type = priv->slice_nal_unit;
  478. switch (pic->type) {
  479. case PICTURE_TYPE_IDR:
  480. vpic->pic_fields.bits.idr_pic_flag = 1;
  481. vpic->pic_fields.bits.coding_type = 1;
  482. vpic->pic_fields.bits.reference_pic_flag = 1;
  483. break;
  484. case PICTURE_TYPE_I:
  485. vpic->pic_fields.bits.idr_pic_flag = 0;
  486. vpic->pic_fields.bits.coding_type = 1;
  487. vpic->pic_fields.bits.reference_pic_flag = 1;
  488. break;
  489. case PICTURE_TYPE_P:
  490. vpic->pic_fields.bits.idr_pic_flag = 0;
  491. vpic->pic_fields.bits.coding_type = 2;
  492. vpic->pic_fields.bits.reference_pic_flag = 1;
  493. break;
  494. case PICTURE_TYPE_B:
  495. vpic->pic_fields.bits.idr_pic_flag = 0;
  496. vpic->pic_fields.bits.coding_type = 3;
  497. vpic->pic_fields.bits.reference_pic_flag = 0;
  498. break;
  499. default:
  500. av_assert0(0 && "invalid picture type");
  501. }
  502. pic->nb_slices = 1;
  503. return 0;
  504. }
  505. static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
  506. VAAPIEncodePicture *pic,
  507. VAAPIEncodeSlice *slice)
  508. {
  509. VAAPIEncodeContext *ctx = avctx->priv_data;
  510. VAAPIEncodeH265Context *priv = ctx->priv_data;
  511. const H265RawSPS *sps = &priv->sps;
  512. const H265RawPPS *pps = &priv->pps;
  513. H265RawSliceHeader *sh = &priv->slice.header;
  514. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  515. VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
  516. int i;
  517. sh->nal_unit_header = (H265RawNALUnitHeader) {
  518. .nal_unit_type = priv->slice_nal_unit,
  519. .nuh_layer_id = 0,
  520. .nuh_temporal_id_plus1 = 1,
  521. };
  522. sh->slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id;
  523. // Currently we only support one slice per frame.
  524. sh->first_slice_segment_in_pic_flag = 1;
  525. sh->slice_segment_address = 0;
  526. sh->slice_type = priv->slice_type;
  527. sh->slice_pic_order_cnt_lsb = priv->pic_order_cnt &
  528. (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
  529. if (pic->type != PICTURE_TYPE_IDR) {
  530. H265RawSTRefPicSet *rps;
  531. VAAPIEncodePicture *st;
  532. int used;
  533. sh->short_term_ref_pic_set_sps_flag = 0;
  534. rps = &sh->short_term_ref_pic_set;
  535. memset(rps, 0, sizeof(*rps));
  536. for (st = ctx->pic_start; st; st = st->next) {
  537. if (st->encode_order >= pic->encode_order) {
  538. // Not yet in DPB.
  539. continue;
  540. }
  541. used = 0;
  542. for (i = 0; i < pic->nb_refs; i++) {
  543. if (pic->refs[i] == st)
  544. used = 1;
  545. }
  546. if (!used) {
  547. // Usually each picture always uses all of the others in the
  548. // DPB as references. The one case we have to treat here is
  549. // a non-IDR IRAP picture, which may need to hold unused
  550. // references across itself to be used for the decoding of
  551. // following RASL pictures. This looks for such an RASL
  552. // picture, and keeps the reference if there is one.
  553. VAAPIEncodePicture *rp;
  554. for (rp = ctx->pic_start; rp; rp = rp->next) {
  555. if (rp->encode_order < pic->encode_order)
  556. continue;
  557. if (rp->type != PICTURE_TYPE_B)
  558. continue;
  559. if (rp->refs[0] == st && rp->refs[1] == pic)
  560. break;
  561. }
  562. if (!rp)
  563. continue;
  564. }
  565. // This only works for one instance of each (delta_poc_sN_minus1
  566. // is relative to the previous frame in the list, not relative to
  567. // the current frame directly).
  568. if (st->display_order < pic->display_order) {
  569. rps->delta_poc_s0_minus1[rps->num_negative_pics] =
  570. pic->display_order - st->display_order - 1;
  571. rps->used_by_curr_pic_s0_flag[rps->num_negative_pics] = used;
  572. ++rps->num_negative_pics;
  573. } else {
  574. rps->delta_poc_s1_minus1[rps->num_positive_pics] =
  575. st->display_order - pic->display_order - 1;
  576. rps->used_by_curr_pic_s1_flag[rps->num_positive_pics] = used;
  577. ++rps->num_positive_pics;
  578. }
  579. }
  580. sh->num_long_term_sps = 0;
  581. sh->num_long_term_pics = 0;
  582. sh->slice_temporal_mvp_enabled_flag =
  583. sps->sps_temporal_mvp_enabled_flag;
  584. if (sh->slice_temporal_mvp_enabled_flag) {
  585. sh->collocated_from_l0_flag = sh->slice_type == HEVC_SLICE_B;
  586. sh->collocated_ref_idx = 0;
  587. }
  588. sh->num_ref_idx_active_override_flag = 0;
  589. sh->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
  590. sh->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
  591. }
  592. sh->slice_sao_luma_flag = sh->slice_sao_chroma_flag =
  593. sps->sample_adaptive_offset_enabled_flag;
  594. if (pic->type == PICTURE_TYPE_B)
  595. sh->slice_qp_delta = priv->fixed_qp_b - (pps->init_qp_minus26 + 26);
  596. else if (pic->type == PICTURE_TYPE_P)
  597. sh->slice_qp_delta = priv->fixed_qp_p - (pps->init_qp_minus26 + 26);
  598. else
  599. sh->slice_qp_delta = priv->fixed_qp_idr - (pps->init_qp_minus26 + 26);
  600. *vslice = (VAEncSliceParameterBufferHEVC) {
  601. .slice_segment_address = sh->slice_segment_address,
  602. .num_ctu_in_slice = priv->ctu_width * priv->ctu_height,
  603. .slice_type = sh->slice_type,
  604. .slice_pic_parameter_set_id = sh->slice_pic_parameter_set_id,
  605. .num_ref_idx_l0_active_minus1 = sh->num_ref_idx_l0_active_minus1,
  606. .num_ref_idx_l1_active_minus1 = sh->num_ref_idx_l1_active_minus1,
  607. .ref_pic_list0[0] = vpic->reference_frames[0],
  608. .ref_pic_list1[0] = vpic->reference_frames[1],
  609. .luma_log2_weight_denom = sh->luma_log2_weight_denom,
  610. .delta_chroma_log2_weight_denom = sh->delta_chroma_log2_weight_denom,
  611. .max_num_merge_cand = 5 - sh->five_minus_max_num_merge_cand,
  612. .slice_qp_delta = sh->slice_qp_delta,
  613. .slice_cb_qp_offset = sh->slice_cb_qp_offset,
  614. .slice_cr_qp_offset = sh->slice_cr_qp_offset,
  615. .slice_beta_offset_div2 = sh->slice_beta_offset_div2,
  616. .slice_tc_offset_div2 = sh->slice_tc_offset_div2,
  617. .slice_fields.bits = {
  618. .last_slice_of_pic_flag = 1,
  619. .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
  620. .colour_plane_id = sh->colour_plane_id,
  621. .slice_temporal_mvp_enabled_flag =
  622. sh->slice_temporal_mvp_enabled_flag,
  623. .slice_sao_luma_flag = sh->slice_sao_luma_flag,
  624. .slice_sao_chroma_flag = sh->slice_sao_chroma_flag,
  625. .num_ref_idx_active_override_flag =
  626. sh->num_ref_idx_active_override_flag,
  627. .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
  628. .cabac_init_flag = sh->cabac_init_flag,
  629. .slice_deblocking_filter_disabled_flag =
  630. sh->slice_deblocking_filter_disabled_flag,
  631. .slice_loop_filter_across_slices_enabled_flag =
  632. sh->slice_loop_filter_across_slices_enabled_flag,
  633. .collocated_from_l0_flag = sh->collocated_from_l0_flag,
  634. },
  635. };
  636. return 0;
  637. }
  638. static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
  639. {
  640. VAAPIEncodeContext *ctx = avctx->priv_data;
  641. VAAPIEncodeH265Context *priv = ctx->priv_data;
  642. VAAPIEncodeH265Options *opt = ctx->codec_options;
  643. int err;
  644. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
  645. if (err < 0)
  646. return err;
  647. priv->ctu_width = FFALIGN(ctx->surface_width, 32) / 32;
  648. priv->ctu_height = FFALIGN(ctx->surface_height, 32) / 32;
  649. av_log(avctx, AV_LOG_VERBOSE, "Input %ux%u -> Surface %ux%u -> CTU %ux%u.\n",
  650. avctx->width, avctx->height, ctx->surface_width,
  651. ctx->surface_height, priv->ctu_width, priv->ctu_height);
  652. if (ctx->va_rc_mode == VA_RC_CQP) {
  653. priv->fixed_qp_p = opt->qp;
  654. if (avctx->i_quant_factor > 0.0)
  655. priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
  656. avctx->i_quant_offset) + 0.5);
  657. else
  658. priv->fixed_qp_idr = priv->fixed_qp_p;
  659. if (avctx->b_quant_factor > 0.0)
  660. priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
  661. avctx->b_quant_offset) + 0.5);
  662. else
  663. priv->fixed_qp_b = priv->fixed_qp_p;
  664. av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
  665. "%d / %d / %d for IDR- / P- / B-frames.\n",
  666. priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
  667. } else if (ctx->va_rc_mode == VA_RC_CBR) {
  668. // These still need to be set for pic_init_qp/slice_qp_delta.
  669. priv->fixed_qp_idr = 30;
  670. priv->fixed_qp_p = 30;
  671. priv->fixed_qp_b = 30;
  672. av_log(avctx, AV_LOG_DEBUG, "Using constant-bitrate = %d bps.\n",
  673. avctx->bit_rate);
  674. } else {
  675. av_assert0(0 && "Invalid RC mode.");
  676. }
  677. return 0;
  678. }
  679. static const VAAPIEncodeType vaapi_encode_type_h265 = {
  680. .priv_data_size = sizeof(VAAPIEncodeH265Context),
  681. .configure = &vaapi_encode_h265_configure,
  682. .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
  683. .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
  684. .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
  685. .init_picture_params = &vaapi_encode_h265_init_picture_params,
  686. .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
  687. .init_slice_params = &vaapi_encode_h265_init_slice_params,
  688. .sequence_header_type = VAEncPackedHeaderSequence,
  689. .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
  690. .slice_header_type = VAEncPackedHeaderHEVC_Slice,
  691. .write_slice_header = &vaapi_encode_h265_write_slice_header,
  692. };
  693. static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
  694. {
  695. VAAPIEncodeContext *ctx = avctx->priv_data;
  696. ctx->codec = &vaapi_encode_type_h265;
  697. switch (avctx->profile) {
  698. case FF_PROFILE_HEVC_MAIN:
  699. case FF_PROFILE_UNKNOWN:
  700. ctx->va_profile = VAProfileHEVCMain;
  701. ctx->va_rt_format = VA_RT_FORMAT_YUV420;
  702. break;
  703. case FF_PROFILE_HEVC_MAIN_10:
  704. #ifdef VA_RT_FORMAT_YUV420_10BPP
  705. ctx->va_profile = VAProfileHEVCMain10;
  706. ctx->va_rt_format = VA_RT_FORMAT_YUV420_10BPP;
  707. break;
  708. #else
  709. av_log(avctx, AV_LOG_ERROR, "10-bit encoding is not "
  710. "supported with this VAAPI version.\n");
  711. return AVERROR(ENOSYS);
  712. #endif
  713. default:
  714. av_log(avctx, AV_LOG_ERROR, "Unknown H.265 profile %d.\n",
  715. avctx->profile);
  716. return AVERROR(EINVAL);
  717. }
  718. ctx->va_entrypoint = VAEntrypointEncSlice;
  719. if (avctx->bit_rate > 0)
  720. ctx->va_rc_mode = VA_RC_CBR;
  721. else
  722. ctx->va_rc_mode = VA_RC_CQP;
  723. ctx->va_packed_headers =
  724. VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
  725. VA_ENC_PACKED_HEADER_SLICE; // Slice headers.
  726. ctx->surface_width = FFALIGN(avctx->width, 16);
  727. ctx->surface_height = FFALIGN(avctx->height, 16);
  728. return ff_vaapi_encode_init(avctx);
  729. }
  730. static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
  731. {
  732. VAAPIEncodeContext *ctx = avctx->priv_data;
  733. VAAPIEncodeH265Context *priv = ctx->priv_data;
  734. if (priv)
  735. ff_cbs_close(&priv->cbc);
  736. return ff_vaapi_encode_close(avctx);
  737. }
  738. #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
  739. offsetof(VAAPIEncodeH265Options, x))
  740. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  741. static const AVOption vaapi_encode_h265_options[] = {
  742. { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
  743. OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, 52, FLAGS },
  744. { NULL },
  745. };
  746. static const AVCodecDefault vaapi_encode_h265_defaults[] = {
  747. { "profile", "1" },
  748. { "level", "51" },
  749. { "b", "0" },
  750. { "bf", "2" },
  751. { "g", "120" },
  752. { "i_qfactor", "1.0" },
  753. { "i_qoffset", "0.0" },
  754. { "b_qfactor", "1.2" },
  755. { "b_qoffset", "0.0" },
  756. { NULL },
  757. };
  758. static const AVClass vaapi_encode_h265_class = {
  759. .class_name = "h265_vaapi",
  760. .item_name = av_default_item_name,
  761. .option = vaapi_encode_h265_options,
  762. .version = LIBAVUTIL_VERSION_INT,
  763. };
  764. AVCodec ff_hevc_vaapi_encoder = {
  765. .name = "hevc_vaapi",
  766. .long_name = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
  767. .type = AVMEDIA_TYPE_VIDEO,
  768. .id = AV_CODEC_ID_HEVC,
  769. .priv_data_size = (sizeof(VAAPIEncodeContext) +
  770. sizeof(VAAPIEncodeH265Options)),
  771. .init = &vaapi_encode_h265_init,
  772. .encode2 = &ff_vaapi_encode2,
  773. .close = &vaapi_encode_h265_close,
  774. .priv_class = &vaapi_encode_h265_class,
  775. .capabilities = AV_CODEC_CAP_DELAY,
  776. .defaults = vaapi_encode_h265_defaults,
  777. .pix_fmts = (const enum AVPixelFormat[]) {
  778. AV_PIX_FMT_VAAPI,
  779. AV_PIX_FMT_NONE,
  780. },
  781. };