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