]> git.sesse.net Git - vlc/blob - modules/stream_out/mosaic_bridge.c
7a82fa02ffc666f5843c99b1a6288ac4c35b062d
[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 #include <vlc/vlc.h>
31 #include <vlc_sout.h>
32 #include <vlc_block.h>
33 #include <vlc_codec.h>
34
35 #include <vlc_image.h>
36 #include <vlc_filter.h>
37
38 #include "../video_filter/mosaic.h"
39
40 /*****************************************************************************
41  * Local structures
42  *****************************************************************************/
43 struct sout_stream_sys_t
44 {
45     bridged_es_t *p_es;
46     vlc_mutex_t *p_lock;
47
48     decoder_t       *p_decoder;
49     image_handler_t *p_image; /* filter for resizing */
50     int i_height, i_width;
51     unsigned int i_sar_num, i_sar_den;
52     char *psz_id;
53     vlc_bool_t b_inited;
54
55     int i_chroma; /* force image format chroma */
56
57     filter_t **pp_vfilters;
58     int i_vfilters;
59 };
60
61 #define PICTURE_RING_SIZE 4
62 struct decoder_owner_sys_t
63 {
64     picture_t *pp_pics[PICTURE_RING_SIZE];
65
66     /* Current format in use by the output */
67     video_format_t video;
68 };
69
70 typedef void (* pf_release_t)( picture_t * );
71 static void ReleasePicture( picture_t *p_pic )
72 {
73     p_pic->i_refcount--;
74
75     if ( p_pic->i_refcount <= 0 )
76     {
77         if ( p_pic->p_sys != NULL )
78         {
79             pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
80             p_pic->p_sys = NULL;
81             pf_release( p_pic );
82         }
83         else
84         {
85             if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
86             if( p_pic ) free( p_pic );
87         }
88     }
89 }
90
91 /*****************************************************************************
92  * Local prototypes
93  *****************************************************************************/
94 static int  Open    ( vlc_object_t * );
95 static void Close   ( vlc_object_t * );
96 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
97 static int               Del ( sout_stream_t *, sout_stream_id_t * );
98 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t * );
99
100 inline static void video_del_buffer_decoder( decoder_t *, picture_t * );
101 inline static void video_del_buffer_filter( filter_t *, picture_t * );
102 static void video_del_buffer( vlc_object_t *, picture_t * );
103
104 inline static picture_t *video_new_buffer_decoder( decoder_t * );
105 inline static picture_t *video_new_buffer_filter( filter_t * );
106 static picture_t *video_new_buffer( vlc_object_t *, decoder_owner_sys_t *,
107                                     es_format_t *, void (*)( picture_t * ) );
108
109 static void video_link_picture_decoder( decoder_t *, picture_t * );
110 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
111 static int MosaicBridgeCallback( vlc_object_t *, char const *,
112                                  vlc_value_t, vlc_value_t, void * );
113
114 /*****************************************************************************
115  * Module descriptor
116  *****************************************************************************/
117 #define ID_TEXT N_("ID")
118 #define ID_LONGTEXT N_( \
119     "Specify an identifier string for this subpicture" )
120
121 #define WIDTH_TEXT N_("Video width")
122 #define WIDTH_LONGTEXT N_( \
123     "Output video width." )
124 #define HEIGHT_TEXT N_("Video height")
125 #define HEIGHT_LONGTEXT N_( \
126     "Output video height." )
127 #define RATIO_TEXT N_("Sample aspect ratio")
128 #define RATIO_LONGTEXT N_( \
129     "Sample aspect ratio of the destination (1:1, 3:4, 2:3)." )
130
131 #define VFILTER_TEXT N_("Video filter")
132 #define VFILTER_LONGTEXT N_( \
133     "Video filters will be applied to the video stream." )
134
135 #define CHROMA_TEXT N_("Image chroma")
136 #define CHROMA_LONGTEXT N_( \
137     "Force the use of a specific chroma. Use YUVA if you're planning " \
138     "to use the Alphamask or Bluescreen video filter." )
139
140 #define CFG_PREFIX "sout-mosaic-bridge-"
141
142 vlc_module_begin();
143     set_shortname( _( "Mosaic bridge" ) );
144     set_description(_("Mosaic bridge stream output") );
145     set_capability( "sout stream", 0 );
146     add_shortcut( "mosaic-bridge" );
147
148     add_string( CFG_PREFIX "id", "Id", NULL, ID_TEXT, ID_LONGTEXT,
149                 VLC_FALSE );
150         change_safe();
151     add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
152                  WIDTH_LONGTEXT, VLC_TRUE );
153         change_safe();
154     add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
155                  HEIGHT_LONGTEXT, VLC_TRUE );
156         change_safe();
157     add_string( CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
158                 RATIO_LONGTEXT, VLC_FALSE );
159         change_safe();
160     add_string( CFG_PREFIX "chroma", 0, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
161                 VLC_FALSE );
162         change_safe();
163
164     add_module_list( CFG_PREFIX "vfilter", "video filter2",
165                      NULL, NULL, VFILTER_TEXT, VFILTER_LONGTEXT, VLC_FALSE );
166         change_safe();
167
168     set_callbacks( Open, Close );
169 vlc_module_end();
170
171 static const char *ppsz_sout_options[] = {
172     "id", "width", "height", "sar", "vfilter", "chroma", NULL
173 };
174
175 /*****************************************************************************
176  * Open
177  *****************************************************************************/
178 static int Open( vlc_object_t *p_this )
179 {
180     sout_stream_t        *p_stream = (sout_stream_t *)p_this;
181     sout_stream_sys_t    *p_sys;
182     vlc_object_t         *p_libvlc = VLC_OBJECT( p_this->p_libvlc );
183     vlc_value_t           val;
184
185     config_ChainParse( p_stream, CFG_PREFIX, ppsz_sout_options,
186                        p_stream->p_cfg );
187
188     p_sys = malloc( sizeof( sout_stream_sys_t ) );
189     if( !p_sys )
190     {
191         return VLC_ENOMEM;
192     }
193
194     p_stream->p_sys = p_sys;
195     p_sys->b_inited = VLC_FALSE;
196
197     var_Create( p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
198     var_Get( p_libvlc, "mosaic-lock", &val );
199     p_sys->p_lock = val.p_address;
200
201     var_Get( p_stream, CFG_PREFIX "id", &val );
202     p_sys->psz_id = val.psz_string;
203
204     p_sys->i_height =
205         var_CreateGetIntegerCommand( p_stream, CFG_PREFIX "height" );
206     var_AddCallback( p_stream, CFG_PREFIX "height", MosaicBridgeCallback,
207                      p_stream );
208
209     p_sys->i_width =
210         var_CreateGetIntegerCommand( p_stream, CFG_PREFIX "width" );
211     var_AddCallback( p_stream, CFG_PREFIX "width", MosaicBridgeCallback,
212                      p_stream );
213
214     var_Get( p_stream, CFG_PREFIX "sar", &val );
215     if ( val.psz_string )
216     {
217         char *psz_parser = strchr( val.psz_string, ':' );
218
219         if( psz_parser )
220         {
221             *psz_parser++ = '\0';
222             p_sys->i_sar_num = atoi( val.psz_string );
223             p_sys->i_sar_den = atoi( psz_parser );
224             vlc_ureduce( &p_sys->i_sar_num, &p_sys->i_sar_den,
225                          p_sys->i_sar_num, p_sys->i_sar_den, 0 );
226         }
227         else
228         {
229             msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
230             p_sys->i_sar_num = p_sys->i_sar_den = 1;
231         }
232
233         free( val.psz_string );
234     }
235     else
236     {
237         p_sys->i_sar_num = p_sys->i_sar_den = 1;
238     }
239
240     p_sys->i_chroma = 0;
241     val.psz_string = var_GetNonEmptyString( p_stream, CFG_PREFIX "chroma" );
242     if( val.psz_string && strlen( val.psz_string ) >= 4 )
243     {
244         memcpy( &p_sys->i_chroma, val.psz_string, 4 );
245         msg_Dbg( p_stream, "Forcing image chroma to 0x%.8x (%4.4s)", p_sys->i_chroma, (char*)&p_sys->i_chroma );
246     }
247
248     p_stream->pf_add    = Add;
249     p_stream->pf_del    = Del;
250     p_stream->pf_send   = Send;
251
252     p_stream->p_sout->i_out_pace_nocontrol++;
253
254     return VLC_SUCCESS;
255 }
256
257 /*****************************************************************************
258  * Close
259  *****************************************************************************/
260 static void Close( vlc_object_t * p_this )
261 {
262     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
263     sout_stream_sys_t *p_sys = p_stream->p_sys;
264
265     p_stream->p_sout->i_out_pace_nocontrol--;
266
267     if ( p_sys->psz_id )
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_destroy( 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_destroy( *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     sout_stream_sys_t *p_sys = p_stream->p_sys;
434     bridge_t *p_bridge;
435     bridged_es_t *p_es;
436     vlc_bool_t b_last_es = VLC_TRUE;
437     filter_t **pp_vfilter, **pp_end;
438     int i;
439
440     if ( !p_sys->b_inited )
441     {
442         return VLC_SUCCESS;
443     }
444
445     if ( p_sys->p_decoder != NULL )
446     {
447         picture_t **pp_ring = p_sys->p_decoder->p_owner->pp_pics;
448
449         if( p_sys->p_decoder->p_module )
450             module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
451         vlc_object_detach( p_sys->p_decoder );
452         vlc_object_destroy( p_sys->p_decoder );
453
454         for( i = 0; i < PICTURE_RING_SIZE; i++ )
455         {
456             if ( pp_ring[i] != NULL )
457             {
458                 if ( pp_ring[i]->p_data_orig != NULL )
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_destroy( *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                     if ( pp_ring[i]->p_data_orig != NULL )
747                         free( pp_ring[i]->p_data_orig );
748                     free( pp_ring[i]->p_sys );
749                     free( pp_ring[i] );
750                 }
751                 else
752                 {
753                     pp_ring[i]->p_sys->b_dead = VLC_TRUE;
754                 }
755                 pp_ring[i] = NULL;
756             }
757         }
758     }
759
760     /* Find an empty space in the picture ring buffer */
761     for( i = 0; i < PICTURE_RING_SIZE; i++ )
762     {
763         if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
764         {
765             pp_ring[i]->i_status = RESERVED_PICTURE;
766             return pp_ring[i];
767         }
768     }
769     for( i = 0; i < PICTURE_RING_SIZE; i++ )
770     {
771         if( pp_ring[i] == NULL ) break;
772     }
773
774     if( i == PICTURE_RING_SIZE )
775     {
776         msg_Err( p_this, "decoder/filter is leaking pictures, "
777                  "resetting its ring buffer" );
778
779         for( i = 0; i < PICTURE_RING_SIZE; i++ )
780         {
781             pp_ring[i]->pf_release( pp_ring[i] );
782         }
783
784         i = 0;
785     }
786
787     p_pic = malloc( sizeof(picture_t) );
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     video_del_buffer( VLC_OBJECT( p_this ), p_pic );
816 }
817
818 inline static void video_del_buffer_filter( filter_t *p_this,
819                                             picture_t *p_pic )
820 {
821     video_del_buffer( VLC_OBJECT( p_this ), p_pic );
822 }
823
824 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
825 {
826     p_pic->i_refcount = 0;
827     p_pic->i_status = DESTROYED_PICTURE;
828     if ( p_pic->p_sys->b_dead )
829     {
830         if ( p_pic->p_data_orig != NULL )
831             free( p_pic->p_data_orig );
832         free( p_pic->p_sys );
833         free( p_pic );
834     }
835 }
836
837 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
838 {
839     p_pic->i_refcount++;
840 }
841
842 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
843 {
844     video_release_buffer_decoder( p_pic );
845 }
846
847
848 /**********************************************************************
849  * Callback to update (some) params on the fly
850  **********************************************************************/
851 static int MosaicBridgeCallback( vlc_object_t *p_this, char const *psz_var,
852                                  vlc_value_t oldval, vlc_value_t newval,
853                                  void *p_data )
854 {
855     sout_stream_t *p_stream = (sout_stream_t *)p_data;
856     sout_stream_sys_t *p_sys = p_stream->p_sys;
857     int i_ret = VLC_SUCCESS;
858
859 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
860     if( VAR_IS( "height" ) )
861     {
862         /* We create the handler before updating the value in p_sys
863          * so we don't have to worry about locking */
864         if( !p_sys->p_image && newval.i_int )
865             p_sys->p_image = image_HandlerCreate( p_stream );
866         p_sys->i_height = newval.i_int;
867     }
868     else if( VAR_IS( "width" ) )
869     {
870         /* We create the handler before updating the value in p_sys
871          * so we don't have to worry about locking */
872         if( !p_sys->p_image && newval.i_int )
873             p_sys->p_image = image_HandlerCreate( p_stream );
874         p_sys->i_width = newval.i_int;
875     }
876 #undef VAR_IS
877
878     return i_ret;
879 }