]> git.sesse.net Git - vlc/blob - modules/video_output/image.c
Change way arguments are given to croppadd module.
[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 );
178         return VLC_EGENERIC;
179     }
180
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;
186
187     return VLC_SUCCESS;
188 }
189
190 /*****************************************************************************
191  * Init: initialize video thread
192  *****************************************************************************/
193 static int Init( vout_thread_t *p_vout )
194 {
195     int i_index;
196     picture_t *p_pic;
197
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;
205
206     p_vout->output.i_rmask = 0xff0000;
207     p_vout->output.i_gmask = 0x00ff00;
208     p_vout->output.i_bmask = 0x0000ff;
209
210     /* Try to initialize 1 direct buffer */
211     p_pic = NULL;
212
213     /* Find an empty picture slot */
214     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
215     {
216         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
217         {
218             p_pic = p_vout->p_picture + i_index;
219             break;
220         }
221     }
222
223     /* Allocate the picture */
224     if( p_pic == NULL )
225     {
226         return VLC_EGENERIC;
227     }
228
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 );
232
233     if( p_pic->i_planes == 0 )
234     {
235         return VLC_EGENERIC;
236     }
237
238     p_pic->i_status = DESTROYED_PICTURE;
239     p_pic->i_type   = DIRECT_PICTURE;
240
241     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
242     I_OUTPUTPICTURES++;
243     return VLC_SUCCESS;
244 }
245
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 )
252 {
253     int i_index;
254     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
255
256     for( i_index = I_OUTPUTPICTURES-1; i_index >= 0; i_index-- )
257     {
258         free( PP_OUTPUTPICTURE[ i_index ]->p_data );
259     }
260
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 );
266 }
267
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 )
274 {
275     video_format_t fmt_in, fmt_out;
276
277     char *psz_filename;
278     char *psz_prefix;
279     char *psz_tmp;
280
281     memset( &fmt_in, 0, sizeof( fmt_in ) );
282     memset( &fmt_out, 0, sizeof( fmt_out ) );
283
284     if( p_vout->p_sys->i_frames % p_vout->p_sys->i_ratio != 0 )
285     {
286         p_vout->p_sys->i_frames++;
287         return;
288     }
289     p_vout->p_sys->i_frames++;
290
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;
294
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;
299
300     if( p_vout->p_sys->b_time )
301     {
302         psz_tmp = str_format_time( p_vout->p_sys->psz_prefix );
303         path_sanitize( psz_tmp );
304     }
305     else
306         psz_tmp = p_vout->p_sys->psz_prefix;
307     if( p_vout->p_sys->b_meta )
308     {
309         psz_prefix = str_format_meta( p_vout, psz_tmp );
310         path_sanitize( psz_prefix );
311         if( p_vout->p_sys->b_time )
312             free( psz_tmp );
313     }
314     else
315         psz_prefix = psz_tmp;
316     psz_filename = (char *)malloc( 10 + strlen( psz_prefix )
317                                       + strlen( p_vout->p_sys->psz_format ) );
318     if( !psz_filename )
319         return;
320
321     if( p_vout->p_sys->b_replace )
322     {
323         sprintf( psz_filename, "%s.%s", psz_prefix,
324                                         p_vout->p_sys->psz_format );
325     }
326     else
327     {
328         sprintf( psz_filename, "%s%.6i.%s", psz_prefix,
329                                             p_vout->p_sys->i_current,
330                                             p_vout->p_sys->psz_format );
331     }
332     if( p_vout->p_sys->b_time || p_vout->p_sys->b_meta )
333         free( psz_prefix );
334     image_WriteUrl( p_vout->p_sys->p_image, p_pic,
335                     &fmt_in, &fmt_out, psz_filename ) ;
336     free( psz_filename );
337
338     p_vout->p_sys->i_current++;
339
340     return;
341 }
342
343
344 static void End( vout_thread_t *p_vout )
345 {
346     VLC_UNUSED(p_vout);
347 }