Fix a bunch of warnings and errors
[vlc.git] / 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_( "Width of the snapshot image." )
62
63 #define HEIGHT_TEXT N_( "Snapshot height" )
64 #define HEIGHT_LONGTEXT N_( "Height of the snapshot image." )
65
66 #define CHROMA_TEXT N_( "Chroma" )
67 #define CHROMA_LONGTEXT N_( "Output chroma for the snapshot image " \
68                             "(a 4 character string, like \"RV32\")." )
69
70 #define CACHE_TEXT N_( "Cache size (number of images)" )
71 #define CACHE_LONGTEXT N_( "Snapshot cache size (number of images to keep)." )
72
73
74 vlc_module_begin( );
75     set_description( _( "Snapshot module" ) );
76     set_shortname( N_("Snapshot") );
77
78     set_category( CAT_VIDEO );
79     set_subcategory( SUBCAT_VIDEO_VOUT );
80     set_capability( "video output", 1 );
81
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 );
86
87     set_callbacks( Create, Destroy );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * vout_sys_t: video output descriptor
92  *****************************************************************************/
93 struct vout_sys_t
94 {
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 */
100 };
101
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 )
108 {
109     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
110
111     /* Allocate instance and initialize some members */
112     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
113     if( ! p_vout->p_sys )
114         return VLC_ENOMEM;
115
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 );
121
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;
127
128     return VLC_SUCCESS;
129 }
130
131 /*****************************************************************************
132  * Init: initialize video thread
133  *****************************************************************************/
134 static int Init( vout_thread_t *p_vout )
135 {
136     int i_index;
137     picture_t *p_pic;
138     vlc_value_t val;
139     char* psz_chroma;
140     int i_chroma;
141     int i_width;
142     int i_height;
143     int i_datasize;
144
145     i_width  = config_GetInt( p_vout, "snapshot-width" );
146     i_height = config_GetInt( p_vout, "snapshot-height" );
147
148     psz_chroma = config_GetPsz( p_vout, "snapshot-chroma" );
149     if( psz_chroma )
150     {
151         if( strlen( psz_chroma ) < 4 )
152         {
153             msg_Err( p_vout, "snapshot-chroma should be 4 characters long" );
154             return VLC_EGENERIC;
155         }
156         i_chroma = VLC_FOURCC( psz_chroma[0], psz_chroma[1],
157                                psz_chroma[2], psz_chroma[3] );
158         free( psz_chroma );
159     }
160     else
161     {
162         msg_Err( p_vout, "Cannot find chroma information." );
163         return VLC_EGENERIC;
164     }
165
166     I_OUTPUTPICTURES = 0;
167
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;
175
176
177     /* Define the bitmasks */
178     switch( i_chroma )
179     {
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;
184         break;
185
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;
190         break;
191
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;
196         break;
197
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;
202         break;
203     }
204
205     /* Try to initialize 1 direct buffer */
206     p_pic = NULL;
207
208     /* Find an empty picture slot */
209     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
210     {
211         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
212         {
213             p_pic = p_vout->p_picture + i_index;
214             break;
215         }
216     }
217
218     /* Allocate the picture */
219     if( p_pic == NULL )
220     {
221         return VLC_SUCCESS;
222     }
223
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 );
227
228     if( p_pic->i_planes == 0 )
229     {
230         return VLC_EGENERIC;
231     }
232
233     p_pic->i_status = DESTROYED_PICTURE;
234     p_pic->i_type   = DIRECT_PICTURE;
235
236     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
237
238     I_OUTPUTPICTURES++;
239
240
241     /* Get datasize and set variables */
242     i_datasize = i_width * i_height * p_pic->p->i_pixel_pitch;
243
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" );
247
248     if( p_vout->p_sys->i_size < 2 )
249     {
250         msg_Err( p_vout, "snapshot-cache-size must be at least 1." );
251         return VLC_EGENERIC;
252     }
253
254     p_vout->p_sys->p_list = malloc( p_vout->p_sys->i_size * sizeof( snapshot_t * ) );
255
256     if( p_vout->p_sys->p_list == NULL )
257         return VLC_ENOMEM;
258
259     /* Initialize the structures for the circular buffer */
260     for( i_index = 0; i_index < p_vout->p_sys->i_size; i_index++ )
261     {
262         snapshot_t *p_snapshot = malloc( sizeof( snapshot_t ) );
263
264         if( p_snapshot == NULL )
265             return VLC_ENOMEM;
266
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 )
273             return VLC_ENOMEM;
274         p_vout->p_sys->p_list[i_index] = p_snapshot;
275     }
276
277     val.i_int = i_width;
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 );
283
284     val.i_int = p_vout->p_sys->i_size;
285     var_Set( p_vout, "snapshot-cache-size", val );
286
287     val.p_address = p_vout->p_sys->p_list;
288     var_Set( p_vout, "snapshot-list-pointer", val );
289
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,
292                                               FIND_PARENT );
293
294     if( !p_vout->p_sys->p_input )
295         return VLC_ENOOBJ;
296
297     if( var_Create( p_vout->p_sys->p_input, "snapshot-id", VLC_VAR_INTEGER ) )
298     {
299         msg_Err( p_vout, "Cannot create snapshot-id variable in p_input (%d).",
300                  p_vout->p_sys->p_input->i_object_id );
301         return VLC_EGENERIC;
302     }
303
304     /* Register the snapshot vout module at the input level */
305     val.i_int = p_vout->i_object_id;
306
307     if( var_Set( p_vout->p_sys->p_input, "snapshot-id", val ) )
308     {
309         msg_Err( p_vout, "Cannot register snapshot-id in p_input (%d).",
310                  p_vout->p_sys->p_input->i_object_id );
311         return VLC_EGENERIC;
312     }
313
314     return VLC_SUCCESS;
315 }
316
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 )
323 {
324     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
325     vlc_object_t *p_vlc;
326     int i_index;
327
328     vlc_object_release( p_vout->p_sys->p_input );
329     var_Destroy( p_this, "snapshot-width" );
330     var_Destroy( p_this, "snapshot-height" );
331     var_Destroy( p_this, "snapshot-datasize" );
332
333     p_vlc = vlc_object_find( p_this, VLC_OBJECT_ROOT, FIND_PARENT );
334     if( p_vlc )
335     {
336         /* UnRegister the snapshot vout module at the root level */
337         /* var_Destroy (p_vlc, "snapshot-id"); */
338         var_Destroy( p_this->p_libvlc, "snapshot-id" );
339         vlc_object_release( p_vlc );
340     }
341
342     for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
343     {
344         free( p_vout->p_sys->p_list[ i_index ]->p_data );
345     }
346     free( p_vout->p_sys->p_list );
347     /* Destroy structure */
348     free( p_vout->p_sys );
349 }
350
351 /* Return the position in ms from the start of the movie */
352 static mtime_t snapshot_GetMovietime( vout_thread_t *p_vout )
353 {
354     input_thread_t* p_input;
355     vlc_value_t val;
356     mtime_t i_result;
357
358     p_input = p_vout->p_sys->p_input;
359     if( !p_input )
360         return 0;
361
362     var_Get( p_input, "time", &val );
363
364     i_result = val.i_time - p_input->i_pts_delay;
365
366     return( i_result / 1000 );
367 }
368
369 /*****************************************************************************
370  * Display: displays previously rendered output
371  *****************************************************************************
372  * This function copies the rendered picture into our circular buffer.
373  *****************************************************************************/
374 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
375 {
376     int i_index;
377     mtime_t i_date;
378
379     i_index = p_vout->p_sys->i_index;
380
381     p_vout->p_vlc->pf_memcpy( p_vout->p_sys->p_list[i_index]->p_data,
382                               p_pic->p->p_pixels,
383                               p_vout->p_sys->i_datasize );
384
385     i_date = snapshot_GetMovietime( p_vout );
386
387     p_vout->p_sys->p_list[i_index]->date = i_date;
388
389     i_index++;
390
391     if( i_index >= p_vout->p_sys->i_size )
392     {
393         i_index = 0;
394     }
395
396     p_vout->p_sys->i_index = i_index;
397 }
398