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