FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vaapi_hevc.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 0 399 0.0%
Functions: 0 11 0.0%
Branches: 0 220 0.0%

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