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.

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