vaapi.c 10.6 KB
Newer Older
1
/*****************************************************************************
2
 * vaapi.c: VAAPI helpers for the libavcodec decoder
3
 *****************************************************************************
4
 * Copyright (C) 2017 VLC authors and VideoLAN
5
6
 * Copyright (C) 2009-2010 Laurent Aimar
 * Copyright (C) 2012-2014 Rémi Denis-Courmont
7
8
9
 *
 * Authors: Laurent Aimar <fenrir_AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
10
11
12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13
14
15
16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
17
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
20
21
22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
24
25
26
27
28
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

29
30
#include <assert.h>

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
Laurent Aimar's avatar
Laurent Aimar committed
33
#include <vlc_fourcc.h>
34
#include <vlc_picture.h>
35
#include <vlc_picture_pool.h>
36

37
38
39
40
41
42
43
#ifdef VLC_VA_BACKEND_DRM
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <vlc_fs.h>
# include <va/va_drm.h>
#endif
44
#include <libavcodec/avcodec.h>
45
#include <libavcodec/vaapi.h>
46
47

#include "avcodec.h"
48
#include "va.h"
49
#include "../../hw/vaapi/vlc_vaapi.h"
50

51
struct vlc_va_sys_t
52
{
53
    struct vlc_vaapi_instance *va_inst;
54
55
    struct vaapi_context hw_ctx;

Thomas Guillem's avatar
Thomas Guillem committed
56
#ifdef VLC_VA_BACKEND_DRM
57
    picture_pool_t *pool;
58
#endif
59
};
60

61
static int GetVaProfile(AVCodecContext *ctx, const es_format_t *fmt,
62
63
                        VAProfile *va_profile, int *vlc_chroma,
                        unsigned *pic_count)
Thomas Guillem's avatar
Thomas Guillem committed
64
65
66
{
    VAProfile i_profile;
    unsigned count = 3;
67
    int i_vlc_chroma = VLC_CODEC_VAAPI_420;
Thomas Guillem's avatar
Thomas Guillem committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    switch(ctx->codec_id)
    {
    case AV_CODEC_ID_MPEG1VIDEO:
    case AV_CODEC_ID_MPEG2VIDEO:
        i_profile = VAProfileMPEG2Main;
        count = 4;
        break;
    case AV_CODEC_ID_MPEG4:
        i_profile = VAProfileMPEG4AdvancedSimple;
        break;
    case AV_CODEC_ID_WMV3:
        i_profile = VAProfileVC1Main;
        break;
    case AV_CODEC_ID_VC1:
        i_profile = VAProfileVC1Advanced;
        break;
    case AV_CODEC_ID_H264:
        i_profile = VAProfileH264High;
        count = 18;
        break;
    case AV_CODEC_ID_HEVC:
90
        if (fmt->i_profile == FF_PROFILE_HEVC_MAIN)
Thomas Guillem's avatar
Thomas Guillem committed
91
            i_profile = VAProfileHEVCMain;
92
        else if (fmt->i_profile == FF_PROFILE_HEVC_MAIN_10)
93
        {
Thomas Guillem's avatar
Thomas Guillem committed
94
            i_profile = VAProfileHEVCMain10;
95
96
            i_vlc_chroma = VLC_CODEC_VAAPI_420_10BPP;
        }
Thomas Guillem's avatar
Thomas Guillem committed
97
98
99
100
101
102
103
104
105
        else
            return VLC_EGENERIC;
        count = 18;
        break;
    case AV_CODEC_ID_VP8:
        i_profile = VAProfileVP8Version0_3;
        count = 5;
        break;
    case AV_CODEC_ID_VP9:
106
        if (ctx->profile == FF_PROFILE_VP9_0)
Thomas Guillem's avatar
Thomas Guillem committed
107
108
            i_profile = VAProfileVP9Profile0;
#if VA_CHECK_VERSION( 0, 39, 0 )
109
110
        else if (ctx->profile == FF_PROFILE_VP9_2)
        {
Thomas Guillem's avatar
Thomas Guillem committed
111
            i_profile = VAProfileVP9Profile2;
112
113
            i_vlc_chroma = VLC_CODEC_VAAPI_420_10BPP;
        }
Thomas Guillem's avatar
Thomas Guillem committed
114
115
116
117
118
119
120
121
122
123
124
#endif
        else
            return VLC_EGENERIC;
        count = 10;
        break;
    default:
        return VLC_EGENERIC;
    }

    *va_profile = i_profile;
    *pic_count = count + ctx->thread_count;
125
    *vlc_chroma = i_vlc_chroma;
Thomas Guillem's avatar
Thomas Guillem committed
126
127
128
    return VLC_SUCCESS;
}

129
#ifndef VLC_VA_BACKEND_DRM
130

131
static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
132
133
134
135
136
137
138
139
140
{
    (void) va;

    vlc_vaapi_PicAttachContext(pic);
    *data = (void *) (uintptr_t) vlc_vaapi_PicGetSurface(pic);

    return VLC_SUCCESS;
}

141
static void Delete(vlc_va_t *va, void *hwctx)
142
143
144
145
{
    vlc_va_sys_t *sys = va->sys;
    vlc_object_t *o = VLC_OBJECT(va);

146
    (void) hwctx;
147
148
149

    vlc_vaapi_DestroyContext(o, sys->hw_ctx.display, sys->hw_ctx.context_id);
    vlc_vaapi_DestroyConfig(o, sys->hw_ctx.display, sys->hw_ctx.config_id);
150
    vlc_vaapi_ReleaseInstance(sys->va_inst);
151
152
153
    free(sys);
}

154
155
static int Create(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
                  const es_format_t *fmt, picture_sys_t *p_sys)
156
{
157
    if (pix_fmt != AV_PIX_FMT_VAAPI_VLD || p_sys == NULL)
158
159
160
161
162
163
164
165
166
        return VLC_EGENERIC;

    (void) fmt;
    vlc_object_t *o = VLC_OBJECT(va);

    int ret = VLC_EGENERIC;
    vlc_va_sys_t *sys = NULL;

    /* The picture must be allocated by the vout */
