]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/hwdummy.c
112087db9cafdea7eba264ca998386ee297b4500
[vlc] / modules / codec / avcodec / hwdummy.c
1 /*****************************************************************************
2  * hwdummy.c: dummy hardware decoding acceleration plugin for VLC/libav
3  *****************************************************************************
4  * Copyright (C) 2013 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include <assert.h>
28
29 #include <libavutil/mem.h>
30 #include <libavcodec/avcodec.h>
31 #include <libavcodec/vdpau.h>
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_fourcc.h>
35 #include <vlc_picture.h>
36 #include "../../codec/avcodec/va.h"
37
38 static int Open(vlc_va_t *, AVCodecContext *, const es_format_t *);
39 static void Close(vlc_va_t *);
40
41 vlc_module_begin()
42     set_description(N_("Dummy video decoding accelerator"))
43     set_capability("hw decoder", 0)
44     set_category(CAT_INPUT)
45     set_subcategory(SUBCAT_INPUT_VCODEC)
46     set_callbacks(Open, Close)
47     add_shortcut("dummy")
48 vlc_module_end()
49
50 #define DECODER_MAGIC 0xdec0dea0
51 #define DATA_MAGIC    0xda1a0000
52 #define OPAQUE_MAGIC  0x0da00e00
53
54 static int Lock(vlc_va_t *va, void **opaque, uint8_t **data)
55 {
56     *data = (void *)(uintptr_t)DATA_MAGIC;
57     *opaque = (void *)(uintptr_t)OPAQUE_MAGIC;
58     (void) va;
59     return VLC_SUCCESS;
60 }
61
62 static void Unlock(void *opaque, uint8_t *data)
63 {
64     assert((uintptr_t)opaque == OPAQUE_MAGIC);
65     assert((uintptr_t)data == DATA_MAGIC);
66 }
67
68 static VdpStatus Render(VdpDecoder decoder, VdpVideoSurface target,
69                         VdpPictureInfo const *picture_info,
70                         uint32_t bitstream_buffer_count,
71                         VdpBitstreamBuffer const *bitstream_buffers)
72 {
73     (void) decoder; (void) target; (void) picture_info;
74     (void) bitstream_buffer_count; (void) bitstream_buffers;
75     assert(decoder == DECODER_MAGIC);
76     assert(target == DATA_MAGIC);
77     return VDP_STATUS_OK;
78 }
79
80 static int Copy(vlc_va_t *va, picture_t *pic, void *opaque, uint8_t *data)
81 {
82     (void) va;
83
84     assert((uintptr_t)opaque == OPAQUE_MAGIC);
85     assert((uintptr_t)data == DATA_MAGIC);
86
87     /* Put some dummy picture content */
88     memset(pic->p[0].p_pixels, 0xF0,
89            pic->p[0].i_pitch * pic->p[0].i_visible_lines);
90     for (int i = 0; i < pic->p[1].i_visible_lines; i++)
91         memset(pic->p[1].p_pixels + (i * pic->p[1].i_pitch), i,
92                pic->p[1].i_visible_pitch);
93     for (int i = 0; i < pic->p[2].i_visible_lines; i++)
94         for (int j = 0; j < pic->p[2].i_visible_pitch; j++)
95             pic->p[2].p_pixels[(i * pic->p[2].i_pitch) + j] = j;
96     return VLC_SUCCESS;
97 }
98
99 static int Setup(vlc_va_t *va, void **ctxp, vlc_fourcc_t *chromap,
100                  int width, int height)
101 {
102     (void) width; (void) height;
103     *ctxp = (AVVDPAUContext *)va->sys;
104     *chromap = VLC_CODEC_YV12;
105     return VLC_SUCCESS;
106 }
107
108 static int Open(vlc_va_t *va, AVCodecContext *ctx, const es_format_t *fmt)
109 {
110     union
111     {
112         char str[4];
113         vlc_fourcc_t fourcc;
114     } u = { .fourcc = fmt->i_codec };
115
116     AVVDPAUContext *hwctx = av_vdpau_alloc_context();
117     if (unlikely(hwctx == NULL))
118        return VLC_ENOMEM;
119
120     msg_Dbg(va, "codec %d (%4.4s) profile %d level %d", ctx->codec_id, u.str,
121             fmt->i_profile, fmt->i_level);
122
123     hwctx->decoder = DECODER_MAGIC;
124     hwctx->render = Render;
125
126     va->sys = (vlc_va_sys_t *)hwctx;
127     va->description = (char *)"Dummy video decoding accelerator";
128     va->pix_fmt = AV_PIX_FMT_VDPAU;
129     va->setup = Setup;
130     va->get = Lock;
131     va->release = Unlock;
132     va->extract = Copy;
133     return VLC_SUCCESS;
134 }
135
136 static void Close(vlc_va_t *va)
137 {
138     av_free(va->sys);
139 }