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