GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/vaapi_av1.c Lines: 0 163 0.0 %
Date: 2020-11-28 20:53:16 Branches: 0 44 0.0 %

Line Branch Exec Source
1
/*
2
 * AV1 HW decode acceleration through VA API
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include "libavutil/pixdesc.h"
22
#include "hwconfig.h"
23
#include "vaapi_decode.h"
24
#include "av1dec.h"
25
26
static VASurfaceID vaapi_av1_surface_id(AV1Frame *vf)
27
{
28
    if (vf)
29
        return ff_vaapi_get_surface_id(vf->tf.f);
30
    else
31
        return VA_INVALID_SURFACE;
32
}
33
34
static int8_t vaapi_av1_get_bit_depth_idx(AVCodecContext *avctx)
35
{
36
    AV1DecContext *s = avctx->priv_data;
37
    const AV1RawSequenceHeader *seq = s->raw_seq;
38
    int8_t bit_depth = 8;
39
40
    if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
41
        bit_depth = seq->color_config.twelve_bit ? 12 : 10;
42
    else if (seq->seq_profile <= 2)
43
        bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
44
    else {
45
        av_log(avctx, AV_LOG_ERROR,
46
               "Couldn't get bit depth from profile:%d.\n", seq->seq_profile);
47
        return -1;
48
    }
49
    return bit_depth == 8 ? 0 : bit_depth == 10 ? 1 : 2;
50
}
51
52
static int vaapi_av1_start_frame(AVCodecContext *avctx,
53
                                 av_unused const uint8_t *buffer,
54
                                 av_unused uint32_t size)
55
{
56
    AV1DecContext *s = avctx->priv_data;
57
    const AV1RawSequenceHeader *seq = s->raw_seq;
58
    const AV1RawFrameHeader *frame_header = s->raw_frame_header;
59
    VAAPIDecodePicture *pic = s->cur_frame.hwaccel_picture_private;
60
    VADecPictureParameterBufferAV1 pic_param;
61
    int8_t bit_depth_idx;
62
    int err = 0;
63
    uint8_t remap_lr_type[4] = {AV1_RESTORE_NONE, AV1_RESTORE_SWITCHABLE, AV1_RESTORE_WIENER, AV1_RESTORE_SGRPROJ};
64
65
    pic->output_surface = vaapi_av1_surface_id(&s->cur_frame);
66
67
    bit_depth_idx = vaapi_av1_get_bit_depth_idx(avctx);
68
    if (bit_depth_idx < 0)
69
        goto fail;
70
71
    memset(&pic_param, 0, sizeof(VADecPictureParameterBufferAV1));
72
    pic_param = (VADecPictureParameterBufferAV1) {
73
        .profile                 = seq->seq_profile,
74
        .order_hint_bits_minus_1 = seq->order_hint_bits_minus_1,
75
        .bit_depth_idx           = bit_depth_idx,
76
        .current_frame           = pic->output_surface,
77
        .current_display_picture = pic->output_surface,
78
        .frame_width_minus1      = frame_header->frame_width_minus_1,
79
        .frame_height_minus1     = frame_header->frame_height_minus_1,
80
        .primary_ref_frame       = frame_header->primary_ref_frame,
81
        .order_hint              = frame_header->order_hint,
82
        .tile_cols               = frame_header->tile_cols,
83
        .tile_rows               = frame_header->tile_rows,
84
        .context_update_tile_id  = frame_header->context_update_tile_id,
85
        .interp_filter           = frame_header->interpolation_filter,
86
        .filter_level[0]         = frame_header->loop_filter_level[0],
87
        .filter_level[1]         = frame_header->loop_filter_level[1],
88
        .filter_level_u          = frame_header->loop_filter_level[2],
89
        .filter_level_v          = frame_header->loop_filter_level[3],
90
        .base_qindex             = frame_header->base_q_idx,
91
        .cdef_damping_minus_3    = frame_header->cdef_damping_minus_3,
92
        .cdef_bits               = frame_header->cdef_bits,
93
        .seq_info_fields.fields = {
94
            .still_picture              = seq->still_picture,
95
            .use_128x128_superblock     = seq->use_128x128_superblock,
96
            .enable_filter_intra        = seq->enable_filter_intra,
97
            .enable_intra_edge_filter   = seq->enable_intra_edge_filter,
98
            .enable_interintra_compound = seq->enable_interintra_compound,
99
            .enable_masked_compound     = seq->enable_masked_compound,
100
            .enable_dual_filter         = seq->enable_dual_filter,
101
            .enable_order_hint          = seq->enable_order_hint,
102
            .enable_jnt_comp            = seq->enable_jnt_comp,
103
            .enable_cdef                = seq->enable_cdef,
104
            .mono_chrome                = seq->color_config.mono_chrome,
105
            .color_range                = seq->color_config.color_range,
106
            .subsampling_x              = seq->color_config.subsampling_x,
107
            .subsampling_y              = seq->color_config.subsampling_y,
108
            .chroma_sample_position     = seq->color_config.chroma_sample_position,
109
            .film_grain_params_present  = seq->film_grain_params_present,
110
        },
111
        .seg_info.segment_info_fields.bits = {
112
            .enabled         = frame_header->segmentation_enabled,
113
            .update_map      = frame_header->segmentation_update_map,
114
            .temporal_update = frame_header->segmentation_temporal_update,
115
            .update_data     = frame_header->segmentation_update_data,
116
        },
117
        .film_grain_info.film_grain_info_fields.bits = {
118
            .apply_grain              = frame_header->apply_grain,
119
            .chroma_scaling_from_luma = frame_header->chroma_scaling_from_luma,
120
            .grain_scaling_minus_8    = frame_header->grain_scaling_minus_8,
121
            .ar_coeff_lag             = frame_header->ar_coeff_lag,
122
            .ar_coeff_shift_minus_6   = frame_header->ar_coeff_shift_minus_6,
123
            .grain_scale_shift        = frame_header->grain_scale_shift,
124
            .overlap_flag             = frame_header->overlap_flag,
125
            .clip_to_restricted_range = frame_header->clip_to_restricted_range,
126
        },
127
        .pic_info_fields.bits = {
128
            .frame_type                   = frame_header->frame_type,
129
            .show_frame                   = frame_header->show_frame,
130
            .showable_frame               = frame_header->showable_frame,
131
            .error_resilient_mode         = frame_header->error_resilient_mode,
132
            .disable_cdf_update           = frame_header->disable_cdf_update,
133
            .allow_screen_content_tools   = frame_header->allow_screen_content_tools,
134
            .force_integer_mv             = frame_header->force_integer_mv,
135
            .allow_intrabc                = frame_header->allow_intrabc,
136
            .use_superres                 = frame_header->use_superres,
137
            .allow_high_precision_mv      = frame_header->allow_high_precision_mv,
138
            .is_motion_mode_switchable    = frame_header->is_motion_mode_switchable,
139
            .use_ref_frame_mvs            = frame_header->use_ref_frame_mvs,
140
            .disable_frame_end_update_cdf = frame_header->disable_frame_end_update_cdf,
141
            .uniform_tile_spacing_flag    = frame_header->uniform_tile_spacing_flag,
142
            .allow_warped_motion          = frame_header->allow_warped_motion,
143
        },
144
        .loop_filter_info_fields.bits = {
145
            .sharpness_level        = frame_header->loop_filter_sharpness,
146
            .mode_ref_delta_enabled = frame_header->loop_filter_delta_enabled,
147
            .mode_ref_delta_update  = frame_header->loop_filter_delta_update,
148
        },
149
        .mode_control_fields.bits = {
150
            .delta_q_present_flag = frame_header->delta_q_present,
151
            .log2_delta_q_res     = frame_header->delta_q_res,
152
            .tx_mode              = frame_header->tx_mode,
153
            .reference_select     = frame_header->reference_select,
154
            .reduced_tx_set_used  = frame_header->reduced_tx_set,
155
            .skip_mode_present    = frame_header->skip_mode_present,
156
        },
157
        .loop_restoration_fields.bits = {
158
            .yframe_restoration_type  = remap_lr_type[frame_header->lr_type[0]],
159
            .cbframe_restoration_type = remap_lr_type[frame_header->lr_type[1]],
160
            .crframe_restoration_type = remap_lr_type[frame_header->lr_type[2]],
161
            .lr_unit_shift            = frame_header->lr_unit_shift,
162
            .lr_uv_shift              = frame_header->lr_uv_shift,
163
        },
164
        .qmatrix_fields.bits = {
165
            .using_qmatrix = frame_header->using_qmatrix,
166
        }
167
    };
168
169
    for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
170
        if (pic_param.pic_info_fields.bits.frame_type == AV1_FRAME_KEY)
171
            pic_param.ref_frame_map[i] = VA_INVALID_ID;
172
        else
173
            pic_param.ref_frame_map[i] = vaapi_av1_surface_id(&s->ref[i]);
174
    }
175
    for (int i = 0; i < AV1_REFS_PER_FRAME; i++) {
176
        pic_param.ref_frame_idx[i] = frame_header->ref_frame_idx[i];
177
    }
178
    for (int i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++) {
179
        pic_param.ref_deltas[i] = frame_header->loop_filter_ref_deltas[i];
180
    }
181
    for (int i = 0; i < 2; i++) {
182
        pic_param.mode_deltas[i] = frame_header->loop_filter_mode_deltas[i];
183
    }
184
    for (int i = 0; i < (1 << frame_header->cdef_bits); i++) {
185
        pic_param.cdef_y_strengths[i] =
186
            (frame_header->cdef_y_pri_strength[i] << 2) +
187
                frame_header->cdef_y_sec_strength[i];
188
        pic_param.cdef_uv_strengths[i] =
189
            (frame_header->cdef_uv_pri_strength[i] << 2) +
190
                frame_header->cdef_uv_sec_strength[i];
191
    }
192
    for (int i = 0; i < frame_header->tile_cols; i++) {
193
        pic_param.width_in_sbs_minus_1[i] =
194
            frame_header->width_in_sbs_minus_1[i];
195
    }
196
    for (int i = 0; i < frame_header->tile_rows; i++) {
197
        pic_param.height_in_sbs_minus_1[i] =
198
            frame_header->height_in_sbs_minus_1[i];
199
    }
200
    for (int i = AV1_REF_FRAME_LAST; i <= AV1_REF_FRAME_ALTREF; i++) {
201
        pic_param.wm[i - 1].wmtype = s->cur_frame.gm_type[i];
202
        for (int j = 0; j < 6; j++)
203
            pic_param.wm[i - 1].wmmat[j] = s->cur_frame.gm_params[i][j];
204
    }
205
    err = ff_vaapi_decode_make_param_buffer(avctx, pic,
206
                                            VAPictureParameterBufferType,
207
                                            &pic_param, sizeof(pic_param));
208
    if (err < 0)
209
        goto fail;
210
211
    return 0;
212
213
fail:
214
    ff_vaapi_decode_cancel(avctx, pic);
215
    return err;
216
}
217
218
static int vaapi_av1_end_frame(AVCodecContext *avctx)
219
{
220
    const AV1DecContext *s = avctx->priv_data;
221
    VAAPIDecodePicture *pic = s->cur_frame.hwaccel_picture_private;
222
    return ff_vaapi_decode_issue(avctx, pic);
223
}
224
225
static int vaapi_av1_decode_slice(AVCodecContext *avctx,
226
                                  const uint8_t *buffer,
227
                                  uint32_t size)
228
{
229
    const AV1DecContext *s = avctx->priv_data;
230
    VAAPIDecodePicture *pic = s->cur_frame.hwaccel_picture_private;
231
    VASliceParameterBufferAV1 slice_param;
232
    int err = 0;
233
234
    for (int i = s->tg_start; i <= s->tg_end; i++) {
235
        memset(&slice_param, 0, sizeof(VASliceParameterBufferAV1));
236
237
        slice_param = (VASliceParameterBufferAV1) {
238
            .slice_data_size   = s->tile_group_info[i].tile_size,
239
            .slice_data_offset = s->tile_group_info[i].tile_offset,
240
            .slice_data_flag   = VA_SLICE_DATA_FLAG_ALL,
241
            .tile_row          = s->tile_group_info[i].tile_row,
242
            .tile_column       = s->tile_group_info[i].tile_column,
243
            .tg_start          = s->tg_start,
244
            .tg_end            = s->tg_end,
245
        };
246
247
        err = ff_vaapi_decode_make_slice_buffer(avctx, pic, &slice_param,
248
                                                sizeof(VASliceParameterBufferAV1),
249
                                                buffer,
250
                                                s->tile_group_info[i].tile_size);
251
        if (err) {
252
            ff_vaapi_decode_cancel(avctx, pic);
253
            return err;
254
        }
255
    }
256
257
    return 0;
258
}
259
260
const AVHWAccel ff_av1_vaapi_hwaccel = {
261
    .name                 = "av1_vaapi",
262
    .type                 = AVMEDIA_TYPE_VIDEO,
263
    .id                   = AV_CODEC_ID_AV1,
264
    .pix_fmt              = AV_PIX_FMT_VAAPI,
265
    .start_frame          = vaapi_av1_start_frame,
266
    .end_frame            = vaapi_av1_end_frame,
267
    .decode_slice         = vaapi_av1_decode_slice,
268
    .frame_priv_data_size = sizeof(VAAPIDecodePicture),
269
    .init                 = ff_vaapi_decode_init,
270
    .uninit               = ff_vaapi_decode_uninit,
271
    .frame_params         = ff_vaapi_common_frame_params,
272
    .priv_data_size       = sizeof(VAAPIDecodeContext),
273
    .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
274
};