Browse code

vaapi: Add hevc hwaccel support

Signed-off-by: Timo Rothenpieler <timo@rothenpieler.org>

Timo Rothenpieler authored on 2015/08/26 21:55:40
Showing 7 changed files
... ...
@@ -36,6 +36,7 @@ version <next>:
36 36
 - waveform filter
37 37
 - hstack and vstack filter
38 38
 - Support DNx100 (1440x1080@8)
39
+- VAAPI hevc hwaccel
39 40
 
40 41
 
41 42
 version 2.7:
... ...
@@ -2419,6 +2419,8 @@ hevc_d3d11va_hwaccel_select="hevc_decoder"
2419 2419
 hevc_dxva2_hwaccel_deps="dxva2 DXVA_PicParams_HEVC"
2420 2420
 hevc_dxva2_hwaccel_select="hevc_decoder"
2421 2421
 hevc_qsv_hwaccel_deps="libmfx"
2422
+hevc_vaapi_hwaccel_deps="vaapi VAPictureParameterBufferHEVC"
2423
+hevc_vaapi_hwaccel_select="hevc_decoder"
2422 2424
 hevc_vdpau_hwaccel_deps="vdpau VdpPictureInfoHEVC"
2423 2425
 hevc_vdpau_hwaccel_select="hevc_decoder"
2424 2426
 mpeg_vdpau_decoder_deps="vdpau"
... ...
@@ -5148,6 +5150,8 @@ check_type "windows.h dxva.h" "DXVA_PicParams_HEVC" -DWINAPI_FAMILY=WINAPI_FAMIL
5148 5148
 check_type "windows.h d3d11.h" "ID3D11VideoDecoder"
5149 5149
 check_type "d3d9.h dxva2api.h" DXVA2_ConfigPictureDecode -D_WIN32_WINNT=0x0602
5150 5150
 
5151
+check_type "va/va.h" "VAPictureParameterBufferHEVC"
5152
+
5151 5153
 check_type "vdpau/vdpau.h" "VdpPictureInfoHEVC"
5152 5154
 
5153 5155
 if ! disabled w32threads && ! enabled pthreads; then
... ...
@@ -715,6 +715,7 @@ OBJS-$(CONFIG_H264_VDPAU_HWACCEL)         += vdpau_h264.o
715 715
 OBJS-$(CONFIG_H264_VIDEOTOOLBOX_HWACCEL)  += videotoolbox.o
716 716
 OBJS-$(CONFIG_HEVC_D3D11VA_HWACCEL)       += dxva2_hevc.o
717 717
 OBJS-$(CONFIG_HEVC_DXVA2_HWACCEL)         += dxva2_hevc.o
718
+OBJS-$(CONFIG_HEVC_VAAPI_HWACCEL)         += vaapi_hevc.o
718 719
 OBJS-$(CONFIG_HEVC_VDPAU_HWACCEL)         += vdpau_hevc.o
719 720
 OBJS-$(CONFIG_MPEG1_VDPAU_HWACCEL)        += vdpau_mpeg12.o
