1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2007 VLC authors and VideoLAN
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
8 * Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
37 #include <vlc_codec.h>
40 #include <vlc_image.h>
41 #include <vlc_filter.h>
42 #include <vlc_modules.h>
44 #include "../video_filter/mosaic.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 struct sout_stream_sys_t
54 image_handler_t *p_image; /* filter for resizing */
55 int i_height, i_width;
56 unsigned int i_sar_num, i_sar_den;
60 int i_chroma; /* force image format chroma */
62 filter_chain_t *p_vf2;
65 struct decoder_owner_sys_t
67 /* Current format in use by the output */
71 /*****************************************************************************
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 * );
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 *,
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 * );
97 /*****************************************************************************
99 *****************************************************************************/
100 #define ID_TEXT N_("ID")
101 #define ID_LONGTEXT N_( \
102 "Specify an identifier string for this subpicture" )
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)." )
114 #define VFILTER_TEXT N_("Video filter")
115 #define VFILTER_LONGTEXT N_( \
116 "Video filters will be applied to the video stream." )
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." )
123 #define ALPHA_TEXT N_("Transparency")
124 #define ALPHA_LONGTEXT N_( \
125 "Transparency of the mosaic picture." )
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." )
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." )
135 #define CFG_PREFIX "sout-mosaic-bridge-"
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" )
143 add_string( CFG_PREFIX "id", "Id", ID_TEXT, ID_LONGTEXT,
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,
154 add_module_list( CFG_PREFIX "vfilter", "video filter2",
155 NULL, VFILTER_TEXT, VFILTER_LONGTEXT, false )
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 )
162 set_callbacks( Open, Close )
165 static const char *const ppsz_sout_options[] = {
166 "id", "width", "height", "sar", "vfilter", "chroma", "alpha", "x", "y", NULL
169 /*****************************************************************************
171 *****************************************************************************/
172 static int Open( vlc_object_t *p_this )
174 sout_stream_t *p_stream = (sout_stream_t *)p_this;
175 sout_stream_sys_t *p_sys;
178 config_ChainParse( p_stream, CFG_PREFIX, ppsz_sout_options,
181 p_sys = malloc( sizeof( sout_stream_sys_t ) );
185 p_stream->p_sys = p_sys;
186 p_sys->b_inited = false;
188 p_sys->psz_id = var_CreateGetString( p_stream, CFG_PREFIX "id" );
191 var_CreateGetIntegerCommand( p_stream, CFG_PREFIX "height" );
192 var_AddCallback( p_stream, CFG_PREFIX "height", HeightCallback, p_stream );
195 var_CreateGetIntegerCommand( p_stream, CFG_PREFIX "width" );
196 var_AddCallback( p_stream, CFG_PREFIX "width", WidthCallback, p_stream );
198 var_Get( p_stream, CFG_PREFIX "sar", &val );
201 char *psz_parser = strchr( val.psz_string, ':' );
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 );
213 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
214 p_sys->i_sar_num = p_sys->i_sar_den = 1;
217 free( val.psz_string );
221 p_sys->i_sar_num = p_sys->i_sar_den = 1;
225 val.psz_string = var_GetNonEmptyString( p_stream, CFG_PREFIX "chroma" );
226 if( val.psz_string && strlen( val.psz_string ) >= 4 )
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 );
231 free( val.psz_string );
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 );
244 p_stream->pf_add = Add;
245 p_stream->pf_del = Del;
246 p_stream->pf_send = Send;
247 p_stream->pace_nocontrol = true;
252 /*****************************************************************************
254 *****************************************************************************/
255 static void Close( vlc_object_t * p_this )
257 sout_stream_t *p_stream = (sout_stream_t*)p_this;
258 sout_stream_sys_t *p_sys = p_stream->p_sys;
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 );
267 free( p_sys->psz_id );
272 static sout_stream_id_sys_t * Add( sout_stream_t *p_stream, const es_format_t *p_fmt )
274 sout_stream_sys_t *p_sys = p_stream->p_sys;
280 if( p_sys->b_inited || p_fmt->i_cat != VIDEO_ES )
283 /* Create decoder object */
284 p_sys->p_decoder = vlc_object_create( p_stream, sizeof( decoder_t ) );
285 if( !p_sys->p_decoder )
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 )
299 vlc_object_release( p_sys->p_decoder );
303 p_sys->p_decoder->p_owner->video = p_fmt->video;
304 //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
306 p_sys->p_decoder->p_module =
307 module_need( p_sys->p_decoder, "decoder", "$codec", false );
309 if( !p_sys->p_decoder->p_module || !p_sys->p_decoder->pf_decode_video )
311 if( p_sys->p_decoder->p_module )
313 msg_Err( p_stream, "instanciated a non video decoder" );
314 module_unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
318 msg_Err( p_stream, "cannot find decoder" );
320 free( p_sys->p_decoder->p_owner );
321 vlc_object_release( p_sys->p_decoder );
325 p_sys->b_inited = true;
326 vlc_global_lock( VLC_MOSAIC_MUTEX );
328 p_bridge = GetBridge( p_stream );
329 if ( p_bridge == NULL )
331 vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
334 p_bridge = xmalloc( sizeof( bridge_t ) );
336 var_Create( p_libvlc, "mosaic-struct", VLC_VAR_ADDRESS );
337 val.p_address = p_bridge;
338 var_Set( p_libvlc, "mosaic-struct", val );
340 p_bridge->i_es_num = 0;
341 p_bridge->pp_es = NULL;
344 for ( i = 0; i < p_bridge->i_es_num; i++ )
346 if ( p_bridge->pp_es[i]->b_empty )
350 if ( i == p_bridge->i_es_num )
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) );
358 p_sys->p_es = p_es = p_bridge->pp_es[i];
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" );
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;
370 vlc_global_unlock( VLC_MOSAIC_MUTEX );
372 if ( p_sys->i_height || p_sys->i_width )
374 p_sys->p_image = image_HandlerCreate( p_stream );
378 p_sys->p_image = NULL;
381 msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
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 );
388 filter_owner_t owner = {
389 .sys = p_sys->p_decoder->p_owner,
391 .buffer_new = video_new_buffer_filter,
395 p_sys->p_vf2 = filter_chain_NewVideo( p_stream, false, &owner );
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 );
409 return (sout_stream_id_sys_t *)p_sys;
412 static void Del( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
415 sout_stream_sys_t *p_sys = p_stream->p_sys;
418 bool b_last_es = true;
421 if( !p_sys->b_inited )
424 if( p_sys->p_decoder != NULL )
426 decoder_owner_sys_t *p_owner = p_sys->p_decoder->p_owner;
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 );
433 vlc_object_release( p_sys->p_decoder );
438 /* Destroy user specified video filters */
440 filter_chain_Delete( p_sys->p_vf2 );
442 vlc_global_lock( VLC_MOSAIC_MUTEX );
444 p_bridge = GetBridge( p_stream );
447 p_es->b_empty = true;
448 while ( p_es->p_picture )
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;
455 for ( i = 0; i < p_bridge->i_es_num; i++ )
457 if ( !p_bridge->pp_es[i]->b_empty )
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 );
471 var_Destroy( p_libvlc, "mosaic-struct" );
474 vlc_global_unlock( VLC_MOSAIC_MUTEX );
476 if ( p_sys->p_image )
478 image_HandlerDelete( p_sys->p_image );
481 p_sys->b_inited = false;
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 )
489 sout_stream_sys_t *p_sys = p_stream->p_sys;
490 bridged_es_t *p_es = p_sys->p_es;
492 vlc_global_lock( VLC_MOSAIC_MUTEX );
494 *p_es->pp_last = p_picture;
495 p_picture->p_next = NULL;
496 p_es->pp_last = &p_picture->p_next;
498 vlc_global_unlock( VLC_MOSAIC_MUTEX );
501 static int Send( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
504 sout_stream_sys_t *p_sys = p_stream->p_sys;
507 if ( (sout_stream_sys_t *)id != p_sys )
509 block_ChainRelease( p_buffer );
513 while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
516 picture_t *p_new_pic;
518 if( p_sys->i_height || p_sys->i_width )
520 video_format_t fmt_out, fmt_in;
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;
527 if( p_sys->i_chroma )
528 fmt_out.i_chroma = p_sys->i_chroma;
530 fmt_out.i_chroma = VLC_CODEC_I420;
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 )
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)
543 else if ( !p_sys->i_width )
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)
552 fmt_out.i_width = p_sys->i_width;
553 fmt_out.i_height = p_sys->i_height;
555 fmt_out.i_visible_width = fmt_out.i_width;
556 fmt_out.i_visible_height = fmt_out.i_height;
558 p_new_pic = image_Convert( p_sys->p_image,
559 p_pic, &fmt_in, &fmt_out );
560 if( p_new_pic == NULL )
562 msg_Err( p_stream, "image conversion failed" );
563 picture_Release( p_pic );
569 /* TODO: chroma conversion if needed */
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 );
577 picture_Release( p_pic );
578 msg_Err( p_stream, "image allocation failed" );
582 picture_Copy( p_new_pic, p_pic );
584 picture_Release( p_pic );
587 p_new_pic = filter_chain_VideoFilter( p_sys->p_vf2, p_new_pic );
589 PushPicture( p_stream, p_new_pic );
595 inline static int video_update_format_decoder( decoder_t *p_dec )
597 return video_update_format( VLC_OBJECT( p_dec ),
598 (decoder_owner_sys_t *)p_dec->p_owner,
602 inline static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
604 return picture_NewFromFormat( &p_dec->fmt_out.video );
607 inline static picture_t *video_new_buffer_filter( filter_t *p_filter )
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" );
615 return picture_NewFromFormat( &p_filter->fmt_out.video );
618 static int video_update_format( vlc_object_t *p_this,
619 decoder_owner_sys_t *p_sys,
620 es_format_t *fmt_out )
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 )
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 );
634 if( !fmt_out->video.i_visible_width ||
635 !fmt_out->video.i_visible_height )
637 fmt_out->video.i_visible_width = fmt_out->video.i_width;
638 fmt_out->video.i_visible_height = fmt_out->video.i_height;
641 fmt_out->video.i_chroma = fmt_out->i_codec;
642 p_sys->video = fmt_out->video;
646 fmt_out->video.i_chroma = fmt_out->i_codec;
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,
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;
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;
670 static int WidthCallback( vlc_object_t *p_this, char const *psz_var,
671 vlc_value_t oldval, vlc_value_t newval,
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;
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;
687 static int alphaCallback( vlc_object_t *p_this, char const *psz_var,
688 vlc_value_t oldval, vlc_value_t newval,
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;
696 p_sys->p_es->i_alpha = newval.i_int;
701 static int xCallback( vlc_object_t *p_this, char const *psz_var,
702 vlc_value_t oldval, vlc_value_t newval,
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;
710 p_sys->p_es->i_x = newval.i_int;
715 static int yCallback( vlc_object_t *p_this, char const *psz_var,
716 vlc_value_t oldval, vlc_value_t newval,
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;
724 p_sys->p_es->i_y = newval.i_int;