File: vaapi.c

package info (click to toggle)
vlc 3.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208,020 kB
  • sloc: ansic: 443,448; cpp: 111,223; objc: 36,399; sh: 6,737; makefile: 6,627; javascript: 4,902; xml: 1,611; asm: 1,355; yacc: 644; python: 321; lex: 88; perl: 77; sed: 16
file content (400 lines) | stat: -rw-r--r-- 11,692 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*****************************************************************************
 * vaapi.c: VAAPI helpers for the libavcodec decoder
 *****************************************************************************
 * Copyright (C) 2017 VLC authors and VideoLAN
 * Copyright (C) 2009-2010 Laurent Aimar
 * Copyright (C) 2012-2014 Rémi Denis-Courmont
 *
 * Authors: Laurent Aimar <fenrir_AT_ videolan _DOT_ org>
 *
 * 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
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * 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.
 *****************************************************************************/

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

#include <assert.h>

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_fourcc.h>
#include <vlc_picture.h>
#include <vlc_picture_pool.h>

#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
#include <libavcodec/avcodec.h>
#if FF_API_STRUCT_VAAPI_CONTEXT
#include <libavcodec/vaapi.h>
#else
#include <libavutil/hwcontext_vaapi.h>
#endif

#include "avcodec.h"
#include "va.h"
#include "../../hw/vaapi/vlc_vaapi.h"

#if !FF_API_STRUCT_VAAPI_CONTEXT
struct vaapi_context
{
    VADisplay display;
    VAConfigID config_id;
    VAContextID context_id;
};
#endif

struct vlc_va_sys_t
{
    struct vlc_vaapi_instance *va_inst;
    struct vaapi_context hw_ctx;

#ifdef VLC_VA_BACKEND_DRM
    picture_pool_t *pool;
#endif
};

static int GetVaProfile(AVCodecContext *ctx, const es_format_t *fmt,
                        VAProfile *va_profile, int *vlc_chroma,
                        unsigned *pic_count)
{
    VAProfile i_profile;
    unsigned count = 3;
    int i_vlc_chroma = VLC_CODEC_VAAPI_420;

    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:
        if (ctx->profile == AVPROFILE(HEVC_MAIN))
            i_profile = VAProfileHEVCMain;
        else if (ctx->profile == AVPROFILE(HEVC_MAIN_10))
        {
            i_profile = VAProfileHEVCMain10;
            i_vlc_chroma = VLC_CODEC_VAAPI_420_10BPP;
        }
        else
            return VLC_EGENERIC;
        count = 18;
        break;
    case AV_CODEC_ID_VP8:
        i_profile = VAProfileVP8Version0_3;
        count = 5;
        break;
    case AV_CODEC_ID_VP9:
        if (ctx->profile == AVPROFILE(VP9_0))
            i_profile = VAProfileVP9Profile0;
#if VA_CHECK_VERSION( 0, 39, 0 )
        else if (ctx->profile == AVPROFILE(VP9_2))
        {
            i_profile = VAProfileVP9Profile2;
            i_vlc_chroma = VLC_CODEC_VAAPI_420_10BPP;
        }
#endif
        else
            return VLC_EGENERIC;
        count = 10;
        break;
    default:
        return VLC_EGENERIC;
    }

    *va_profile = i_profile;
    *pic_count = count + ctx->thread_count;
    *vlc_chroma = i_vlc_chroma;
    return VLC_SUCCESS;
}

#ifndef VLC_VA_BACKEND_DRM

static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
{
    (void) va;

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

    return VLC_SUCCESS;
}

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

    (void) hwctx;

    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);
    vlc_vaapi_ReleaseInstance(sys->va_inst);
    free(sys);
}

static int Create(vlc_va_t *va, AVCodecContext *ctx, const AVPixFmtDescriptor *desc,
                  enum PixelFormat pix_fmt,
                  const es_format_t *fmt, picture_sys_t *p_sys)
{
    VLC_UNUSED(desc);
    if (pix_fmt != AV_PIX_FMT_VAAPI || p_sys == NULL)
        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 */
    VADisplay va_dpy;
    struct vlc_vaapi_instance *va_inst =
        vlc_vaapi_PicSysHoldInstance(p_sys, &va_dpy);

    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;
    int i_vlc_chroma;
    if (GetVaProfile(ctx, fmt, &i_profile, &i_vlc_chroma, &count) != VLC_SUCCESS)
        goto error;

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

    /* */
    sys->va_inst = va_inst;
    sys->hw_ctx.display = va_dpy;
    sys->hw_ctx.config_id = VA_INVALID_ID;
    sys->hw_ctx.context_id = VA_INVALID_ID;

#if FF_API_STRUCT_VAAPI_CONTEXT
    sys->hw_ctx.config_id =
        vlc_vaapi_CreateConfigChecked(o, sys->hw_ctx.display, i_profile,
                                      VAEntrypointVLD, i_vlc_chroma);
    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;
#else
    AVBufferRef *hwdev_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
    if (hwdev_ref == NULL)
        goto error;

    AVHWDeviceContext *hwdev_ctx = (void *) hwdev_ref->data;
    AVVAAPIDeviceContext *vadev_ctx = hwdev_ctx->hwctx;
    vadev_ctx->display = va_dpy;

    if (av_hwdevice_ctx_init(hwdev_ref) < 0)
    {
        av_buffer_unref(&hwdev_ref);
        goto error;
    }

    ctx->hw_device_ctx = hwdev_ref;
#endif

    va->sys = sys;
    va->description = vaQueryVendorString(sys->hw_ctx.display);
    va->get = Get;
    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);
    }
    vlc_vaapi_ReleaseInstance(va_inst);
    return ret;
}