167
168
169
    VADisplay va_dpy;
    struct vlc_vaapi_instance *va_inst =
        vlc_vaapi_PicSysHoldInstance(p_sys, &va_dpy);
170
171
172
173
174
175
176
177
178

    VASurfaceID *render_targets;
    unsigned num_render_targets =
        vlc_vaapi_PicSysGetRenderTargets(p_sys, &render_targets);
    if (num_render_targets == 0)
        goto error;

    VAProfile i_profile;
    unsigned count;
179
180
    int i_vlc_chroma;
    if (GetVaProfile(ctx, fmt, &i_profile, &i_vlc_chroma, &count) != VLC_SUCCESS)
181
182
183
184
185
186
187
188
189
190
191
        goto error;

    sys = malloc(sizeof *sys);
    if (unlikely(sys == NULL))
    {
        ret = VLC_ENOMEM;
        goto error;
    }
    memset(sys, 0, sizeof (*sys));

    /* */
192
    sys->va_inst = va_inst;
193
194
195
196
197
198
    sys->hw_ctx.display = va_dpy;
    sys->hw_ctx.config_id = VA_INVALID_ID;
    sys->hw_ctx.context_id = VA_INVALID_ID;

    sys->hw_ctx.config_id =
        vlc_vaapi_CreateConfigChecked(o, sys->hw_ctx.display, i_profile,
199
                                      VAEntrypointVLD, i_vlc_chroma);
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    if (sys->hw_ctx.config_id == VA_INVALID_ID)
        goto error;

    /* Create a context */
    sys->hw_ctx.context_id =
        vlc_vaapi_CreateContext(o, sys->hw_ctx.display, sys->hw_ctx.config_id,
                                ctx->coded_width, ctx->coded_height, VA_PROGRESSIVE,
                                render_targets, num_render_targets);
    if (sys->hw_ctx.context_id == VA_INVALID_ID)
        goto error;

    ctx->hwaccel_context = &sys->hw_ctx;
    va->sys = sys;
    va->description = vaQueryVendorString(sys->hw_ctx.display);
214
    va->get = Get;
215
216
217
218
219
220
221
222
223
224
225
    return VLC_SUCCESS;

error:
    if (sys != NULL)
    {
        if (sys->hw_ctx.context_id != VA_INVALID_ID)
            vlc_vaapi_DestroyContext(o, sys->hw_ctx.display, sys->hw_ctx.context_id);
        if (sys->hw_ctx.config_id != VA_INVALID_ID)
            vlc_vaapi_DestroyConfig(o, sys->hw_ctx.display, sys->hw_ctx.config_id);
        free(sys);
    }
226
    vlc_vaapi_ReleaseInstance(va_inst);
227
228
229
    return ret;
}

Thomas Guillem's avatar
Thomas Guillem committed
230
#else /* DRM */
231

232
static int GetDRM(vlc_va_t *va, picture_t *pic, uint8_t **data)
233
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
234
    vlc_va_sys_t *sys = va->sys;
235

236
237
238
239
    picture_t *vapic = picture_pool_Wait(sys->pool);
    if (vapic == NULL)
        return VLC_EGENERIC;
    vlc_vaapi_PicAttachContext(vapic);
240

241
242
243
244
    pic->context = vapic->context->copy(vapic->context);
    picture_Release(vapic);
    if (pic->context == NULL)
        return VLC_EGENERIC;
245

246
    *data = (void *)(uintptr_t)vlc_vaapi_PicGetSurface(pic);
247
248
    return VLC_SUCCESS;
}
249

