]> git.sesse.net Git - vlc/blob - modules/codec/fake.c
0cf6297d5ec748aa658e073940ee2125c2a4a1c3
[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_description( _("fake video decoder") );
62     set_capability( "decoder", 1000 );
63     set_callbacks( OpenDecoder, CloseDecoder );
64     add_shortcut( "fake" );
65
66     add_string( "fake-file", "", NULL, FILE_TEXT,
67                 FILE_LONGTEXT, VLC_TRUE );
68     add_string( "fake-aspect-ratio", "4:3", NULL,
69                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, VLC_FALSE );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * OpenDecoder: probe the decoder and return score
74  *****************************************************************************/
75 static int OpenDecoder( vlc_object_t *p_this )
76 {
77     decoder_t *p_dec = (decoder_t*)p_this;
78     decoder_sys_t *p_sys;
79     vlc_value_t val;
80
81     if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','a','k','e') )
82     {
83         return VLC_EGENERIC;
84     }
85
86     /* Allocate the memory needed to store the decoder's structure */
87     if( ( p_dec->p_sys = p_sys =
88           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
89     {
90         msg_Err( p_dec, "out of memory" );
91         return VLC_EGENERIC;
92     }
93
94     var_Create( p_dec, "fake-file", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
95     var_Get( p_dec, "fake-file", &val );
96     if( val.psz_string != NULL && *val.psz_string )
97     {
98         image_handler_t *p_handler = image_HandlerCreate( p_dec );
99         video_format_t fmt_in, fmt_out;
100
101         memset( &fmt_in, 0, sizeof(fmt_in) );
102         memset( &fmt_out, 0, sizeof(fmt_out) );
103         fmt_out.i_chroma = VLC_FOURCC('Y', 'V', '1', '2');
104         p_sys->p_image = image_ReadUrl( p_handler, val.psz_string,
105                                         &fmt_in, &fmt_out );
106         image_HandlerDelete( p_handler );
107
108         if( p_sys->p_image == NULL )
109         {
110             msg_Err( p_dec, "unable to read image file %s", val.psz_string );
111             free( p_dec->p_sys );
112             return VLC_EGENERIC;
113         }
114         msg_Dbg( p_dec, "file %s loaded successfully", val.psz_string );
115         p_dec->fmt_out.video = fmt_out;
116     }
117     if( val.psz_string ) free( val.psz_string );
118
119     var_Create( p_dec, "fake-aspect-ratio",
120                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
121     var_Get( p_dec, "fake-aspect-ratio", &val );
122     if ( val.psz_string )
123     {
124         char *psz_parser = strchr( val.psz_string, ':' );
125
126         if( psz_parser )
127         {
128             *psz_parser++ = '\0';
129             p_dec->fmt_out.video.i_aspect = atoi( val.psz_string )
130                                    * VOUT_ASPECT_FACTOR / atoi( psz_parser );
131         }
132         else
133         {
134             msg_Warn( p_dec, "bad aspect ratio %s", val.psz_string );
135             p_dec->fmt_out.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
136         }
137
138         free( val.psz_string );
139     }
140     else
141     {
142         p_dec->fmt_out.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
143     }
144
145     /* Set output properties */
146     p_dec->fmt_out.i_cat = VIDEO_ES;
147     p_dec->fmt_out.i_codec = VLC_FOURCC('Y','V','1','2');
148
149     /* Set callbacks */
150     p_dec->pf_decode_video = DecodeBlock;
151
152     return VLC_SUCCESS;
153 }
154
155 /****************************************************************************
156  * DecodeBlock: the whole thing
157  ****************************************************************************/
158 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
159 {
160     decoder_sys_t *p_sys = p_dec->p_sys;
161     picture_t *p_pic;
162
163     if( pp_block == NULL || !*pp_block ) return NULL;
164     p_pic = p_dec->pf_vout_buffer_new( p_dec );
165     if( p_pic == NULL )
166     {
167         msg_Err( p_dec, "cannot get picture" );
168         goto error;
169     }
170
171     vout_CopyPicture( p_dec, p_pic, p_sys->p_image );
172     p_pic->date = (*pp_block)->i_pts;
173
174 error:
175     block_Release( *pp_block );
176     *pp_block = NULL;
177
178     return p_pic;
179 }
180
181 /*****************************************************************************
182  * CloseDecoder: fake decoder destruction
183  *****************************************************************************/
184 static void CloseDecoder( vlc_object_t *p_this )
185 {
186     decoder_t *p_dec = (decoder_t *)p_this;
187     decoder_sys_t *p_sys = p_dec->p_sys;
188
189     if( p_sys->p_image != NULL )
190         p_sys->p_image->pf_release( p_sys->p_image );
191     free( p_sys );
192 }