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