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