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