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