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.

978 lines
34KB

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