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