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