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