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.

935 lines
32KB

  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 "internal.h"
  27. #include "vaapi_encode.h"
  28. #include "vaapi_encode_h26x.h"
  29. enum {
  30. SLICE_TYPE_P = 0,
  31. SLICE_TYPE_B = 1,
  32. SLICE_TYPE_I = 2,
  33. SLICE_TYPE_SP = 3,
  34. SLICE_TYPE_SI = 4,
  35. };
  36. // This structure contains all possibly-useful per-sequence syntax elements
  37. // which are not already contained in the various VAAPI structures.
  38. typedef struct VAAPIEncodeH264MiscSequenceParams {
  39. unsigned int profile_idc;
  40. char constraint_set0_flag;
  41. char constraint_set1_flag;
  42. char constraint_set2_flag;
  43. char constraint_set3_flag;
  44. char constraint_set4_flag;
  45. char constraint_set5_flag;
  46. char separate_colour_plane_flag;
  47. char qpprime_y_zero_transform_bypass_flag;
  48. char gaps_in_frame_num_allowed_flag;
  49. char delta_pic_order_always_zero_flag;
  50. char bottom_field_pic_order_in_frame_present_flag;
  51. unsigned int num_slice_groups_minus1;
  52. unsigned int slice_group_map_type;
  53. int pic_init_qs_minus26;
  54. char vui_parameters_present_flag;
  55. } VAAPIEncodeH264MiscSequenceParams;
  56. // This structure contains all possibly-useful per-slice syntax elements
  57. // which are not already contained in the various VAAPI structures.
  58. typedef struct VAAPIEncodeH264MiscSliceParams {
  59. unsigned int nal_unit_type;
  60. unsigned int nal_ref_idc;
  61. unsigned int colour_plane_id;
  62. char field_pic_flag;
  63. char bottom_field_flag;
  64. unsigned int redundant_pic_cnt;
  65. char sp_for_switch_flag;
  66. int slice_qs_delta;
  67. char ref_pic_list_modification_flag_l0;
  68. char ref_pic_list_modification_flag_l1;
  69. char no_output_of_prior_pics_flag;
  70. char long_term_reference_flag;
  71. char adaptive_ref_pic_marking_mode_flag;
  72. } VAAPIEncodeH264MiscSliceParams;
  73. typedef struct VAAPIEncodeH264Slice {
  74. VAAPIEncodeH264MiscSliceParams misc_slice_params;
  75. } VAAPIEncodeH264Slice;
  76. typedef struct VAAPIEncodeH264Context {
  77. VAAPIEncodeH264MiscSequenceParams misc_sequence_params;
  78. int mb_width;
  79. int mb_height;
  80. int fixed_qp_idr;
  81. int fixed_qp_p;
  82. int fixed_qp_b;
  83. int64_t idr_pic_count;
  84. int64_t last_idr_frame;
  85. // Rate control configuration.
  86. struct {
  87. VAEncMiscParameterBuffer misc;
  88. VAEncMiscParameterRateControl rc;
  89. } rc_params;
  90. struct {
  91. VAEncMiscParameterBuffer misc;
  92. VAEncMiscParameterHRD hrd;
  93. } hrd_params;
  94. } VAAPIEncodeH264Context;
  95. #define vseq_var(name) vseq->name, name
  96. #define vseq_field(name) vseq->seq_fields.bits.name, name
  97. #define vpic_var(name) vpic->name, name
  98. #define vpic_field(name) vpic->pic_fields.bits.name, name
  99. #define vslice_var(name) vslice->name, name
  100. #define vslice_field(name) vslice->slice_fields.bits.name, name
  101. #define mseq_var(name) mseq->name, name
  102. #define mslice_var(name) mslice->name, name
  103. static void vaapi_encode_h264_write_nal_header(PutBitContext *pbc,
  104. int nal_unit_type, int nal_ref_idc)
  105. {
  106. u(1, 0, forbidden_zero_bit);
  107. u(2, nal_ref_idc, nal_ref_idc);
  108. u(5, nal_unit_type, nal_unit_type);
  109. }
  110. static void vaapi_encode_h264_write_trailing_rbsp(PutBitContext *pbc)
  111. {
  112. u(1, 1, rbsp_stop_one_bit);
  113. while (put_bits_count(pbc) & 7)
  114. u(1, 0, rbsp_alignment_zero_bit);
  115. }
  116. static void vaapi_encode_h264_write_sps(PutBitContext *pbc,
  117. VAAPIEncodeContext *ctx)
  118. {
  119. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  120. VAAPIEncodeH264Context *priv = ctx->priv_data;
  121. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  122. int i;
  123. vaapi_encode_h264_write_nal_header(pbc, NAL_SPS, 3);
  124. u(8, mseq_var(profile_idc));
  125. u(1, mseq_var(constraint_set0_flag));
  126. u(1, mseq_var(constraint_set1_flag));
  127. u(1, mseq_var(constraint_set2_flag));
  128. u(1, mseq_var(constraint_set3_flag));
  129. u(1, mseq_var(constraint_set4_flag));
  130. u(1, mseq_var(constraint_set5_flag));
  131. u(2, 0, reserved_zero_2bits);
  132. u(8, vseq_var(level_idc));
  133. ue(vseq_var(seq_parameter_set_id));
  134. if (mseq->profile_idc == 100 || mseq->profile_idc == 110 ||
  135. mseq->profile_idc == 122 || mseq->profile_idc == 244 ||
  136. mseq->profile_idc == 44 || mseq->profile_idc == 83 ||
  137. mseq->profile_idc == 86 || mseq->profile_idc == 118 ||
  138. mseq->profile_idc == 128 || mseq->profile_idc == 138) {
  139. ue(vseq_field(chroma_format_idc));
  140. if (vseq->seq_fields.bits.chroma_format_idc == 3)
  141. u(1, mseq_var(separate_colour_plane_flag));
  142. ue(vseq_var(bit_depth_luma_minus8));
  143. ue(vseq_var(bit_depth_chroma_minus8));
  144. u(1, mseq_var(qpprime_y_zero_transform_bypass_flag));
  145. u(1, vseq_field(seq_scaling_matrix_present_flag));
  146. if (vseq->seq_fields.bits.seq_scaling_matrix_present_flag) {
  147. av_assert0(0 && "scaling matrices not supported");
  148. }
  149. }
  150. ue(vseq_field(log2_max_frame_num_minus4));
  151. ue(vseq_field(pic_order_cnt_type));
  152. if (vseq->seq_fields.bits.pic_order_cnt_type == 0) {
  153. ue(vseq_field(log2_max_pic_order_cnt_lsb_minus4));
  154. } else if (vseq->seq_fields.bits.pic_order_cnt_type == 1) {
  155. u(1, mseq_var(delta_pic_order_always_zero_flag));
  156. se(vseq_var(offset_for_non_ref_pic));
  157. se(vseq_var(offset_for_top_to_bottom_field));
  158. ue(vseq_var(num_ref_frames_in_pic_order_cnt_cycle));
  159. for (i = 0; i < vseq->num_ref_frames_in_pic_order_cnt_cycle; i++)
  160. se(vseq_var(offset_for_ref_frame[i]));
  161. }
  162. ue(vseq_var(max_num_ref_frames));
  163. u(1, mseq_var(gaps_in_frame_num_allowed_flag));
  164. ue(vseq->picture_width_in_mbs - 1, pic_width_in_mbs_minus1);
  165. ue(vseq->picture_height_in_mbs - 1, pic_height_in_mbs_minus1);
  166. u(1, vseq_field(frame_mbs_only_flag));
  167. if (!vseq->seq_fields.bits.frame_mbs_only_flag)
  168. u(1, vseq_field(mb_adaptive_frame_field_flag));
  169. u(1, vseq_field(direct_8x8_inference_flag));
  170. u(1, vseq_var(frame_cropping_flag));
  171. if (vseq->frame_cropping_flag) {
  172. ue(vseq_var(frame_crop_left_offset));
  173. ue(vseq_var(frame_crop_right_offset));
  174. ue(vseq_var(frame_crop_top_offset));
  175. ue(vseq_var(frame_crop_bottom_offset));
  176. }
  177. u(1, mseq_var(vui_parameters_present_flag));
  178. vaapi_encode_h264_write_trailing_rbsp(pbc);
  179. }
  180. static void vaapi_encode_h264_write_pps(PutBitContext *pbc,
  181. VAAPIEncodeContext *ctx)
  182. {
  183. VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
  184. VAAPIEncodeH264Context *priv = ctx->priv_data;
  185. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  186. vaapi_encode_h264_write_nal_header(pbc, NAL_PPS, 3);
  187. ue(vpic_var(pic_parameter_set_id));
  188. ue(vpic_var(seq_parameter_set_id));
  189. u(1, vpic_field(entropy_coding_mode_flag));
  190. u(1, mseq_var(bottom_field_pic_order_in_frame_present_flag));
  191. ue(mseq_var(num_slice_groups_minus1));
  192. if (mseq->num_slice_groups_minus1 > 0) {
  193. ue(mseq_var(slice_group_map_type));
  194. av_assert0(0 && "slice groups not supported");
  195. }
  196. ue(vpic_var(num_ref_idx_l0_active_minus1));
  197. ue(vpic_var(num_ref_idx_l1_active_minus1));
  198. u(1, vpic_field(weighted_pred_flag));
  199. u(2, vpic_field(weighted_bipred_idc));
  200. se(vpic->pic_init_qp - 26, pic_init_qp_minus26);
  201. se(mseq_var(pic_init_qs_minus26));
  202. se(vpic_var(chroma_qp_index_offset));
  203. u(1, vpic_field(deblocking_filter_control_present_flag));
  204. u(1, vpic_field(constrained_intra_pred_flag));
  205. u(1, vpic_field(redundant_pic_cnt_present_flag));
  206. u(1, vpic_field(transform_8x8_mode_flag));
  207. u(1, vpic_field(pic_scaling_matrix_present_flag));
  208. if (vpic->pic_fields.bits.pic_scaling_matrix_present_flag) {
  209. av_assert0(0 && "scaling matrices not supported");
  210. }
  211. se(vpic_var(second_chroma_qp_index_offset));
  212. vaapi_encode_h264_write_trailing_rbsp(pbc);
  213. }
  214. static void vaapi_encode_h264_write_slice_header2(PutBitContext *pbc,
  215. VAAPIEncodeContext *ctx,
  216. VAAPIEncodePicture *pic,
  217. VAAPIEncodeSlice *slice)
  218. {
  219. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  220. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  221. VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
  222. VAAPIEncodeH264Context *priv = ctx->priv_data;
  223. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  224. VAAPIEncodeH264Slice *pslice = slice->priv_data;
  225. VAAPIEncodeH264MiscSliceParams *mslice = &pslice->misc_slice_params;
  226. vaapi_encode_h264_write_nal_header(pbc, mslice->nal_unit_type,
  227. mslice->nal_ref_idc);
  228. ue(vslice->macroblock_address, first_mb_in_slice);
  229. ue(vslice_var(slice_type));
  230. ue(vpic_var(pic_parameter_set_id));
  231. if (mseq->separate_colour_plane_flag) {
  232. u(2, mslice_var(colour_plane_id));
  233. }
  234. u(4 + vseq->seq_fields.bits.log2_max_frame_num_minus4,
  235. (vpic->frame_num &
  236. ((1 << (4 + vseq->seq_fields.bits.log2_max_frame_num_minus4)) - 1)),
  237. frame_num);
  238. if (!vseq->seq_fields.bits.frame_mbs_only_flag) {
  239. u(1, mslice_var(field_pic_flag));
  240. if (mslice->field_pic_flag)
  241. u(1, mslice_var(bottom_field_flag));
  242. }
  243. if (vpic->pic_fields.bits.idr_pic_flag) {
  244. ue(vslice_var(idr_pic_id));
  245. }
  246. if (vseq->seq_fields.bits.pic_order_cnt_type == 0) {
  247. u(4 + vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4,
  248. vslice_var(pic_order_cnt_lsb));
  249. if (mseq->bottom_field_pic_order_in_frame_present_flag &&
  250. !mslice->field_pic_flag) {
  251. se(vslice_var(delta_pic_order_cnt_bottom));
  252. }
  253. }
  254. if (vseq->seq_fields.bits.pic_order_cnt_type == 1 &&
  255. !vseq->seq_fields.bits.delta_pic_order_always_zero_flag) {
  256. se(vslice_var(delta_pic_order_cnt[0]));
  257. if (mseq->bottom_field_pic_order_in_frame_present_flag &&
  258. !mslice->field_pic_flag) {
  259. se(vslice_var(delta_pic_order_cnt[1]));
  260. }
  261. }
  262. if (vpic->pic_fields.bits.redundant_pic_cnt_present_flag) {
  263. ue(mslice_var(redundant_pic_cnt));
  264. }
  265. if (vslice->slice_type == SLICE_TYPE_B) {
  266. u(1, vslice_var(direct_spatial_mv_pred_flag));
  267. }
  268. if (vslice->slice_type == SLICE_TYPE_P ||
  269. vslice->slice_type == SLICE_TYPE_SP ||
  270. vslice->slice_type == SLICE_TYPE_B) {
  271. u(1, vslice_var(num_ref_idx_active_override_flag));
  272. if (vslice->num_ref_idx_active_override_flag) {
  273. ue(vslice_var(num_ref_idx_l0_active_minus1));
  274. if (vslice->slice_type == SLICE_TYPE_B)
  275. ue(vslice_var(num_ref_idx_l1_active_minus1));
  276. }
  277. }
  278. if (mslice->nal_unit_type == 20 || mslice->nal_unit_type == 21) {
  279. av_assert0(0 && "no MVC support");
  280. } else {
  281. if (vslice->slice_type % 5 != 2 && vslice->slice_type % 5 != 4) {
  282. u(1, mslice_var(ref_pic_list_modification_flag_l0));
  283. if (mslice->ref_pic_list_modification_flag_l0) {
  284. av_assert0(0 && "ref pic list modification");
  285. }
  286. }
  287. if (vslice->slice_type % 5 == 1) {
  288. u(1, mslice_var(ref_pic_list_modification_flag_l1));
  289. if (mslice->ref_pic_list_modification_flag_l1) {
  290. av_assert0(0 && "ref pic list modification");
  291. }
  292. }
  293. }
  294. if ((vpic->pic_fields.bits.weighted_pred_flag &&
  295. (vslice->slice_type == SLICE_TYPE_P ||
  296. vslice->slice_type == SLICE_TYPE_SP)) ||
  297. (vpic->pic_fields.bits.weighted_bipred_idc == 1 &&
  298. vslice->slice_type == SLICE_TYPE_B)) {
  299. av_assert0(0 && "prediction weights not supported");
  300. }
  301. av_assert0(mslice->nal_ref_idc > 0 ==
  302. vpic->pic_fields.bits.reference_pic_flag);
  303. if (mslice->nal_ref_idc != 0) {
  304. if (vpic->pic_fields.bits.idr_pic_flag) {
  305. u(1, mslice_var(no_output_of_prior_pics_flag));
  306. u(1, mslice_var(long_term_reference_flag));
  307. } else {
  308. u(1, mslice_var(adaptive_ref_pic_marking_mode_flag));
  309. if (mslice->adaptive_ref_pic_marking_mode_flag) {
  310. av_assert0(0 && "MMCOs not supported");
  311. }
  312. }
  313. }
  314. if (vpic->pic_fields.bits.entropy_coding_mode_flag &&
  315. vslice->slice_type != SLICE_TYPE_I &&
  316. vslice->slice_type != SLICE_TYPE_SI) {
  317. ue(vslice_var(cabac_init_idc));
  318. }
  319. se(vslice_var(slice_qp_delta));
  320. if (vslice->slice_type == SLICE_TYPE_SP ||
  321. vslice->slice_type == SLICE_TYPE_SI) {
  322. if (vslice->slice_type == SLICE_TYPE_SP)
  323. u(1, mslice_var(sp_for_switch_flag));
  324. se(mslice_var(slice_qs_delta));
  325. }
  326. if (vpic->pic_fields.bits.deblocking_filter_control_present_flag) {
  327. ue(vslice_var(disable_deblocking_filter_idc));
  328. if (vslice->disable_deblocking_filter_idc != 1) {
  329. se(vslice_var(slice_alpha_c0_offset_div2));
  330. se(vslice_var(slice_beta_offset_div2));
  331. }
  332. }
  333. if (mseq->num_slice_groups_minus1 > 0 &&
  334. mseq->slice_group_map_type >= 3 && mseq->slice_group_map_type <= 5) {
  335. av_assert0(0 && "slice groups not supported");
  336. }
  337. // No alignment - this need not be a byte boundary.
  338. }
  339. static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
  340. char *data, size_t *data_len)
  341. {
  342. VAAPIEncodeContext *ctx = avctx->priv_data;
  343. PutBitContext pbc;
  344. char tmp[256];
  345. int err;
  346. size_t nal_len, bit_len, bit_pos, next_len;
  347. bit_len = *data_len;
  348. bit_pos = 0;
  349. init_put_bits(&pbc, tmp, sizeof(tmp));
  350. vaapi_encode_h264_write_sps(&pbc, ctx);
  351. nal_len = put_bits_count(&pbc);
  352. flush_put_bits(&pbc);
  353. next_len = bit_len - bit_pos;
  354. err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
  355. &next_len,
  356. tmp, nal_len);
  357. if (err < 0)
  358. return err;
  359. bit_pos += next_len;
  360. init_put_bits(&pbc, tmp, sizeof(tmp));
  361. vaapi_encode_h264_write_pps(&pbc, ctx);
  362. nal_len = put_bits_count(&pbc);
  363. flush_put_bits(&pbc);
  364. next_len = bit_len - bit_pos;
  365. err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
  366. &next_len,
  367. tmp, nal_len);
  368. if (err < 0)
  369. return err;
  370. bit_pos += next_len;
  371. *data_len = bit_pos;
  372. return 0;
  373. }
  374. static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
  375. VAAPIEncodePicture *pic,
  376. VAAPIEncodeSlice *slice,
  377. char *data, size_t *data_len)
  378. {
  379. VAAPIEncodeContext *ctx = avctx->priv_data;
  380. PutBitContext pbc;
  381. char tmp[256];
  382. size_t header_len;
  383. init_put_bits(&pbc, tmp, sizeof(tmp));
  384. vaapi_encode_h264_write_slice_header2(&pbc, ctx, pic, slice);
  385. header_len = put_bits_count(&pbc);
  386. flush_put_bits(&pbc);
  387. return ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data, data_len,
  388. tmp, header_len);
  389. }
  390. static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
  391. {
  392. VAAPIEncodeContext *ctx = avctx->priv_data;
  393. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  394. VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
  395. VAAPIEncodeH264Context *priv = ctx->priv_data;
  396. VAAPIEncodeH264MiscSequenceParams *mseq = &priv->misc_sequence_params;
  397. int i;
  398. {
  399. vseq->seq_parameter_set_id = 0;
  400. vseq->level_idc = avctx->level;
  401. vseq->max_num_ref_frames = 2;
  402. vseq->picture_width_in_mbs = priv->mb_width;
  403. vseq->picture_height_in_mbs = priv->mb_height;
  404. vseq->seq_fields.bits.chroma_format_idc = 1;
  405. vseq->seq_fields.bits.frame_mbs_only_flag = 1;
  406. vseq->seq_fields.bits.direct_8x8_inference_flag = 1;
  407. vseq->seq_fields.bits.log2_max_frame_num_minus4 = 4;
  408. vseq->seq_fields.bits.pic_order_cnt_type = 0;
  409. if (ctx->input_width != ctx->aligned_width ||
  410. ctx->input_height != ctx->aligned_height) {
  411. vseq->frame_cropping_flag = 1;
  412. vseq->frame_crop_left_offset = 0;
  413. vseq->frame_crop_right_offset =
  414. (ctx->aligned_width - ctx->input_width) / 2;
  415. vseq->frame_crop_top_offset = 0;
  416. vseq->frame_crop_bottom_offset =
  417. (ctx->aligned_height - ctx->input_height) / 2;
  418. } else {
  419. vseq->frame_cropping_flag = 0;
  420. }
  421. vseq->bits_per_second = avctx->bit_rate;
  422. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  423. vseq->num_units_in_tick = avctx->framerate.num;
  424. vseq->time_scale = 2 * avctx->framerate.den;
  425. } else {
  426. vseq->num_units_in_tick = avctx->time_base.num;
  427. vseq->time_scale = 2 * avctx->time_base.den;
  428. }
  429. vseq->intra_period = ctx->p_per_i * (ctx->b_per_p + 1);
  430. vseq->intra_idr_period = vseq->intra_period;
  431. vseq->ip_period = ctx->b_per_p + 1;
  432. }
  433. {
  434. vpic->CurrPic.picture_id = VA_INVALID_ID;
  435. vpic->CurrPic.flags = VA_PICTURE_H264_INVALID;
  436. for (i = 0; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
  437. vpic->ReferenceFrames[i].picture_id = VA_INVALID_ID;
  438. vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
  439. }
  440. vpic->coded_buf = VA_INVALID_ID;
  441. vpic->pic_parameter_set_id = 0;
  442. vpic->seq_parameter_set_id = 0;
  443. vpic->num_ref_idx_l0_active_minus1 = 0;
  444. vpic->num_ref_idx_l1_active_minus1 = 0;
  445. vpic->pic_fields.bits.entropy_coding_mode_flag =
  446. ((avctx->profile & 0xff) != 66);
  447. vpic->pic_fields.bits.weighted_pred_flag = 0;
  448. vpic->pic_fields.bits.weighted_bipred_idc = 0;
  449. vpic->pic_fields.bits.transform_8x8_mode_flag =
  450. ((avctx->profile & 0xff) >= 100);
  451. vpic->pic_init_qp = priv->fixed_qp_idr;
  452. }
  453. {
  454. mseq->profile_idc = avctx->profile & 0xff;
  455. if (avctx->profile & FF_PROFILE_H264_CONSTRAINED)
  456. mseq->constraint_set1_flag = 1;
  457. if (avctx->profile & FF_PROFILE_H264_INTRA)
  458. mseq->constraint_set3_flag = 1;
  459. }
  460. return 0;
  461. }
  462. static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
  463. VAAPIEncodePicture *pic)
  464. {
  465. VAAPIEncodeContext *ctx = avctx->priv_data;
  466. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  467. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  468. VAAPIEncodeH264Context *priv = ctx->priv_data;
  469. int i;
  470. if (pic->type == PICTURE_TYPE_IDR) {
  471. av_assert0(pic->display_order == pic->encode_order);
  472. priv->last_idr_frame = pic->display_order;
  473. } else {
  474. av_assert0(pic->display_order > priv->last_idr_frame);
  475. }
  476. vpic->frame_num = (pic->encode_order - priv->last_idr_frame) &
  477. ((1 << (4 + vseq->seq_fields.bits.log2_max_frame_num_minus4)) - 1);
  478. vpic->CurrPic.picture_id = pic->recon_surface;
  479. vpic->CurrPic.frame_idx = vpic->frame_num;
  480. vpic->CurrPic.flags = 0;
  481. vpic->CurrPic.TopFieldOrderCnt = pic->display_order;
  482. vpic->CurrPic.BottomFieldOrderCnt = pic->display_order;
  483. for (i = 0; i < pic->nb_refs; i++) {
  484. VAAPIEncodePicture *ref = pic->refs[i];
  485. av_assert0(ref && ref->encode_order >= priv->last_idr_frame);
  486. vpic->ReferenceFrames[i].picture_id = ref->recon_surface;
  487. vpic->ReferenceFrames[i].frame_idx =
  488. ref->encode_order - priv->last_idr_frame;
  489. vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
  490. vpic->ReferenceFrames[i].TopFieldOrderCnt = ref->display_order;
  491. vpic->ReferenceFrames[i].BottomFieldOrderCnt = ref->display_order;
  492. }
  493. for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
  494. vpic->ReferenceFrames[i].picture_id = VA_INVALID_ID;
  495. vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
  496. }
  497. vpic->coded_buf = pic->output_buffer;
  498. vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
  499. vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
  500. pic->nb_slices = 1;
  501. return 0;
  502. }
  503. static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
  504. VAAPIEncodePicture *pic,
  505. VAAPIEncodeSlice *slice)
  506. {
  507. VAAPIEncodeContext *ctx = avctx->priv_data;
  508. VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
  509. VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
  510. VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
  511. VAAPIEncodeH264Context *priv = ctx->priv_data;
  512. VAAPIEncodeH264Slice *pslice;
  513. VAAPIEncodeH264MiscSliceParams *mslice;
  514. int i;
  515. slice->priv_data = av_mallocz(sizeof(*pslice));
  516. if (!slice->priv_data)
  517. return AVERROR(ENOMEM);
  518. pslice = slice->priv_data;
  519. mslice = &pslice->misc_slice_params;
  520. if (pic->type == PICTURE_TYPE_IDR)
  521. mslice->nal_unit_type = NAL_IDR_SLICE;
  522. else
  523. mslice->nal_unit_type = NAL_SLICE;
  524. switch (pic->type) {
  525. case PICTURE_TYPE_IDR:
  526. vslice->slice_type = SLICE_TYPE_I;
  527. mslice->nal_ref_idc = 3;
  528. break;
  529. case PICTURE_TYPE_I:
  530. vslice->slice_type = SLICE_TYPE_I;
  531. mslice->nal_ref_idc = 2;
  532. break;
  533. case PICTURE_TYPE_P:
  534. vslice->slice_type = SLICE_TYPE_P;
  535. mslice->nal_ref_idc = 1;
  536. break;
  537. case PICTURE_TYPE_B:
  538. vslice->slice_type = SLICE_TYPE_B;
  539. mslice->nal_ref_idc = 0;
  540. break;
  541. default:
  542. av_assert0(0 && "invalid picture type");
  543. }
  544. // Only one slice per frame.
  545. vslice->macroblock_address = 0;
  546. vslice->num_macroblocks = priv->mb_width * priv->mb_height;
  547. vslice->macroblock_info = VA_INVALID_ID;
  548. vslice->pic_parameter_set_id = vpic->pic_parameter_set_id;
  549. vslice->idr_pic_id = priv->idr_pic_count++;
  550. vslice->pic_order_cnt_lsb = pic->display_order &
  551. ((1 << (4 + vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4)) - 1);
  552. for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
  553. vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
  554. vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
  555. vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
  556. vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
  557. }
  558. av_assert0(pic->nb_refs <= 2);
  559. if (pic->nb_refs >= 1) {
  560. // Backward reference for P or B frame.
  561. av_assert0(pic->type == PICTURE_TYPE_P ||
  562. pic->type == PICTURE_TYPE_B);
  563. vslice->num_ref_idx_l0_active_minus1 = 0;
  564. vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
  565. }
  566. if (pic->nb_refs >= 2) {
  567. // Forward reference for B frame.
  568. av_assert0(pic->type == PICTURE_TYPE_B);
  569. vslice->num_ref_idx_l1_active_minus1 = 0;
  570. vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
  571. }
  572. if (pic->type == PICTURE_TYPE_B)
  573. vslice->slice_qp_delta = priv->fixed_qp_b - vpic->pic_init_qp;
  574. else if (pic->type == PICTURE_TYPE_P)
  575. vslice->slice_qp_delta = priv->fixed_qp_p - vpic->pic_init_qp;
  576. else
  577. vslice->slice_qp_delta = priv->fixed_qp_idr - vpic->pic_init_qp;
  578. vslice->direct_spatial_mv_pred_flag = 1;
  579. return 0;
  580. }
  581. static av_cold int vaapi_encode_h264_init_constant_bitrate(AVCodecContext *avctx)
  582. {
  583. VAAPIEncodeContext *ctx = avctx->priv_data;
  584. VAAPIEncodeH264Context *priv = ctx->priv_data;
  585. int hrd_buffer_size;
  586. int hrd_initial_buffer_fullness;
  587. if (avctx->rc_buffer_size)
  588. hrd_buffer_size = avctx->rc_buffer_size;
  589. else
  590. hrd_buffer_size = avctx->bit_rate;
  591. if (avctx->rc_initial_buffer_occupancy)
  592. hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy;
  593. else
  594. hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
  595. priv->rc_params.misc.type = VAEncMiscParameterTypeRateControl;
  596. priv->rc_params.rc = (VAEncMiscParameterRateControl) {
  597. .bits_per_second = avctx->bit_rate,
  598. .target_percentage = 66,
  599. .window_size = 1000,
  600. .initial_qp = (avctx->qmax >= 0 ? avctx->qmax : 40),
  601. .min_qp = (avctx->qmin >= 0 ? avctx->qmin : 18),
  602. .basic_unit_size = 0,
  603. };
  604. ctx->global_params[ctx->nb_global_params] =
  605. &priv->rc_params.misc;
  606. ctx->global_params_size[ctx->nb_global_params++] =
  607. sizeof(priv->rc_params);
  608. priv->hrd_params.misc.type = VAEncMiscParameterTypeHRD;
  609. priv->hrd_params.hrd = (VAEncMiscParameterHRD) {
  610. .initial_buffer_fullness = hrd_initial_buffer_fullness,
  611. .buffer_size = hrd_buffer_size,
  612. };
  613. ctx->global_params[ctx->nb_global_params] =
  614. &priv->hrd_params.misc;
  615. ctx->global_params_size[ctx->nb_global_params++] =
  616. sizeof(priv->hrd_params);
  617. // These still need to be set for pic_init_qp/slice_qp_delta.
  618. priv->fixed_qp_idr = 26;
  619. priv->fixed_qp_p = 26;
  620. priv->fixed_qp_b = 26;
  621. av_log(avctx, AV_LOG_DEBUG, "Using constant-bitrate = %d bps.\n",
  622. avctx->bit_rate);
  623. return 0;
  624. }
  625. static av_cold int vaapi_encode_h264_init_fixed_qp(AVCodecContext *avctx)
  626. {
  627. VAAPIEncodeContext *ctx = avctx->priv_data;
  628. VAAPIEncodeH264Context *priv = ctx->priv_data;
  629. priv->fixed_qp_p = avctx->global_quality;
  630. if (avctx->i_quant_factor > 0.0)
  631. priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
  632. avctx->i_quant_offset) + 0.5);
  633. else
  634. priv->fixed_qp_idr = priv->fixed_qp_p;
  635. if (avctx->b_quant_factor > 0.0)
  636. priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
  637. avctx->b_quant_offset) + 0.5);
  638. else
  639. priv->fixed_qp_b = priv->fixed_qp_p;
  640. av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
  641. "%d / %d / %d for IDR / P / B frames.\n",
  642. priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
  643. return 0;
  644. }
  645. static av_cold int vaapi_encode_h264_init_internal(AVCodecContext *avctx)
  646. {
  647. static const VAConfigAttrib default_config_attributes[] = {
  648. { .type = VAConfigAttribRTFormat,
  649. .value = VA_RT_FORMAT_YUV420 },
  650. { .type = VAConfigAttribEncPackedHeaders,
  651. .value = (VA_ENC_PACKED_HEADER_SEQUENCE |
  652. VA_ENC_PACKED_HEADER_SLICE) },
  653. };
  654. VAAPIEncodeContext *ctx = avctx->priv_data;
  655. VAAPIEncodeH264Context *priv = ctx->priv_data;
  656. int i, err;
  657. switch (avctx->profile) {
  658. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  659. ctx->va_profile = VAProfileH264ConstrainedBaseline;
  660. break;
  661. case FF_PROFILE_H264_BASELINE:
  662. ctx->va_profile = VAProfileH264Baseline;
  663. break;
  664. case FF_PROFILE_H264_MAIN:
  665. ctx->va_profile = VAProfileH264Main;
  666. break;
  667. case FF_PROFILE_H264_EXTENDED:
  668. av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
  669. "is not supported.\n");
  670. return AVERROR_PATCHWELCOME;
  671. case FF_PROFILE_UNKNOWN:
  672. case FF_PROFILE_H264_HIGH:
  673. ctx->va_profile = VAProfileH264High;
  674. break;
  675. case FF_PROFILE_H264_HIGH_10:
  676. case FF_PROFILE_H264_HIGH_10_INTRA:
  677. av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
  678. "are not supported.\n");
  679. return AVERROR_PATCHWELCOME;
  680. case FF_PROFILE_H264_HIGH_422:
  681. case FF_PROFILE_H264_HIGH_422_INTRA:
  682. case FF_PROFILE_H264_HIGH_444:
  683. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  684. case FF_PROFILE_H264_HIGH_444_INTRA:
  685. case FF_PROFILE_H264_CAVLC_444:
  686. av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
  687. "are not supported.\n");
  688. return AVERROR_PATCHWELCOME;
  689. default:
  690. av_log(avctx, AV_LOG_ERROR, "Unknown H.264 profile %d.\n",
  691. avctx->profile);
  692. return AVERROR(EINVAL);
  693. }
  694. ctx->va_entrypoint = VAEntrypointEncSlice;
  695. ctx->input_width = avctx->width;
  696. ctx->input_height = avctx->height;
  697. ctx->aligned_width = FFALIGN(ctx->input_width, 16);
  698. ctx->aligned_height = FFALIGN(ctx->input_height, 16);
  699. priv->mb_width = ctx->aligned_width / 16;
  700. priv->mb_height = ctx->aligned_height / 16;
  701. for (i = 0; i < FF_ARRAY_ELEMS(default_config_attributes); i++) {
  702. ctx->config_attributes[ctx->nb_config_attributes++] =
  703. default_config_attributes[i];
  704. }
  705. if (avctx->bit_rate > 0) {
  706. ctx->va_rc_mode = VA_RC_CBR;
  707. err = vaapi_encode_h264_init_constant_bitrate(avctx);
  708. } else {
  709. ctx->va_rc_mode = VA_RC_CQP;
  710. err = vaapi_encode_h264_init_fixed_qp(avctx);
  711. }
  712. if (err < 0)
  713. return err;
  714. ctx->config_attributes[ctx->nb_config_attributes++] = (VAConfigAttrib) {
  715. .type = VAConfigAttribRateControl,
  716. .value = ctx->va_rc_mode,
  717. };
  718. ctx->nb_recon_frames = 20;
  719. return 0;
  720. }
  721. static VAAPIEncodeType vaapi_encode_type_h264 = {
  722. .priv_data_size = sizeof(VAAPIEncodeH264Context),
  723. .init = &vaapi_encode_h264_init_internal,
  724. .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
  725. .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
  726. .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
  727. .init_picture_params = &vaapi_encode_h264_init_picture_params,
  728. .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
  729. .init_slice_params = &vaapi_encode_h264_init_slice_params,
  730. .sequence_header_type = VAEncPackedHeaderSequence,
  731. .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
  732. .slice_header_type = VAEncPackedHeaderH264_Slice,
  733. .write_slice_header = &vaapi_encode_h264_write_slice_header,
  734. };
  735. static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
  736. {
  737. return ff_vaapi_encode_init(avctx, &vaapi_encode_type_h264);
  738. }
  739. static const AVCodecDefault vaapi_encode_h264_defaults[] = {
  740. { "profile", "100" },
  741. { "level", "51" },
  742. { "b", "0" },
  743. { "bf", "2" },
  744. { "g", "120" },
  745. { "global_quality", "20" },
  746. { "i_qfactor", "1.0" },
  747. { "i_qoffset", "0.0" },
  748. { "b_qfactor", "1.2" },
  749. { "b_qoffset", "0.0" },
  750. { NULL },
  751. };
  752. static const AVClass vaapi_encode_h264_class = {
  753. .class_name = "h264_vaapi",
  754. .item_name = av_default_item_name,
  755. .version = LIBAVUTIL_VERSION_INT,
  756. };
  757. AVCodec ff_h264_vaapi_encoder = {
  758. .name = "h264_vaapi",
  759. .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
  760. .type = AVMEDIA_TYPE_VIDEO,
  761. .id = AV_CODEC_ID_H264,
  762. .priv_data_size = sizeof(VAAPIEncodeContext),
  763. .init = &vaapi_encode_h264_init,
  764. .encode2 = &ff_vaapi_encode2,
  765. .close = &ff_vaapi_encode_close,
  766. .priv_class = &vaapi_encode_h264_class,
  767. .capabilities = AV_CODEC_CAP_DELAY,
  768. .defaults = vaapi_encode_h264_defaults,
  769. .pix_fmts = (const enum AVPixelFormat[]) {
  770. AV_PIX_FMT_VAAPI,
  771. AV_PIX_FMT_NONE,
  772. },
  773. };