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