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.

848 lines
29KB

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