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