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