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.

1321 lines
48KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <va/va.h>
  19. #include <va/va_enc_hevc.h>
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/internal.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixfmt.h"
  24. #include "avcodec.h"
  25. #include "hevc.h"
  26. #include "internal.h"
  27. #include "put_bits.h"
  28. #include "vaapi_encode.h"
  29. #include "vaapi_encode_h26x.h"
  30. #define MAX_ST_REF_PIC_SETS 32
  31. #define MAX_DPB_PICS 16
  32. #define MAX_LAYERS 1
  33. typedef struct VAAPIEncodeH265STRPS {
  34. char inter_ref_pic_set_prediction_flag;
  35. unsigned int num_negative_pics;
  36. unsigned int num_positive_pics;
  37. unsigned int delta_poc_s0_minus1[MAX_DPB_PICS];
  38. char used_by_curr_pic_s0_flag[MAX_DPB_PICS];
  39. unsigned int delta_poc_s1_minus1[MAX_DPB_PICS];
  40. char used_by_curr_pic_s1_flag[MAX_DPB_PICS];
  41. } VAAPIEncodeH265STRPS;
  42. // This structure contains all possibly-useful per-sequence syntax elements
  43. // which are not already contained in the various VAAPI structures.
  44. typedef struct VAAPIEncodeH265MiscSequenceParams {
  45. // Parameter set IDs.
  46. unsigned int video_parameter_set_id;
  47. unsigned int seq_parameter_set_id;
  48. // Layering.
  49. unsigned int vps_max_layers_minus1;
  50. unsigned int vps_max_sub_layers_minus1;
  51. char vps_temporal_id_nesting_flag;
  52. unsigned int vps_max_layer_id;
  53. unsigned int vps_num_layer_sets_minus1;
  54. unsigned int sps_max_sub_layers_minus1;
  55. char sps_temporal_id_nesting_flag;
  56. char layer_id_included_flag[MAX_LAYERS][64];
  57. // Profile/tier/level parameters.
  58. char general_profile_compatibility_flag[32];
  59. char general_progressive_source_flag;
  60. char general_interlaced_source_flag;
  61. char general_non_packed_constraint_flag;
  62. char general_frame_only_constraint_flag;
  63. char general_inbld_flag;
  64. // Decode/display ordering parameters.
  65. unsigned int log2_max_pic_order_cnt_lsb_minus4;
  66. char vps_sub_layer_ordering_info_present_flag;
  67. unsigned int vps_max_dec_pic_buffering_minus1[MAX_LAYERS];
  68. unsigned int vps_max_num_reorder_pics[MAX_LAYERS];
  69. unsigned int vps_max_latency_increase_plus1[MAX_LAYERS];
  70. char sps_sub_layer_ordering_info_present_flag;
  71. unsigned int sps_max_dec_pic_buffering_minus1[MAX_LAYERS];
  72. unsigned int sps_max_num_reorder_pics[MAX_LAYERS];
  73. unsigned int sps_max_latency_increase_plus1[MAX_LAYERS];
  74. // Timing information.
  75. char vps_timing_info_present_flag;
  76. unsigned int vps_num_units_in_tick;
  77. unsigned int vps_time_scale;
  78. char vps_poc_proportional_to_timing_flag;
  79. unsigned int vps_num_ticks_poc_diff_minus1;
  80. // Cropping information.
  81. char conformance_window_flag;
  82. unsigned int conf_win_left_offset;
  83. unsigned int conf_win_right_offset;
  84. unsigned int conf_win_top_offset;
  85. unsigned int conf_win_bottom_offset;
  86. // Short-term reference picture sets.
  87. unsigned int num_short_term_ref_pic_sets;
  88. VAAPIEncodeH265STRPS st_ref_pic_set[MAX_ST_REF_PIC_SETS];
  89. // Long-term reference pictures.
  90. char long_term_ref_pics_present_flag;
  91. unsigned int num_long_term_ref_pics_sps;
  92. struct {
  93. unsigned int lt_ref_pic_poc_lsb_sps;
  94. char used_by_curr_pic_lt_sps_flag;
  95. } lt_ref_pic;
  96. // Deblocking filter control.
  97. char deblocking_filter_control_present_flag;
  98. char deblocking_filter_override_enabled_flag;
  99. char pps_deblocking_filter_disabled_flag;
  100. int pps_beta_offset_div2;
  101. int pps_tc_offset_div2;
  102. // Video Usability Information.
  103. char vui_parameters_present_flag;
  104. char aspect_ratio_info_present_flag;
  105. unsigned int aspect_ratio_idc;
  106. unsigned int sar_width;
  107. unsigned int sar_height;
  108. char video_signal_type_present_flag;
  109. unsigned int video_format;
  110. char video_full_range_flag;
  111. char colour_description_present_flag;
  112. unsigned int colour_primaries;
  113. unsigned int transfer_characteristics;
  114. unsigned int matrix_coeffs;
  115. // Oddments.
  116. char uniform_spacing_flag;
  117. char output_flag_present_flag;
  118. char cabac_init_present_flag;
  119. unsigned int num_extra_slice_header_bits;
  120. char lists_modification_present_flag;
  121. char pps_slice_chroma_qp_offsets_present_flag;
  122. char pps_slice_chroma_offset_list_enabled_flag;
  123. } VAAPIEncodeH265MiscSequenceParams;
  124. // This structure contains all possibly-useful per-slice syntax elements
  125. // which are not already contained in the various VAAPI structures.
  126. typedef struct VAAPIEncodeH265MiscSliceParams {
  127. // Slice segments.
  128. char first_slice_segment_in_pic_flag;
  129. // Short-term reference picture sets.
  130. char short_term_ref_pic_set_sps_flag;
  131. unsigned int short_term_ref_pic_idx;
  132. VAAPIEncodeH265STRPS st_ref_pic_set;
  133. // Deblocking filter.
  134. char deblocking_filter_override_flag;
  135. // Oddments.
  136. char slice_reserved_flag[8];
  137. char no_output_of_prior_pics_flag;
  138. char pic_output_flag;
  139. } VAAPIEncodeH265MiscSliceParams;
  140. typedef struct VAAPIEncodeH265Slice {
  141. VAAPIEncodeH265MiscSliceParams misc_slice_params;
  142. int64_t pic_order_cnt;
  143. } VAAPIEncodeH265Slice;
  144. typedef struct VAAPIEncodeH265Context {
  145. VAAPIEncodeH265MiscSequenceParams misc_sequence_params;
  146. unsigned int ctu_width;
  147. unsigned int ctu_height;
  148. int fixed_qp_idr;
  149. int fixed_qp_p;
  150. int fixed_qp_b;
  151. int64_t last_idr_frame;
  152. // Rate control configuration.
  153. struct {
  154. VAEncMiscParameterBuffer misc;
  155. VAEncMiscParameterRateControl rc;
  156. } rc_params;
  157. struct {
  158. VAEncMiscParameterBuffer misc;
  159. VAEncMiscParameterHRD hrd;
  160. } hrd_params;
  161. } VAAPIEncodeH265Context;
  162. typedef struct VAAPIEncodeH265Options {
  163. int qp;
  164. } VAAPIEncodeH265Options;
  165. #define vseq_var(name) vseq->name, name
  166. #define vseq_field(name) vseq->seq_fields.bits.name, name
  167. #define vpic_var(name) vpic->name, name
  168. #define vpic_field(name) vpic->pic_fields.bits.name, name
  169. #define vslice_var(name) vslice->name, name
  170. #define vslice_field(name) vslice->slice_fields.bits.name, name
  171. #define mseq_var(name) mseq->name, name
  172. #define mslice_var(name) mslice->name, name
  173. #define mstrps_var(name) mstrps->name, name
  174. static void vaapi_encode_h265_write_nal_unit_header(PutBitContext *pbc,
  175. int nal_unit_type)
  176. {
  177. u(1, 0, forbidden_zero_bit);
  178. u(6, nal_unit_type, nal_unit_type);
  179. u(6, 0, nuh_layer_id);
  180. u(3, 1, nuh_temporal_id_plus1);
  181. }
  182. static void vaapi_encode_h265_write_rbsp_trailing_bits(PutBitContext *pbc)
  183. {
  184. u(1, 1, rbsp_stop_one_bit);
  185. while (put_bits_count(pbc) & 7)
  186. u(1, 0, rbsp_alignment_zero_bit);
  187. }
  188. static void vaapi_encode_h265_write_profile_tier_level(PutBitContext *pbc,
  189. VAAPIEncodeContext *ctx)
  190. {
  191. VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
  192. VAAPIEncodeH265Context *priv = ctx->priv_data;
  193. VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
  194. int j;
  195. if (1) {
  196. u(2, 0, general_profile_space);
  197. u(1, vseq_var(general_tier_flag));
  198. u(5, vseq_var(general_profile_idc));
  199. for (j = 0; j < 32; j++) {
  200. u(1, mseq_var(general_profile_compatibility_flag[j]));
  201. }
  202. u(1, mseq_var(general_progressive_source_flag));
  203. u(1, mseq_var(general_interlaced_source_flag));
  204. u(1, mseq_var(general_non_packed_constraint_flag));
  205. u(1, mseq_var(general_frame_only_constraint_flag));
  206. if (0) {
  207. // Not main profile.
  208. // Lots of extra constraint flags.
  209. } else {
  210. // put_bits only handles up to 31 bits.
  211. u(23, 0, general_reserved_zero_43bits);
  212. u(20, 0, general_reserved_zero_43bits);
  213. }
  214. if (vseq->general_profile_idc >= 1 && vseq->general_profile_idc <= 5) {
  215. u(1, mseq_var(general_inbld_flag));
  216. } else {
  217. u(1, 0, general_reserved_zero_bit);
  218. }
  219. }
  220. u(8, vseq_var(general_level_idc));
  221. // No sublayers.
  222. }
  223. static void vaapi_encode_h265_write_vps(PutBitContext *pbc,
  224. VAAPIEncodeContext *ctx)
  225. {
  226. VAAPIEncodeH265Context *priv = ctx->priv_data;
  227. VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
  228. int i, j;
  229. vaapi_encode_h265_write_nal_unit_header(pbc, HEVC_NAL_VPS);
  230. u(4, mseq->video_parameter_set_id, vps_video_parameter_set_id);
  231. u(1, 1, vps_base_layer_internal_flag);
  232. u(1, 1, vps_base_layer_available_flag);
  233. u(6, mseq_var(vps_max_layers_minus1));
  234. u(3, mseq_var(vps_max_sub_layers_minus1));
  235. u(1, mseq_var(vps_temporal_id_nesting_flag));
  236. u(16, 0xffff, vps_reserved_0xffff_16bits);
  237. vaapi_encode_h265_write_profile_tier_level(pbc, ctx);
  238. u(1, mseq_var(vps_sub_layer_ordering_info_present_flag));
  239. for (i = (mseq->vps_sub_layer_ordering_info_present_flag ?
  240. 0 : mseq->vps_max_sub_layers_minus1);
  241. i <= mseq->vps_max_sub_layers_minus1; i++) {
  242. ue(mseq_var(vps_max_dec_pic_buffering_minus1[i]));
  243. ue(mseq_var(vps_max_num_reorder_pics[i]));
  244. ue(mseq_var(vps_max_latency_increase_plus1[i]));
  245. }
  246. u(6, mseq_var(vps_max_layer_id));
  247. ue(mseq_var(vps_num_layer_sets_minus1));
  248. for (i = 1; i <= mseq->vps_num_layer_sets_minus1; i++) {
  249. for (j = 0; j < mseq->vps_max_layer_id; j++)
  250. u(1, mseq_var(layer_id_included_flag[i][j]));
  251. }
  252. u(1, mseq_var(vps_timing_info_present_flag));
  253. if (mseq->vps_timing_info_present_flag) {
  254. u(1, 0, put_bits_hack_zero_bit);
  255. u(31, mseq_var(vps_num_units_in_tick));
  256. u(1, 0, put_bits_hack_zero_bit);
  257. u(31, mseq_var(vps_time_scale));
  258. u(1, mseq_var(vps_poc_proportional_to_timing_flag));
  259. if (mseq->vps_poc_proportional_to_timing_flag) {
  260. ue(mseq_var(vps_num_ticks_poc_diff_minus1));
  261. }
  262. ue(0, vps_num_hrd_parameters);
  263. }
  264. u(1, 0, vps_extension_flag);
  265. vaapi_encode_h265_write_rbsp_trailing_bits(pbc);
  266. }
  267. static void vaapi_encode_h265_write_st_ref_pic_set(PutBitContext *pbc,
  268. int st_rps_idx,
  269. VAAPIEncodeH265STRPS *mstrps)
  270. {
  271. int i;
  272. if (st_rps_idx != 0)
  273. u(1, mstrps_var(inter_ref_pic_set_prediction_flag));
  274. if (mstrps->inter_ref_pic_set_prediction_flag) {
  275. av_assert0(0 && "inter ref pic set prediction not supported");
  276. } else {
  277. ue(mstrps_var(num_negative_pics));
  278. ue(mstrps_var(num_positive_pics));
  279. for (i = 0; i < mstrps->num_negative_pics; i++) {
  280. ue(mstrps_var(delta_poc_s0_minus1[i]));
  281. u(1, mstrps_var(used_by_curr_pic_s0_flag[i]));
  282. }
  283. for (i = 0; i < mstrps->num_positive_pics; i++) {
  284. ue(mstrps_var(delta_poc_s1_minus1[i]));
  285. u(1, mstrps_var(used_by_curr_pic_s1_flag[i]));
  286. }
  287. }
  288. }
  289. static void vaapi_encode_h265_write_vui_parameters(PutBitContext *pbc,
  290. VAAPIEncodeContext *ctx)
  291. {
  292. VAAPIEncodeH265Context *priv = ctx->priv_data;
  293. VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
  294. u(1, mseq_var(aspect_ratio_info_present_flag));
  295. if (mseq->aspect_ratio_info_present_flag) {
  296. u(8, mseq_var(aspect_ratio_idc));
  297. if (mseq->aspect_ratio_idc == 255) {
  298. u(16, mseq_var(sar_width));
  299. u(16, mseq_var(sar_height));
  300. }
  301. }
  302. u(1, 0, overscan_info_present_flag);
  303. u(1, mseq_var(video_signal_type_present_flag));
  304. if (mseq->video_signal_type_present_flag) {
  305. u(3, mseq_var(video_format));
  306. u(1, mseq_var(video_full_range_flag));
  307. u(1, mseq_var(colour_description_present_flag));
  308. if (mseq->colour_description_present_flag) {
  309. u(8, mseq_var(colour_primaries));
  310. u(8, mseq_var(transfer_characteristics));
  311. u(8, mseq_var(matrix_coeffs));
  312. }
  313. }
  314. u(1, 0, chroma_loc_info_present_flag);
  315. u(1, 0, neutral_chroma_indication_flag);
  316. u(1, 0, field_seq_flag);
  317. u(1, 0, frame_field_info_present_flag);
  318. u(1, 0, default_display_window_flag);
  319. u(1, 0, vui_timing_info_present_flag);
  320. u(1, 0, bitstream_restriction_flag_flag);
  321. }
  322. static void vaapi_encode_h265_write_sps(PutBitContext *pbc,
  323. VAAPIEncodeContext *ctx)
  324. {
  325. VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
  326. VAAPIEncodeH265Context *priv = ctx->priv_data;
  327. VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
  328. int i;
  329. vaapi_encode_h265_write_nal_unit_header(pbc, HEVC_NAL_SPS);
  330. u(4, mseq->video_parameter_set_id, sps_video_parameter_set_id);
  331. u(3, mseq_var(sps_max_sub_layers_minus1));
  332. u(1, mseq_var(sps_temporal_id_nesting_flag));
  333. vaapi_encode_h265_write_profile_tier_level(pbc, ctx);
  334. ue(mseq->seq_parameter_set_id, sps_seq_parameter_set_id);
  335. ue(vseq_field(chroma_format_idc));
  336. if (vseq->seq_fields.bits.chroma_format_idc == 3)
  337. u(1, 0, separate_colour_plane_flag);
  338. ue(vseq_var(pic_width_in_luma_samples));
  339. ue(vseq_var(pic_height_in_luma_samples));
  340. u(1, mseq_var(conformance_window_flag));
  341. if (mseq->conformance_window_flag) {
  342. ue(mseq_var(conf_win_left_offset));
  343. ue(mseq_var(conf_win_right_offset));
  344. ue(mseq_var(conf_win_top_offset));
  345. ue(mseq_var(conf_win_bottom_offset));
  346. }
  347. ue(vseq_field(bit_depth_luma_minus8));
  348. ue(vseq_field(bit_depth_chroma_minus8));
  349. ue(mseq_var(log2_max_pic_order_cnt_lsb_minus4));
  350. u(1, mseq_var(sps_sub_layer_ordering_info_present_flag));
  351. for (i = (mseq->sps_sub_layer_ordering_info_present_flag ?
  352. 0 : mseq->sps_max_sub_layers_minus1);
  353. i <= mseq->sps_max_sub_layers_minus1; i++) {
  354. ue(mseq_var(sps_max_dec_pic_buffering_minus1[i]));
  355. ue(mseq_var(sps_max_num_reorder_pics[i]));
  356. ue(mseq_var(sps_max_latency_increase_plus1[i]));
  357. }
  358. ue(vseq_var(log2_min_luma_coding_block_size_minus3));
  359. ue(vseq_var(log2_diff_max_min_luma_coding_block_size));
  360. ue(vseq_var(log2_min_transform_block_size_minus2));
  361. ue(vseq_var(log2_diff_max_min_transform_block_size));
  362. ue(vseq_var(max_transform_hierarchy_depth_inter));
  363. ue(vseq_var(max_transform_hierarchy_depth_intra));
  364. u(1, vseq_field(scaling_list_enabled_flag));
  365. if (vseq->seq_fields.bits.scaling_list_enabled_flag) {
  366. u(1, 0, sps_scaling_list_data_present_flag);
  367. }
  368. u(1, vseq_field(amp_enabled_flag));
  369. u(1, vseq_field(sample_adaptive_offset_enabled_flag));
  370. u(1, vseq_field(pcm_enabled_flag));
  371. if (vseq->seq_fields.bits.pcm_enabled_flag) {
  372. u(4, vseq_var(pcm_sample_bit_depth_luma_minus1));
  373. u(4, vseq_var(pcm_sample_bit_depth_chroma_minus1));
  374. ue(vseq_var(log2_min_pcm_luma_coding_block_size_minus3));
  375. ue(vseq->log2_max_pcm_luma_coding_block_size_minus3 -
  376. vseq->log2_min_pcm_luma_coding_block_size_minus3,
  377. log2_diff_max_min_pcm_luma_coding_block_size);
  378. u(1, vseq_field(pcm_loop_filter_disabled_flag));
  379. }
  380. ue(mseq_var(num_short_term_ref_pic_sets));
  381. for (i = 0; i < mseq->num_short_term_ref_pic_sets; i++)
  382. vaapi_encode_h265_write_st_ref_pic_set(pbc, i,
  383. &mseq->st_ref_pic_set[i]);
  384. u(1, mseq_var(long_term_ref_pics_present_flag));
  385. if (mseq->long_term_ref_pics_present_flag) {
  386. ue(0, num_long_term_ref_pics_sps);
  387. }
  388. u(1, vseq_field(sps_temporal_mvp_enabled_flag));
  389. u(1, vseq_field(strong_intra_smoothing_enabled_flag));
  390. u(1, mseq_var(vui_parameters_present_flag));
  391. if (mseq->vui_parameters_present_flag) {
  392. vaapi_encode_h265_write_vui_parameters(pbc, ctx);
  393. }
  394. u(1, 0, sps_extension_present_flag);
  395. vaapi_encode_h265_write_rbsp_trailing_bits(pbc);
  396. }
  397. static void vaapi_encode_h265_write_pps(PutBitContext *pbc,
  398. VAAPIEncodeContext *ctx)
  399. {
  400. VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
  401. VAAPIEncodeH265Context *priv = ctx->priv_data;
  402. VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
  403. int i;
  404. vaapi_encode_h265_write_nal_unit_header(pbc, HEVC_NAL_PPS);
  405. ue(vpic->slice_pic_parameter_set_id, pps_pic_parameter_set_id);
  406. ue(mseq->seq_parameter_set_id, pps_seq_parameter_set_id);
  407. u(1, vpic_field(dependent_slice_segments_enabled_flag));
  408. u(1, mseq_var(output_flag_present_flag));
  409. u(3, mseq_var(num_extra_slice_header_bits));
  410. u(1, vpic_field(sign_data_hiding_enabled_flag));
  411. u(1, mseq_var(cabac_init_present_flag));
  412. ue(vpic_var(num_ref_idx_l0_default_active_minus1));
  413. ue(vpic_var(num_ref_idx_l1_default_active_minus1));
  414. se(vpic->pic_init_qp - 26, init_qp_minus26);
  415. u(1, vpic_field(constrained_intra_pred_flag));
  416. u(1, vpic_field(transform_skip_enabled_flag));
  417. u(1, vpic_field(cu_qp_delta_enabled_flag));
  418. if (vpic->pic_fields.bits.cu_qp_delta_enabled_flag)
  419. ue(vpic_var(diff_cu_qp_delta_depth));
  420. se(vpic_var(pps_cb_qp_offset));
  421. se(vpic_var(pps_cr_qp_offset));
  422. u(1, mseq_var(pps_slice_chroma_qp_offsets_present_flag));
  423. u(1, vpic_field(weighted_pred_flag));
  424. u(1, vpic_field(weighted_bipred_flag));
  425. u(1, vpic_field(transquant_bypass_enabled_flag));
  426. u(1, vpic_field(tiles_enabled_flag));
  427. u(1, vpic_field(entropy_coding_sync_enabled_flag));
  428. if (vpic->pic_fields.bits.tiles_enabled_flag) {
  429. ue(vpic_var(num_tile_columns_minus1));
  430. ue(vpic_var(num_tile_rows_minus1));
  431. u(1, mseq_var(uniform_spacing_flag));
  432. if (!mseq->uniform_spacing_flag) {
  433. for (i = 0; i < vpic->num_tile_columns_minus1; i++)
  434. ue(vpic_var(column_width_minus1[i]));
  435. for (i = 0; i < vpic->num_tile_rows_minus1; i++)
  436. ue(vpic_var(row_height_minus1[i]));
  437. }
  438. u(1, vpic_field(loop_filter_across_tiles_enabled_flag));
  439. }
  440. u(1, vpic_field(pps_loop_filter_across_slices_enabled_flag));
  441. u(1, mseq_var(deblocking_filter_control_present_flag));
  442. if (mseq->deblocking_filter_control_present_flag) {
  443. u(1, mseq_var(deblocking_filter_override_enabled_flag));
  444. u(1, mseq_var(pps_deblocking_filter_disabled_flag));
  445. if (!mseq->pps_deblocking_filter_disabled_flag) {
  446. se(mseq_var(pps_beta_offset_div2));
  447. se(mseq_var(pps_tc_offset_div2));
  448. }
  449. }
  450. u(1, 0, pps_scaling_list_data_present_flag);
  451. // No scaling list data.
  452. u(1, mseq_var(lists_modification_present_flag));
  453. ue(vpic_var(log2_parallel_merge_level_minus2));
  454. u(1, 0, slice_segment_header_extension_present_flag);
  455. u(1, 0, pps_extension_present_flag);
  456. vaapi_encode_h265_write_rbsp_trailing_bits(pbc);
  457. }
  458. static void vaapi_encode_h265_write_slice_header2(PutBitContext *pbc,
  459. VAAPIEncodeContext *ctx,
  460. VAAPIEncodePicture *pic,
  461. VAAPIEncodeSlice *slice)
  462. {
  463. VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
  464. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  465. VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
  466. VAAPIEncodeH265Context *priv = ctx->priv_data;
  467. VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
  468. VAAPIEncodeH265Slice *pslice = slice->priv_data;
  469. VAAPIEncodeH265MiscSliceParams *mslice = &pslice->misc_slice_params;
  470. int i;
  471. vaapi_encode_h265_write_nal_unit_header(pbc, vpic->nal_unit_type);
  472. u(1, mslice_var(first_slice_segment_in_pic_flag));
  473. if (vpic->nal_unit_type >= HEVC_NAL_BLA_W_LP &&
  474. vpic->nal_unit_type <= 23)
  475. u(1, mslice_var(no_output_of_prior_pics_flag));
  476. ue(vslice_var(slice_pic_parameter_set_id));
  477. if (!mslice->first_slice_segment_in_pic_flag) {
  478. if (vpic->pic_fields.bits.dependent_slice_segments_enabled_flag)
  479. u(1, vslice_field(dependent_slice_segment_flag));
  480. u(av_log2((priv->ctu_width * priv->ctu_height) - 1) + 1,
  481. vslice_var(slice_segment_address));
  482. }
  483. if (!vslice->slice_fields.bits.dependent_slice_segment_flag) {
  484. for (i = 0; i < mseq->num_extra_slice_header_bits; i++)
  485. u(1, mslice_var(slice_reserved_flag[i]));
  486. ue(vslice_var(slice_type));
  487. if (mseq->output_flag_present_flag)
  488. u(1, 1, pic_output_flag);
  489. if (vseq->seq_fields.bits.separate_colour_plane_flag)
  490. u(2, vslice_field(colour_plane_id));
  491. if (vpic->nal_unit_type != HEVC_NAL_IDR_W_RADL &&
  492. vpic->nal_unit_type != HEVC_NAL_IDR_N_LP) {
  493. u(4 + mseq->log2_max_pic_order_cnt_lsb_minus4,
  494. (pslice->pic_order_cnt &
  495. ((1 << (mseq->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1)),
  496. slice_pic_order_cnt_lsb);
  497. u(1, mslice_var(short_term_ref_pic_set_sps_flag));
  498. if (!mslice->short_term_ref_pic_set_sps_flag) {
  499. vaapi_encode_h265_write_st_ref_pic_set(pbc, mseq->num_short_term_ref_pic_sets,
  500. &mslice->st_ref_pic_set);
  501. } else if (mseq->num_short_term_ref_pic_sets > 1) {
  502. u(av_log2(mseq->num_short_term_ref_pic_sets - 1) + 1,
  503. mslice_var(short_term_ref_pic_idx));
  504. }
  505. if (mseq->long_term_ref_pics_present_flag) {
  506. av_assert0(0);
  507. }
  508. }
  509. if (vseq->seq_fields.bits.sps_temporal_mvp_enabled_flag) {
  510. u(1, vslice_field(slice_temporal_mvp_enabled_flag));
  511. }
  512. if (vseq->seq_fields.bits.sample_adaptive_offset_enabled_flag) {
  513. u(1, vslice_field(slice_sao_luma_flag));
  514. if (!vseq->seq_fields.bits.separate_colour_plane_flag &&
  515. vseq->seq_fields.bits.chroma_format_idc != 0) {
  516. u(1, vslice_field(slice_sao_chroma_flag));
  517. }
  518. }
  519. if (vslice->slice_type == HEVC_SLICE_P || vslice->slice_type == HEVC_SLICE_B) {
  520. u(1, vslice_field(num_ref_idx_active_override_flag));
  521. if (vslice->slice_fields.bits.num_ref_idx_active_override_flag) {
  522. ue(vslice_var(num_ref_idx_l0_active_minus1));
  523. if (vslice->slice_type == HEVC_SLICE_B) {
  524. ue(vslice_var(num_ref_idx_l1_active_minus1));
  525. }
  526. }
  527. if (mseq->lists_modification_present_flag) {
  528. av_assert0(0);
  529. // ref_pic_lists_modification()
  530. }
  531. if (vslice->slice_type == HEVC_SLICE_B) {
  532. u(1, vslice_field(mvd_l1_zero_flag));
  533. }
  534. if (mseq->cabac_init_present_flag) {
  535. u(1, vslice_field(cabac_init_flag));
  536. }
  537. if (vslice->slice_fields.bits.slice_temporal_mvp_enabled_flag) {
  538. if (vslice->slice_type == HEVC_SLICE_B)
  539. u(1, vslice_field(collocated_from_l0_flag));
  540. ue(vpic->collocated_ref_pic_index, collocated_ref_idx);
  541. }
  542. if ((vpic->pic_fields.bits.weighted_pred_flag &&
  543. vslice->slice_type == HEVC_SLICE_P) ||
  544. (vpic->pic_fields.bits.weighted_bipred_flag &&
  545. vslice->slice_type == HEVC_SLICE_B)) {
  546. av_assert0(0);
  547. // pred_weight_table()
  548. }
  549. ue(5 - vslice->max_num_merge_cand, five_minus_max_num_merge_cand);
  550. }
  551. se(vslice_var(slice_qp_delta));
  552. if (mseq->pps_slice_chroma_qp_offsets_present_flag) {
  553. se(vslice_var(slice_cb_qp_offset));
  554. se(vslice_var(slice_cr_qp_offset));
  555. }
  556. if (mseq->pps_slice_chroma_offset_list_enabled_flag) {
  557. u(1, 0, cu_chroma_qp_offset_enabled_flag);
  558. }
  559. if (mseq->deblocking_filter_override_enabled_flag) {
  560. u(1, mslice_var(deblocking_filter_override_flag));
  561. }
  562. if (mslice->deblocking_filter_override_flag) {
  563. u(1, vslice_field(slice_deblocking_filter_disabled_flag));
  564. if (!vslice->slice_fields.bits.slice_deblocking_filter_disabled_flag) {
  565. se(vslice_var(slice_beta_offset_div2));
  566. se(vslice_var(slice_tc_offset_div2));
  567. }
  568. }
  569. if (vpic->pic_fields.bits.pps_loop_filter_across_slices_enabled_flag &&
  570. (vslice->slice_fields.bits.slice_sao_luma_flag ||
  571. vslice->slice_fields.bits.slice_sao_chroma_flag ||
  572. vslice->slice_fields.bits.slice_deblocking_filter_disabled_flag)) {
  573. u(1, vslice_field(slice_loop_filter_across_slices_enabled_flag));
  574. }
  575. if (vpic->pic_fields.bits.tiles_enabled_flag ||
  576. vpic->pic_fields.bits.entropy_coding_sync_enabled_flag) {
  577. // num_entry_point_offsets
  578. }
  579. if (0) {
  580. // slice_segment_header_extension_length
  581. }
  582. }
  583. u(1, 1, alignment_bit_equal_to_one);
  584. while (put_bits_count(pbc) & 7)
  585. u(1, 0, alignment_bit_equal_to_zero);
  586. }
  587. static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
  588. char *data, size_t *data_len)
  589. {
  590. VAAPIEncodeContext *ctx = avctx->priv_data;
  591. PutBitContext pbc;
  592. char tmp[256];
  593. int err;
  594. size_t nal_len, bit_len, bit_pos, next_len;
  595. bit_len = *data_len;
  596. bit_pos = 0;
  597. init_put_bits(&pbc, tmp, sizeof(tmp));
  598. vaapi_encode_h265_write_vps(&pbc, ctx);
  599. nal_len = put_bits_count(&pbc);
  600. flush_put_bits(&pbc);
  601. next_len = bit_len - bit_pos;
  602. err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
  603. &next_len,
  604. tmp, nal_len);
  605. if (err < 0)
  606. return err;
  607. bit_pos += next_len;
  608. init_put_bits(&pbc, tmp, sizeof(tmp));
  609. vaapi_encode_h265_write_sps(&pbc, ctx);
  610. nal_len = put_bits_count(&pbc);
  611. flush_put_bits(&pbc);
  612. next_len = bit_len - bit_pos;
  613. err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
  614. &next_len,
  615. tmp, nal_len);
  616. if (err < 0)
  617. return err;
  618. bit_pos += next_len;
  619. init_put_bits(&pbc, tmp, sizeof(tmp));
  620. vaapi_encode_h265_write_pps(&pbc, ctx);
  621. nal_len = put_bits_count(&pbc);
  622. flush_put_bits(&pbc);
  623. next_len = bit_len - bit_pos;
  624. err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
  625. &next_len,
  626. tmp, nal_len);
  627. if (err < 0)
  628. return err;
  629. bit_pos += next_len;
  630. *data_len = bit_pos;
  631. return 0;
  632. }
  633. static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
  634. VAAPIEncodePicture *pic,
  635. VAAPIEncodeSlice *slice,
  636. char *data, size_t *data_len)
  637. {
  638. VAAPIEncodeContext *ctx = avctx->priv_data;
  639. PutBitContext pbc;
  640. char tmp[256];
  641. size_t header_len;
  642. init_put_bits(&pbc, tmp, sizeof(tmp));
  643. vaapi_encode_h265_write_slice_header2(&pbc, ctx, pic, slice);
  644. header_len = put_bits_count(&pbc);
  645. flush_put_bits(&pbc);
  646. return ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data, data_len,
  647. tmp, header_len);
  648. }
  649. static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
  650. {
  651. VAAPIEncodeContext *ctx = avctx->priv_data;
  652. VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
  653. VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
  654. VAAPIEncodeH265Context *priv = ctx->priv_data;
  655. VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
  656. int i;
  657. {
  658. // general_profile_space == 0.
  659. vseq->general_profile_idc = 1; // Main profile (ctx->codec_profile?)
  660. vseq->general_tier_flag = 0;
  661. vseq->general_level_idc = avctx->level * 3;
  662. vseq->intra_period = 0;
  663. vseq->intra_idr_period = 0;
  664. vseq->ip_period = 0;
  665. vseq->pic_width_in_luma_samples = ctx->surface_width;
  666. vseq->pic_height_in_luma_samples = ctx->surface_height;
  667. vseq->seq_fields.bits.chroma_format_idc = 1; // 4:2:0.
  668. vseq->seq_fields.bits.separate_colour_plane_flag = 0;
  669. vseq->seq_fields.bits.bit_depth_luma_minus8 =
  670. avctx->profile == FF_PROFILE_HEVC_MAIN_10 ? 2 : 0;
  671. vseq->seq_fields.bits.bit_depth_chroma_minus8 =
  672. avctx->profile == FF_PROFILE_HEVC_MAIN_10 ? 2 : 0;
  673. // Other misc flags all zero.
  674. // These have to come from the capabilities of the encoder. We have
  675. // no way to query it, so just hardcode ones which worked for me...
  676. // CTB size from 8x8 to 32x32.
  677. vseq->log2_min_luma_coding_block_size_minus3 = 0;
  678. vseq->log2_diff_max_min_luma_coding_block_size = 2;
  679. // Transform size from 4x4 to 32x32.
  680. vseq->log2_min_transform_block_size_minus2 = 0;
  681. vseq->log2_diff_max_min_transform_block_size = 3;
  682. // Full transform hierarchy allowed (2-5).
  683. vseq->max_transform_hierarchy_depth_inter = 3;
  684. vseq->max_transform_hierarchy_depth_intra = 3;
  685. vseq->vui_parameters_present_flag = 0;
  686. vseq->bits_per_second = avctx->bit_rate;
  687. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  688. vseq->vui_num_units_in_tick = avctx->framerate.den;
  689. vseq->vui_time_scale = avctx->framerate.num;
  690. } else {
  691. vseq->vui_num_units_in_tick = avctx->time_base.num;
  692. vseq->vui_time_scale = avctx->time_base.den;
  693. }
  694. vseq->intra_period = avctx->gop_size;
  695. vseq->intra_idr_period = avctx->gop_size;
  696. vseq->ip_period = ctx->b_per_p + 1;
  697. }
  698. {
  699. vpic->decoded_curr_pic.picture_id = VA_INVALID_ID;
  700. vpic->decoded_curr_pic.flags = VA_PICTURE_HEVC_INVALID;
  701. for (i = 0; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
  702. vpic->reference_frames[i].picture_id = VA_INVALID_ID;
  703. vpic->reference_frames[i].flags = VA_PICTURE_HEVC_INVALID;
  704. }
  705. vpic->collocated_ref_pic_index = 0xff;
  706. vpic->last_picture = 0;
  707. vpic->pic_init_qp = priv->fixed_qp_idr;
  708. vpic->diff_cu_qp_delta_depth = 0;
  709. vpic->pps_cb_qp_offset = 0;
  710. vpic->pps_cr_qp_offset = 0;
  711. // tiles_enabled_flag == 0, so ignore num_tile_(rows|columns)_minus1.
  712. vpic->log2_parallel_merge_level_minus2 = 0;
  713. // No limit on size.
  714. vpic->ctu_max_bitsize_allowed = 0;
  715. vpic->num_ref_idx_l0_default_active_minus1 = 0;
  716. vpic->num_ref_idx_l1_default_active_minus1 = 0;
  717. vpic->slice_pic_parameter_set_id = 0;
  718. vpic->pic_fields.bits.screen_content_flag = 0;
  719. vpic->pic_fields.bits.enable_gpu_weighted_prediction = 0;
  720. // Per-CU QP changes are required for non-constant-QP modes.
  721. vpic->pic_fields.bits.cu_qp_delta_enabled_flag =
  722. ctx->va_rc_mode != VA_RC_CQP;
  723. }
  724. {
  725. mseq->video_parameter_set_id = 5;
  726. mseq->seq_parameter_set_id = 5;
  727. mseq->vps_max_layers_minus1 = 0;
  728. mseq->vps_max_sub_layers_minus1 = 0;
  729. mseq->vps_temporal_id_nesting_flag = 1;
  730. mseq->sps_max_sub_layers_minus1 = 0;
  731. mseq->sps_temporal_id_nesting_flag = 1;
  732. for (i = 0; i < 32; i++) {
  733. mseq->general_profile_compatibility_flag[i] =
  734. (i == vseq->general_profile_idc);
  735. }
  736. mseq->general_progressive_source_flag = 1;
  737. mseq->general_interlaced_source_flag = 0;
  738. mseq->general_non_packed_constraint_flag = 0;
  739. mseq->general_frame_only_constraint_flag = 1;
  740. mseq->general_inbld_flag = 0;
  741. mseq->log2_max_pic_order_cnt_lsb_minus4 = 8;
  742. mseq->vps_sub_layer_ordering_info_present_flag = 0;
  743. mseq->vps_max_dec_pic_buffering_minus1[0] = (avctx->max_b_frames > 0) + 1;
  744. mseq->vps_max_num_reorder_pics[0] = (avctx->max_b_frames > 0);
  745. mseq->vps_max_latency_increase_plus1[0] = 0;
  746. mseq->sps_sub_layer_ordering_info_present_flag = 0;
  747. mseq->sps_max_dec_pic_buffering_minus1[0] = (avctx->max_b_frames > 0) + 1;
  748. mseq->sps_max_num_reorder_pics[0] = (avctx->max_b_frames > 0);
  749. mseq->sps_max_latency_increase_plus1[0] = 0;
  750. mseq->vps_timing_info_present_flag = 1;
  751. mseq->vps_num_units_in_tick = avctx->time_base.num;
  752. mseq->vps_time_scale = avctx->time_base.den;
  753. mseq->vps_poc_proportional_to_timing_flag = 1;
  754. mseq->vps_num_ticks_poc_diff_minus1 = 0;
  755. if (avctx->width != ctx->surface_width ||
  756. avctx->height != ctx->surface_height) {
  757. mseq->conformance_window_flag = 1;
  758. mseq->conf_win_left_offset = 0;
  759. mseq->conf_win_right_offset =
  760. (ctx->surface_width - avctx->width) / 2;
  761. mseq->conf_win_top_offset = 0;
  762. mseq->conf_win_bottom_offset =
  763. (ctx->surface_height - avctx->height) / 2;
  764. } else {
  765. mseq->conformance_window_flag = 0;
  766. }
  767. mseq->num_short_term_ref_pic_sets = 0;
  768. // STRPSs should ideally be here rather than repeated in each slice.
  769. mseq->vui_parameters_present_flag = 1;
  770. if (avctx->sample_aspect_ratio.num != 0) {
  771. mseq->aspect_ratio_info_present_flag = 1;
  772. if (avctx->sample_aspect_ratio.num ==
  773. avctx->sample_aspect_ratio.den) {
  774. mseq->aspect_ratio_idc = 1;
  775. } else {
  776. mseq->aspect_ratio_idc = 255; // Extended SAR.
  777. mseq->sar_width = avctx->sample_aspect_ratio.num;
  778. mseq->sar_height = avctx->sample_aspect_ratio.den;
  779. }
  780. }
  781. if (1) {
  782. // Should this be conditional on some of these being set?
  783. mseq->video_signal_type_present_flag = 1;
  784. mseq->video_format = 5; // Unspecified.
  785. mseq->video_full_range_flag = 0;
  786. mseq->colour_description_present_flag = 1;
  787. mseq->colour_primaries = avctx->color_primaries;
  788. mseq->transfer_characteristics = avctx->color_trc;
  789. mseq->matrix_coeffs = avctx->colorspace;
  790. }
  791. }
  792. return 0;
  793. }
  794. static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
  795. VAAPIEncodePicture *pic)
  796. {
  797. VAAPIEncodeContext *ctx = avctx->priv_data;
  798. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  799. VAAPIEncodeH265Context *priv = ctx->priv_data;
  800. int i;
  801. if (pic->type == PICTURE_TYPE_IDR) {
  802. av_assert0(pic->display_order == pic->encode_order);
  803. priv->last_idr_frame = pic->display_order;
  804. } else {
  805. av_assert0(pic->encode_order > priv->last_idr_frame);
  806. // Display order need not be if we have RA[SD]L pictures, though.
  807. }
  808. vpic->decoded_curr_pic.picture_id = pic->recon_surface;
  809. vpic->decoded_curr_pic.pic_order_cnt =
  810. pic->display_order - priv->last_idr_frame;
  811. vpic->decoded_curr_pic.flags = 0;
  812. for (i = 0; i < pic->nb_refs; i++) {
  813. VAAPIEncodePicture *ref = pic->refs[i];
  814. av_assert0(ref);
  815. vpic->reference_frames[i].picture_id = ref->recon_surface;
  816. vpic->reference_frames[i].pic_order_cnt =
  817. ref->display_order - priv->last_idr_frame;
  818. vpic->reference_frames[i].flags =
  819. (ref->display_order < pic->display_order ?
  820. VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
  821. (ref->display_order > pic->display_order ?
  822. VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0);
  823. }
  824. for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
  825. vpic->reference_frames[i].picture_id = VA_INVALID_ID;
  826. vpic->reference_frames[i].flags = VA_PICTURE_HEVC_INVALID;
  827. }
  828. vpic->coded_buf = pic->output_buffer;
  829. switch (pic->type) {
  830. case PICTURE_TYPE_IDR:
  831. vpic->nal_unit_type = HEVC_NAL_IDR_W_RADL;
  832. vpic->pic_fields.bits.idr_pic_flag = 1;
  833. vpic->pic_fields.bits.coding_type = 1;
  834. vpic->pic_fields.bits.reference_pic_flag = 1;
  835. break;
  836. case PICTURE_TYPE_I:
  837. vpic->nal_unit_type = HEVC_NAL_TRAIL_R;
  838. vpic->pic_fields.bits.idr_pic_flag = 0;
  839. vpic->pic_fields.bits.coding_type = 1;
  840. vpic->pic_fields.bits.reference_pic_flag = 1;
  841. break;
  842. case PICTURE_TYPE_P:
  843. vpic->nal_unit_type = HEVC_NAL_TRAIL_R;
  844. vpic->pic_fields.bits.idr_pic_flag = 0;
  845. vpic->pic_fields.bits.coding_type = 2;
  846. vpic->pic_fields.bits.reference_pic_flag = 1;
  847. break;
  848. case PICTURE_TYPE_B:
  849. vpic->nal_unit_type = HEVC_NAL_TRAIL_R;
  850. vpic->pic_fields.bits.idr_pic_flag = 0;
  851. vpic->pic_fields.bits.coding_type = 3;
  852. vpic->pic_fields.bits.reference_pic_flag = 0;
  853. break;
  854. default:
  855. av_assert0(0 && "invalid picture type");
  856. }
  857. pic->nb_slices = 1;
  858. return 0;
  859. }
  860. static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
  861. VAAPIEncodePicture *pic,
  862. VAAPIEncodeSlice *slice)
  863. {
  864. VAAPIEncodeContext *ctx = avctx->priv_data;
  865. VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
  866. VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
  867. VAAPIEncodeH265Context *priv = ctx->priv_data;
  868. VAAPIEncodeH265Slice *pslice;
  869. VAAPIEncodeH265MiscSliceParams *mslice;
  870. int i;
  871. slice->priv_data = av_mallocz(sizeof(*pslice));
  872. if (!slice->priv_data)
  873. return AVERROR(ENOMEM);
  874. pslice = slice->priv_data;
  875. mslice = &pslice->misc_slice_params;
  876. // Currently we only support one slice per frame.
  877. vslice->slice_segment_address = 0;
  878. vslice->num_ctu_in_slice = priv->ctu_width * priv->ctu_height;
  879. switch (pic->type) {
  880. case PICTURE_TYPE_IDR:
  881. case PICTURE_TYPE_I:
  882. vslice->slice_type = HEVC_SLICE_I;
  883. break;
  884. case PICTURE_TYPE_P:
  885. vslice->slice_type = HEVC_SLICE_P;
  886. break;
  887. case PICTURE_TYPE_B:
  888. vslice->slice_type = HEVC_SLICE_B;
  889. break;
  890. default:
  891. av_assert0(0 && "invalid picture type");
  892. }
  893. vslice->slice_pic_parameter_set_id = vpic->slice_pic_parameter_set_id;
  894. pslice->pic_order_cnt = pic->display_order - priv->last_idr_frame;
  895. for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
  896. vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
  897. vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
  898. vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
  899. vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
  900. }
  901. av_assert0(pic->nb_refs <= 2);
  902. if (pic->nb_refs >= 1) {
  903. // Backward reference for P- or B-frame.
  904. av_assert0(pic->type == PICTURE_TYPE_P ||
  905. pic->type == PICTURE_TYPE_B);
  906. vslice->num_ref_idx_l0_active_minus1 = 0;
  907. vslice->ref_pic_list0[0] = vpic->reference_frames[0];
  908. }
  909. if (pic->nb_refs >= 2) {
  910. // Forward reference for B-frame.
  911. av_assert0(pic->type == PICTURE_TYPE_B);
  912. vslice->num_ref_idx_l1_active_minus1 = 0;
  913. vslice->ref_pic_list1[0] = vpic->reference_frames[1];
  914. }
  915. vslice->max_num_merge_cand = 5;
  916. if (pic->type == PICTURE_TYPE_B)
  917. vslice->slice_qp_delta = priv->fixed_qp_b - vpic->pic_init_qp;
  918. else if (pic->type == PICTURE_TYPE_P)
  919. vslice->slice_qp_delta = priv->fixed_qp_p - vpic->pic_init_qp;
  920. else
  921. vslice->slice_qp_delta = priv->fixed_qp_idr - vpic->pic_init_qp;
  922. vslice->slice_fields.bits.last_slice_of_pic_flag = 1;
  923. mslice->first_slice_segment_in_pic_flag = 1;
  924. if (pic->type == PICTURE_TYPE_IDR) {
  925. // No reference pictures.
  926. } else if (0) {
  927. mslice->short_term_ref_pic_set_sps_flag = 1;
  928. mslice->short_term_ref_pic_idx = 0;
  929. } else {
  930. VAAPIEncodePicture *st;
  931. int used;
  932. mslice->short_term_ref_pic_set_sps_flag = 0;
  933. mslice->st_ref_pic_set.inter_ref_pic_set_prediction_flag = 0;
  934. for (st = ctx->pic_start; st; st = st->next) {
  935. if (st->encode_order >= pic->encode_order) {
  936. // Not yet in DPB.
  937. continue;
  938. }
  939. used = 0;
  940. for (i = 0; i < pic->nb_refs; i++) {
  941. if (pic->refs[i] == st)
  942. used = 1;
  943. }
  944. if (!used) {
  945. // Currently true, but need not be.
  946. continue;
  947. }
  948. // This only works for one instance of each (delta_poc_sN_minus1
  949. // is relative to the previous frame in the list, not relative to
  950. // the current frame directly).
  951. if (st->display_order < pic->display_order) {
  952. i = mslice->st_ref_pic_set.num_negative_pics;
  953. mslice->st_ref_pic_set.delta_poc_s0_minus1[i] =
  954. pic->display_order - st->display_order - 1;
  955. mslice->st_ref_pic_set.used_by_curr_pic_s0_flag[i] = used;
  956. ++mslice->st_ref_pic_set.num_negative_pics;
  957. } else {
  958. i = mslice->st_ref_pic_set.num_positive_pics;
  959. mslice->st_ref_pic_set.delta_poc_s1_minus1[i] =
  960. st->display_order - pic->display_order - 1;
  961. mslice->st_ref_pic_set.used_by_curr_pic_s1_flag[i] = used;
  962. ++mslice->st_ref_pic_set.num_positive_pics;
  963. }
  964. }
  965. }
  966. return 0;
  967. }
  968. static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
  969. {
  970. VAAPIEncodeContext *ctx = avctx->priv_data;
  971. VAAPIEncodeH265Context *priv = ctx->priv_data;
  972. VAAPIEncodeH265Options *opt = ctx->codec_options;
  973. priv->ctu_width = FFALIGN(ctx->surface_width, 32) / 32;
  974. priv->ctu_height = FFALIGN(ctx->surface_height, 32) / 32;
  975. av_log(avctx, AV_LOG_VERBOSE, "Input %ux%u -> Surface %ux%u -> CTU %ux%u.\n",
  976. avctx->width, avctx->height, ctx->surface_width,
  977. ctx->surface_height, priv->ctu_width, priv->ctu_height);
  978. if (ctx->va_rc_mode == VA_RC_CQP) {
  979. priv->fixed_qp_p = opt->qp;
  980. if (avctx->i_quant_factor > 0.0)
  981. priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
  982. avctx->i_quant_offset) + 0.5);
  983. else
  984. priv->fixed_qp_idr = priv->fixed_qp_p;
  985. if (avctx->b_quant_factor > 0.0)
  986. priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
  987. avctx->b_quant_offset) + 0.5);
  988. else
  989. priv->fixed_qp_b = priv->fixed_qp_p;
  990. av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
  991. "%d / %d / %d for IDR- / P- / B-frames.\n",
  992. priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
  993. } else if (ctx->va_rc_mode == VA_RC_CBR ||
  994. ctx->va_rc_mode == VA_RC_VBR) {
  995. // These still need to be set for pic_init_qp/slice_qp_delta.
  996. priv->fixed_qp_idr = 30;
  997. priv->fixed_qp_p = 30;
  998. priv->fixed_qp_b = 30;
  999. av_log(avctx, AV_LOG_DEBUG, "Using %s-bitrate = %"PRId64" bps.\n",
  1000. ctx->va_rc_mode == VA_RC_CBR ? "constant" : "variable",
  1001. avctx->bit_rate);
  1002. } else {
  1003. av_assert0(0 && "Invalid RC mode.");
  1004. }
  1005. return 0;
  1006. }
  1007. static const VAAPIEncodeType vaapi_encode_type_h265 = {
  1008. .priv_data_size = sizeof(VAAPIEncodeH265Context),
  1009. .configure = &vaapi_encode_h265_configure,
  1010. .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
  1011. .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
  1012. .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
  1013. .init_picture_params = &vaapi_encode_h265_init_picture_params,
  1014. .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
  1015. .init_slice_params = &vaapi_encode_h265_init_slice_params,
  1016. .sequence_header_type = VAEncPackedHeaderSequence,
  1017. .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
  1018. .slice_header_type = VAEncPackedHeaderHEVC_Slice,
  1019. .write_slice_header = &vaapi_encode_h265_write_slice_header,
  1020. };
  1021. static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
  1022. {
  1023. VAAPIEncodeContext *ctx = avctx->priv_data;
  1024. ctx->codec = &vaapi_encode_type_h265;
  1025. switch (avctx->profile) {
  1026. case FF_PROFILE_HEVC_MAIN:
  1027. case FF_PROFILE_UNKNOWN:
  1028. ctx->va_profile = VAProfileHEVCMain;
  1029. ctx->va_rt_format = VA_RT_FORMAT_YUV420;
  1030. break;
  1031. case FF_PROFILE_HEVC_MAIN_10:
  1032. #ifdef VA_RT_FORMAT_YUV420_10BPP
  1033. ctx->va_profile = VAProfileHEVCMain10;
  1034. ctx->va_rt_format = VA_RT_FORMAT_YUV420_10BPP;
  1035. break;
  1036. #else
  1037. av_log(avctx, AV_LOG_ERROR, "10-bit encoding is not "
  1038. "supported with this VAAPI version.\n");
  1039. return AVERROR(ENOSYS);
  1040. #endif
  1041. default:
  1042. av_log(avctx, AV_LOG_ERROR, "Unknown H.265 profile %d.\n",
  1043. avctx->profile);
  1044. return AVERROR(EINVAL);
  1045. }
  1046. ctx->va_entrypoint = VAEntrypointEncSlice;
  1047. if (avctx->bit_rate > 0) {
  1048. if (avctx->rc_max_rate == avctx->bit_rate)
  1049. ctx->va_rc_mode = VA_RC_CBR;
  1050. else
  1051. ctx->va_rc_mode = VA_RC_VBR;
  1052. } else
  1053. ctx->va_rc_mode = VA_RC_CQP;
  1054. ctx->va_packed_headers =
  1055. VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
  1056. VA_ENC_PACKED_HEADER_SLICE; // Slice headers.
  1057. ctx->surface_width = FFALIGN(avctx->width, 16);
  1058. ctx->surface_height = FFALIGN(avctx->height, 16);
  1059. return ff_vaapi_encode_init(avctx);
  1060. }
  1061. #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
  1062. offsetof(VAAPIEncodeH265Options, x))
  1063. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  1064. static const AVOption vaapi_encode_h265_options[] = {
  1065. { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
  1066. OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, 52, FLAGS },
  1067. { NULL },
  1068. };
  1069. static const AVCodecDefault vaapi_encode_h265_defaults[] = {
  1070. { "profile", "1" },
  1071. { "level", "51" },
  1072. { "b", "0" },
  1073. { "bf", "2" },
  1074. { "g", "120" },
  1075. { "i_qfactor", "1" },
  1076. { "i_qoffset", "0" },
  1077. { "b_qfactor", "6/5" },
  1078. { "b_qoffset", "0" },
  1079. { NULL },
  1080. };
  1081. static const AVClass vaapi_encode_h265_class = {
  1082. .class_name = "h265_vaapi",
  1083. .item_name = av_default_item_name,
  1084. .option = vaapi_encode_h265_options,
  1085. .version = LIBAVUTIL_VERSION_INT,
  1086. };
  1087. AVCodec ff_hevc_vaapi_encoder = {
  1088. .name = "hevc_vaapi",
  1089. .long_name = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
  1090. .type = AVMEDIA_TYPE_VIDEO,
  1091. .id = AV_CODEC_ID_HEVC,
  1092. .priv_data_size = (sizeof(VAAPIEncodeContext) +
  1093. sizeof(VAAPIEncodeH265Options)),
  1094. .init = &vaapi_encode_h265_init,
  1095. .encode2 = &ff_vaapi_encode2,
  1096. .close = &ff_vaapi_encode_close,
  1097. .priv_class = &vaapi_encode_h265_class,
  1098. .capabilities = AV_CODEC_CAP_DELAY,
  1099. .defaults = vaapi_encode_h265_defaults,
  1100. .pix_fmts = (const enum AVPixelFormat[]) {
  1101. AV_PIX_FMT_VAAPI,
  1102. AV_PIX_FMT_NONE,
  1103. },
  1104. };