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