]> git.sesse.net Git - vlc/blob - modules/video_output/snapshot.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / video_output / snapshot.c
1 /*****************************************************************************
2  * snapshot.c : snapshot plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Aubert <oaubert@lisi.univ-lyon1.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
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
33  *
34  * It is used for the moment by the CORBA module and a specialized
35  * python-vlc binding.
36  *****************************************************************************/
37
38 /*****************************************************************************
39  * Preamble
40  *****************************************************************************/
41
42 #include <vlc/vlc.h>
43 #include <vlc_vout.h>
44 #include <vlc_interface.h>
45 #include <vlc_input.h>
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int  Create    ( vlc_object_t * );
51 static void Destroy   ( vlc_object_t * );
52
53 static int  Init      ( vout_thread_t * );
54 static void Display   ( vout_thread_t *, picture_t * );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 #define WIDTH_TEXT N_( "Snapshot width" )
60 #define WIDTH_LONGTEXT N_( "Width of the snapshot image." )
61
62 #define HEIGHT_TEXT N_( "Snapshot height" )
63 #define HEIGHT_LONGTEXT N_( "Height of the snapshot image." )
64
65 #define CHROMA_TEXT N_( "Chroma" )
66 #define CHROMA_LONGTEXT N_( "Output chroma for the snapshot image " \
67                             "(a 4 character string, like \"RV32\")." )
68
69 #define CACHE_TEXT N_( "Cache size (number of images)" )
70 #define CACHE_LONGTEXT N_( "Snapshot cache size (number of images to keep)." )
71
72
73 vlc_module_begin( );
74     set_description( _( "Snapshot module" ) );
75     set_shortname( _("Snapshot") );
76
77     set_category( CAT_VIDEO );
78     set_subcategory( SUBCAT_VIDEO_VOUT );
79     set_capability( "video output", 1 );
80
81     add_integer( "snapshot-width", 320, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
82         change_safe();
83     add_integer( "snapshot-height", 200, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
84         change_safe();
85     add_string( "snapshot-chroma", "RV32", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, VLC_TRUE );
86         change_safe();
87     add_integer( "snapshot-cache-size", 50, NULL, CACHE_TEXT, CACHE_LONGTEXT, VLC_TRUE );
88         change_safe();
89
90     set_callbacks( Create, Destroy );
91 vlc_module_end();
92
93 /*****************************************************************************
94  * vout_sys_t: video output descriptor
95  *****************************************************************************/
96 struct vout_sys_t
97 {
98     snapshot_t **p_list;    /* List of available snapshots */
99     int i_index;            /* Index of the next available list member */
100     int i_size;             /* Size of the cache */
101     int i_datasize;         /* Size of an image */
102     input_thread_t *p_input;             /* The input thread */
103 };
104
105 /*****************************************************************************
106  * Create: allocates video thread
107  *****************************************************************************
108  * This function allocates and initializes a vout method.
109  *****************************************************************************/
110 static int Create( vlc_object_t *p_this )
111 {
112     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
113
114     /* Allocate instance and initialize some members */
115     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
116     if( ! p_vout->p_sys )
117         return VLC_ENOMEM;
118
119     var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER );
120     var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER );
121     var_Create( p_vout, "snapshot-datasize", VLC_VAR_INTEGER );
122     var_Create( p_vout, "snapshot-cache-size", VLC_VAR_INTEGER );
123     var_Create( p_vout, "snapshot-list-pointer", VLC_VAR_ADDRESS );
124
125     p_vout->pf_init = Init;
126     p_vout->pf_end = NULL;
127     p_vout->pf_manage = NULL;
128     p_vout->pf_render = NULL;
129     p_vout->pf_display = Display;
130
131     return VLC_SUCCESS;
132 }
133
134 /*****************************************************************************
135  * Init: initialize video thread
136  *****************************************************************************/
137 static int Init( vout_thread_t *p_vout )
138 {
139     int i_index;
140     picture_t *p_pic;
141     vlc_value_t val;
142     char* psz_chroma;
143     int i_chroma;
144     int i_width;
145     int i_height;
146     int i_datasize;
147
148     i_width  = config_GetInt( p_vout, "snapshot-width" );
149     i_height = config_GetInt( p_vout, "snapshot-height" );
150
151     psz_chroma = config_GetPsz( p_vout, "snapshot-chroma" );
152     if( psz_chroma )
153     {
154         if( strlen( psz_chroma ) < 4 )
155         {
156             msg_Err( p_vout, "snapshot-chroma should be 4 characters long" );
157             return VLC_EGENERIC;
158         }
159         i_chroma = VLC_FOURCC( psz_chroma[0], psz_chroma[1],
160                                psz_chroma[2], psz_chroma[3] );
161         free( psz_chroma );
162     }
163     else
164     {
165         msg_Err( p_vout, "Cannot find chroma information." );
166         return VLC_EGENERIC;
167     }
168
169     I_OUTPUTPICTURES = 0;
170
171     /* Initialize the output structure */
172     p_vout->output.i_chroma = i_chroma;
173     p_vout->output.pf_setpalette = NULL;
174     p_vout->output.i_width = i_width;
175     p_vout->output.i_height = i_height;
176     p_vout->output.i_aspect = p_vout->output.i_width
177                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
178
179
180     /* Define the bitmasks */
181     switch( i_chroma )
182     {
183       case VLC_FOURCC( 'R','V','1','5' ):
184         p_vout->output.i_rmask = 0x001f;
185         p_vout->output.i_gmask = 0x03e0;
186         p_vout->output.i_bmask = 0x7c00;
187         break;
188
189       case VLC_FOURCC( 'R','V','1','6' ):
190         p_vout->output.i_rmask = 0x001f;
191         p_vout->output.i_gmask = 0x07e0;
192         p_vout->output.i_bmask = 0xf800;
193         break;
194
195       case VLC_FOURCC( 'R','V','2','4' ):
196         p_vout->output.i_rmask = 0xff0000;
197         p_vout->output.i_gmask = 0x00ff00;
198         p_vout->output.i_bmask = 0x0000ff;
199         break;
200
201       case VLC_FOURCC( 'R','V','3','2' ):
202         p_vout->output.i_rmask = 0xff0000;
203         p_vout->output.i_gmask = 0x00ff00;
204         p_vout->output.i_bmask = 0x0000ff;
205         break;
206     }
207
208     /* Try to initialize 1 direct buffer */
209     p_pic = NULL;
210
211     /* Find an empty picture slot */
212     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
213     {
214         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
215         {
216             p_pic = p_vout->p_picture + i_index;
217             break;
218         }
219     }
220
221     /* Allocate the picture */
222     if( p_pic == NULL )
223     {
224         return VLC_SUCCESS;
225     }
226
227     vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
228                           p_vout->output.i_width, p_vout->output.i_height,
229                           p_vout->output.i_aspect );
230
231     if( p_pic->i_planes == 0 )
232     {
233         return VLC_EGENERIC;
234     }
235
236     p_pic->i_status = DESTROYED_PICTURE;
237     p_pic->i_type   = DIRECT_PICTURE;
238
239     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
240
241     I_OUTPUTPICTURES++;
242
243
244     /* Get datasize and set variables */
245     i_datasize = i_width * i_height * p_pic->p->i_pixel_pitch;
246
247     p_vout->p_sys->i_datasize = i_datasize;
248     p_vout->p_sys->i_index = 0;
249     p_vout->p_sys->i_size = config_GetInt( p_vout, "snapshot-cache-size" );
250
251     if( p_vout->p_sys->i_size < 2 )
252     {
253         msg_Err( p_vout, "snapshot-cache-size must be at least 1." );
254         return VLC_EGENERIC;
255     }
256
257     p_vout->p_sys->p_list = malloc( p_vout->p_sys->i_size * sizeof( snapshot_t * ) );
258
259     if( p_vout->p_sys->p_list == NULL )
260         return VLC_ENOMEM;
261
262     /* Initialize the structures for the circular buffer */
263     for( i_index = 0; i_index < p_vout->p_sys->i_size; i_index++ )
264     {
265         snapshot_t *p_snapshot = malloc( sizeof( snapshot_t ) );
266
267         if( p_snapshot == NULL )
268             return VLC_ENOMEM;
269
270         p_snapshot->i_width = i_width;
271         p_snapshot->i_height = i_height;
272         p_snapshot->i_datasize = i_datasize;
273         p_snapshot->date = 0;
274         p_snapshot->p_data = ( char* ) malloc( i_datasize );
275         if( p_snapshot->p_data == NULL )
276             return VLC_ENOMEM;
277         p_vout->p_sys->p_list[i_index] = p_snapshot;
278     }
279
280     val.i_int = i_width;
281     var_Set( p_vout, "snapshot-width", val );
282     val.i_int = i_height;
283     var_Set( p_vout, "snapshot-height", val );
284     val.i_int = i_datasize;
285     var_Set( p_vout, "snapshot-datasize", val );
286
287     val.i_int = p_vout->p_sys->i_size;
288     var_Set( p_vout, "snapshot-cache-size", val );
289
290     val.p_address = p_vout->p_sys->p_list;
291     var_Set( p_vout, "snapshot-list-pointer", val );
292
293     /* Get the p_input pointer (to access video times) */
294     p_vout->p_sys->p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
295                                               FIND_PARENT );
296
297     if( !p_vout->p_sys->p_input )
298         return VLC_ENOOBJ;
299
300     if( var_Create( p_vout->p_sys->p_input, "snapshot-id", VLC_VAR_INTEGER ) )
301     {
302         msg_Err( p_vout, "Cannot create snapshot-id variable in p_input (%d).",
303                  p_vout->p_sys->p_input->i_object_id );
304         return VLC_EGENERIC;
305     }
306
307     /* Register the snapshot vout module at the input level */
308     val.i_int = p_vout->i_object_id;
309
310     if( var_Set( p_vout->p_sys->p_input, "snapshot-id", val ) )
311     {
312         msg_Err( p_vout, "Cannot register snapshot-id in p_input (%d).",
313                  p_vout->p_sys->p_input->i_object_id );
314         return VLC_EGENERIC;
315     }
316
317     return VLC_SUCCESS;
318 }
319
320 /*****************************************************************************
321  * Destroy: destroy video thread
322  *****************************************************************************
323  * Terminate an output method created by Create
324  *****************************************************************************/
325 static void Destroy( vlc_object_t *p_this )
326 {
327     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
328     int i_index;
329
330     var_Destroy( p_vout->p_sys->p_input, "snapshot-id" );
331
332     vlc_object_release( p_vout->p_sys->p_input );
333     var_Destroy( p_this, "snapshot-width" );
334     var_Destroy( p_this, "snapshot-height" );
335     var_Destroy( p_this, "snapshot-datasize" );
336
337     for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
338     {
339         free( p_vout->p_sys->p_list[ i_index ]->p_data );
340     }
341     free( p_vout->p_sys->p_list );
342     /* Destroy structure */
343     free( p_vout->p_sys );
344 }
345
346 /* Return the position in ms from the start of the movie */
347 static mtime_t snapshot_GetMovietime( vout_thread_t *p_vout )
348 {
349     input_thread_t* p_input;
350     vlc_value_t val;
351     mtime_t i_result;
352
353     p_input = p_vout->p_sys->p_input;
354     if( !p_input )
355         return 0;
356
357     var_Get( p_input, "time", &val );
358
359     i_result = val.i_time - p_input->i_pts_delay;
360
361     return( i_result / 1000 );
362 }
363
364 /*****************************************************************************
365  * Display: displays previously rendered output
366  *****************************************************************************
367  * This function copies the rendered picture into our circular buffer.
368  *****************************************************************************/
369 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
370 {
371     int i_index;
372     mtime_t i_date;
373
374     i_index = p_vout->p_sys->i_index;
375
376     p_vout->p_libvlc->pf_memcpy( p_vout->p_sys->p_list[i_index]->p_data,
377                                  p_pic->p->p_pixels,
378                                   p_vout->p_sys->i_datasize );
379
380     i_date = snapshot_GetMovietime( p_vout );
381
382     p_vout->p_sys->p_list[i_index]->date = i_date;
383
384     i_index++;
385
386     if( i_index >= p_vout->p_sys->i_size )
387     {
388         i_index = 0;
389     }
390
391     p_vout->p_sys->i_index = i_index;
392 }
393