250
static void DeleteDRM(vlc_va_t *va, void **hwctx)
251
{
252
    vlc_va_sys_t *sys = va->sys;
253
    vlc_object_t *o = VLC_OBJECT(va);
254

255
    (void) hwctx;
256
257
258
    picture_pool_Release(sys->pool);
    vlc_vaapi_DestroyContext(o, sys->hw_ctx.display, sys->hw_ctx.context_id);
    vlc_vaapi_DestroyConfig(o, sys->hw_ctx.display, sys->hw_ctx.config_id);
259
    vlc_vaapi_ReleaseInstance(sys->va_inst);
260
261
262
    free(sys);
}

263
264
static int CreateDRM(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
                     const es_format_t *fmt, picture_sys_t *p_sys)
265
{
266
    if (pix_fmt != AV_PIX_FMT_VAAPI_VLD || p_sys)
267
268
        return VLC_EGENERIC;

269
    (void) fmt;
270
    (void) p_sys;
271
272
273
    vlc_object_t *o = VLC_OBJECT(va);

    VAProfile i_profile;
Thomas Guillem's avatar
Thomas Guillem committed
274
    unsigned count;
275
276
    int i_vlc_chroma;
    if (GetVaProfile(ctx, fmt, &i_profile, &i_vlc_chroma, &count) != VLC_SUCCESS)
277
        return VLC_EGENERIC;
278

279
280
    vlc_va_sys_t *sys;

281
282
    sys = malloc(sizeof(vlc_va_sys_t));
    if (!sys)
283
       return VLC_ENOMEM;
284
285
    memset(sys, 0, sizeof (*sys));

286
    /* */
287
288
289
    sys->hw_ctx.display = NULL;
    sys->hw_ctx.config_id = VA_INVALID_ID;
    sys->hw_ctx.context_id = VA_INVALID_ID;
290
    sys->pool = NULL;
291
    sys->va_inst = NULL;
292
293

    /* Create a VA display */
294
295
    sys->va_inst = vlc_vaapi_InitializeInstanceDRM(o, vaGetDisplayDRM,
                                                   &sys->hw_ctx.display, NULL);
296
    if (!sys->va_inst)
297
298
        goto error;

299
300
301
    sys->hw_ctx.config_id =
        vlc_vaapi_CreateConfigChecked(o, sys->hw_ctx.display, i_profile,
                                      VAEntrypointVLD, 0);
302
303
    if (sys->hw_ctx.config_id == VA_INVALID_ID)
        goto error;
304

305
306
    /* Create surfaces */
    assert(ctx->coded_width > 0 && ctx->coded_height > 0);
307
    video_format_t vfmt = {
308
        .i_chroma = i_vlc_chroma,
309
310
311
312
313
        .i_width = ctx->coded_width,
        .i_height = ctx->coded_height,
        .i_visible_width = ctx->coded_width,
        .i_visible_height = ctx->coded_height
    };
314

315
    VASurfaceID *surfaces;
316
    sys->pool = vlc_vaapi_PoolNew(o, sys->va_inst, sys->hw_ctx.display, count,
317
                                  &surfaces, &vfmt, false);
318

319
    if (!sys->pool)
320
321
        goto error;

322
323
324
325
326
327
    /* Create a context */
    sys->hw_ctx.context_id =
        vlc_vaapi_CreateContext(o, sys->hw_ctx.display, sys->hw_ctx.config_id,
                                ctx->coded_width, ctx->coded_height,
                                VA_PROGRESSIVE, surfaces, count);
    if (sys->hw_ctx.context_id == VA_INVALID_ID)
328
329
330
        goto error;

    ctx->hwaccel_context = &sys->hw_ctx;
331
    va->sys = sys;
332
    va->description = vaQueryVendorString(sys->hw_ctx.display);
333
    va->get = GetDRM;
334
335
336
    return VLC_SUCCESS;

error:
337
    if (sys->hw_ctx.context_id != VA_INVALID_ID)
338
339
340
        vlc_vaapi_DestroyContext(o, sys->hw_ctx.display, sys->hw_ctx.context_id);
    if (sys->pool != NULL)
        picture_pool_Release(sys->pool);
341
    if (sys->hw_ctx.config_id != VA_INVALID_ID)
342
        vlc_vaapi_DestroyConfig(o, sys->hw_ctx.display, sys->hw_ctx.config_id);
343
344
    if (sys->va_inst != NULL)
        vlc_vaapi_ReleaseInstance(sys->va_inst);
345
    free(sys);
346
347
    return VLC_EGENERIC;
}
348
#endif
349
350

vlc_module_begin ()
351
#ifdef VLC_VA_BACKEND_DRM
352
353
    set_description( N_("VA-API video decoder via DRM") )
    set_capability( "hw decoder", 0 )
354
355
356
357
    set_callbacks( CreateDRM, DeleteDRM )
    add_shortcut( "vaapi", "vaapi_drm" )
#else
    set_description( N_("VA-API video decoder") )
358
    set_capability( "hw decoder", 100 )
359
360
    set_callbacks( Create, Delete )
    add_shortcut( "vaapi" )
361
#endif
362
363
364
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_VCODEC )
vlc_module_end ()