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 *****************************************************************************/
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
49 #include <vlc_interface.h>
50 #include <vlc_input.h>
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Create ( vlc_object_t * );
56 static void Destroy ( vlc_object_t * );
58 static int Init ( vout_thread_t * );
59 static void End ( vout_thread_t * );
60 static void Display ( vout_thread_t *, picture_t * );
62 /*****************************************************************************
64 *****************************************************************************/
65 #define WIDTH_TEXT N_( "Snapshot width" )
66 #define WIDTH_LONGTEXT N_( "Width of the snapshot image." )
68 #define HEIGHT_TEXT N_( "Snapshot height" )
69 #define HEIGHT_LONGTEXT N_( "Height of the snapshot image." )
71 #define CHROMA_TEXT N_( "Chroma" )
72 #define CHROMA_LONGTEXT N_( "Output chroma for the snapshot image " \
73 "(a 4 character string, like \"RV32\")." )
75 #define CACHE_TEXT N_( "Cache size (number of images)" )
76 #define CACHE_LONGTEXT N_( "Snapshot cache size (number of images to keep)." )
80 set_description( N_( "Snapshot output" ) )
81 set_shortname( N_("Snapshot") )
83 set_category( CAT_VIDEO )
84 set_subcategory( SUBCAT_VIDEO_VOUT )
85 set_capability( "video output", 1 )
87 add_integer( "snapshot-width", 320, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, false )
88 add_integer( "snapshot-height", 200, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
89 add_string( "snapshot-chroma", "RV32", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
90 add_integer( "snapshot-cache-size", 50, NULL, CACHE_TEXT, CACHE_LONGTEXT, true )
92 set_callbacks( Create, Destroy )
95 /*****************************************************************************
96 * vout_sys_t: video output descriptor
97 *****************************************************************************/
98 typedef struct snapshot_t
100 uint8_t *p_data; /* Data area */
102 int i_width; /* In pixels */
103 int i_height; /* In pixels */
104 int i_datasize; /* In bytes */
105 mtime_t date; /* Presentation time */
110 snapshot_t **p_list; /* List of available snapshots */
111 int i_index; /* Index of the next available list member */
112 int i_size; /* Size of the cache */
113 int i_datasize; /* Size of an image */
114 input_thread_t *p_input; /* The input thread */
117 /*****************************************************************************
118 * Create: allocates video thread
119 *****************************************************************************
120 * This function allocates and initializes a vout method.
121 *****************************************************************************/
122 static int Create( vlc_object_t *p_this )
124 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
126 /* Allocate instance and initialize some members */
127 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
128 if( ! p_vout->p_sys )
131 var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER );
132 var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER );
133 var_Create( p_vout, "snapshot-datasize", VLC_VAR_INTEGER );
134 var_Create( p_vout, "snapshot-cache-size", VLC_VAR_INTEGER );
135 var_Create( p_vout, "snapshot-list-pointer", VLC_VAR_ADDRESS );
137 p_vout->pf_init = Init;
138 p_vout->pf_end = End;
139 p_vout->pf_manage = NULL;
140 p_vout->pf_render = NULL;
141 p_vout->pf_display = Display;
146 /*****************************************************************************
147 * Init: initialize video thread
148 *****************************************************************************/
149 static int Init( vout_thread_t *p_vout )
160 i_width = config_GetInt( p_vout, "snapshot-width" );
161 i_height = config_GetInt( p_vout, "snapshot-height" );
163 psz_chroma = config_GetPsz( p_vout, "snapshot-chroma" );
166 if( strlen( psz_chroma ) < 4 )
168 msg_Err( p_vout, "snapshot-chroma should be 4 characters long" );
171 i_chroma = VLC_FOURCC( psz_chroma[0], psz_chroma[1],
172 psz_chroma[2], psz_chroma[3] );
177 msg_Err( p_vout, "Cannot find chroma information." );
181 I_OUTPUTPICTURES = 0;
183 /* Initialize the output structure */
184 p_vout->output.i_chroma = i_chroma;
185 p_vout->output.pf_setpalette = NULL;
186 p_vout->output.i_width = i_width;
187 p_vout->output.i_height = i_height;
188 p_vout->output.i_aspect = p_vout->output.i_width
189 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
192 /* Define the bitmasks */
195 case VLC_FOURCC( 'R','V','1','5' ):
196 p_vout->output.i_rmask = 0x001f;
197 p_vout->output.i_gmask = 0x03e0;
198 p_vout->output.i_bmask = 0x7c00;
201 case VLC_FOURCC( 'R','V','1','6' ):
202 p_vout->output.i_rmask = 0x001f;
203 p_vout->output.i_gmask = 0x07e0;
204 p_vout->output.i_bmask = 0xf800;
207 case VLC_FOURCC( 'R','V','2','4' ):
208 p_vout->output.i_rmask = 0xff0000;
209 p_vout->output.i_gmask = 0x00ff00;
210 p_vout->output.i_bmask = 0x0000ff;
213 case VLC_FOURCC( 'R','V','3','2' ):
214 p_vout->output.i_rmask = 0xff0000;
215 p_vout->output.i_gmask = 0x00ff00;
216 p_vout->output.i_bmask = 0x0000ff;
220 /* Try to initialize 1 direct buffer */
223 /* Find an empty picture slot */
224 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
226 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
228 p_pic = p_vout->p_picture + i_index;
233 /* Allocate the picture */
239 vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
240 p_vout->output.i_width, p_vout->output.i_height,
241 p_vout->output.i_aspect );
243 if( p_pic->i_planes == 0 )
248 p_pic->i_status = DESTROYED_PICTURE;
249 p_pic->i_type = DIRECT_PICTURE;
251 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
256 /* Get datasize and set variables */
257 i_datasize = i_width * i_height * p_pic->p->i_pixel_pitch;
259 p_vout->p_sys->i_datasize = i_datasize;
260 p_vout->p_sys->i_index = 0;
261 p_vout->p_sys->i_size = config_GetInt( p_vout, "snapshot-cache-size" );
263 if( p_vout->p_sys->i_size < 2 )
265 msg_Err( p_vout, "snapshot-cache-size must be at least 1." );
269 p_vout->p_sys->p_list = malloc( p_vout->p_sys->i_size * sizeof( snapshot_t * ) );
271 if( p_vout->p_sys->p_list == NULL )
274 /* Initialize the structures for the circular buffer */
275 for( i_index = 0; i_index < p_vout->p_sys->i_size; i_index++ )
277 snapshot_t *p_snapshot = malloc( sizeof( snapshot_t ) );
279 if( p_snapshot == NULL )
282 p_snapshot->i_width = i_width;
283 p_snapshot->i_height = i_height;
284 p_snapshot->i_datasize = i_datasize;
285 p_snapshot->date = 0;
286 p_snapshot->p_data = malloc( i_datasize );
287 if( p_snapshot->p_data == NULL )
292 p_vout->p_sys->p_list[i_index] = p_snapshot;
296 var_Set( p_vout, "snapshot-width", val );
297 val.i_int = i_height;
298 var_Set( p_vout, "snapshot-height", val );
299 val.i_int = i_datasize;
300 var_Set( p_vout, "snapshot-datasize", val );
302 val.i_int = p_vout->p_sys->i_size;
303 var_Set( p_vout, "snapshot-cache-size", val );
305 val.p_address = p_vout->p_sys->p_list;
306 var_Set( p_vout, "snapshot-list-pointer", val );
308 /* Get the p_input pointer (to access video times) */
309 p_vout->p_sys->p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
312 if( !p_vout->p_sys->p_input )
315 if( var_Create( p_vout->p_sys->p_input, "snapshot-id", VLC_VAR_INTEGER ) )
317 msg_Err( p_vout, "Cannot create snapshot-id variable in p_input(%p).",
318 p_vout->p_sys->p_input );
322 /* Register the snapshot vout module at the input level */
323 val.p_address = p_vout;
325 if( var_Set( p_vout->p_sys->p_input, "snapshot-id", val ) )
327 msg_Err( p_vout, "Cannot register snapshot-id in p_input(%p).",
328 p_vout->p_sys->p_input );
335 /*****************************************************************************
336 * End: terminate video thread output method
337 *****************************************************************************/
338 static void End( vout_thread_t *p_vout )
343 /*****************************************************************************
344 * Destroy: destroy video thread
345 *****************************************************************************
346 * Terminate an output method created by Create
347 *****************************************************************************/
348 static void Destroy( vlc_object_t *p_this )
350 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
353 var_Destroy( p_vout->p_sys->p_input, "snapshot-id" );
355 vlc_object_release( p_vout->p_sys->p_input );
356 var_Destroy( p_this, "snapshot-width" );
357 var_Destroy( p_this, "snapshot-height" );
358 var_Destroy( p_this, "snapshot-datasize" );
360 for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
362 free( p_vout->p_sys->p_list[ i_index ]->p_data );
364 free( p_vout->p_sys->p_list );
365 /* Destroy structure */
366 free( p_vout->p_sys );
369 /* Return the position in ms from the start of the movie */
370 static mtime_t snapshot_GetMovietime( vout_thread_t *p_vout )
372 input_thread_t *p_input = p_vout->p_sys->p_input;
376 return var_GetTime( p_input, "time" ) / 1000;
379 /*****************************************************************************
380 * Display: displays previously rendered output
381 *****************************************************************************
382 * This function copies the rendered picture into our circular buffer.
383 *****************************************************************************/
384 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
389 i_index = p_vout->p_sys->i_index;
391 vlc_memcpy( p_vout->p_sys->p_list[i_index]->p_data, p_pic->p->p_pixels,
392 p_vout->p_sys->i_datasize );
394 i_date = snapshot_GetMovietime( p_vout );
396 p_vout->p_sys->p_list[i_index]->date = i_date;
400 if( i_index >= p_vout->p_sys->i_size )
405 p_vout->p_sys->i_index = i_index;