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