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 )
134 msg_Err( p_vout, "snapshot-chroma should be 4 characters long." );
137 i_chroma = VLC_FOURCC( psz_chroma[0], psz_chroma[1],
138 psz_chroma[2], psz_chroma[3] );
143 msg_Err( p_vout, "Cannot find chroma information." );
147 I_OUTPUTPICTURES = 0;
149 /* Initialize the output structure */
150 p_vout->output.i_chroma = i_chroma;
151 p_vout->output.pf_setpalette = NULL;
152 p_vout->output.i_width = i_width;
153 p_vout->output.i_height = i_height;
154 p_vout->output.i_aspect = p_vout->output.i_width
155 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
158 /* Define the bitmasks */
161 case VLC_FOURCC( 'R','V','1','5' ):
162 p_vout->output.i_rmask = 0x001f;
163 p_vout->output.i_gmask = 0x03e0;
164 p_vout->output.i_bmask = 0x7c00;
167 case VLC_FOURCC( 'R','V','1','6' ):
168 p_vout->output.i_rmask = 0x001f;
169 p_vout->output.i_gmask = 0x07e0;
170 p_vout->output.i_bmask = 0xf800;
173 case VLC_FOURCC( 'R','V','2','4' ):
174 p_vout->output.i_rmask = 0xff0000;
175 p_vout->output.i_gmask = 0x00ff00;
176 p_vout->output.i_bmask = 0x0000ff;
179 case VLC_FOURCC( 'R','V','3','2' ):
180 p_vout->output.i_rmask = 0xff0000;
181 p_vout->output.i_gmask = 0x00ff00;
182 p_vout->output.i_bmask = 0x0000ff;
186 /* Try to initialize 1 direct buffer */
189 /* Find an empty picture slot */
190 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
192 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
194 p_pic = p_vout->p_picture + i_index;
199 /* Allocate the picture */
205 vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
206 p_vout->output.i_width, p_vout->output.i_height,
207 p_vout->output.i_aspect );
209 if( p_pic->i_planes == 0 )
214 p_pic->i_status = DESTROYED_PICTURE;
215 p_pic->i_type = DIRECT_PICTURE;
217 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
222 /* Get datasize and set variables */
223 i_datasize = i_width * i_height * p_pic->p->i_pixel_pitch;
225 p_vout->p_sys->i_datasize = i_datasize;
226 p_vout->p_sys->i_index = 0;
227 p_vout->p_sys->i_size = config_GetInt( p_vout, "snapshot-cache-size" );
229 if( p_vout->p_sys->i_size < 2 )
231 msg_Err( p_vout, "snapshot-cache-size must be at least 1." );
235 p_vout->p_sys->p_list = malloc( p_vout->p_sys->i_size * sizeof( snapshot_t * ) );
237 if( p_vout->p_sys->p_list == NULL )
240 /* Initialize the structures for the circular buffer */
241 for( i_index = 0; i_index < p_vout->p_sys->i_size; i_index++ )
243 snapshot_t *p_snapshot = malloc( sizeof( snapshot_t ) );
245 if( p_snapshot == NULL )
248 p_snapshot->i_width = i_width;
249 p_snapshot->i_height = i_height;
250 p_snapshot->i_datasize = i_datasize;
251 p_snapshot->date = 0;
252 p_snapshot->p_data = ( char* ) malloc( i_datasize );
253 if( p_snapshot->p_data == NULL )
255 p_vout->p_sys->p_list[i_index] = p_snapshot;
259 var_Set( p_vout, "snapshot-width", val );
260 val.i_int = i_height;
261 var_Set( p_vout, "snapshot-height", val );
262 val.i_int = i_datasize;
263 var_Set( p_vout, "snapshot-datasize", val );
265 val.i_int = p_vout->p_sys->i_size;
266 var_Set( p_vout, "snapshot-cache-size", val );
268 val.p_address = p_vout->p_sys->p_list;
269 var_Set( p_vout, "snapshot-list-pointer", val );
271 /* Get the p_input pointer (to access video times) */
272 p_vout->p_sys->p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
275 if( !p_vout->p_sys->p_input )
278 if( var_Create( p_vout->p_sys->p_input, "snapshot-id", VLC_VAR_INTEGER ) )
280 msg_Err( p_vout, "Cannot create snapshot-id variable in p_input (%d).",
281 p_vout->p_sys->p_input->i_object_id );
285 /* Register the snapshot vout module at the input level */
286 val.i_int = p_vout->i_object_id;
288 if( var_Set( p_vout->p_sys->p_input, "snapshot-id", val ) )
290 msg_Err( p_vout, "Cannot register snapshot-id in p_input (%d).",
291 p_vout->p_sys->p_input->i_object_id );
298 /*****************************************************************************
299 * Destroy: destroy video thread
300 *****************************************************************************
301 * Terminate an output method created by Create
302 *****************************************************************************/
303 static void Destroy( vlc_object_t *p_this )
305 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
309 vlc_object_release( p_vout->p_sys->p_input );
310 var_Destroy( p_this, "snapshot-width" );
311 var_Destroy( p_this, "snapshot-height" );
312 var_Destroy( p_this, "snapshot-datasize" );
314 p_vlc = vlc_object_find( p_this, VLC_OBJECT_ROOT, FIND_PARENT );
317 /* UnRegister the snapshot vout module at the root level */
318 /* var_Destroy (p_vlc, "snapshot-id"); */
319 var_Destroy( p_this->p_libvlc, "snapshot-id" );
320 vlc_object_release( p_vlc );
323 for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
325 free( p_vout->p_sys->p_list[ i_index ]->p_data );
327 free( p_vout->p_sys->p_list );
328 /* Destroy structure */
329 free( p_vout->p_sys );
332 /* Return the position in ms from the start of the movie */
333 static mtime_t snapshot_GetMovietime( vout_thread_t *p_vout )
335 input_thread_t* p_input;
339 p_input = p_vout->p_sys->p_input;
343 var_Get( p_input, "time", &val );
345 i_result = val.i_time;
347 return( i_result / 1000 );
350 /*****************************************************************************
351 * Display: displays previously rendered output
352 *****************************************************************************
353 * This function copies the rendered picture into our circular buffer.
354 *****************************************************************************/
355 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
360 i_index = p_vout->p_sys->i_index;
362 p_vout->p_vlc->pf_memcpy( p_vout->p_sys->p_list[i_index]->p_data,
364 p_vout->p_sys->i_datasize );
366 i_date = snapshot_GetMovietime( p_vout );
368 p_vout->p_sys->p_list[i_index]->date = i_date;
372 if( i_index >= p_vout->p_sys->i_size )
377 p_vout->p_sys->i_index = i_index;