]> git.sesse.net Git - vlc/blob - modules/codec/fake.c
Potential memleak.
[vlc] / modules / codec / fake.c
1 /*****************************************************************************
2  * fake.c: decoder reading from a fake stream, outputting a fixed image
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Jean-Paul Saman <jpsaman at m2x dot nl>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35
36 #include <vlc_image.h>
37 #include <vlc_filter.h>
38 #include <vlc_charset.h>
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  OpenDecoder   ( vlc_object_t * );
44 static void CloseDecoder  ( vlc_object_t * );
45
46 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
47 static int FakeCallback( vlc_object_t *, char const *,
48                          vlc_value_t, vlc_value_t, void * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 #define FILE_TEXT N_("Image file")
54 #define FILE_LONGTEXT N_( \
55     "Path of the image file for fake input." )
56 #define RELOAD_TEXT N_("Reload image file")
57 #define RELOAD_LONGTEXT N_( \
58     "Reload image file every n seconds." )
59 #define WIDTH_TEXT N_("Video width")
60 #define WIDTH_LONGTEXT N_( \
61     "Output video width." )
62 #define HEIGHT_TEXT N_("Video height")
63 #define HEIGHT_LONGTEXT N_( \
64     "Output video height." )
65 #define KEEP_AR_TEXT N_("Keep aspect ratio")
66 #define KEEP_AR_LONGTEXT N_( \
67     "Consider width and height as maximum values." )
68 #define ASPECT_RATIO_TEXT N_("Background aspect ratio")
69 #define ASPECT_RATIO_LONGTEXT N_( \
70     "Aspect ratio of the image file (4:3, 16:9). Default is square pixels." )
71 #define DEINTERLACE_TEXT N_("Deinterlace video")
72 #define DEINTERLACE_LONGTEXT N_( \
73     "Deinterlace the image after loading it." )
74 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
75 #define DEINTERLACE_MODULE_LONGTEXT N_( \
76     "Deinterlace module to use." )
77 #define CHROMA_TEXT N_("Chroma used.")
78 #define CHROMA_LONGTEXT N_( \
79     "Force use of a specific chroma for output. Default is I420." )
80
81 static const char *const ppsz_deinterlace_type[] =
82 {
83     "deinterlace", "ffmpeg-deinterlace"
84 };
85
86 vlc_module_begin();
87     set_category( CAT_INPUT );
88     set_subcategory( SUBCAT_INPUT_VCODEC );
89     set_shortname( N_("Fake") );
90     set_description( N_("Fake video decoder") );
91     set_capability( "decoder", 1000 );
92     set_callbacks( OpenDecoder, CloseDecoder );
93     add_shortcut( "fake" );
94
95     add_file( "fake-file", "", NULL, FILE_TEXT,
96                 FILE_LONGTEXT, false );
97     add_integer( "fake-file-reload", 0, NULL, RELOAD_TEXT,
98                 RELOAD_LONGTEXT, false );
99     add_integer( "fake-width", 0, NULL, WIDTH_TEXT,
100                  WIDTH_LONGTEXT, true );
101     add_integer( "fake-height", 0, NULL, HEIGHT_TEXT,
102                  HEIGHT_LONGTEXT, true );
103     add_bool( "fake-keep-ar", 0, NULL, KEEP_AR_TEXT, KEEP_AR_LONGTEXT,
104               true );
105     add_string( "fake-aspect-ratio", "", NULL,
106                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true );
107     add_bool( "fake-deinterlace", 0, NULL, DEINTERLACE_TEXT,
108               DEINTERLACE_LONGTEXT, false );
109     add_string( "fake-deinterlace-module", "deinterlace", NULL,
110                 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
111                 false );
112         change_string_list( ppsz_deinterlace_type, 0, 0 );
113     add_string( "fake-chroma", "I420", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
114                 true );
115 vlc_module_end();
116
117 struct decoder_sys_t
118 {
119     picture_t *p_image;
120     vlc_mutex_t lock;
121
122     bool  b_reload;
123     mtime_t     i_reload;
124     mtime_t     i_next;
125 };
126
127 /*****************************************************************************
128  * OpenDecoder: probe the decoder and return score
129  *****************************************************************************/
130 static int OpenDecoder( vlc_object_t *p_this )
131 {
132     decoder_t *p_dec = (decoder_t*)p_this;
133     vlc_value_t val;
134     image_handler_t *p_handler;
135     video_format_t fmt_in, fmt_out;
136     picture_t *p_image;
137     char *psz_file, *psz_chroma;
138     bool b_keep_ar;
139     int i_aspect = 0;
140
141     if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','a','k','e') )
142     {
143         return VLC_EGENERIC;
144     }
145
146     p_dec->p_sys = (decoder_sys_t *)malloc( sizeof( decoder_sys_t ) );
147     if( !p_dec->p_sys )
148     {
149         return VLC_ENOMEM;
150     }
151     memset( p_dec->p_sys, 0, sizeof( decoder_sys_t ) );
152
153     psz_file = var_CreateGetNonEmptyStringCommand( p_dec, "fake-file" );
154     if( !psz_file )
155     {
156         msg_Err( p_dec, "specify a file with --fake-file=..." );
157         free( p_dec->p_sys );
158         return VLC_EGENERIC;
159     }
160     var_AddCallback( p_dec, "fake-file", FakeCallback, p_dec );
161
162     memset( &fmt_in, 0, sizeof(fmt_in) );
163     memset( &fmt_out, 0, sizeof(fmt_out) );
164
165     val.i_int = var_CreateGetIntegerCommand( p_dec, "fake-file-reload" );
166     if( val.i_int > 0)
167     {
168         p_dec->p_sys->b_reload = true;
169         p_dec->p_sys->i_reload = (mtime_t)(val.i_int * 1000000);
170         p_dec->p_sys->i_next   = (mtime_t)(p_dec->p_sys->i_reload + mdate());
171     }
172
173     psz_chroma = var_CreateGetString( p_dec, "fake-chroma" );
174     if( strlen( psz_chroma ) != 4 )
175     {
176         msg_Warn( p_dec, "Invalid chroma (%s). Using I420.", psz_chroma );
177         fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
178     }
179     else
180     {
181         fmt_out.i_chroma = VLC_FOURCC( psz_chroma[0],
182                                        psz_chroma[1],
183                                        psz_chroma[2],
184                                        psz_chroma[3] );
185     }
186     free( psz_chroma );
187
188     var_Create( p_dec, "fake-keep-ar", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
189     var_Get( p_dec, "fake-keep-ar", &val );
190     b_keep_ar = val.b_bool;
191
192     var_Create( p_dec, "fake-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
193     var_Create( p_dec, "fake-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
194     var_Create( p_dec, "fake-aspect-ratio",
195                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
196
197     var_Get( p_dec, "fake-aspect-ratio", &val );
198     if ( val.psz_string )
199     {
200         char *psz_parser = strchr( val.psz_string, ':' );
201
202         if( psz_parser )
203         {
204             *psz_parser++ = '\0';
205             i_aspect = atoi( val.psz_string )
206                                    * VOUT_ASPECT_FACTOR / atoi( psz_parser );
207         }
208         free( val.psz_string );
209     }
210
211     if ( !b_keep_ar )
212     {
213         var_Get( p_dec, "fake-width", &val );
214         fmt_out.i_width = val.i_int;
215         var_Get( p_dec, "fake-height", &val );
216         fmt_out.i_height = val.i_int;
217     }
218
219     p_handler = image_HandlerCreate( p_dec );
220     p_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
221     image_HandlerDelete( p_handler );
222
223     if ( p_image == NULL )
224     {
225         msg_Err( p_dec, "unable to read image file %s", psz_file );
226         free( psz_file );
227         free( p_dec->p_sys );
228         return VLC_EGENERIC;
229     }
230     msg_Dbg( p_dec, "file %s loaded successfully", psz_file );
231
232     free( psz_file );
233
234     if ( b_keep_ar )
235     {
236         picture_t *p_old = p_image;
237         int i_width, i_height;
238
239         var_Get( p_dec, "fake-width", &val );
240         i_width = val.i_int;
241         var_Get( p_dec, "fake-height", &val );
242         i_height = val.i_int;
243
244         if ( i_width && i_height )
245         {
246             int i_image_ar = fmt_out.i_width * VOUT_ASPECT_FACTOR
247                               / fmt_out.i_height;
248             int i_region_ar = i_width * VOUT_ASPECT_FACTOR / i_height;
249             fmt_in = fmt_out;
250
251             if ( i_aspect == i_image_ar )
252             {
253                 fmt_out.i_width = i_width;
254                 fmt_out.i_height = i_height;
255             }
256             else if ( i_image_ar > i_region_ar )
257             {
258                 fmt_out.i_width = i_width;
259                 fmt_out.i_height = i_width * VOUT_ASPECT_FACTOR
260                                     / i_image_ar;
261                 i_aspect = i_image_ar;
262             }
263             else
264             {
265                 fmt_out.i_height = i_height;
266                 fmt_out.i_width = i_height * i_image_ar
267                                     / VOUT_ASPECT_FACTOR;
268                 i_aspect = i_image_ar;
269             }
270
271             p_handler = image_HandlerCreate( p_dec );
272             p_image = image_Convert( p_handler, p_old, &fmt_in, &fmt_out );
273             image_HandlerDelete( p_handler );
274
275             if ( p_image == NULL )
276             {
277                 msg_Warn( p_dec, "couldn't load resizing module" );
278                 p_image = p_old;
279                 fmt_out = fmt_in;
280             }
281             else
282             {
283                 picture_Release( p_old );
284             }
285         }
286     }
287
288     if ( i_aspect )
289     {
290         fmt_out.i_aspect = i_aspect;
291     }
292     else
293     {
294         fmt_out.i_aspect = fmt_out.i_width
295                             * VOUT_ASPECT_FACTOR / fmt_out.i_height;
296     }
297
298     var_Create( p_dec, "fake-deinterlace", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
299     var_Get( p_dec, "fake-deinterlace", &val );
300     if ( val.b_bool )
301     {
302         picture_t *p_old = p_image;
303
304         var_Create( p_dec, "fake-deinterlace-module",
305                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
306         var_Get( p_dec, "fake-deinterlace-module", &val );
307
308         p_handler = image_HandlerCreate( p_dec );
309         p_image = image_Filter( p_handler, p_old, &fmt_out, val.psz_string );
310         image_HandlerDelete( p_handler );
311         free( val.psz_string );
312
313         if ( p_image == NULL )
314         {
315             msg_Warn( p_dec, "couldn't load deinterlace module" );
316             p_image = p_old;
317         }
318         else
319         {
320             picture_Release( p_old );
321         }
322     }
323
324     /* Set output properties */
325     p_dec->fmt_out.i_cat = VIDEO_ES;
326     p_dec->fmt_out.i_codec = fmt_out.i_chroma;
327     p_dec->fmt_out.video = fmt_out;
328
329     /* Set callbacks */
330     p_dec->pf_decode_video = DecodeBlock;
331
332     p_dec->p_sys->p_image = p_image;
333     vlc_mutex_init( &p_dec->p_sys->lock );
334
335     return VLC_SUCCESS;
336 }
337
338 /****************************************************************************
339  * DecodeBlock: the whole thing
340  ****************************************************************************/
341 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
342 {
343     decoder_sys_t *p_sys = (decoder_sys_t*) p_dec->p_sys;
344     picture_t *p_pic;
345
346     if( pp_block == NULL || !*pp_block ) return NULL;
347     p_pic = p_dec->pf_vout_buffer_new( p_dec );
348     if( p_pic == NULL )
349     {
350         msg_Err( p_dec, "cannot get picture" );
351         goto error;
352     }
353
354     if( p_sys->b_reload && (mdate() >= p_sys->i_next) )
355     {
356         var_TriggerCallback( p_dec, "fake-file" );
357         /* next period */
358         p_sys->i_next = (mtime_t)(p_sys->i_reload + mdate());
359     }
360     vlc_mutex_lock( &p_dec->p_sys->lock );
361     vout_CopyPicture( p_dec, p_pic, p_dec->p_sys->p_image );
362     vlc_mutex_unlock( &p_dec->p_sys->lock );
363
364     p_pic->date = (*pp_block)->i_pts;
365
366 error:
367     block_Release( *pp_block );
368     *pp_block = NULL;
369
370     return p_pic;
371 }
372
373 /*****************************************************************************
374  * CloseDecoder: fake decoder destruction
375  *****************************************************************************/
376 static void CloseDecoder( vlc_object_t *p_this )
377 {
378     decoder_t *p_dec = (decoder_t *)p_this;
379     picture_t *p_image = p_dec->p_sys->p_image;
380
381     if( p_image != NULL )
382         picture_Release( p_image );
383
384     vlc_mutex_destroy( &p_dec->p_sys->lock );
385     free( p_dec->p_sys );
386 }
387
388 /*****************************************************************************
389  * FakeCallback: Image source change callback.
390  *****************************************************************************/
391 static int FakeCallback( vlc_object_t *p_this, char const *psz_var,
392                          vlc_value_t oldval, vlc_value_t newval,
393                          void *p_data )
394 {
395     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
396     decoder_t *p_dec = (decoder_t *)p_data;
397
398     if( !strcmp( psz_var, "fake-file" ) )
399     {
400         image_handler_t *p_handler;
401         picture_t *p_new_image;
402         video_format_t fmt_in, fmt_out;
403         char *psz_file = newval.psz_string;
404         picture_t *p_image;
405
406         vlc_mutex_lock( &p_dec->p_sys->lock );
407         p_image = p_dec->p_sys->p_image;
408
409         if( !psz_file || !*psz_file )
410         {
411             msg_Err( p_dec, "fake-file value must be non empty." );
412             vlc_mutex_unlock( &p_dec->p_sys->lock );
413             return VLC_EGENERIC;
414         }
415         msg_Dbg( p_dec, "Changing fake-file to %s.", psz_file );
416
417         memset( &fmt_in, 0, sizeof(fmt_in) );
418         fmt_out = p_dec->fmt_out.video;
419         p_handler = image_HandlerCreate( p_dec );
420         p_new_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
421         image_HandlerDelete( p_handler );
422
423         if( !p_new_image )
424         {
425             msg_Err( p_dec, "error while reading image (%s)", psz_file );
426             vlc_mutex_unlock( &p_dec->p_sys->lock );
427             return VLC_EGENERIC;
428         }
429
430         p_dec->p_sys->p_image = p_new_image;
431         picture_Release( p_image );
432         vlc_mutex_unlock( &p_dec->p_sys->lock );
433     }
434     else if( !strcmp( psz_var, "fake-file-reload" ) )
435     {
436         if( newval.i_int > 0)
437         {
438             p_dec->p_sys->b_reload = true;
439             p_dec->p_sys->i_reload = (mtime_t)(newval.i_int * 1000000);
440             p_dec->p_sys->i_next   = (mtime_t)(p_dec->p_sys->i_reload + mdate());
441         }
442         else
443         {
444             p_dec->p_sys->b_reload = false;
445         }
446     }
447
448     return VLC_SUCCESS;
449 }