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