avcodec.c 10.4 KB
Newer Older
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
/*****************************************************************************
 * avcodec.c: VDPAU decoder for libav
 *****************************************************************************
 * Copyright (C) 2012-2013 Rémi Denis-Courmont
 *
 * 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 <string.h>
#include <stdlib.h>
27
#include <stdio.h>
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
#include <assert.h>

#include <libavutil/mem.h>
#include <libavcodec/avcodec.h>
#include <libavcodec/vdpau.h>
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_fourcc.h>
#include <vlc_picture.h>
#include <vlc_xlib.h>
#include "vlc_vdpau.h"
#include "../../codec/avcodec/va.h"

static int Open(vlc_va_t *, int, const es_format_t *);
static void Close(vlc_va_t *);

vlc_module_begin()
    set_description(N_("Video Decode and Presentation API for Unix (VDPAU)"))
    set_capability("hw decoder", 100)
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_VCODEC)
    set_callbacks(Open, Close)
    add_shortcut("vdpau")
vlc_module_end()

struct vlc_va_sys_t
{
    vdp_t *vdp;
    VdpDevice device;
    VdpDecoderProfile profile;
    AVVDPAUContext context;
    uint16_t width;
    uint16_t height;
};

63
static int Lock(vlc_va_t *va, void **opaque, uint8_t **data)
64
65
{
    vlc_va_sys_t *sys = va->sys;
66
67
    VdpVideoSurface surface;
    VdpStatus err;
68

69
70
71
    err = vdp_video_surface_create(sys->vdp, sys->device, VDP_CHROMA_TYPE_420,
                                   sys->width, sys->height, &surface);
    if (err != VDP_STATUS_OK)
72
    {
73
74
        msg_Err(va, "%s creation failure: %s", "video surface",
                vdp_get_error_string(sys->vdp, err));
75
76
77
        return VLC_EGENERIC;
    }

78
79
80
81
    vlc_vdp_video_field_t *field = vlc_vdp_video_create(sys->vdp, surface);
    if (unlikely(field == NULL))
        return VLC_ENOMEM;

82
83
    *data = (void *)(uintptr_t)surface;
    *opaque = field;
84
85
86
    return VLC_SUCCESS;
}

87
static void Unlock(void *opaque, uint8_t *data)
88
{
89
    vlc_vdp_video_field_t *field = opaque;
90
91
92

    assert(field != NULL);
    field->destroy(field);
93
    (void) data;
94
95
}

96
static int Copy(vlc_va_t *va, picture_t *pic, void *opaque, uint8_t *data)
97
{
98
    vlc_vdp_video_field_t *field = opaque;
99
100
101
102
103

    assert(field != NULL);
    field = vlc_vdp_video_copy(field);
    if (unlikely(field == NULL))
        return VLC_ENOMEM;
104

105
106
    assert(pic->context == NULL);
    pic->context = field;
107
    (void) va; (void) data;
108
    return VLC_SUCCESS;
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
}

static int Init(vlc_va_t *va, void **ctxp, vlc_fourcc_t *chromap,
                int width, int height)
{
    vlc_va_sys_t *sys = va->sys;
    VdpStatus err;

    width = (width + 1) & ~1;
    height = (height + 3) & ~3;
    sys->width = width;
    sys->height = height;

    unsigned surfaces = 2;
    switch (sys->profile)
    {
      case VDP_DECODER_PROFILE_H264_BASELINE:
      case VDP_DECODER_PROFILE_H264_MAIN:
      case VDP_DECODER_PROFILE_H264_HIGH:
        surfaces = 16;
        break;
    }

    err = vdp_decoder_create(sys->vdp, sys->device, sys->profile, width,
                             height, surfaces, &sys->context.decoder);
    if (err != VDP_STATUS_OK)
    {
        msg_Err(va, "%s creation failure: %s", "decoder",
                vdp_get_error_string(sys->vdp, err));
        sys->context.decoder = VDP_INVALID_HANDLE;
        return VLC_EGENERIC;
    }

    *ctxp = &sys->context;
143
144
    /* TODO: select better chromas when appropriate */
    *chromap = VLC_CODEC_VDPAU_VIDEO_420;
