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.

405 lines
17KB

  1. /*
  2. * H.264 HW decode acceleration through VA API
  3. *
  4. * Copyright (C) 2008-2009 Splitted-Desktop Systems
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "h264dec.h"
  23. #include "h264_ps.h"
  24. #include "hwaccel.h"
  25. #include "vaapi_decode.h"
  26. /**
  27. * @file
  28. * This file implements the glue code between Libav's and VA API's
  29. * structures for H.264 decoding.
  30. */
  31. /**
  32. * Initialize an empty VA API picture.
  33. *
  34. * VA API requires a fixed-size reference picture array.
  35. */
  36. static void init_vaapi_pic(VAPictureH264 *va_pic)
  37. {
  38. va_pic->picture_id = VA_INVALID_ID;
  39. va_pic->flags = VA_PICTURE_H264_INVALID;
  40. va_pic->TopFieldOrderCnt = 0;
  41. va_pic->BottomFieldOrderCnt = 0;
  42. }
  43. /**
  44. * Translate an Libav Picture into its VA API form.
  45. *
  46. * @param[out] va_pic A pointer to VA API's own picture struct
  47. * @param[in] pic A pointer to the Libav picture struct to convert
  48. * @param[in] pic_structure The picture field type (as defined in mpegvideo.h),
  49. * supersedes pic's field type if nonzero.
  50. */
  51. static void fill_vaapi_pic(VAPictureH264 *va_pic,
  52. const H264Picture *pic,
  53. int pic_structure)
  54. {
  55. if (pic_structure == 0)
  56. pic_structure = pic->reference;
  57. pic_structure &= PICT_FRAME; /* PICT_TOP_FIELD|PICT_BOTTOM_FIELD */
  58. va_pic->picture_id = ff_vaapi_get_surface_id(pic->f);
  59. va_pic->frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
  60. va_pic->flags = 0;
  61. if (pic_structure != PICT_FRAME)
  62. va_pic->flags |= (pic_structure & PICT_TOP_FIELD) ? VA_PICTURE_H264_TOP_FIELD : VA_PICTURE_H264_BOTTOM_FIELD;
  63. if (pic->reference)
  64. va_pic->flags |= pic->long_ref ? VA_PICTURE_H264_LONG_TERM_REFERENCE : VA_PICTURE_H264_SHORT_TERM_REFERENCE;
  65. va_pic->TopFieldOrderCnt = 0;
  66. if (pic->field_poc[0] != INT_MAX)
  67. va_pic->TopFieldOrderCnt = pic->field_poc[0];
  68. va_pic->BottomFieldOrderCnt = 0;
  69. if (pic->field_poc[1] != INT_MAX)
  70. va_pic->BottomFieldOrderCnt = pic->field_poc[1];
  71. }
  72. /** Decoded Picture Buffer (DPB). */
  73. typedef struct DPB {
  74. int size; ///< Current number of reference frames in the DPB
  75. int max_size; ///< Max number of reference frames. This is FF_ARRAY_ELEMS(VAPictureParameterBufferH264.ReferenceFrames)
  76. VAPictureH264 *va_pics; ///< Pointer to VAPictureParameterBufferH264.ReferenceFrames array
  77. } DPB;
  78. /**
  79. * Append picture to the decoded picture buffer, in a VA API form that
  80. * merges the second field picture attributes with the first, if
  81. * available. The decoded picture buffer's size must be large enough
  82. * to receive the new VA API picture object.
  83. */
  84. static int dpb_add(DPB *dpb, const H264Picture *pic)
  85. {
  86. int i;
  87. if (dpb->size >= dpb->max_size)
  88. return -1;
  89. for (i = 0; i < dpb->size; i++) {
  90. VAPictureH264 * const va_pic = &dpb->va_pics[i];
  91. if (va_pic->picture_id == ff_vaapi_get_surface_id(pic->f)) {
  92. VAPictureH264 temp_va_pic;
  93. fill_vaapi_pic(&temp_va_pic, pic, 0);
  94. if ((temp_va_pic.flags ^ va_pic->flags) & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD)) {
  95. va_pic->flags |= temp_va_pic.flags & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD);
  96. /* Merge second field */
  97. if (temp_va_pic.flags & VA_PICTURE_H264_TOP_FIELD) {
  98. va_pic->TopFieldOrderCnt = temp_va_pic.TopFieldOrderCnt;
  99. } else {
  100. va_pic->BottomFieldOrderCnt = temp_va_pic.BottomFieldOrderCnt;
  101. }
  102. }
  103. return 0;
  104. }
  105. }
  106. fill_vaapi_pic(&dpb->va_pics[dpb->size++], pic, 0);
  107. return 0;
  108. }
  109. /** Fill in VA API reference frames array. */
  110. static int fill_vaapi_ReferenceFrames(VAPictureParameterBufferH264 *pic_param,
  111. const H264Context *h)
  112. {
  113. DPB dpb;
  114. int i;
  115. dpb.size = 0;
  116. dpb.max_size = FF_ARRAY_ELEMS(pic_param->ReferenceFrames);
  117. dpb.va_pics = pic_param->ReferenceFrames;
  118. for (i = 0; i < dpb.max_size; i++)
  119. init_vaapi_pic(&dpb.va_pics[i]);
  120. for (i = 0; i < h->short_ref_count; i++) {
  121. const H264Picture *pic = h->short_ref[i];
  122. if (pic && pic->reference && dpb_add(&dpb, pic) < 0)
  123. return -1;
  124. }
  125. for (i = 0; i < 16; i++) {
  126. const H264Picture *pic = h->long_ref[i];
  127. if (pic && pic->reference && dpb_add(&dpb, pic) < 0)
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. /**
  133. * Fill in VA API reference picture lists from the Libav reference
  134. * picture list.
  135. *
  136. * @param[out] RefPicList VA API internal reference picture list
  137. * @param[in] ref_list A pointer to the Libav reference list
  138. * @param[in] ref_count The number of reference pictures in ref_list
  139. */
  140. static void fill_vaapi_RefPicList(VAPictureH264 RefPicList[32],
  141. const H264Ref *ref_list,
  142. unsigned int ref_count)
  143. {
  144. unsigned int i, n = 0;
  145. for (i = 0; i < ref_count; i++)
  146. if (ref_list[i].reference)
  147. fill_vaapi_pic(&RefPicList[n++], ref_list[i].parent,
  148. ref_list[i].reference);
  149. for (; n < 32; n++)
  150. init_vaapi_pic(&RefPicList[n]);
  151. }
  152. /**
  153. * Fill in prediction weight table.
  154. *
  155. * VA API requires a plain prediction weight table as it does not infer
  156. * any value.
  157. *
  158. * @param[in] h A pointer to the current H.264 context
  159. * @param[in] list The reference frame list index to use
  160. * @param[out] luma_weight_flag VA API plain luma weight flag
  161. * @param[out] luma_weight VA API plain luma weight table
  162. * @param[out] luma_offset VA API plain luma offset table
  163. * @param[out] chroma_weight_flag VA API plain chroma weight flag
  164. * @param[out] chroma_weight VA API plain chroma weight table
  165. * @param[out] chroma_offset VA API plain chroma offset table
  166. */
  167. static void fill_vaapi_plain_pred_weight_table(const H264Context *h,
  168. int list,
  169. unsigned char *luma_weight_flag,
  170. short luma_weight[32],
  171. short luma_offset[32],
  172. unsigned char *chroma_weight_flag,
  173. short chroma_weight[32][2],
  174. short chroma_offset[32][2])
  175. {
  176. const H264SliceContext *sl = &h->slice_ctx[0];
  177. unsigned int i, j;
  178. *luma_weight_flag = sl->pwt.luma_weight_flag[list];
  179. *chroma_weight_flag = sl->pwt.chroma_weight_flag[list];
  180. for (i = 0; i < sl->ref_count[list]; i++) {
  181. /* VA API also wants the inferred (default) values, not
  182. only what is available in the bitstream (7.4.3.2). */
  183. if (sl->pwt.luma_weight_flag[list]) {
  184. luma_weight[i] = sl->pwt.luma_weight[i][list][0];
  185. luma_offset[i] = sl->pwt.luma_weight[i][list][1];
  186. } else {
  187. luma_weight[i] = 1 << sl->pwt.luma_log2_weight_denom;
  188. luma_offset[i] = 0;
  189. }
  190. for (j = 0; j < 2; j++) {
  191. if (sl->pwt.chroma_weight_flag[list]) {
  192. chroma_weight[i][j] = sl->pwt.chroma_weight[i][list][j][0];
  193. chroma_offset[i][j] = sl->pwt.chroma_weight[i][list][j][1];
  194. } else {
  195. chroma_weight[i][j] = 1 << sl->pwt.chroma_log2_weight_denom;
  196. chroma_offset[i][j] = 0;
  197. }
  198. }
  199. }
  200. }
  201. /** Initialize and start decoding a frame with VA API. */
  202. static int vaapi_h264_start_frame(AVCodecContext *avctx,
  203. av_unused const uint8_t *buffer,
  204. av_unused uint32_t size)
  205. {
  206. const H264Context *h = avctx->priv_data;
  207. VAAPIDecodePicture *pic = h->cur_pic_ptr->hwaccel_picture_private;
  208. const PPS *pps = h->ps.pps;
  209. const SPS *sps = h->ps.sps;
  210. VAPictureParameterBufferH264 pic_param;
  211. VAIQMatrixBufferH264 iq_matrix;
  212. int err;
  213. pic->output_surface = ff_vaapi_get_surface_id(h->cur_pic_ptr->f);
  214. pic_param = (VAPictureParameterBufferH264) {
  215. .picture_width_in_mbs_minus1 = h->mb_width - 1,
  216. .picture_height_in_mbs_minus1 = h->mb_height - 1,
  217. .bit_depth_luma_minus8 = sps->bit_depth_luma - 8,
  218. .bit_depth_chroma_minus8 = sps->bit_depth_chroma - 8,
  219. .num_ref_frames = sps->ref_frame_count,
  220. .seq_fields.bits = {
  221. .chroma_format_idc = sps->chroma_format_idc,
  222. .residual_colour_transform_flag = sps->residual_color_transform_flag,
  223. .gaps_in_frame_num_value_allowed_flag = sps->gaps_in_frame_num_allowed_flag,
  224. .frame_mbs_only_flag = sps->frame_mbs_only_flag,
  225. .mb_adaptive_frame_field_flag = sps->mb_aff,
  226. .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
  227. .MinLumaBiPredSize8x8 = sps->level_idc >= 31, /* A.3.3.2 */
  228. .log2_max_frame_num_minus4 = sps->log2_max_frame_num - 4,
  229. .pic_order_cnt_type = sps->poc_type,
  230. .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4,
  231. .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
  232. },
  233. .num_slice_groups_minus1 = pps->slice_group_count - 1,
  234. .slice_group_map_type = pps->mb_slice_group_map_type,
  235. .slice_group_change_rate_minus1 = 0, /* FMO is not implemented */
  236. .pic_init_qp_minus26 = pps->init_qp - 26,
  237. .pic_init_qs_minus26 = pps->init_qs - 26,
  238. .chroma_qp_index_offset = pps->chroma_qp_index_offset[0],
  239. .second_chroma_qp_index_offset = pps->chroma_qp_index_offset[1],
  240. .pic_fields.bits = {
  241. .entropy_coding_mode_flag = pps->cabac,
  242. .weighted_pred_flag = pps->weighted_pred,
  243. .weighted_bipred_idc = pps->weighted_bipred_idc,
  244. .transform_8x8_mode_flag = pps->transform_8x8_mode,
  245. .field_pic_flag = h->picture_structure != PICT_FRAME,
  246. .constrained_intra_pred_flag = pps->constrained_intra_pred,
  247. .pic_order_present_flag = pps->pic_order_present,
  248. .deblocking_filter_control_present_flag = pps->deblocking_filter_parameters_present,
  249. .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present,
  250. .reference_pic_flag = h->nal_ref_idc != 0,
  251. },
  252. .frame_num = h->poc.frame_num,
  253. };
  254. fill_vaapi_pic(&pic_param.CurrPic, h->cur_pic_ptr, h->picture_structure);
  255. err = fill_vaapi_ReferenceFrames(&pic_param, h);
  256. if (err < 0)
  257. goto fail;
  258. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  259. VAPictureParameterBufferType,
  260. &pic_param, sizeof(pic_param));
  261. if (err < 0)
  262. goto fail;
  263. memcpy(iq_matrix.ScalingList4x4,
  264. pps->scaling_matrix4, sizeof(iq_matrix.ScalingList4x4));
  265. memcpy(iq_matrix.ScalingList8x8[0],
  266. pps->scaling_matrix8[0], sizeof(iq_matrix.ScalingList8x8[0]));
  267. memcpy(iq_matrix.ScalingList8x8[1],
  268. pps->scaling_matrix8[3], sizeof(iq_matrix.ScalingList8x8[0]));
  269. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  270. VAIQMatrixBufferType,
  271. &iq_matrix, sizeof(iq_matrix));
  272. if (err < 0)
  273. goto fail;
  274. return 0;
  275. fail:
  276. ff_vaapi_decode_cancel(avctx, pic);
  277. return err;
  278. }
  279. /** End a hardware decoding based frame. */
  280. static int vaapi_h264_end_frame(AVCodecContext *avctx)
  281. {
  282. const H264Context *h = avctx->priv_data;
  283. VAAPIDecodePicture *pic = h->cur_pic_ptr->hwaccel_picture_private;
  284. H264SliceContext *sl = &h->slice_ctx[0];
  285. int ret;
  286. ret = ff_vaapi_decode_issue(avctx, pic);
  287. if (ret < 0)
  288. goto finish;
  289. ff_h264_draw_horiz_band(h, sl, 0, h->avctx->height);
  290. finish:
  291. return ret;
  292. }
  293. /** Decode the given H.264 slice with VA API. */
  294. static int vaapi_h264_decode_slice(AVCodecContext *avctx,
  295. const uint8_t *buffer,
  296. uint32_t size)
  297. {
  298. const H264Context *h = avctx->priv_data;
  299. VAAPIDecodePicture *pic = h->cur_pic_ptr->hwaccel_picture_private;
  300. const H264SliceContext *sl = &h->slice_ctx[0];
  301. VASliceParameterBufferH264 slice_param;
  302. int err;
  303. slice_param = (VASliceParameterBufferH264) {
  304. .slice_data_size = size,
  305. .slice_data_offset = 0,
  306. .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
  307. .slice_data_bit_offset = get_bits_count(&sl->gb),
  308. .first_mb_in_slice = (sl->mb_y >> FIELD_OR_MBAFF_PICTURE(h)) * h->mb_width + sl->mb_x,
  309. .slice_type = ff_h264_get_slice_type(sl),
  310. .direct_spatial_mv_pred_flag = sl->slice_type == AV_PICTURE_TYPE_B ? sl->direct_spatial_mv_pred : 0,
  311. .num_ref_idx_l0_active_minus1 = sl->list_count > 0 ? sl->ref_count[0] - 1 : 0,
  312. .num_ref_idx_l1_active_minus1 = sl->list_count > 1 ? sl->ref_count[1] - 1 : 0,
  313. .cabac_init_idc = sl->cabac_init_idc,
  314. .slice_qp_delta = sl->qscale - h->ps.pps->init_qp,
  315. .disable_deblocking_filter_idc = sl->deblocking_filter < 2 ? !sl->deblocking_filter : sl->deblocking_filter,
  316. .slice_alpha_c0_offset_div2 = sl->slice_alpha_c0_offset / 2,
  317. .slice_beta_offset_div2 = sl->slice_beta_offset / 2,
  318. .luma_log2_weight_denom = sl->pwt.luma_log2_weight_denom,
  319. .chroma_log2_weight_denom = sl->pwt.chroma_log2_weight_denom,
  320. };
  321. fill_vaapi_RefPicList(slice_param.RefPicList0, sl->ref_list[0],
  322. sl->list_count > 0 ? sl->ref_count[0] : 0);
  323. fill_vaapi_RefPicList(slice_param.RefPicList1, sl->ref_list[1],
  324. sl->list_count > 1 ? sl->ref_count[1] : 0);
  325. fill_vaapi_plain_pred_weight_table(h, 0,
  326. &slice_param.luma_weight_l0_flag,
  327. slice_param.luma_weight_l0,
  328. slice_param.luma_offset_l0,
  329. &slice_param.chroma_weight_l0_flag,
  330. slice_param.chroma_weight_l0,
  331. slice_param.chroma_offset_l0);
  332. fill_vaapi_plain_pred_weight_table(h, 1,
  333. &slice_param.luma_weight_l1_flag,
  334. slice_param.luma_weight_l1,
  335. slice_param.luma_offset_l1,
  336. &slice_param.chroma_weight_l1_flag,
  337. slice_param.chroma_weight_l1,
  338. slice_param.chroma_offset_l1);
  339. err = ff_vaapi_decode_make_slice_buffer(avctx, pic,
  340. &slice_param, sizeof(slice_param),
  341. buffer, size);
  342. if (err) {
  343. ff_vaapi_decode_cancel(avctx, pic);
  344. return err;
  345. }
  346. return 0;
  347. }
  348. AVHWAccel ff_h264_vaapi_hwaccel = {
  349. .name = "h264_vaapi",
  350. .type = AVMEDIA_TYPE_VIDEO,
  351. .id = AV_CODEC_ID_H264,
  352. .pix_fmt = AV_PIX_FMT_VAAPI,
  353. .start_frame = &vaapi_h264_start_frame,
  354. .end_frame = &vaapi_h264_end_frame,
  355. .decode_slice = &vaapi_h264_decode_slice,
  356. .frame_priv_data_size = sizeof(VAAPIDecodePicture),
  357. .init = &ff_vaapi_decode_init,
  358. .uninit = &ff_vaapi_decode_uninit,
  359. .priv_data_size = sizeof(VAAPIDecodeContext),
  360. .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
  361. };