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