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.

1029 lines
37KB

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