]> git.sesse.net Git - vlc/blob - modules/stream_out/mosaic_bridge.c
fixed compilation on debian etch.
[vlc] / modules / stream_out / mosaic_bridge.c
1 /*****************************************************************************
2  * mosaic_bridge.c:
3  *****************************************************************************
4  * Copyright (C) 2004-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <errno.h>                                                 /* ENOMEM */
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_sout.h>
38 #include <vlc_block.h>
39 #include <vlc_codec.h>
40
41 #include <vlc_image.h>
42 #include <vlc_filter.h>
43
44 #include "../video_filter/mosaic.h"
45
46 #include <assert.h>
47
48 /*****************************************************************************
49  * Local structures
50  *****************************************************************************/
51 struct sout_stream_sys_t
52 {
53     bridged_es_t *p_es;
54     vlc_mutex_t *p_lock;
55
56     decoder_t       *p_decoder;
57     image_handler_t *p_image; /* filter for resizing */
58     int i_height, i_width;
59     unsigned int i_sar_num, i_sar_den;
60     char *psz_id;
61     bool b_inited;
62
63     int i_chroma; /* force image format chroma */
64
65     filter_chain_t *p_vf2;
66 };
67
68 #define PICTURE_RING_SIZE 4
69 struct decoder_owner_sys_t
70 {
71     picture_t *pp_pics[PICTURE_RING_SIZE];
72
73     /* Current format in use by the output */
74     video_format_t video;
75 };
76
77 typedef void (* pf_release_t)( picture_t * );
78 static void ReleasePicture( picture_t *p_pic )
79 {
80     assert( p_pic );
81
82     if( --p_pic->i_refcount > 0 )
83         return;
84
85     assert( p_pic->p_sys );
86     if( p_pic->p_sys )
87     {
88         pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
89         p_pic->p_sys = NULL;
90         pf_release( p_pic );
91     }
92     else
93     {
94         free( p_pic->p_data_orig );
95         free( p_pic );
96     }
97 }
98
99 /*****************************************************************************
100  * Local prototypes
101  *****************************************************************************/
102 static int  Open    ( vlc_object_t * );
103 static void Close   ( vlc_object_t * );
104 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
105 static int               Del ( sout_stream_t *, sout_stream_id_t * );
106 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t * );
107
108 inline static void video_del_buffer_decoder( decoder_t *, picture_t * );
109 inline static void video_del_buffer_filter( filter_t *, picture_t * );
110 static void video_del_buffer( picture_t * );
111
112 inline static picture_t *video_new_buffer_decoder( decoder_t * );
113 inline static picture_t *video_new_buffer_filter( filter_t * );
114 static picture_t *video_new_buffer( vlc_object_t *, decoder_owner_sys_t *,
115                                     es_format_t *, void (*)( picture_t * ) );
116
117 static void video_link_picture_decoder( decoder_t *, picture_t * );
118 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
119
120 static int HeightCallback( vlc_object_t *, char const *,
121                            vlc_value_t, vlc_value_t, void * );
122 static int WidthCallback( vlc_object_t *, char const *,
123                           vlc_value_t, vlc_value_t, void * );
124 static int alphaCallback( vlc_object_t *, char const *,
125                           vlc_value_t, vlc_value_t, void * );
126 static int xCallback( vlc_object_t *, char const *,
127                       vlc_value_t, vlc_value_t, void * );
128 static int yCallback( vlc_object_t *, char const *,
129                       vlc_value_t, vlc_value_t, void * );
130
131 /*****************************************************************************
132  * Module descriptor
133  *****************************************************************************/
134 #define ID_TEXT N_("ID")
135 #define ID_LONGTEXT N_( \
136     "Specify an identifier string for this subpicture" )
137
138 #define WIDTH_TEXT N_("Video width")
139 #define WIDTH_LONGTEXT N_( \
140     "Output video width." )
141 #define HEIGHT_TEXT N_("Video height")
142 #define HEIGHT_LONGTEXT N_( \
143     "Output video height." )
144 #define RATIO_TEXT N_("Sample aspect ratio")
145 #define RATIO_LONGTEXT N_( \
146     "Sample aspect ratio of the destination (1:1, 3:4, 2:3)." )
147
148 #define VFILTER_TEXT N_("Video filter")
149 #define VFILTER_LONGTEXT N_( \
150     "Video filters will be applied to the video stream." )
151
152 #define CHROMA_TEXT N_("Image chroma")
153 #define CHROMA_LONGTEXT N_( \
154     "Force the use of a specific chroma. Use YUVA if you're planning " \
155     "to use the Alphamask or Bluescreen video filter." )
156
157 #define ALPHA_TEXT N_("Transparency")
158 #define ALPHA_LONGTEXT N_( \
159     "Transparency of the mosaic picture." )
160
161 #define X_TEXT N_("X offset")
162 #define X_LONGTEXT N_( \
163     "X coordinate of the upper left corner in the mosaic if non negative." )
164
165 #define Y_TEXT N_("Y offset")
166 #define Y_LONGTEXT N_( \
167     "Y coordinate of the upper left corner in the mosaic if non negative." )
168
169 #define CFG_PREFIX "sout-mosaic-bridge-"
170
171 vlc_module_begin();
172     set_shortname( N_( "Mosaic bridge" ) );
173     set_description(N_("Mosaic bridge stream output") );
174     set_capability( "sout stream", 0 );
175     add_shortcut( "mosaic-bridge" );
176
177     add_string( CFG_PREFIX "id", "Id", NULL, ID_TEXT, ID_LONGTEXT,
178                 false );
179     add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
180                  WIDTH_LONGTEXT, true );
181     add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
182                  HEIGHT_LONGTEXT, true );
183     add_string( CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
184                 RATIO_LONGTEXT, false );
185     add_string( CFG_PREFIX "chroma", 0, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
186                 false );
187
188     add_module_list( CFG_PREFIX "vfilter", "video filter2",
189                      NULL, NULL, VFILTER_TEXT, VFILTER_LONGTEXT, false );
190
191     add_integer_with_range( CFG_PREFIX "alpha", 255, 0, 255, NULL,
192                             ALPHA_TEXT, ALPHA_LONGTEXT, false );
193     add_integer( CFG_PREFIX "x", -1, NULL, X_TEXT, X_LONGTEXT, false );
194     add_integer( CFG_PREFIX "y", -1, NULL, Y_TEXT, Y_LONGTEXT, false );
195
196     set_callbacks( Open, Close );
197 vlc_module_end();
198
199 static const char *const ppsz_sout_options[] = {
200     "id", "width", "height", "sar", "vfilter", "chroma", "alpha", "x", "y", NULL
201 };
202
203 /*****************************************************************************
204  * Open
205  *****************************************************************************/
206 static int Open( vlc_object_t *p_this )
207 {
208     sout_stream_t        *p_stream = (sout_stream_t *)p_this;
209     sout_stream_sys_t    *p_sys;
210     vlc_object_t         *p_libvlc = VLC_OBJECT( p_this->p_libvlc );
211     vlc_value_t           val;
212
213     config_ChainParse( p_stream, CFG_PREFIX, ppsz_sout_options,
214                        p_stream->p_cfg );
215
216     p_sys = malloc( sizeof( sout_stream_sys_t ) );
217     if( !p_sys )
218     {
219         return VLC_ENOMEM;
220     }
221
222     p_stream->p_sys = p_sys;
223     p_sys->b_inited = false;
224
225     var_Create( p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
226     var_Get( p_libvlc, "mosaic-lock", &val );
227     p_sys->p_lock = val.p_address;
228
229     var_Get( p_stream, CFG_PREFIX "id", &val );
230     p_sys->psz_id = val.psz_string;
231
232     p_sys->i_height =
233         var_CreateGetIntegerCommand( p_stream, CFG_PREFIX "height" );
234     var_AddCallback( p_stream, CFG_PREFIX "height", HeightCallback, p_stream );
235
236     p_sys->i_width =
237         var_CreateGetIntegerCommand( p_stream, CFG_PREFIX "width" );
238     var_AddCallback( p_stream, CFG_PREFIX "width", WidthCallback, p_stream );
239
240     var_Get( p_stream, CFG_PREFIX "sar", &val );
241     if ( val.psz_string )
242     {
243         char *psz_parser = strchr( val.psz_string, ':' );
244
245         if( psz_parser )
246         {
247             *psz_parser++ = '\0';
248             p_sys->i_sar_num = atoi( val.psz_string );
249             p_sys->i_sar_den = atoi( psz_parser );
250             vlc_ureduce( &p_sys->i_sar_num, &p_sys->i_sar_den,
251                          p_sys->i_sar_num, p_sys->i_sar_den, 0 );
252         }
253         else
254         {
255             msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
256             p_sys->i_sar_num = p_sys->i_sar_den = 1;
257         }
258
259         free( val.psz_string );
260     }
261     else
262     {
263         p_sys->i_sar_num = p_sys->i_sar_den = 1;
264     }
265
266     p_sys->i_chroma = 0;
267     val.psz_string = var_GetNonEmptyString( p_stream, CFG_PREFIX "chroma" );
268     if( val.psz_string && strlen( val.psz_string ) >= 4 )
269     {
270         memcpy( &p_sys->i_chroma, val.psz_string, 4 );
271         msg_Dbg( p_stream, "Forcing image chroma to 0x%.8x (%4.4s)", p_sys->i_chroma, (char*)&p_sys->i_chroma );
272     }
273
274 #define INT_COMMAND( a ) \
275     var_Create( p_stream, CFG_PREFIX #a, \
276                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND ); \
277     var_AddCallback( p_stream, CFG_PREFIX #a, a ## Callback, \
278                      p_stream );
279     INT_COMMAND( alpha )
280     INT_COMMAND( x )
281     INT_COMMAND( y )
282
283     p_stream->pf_add    = Add;
284     p_stream->pf_del    = Del;
285     p_stream->pf_send   = Send;
286
287     p_stream->p_sout->i_out_pace_nocontrol++;
288
289     return VLC_SUCCESS;
290 }
291
292 /*****************************************************************************
293  * Close
294  *****************************************************************************/
295 static void Close( vlc_object_t * p_this )
296 {
297     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
298     sout_stream_sys_t *p_sys = p_stream->p_sys;
299
300     p_stream->p_sout->i_out_pace_nocontrol--;
301
302     free( p_sys->psz_id );
303
304     free( p_sys );
305 }
306
307 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
308 {
309     p_filter->pf_vout_buffer_new = video_new_buffer_filter;
310     p_filter->pf_vout_buffer_del = video_del_buffer_filter;
311     p_filter->p_owner = p_data;
312     return VLC_SUCCESS;
313 }
314
315 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
316 {
317     sout_stream_sys_t *p_sys = p_stream->p_sys;
318     bridge_t *p_bridge;
319     bridged_es_t *p_es;
320     char *psz_chain;
321     int i;
322
323     if ( p_sys->b_inited )
324     {
325         return NULL;
326     }
327
328     /* Create decoder object */
329     p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
330     if( !p_sys->p_decoder )
331         return NULL;
332     vlc_object_attach( p_sys->p_decoder, p_stream );
333     p_sys->p_decoder->p_module = NULL;
334     p_sys->p_decoder->fmt_in = *p_fmt;
335     p_sys->p_decoder->b_pace_control = false;
336     p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
337     p_sys->p_decoder->fmt_out.i_extra = 0;
338     p_sys->p_decoder->fmt_out.p_extra = 0;
339     p_sys->p_decoder->pf_decode_video = 0;
340     p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
341     p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
342     p_sys->p_decoder->pf_picture_link    = video_link_picture_decoder;
343     p_sys->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
344     p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
345     if( !p_sys->p_decoder->p_owner )
346     {
347         vlc_object_detach( p_sys->p_decoder );
348         vlc_object_release( p_sys->p_decoder );
349         return NULL;
350     }
351
352     for( i = 0; i < PICTURE_RING_SIZE; i++ )
353         p_sys->p_decoder->p_owner->pp_pics[i] = NULL;
354     p_sys->p_decoder->p_owner->video = p_fmt->video;
355     //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
356
357     p_sys->p_decoder->p_module =
358         module_Need( p_sys->p_decoder, "decoder", "$codec", 0 );
359
360     if( !p_sys->p_decoder->p_module )
361     {
362         msg_Err( p_stream, "cannot find decoder" );
363         vlc_object_detach( p_sys->p_decoder );
364         vlc_object_release( p_sys->p_decoder );
365         return NULL;
366     }
367
368     p_sys->b_inited = true;
369     vlc_mutex_lock( p_sys->p_lock );
370
371     p_bridge = GetBridge( p_stream );
372     if ( p_bridge == NULL )
373     {
374         vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
375         vlc_value_t val;
376
377         p_bridge = malloc( sizeof( bridge_t ) );
378
379         var_Create( p_libvlc, "mosaic-struct", VLC_VAR_ADDRESS );
380         val.p_address = p_bridge;
381         var_Set( p_libvlc, "mosaic-struct", val );
382
383         p_bridge->i_es_num = 0;
384         p_bridge->pp_es = NULL;
385     }
386
387     for ( i = 0; i < p_bridge->i_es_num; i++ )
388     {
389         if ( p_bridge->pp_es[i]->b_empty )
390             break;
391     }
392
393     if ( i == p_bridge->i_es_num )
394     {
395         p_bridge->pp_es = realloc( p_bridge->pp_es,
396                                    (p_bridge->i_es_num + 1)
397                                      * sizeof(bridged_es_t *) );
398         p_bridge->i_es_num++;
399         p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
400     }
401
402     p_sys->p_es = p_es = p_bridge->pp_es[i];
403
404     p_es->i_alpha = var_GetInteger( p_stream, CFG_PREFIX "alpha" );
405     p_es->i_x = var_GetInteger( p_stream, CFG_PREFIX "x" );
406     p_es->i_y = var_GetInteger( p_stream, CFG_PREFIX "y" );
407
408     //p_es->fmt = *p_fmt;
409     p_es->psz_id = p_sys->psz_id;
410     p_es->p_picture = NULL;
411     p_es->pp_last = &p_es->p_picture;
412     p_es->b_empty = false;
413
414     vlc_mutex_unlock( p_sys->p_lock );
415
416     if ( p_sys->i_height || p_sys->i_width )
417     {
418         p_sys->p_image = image_HandlerCreate( p_stream );
419     }
420     else
421     {
422         p_sys->p_image = NULL;
423     }
424
425     msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
426
427     /* Create user specified video filters */
428     psz_chain = var_GetNonEmptyString( p_stream, CFG_PREFIX "vfilter" );
429     msg_Dbg( p_stream, "psz_chain: %s\n", psz_chain );
430     if( psz_chain )
431     {
432         p_sys->p_vf2 = filter_chain_New( p_stream, "video filter2", false,
433                                          video_filter_buffer_allocation_init,
434                                          NULL, p_sys->p_decoder->p_owner );
435         es_format_t fmt;
436         es_format_Copy( &fmt, &p_sys->p_decoder->fmt_out );
437         if( p_sys->i_chroma )
438             fmt.video.i_chroma = p_sys->i_chroma;
439         filter_chain_Reset( p_sys->p_vf2, &fmt, &fmt );
440         filter_chain_AppendFromString( p_sys->p_vf2, psz_chain );
441         free( psz_chain );
442     }
443
444     return (sout_stream_id_t *)p_sys;
445 }
446
447 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
448 {
449     VLC_UNUSED(id);
450     sout_stream_sys_t *p_sys = p_stream->p_sys;
451     bridge_t *p_bridge;
452     bridged_es_t *p_es;
453     bool b_last_es = true;
454     int i;
455
456     if ( !p_sys->b_inited )
457     {
458         return VLC_SUCCESS;
459     }
460
461     if ( p_sys->p_decoder != NULL )
462     {
463         decoder_owner_sys_t *p_owner = p_sys->p_decoder->p_owner;
464
465         if( p_sys->p_decoder->p_module )
466             module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
467         vlc_object_detach( p_sys->p_decoder );
468         vlc_object_release( p_sys->p_decoder );
469
470         picture_t **pp_ring = p_owner->pp_pics;
471         for( i = 0; i < PICTURE_RING_SIZE; i++ )
472         {
473             if ( pp_ring[i] != NULL )
474             {
475                 free( pp_ring[i]->p_data_orig );
476                 free( pp_ring[i]->p_sys );
477                 free( pp_ring[i] );
478             }
479         }
480         free( p_owner );
481     }
482
483     /* Destroy user specified video filters */
484     filter_chain_Delete( p_sys->p_vf2 );
485
486     vlc_mutex_lock( p_sys->p_lock );
487
488     p_bridge = GetBridge( p_stream );
489     p_es = p_sys->p_es;
490
491     p_es->b_empty = true;
492     while ( p_es->p_picture )
493     {
494         picture_t *p_next = p_es->p_picture->p_next;
495         p_es->p_picture->pf_release( p_es->p_picture );
496         p_es->p_picture = p_next;
497     }
498
499     for ( i = 0; i < p_bridge->i_es_num; i++ )
500     {
501         if ( !p_bridge->pp_es[i]->b_empty )
502         {
503             b_last_es = false;
504             break;
505         }
506     }
507
508     if ( b_last_es )
509     {
510         vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
511         for ( i = 0; i < p_bridge->i_es_num; i++ )
512             free( p_bridge->pp_es[i] );
513         free( p_bridge->pp_es );
514         free( p_bridge );
515         var_Destroy( p_libvlc, "mosaic-struct" );
516     }
517
518     vlc_mutex_unlock( p_sys->p_lock );
519
520     if ( p_sys->p_image )
521     {
522         image_HandlerDelete( p_sys->p_image );
523     }
524
525     p_sys->b_inited = false;
526
527     return VLC_SUCCESS;
528 }
529
530 /*****************************************************************************
531  * PushPicture : push a picture in the mosaic-struct structure
532  *****************************************************************************/
533 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
534 {
535     sout_stream_sys_t *p_sys = p_stream->p_sys;
536     bridged_es_t *p_es = p_sys->p_es;
537
538     vlc_mutex_lock( p_sys->p_lock );
539
540     *p_es->pp_last = p_picture;
541     p_picture->p_next = NULL;
542     p_es->pp_last = &p_picture->p_next;
543
544     vlc_mutex_unlock( p_sys->p_lock );
545 }
546
547 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
548                  block_t *p_buffer )
549 {
550     sout_stream_sys_t *p_sys = p_stream->p_sys;
551     picture_t *p_pic;
552
553     if ( (sout_stream_sys_t *)id != p_sys )
554     {
555         block_ChainRelease( p_buffer );
556         return VLC_SUCCESS;
557     }
558
559     while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
560                                                         &p_buffer )) )
561     {
562         picture_t *p_new_pic;
563
564         if( p_sys->i_height || p_sys->i_width )
565         {
566             video_format_t fmt_out, fmt_in;
567
568             memset( &fmt_in, 0, sizeof(video_format_t) );
569             memset( &fmt_out, 0, sizeof(video_format_t) );
570             fmt_in = p_sys->p_decoder->fmt_out.video;
571
572
573             if( p_sys->i_chroma )
574                 fmt_out.i_chroma = p_sys->i_chroma;
575             else
576                 fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
577
578             if ( !p_sys->i_height )
579             {
580                 fmt_out.i_width = p_sys->i_width;
581                 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
582                     * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
583                       & ~0x1;
584             }
585             else if ( !p_sys->i_width )
586             {
587                 fmt_out.i_height = p_sys->i_height;
588                 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
589                     * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
590                       & ~0x1;
591             }
592             else
593             {
594                 fmt_out.i_width = p_sys->i_width;
595                 fmt_out.i_height = p_sys->i_height;
596             }
597             fmt_out.i_visible_width = fmt_out.i_width;
598             fmt_out.i_visible_height = fmt_out.i_height;
599
600             p_new_pic = image_Convert( p_sys->p_image,
601                                        p_pic, &fmt_in, &fmt_out );
602             if ( p_new_pic == NULL )
603             {
604                 msg_Err( p_stream, "image conversion failed" );
605                 p_pic->pf_release( p_pic );
606                 continue;
607             }
608         }
609         else
610         {
611             /* TODO: chroma conversion if needed */
612
613             p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
614             if( p_new_pic == NULL )
615             {
616                 msg_Err( p_stream, "image conversion failed" );
617                 continue;
618             }
619
620             if( vout_AllocatePicture(
621                                   p_stream, p_new_pic, p_pic->format.i_chroma,
622                                   p_pic->format.i_width, p_pic->format.i_height,
623                                   p_sys->p_decoder->fmt_out.video.i_aspect )
624                 != VLC_SUCCESS )
625             {
626                 p_pic->pf_release( p_pic );
627                 free( p_new_pic );
628                 msg_Err( p_stream, "image allocation failed" );
629                 continue;
630             }
631
632             vout_CopyPicture( p_stream, p_new_pic, p_pic );
633         }
634
635         p_new_pic->i_refcount = 1;
636         p_new_pic->i_status = DESTROYED_PICTURE;
637         p_new_pic->i_type   = DIRECT_PICTURE;
638         p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
639         p_new_pic->pf_release = ReleasePicture;
640         p_new_pic->date = p_pic->date;
641         p_pic->pf_release( p_pic );
642
643         if( p_sys->p_vf2 )
644             p_new_pic = filter_chain_VideoFilter( p_sys->p_vf2, p_new_pic );
645
646         PushPicture( p_stream, p_new_pic );
647     }
648
649     return VLC_SUCCESS;
650 }
651
652 struct picture_sys_t
653 {
654     vlc_object_t *p_owner;
655     bool b_dead;
656 };
657
658 static void video_release_buffer_decoder( picture_t *p_pic )
659 {
660     assert( p_pic && p_pic->p_sys );
661
662     if( --p_pic->i_refcount > 0 )
663         return;
664
665     video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
666 }
667
668 static void video_release_buffer_filter( picture_t *p_pic )
669 {
670     assert( p_pic );
671
672     if( --p_pic->i_refcount > 0 )
673         return;
674
675     assert( p_pic->p_sys );
676
677     video_del_buffer_filter( (filter_t *)p_pic->p_sys->p_owner, p_pic );
678 }
679
680 inline static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
681 {
682     return video_new_buffer( VLC_OBJECT( p_dec ),
683                              (decoder_owner_sys_t *)p_dec->p_owner,
684                              &p_dec->fmt_out,
685                              video_release_buffer_decoder );
686 }
687
688 inline static picture_t *video_new_buffer_filter( filter_t *p_filter )
689 {
690     return video_new_buffer( VLC_OBJECT( p_filter ),
691                              (decoder_owner_sys_t *)p_filter->p_owner,
692                              &p_filter->fmt_out,
693                              video_release_buffer_filter );
694 }
695
696 static picture_t *video_new_buffer( vlc_object_t *p_this,
697                                     decoder_owner_sys_t *p_sys,
698                                     es_format_t *fmt_out,
699                                     void ( *pf_release )( picture_t * ) )
700 {
701     picture_t **pp_ring = p_sys->pp_pics;
702     picture_t *p_pic;
703     int i;
704
705     if( fmt_out->video.i_width != p_sys->video.i_width ||
706         fmt_out->video.i_height != p_sys->video.i_height ||
707         fmt_out->video.i_chroma != p_sys->video.i_chroma ||
708         fmt_out->video.i_aspect != p_sys->video.i_aspect )
709     {
710         if( !fmt_out->video.i_sar_num ||
711             !fmt_out->video.i_sar_den )
712         {
713             fmt_out->video.i_sar_num =
714                 fmt_out->video.i_aspect * fmt_out->video.i_height;
715
716             fmt_out->video.i_sar_den =
717                 VOUT_ASPECT_FACTOR * fmt_out->video.i_width;
718         }
719
720         vlc_ureduce( &fmt_out->video.i_sar_num,
721                      &fmt_out->video.i_sar_den,
722                      fmt_out->video.i_sar_num,
723                      fmt_out->video.i_sar_den, 0 );
724
725         if( !fmt_out->video.i_visible_width ||
726             !fmt_out->video.i_visible_height )
727         {
728             fmt_out->video.i_visible_width = fmt_out->video.i_width;
729             fmt_out->video.i_visible_height = fmt_out->video.i_height;
730         }
731
732         fmt_out->video.i_chroma = fmt_out->i_codec;
733         p_sys->video = fmt_out->video;
734
735         for( i = 0; i < PICTURE_RING_SIZE; i++ )
736         {
737             if ( pp_ring[i] != NULL )
738             {
739                 if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
740                 {
741                     free( pp_ring[i]->p_data_orig );
742                     free( pp_ring[i]->p_sys );
743                     free( pp_ring[i] );
744                 }
745                 else
746                 {
747                     pp_ring[i]->p_sys->b_dead = true;
748                 }
749                 pp_ring[i] = NULL;
750             }
751         }
752     }
753
754     /* Find an empty space in the picture ring buffer */
755     for( i = 0; i < PICTURE_RING_SIZE; i++ )
756     {
757         if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
758         {
759             pp_ring[i]->i_status = RESERVED_PICTURE;
760             pp_ring[i]->i_refcount = 1;
761             return pp_ring[i];
762         }
763     }
764     for( i = 0; i < PICTURE_RING_SIZE; i++ )
765     {
766         if( pp_ring[i] == NULL ) break;
767     }
768
769     if( i == PICTURE_RING_SIZE )
770     {
771         msg_Err( p_this, "decoder/filter is leaking pictures, "
772                  "resetting its ring buffer" );
773
774         for( i = 0; i < PICTURE_RING_SIZE; i++ )
775         {
776             pp_ring[i]->p_sys->b_dead = true;
777             pp_ring[i]->pf_release( pp_ring[i] );
778             pp_ring[i] = NULL;
779         }
780
781         i = 0;
782     }
783
784     p_pic = malloc( sizeof(picture_t) );
785     if( !p_pic ) return NULL;
786     fmt_out->video.i_chroma = fmt_out->i_codec;
787     if( vout_AllocatePicture( p_this, p_pic,
788                           fmt_out->video.i_chroma,
789                           fmt_out->video.i_width,
790                           fmt_out->video.i_height,
791                           fmt_out->video.i_aspect ) != VLC_SUCCESS )
792     {
793         free( p_pic );
794         return NULL;
795     }
796
797     if( !p_pic->i_planes )
798     {
799         free( p_pic );
800         return NULL;
801     }
802
803     p_pic->pf_release = pf_release;
804     p_pic->i_refcount = 1;
805     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
806     p_pic->p_sys->p_owner = p_this;
807     p_pic->p_sys->b_dead = false;
808     p_pic->i_status = RESERVED_PICTURE;
809
810     pp_ring[i] = p_pic;
811
812     return p_pic;
813 }
814
815 inline static void video_del_buffer_decoder( decoder_t *p_this,
816                                              picture_t *p_pic )
817 {
818     VLC_UNUSED(p_this);
819     video_del_buffer( p_pic );
820 }
821
822 inline static void video_del_buffer_filter( filter_t *p_this,
823                                             picture_t *p_pic )
824 {
825     VLC_UNUSED(p_this);
826     video_del_buffer( p_pic );
827 }
828
829 static void video_del_buffer( picture_t *p_pic )
830 {
831     p_pic->i_refcount = 0;
832     p_pic->i_status = DESTROYED_PICTURE;
833     if ( p_pic->p_sys->b_dead )
834     {
835         free( p_pic->p_data_orig );
836         free( p_pic->p_sys );
837         free( p_pic );
838     }
839 }
840
841 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
842 {
843     VLC_UNUSED(p_dec);
844     p_pic->i_refcount++;
845 }
846
847 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
848 {
849     VLC_UNUSED(p_dec);
850     video_release_buffer_decoder( p_pic );
851 }
852
853
854 /**********************************************************************
855  * Callback to update (some) params on the fly
856  **********************************************************************/
857 static int HeightCallback( vlc_object_t *p_this, char const *psz_var,
858                            vlc_value_t oldval, vlc_value_t newval,
859                            void *p_data )
860 {
861     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
862     sout_stream_t *p_stream = (sout_stream_t *)p_data;
863     sout_stream_sys_t *p_sys = p_stream->p_sys;
864
865     /* We create the handler before updating the value in p_sys
866      * so we don't have to worry about locking */
867     if( !p_sys->p_image && newval.i_int )
868         p_sys->p_image = image_HandlerCreate( p_stream );
869     p_sys->i_height = newval.i_int;
870
871     return VLC_SUCCESS;
872 }
873
874 static int WidthCallback( vlc_object_t *p_this, char const *psz_var,
875                            vlc_value_t oldval, vlc_value_t newval,
876                            void *p_data )
877 {
878     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
879     sout_stream_t *p_stream = (sout_stream_t *)p_data;
880     sout_stream_sys_t *p_sys = p_stream->p_sys;
881
882     /* We create the handler before updating the value in p_sys
883      * so we don't have to worry about locking */
884     if( !p_sys->p_image && newval.i_int )
885         p_sys->p_image = image_HandlerCreate( p_stream );
886     p_sys->i_width = newval.i_int;
887
888     return VLC_SUCCESS;
889 }
890
891 static int alphaCallback( vlc_object_t *p_this, char const *psz_var,
892                           vlc_value_t oldval, vlc_value_t newval,
893                           void *p_data )
894 {
895     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
896     sout_stream_t *p_stream = (sout_stream_t *)p_data;
897     sout_stream_sys_t *p_sys = p_stream->p_sys;
898
899     if( p_sys->p_es )
900         p_sys->p_es->i_alpha = newval.i_int;
901
902     return VLC_SUCCESS;
903 }
904
905 static int xCallback( vlc_object_t *p_this, char const *psz_var,
906                       vlc_value_t oldval, vlc_value_t newval,
907                       void *p_data )
908 {
909     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
910     sout_stream_t *p_stream = (sout_stream_t *)p_data;
911     sout_stream_sys_t *p_sys = p_stream->p_sys;
912
913     if( p_sys->p_es )
914         p_sys->p_es->i_x = newval.i_int;
915
916     return VLC_SUCCESS;
917 }
918
919 static int yCallback( vlc_object_t *p_this, char const *psz_var,
920                       vlc_value_t oldval, vlc_value_t newval,
921                       void *p_data )
922 {
923     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
924     sout_stream_t *p_stream = (sout_stream_t *)p_data;
925     sout_stream_sys_t *p_sys = p_stream->p_sys;
926
927     if( p_sys->p_es )
928         p_sys->p_es->i_y = newval.i_int;
929
930     return VLC_SUCCESS;
931 }