1 /*****************************************************************************
2 * fake.c: decoder reading from a fake stream, outputting a fixed image
3 *****************************************************************************
4 * Copyright (C) 2005 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 *****************************************************************************/
29 #include <vlc_codec.h>
31 #include <vlc_image.h>
32 #include <vlc_filter.h>
33 #include <vlc_charset.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int OpenDecoder ( vlc_object_t * );
39 static void CloseDecoder ( vlc_object_t * );
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 * );
45 /*****************************************************************************
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." )
76 static const char *ppsz_deinterlace_type[] =
78 "deinterlace", "ffmpeg-deinterlace"
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" );
90 add_file( "fake-file", "", NULL, FILE_TEXT,
91 FILE_LONGTEXT, VLC_FALSE );
92 add_integer( "fake-file-reload", 0, NULL, RELOAD_TEXT,
93 RELOAD_LONGTEXT, VLC_FALSE );
94 add_integer( "fake-width", 0, NULL, WIDTH_TEXT,
95 WIDTH_LONGTEXT, VLC_TRUE );
96 add_integer( "fake-height", 0, NULL, HEIGHT_TEXT,
97 HEIGHT_LONGTEXT, VLC_TRUE );
98 add_bool( "fake-keep-ar", 0, NULL, KEEP_AR_TEXT, KEEP_AR_LONGTEXT,
100 add_string( "fake-aspect-ratio", "", NULL,
101 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, VLC_TRUE );
102 add_bool( "fake-deinterlace", 0, NULL, DEINTERLACE_TEXT,
103 DEINTERLACE_LONGTEXT, VLC_FALSE );
104 add_string( "fake-deinterlace-module", "deinterlace", NULL,
105 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
107 change_string_list( ppsz_deinterlace_type, 0, 0 );
108 add_string( "fake-chroma", "I420", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
122 /*****************************************************************************
123 * OpenDecoder: probe the decoder and return score
124 *****************************************************************************/
125 static int OpenDecoder( vlc_object_t *p_this )
127 decoder_t *p_dec = (decoder_t*)p_this;
129 image_handler_t *p_handler;
130 video_format_t fmt_in, fmt_out;
132 char *psz_file, *psz_chroma;
133 vlc_bool_t b_keep_ar;
136 if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','a','k','e') )
141 p_dec->p_sys = (decoder_sys_t *)malloc( sizeof( decoder_sys_t ) );
146 memset( p_dec->p_sys, 0, sizeof( decoder_sys_t ) );
148 psz_file = var_CreateGetNonEmptyStringCommand( p_dec, "fake-file" );
151 msg_Err( p_dec, "specify a file with --fake-file=..." );
154 var_AddCallback( p_dec, "fake-file", FakeCallback, p_dec );
156 memset( &fmt_in, 0, sizeof(fmt_in) );
157 memset( &fmt_out, 0, sizeof(fmt_out) );
159 val.i_int = var_CreateGetIntegerCommand( p_dec, "fake-file-reload" );
162 p_dec->p_sys->b_reload = VLC_TRUE;
163 p_dec->p_sys->i_reload = (mtime_t)(val.i_int * 1000000);
164 p_dec->p_sys->i_next = (mtime_t)(p_dec->p_sys->i_reload + mdate());
167 psz_chroma = var_CreateGetString( p_dec, "fake-chroma" );
168 if( strlen( psz_chroma ) != 4 )
170 msg_Warn( p_dec, "Invalid chroma (%s). Using I420.", psz_chroma );
171 fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
175 fmt_out.i_chroma = VLC_FOURCC( psz_chroma[0],
182 var_Create( p_dec, "fake-keep-ar", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
183 var_Get( p_dec, "fake-keep-ar", &val );
184 b_keep_ar = val.b_bool;
186 var_Create( p_dec, "fake-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
187 var_Create( p_dec, "fake-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
188 var_Create( p_dec, "fake-aspect-ratio",
189 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
191 var_Get( p_dec, "fake-aspect-ratio", &val );
192 if ( val.psz_string )
194 char *psz_parser = strchr( val.psz_string, ':' );
198 *psz_parser++ = '\0';
199 i_aspect = atoi( val.psz_string )
200 * VOUT_ASPECT_FACTOR / atoi( psz_parser );
202 free( val.psz_string );
207 var_Get( p_dec, "fake-width", &val );
208 fmt_out.i_width = val.i_int;
209 var_Get( p_dec, "fake-height", &val );
210 fmt_out.i_height = val.i_int;
213 p_handler = image_HandlerCreate( p_dec );
214 p_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
215 image_HandlerDelete( p_handler );
217 if ( p_image == NULL )
219 msg_Err( p_dec, "unable to read image file %s", psz_file );
222 msg_Dbg( p_dec, "file %s loaded successfully", psz_file );
224 if ( psz_file ) free( psz_file );
228 picture_t *p_old = p_image;
229 int i_width, i_height;
231 var_Get( p_dec, "fake-width", &val );
233 var_Get( p_dec, "fake-height", &val );
234 i_height = val.i_int;
236 if ( i_width && i_height )
238 int i_image_ar = fmt_out.i_width * VOUT_ASPECT_FACTOR
240 int i_region_ar = i_width * VOUT_ASPECT_FACTOR / i_height;
243 if ( i_aspect == i_image_ar )
245 fmt_out.i_width = i_width;
246 fmt_out.i_height = i_height;
248 else if ( i_image_ar > i_region_ar )
250 fmt_out.i_width = i_width;
251 fmt_out.i_height = i_width * VOUT_ASPECT_FACTOR
253 i_aspect = i_image_ar;
257 fmt_out.i_height = i_height;
258 fmt_out.i_width = i_height * i_image_ar
259 / VOUT_ASPECT_FACTOR;
260 i_aspect = i_image_ar;
263 p_handler = image_HandlerCreate( p_dec );
264 p_image = image_Convert( p_handler, p_old, &fmt_in, &fmt_out );
265 image_HandlerDelete( p_handler );
267 if ( p_image == NULL )
269 msg_Warn( p_dec, "couldn't load resizing module" );
275 p_old->pf_release( p_old );
282 fmt_out.i_aspect = i_aspect;
286 fmt_out.i_aspect = fmt_out.i_width
287 * VOUT_ASPECT_FACTOR / fmt_out.i_height;
290 var_Create( p_dec, "fake-deinterlace", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
291 var_Get( p_dec, "fake-deinterlace", &val );
294 picture_t *p_old = p_image;
296 var_Create( p_dec, "fake-deinterlace-module",
297 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
298 var_Get( p_dec, "fake-deinterlace-module", &val );
300 p_handler = image_HandlerCreate( p_dec );
301 p_image = image_Filter( p_handler, p_old, &fmt_out, val.psz_string );
302 image_HandlerDelete( p_handler );
303 if ( val.psz_string != NULL ) free( val.psz_string );
305 if ( p_image == NULL )
307 msg_Warn( p_dec, "couldn't load deinterlace module" );
312 p_old->pf_release( p_old );
316 /* Set output properties */
317 p_dec->fmt_out.i_cat = VIDEO_ES;
318 p_dec->fmt_out.i_codec = fmt_out.i_chroma;
319 p_dec->fmt_out.video = fmt_out;
322 p_dec->pf_decode_video = DecodeBlock;
324 p_dec->p_sys->p_image = p_image;
325 vlc_mutex_init( p_dec, &p_dec->p_sys->lock );
330 /****************************************************************************
331 * DecodeBlock: the whole thing
332 ****************************************************************************/
333 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
335 decoder_sys_t *p_sys = (decoder_sys_t*) p_dec->p_sys;
338 if( pp_block == NULL || !*pp_block ) return NULL;
339 p_pic = p_dec->pf_vout_buffer_new( p_dec );
342 msg_Err( p_dec, "cannot get picture" );
346 if( p_sys->b_reload && (mdate() >= p_sys->i_next) )
348 var_TriggerCallback( p_dec, "fake-file" );
350 p_sys->i_next = (mtime_t)(p_sys->i_reload + mdate());
352 vlc_mutex_lock( &p_dec->p_sys->lock );
353 vout_CopyPicture( p_dec, p_pic, p_dec->p_sys->p_image );
354 vlc_mutex_unlock( &p_dec->p_sys->lock );
356 p_pic->date = (*pp_block)->i_pts;
359 block_Release( *pp_block );
365 /*****************************************************************************
366 * CloseDecoder: fake decoder destruction
367 *****************************************************************************/
368 static void CloseDecoder( vlc_object_t *p_this )
370 decoder_t *p_dec = (decoder_t *)p_this;
371 picture_t *p_image = p_dec->p_sys->p_image;
373 if( p_image != NULL )
374 p_image->pf_release( p_image );
376 vlc_mutex_destroy( &p_dec->p_sys->lock );
377 free( p_dec->p_sys );
380 /*****************************************************************************
381 * FakeCallback: Image source change callback.
382 *****************************************************************************/
383 static int FakeCallback( vlc_object_t *p_this, char const *psz_var,
384 vlc_value_t oldval, vlc_value_t newval,
387 decoder_t *p_dec = (decoder_t *)p_data;
389 if( !strcmp( psz_var, "fake-file" ) )
391 image_handler_t *p_handler;
392 picture_t *p_new_image;
393 video_format_t fmt_in, fmt_out;
394 char *psz_file = newval.psz_string;
397 vlc_mutex_lock( &p_dec->p_sys->lock );
398 p_image = p_dec->p_sys->p_image;
400 if( !psz_file || !*psz_file )
402 msg_Err( p_dec, "fake-file value must be non empty." );
403 vlc_mutex_unlock( &p_dec->p_sys->lock );
406 msg_Dbg( p_dec, "Changing fake-file to %s.", psz_file );
408 memset( &fmt_in, 0, sizeof(fmt_in) );
409 fmt_out = p_dec->fmt_out.video;
410 p_handler = image_HandlerCreate( p_dec );
411 p_new_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
412 image_HandlerDelete( p_handler );
416 msg_Err( p_dec, "error while reading image (%s)", psz_file );
417 vlc_mutex_unlock( &p_dec->p_sys->lock );
421 p_dec->p_sys->p_image = p_new_image;
422 p_image->pf_release( p_image );
423 vlc_mutex_unlock( &p_dec->p_sys->lock );
425 else if( !strcmp( psz_var, "fake-file-reload" ) )
427 if( newval.i_int > 0)
429 p_dec->p_sys->b_reload = VLC_TRUE;
430 p_dec->p_sys->i_reload = (mtime_t)(newval.i_int * 1000000);
431 p_dec->p_sys->i_next = (mtime_t)(p_dec->p_sys->i_reload + mdate());
435 p_dec->p_sys->b_reload = VLC_FALSE;