1 /*****************************************************************************
2 * image.c : image video output
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_interface.h>
37 #include "vlc_image.h"
38 #include "vlc_strings.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Create ( vlc_object_t * );
44 static void Destroy ( vlc_object_t * );
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 * );
50 /*****************************************************************************
52 *****************************************************************************/
53 #define FORMAT_TEXT N_( "Image format" )
54 #define FORMAT_LONGTEXT N_( "Format of the output images (png or jpg)." )
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 " \
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 " \
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." )
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\" "\
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." )
80 static const char *const psz_format_list[] = { "png", "jpeg" };
81 static const char *const psz_format_list_text[] = { "PNG", "JPEG" };
83 #define CFG_PREFIX "image-out-"
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 );
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 );
110 static const char *const ppsz_vout_options[] = {
111 "format", "width", "height", "ratio", "prefix", "replace", NULL
114 /*****************************************************************************
115 * vout_sys_t: video output descriptor
116 *****************************************************************************/
119 char *psz_prefix; /* Prefix */
120 char *psz_format; /* Format */
121 int i_ratio; /* Image ratio */
123 unsigned int i_width; /* Image width */
124 unsigned int i_height; /* Image height */
126 int i_current; /* Current image */
127 int i_frames; /* Number of frames */
134 image_handler_t *p_image;
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 )
144 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
146 /* Allocate instance and initialize some members */
147 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
148 if( ! p_vout->p_sys )
151 config_ChainParse( p_vout, CFG_PREFIX, ppsz_vout_options,
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, '%' )
158 p_vout->p_sys->b_meta = strchr( p_vout->p_sys->psz_prefix, '$' )
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 );
173 if( !p_vout->p_sys->p_image )
175 msg_Err( p_this, "unable to create image handler") ;
176 FREENULL( p_vout->p_sys->psz_prefix );
177 FREENULL( p_vout->p_sys );
181 p_vout->pf_init = Init;
182 p_vout->pf_end = End;
183 p_vout->pf_manage = NULL;
184 p_vout->pf_render = Display;
185 p_vout->pf_display = NULL;
190 /*****************************************************************************
191 * Init: initialize video thread
192 *****************************************************************************/
193 static int Init( vout_thread_t *p_vout )
198 /* Initialize the output structure */
199 p_vout->output.i_chroma = p_vout->render.i_chroma;
200 p_vout->output.pf_setpalette = NULL;
201 p_vout->output.i_width = p_vout->render.i_width;
202 p_vout->output.i_height = p_vout->render.i_height;
203 p_vout->output.i_aspect = p_vout->output.i_width
204 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
206 p_vout->output.i_rmask = 0xff0000;
207 p_vout->output.i_gmask = 0x00ff00;
208 p_vout->output.i_bmask = 0x0000ff;
210 /* Try to initialize 1 direct buffer */
213 /* Find an empty picture slot */
214 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
216 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
218 p_pic = p_vout->p_picture + i_index;
223 /* Allocate the picture */
229 vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
230 p_vout->output.i_width, p_vout->output.i_height,
231 p_vout->output.i_aspect );
233 if( p_pic->i_planes == 0 )
238 p_pic->i_status = DESTROYED_PICTURE;
239 p_pic->i_type = DIRECT_PICTURE;
241 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
246 /*****************************************************************************
247 * Destroy: destroy video thread
248 *****************************************************************************
249 * Terminate an output method created by Create
250 *****************************************************************************/
251 static void Destroy( vlc_object_t *p_this )
254 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
256 for( i_index = I_OUTPUTPICTURES-1; i_index >= 0; i_index-- )
258 free( PP_OUTPUTPICTURE[ i_index ]->p_data );
261 /* Destroy structure */
262 image_HandlerDelete( p_vout->p_sys->p_image );
263 FREENULL( p_vout->p_sys->psz_prefix );
264 FREENULL( p_vout->p_sys->psz_format );
265 FREENULL( p_vout->p_sys );
268 /*****************************************************************************
269 * Display: displays previously rendered output
270 *****************************************************************************
271 * This function copies the rendered picture into our circular buffer.
272 *****************************************************************************/
273 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
275 video_format_t fmt_in, fmt_out;
281 memset( &fmt_in, 0, sizeof( fmt_in ) );
282 memset( &fmt_out, 0, sizeof( fmt_out ) );
284 if( p_vout->p_sys->i_frames % p_vout->p_sys->i_ratio != 0 )
286 p_vout->p_sys->i_frames++;
289 p_vout->p_sys->i_frames++;
291 fmt_in.i_chroma = p_vout->render.i_chroma;
292 fmt_in.i_width = p_vout->render.i_width;
293 fmt_in.i_height = p_vout->render.i_height;
295 fmt_out.i_width = (p_vout->p_sys->i_width > 0) ? p_vout->p_sys->i_width :
296 p_vout->render.i_width;
297 fmt_out.i_height = (p_vout->p_sys->i_height > 0) ? p_vout->p_sys->i_height :
298 p_vout->render.i_height;
300 if( p_vout->p_sys->b_time )
302 psz_tmp = str_format_time( p_vout->p_sys->psz_prefix );
303 path_sanitize( psz_tmp );
306 psz_tmp = p_vout->p_sys->psz_prefix;
307 if( p_vout->p_sys->b_meta )
309 psz_prefix = str_format_meta( p_vout, psz_tmp );
310 path_sanitize( psz_prefix );
311 if( p_vout->p_sys->b_time )
315 psz_prefix = psz_tmp;
316 psz_filename = (char *)malloc( 10 + strlen( psz_prefix )
317 + strlen( p_vout->p_sys->psz_format ) );
321 if( p_vout->p_sys->b_replace )
323 sprintf( psz_filename, "%s.%s", psz_prefix,
324 p_vout->p_sys->psz_format );
328 sprintf( psz_filename, "%s%.6i.%s", psz_prefix,
329 p_vout->p_sys->i_current,
330 p_vout->p_sys->psz_format );
332 if( p_vout->p_sys->b_time || p_vout->p_sys->b_meta )
334 image_WriteUrl( p_vout->p_sys->p_image, p_pic,
335 &fmt_in, &fmt_out, psz_filename ) ;
336 free( psz_filename );
338 p_vout->p_sys->i_current++;
344 static void End( vout_thread_t *p_vout )