modules/video_output/snapshot.c: remove ref. to snapshot.h, whose data has been moved...
[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_interface.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     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     int i_index;
325
326     vlc_object_release( p_vout->p_sys->p_input );
327     var_Destroy( p_this, "snapshot-width" );
328     var_Destroy( p_this, "snapshot-height" );
329     var_Destroy( p_this, "snapshot-datasize" );
330
331     var_Destroy( p_this->p_libvlc_global, "snapshot-id" );
332
333     for( i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++ )
334     {
335         free( p_vout->p_sys->p_list[ i_index ]->p_data );
336     }
337     free( p_vout->p_sys->p_list );
338     /* Destroy structure */
339     free( p_vout->p_sys );
340 }
341
342 /* Return the position in ms from the start of the movie */
343 static mtime_t snapshot_GetMovietime( vout_thread_t *p_vout )
344 {
345     input_thread_t* p_input;
346     vlc_value_t val;
347     mtime_t i_result;
348
349     p_input = p_vout->p_sys->p_input;
350     if( !p_input )
351         return 0;
352
353     var_Get( p_input, "time", &val );
354
355     i_result = val.i_time - p_input->i_pts_delay;
356
357     return( i_result / 1000 );
358 }
359
360 /*****************************************************************************
361  * Display: displays previously rendered output
362  *****************************************************************************
363  * This function copies the rendered picture into our circular buffer.
364  *****************************************************************************/
365 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
366 {
367     int i_index;
368     mtime_t i_date;
369
370     i_index = p_vout->p_sys->i_index;
371
372     p_vout->p_libvlc->pf_memcpy( p_vout->p_sys->p_list[i_index]->p_data,
373                                  p_pic->p->p_pixels,
374                                   p_vout->p_sys->i_datasize );
375
376     i_date = snapshot_GetMovietime( p_vout );
377
378     p_vout->p_sys->p_list[i_index]->date = i_date;
379
380     i_index++;
381
382     if( i_index >= p_vout->p_sys->i_size )
383     {
384         i_index = 0;
385     }
386
387     p_vout->p_sys->i_index = i_index;
388 }
389