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.

985 lines
36KB

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