image vout: assigning -1 to an unsigned int is not that clever.
[vlc.git] / 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_string_list( psz_format_list, psz_format_list_text, 0 );
90     add_integer( CFG_PREFIX "width", 0, NULL,
91                  WIDTH_TEXT, WIDTH_LONGTEXT, VLC_TRUE );
92         add_deprecated_alias( "image-width" ); /* since 0.9.0 */
93     add_integer( CFG_PREFIX "height", 0, NULL,
94                  HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_TRUE );
95         add_deprecated_alias( "image-height" ); /* since 0.9.0 */
96     add_integer( CFG_PREFIX "ratio", 3, NULL,
97                  RATIO_TEXT, RATIO_LONGTEXT, VLC_FALSE );
98     add_string(  CFG_PREFIX "prefix", "img", NULL,
99                  PREFIX_TEXT, PREFIX_LONGTEXT, VLC_FALSE );
100     add_bool(    CFG_PREFIX "replace", 0, NULL,
101                  REPLACE_TEXT, REPLACE_LONGTEXT, VLC_FALSE );
102     set_callbacks( Create, Destroy );
103 vlc_module_end();
104
105 static const char *ppsz_vout_options[] = {
106     "format", "width", "height", "ratio", "prefix", "replace", NULL
107 };
108
109 /*****************************************************************************
110  * vout_sys_t: video output descriptor
111  *****************************************************************************/
112 struct vout_sys_t
113 {
114     char        *psz_prefix;          /* Prefix */
115     char        *psz_format;          /* Format */
116     int         i_ratio;         /* Image ratio */
117
118     unsigned int i_width;        /* Image width */
119     unsigned int i_height;      /* Image height */
120
121     int         i_current;     /* Current image */
122     int         i_frames;   /* Number of frames */
123
124     vlc_bool_t  b_replace;
125
126     vlc_bool_t b_time;
127     vlc_bool_t b_meta;
128
129     image_handler_t *p_image;
130 };
131
132 /*****************************************************************************
133  * Create: allocates video thread
134  *****************************************************************************
135  * This function allocates and initializes a vout method.
136  *****************************************************************************/
137 static int Create( vlc_object_t *p_this )
138 {
139     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
140
141     /* Allocate instance and initialize some members */
142     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
143     if( ! p_vout->p_sys )
144         return VLC_ENOMEM;
145
146     config_ChainParse( p_vout, CFG_PREFIX, ppsz_vout_options,
147                        p_vout->p_cfg );
148
149     p_vout->p_sys->psz_prefix =
150             var_CreateGetString( p_this, CFG_PREFIX "prefix" );
151     p_vout->p_sys->b_time = strchr( p_vout->p_sys->psz_prefix, '%' )
152                             ? VLC_TRUE : VLC_FALSE;
153     p_vout->p_sys->b_meta = strchr( p_vout->p_sys->psz_prefix, '$' )
154                             ? VLC_TRUE : VLC_FALSE;
155     p_vout->p_sys->psz_format =
156             var_CreateGetString( p_this, CFG_PREFIX "format" );
157     p_vout->p_sys->i_width =
158             var_CreateGetInteger( p_this, CFG_PREFIX "width" );
159     p_vout->p_sys->i_height =
160             var_CreateGetInteger( p_this, CFG_PREFIX "height" );
161     p_vout->p_sys->i_ratio =
162             var_CreateGetInteger( p_this, CFG_PREFIX "ratio" );
163     p_vout->p_sys->b_replace =
164             var_CreateGetBool( p_this, CFG_PREFIX "replace" );
165     p_vout->p_sys->i_current = 0;
166     p_vout->p_sys->p_image = image_HandlerCreate( p_vout );
167
168     if( !p_vout->p_sys->p_image )
169     {
170         msg_Err( p_this, "unable to create image handler") ;
171         FREENULL( p_vout->p_sys->psz_prefix );
172         FREENULL( p_vout->p_sys );
173         return VLC_EGENERIC;
174     }
175
176     p_vout->pf_init = Init;
177     p_vout->pf_end = End;
178     p_vout->pf_manage = NULL;
179     p_vout->pf_render = Display;
180     p_vout->pf_display = NULL;
181
182     return VLC_SUCCESS;
183 }
184
185 /*****************************************************************************
186  * Init: initialize video thread
187  *****************************************************************************/
188 static int Init( vout_thread_t *p_vout )
189 {
190     int i_index;
191     picture_t *p_pic;
192
193     /* Initialize the output structure */
194     p_vout->output.i_chroma = p_vout->render.i_chroma;
195     p_vout->output.pf_setpalette = NULL;
196     p_vout->output.i_width = p_vout->render.i_width;
197     p_vout->output.i_height = p_vout->render.i_height;
198     p_vout->output.i_aspect = p_vout->output.i_width
199                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
200
201     p_vout->output.i_rmask = 0xff0000;
202     p_vout->output.i_gmask = 0x00ff00;
203     p_vout->output.i_bmask = 0x0000ff;
204
205     /* Try to initialize 1 direct buffer */
206     p_pic = NULL;
207
208     /* Find an empty picture slot */
209     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
210     {
211         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
212         {
213             p_pic = p_vout->p_picture + i_index;
214             break;
215         }
216     }
217
218     /* Allocate the picture */
219     if( p_pic == NULL )
220     {
221         return VLC_EGENERIC;
222     }
223
224     vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
225                           p_vout->output.i_width, p_vout->output.i_height,
226                           p_vout->output.i_aspect );
227
228     if( p_pic->i_planes == 0 )
229     {
230         return VLC_EGENERIC;
231     }
232
233     p_pic->i_status = DESTROYED_PICTURE;
234     p_pic->i_type   = DIRECT_PICTURE;
235
236     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
237     I_OUTPUTPICTURES++;
238     return VLC_SUCCESS;
239 }
240
241 /*****************************************************************************
242  * Destroy: destroy video thread
243  *****************************************************************************
244  * Terminate an output method created by Create
245  *****************************************************************************/
246 static void Destroy( vlc_object_t *p_this )
247 {
248     int i_index;
249     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
250
251     for( i_index = I_OUTPUTPICTURES-1; i_index >= 0; i_index-- )
252     {
253         free( PP_OUTPUTPICTURE[ i_index ]->p_data );
254     }
255
256     /* Destroy structure */
257     image_HandlerDelete( p_vout->p_sys->p_image );
258     FREENULL( p_vout->p_sys->psz_prefix );
259     FREENULL( p_vout->p_sys->psz_format );
260     FREENULL( p_vout->p_sys );
261 }
262
263 /*****************************************************************************
264  * Display: displays previously rendered output
265  *****************************************************************************
266  * This function copies the rendered picture into our circular buffer.
267  *****************************************************************************/
268 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
269 {
270     video_format_t fmt_in, fmt_out;
271
272     char *psz_filename;
273     char *psz_prefix;
274     char *psz_tmp;
275
276     memset( &fmt_in, 0, sizeof( fmt_in ) );
277     memset( &fmt_out, 0, sizeof( fmt_out ) );
278
279     if( p_vout->p_sys->i_frames % p_vout->p_sys->i_ratio != 0 )
280     {
281         p_vout->p_sys->i_frames++;
282         return;
283     }
284     p_vout->p_sys->i_frames++;
285
286     fmt_in.i_chroma = p_vout->render.i_chroma;
287     fmt_in.i_width = p_vout->render.i_width;
288     fmt_in.i_height = p_vout->render.i_height;
289
290     fmt_out.i_width = (p_vout->p_sys->i_width > 0) ? p_vout->p_sys->i_width :
291                                                    p_vout->render.i_width;
292     fmt_out.i_height = (p_vout->p_sys->i_height > 0) ? p_vout->p_sys->i_height :
293                                                      p_vout->render.i_height;
294
295     if( p_vout->p_sys->b_time )
296     {
297         psz_tmp = str_format_time( p_vout->p_sys->psz_prefix );
298         path_sanitize( psz_tmp );
299     }
300     else
301         psz_tmp = p_vout->p_sys->psz_prefix;
302     if( p_vout->p_sys->b_meta )
303     {
304         psz_prefix = str_format_meta( p_vout, psz_tmp );
305         path_sanitize( psz_prefix );
306         if( p_vout->p_sys->b_time )
307             free( psz_tmp );
308     }
309     else
310         psz_prefix = psz_tmp;
311     psz_filename = (char *)malloc( 10 + strlen( psz_prefix )
312                                       + strlen( p_vout->p_sys->psz_format ) );
313     if( p_vout->p_sys->b_replace )
314     {
315         sprintf( psz_filename, "%s.%s", psz_prefix,
316                                         p_vout->p_sys->psz_format );
317     }
318     else
319     {
320         sprintf( psz_filename, "%s%.6i.%s", psz_prefix,
321                                             p_vout->p_sys->i_current,
322                                             p_vout->p_sys->psz_format );
323     }
324     if( p_vout->p_sys->b_time || p_vout->p_sys->b_meta )
325         free( psz_prefix );
326     image_WriteUrl( p_vout->p_sys->p_image, p_pic,
327                     &fmt_in, &fmt_out, psz_filename ) ;
328     free( psz_filename );
329
330     p_vout->p_sys->i_current++;
331
332     return;
333 }
334
335
336 static void End( vout_thread_t *p_vout )
337 {
338 }