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 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_( "Width of the snapshot image." )
63 #define HEIGHT_TEXT N_( "Snapshot height" )
64 #define HEIGHT_LONGTEXT N_( "Height of the snapshot image." )
66 #define CHROMA_TEXT N_( "Chroma" )
67 #define CHROMA_LONGTEXT N_( "Output chroma for the snapshot image " \
68 "(a 4 character string, like \"RV32\")." )
70 #define CACHE_TEXT N_( "Cache size (number of images)" )
71 #define CACHE_LONGTEXT N_( "Snapshot cache size (number of images to keep)." )
75 set_description( _( "Snapshot module" ) );
76 set_shortname( N_("Snapshot") );
78 set_category( CAT_VIDEO );
79 set_subcategory( SUBCAT_VIDEO_VOUT );
80 set_capability( "video output", 1 );
82 add_integer( "snapshot-width", 320, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
83 add_integer( "snapshot-height", 200, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
84 add_string( "snapshot-chroma", "RV32", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, VLC_TRUE );
85 add_integer( "snapshot-cache-size", 50, NULL, CACHE_TEXT, CACHE_LONGTEXT, VLC_TRUE );
87 set_callbacks( Create, Destroy );
90 /*****************************************************************************
91 * vout_sys_t: video output descriptor
92 *****************************************************************************/
95 snapshot_t **p_list; /* List of available snapshots */
96 int i_index; /* Index of the next available list member */
97 int i_size; /* Size of the cache */
98 int i_datasize; /* Size of an image */
99 input_thread_t *p_input; /* The input thread */
102 /*****************************************************************************
103 * Create: allocates video thread
104 *****************************************************************************
105 * This function allocates and initializes a vout method.
106 *****************************************************************************/
107 static int Create( vlc_object_t *p_this )
109 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
111 /* Allocate instance and initialize some members */
112 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
113 if( ! p_vout->p_sys )
116 var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER );
117 var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER );
118 var_Create( p_vout, "snapshot-datasize", VLC_VAR_INTEGER );
119 var_Create( p_vout, "snapshot-cache-size", VLC_VAR_INTEGER );
120 var_Create( p_vout, "snapshot-list-pointer", VLC_VAR_ADDRESS );
122 p_vout->pf_init = Init;
123 p_vout->pf_end = NULL;
124 p_vout->pf_manage = NULL;
125 p_vout->pf_render = NULL;
126 p_vout->pf_display = Display;
131 /*****************************************************************************
132 * Init: initialize video thread
133 *****************************************************************************/
134 static int Init( vout_thread_t *p_vout )
145 i_width = config_GetInt( p_vout, "snapshot-width" );
146 i_height = config_GetInt( p_vout, "snapshot-height" );
148 psz_chroma = config_GetPsz( p_vout, "snapshot-chroma" );
151 if( strlen( psz_chroma ) < 4 )
153 msg_Err( p_vout, "snapshot-chroma should be 4 characters long" );
156 i_chroma = VLC_FOURCC( psz_chroma[0], psz_chroma[1],
157 psz_chroma[2], psz_chroma[3] );
162 msg_Err( p_vout, "Cannot find chroma information." );
166 I_OUTPUTPICTURES = 0;
168 /* Initialize the output structure */
169 p_vout->output.i_chroma = i_chroma;
170 p_vout->output.pf_setpalette = NULL;
171 p_vout->output.i_width = i_width;
172 p_vout->output.i_height = i_height;
173 p_vout->output.i_aspect = p_vout->output.i_width
174 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
177 /* Define the bitmasks */
180 case VLC_FOURCC( 'R','V','1','5' ):
181 p_vout->output.i_rmask = 0x001f;
182 p_vout->output.i_gmask = 0x03e0;
183 p_vout->output.i_bmask = 0x7c00;
186 case VLC_FOURCC( 'R','V','1','6' ):
187 p_vout->output.i_rmask = 0x001f;
188 p_vout->output.i_gmask = 0x07e0;
189 p_vout->output.i_bmask = 0xf800;
192 case VLC_FOURCC( 'R','V','2','4' ):
193 p_vout->output.i_rmask = 0xff0000;
194 p_vout->output.i_gmask = 0x00ff00;
195 p_vout->output.i_bmask = 0x0000ff;
198 case VLC_FOURCC( 'R','V','3','2' ):
199 p_vout->output.i_rmask = 0xff0000;
200 p_vout->output.i_gmask = 0x00ff00;
201 p_vout->output.i_bmask = 0x0000ff;
205 /* Try to initialize 1 direct buffer */
208 /* Find an empty picture slot */
209 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
211 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
213 p_pic = p_vout->p_picture + i_index;
218 /* Allocate the picture */
224 vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
225 p_vout->output.i_width, p_vout->output.i_height,
226 p_vout->output.i_aspect );
228 if( p_pic->i_planes == 0 )
233 p_pic->i_status = DESTROYED_PICTURE;
234 p_pic->i_type = DIRECT_PICTURE;
236 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
241 /* Get datasize and set variables */
242 i_datasize = i_width * i_height * p_pic->p->i_pixel_pitch;
244 p_vout->p_sys->i_datasize = i_datasize;
245 p_vout->p_sys->i_index = 0;
246 p_vout->p_sys->i_size = config_GetInt( p_vout, "snapshot-cache-size" );
248 if( p_vout->p_sys->i_size < 2 )
250 msg_Err( p_vout, "snapshot-cache-size must be at least 1." );
254 p_vout->p_sys->p_list = malloc( p_vout->p_sys->i_size * sizeof( snapshot_t * ) );
256 if( p_vout->p_sys->p_list == NULL )
259 /* Initialize the structures for the circular buffer */
260 for( i_index = 0; i_index < p_vout->p_sys->i_size; i_index++ )
262 snapshot_t *p_snapshot = malloc( sizeof( snapshot_t ) );
264 if( p_snapshot == NULL )
267 p_snapshot->i_width = i_width;
268 p_snapshot->i_height = i_height;
269 p_snapshot->i_datasize = i_datasize;
270 p_snapshot->date = 0;
271 p_snapshot->p_data = ( char* ) malloc( i_datasize );
272 if( p_snapshot->p_data == NULL )
274 p_vout->p_sys->p_list[i_index] = p_snapshot;
278 var_Set( p_vout, "snapshot-width", val );
279 val.i_int = i_height;
280 var_Set( p_vout, "snapshot-height", val );
281 val.i_int = i_datasize;
282 var_Set( p_vout, "snapshot-datasize", val );
284 val.i_int = p_vout->p_sys->i_size;
285 var_Set( p_vout, "snapshot-cache-size", val );
287 val.p_address = p_vout->p_sys->p_list;
288 var_Set( p_vout, "snapshot-list-pointer", val );
290 /* Get the p_input pointer (to access video times) */
291 p_vout->p_sys->p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
294 if( !p_vout->p_sys->p_input )
297 if( var_Create( p_vout->p_sys->p_input, "snapshot-id", VLC_VAR_INTEGER ) )
299 msg_Err( p_vout, "Cannot create snapshot-id variable in p_input (%d).",
300 p_vout->p_sys->p_input->i_object_id );
304 /* Register the snapshot vout module at the input level */
305 val.i_int = p_vout->i_object_id;
307 if( var_Set( p_vout->p_sys->p_input, "snapshot-id", val ) )
309 msg_Err( p_vout, "Cannot register snapshot-id in p_input (%d).",
310 p_vout->p_sys->p_input->i_object_id );
317 /*****************************************************************************
318 * Destroy: destroy video thread
319 *****************************************************************************
320 * Terminate an output method created by Create
321 *****************************************************************************/
322 static void Destroy( vlc_object_t *p_this )
324 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 var_Destroy( p_this->p_libvlc_global, "snapshot-id" );
334 for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
336 free( p_vout->p_sys->p_list[ i_index ]->p_data );
338 free( p_vout->p_sys->p_list );
339 /* Destroy structure */
340 free( p_vout->p_sys );
343 /* Return the position in ms from the start of the movie */
344 static mtime_t snapshot_GetMovietime( vout_thread_t *p_vout )
346 input_thread_t* p_input;
350 p_input = p_vout->p_sys->p_input;
354 var_Get( p_input, "time", &val );
356 i_result = val.i_time - p_input->i_pts_delay;
358 return( i_result / 1000 );
361 /*****************************************************************************
362 * Display: displays previously rendered output
363 *****************************************************************************
364 * This function copies the rendered picture into our circular buffer.
365 *****************************************************************************/
366 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
371 i_index = p_vout->p_sys->i_index;
373 p_vout->p_libvlc->pf_memcpy( p_vout->p_sys->p_list[i_index]->p_data,
375 p_vout->p_sys->i_datasize );
377 i_date = snapshot_GetMovietime( p_vout );
379 p_vout->p_sys->p_list[i_index]->date = i_date;
383 if( i_index >= p_vout->p_sys->i_size )
388 p_vout->p_sys->i_index = i_index;