145
146
147
148
149
150
151
152
153
    return VLC_SUCCESS;
}

static void Deinit(vlc_va_t *va)
{
    vlc_va_sys_t *sys = va->sys;

    assert(sys->context.decoder != VDP_INVALID_HANDLE);
    vdp_decoder_destroy(sys->vdp, sys->context.decoder);
154
#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 13, 0))
155
    av_freep(&sys->context.bitstream_buffers);
156
#endif
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
}

static int Setup(vlc_va_t *va, void **ctxp, vlc_fourcc_t *chromap,
                 int width, int height)
{
    vlc_va_sys_t *sys = va->sys;

    if (sys->context.decoder != VDP_INVALID_HANDLE)
    {
        if (sys->width == width && sys->height == height)
            return VLC_SUCCESS;
        Deinit(va);
        sys->context.decoder = VDP_INVALID_HANDLE;
    }

    return Init(va, ctxp, chromap, width, height);
}

static int Open(vlc_va_t *va, int codec, const es_format_t *fmt)
{
    VdpStatus err;
    VdpDecoderProfile profile;
    int level;

    switch (codec)
    {
      case AV_CODEC_ID_MPEG1VIDEO:
        profile = VDP_DECODER_PROFILE_MPEG1;
        level = VDP_DECODER_LEVEL_MPEG1_NA;
        break;

      case AV_CODEC_ID_MPEG2VIDEO:
        switch (fmt->i_profile)
        {
          case FF_PROFILE_MPEG2_MAIN:
            profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
            break;
          case FF_PROFILE_MPEG2_SIMPLE:
            profile = VDP_DECODER_PROFILE_MPEG2_SIMPLE;
            break;
          default:
            msg_Err(va, "unsupported %s profile %d", "MPEG2", fmt->i_profile);
            return VLC_EGENERIC;
        }
        level = VDP_DECODER_LEVEL_MPEG2_HL;
        break;

      case AV_CODEC_ID_H263:
        profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
        level = VDP_DECODER_LEVEL_MPEG4_PART2_ASP_L5;
        break;
      case AV_CODEC_ID_MPEG4:
        switch (fmt->i_profile)
        {
          case FF_PROFILE_MPEG4_SIMPLE:
            profile = VDP_DECODER_PROFILE_MPEG4_PART2_SP;
            break;
214
          case FF_PROFILE_MPEG4_ADVANCED_SIMPLE:
215
216
            profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
            break;
217
218
219
          default:
            msg_Err(va, "unsupported %s profile %d", "MPEG4", fmt->i_profile);
            return VLC_EGENERIC;
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
        }
        level = fmt->i_level;
        break;

      case AV_CODEC_ID_H264:
        switch (fmt->i_profile
                        & ~(FF_PROFILE_H264_CONSTRAINED|FF_PROFILE_H264_INTRA))
        {
          case FF_PROFILE_H264_BASELINE:
            profile = VDP_DECODER_PROFILE_H264_BASELINE;
            break;
          case FF_PROFILE_H264_MAIN:
            profile = VDP_DECODER_PROFILE_H264_MAIN;
            break;
          case FF_PROFILE_H264_HIGH:
            profile = VDP_DECODER_PROFILE_H264_HIGH;
            break;
          case FF_PROFILE_H264_EXTENDED:
          default:
            msg_Err(va, "unsupported %s profile %d", "H.264", fmt->i_profile);
            return VLC_EGENERIC;
        }
        level = fmt->i_level;
        if ((fmt->i_profile & FF_PROFILE_H264_INTRA) && (fmt->i_level == 11))
            level = VDP_DECODER_LEVEL_H264_1b;
        break;

      case AV_CODEC_ID_WMV3:
      case AV_CODEC_ID_VC1:
        switch (fmt->i_profile)
        {
          case FF_PROFILE_VC1_SIMPLE:
            profile = VDP_DECODER_PROFILE_VC1_SIMPLE;
            break;
          case FF_PROFILE_VC1_MAIN:
            profile = VDP_DECODER_PROFILE_VC1_MAIN;
            break;
          case FF_PROFILE_VC1_ADVANCED:
            profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
            break;
          default:
            msg_Err(va, "unsupported %s profile %d", "VC-1", fmt->i_profile);
            return VLC_EGENERIC;
        }
        level = fmt->i_level;
        break;

      default:
        msg_Err(va, "unknown codec %d", codec);
        return VLC_EGENERIC;
    }

    if (!vlc_xlib_init(VLC_OBJECT(va)))
    {
        msg_Err(va, "Xlib is required for VDPAU");
        return VLC_EGENERIC;
    }

    vlc_va_sys_t *sys = malloc(sizeof (*sys));
    if (unlikely(sys == NULL))
       return VLC_ENOMEM;

    sys->profile = profile;
    memset(&sys->context, 0, sizeof (sys->context));
    sys->context.decoder = VDP_INVALID_HANDLE;

    err = vdp_get_x11(NULL, -1, &sys->vdp, &sys->device);
    if (err != VDP_STATUS_OK)
    {
        free(sys);
        return VLC_EGENERIC;
    }

    void *func;
    err = vdp_get_proc_address(sys->vdp, sys->device,
                               VDP_FUNC_ID_DECODER_RENDER, &func);
    if (err != VDP_STATUS_OK)
        goto error;
    sys->context.render = func;

    /* Check capabilities */
    VdpBool support;
302
    uint32_t l, mb, w, h;
303
304

    if (vdp_video_surface_query_capabilities(sys->vdp, sys->device,
305
              VDP_CHROMA_TYPE_420, &support, &w, &h) != VDP_STATUS_OK)
306
        support = VDP_FALSE;
307
    if (!support)
308
    {
309
310
311
312
313
314
315
316
        msg_Err(va, "video surface format not supported: %s", "YUV 4:2:0");
        goto error;
    }
    msg_Dbg(va, "video surface limits: %"PRIu32"x%"PRIu32, w, h);
    if (w < fmt->video.i_width || h < fmt->video.i_height)
    {
        msg_Err(va, "video surface above limits: %ux%u",
                fmt->video.i_width, fmt->video.i_height);
317
318
319
320
        goto error;
    }

    if (vdp_decoder_query_capabilities(sys->vdp, sys->device, profile,
321
                                   &support, &l, &mb, &w, &h) != VDP_STATUS_OK)
322
        support = VDP_FALSE;
323
324
325
326
327
328
329
330
    if (!support)
    {
        msg_Err(va, "decoder profile not supported: %u", profile);
        goto error;
    }
    msg_Dbg(va, "decoder profile limits: level %"PRIu32" mb %"PRIu32" "
            "%"PRIu32"x%"PRIu32, l, mb, w, h);
    if ((int)l < level || w < fmt->video.i_width || h < fmt->video.i_height)
331
    {
332
333
        msg_Err(va, "decoder profile above limits: level %d %ux%u",
                level, fmt->video.i_width, fmt->video.i_height);
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
        goto error;
    }

    const char *infos;
    if (vdp_get_information_string(sys->vdp, &infos) != VDP_STATUS_OK)
        infos = "VDPAU";

    va->sys = sys;
    va->description = (char *)infos;
    va->pix_fmt = AV_PIX_FMT_VDPAU;
    va->setup = Setup;
    va->get = Lock;
    va->release = Unlock;
    va->extract = Copy;
    return VLC_SUCCESS;

error:
    vdp_release_x11(sys->vdp);
    free(sys);
    return VLC_EGENERIC;
}

static void Close(vlc_va_t *va)
{
    vlc_va_sys_t *sys = va->sys;

    if (sys->context.decoder != VDP_INVALID_HANDLE)
        Deinit(va);
    vdp_release_x11(sys->vdp);
    free(sys);
}