equalizer.c: compile fix for old compilers (thx sam)
[vlc.git] / modules / video_output / snapshot.c
1 /*****************************************************************************
2  * snapshot.c : snapshot plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31 #include <vlc/intf.h>
32 #include <snapshot.h>
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static int  Create    ( vlc_object_t * );
38 static void Destroy   ( vlc_object_t * );
39
40 static int  Init      ( vout_thread_t * );
41 static void Display   ( vout_thread_t *, picture_t * );
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 #define WIDTH_TEXT N_( "snapshot width" )
47 #define WIDTH_LONGTEXT N_( "Set the width of the snapshot image" )
48
49 #define HEIGHT_TEXT N_( "snapshot height" )
50 #define HEIGHT_LONGTEXT N_( "Set the height of the snapshot image" )
51
52 #define CHROMA_TEXT N_( "chroma" )
53 #define CHROMA_LONGTEXT N_( "Set the desired chroma for the snapshot image (a 4 character string)" )
54
55 #define CACHE_TEXT N_( "cache size (number of images)" )
56 #define CACHE_LONGTEXT N_( "Set the cache size (number of images to keep)" )
57
58
59 vlc_module_begin( );
60     set_description( _( "snapshot module" ) );
61     set_capability( "video output", 0 );
62
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 );
67
68     set_callbacks( Create, Destroy );
69 vlc_module_end();
70
71 /*****************************************************************************
72  * vout_sys_t: video output descriptor
73  *****************************************************************************/
74 struct vout_sys_t
75 {
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 */
81 };
82
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 )
89 {
90     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
91
92     /* Allocate instance and initialize some members */
93     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
94     if( ! p_vout->p_sys )
95         return VLC_ENOMEM;
96
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 );
102
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;
108
109     return VLC_SUCCESS;
110 }
111
112 /*****************************************************************************
113  * Init: initialize video thread
114  *****************************************************************************/
115 static int Init( vout_thread_t *p_vout )
116 {
117     int i_index;
118     picture_t *p_pic;
119     vlc_value_t val;
120     char* psz_chroma;
121     int i_chroma;
122     int i_width;
123     int i_height;
124     int i_datasize;
125
126     i_width  = config_GetInt( p_vout, "snapshot-width" );
127     i_height = config_GetInt( p_vout, "snapshot-height" );
128
129     psz_chroma = config_GetPsz( p_vout, "snapshot-chroma" );
130     if( psz_chroma )
131     {
132         if( strlen( psz_chroma ) < 4 )
133         {
134             msg_Err( p_vout, "snapshot-chroma should be 4 characters long." );
135             return VLC_EGENERIC;
136         }
137         i_chroma = VLC_FOURCC( psz_chroma[0], psz_chroma[1],
138                                psz_chroma[2], psz_chroma[3] );
139         free( psz_chroma );
140     }
141     else
142     {
143         msg_Err( p_vout, "Cannot find chroma information." );
144         return VLC_EGENERIC;
145     }
146
147     I_OUTPUTPICTURES = 0;
148
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;
156
157
158     /* Define the bitmasks */
159     switch( i_chroma )
160     {
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;
165         break;
166
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;
171         break;
172
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;
177         break;
178
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;
183         break;
184     }
185
186     /* Try to initialize 1 direct buffer */
187     p_pic = NULL;
188
189     /* Find an empty picture slot */
190     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
191     {
192         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
193         {
194             p_pic = p_vout->p_picture + i_index;
195             break;
196         }
197     }
198
199     /* Allocate the picture */
200     if( p_pic == NULL )
201     {
202         return VLC_SUCCESS;
203     }
204
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 );
208
209     if( p_pic->i_planes == 0 )
210     {
211         return VLC_EGENERIC;
212     }
213
214     p_pic->i_status = DESTROYED_PICTURE;
215     p_pic->i_type   = DIRECT_PICTURE;
216
217     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
218
219     I_OUTPUTPICTURES++;
220
221
222     /* Get datasize and set variables */
223     i_datasize = i_width * i_height * p_pic->p->i_pixel_pitch;
224
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" );
228
229     if( p_vout->p_sys->i_size < 2 )
230     {
231         msg_Err( p_vout, "snapshot-cache-size must be at least 1." );
232         return VLC_EGENERIC;
233     }
234
235     p_vout->p_sys->p_list = malloc( p_vout->p_sys->i_size * sizeof( snapshot_t * ) );
236
237     if( p_vout->p_sys->p_list == NULL )
238         return VLC_ENOMEM;
239
240     /* Initialize the structures for the circular buffer */
241     for( i_index = 0; i_index < p_vout->p_sys->i_size; i_index++ )
242     {
243         snapshot_t *p_snapshot = malloc( sizeof( snapshot_t ) );
244
245         if( p_snapshot == NULL )
246             return VLC_ENOMEM;
247
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 )
254             return VLC_ENOMEM;
255         p_vout->p_sys->p_list[i_index] = p_snapshot;
256     }
257
258     val.i_int = i_width;
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 );
264
265     val.i_int = p_vout->p_sys->i_size;
266     var_Set( p_vout, "snapshot-cache-size", val );
267
268     val.p_address = p_vout->p_sys->p_list;
269     var_Set( p_vout, "snapshot-list-pointer", val );
270
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,
273                                               FIND_PARENT );
274
275     if( !p_vout->p_sys->p_input )
276         return VLC_ENOOBJ;
277
278     if( var_Create( p_vout->p_sys->p_input, "snapshot-id", VLC_VAR_INTEGER ) )
279     {
280         msg_Err( p_vout, "Cannot create snapshot-id variable in p_input (%d).",
281                  p_vout->p_sys->p_input->i_object_id );
282         return VLC_EGENERIC;
283     }
284
285     /* Register the snapshot vout module at the input level */
286     val.i_int = p_vout->i_object_id;
287
288     if( var_Set( p_vout->p_sys->p_input, "snapshot-id", val ) )
289     {
290         msg_Err( p_vout, "Cannot register snapshot-id in p_input (%d).",
291                  p_vout->p_sys->p_input->i_object_id );
292         return VLC_EGENERIC;
293     }
294
295     return VLC_SUCCESS;
296 }
297
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 )
304 {
305     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
306     vlc_object_t *p_vlc;
307     int i_index;
308
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" );
313
314     p_vlc = vlc_object_find( p_this, VLC_OBJECT_ROOT, FIND_PARENT );
315     if( p_vlc )
316     {
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 );
321     }
322
323     for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
324     {
325         free( p_vout->p_sys->p_list[ i_index ]->p_data );
326     }
327     free( p_vout->p_sys->p_list );
328     /* Destroy structure */
329     free( p_vout->p_sys );
330 }
331
332 /* Return the position in ms from the start of the movie */
333 static mtime_t snapshot_GetMovietime( vout_thread_t *p_vout )
334 {
335     input_thread_t* p_input;
336     vlc_value_t val;
337     mtime_t i_result;
338
339     p_input = p_vout->p_sys->p_input;
340     if( !p_input )
341         return 0;
342
343     var_Get( p_input, "time", &val );
344
345     i_result = val.i_time;
346
347     return( i_result / 1000 );
348 }
349
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 )
356 {
357     int i_index;
358     mtime_t i_date;
359
360     i_index = p_vout->p_sys->i_index;
361
362     p_vout->p_vlc->pf_memcpy( p_vout->p_sys->p_list[i_index]->p_data,
363                               p_pic->p->p_pixels,
364                               p_vout->p_sys->i_datasize );
365
366     i_date = snapshot_GetMovietime( p_vout );
367
368     p_vout->p_sys->p_list[i_index]->date = i_date;
369
370     i_index++;
371
372     if( i_index >= p_vout->p_sys->i_size )
373     {
374         i_index = 0;
375     }
376
377     p_vout->p_sys->i_index = i_index;
378 }
379