Fix a segfault (buffer overflow for win32 only).
[vlc.git] / 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 #include <vlc_meta.h>
41
42 #include <vlc_image.h>
43 #include <vlc_filter.h>
44
45 #include "../video_filter/mosaic.h"
46
47 #include <assert.h>
48
49 /*****************************************************************************
50  * Local structures
51  *****************************************************************************/
52 struct sout_stream_sys_t
53 {
54     bridged_es_t *p_es;
55     vlc_mutex_t *p_lock;
56
57     decoder_t       *p_decoder;
58     image_handler_t *p_image; /* filter for resizing */
59     int i_height, i_width;
60     unsigned int i_sar_num, i_sar_den;
61     char *psz_id;
62     bool b_inited;
63
64     int i_chroma; /* force image format chroma */
65
66     filter_chain_t *p_vf2;
67 };
68
69 struct decoder_owner_sys_t
70 {
71     /* Current format in use by the output */
72     video_format_t video;
73 };
74
75 /*****************************************************************************
76  * Local prototypes
77  *****************************************************************************/
78 static int  Open    ( vlc_object_t * );
79 static void Close   ( vlc_object_t * );
80 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
81 static int               Del ( sout_stream_t *, sout_stream_id_t * );
82 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t * );
83
84 inline static void video_del_buffer_decoder( decoder_t *, picture_t * );
85 inline static void video_del_buffer_filter( filter_t *, picture_t * );
86
87 inline static picture_t *video_new_buffer_decoder( decoder_t * );
88 inline static picture_t *video_new_buffer_filter( filter_t * );
89 static picture_t *video_new_buffer( vlc_object_t *, decoder_owner_sys_t *,
90                                     es_format_t * );
91
92 static void video_link_picture_decoder( decoder_t *, picture_t * );
93 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
94
95 static int HeightCallback( vlc_object_t *, char const *,
96                            vlc_value_t, vlc_value_t, void * );
97 static int WidthCallback( vlc_object_t *, char const *,
98                           vlc_value_t, vlc_value_t, void * );
99 static int alphaCallback( vlc_object_t *, char const *,
100                           vlc_value_t, vlc_value_t, void * );
101 static int xCallback( vlc_object_t *, char const *,
102                       vlc_value_t, vlc_value_t, void * );
103 static int yCallback( vlc_object_t *, char const *,
104                       vlc_value_t, vlc_value_t, void * );
105
106 /*****************************************************************************
107  * Module descriptor
108  *****************************************************************************/
109 #define ID_TEXT N_("ID")
110 #define ID_LONGTEXT N_( \
111     "Specify an identifier string for this subpicture" )
112
113 #define WIDTH_TEXT N_("Video width")
114 #define WIDTH_LONGTEXT N_( \
115     "Output video width." )
116 #define HEIGHT_TEXT N_("Video height")
117 #define HEIGHT_LONGTEXT N_( \
118     "Output video height." )
119 #define RATIO_TEXT N_("Sample aspect ratio")
120 #define RATIO_LONGTEXT N_( \
121     "Sample aspect ratio of the destination (1:1, 3:4, 2:3)." )
122
123 #define VFILTER_TEXT N_("Video filter")
124 #define VFILTER_LONGTEXT N_( \
125     "Video filters will be applied to the video stream." )
126
127 #define CHROMA_TEXT N_("Image chroma")
128 #define CHROMA_LONGTEXT N_( \
129     "Force the use of a specific chroma. Use YUVA if you're planning " \
130     "to use the Alphamask or Bluescreen video filter." )
131
132 #define ALPHA_TEXT N_("Transparency")
133 #define ALPHA_LONGTEXT N_( \
134     "Transparency of the mosaic picture." )
135
136 #define X_TEXT N_("X offset")
137 #define X_LONGTEXT N_( \
138     "X coordinate of the upper left corner in the mosaic if non negative." )
139
140 #define Y_TEXT N_("Y offset")
141 #define Y_LONGTEXT N_( \
142     "Y coordinate of the upper left corner in the mosaic if non negative." )
143
144 #define CFG_PREFIX "sout-mosaic-bridge-"
145
146 vlc_module_begin ()
147     set_shortname( N_( "Mosaic bridge" ) )
148     set_description(N_("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                 false )
154     add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
155                  WIDTH_LONGTEXT, true )
156     add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
157                  HEIGHT_LONGTEXT, true )
158     add_string( CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
159                 RATIO_LONGTEXT, false )
160     add_string( CFG_PREFIX "chroma", 0, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
161                 false )
162
163     add_module_list( CFG_PREFIX "vfilter", "video filter2",
164                      NULL, NULL, VFILTER_TEXT, VFILTER_LONGTEXT, false )
165
166     add_integer_with_range( CFG_PREFIX "alpha", 255, 0, 255, NULL,
167                             ALPHA_TEXT, ALPHA_LONGTEXT, false )
168     add_integer( CFG_PREFIX "x", -1, NULL, X_TEXT, X_LONGTEXT, false )
169     add_integer( CFG_PREFIX "y", -1, NULL, Y_TEXT, Y_LONGTEXT, false )
170
171     set_callbacks( Open, Close )
172 vlc_module_end ()
173
174 static const char *const ppsz_sout_options[] = {
175     "id", "width", "height", "sar", "vfilter", "chroma", "alpha", "x", "y", NULL
176 };
177
178 /*****************************************************************************
179  * Open
180  *****************************************************************************/
181 static int Open( vlc_object_t *p_this )
182 {
183     sout_stream_t        *p_stream = (sout_stream_t *)p_this;
184     sout_stream_sys_t    *p_sys;
185     vlc_object_t         *p_libvlc = VLC_OBJECT( p_this->p_libvlc );
186     vlc_value_t           val;
187
188     config_ChainParse( p_stream, CFG_PREFIX, ppsz_sout_options,
189                        p_stream->p_cfg );
190
191     p_sys = malloc( sizeof( sout_stream_sys_t ) );
192     if( !p_sys )
193         return VLC_ENOMEM;
194
195     p_stream->p_sys = p_sys;
196     p_sys->b_inited = false;
197
198     var_Create( p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
199     var_Get( p_libvlc, "mosaic-lock", &val );
200     p_sys->p_lock = val.p_address;
201
202     p_sys->psz_id = var_CreateGetString( p_stream, CFG_PREFIX "id" );
203
204     p_sys->i_height =
205         var_CreateGetIntegerCommand( p_stream, CFG_PREFIX "height" );
206     var_AddCallback( p_stream, CFG_PREFIX "height", HeightCallback, p_stream );
207
208     p_sys->i_width =
209         var_CreateGetIntegerCommand( p_stream, CFG_PREFIX "width" );
210     var_AddCallback( p_stream, CFG_PREFIX "width", WidthCallback, 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     free( val.psz_string );
246
247 #define INT_COMMAND( a ) do { \
248     var_Create( p_stream, CFG_PREFIX #a, \
249                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND ); \
250     var_AddCallback( p_stream, CFG_PREFIX #a, a ## Callback, \
251                      p_stream ); } while(0)
252     INT_COMMAND( alpha );
253     INT_COMMAND( x );
254     INT_COMMAND( y );
255
256 #undef INT_COMMAND
257
258     p_stream->pf_add    = Add;
259     p_stream->pf_del    = Del;
260     p_stream->pf_send   = Send;
261
262     p_stream->p_sout->i_out_pace_nocontrol++;
263
264     return VLC_SUCCESS;
265 }
266
267 /*****************************************************************************
268  * Close
269  *****************************************************************************/
270 static void Close( vlc_object_t * p_this )
271 {
272     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
273     sout_stream_sys_t *p_sys = p_stream->p_sys;
274
275     p_stream->p_sout->i_out_pace_nocontrol--;
276
277     free( p_sys->psz_id );
278
279     free( p_sys );
280 }
281
282 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
283 {
284     p_filter->pf_vout_buffer_new = video_new_buffer_filter;
285     p_filter->pf_vout_buffer_del = video_del_buffer_filter;
286     p_filter->p_owner = p_data;
287     return VLC_SUCCESS;
288 }
289
290 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
291 {
292     sout_stream_sys_t *p_sys = p_stream->p_sys;
293     bridge_t *p_bridge;
294     bridged_es_t *p_es;
295     char *psz_chain;
296     int i;
297
298     if( p_sys->b_inited || p_fmt->i_cat != VIDEO_ES )
299         return NULL;
300
301     /* Create decoder object */
302     p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
303     if( !p_sys->p_decoder )
304         return NULL;
305     vlc_object_attach( p_sys->p_decoder, p_stream );
306     p_sys->p_decoder->p_module = NULL;
307     p_sys->p_decoder->fmt_in = *p_fmt;
308     p_sys->p_decoder->b_pace_control = false;
309     p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
310     p_sys->p_decoder->fmt_out.i_extra = 0;
311     p_sys->p_decoder->fmt_out.p_extra = 0;
312     p_sys->p_decoder->pf_decode_video = 0;
313     p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
314     p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
315     p_sys->p_decoder->pf_picture_link    = video_link_picture_decoder;
316     p_sys->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
317     p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
318     if( !p_sys->p_decoder->p_owner )
319     {
320         vlc_object_detach( p_sys->p_decoder );
321         vlc_object_release( p_sys->p_decoder );
322         return NULL;
323     }
324
325     p_sys->p_decoder->p_owner->video = p_fmt->video;
326     //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
327
328     p_sys->p_decoder->p_module =
329         module_need( p_sys->p_decoder, "decoder", "$codec", false );
330
331     if( !p_sys->p_decoder->p_module || !p_sys->p_decoder->pf_decode_video )
332     {
333         if( p_sys->p_decoder->p_module )
334         {
335             msg_Err( p_stream, "instanciated a non video decoder" );
336             module_unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
337         }
338         else
339         {
340             msg_Err( p_stream, "cannot find decoder" );
341         }
342         free( p_sys->p_decoder->p_owner );
343         vlc_object_detach( p_sys->p_decoder );
344         vlc_object_release( p_sys->p_decoder );
345         return NULL;
346     }
347
348     p_sys->b_inited = true;
349     vlc_mutex_lock( p_sys->p_lock );
350
351     p_bridge = GetBridge( p_stream );
352     if ( p_bridge == NULL )
353     {
354         vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
355         vlc_value_t val;
356
357         p_bridge = malloc( sizeof( bridge_t ) );
358
359         var_Create( p_libvlc, "mosaic-struct", VLC_VAR_ADDRESS );
360         val.p_address = p_bridge;
361         var_Set( p_libvlc, "mosaic-struct", val );
362
363         p_bridge->i_es_num = 0;
364         p_bridge->pp_es = NULL;
365     }
366
367     for ( i = 0; i < p_bridge->i_es_num; i++ )
368     {
369         if ( p_bridge->pp_es[i]->b_empty )
370             break;
371     }
372
373     if ( i == p_bridge->i_es_num )
374     {
375         p_bridge->pp_es = realloc( p_bridge->pp_es,
376                                    (p_bridge->i_es_num + 1)
377                                      * sizeof(bridged_es_t *) );
378         p_bridge->i_es_num++;
379         p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
380     }
381
382     p_sys->p_es = p_es = p_bridge->pp_es[i];
383
384     p_es->i_alpha = var_GetInteger( p_stream, CFG_PREFIX "alpha" );
385     p_es->i_x = var_GetInteger( p_stream, CFG_PREFIX "x" );
386     p_es->i_y = var_GetInteger( p_stream, CFG_PREFIX "y" );
387
388     //p_es->fmt = *p_fmt;
389     p_es->psz_id = p_sys->psz_id;
390     p_es->p_picture = NULL;
391     p_es->pp_last = &p_es->p_picture;
392     p_es->b_empty = false;
393
394     vlc_mutex_unlock( p_sys->p_lock );
395
396     if ( p_sys->i_height || p_sys->i_width )
397     {
398         p_sys->p_image = image_HandlerCreate( p_stream );
399     }
400     else
401     {
402         p_sys->p_image = NULL;
403     }
404
405     msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
406
407     /* Create user specified video filters */
408     psz_chain = var_GetNonEmptyString( p_stream, CFG_PREFIX "vfilter" );
409     msg_Dbg( p_stream, "psz_chain: %s", psz_chain );
410     if( psz_chain )
411     {
412         p_sys->p_vf2 = filter_chain_New( p_stream, "video filter2", false,
413                                          video_filter_buffer_allocation_init,
414                                          NULL, p_sys->p_decoder->p_owner );
415         es_format_t fmt;
416         es_format_Copy( &fmt, &p_sys->p_decoder->fmt_out );
417         if( p_sys->i_chroma )
418             fmt.video.i_chroma = p_sys->i_chroma;
419         filter_chain_Reset( p_sys->p_vf2, &fmt, &fmt );
420         filter_chain_AppendFromString( p_sys->p_vf2, psz_chain );
421         free( psz_chain );
422     }
423     else
424     {
425         p_sys->p_vf2 = NULL;
426     }
427
428     return (sout_stream_id_t *)p_sys;
429 }
430
431 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
432 {
433     VLC_UNUSED(id);
434     sout_stream_sys_t *p_sys = p_stream->p_sys;
435     bridge_t *p_bridge;
436     bridged_es_t *p_es;
437     bool b_last_es = true;
438     int i;
439
440     if( !p_sys->b_inited )
441         return VLC_SUCCESS;
442
443     if( p_sys->p_decoder != NULL )
444     {
445         decoder_owner_sys_t *p_owner = p_sys->p_decoder->p_owner;
446
447         if( p_sys->p_decoder->p_module )
448             module_unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
449         if( p_sys->p_decoder->p_description )
450             vlc_meta_Delete( p_sys->p_decoder->p_description );
451
452         vlc_object_detach( p_sys->p_decoder );
453         vlc_object_release( p_sys->p_decoder );
454
455         free( p_owner );
456     }
457
458     /* Destroy user specified video filters */
459     if( p_sys->p_vf2 )
460         filter_chain_Delete( p_sys->p_vf2 );
461
462     vlc_mutex_lock( p_sys->p_lock );
463
464     p_bridge = GetBridge( p_stream );
465     p_es = p_sys->p_es;
466
467     p_es->b_empty = true;
468     while ( p_es->p_picture )
469     {
470         picture_t *p_next = p_es->p_picture->p_next;
471         picture_Release( p_es->p_picture );
472         p_es->p_picture = p_next;
473     }
474
475     for ( i = 0; i < p_bridge->i_es_num; i++ )
476     {
477         if ( !p_bridge->pp_es[i]->b_empty )
478         {
479             b_last_es = false;
480             break;
481         }
482     }
483
484     if ( b_last_es )
485     {
486         vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
487         for ( i = 0; i < p_bridge->i_es_num; i++ )
488             free( p_bridge->pp_es[i] );
489         free( p_bridge->pp_es );
490         free( p_bridge );
491         var_Destroy( p_libvlc, "mosaic-struct" );
492     }
493
494     vlc_mutex_unlock( p_sys->p_lock );
495
496     if ( p_sys->p_image )
497     {
498         image_HandlerDelete( p_sys->p_image );
499     }
500
501     p_sys->b_inited = false;
502
503     return VLC_SUCCESS;
504 }
505
506 /*****************************************************************************
507  * PushPicture : push a picture in the mosaic-struct structure
508  *****************************************************************************/
509 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
510 {
511     sout_stream_sys_t *p_sys = p_stream->p_sys;
512     bridged_es_t *p_es = p_sys->p_es;
513
514     vlc_mutex_lock( p_sys->p_lock );
515
516     *p_es->pp_last = p_picture;
517     p_picture->p_next = NULL;
518     p_es->pp_last = &p_picture->p_next;
519
520     vlc_mutex_unlock( p_sys->p_lock );
521 }
522
523 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
524                  block_t *p_buffer )
525 {
526     sout_stream_sys_t *p_sys = p_stream->p_sys;
527     picture_t *p_pic;
528
529     if ( (sout_stream_sys_t *)id != p_sys )
530     {
531         block_ChainRelease( p_buffer );
532         return VLC_SUCCESS;
533     }
534
535     while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
536                                                         &p_buffer )) )
537     {
538         picture_t *p_new_pic;
539
540         if( p_sys->i_height || p_sys->i_width )
541         {
542             video_format_t fmt_out, fmt_in;
543
544             memset( &fmt_in, 0, sizeof(video_format_t) );
545             memset( &fmt_out, 0, sizeof(video_format_t) );
546             fmt_in = p_sys->p_decoder->fmt_out.video;
547
548
549             if( p_sys->i_chroma )
550                 fmt_out.i_chroma = p_sys->i_chroma;
551             else
552                 fmt_out.i_chroma = VLC_CODEC_I420;
553
554             if ( !p_sys->i_height )
555             {
556                 fmt_out.i_width = p_sys->i_width;
557                 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
558                     * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
559                       & ~0x1;
560             }
561             else if ( !p_sys->i_width )
562             {
563                 fmt_out.i_height = p_sys->i_height;
564                 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
565                     * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
566                       & ~0x1;
567             }
568             else
569             {
570                 fmt_out.i_width = p_sys->i_width;
571                 fmt_out.i_height = p_sys->i_height;
572             }
573             fmt_out.i_visible_width = fmt_out.i_width;
574             fmt_out.i_visible_height = fmt_out.i_height;
575
576             p_new_pic = image_Convert( p_sys->p_image,
577                                        p_pic, &fmt_in, &fmt_out );
578             if( p_new_pic == NULL )
579             {
580                 msg_Err( p_stream, "image conversion failed" );
581                 picture_Release( p_pic );
582                 continue;
583             }
584         }
585         else
586         {
587             /* TODO: chroma conversion if needed */
588
589             p_new_pic = picture_New( p_pic->format.i_chroma,
590                                      p_pic->format.i_width, p_pic->format.i_height,
591                                      p_sys->p_decoder->fmt_out.video.i_aspect );
592             if( !p_new_pic )
593             {
594                 picture_Release( p_pic );
595                 msg_Err( p_stream, "image allocation failed" );
596                 continue;
597             }
598
599             picture_Copy( p_new_pic, p_pic );
600         }
601         picture_Release( p_pic );
602
603         if( p_sys->p_vf2 )
604             p_new_pic = filter_chain_VideoFilter( p_sys->p_vf2, p_new_pic );
605
606         PushPicture( p_stream, p_new_pic );
607     }
608
609     return VLC_SUCCESS;
610 }
611
612 inline static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
613 {
614     return video_new_buffer( VLC_OBJECT( p_dec ),
615                              (decoder_owner_sys_t *)p_dec->p_owner,
616                              &p_dec->fmt_out );
617 }
618
619 inline static picture_t *video_new_buffer_filter( filter_t *p_filter )
620 {
621     return video_new_buffer( VLC_OBJECT( p_filter ),
622                              (decoder_owner_sys_t *)p_filter->p_owner,
623                              &p_filter->fmt_out );
624 }
625
626 static picture_t *video_new_buffer( vlc_object_t *p_this,
627                                     decoder_owner_sys_t *p_sys,
628                                     es_format_t *fmt_out )
629 {
630     if( fmt_out->video.i_width != p_sys->video.i_width ||
631         fmt_out->video.i_height != p_sys->video.i_height ||
632         fmt_out->video.i_chroma != p_sys->video.i_chroma ||
633         fmt_out->video.i_aspect != p_sys->video.i_aspect )
634     {
635         if( !fmt_out->video.i_sar_num ||
636             !fmt_out->video.i_sar_den )
637         {
638             fmt_out->video.i_sar_num =
639                 fmt_out->video.i_aspect * fmt_out->video.i_height;
640
641             fmt_out->video.i_sar_den =
642                 VOUT_ASPECT_FACTOR * fmt_out->video.i_width;
643         }
644
645         vlc_ureduce( &fmt_out->video.i_sar_num,
646                      &fmt_out->video.i_sar_den,
647                      fmt_out->video.i_sar_num,
648                      fmt_out->video.i_sar_den, 0 );
649
650         if( !fmt_out->video.i_visible_width ||
651             !fmt_out->video.i_visible_height )
652         {
653             fmt_out->video.i_visible_width = fmt_out->video.i_width;
654             fmt_out->video.i_visible_height = fmt_out->video.i_height;
655         }
656
657         fmt_out->video.i_chroma = fmt_out->i_codec;
658         p_sys->video = fmt_out->video;
659     }
660
661     /* */
662     fmt_out->video.i_chroma = fmt_out->i_codec;
663
664     return picture_New( fmt_out->video.i_chroma,
665                         fmt_out->video.i_width,
666                         fmt_out->video.i_height,
667                         fmt_out->video.i_aspect );
668 }
669
670 inline static void video_del_buffer_decoder( decoder_t *p_this,
671                                              picture_t *p_pic )
672 {
673     VLC_UNUSED(p_this);
674     picture_Release( p_pic );
675 }
676
677 inline static void video_del_buffer_filter( filter_t *p_this,
678                                             picture_t *p_pic )
679 {
680     VLC_UNUSED(p_this);
681     picture_Release( p_pic );
682 }
683
684 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
685 {
686     VLC_UNUSED(p_dec);
687     picture_Hold( p_pic );
688 }
689
690 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
691 {
692     VLC_UNUSED(p_dec);
693     picture_Release( p_pic );
694 }
695
696
697 /**********************************************************************
698  * Callback to update (some) params on the fly
699  **********************************************************************/
700 static int HeightCallback( vlc_object_t *p_this, char const *psz_var,
701                            vlc_value_t oldval, vlc_value_t newval,
702                            void *p_data )
703 {
704     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
705     sout_stream_t *p_stream = (sout_stream_t *)p_data;
706     sout_stream_sys_t *p_sys = p_stream->p_sys;
707
708     /* We create the handler before updating the value in p_sys
709      * so we don't have to worry about locking */
710     if( !p_sys->p_image && newval.i_int )
711         p_sys->p_image = image_HandlerCreate( p_stream );
712     p_sys->i_height = newval.i_int;
713
714     return VLC_SUCCESS;
715 }
716
717 static int WidthCallback( vlc_object_t *p_this, char const *psz_var,
718                            vlc_value_t oldval, vlc_value_t newval,
719                            void *p_data )
720 {
721     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
722     sout_stream_t *p_stream = (sout_stream_t *)p_data;
723     sout_stream_sys_t *p_sys = p_stream->p_sys;
724
725     /* We create the handler before updating the value in p_sys
726      * so we don't have to worry about locking */
727     if( !p_sys->p_image && newval.i_int )
728         p_sys->p_image = image_HandlerCreate( p_stream );
729     p_sys->i_width = newval.i_int;
730
731     return VLC_SUCCESS;
732 }
733
734 static int alphaCallback( vlc_object_t *p_this, char const *psz_var,
735                           vlc_value_t oldval, vlc_value_t newval,
736                           void *p_data )
737 {
738     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
739     sout_stream_t *p_stream = (sout_stream_t *)p_data;
740     sout_stream_sys_t *p_sys = p_stream->p_sys;
741
742     if( p_sys->p_es )
743         p_sys->p_es->i_alpha = newval.i_int;
744
745     return VLC_SUCCESS;
746 }
747
748 static int xCallback( vlc_object_t *p_this, char const *psz_var,
749                       vlc_value_t oldval, vlc_value_t newval,
750                       void *p_data )
751 {
752     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
753     sout_stream_t *p_stream = (sout_stream_t *)p_data;
754     sout_stream_sys_t *p_sys = p_stream->p_sys;
755
756     if( p_sys->p_es )
757         p_sys->p_es->i_x = newval.i_int;
758
759     return VLC_SUCCESS;
760 }
761
762 static int yCallback( vlc_object_t *p_this, char const *psz_var,
763                       vlc_value_t oldval, vlc_value_t newval,
764                       void *p_data )
765 {
766     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
767     sout_stream_t *p_stream = (sout_stream_t *)p_data;
768     sout_stream_sys_t *p_sys = p_stream->p_sys;
769
770     if( p_sys->p_es )
771         p_sys->p_es->i_y = newval.i_int;
772
773     return VLC_SUCCESS;
774 }