]> git.sesse.net Git - vlc/blob - modules/stream_out/mosaic_bridge.c
7ed0f171c5ad380d19603aa42c3b2773effd958a
[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                 if ( pp_ring[i]->p_data_orig != NULL )
460                     free( pp_ring[i]->p_data_orig );
461                 free( pp_ring[i]->p_sys );
462                 free( pp_ring[i] );
463             }
464         }
465     }
466
467     /* Destroy user specified video filters */
468     pp_vfilter = p_sys->pp_vfilters;
469     pp_end = pp_vfilter + p_sys->i_vfilters;
470     for( ; pp_vfilter < pp_end; pp_vfilter++ )
471     {
472         vlc_object_detach( *pp_vfilter );
473         if( (*pp_vfilter)->p_module )
474             module_Unneed( *pp_vfilter, (*pp_vfilter)->p_module );
475         vlc_object_release( *pp_vfilter );
476     }
477     free( p_sys->pp_vfilters );
478
479     vlc_mutex_lock( p_sys->p_lock );
480
481     p_bridge = GetBridge( p_stream );
482     p_es = p_sys->p_es;
483
484     p_es->b_empty = VLC_TRUE;
485     while ( p_es->p_picture )
486     {
487         picture_t *p_next = p_es->p_picture->p_next;
488         p_es->p_picture->pf_release( p_es->p_picture );
489         p_es->p_picture = p_next;
490     }
491
492     for ( i = 0; i < p_bridge->i_es_num; i++ )
493     {
494         if ( !p_bridge->pp_es[i]->b_empty )
495         {
496             b_last_es = VLC_FALSE;
497             break;
498         }
499     }
500
501     if ( b_last_es )
502     {
503         vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
504         for ( i = 0; i < p_bridge->i_es_num; i++ )
505             free( p_bridge->pp_es[i] );
506         free( p_bridge->pp_es );
507         free( p_bridge );
508         var_Destroy( p_libvlc, "mosaic-struct" );
509     }
510
511     vlc_mutex_unlock( p_sys->p_lock );
512
513     if ( p_sys->p_image )
514     {
515         image_HandlerDelete( p_sys->p_image );
516     }
517
518     p_sys->b_inited = VLC_FALSE;
519
520     return VLC_SUCCESS;
521 }
522
523 /*****************************************************************************
524  * PushPicture : push a picture in the mosaic-struct structure
525  *****************************************************************************/
526 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
527 {
528     sout_stream_sys_t *p_sys = p_stream->p_sys;
529     bridged_es_t *p_es = p_sys->p_es;
530
531     vlc_mutex_lock( p_sys->p_lock );
532
533     *p_es->pp_last = p_picture;
534     p_picture->p_next = NULL;
535     p_es->pp_last = &p_picture->p_next;
536
537     vlc_mutex_unlock( p_sys->p_lock );
538 }
539
540 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
541                  block_t *p_buffer )
542 {
543     sout_stream_sys_t *p_sys = p_stream->p_sys;
544     picture_t *p_pic;
545
546     if ( (sout_stream_sys_t *)id != p_sys )
547     {
548         block_ChainRelease( p_buffer );
549         return VLC_SUCCESS;
550     }
551
552     while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
553                                                         &p_buffer )) )
554     {
555         picture_t *p_new_pic;
556
557         if( p_sys->i_height || p_sys->i_width )
558         {
559             video_format_t fmt_out, fmt_in;
560
561             memset( &fmt_in, 0, sizeof(video_format_t) );
562             memset( &fmt_out, 0, sizeof(video_format_t) );
563             fmt_in = p_sys->p_decoder->fmt_out.video;
564
565
566             if( p_sys->i_chroma )
567                 fmt_out.i_chroma = p_sys->i_chroma;
568             else
569                 fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
570
571             if ( !p_sys->i_height )
572             {
573                 fmt_out.i_width = p_sys->i_width;
574                 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
575                     * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
576                       & ~0x1;
577             }
578             else if ( !p_sys->i_width )
579             {
580                 fmt_out.i_height = p_sys->i_height;
581                 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
582                     * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
583                       & ~0x1;
584             }
585             else
586             {
587                 fmt_out.i_width = p_sys->i_width;
588                 fmt_out.i_height = p_sys->i_height;
589             }
590             fmt_out.i_visible_width = fmt_out.i_width;
591             fmt_out.i_visible_height = fmt_out.i_height;
592
593             p_new_pic = image_Convert( p_sys->p_image,
594                                        p_pic, &fmt_in, &fmt_out );
595             if ( p_new_pic == NULL )
596             {
597                 msg_Err( p_stream, "image conversion failed" );
598                 continue;
599             }
600         }
601         else
602         {
603             /* TODO: chroma conversion if needed */
604
605             p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
606             if( p_new_pic == NULL )
607             {
608                 msg_Err( p_stream, "image conversion failed" );
609                 continue;
610             }
611
612             if( vout_AllocatePicture(
613                                   p_stream, p_new_pic, p_pic->format.i_chroma,
614                                   p_pic->format.i_width, p_pic->format.i_height,
615                                   p_sys->p_decoder->fmt_out.video.i_aspect )
616                 != VLC_SUCCESS )
617             {
618                 free( p_new_pic );
619                 msg_Err( p_stream, "image allocation failed" );
620                 continue;
621             }
622
623             vout_CopyPicture( p_stream, p_new_pic, p_pic );
624         }
625
626         p_new_pic->i_refcount = 1;
627         p_new_pic->i_status = DESTROYED_PICTURE;
628         p_new_pic->i_type   = DIRECT_PICTURE;
629         p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
630         p_new_pic->pf_release = ReleasePicture;
631         p_new_pic->date = p_pic->date;
632         p_pic->pf_release( p_pic );
633
634         if( p_sys->pp_vfilters )
635         {
636             /* Apply user specified video filters */
637             filter_t **pp_vfilter = p_sys->pp_vfilters;
638             filter_t **pp_end = pp_vfilter + p_sys->i_vfilters;
639             for( ; pp_vfilter < pp_end; pp_vfilter++ )
640             {
641                 (*pp_vfilter)->fmt_in.i_codec = p_new_pic->format.i_chroma;
642                 (*pp_vfilter)->fmt_out.i_codec = p_new_pic->format.i_chroma;
643                 (*pp_vfilter)->fmt_in.video = p_new_pic->format;
644                 (*pp_vfilter)->fmt_out.video = p_new_pic->format;
645                 p_new_pic = (*pp_vfilter)->pf_video_filter( *pp_vfilter,
646                                                              p_new_pic );
647                 if( !p_new_pic )
648                 {
649                     msg_Err( p_stream, "video filter failed" );
650                     break;
651                 }
652             }
653             if( !p_new_pic ) continue;
654         }
655
656         PushPicture( p_stream, p_new_pic );
657     }
658
659     return VLC_SUCCESS;
660 }
661
662 struct picture_sys_t
663 {
664     vlc_object_t *p_owner;
665     vlc_bool_t b_dead;
666 };
667
668 static void video_release_buffer_decoder( picture_t *p_pic )
669 {
670     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
671     {
672         video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
673     }
674     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
675 }
676
677 static void video_release_buffer_filter( picture_t *p_pic )
678 {
679     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
680     {
681         video_del_buffer_filter( (filter_t *)p_pic->p_sys->p_owner, p_pic );
682     }
683     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
684 }
685
686 inline static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
687 {
688     return video_new_buffer( VLC_OBJECT( p_dec ),
689                              (decoder_owner_sys_t *)p_dec->p_owner,
690                              &p_dec->fmt_out,
691                              video_release_buffer_decoder );
692 }
693
694 inline static picture_t *video_new_buffer_filter( filter_t *p_filter )
695 {
696     return video_new_buffer( VLC_OBJECT( p_filter ),
697                              (decoder_owner_sys_t *)p_filter->p_owner,
698                              &p_filter->fmt_out,
699                              video_release_buffer_filter );
700 }
701
702 static picture_t *video_new_buffer( vlc_object_t *p_this,
703                                     decoder_owner_sys_t *p_sys,
704                                     es_format_t *fmt_out,
705                                     void ( *pf_release )( picture_t * ) )
706 {
707     picture_t **pp_ring = p_sys->pp_pics;
708     picture_t *p_pic;
709     int i;
710
711     if( fmt_out->video.i_width != p_sys->video.i_width ||
712         fmt_out->video.i_height != p_sys->video.i_height ||
713         fmt_out->video.i_chroma != p_sys->video.i_chroma ||
714         fmt_out->video.i_aspect != p_sys->video.i_aspect )
715     {
716         if( !fmt_out->video.i_sar_num ||
717             !fmt_out->video.i_sar_den )
718         {
719             fmt_out->video.i_sar_num =
720                 fmt_out->video.i_aspect * fmt_out->video.i_height;
721
722             fmt_out->video.i_sar_den =
723                 VOUT_ASPECT_FACTOR * fmt_out->video.i_width;
724         }
725
726         vlc_ureduce( &fmt_out->video.i_sar_num,
727                      &fmt_out->video.i_sar_den,
728                      fmt_out->video.i_sar_num,
729                      fmt_out->video.i_sar_den, 0 );
730
731         if( !fmt_out->video.i_visible_width ||
732             !fmt_out->video.i_visible_height )
733         {
734             fmt_out->video.i_visible_width = fmt_out->video.i_width;
735             fmt_out->video.i_visible_height = fmt_out->video.i_height;
736         }
737
738         fmt_out->video.i_chroma = fmt_out->i_codec;
739         p_sys->video = fmt_out->video;
740
741         for( i = 0; i < PICTURE_RING_SIZE; i++ )
742         {
743             if ( pp_ring[i] != NULL )
744             {
745                 if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
746                 {
747                     if ( pp_ring[i]->p_data_orig != NULL )
748                         free( pp_ring[i]->p_data_orig );
749                     free( pp_ring[i]->p_sys );
750                     free( pp_ring[i] );
751                 }
752                 else
753                 {
754                     pp_ring[i]->p_sys->b_dead = VLC_TRUE;
755                 }
756                 pp_ring[i] = NULL;
757             }
758         }
759     }
760
761     /* Find an empty space in the picture ring buffer */
762     for( i = 0; i < PICTURE_RING_SIZE; i++ )
763     {
764         if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
765         {
766             pp_ring[i]->i_status = RESERVED_PICTURE;
767             return pp_ring[i];
768         }
769     }
770     for( i = 0; i < PICTURE_RING_SIZE; i++ )
771     {
772         if( pp_ring[i] == NULL ) break;
773     }
774
775     if( i == PICTURE_RING_SIZE )
776     {
777         msg_Err( p_this, "decoder/filter is leaking pictures, "
778                  "resetting its ring buffer" );
779
780         for( i = 0; i < PICTURE_RING_SIZE; i++ )
781         {
782             pp_ring[i]->pf_release( pp_ring[i] );
783         }
784
785         i = 0;
786     }
787
788     p_pic = malloc( sizeof(picture_t) );
789     if( !p_pic ) return NULL;
790     fmt_out->video.i_chroma = fmt_out->i_codec;
791     vout_AllocatePicture( p_this, p_pic,
792                           fmt_out->video.i_chroma,
793                           fmt_out->video.i_width,
794                           fmt_out->video.i_height,
795                           fmt_out->video.i_aspect );
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->p_sys = malloc( sizeof(picture_sys_t) );
805     p_pic->p_sys->p_owner = p_this;
806     p_pic->p_sys->b_dead = VLC_FALSE;
807     p_pic->i_status = RESERVED_PICTURE;
808
809     pp_ring[i] = p_pic;
810
811     return p_pic;
812 }
813
814 inline static void video_del_buffer_decoder( decoder_t *p_this,
815                                              picture_t *p_pic )
816 {
817     VLC_UNUSED(p_this);
818     video_del_buffer( p_pic );
819 }
820
821 inline static void video_del_buffer_filter( filter_t *p_this,
822                                             picture_t *p_pic )
823 {
824     VLC_UNUSED(p_this);
825     video_del_buffer( p_pic );
826 }
827
828 static void video_del_buffer( picture_t *p_pic )
829 {
830     p_pic->i_refcount = 0;
831     p_pic->i_status = DESTROYED_PICTURE;
832     if ( p_pic->p_sys->b_dead )
833     {
834         free( p_pic->p_data_orig );
835         free( p_pic->p_sys );
836         free( p_pic );
837     }
838 }
839
840 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
841 {
842     VLC_UNUSED(p_dec);
843     p_pic->i_refcount++;
844 }
845
846 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
847 {
848     VLC_UNUSED(p_dec);
849     video_release_buffer_decoder( p_pic );
850 }
851
852
853 /**********************************************************************
854  * Callback to update (some) params on the fly
855  **********************************************************************/
856 static int MosaicBridgeCallback( vlc_object_t *p_this, char const *psz_var,
857                                  vlc_value_t oldval, vlc_value_t newval,
858                                  void *p_data )
859 {
860     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
861     sout_stream_t *p_stream = (sout_stream_t *)p_data;
862     sout_stream_sys_t *p_sys = p_stream->p_sys;
863     int i_ret = VLC_SUCCESS;
864
865 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
866     if( VAR_IS( "height" ) )
867     {
868         /* We create the handler before updating the value in p_sys
869          * so we don't have to worry about locking */
870         if( !p_sys->p_image && newval.i_int )
871             p_sys->p_image = image_HandlerCreate( p_stream );
872         p_sys->i_height = newval.i_int;
873     }
874     else if( VAR_IS( "width" ) )
875     {
876         /* We create the handler before updating the value in p_sys
877          * so we don't have to worry about locking */
878         if( !p_sys->p_image && newval.i_int )
879             p_sys->p_image = image_HandlerCreate( p_stream );
880         p_sys->i_width = newval.i_int;
881     }
882 #undef VAR_IS
883
884     return i_ret;
885 }