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.

440 lines
20KB

  1. /*
  2. * HEVC HW decode acceleration through VA API
  3. *
  4. * Copyright (C) 2015 Timo Rothenpieler <timo@rothenpieler.org>
  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 <va/va.h>
  23. #include <va/va_dec_hevc.h>
  24. #include "avcodec.h"
  25. #include "hevcdec.h"
  26. #include "hwaccel.h"
  27. #include "vaapi_decode.h"
  28. typedef struct VAAPIDecodePictureHEVC {
  29. VAPictureParameterBufferHEVC pic_param;
  30. VASliceParameterBufferHEVC last_slice_param;
  31. const uint8_t *last_buffer;
  32. size_t last_size;
  33. VAAPIDecodePicture pic;
  34. } VAAPIDecodePictureHEVC;
  35. static void init_vaapi_pic(VAPictureHEVC *va_pic)
  36. {
  37. va_pic->picture_id = VA_INVALID_ID;
  38. va_pic->flags = VA_PICTURE_HEVC_INVALID;
  39. va_pic->pic_order_cnt = 0;
  40. }
  41. static void fill_vaapi_pic(VAPictureHEVC *va_pic, const HEVCFrame *pic, int rps_type)
  42. {
  43. va_pic->picture_id = ff_vaapi_get_surface_id(pic->frame);
  44. va_pic->pic_order_cnt = pic->poc;
  45. va_pic->flags = rps_type;
  46. if (pic->flags & HEVC_FRAME_FLAG_LONG_REF)
  47. va_pic->flags |= VA_PICTURE_HEVC_LONG_TERM_REFERENCE;
  48. if (pic->frame->interlaced_frame) {
  49. va_pic->flags |= VA_PICTURE_HEVC_FIELD_PIC;
  50. if (!pic->frame->top_field_first)
  51. va_pic->flags |= VA_PICTURE_HEVC_BOTTOM_FIELD;
  52. }
  53. }
  54. static int find_frame_rps_type(const HEVCContext *h, const HEVCFrame *pic)
  55. {
  56. VASurfaceID pic_surf = ff_vaapi_get_surface_id(pic->frame);
  57. int i;
  58. for (i = 0; i < h->rps[ST_CURR_BEF].nb_refs; i++) {
  59. if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_BEF].ref[i]->frame))
  60. return VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE;
  61. }
  62. for (i = 0; i < h->rps[ST_CURR_AFT].nb_refs; i++) {
  63. if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_AFT].ref[i]->frame))
  64. return VA_PICTURE_HEVC_RPS_ST_CURR_AFTER;
  65. }
  66. for (i = 0; i < h->rps[LT_CURR].nb_refs; i++) {
  67. if (pic_surf == ff_vaapi_get_surface_id(h->rps[LT_CURR].ref[i]->frame))
  68. return VA_PICTURE_HEVC_RPS_LT_CURR;
  69. }
  70. return 0;
  71. }
  72. static void fill_vaapi_reference_frames(const HEVCContext *h, VAPictureParameterBufferHEVC *pp)
  73. {
  74. const HEVCFrame *current_picture = h->ref;
  75. int i, j, rps_type;
  76. for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) {
  77. const HEVCFrame *frame = NULL;
  78. while (!frame && j < FF_ARRAY_ELEMS(h->DPB)) {
  79. if (&h->DPB[j] != current_picture && (h->DPB[j].flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF)))
  80. frame = &h->DPB[j];
  81. j++;
  82. }
  83. init_vaapi_pic(&pp->ReferenceFrames[i]);
  84. if (frame) {
  85. rps_type = find_frame_rps_type(h, frame);
  86. fill_vaapi_pic(&pp->ReferenceFrames[i], frame, rps_type);
  87. }
  88. }
  89. }
  90. static int vaapi_hevc_start_frame(AVCodecContext *avctx,
  91. av_unused const uint8_t *buffer,
  92. av_unused uint32_t size)
  93. {
  94. const HEVCContext *h = avctx->priv_data;
  95. VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
  96. const HEVCSPS *sps = h->ps.sps;
  97. const HEVCPPS *pps = h->ps.pps;
  98. const ScalingList *scaling_list = NULL;
  99. int err, i;
  100. pic->pic.output_surface = ff_vaapi_get_surface_id(h->ref->frame);
  101. pic->pic_param = (VAPictureParameterBufferHEVC) {
  102. .pic_fields.value = 0,
  103. .slice_parsing_fields.value = 0,
  104. .pic_width_in_luma_samples = sps->width,
  105. .pic_height_in_luma_samples = sps->height,
  106. .log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3,
  107. .sps_max_dec_pic_buffering_minus1 = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1,
  108. .log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size,
  109. .log2_min_transform_block_size_minus2 = sps->log2_min_tb_size - 2,
  110. .log2_diff_max_min_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size,
  111. .max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter,
  112. .max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra,
  113. .num_short_term_ref_pic_sets = sps->nb_st_rps,
  114. .num_long_term_ref_pic_sps = sps->num_long_term_ref_pics_sps,
  115. .num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1,
  116. .num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1,
  117. .init_qp_minus26 = pps->pic_init_qp_minus26,
  118. .pps_cb_qp_offset = pps->cb_qp_offset,
  119. .pps_cr_qp_offset = pps->cr_qp_offset,
  120. .pcm_sample_bit_depth_luma_minus1 = sps->pcm.bit_depth - 1,
  121. .pcm_sample_bit_depth_chroma_minus1 = sps->pcm.bit_depth_chroma - 1,
  122. .log2_min_pcm_luma_coding_block_size_minus3 = sps->pcm.log2_min_pcm_cb_size - 3,
  123. .log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size,
  124. .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
  125. .pps_beta_offset_div2 = pps->beta_offset / 2,
  126. .pps_tc_offset_div2 = pps->tc_offset / 2,
  127. .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2,
  128. .bit_depth_luma_minus8 = sps->bit_depth - 8,
  129. .bit_depth_chroma_minus8 = sps->bit_depth - 8,
  130. .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4,
  131. .num_extra_slice_header_bits = pps->num_extra_slice_header_bits,
  132. .pic_fields.bits = {
  133. .chroma_format_idc = sps->chroma_format_idc,
  134. .tiles_enabled_flag = pps->tiles_enabled_flag,
  135. .separate_colour_plane_flag = sps->separate_colour_plane_flag,
  136. .pcm_enabled_flag = sps->pcm_enabled_flag,
  137. .scaling_list_enabled_flag = sps->scaling_list_enable_flag,
  138. .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
  139. .amp_enabled_flag = sps->amp_enabled_flag,
  140. .strong_intra_smoothing_enabled_flag = sps->sps_strong_intra_smoothing_enable_flag,
  141. .sign_data_hiding_enabled_flag = pps->sign_data_hiding_flag,
  142. .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
  143. .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
  144. .weighted_pred_flag = pps->weighted_pred_flag,
  145. .weighted_bipred_flag = pps->weighted_bipred_flag,
  146. .transquant_bypass_enabled_flag = pps->transquant_bypass_enable_flag,
  147. .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
  148. .pps_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag,
  149. .loop_filter_across_tiles_enabled_flag = pps->loop_filter_across_tiles_enabled_flag,
  150. .pcm_loop_filter_disabled_flag = sps->pcm.loop_filter_disable_flag,
  151. },
  152. .slice_parsing_fields.bits = {
  153. .lists_modification_present_flag = pps->lists_modification_present_flag,
  154. .long_term_ref_pics_present_flag = sps->long_term_ref_pics_present_flag,
  155. .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
  156. .cabac_init_present_flag = pps->cabac_init_present_flag,
  157. .output_flag_present_flag = pps->output_flag_present_flag,
  158. .dependent_slice_segments_enabled_flag = pps->dependent_slice_segments_enabled_flag,
  159. .pps_slice_chroma_qp_offsets_present_flag = pps->pic_slice_level_chroma_qp_offsets_present_flag,
  160. .sample_adaptive_offset_enabled_flag = sps->sao_enabled,
  161. .deblocking_filter_override_enabled_flag = pps->deblocking_filter_override_enabled_flag,
  162. .pps_disable_deblocking_filter_flag = pps->disable_dbf,
  163. .slice_segment_header_extension_present_flag = pps->slice_header_extension_present_flag,
  164. .RapPicFlag = IS_IRAP(h),
  165. .IdrPicFlag = IS_IDR(h),
  166. .IntraPicFlag = IS_IRAP(h),
  167. },
  168. };
  169. fill_vaapi_pic(&pic->pic_param.CurrPic, h->ref, 0);
  170. fill_vaapi_reference_frames(h, &pic->pic_param);
  171. if (pps->tiles_enabled_flag) {
  172. pic->pic_param.num_tile_columns_minus1 = pps->num_tile_columns - 1;
  173. pic->pic_param.num_tile_rows_minus1 = pps->num_tile_rows - 1;
  174. for (i = 0; i < pps->num_tile_columns; i++)
  175. pic->pic_param.column_width_minus1[i] = pps->column_width[i] - 1;
  176. for (i = 0; i < pps->num_tile_rows; i++)
  177. pic->pic_param.row_height_minus1[i] = pps->row_height[i] - 1;
  178. }
  179. if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) {
  180. pic->pic_param.st_rps_bits = h->sh.short_term_ref_pic_set_size;
  181. } else {
  182. pic->pic_param.st_rps_bits = 0;
  183. }
  184. err = ff_vaapi_decode_make_param_buffer(avctx, &pic->pic,
  185. VAPictureParameterBufferType,
  186. &pic->pic_param, sizeof(pic->pic_param));
  187. if (err < 0)
  188. goto fail;
  189. if (pps->scaling_list_data_present_flag)
  190. scaling_list = &pps->scaling_list;
  191. else if (sps->scaling_list_enable_flag)
  192. scaling_list = &sps->scaling_list;
  193. if (scaling_list) {
  194. VAIQMatrixBufferHEVC iq_matrix;
  195. int j;
  196. for (i = 0; i < 6; i++) {
  197. for (j = 0; j < 16; j++)
  198. iq_matrix.ScalingList4x4[i][j] = scaling_list->sl[0][i][j];
  199. for (j = 0; j < 64; j++) {
  200. iq_matrix.ScalingList8x8[i][j] = scaling_list->sl[1][i][j];
  201. iq_matrix.ScalingList16x16[i][j] = scaling_list->sl[2][i][j];
  202. if (i < 2)
  203. iq_matrix.ScalingList32x32[i][j] = scaling_list->sl[3][i][j];
  204. }
  205. iq_matrix.ScalingListDC16x16[i] = scaling_list->sl_dc[0][i];
  206. if (i < 2)
  207. iq_matrix.ScalingListDC32x32[i] = scaling_list->sl_dc[1][i];
  208. }
  209. err = ff_vaapi_decode_make_param_buffer(avctx, &pic->pic,
  210. VAIQMatrixBufferType,
  211. &iq_matrix, sizeof(iq_matrix));
  212. if (err < 0)
  213. goto fail;
  214. }
  215. return 0;
  216. fail:
  217. ff_vaapi_decode_cancel(avctx, &pic->pic);
  218. return err;
  219. }
  220. static int vaapi_hevc_end_frame(AVCodecContext *avctx)
  221. {
  222. const HEVCContext *h = avctx->priv_data;
  223. VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
  224. int ret;
  225. if (pic->last_size) {
  226. pic->last_slice_param.LongSliceFlags.fields.LastSliceOfPic = 1;
  227. ret = ff_vaapi_decode_make_slice_buffer(avctx, &pic->pic,
  228. &pic->last_slice_param, sizeof(pic->last_slice_param),
  229. pic->last_buffer, pic->last_size);
  230. if (ret < 0)
  231. goto fail;
  232. }
  233. ret = ff_vaapi_decode_issue(avctx, &pic->pic);
  234. if (ret < 0)
  235. goto fail;
  236. return 0;
  237. fail:
  238. ff_vaapi_decode_cancel(avctx, &pic->pic);
  239. return ret;
  240. }
  241. static void fill_pred_weight_table(const HEVCContext *h,
  242. const SliceHeader *sh,
  243. VASliceParameterBufferHEVC *slice_param)
  244. {
  245. int i;
  246. memset(slice_param->delta_luma_weight_l0, 0, sizeof(slice_param->delta_luma_weight_l0));
  247. memset(slice_param->delta_luma_weight_l1, 0, sizeof(slice_param->delta_luma_weight_l1));
  248. memset(slice_param->luma_offset_l0, 0, sizeof(slice_param->luma_offset_l0));
  249. memset(slice_param->luma_offset_l1, 0, sizeof(slice_param->luma_offset_l1));
  250. memset(slice_param->delta_chroma_weight_l0, 0, sizeof(slice_param->delta_chroma_weight_l0));
  251. memset(slice_param->delta_chroma_weight_l1, 0, sizeof(slice_param->delta_chroma_weight_l1));
  252. memset(slice_param->ChromaOffsetL0, 0, sizeof(slice_param->ChromaOffsetL0));
  253. memset(slice_param->ChromaOffsetL1, 0, sizeof(slice_param->ChromaOffsetL1));
  254. slice_param->delta_chroma_log2_weight_denom = 0;
  255. slice_param->luma_log2_weight_denom = 0;
  256. if (sh->slice_type == HEVC_SLICE_I ||
  257. (sh->slice_type == HEVC_SLICE_P && !h->ps.pps->weighted_pred_flag) ||
  258. (sh->slice_type == HEVC_SLICE_B && !h->ps.pps->weighted_bipred_flag))
  259. return;
  260. slice_param->luma_log2_weight_denom = sh->luma_log2_weight_denom;
  261. if (h->ps.sps->chroma_format_idc) {
  262. slice_param->delta_chroma_log2_weight_denom = sh->chroma_log2_weight_denom - sh->luma_log2_weight_denom;
  263. }
  264. for (i = 0; i < 15 && i < sh->nb_refs[L0]; i++) {
  265. slice_param->delta_luma_weight_l0[i] = sh->luma_weight_l0[i] - (1 << sh->luma_log2_weight_denom);
  266. slice_param->luma_offset_l0[i] = sh->luma_offset_l0[i];
  267. slice_param->delta_chroma_weight_l0[i][0] = sh->chroma_weight_l0[i][0] - (1 << sh->chroma_log2_weight_denom);
  268. slice_param->delta_chroma_weight_l0[i][1] = sh->chroma_weight_l0[i][1] - (1 << sh->chroma_log2_weight_denom);
  269. slice_param->ChromaOffsetL0[i][0] = sh->chroma_offset_l0[i][0];
  270. slice_param->ChromaOffsetL0[i][1] = sh->chroma_offset_l0[i][1];
  271. }
  272. if (sh->slice_type == HEVC_SLICE_B) {
  273. for (i = 0; i < 15 && i < sh->nb_refs[L1]; i++) {
  274. slice_param->delta_luma_weight_l1[i] = sh->luma_weight_l1[i] - (1 << sh->luma_log2_weight_denom);
  275. slice_param->luma_offset_l1[i] = sh->luma_offset_l1[i];
  276. slice_param->delta_chroma_weight_l1[i][0] = sh->chroma_weight_l1[i][0] - (1 << sh->chroma_log2_weight_denom);
  277. slice_param->delta_chroma_weight_l1[i][1] = sh->chroma_weight_l1[i][1] - (1 << sh->chroma_log2_weight_denom);
  278. slice_param->ChromaOffsetL1[i][0] = sh->chroma_offset_l1[i][0];
  279. slice_param->ChromaOffsetL1[i][1] = sh->chroma_offset_l1[i][1];
  280. }
  281. }
  282. }
  283. static uint8_t get_ref_pic_index(const HEVCContext *h, const HEVCFrame *frame)
  284. {
  285. VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
  286. VAPictureParameterBufferHEVC *pp = &pic->pic_param;
  287. uint8_t i;
  288. if (!frame)
  289. return 0xff;
  290. for (i = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) {
  291. VASurfaceID pid = pp->ReferenceFrames[i].picture_id;
  292. int poc = pp->ReferenceFrames[i].pic_order_cnt;
  293. if (pid != VA_INVALID_ID && pid == ff_vaapi_get_surface_id(frame->frame) && poc == frame->poc)
  294. return i;
  295. }
  296. return 0xff;
  297. }
  298. static int vaapi_hevc_decode_slice(AVCodecContext *avctx,
  299. const uint8_t *buffer,
  300. uint32_t size)
  301. {
  302. const HEVCContext *h = avctx->priv_data;
  303. const SliceHeader *sh = &h->sh;
  304. VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
  305. int nb_list = (sh->slice_type == HEVC_SLICE_B) ?
  306. 2 : (sh->slice_type == HEVC_SLICE_I ? 0 : 1);
  307. int err, i, list_idx;
  308. if (!sh->first_slice_in_pic_flag) {
  309. err = ff_vaapi_decode_make_slice_buffer(avctx, &pic->pic,
  310. &pic->last_slice_param, sizeof(pic->last_slice_param),
  311. pic->last_buffer, pic->last_size);
  312. pic->last_buffer = NULL;
  313. pic->last_size = 0;
  314. if (err) {
  315. ff_vaapi_decode_cancel(avctx, &pic->pic);
  316. return err;
  317. }
  318. }
  319. pic->last_slice_param = (VASliceParameterBufferHEVC) {
  320. .slice_data_size = size,
  321. .slice_data_offset = 0,
  322. .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
  323. /* Add 1 to the bits count here to account for the byte_alignment bit, which
  324. * always is at least one bit and not accounted for otherwise. */
  325. .slice_data_byte_offset = (get_bits_count(&h->HEVClc.gb) + 1 + 7) / 8,
  326. .slice_segment_address = sh->slice_segment_addr,
  327. .slice_qp_delta = sh->slice_qp_delta,
  328. .slice_cb_qp_offset = sh->slice_cb_qp_offset,
  329. .slice_cr_qp_offset = sh->slice_cr_qp_offset,
  330. .slice_beta_offset_div2 = sh->beta_offset / 2,
  331. .slice_tc_offset_div2 = sh->tc_offset / 2,
  332. .collocated_ref_idx = sh->slice_temporal_mvp_enabled_flag ? sh->collocated_ref_idx : 0xFF,
  333. .five_minus_max_num_merge_cand = sh->slice_type == HEVC_SLICE_I ? 0 : 5 - sh->max_num_merge_cand,
  334. .num_ref_idx_l0_active_minus1 = sh->nb_refs[L0] ? sh->nb_refs[L0] - 1 : 0,
  335. .num_ref_idx_l1_active_minus1 = sh->nb_refs[L1] ? sh->nb_refs[L1] - 1 : 0,
  336. .LongSliceFlags.fields = {
  337. .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
  338. .slice_type = sh->slice_type,
  339. .color_plane_id = sh->colour_plane_id,
  340. .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
  341. .cabac_init_flag = sh->cabac_init_flag,
  342. .slice_temporal_mvp_enabled_flag = sh->slice_temporal_mvp_enabled_flag,
  343. .slice_deblocking_filter_disabled_flag = sh->disable_deblocking_filter_flag,
  344. .collocated_from_l0_flag = sh->collocated_list == L0 ? 1 : 0,
  345. .slice_loop_filter_across_slices_enabled_flag = sh->slice_loop_filter_across_slices_enabled_flag,
  346. .slice_sao_luma_flag = sh->slice_sample_adaptive_offset_flag[0],
  347. .slice_sao_chroma_flag = sh->slice_sample_adaptive_offset_flag[1],
  348. },
  349. };
  350. memset(pic->last_slice_param.RefPicList, 0xFF, sizeof(pic->last_slice_param.RefPicList));
  351. for (list_idx = 0; list_idx < nb_list; list_idx++) {
  352. RefPicList *rpl = &h->ref->refPicList[list_idx];
  353. for (i = 0; i < rpl->nb_refs; i++)
  354. pic->last_slice_param.RefPicList[list_idx][i] = get_ref_pic_index(h, rpl->ref[i]);
  355. }
  356. fill_pred_weight_table(h, sh, &pic->last_slice_param);
  357. pic->last_buffer = buffer;
  358. pic->last_size = size;
  359. return 0;
  360. }
  361. AVHWAccel ff_hevc_vaapi_hwaccel = {
  362. .name = "hevc_vaapi",
  363. .type = AVMEDIA_TYPE_VIDEO,
  364. .id = AV_CODEC_ID_HEVC,
  365. .pix_fmt = AV_PIX_FMT_VAAPI,
  366. .start_frame = vaapi_hevc_start_frame,
  367. .end_frame = vaapi_hevc_end_frame,
  368. .decode_slice = vaapi_hevc_decode_slice,
  369. .frame_priv_data_size = sizeof(VAAPIDecodePictureHEVC),
  370. .init = ff_vaapi_decode_init,
  371. .uninit = ff_vaapi_decode_uninit,
  372. .priv_data_size = sizeof(VAAPIDecodeContext),
  373. .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
  374. };