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