]> git.sesse.net Git - vlc/blob - modules/video_output/snapshot.c
Snapshot module (dummy video output module keeping a configurable image cache).
[vlc] / modules / video_output / snapshot.c
1 /*****************************************************************************
2  * snapshot.c : snapshot plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: snapshot.c,v 1.5 2004/01/30 11:26:20 oaubert Exp $
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, 
135                      "snapshot-chroma should be 4 characters long.\n");
136             return VLC_EGENERIC;
137           }
138         i_chroma = VLC_FOURCC (psz_chroma[0],
139                                psz_chroma[1],
140                                psz_chroma[2],
141                                psz_chroma[3]);
142         free (psz_chroma);
143       }
144     else
145       {
146         msg_Err (p_vout, "Cannot find chroma information.\n");
147         return VLC_EGENERIC;
148       }
149
150     I_OUTPUTPICTURES = 0;
151
152     /* Initialize the output structure */
153     p_vout->output.i_chroma = i_chroma;
154     p_vout->output.pf_setpalette = NULL;
155     p_vout->output.i_width = i_width;
156     p_vout->output.i_height = i_height;
157     p_vout->output.i_aspect = p_vout->output.i_width
158                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
159
160
161     /* Define the bitmasks */
162     switch( i_chroma )
163       {
164       case VLC_FOURCC('R','V','1','5'):
165         p_vout->output.i_rmask = 0x001f;
166         p_vout->output.i_gmask = 0x03e0;
167         p_vout->output.i_bmask = 0x7c00;
168         break;
169         
170       case VLC_FOURCC('R','V','1','6'):
171         p_vout->output.i_rmask = 0x001f;
172         p_vout->output.i_gmask = 0x07e0;
173         p_vout->output.i_bmask = 0xf800;
174         break;
175           
176       case VLC_FOURCC('R','V','2','4'):
177         p_vout->output.i_rmask = 0xff0000;
178         p_vout->output.i_gmask = 0x00ff00;
179         p_vout->output.i_bmask = 0x0000ff;
180         break;
181         
182       case VLC_FOURCC('R','V','3','2'):
183         p_vout->output.i_rmask = 0xff0000;
184         p_vout->output.i_gmask = 0x00ff00;
185         p_vout->output.i_bmask = 0x0000ff;
186         break;
187       }
188
189     /* Try to initialize 1 direct buffer */
190     p_pic = NULL;
191
192     /* Find an empty picture slot */
193     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
194     {
195         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
196         {
197             p_pic = p_vout->p_picture + i_index;
198             break;
199         }
200     }
201     
202     /* Allocate the picture */
203     if( p_pic == NULL )
204     {
205         return VLC_SUCCESS;
206     }
207
208     vout_AllocatePicture( p_vout, p_pic, p_vout->output.i_chroma,
209                           p_vout->output.i_width, p_vout->output.i_height,
210                           p_vout->output.i_aspect );
211
212     if( p_pic->i_planes == 0 )
213     {
214         return VLC_EGENERIC;
215     }
216
217     p_pic->i_status = DESTROYED_PICTURE;
218     p_pic->i_type   = DIRECT_PICTURE;
219
220     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
221
222     I_OUTPUTPICTURES++;
223
224
225     /* Get datasize and set variables */
226     i_datasize = i_width * i_height * p_pic->p->i_pixel_pitch;
227
228     p_vout->p_sys->i_datasize = i_datasize;
229     p_vout->p_sys->i_index = 0;
230     p_vout->p_sys->i_size = config_GetInt( p_vout, "snapshot-cache-size" );
231
232     if (p_vout->p_sys->i_size < 2) 
233       {
234         msg_Err (p_vout, "snapshot-cache-size must be at least 1.");
235         return VLC_EGENERIC;
236       }
237
238     p_vout->p_sys->p_list = (snapshot_t **)malloc (p_vout->p_sys->i_size * sizeof (snapshot_t *));
239
240     if( p_vout->p_sys->p_list == NULL )
241       return VLC_ENOMEM;
242
243     /* Initialize the structures for the circular buffer */
244     for (i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++)
245       {
246         snapshot_t *p_snapshot;
247         p_snapshot = (snapshot_t *)malloc (sizeof (snapshot_t));
248
249         if( p_snapshot == NULL )
250           return VLC_ENOMEM;
251
252         p_snapshot->i_width = i_width;
253         p_snapshot->i_height = i_height;
254         p_snapshot->i_datasize = i_datasize;
255         p_snapshot->date = 0;
256         p_snapshot->p_data = (char*) malloc (i_datasize);
257         if( p_snapshot->p_data == NULL )
258           return VLC_ENOMEM;
259         p_vout->p_sys->p_list[i_index] = p_snapshot;
260       }
261
262     val.i_int = i_width;
263     var_Set (p_vout, "snapshot-width", val);
264     val.i_int = i_height;
265     var_Set (p_vout, "snapshot-height", val);
266     val.i_int = i_datasize;
267     var_Set (p_vout, "snapshot-datasize", val);
268
269     val.i_int = p_vout->p_sys->i_size;
270     var_Set (p_vout, "snapshot-cache-size", val);
271
272     val.p_address = p_vout->p_sys->p_list;
273     var_Set (p_vout, "snapshot-list-pointer", val);
274
275     /* Get the p_input pointer (to access video times) */
276     p_vout->p_sys->p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
277                                               FIND_PARENT );
278     
279     if (! p_vout->p_sys->p_input)
280       return VLC_ENOOBJ;
281
282     if (var_Create (p_vout->p_sys->p_input, "snapshot-id", VLC_VAR_INTEGER) != VLC_SUCCESS)
283       {
284         msg_Err (p_vout, "Cannot create snapshot-id variable in p_input (%d).", p_vout->p_sys->p_input->i_object_id);
285         return VLC_EGENERIC;
286       }
287
288     /* Register the snapshot vout module at the input level */
289     val.i_int = p_vout->i_object_id;
290
291     if (var_Set (p_vout->p_sys->p_input, "snapshot-id", val) != VLC_SUCCESS)
292       {
293         msg_Err (p_vout, "Cannot register snapshot-id in p_input (%d).", p_vout->p_sys->p_input->i_object_id);
294         return VLC_EGENERIC;
295       }
296
297     return VLC_SUCCESS;
298 }
299
300 /*****************************************************************************
301  * Destroy: destroy video thread
302  *****************************************************************************
303  * Terminate an output method created by Create
304  *****************************************************************************/
305 static void Destroy( vlc_object_t *p_this )
306 {
307   vout_thread_t *p_vout = (vout_thread_t *)p_this;
308   vlc_object_t *p_vlc;
309   int i_index;
310
311   fprintf (stderr, "Destroying snapshot module\n");
312   vlc_object_release(p_vout->p_sys->p_input);
313   var_Destroy (p_this, "snapshot-width");
314   var_Destroy (p_this, "snapshot-height");
315   var_Destroy (p_this, "snapshot-datasize");
316
317   p_vlc = vlc_object_find(p_this, VLC_OBJECT_ROOT, FIND_PARENT);
318   if (p_vlc)
319     {
320       /* UnRegister the snapshot vout module at the root level */
321       /* var_Destroy (p_vlc, "snapshot-id"); */
322       var_Destroy (p_this->p_libvlc, "snapshot-id");
323       vlc_object_release(p_vlc);
324     }
325
326   for (i_index = 0 ; i_index < p_vout->p_sys->i_size ; i_index++)
327     {
328       free (p_vout->p_sys->p_list[i_index]->p_data);
329     }
330   free (p_vout->p_sys->p_list);
331   /* Destroy structure */
332   free( p_vout->p_sys );
333 }
334
335 /* Return the position in ms from the start of the movie */
336 mtime_t get_movietime(vout_thread_t *p_vout)
337 {
338   input_thread_t* p_input;
339   vlc_value_t val;
340   mtime_t result;
341
342   p_input = p_vout->p_sys->p_input;
343   if (! p_input)
344     return 0;
345
346   var_Get( p_input, "time", &val );
347
348   result = val.i_time;
349
350   if (result == 0)
351     {
352       /* Either we are at the start, or (more probably) the demuxer
353          does not support the DEMUX_GET_TIME call. Try to fallback to
354          another method. */
355       stream_position_t pos;
356
357       input_Tell (p_input, &pos);
358       result = pos.i_mux_rate * 50 * pos.i_tell;
359     }
360   return (result / 1000);
361 }
362
363 /*****************************************************************************
364  * Display: displays previously rendered output
365  *****************************************************************************
366  * This function copies the rendered picture into our circular buffer.
367  *****************************************************************************/
368 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
369 {
370    int i_index;
371    mtime_t date;
372  
373    i_index = p_vout->p_sys->i_index;
374  
375    p_vout->p_vlc->pf_memcpy(
376                            p_vout->p_sys->p_list[i_index]->p_data,
377                            p_pic->p->p_pixels,
378                            p_vout->p_sys->i_datasize);
379  
380    date = get_movietime(p_vout);
381
382    p_vout->p_sys->p_list[i_index]->date = date;
383
384    i_index++;
385  
386    if (i_index >= p_vout->p_sys->i_size)
387      {
388        i_index = 0;
389      }
390  
391    p_vout->p_sys->i_index = i_index;
392  
393   return;
394 }