720 721
 OBJS-$(CONFIG_MPEG1_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
... ...
@@ -89,6 +89,7 @@ void avcodec_register_all(void)
89 89
     REGISTER_HWACCEL(HEVC_D3D11VA,      hevc_d3d11va);
90 90
     REGISTER_HWACCEL(HEVC_DXVA2,        hevc_dxva2);
91 91
     REGISTER_HWACCEL(HEVC_QSV,          hevc_qsv);
92
+    REGISTER_HWACCEL(HEVC_VAAPI,        hevc_vaapi);
92 93
     REGISTER_HWACCEL(HEVC_VDPAU,        hevc_vdpau);
93 94
     REGISTER_HWACCEL(MPEG1_XVMC,        mpeg1_xvmc);
94 95
     REGISTER_HWACCEL(MPEG1_VDPAU,       mpeg1_vdpau);
... ...
@@ -328,7 +328,7 @@ static void export_stream_params(AVCodecContext *avctx, const HEVCParamSets *ps,
328 328
 
329 329
 static int set_sps(HEVCContext *s, const HEVCSPS *sps, enum AVPixelFormat pix_fmt)
330 330
 {
331
-    #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + CONFIG_HEVC_D3D11VA_HWACCEL + CONFIG_HEVC_VDPAU_HWACCEL)
331
+    #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + CONFIG_HEVC_D3D11VA_HWACCEL + CONFIG_HEVC_VAAPI_HWACCEL + CONFIG_HEVC_VDPAU_HWACCEL)
332 332
     enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
333 333
     int ret, i;
334 334
 
... ...
@@ -352,6 +352,9 @@ static int set_sps(HEVCContext *s, const HEVCSPS *sps, enum AVPixelFormat pix_fm
352 352
 #if CONFIG_HEVC_D3D11VA_HWACCEL
353 353
         *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
354 354
 #endif
355
+#if CONFIG_HEVC_VAAPI_HWACCEL
356
+        *fmt++ = AV_PIX_FMT_VAAPI;
357
+#endif
355 358
 #if CONFIG_HEVC_VDPAU_HWACCEL
356 359
         *fmt++ = AV_PIX_FMT_VDPAU;
357 360
 #endif
358 361
new file mode 100644
... ...
@@ -0,0 +1,490 @@
0
+/*
1
+ * HEVC HW decode acceleration through VA API
2
+ *
3
+ * Copyright (C) 2015 Timo Rothenpieler <timo@rothenpieler.org>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include "vaapi_internal.h"
23
+#include "hevc.h"
24
+#include "mpegutils.h"
25
+
26
+/**
27
+ * @file
28
+ * This file implements the glue code between FFmpeg's and VA API's
29
+ * structures for HEVC decoding.
30
+ */
31
+
32
+typedef struct vaapi_hevc_frame_data {
33
+    VAPictureParameterBufferHEVC *pic_param;
34
+    VASliceParameterBufferHEVC *last_slice_param;
35
+} vaapi_hevc_frame_data;
36
+
37
+/**
38
+ * Initialize an empty VA API picture.
39
+ *
40
+ * VA API requires a fixed-size reference picture array.
41
+ */
42
+static void init_vaapi_pic(VAPictureHEVC *va_pic)
43
+{
44
+    va_pic->picture_id = VA_INVALID_ID;
45
+    va_pic->flags = VA_PICTURE_HEVC_INVALID;
46
+    va_pic->pic_order_cnt = 0;
47
+}
48
+
49
+static void fill_vaapi_pic(VAPictureHEVC *va_pic, const HEVCFrame *pic, int rps_type)
50
+{
51
+    va_pic->picture_id = ff_vaapi_get_surface_id(pic->frame);
52
+    va_pic->pic_order_cnt = pic->poc;
53
+    va_pic->flags = rps_type;
54
+
55
+    if (pic->flags & HEVC_FRAME_FLAG_LONG_REF)
56
+        va_pic->flags |= VA_PICTURE_HEVC_LONG_TERM_REFERENCE;
57
+
58
+    if (pic->frame->interlaced_frame) {
59
+        va_pic->flags |= VA_PICTURE_HEVC_FIELD_PIC;
60
+
61
+        if (!pic->frame->top_field_first) {
62
+            va_pic->flags |= VA_PICTURE_HEVC_BOTTOM_FIELD;
63
+        }
64
+    }
65
+}
66
+
67
+static int find_frame_rps_type(const HEVCContext *h, const HEVCFrame *pic)
68
+{
69
+    VASurfaceID pic_surf = ff_vaapi_get_surface_id(pic->frame);
70
+    int i;
71
+
72
+    for (i = 0; i < h->rps[ST_CURR_BEF].nb_refs; ++i) {
73
+        if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_BEF].ref[i]->frame))
74
+            return VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE;
75
+    }
76
+
77
+    for (i = 0; i < h->rps[ST_CURR_AFT].nb_refs; ++i) {
78
+        if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_AFT].ref[i]->frame))
79
+            return VA_PICTURE_HEVC_RPS_ST_CURR_AFTER;
80
+    }
81
+
82
+    for (i = 0; i < h->rps[LT_CURR].nb_refs; ++i) {
83
+        if (pic_surf == ff_vaapi_get_surface_id(h->rps[LT_CURR].ref[i]->frame))
84
+            return VA_PICTURE_HEVC_RPS_LT_CURR;
85
+    }
86
+
87
+    return 0;
88
+}
89
+
90
+static void fill_vaapi_ReferenceFrames(const HEVCContext *h, VAPictureParameterBufferHEVC *pp)
91
+{
92
+    const HEVCFrame *current_picture = h->ref;
93
+    int i, j, rps_type;
94
+
95
+    for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) {
96
+        const HEVCFrame *frame = NULL;
97
+
98
+        while (!frame && j < FF_ARRAY_ELEMS(h->DPB)) {
99
+            if (&h->DPB[j] != current_picture && (h->DPB[j].flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF)))
100
+                frame = &h->DPB[j];
101
+            j++;
102
+        }
103
+
104
+        init_vaapi_pic(&pp->ReferenceFrames[i]);
105
+
106
+        if (frame) {
107
+            rps_type = find_frame_rps_type(h, frame);
108
+            fill_vaapi_pic(&pp->ReferenceFrames[i], frame, rps_type);
109
+        }
110
+    }
111
+}
112
+
113
+static uint8_t get_ref_pic_index(const HEVCContext *h, const HEVCFrame *frame)
114
+{
115
+    vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private;
116
+    VAPictureParameterBufferHEVC *pp = frame_data->pic_param;
117
+    uint8_t i;
118
+
119
+    if (!frame)
120
+        return 0xff;
121
+
122
+    for (i = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); ++i) {
123
+        VASurfaceID pid = pp->ReferenceFrames[i].picture_id;
124
+        int poc = pp->ReferenceFrames[i].pic_order_cnt;
125
+        if (pid != VA_INVALID_ID && pid == ff_vaapi_get_surface_id(frame->frame) && poc == frame->poc)
126
+            return i;
127
+    }
128
+
129
+    return 0xff;
130
+}
131
+
132
+static void fill_picture_parameters(const HEVCContext *h, VAPictureParameterBufferHEVC *pp)
133
+{
134
+    int i;
135
+
136
+    pp->pic_fields.value = 0;
137
+    pp->slice_parsing_fields.value = 0;
138
+
139
+    fill_vaapi_pic(&pp->CurrPic, h->ref, 0);
140
+    fill_vaapi_ReferenceFrames(h, pp);
141
+
142
+    pp->pic_width_in_luma_samples  = h->ps.sps->width;
143
+    pp->pic_height_in_luma_samples = h->ps.sps->height;
144
+
145
+    pp->log2_min_luma_coding_block_size_minus3 = h->ps.sps->log2_min_cb_size - 3;
146
+
147
+    pp->pic_fields.bits.chroma_format_idc = h->ps.sps->chroma_format_idc;
148
+
149
+    pp->sps_max_dec_pic_buffering_minus1 = h->ps.sps->temporal_layer[h->ps.sps->max_sub_layers - 1].max_dec_pic_buffering - 1;
150
+    pp->log2_diff_max_min_luma_coding_block_size = h->ps.sps->log2_diff_max_min_coding_block_size;
151
+    pp->log2_min_transform_block_size_minus2 = h->ps.sps->log2_min_tb_size - 2;
152
+    pp->log2_diff_max_min_transform_block_size = h->ps.sps->log2_max_trafo_size  - h->ps.sps->log2_min_tb_size;
153
+    pp->max_transform_hierarchy_depth_inter = h->ps.sps->max_transform_hierarchy_depth_inter;
154
+    pp->max_transform_hierarchy_depth_intra = h->ps.sps->max_transform_hierarchy_depth_intra;
155
+    pp->num_short_term_ref_pic_sets = h->ps.sps->nb_st_rps;
156
+    pp->num_long_term_ref_pic_sps = h->ps.sps->num_long_term_ref_pics_sps;
157
+
158
+    pp->num_ref_idx_l0_default_active_minus1 = h->ps.pps->num_ref_idx_l0_default_active - 1;
159
+    pp->num_ref_idx_l1_default_active_minus1 = h->ps.pps->num_ref_idx_l1_default_active - 1;
160
+    pp->init_qp_minus26 = h->ps.pps->pic_init_qp_minus26;
161
+
162
+    pp->pps_cb_qp_offset = h->ps.pps->cb_qp_offset;
163
+    pp->pps_cr_qp_offset = h->ps.pps->cr_qp_offset;
164
+
165
+    pp->pic_fields.bits.tiles_enabled_flag = h->ps.pps->tiles_enabled_flag;
166
+    pp->pic_fields.bits.separate_colour_plane_flag = h->ps.sps->separate_colour_plane_flag;
167
+    pp->pic_fields.bits.pcm_enabled_flag = h->ps.sps->pcm_enabled_flag;
168
+    pp->pic_fields.bits.scaling_list_enabled_flag = h->ps.sps->scaling_list_enable_flag;
169
+    pp->pic_fields.bits.transform_skip_enabled_flag = h->ps.pps->transform_skip_enabled_flag;
170
+    pp->pic_fields.bits.amp_enabled_flag = h->ps.sps->amp_enabled_flag;
171
+    pp->pic_fields.bits.strong_intra_smoothing_enabled_flag = h->ps.sps->sps_strong_intra_smoothing_enable_flag;
172
+    pp->pic_fields.bits.sign_data_hiding_enabled_flag = h->ps.pps->sign_data_hiding_flag;
173
+    pp->pic_fields.bits.constrained_intra_pred_flag = h->ps.pps->constrained_intra_pred_flag;
174
+    pp->pic_fields.bits.cu_qp_delta_enabled_flag = h->ps.pps->cu_qp_delta_enabled_flag;
175
+    pp->pic_fields.bits.weighted_pred_flag = h->ps.pps->weighted_pred_flag;
176
+    pp->pic_fields.bits.weighted_bipred_flag = h->ps.pps->weighted_bipred_flag;
177
+    pp->pic_fields.bits.transquant_bypass_enabled_flag = h->ps.pps->transquant_bypass_enable_flag;
178
+    pp->pic_fields.bits.entropy_coding_sync_enabled_flag = h->ps.pps->entropy_coding_sync_enabled_flag;
179
+    pp->pic_fields.bits.pps_loop_filter_across_slices_enabled_flag = h->ps.pps->seq_loop_filter_across_slices_enabled_flag;
180
+    pp->pic_fields.bits.loop_filter_across_tiles_enabled_flag = h->ps.pps->loop_filter_across_tiles_enabled_flag;
181
+
182
+    pp->pic_fields.bits.pcm_loop_filter_disabled_flag = h->ps.sps->pcm.loop_filter_disable_flag;
183
+    pp->pcm_sample_bit_depth_luma_minus1 = h->ps.sps->pcm.bit_depth - 1;
184
+    pp->pcm_sample_bit_depth_chroma_minus1 = h->ps.sps->pcm.bit_depth_chroma - 1;
185
+    pp->log2_min_pcm_luma_coding_block_size_minus3 = h->ps.sps->pcm.log2_min_pcm_cb_size - 3;
186
+    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;
187
+
188
+    memset(pp->column_width_minus1, 0, sizeof(pp->column_width_minus1));
189
+    memset(pp->row_height_minus1, 0, sizeof(pp->row_height_minus1));
190
+
191
+    if (h->ps.pps->tiles_enabled_flag) {
192
+        pp->num_tile_columns_minus1 = h->ps.pps->num_tile_columns - 1;
193
+        pp->num_tile_rows_minus1 = h->ps.pps->num_tile_rows - 1;
194
+
195
+        for (i = 0; i < h->ps.pps->num_tile_columns; i++)
196
+            pp->column_width_minus1[i] = h->ps.pps->column_width[i] - 1;
197
+
198
+        for (i = 0; i < h->ps.pps->num_tile_rows; i++)
199
+            pp->row_height_minus1[i] = h->ps.pps->row_height[i] - 1;
200
+    }
201
+
202
+    pp->diff_cu_qp_delta_depth = h->ps.pps->diff_cu_qp_delta_depth;
203
+    pp->pps_beta_offset_div2 = h->ps.pps->beta_offset / 2;
204
+    pp->pps_tc_offset_div2 = h->ps.pps->tc_offset / 2;
205
+    pp->log2_parallel_merge_level_minus2 = h->ps.pps->log2_parallel_merge_level - 2;
206
+
207
+    /* Diffrent chroma/luma bit depths are currently not supported by ffmpeg. */
208
+    pp->bit_depth_luma_minus8 = h->ps.sps->bit_depth - 8;
209
+    pp->bit_depth_chroma_minus8 = h->ps.sps->bit_depth - 8;
210
+
211
+    pp->slice_parsing_fields.bits.lists_modification_present_flag = h->ps.pps->lists_modification_present_flag;
212
+    pp->slice_parsing_fields.bits.long_term_ref_pics_present_flag = h->ps.sps->long_term_ref_pics_present_flag;
213
+    pp->slice_parsing_fields.bits.sps_temporal_mvp_enabled_flag = h->ps.sps->sps_temporal_mvp_enabled_flag;
214
+    pp->slice_parsing_fields.bits.cabac_init_present_flag = h->ps.pps->cabac_init_present_flag;
215
+    pp->slice_parsing_fields.bits.output_flag_present_flag = h->ps.pps->output_flag_present_flag;
216
+    pp->slice_parsing_fields.bits.dependent_slice_segments_enabled_flag = h->ps.pps->dependent_slice_segments_enabled_flag;
217
+    pp->slice_parsing_fields.bits.pps_slice_chroma_qp_offsets_present_flag = h->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag;
218
+    pp->slice_parsing_fields.bits.sample_adaptive_offset_enabled_flag = h->ps.sps->sao_enabled;
219
+    pp->slice_parsing_fields.bits.deblocking_filter_override_enabled_flag = h->ps.pps->deblocking_filter_override_enabled_flag;
220
+    pp->slice_parsing_fields.bits.pps_disable_deblocking_filter_flag = h->ps.pps->disable_dbf;
221
+    pp->slice_parsing_fields.bits.slice_segment_header_extension_present_flag = h->ps.pps->slice_header_extension_present_flag;
222
+
223
+    pp->log2_max_pic_order_cnt_lsb_minus4 = h->ps.sps->log2_max_poc_lsb - 4;
224
+    pp->num_extra_slice_header_bits = h->ps.pps->num_extra_slice_header_bits;
225
+
226
+    if (h->nal_unit_type >= NAL_BLA_W_LP && h->nal_unit_type <= NAL_CRA_NUT) {
227
+        pp->slice_parsing_fields.bits.RapPicFlag = 1;
228
+    } else {
229
+        pp->slice_parsing_fields.bits.RapPicFlag = 0;
230
+    }
231
+
232
+    if (IS_IDR(h)) {
233
+        pp->slice_parsing_fields.bits.IdrPicFlag = 1;
234
+    } else {
235
+        pp->slice_parsing_fields.bits.IdrPicFlag = 0;
236
+    }
237
+
238
+    if (IS_IRAP(h)) {
239
+        pp->slice_parsing_fields.bits.IntraPicFlag = 1;
240
+    } else {
241
+        pp->slice_parsing_fields.bits.IntraPicFlag = 0;
242
+    }
243
+
244
+    if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) {
245
+        pp->st_rps_bits = h->sh.short_term_ref_pic_set_size;
246
+    } else {
247
+        pp->st_rps_bits = 0;
248
+    }
249
+
250
+    /* TODO */
251
+    pp->pic_fields.bits.NoPicReorderingFlag = 0;
252
+    pp->pic_fields.bits.NoBiPredFlag = 0;
253
+}
254
+
255
+
256
+/** Initialize and start decoding a frame with VA API. */
257
+static int vaapi_hevc_start_frame(AVCodecContext          *avctx,
258
+                                  av_unused const uint8_t *buffer,
259
+                                  av_unused uint32_t       size)
260
+{
261
+    HEVCContext * const h = avctx->priv_data;
262
+    FFVAContext * const vactx = ff_vaapi_get_context(avctx);
263
+    vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private;
264
+    VAPictureParameterBufferHEVC *pic_param;
265
+    VAIQMatrixBufferHEVC *iq_matrix;
266
+    ScalingList const * scaling_list;
267
+    int i, j, pos;
268
+
269
+    ff_dlog(avctx, "vaapi_hevc_start_frame()\n");
270
+
271
+    vactx->slice_param_size = sizeof(VASliceParameterBufferHEVC);
272
+
273
+    /* Fill in VAPictureParameterBufferHEVC. */
274
+    pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VAPictureParameterBufferHEVC));
275
+    if (!pic_param)
276
+        return -1;
277
+    fill_picture_parameters(h, pic_param);
278
+    frame_data->pic_param = pic_param;
279
+
280
+    /* Fill in VAIQMatrixBufferHEVC. */
281
+    if (h->ps.pps->scaling_list_data_present_flag) {
282
+        scaling_list = &h->ps.pps->scaling_list;
283
+    } else if (h->ps.sps->scaling_list_enable_flag) {
284
+        scaling_list = &h->ps.sps->scaling_list;
285
+    } else {
286
+        return 0;
287
+    }
288
+
289
+    iq_matrix = ff_vaapi_alloc_iq_matrix(vactx, sizeof(VAIQMatrixBufferHEVC));
290
+    if (!iq_matrix)
291
+        return -1;
292
+
293
+    for (i = 0; i < 6; ++i) {
294
+        for (j = 0; j < 16; ++j) {
295
+            pos = 4 * ff_hevc_diag_scan4x4_y[j] + ff_hevc_diag_scan4x4_x[j];
296
+            iq_matrix->ScalingList4x4[i][j] = scaling_list->sl[0][i][pos];
297
+        }
298
+        for (j = 0; j < 64; ++j) {
299
+            pos = 8 * ff_hevc_diag_scan8x8_y[j] + ff_hevc_diag_scan8x8_x[j];
300
+            iq_matrix->ScalingList8x8[i][j] = scaling_list->sl[1][i][pos];
301
+            iq_matrix->ScalingList16x16[i][j] = scaling_list->sl[2][i][pos];
302
+            if (i < 2) {
303
+                iq_matrix->ScalingList32x32[i][j] = scaling_list->sl[3][i * 3][pos];
304
+            }
305
+        }
306
+        iq_matrix->ScalingListDC16x16[i] = scaling_list->sl_dc[0][i];
307
+        if (i < 2) {
308
+            iq_matrix->ScalingListDC32x32[i] = scaling_list->sl_dc[1][i * 3];
309
+        }
310
+    }
311
+
312
+    return 0;
313
+}
314
+
315
+/** End a hardware decoding based frame. */
316
+static int vaapi_hevc_end_frame(AVCodecContext *avctx)
317
+{
318
+    FFVAContext * const vactx = ff_vaapi_get_context(avctx);
319
+    HEVCContext * const h = avctx->priv_data;
320
+    vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private;
321
+    int ret;
322
+
323
+    ff_dlog(avctx, "vaapi_hevc_end_frame()\n");
324
+
325
+    frame_data->last_slice_param->LongSliceFlags.fields.LastSliceOfPic = 1;
326
+
327
+    ret = ff_vaapi_commit_slices(vactx);
328
+    if (ret < 0)
329
+        goto finish;
330
+
331
+    ret = ff_vaapi_render_picture(vactx, ff_vaapi_get_surface_id(h->ref->frame));
332
+    if (ret < 0)
333
+        goto finish;
334
+
335
+finish:
336
+    ff_vaapi_common_end_frame(avctx);
337
+    return ret;
338
+}
339
+
340
+static int fill_pred_weight_table(HEVCContext * const h,
341
+                                  VASliceParameterBufferHEVC *slice_param,
342
+                                  SliceHeader * const sh)
343
+{
344
+    int i;
345
+
346
+    memset(slice_param->delta_luma_weight_l0, 0, sizeof(slice_param->delta_luma_weight_l0));
347
+    memset(slice_param->delta_luma_weight_l1, 0, sizeof(slice_param->delta_luma_weight_l1));
348
+    memset(slice_param->luma_offset_l0, 0, sizeof(slice_param->luma_offset_l0));
349
+    memset(slice_param->luma_offset_l1, 0, sizeof(slice_param->luma_offset_l1));
350
+    memset(slice_param->delta_chroma_weight_l0, 0, sizeof(slice_param->delta_chroma_weight_l0));
351
+    memset(slice_param->delta_chroma_weight_l1, 0, sizeof(slice_param->delta_chroma_weight_l1));
352
+    memset(slice_param->ChromaOffsetL0, 0, sizeof(slice_param->ChromaOffsetL0));
353
+    memset(slice_param->ChromaOffsetL1, 0, sizeof(slice_param->ChromaOffsetL1));
354
+
355
+    slice_param->delta_chroma_log2_weight_denom = 0;
356
+    slice_param->luma_log2_weight_denom = 0;
357
+
358
+    if (  sh->slice_type == I_SLICE
359
+      || (sh->slice_type == P_SLICE && !h->ps.pps->weighted_pred_flag)
360
+      || (sh->slice_type == B_SLICE && !h->ps.pps->weighted_bipred_flag)) {
361
+        return 0;
362
+    }
363
+
364
+    slice_param->luma_log2_weight_denom = sh->luma_log2_weight_denom;
365
+
366
+    if (h->ps.sps->chroma_format_idc) {
367
+        slice_param->delta_chroma_log2_weight_denom = sh->chroma_log2_weight_denom - sh->luma_log2_weight_denom;
368
+    }
369
+
370
+    for (i = 0; i < 15 && i < sh->nb_refs[L0]; ++i) {
371
+        slice_param->delta_luma_weight_l0[i] = sh->luma_weight_l0[i] - (1 << sh->luma_log2_weight_denom);
372
+        slice_param->luma_offset_l0[i] = sh->luma_offset_l0[i];
373
+        slice_param->delta_chroma_weight_l0[i][0] = sh->chroma_weight_l0[i][0] - (1 << sh->chroma_log2_weight_denom);
374
+        slice_param->delta_chroma_weight_l0[i][1] = sh->chroma_weight_l0[i][1] - (1 << sh->chroma_log2_weight_denom);
375
+        slice_param->ChromaOffsetL0[i][0] = sh->chroma_offset_l0[i][0];
376
+        slice_param->ChromaOffsetL0[i][1] = sh->chroma_offset_l0[i][1];
377
+    }
378
+
379
+    if (sh->slice_type == B_SLICE) {
380
+        for (i = 0; i < 15 && i < sh->nb_refs[L1]; ++i) {
381
+            slice_param->delta_luma_weight_l1[i] = sh->luma_weight_l1[i] - (1 << sh->luma_log2_weight_denom);
382
+            slice_param->luma_offset_l1[i] = sh->luma_offset_l1[i];
383
+            slice_param->delta_chroma_weight_l1[i][0] = sh->chroma_weight_l1[i][0] - (1 << sh->chroma_log2_weight_denom);
384
+            slice_param->delta_chroma_weight_l1[i][1] = sh->chroma_weight_l1[i][1] - (1 << sh->chroma_log2_weight_denom);
385
+            slice_param->ChromaOffsetL1[i][0] = sh->chroma_offset_l1[i][0];
386
+            slice_param->ChromaOffsetL1[i][1] = sh->chroma_offset_l1[i][1];
387
+        }
388
+    }
389
+
390
+    return 0;
391
+}
392
+
393
+/** Decode the given hevc slice with VA API. */
394
+static int vaapi_hevc_decode_slice(AVCodecContext *avctx,
395
+                                   const uint8_t  *buffer,
396
+                                   uint32_t        size)
397
+{
398
+    FFVAContext * const vactx = ff_vaapi_get_context(avctx);
399
+    HEVCContext * const h = avctx->priv_data;
400
+    vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private;
401
+    SliceHeader * const sh = &h->sh;
402
+    VASliceParameterBufferHEVC *slice_param;
403
+    int i, list_idx;
404
+    uint8_t nb_list = sh->slice_type == B_SLICE ? 2 : 1;
405
+
406
+    if (sh->slice_type == I_SLICE)
407
+        nb_list = 0;
408
+
409
+    ff_dlog(avctx, "vaapi_hevc_decode_slice(): buffer %p, size %d\n", buffer, size);
410
+
411
+    /* Fill in VASliceParameterBufferH264. */
412
+    slice_param = (VASliceParameterBufferHEVC *)ff_vaapi_alloc_slice(vactx, buffer, size);
413
+    if (!slice_param)
414
+        return -1;
415
+
416
+    frame_data->last_slice_param = slice_param;
417
+
418
+    /* The base structure changed, so this has to be re-set in order to be valid on every byte order. */
419
+    slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
420
+
421
+    /* 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. */
422
+    slice_param->slice_data_byte_offset = (get_bits_count(&h->HEVClc->gb) + 1 + 7) / 8;
423
+
424
+    slice_param->slice_segment_address = sh->slice_segment_addr;
425
+
426
+    slice_param->LongSliceFlags.value = 0;
427
+    slice_param->LongSliceFlags.fields.dependent_slice_segment_flag = sh->dependent_slice_segment_flag;
428
+    slice_param->LongSliceFlags.fields.slice_type = sh->slice_type;
429
+    slice_param->LongSliceFlags.fields.color_plane_id = sh->colour_plane_id;
430
+    slice_param->LongSliceFlags.fields.mvd_l1_zero_flag = sh->mvd_l1_zero_flag;
431
+    slice_param->LongSliceFlags.fields.cabac_init_flag = sh->cabac_init_flag;
432
+    slice_param->LongSliceFlags.fields.slice_temporal_mvp_enabled_flag = sh->slice_temporal_mvp_enabled_flag;
433
+    slice_param->LongSliceFlags.fields.slice_deblocking_filter_disabled_flag = sh->disable_deblocking_filter_flag;
434
+    slice_param->LongSliceFlags.fields.collocated_from_l0_flag = sh->collocated_list == L0 ? 1 : 0;
435
+    slice_param->LongSliceFlags.fields.slice_loop_filter_across_slices_enabled_flag = sh->slice_loop_filter_across_slices_enabled_flag;
436
+
437
+    slice_param->LongSliceFlags.fields.slice_sao_luma_flag = sh->slice_sample_adaptive_offset_flag[0];
438
+    if (h->ps.sps->chroma_format_idc) {
439
+        slice_param->LongSliceFlags.fields.slice_sao_chroma_flag = sh->slice_sample_adaptive_offset_flag[1];
440
+    }
441
+
442
+    if (sh->slice_temporal_mvp_enabled_flag) {
443
+        slice_param->collocated_ref_idx = sh->collocated_ref_idx;
444
+    } else {
445
+        slice_param->collocated_ref_idx = 0xFF;
446
+    }
447
+
448
+    slice_param->slice_qp_delta = sh->slice_qp_delta;
449
+    slice_param->slice_cb_qp_offset = sh->slice_cb_qp_offset;
450
+    slice_param->slice_cr_qp_offset = sh->slice_cr_qp_offset;
451
+    slice_param->slice_beta_offset_div2 = sh->beta_offset / 2;
452
+    slice_param->slice_tc_offset_div2 = sh->tc_offset / 2;
453
+
454
+    if (sh->slice_type == I_SLICE) {
455
+        slice_param->five_minus_max_num_merge_cand = 0;
456
+    } else {
457
+        slice_param->five_minus_max_num_merge_cand = 5 - sh->max_num_merge_cand;
458
+    }
459
+
460
+    slice_param->num_ref_idx_l0_active_minus1 = sh->nb_refs[L0] ? sh->nb_refs[L0] - 1 : 0;
461
+    slice_param->num_ref_idx_l1_active_minus1 = sh->nb_refs[L1] ? sh->nb_refs[L1] - 1 : 0;
462
+
463
+    memset(slice_param->RefPicList, 0xFF, sizeof(slice_param->RefPicList));
464
+
465
+    /* h->ref->refPicList is updated befor calling each slice */
466
+    for (list_idx = 0; list_idx < nb_list; ++list_idx) {
467
+        RefPicList *rpl = &h->ref->refPicList[list_idx];
468
+
469
+        for (i = 0; i < rpl->nb_refs; ++i) {
470
+            slice_param->RefPicList[list_idx][i] = get_ref_pic_index(h, rpl->ref[i]);
471
+        }
472
+    }
473
+
474
+    return fill_pred_weight_table(h, slice_param, sh);
475
+}
476
+
477
+AVHWAccel ff_hevc_vaapi_hwaccel = {
478
+    .name                 = "hevc_vaapi",
479
+    .type                 = AVMEDIA_TYPE_VIDEO,
480
+    .id                   = AV_CODEC_ID_HEVC,
481
+    .pix_fmt              = AV_PIX_FMT_VAAPI,
482
+    .start_frame          = vaapi_hevc_start_frame,
483
+    .end_frame            = vaapi_hevc_end_frame,
484
+    .decode_slice         = vaapi_hevc_decode_slice,
485
+    .init                 = ff_vaapi_context_init,
486
+    .uninit               = ff_vaapi_context_fini,
487
+    .priv_data_size       = sizeof(FFVAContext),
488
+    .frame_priv_data_size = sizeof(vaapi_hevc_frame_data),
489
+};
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 56
32
-#define LIBAVCODEC_VERSION_MINOR 58
32
+#define LIBAVCODEC_VERSION_MINOR 59
33 33
 #define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \