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.

1317 lines
47KB

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