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