]> git.sesse.net Git - vlc/blob - modules/codec/fake.c
47665271717df5e7c534b9705b28346cfe68a16a
[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 #include <vlc/vlc.h>
29 #include <vlc_codec.h>
30
31 #include <vlc_image.h>
32 #include <vlc_filter.h>
33 #include <vlc_charset.h>
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  OpenDecoder   ( vlc_object_t * );
39 static void CloseDecoder  ( vlc_object_t * );
40
41 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
42 static int FakeCallback( vlc_object_t *, char const *,
43                          vlc_value_t, vlc_value_t, void * );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 #define FILE_TEXT N_("Image file")
49 #define FILE_LONGTEXT N_( \
50     "Path of the image file for fake input." )
51 #define RELOAD_TEXT N_("Reload image file")
52 #define RELOAD_LONGTEXT N_( \
53     "Reload image file every n seconds." )
54 #define WIDTH_TEXT N_("Video width")
55 #define WIDTH_LONGTEXT N_( \
56     "Output video width." )
57 #define HEIGHT_TEXT N_("Video height")
58 #define HEIGHT_LONGTEXT N_( \
59     "Output video height." )
60 #define KEEP_AR_TEXT N_("Keep aspect ratio")
61 #define KEEP_AR_LONGTEXT N_( \
62     "Consider width and height as maximum values." )
63 #define ASPECT_RATIO_TEXT N_("Background aspect ratio")
64 #define ASPECT_RATIO_LONGTEXT N_( \
65     "Aspect ratio of the image file (4:3, 16:9). Default is square pixels." )
66 #define DEINTERLACE_TEXT N_("Deinterlace video")
67 #define DEINTERLACE_LONGTEXT N_( \
68     "Deinterlace the image after loading it." )
69 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
70 #define DEINTERLACE_MODULE_LONGTEXT N_( \
71     "Deinterlace module to use." )
72 #define CHROMA_TEXT N_("Chroma used.")
73 #define CHROMA_LONGTEXT N_( \
74     "Force use of a specific chroma for output. Default is I420." )
75
76 static const char *ppsz_deinterlace_type[] =
77 {
78     "deinterlace", "ffmpeg-deinterlace"
79 };
80
81 vlc_module_begin();
82     set_category( CAT_INPUT );
83     set_subcategory( SUBCAT_INPUT_VCODEC );
84     set_shortname( _("Fake") );
85     set_description( _("Fake video decoder") );
86     set_capability( "decoder", 1000 );
87     set_callbacks( OpenDecoder, CloseDecoder );
88     add_shortcut( "fake" );
89
90     add_file( "fake-file", "", NULL, FILE_TEXT,
91                 FILE_LONGTEXT, VLC_FALSE );
92         change_safe();
93     add_integer( "fake-file-reload", 0, NULL, RELOAD_TEXT,
94                 RELOAD_LONGTEXT, VLC_FALSE );
95         change_safe();
96     add_integer( "fake-width", 0, NULL, WIDTH_TEXT,
97                  WIDTH_LONGTEXT, VLC_TRUE );
98         change_safe();
99     add_integer( "fake-height", 0, NULL, HEIGHT_TEXT,
100                  HEIGHT_LONGTEXT, VLC_TRUE );
101         change_safe();
102     add_bool( "fake-keep-ar", 0, NULL, KEEP_AR_TEXT, KEEP_AR_LONGTEXT,
103               VLC_TRUE );
104         change_safe();
105     add_string( "fake-aspect-ratio", "", NULL,
106                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, VLC_TRUE );
107         change_safe();
108     add_bool( "fake-deinterlace", 0, NULL, DEINTERLACE_TEXT,
109               DEINTERLACE_LONGTEXT, VLC_FALSE );
110         change_safe();
111     add_string( "fake-deinterlace-module", "deinterlace", NULL,
112                 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
113                 VLC_FALSE );
114         change_safe();
115         change_string_list( ppsz_deinterlace_type, 0, 0 );
116     add_string( "fake-chroma", "I420", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
117                 VLC_TRUE );
118         change_safe();
119 vlc_module_end();
120
121 struct decoder_sys_t
122 {
123     picture_t *p_image;
124     vlc_mutex_t lock;
125
126     vlc_bool_t  b_reload;
127     mtime_t     i_reload;
128     mtime_t     i_next;
129 };
130
131 /*****************************************************************************
132  * OpenDecoder: probe the decoder and return score
133  *****************************************************************************/
134 static int OpenDecoder( vlc_object_t *p_this )
135 {
136     decoder_t *p_dec = (decoder_t*)p_this;
137     vlc_value_t val;
138     image_handler_t *p_handler;
139     video_format_t fmt_in, fmt_out;
140     picture_t *p_image;
141     char *psz_file, *psz_chroma;
142     vlc_bool_t b_keep_ar;
143     int i_aspect = 0;
144
145     if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','a','k','e') )
146     {
147         return VLC_EGENERIC;
148     }
149
150     p_dec->p_sys = (decoder_sys_t *)malloc( sizeof( decoder_sys_t ) );
151     if( !p_dec->p_sys )
152     {
153         return VLC_ENOMEM;
154     }
155     memset( p_dec->p_sys, 0, sizeof( decoder_sys_t ) );
156
157     psz_file = var_CreateGetNonEmptyStringCommand( p_dec, "fake-file" );
158     if( !psz_file )
159     {
160         msg_Err( p_dec, "specify a file with --fake-file=..." );
161         return VLC_EGENERIC;
162     }
163     var_AddCallback( p_dec, "fake-file", FakeCallback, p_dec );
164
165     memset( &fmt_in, 0, sizeof(fmt_in) );
166     memset( &fmt_out, 0, sizeof(fmt_out) );
167
168     val.i_int = var_CreateGetIntegerCommand( p_dec, "fake-file-reload" );
169     if( val.i_int > 0)
170     {
171         p_dec->p_sys->b_reload = VLC_TRUE;
172         p_dec->p_sys->i_reload = (mtime_t)(val.i_int * 1000000);
173         p_dec->p_sys->i_next   = (mtime_t)(p_dec->p_sys->i_reload + mdate());
174     }
175
176     psz_chroma = var_CreateGetString( p_dec, "fake-chroma" );
177     if( strlen( psz_chroma ) != 4 )
178     {
179         msg_Warn( p_dec, "Invalid chroma (%s). Using I420.", psz_chroma );
180         fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
181     }
182     else
183     {
184         fmt_out.i_chroma = VLC_FOURCC( psz_chroma[0],
185                                        psz_chroma[1],
186                                        psz_chroma[2],
187                                        psz_chroma[3] );
188     }
189     free( psz_chroma );
190
191     var_Create( p_dec, "fake-keep-ar", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
192     var_Get( p_dec, "fake-keep-ar", &val );
193     b_keep_ar = val.b_bool;
194
195     var_Create( p_dec, "fake-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
196     var_Create( p_dec, "fake-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
197     var_Create( p_dec, "fake-aspect-ratio",
198                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
199
200     var_Get( p_dec, "fake-aspect-ratio", &val );
201     if ( val.psz_string )
202     {
203         char *psz_parser = strchr( val.psz_string, ':' );
204
205         if( psz_parser )
206         {
207             *psz_parser++ = '\0';
208             i_aspect = atoi( val.psz_string )
209                                    * VOUT_ASPECT_FACTOR / atoi( psz_parser );
210         }
211         free( val.psz_string );
212     }
213
214     if ( !b_keep_ar )
215     {
216         var_Get( p_dec, "fake-width", &val );
217         fmt_out.i_width = val.i_int;
218         var_Get( p_dec, "fake-height", &val );
219         fmt_out.i_height = val.i_int;
220     }
221
222     p_handler = image_HandlerCreate( p_dec );
223     p_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
224     image_HandlerDelete( p_handler );
225
226     if ( p_image == NULL )
227     {
228         msg_Err( p_dec, "unable to read image file %s", psz_file );
229         return VLC_EGENERIC;
230     }
231     msg_Dbg( p_dec, "file %s loaded successfully", psz_file );
232
233     if ( psz_file ) free( psz_file );
234
235     if ( b_keep_ar )
236     {
237         picture_t *p_old = p_image;
238         int i_width, i_height;
239
240         var_Get( p_dec, "fake-width", &val );
241         i_width = val.i_int;
242         var_Get( p_dec, "fake-height", &val );
243         i_height = val.i_int;
244
245         if ( i_width && i_height )
246         {
247             int i_image_ar = fmt_out.i_width * VOUT_ASPECT_FACTOR
248                               / fmt_out.i_height;
249             int i_region_ar = i_width * VOUT_ASPECT_FACTOR / i_height;
250             fmt_in = fmt_out;
251
252             if ( i_aspect == i_image_ar )
253             {
254                 fmt_out.i_width = i_width;
255                 fmt_out.i_height = i_height;
256             }
257             else if ( i_image_ar > i_region_ar )
258             {
259                 fmt_out.i_width = i_width;
260                 fmt_out.i_height = i_width * VOUT_ASPECT_FACTOR
261                                     / i_image_ar;
262                 i_aspect = i_image_ar;
263             }
264             else
265             {
266                 fmt_out.i_height = i_height;
267                 fmt_out.i_width = i_height * i_image_ar
268                                     / VOUT_ASPECT_FACTOR;
269                 i_aspect = i_image_ar;
270             }
271
272             p_handler = image_HandlerCreate( p_dec );
273             p_image = image_Convert( p_handler, p_old, &fmt_in, &fmt_out );
274             image_HandlerDelete( p_handler );
275
276             if ( p_image == NULL )
277             {
278                 msg_Warn( p_dec, "couldn't load resizing module" );
279                 p_image = p_old;
280                 fmt_out = fmt_in;
281             }
282             else
283             {
284                 p_old->pf_release( p_old );
285             }
286         }
287     }
288
289     if ( i_aspect )
290     {
291         fmt_out.i_aspect = i_aspect;
292     }
293     else
294     {
295         fmt_out.i_aspect = fmt_out.i_width
296                             * VOUT_ASPECT_FACTOR / fmt_out.i_height;
297     }
298
299     var_Create( p_dec, "fake-deinterlace", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
300     var_Get( p_dec, "fake-deinterlace", &val );
301     if ( val.b_bool )
302     {
303         picture_t *p_old = p_image;
304
305         var_Create( p_dec, "fake-deinterlace-module",
306                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
307         var_Get( p_dec, "fake-deinterlace-module", &val );
308
309         p_handler = image_HandlerCreate( p_dec );
310         p_image = image_Filter( p_handler, p_old, &fmt_out, val.psz_string );
311         image_HandlerDelete( p_handler );
312         if ( val.psz_string != NULL ) free( val.psz_string );
313
314         if ( p_image == NULL )
315         {
316             msg_Warn( p_dec, "couldn't load deinterlace module" );
317             p_image = p_old;
318         }
319         else
320         {
321             p_old->pf_release( p_old );
322         }
323     }
324
325     /* Set output properties */
326     p_dec->fmt_out.i_cat = VIDEO_ES;
327     p_dec->fmt_out.i_codec = fmt_out.i_chroma;
328     p_dec->fmt_out.video = fmt_out;
329
330     /* Set callbacks */
331     p_dec->pf_decode_video = DecodeBlock;
332
333     p_dec->p_sys->p_image = p_image;
334     vlc_mutex_init( p_dec, &p_dec->p_sys->lock );
335
336     return VLC_SUCCESS;
337 }
338
339 /****************************************************************************
340  * DecodeBlock: the whole thing
341  ****************************************************************************/
342 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
343 {
344     decoder_sys_t *p_sys = (decoder_sys_t*) p_dec->p_sys;
345     picture_t *p_pic;
346
347     if( pp_block == NULL || !*pp_block ) return NULL;
348     p_pic = p_dec->pf_vout_buffer_new( p_dec );
349     if( p_pic == NULL )
350     {
351         msg_Err( p_dec, "cannot get picture" );
352         goto error;
353     }
354
355     if( p_sys->b_reload && (mdate() >= p_sys->i_next) )
356     {
357         var_TriggerCallback( p_dec, "fake-file" );
358         /* next period */
359         p_sys->i_next = (mtime_t)(p_sys->i_reload + mdate());
360     }
361     vlc_mutex_lock( &p_dec->p_sys->lock );
362     vout_CopyPicture( p_dec, p_pic, p_dec->p_sys->p_image );
363     vlc_mutex_unlock( &p_dec->p_sys->lock );
364
365     p_pic->date = (*pp_block)->i_pts;
366
367 error:
368     block_Release( *pp_block );
369     *pp_block = NULL;
370
371     return p_pic;
372 }
373
374 /*****************************************************************************
375  * CloseDecoder: fake decoder destruction
376  *****************************************************************************/
377 static void CloseDecoder( vlc_object_t *p_this )
378 {
379     decoder_t *p_dec = (decoder_t *)p_this;
380     picture_t *p_image = p_dec->p_sys->p_image;
381
382     if( p_image != NULL )
383         p_image->pf_release( p_image );
384
385     vlc_mutex_destroy( &p_dec->p_sys->lock );
386     free( p_dec->p_sys );
387 }
388
389 /*****************************************************************************
390  * FakeCallback: Image source change callback.
391  *****************************************************************************/
392 static int FakeCallback( vlc_object_t *p_this, char const *psz_var,
393                          vlc_value_t oldval, vlc_value_t newval,
394                          void *p_data )
395 {
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         p_image->pf_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 = VLC_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 = VLC_FALSE;
445         }
446     }
447
448     return VLC_SUCCESS;
449 }