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.

1308 lines
46KB

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