]> git.sesse.net Git - vlc/blob - modules/video_output/image.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / video_output / image.c
1 /*****************************************************************************
2  * image.c : image video output
3  *****************************************************************************
4  * Copyright (C) 2004-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc_vout.h>
30 #include <vlc_interface.h>
31
32 #include "vlc_image.h"
33 #include "vlc_strings.h"
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Create    ( vlc_object_t * );
39 static void Destroy   ( vlc_object_t * );
40
41 static int  Init      ( vout_thread_t * );
42 static void End       ( vout_thread_t *p_vout );
43 static void Display   ( vout_thread_t *, picture_t * );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 #define FORMAT_TEXT N_( "Image format" )
49 #define FORMAT_LONGTEXT N_( "Format of the output images (png or jpg)." )
50
51 #define WIDTH_TEXT N_( "Image width" )
52 #define WIDTH_LONGTEXT N_( "You can enforce the image width. By default " \
53                             "(-1) VLC will adapt to the video " \
54                             "characteristics.")
55
56 #define HEIGHT_TEXT N_( "Image height" )
57 #define HEIGHT_LONGTEXT N_( "You can enforce the image height. By default " \
58                             "(-1) VLC will adapt to the video " \
59                             "characteristics.")
60
61 #define RATIO_TEXT N_( "Recording ratio" )
62 #define RATIO_LONGTEXT N_( "Ratio of images to record. "\
63                            "3 means that one image out of three is recorded." )
64
65 #define PREFIX_TEXT N_( "Filename prefix" )
66 #define PREFIX_LONGTEXT N_( "Prefix of the output images filenames. Output " \
67                             "filenames will have the \"prefixNUMBER.format\" "\
68                             "form." )
69
70 #define REPLACE_TEXT N_( "Always write to the same file" )
71 #define REPLACE_LONGTEXT N_( "Always write to the same file instead of " \
72                             "creating one file per image. In this case, " \
73                              "the number is not appended to the filename." )
74
75 static const char *psz_format_list[] = { "png", "jpeg" };
76 static const char *psz_format_list_text[] = { "PNG", "JPEG" };
77
78 #define CFG_PREFIX "image-out-"
79
80 vlc_module_begin( );
81     set_shortname( _( "Image file" ) );
82     set_description( _( "Image video output" ) );
83     set_category( CAT_VIDEO );
84     set_subcategory( SUBCAT_VIDEO_VOUT );
85     set_capability( "video output", 0 );
86
87     add_string(  CFG_PREFIX "format", "png", NULL,
88                  FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );
89         change_safe();
90     change_string_list( psz_format_list, psz_format_list_text, 0 );
91     add_integer( CFG_PREFIX "width", 0, NULL,
92                  WIDTH_TEXT, WIDTH_LONGTEXT, VLC_TRUE );
93         change_safe();
94         add_deprecated_alias( "image-width" ); /* since 0.9.0 */
95     add_integer( CFG_PREFIX "height", 0, NULL,
96                  HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_TRUE );
97         change_safe();
98         add_deprecated_alias( "image-height" ); /* since 0.9.0 */
99     add_integer( CFG_PREFIX "ratio", 3, NULL,
100                  RATIO_TEXT, RATIO_LONGTEXT, VLC_FALSE );
101         change_safe();
102     add_string(  CFG_PREFIX "prefix", "img", NULL,
103                  PREFIX_TEXT, PREFIX_LONGTEXT, VLC_FALSE );
104     add_bool(    CFG_PREFIX "replace", 0, NULL,
105                  REPLACE_TEXT, REPLACE_LONGTEXT, VLC_FALSE );
106         change_safe();
107     set_callbacks( Create, Destroy );
108 vlc_module_end();
109
110 static const char *ppsz_vout_options[] = {
111     "format", "width", "height", "ratio", "prefix", "replace", NULL
112 };
113
114 /*****************************************************************************
115  * vout_sys_t: video output descriptor
116  *****************************************************************************/
117 struct vout_sys_t
118 {
119     char        *psz_prefix;          /* Prefix */
120     char        *psz_format;          /* Format */
121     int         i_ratio;         /* Image ratio */
122
123     unsigned int i_width;        /* Image width */
124     unsigned int i_height;      /* Image height */
125
126     int         i_current;     /* Current image */
127     int         i_frames;   /* Number of frames */
128
129     vlc_bool_t  b_replace;
130
131     vlc_bool_t b_time;
132     vlc_bool_t b_meta;
133
134     image_handler_t *p_image;
135 };
136
137 /*****************************************************************************
138  * Create: allocates video thread
139  *****************************************************************************
140  * This function allocates and initializes a vout method.
141  *****************************************************************************/
142 static int Create( vlc_object_t *p_this )
143 {
144     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
145
146     /* Allocate instance and initialize some members */
147     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
148     if( ! p_vout->p_sys )
149         return VLC_ENOMEM;
150
151     config_ChainParse( p_vout, CFG_PREFIX, ppsz_vout_options,
152                        p_vout->p_cfg );
153
154     p_vout->p_sys->psz_prefix =
155             var_CreateGetString( p_this, CFG_PREFIX "prefix" );
156     p_vout->p_sys->b_time = strchr( p_vout->p_sys->psz_prefix, '%' )
157                             ? VLC_TRUE : VLC_FALSE;
158     p_vout->p_sys->b_meta = strchr( p_vout->p_sys->psz_prefix, '$' )
159                             ? VLC_TRUE : VLC_FALSE;
160     p_vout->p_sys->psz_format =
161             var_CreateGetString( p_this, CFG_PREFIX "format" );
162     p_vout->p_sys->i_width =
163             var_CreateGetInteger( p_this, CFG_PREFIX "width" );
164     p_vout->p_sys->i_height =
165             var_CreateGetInteger( p_this, CFG_PREFIX "height" );
166     p_vout->p_sys->i_ratio =
167             var_CreateGetInteger( p_this, CFG_PREFIX "ratio" );
168     p_vout->p_sys->b_replace =
169             var_CreateGetBool( p_this, CFG_PREFIX "replace" );
170     p_vout->p_sys->i_current = 0;
171     p_vout->p_sys->p_image = image_HandlerCreate( p_vout );
172
173     if( !p_vout->p_sys->p_image )
174     {
175         msg_Err( p_this, "unable to create image handler") ;
176         FREENULL( p_vout->p_sys->psz_prefix );
177         FREENULL( p_vout->p_sys );
178         return VLC_EGENERIC;
179     }
180
181     p_vout->pf_init = Init;
182     p_vout->pf_end = End;
183     p_vout->pf_manage = NULL;
184     p_vout->pf_render = Display;
185     p_vout->pf_display = NULL;
186
187     return VLC_SUCCESS;
188 }
189
190 /*****************************************************************************
191  * Init: initialize video thread
192  *****************************************************************************/
193 static int Init( vout_thread_t *p_vout )
194 {
195     int i_index;
196     picture_t *p_pic;
197
198     /* Initialize the output structure */
199     p_vout->output.i_chroma = p_vout->render.i_chroma;
200     p_vout->output.pf_setpalette = NULL;
201     p_vout->output.i_width = p_vout->render.i_width;
202     p_vout->output.i_height = p_vout->render.i_height;
203     p_vout->output.i_aspect = p_vout->output.i_width
204                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
205
206     p_vout->output.i_rmask = 0xff0000;
207     p_vout->output.i_gmask = 0x00ff00;
208     p_vout->output.i_bmask = 0x0000ff;
209
210     /* Try to initialize 1 direct buffer */
211     p_pic = NULL;
212
213     /* Find an empty picture slot */
214     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
215     {
216         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
217         {
218             p_pic = p_vout->p_picture + i_index;
219             break;
220         }
221     }
222
223     /* Allocate the picture */
224     if( p_pic == NULL )
225     {
226         return VLC_EGENERIC;
227     }
228
229     vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
230                           p_vout->output.i_width, p_vout->output.i_height,
231                           p_vout->output.i_aspect );
232
233     if( p_pic->i_planes == 0 )
234     {
235         return VLC_EGENERIC;
236     }
237
238     p_pic->i_status = DESTROYED_PICTURE;
239     p_pic->i_type   = DIRECT_PICTURE;
240
241     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
242     I_OUTPUTPICTURES++;
243     return VLC_SUCCESS;
244 }
245
246 /*****************************************************************************
247  * Destroy: destroy video thread
248  *****************************************************************************
249  * Terminate an output method created by Create
250  *****************************************************************************/
251 static void Destroy( vlc_object_t *p_this )
252 {
253     int i_index;
254     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
255
256     for( i_index = I_OUTPUTPICTURES-1; i_index >= 0; i_index-- )
257     {
258         free( PP_OUTPUTPICTURE[ i_index ]->p_data );
259     }
260
261     /* Destroy structure */
262     image_HandlerDelete( p_vout->p_sys->p_image );
263     FREENULL( p_vout->p_sys->psz_prefix );
264     FREENULL( p_vout->p_sys->psz_format );
265     FREENULL( p_vout->p_sys );
266 }
267
268 /*****************************************************************************
269  * Display: displays previously rendered output
270  *****************************************************************************
271  * This function copies the rendered picture into our circular buffer.
272  *****************************************************************************/
273 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
274 {
275     video_format_t fmt_in, fmt_out;
276
277     char *psz_filename;
278     char *psz_prefix;
279     char *psz_tmp;
280
281     memset( &fmt_in, 0, sizeof( fmt_in ) );
282     memset( &fmt_out, 0, sizeof( fmt_out ) );
283
284     if( p_vout->p_sys->i_frames % p_vout->p_sys->i_ratio != 0 )
285     {
286         p_vout->p_sys->i_frames++;
287         return;
288     }
289     p_vout->p_sys->i_frames++;
290
291     fmt_in.i_chroma = p_vout->render.i_chroma;
292     fmt_in.i_width = p_vout->render.i_width;
293     fmt_in.i_height = p_vout->render.i_height;
294
295     fmt_out.i_width = (p_vout->p_sys->i_width > 0) ? p_vout->p_sys->i_width :
296                                                    p_vout->render.i_width;
297     fmt_out.i_height = (p_vout->p_sys->i_height > 0) ? p_vout->p_sys->i_height :
298                                                      p_vout->render.i_height;
299
300     if( p_vout->p_sys->b_time )
301     {
302         psz_tmp = str_format_time( p_vout->p_sys->psz_prefix );
303         path_sanitize( psz_tmp );
304     }
305     else
306         psz_tmp = p_vout->p_sys->psz_prefix;
307     if( p_vout->p_sys->b_meta )
308     {
309         psz_prefix = str_format_meta( p_vout, psz_tmp );
310         path_sanitize( psz_prefix );
311         if( p_vout->p_sys->b_time )
312             free( psz_tmp );
313     }
314     else
315         psz_prefix = psz_tmp;
316     psz_filename = (char *)malloc( 10 + strlen( psz_prefix )
317                                       + strlen( p_vout->p_sys->psz_format ) );
318     if( p_vout->p_sys->b_replace )
319     {
320         sprintf( psz_filename, "%s.%s", psz_prefix,
321                                         p_vout->p_sys->psz_format );
322     }
323     else
324     {
325         sprintf( psz_filename, "%s%.6i.%s", psz_prefix,
326                                             p_vout->p_sys->i_current,
327                                             p_vout->p_sys->psz_format );
328     }
329     if( p_vout->p_sys->b_time || p_vout->p_sys->b_meta )
330         free( psz_prefix );
331     image_WriteUrl( p_vout->p_sys->p_image, p_pic,
332                     &fmt_in, &fmt_out, psz_filename ) ;
333     free( psz_filename );
334
335     p_vout->p_sys->i_current++;
336
337     return;
338 }
339
340
341 static void End( vout_thread_t *p_vout )
342 {
343 }