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.

1318 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_h264.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 "h264.h"
  26. #include "h264_sei.h"
  27. #include "internal.h"
  28. #include "vaapi_encode.h"
  29. #include "vaapi_encode_h26x.h"
  30. enum {
  31. SLICE_TYPE_P = 0,
  32. SLICE_TYPE_B = 1,
  33. SLICE_TYPE_I = 2,
  34. SLICE_TYPE_SP = 3,
  35. SLICE_TYPE_SI = 4,
  36. };
  37. // This structure contains all possibly-useful per-sequence syntax elements
  38. // which are not already contained in the various VAAPI structures.
  39. typedef struct VAAPIEncodeH264MiscSequenceParams {
  40. unsigned int profile_idc;
  41. char constraint_set0_flag;
  42. char constraint_set1_flag;
  43. char constraint_set2_flag;
  44. char constraint_set3_flag;
  45. char constraint_set4_flag;
  46. char constraint_set5_flag;
  47. char separate_colour_plane_flag;
  48. char qpprime_y_zero_transform_bypass_flag;
  49. char gaps_in_frame_num_allowed_flag;
  50. char delta_pic_order_always_zero_flag;
  51. char bottom_field_pic_order_in_frame_present_flag;
  52. unsigned int num_slice_groups_minus1;
  53. unsigned int slice_group_map_type;
  54. int pic_init_qs_minus26;
  55. char overscan_info_present_flag;
  56. char overscan_appropriate_flag;
  57. char video_signal_type_present_flag;
  58. unsigned int video_format;
  59. char video_full_range_flag;
  60. char colour_description_present_flag;
  61. unsigned int colour_primaries;
  62. unsigned int transfer_characteristics;
  63. unsigned int matrix_coefficients;
  64. char chroma_loc_info_present_flag;
  65. unsigned int chroma_sample_loc_type_top_field;
  66. unsigned int chroma_sample_loc_type_bottom_field;
  67. // Some timing elements are in VAEncSequenceParameterBufferH264.
  68. char fixed_frame_rate_flag;
  69. char nal_hrd_parameters_present_flag;
  70. char vcl_hrd_parameters_present_flag;
  71. char low_delay_hrd_flag;
  72. char pic_struct_present_flag;
  73. char motion_vectors_over_pic_boundaries_flag;
  74. unsigned int max_bytes_per_pic_denom;
  75. unsigned int max_bits_per_mb_denom;
  76. unsigned int max_num_reorder_frames;
  77. unsigned int max_dec_pic_buffering;
  78. unsigned int cpb_cnt_minus1;
  79. unsigned int bit_rate_scale;
  80. unsigned int cpb_size_scale;
  81. unsigned int bit_rate_value_minus1[32];
  82. unsigned int cpb_size_value_minus1[32];
  83. char cbr_flag[32];
  84. unsigned int initial_cpb_removal_delay_length_minus1;
  85. unsigned int cpb_removal_delay_length_minus1;
  86. unsigned int dpb_output_delay_length_minus1;
  87. unsigned int time_offset_length;
  88. unsigned int initial_cpb_removal_delay;
  89. unsigned int initial_cpb_removal_delay_offset;
  90. unsigned int pic_struct;
  91. } VAAPIEncodeH264MiscSequenceParams;
  92. // This structure contains all possibly-useful per-slice syntax elements
  93. // which are not already contained in the various VAAPI structures.
  94. typedef struct VAAPIEncodeH264MiscSliceParams {
  95. unsigned int nal_unit_type;
  96. unsigned int nal_ref_idc;
  97. unsigned int colour_plane_id;
  98. char field_pic_flag;
  99. char bottom_field_flag;
  100. unsigned int redundant_pic_cnt;
  101. char sp_for_switch_flag;
  102. int slice_qs_delta;
  103. char ref_pic_list_modification_flag_l0;
  104. char ref_pic_list_modification_flag_l1;
  105. char no_output_of_prior_pics_flag;
  106. char long_term_reference_flag;
  107. char adaptive_ref_pic_marking_mode_flag;
  108. } VAAPIEncodeH264MiscSliceParams;
  109. typedef struct VAAPIEncodeH264Slice {
  110. VAAPIEncodeH264MiscSliceParams misc_slice_params;
  111. } VAAPIEncodeH264Slice;
  112. typedef struct VAAPIEncodeH264Context {
  113. VAAPIEncodeH264MiscSequenceParams misc_sequence_params;
  114. int mb_width;
  115. int mb_height;
  116. int fixed_qp_idr;
  117. int fixed_qp_p;
  118. int fixed_qp_b;
  119. int next_frame_num;
  120. int64_t last_idr_frame;
  121. int64_t idr_pic_count;
  122. int cpb_delay;
  123. int dpb_delay;
  124. // Rate control configuration.
  125. int send_timing_sei;
  126. #if VA_CHECK_VERSION(0, 36, 0)
  127. // Speed-quality tradeoff setting.
  128. struct {
  129. VAEncMiscParameterBuffer misc;
  130. VAEncMiscParameterBufferQualityLevel quality;
  131. } quality_params;
  132. #endif
  133. } VAAPIEncodeH264Context;
  134. typedef struct VAAPIEncodeH264Options {
  135. int qp;
  136. int quality;
  137. int low_power;
  138. } VAAPIEncodeH264Options;
  139. #define vseq_var(name) vseq->name, name
  140. #define vseq_field(name) vseq->seq_fields.bits.name, name
  141. #define vvui_field(name) vseq->vui_fields.bits.name, name
  142. #define vpic_var(name) vpic->name, name
  143. #define vpic_field(name) vpic->pic_fields.bits.name, name
  144. #define vslice_var(name) vslice->name, name
  145. #define vslice_field(name) vslice->slice_fields.bits.name, name
  146. #define mseq_var(name) mseq->name, name
  147. #define mslice_var(name) mslice->name, name
  148. static void vaapi_encode_h264_write_nal_header(PutBitContext *pbc,
  149. int nal_unit_type, int nal_ref_idc)
  150. {
  151. u(1, 0, forbidden_zero_bit);
  152. u(2, nal_ref_idc, nal_ref_idc);
  153. u(5, nal_unit_type, nal_unit_type);
  154. }
  155. static void vaapi_encode_h264_write_trailing_rbsp(PutBitContext *pbc)
  156. {
  157. u(1, 1, rbsp_stop_one_bit);
  158. while (put_bits_count(pbc) & 7)
  159. u(1, 0, rbsp_alignment_zero_bit);
  160. }
  161. static void vaapi_encode_h264_write_vui(PutBitContext *pbc,
  162. VAAPIEncodeContext *ctx)
  163. {
  164. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  165. VAAPIEncodeH264Context *priv = ctx->priv_data;
  166. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  167. int i;
  168. u(1, vvui_field(aspect_ratio_info_present_flag));
  169. if (vseq->vui_fields.bits.aspect_ratio_info_present_flag) {
  170. u(8, vseq_var(aspect_ratio_idc));
  171. if (vseq->aspect_ratio_idc == 255) {
  172. u(16, vseq_var(sar_width));
  173. u(16, vseq_var(sar_height));
  174. }
  175. }
  176. u(1, mseq_var(overscan_info_present_flag));
  177. if (mseq->overscan_info_present_flag)
  178. u(1, mseq_var(overscan_appropriate_flag));
  179. u(1, mseq_var(video_signal_type_present_flag));
  180. if (mseq->video_signal_type_present_flag) {
  181. u(3, mseq_var(video_format));
  182. u(1, mseq_var(video_full_range_flag));
  183. u(1, mseq_var(colour_description_present_flag));
  184. if (mseq->colour_description_present_flag) {
  185. u(8, mseq_var(colour_primaries));
  186. u(8, mseq_var(transfer_characteristics));
  187. u(8, mseq_var(matrix_coefficients));
  188. }
  189. }
  190. u(1, mseq_var(chroma_loc_info_present_flag));
  191. if (mseq->chroma_loc_info_present_flag) {
  192. ue(mseq_var(chroma_sample_loc_type_top_field));
  193. ue(mseq_var(chroma_sample_loc_type_bottom_field));
  194. }
  195. u(1, vvui_field(timing_info_present_flag));
  196. if (vseq->vui_fields.bits.timing_info_present_flag) {
  197. u(32, vseq_var(num_units_in_tick));
  198. u(32, vseq_var(time_scale));
  199. u(1, mseq_var(fixed_frame_rate_flag));
  200. }
  201. u(1, mseq_var(nal_hrd_parameters_present_flag));
  202. if (mseq->nal_hrd_parameters_present_flag) {
  203. ue(mseq_var(cpb_cnt_minus1));
  204. u(4, mseq_var(bit_rate_scale));
  205. u(4, mseq_var(cpb_size_scale));
  206. for (i = 0; i <= mseq->cpb_cnt_minus1; i++) {
  207. ue(mseq_var(bit_rate_value_minus1[i]));
  208. ue(mseq_var(cpb_size_value_minus1[i]));
  209. u(1, mseq_var(cbr_flag[i]));
  210. }
  211. u(5, mseq_var(initial_cpb_removal_delay_length_minus1));
  212. u(5, mseq_var(cpb_removal_delay_length_minus1));
  213. u(5, mseq_var(dpb_output_delay_length_minus1));
  214. u(5, mseq_var(time_offset_length));
  215. }
  216. u(1, mseq_var(vcl_hrd_parameters_present_flag));
  217. if (mseq->vcl_hrd_parameters_present_flag) {
  218. av_assert0(0 && "vcl hrd parameters not supported");
  219. }
  220. if (mseq->nal_hrd_parameters_present_flag ||
  221. mseq->vcl_hrd_parameters_present_flag)
  222. u(1, mseq_var(low_delay_hrd_flag));
  223. u(1, mseq_var(pic_struct_present_flag));
  224. u(1, vvui_field(bitstream_restriction_flag));
  225. if (vseq->vui_fields.bits.bitstream_restriction_flag) {
  226. u(1, mseq_var(motion_vectors_over_pic_boundaries_flag));
  227. ue(mseq_var(max_bytes_per_pic_denom));
  228. ue(mseq_var(max_bits_per_mb_denom));
  229. ue(vvui_field(log2_max_mv_length_horizontal));
  230. ue(vvui_field(log2_max_mv_length_vertical));
  231. ue(mseq_var(max_num_reorder_frames));
  232. ue(mseq_var(max_dec_pic_buffering));
  233. }
  234. }
  235. static void vaapi_encode_h264_write_sps(PutBitContext *pbc,
  236. VAAPIEncodeContext *ctx)
  237. {
  238. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  239. VAAPIEncodeH264Context *priv = ctx->priv_data;
  240. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  241. int i;
  242. vaapi_encode_h264_write_nal_header(pbc, H264_NAL_SPS, 3);
  243. u(8, mseq_var(profile_idc));
  244. u(1, mseq_var(constraint_set0_flag));
  245. u(1, mseq_var(constraint_set1_flag));
  246. u(1, mseq_var(constraint_set2_flag));
  247. u(1, mseq_var(constraint_set3_flag));
  248. u(1, mseq_var(constraint_set4_flag));
  249. u(1, mseq_var(constraint_set5_flag));
  250. u(2, 0, reserved_zero_2bits);
  251. u(8, vseq_var(level_idc));
  252. ue(vseq_var(seq_parameter_set_id));
  253. if (mseq->profile_idc == 100 || mseq->profile_idc == 110 ||
  254. mseq->profile_idc == 122 || mseq->profile_idc == 244 ||
  255. mseq->profile_idc == 44 || mseq->profile_idc == 83 ||
  256. mseq->profile_idc == 86 || mseq->profile_idc == 118 ||
  257. mseq->profile_idc == 128 || mseq->profile_idc == 138) {
  258. ue(vseq_field(chroma_format_idc));
  259. if (vseq->seq_fields.bits.chroma_format_idc == 3)
  260. u(1, mseq_var(separate_colour_plane_flag));
  261. ue(vseq_var(bit_depth_luma_minus8));
  262. ue(vseq_var(bit_depth_chroma_minus8));
  263. u(1, mseq_var(qpprime_y_zero_transform_bypass_flag));
  264. u(1, vseq_field(seq_scaling_matrix_present_flag));
  265. if (vseq->seq_fields.bits.seq_scaling_matrix_present_flag) {
  266. av_assert0(0 && "scaling matrices not supported");
  267. }
  268. }
  269. ue(vseq_field(log2_max_frame_num_minus4));
  270. ue(vseq_field(pic_order_cnt_type));
  271. if (vseq->seq_fields.bits.pic_order_cnt_type == 0) {
  272. ue(vseq_field(log2_max_pic_order_cnt_lsb_minus4));
  273. } else if (vseq->seq_fields.bits.pic_order_cnt_type == 1) {
  274. u(1, mseq_var(delta_pic_order_always_zero_flag));
  275. se(vseq_var(offset_for_non_ref_pic));
  276. se(vseq_var(offset_for_top_to_bottom_field));
  277. ue(vseq_var(num_ref_frames_in_pic_order_cnt_cycle));
  278. for (i = 0; i < vseq->num_ref_frames_in_pic_order_cnt_cycle; i++)
  279. se(vseq_var(offset_for_ref_frame[i]));
  280. }
  281. ue(vseq_var(max_num_ref_frames));
  282. u(1, mseq_var(gaps_in_frame_num_allowed_flag));
  283. ue(vseq->picture_width_in_mbs - 1, pic_width_in_mbs_minus1);
  284. ue(vseq->picture_height_in_mbs - 1, pic_height_in_mbs_minus1);
  285. u(1, vseq_field(frame_mbs_only_flag));
  286. if (!vseq->seq_fields.bits.frame_mbs_only_flag)
  287. u(1, vseq_field(mb_adaptive_frame_field_flag));
  288. u(1, vseq_field(direct_8x8_inference_flag));
  289. u(1, vseq_var(frame_cropping_flag));
  290. if (vseq->frame_cropping_flag) {
  291. ue(vseq_var(frame_crop_left_offset));
  292. ue(vseq_var(frame_crop_right_offset));
  293. ue(vseq_var(frame_crop_top_offset));
  294. ue(vseq_var(frame_crop_bottom_offset));
  295. }
  296. u(1, vseq_var(vui_parameters_present_flag));
  297. if (vseq->vui_parameters_present_flag)
  298. vaapi_encode_h264_write_vui(pbc, ctx);
  299. vaapi_encode_h264_write_trailing_rbsp(pbc);
  300. }
  301. static void vaapi_encode_h264_write_pps(PutBitContext *pbc,
  302. VAAPIEncodeContext *ctx)
  303. {
  304. VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
  305. VAAPIEncodeH264Context *priv = ctx->priv_data;
  306. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  307. vaapi_encode_h264_write_nal_header(pbc, H264_NAL_PPS, 3);
  308. ue(vpic_var(pic_parameter_set_id));
  309. ue(vpic_var(seq_parameter_set_id));
  310. u(1, vpic_field(entropy_coding_mode_flag));
  311. u(1, mseq_var(bottom_field_pic_order_in_frame_present_flag));
  312. ue(mseq_var(num_slice_groups_minus1));
  313. if (mseq->num_slice_groups_minus1 > 0) {
  314. ue(mseq_var(slice_group_map_type));
  315. av_assert0(0 && "slice groups not supported");
  316. }
  317. ue(vpic_var(num_ref_idx_l0_active_minus1));
  318. ue(vpic_var(num_ref_idx_l1_active_minus1));
  319. u(1, vpic_field(weighted_pred_flag));
  320. u(2, vpic_field(weighted_bipred_idc));
  321. se(vpic->pic_init_qp - 26, pic_init_qp_minus26);
  322. se(mseq_var(pic_init_qs_minus26));
  323. se(vpic_var(chroma_qp_index_offset));
  324. u(1, vpic_field(deblocking_filter_control_present_flag));
  325. u(1, vpic_field(constrained_intra_pred_flag));
  326. u(1, vpic_field(redundant_pic_cnt_present_flag));
  327. u(1, vpic_field(transform_8x8_mode_flag));
  328. u(1, vpic_field(pic_scaling_matrix_present_flag));
  329. if (vpic->pic_fields.bits.pic_scaling_matrix_present_flag) {
  330. av_assert0(0 && "scaling matrices not supported");
  331. }
  332. se(vpic_var(second_chroma_qp_index_offset));
  333. vaapi_encode_h264_write_trailing_rbsp(pbc);
  334. }
  335. static void vaapi_encode_h264_write_slice_header2(PutBitContext *pbc,
  336. VAAPIEncodeContext *ctx,
  337. VAAPIEncodePicture *pic,
  338. VAAPIEncodeSlice *slice)
  339. {
  340. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  341. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  342. VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
  343. VAAPIEncodeH264Context *priv = ctx->priv_data;
  344. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  345. VAAPIEncodeH264Slice *pslice = slice->priv_data;
  346. VAAPIEncodeH264MiscSliceParams *mslice = &pslice->misc_slice_params;
  347. vaapi_encode_h264_write_nal_header(pbc, mslice->nal_unit_type,
  348. mslice->nal_ref_idc);
  349. ue(vslice->macroblock_address, first_mb_in_slice);
  350. ue(vslice_var(slice_type));
  351. ue(vpic_var(pic_parameter_set_id));
  352. if (mseq->separate_colour_plane_flag) {
  353. u(2, mslice_var(colour_plane_id));
  354. }
  355. u(4 + vseq->seq_fields.bits.log2_max_frame_num_minus4,
  356. (vpic->frame_num &
  357. ((1 << (4 + vseq->seq_fields.bits.log2_max_frame_num_minus4)) - 1)),
  358. frame_num);
  359. if (!vseq->seq_fields.bits.frame_mbs_only_flag) {
  360. u(1, mslice_var(field_pic_flag));
  361. if (mslice->field_pic_flag)
  362. u(1, mslice_var(bottom_field_flag));
  363. }
  364. if (vpic->pic_fields.bits.idr_pic_flag) {
  365. ue(vslice_var(idr_pic_id));
  366. }
  367. if (vseq->seq_fields.bits.pic_order_cnt_type == 0) {
  368. u(4 + vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4,
  369. vslice_var(pic_order_cnt_lsb));
  370. if (mseq->bottom_field_pic_order_in_frame_present_flag &&
  371. !mslice->field_pic_flag) {
  372. se(vslice_var(delta_pic_order_cnt_bottom));
  373. }
  374. }
  375. if (vseq->seq_fields.bits.pic_order_cnt_type == 1 &&
  376. !vseq->seq_fields.bits.delta_pic_order_always_zero_flag) {
  377. se(vslice_var(delta_pic_order_cnt[0]));
  378. if (mseq->bottom_field_pic_order_in_frame_present_flag &&
  379. !mslice->field_pic_flag) {
  380. se(vslice_var(delta_pic_order_cnt[1]));
  381. }
  382. }
  383. if (vpic->pic_fields.bits.redundant_pic_cnt_present_flag) {
  384. ue(mslice_var(redundant_pic_cnt));
  385. }
  386. if (vslice->slice_type == SLICE_TYPE_B) {
  387. u(1, vslice_var(direct_spatial_mv_pred_flag));
  388. }
  389. if (vslice->slice_type == SLICE_TYPE_P ||
  390. vslice->slice_type == SLICE_TYPE_SP ||
  391. vslice->slice_type == SLICE_TYPE_B) {
  392. u(1, vslice_var(num_ref_idx_active_override_flag));
  393. if (vslice->num_ref_idx_active_override_flag) {
  394. ue(vslice_var(num_ref_idx_l0_active_minus1));
  395. if (vslice->slice_type == SLICE_TYPE_B)
  396. ue(vslice_var(num_ref_idx_l1_active_minus1));
  397. }
  398. }
  399. if (mslice->nal_unit_type == 20 || mslice->nal_unit_type == 21) {
  400. av_assert0(0 && "no MVC support");
  401. } else {
  402. if (vslice->slice_type % 5 != 2 && vslice->slice_type % 5 != 4) {
  403. u(1, mslice_var(ref_pic_list_modification_flag_l0));
  404. if (mslice->ref_pic_list_modification_flag_l0) {
  405. av_assert0(0 && "ref pic list modification");
  406. }
  407. }
  408. if (vslice->slice_type % 5 == 1) {
  409. u(1, mslice_var(ref_pic_list_modification_flag_l1));
  410. if (mslice->ref_pic_list_modification_flag_l1) {
  411. av_assert0(0 && "ref pic list modification");
  412. }
  413. }
  414. }
  415. if ((vpic->pic_fields.bits.weighted_pred_flag &&
  416. (vslice->slice_type == SLICE_TYPE_P ||
  417. vslice->slice_type == SLICE_TYPE_SP)) ||
  418. (vpic->pic_fields.bits.weighted_bipred_idc == 1 &&
  419. vslice->slice_type == SLICE_TYPE_B)) {
  420. av_assert0(0 && "prediction weights not supported");
  421. }
  422. av_assert0(mslice->nal_ref_idc > 0 ==
  423. vpic->pic_fields.bits.reference_pic_flag);
  424. if (mslice->nal_ref_idc != 0) {
  425. if (vpic->pic_fields.bits.idr_pic_flag) {
  426. u(1, mslice_var(no_output_of_prior_pics_flag));
  427. u(1, mslice_var(long_term_reference_flag));
  428. } else {
  429. u(1, mslice_var(adaptive_ref_pic_marking_mode_flag));
  430. if (mslice->adaptive_ref_pic_marking_mode_flag) {
  431. av_assert0(0 && "MMCOs not supported");
  432. }
  433. }
  434. }
  435. if (vpic->pic_fields.bits.entropy_coding_mode_flag &&
  436. vslice->slice_type != SLICE_TYPE_I &&
  437. vslice->slice_type != SLICE_TYPE_SI) {
  438. ue(vslice_var(cabac_init_idc));
  439. }
  440. se(vslice_var(slice_qp_delta));
  441. if (vslice->slice_type == SLICE_TYPE_SP ||
  442. vslice->slice_type == SLICE_TYPE_SI) {
  443. if (vslice->slice_type == SLICE_TYPE_SP)
  444. u(1, mslice_var(sp_for_switch_flag));
  445. se(mslice_var(slice_qs_delta));
  446. }
  447. if (vpic->pic_fields.bits.deblocking_filter_control_present_flag) {
  448. ue(vslice_var(disable_deblocking_filter_idc));
  449. if (vslice->disable_deblocking_filter_idc != 1) {
  450. se(vslice_var(slice_alpha_c0_offset_div2));
  451. se(vslice_var(slice_beta_offset_div2));
  452. }
  453. }
  454. if (mseq->num_slice_groups_minus1 > 0 &&
  455. mseq->slice_group_map_type >= 3 && mseq->slice_group_map_type <= 5) {
  456. av_assert0(0 && "slice groups not supported");
  457. }
  458. // No alignment - this need not be a byte boundary.
  459. }
  460. static void vaapi_encode_h264_write_buffering_period(PutBitContext *pbc,
  461. VAAPIEncodeContext *ctx,
  462. VAAPIEncodePicture *pic)
  463. {
  464. VAAPIEncodeH264Context *priv = ctx->priv_data;
  465. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  466. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  467. int i;
  468. ue(vpic_var(seq_parameter_set_id));
  469. if (mseq->nal_hrd_parameters_present_flag) {
  470. for (i = 0; i <= mseq->cpb_cnt_minus1; i++) {
  471. u(mseq->initial_cpb_removal_delay_length_minus1 + 1,
  472. mseq_var(initial_cpb_removal_delay));
  473. u(mseq->initial_cpb_removal_delay_length_minus1 + 1,
  474. mseq_var(initial_cpb_removal_delay_offset));
  475. }
  476. }
  477. if (mseq->vcl_hrd_parameters_present_flag) {
  478. av_assert0(0 && "vcl hrd parameters not supported");
  479. }
  480. }
  481. static void vaapi_encode_h264_write_pic_timing(PutBitContext *pbc,
  482. VAAPIEncodeContext *ctx,
  483. VAAPIEncodePicture *pic)
  484. {
  485. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  486. VAAPIEncodeH264Context *priv = ctx->priv_data;
  487. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  488. int i, num_clock_ts;
  489. if (mseq->nal_hrd_parameters_present_flag ||
  490. mseq->vcl_hrd_parameters_present_flag) {
  491. u(mseq->cpb_removal_delay_length_minus1 + 1,
  492. 2 * vseq->num_units_in_tick * priv->cpb_delay,
  493. cpb_removal_delay);
  494. u(mseq->dpb_output_delay_length_minus1 + 1,
  495. 2 * vseq->num_units_in_tick * priv->dpb_delay,
  496. dpb_output_delay);
  497. }
  498. if (mseq->pic_struct_present_flag) {
  499. u(4, mseq_var(pic_struct));
  500. num_clock_ts = (mseq->pic_struct <= 2 ? 1 :
  501. mseq->pic_struct <= 4 ? 2 :
  502. mseq->pic_struct <= 8 ? 3 : 0);
  503. for (i = 0; i < num_clock_ts; i++) {
  504. u(1, 0, clock_timestamp_flag[i]);
  505. // No full timestamp information.
  506. }
  507. }
  508. }
  509. static void vaapi_encode_h264_write_identifier(PutBitContext *pbc,
  510. VAAPIEncodeContext *ctx,
  511. VAAPIEncodePicture *pic)
  512. {
  513. const char *lavc = LIBAVCODEC_IDENT;
  514. const char *vaapi = VA_VERSION_S;
  515. const char *driver = vaQueryVendorString(ctx->hwctx->display);
  516. char tmp[256];
  517. int i;
  518. // Random (version 4) ISO 11578 UUID.
  519. uint8_t uuid[16] = {
  520. 0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
  521. 0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
  522. };
  523. for (i = 0; i < 16; i++)
  524. u(8, uuid[i], uuid_iso_iec_11578);
  525. snprintf(tmp, sizeof(tmp), "%s / VAAPI %s / %s", lavc, vaapi, driver);
  526. for (i = 0; i < sizeof(tmp) && tmp[i]; i++)
  527. u(8, tmp[i], user_data_payload_byte);
  528. }
  529. static void vaapi_encode_h264_write_sei(PutBitContext *pbc,
  530. VAAPIEncodeContext *ctx,
  531. VAAPIEncodePicture *pic)
  532. {
  533. VAAPIEncodeH264Context *priv = ctx->priv_data;
  534. PutBitContext payload_bits;
  535. char payload[256];
  536. int payload_type, payload_size, i;
  537. void (*write_payload)(PutBitContext *pbc,
  538. VAAPIEncodeContext *ctx,
  539. VAAPIEncodePicture *pic) = NULL;
  540. vaapi_encode_h264_write_nal_header(pbc, H264_NAL_SEI, 0);
  541. for (payload_type = 0; payload_type < 64; payload_type++) {
  542. switch (payload_type) {
  543. case SEI_TYPE_BUFFERING_PERIOD:
  544. if (!priv->send_timing_sei ||
  545. pic->type != PICTURE_TYPE_IDR)
  546. continue;
  547. write_payload = &vaapi_encode_h264_write_buffering_period;
  548. break;
  549. case SEI_TYPE_PIC_TIMING:
  550. if (!priv->send_timing_sei)
  551. continue;
  552. write_payload = &vaapi_encode_h264_write_pic_timing;
  553. break;
  554. case SEI_TYPE_USER_DATA_UNREGISTERED:
  555. if (pic->encode_order != 0)
  556. continue;
  557. write_payload = &vaapi_encode_h264_write_identifier;
  558. break;
  559. default:
  560. continue;
  561. }
  562. init_put_bits(&payload_bits, payload, sizeof(payload));
  563. write_payload(&payload_bits, ctx, pic);
  564. if (put_bits_count(&payload_bits) & 7) {
  565. write_u(&payload_bits, 1, 1, bit_equal_to_one);
  566. while (put_bits_count(&payload_bits) & 7)
  567. write_u(&payload_bits, 1, 0, bit_equal_to_zero);
  568. }
  569. payload_size = put_bits_count(&payload_bits) / 8;
  570. flush_put_bits(&payload_bits);
  571. u(8, payload_type, last_payload_type_byte);
  572. u(8, payload_size, last_payload_size_byte);
  573. for (i = 0; i < payload_size; i++)
  574. u(8, payload[i] & 0xff, sei_payload);
  575. }
  576. vaapi_encode_h264_write_trailing_rbsp(pbc);
  577. }
  578. static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
  579. char *data, size_t *data_len)
  580. {
  581. VAAPIEncodeContext *ctx = avctx->priv_data;
  582. PutBitContext pbc;
  583. char tmp[256];
  584. int err;
  585. size_t nal_len, bit_len, bit_pos, next_len;
  586. bit_len = *data_len;
  587. bit_pos = 0;
  588. init_put_bits(&pbc, tmp, sizeof(tmp));
  589. vaapi_encode_h264_write_sps(&pbc, ctx);
  590. nal_len = put_bits_count(&pbc);
  591. flush_put_bits(&pbc);
  592. next_len = bit_len - bit_pos;
  593. err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
  594. &next_len,
  595. tmp, nal_len);
  596. if (err < 0)
  597. return err;
  598. bit_pos += next_len;
  599. init_put_bits(&pbc, tmp, sizeof(tmp));
  600. vaapi_encode_h264_write_pps(&pbc, ctx);
  601. nal_len = put_bits_count(&pbc);
  602. flush_put_bits(&pbc);
  603. next_len = bit_len - bit_pos;
  604. err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
  605. &next_len,
  606. tmp, nal_len);
  607. if (err < 0)
  608. return err;
  609. bit_pos += next_len;
  610. *data_len = bit_pos;
  611. return 0;
  612. }
  613. static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
  614. VAAPIEncodePicture *pic,
  615. VAAPIEncodeSlice *slice,
  616. char *data, size_t *data_len)
  617. {
  618. VAAPIEncodeContext *ctx = avctx->priv_data;
  619. PutBitContext pbc;
  620. char tmp[256];
  621. size_t header_len;
  622. init_put_bits(&pbc, tmp, sizeof(tmp));
  623. vaapi_encode_h264_write_slice_header2(&pbc, ctx, pic, slice);
  624. header_len = put_bits_count(&pbc);
  625. flush_put_bits(&pbc);
  626. return ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data, data_len,
  627. tmp, header_len);
  628. }
  629. static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
  630. VAAPIEncodePicture *pic,
  631. int index, int *type,
  632. char *data, size_t *data_len)
  633. {
  634. VAAPIEncodeContext *ctx = avctx->priv_data;
  635. PutBitContext pbc;
  636. char tmp[256];
  637. size_t header_len;
  638. if (index == 0 && ctx->va_rc_mode == VA_RC_CBR) {
  639. *type = VAEncPackedHeaderH264_SEI;
  640. init_put_bits(&pbc, tmp, sizeof(tmp));
  641. vaapi_encode_h264_write_sei(&pbc, ctx, pic);
  642. header_len = put_bits_count(&pbc);
  643. flush_put_bits(&pbc);
  644. return ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data, data_len,
  645. tmp, header_len);
  646. } else {
  647. return AVERROR_EOF;
  648. }
  649. }
  650. static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
  651. {
  652. VAAPIEncodeContext *ctx = avctx->priv_data;
  653. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  654. VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
  655. VAAPIEncodeH264Context *priv = ctx->priv_data;
  656. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  657. int i;
  658. {
  659. vseq->seq_parameter_set_id = 0;
  660. vseq->level_idc = avctx->level;
  661. vseq->max_num_ref_frames = 1 + (avctx->max_b_frames > 0);
  662. vseq->picture_width_in_mbs = priv->mb_width;
  663. vseq->picture_height_in_mbs = priv->mb_height;
  664. vseq->seq_fields.bits.chroma_format_idc = 1;
  665. vseq->seq_fields.bits.frame_mbs_only_flag = 1;
  666. vseq->seq_fields.bits.direct_8x8_inference_flag = 1;
  667. vseq->seq_fields.bits.log2_max_frame_num_minus4 = 4;
  668. vseq->seq_fields.bits.pic_order_cnt_type = 0;
  669. vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 =
  670. av_clip(av_log2(avctx->max_b_frames + 1) - 2, 0, 12);
  671. if (avctx->width != ctx->surface_width ||
  672. avctx->height != ctx->surface_height) {
  673. vseq->frame_cropping_flag = 1;
  674. vseq->frame_crop_left_offset = 0;
  675. vseq->frame_crop_right_offset =
  676. (ctx->surface_width - avctx->width) / 2;
  677. vseq->frame_crop_top_offset = 0;
  678. vseq->frame_crop_bottom_offset =
  679. (ctx->surface_height - avctx->height) / 2;
  680. } else {
  681. vseq->frame_cropping_flag = 0;
  682. }
  683. vseq->vui_parameters_present_flag = 1;
  684. if (avctx->sample_aspect_ratio.num != 0) {
  685. vseq->vui_fields.bits.aspect_ratio_info_present_flag = 1;
  686. // There is a large enum of these which we could support
  687. // individually rather than using the generic X/Y form?
  688. if (avctx->sample_aspect_ratio.num ==
  689. avctx->sample_aspect_ratio.den) {
  690. vseq->aspect_ratio_idc = 1;
  691. } else {
  692. vseq->aspect_ratio_idc = 255; // Extended SAR.
  693. vseq->sar_width = avctx->sample_aspect_ratio.num;
  694. vseq->sar_height = avctx->sample_aspect_ratio.den;
  695. }
  696. }
  697. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  698. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  699. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  700. mseq->video_signal_type_present_flag = 1;
  701. mseq->video_format = 5; // Unspecified.
  702. mseq->video_full_range_flag = 0;
  703. mseq->colour_description_present_flag = 1;
  704. // These enums are derived from the standard and hence
  705. // we can just use the values directly.
  706. mseq->colour_primaries = avctx->color_primaries;
  707. mseq->transfer_characteristics = avctx->color_trc;
  708. mseq->matrix_coefficients = avctx->colorspace;
  709. }
  710. vseq->vui_fields.bits.bitstream_restriction_flag = 1;
  711. mseq->motion_vectors_over_pic_boundaries_flag = 1;
  712. mseq->max_bytes_per_pic_denom = 0;
  713. mseq->max_bits_per_mb_denom = 0;
  714. vseq->vui_fields.bits.log2_max_mv_length_horizontal = 16;
  715. vseq->vui_fields.bits.log2_max_mv_length_vertical = 16;
  716. mseq->max_num_reorder_frames = (avctx->max_b_frames > 0);
  717. mseq->max_dec_pic_buffering = vseq->max_num_ref_frames;
  718. vseq->bits_per_second = avctx->bit_rate;
  719. vseq->vui_fields.bits.timing_info_present_flag = 1;
  720. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  721. vseq->num_units_in_tick = avctx->framerate.den;
  722. vseq->time_scale = 2 * avctx->framerate.num;
  723. mseq->fixed_frame_rate_flag = 1;
  724. } else {
  725. vseq->num_units_in_tick = avctx->time_base.num;
  726. vseq->time_scale = 2 * avctx->time_base.den;
  727. mseq->fixed_frame_rate_flag = 0;
  728. }
  729. if (ctx->va_rc_mode == VA_RC_CBR) {
  730. priv->send_timing_sei = 1;
  731. mseq->nal_hrd_parameters_present_flag = 1;
  732. mseq->cpb_cnt_minus1 = 0;
  733. // Try to scale these to a sensible range so that the
  734. // golomb encode of the value is not overlong.
  735. mseq->bit_rate_scale =
  736. av_clip_uintp2(av_log2(avctx->bit_rate) - 15 - 6, 4);
  737. mseq->bit_rate_value_minus1[0] =
  738. (avctx->bit_rate >> mseq->bit_rate_scale + 6) - 1;
  739. mseq->cpb_size_scale =
  740. av_clip_uintp2(av_log2(ctx->hrd_params.hrd.buffer_size) - 15 - 4, 4);
  741. mseq->cpb_size_value_minus1[0] =
  742. (ctx->hrd_params.hrd.buffer_size >> mseq->cpb_size_scale + 4) - 1;
  743. // CBR mode isn't actually available here, despite naming.
  744. mseq->cbr_flag[0] = 0;
  745. mseq->initial_cpb_removal_delay_length_minus1 = 23;
  746. mseq->cpb_removal_delay_length_minus1 = 23;
  747. mseq->dpb_output_delay_length_minus1 = 7;
  748. mseq->time_offset_length = 0;
  749. // This calculation can easily overflow 32 bits.
  750. mseq->initial_cpb_removal_delay = 90000 *
  751. (uint64_t)ctx->hrd_params.hrd.initial_buffer_fullness /
  752. ctx->hrd_params.hrd.buffer_size;
  753. mseq->initial_cpb_removal_delay_offset = 0;
  754. } else {
  755. priv->send_timing_sei = 0;
  756. mseq->nal_hrd_parameters_present_flag = 0;
  757. }
  758. vseq->intra_period = avctx->gop_size;
  759. vseq->intra_idr_period = avctx->gop_size;
  760. vseq->ip_period = ctx->b_per_p + 1;
  761. }
  762. {
  763. vpic->CurrPic.picture_id = VA_INVALID_ID;
  764. vpic->CurrPic.flags = VA_PICTURE_H264_INVALID;
  765. for (i = 0; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
  766. vpic->ReferenceFrames[i].picture_id = VA_INVALID_ID;
  767. vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
  768. }
  769. vpic->coded_buf = VA_INVALID_ID;
  770. vpic->pic_parameter_set_id = 0;
  771. vpic->seq_parameter_set_id = 0;
  772. vpic->num_ref_idx_l0_active_minus1 = 0;
  773. vpic->num_ref_idx_l1_active_minus1 = 0;
  774. vpic->pic_fields.bits.entropy_coding_mode_flag =
  775. ((avctx->profile & 0xff) != 66);
  776. vpic->pic_fields.bits.weighted_pred_flag = 0;
  777. vpic->pic_fields.bits.weighted_bipred_idc = 0;
  778. vpic->pic_fields.bits.transform_8x8_mode_flag =
  779. ((avctx->profile & 0xff) >= 100);
  780. vpic->pic_init_qp = priv->fixed_qp_idr;
  781. }
  782. {
  783. mseq->profile_idc = avctx->profile & 0xff;
  784. if (avctx->profile & FF_PROFILE_H264_CONSTRAINED)
  785. mseq->constraint_set1_flag = 1;
  786. if (avctx->profile & FF_PROFILE_H264_INTRA)
  787. mseq->constraint_set3_flag = 1;
  788. }
  789. return 0;
  790. }
  791. static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
  792. VAAPIEncodePicture *pic)
  793. {
  794. VAAPIEncodeContext *ctx = avctx->priv_data;
  795. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  796. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  797. VAAPIEncodeH264Context *priv = ctx->priv_data;
  798. int i;
  799. if (pic->type == PICTURE_TYPE_IDR) {
  800. av_assert0(pic->display_order == pic->encode_order);
  801. vpic->frame_num = 0;
  802. priv->next_frame_num = 1;
  803. priv->cpb_delay = 0;
  804. priv->last_idr_frame = pic->display_order;
  805. } else {
  806. vpic->frame_num = priv->next_frame_num;
  807. if (pic->type != PICTURE_TYPE_B) {
  808. // nal_ref_idc != 0
  809. ++priv->next_frame_num;
  810. }
  811. ++priv->cpb_delay;
  812. }
  813. priv->dpb_delay = pic->display_order - pic->encode_order + 1;
  814. vpic->frame_num = vpic->frame_num &
  815. ((1 << (4 + vseq->seq_fields.bits.log2_max_frame_num_minus4)) - 1);
  816. vpic->CurrPic.picture_id = pic->recon_surface;
  817. vpic->CurrPic.frame_idx = vpic->frame_num;
  818. vpic->CurrPic.flags = 0;
  819. vpic->CurrPic.TopFieldOrderCnt = pic->display_order - priv->last_idr_frame;
  820. vpic->CurrPic.BottomFieldOrderCnt = pic->display_order - priv->last_idr_frame;
  821. for (i = 0; i < pic->nb_refs; i++) {
  822. VAAPIEncodePicture *ref = pic->refs[i];
  823. av_assert0(ref && ref->encode_order < pic->encode_order);
  824. vpic->ReferenceFrames[i].picture_id = ref->recon_surface;
  825. vpic->ReferenceFrames[i].frame_idx = ref->encode_order;
  826. vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
  827. vpic->ReferenceFrames[i].TopFieldOrderCnt = ref->display_order - priv->last_idr_frame;
  828. vpic->ReferenceFrames[i].BottomFieldOrderCnt = ref->display_order - priv->last_idr_frame;
  829. }
  830. for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
  831. vpic->ReferenceFrames[i].picture_id = VA_INVALID_ID;
  832. vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
  833. }
  834. vpic->coded_buf = pic->output_buffer;
  835. vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
  836. vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
  837. pic->nb_slices = 1;
  838. return 0;
  839. }
  840. static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
  841. VAAPIEncodePicture *pic,
  842. VAAPIEncodeSlice *slice)
  843. {
  844. VAAPIEncodeContext *ctx = avctx->priv_data;
  845. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  846. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  847. VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
  848. VAAPIEncodeH264Context *priv = ctx->priv_data;
  849. VAAPIEncodeH264Slice *pslice;
  850. VAAPIEncodeH264MiscSliceParams *mslice;
  851. int i;
  852. slice->priv_data = av_mallocz(sizeof(*pslice));
  853. if (!slice->priv_data)
  854. return AVERROR(ENOMEM);
  855. pslice = slice->priv_data;
  856. mslice = &pslice->misc_slice_params;
  857. if (pic->type == PICTURE_TYPE_IDR)
  858. mslice->nal_unit_type = H264_NAL_IDR_SLICE;
  859. else
  860. mslice->nal_unit_type = H264_NAL_SLICE;
  861. switch (pic->type) {
  862. case PICTURE_TYPE_IDR:
  863. vslice->slice_type = SLICE_TYPE_I;
  864. mslice->nal_ref_idc = 3;
  865. break;
  866. case PICTURE_TYPE_I:
  867. vslice->slice_type = SLICE_TYPE_I;
  868. mslice->nal_ref_idc = 2;
  869. break;
  870. case PICTURE_TYPE_P:
  871. vslice->slice_type = SLICE_TYPE_P;
  872. mslice->nal_ref_idc = 1;
  873. break;
  874. case PICTURE_TYPE_B:
  875. vslice->slice_type = SLICE_TYPE_B;
  876. mslice->nal_ref_idc = 0;
  877. break;
  878. default:
  879. av_assert0(0 && "invalid picture type");
  880. }
  881. // Only one slice per frame.
  882. vslice->macroblock_address = 0;
  883. vslice->num_macroblocks = priv->mb_width * priv->mb_height;
  884. vslice->macroblock_info = VA_INVALID_ID;
  885. vslice->pic_parameter_set_id = vpic->pic_parameter_set_id;
  886. vslice->idr_pic_id = priv->idr_pic_count++;
  887. vslice->pic_order_cnt_lsb = (pic->display_order - priv->last_idr_frame) &
  888. ((1 << (4 + vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4)) - 1);
  889. for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
  890. vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
  891. vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
  892. vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
  893. vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
  894. }
  895. av_assert0(pic->nb_refs <= 2);
  896. if (pic->nb_refs >= 1) {
  897. // Backward reference for P- or B-frame.
  898. av_assert0(pic->type == PICTURE_TYPE_P ||
  899. pic->type == PICTURE_TYPE_B);
  900. vslice->num_ref_idx_l0_active_minus1 = 0;
  901. vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
  902. }
  903. if (pic->nb_refs >= 2) {
  904. // Forward reference for B-frame.
  905. av_assert0(pic->type == PICTURE_TYPE_B);
  906. vslice->num_ref_idx_l1_active_minus1 = 0;
  907. vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
  908. }
  909. if (pic->type == PICTURE_TYPE_B)
  910. vslice->slice_qp_delta = priv->fixed_qp_b - vpic->pic_init_qp;
  911. else if (pic->type == PICTURE_TYPE_P)
  912. vslice->slice_qp_delta = priv->fixed_qp_p - vpic->pic_init_qp;
  913. else
  914. vslice->slice_qp_delta = priv->fixed_qp_idr - vpic->pic_init_qp;
  915. vslice->direct_spatial_mv_pred_flag = 1;
  916. return 0;
  917. }
  918. static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
  919. {
  920. VAAPIEncodeContext *ctx = avctx->priv_data;
  921. VAAPIEncodeH264Context *priv = ctx->priv_data;
  922. VAAPIEncodeH264Options *opt = ctx->codec_options;
  923. priv->mb_width = FFALIGN(avctx->width, 16) / 16;
  924. priv->mb_height = FFALIGN(avctx->height, 16) / 16;
  925. if (ctx->va_rc_mode == VA_RC_CQP) {
  926. priv->fixed_qp_p = opt->qp;
  927. if (avctx->i_quant_factor > 0.0)
  928. priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
  929. avctx->i_quant_offset) + 0.5);
  930. else
  931. priv->fixed_qp_idr = priv->fixed_qp_p;
  932. if (avctx->b_quant_factor > 0.0)
  933. priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
  934. avctx->b_quant_offset) + 0.5);
  935. else
  936. priv->fixed_qp_b = priv->fixed_qp_p;
  937. av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
  938. "%d / %d / %d for IDR- / P- / B-frames.\n",
  939. priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
  940. } else if (ctx->va_rc_mode == VA_RC_CBR ||
  941. ctx->va_rc_mode == VA_RC_VBR) {
  942. // These still need to be set for pic_init_qp/slice_qp_delta.
  943. priv->fixed_qp_idr = 26;
  944. priv->fixed_qp_p = 26;
  945. priv->fixed_qp_b = 26;
  946. av_log(avctx, AV_LOG_DEBUG, "Using %s-bitrate = %d bps.\n",
  947. ctx->va_rc_mode == VA_RC_CBR ? "constant" : "variable",
  948. avctx->bit_rate);
  949. } else {
  950. av_assert0(0 && "Invalid RC mode.");
  951. }
  952. if (opt->quality > 0) {
  953. #if VA_CHECK_VERSION(0, 36, 0)
  954. priv->quality_params.misc.type =
  955. VAEncMiscParameterTypeQualityLevel;
  956. priv->quality_params.quality.quality_level = opt->quality;
  957. ctx->global_params[ctx->nb_global_params] =
  958. &priv->quality_params.misc;
  959. ctx->global_params_size[ctx->nb_global_params++] =
  960. sizeof(priv->quality_params);
  961. #else
  962. av_log(avctx, AV_LOG_WARNING, "The encode quality option is not "
  963. "supported with this VAAPI version.\n");
  964. #endif
  965. }
  966. return 0;
  967. }
  968. static const VAAPIEncodeType vaapi_encode_type_h264 = {
  969. .priv_data_size = sizeof(VAAPIEncodeH264Context),
  970. .configure = &vaapi_encode_h264_configure,
  971. .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
  972. .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
  973. .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
  974. .init_picture_params = &vaapi_encode_h264_init_picture_params,
  975. .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
  976. .init_slice_params = &vaapi_encode_h264_init_slice_params,
  977. .sequence_header_type = VAEncPackedHeaderSequence,
  978. .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
  979. .slice_header_type = VAEncPackedHeaderH264_Slice,
  980. .write_slice_header = &vaapi_encode_h264_write_slice_header,
  981. .write_extra_header = &vaapi_encode_h264_write_extra_header,
  982. };
  983. static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
  984. {
  985. VAAPIEncodeContext *ctx = avctx->priv_data;
  986. VAAPIEncodeH264Options *opt =
  987. (VAAPIEncodeH264Options*)ctx->codec_options_data;
  988. ctx->codec = &vaapi_encode_type_h264;
  989. switch (avctx->profile) {
  990. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  991. ctx->va_profile = VAProfileH264ConstrainedBaseline;
  992. break;
  993. case FF_PROFILE_H264_BASELINE:
  994. ctx->va_profile = VAProfileH264Baseline;
  995. break;
  996. case FF_PROFILE_H264_MAIN:
  997. ctx->va_profile = VAProfileH264Main;
  998. break;
  999. case FF_PROFILE_H264_EXTENDED:
  1000. av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
  1001. "is not supported.\n");
  1002. return AVERROR_PATCHWELCOME;
  1003. case FF_PROFILE_UNKNOWN:
  1004. case FF_PROFILE_H264_HIGH:
  1005. ctx->va_profile = VAProfileH264High;
  1006. break;
  1007. case FF_PROFILE_H264_HIGH_10:
  1008. case FF_PROFILE_H264_HIGH_10_INTRA:
  1009. av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
  1010. "are not supported.\n");
  1011. return AVERROR_PATCHWELCOME;
  1012. case FF_PROFILE_H264_HIGH_422:
  1013. case FF_PROFILE_H264_HIGH_422_INTRA:
  1014. case FF_PROFILE_H264_HIGH_444:
  1015. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  1016. case FF_PROFILE_H264_HIGH_444_INTRA:
  1017. case FF_PROFILE_H264_CAVLC_444:
  1018. av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
  1019. "are not supported.\n");
  1020. return AVERROR_PATCHWELCOME;
  1021. default:
  1022. av_log(avctx, AV_LOG_ERROR, "Unknown H.264 profile %d.\n",
  1023. avctx->profile);
  1024. return AVERROR(EINVAL);
  1025. }
  1026. if (opt->low_power) {
  1027. #if VA_CHECK_VERSION(0, 39, 2)
  1028. ctx->va_entrypoint = VAEntrypointEncSliceLP;
  1029. #else
  1030. av_log(avctx, AV_LOG_ERROR, "Low-power encoding is not "
  1031. "supported with this VAAPI version.\n");
  1032. return AVERROR(EINVAL);
  1033. #endif
  1034. } else {
  1035. ctx->va_entrypoint = VAEntrypointEncSlice;
  1036. }
  1037. // Only 8-bit encode is supported.
  1038. ctx->va_rt_format = VA_RT_FORMAT_YUV420;
  1039. if (avctx->bit_rate > 0) {
  1040. if (avctx->rc_max_rate == avctx->bit_rate)
  1041. ctx->va_rc_mode = VA_RC_CBR;
  1042. else
  1043. ctx->va_rc_mode = VA_RC_VBR;
  1044. } else
  1045. ctx->va_rc_mode = VA_RC_CQP;
  1046. ctx->va_packed_headers =
  1047. VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
  1048. VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
  1049. VA_ENC_PACKED_HEADER_MISC; // SEI.
  1050. ctx->surface_width = FFALIGN(avctx->width, 16);
  1051. ctx->surface_height = FFALIGN(avctx->height, 16);
  1052. return ff_vaapi_encode_init(avctx);
  1053. }
  1054. #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
  1055. offsetof(VAAPIEncodeH264Options, x))
  1056. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  1057. static const AVOption vaapi_encode_h264_options[] = {
  1058. { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
  1059. OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
  1060. { "quality", "Set encode quality (trades off against speed, higher is faster)",
  1061. OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8, FLAGS },
  1062. { "low_power", "Use low-power encoding mode (experimental: only supported "
  1063. "on some platforms, does not support all features)",
  1064. OFFSET(low_power), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  1065. { NULL },
  1066. };
  1067. static const AVCodecDefault vaapi_encode_h264_defaults[] = {
  1068. { "profile", "100" },
  1069. { "level", "51" },
  1070. { "b", "0" },
  1071. { "bf", "2" },
  1072. { "g", "120" },
  1073. { "i_qfactor", "1.0" },
  1074. { "i_qoffset", "0.0" },
  1075. { "b_qfactor", "1.2" },
  1076. { "b_qoffset", "0.0" },
  1077. { "qmin", "0" },
  1078. { NULL },
  1079. };
  1080. static const AVClass vaapi_encode_h264_class = {
  1081. .class_name = "h264_vaapi",
  1082. .item_name = av_default_item_name,
  1083. .option = vaapi_encode_h264_options,
  1084. .version = LIBAVUTIL_VERSION_INT,
  1085. };
  1086. AVCodec ff_h264_vaapi_encoder = {
  1087. .name = "h264_vaapi",
  1088. .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
  1089. .type = AVMEDIA_TYPE_VIDEO,
  1090. .id = AV_CODEC_ID_H264,
  1091. .priv_data_size = (sizeof(VAAPIEncodeContext) +
  1092. sizeof(VAAPIEncodeH264Options)),
  1093. .init = &vaapi_encode_h264_init,
  1094. .encode2 = &ff_vaapi_encode2,
  1095. .close = &ff_vaapi_encode_close,
  1096. .priv_class = &vaapi_encode_h264_class,
  1097. .capabilities = AV_CODEC_CAP_DELAY,
  1098. .defaults = vaapi_encode_h264_defaults,
  1099. .pix_fmts = (const enum AVPixelFormat[]) {
  1100. AV_PIX_FMT_VAAPI,
  1101. AV_PIX_FMT_NONE,
  1102. },
  1103. };