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 /*****************************************************************************
25 * This module is a pseudo video output the offers the possibility to
26 * keep a cache of low-res snapshots.
27 * The snapshot structure is defined in include/snapshot.h
28 * In order to access the current snapshot cache, object variables are used:
29 * snapshot-list-pointer : the pointer on the first element in the list
30 * snapshot-datasize : size of a snapshot
31 * (also available in snapshot_t->i_datasize)
32 * snapshot-cache-size : size of the cache list
34 * It is used for the moment by the CORBA module and a specialized
36 *****************************************************************************/
38 /*****************************************************************************
40 *****************************************************************************/
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Create ( vlc_object_t * );
52 static void Destroy ( vlc_object_t * );
54 static int Init ( vout_thread_t * );
55 static void Display ( vout_thread_t *, picture_t * );
57 /*****************************************************************************
59 *****************************************************************************/
60 #define WIDTH_TEXT N_( "snapshot width" )
61 #define WIDTH_LONGTEXT N_( "Set the width of the snapshot image." )
63 #define HEIGHT_TEXT N_( "snapshot height" )
64 #define HEIGHT_LONGTEXT N_( "Set the height of the snapshot image." )
66 #define CHROMA_TEXT N_( "chroma" )
67 #define CHROMA_LONGTEXT N_( "Set the desired chroma for the snapshot image (a 4 character string)." )
69 #define CACHE_TEXT N_( "cache size (number of images)" )
70 #define CACHE_LONGTEXT N_( "Set the cache size (number of images to keep)." )
74 set_description( _( "snapshot module" ) );
75 set_shortname( N_("Snapshot") );
77 set_category( CAT_VIDEO );
78 set_subcategory( SUBCAT_VIDEO_VOUT );
79 set_capability( "video output", 1 );
81 add_integer( "snapshot-width", 320, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
82 add_integer( "snapshot-height", 200, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
83 add_string( "snapshot-chroma", "RV32", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, VLC_TRUE );
84 add_integer( "snapshot-cache-size", 50, NULL, CACHE_TEXT, CACHE_LONGTEXT, VLC_TRUE );
86 set_callbacks( Create, Destroy );
89 /*****************************************************************************
90 * vout_sys_t: video output descriptor
91 *****************************************************************************/
94 snapshot_t **p_list; /* List of available snapshots */
95 int i_index; /* Index of the next available list member */
96 int i_size; /* Size of the cache */
97 int i_datasize; /* Size of an image */
98 input_thread_t *p_input; /* The input thread */
101 /*****************************************************************************
102 * Create: allocates video thread
103 *****************************************************************************
104 * This function allocates and initializes a vout method.
105 *****************************************************************************/
106 static int Create( vlc_object_t *p_this )
108 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
110 /* Allocate instance and initialize some members */
111 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
112 if( ! p_vout->p_sys )
115 var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER );
116 var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER );
117 var_Create( p_vout, "snapshot-datasize", VLC_VAR_INTEGER );
118 var_Create( p_vout, "snapshot-cache-size", VLC_VAR_INTEGER );
119 var_Create( p_vout, "snapshot-list-pointer", VLC_VAR_ADDRESS );
121 p_vout->pf_init = Init;
122 p_vout->pf_end = NULL;
123 p_vout->pf_manage = NULL;
124 p_vout->pf_render = NULL;
125 p_vout->pf_display = Display;
130 /*****************************************************************************
131 * Init: initialize video thread
132 *****************************************************************************/
133 static int Init( vout_thread_t *p_vout )
144 i_width = config_GetInt( p_vout, "snapshot-width" );
145 i_height = config_GetInt( p_vout, "snapshot-height" );
147 psz_chroma = config_GetPsz( p_vout, "snapshot-chroma" );
150 if( strlen( psz_chroma ) < 4 )
152 msg_Err( p_vout, "snapshot-chroma should be 4 characters long." );
155 i_chroma = VLC_FOURCC( psz_chroma[0], psz_chroma[1],
156 psz_chroma[2], psz_chroma[3] );
161 msg_Err( p_vout, "Cannot find chroma information." );
165 I_OUTPUTPICTURES = 0;
167 /* Initialize the output structure */
168 p_vout->output.i_chroma = i_chroma;
169 p_vout->output.pf_setpalette = NULL;
170 p_vout->output.i_width = i_width;
171 p_vout->output.i_height = i_height;
172 p_vout->output.i_aspect = p_vout->output.i_width
173 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
176 /* Define the bitmasks */
179 case VLC_FOURCC( 'R','V','1','5' ):
180 p_vout->output.i_rmask = 0x001f;
181 p_vout->output.i_gmask = 0x03e0;
182 p_vout->output.i_bmask = 0x7c00;
185 case VLC_FOURCC( 'R','V','1','6' ):
186 p_vout->output.i_rmask = 0x001f;
187 p_vout->output.i_gmask = 0x07e0;
188 p_vout->output.i_bmask = 0xf800;
191 case VLC_FOURCC( 'R','V','2','4' ):
192 p_vout->output.i_rmask = 0xff0000;
193 p_vout->output.i_gmask = 0x00ff00;
194 p_vout->output.i_bmask = 0x0000ff;
197 case VLC_FOURCC( 'R','V','3','2' ):
198 p_vout->output.i_rmask = 0xff0000;
199 p_vout->output.i_gmask = 0x00ff00;
200 p_vout->output.i_bmask = 0x0000ff;
204 /* Try to initialize 1 direct buffer */
207 /* Find an empty picture slot */
208 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
210 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
212 p_pic = p_vout->p_picture + i_index;
217 /* Allocate the picture */
223 vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
224 p_vout->output.i_width, p_vout->output.i_height,
225 p_vout->output.i_aspect );
227 if( p_pic->i_planes == 0 )
232 p_pic->i_status = DESTROYED_PICTURE;
233 p_pic->i_type = DIRECT_PICTURE;
235 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
240 /* Get datasize and set variables */
241 i_datasize = i_width * i_height * p_pic->p->i_pixel_pitch;
243 p_vout->p_sys->i_datasize = i_datasize;
244 p_vout->p_sys->i_index = 0;
245 p_vout->p_sys->i_size = config_GetInt( p_vout, "snapshot-cache-size" );
247 if( p_vout->p_sys->i_size < 2 )
249 msg_Err( p_vout, "snapshot-cache-size must be at least 1." );
253 p_vout->p_sys->p_list = malloc( p_vout->p_sys->i_size * sizeof( snapshot_t * ) );
255 if( p_vout->p_sys->p_list == NULL )
258 /* Initialize the structures for the circular buffer */
259 for( i_index = 0; i_index < p_vout->p_sys->i_size; i_index++ )
261 snapshot_t *p_snapshot = malloc( sizeof( snapshot_t ) );
263 if( p_snapshot == NULL )
266 p_snapshot->i_width = i_width;
267 p_snapshot->i_height = i_height;
268 p_snapshot->i_datasize = i_datasize;
269 p_snapshot->date = 0;
270 p_snapshot->p_data = ( char* ) malloc( i_datasize );
271 if( p_snapshot->p_data == NULL )
273 p_vout->p_sys->p_list[i_index] = p_snapshot;
277 var_Set( p_vout, "snapshot-width", val );
278 val.i_int = i_height;
279 var_Set( p_vout, "snapshot-height", val );
280 val.i_int = i_datasize;
281 var_Set( p_vout, "snapshot-datasize", val );
283 val.i_int = p_vout->p_sys->i_size;
284 var_Set( p_vout, "snapshot-cache-size", val );
286 val.p_address = p_vout->p_sys->p_list;
287 var_Set( p_vout, "snapshot-list-pointer", val );
289 /* Get the p_input pointer (to access video times) */
290 p_vout->p_sys->p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
293 if( !p_vout->p_sys->p_input )
296 if( var_Create( p_vout->p_sys->p_input, "snapshot-id", VLC_VAR_INTEGER ) )
298 msg_Err( p_vout, "Cannot create snapshot-id variable in p_input (%d).",
299 p_vout->p_sys->p_input->i_object_id );
303 /* Register the snapshot vout module at the input level */
304 val.i_int = p_vout->i_object_id;
306 if( var_Set( p_vout->p_sys->p_input, "snapshot-id", val ) )
308 msg_Err( p_vout, "Cannot register snapshot-id in p_input (%d).",
309 p_vout->p_sys->p_input->i_object_id );
316 /*****************************************************************************
317 * Destroy: destroy video thread
318 *****************************************************************************
319 * Terminate an output method created by Create
320 *****************************************************************************/
321 static void Destroy( vlc_object_t *p_this )
323 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
327 vlc_object_release( p_vout->p_sys->p_input );
328 var_Destroy( p_this, "snapshot-width" );
329 var_Destroy( p_this, "snapshot-height" );
330 var_Destroy( p_this, "snapshot-datasize" );
332 p_vlc = vlc_object_find( p_this, VLC_OBJECT_ROOT, FIND_PARENT );
335 /* UnRegister the snapshot vout module at the root level */
336 /* var_Destroy (p_vlc, "snapshot-id"); */
337 var_Destroy( p_this->p_libvlc, "snapshot-id" );
338 vlc_object_release( p_vlc );
341 for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
343 free( p_vout->p_sys->p_list[ i_index ]->p_data );
345 free( p_vout->p_sys->p_list );
346 /* Destroy structure */
347 free( p_vout->p_sys );
350 /* Return the position in ms from the start of the movie */
351 static mtime_t snapshot_GetMovietime( vout_thread_t *p_vout )
353 input_thread_t* p_input;
357 p_input = p_vout->p_sys->p_input;
361 var_Get( p_input, "time", &val );
363 i_result = val.i_time - p_input->i_pts_delay;
365 return( i_result / 1000 );
368 /*****************************************************************************
369 * Display: displays previously rendered output
370 *****************************************************************************
371 * This function copies the rendered picture into our circular buffer.
372 *****************************************************************************/
373 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
378 i_index = p_vout->p_sys->i_index;
380 p_vout->p_vlc->pf_memcpy( p_vout->p_sys->p_list[i_index]->p_data,
382 p_vout->p_sys->i_datasize );
384 i_date = snapshot_GetMovietime( p_vout );
386 p_vout->p_sys->p_list[i_index]->date = i_date;
390 if( i_index >= p_vout->p_sys->i_size )
395 p_vout->p_sys->i_index = i_index;