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