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