1 /*****************************************************************************
2 * snapshot.c : snapshot plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * This module is a pseudo video output that 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 #include <vlc_interface.h>
49 #include <vlc_input.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Create ( vlc_object_t * );
55 static void Destroy ( vlc_object_t * );
57 static int Init ( vout_thread_t * );
58 static void End ( vout_thread_t * );
59 static void Display ( vout_thread_t *, picture_t * );
61 /*****************************************************************************
63 *****************************************************************************/
64 #define WIDTH_TEXT N_( "Snapshot width" )
65 #define WIDTH_LONGTEXT N_( "Width of the snapshot image." )
67 #define HEIGHT_TEXT N_( "Snapshot height" )
68 #define HEIGHT_LONGTEXT N_( "Height of the snapshot image." )
70 #define CHROMA_TEXT N_( "Chroma" )
71 #define CHROMA_LONGTEXT N_( "Output chroma for the snapshot image " \
72 "(a 4 character string, like \"RV32\")." )
74 #define CACHE_TEXT N_( "Cache size (number of images)" )
75 #define CACHE_LONGTEXT N_( "Snapshot cache size (number of images to keep)." )
79 set_description( _( "Snapshot module" ) );
80 set_shortname( _("Snapshot") );
82 set_category( CAT_VIDEO );
83 set_subcategory( SUBCAT_VIDEO_VOUT );
84 set_capability( "video output", 1 );
86 add_integer( "snapshot-width", 320, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, false );
87 add_integer( "snapshot-height", 200, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, false );
88 add_string( "snapshot-chroma", "RV32", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true );
89 add_integer( "snapshot-cache-size", 50, NULL, CACHE_TEXT, CACHE_LONGTEXT, true );
91 set_callbacks( Create, Destroy );
94 /*****************************************************************************
95 * vout_sys_t: video output descriptor
96 *****************************************************************************/
99 snapshot_t **p_list; /* List of available snapshots */
100 int i_index; /* Index of the next available list member */
101 int i_size; /* Size of the cache */
102 int i_datasize; /* Size of an image */
103 input_thread_t *p_input; /* The input thread */
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 )
113 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
115 /* Allocate instance and initialize some members */
116 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
117 if( ! p_vout->p_sys )
120 var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER );
121 var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER );
122 var_Create( p_vout, "snapshot-datasize", VLC_VAR_INTEGER );
123 var_Create( p_vout, "snapshot-cache-size", VLC_VAR_INTEGER );
124 var_Create( p_vout, "snapshot-list-pointer", VLC_VAR_ADDRESS );
126 p_vout->pf_init = Init;
127 p_vout->pf_end = End;
128 p_vout->pf_manage = NULL;
129 p_vout->pf_render = NULL;
130 p_vout->pf_display = Display;
135 /*****************************************************************************
136 * Init: initialize video thread
137 *****************************************************************************/
138 static int Init( vout_thread_t *p_vout )
149 i_width = config_GetInt( p_vout, "snapshot-width" );
150 i_height = config_GetInt( p_vout, "snapshot-height" );
152 psz_chroma = config_GetPsz( p_vout, "snapshot-chroma" );
155 if( strlen( psz_chroma ) < 4 )
157 msg_Err( p_vout, "snapshot-chroma should be 4 characters long" );
160 i_chroma = VLC_FOURCC( psz_chroma[0], psz_chroma[1],
161 psz_chroma[2], psz_chroma[3] );
166 msg_Err( p_vout, "Cannot find chroma information." );
170 I_OUTPUTPICTURES = 0;
172 /* Initialize the output structure */
173 p_vout->output.i_chroma = i_chroma;
174 p_vout->output.pf_setpalette = NULL;
175 p_vout->output.i_width = i_width;
176 p_vout->output.i_height = i_height;
177 p_vout->output.i_aspect = p_vout->output.i_width
178 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
181 /* Define the bitmasks */
184 case VLC_FOURCC( 'R','V','1','5' ):
185 p_vout->output.i_rmask = 0x001f;
186 p_vout->output.i_gmask = 0x03e0;
187 p_vout->output.i_bmask = 0x7c00;
190 case VLC_FOURCC( 'R','V','1','6' ):
191 p_vout->output.i_rmask = 0x001f;
192 p_vout->output.i_gmask = 0x07e0;
193 p_vout->output.i_bmask = 0xf800;
196 case VLC_FOURCC( 'R','V','2','4' ):
197 p_vout->output.i_rmask = 0xff0000;
198 p_vout->output.i_gmask = 0x00ff00;
199 p_vout->output.i_bmask = 0x0000ff;
202 case VLC_FOURCC( 'R','V','3','2' ):
203 p_vout->output.i_rmask = 0xff0000;
204 p_vout->output.i_gmask = 0x00ff00;
205 p_vout->output.i_bmask = 0x0000ff;
209 /* Try to initialize 1 direct buffer */
212 /* Find an empty picture slot */
213 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
215 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
217 p_pic = p_vout->p_picture + i_index;
222 /* Allocate the picture */
228 vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
229 p_vout->output.i_width, p_vout->output.i_height,
230 p_vout->output.i_aspect );
232 if( p_pic->i_planes == 0 )
237 p_pic->i_status = DESTROYED_PICTURE;
238 p_pic->i_type = DIRECT_PICTURE;
240 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
245 /* Get datasize and set variables */
246 i_datasize = i_width * i_height * p_pic->p->i_pixel_pitch;
248 p_vout->p_sys->i_datasize = i_datasize;
249 p_vout->p_sys->i_index = 0;
250 p_vout->p_sys->i_size = config_GetInt( p_vout, "snapshot-cache-size" );
252 if( p_vout->p_sys->i_size < 2 )
254 msg_Err( p_vout, "snapshot-cache-size must be at least 1." );
258 p_vout->p_sys->p_list = malloc( p_vout->p_sys->i_size * sizeof( snapshot_t * ) );
260 if( p_vout->p_sys->p_list == NULL )
263 /* Initialize the structures for the circular buffer */
264 for( i_index = 0; i_index < p_vout->p_sys->i_size; i_index++ )
266 snapshot_t *p_snapshot = malloc( sizeof( snapshot_t ) );
268 if( p_snapshot == NULL )
271 p_snapshot->i_width = i_width;
272 p_snapshot->i_height = i_height;
273 p_snapshot->i_datasize = i_datasize;
274 p_snapshot->date = 0;
275 p_snapshot->p_data = ( char* ) malloc( i_datasize );
276 if( p_snapshot->p_data == NULL )
278 p_vout->p_sys->p_list[i_index] = p_snapshot;
282 var_Set( p_vout, "snapshot-width", val );
283 val.i_int = i_height;
284 var_Set( p_vout, "snapshot-height", val );
285 val.i_int = i_datasize;
286 var_Set( p_vout, "snapshot-datasize", val );
288 val.i_int = p_vout->p_sys->i_size;
289 var_Set( p_vout, "snapshot-cache-size", val );
291 val.p_address = p_vout->p_sys->p_list;
292 var_Set( p_vout, "snapshot-list-pointer", val );
294 /* Get the p_input pointer (to access video times) */
295 p_vout->p_sys->p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
298 if( !p_vout->p_sys->p_input )
301 if( var_Create( p_vout->p_sys->p_input, "snapshot-id", VLC_VAR_INTEGER ) )
303 msg_Err( p_vout, "Cannot create snapshot-id variable in p_input (%d).",
304 p_vout->p_sys->p_input->i_object_id );
308 /* Register the snapshot vout module at the input level */
309 val.i_int = p_vout->i_object_id;
311 if( var_Set( p_vout->p_sys->p_input, "snapshot-id", val ) )
313 msg_Err( p_vout, "Cannot register snapshot-id in p_input (%d).",
314 p_vout->p_sys->p_input->i_object_id );
321 /*****************************************************************************
322 * End: terminate video thread output method
323 *****************************************************************************/
324 static void End( vout_thread_t *p_vout )
329 /*****************************************************************************
330 * Destroy: destroy video thread
331 *****************************************************************************
332 * Terminate an output method created by Create
333 *****************************************************************************/
334 static void Destroy( vlc_object_t *p_this )
336 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
339 var_Destroy( p_vout->p_sys->p_input, "snapshot-id" );
341 vlc_object_release( p_vout->p_sys->p_input );
342 var_Destroy( p_this, "snapshot-width" );
343 var_Destroy( p_this, "snapshot-height" );
344 var_Destroy( p_this, "snapshot-datasize" );
346 for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
348 free( p_vout->p_sys->p_list[ i_index ]->p_data );
350 free( p_vout->p_sys->p_list );
351 /* Destroy structure */
352 free( p_vout->p_sys );
355 /* Return the position in ms from the start of the movie */
356 static mtime_t snapshot_GetMovietime( vout_thread_t *p_vout )
358 input_thread_t* p_input;
362 p_input = p_vout->p_sys->p_input;
366 var_Get( p_input, "time", &val );
368 i_result = val.i_time - p_input->i_pts_delay;
370 return( i_result / 1000 );
373 /*****************************************************************************
374 * Display: displays previously rendered output
375 *****************************************************************************
376 * This function copies the rendered picture into our circular buffer.
377 *****************************************************************************/
378 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
383 i_index = p_vout->p_sys->i_index;
385 p_vout->p_libvlc->pf_memcpy( p_vout->p_sys->p_list[i_index]->p_data,
387 p_vout->p_sys->i_datasize );
389 i_date = snapshot_GetMovietime( p_vout );
391 p_vout->p_sys->p_list[i_index]->date = i_date;
395 if( i_index >= p_vout->p_sys->i_size )
400 p_vout->p_sys->i_index = i_index;