]> git.sesse.net Git - vlc/blob - modules/codec/fake.c
* modules/codec/fake.c: Fixed compilation.
[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  *
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 #include "vlc_filter.h"
32
33 /*****************************************************************************
34  * Local prototypes
35  *****************************************************************************/
36 static int  OpenDecoder   ( vlc_object_t * );
37 static void CloseDecoder  ( vlc_object_t * );
38
39 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 #define FILE_TEXT N_("Image file")
45 #define FILE_LONGTEXT N_( \
46     "Path of the image file when using the fake input." )
47 #define WIDTH_TEXT N_("Video width")
48 #define WIDTH_LONGTEXT N_( \
49     "Allows you to specify the output video width." )
50 #define HEIGHT_TEXT N_("Video height")
51 #define HEIGHT_LONGTEXT N_( \
52     "Allows you to specify the output video height." )
53 #define KEEP_AR_TEXT N_("Keep aspect ratio")
54 #define KEEP_AR_LONGTEXT N_( \
55     "If selected, width and height will be considered as maximum values." )
56 #define ASPECT_RATIO_TEXT N_("Background aspect ratio")
57 #define ASPECT_RATIO_LONGTEXT N_( \
58     "Aspect ratio of the image file (4:3, 16:9). Default is square pixels." )
59 #define DEINTERLACE_TEXT N_("Deinterlace video")
60 #define DEINTERLACE_LONGTEXT N_( \
61     "Allows you to deinterlace the image after loading." )
62 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
63 #define DEINTERLACE_MODULE_LONGTEXT N_( \
64     "Specifies the deinterlace module to use." )
65
66 static char *ppsz_deinterlace_type[] =
67 {
68     "deinterlace", "ffmpeg-deinterlace"
69 };
70
71 vlc_module_begin();
72     set_category( CAT_INPUT );
73     set_subcategory( SUBCAT_INPUT_VCODEC );
74     set_shortname( _("Fake") );
75     set_description( _("Fake video decoder") );
76     set_capability( "decoder", 1000 );
77     set_callbacks( OpenDecoder, CloseDecoder );
78     add_shortcut( "fake" );
79
80     add_file( "fake-file", "", NULL, FILE_TEXT,
81                 FILE_LONGTEXT, VLC_FALSE );
82     add_integer( "fake-width", 0, NULL, WIDTH_TEXT,
83                  WIDTH_LONGTEXT, VLC_TRUE );
84     add_integer( "fake-height", 0, NULL, HEIGHT_TEXT,
85                  HEIGHT_LONGTEXT, VLC_TRUE );
86     add_bool( "fake-keep-ar", 0, NULL, KEEP_AR_TEXT, KEEP_AR_LONGTEXT,
87               VLC_TRUE );
88     add_string( "fake-aspect-ratio", "", NULL,
89                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, VLC_TRUE );
90     add_bool( "fake-deinterlace", 0, NULL, DEINTERLACE_TEXT,
91               DEINTERLACE_LONGTEXT, VLC_FALSE );
92     add_string( "fake-deinterlace-module", "deinterlace", NULL,
93                 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
94                 VLC_FALSE );
95         change_string_list( ppsz_deinterlace_type, 0, 0 );
96 vlc_module_end();
97
98 /*****************************************************************************
99  * OpenDecoder: probe the decoder and return score
100  *****************************************************************************/
101 static int OpenDecoder( vlc_object_t *p_this )
102 {
103     decoder_t *p_dec = (decoder_t*)p_this;
104     vlc_value_t val;
105     image_handler_t *p_handler;
106     video_format_t fmt_in, fmt_out;
107     picture_t *p_image;
108     char *psz_file;
109     vlc_bool_t b_keep_ar;
110     int i_aspect = 0;
111
112     if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','a','k','e') )
113     {
114         return VLC_EGENERIC;
115     }
116
117     var_Create( p_dec, "fake-file", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
118     var_Get( p_dec, "fake-file", &val );
119     if( val.psz_string == NULL || !*val.psz_string )
120     {
121         if( val.psz_string ) free( val.psz_string );
122         msg_Err( p_dec, "specify a file with --fake-file=..." );
123         return VLC_EGENERIC;
124     }
125     psz_file = val.psz_string;
126
127     memset( &fmt_in, 0, sizeof(fmt_in) );
128     memset( &fmt_out, 0, sizeof(fmt_out) );
129     fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
130
131     var_Create( p_dec, "fake-keep-ar", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
132     var_Get( p_dec, "fake-keep-ar", &val );
133     b_keep_ar = val.b_bool;
134
135     var_Create( p_dec, "fake-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
136     var_Create( p_dec, "fake-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
137     var_Create( p_dec, "fake-aspect-ratio",
138                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
139
140     var_Get( p_dec, "fake-aspect-ratio", &val );
141     if ( val.psz_string )
142     {
143         char *psz_parser = strchr( val.psz_string, ':' );
144
145         if( psz_parser )
146         {
147             *psz_parser++ = '\0';
148             i_aspect = atoi( val.psz_string )
149                                    * VOUT_ASPECT_FACTOR / atoi( psz_parser );
150         }
151         free( val.psz_string );
152     }
153
154     if ( !b_keep_ar )
155     {
156         var_Get( p_dec, "fake-width", &val );
157         fmt_out.i_width = val.i_int;
158         var_Get( p_dec, "fake-height", &val );
159         fmt_out.i_height = val.i_int;
160     }
161
162     p_handler = image_HandlerCreate( p_dec );
163     p_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
164     image_HandlerDelete( p_handler );
165
166     if ( p_image == NULL )
167     {
168         msg_Err( p_dec, "unable to read image file %s", psz_file );
169         return VLC_EGENERIC;
170     }
171     msg_Dbg( p_dec, "file %s loaded successfully", psz_file );
172
173     if ( psz_file ) free( psz_file );
174
175     if ( b_keep_ar )
176     {
177         picture_t *p_old = p_image;
178         int i_width, i_height;
179
180         var_Get( p_dec, "fake-width", &val );
181         i_width = val.i_int;
182         var_Get( p_dec, "fake-height", &val );
183         i_height = val.i_int;
184
185         if ( i_width && i_height )
186         {
187             int i_image_ar = fmt_out.i_width * VOUT_ASPECT_FACTOR
188                               / fmt_out.i_height;
189             int i_region_ar = i_width * VOUT_ASPECT_FACTOR / i_height;
190             fmt_in = fmt_out;
191
192             if ( i_aspect == i_image_ar )
193             {
194                 fmt_out.i_width = i_width;
195                 fmt_out.i_height = i_height;
196             }
197             else if ( i_image_ar > i_region_ar )
198             {
199                 fmt_out.i_width = i_width;
200                 fmt_out.i_height = i_width * VOUT_ASPECT_FACTOR
201                                     / i_image_ar;
202                 i_aspect = i_image_ar;
203             }
204             else
205             {
206                 fmt_out.i_height = i_height;
207                 fmt_out.i_width = i_height * i_image_ar
208                                     / VOUT_ASPECT_FACTOR;
209                 i_aspect = i_image_ar;
210             }
211
212             p_handler = image_HandlerCreate( p_dec );
213             p_image = image_Convert( p_handler, p_old, &fmt_in, &fmt_out );
214             image_HandlerDelete( p_handler );
215
216             if ( p_image == NULL )
217             {
218                 msg_Warn( p_dec, "couldn't load resizing module" );
219                 p_image = p_old;
220                 fmt_out = fmt_in;
221             }
222             else
223             {
224                 p_old->pf_release( p_old );
225             }
226         }
227     }
228
229     if ( i_aspect )
230     {
231         fmt_out.i_aspect = i_aspect;
232     }
233     else
234     {
235         fmt_out.i_aspect = fmt_out.i_width
236                             * VOUT_ASPECT_FACTOR / fmt_out.i_height;
237     }
238
239     var_Create( p_dec, "fake-deinterlace", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
240     var_Get( p_dec, "fake-deinterlace", &val );
241     if ( val.b_bool )
242     {
243         picture_t *p_old = p_image;
244
245         var_Create( p_dec, "fake-deinterlace-module",
246                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
247         var_Get( p_dec, "fake-deinterlace-module", &val );
248
249         p_handler = image_HandlerCreate( p_dec );
250         p_image = image_Filter( p_handler, p_old, &fmt_out, val.psz_string );
251         image_HandlerDelete( p_handler );
252         if ( val.psz_string != NULL ) free( val.psz_string );
253
254         if ( p_image == NULL )
255         {
256             msg_Warn( p_dec, "couldn't load deinterlace module" );
257             p_image = p_old;
258         }
259         else
260         {
261             p_old->pf_release( p_old );
262         }
263     }
264
265     /* Set output properties */
266     p_dec->fmt_out.i_cat = VIDEO_ES;
267     p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
268     p_dec->fmt_out.video = fmt_out;
269
270     /* Set callbacks */
271     p_dec->pf_decode_video = DecodeBlock;
272     p_dec->p_sys = (decoder_sys_t *)p_image;
273
274     return VLC_SUCCESS;
275 }
276
277 /****************************************************************************
278  * DecodeBlock: the whole thing
279  ****************************************************************************/
280 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
281 {
282     picture_t *p_image = (picture_t *)p_dec->p_sys;
283     picture_t *p_pic;
284
285     if( pp_block == NULL || !*pp_block ) return NULL;
286     p_pic = p_dec->pf_vout_buffer_new( p_dec );
287     if( p_pic == NULL )
288     {
289         msg_Err( p_dec, "cannot get picture" );
290         goto error;
291     }
292
293     vout_CopyPicture( p_dec, p_pic, p_image );
294     p_pic->date = (*pp_block)->i_pts;
295
296 error:
297     block_Release( *pp_block );
298     *pp_block = NULL;
299
300     return p_pic;
301 }
302
303 /*****************************************************************************
304  * CloseDecoder: fake decoder destruction
305  *****************************************************************************/
306 static void CloseDecoder( vlc_object_t *p_this )
307 {
308     decoder_t *p_dec = (decoder_t *)p_this;
309     picture_t *p_image = (picture_t *)p_dec->p_sys;
310
311     if( p_image != NULL )
312         p_image->pf_release( p_image );
313 }