]> git.sesse.net Git - vlc/blob - modules/codec/fake.c
ALL: Add some set_shorname() so the preferences look nice.
[vlc] / modules / codec / fake.c
1 /*****************************************************************************
2  * fake.c: decoder reading from a fake stream, outputting a fixed image
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/decoder.h>
29
30 #include "vlc_image.h"
31
32 /*****************************************************************************
33  * decoder_sys_t : fake decoder descriptor
34  *****************************************************************************/
35 struct decoder_sys_t
36 {
37     picture_t *p_image;
38 };
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
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 #define FILE_TEXT N_("Image file")
52 #define FILE_LONGTEXT N_( \
53     "Path of the image file when using the fake input." )
54 #define ASPECT_RATIO_TEXT N_("Background aspect ratio")
55 #define ASPECT_RATIO_LONGTEXT N_( \
56     "Aspect ratio of the image file (4:3, 16:9)." )
57
58 vlc_module_begin();
59     set_category( CAT_INPUT );
60     set_subcategory( SUBCAT_INPUT_VCODEC );
61     set_shortname( _("Fake") );
62     set_description( _("Fake video decoder") );
63     set_capability( "decoder", 1000 );
64     set_callbacks( OpenDecoder, CloseDecoder );
65     add_shortcut( "fake" );
66
67     add_file( "fake-file", "", NULL, FILE_TEXT,
68                 FILE_LONGTEXT, VLC_FALSE );
69     add_string( "fake-aspect-ratio", "4:3", NULL,
70                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, VLC_TRUE );
71 vlc_module_end();
72
73 /*****************************************************************************
74  * OpenDecoder: probe the decoder and return score
75  *****************************************************************************/
76 static int OpenDecoder( vlc_object_t *p_this )
77 {
78     decoder_t *p_dec = (decoder_t*)p_this;
79     decoder_sys_t *p_sys;
80     vlc_value_t val;
81
82     if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','a','k','e') )
83     {
84         return VLC_EGENERIC;
85     }
86
87     /* Allocate the memory needed to store the decoder's structure */
88     if( ( p_dec->p_sys = p_sys =
89           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
90     {
91         msg_Err( p_dec, "out of memory" );
92         return VLC_EGENERIC;
93     }
94
95     var_Create( p_dec, "fake-file", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
96     var_Get( p_dec, "fake-file", &val );
97     if( val.psz_string != NULL && *val.psz_string )
98     {
99         image_handler_t *p_handler = image_HandlerCreate( p_dec );
100         video_format_t fmt_in, fmt_out;
101
102         memset( &fmt_in, 0, sizeof(fmt_in) );
103         memset( &fmt_out, 0, sizeof(fmt_out) );
104         fmt_out.i_chroma = VLC_FOURCC('Y', 'V', '1', '2');
105         p_sys->p_image = image_ReadUrl( p_handler, val.psz_string,
106                                         &fmt_in, &fmt_out );
107         image_HandlerDelete( p_handler );
108
109         if( p_sys->p_image == NULL )
110         {
111             msg_Err( p_dec, "unable to read image file %s", val.psz_string );
112             free( p_dec->p_sys );
113             return VLC_EGENERIC;
114         }
115         msg_Dbg( p_dec, "file %s loaded successfully", val.psz_string );
116         p_dec->fmt_out.video = fmt_out;
117     }
118     if( val.psz_string ) free( val.psz_string );
119
120     var_Create( p_dec, "fake-aspect-ratio",
121                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
122     var_Get( p_dec, "fake-aspect-ratio", &val );
123     if ( val.psz_string )
124     {
125         char *psz_parser = strchr( val.psz_string, ':' );
126
127         if( psz_parser )
128         {
129             *psz_parser++ = '\0';
130             p_dec->fmt_out.video.i_aspect = atoi( val.psz_string )
131                                    * VOUT_ASPECT_FACTOR / atoi( psz_parser );
132         }
133         else
134         {
135             msg_Warn( p_dec, "bad aspect ratio %s", val.psz_string );
136             p_dec->fmt_out.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
137         }
138
139         free( val.psz_string );
140     }
141     else
142     {
143         p_dec->fmt_out.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
144     }
145
146     /* Set output properties */
147     p_dec->fmt_out.i_cat = VIDEO_ES;
148     p_dec->fmt_out.i_codec = VLC_FOURCC('Y','V','1','2');
149
150     /* Set callbacks */
151     p_dec->pf_decode_video = DecodeBlock;
152
153     return VLC_SUCCESS;
154 }
155
156 /****************************************************************************
157  * DecodeBlock: the whole thing
158  ****************************************************************************/
159 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
160 {
161     decoder_sys_t *p_sys = p_dec->p_sys;
162     picture_t *p_pic;
163
164     if( pp_block == NULL || !*pp_block ) return NULL;
165     p_pic = p_dec->pf_vout_buffer_new( p_dec );
166     if( p_pic == NULL )
167     {
168         msg_Err( p_dec, "cannot get picture" );
169         goto error;
170     }
171
172     vout_CopyPicture( p_dec, p_pic, p_sys->p_image );
173     p_pic->date = (*pp_block)->i_pts;
174
175 error:
176     block_Release( *pp_block );
177     *pp_block = NULL;
178
179     return p_pic;
180 }
181
182 /*****************************************************************************
183  * CloseDecoder: fake decoder destruction
184  *****************************************************************************/
185 static void CloseDecoder( vlc_object_t *p_this )
186 {
187     decoder_t *p_dec = (decoder_t *)p_this;
188     decoder_sys_t *p_sys = p_dec->p_sys;
189
190     if( p_sys->p_image != NULL )
191         p_sys->p_image->pf_release( p_sys->p_image );
192     free( p_sys );
193 }