* src/misc/image.c, src/video_output/vout_intf.c: handle aspect ratio changes in...
[vlc.git] / modules / video_output / picture.c
1 /*****************************************************************************
2  * picture.c:
3  *****************************************************************************
4  * Copyright (C) 2004-2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/vout.h>
34
35 #include "vlc_image.h"
36
37 #include "picture.h"
38
39 /*****************************************************************************
40  * Local structures
41  *****************************************************************************/
42 struct vout_sys_t
43 {
44     picture_vout_t *p_picture_vout;
45     vlc_mutex_t *p_lock;
46
47     int i_picture_pos; /* picture position in p_picture_vout */
48     image_handler_t *p_image; /* filters for resizing */
49 #ifdef IMAGE_2PASSES
50     image_handler_t *p_image2;
51 #endif
52     int i_height, i_width;
53     mtime_t i_last_pic;
54 };
55
56 /* Delay after which the picture is blanked out if there hasn't been any
57  * new picture. */
58 #define BLANK_DELAY     I64C(1000000)
59
60 typedef void (* pf_release_t)( picture_t * );
61 static void ReleasePicture( picture_t *p_pic )
62 {
63     p_pic->i_refcount--;
64
65     if ( p_pic->i_refcount <= 0 )
66     {
67         if ( p_pic->p_sys != NULL )
68         {
69             pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
70             p_pic->p_sys = NULL;
71             pf_release( p_pic );
72         }
73         else
74         {
75             if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
76             if( p_pic ) free( p_pic );
77         }
78     }
79 }
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 static int  Open    ( vlc_object_t * );
85 static void Close   ( vlc_object_t * );
86 static int  Init    ( vout_thread_t * );
87 static void End     ( vout_thread_t * );
88 static int  Manage  ( vout_thread_t * );
89 static void Display ( vout_thread_t *, picture_t * );
90
91 /*****************************************************************************
92  * Module descriptor
93  *****************************************************************************/
94 #define ID_TEXT N_("ID")
95 #define ID_LONGTEXT N_( \
96     "Specify an identifier string for this subpicture" )
97
98 #define WIDTH_TEXT N_("Video width")
99 #define WIDTH_LONGTEXT N_( \
100     "Allows you to specify the output video width." )
101 #define HEIGHT_TEXT N_("Video height")
102 #define HEIGHT_LONGTEXT N_( \
103     "Allows you to specify the output video height." )
104
105 vlc_module_begin();
106     set_shortname( _( "Picture" ) );
107     set_description(_("VLC internal picture video output") );
108     set_capability( "video output", 0 );
109
110     add_string( "picture-id", "Id", NULL, ID_TEXT, ID_LONGTEXT, VLC_FALSE );
111     add_integer( "picture-width", 0, NULL, WIDTH_TEXT,
112                  WIDTH_LONGTEXT, VLC_TRUE );
113     add_integer( "picture-height", 0, NULL, HEIGHT_TEXT,
114                  HEIGHT_LONGTEXT, VLC_TRUE );
115
116     set_callbacks( Open, Close );
117
118     var_Create( p_module->p_libvlc, "picture-lock", VLC_VAR_MUTEX );
119 vlc_module_end();
120
121 /*****************************************************************************
122  * Open : allocate video thread output method
123  *****************************************************************************/
124 static int Open( vlc_object_t *p_this )
125 {
126     vout_thread_t *p_vout = (vout_thread_t *)p_this;
127     vout_sys_t *p_sys;
128     libvlc_t *p_libvlc = p_vout->p_libvlc;
129     picture_vout_t *p_picture_vout = NULL;
130     picture_vout_e_t *p_pic;
131     vlc_value_t val, lockval;
132
133     p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
134     if( p_sys == NULL )
135     {
136         msg_Err( p_vout, "out of memory" );
137         return VLC_ENOMEM;
138     }
139
140     var_Get( p_libvlc, "picture-lock", &lockval );
141     p_sys->p_lock = lockval.p_address;
142     vlc_mutex_lock( p_sys->p_lock );
143
144     p_sys->i_picture_pos = -1;
145     if( var_Get( p_libvlc, "p_picture_vout", &val ) != VLC_SUCCESS )
146     {
147         p_picture_vout = malloc( sizeof( struct picture_vout_t ) );
148         if( p_picture_vout == NULL )
149         {
150             msg_Err( p_vout, "out of memory" );
151             return VLC_ENOMEM;
152         }
153
154         var_Create( p_libvlc, "p_picture_vout", VLC_VAR_ADDRESS );
155         val.p_address = p_picture_vout;
156         var_Set( p_libvlc, "p_picture_vout", val );
157
158         p_picture_vout->i_picture_num = 0;
159         p_picture_vout->p_pic = NULL;
160     }
161     else
162     {
163         int i;
164         p_picture_vout = val.p_address;
165         for ( i = 0; i < p_picture_vout->i_picture_num; i++ )
166         {
167             if ( p_picture_vout->p_pic[i].i_status == PICTURE_VOUT_E_AVAILABLE )
168                 break;
169         }
170
171         if ( i != p_picture_vout->i_picture_num )
172             p_sys->i_picture_pos = i;
173     }
174
175     p_sys->p_picture_vout = p_picture_vout;
176
177     if ( p_sys->i_picture_pos == -1 )
178     {
179         p_picture_vout->p_pic = realloc( p_picture_vout->p_pic,
180                                          (p_picture_vout->i_picture_num + 1)
181                                            * sizeof(picture_vout_e_t) );
182         p_sys->i_picture_pos = p_picture_vout->i_picture_num;
183         p_picture_vout->i_picture_num++;
184     }
185
186     p_pic = &p_picture_vout->p_pic[p_sys->i_picture_pos];
187     p_pic->p_picture = NULL;
188     p_pic->i_status = PICTURE_VOUT_E_OCCUPIED;
189
190     var_Create( p_vout, "picture-id", VLC_VAR_STRING );
191     var_Change( p_vout, "picture-id", VLC_VAR_INHERITVALUE, &val, NULL );
192     p_pic->psz_id = val.psz_string;
193
194     vlc_mutex_unlock( p_sys->p_lock );
195
196     var_Create( p_vout, "picture-height", VLC_VAR_INTEGER );
197     var_Change( p_vout, "picture-height", VLC_VAR_INHERITVALUE, &val, NULL );
198     p_sys->i_height = val.i_int; 
199
200     var_Create( p_vout, "picture-width", VLC_VAR_INTEGER );
201     var_Change( p_vout, "picture-width", VLC_VAR_INHERITVALUE, &val, NULL );
202     p_sys->i_width = val.i_int; 
203
204     if ( p_sys->i_height || p_sys->i_width )
205     {
206         p_sys->p_image = image_HandlerCreate( p_vout );
207 #ifdef IMAGE_2PASSES
208         p_sys->p_image2 = image_HandlerCreate( p_vout );
209 #endif
210     }
211
212     p_sys->i_last_pic = 0;
213
214     p_vout->pf_init = Init;
215     p_vout->pf_end = End;
216     p_vout->pf_manage = Manage;
217     p_vout->pf_render = NULL;
218     p_vout->pf_display = Display;
219
220     return VLC_SUCCESS;
221 }
222
223
224 /*****************************************************************************
225  * Init
226  *****************************************************************************/
227 static int Init( vout_thread_t *p_vout )
228 {
229     picture_t *p_pic;
230     int i_index;
231
232     I_OUTPUTPICTURES = 0;
233
234     p_vout->output.i_chroma = p_vout->render.i_chroma;
235     p_vout->output.i_width  = p_vout->render.i_width;
236     p_vout->output.i_height = p_vout->render.i_height;
237     p_vout->output.i_aspect = p_vout->render.i_aspect;
238
239     while( VLC_TRUE )
240     {
241         p_pic = NULL;
242
243         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
244         {
245             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
246             {
247                 p_pic = p_vout->p_picture + i_index;
248                 break;
249             }
250         }
251
252         if( p_pic == NULL )
253         {
254             return VLC_SUCCESS;
255         }
256
257         vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic,
258                               p_vout->output.i_chroma,
259                               p_vout->output.i_width, p_vout->output.i_height,
260                               p_vout->output.i_aspect );
261
262         if( p_pic->i_planes == 0 )
263         {
264             return VLC_EGENERIC;
265         }
266
267         p_pic->i_status = DESTROYED_PICTURE;
268         p_pic->i_type   = DIRECT_PICTURE;
269
270         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
271
272         I_OUTPUTPICTURES++;
273
274         //return VLC_SUCCESS;
275     }
276
277 }
278
279 /*****************************************************************************
280  * End
281  *****************************************************************************/
282 static void End( vout_thread_t *p_vout )
283 {
284     return;
285 }
286
287 /*****************************************************************************
288  * Close
289  *****************************************************************************/
290 static void Close( vlc_object_t *p_this )
291 {
292     vout_thread_t *p_vout = (vout_thread_t *)p_this;
293     vout_sys_t *p_sys = p_vout->p_sys;
294     picture_vout_t *p_picture_vout = p_sys->p_picture_vout;
295     picture_vout_e_t *p_pic;
296     vlc_bool_t b_last_picture = VLC_TRUE;
297     int i;
298
299     vlc_mutex_lock( p_sys->p_lock );
300     p_pic = &p_picture_vout->p_pic[p_sys->i_picture_pos];
301
302     if( p_pic->p_picture )
303     {
304         p_pic->p_picture->pf_release( p_pic->p_picture );
305         p_pic->p_picture = NULL;
306     }
307     p_pic->i_status = PICTURE_VOUT_E_AVAILABLE;
308     if( p_pic->psz_id )
309         free( p_pic->psz_id );
310
311     for( i = 0; i < p_picture_vout->i_picture_num; i ++)
312     {
313         if( p_picture_vout->p_pic[i].i_status == PICTURE_VOUT_E_OCCUPIED )
314         {
315             b_last_picture = VLC_FALSE;
316             break;
317         }
318     }
319
320     if( b_last_picture )
321     {
322         free( p_picture_vout->p_pic );
323         free( p_picture_vout );
324         var_Destroy( p_this->p_libvlc, "p_picture_vout" );
325     }
326
327     vlc_mutex_unlock( p_sys->p_lock );
328
329     if ( p_sys->i_height || p_sys->i_width )
330     {
331         image_HandlerDelete( p_sys->p_image );
332 #ifdef IMAGE_2PASSES
333         image_HandlerDelete( p_sys->p_image2 );
334 #endif
335     }
336
337     free( p_sys );
338 }
339
340 /*****************************************************************************
341  * PushPicture : push a picture in the p_picture_vout structure
342  *****************************************************************************/
343 static void PushPicture( vout_thread_t *p_vout, picture_t *p_picture )
344 {
345     vout_sys_t *p_sys = p_vout->p_sys;
346     picture_vout_t *p_picture_vout = p_sys->p_picture_vout;
347     picture_vout_e_t *p_pic;
348
349     vlc_mutex_lock( p_sys->p_lock );
350     p_pic = &p_picture_vout->p_pic[p_sys->i_picture_pos];
351
352     if( p_pic->p_picture != NULL )
353     {
354         p_pic->p_picture->pf_release( p_pic->p_picture );
355     }
356     p_pic->p_picture = p_picture;
357
358     vlc_mutex_unlock( p_sys->p_lock );
359 }
360
361 /*****************************************************************************
362  * Manage
363  *****************************************************************************/
364 static int Manage( vout_thread_t *p_vout )
365 {
366     vout_sys_t *p_sys = p_vout->p_sys;
367
368     if ( mdate() - p_sys->i_last_pic > BLANK_DELAY )
369     {
370         /* Display black */
371 #if 0
372         picture_t *p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
373         int i;
374
375         if ( p_sys->i_height || p_sys->i_width )
376         {
377             vout_AllocatePicture( p_vout, p_new_pic,
378                                   VLC_FOURCC('Y','U','V','A'),
379                                   p_sys->i_width, p_sys->i_height,
380                                   p_vout->render.i_aspect );
381         }
382         else
383         {
384             vout_AllocatePicture( p_vout, p_new_pic, p_vout->render.i_chroma,
385                                   p_vout->render.i_width,
386                                   p_vout->render.i_height,
387                                   p_vout->render.i_aspect );
388         }
389
390         p_new_pic->i_refcount++;
391         p_new_pic->i_status = DESTROYED_PICTURE;
392         p_new_pic->i_type   = DIRECT_PICTURE;
393         p_new_pic->pf_release = ReleasePicture;
394
395         for ( i = 0; i < p_pic->i_planes; i++ )
396         {
397             /* This assumes planar YUV format */
398             p_vout->p_vlc->pf_memset( p_pic->p[i].p_pixels, i ? 0x80 : 0,
399                                       p_pic->p[i].i_lines
400                                        * p_pic->p[i].i_pitch );
401         }
402
403         PushPicture( p_vout, p_new_pic );
404 #else
405         PushPicture( p_vout, NULL );
406 #endif
407         p_sys->i_last_pic = INT64_MAX;
408     }
409
410     return VLC_SUCCESS;
411 }
412
413 /*****************************************************************************
414  * Display
415  *****************************************************************************/
416 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
417 {
418     vout_sys_t *p_sys = p_vout->p_sys;
419     picture_t *p_new_pic;
420
421     if ( p_sys->i_height || p_sys->i_width )
422     {
423         video_format_t fmt_out = {0}, fmt_in = {0};
424 #ifdef IMAGE_2PASSES
425         vide_format_t fmt_middle = {0};
426         picture_t *p_new_pic2;
427 #endif
428
429         fmt_in = p_vout->fmt_in;
430
431 #ifdef IMAGE_2PASSES
432         fmt_middle.i_chroma = p_vout->render.i_chroma;
433         fmt_middle.i_width = p_vout->p_sys->i_width;
434         fmt_middle.i_height = p_vout->p_sys->i_height;
435         fmt_middle.i_visible_width = fmt_middle.i_width;
436         fmt_middle.i_visible_height = fmt_middle.i_height;
437
438         p_new_pic2 = image_Convert( p_vout->p_sys->p_image2,
439                                     p_pic, &fmt_in, &fmt_middle );
440         if ( p_new_pic2 == NULL )
441         {
442             msg_Err( p_vout, "image resizing failed %dx%d->%dx%d %4.4s",
443                      p_vout->render.i_width, p_vout->render.i_height,
444                      fmt_middle.i_width, fmt_middle.i_height,
445                      (char *)&p_vout->render.i_chroma);
446             return;
447         }
448 #endif
449
450         fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
451         fmt_out.i_width = p_sys->i_width;
452         fmt_out.i_height = p_sys->i_height;
453         fmt_out.i_visible_width = fmt_out.i_width;
454         fmt_out.i_visible_height = fmt_out.i_height;
455
456 #ifdef IMAGE_2PASSES
457         p_new_pic = image_Convert( p_vout->p_sys->p_image,
458                                    p_new_pic2, &fmt_middle, &fmt_out );
459         p_new_pic2->pf_release( p_new_pic2 );
460 #else
461         p_new_pic = image_Convert( p_vout->p_sys->p_image,
462                                    p_pic, &fmt_in, &fmt_out );
463 #endif
464         if ( p_new_pic == NULL )
465         {
466             msg_Err( p_vout, "image conversion failed" );
467             return;
468         }
469     }
470     else
471     {
472         p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
473         vout_AllocatePicture( p_vout, p_new_pic, p_pic->format.i_chroma,
474                               p_pic->format.i_width, p_pic->format.i_height,
475                               p_vout->render.i_aspect );
476
477         vout_CopyPicture( p_vout, p_new_pic, p_pic );
478     }
479
480     p_new_pic->i_refcount = 1;
481     p_new_pic->i_status = DESTROYED_PICTURE;
482     p_new_pic->i_type   = DIRECT_PICTURE;
483     p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
484     p_new_pic->pf_release = ReleasePicture;
485
486     PushPicture( p_vout, p_new_pic );
487     p_sys->i_last_pic = p_pic->date;
488 }