1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
34 #include <vlc_block.h>
35 #include <vlc_codec.h>
37 #include "vlc_image.h"
39 #include "../video_filter/mosaic.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 struct sout_stream_sys_t
50 image_handler_t *p_image; /* filter for resizing */
51 int i_height, i_width;
52 unsigned int i_sar_num, i_sar_den;
59 #define PICTURE_RING_SIZE 4
60 struct decoder_owner_sys_t
62 picture_t *pp_pics[PICTURE_RING_SIZE];
64 /* Current format in use by the output */
68 typedef void (* pf_release_t)( picture_t * );
69 static void ReleasePicture( picture_t *p_pic )
73 if ( p_pic->i_refcount <= 0 )
75 if ( p_pic->p_sys != NULL )
77 pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
83 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
84 if( p_pic ) free( p_pic );
89 /* copied from video_filters/erase.c . Gruik ? */
90 static void LoadMask( sout_stream_t *p_stream, const char *psz_filename )
92 image_handler_t *p_image;
93 video_format_t fmt_in, fmt_out;
94 memset( &fmt_in, 0, sizeof( video_format_t ) );
95 memset( &fmt_out, 0, sizeof( video_format_t ) );
96 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
97 if( p_stream->p_sys->p_mask )
98 p_stream->p_sys->p_mask->pf_release( p_stream->p_sys->p_mask );
99 p_image = image_HandlerCreate( p_stream );
100 p_stream->p_sys->p_mask =
101 image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
102 image_HandlerDelete( p_image );
105 /*****************************************************************************
107 *****************************************************************************/
108 static int Open ( vlc_object_t * );
109 static void Close ( vlc_object_t * );
110 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
111 static int Del ( sout_stream_t *, sout_stream_id_t * );
112 static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
114 static void video_del_buffer( decoder_t *, picture_t * );
115 static picture_t *video_new_buffer( decoder_t * );
116 static void video_link_picture_decoder( decoder_t *, picture_t * );
117 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
119 /*****************************************************************************
121 *****************************************************************************/
122 #define ID_TEXT N_("ID")
123 #define ID_LONGTEXT N_( \
124 "Specify an identifier string for this subpicture" )
126 #define WIDTH_TEXT N_("Video width")
127 #define WIDTH_LONGTEXT N_( \
128 "Output video width." )
129 #define HEIGHT_TEXT N_("Video height")
130 #define HEIGHT_LONGTEXT N_( \
131 "Output video height." )
132 #define RATIO_TEXT N_("Sample aspect ratio")
133 #define RATIO_LONGTEXT N_( \
134 "Sample aspect ratio of the destination (1:1, 3:4, 2:3)." )
135 #define MASK_TEXT N_("Transparency mask")
136 #define MASK_LONGTEXT N_( \
137 "Alpha blending transparency mask. Use's a png alpha channel.")
139 #define SOUT_CFG_PREFIX "sout-mosaic-bridge-"
142 set_shortname( _( "Mosaic bridge" ) );
143 set_description(_("Mosaic bridge stream output") );
144 set_capability( "sout stream", 0 );
145 add_shortcut( "mosaic-bridge" );
147 add_string( SOUT_CFG_PREFIX "id", "Id", NULL, ID_TEXT, ID_LONGTEXT,
149 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
150 WIDTH_LONGTEXT, VLC_TRUE );
151 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
152 HEIGHT_LONGTEXT, VLC_TRUE );
153 add_string( SOUT_CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
154 RATIO_LONGTEXT, VLC_FALSE );
155 add_string( SOUT_CFG_PREFIX "mask", NULL, NULL, MASK_TEXT,
156 MASK_LONGTEXT, VLC_FALSE );
158 set_callbacks( Open, Close );
160 var_Create( p_module->p_libvlc_global, "mosaic-lock", VLC_VAR_MUTEX );
163 static const char *ppsz_sout_options[] = {
164 "id", "width", "height", "sar", "mask", NULL
167 /*****************************************************************************
169 *****************************************************************************/
170 static int Open( vlc_object_t *p_this )
172 sout_stream_t *p_stream = (sout_stream_t *)p_this;
173 sout_stream_sys_t *p_sys;
174 libvlc_global_data_t *p_libvlc_global = p_this->p_libvlc_global;
177 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
180 p_sys = malloc( sizeof( sout_stream_sys_t ) );
181 p_stream->p_sys = p_sys;
182 p_sys->b_inited = VLC_FALSE;
184 var_Get( p_libvlc_global, "mosaic-lock", &val );
185 p_sys->p_lock = val.p_address;
187 var_Get( p_stream, SOUT_CFG_PREFIX "id", &val );
188 p_sys->psz_id = val.psz_string;
190 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
191 p_sys->i_height = val.i_int;
193 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
194 p_sys->i_width = val.i_int;
196 var_Get( p_stream, SOUT_CFG_PREFIX "mask", &val );
197 if( val.psz_string && *val.psz_string )
199 p_sys->p_mask = NULL;
200 LoadMask( p_stream, val.psz_string );
202 msg_Err( p_stream, "Error while loading mask (%s).",
206 p_sys->p_mask = NULL;
207 free( val.psz_string );
209 var_Get( p_stream, SOUT_CFG_PREFIX "sar", &val );
210 if ( val.psz_string )
212 char *psz_parser = strchr( val.psz_string, ':' );
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 );
224 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
225 p_sys->i_sar_num = p_sys->i_sar_den = 1;
228 free( val.psz_string );
232 p_sys->i_sar_num = p_sys->i_sar_den = 1;
235 p_stream->pf_add = Add;
236 p_stream->pf_del = Del;
237 p_stream->pf_send = Send;
239 p_stream->p_sout->i_out_pace_nocontrol++;
244 /*****************************************************************************
246 *****************************************************************************/
247 static void Close( vlc_object_t * p_this )
249 sout_stream_t *p_stream = (sout_stream_t*)p_this;
250 sout_stream_sys_t *p_sys = p_stream->p_sys;
252 p_stream->p_sout->i_out_pace_nocontrol--;
255 free( p_sys->psz_id );
257 if( p_stream->p_sys->p_mask )
258 p_stream->p_sys->p_mask->pf_release( p_stream->p_sys->p_mask );
263 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
265 sout_stream_sys_t *p_sys = p_stream->p_sys;
270 if ( p_sys->b_inited )
275 /* Create decoder object */
276 p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
277 vlc_object_attach( p_sys->p_decoder, p_stream );
278 p_sys->p_decoder->p_module = NULL;
279 p_sys->p_decoder->fmt_in = *p_fmt;
280 p_sys->p_decoder->b_pace_control = VLC_FALSE;
281 p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
282 p_sys->p_decoder->fmt_out.i_extra = 0;
283 p_sys->p_decoder->fmt_out.p_extra = 0;
284 p_sys->p_decoder->pf_decode_video = 0;
285 p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer;
286 p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer;
287 p_sys->p_decoder->pf_picture_link = video_link_picture_decoder;
288 p_sys->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
289 p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
290 for( i = 0; i < PICTURE_RING_SIZE; i++ )
291 p_sys->p_decoder->p_owner->pp_pics[i] = 0;
292 p_sys->p_decoder->p_owner->video = p_fmt->video;
293 //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
295 p_sys->p_decoder->p_module =
296 module_Need( p_sys->p_decoder, "decoder", "$codec", 0 );
298 if( !p_sys->p_decoder->p_module )
300 msg_Err( p_stream, "cannot find decoder" );
301 vlc_object_detach( p_sys->p_decoder );
302 vlc_object_destroy( p_sys->p_decoder );
306 p_sys->b_inited = VLC_TRUE;
307 vlc_mutex_lock( p_sys->p_lock );
309 p_bridge = GetBridge( p_stream );
310 if ( p_bridge == NULL )
312 libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
315 p_bridge = malloc( sizeof( bridge_t ) );
317 var_Create( p_libvlc_global, "mosaic-struct", VLC_VAR_ADDRESS );
318 val.p_address = p_bridge;
319 var_Set( p_libvlc_global, "mosaic-struct", val );
321 p_bridge->i_es_num = 0;
322 p_bridge->pp_es = NULL;
325 for ( i = 0; i < p_bridge->i_es_num; i++ )
327 if ( p_bridge->pp_es[i]->b_empty )
331 if ( i == p_bridge->i_es_num )
333 p_bridge->pp_es = realloc( p_bridge->pp_es,
334 (p_bridge->i_es_num + 1)
335 * sizeof(bridged_es_t *) );
336 p_bridge->i_es_num++;
337 p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
340 p_sys->p_es = p_es = p_bridge->pp_es[i];
342 //p_es->fmt = *p_fmt;
343 p_es->psz_id = p_sys->psz_id;
344 p_es->p_picture = NULL;
345 p_es->pp_last = &p_es->p_picture;
346 p_es->b_empty = VLC_FALSE;
348 vlc_mutex_unlock( p_sys->p_lock );
350 if ( p_sys->i_height || p_sys->i_width )
352 p_sys->p_image = image_HandlerCreate( p_stream );
355 msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
357 return (sout_stream_id_t *)p_sys;
360 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
362 sout_stream_sys_t *p_sys = p_stream->p_sys;
365 vlc_bool_t b_last_es = VLC_TRUE;
368 if ( !p_sys->b_inited )
373 if ( p_sys->p_decoder != NULL )
375 picture_t **pp_ring = p_sys->p_decoder->p_owner->pp_pics;
377 if( p_sys->p_decoder->p_module )
378 module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
379 vlc_object_detach( p_sys->p_decoder );
380 vlc_object_destroy( p_sys->p_decoder );
382 for( i = 0; i < PICTURE_RING_SIZE; i++ )
384 if ( pp_ring[i] != NULL )
386 if ( pp_ring[i]->p_data_orig != NULL )
387 free( pp_ring[i]->p_data_orig );
388 free( pp_ring[i]->p_sys );
394 vlc_mutex_lock( p_sys->p_lock );
396 p_bridge = GetBridge( p_stream );
399 p_es->b_empty = VLC_TRUE;
400 while ( p_es->p_picture )
402 picture_t *p_next = p_es->p_picture->p_next;
403 p_es->p_picture->pf_release( p_es->p_picture );
404 p_es->p_picture = p_next;
407 for ( i = 0; i < p_bridge->i_es_num; i++ )
409 if ( !p_bridge->pp_es[i]->b_empty )
411 b_last_es = VLC_FALSE;
418 libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
419 for ( i = 0; i < p_bridge->i_es_num; i++ )
420 free( p_bridge->pp_es[i] );
421 free( p_bridge->pp_es );
423 var_Destroy( p_libvlc_global, "mosaic-struct" );
426 vlc_mutex_unlock( p_sys->p_lock );
428 if ( p_sys->i_height || p_sys->i_width )
430 image_HandlerDelete( p_sys->p_image );
433 p_sys->b_inited = VLC_FALSE;
438 /*****************************************************************************
439 * PushPicture : push a picture in the mosaic-struct structure
440 *****************************************************************************/
441 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
443 sout_stream_sys_t *p_sys = p_stream->p_sys;
444 bridged_es_t *p_es = p_sys->p_es;
446 vlc_mutex_lock( p_sys->p_lock );
448 *p_es->pp_last = p_picture;
449 p_picture->p_next = NULL;
450 p_es->pp_last = &p_picture->p_next;
452 vlc_mutex_unlock( p_sys->p_lock );
455 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
458 sout_stream_sys_t *p_sys = p_stream->p_sys;
461 if ( (sout_stream_sys_t *)id != p_sys )
463 block_ChainRelease( p_buffer );
467 while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
470 picture_t *p_new_pic;
472 if( p_sys->i_height || p_sys->i_width )
474 video_format_t fmt_out, fmt_in;
476 memset( &fmt_in, 0, sizeof(video_format_t) );
477 memset( &fmt_out, 0, sizeof(video_format_t) );
478 fmt_in = p_sys->p_decoder->fmt_out.video;
481 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
483 fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
485 if ( !p_sys->i_height )
487 fmt_out.i_width = p_sys->i_width;
488 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
489 * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
492 else if ( !p_sys->i_width )
494 fmt_out.i_height = p_sys->i_height;
495 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
496 * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
501 fmt_out.i_width = p_sys->i_width;
502 fmt_out.i_height = p_sys->i_height;
504 fmt_out.i_visible_width = fmt_out.i_width;
505 fmt_out.i_visible_height = fmt_out.i_height;
507 p_new_pic = image_Convert( p_sys->p_image,
508 p_pic, &fmt_in, &fmt_out );
509 if ( p_new_pic == NULL )
511 msg_Err( p_stream, "image conversion failed" );
517 plane_t *p_mask = p_sys->p_mask->p+A_PLANE;
518 plane_t *p_apic = p_new_pic->p+A_PLANE;
519 if( p_mask->i_visible_pitch
520 != p_apic->i_visible_pitch
521 || p_mask->i_visible_lines
522 != p_apic->i_visible_lines )
525 "Mask size (%d x %d) and image size (%d x %d) "
526 "don't match. The mask will not be applied.",
527 p_mask->i_visible_pitch,
528 p_mask->i_visible_lines,
529 p_apic->i_visible_pitch,
530 p_apic->i_visible_lines );
534 if( p_mask->i_pitch != p_apic->i_pitch
535 || p_mask->i_lines != p_apic->i_lines )
537 /* visible plane sizes match ... but not the undelying
538 * buffer. I'm not sure that this can happen,
539 * but better safe than sorry. */
541 int i_lines = p_mask->i_visible_lines;
542 uint8_t *p_src = p_mask->p_pixels;
543 uint8_t *p_dst = p_apic->p_pixels;
544 int i_src_pitch = p_mask->i_pitch;
545 int i_dst_pitch = p_apic->i_pitch;
546 int i_visible_pitch = p_mask->i_visible_pitch;
547 for( i_line = 0; i_line < i_lines; i_line++,
548 p_src += i_src_pitch, p_dst += i_dst_pitch )
550 p_stream->p_libvlc->pf_memcpy(
551 p_dst, p_src, i_visible_pitch );
556 /* plane sizes match */
557 p_stream->p_libvlc->pf_memcpy(
558 p_apic->p_pixels, p_mask->p_pixels,
559 p_mask->i_pitch * p_mask->i_lines );
566 p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
567 vout_AllocatePicture( p_stream, p_new_pic, p_pic->format.i_chroma,
568 p_pic->format.i_width, p_pic->format.i_height,
569 p_sys->p_decoder->fmt_out.video.i_aspect );
571 vout_CopyPicture( p_stream, p_new_pic, p_pic );
574 p_new_pic->i_refcount = 1;
575 p_new_pic->i_status = DESTROYED_PICTURE;
576 p_new_pic->i_type = DIRECT_PICTURE;
577 p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
578 p_new_pic->pf_release = ReleasePicture;
579 p_new_pic->date = p_pic->date;
581 p_pic->pf_release( p_pic );
582 PushPicture( p_stream, p_new_pic );
590 vlc_object_t *p_owner;
594 static void video_release_buffer( picture_t *p_pic )
596 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
598 video_del_buffer( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
600 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
603 static picture_t *video_new_buffer( decoder_t *p_dec )
605 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
606 picture_t **pp_ring = p_dec->p_owner->pp_pics;
610 if( p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
611 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
612 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
613 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
615 if( !p_dec->fmt_out.video.i_sar_num ||
616 !p_dec->fmt_out.video.i_sar_den )
618 p_dec->fmt_out.video.i_sar_num =
619 p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
621 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
622 p_dec->fmt_out.video.i_width;
625 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
626 &p_dec->fmt_out.video.i_sar_den,
627 p_dec->fmt_out.video.i_sar_num,
628 p_dec->fmt_out.video.i_sar_den, 0 );
630 if( !p_dec->fmt_out.video.i_visible_width ||
631 !p_dec->fmt_out.video.i_visible_height )
633 p_dec->fmt_out.video.i_visible_width =
634 p_dec->fmt_out.video.i_width;
635 p_dec->fmt_out.video.i_visible_height =
636 p_dec->fmt_out.video.i_height;
639 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
640 p_sys->video = p_dec->fmt_out.video;
642 for( i = 0; i < PICTURE_RING_SIZE; i++ )
644 if ( pp_ring[i] != NULL )
646 if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
648 if ( pp_ring[i]->p_data_orig != NULL )
649 free( pp_ring[i]->p_data_orig );
650 free( pp_ring[i]->p_sys );
655 pp_ring[i]->p_sys->b_dead = VLC_TRUE;
662 /* Find an empty space in the picture ring buffer */
663 for( i = 0; i < PICTURE_RING_SIZE; i++ )
665 if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
667 pp_ring[i]->i_status = RESERVED_PICTURE;
671 for( i = 0; i < PICTURE_RING_SIZE; i++ )
673 if( pp_ring[i] == NULL ) break;
676 if( i == PICTURE_RING_SIZE )
678 msg_Err( p_dec, "decoder/filter is leaking pictures, "
679 "resetting its ring buffer" );
681 for( i = 0; i < PICTURE_RING_SIZE; i++ )
683 pp_ring[i]->pf_release( pp_ring[i] );
689 p_pic = malloc( sizeof(picture_t) );
690 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
691 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
692 p_dec->fmt_out.video.i_chroma,
693 p_dec->fmt_out.video.i_width,
694 p_dec->fmt_out.video.i_height,
695 p_dec->fmt_out.video.i_aspect );
697 if( !p_pic->i_planes )
703 p_pic->pf_release = video_release_buffer;
704 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
705 p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
706 p_pic->p_sys->b_dead = VLC_FALSE;
707 p_pic->i_status = RESERVED_PICTURE;
714 static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
716 p_pic->i_refcount = 0;
717 p_pic->i_status = DESTROYED_PICTURE;
718 if ( p_pic->p_sys->b_dead )
720 if ( p_pic->p_data_orig != NULL )
721 free( p_pic->p_data_orig );
722 free( p_pic->p_sys );
727 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
732 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
734 video_release_buffer( p_pic );