1 /*****************************************************************************
2 * fake.c: decoder reading from a fake stream, outputting a fixed image
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman at m2x dot nl>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
36 #include <vlc_image.h>
37 #include <vlc_filter.h>
38 #include <vlc_charset.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int OpenDecoder ( vlc_object_t * );
44 static void CloseDecoder ( vlc_object_t * );
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 * );
50 /*****************************************************************************
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." )
81 static const char *const ppsz_deinterlace_type[] =
83 "deinterlace", "ffmpeg-deinterlace"
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" )
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", false, NULL, KEEP_AR_TEXT, KEEP_AR_LONGTEXT,
105 add_string( "fake-aspect-ratio", "", NULL,
106 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
107 add_bool( "fake-deinterlace", false, NULL, DEINTERLACE_TEXT,
108 DEINTERLACE_LONGTEXT, false )
109 add_string( "fake-deinterlace-module", "deinterlace", NULL,
110 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
112 change_string_list( ppsz_deinterlace_type, 0, 0 )
113 add_string( "fake-chroma", "I420", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
127 /*****************************************************************************
128 * OpenDecoder: probe the decoder and return score
129 *****************************************************************************/
130 static int OpenDecoder( vlc_object_t *p_this )
132 decoder_t *p_dec = (decoder_t*)p_this;
133 image_handler_t *p_handler;
134 video_format_t fmt_in, fmt_out;
136 char *psz_file, *psz_chroma, *psz_string;
141 if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','a','k','e') )
146 p_dec->p_sys = calloc( 1, sizeof( *p_dec->p_sys ) );
150 psz_file = var_CreateGetNonEmptyStringCommand( p_dec, "fake-file" );
153 msg_Err( p_dec, "specify a file with --fake-file=..." );
154 free( p_dec->p_sys );
158 memset( &fmt_in, 0, sizeof(fmt_in) );
159 memset( &fmt_out, 0, sizeof(fmt_out) );
161 i_int = var_CreateGetIntegerCommand( p_dec, "fake-file-reload" );
164 p_dec->p_sys->b_reload = true;
165 p_dec->p_sys->i_reload = (mtime_t)(i_int * 1000000);
166 p_dec->p_sys->i_next = (mtime_t)(p_dec->p_sys->i_reload + mdate());
169 psz_chroma = var_CreateGetString( p_dec, "fake-chroma" );
170 fmt_out.i_chroma = vlc_fourcc_GetCodecFromString( VIDEO_ES, psz_chroma );
171 if( !fmt_out.i_chroma )
173 msg_Warn( p_dec, "Invalid chroma (%s). Using I420.", psz_chroma );
174 fmt_out.i_chroma = VLC_CODEC_I420;
178 var_Create( p_dec, "fake-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
179 var_Create( p_dec, "fake-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
180 psz_string = var_CreateGetString( p_dec, "fake-aspect-ratio" );
184 char *psz_parser = strchr( psz_string, ':' );
188 *psz_parser++ = '\0';
189 i_aspect = atoi( psz_string )
190 * VOUT_ASPECT_FACTOR / atoi( psz_parser );
195 b_keep_ar = var_CreateGetBool( p_dec, "fake-keep-ar" );
199 fmt_out.i_width = var_GetInteger( p_dec, "fake-width" );
200 fmt_out.i_height = var_GetInteger( p_dec, "fake-height" );
203 p_handler = image_HandlerCreate( p_dec );
204 p_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
205 image_HandlerDelete( p_handler );
207 if ( p_image == NULL )
209 msg_Err( p_dec, "unable to read image file %s", psz_file );
211 free( p_dec->p_sys );
214 msg_Dbg( p_dec, "file %s loaded successfully", psz_file );
220 picture_t *p_old = p_image;
221 int i_width, i_height;
223 i_width = var_GetInteger( p_dec, "fake-width" );
224 i_height = var_GetInteger( p_dec, "fake-height" );
226 if ( i_width && i_height )
228 int i_image_ar = fmt_out.i_width * VOUT_ASPECT_FACTOR
230 int i_region_ar = i_width * VOUT_ASPECT_FACTOR / i_height;
233 if ( i_aspect == i_image_ar )
235 fmt_out.i_width = i_width;
236 fmt_out.i_height = i_height;
238 else if ( i_image_ar > i_region_ar )
240 fmt_out.i_width = i_width;
241 fmt_out.i_height = i_width * VOUT_ASPECT_FACTOR
243 i_aspect = i_image_ar;
247 fmt_out.i_height = i_height;
248 fmt_out.i_width = i_height * i_image_ar
249 / VOUT_ASPECT_FACTOR;
250 i_aspect = i_image_ar;
253 p_handler = image_HandlerCreate( p_dec );
254 p_image = image_Convert( p_handler, p_old, &fmt_in, &fmt_out );
255 image_HandlerDelete( p_handler );
257 if ( p_image == NULL )
259 msg_Warn( p_dec, "couldn't load resizing module" );
265 picture_Release( p_old );
272 fmt_out.i_sar_num = i_aspect * fmt_out.i_height;
273 fmt_out.i_sar_den = VOUT_ASPECT_FACTOR * fmt_out.i_width;
277 fmt_out.i_sar_num = 1;
278 fmt_out.i_sar_den = 1;
281 if( var_CreateGetBool( p_dec, "fake-deinterlace" ) )
283 picture_t *p_old = p_image;
285 psz_string = var_CreateGetString( p_dec, "fake-deinterlace-module" );
287 p_handler = image_HandlerCreate( p_dec );
288 p_image = image_Filter( p_handler, p_old, &fmt_out, psz_string );
289 image_HandlerDelete( p_handler );
292 if ( p_image == NULL )
294 msg_Warn( p_dec, "couldn't load deinterlace module" );
299 picture_Release( p_old );
303 /* Set output properties */
304 p_dec->fmt_out.i_cat = VIDEO_ES;
305 p_dec->fmt_out.i_codec = fmt_out.i_chroma;
306 p_dec->fmt_out.video = fmt_out;
309 p_dec->pf_decode_video = DecodeBlock;
311 p_dec->p_sys->p_image = p_image;
312 vlc_mutex_init( &p_dec->p_sys->lock );
314 /* Add the callback when every variables are available */
315 var_AddCallback( p_dec, "fake-file", FakeCallback, p_dec );
316 var_AddCallback( p_dec, "fake-file-reload", FakeCallback , p_dec );
321 /****************************************************************************
322 * DecodeBlock: the whole thing
323 ****************************************************************************/
324 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
326 decoder_sys_t *p_sys = (decoder_sys_t*) p_dec->p_sys;
329 if( pp_block == NULL || !*pp_block ) return NULL;
330 p_pic = decoder_NewPicture( p_dec );
333 msg_Err( p_dec, "cannot get picture" );
337 if( p_sys->b_reload && (mdate() >= p_sys->i_next) )
339 var_TriggerCallback( p_dec, "fake-file" );
341 p_sys->i_next = (mtime_t)(p_sys->i_reload + mdate());
343 vlc_mutex_lock( &p_dec->p_sys->lock );
344 picture_Copy( p_pic, p_dec->p_sys->p_image );
345 vlc_mutex_unlock( &p_dec->p_sys->lock );
347 p_pic->date = (*pp_block)->i_pts;
350 block_Release( *pp_block );
356 /*****************************************************************************
357 * CloseDecoder: fake decoder destruction
358 *****************************************************************************/
359 static void CloseDecoder( vlc_object_t *p_this )
361 decoder_t *p_dec = (decoder_t *)p_this;
362 picture_t *p_image = p_dec->p_sys->p_image;
364 var_DelCallback( p_dec, "fake-file", FakeCallback, p_dec );
365 var_DelCallback( p_dec, "fake-file-reload", FakeCallback , p_dec );
367 if( p_image != NULL )
368 picture_Release( p_image );
370 vlc_mutex_destroy( &p_dec->p_sys->lock );
371 free( p_dec->p_sys );
374 /*****************************************************************************
375 * FakeCallback: Image source change callback.
376 *****************************************************************************/
377 static int FakeCallback( vlc_object_t *p_this, char const *psz_var,
378 vlc_value_t oldval, vlc_value_t newval,
381 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
382 decoder_t *p_dec = (decoder_t *)p_data;
384 if( !strcmp( psz_var, "fake-file" ) )
386 image_handler_t *p_handler;
387 picture_t *p_new_image;
388 video_format_t fmt_in, fmt_out;
389 char *psz_file = newval.psz_string;
392 vlc_mutex_lock( &p_dec->p_sys->lock );
393 p_image = p_dec->p_sys->p_image;
395 if( !psz_file || !*psz_file )
397 msg_Err( p_dec, "fake-file value must be non empty." );
398 vlc_mutex_unlock( &p_dec->p_sys->lock );
401 msg_Dbg( p_dec, "Changing fake-file to %s.", psz_file );
403 memset( &fmt_in, 0, sizeof(fmt_in) );
404 fmt_out = p_dec->fmt_out.video;
405 p_handler = image_HandlerCreate( p_dec );
406 p_new_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
407 image_HandlerDelete( p_handler );
411 msg_Err( p_dec, "error while reading image (%s)", psz_file );
412 vlc_mutex_unlock( &p_dec->p_sys->lock );
416 p_dec->p_sys->p_image = p_new_image;
417 picture_Release( p_image );
418 vlc_mutex_unlock( &p_dec->p_sys->lock );
420 else if( !strcmp( psz_var, "fake-file-reload" ) )
422 if( newval.i_int > 0)
424 p_dec->p_sys->b_reload = true;
425 p_dec->p_sys->i_reload = (mtime_t)(newval.i_int * 1000000);
426 p_dec->p_sys->i_next = (mtime_t)(p_dec->p_sys->i_reload + mdate());
430 p_dec->p_sys->b_reload = false;