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