]> git.sesse.net Git - vlc/blob - modules/codec/invmem.c
Adding a invmem-chroma option
[vlc] / modules / codec / invmem.c
1 /*****************************************************************************
2  * invmem_decoder.c: memory video driver for vlc
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Robert Paciorek <robert@opcode.eu.org> <http://opcode.eu.org/bercik>
8  * based on:
9  *  - vmem video output module (Gildas Bazin <gbazin@videolan.org>)
10  *  - png video decodec module (Sam Hocevar <sam@zoy.org>)
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_codec.h>
38
39 #include <vlc_image.h>
40 #include <vlc_filter.h>
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  OpenDecoder   ( vlc_object_t * );
46 static void CloseDecoder  ( vlc_object_t * );
47
48 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 #define T_WIDTH N_( "Width" )
54 #define LT_WIDTH N_( "Video memory buffer width." )
55
56 #define T_HEIGHT N_( "Height" )
57 #define LT_HEIGHT N_( "Video memory buffer height." )
58
59 #define T_LOCK N_( "Lock function" )
60 #define LT_LOCK N_( "Address of the locking callback function. This " \
61                     "function must return a valid memory address for use " \
62                     "by the video renderer." )
63
64 #define T_UNLOCK N_( "Unlock function" )
65 #define LT_UNLOCK N_( "Address of the unlocking callback function" )
66
67 #define T_DATA N_( "Callback data" )
68 #define LT_DATA N_( "Data for the locking and unlocking functions" )
69
70 #define T_CHROMA N_("Chroma")
71 #define LT_CHROMA N_("Output chroma for the memory image as a 4-character " \
72                       "string, eg. \"RV32\".")
73
74 #define INVMEM_HELP N_( "This module make possible making video stream from raw-image " \
75                         "generating (to memory) from rendering program uses libvlc. " \
76                         "To use this module from libvlc set --codec to invmem, "\
77                         "set all --invmem-* options in vlc_argv an use " \
78                         "libvlc_media_new(libvlc, \"fake://\", &ex);. " \
79                         "Besides is simillar to vmem video output module." )
80
81 vlc_module_begin()
82     set_category( CAT_INPUT )
83     set_subcategory( SUBCAT_INPUT_VCODEC )
84     set_shortname( N_("Memory video decoder") )
85     set_description( N_("Memory video decoder") )
86     set_help( INVMEM_HELP )
87     set_capability( "decoder", 50 )
88     set_callbacks( OpenDecoder, CloseDecoder )
89     add_shortcut( "invmem" )
90
91     add_integer( "invmem-width", 0, NULL, T_WIDTH, LT_WIDTH, false )
92     add_integer( "invmem-height", 0, NULL, T_HEIGHT, LT_HEIGHT, false )
93     add_string( "invmem-lock", "0", NULL, T_LOCK, LT_LOCK, true )
94     add_string( "invmem-unlock", "0", NULL, T_UNLOCK, LT_UNLOCK, true )
95     add_string( "invmem-data", "0", NULL, T_DATA, LT_DATA, true )
96     add_string( "invmem-chroma", "RV24", NULL, T_CHROMA, LT_CHROMA, true)
97 vlc_module_end()
98
99
100 struct decoder_sys_t
101 {
102     void * (*pf_lock) (void *);
103     void (*pf_unlock) (void *);
104     void *p_data;
105
106     int i_width;
107     int i_height;
108     int i_pitch;
109
110     vlc_fourcc_t i_chroma;
111
112     picture_t *p_pic;
113 };
114
115
116 /*****************************************************************************
117  * OpenDecoder: probe the decoder and return score
118  *****************************************************************************/
119 static int OpenDecoder( vlc_object_t *p_this )
120 {
121     decoder_t *p_dec = (decoder_t*)p_this;
122     decoder_sys_t *p_sys;
123     char *psz_tmp;
124     int pitch;
125
126     if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','a','k','e'))
127     {
128         return VLC_EGENERIC;
129     }
130
131     /* Allocate the memory needed to store the decoder's structure */
132     if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
133         return VLC_ENOMEM;
134
135     // get parameters
136     char* chromaStr = var_CreateGetString( p_dec, "invmem-chroma" );
137     p_sys->i_width = var_CreateGetInteger( p_this, "invmem-width" );
138     p_sys->i_height = var_CreateGetInteger( p_this, "invmem-height" );
139     if( p_sys->i_width == 0 || p_sys->i_height == 0 )
140     {
141         msg_Err( p_dec, "--invmem-width and --invmem-height must be > 0" );
142         goto error;
143     }
144
145     psz_tmp = var_CreateGetString( p_dec, "invmem-lock" );
146     p_sys->pf_lock = (void * (*) (void *))(intptr_t)atoll( psz_tmp );
147     free( psz_tmp );
148
149     psz_tmp = var_CreateGetString( p_dec, "invmem-unlock" );
150     p_sys->pf_unlock = (void (*) (void *))(intptr_t)atoll( psz_tmp );
151     free( psz_tmp );
152
153     psz_tmp = var_CreateGetString( p_dec, "invmem-data" );
154     p_sys->p_data = (void *)(intptr_t)atoll( psz_tmp );
155     free( psz_tmp );
156
157     if( !p_sys->pf_lock || !p_sys->pf_unlock )
158     {
159         msg_Err( p_dec, "Invalid lock or unlock callbacks" );
160         goto error;
161     }
162
163     if ( chromaStr == NULL )
164     {
165         msg_Err( p_dec, "Invalid invmem-chroma string." );
166         goto error;
167     }
168     const vlc_fourcc_t chroma = vlc_fourcc_GetCodecFromString( VIDEO_ES, chromaStr );
169
170     if ( !chroma )
171     {
172         msg_Err( p_dec, "invmem-chroma should be 4 characters long." );
173         goto error;
174     }
175
176     /* Set output properties */
177     switch (chroma)
178     {
179     case VLC_CODEC_RGB15:
180         p_dec->fmt_out.video.i_rmask = 0x001f;
181         p_dec->fmt_out.video.i_gmask = 0x03e0;
182         p_dec->fmt_out.video.i_bmask = 0x7c00;
183         pitch = p_sys->i_width * 2;
184         break;
185     case VLC_CODEC_RGB16:
186         p_dec->fmt_out.video.i_rmask = 0x001f;
187         p_dec->fmt_out.video.i_gmask = 0x07e0;
188         p_dec->fmt_out.video.i_bmask = 0xf800;
189         pitch = p_sys->i_width * 2;
190         break;
191     case VLC_CODEC_RGB24:
192         p_dec->fmt_out.video.i_rmask = 0xff0000;
193         p_dec->fmt_out.video.i_gmask = 0x00ff00;
194         p_dec->fmt_out.video.i_bmask = 0x0000ff;
195         pitch = p_sys->i_width * 3;
196         break;
197     case VLC_CODEC_RGB32:
198         p_dec->fmt_out.video.i_rmask = 0xff0000;
199         p_dec->fmt_out.video.i_gmask = 0x00ff00;
200         p_dec->fmt_out.video.i_bmask = 0x0000ff;
201         pitch = p_sys->i_width * 4;
202         break;
203     default:
204         p_dec->fmt_out.video.i_rmask = 0;
205         p_dec->fmt_out.video.i_gmask = 0;
206         p_dec->fmt_out.video.i_bmask = 0;
207         pitch = 0;
208         msg_Warn( p_dec, "Unknown chroma %s", chromaStr );
209         goto error;
210     }
211
212     free( chromaStr );
213
214     p_dec->fmt_out.i_codec = chroma;
215     p_dec->fmt_out.video.i_width = p_dec->p_sys->i_width;
216     p_dec->fmt_out.video.i_height = p_dec->p_sys->i_height;
217     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->p_sys->i_width / p_dec->p_sys->i_height;
218     p_dec->fmt_out.i_cat = VIDEO_ES;
219
220     p_sys->i_pitch = pitch;
221
222     p_sys->p_pic = NULL;
223
224     /* Set callbacks */
225     p_dec->pf_decode_video = DecodeBlock;
226
227     return VLC_SUCCESS;
228 error:
229     free( p_sys );
230     free( chromaStr );
231     return VLC_EGENERIC;
232 }
233
234 /****************************************************************************
235  * DecodeBlock: the whole thing
236  ****************************************************************************
237  * This function must be fed with a complete compressed frame.
238  ****************************************************************************/
239 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
240 {
241     decoder_sys_t *p_sys = p_dec->p_sys;
242     block_t *p_block;
243
244     if( !pp_block || !*pp_block ) return NULL;
245
246     p_block = *pp_block;
247
248     // create new picture
249     if( p_sys->p_pic != NULL )
250         picture_Release( p_sys->p_pic );
251     p_sys->p_pic = decoder_NewPicture( p_dec );
252     if ( !p_sys->p_pic ) return NULL;
253     p_sys->p_pic->b_force = true;
254     p_sys->p_pic->p->i_pitch = p_dec->p_sys->i_pitch;
255     p_sys->p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
256
257     // lock input and copy to picture
258     p_sys->p_pic->p->p_pixels = p_sys->pf_lock( p_dec->p_sys->p_data );
259
260     // unlock input
261     p_sys->pf_unlock( p_dec->p_sys->p_data );
262
263     block_Release( *pp_block ); *pp_block = NULL;
264     return p_sys->p_pic;
265 }
266
267 /*****************************************************************************
268  * CloseDecoder: invmem decoder destruction
269  *****************************************************************************/
270 static void CloseDecoder( vlc_object_t *p_this )
271 {
272     decoder_t *p_dec = (decoder_t *)p_this;
273     decoder_sys_t *p_sys = p_dec->p_sys;
274
275     if( p_sys->p_pic != NULL )
276         picture_Release( p_sys->p_pic );
277
278     free( p_sys );
279 }