FFmpeg
Loading...
Searching...
No Matches
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 "hwaccel_internal.h"
28#include "vaapi_decode.h"
29#include "vaapi_hevc.h"
30#include "h265_profile_level.h"
31
32#include "hevc/hevcdec.h"
33
34typedef struct VAAPIDecodePictureHEVC {
36
37#if VA_CHECK_VERSION(1, 2, 0)
38 VAPictureParameterBufferHEVCExtension pic_param;
39 VASliceParameterBufferHEVCExtension last_slice_param;
40#else
41 VAPictureParameterBufferHEVC pic_param;
42 VASliceParameterBufferHEVC last_slice_param;
43#endif
44 const uint8_t *last_buffer;
45 size_t last_size;
47
48static void init_vaapi_pic(VAPictureHEVC *va_pic)
49{
50 va_pic->picture_id = VA_INVALID_ID;
51 va_pic->flags = VA_PICTURE_HEVC_INVALID;
52 va_pic->pic_order_cnt = 0;
53}
54
55static void fill_vaapi_pic(VAPictureHEVC *va_pic, const HEVCFrame *pic, int rps_type)
56{
57 va_pic->picture_id = ff_vaapi_get_surface_id(pic->f);
58 va_pic->pic_order_cnt = pic->poc;
59 va_pic->flags = rps_type;
60
62 va_pic->flags |= VA_PICTURE_HEVC_LONG_TERM_REFERENCE;
63
64 if (pic->f->flags & AV_FRAME_FLAG_INTERLACED) {
65 va_pic->flags |= VA_PICTURE_HEVC_FIELD_PIC;
66
68 va_pic->flags |= VA_PICTURE_HEVC_BOTTOM_FIELD;
69 }
70}
71
72static int find_frame_rps_type(const HEVCContext *h, const HEVCFrame *pic)
73{
74 VASurfaceID pic_surf = ff_vaapi_get_surface_id(pic->f);
75 const HEVCFrame *current_picture = h->cur_frame;
76 int i;
77
78 for (i = 0; i < h->rps[ST_CURR_BEF].nb_refs; i++) {
79 if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_BEF].ref[i]->f))
80 return VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE;
81 }
82
83 for (i = 0; i < h->rps[ST_CURR_AFT].nb_refs; i++) {
84 if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_AFT].ref[i]->f))
85 return VA_PICTURE_HEVC_RPS_ST_CURR_AFTER;
86 }
87
88 for (i = 0; i < h->rps[LT_CURR].nb_refs; i++) {
89 if (pic_surf == ff_vaapi_get_surface_id(h->rps[LT_CURR].ref[i]->f))
90 return VA_PICTURE_HEVC_RPS_LT_CURR;
91 }
92
93 if (h->pps->pps_curr_pic_ref_enabled_flag && current_picture->poc == pic->poc)
94 return VA_PICTURE_HEVC_LONG_TERM_REFERENCE;
95
96 return 0;
97}
98
100 VAPictureParameterBufferHEVC *pp)
101{
102 const HEVCFrame *current_picture = h->cur_frame;
103 int i, j, rps_type;
104
105 for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) {
106 const HEVCFrame *frame = NULL;
107
108 while (!frame && j < FF_ARRAY_ELEMS(l->DPB)) {
109 if ((&l->DPB[j] != current_picture || h->pps->pps_curr_pic_ref_enabled_flag) &&
111 frame = &l->DPB[j];
112 j++;
113 }
114
115 init_vaapi_pic(&pp->ReferenceFrames[i]);
116
117 if (frame) {
118 rps_type = find_frame_rps_type(h, frame);
119 fill_vaapi_pic(&pp->ReferenceFrames[i], frame, rps_type);
120 }
121 }
122}
123
125 av_unused const AVBufferRef *buffer_ref,
126 av_unused const uint8_t *buffer,
127 av_unused uint32_t size)
128{
129 const HEVCContext *h = avctx->priv_data;
130 const HEVCLayerContext *l = &h->layers[h->cur_layer];
131 VAAPIDecodePictureHEVC *pic = h->cur_frame->hwaccel_picture_private;
132 const HEVCPPS *pps = h->pps;
133 const HEVCSPS *sps = pps->sps;
134
136 int pic_param_size, err, i;
137
138#if VA_CHECK_VERSION(1, 2, 0)
139 int num_comps, pre_palette_size;
140#endif
141
142 VAPictureParameterBufferHEVC *pic_param = (VAPictureParameterBufferHEVC *)&pic->pic_param;
143
144 pic->pic.output_surface = ff_vaapi_get_surface_id(h->cur_frame->f);
145
146 *pic_param = (VAPictureParameterBufferHEVC) {
147 .pic_width_in_luma_samples = sps->width,
148 .pic_height_in_luma_samples = sps->height,
149 .log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3,
150 .sps_max_dec_pic_buffering_minus1 = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1,
151 .log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size,
152 .log2_min_transform_block_size_minus2 = sps->log2_min_tb_size - 2,
153 .log2_diff_max_min_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size,
154 .max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter,
155 .max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra,
156 .num_short_term_ref_pic_sets = sps->nb_st_rps,
157 .num_long_term_ref_pic_sps = sps->num_long_term_ref_pics_sps,
158 .num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1,
159 .num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1,
160 .init_qp_minus26 = pps->pic_init_qp_minus26,
161 .pps_cb_qp_offset = pps->cb_qp_offset,
162 .pps_cr_qp_offset = pps->cr_qp_offset,
163 .pcm_sample_bit_depth_luma_minus1 = sps->pcm.bit_depth - 1,
164 .pcm_sample_bit_depth_chroma_minus1 = sps->pcm.bit_depth_chroma - 1,
165 .log2_min_pcm_luma_coding_block_size_minus3 = sps->pcm.log2_min_pcm_cb_size - 3,
166 .log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size,
167 .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
168 .pps_beta_offset_div2 = pps->beta_offset / 2,
169 .pps_tc_offset_div2 = pps->tc_offset / 2,
170 .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2,
171 .bit_depth_luma_minus8 = sps->bit_depth - 8,
172 .bit_depth_chroma_minus8 = sps->bit_depth - 8,
173 .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4,
174 .num_extra_slice_header_bits = pps->num_extra_slice_header_bits,
175 .pic_fields.bits = {
176 .chroma_format_idc = sps->chroma_format_idc,
177 .tiles_enabled_flag = pps->tiles_enabled_flag,
178 .separate_colour_plane_flag = sps->separate_colour_plane,
179 .pcm_enabled_flag = sps->pcm_enabled,
180 .scaling_list_enabled_flag = sps->scaling_list_enabled,
181 .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
182 .amp_enabled_flag = sps->amp_enabled,
183 .strong_intra_smoothing_enabled_flag = sps->strong_intra_smoothing_enabled,
184 .sign_data_hiding_enabled_flag = pps->sign_data_hiding_flag,
185 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
186 .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
187 .weighted_pred_flag = pps->weighted_pred_flag,
188 .weighted_bipred_flag = pps->weighted_bipred_flag,
189 .transquant_bypass_enabled_flag = pps->transquant_bypass_enable_flag,
190 .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
191 .pps_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag,
192 .loop_filter_across_tiles_enabled_flag = pps->loop_filter_across_tiles_enabled_flag,
193 .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled,
194 },
195 .slice_parsing_fields.bits = {
196 .lists_modification_present_flag = pps->lists_modification_present_flag,
197 .long_term_ref_pics_present_flag = sps->long_term_ref_pics_present,
198 .sps_temporal_mvp_enabled_flag = sps->temporal_mvp_enabled,
199 .cabac_init_present_flag = pps->cabac_init_present_flag,
200 .output_flag_present_flag = pps->output_flag_present_flag,
201 .dependent_slice_segments_enabled_flag = pps->dependent_slice_segments_enabled_flag,
202 .pps_slice_chroma_qp_offsets_present_flag = pps->pic_slice_level_chroma_qp_offsets_present_flag,
203 .sample_adaptive_offset_enabled_flag = sps->sao_enabled,
204 .deblocking_filter_override_enabled_flag = pps->deblocking_filter_override_enabled_flag,
205 .pps_disable_deblocking_filter_flag = pps->disable_dbf,
206 .slice_segment_header_extension_present_flag = pps->slice_header_extension_present_flag,
207 .RapPicFlag = IS_IRAP(h),
208 .IdrPicFlag = IS_IDR(h),
209 .IntraPicFlag = IS_IRAP(h),
210 },
211 };
212
213 fill_vaapi_pic(&pic_param->CurrPic, h->cur_frame, 0);
214 fill_vaapi_reference_frames(h, l, pic_param);
215
216 if (pps->tiles_enabled_flag) {
217 if (pps->num_tile_columns > FF_ARRAY_ELEMS(pic_param->column_width_minus1) + 1 ||
218 pps->num_tile_rows > FF_ARRAY_ELEMS(pic_param->row_height_minus1) + 1) {
219 av_log(avctx, AV_LOG_ERROR, "Too many tiles\n");
220 return AVERROR(ENOSYS);
221 }
222 pic_param->num_tile_columns_minus1 = pps->num_tile_columns - 1;
223 pic_param->num_tile_rows_minus1 = pps->num_tile_rows - 1;
224
225 for (i = 0; i < FFMIN(pps->num_tile_columns, FF_ARRAY_ELEMS(pic_param->column_width_minus1)); i++)
226 pic_param->column_width_minus1[i] = pps->column_width[i] - 1;
227
228 for (i = 0; i < FFMIN(pps->num_tile_rows, FF_ARRAY_ELEMS(pic_param->row_height_minus1)); i++)
229 pic_param->row_height_minus1[i] = pps->row_height[i] - 1;
230 }
231
232 if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) {
233 pic_param->st_rps_bits = h->sh.short_term_ref_pic_set_size;
234 } else {
235 pic_param->st_rps_bits = 0;
236 }
237
238#if VA_CHECK_VERSION(1, 2, 0)
239 if (avctx->profile == AV_PROFILE_HEVC_REXT ||
240 avctx->profile == AV_PROFILE_HEVC_SCC) {
241 pic->pic_param.rext = (VAPictureParameterBufferHEVCRext) {
242 .range_extension_pic_fields.bits = {
243 .transform_skip_rotation_enabled_flag = sps->transform_skip_rotation_enabled,
244 .transform_skip_context_enabled_flag = sps->transform_skip_context_enabled,
245 .implicit_rdpcm_enabled_flag = sps->implicit_rdpcm_enabled,
246 .explicit_rdpcm_enabled_flag = sps->explicit_rdpcm_enabled,
247 .extended_precision_processing_flag = sps->extended_precision_processing,
248 .intra_smoothing_disabled_flag = sps->intra_smoothing_disabled,
249 .high_precision_offsets_enabled_flag = sps->high_precision_offsets_enabled,
250 .persistent_rice_adaptation_enabled_flag = sps->persistent_rice_adaptation_enabled,
251 .cabac_bypass_alignment_enabled_flag = sps->cabac_bypass_alignment_enabled,
252 .cross_component_prediction_enabled_flag = pps->cross_component_prediction_enabled_flag,
253 .chroma_qp_offset_list_enabled_flag = pps->chroma_qp_offset_list_enabled_flag,
254 },
255 .diff_cu_chroma_qp_offset_depth = pps->diff_cu_chroma_qp_offset_depth,
256 .chroma_qp_offset_list_len_minus1 = pps->chroma_qp_offset_list_len_minus1,
257 .log2_sao_offset_scale_luma = pps->log2_sao_offset_scale_luma,
258 .log2_sao_offset_scale_chroma = pps->log2_sao_offset_scale_chroma,
259 .log2_max_transform_skip_block_size_minus2 = pps->log2_max_transform_skip_block_size - 2,
260 };
261
262 for (i = 0; i < 6; i++)
263 pic->pic_param.rext.cb_qp_offset_list[i] = pps->cb_qp_offset_list[i];
264 for (i = 0; i < 6; i++)
265 pic->pic_param.rext.cr_qp_offset_list[i] = pps->cr_qp_offset_list[i];
266 }
267
268 pre_palette_size = pps->pps_palette_predictor_initializers_present_flag ?
269 pps->pps_num_palette_predictor_initializers :
270 (sps->palette_predictor_initializers_present ?
271 sps->sps_num_palette_predictor_initializers :
272 0);
273
274 if (avctx->profile == AV_PROFILE_HEVC_SCC) {
275 pic->pic_param.scc = (VAPictureParameterBufferHEVCScc) {
276 .screen_content_pic_fields.bits = {
277 .pps_curr_pic_ref_enabled_flag = pps->pps_curr_pic_ref_enabled_flag,
278 .palette_mode_enabled_flag = sps->palette_mode_enabled,
279 .motion_vector_resolution_control_idc = sps->motion_vector_resolution_control_idc,
280 .intra_boundary_filtering_disabled_flag = sps->intra_boundary_filtering_disabled,
281 .residual_adaptive_colour_transform_enabled_flag
282 = pps->residual_adaptive_colour_transform_enabled_flag,
283 .pps_slice_act_qp_offsets_present_flag = pps->pps_slice_act_qp_offsets_present_flag,
284 },
285 .palette_max_size = sps->palette_max_size,
286 .delta_palette_max_predictor_size = sps->delta_palette_max_predictor_size,
287 .predictor_palette_size = pre_palette_size,
288 .pps_act_y_qp_offset_plus5 = pps->residual_adaptive_colour_transform_enabled_flag ?
289 pps->pps_act_y_qp_offset + 5 : 0,
290 .pps_act_cb_qp_offset_plus5 = pps->residual_adaptive_colour_transform_enabled_flag ?
291 pps->pps_act_cb_qp_offset + 5 : 0,
292 .pps_act_cr_qp_offset_plus3 = pps->residual_adaptive_colour_transform_enabled_flag ?
293 pps->pps_act_cr_qp_offset + 3 : 0,
294 };
295
296 num_comps = pps->monochrome_palette_flag ? 1 : 3;
297 for (int comp = 0; comp < num_comps; comp++)
298 for (int j = 0; j < pre_palette_size; j++)
299 pic->pic_param.scc.predictor_palette_entries[comp][j] =
300 pps->pps_palette_predictor_initializers_present_flag ?
301 pps->pps_palette_predictor_initializer[comp][j]:
302 sps->sps_palette_predictor_initializer[comp][j];
303 }
304
305#endif
306 pic_param_size = avctx->profile >= AV_PROFILE_HEVC_REXT ?
307 sizeof(pic->pic_param) : sizeof(VAPictureParameterBufferHEVC);
308
309 err = ff_vaapi_decode_make_param_buffer(avctx, &pic->pic,
310 VAPictureParameterBufferType,
311 &pic->pic_param, pic_param_size);
312 if (err < 0)
313 goto fail;
314
315 if (pps->scaling_list_data_present_flag)
316 scaling_list = &pps->scaling_list;
317 else if (sps->scaling_list_enabled)
318 scaling_list = &sps->scaling_list;
319
320 if (scaling_list) {
321 VAIQMatrixBufferHEVC iq_matrix;
322 int j;
323
324 for (i = 0; i < 6; i++) {
325 for (j = 0; j < 16; j++)
326 iq_matrix.ScalingList4x4[i][j] = scaling_list->sl[0][i][j];
327 for (j = 0; j < 64; j++) {
328 iq_matrix.ScalingList8x8[i][j] = scaling_list->sl[1][i][j];
329 iq_matrix.ScalingList16x16[i][j] = scaling_list->sl[2][i][j];
330 if (i < 2)
331 iq_matrix.ScalingList32x32[i][j] = scaling_list->sl[3][i * 3][j];
332 }
333 iq_matrix.ScalingListDC16x16[i] = scaling_list->sl_dc[0][i];
334 if (i < 2)
335 iq_matrix.ScalingListDC32x32[i] = scaling_list->sl_dc[1][i * 3];
336 }
337
338 err = ff_vaapi_decode_make_param_buffer(avctx, &pic->pic,
339 VAIQMatrixBufferType,
340 &iq_matrix, sizeof(iq_matrix));
341 if (err < 0)
342 goto fail;
343 }
344
345 return 0;
346
347fail:
348 ff_vaapi_decode_cancel(avctx, &pic->pic);
349 return err;
350}
351
353{
354 const HEVCContext *h = avctx->priv_data;
355 VAAPIDecodePictureHEVC *pic = h->cur_frame->hwaccel_picture_private;
356 VASliceParameterBufferHEVC *last_slice_param = (VASliceParameterBufferHEVC *)&pic->last_slice_param;
357 int ret;
358
359 int slice_param_size = avctx->profile >= AV_PROFILE_HEVC_REXT ?
360 sizeof(pic->last_slice_param) : sizeof(VASliceParameterBufferHEVC);
361
362 if (pic->last_size) {
363 last_slice_param->LongSliceFlags.fields.LastSliceOfPic = 1;
365 &pic->last_slice_param, 1, slice_param_size,
366 pic->last_buffer, pic->last_size);
367 if (ret < 0)
368 goto fail;
369 }
370
371
372 ret = ff_vaapi_decode_issue(avctx, &pic->pic);
373 if (ret < 0)
374 goto fail;
375
376 return 0;
377fail:
378 ff_vaapi_decode_cancel(avctx, &pic->pic);
379 return ret;
380}
381
383 const HEVCContext *h,
384 const SliceHeader *sh,
385 VASliceParameterBufferHEVC *slice_param)
386{
387 int i;
388#if VA_CHECK_VERSION(1, 2, 0)
389 int is_rext = avctx->profile >= AV_PROFILE_HEVC_REXT;
390#else
391 int is_rext = 0;
392 if (avctx->profile >= AV_PROFILE_HEVC_REXT)
393 av_log(avctx, AV_LOG_WARNING, "Please consider to update to VAAPI 1.2.0 "
394 "or above, which can support REXT related setting correctly.\n");
395#endif
396
397 memset(slice_param->delta_luma_weight_l0, 0, sizeof(slice_param->delta_luma_weight_l0));
398 memset(slice_param->delta_luma_weight_l1, 0, sizeof(slice_param->delta_luma_weight_l1));
399 memset(slice_param->luma_offset_l0, 0, sizeof(slice_param->luma_offset_l0));
400 memset(slice_param->luma_offset_l1, 0, sizeof(slice_param->luma_offset_l1));
401 memset(slice_param->delta_chroma_weight_l0, 0, sizeof(slice_param->delta_chroma_weight_l0));
402 memset(slice_param->delta_chroma_weight_l1, 0, sizeof(slice_param->delta_chroma_weight_l1));
403 memset(slice_param->ChromaOffsetL0, 0, sizeof(slice_param->ChromaOffsetL0));
404 memset(slice_param->ChromaOffsetL1, 0, sizeof(slice_param->ChromaOffsetL1));
405
406 slice_param->delta_chroma_log2_weight_denom = 0;
407 slice_param->luma_log2_weight_denom = 0;
408
409 if (sh->slice_type == HEVC_SLICE_I ||
410 (sh->slice_type == HEVC_SLICE_P && !h->pps->weighted_pred_flag) ||
411 (sh->slice_type == HEVC_SLICE_B && !h->pps->weighted_bipred_flag))
412 return;
413
414 slice_param->luma_log2_weight_denom = sh->luma_log2_weight_denom;
415
416 if (h->pps->sps->chroma_format_idc) {
417 slice_param->delta_chroma_log2_weight_denom = sh->chroma_log2_weight_denom - sh->luma_log2_weight_denom;
418 }
419
420 for (i = 0; i < 15 && i < sh->nb_refs[L0]; i++) {
421 slice_param->delta_luma_weight_l0[i] = sh->luma_weight_l0[i] - (1 << sh->luma_log2_weight_denom);
422 slice_param->delta_chroma_weight_l0[i][0] = sh->chroma_weight_l0[i][0] - (1 << sh->chroma_log2_weight_denom);
423 slice_param->delta_chroma_weight_l0[i][1] = sh->chroma_weight_l0[i][1] - (1 << sh->chroma_log2_weight_denom);
424 if (!is_rext) {
425 slice_param->luma_offset_l0[i] = sh->luma_offset_l0[i];
426 slice_param->ChromaOffsetL0[i][0] = sh->chroma_offset_l0[i][0];
427 slice_param->ChromaOffsetL0[i][1] = sh->chroma_offset_l0[i][1];
428 }
429 }
430
431 if (sh->slice_type == HEVC_SLICE_B) {
432 for (i = 0; i < 15 && i < sh->nb_refs[L1]; i++) {
433 slice_param->delta_luma_weight_l1[i] = sh->luma_weight_l1[i] - (1 << sh->luma_log2_weight_denom);
434 slice_param->delta_chroma_weight_l1[i][0] = sh->chroma_weight_l1[i][0] - (1 << sh->chroma_log2_weight_denom);
435 slice_param->delta_chroma_weight_l1[i][1] = sh->chroma_weight_l1[i][1] - (1 << sh->chroma_log2_weight_denom);
436 if (!is_rext) {
437 slice_param->luma_offset_l1[i] = sh->luma_offset_l1[i];
438 slice_param->ChromaOffsetL1[i][0] = sh->chroma_offset_l1[i][0];
439 slice_param->ChromaOffsetL1[i][1] = sh->chroma_offset_l1[i][1];
440 }
441 }
442 }
443}
444
445static uint8_t get_ref_pic_index(const HEVCContext *h, const HEVCFrame *frame)
446{
447 VAAPIDecodePictureHEVC *pic = h->cur_frame->hwaccel_picture_private;
448 VAPictureParameterBufferHEVC *pp = (VAPictureParameterBufferHEVC *)&pic->pic_param;
449 uint8_t i;
450
451 if (!frame)
452 return 0xff;
453
454 for (i = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) {
455 VASurfaceID pid = pp->ReferenceFrames[i].picture_id;
456 int poc = pp->ReferenceFrames[i].pic_order_cnt;
457 if (pid != VA_INVALID_ID && pid == ff_vaapi_get_surface_id(frame->f) && poc == frame->poc)
458 return i;
459 }
460
461 return 0xff;
462}
463
465 const uint8_t *buffer,
466 uint32_t size)
467{
468 const HEVCContext *h = avctx->priv_data;
469 const SliceHeader *sh = &h->sh;
470 VAAPIDecodePictureHEVC *pic = h->cur_frame->hwaccel_picture_private;
471 VASliceParameterBufferHEVC *last_slice_param = (VASliceParameterBufferHEVC *)&pic->last_slice_param;
472
473 int slice_param_size = avctx->profile >= AV_PROFILE_HEVC_REXT ?
474 sizeof(pic->last_slice_param) : sizeof(VASliceParameterBufferHEVC);
475
476 int nb_list = (sh->slice_type == HEVC_SLICE_B) ?
477 2 : (sh->slice_type == HEVC_SLICE_I ? 0 : 1);
478
479 int err, i, list_idx;
480
481 if (!sh->first_slice_in_pic_flag) {
482 err = ff_vaapi_decode_make_slice_buffer(avctx, &pic->pic,
483 &pic->last_slice_param, 1, slice_param_size,
484 pic->last_buffer, pic->last_size);
485 pic->last_buffer = NULL;
486 pic->last_size = 0;
487 if (err) {
488 ff_vaapi_decode_cancel(avctx, &pic->pic);
489 return err;
490 }
491 }
492
493 *last_slice_param = (VASliceParameterBufferHEVC) {
494 .slice_data_size = size,
495 .slice_data_offset = 0,
496 .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
497 .slice_data_byte_offset = sh->data_offset,
498 .slice_segment_address = sh->slice_segment_addr,
499 .slice_qp_delta = sh->slice_qp_delta,
500 .slice_cb_qp_offset = sh->slice_cb_qp_offset,
501 .slice_cr_qp_offset = sh->slice_cr_qp_offset,
502 .slice_beta_offset_div2 = sh->beta_offset / 2,
503 .slice_tc_offset_div2 = sh->tc_offset / 2,
504 .collocated_ref_idx = sh->slice_temporal_mvp_enabled_flag ? sh->collocated_ref_idx : 0xFF,
505 .five_minus_max_num_merge_cand = sh->slice_type == HEVC_SLICE_I ? 0 : 5 - sh->max_num_merge_cand,
506 .num_ref_idx_l0_active_minus1 = sh->nb_refs[L0] ? sh->nb_refs[L0] - 1 : 0,
507 .num_ref_idx_l1_active_minus1 = sh->nb_refs[L1] ? sh->nb_refs[L1] - 1 : 0,
508
509 .LongSliceFlags.fields = {
510 .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
511 .slice_type = sh->slice_type,
512 .color_plane_id = sh->colour_plane_id,
513 .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
514 .cabac_init_flag = sh->cabac_init_flag,
515 .slice_temporal_mvp_enabled_flag = sh->slice_temporal_mvp_enabled_flag,
516 .slice_deblocking_filter_disabled_flag = sh->disable_deblocking_filter_flag,
517 .collocated_from_l0_flag = sh->collocated_list == L0 ? 1 : 0,
518 .slice_loop_filter_across_slices_enabled_flag = sh->slice_loop_filter_across_slices_enabled_flag,
519 .slice_sao_luma_flag = sh->slice_sample_adaptive_offset_flag[0],
520 .slice_sao_chroma_flag = sh->slice_sample_adaptive_offset_flag[1],
521 },
522 };
523
524 memset(last_slice_param->RefPicList, 0xFF, sizeof(last_slice_param->RefPicList));
525
526 for (list_idx = 0; list_idx < nb_list; list_idx++) {
527 RefPicList *rpl = &h->cur_frame->refPicList[list_idx];
528
529 for (i = 0; i < rpl->nb_refs; i++)
530 last_slice_param->RefPicList[list_idx][i] = get_ref_pic_index(h, rpl->ref[i]);
531 }
532
533 fill_pred_weight_table(avctx, h, sh, last_slice_param);
534
535#if VA_CHECK_VERSION(1, 2, 0)
536 if (avctx->profile >= AV_PROFILE_HEVC_REXT) {
537 pic->last_slice_param.rext = (VASliceParameterBufferHEVCRext) {
538 .slice_ext_flags.bits = {
539 .cu_chroma_qp_offset_enabled_flag = sh->cu_chroma_qp_offset_enabled_flag,
540 .use_integer_mv_flag = sh->use_integer_mv_flag,
541 },
542 .slice_act_y_qp_offset = sh->slice_act_y_qp_offset,
543 .slice_act_cb_qp_offset = sh->slice_act_cb_qp_offset,
544 .slice_act_cr_qp_offset = sh->slice_act_cr_qp_offset,
545 };
546 for (i = 0; i < 15 && i < sh->nb_refs[L0]; i++) {
547 pic->last_slice_param.rext.luma_offset_l0[i] = sh->luma_offset_l0[i];
548 pic->last_slice_param.rext.ChromaOffsetL0[i][0] = sh->chroma_offset_l0[i][0];
549 pic->last_slice_param.rext.ChromaOffsetL0[i][1] = sh->chroma_offset_l0[i][1];
550 }
551
552 if (sh->slice_type == HEVC_SLICE_B) {
553 for (i = 0; i < 15 && i < sh->nb_refs[L1]; i++) {
554 pic->last_slice_param.rext.luma_offset_l1[i] = sh->luma_offset_l1[i];
555 pic->last_slice_param.rext.ChromaOffsetL1[i][0] = sh->chroma_offset_l1[i][0];
556 pic->last_slice_param.rext.ChromaOffsetL1[i][1] = sh->chroma_offset_l1[i][1];
557 }
558 }
559 }
560#endif
561
562 pic->last_buffer = buffer;
563 pic->last_size = size;
564
565 return 0;
566}
567
568static int ptl_convert(const PTLCommon *general_ptl, H265RawProfileTierLevel *h265_raw_ptl)
569{
570 h265_raw_ptl->general_profile_space = general_ptl->profile_space;
571 h265_raw_ptl->general_tier_flag = general_ptl->tier_flag;
572 h265_raw_ptl->general_profile_idc = general_ptl->profile_idc;
573
574 memcpy(h265_raw_ptl->general_profile_compatibility_flag,
575 general_ptl->profile_compatibility_flag, 32 * sizeof(uint8_t));
576
577#define copy_field(name) h265_raw_ptl->general_ ## name = general_ptl->name
578 copy_field(progressive_source_flag);
579 copy_field(interlaced_source_flag);
580 copy_field(non_packed_constraint_flag);
581 copy_field(frame_only_constraint_flag);
582 copy_field(max_12bit_constraint_flag);
583 copy_field(max_10bit_constraint_flag);
584 copy_field(max_8bit_constraint_flag);
585 copy_field(max_422chroma_constraint_flag);
586 copy_field(max_420chroma_constraint_flag);
587 copy_field(max_monochrome_constraint_flag);
588 copy_field(intra_constraint_flag);
589 copy_field(one_picture_only_constraint_flag);
590 copy_field(lower_bit_rate_constraint_flag);
591 copy_field(max_14bit_constraint_flag);
592 copy_field(inbld_flag);
594#undef copy_field
595
596 return 0;
597}
598
599/*
600 * Find exact va_profile for HEVC Range Extension and Screen Content Coding Extension
601 */
603{
604 const HEVCContext *h = avctx->priv_data;
605 const HEVCSPS *sps = h->pps->sps;
606 const PTL *ptl = &sps->ptl;
607 const PTLCommon *general_ptl = &ptl->general_ptl;
609 H265RawProfileTierLevel h265_raw_ptl = {0};
610
611 /* convert PTLCommon to H265RawProfileTierLevel */
612 ptl_convert(general_ptl, &h265_raw_ptl);
613
614 profile = ff_h265_get_profile(&h265_raw_ptl);
615 if (!profile) {
616 av_log(avctx, AV_LOG_WARNING, "HEVC profile is not found.\n");
617 goto end;
618 } else {
619 av_log(avctx, AV_LOG_VERBOSE, "HEVC profile %s is found.\n", profile->name);
620 }
621
622#if VA_CHECK_VERSION(1, 2, 0)
623 if (!strcmp(profile->name, "Main 12") ||
624 !strcmp(profile->name, "Main 12 Intra"))
625 return VAProfileHEVCMain12;
626 else if (!strcmp(profile->name, "Main 4:2:2 10") ||
627 !strcmp(profile->name, "Main 4:2:2 10 Intra"))
628 return VAProfileHEVCMain422_10;
629 else if (!strcmp(profile->name, "Main 4:2:2 12") ||
630 !strcmp(profile->name, "Main 4:2:2 12 Intra"))
631 return VAProfileHEVCMain422_12;
632 else if (!strcmp(profile->name, "Main 4:4:4") ||
633 !strcmp(profile->name, "Main 4:4:4 Intra"))
634 return VAProfileHEVCMain444;
635 else if (!strcmp(profile->name, "Main 4:4:4 10") ||
636 !strcmp(profile->name, "Main 4:4:4 10 Intra"))
637 return VAProfileHEVCMain444_10;
638 else if (!strcmp(profile->name, "Main 4:4:4 12") ||
639 !strcmp(profile->name, "Main 4:4:4 12 Intra"))
640 return VAProfileHEVCMain444_12;
641 else if (!strcmp(profile->name, "Screen-Extended Main"))
642 return VAProfileHEVCSccMain;
643 else if (!strcmp(profile->name, "Screen-Extended Main 10"))
644 return VAProfileHEVCSccMain10;
645 else if (!strcmp(profile->name, "Screen-Extended Main 4:4:4"))
646 return VAProfileHEVCSccMain444;
647#if VA_CHECK_VERSION(1, 8, 0)
648 else if (!strcmp(profile->name, "Screen-Extended Main 4:4:4 10"))
649 return VAProfileHEVCSccMain444_10;
650#endif
651#else
652 av_log(avctx, AV_LOG_WARNING, "HEVC profile %s is "
653 "not supported with this VA version.\n", profile->name);
654#endif
655
656end:
658 // Default to selecting Main profile if profile mismatch is allowed
659 return VAProfileHEVCMain;
660 } else
661 return VAProfileNone;
662}
663
665 .p.name = "hevc_vaapi",
666 .p.type = AVMEDIA_TYPE_VIDEO,
667 .p.id = AV_CODEC_ID_HEVC,
668 .p.pix_fmt = AV_PIX_FMT_VAAPI,
669 .start_frame = vaapi_hevc_start_frame,
670 .end_frame = vaapi_hevc_end_frame,
671 .decode_slice = vaapi_hevc_decode_slice,
672 .frame_priv_data_size = sizeof(VAAPIDecodePictureHEVC),
673 .free_frame_priv = ff_vaapi_decode_free_frame_priv,
676 .frame_params = ff_vaapi_common_frame_params,
677 .priv_data_size = sizeof(VAAPIDecodeContext),
678 .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
679};
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC scaling_list(CodedBitstreamContext *ctx, RWContext *rw, H264RawScalingList *current, int size_of_scaling_list)
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define NULL
Definition coverity.c:32
#define AV_PROFILE_HEVC_REXT
Definition defs.h:176
#define AV_PROFILE_HEVC_SCC
Definition defs.h:178
static AVFrame * frame
uint64_t pps
Definition dovi_rpuenc.c:36
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ret
Definition ffplay.c:391
#define fail
Definition test.h:479
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
#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:2038
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition frame.h:702
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition frame.h:707
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const H265RawProfileTierLevel * ptl
const H265ProfileDescriptor * ff_h265_get_profile(const H265RawProfileTierLevel *ptl)
#define HEVC_FRAME_FLAG_SHORT_REF
Definition hevcdec.h:354
#define IS_IDR(s)
Definition hevcdec.h:75
#define L1
Definition hevcdec.h:57
#define HEVC_FRAME_FLAG_LONG_REF
Definition hevcdec.h:355
#define L0
Definition hevcdec.h:56
#define IS_IRAP(s)
Definition hevcdec.h:78
@ ST_CURR_AFT
Definition hevcdec.h:86
@ ST_CURR_BEF
Definition hevcdec.h:85
@ LT_CURR
Definition hevcdec.h:88
#define HWACCEL_CAP_ASYNC_SAFE
Header providing the internals of AVHWAccel.
const struct FFHWAccel ff_hevc_vaapi_hwaccel
Definition vaapi_hevc.c:664
static av_cold void uninit(AVBitStreamFilterContext *ctx)
@ HEVC_SLICE_P
Definition hevc.h:99
@ HEVC_SLICE_I
Definition hevc.h:100
@ HEVC_SLICE_B
Definition hevc.h:98
#define av_unused
Definition attributes.h:164
#define FFMIN(a, b)
Definition macros.h:49
int profile
Definition mxfenc.c:2393
@ AV_PIX_FMT_VAAPI
Hardware acceleration through VA-API, data[3] contains a VASurfaceID.
Definition pixfmt.h:126
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active).
Definition avcodec.h:1508
int profile
profile
Definition avcodec.h:1646
void * priv_data
Definition avcodec.h:470
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition frame.h:723
uint8_t general_profile_compatibility_flag[32]
Definition cbs_h265.h:41
uint8_t general_profile_space
Definition cbs_h265.h:37
uint8_t general_profile_idc
Definition cbs_h265.h:39
AVFrame * f
Definition hevcdec.h:362
uint8_t flags
A combination of HEVC_FRAME_FLAG_*.
Definition hevcdec.h:387
int poc
Definition hevcdec.h:372
HEVCFrame DPB[32]
Definition hevcdec.h:470
Definition ps.h:371
Definition ps.h:252
uint8_t profile_idc
Definition ps.h:130
uint8_t profile_space
Definition ps.h:128
uint8_t tier_flag
Definition ps.h:129
uint8_t profile_compatibility_flag[32]
Definition ps.h:131
Definition ps.h:150
struct HEVCFrame * ref[HEVC_MAX_REFS]
Definition hevcdec.h:196
int nb_refs
Definition hevcdec.h:199
int16_t chroma_weight_l1[16][2]
Definition hevcdec.h:278
uint8_t cu_chroma_qp_offset_enabled_flag
Definition hevcdec.h:258
int16_t luma_weight_l0[16]
Definition hevcdec.h:276
uint8_t disable_deblocking_filter_flag
slice_header_disable_deblocking_filter_flag
Definition hevcdec.h:244
uint8_t max_num_merge_cand
5 - 5_minus_max_num_merge_cand
Definition hevcdec.h:263
uint8_t mvd_l1_zero_flag
Definition hevcdec.h:241
uint8_t slice_temporal_mvp_enabled_flag
Definition hevcdec.h:236
int tc_offset
tc_offset_div2 * 2
Definition hevcdec.h:261
uint8_t luma_log2_weight_denom
Definition hevcdec.h:273
int16_t luma_weight_l1[16]
Definition hevcdec.h:279
uint8_t colour_plane_id
Definition hevcdec.h:222
unsigned data_offset
Definition hevcdec.h:288
uint8_t use_integer_mv_flag
Definition hevcdec.h:264
int slice_cb_qp_offset
Definition hevcdec.h:251
int16_t luma_offset_l1[16]
Definition hevcdec.h:284
int16_t chroma_offset_l1[16][2]
Definition hevcdec.h:285
enum HEVCSliceType slice_type
Definition hevcdec.h:214
int16_t chroma_offset_l0[16][2]
Definition hevcdec.h:282
unsigned int slice_segment_addr
address (in raster order) of the first block in the current slice segment
Definition hevcdec.h:210
unsigned int nb_refs[2]
Definition hevcdec.h:238
int slice_act_y_qp_offset
Definition hevcdec.h:254
int slice_qp_delta
Definition hevcdec.h:250
uint8_t cabac_init_flag
Definition hevcdec.h:243
int16_t chroma_log2_weight_denom
Definition hevcdec.h:274
int16_t chroma_weight_l0[16][2]
Definition hevcdec.h:277
int slice_act_cb_qp_offset
Definition hevcdec.h:255
uint8_t dependent_slice_segment_flag
Definition hevcdec.h:220
uint8_t first_slice_in_pic_flag
Definition hevcdec.h:219
uint8_t slice_sample_adaptive_offset_flag[3]
Definition hevcdec.h:240
unsigned int collocated_ref_idx
Definition hevcdec.h:248
int16_t luma_offset_l0[16]
Definition hevcdec.h:281
int beta_offset
beta_offset_div2 * 2
Definition hevcdec.h:260
int slice_act_cr_qp_offset
Definition hevcdec.h:256
uint8_t slice_loop_filter_across_slices_enabled_flag
Definition hevcdec.h:245
uint8_t collocated_list
Definition hevcdec.h:246
int slice_cr_qp_offset
Definition hevcdec.h:252
const uint8_t * last_buffer
Definition vaapi_hevc.c:44
VAAPIDecodePicture pic
Definition vaapi_hevc.c:35
VASliceParameterBufferHEVC last_slice_param
Definition vaapi_hevc.c:42
VAPictureParameterBufferHEVC pic_param
Definition vaapi_hevc.c:41
VASurfaceID output_surface
Definition format.c:918
#define av_log(a,...)
int level_idc
Definition h264_levels.c:29
static char buffer[20]
Definition seek.c:33
int size
int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx, VAAPIDecodePicture *pic, int type, const void *data, size_t size)
int ff_vaapi_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
int ff_vaapi_decode_issue(AVCodecContext *avctx, VAAPIDecodePicture *pic)
int ff_vaapi_decode_init(AVCodecContext *avctx)
int ff_vaapi_decode_uninit(AVCodecContext *avctx)
int ff_vaapi_decode_cancel(AVCodecContext *avctx, VAAPIDecodePicture *pic)
void ff_vaapi_decode_free_frame_priv(AVRefStructOpaque hwctx, void *data)
int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx, VAAPIDecodePicture *pic, const void *params_data, int nb_params, size_t params_size, const void *slice_data, size_t slice_size)
static VASurfaceID ff_vaapi_get_surface_id(AVFrame *pic)
static int find_frame_rps_type(const HEVCContext *h, const HEVCFrame *pic)
Definition vaapi_hevc.c:72
static int vaapi_hevc_end_frame(AVCodecContext *avctx)
Definition vaapi_hevc.c:352
static int ptl_convert(const PTLCommon *general_ptl, H265RawProfileTierLevel *h265_raw_ptl)
Definition vaapi_hevc.c:568
#define copy_field(name)
static int vaapi_hevc_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition vaapi_hevc.c:464
static uint8_t get_ref_pic_index(const HEVCContext *h, const HEVCFrame *frame)
Definition vaapi_hevc.c:445
static void fill_vaapi_pic(VAPictureHEVC *va_pic, const HEVCFrame *pic, int rps_type)
Definition vaapi_hevc.c:55
VAProfile ff_vaapi_parse_hevc_rext_scc_profile(AVCodecContext *avctx)
Definition vaapi_hevc.c:602
static void fill_vaapi_reference_frames(const HEVCContext *h, const HEVCLayerContext *l, VAPictureParameterBufferHEVC *pp)
Definition vaapi_hevc.c:99
static void init_vaapi_pic(VAPictureHEVC *va_pic)
Definition vaapi_hevc.c:48
static int vaapi_hevc_start_frame(AVCodecContext *avctx, av_unused const AVBufferRef *buffer_ref, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition vaapi_hevc.c:124
static void fill_pred_weight_table(AVCodecContext *avctx, const HEVCContext *h, const SliceHeader *sh, VASliceParameterBufferHEVC *slice_param)
Definition vaapi_hevc.c:382