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