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.

583 lines
27KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #include "vaapi_hevc.h"
  29. #include "h265_profile_level.h"
  30. typedef struct VAAPIDecodePictureHEVC {
  31. #if VA_CHECK_VERSION(1, 2, 0)
  32. VAPictureParameterBufferHEVCExtension pic_param;
  33. VASliceParameterBufferHEVCExtension last_slice_param;
  34. #else
  35. VAPictureParameterBufferHEVC pic_param;
  36. VASliceParameterBufferHEVC last_slice_param;
  37. #endif
  38. const uint8_t *last_buffer;
  39. size_t last_size;
  40. VAAPIDecodePicture pic;
  41. } VAAPIDecodePictureHEVC;
  42. static void init_vaapi_pic(VAPictureHEVC *va_pic)
  43. {
  44. va_pic->picture_id = VA_INVALID_ID;
  45. va_pic->flags = VA_PICTURE_HEVC_INVALID;
  46. va_pic->pic_order_cnt = 0;
  47. }
  48. static void fill_vaapi_pic(VAPictureHEVC *va_pic, const HEVCFrame *pic, int rps_type)
  49. {
  50. va_pic->picture_id = ff_vaapi_get_surface_id(pic->frame);
  51. va_pic->pic_order_cnt = pic->poc;
  52. va_pic->flags = rps_type;
  53. if (pic->flags & HEVC_FRAME_FLAG_LONG_REF)
  54. va_pic->flags |= VA_PICTURE_HEVC_LONG_TERM_REFERENCE;
  55. if (pic->frame->interlaced_frame) {
  56. va_pic->flags |= VA_PICTURE_HEVC_FIELD_PIC;
  57. if (!pic->frame->top_field_first)
  58. va_pic->flags |= VA_PICTURE_HEVC_BOTTOM_FIELD;
  59. }
  60. }
  61. static int find_frame_rps_type(const HEVCContext *h, const HEVCFrame *pic)
  62. {
  63. VASurfaceID pic_surf = ff_vaapi_get_surface_id(pic->frame);
  64. int i;
  65. for (i = 0; i < h->rps[ST_CURR_BEF].nb_refs; i++) {
  66. if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_BEF].ref[i]->frame))
  67. return VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE;
  68. }
  69. for (i = 0; i < h->rps[ST_CURR_AFT].nb_refs; i++) {
  70. if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_AFT].ref[i]->frame))
  71. return VA_PICTURE_HEVC_RPS_ST_CURR_AFTER;
  72. }
  73. for (i = 0; i < h->rps[LT_CURR].nb_refs; i++) {
  74. if (pic_surf == ff_vaapi_get_surface_id(h->rps[LT_CURR].ref[i]->frame))
  75. return VA_PICTURE_HEVC_RPS_LT_CURR;
  76. }
  77. return 0;
  78. }
  79. static void fill_vaapi_reference_frames(const HEVCContext *h, VAPictureParameterBufferHEVC *pp)
  80. {
  81. const HEVCFrame *current_picture = h->ref;
  82. int i, j, rps_type;
  83. for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) {
  84. const HEVCFrame *frame = NULL;
  85. while (!frame && j < FF_ARRAY_ELEMS(h->DPB)) {
  86. if (&h->DPB[j] != current_picture && (h->DPB[j].flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF)))
  87. frame = &h->DPB[j];
  88. j++;
  89. }
  90. init_vaapi_pic(&pp->ReferenceFrames[i]);
  91. if (frame) {
  92. rps_type = find_frame_rps_type(h, frame);
  93. fill_vaapi_pic(&pp->ReferenceFrames[i], frame, rps_type);
  94. }
  95. }
  96. }
  97. static int vaapi_hevc_start_frame(AVCodecContext *avctx,
  98. av_unused const uint8_t *buffer,
  99. av_unused uint32_t size)
  100. {
  101. const HEVCContext *h = avctx->priv_data;
  102. VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
  103. const HEVCSPS *sps = h->ps.sps;
  104. const HEVCPPS *pps = h->ps.pps;
  105. const ScalingList *scaling_list = NULL;
  106. int pic_param_size, err, i;
  107. VAPictureParameterBufferHEVC *pic_param = (VAPictureParameterBufferHEVC *)&pic->pic_param;
  108. pic->pic.output_surface = ff_vaapi_get_surface_id(h->ref->frame);
  109. *pic_param = (VAPictureParameterBufferHEVC) {
  110. .pic_width_in_luma_samples = sps->width,
  111. .pic_height_in_luma_samples = sps->height,
  112. .log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3,
  113. .sps_max_dec_pic_buffering_minus1 = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1,
  114. .log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size,
  115. .log2_min_transform_block_size_minus2 = sps->log2_min_tb_size - 2,
  116. .log2_diff_max_min_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size,
  117. .max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter,
  118. .max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra,
  119. .num_short_term_ref_pic_sets = sps->nb_st_rps,
  120. .num_long_term_ref_pic_sps = sps->num_long_term_ref_pics_sps,
  121. .num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1,
  122. .num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1,
  123. .init_qp_minus26 = pps->pic_init_qp_minus26,
  124. .pps_cb_qp_offset = pps->cb_qp_offset,
  125. .pps_cr_qp_offset = pps->cr_qp_offset,
  126. .pcm_sample_bit_depth_luma_minus1 = sps->pcm.bit_depth - 1,
  127. .pcm_sample_bit_depth_chroma_minus1 = sps->pcm.bit_depth_chroma - 1,
  128. .log2_min_pcm_luma_coding_block_size_minus3 = sps->pcm.log2_min_pcm_cb_size - 3,
  129. .log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size,
  130. .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
  131. .pps_beta_offset_div2 = pps->beta_offset / 2,
  132. .pps_tc_offset_div2 = pps->tc_offset / 2,
  133. .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2,
  134. .bit_depth_luma_minus8 = sps->bit_depth - 8,
  135. .bit_depth_chroma_minus8 = sps->bit_depth - 8,
  136. .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4,
  137. .num_extra_slice_header_bits = pps->num_extra_slice_header_bits,
  138. .pic_fields.bits = {
  139. .chroma_format_idc = sps->chroma_format_idc,
  140. .tiles_enabled_flag = pps->tiles_enabled_flag,
  141. .separate_colour_plane_flag = sps->separate_colour_plane_flag,
  142. .pcm_enabled_flag = sps->pcm_enabled_flag,
  143. .scaling_list_enabled_flag = sps->scaling_list_enable_flag,
  144. .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
  145. .amp_enabled_flag = sps->amp_enabled_flag,
  146. .strong_intra_smoothing_enabled_flag = sps->sps_strong_intra_smoothing_enable_flag,
  147. .sign_data_hiding_enabled_flag = pps->sign_data_hiding_flag,
  148. .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
  149. .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
  150. .weighted_pred_flag = pps->weighted_pred_flag,
  151. .weighted_bipred_flag = pps->weighted_bipred_flag,
  152. .transquant_bypass_enabled_flag = pps->transquant_bypass_enable_flag,
  153. .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
  154. .pps_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag,
  155. .loop_filter_across_tiles_enabled_flag = pps->loop_filter_across_tiles_enabled_flag,
  156. .pcm_loop_filter_disabled_flag = sps->pcm.loop_filter_disable_flag,
  157. },
  158. .slice_parsing_fields.bits = {
  159. .lists_modification_present_flag = pps->lists_modification_present_flag,
  160. .long_term_ref_pics_present_flag = sps->long_term_ref_pics_present_flag,
  161. .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
  162. .cabac_init_present_flag = pps->cabac_init_present_flag,
  163. .output_flag_present_flag = pps->output_flag_present_flag,
  164. .dependent_slice_segments_enabled_flag = pps->dependent_slice_segments_enabled_flag,
  165. .pps_slice_chroma_qp_offsets_present_flag = pps->pic_slice_level_chroma_qp_offsets_present_flag,
  166. .sample_adaptive_offset_enabled_flag = sps->sao_enabled,
  167. .deblocking_filter_override_enabled_flag = pps->deblocking_filter_override_enabled_flag,
  168. .pps_disable_deblocking_filter_flag = pps->disable_dbf,
  169. .slice_segment_header_extension_present_flag = pps->slice_header_extension_present_flag,
  170. .RapPicFlag = IS_IRAP(h),
  171. .IdrPicFlag = IS_IDR(h),
  172. .IntraPicFlag = IS_IRAP(h),
  173. },
  174. };
  175. fill_vaapi_pic(&pic_param->CurrPic, h->ref, 0);
  176. fill_vaapi_reference_frames(h, pic_param);
  177. if (pps->tiles_enabled_flag) {
  178. pic_param->num_tile_columns_minus1 = pps->num_tile_columns - 1;
  179. pic_param->num_tile_rows_minus1 = pps->num_tile_rows - 1;
  180. for (i = 0; i < pps->num_tile_columns; i++)
  181. pic_param->column_width_minus1[i] = pps->column_width[i] - 1;
  182. for (i = 0; i < pps->num_tile_rows; i++)
  183. pic_param->row_height_minus1[i] = pps->row_height[i] - 1;
  184. }
  185. if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) {
  186. pic_param->st_rps_bits = h->sh.short_term_ref_pic_set_size;
  187. } else {
  188. pic_param->st_rps_bits = 0;
  189. }
  190. #if VA_CHECK_VERSION(1, 2, 0)
  191. if (avctx->profile == FF_PROFILE_HEVC_REXT) {
  192. pic->pic_param.rext = (VAPictureParameterBufferHEVCRext) {
  193. .range_extension_pic_fields.bits = {
  194. .transform_skip_rotation_enabled_flag = sps->transform_skip_rotation_enabled_flag,
  195. .transform_skip_context_enabled_flag = sps->transform_skip_context_enabled_flag,
  196. .implicit_rdpcm_enabled_flag = sps->implicit_rdpcm_enabled_flag,
  197. .explicit_rdpcm_enabled_flag = sps->explicit_rdpcm_enabled_flag,
  198. .extended_precision_processing_flag = sps->extended_precision_processing_flag,
  199. .intra_smoothing_disabled_flag = sps->intra_smoothing_disabled_flag,
  200. .high_precision_offsets_enabled_flag = sps->high_precision_offsets_enabled_flag,
  201. .persistent_rice_adaptation_enabled_flag = sps->persistent_rice_adaptation_enabled_flag,
  202. .cabac_bypass_alignment_enabled_flag = sps->cabac_bypass_alignment_enabled_flag,
  203. .cross_component_prediction_enabled_flag = pps->cross_component_prediction_enabled_flag,
  204. .chroma_qp_offset_list_enabled_flag = pps->chroma_qp_offset_list_enabled_flag,
  205. },
  206. .diff_cu_chroma_qp_offset_depth = pps->diff_cu_chroma_qp_offset_depth,
  207. .chroma_qp_offset_list_len_minus1 = pps->chroma_qp_offset_list_len_minus1,
  208. .log2_sao_offset_scale_luma = pps->log2_sao_offset_scale_luma,
  209. .log2_sao_offset_scale_chroma = pps->log2_sao_offset_scale_chroma,
  210. .log2_max_transform_skip_block_size_minus2 = pps->log2_max_transform_skip_block_size - 2,
  211. };
  212. for (i = 0; i < 6; i++)
  213. pic->pic_param.rext.cb_qp_offset_list[i] = pps->cb_qp_offset_list[i];
  214. for (i = 0; i < 6; i++)
  215. pic->pic_param.rext.cr_qp_offset_list[i] = pps->cr_qp_offset_list[i];
  216. }
  217. #endif
  218. pic_param_size = avctx->profile == FF_PROFILE_HEVC_REXT ?
  219. sizeof(pic->pic_param) : sizeof(VAPictureParameterBufferHEVC);
  220. err = ff_vaapi_decode_make_param_buffer(avctx, &pic->pic,
  221. VAPictureParameterBufferType,
  222. &pic->pic_param, pic_param_size);
  223. if (err < 0)
  224. goto fail;
  225. if (pps->scaling_list_data_present_flag)
  226. scaling_list = &pps->scaling_list;
  227. else if (sps->scaling_list_enable_flag)
  228. scaling_list = &sps->scaling_list;
  229. if (scaling_list) {
  230. VAIQMatrixBufferHEVC iq_matrix;
  231. int j;
  232. for (i = 0; i < 6; i++) {
  233. for (j = 0; j < 16; j++)
  234. iq_matrix.ScalingList4x4[i][j] = scaling_list->sl[0][i][j];
  235. for (j = 0; j < 64; j++) {
  236. iq_matrix.ScalingList8x8[i][j] = scaling_list->sl[1][i][j];
  237. iq_matrix.ScalingList16x16[i][j] = scaling_list->sl[2][i][j];
  238. if (i < 2)
  239. iq_matrix.ScalingList32x32[i][j] = scaling_list->sl[3][i * 3][j];
  240. }
  241. iq_matrix.ScalingListDC16x16[i] = scaling_list->sl_dc[0][i];
  242. if (i < 2)
  243. iq_matrix.ScalingListDC32x32[i] = scaling_list->sl_dc[1][i * 3];
  244. }
  245. err = ff_vaapi_decode_make_param_buffer(avctx, &pic->pic,
  246. VAIQMatrixBufferType,
  247. &iq_matrix, sizeof(iq_matrix));
  248. if (err < 0)
  249. goto fail;
  250. }
  251. return 0;
  252. fail:
  253. ff_vaapi_decode_cancel(avctx, &pic->pic);
  254. return err;
  255. }
  256. static int vaapi_hevc_end_frame(AVCodecContext *avctx)
  257. {
  258. const HEVCContext *h = avctx->priv_data;
  259. VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
  260. VASliceParameterBufferHEVC *last_slice_param = (VASliceParameterBufferHEVC *)&pic->last_slice_param;
  261. int ret;
  262. int slice_param_size = avctx->profile == FF_PROFILE_HEVC_REXT ?
  263. sizeof(pic->last_slice_param) : sizeof(VASliceParameterBufferHEVC);
  264. if (pic->last_size) {
  265. last_slice_param->LongSliceFlags.fields.LastSliceOfPic = 1;
  266. ret = ff_vaapi_decode_make_slice_buffer(avctx, &pic->pic,
  267. &pic->last_slice_param, slice_param_size,
  268. pic->last_buffer, pic->last_size);
  269. if (ret < 0)
  270. goto fail;
  271. }
  272. ret = ff_vaapi_decode_issue(avctx, &pic->pic);
  273. if (ret < 0)
  274. goto fail;
  275. return 0;
  276. fail:
  277. ff_vaapi_decode_cancel(avctx, &pic->pic);
  278. return ret;
  279. }
  280. static void fill_pred_weight_table(const HEVCContext *h,
  281. const SliceHeader *sh,
  282. VASliceParameterBufferHEVC *slice_param)
  283. {
  284. int i;
  285. memset(slice_param->delta_luma_weight_l0, 0, sizeof(slice_param->delta_luma_weight_l0));
  286. memset(slice_param->delta_luma_weight_l1, 0, sizeof(slice_param->delta_luma_weight_l1));
  287. memset(slice_param->luma_offset_l0, 0, sizeof(slice_param->luma_offset_l0));
  288. memset(slice_param->luma_offset_l1, 0, sizeof(slice_param->luma_offset_l1));
  289. memset(slice_param->delta_chroma_weight_l0, 0, sizeof(slice_param->delta_chroma_weight_l0));
  290. memset(slice_param->delta_chroma_weight_l1, 0, sizeof(slice_param->delta_chroma_weight_l1));
  291. memset(slice_param->ChromaOffsetL0, 0, sizeof(slice_param->ChromaOffsetL0));
  292. memset(slice_param->ChromaOffsetL1, 0, sizeof(slice_param->ChromaOffsetL1));
  293. slice_param->delta_chroma_log2_weight_denom = 0;
  294. slice_param->luma_log2_weight_denom = 0;
  295. if (sh->slice_type == HEVC_SLICE_I ||
  296. (sh->slice_type == HEVC_SLICE_P && !h->ps.pps->weighted_pred_flag) ||
  297. (sh->slice_type == HEVC_SLICE_B && !h->ps.pps->weighted_bipred_flag))
  298. return;
  299. slice_param->luma_log2_weight_denom = sh->luma_log2_weight_denom;
  300. if (h->ps.sps->chroma_format_idc) {
  301. slice_param->delta_chroma_log2_weight_denom = sh->chroma_log2_weight_denom - sh->luma_log2_weight_denom;
  302. }
  303. for (i = 0; i < 15 && i < sh->nb_refs[L0]; i++) {
  304. slice_param->delta_luma_weight_l0[i] = sh->luma_weight_l0[i] - (1 << sh->luma_log2_weight_denom);
  305. slice_param->luma_offset_l0[i] = sh->luma_offset_l0[i];
  306. slice_param->delta_chroma_weight_l0[i][0] = sh->chroma_weight_l0[i][0] - (1 << sh->chroma_log2_weight_denom);
  307. slice_param->delta_chroma_weight_l0[i][1] = sh->chroma_weight_l0[i][1] - (1 << sh->chroma_log2_weight_denom);
  308. slice_param->ChromaOffsetL0[i][0] = sh->chroma_offset_l0[i][0];
  309. slice_param->ChromaOffsetL0[i][1] = sh->chroma_offset_l0[i][1];
  310. }
  311. if (sh->slice_type == HEVC_SLICE_B) {
  312. for (i = 0; i < 15 && i < sh->nb_refs[L1]; i++) {
  313. slice_param->delta_luma_weight_l1[i] = sh->luma_weight_l1[i] - (1 << sh->luma_log2_weight_denom);
  314. slice_param->luma_offset_l1[i] = sh->luma_offset_l1[i];
  315. slice_param->delta_chroma_weight_l1[i][0] = sh->chroma_weight_l1[i][0] - (1 << sh->chroma_log2_weight_denom);
  316. slice_param->delta_chroma_weight_l1[i][1] = sh->chroma_weight_l1[i][1] - (1 << sh->chroma_log2_weight_denom);
  317. slice_param->ChromaOffsetL1[i][0] = sh->chroma_offset_l1[i][0];
  318. slice_param->ChromaOffsetL1[i][1] = sh->chroma_offset_l1[i][1];
  319. }
  320. }
  321. }
  322. static uint8_t get_ref_pic_index(const HEVCContext *h, const HEVCFrame *frame)
  323. {
  324. VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
  325. VAPictureParameterBufferHEVC *pp = (VAPictureParameterBufferHEVC *)&pic->pic_param;
  326. uint8_t i;
  327. if (!frame)
  328. return 0xff;
  329. for (i = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) {
  330. VASurfaceID pid = pp->ReferenceFrames[i].picture_id;
  331. int poc = pp->ReferenceFrames[i].pic_order_cnt;
  332. if (pid != VA_INVALID_ID && pid == ff_vaapi_get_surface_id(frame->frame) && poc == frame->poc)
  333. return i;
  334. }
  335. return 0xff;
  336. }
  337. static int vaapi_hevc_decode_slice(AVCodecContext *avctx,
  338. const uint8_t *buffer,
  339. uint32_t size)
  340. {
  341. const HEVCContext *h = avctx->priv_data;
  342. const SliceHeader *sh = &h->sh;
  343. VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
  344. VASliceParameterBufferHEVC *last_slice_param = (VASliceParameterBufferHEVC *)&pic->last_slice_param;
  345. int slice_param_size = avctx->profile == FF_PROFILE_HEVC_REXT ?
  346. sizeof(pic->last_slice_param) : sizeof(VASliceParameterBufferHEVC);
  347. int nb_list = (sh->slice_type == HEVC_SLICE_B) ?
  348. 2 : (sh->slice_type == HEVC_SLICE_I ? 0 : 1);
  349. int err, i, list_idx;
  350. if (!sh->first_slice_in_pic_flag) {
  351. err = ff_vaapi_decode_make_slice_buffer(avctx, &pic->pic,
  352. &pic->last_slice_param, slice_param_size,
  353. pic->last_buffer, pic->last_size);
  354. pic->last_buffer = NULL;
  355. pic->last_size = 0;
  356. if (err) {
  357. ff_vaapi_decode_cancel(avctx, &pic->pic);
  358. return err;
  359. }
  360. }
  361. *last_slice_param = (VASliceParameterBufferHEVC) {
  362. .slice_data_size = size,
  363. .slice_data_offset = 0,
  364. .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
  365. /* Add 1 to the bits count here to account for the byte_alignment bit, which
  366. * always is at least one bit and not accounted for otherwise. */
  367. .slice_data_byte_offset = (get_bits_count(&h->HEVClc->gb) + 1 + 7) / 8,
  368. .slice_segment_address = sh->slice_segment_addr,
  369. .slice_qp_delta = sh->slice_qp_delta,
  370. .slice_cb_qp_offset = sh->slice_cb_qp_offset,
  371. .slice_cr_qp_offset = sh->slice_cr_qp_offset,
  372. .slice_beta_offset_div2 = sh->beta_offset / 2,
  373. .slice_tc_offset_div2 = sh->tc_offset / 2,
  374. .collocated_ref_idx = sh->slice_temporal_mvp_enabled_flag ? sh->collocated_ref_idx : 0xFF,
  375. .five_minus_max_num_merge_cand = sh->slice_type == HEVC_SLICE_I ? 0 : 5 - sh->max_num_merge_cand,
  376. .num_ref_idx_l0_active_minus1 = sh->nb_refs[L0] ? sh->nb_refs[L0] - 1 : 0,
  377. .num_ref_idx_l1_active_minus1 = sh->nb_refs[L1] ? sh->nb_refs[L1] - 1 : 0,
  378. .LongSliceFlags.fields = {
  379. .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
  380. .slice_type = sh->slice_type,
  381. .color_plane_id = sh->colour_plane_id,
  382. .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
  383. .cabac_init_flag = sh->cabac_init_flag,
  384. .slice_temporal_mvp_enabled_flag = sh->slice_temporal_mvp_enabled_flag,
  385. .slice_deblocking_filter_disabled_flag = sh->disable_deblocking_filter_flag,
  386. .collocated_from_l0_flag = sh->collocated_list == L0 ? 1 : 0,
  387. .slice_loop_filter_across_slices_enabled_flag = sh->slice_loop_filter_across_slices_enabled_flag,
  388. .slice_sao_luma_flag = sh->slice_sample_adaptive_offset_flag[0],
  389. .slice_sao_chroma_flag = sh->slice_sample_adaptive_offset_flag[1],
  390. },
  391. };
  392. memset(last_slice_param->RefPicList, 0xFF, sizeof(last_slice_param->RefPicList));
  393. for (list_idx = 0; list_idx < nb_list; list_idx++) {
  394. RefPicList *rpl = &h->ref->refPicList[list_idx];
  395. for (i = 0; i < rpl->nb_refs; i++)
  396. last_slice_param->RefPicList[list_idx][i] = get_ref_pic_index(h, rpl->ref[i]);
  397. }
  398. fill_pred_weight_table(h, sh, last_slice_param);
  399. #if VA_CHECK_VERSION(1, 2, 0)
  400. if (avctx->profile == FF_PROFILE_HEVC_REXT) {
  401. pic->last_slice_param.rext = (VASliceParameterBufferHEVCRext) {
  402. .slice_ext_flags.bits = {
  403. .cu_chroma_qp_offset_enabled_flag = sh->cu_chroma_qp_offset_enabled_flag,
  404. },
  405. };
  406. memcpy(pic->last_slice_param.rext.luma_offset_l0, pic->last_slice_param.base.luma_offset_l0,
  407. sizeof(pic->last_slice_param.base.luma_offset_l0));
  408. memcpy(pic->last_slice_param.rext.luma_offset_l1, pic->last_slice_param.base.luma_offset_l1,
  409. sizeof(pic->last_slice_param.base.luma_offset_l1));
  410. memcpy(pic->last_slice_param.rext.ChromaOffsetL0, pic->last_slice_param.base.ChromaOffsetL0,
  411. sizeof(pic->last_slice_param.base.ChromaOffsetL0));
  412. memcpy(pic->last_slice_param.rext.ChromaOffsetL1, pic->last_slice_param.base.ChromaOffsetL1,
  413. sizeof(pic->last_slice_param.base.ChromaOffsetL1));
  414. }
  415. #endif
  416. pic->last_buffer = buffer;
  417. pic->last_size = size;
  418. return 0;
  419. }
  420. static int ptl_convert(const PTLCommon *general_ptl, H265RawProfileTierLevel *h265_raw_ptl)
  421. {
  422. h265_raw_ptl->general_profile_space = general_ptl->profile_space;
  423. h265_raw_ptl->general_tier_flag = general_ptl->tier_flag;
  424. h265_raw_ptl->general_profile_idc = general_ptl->profile_idc;
  425. memcpy(h265_raw_ptl->general_profile_compatibility_flag,
  426. general_ptl->profile_compatibility_flag, 32 * sizeof(uint8_t));
  427. #define copy_field(name) h265_raw_ptl->general_ ## name = general_ptl->name
  428. copy_field(progressive_source_flag);
  429. copy_field(interlaced_source_flag);
  430. copy_field(non_packed_constraint_flag);
  431. copy_field(frame_only_constraint_flag);
  432. copy_field(max_12bit_constraint_flag);
  433. copy_field(max_10bit_constraint_flag);
  434. copy_field(max_422chroma_constraint_flag);
  435. copy_field(max_420chroma_constraint_flag);
  436. copy_field(max_monochrome_constraint_flag);
  437. copy_field(intra_constraint_flag);
  438. copy_field(one_picture_only_constraint_flag);
  439. copy_field(lower_bit_rate_constraint_flag);
  440. copy_field(max_14bit_constraint_flag);
  441. copy_field(inbld_flag);
  442. copy_field(level_idc);
  443. #undef copy_field
  444. return 0;
  445. }
  446. /*
  447. * Find exact va_profile for HEVC Range Extension
  448. */
  449. VAProfile ff_vaapi_parse_hevc_rext_profile(AVCodecContext *avctx)
  450. {
  451. const HEVCContext *h = avctx->priv_data;
  452. const HEVCSPS *sps = h->ps.sps;
  453. const PTL *ptl = &sps->ptl;
  454. const PTLCommon *general_ptl = &ptl->general_ptl;
  455. const H265ProfileDescriptor *profile;
  456. H265RawProfileTierLevel h265_raw_ptl = {0};
  457. /* convert PTLCommon to H265RawProfileTierLevel */
  458. ptl_convert(general_ptl, &h265_raw_ptl);
  459. profile = ff_h265_get_profile(&h265_raw_ptl);
  460. if (!profile) {
  461. av_log(avctx, AV_LOG_WARNING, "HEVC profile is not found.\n");
  462. goto end;
  463. } else {
  464. av_log(avctx, AV_LOG_VERBOSE, "HEVC profile %s is found.\n", profile->name);
  465. }
  466. #if VA_CHECK_VERSION(1, 2, 0)
  467. if (!strcmp(profile->name, "Main 4:2:2 10") ||
  468. !strcmp(profile->name, "Main 4:2:2 10 Intra"))
  469. return VAProfileHEVCMain422_10;
  470. else if (!strcmp(profile->name, "Main 4:4:4") ||
  471. !strcmp(profile->name, "Main 4:4:4 Intra"))
  472. return VAProfileHEVCMain444;
  473. else if (!strcmp(profile->name, "Main 4:4:4 10") ||
  474. !strcmp(profile->name, "Main 4:4:4 10 Intra"))
  475. return VAProfileHEVCMain444_10;
  476. #else
  477. av_log(avctx, AV_LOG_WARNING, "HEVC profile %s is "
  478. "not supported with this VA version.\n", profile->name);
  479. #endif
  480. end:
  481. if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
  482. // Default to selecting Main profile if profile mismatch is allowed
  483. return VAProfileHEVCMain;
  484. } else
  485. return VAProfileNone;
  486. }
  487. const AVHWAccel ff_hevc_vaapi_hwaccel = {
  488. .name = "hevc_vaapi",
  489. .type = AVMEDIA_TYPE_VIDEO,
  490. .id = AV_CODEC_ID_HEVC,
  491. .pix_fmt = AV_PIX_FMT_VAAPI,
  492. .start_frame = vaapi_hevc_start_frame,
  493. .end_frame = vaapi_hevc_end_frame,
  494. .decode_slice = vaapi_hevc_decode_slice,
  495. .frame_priv_data_size = sizeof(VAAPIDecodePictureHEVC),
  496. .init = ff_vaapi_decode_init,
  497. .uninit = ff_vaapi_decode_uninit,
  498. .frame_params = ff_vaapi_common_frame_params,
  499. .priv_data_size = sizeof(VAAPIDecodeContext),
  500. .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
  501. };