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