1 /*****************************************************************************
2 * snapshot.c : snapshot plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
7 * Authors: Olivier Aubert <oaubert@lisi.univ-lyon1.fr>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Create ( vlc_object_t * );
38 static void Destroy ( vlc_object_t * );
40 static int Init ( vout_thread_t * );
41 static void Display ( vout_thread_t *, picture_t * );
43 /*****************************************************************************
45 *****************************************************************************/
46 #define WIDTH_TEXT N_( "snapshot width" )
47 #define WIDTH_LONGTEXT N_( "Set the width of the snapshot image" )
49 #define HEIGHT_TEXT N_( "snapshot height" )
50 #define HEIGHT_LONGTEXT N_( "Set the height of the snapshot image" )
52 #define CHROMA_TEXT N_( "chroma" )
53 #define CHROMA_LONGTEXT N_( "Set the desired chroma for the snapshot image (a 4 character string)" )
55 #define CACHE_TEXT N_( "cache size (number of images)" )
56 #define CACHE_LONGTEXT N_( "Set the cache size (number of images to keep)" )
60 set_description( _( "snapshot module" ) );
61 set_capability( "video output", 0 );
63 add_integer( "snapshot-width", 320, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
64 add_integer( "snapshot-height", 200, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
65 add_string( "snapshot-chroma", "RV32", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, VLC_TRUE );
66 add_integer( "snapshot-cache-size", 50, NULL, CACHE_TEXT, CACHE_LONGTEXT, VLC_TRUE );
68 set_callbacks( Create, Destroy );
71 /*****************************************************************************
72 * vout_sys_t: video output descriptor
73 *****************************************************************************/
76 snapshot_t **p_list; /* List of available snapshots */
77 int i_index; /* Index of the next available list member */
78 int i_size; /* Size of the cache */
79 int i_datasize; /* Size of an image */
80 input_thread_t * p_input; /* The input thread */
83 /*****************************************************************************
84 * Create: allocates video thread
85 *****************************************************************************
86 * This function allocates and initializes a vout method.
87 *****************************************************************************/
88 static int Create( vlc_object_t *p_this )
90 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
92 /* Allocate instance and initialize some members */
93 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
97 var_Create( p_this, "snapshot-width", VLC_VAR_INTEGER );
98 var_Create( p_this, "snapshot-height", VLC_VAR_INTEGER );
99 var_Create( p_this, "snapshot-datasize", VLC_VAR_INTEGER );
100 var_Create( p_this, "snapshot-cache-size", VLC_VAR_INTEGER );
101 var_Create( p_this, "snapshot-list-pointer", VLC_VAR_ADDRESS );
103 p_vout->pf_init = Init;
104 p_vout->pf_end = NULL;
105 p_vout->pf_manage = NULL;
106 p_vout->pf_render = NULL;
107 p_vout->pf_display = Display;
112 /*****************************************************************************
113 * Init: initialize video thread
114 *****************************************************************************/
115 static int Init( vout_thread_t *p_vout )
126 i_width = config_GetInt( p_vout, "snapshot-width" );
127 i_height = config_GetInt( p_vout, "snapshot-height" );
129 psz_chroma = config_GetPsz( p_vout, "snapshot-chroma" );
132 if ( strlen( psz_chroma ) < 4 )
135 "snapshot-chroma should be 4 characters long.\n" );
138 i_chroma = VLC_FOURCC( psz_chroma[0],
146 msg_Err( p_vout, "Cannot find chroma information.\n" );
150 I_OUTPUTPICTURES = 0;
152 /* Initialize the output structure */
153 p_vout->output.i_chroma = i_chroma;
154 p_vout->output.pf_setpalette = NULL;
155 p_vout->output.i_width = i_width;
156 p_vout->output.i_height = i_height;
157 p_vout->output.i_aspect = p_vout->output.i_width
158 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
161 /* Define the bitmasks */
164 case VLC_FOURCC( 'R','V','1','5' ):
165 p_vout->output.i_rmask = 0x001f;
166 p_vout->output.i_gmask = 0x03e0;
167 p_vout->output.i_bmask = 0x7c00;
170 case VLC_FOURCC( 'R','V','1','6' ):
171 p_vout->output.i_rmask = 0x001f;
172 p_vout->output.i_gmask = 0x07e0;
173 p_vout->output.i_bmask = 0xf800;
176 case VLC_FOURCC( 'R','V','2','4' ):
177 p_vout->output.i_rmask = 0xff0000;
178 p_vout->output.i_gmask = 0x00ff00;
179 p_vout->output.i_bmask = 0x0000ff;
182 case VLC_FOURCC( 'R','V','3','2' ):
183 p_vout->output.i_rmask = 0xff0000;
184 p_vout->output.i_gmask = 0x00ff00;
185 p_vout->output.i_bmask = 0x0000ff;
189 /* Try to initialize 1 direct buffer */
192 /* Find an empty picture slot */
193 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
195 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
197 p_pic = p_vout->p_picture + i_index;
202 /* Allocate the picture */
208 vout_AllocatePicture( p_vout, p_pic, p_vout->output.i_chroma,
209 p_vout->output.i_width, p_vout->output.i_height,
210 p_vout->output.i_aspect );
212 if( p_pic->i_planes == 0 )
217 p_pic->i_status = DESTROYED_PICTURE;
218 p_pic->i_type = DIRECT_PICTURE;
220 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
225 /* Get datasize and set variables */
226 i_datasize = i_width * i_height * p_pic->p->i_pixel_pitch;
228 p_vout->p_sys->i_datasize = i_datasize;
229 p_vout->p_sys->i_index = 0;
230 p_vout->p_sys->i_size = config_GetInt( p_vout, "snapshot-cache-size" );
232 if( p_vout->p_sys->i_size < 2 )
234 msg_Err( p_vout, "snapshot-cache-size must be at least 1." );
238 p_vout->p_sys->p_list = ( snapshot_t ** )malloc( p_vout->p_sys->i_size * sizeof( snapshot_t * ) );
240 if( p_vout->p_sys->p_list == NULL )
243 /* Initialize the structures for the circular buffer */
244 for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
246 snapshot_t *p_snapshot;
247 p_snapshot = ( snapshot_t * )malloc( sizeof( snapshot_t ) );
249 if( p_snapshot == NULL )
252 p_snapshot->i_width = i_width;
253 p_snapshot->i_height = i_height;
254 p_snapshot->i_datasize = i_datasize;
255 p_snapshot->date = 0;
256 p_snapshot->p_data = ( char* ) malloc( i_datasize );
257 if( p_snapshot->p_data == NULL )
259 p_vout->p_sys->p_list[ i_index ] = p_snapshot;
263 var_Set( p_vout, "snapshot-width", val );
264 val.i_int = i_height;
265 var_Set( p_vout, "snapshot-height", val );
266 val.i_int = i_datasize;
267 var_Set( p_vout, "snapshot-datasize", val );
269 val.i_int = p_vout->p_sys->i_size;
270 var_Set( p_vout, "snapshot-cache-size", val );
272 val.p_address = p_vout->p_sys->p_list;
273 var_Set( p_vout, "snapshot-list-pointer", val );
275 /* Get the p_input pointer (to access video times) */
276 p_vout->p_sys->p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
279 if( ! p_vout->p_sys->p_input )
282 if( var_Create( p_vout->p_sys->p_input, "snapshot-id", VLC_VAR_INTEGER ) != VLC_SUCCESS )
284 msg_Err( p_vout, "Cannot create snapshot-id variable in p_input (%d).", p_vout->p_sys->p_input->i_object_id );
288 /* Register the snapshot vout module at the input level */
289 val.i_int = p_vout->i_object_id;
291 if( var_Set( p_vout->p_sys->p_input, "snapshot-id", val ) != VLC_SUCCESS )
293 msg_Err( p_vout, "Cannot register snapshot-id in p_input (%d).",
294 p_vout->p_sys->p_input->i_object_id );
301 /*****************************************************************************
302 * Destroy: destroy video thread
303 *****************************************************************************
304 * Terminate an output method created by Create
305 *****************************************************************************/
306 static void Destroy( vlc_object_t *p_this )
308 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
312 vlc_object_release( p_vout->p_sys->p_input );
313 var_Destroy( p_this, "snapshot-width" );
314 var_Destroy( p_this, "snapshot-height" );
315 var_Destroy( p_this, "snapshot-datasize" );
317 p_vlc = vlc_object_find( p_this, VLC_OBJECT_ROOT, FIND_PARENT );
320 /* UnRegister the snapshot vout module at the root level */
321 /* var_Destroy (p_vlc, "snapshot-id"); */
322 var_Destroy( p_this->p_libvlc, "snapshot-id" );
323 vlc_object_release( p_vlc );
326 for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
328 free( p_vout->p_sys->p_list[ i_index ]->p_data );
330 free( p_vout->p_sys->p_list );
331 /* Destroy structure */
332 free( p_vout->p_sys );
335 /* Return the position in ms from the start of the movie */
336 mtime_t snapshot_GetMovietime( vout_thread_t *p_vout )
338 input_thread_t* p_input;
342 p_input = p_vout->p_sys->p_input;
346 var_Get( p_input, "time", &val );
352 /* Either we are at the start, or (more probably) the demuxer
353 does not support the DEMUX_GET_TIME call. Try to fallback to
355 stream_position_t pos;
357 input_Tell( p_input, &pos );
358 result = pos.i_mux_rate * 50 * pos.i_tell;
360 return( result / 1000 );
363 /*****************************************************************************
364 * Display: displays previously rendered output
365 *****************************************************************************
366 * This function copies the rendered picture into our circular buffer.
367 *****************************************************************************/
368 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
373 i_index = p_vout->p_sys->i_index;
375 p_vout->p_vlc->pf_memcpy(
376 p_vout->p_sys->p_list[i_index]->p_data,
378 p_vout->p_sys->i_datasize );
380 date = snapshot_GetMovietime( p_vout );
382 p_vout->p_sys->p_list[ i_index ]->date = date;
386 if( i_index >= p_vout->p_sys->i_size )
391 p_vout->p_sys->i_index = i_index;