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