#else /* DRM */

static int GetDRM(vlc_va_t *va, picture_t *pic, uint8_t **data)
{
    vlc_va_sys_t *sys = va->sys;

    picture_t *vapic = picture_pool_Wait(sys->pool);
    if (vapic == NULL)
        return VLC_EGENERIC;
    vlc_vaapi_PicAttachContext(vapic);

    pic->context = vapic->context->copy(vapic->context);
    picture_Release(vapic);
    if (pic->context == NULL)
        return VLC_EGENERIC;

    *data = (void *)(uintptr_t)vlc_vaapi_PicGetSurface(pic);
    return VLC_SUCCESS;
}

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

    (void) hwctx;
    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);
    vlc_vaapi_ReleaseInstance(sys->va_inst);
    free(sys);
}

static int CreateDRM(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
                     const es_format_t *fmt, picture_sys_t *p_sys)
{
    if (pix_fmt != AV_PIX_FMT_VAAPI || p_sys)
        return VLC_EGENERIC;

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

    VAProfile i_profile;
    unsigned count;
    int i_vlc_chroma;
    if (GetVaProfile(ctx, fmt, &i_profile, &i_vlc_chroma, &count) != VLC_SUCCESS)
        return VLC_EGENERIC;

    vlc_va_sys_t *sys;

    sys = malloc(sizeof(vlc_va_sys_t));
    if (!sys)
       return VLC_ENOMEM;
    memset(sys, 0, sizeof (*sys));

    /* */
    sys->hw_ctx.display = NULL;
    sys->hw_ctx.config_id = VA_INVALID_ID;
    sys->hw_ctx.context_id = VA_INVALID_ID;
    sys->pool = NULL;
    sys->va_inst = NULL;

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

    sys->hw_ctx.config_id =
        vlc_vaapi_CreateConfigChecked(o, sys->hw_ctx.display, i_profile,
                                      VAEntrypointVLD, 0);
    if (sys->hw_ctx.config_id == VA_INVALID_ID)
        goto error;

    /* Create surfaces */
    assert(ctx->coded_width > 0 && ctx->coded_height > 0);
    video_format_t vfmt = {
        .i_chroma = i_vlc_chroma,
        .i_width = ctx->coded_width,
        .i_height = ctx->coded_height,
        .i_visible_width = ctx->coded_width,
        .i_visible_height = ctx->coded_height
    };

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

    if (!sys->pool)
        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, surfaces, count);
    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);
    va->get = GetDRM;
    return VLC_SUCCESS;

error:
    if (sys->hw_ctx.context_id != VA_INVALID_ID)
        vlc_vaapi_DestroyContext(o, sys->hw_ctx.display, sys->hw_ctx.context_id);
    if (sys->pool != NULL)
        picture_pool_Release(sys->pool);
    if (sys->hw_ctx.config_id != VA_INVALID_ID)
        vlc_vaapi_DestroyConfig(o, sys->hw_ctx.display, sys->hw_ctx.config_id);
    if (sys->va_inst != NULL)
        vlc_vaapi_ReleaseInstance(sys->va_inst);
    free(sys);
    return VLC_EGENERIC;
}
#endif

vlc_module_begin ()
#ifdef VLC_VA_BACKEND_DRM
    set_description( N_("VA-API video decoder via DRM") )
    set_capability( "hw decoder", 0 )
    set_callbacks( CreateDRM, DeleteDRM )
    add_shortcut( "vaapi", "vaapi_drm" )
#else
    set_description( N_("VA-API video decoder") )
    set_capability( "hw decoder", 100 )
    set_callbacks( Create, Delete )
    add_shortcut( "vaapi" )
#endif
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_VCODEC )
vlc_module_end ()