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 );
260 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
262 sout_stream_sys_t *p_sys = p_stream->p_sys;
267 if ( p_sys->b_inited )
272 /* Create decoder object */
273 p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
274 vlc_object_attach( p_sys->p_decoder, p_stream );
275 p_sys->p_decoder->p_module = NULL;
276 p_sys->p_decoder->fmt_in = *p_fmt;
277 p_sys->p_decoder->b_pace_control = VLC_FALSE;
278 p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
279 p_sys->p_decoder->fmt_out.i_extra = 0;
280 p_sys->p_decoder->fmt_out.p_extra = 0;
281 p_sys->p_decoder->pf_decode_video = 0;
282 p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer;
283 p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer;
284 p_sys->p_decoder->pf_picture_link = video_link_picture_decoder;
285 p_sys->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
286 p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
287 for( i = 0; i < PICTURE_RING_SIZE; i++ )
288 p_sys->p_decoder->p_owner->pp_pics[i] = 0;
289 p_sys->p_decoder->p_owner->video = p_fmt->video;
290 //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
292 p_sys->p_decoder->p_module =
293 module_Need( p_sys->p_decoder, "decoder", "$codec", 0 );
295 if( !p_sys->p_decoder->p_module )
297 msg_Err( p_stream, "cannot find decoder" );
298 vlc_object_detach( p_sys->p_decoder );
299 vlc_object_destroy( p_sys->p_decoder );
303 p_sys->b_inited = VLC_TRUE;
304 vlc_mutex_lock( p_sys->p_lock );
306 p_bridge = GetBridge( p_stream );
307 if ( p_bridge == NULL )
309 libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
312 p_bridge = malloc( sizeof( bridge_t ) );
314 var_Create( p_libvlc_global, "mosaic-struct", VLC_VAR_ADDRESS );
315 val.p_address = p_bridge;
316 var_Set( p_libvlc_global, "mosaic-struct", val );
318 p_bridge->i_es_num = 0;
319 p_bridge->pp_es = NULL;
322 for ( i = 0; i < p_bridge->i_es_num; i++ )
324 if ( p_bridge->pp_es[i]->b_empty )
328 if ( i == p_bridge->i_es_num )
330 p_bridge->pp_es = realloc( p_bridge->pp_es,
331 (p_bridge->i_es_num + 1)
332 * sizeof(bridged_es_t *) );
333 p_bridge->i_es_num++;
334 p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
337 p_sys->p_es = p_es = p_bridge->pp_es[i];
339 //p_es->fmt = *p_fmt;
340 p_es->psz_id = p_sys->psz_id;
341 p_es->p_picture = NULL;
342 p_es->pp_last = &p_es->p_picture;
343 p_es->b_empty = VLC_FALSE;
345 vlc_mutex_unlock( p_sys->p_lock );
347 if ( p_sys->i_height || p_sys->i_width )
349 p_sys->p_image = image_HandlerCreate( p_stream );
352 msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
354 return (sout_stream_id_t *)p_sys;
357 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
359 sout_stream_sys_t *p_sys = p_stream->p_sys;
362 vlc_bool_t b_last_es = VLC_TRUE;
365 if ( !p_sys->b_inited )
370 if ( p_sys->p_decoder != NULL )
372 picture_t **pp_ring = p_sys->p_decoder->p_owner->pp_pics;
374 if( p_sys->p_decoder->p_module )
375 module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
376 vlc_object_detach( p_sys->p_decoder );
377 vlc_object_destroy( p_sys->p_decoder );
379 for( i = 0; i < PICTURE_RING_SIZE; i++ )
381 if ( pp_ring[i] != NULL )
383 if ( pp_ring[i]->p_data_orig != NULL )
384 free( pp_ring[i]->p_data_orig );
385 free( pp_ring[i]->p_sys );
391 vlc_mutex_lock( p_sys->p_lock );
393 p_bridge = GetBridge( p_stream );
396 p_es->b_empty = VLC_TRUE;
397 while ( p_es->p_picture )
399 picture_t *p_next = p_es->p_picture->p_next;
400 p_es->p_picture->pf_release( p_es->p_picture );
401 p_es->p_picture = p_next;
404 for ( i = 0; i < p_bridge->i_es_num; i++ )
406 if ( !p_bridge->pp_es[i]->b_empty )
408 b_last_es = VLC_FALSE;
415 libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
416 for ( i = 0; i < p_bridge->i_es_num; i++ )
417 free( p_bridge->pp_es[i] );
418 free( p_bridge->pp_es );
420 var_Destroy( p_libvlc_global, "mosaic-struct" );
423 vlc_mutex_unlock( p_sys->p_lock );
425 if ( p_sys->i_height || p_sys->i_width )
427 image_HandlerDelete( p_sys->p_image );
430 p_sys->b_inited = VLC_FALSE;
435 /*****************************************************************************
436 * PushPicture : push a picture in the mosaic-struct structure
437 *****************************************************************************/
438 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
440 sout_stream_sys_t *p_sys = p_stream->p_sys;
441 bridged_es_t *p_es = p_sys->p_es;
443 vlc_mutex_lock( p_sys->p_lock );
445 *p_es->pp_last = p_picture;
446 p_picture->p_next = NULL;
447 p_es->pp_last = &p_picture->p_next;
449 vlc_mutex_unlock( p_sys->p_lock );
452 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
455 sout_stream_sys_t *p_sys = p_stream->p_sys;
458 if ( (sout_stream_sys_t *)id != p_sys )
460 block_ChainRelease( p_buffer );
464 while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
467 picture_t *p_new_pic;
469 if( p_sys->i_height || p_sys->i_width )
471 video_format_t fmt_out, fmt_in;
473 memset( &fmt_in, 0, sizeof(video_format_t) );
474 memset( &fmt_out, 0, sizeof(video_format_t) );
475 fmt_in = p_sys->p_decoder->fmt_out.video;
478 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
480 fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
482 if ( !p_sys->i_height )
484 fmt_out.i_width = p_sys->i_width;
485 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
486 * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
489 else if ( !p_sys->i_width )
491 fmt_out.i_height = p_sys->i_height;
492 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
493 * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
498 fmt_out.i_width = p_sys->i_width;
499 fmt_out.i_height = p_sys->i_height;
501 fmt_out.i_visible_width = fmt_out.i_width;
502 fmt_out.i_visible_height = fmt_out.i_height;
504 p_new_pic = image_Convert( p_sys->p_image,
505 p_pic, &fmt_in, &fmt_out );
506 if ( p_new_pic == NULL )
508 msg_Err( p_stream, "image conversion failed" );
514 plane_t *p_mask = p_sys->p_mask->p+A_PLANE;
515 plane_t *p_apic = p_new_pic->p+A_PLANE;
516 if( p_mask->i_visible_pitch
517 != p_apic->i_visible_pitch
518 || p_mask->i_visible_lines
519 != p_apic->i_visible_lines )
522 "Mask size (%d x %d) and image size (%d x %d) "
523 "don't match. The mask will not be applied.",
524 p_mask->i_visible_pitch,
525 p_mask->i_visible_lines,
526 p_apic->i_visible_pitch,
527 p_apic->i_visible_lines );
531 if( p_mask->i_pitch != p_apic->i_pitch
532 || p_mask->i_lines != p_apic->i_lines )
534 /* visible plane sizes match ... but not the undelying
535 * buffer. I'm not sure that this can happen,
536 * but better safe than sorry. */
538 int i_lines = p_mask->i_visible_lines;
539 uint8_t *p_src = p_mask->p_pixels;
540 uint8_t *p_dst = p_apic->p_pixels;
541 int i_src_pitch = p_mask->i_pitch;
542 int i_dst_pitch = p_apic->i_pitch;
543 int i_visible_pitch = p_mask->i_visible_pitch;
544 for( i_line = 0; i_line < i_lines; i_line++,
545 p_src += i_src_pitch, p_dst += i_dst_pitch )
547 p_stream->p_libvlc->pf_memcpy(
548 p_dst, p_src, i_visible_pitch );
553 /* plane sizes match */
554 p_stream->p_libvlc->pf_memcpy(
555 p_apic->p_pixels, p_mask->p_pixels,
556 p_mask->i_pitch * p_mask->i_lines );
563 p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
564 vout_AllocatePicture( p_stream, p_new_pic, p_pic->format.i_chroma,
565 p_pic->format.i_width, p_pic->format.i_height,
566 p_sys->p_decoder->fmt_out.video.i_aspect );
568 vout_CopyPicture( p_stream, p_new_pic, p_pic );
571 p_new_pic->i_refcount = 1;
572 p_new_pic->i_status = DESTROYED_PICTURE;
573 p_new_pic->i_type = DIRECT_PICTURE;
574 p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
575 p_new_pic->pf_release = ReleasePicture;
576 p_new_pic->date = p_pic->date;
578 p_pic->pf_release( p_pic );
579 PushPicture( p_stream, p_new_pic );
587 vlc_object_t *p_owner;
591 static void video_release_buffer( picture_t *p_pic )
593 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
595 video_del_buffer( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
597 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
600 static picture_t *video_new_buffer( decoder_t *p_dec )
602 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
603 picture_t **pp_ring = p_dec->p_owner->pp_pics;
607 if( p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
608 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
609 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
610 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
612 if( !p_dec->fmt_out.video.i_sar_num ||
613 !p_dec->fmt_out.video.i_sar_den )
615 p_dec->fmt_out.video.i_sar_num =
616 p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
618 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
619 p_dec->fmt_out.video.i_width;
622 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
623 &p_dec->fmt_out.video.i_sar_den,
624 p_dec->fmt_out.video.i_sar_num,
625 p_dec->fmt_out.video.i_sar_den, 0 );
627 if( !p_dec->fmt_out.video.i_visible_width ||
628 !p_dec->fmt_out.video.i_visible_height )
630 p_dec->fmt_out.video.i_visible_width =
631 p_dec->fmt_out.video.i_width;
632 p_dec->fmt_out.video.i_visible_height =
633 p_dec->fmt_out.video.i_height;
636 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
637 p_sys->video = p_dec->fmt_out.video;
639 for( i = 0; i < PICTURE_RING_SIZE; i++ )
641 if ( pp_ring[i] != NULL )
643 if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
645 if ( pp_ring[i]->p_data_orig != NULL )
646 free( pp_ring[i]->p_data_orig );
647 free( pp_ring[i]->p_sys );
652 pp_ring[i]->p_sys->b_dead = VLC_TRUE;
659 /* Find an empty space in the picture ring buffer */
660 for( i = 0; i < PICTURE_RING_SIZE; i++ )
662 if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
664 pp_ring[i]->i_status = RESERVED_PICTURE;
668 for( i = 0; i < PICTURE_RING_SIZE; i++ )
670 if( pp_ring[i] == NULL ) break;
673 if( i == PICTURE_RING_SIZE )
675 msg_Err( p_dec, "decoder/filter is leaking pictures, "
676 "resetting its ring buffer" );
678 for( i = 0; i < PICTURE_RING_SIZE; i++ )
680 pp_ring[i]->pf_release( pp_ring[i] );
686 p_pic = malloc( sizeof(picture_t) );
687 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
688 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
689 p_dec->fmt_out.video.i_chroma,
690 p_dec->fmt_out.video.i_width,
691 p_dec->fmt_out.video.i_height,
692 p_dec->fmt_out.video.i_aspect );
694 if( !p_pic->i_planes )
700 p_pic->pf_release = video_release_buffer;
701 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
702 p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
703 p_pic->p_sys->b_dead = VLC_FALSE;
704 p_pic->i_status = RESERVED_PICTURE;
711 static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
713 p_pic->i_refcount = 0;
714 p_pic->i_status = DESTROYED_PICTURE;
715 if ( p_pic->p_sys->b_dead )
717 if ( p_pic->p_data_orig != NULL )
718 free( p_pic->p_data_orig );
719 free( p_pic->p_sys );
724 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
729 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
731 video_release_buffer( p_pic );