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/decoder.h>
36 #include "vlc_image.h"
38 #include "../video_filter/mosaic.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 struct sout_stream_sys_t
49 image_handler_t *p_image; /* filter for resizing */
50 int i_height, i_width;
51 unsigned int i_sar_num, i_sar_den;
56 #define PICTURE_RING_SIZE 4
57 struct decoder_owner_sys_t
59 picture_t *pp_pics[PICTURE_RING_SIZE];
61 /* Current format in use by the output */
65 typedef void (* pf_release_t)( picture_t * );
66 static void ReleasePicture( picture_t *p_pic )
70 if ( p_pic->i_refcount <= 0 )
72 if ( p_pic->p_sys != NULL )
74 pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
80 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
81 if( p_pic ) free( p_pic );
86 /*****************************************************************************
88 *****************************************************************************/
89 static int Open ( vlc_object_t * );
90 static void Close ( vlc_object_t * );
91 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
92 static int Del ( sout_stream_t *, sout_stream_id_t * );
93 static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
95 static void video_del_buffer( decoder_t *, picture_t * );
96 static picture_t *video_new_buffer( decoder_t * );
97 static void video_link_picture_decoder( decoder_t *, picture_t * );
98 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
100 /*****************************************************************************
102 *****************************************************************************/
103 #define ID_TEXT N_("ID")
104 #define ID_LONGTEXT N_( \
105 "Specify an identifier string for this subpicture" )
107 #define WIDTH_TEXT N_("Video width")
108 #define WIDTH_LONGTEXT N_( \
109 "Output video width." )
110 #define HEIGHT_TEXT N_("Video height")
111 #define HEIGHT_LONGTEXT N_( \
112 "Output video height." )
113 #define RATIO_TEXT N_("Sample aspect ratio")
114 #define RATIO_LONGTEXT N_( \
115 "Sample aspect ratio of the destination (1:1, 3:4, 2:3)." )
117 #define SOUT_CFG_PREFIX "sout-mosaic-bridge-"
120 set_shortname( _( "Mosaic bridge" ) );
121 set_description(_("Mosaic bridge stream output") );
122 set_capability( "sout stream", 0 );
123 add_shortcut( "mosaic-bridge" );
125 add_string( SOUT_CFG_PREFIX "id", "Id", NULL, ID_TEXT, ID_LONGTEXT,
127 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
128 WIDTH_LONGTEXT, VLC_TRUE );
129 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
130 HEIGHT_LONGTEXT, VLC_TRUE );
131 add_string( SOUT_CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
132 RATIO_LONGTEXT, VLC_FALSE );
134 set_callbacks( Open, Close );
136 var_Create( p_module->p_libvlc_global, "mosaic-lock", VLC_VAR_MUTEX );
139 static const char *ppsz_sout_options[] = {
140 "id", "width", "height", "sar", NULL
143 /*****************************************************************************
145 *****************************************************************************/
146 static int Open( vlc_object_t *p_this )
148 sout_stream_t *p_stream = (sout_stream_t *)p_this;
149 sout_stream_sys_t *p_sys;
150 libvlc_global_data_t *p_libvlc_global = p_this->p_libvlc_global;
153 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
156 p_sys = malloc( sizeof( sout_stream_sys_t ) );
157 p_stream->p_sys = p_sys;
158 p_sys->b_inited = VLC_FALSE;
160 var_Get( p_libvlc_global, "mosaic-lock", &val );
161 p_sys->p_lock = val.p_address;
163 var_Get( p_stream, SOUT_CFG_PREFIX "id", &val );
164 p_sys->psz_id = val.psz_string;
166 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
167 p_sys->i_height = val.i_int;
169 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
170 p_sys->i_width = val.i_int;
172 var_Get( p_stream, SOUT_CFG_PREFIX "sar", &val );
173 if ( val.psz_string )
175 char *psz_parser = strchr( val.psz_string, ':' );
179 *psz_parser++ = '\0';
180 p_sys->i_sar_num = atoi( val.psz_string );
181 p_sys->i_sar_den = atoi( psz_parser );
182 vlc_ureduce( &p_sys->i_sar_num, &p_sys->i_sar_den,
183 p_sys->i_sar_num, p_sys->i_sar_den, 0 );
187 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
188 p_sys->i_sar_num = p_sys->i_sar_den = 1;
191 free( val.psz_string );
195 p_sys->i_sar_num = p_sys->i_sar_den = 1;
198 p_stream->pf_add = Add;
199 p_stream->pf_del = Del;
200 p_stream->pf_send = Send;
202 p_stream->p_sout->i_out_pace_nocontrol++;
207 /*****************************************************************************
209 *****************************************************************************/
210 static void Close( vlc_object_t * p_this )
212 sout_stream_t *p_stream = (sout_stream_t*)p_this;
213 sout_stream_sys_t *p_sys = p_stream->p_sys;
215 p_stream->p_sout->i_out_pace_nocontrol--;
218 free( p_sys->psz_id );
223 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
225 sout_stream_sys_t *p_sys = p_stream->p_sys;
230 if ( p_sys->b_inited )
235 /* Create decoder object */
236 p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
237 vlc_object_attach( p_sys->p_decoder, p_stream );
238 p_sys->p_decoder->p_module = NULL;
239 p_sys->p_decoder->fmt_in = *p_fmt;
240 p_sys->p_decoder->b_pace_control = VLC_FALSE;
241 p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
242 p_sys->p_decoder->fmt_out.i_extra = 0;
243 p_sys->p_decoder->fmt_out.p_extra = 0;
244 p_sys->p_decoder->pf_decode_video = 0;
245 p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer;
246 p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer;
247 p_sys->p_decoder->pf_picture_link = video_link_picture_decoder;
248 p_sys->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
249 p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
250 for( i = 0; i < PICTURE_RING_SIZE; i++ )
251 p_sys->p_decoder->p_owner->pp_pics[i] = 0;
252 p_sys->p_decoder->p_owner->video = p_fmt->video;
253 //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
255 p_sys->p_decoder->p_module =
256 module_Need( p_sys->p_decoder, "decoder", "$codec", 0 );
258 if( !p_sys->p_decoder->p_module )
260 msg_Err( p_stream, "cannot find decoder" );
261 vlc_object_detach( p_sys->p_decoder );
262 vlc_object_destroy( p_sys->p_decoder );
266 p_sys->b_inited = VLC_TRUE;
267 vlc_mutex_lock( p_sys->p_lock );
269 p_bridge = GetBridge( p_stream );
270 if ( p_bridge == NULL )
272 libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
275 p_bridge = malloc( sizeof( bridge_t ) );
277 var_Create( p_libvlc_global, "mosaic-struct", VLC_VAR_ADDRESS );
278 val.p_address = p_bridge;
279 var_Set( p_libvlc_global, "mosaic-struct", val );
281 p_bridge->i_es_num = 0;
282 p_bridge->pp_es = NULL;
285 for ( i = 0; i < p_bridge->i_es_num; i++ )
287 if ( p_bridge->pp_es[i]->b_empty )
291 if ( i == p_bridge->i_es_num )
293 p_bridge->pp_es = realloc( p_bridge->pp_es,
294 (p_bridge->i_es_num + 1)
295 * sizeof(bridged_es_t *) );
296 p_bridge->i_es_num++;
297 p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
300 p_sys->p_es = p_es = p_bridge->pp_es[i];
302 //p_es->fmt = *p_fmt;
303 p_es->psz_id = p_sys->psz_id;
304 p_es->p_picture = NULL;
305 p_es->pp_last = &p_es->p_picture;
306 p_es->b_empty = VLC_FALSE;
308 vlc_mutex_unlock( p_sys->p_lock );
310 if ( p_sys->i_height || p_sys->i_width )
312 p_sys->p_image = image_HandlerCreate( p_stream );
315 msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
317 return (sout_stream_id_t *)p_sys;
320 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
322 sout_stream_sys_t *p_sys = p_stream->p_sys;
325 vlc_bool_t b_last_es = VLC_TRUE;
328 if ( !p_sys->b_inited )
333 if ( p_sys->p_decoder != NULL )
335 picture_t **pp_ring = p_sys->p_decoder->p_owner->pp_pics;
337 if( p_sys->p_decoder->p_module )
338 module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
339 vlc_object_detach( p_sys->p_decoder );
340 vlc_object_destroy( p_sys->p_decoder );
342 for( i = 0; i < PICTURE_RING_SIZE; i++ )
344 if ( pp_ring[i] != NULL )
346 if ( pp_ring[i]->p_data_orig != NULL )
347 free( pp_ring[i]->p_data_orig );
348 free( pp_ring[i]->p_sys );
354 vlc_mutex_lock( p_sys->p_lock );
356 p_bridge = GetBridge( p_stream );
359 p_es->b_empty = VLC_TRUE;
360 while ( p_es->p_picture )
362 picture_t *p_next = p_es->p_picture->p_next;
363 p_es->p_picture->pf_release( p_es->p_picture );
364 p_es->p_picture = p_next;
367 for ( i = 0; i < p_bridge->i_es_num; i++ )
369 if ( !p_bridge->pp_es[i]->b_empty )
371 b_last_es = VLC_FALSE;
378 libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
379 for ( i = 0; i < p_bridge->i_es_num; i++ )
380 free( p_bridge->pp_es[i] );
381 free( p_bridge->pp_es );
383 var_Destroy( p_libvlc_global, "mosaic-struct" );
386 vlc_mutex_unlock( p_sys->p_lock );
388 if ( p_sys->i_height || p_sys->i_width )
390 image_HandlerDelete( p_sys->p_image );
393 p_sys->b_inited = VLC_FALSE;
398 /*****************************************************************************
399 * PushPicture : push a picture in the mosaic-struct structure
400 *****************************************************************************/
401 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
403 sout_stream_sys_t *p_sys = p_stream->p_sys;
404 bridged_es_t *p_es = p_sys->p_es;
406 vlc_mutex_lock( p_sys->p_lock );
408 *p_es->pp_last = p_picture;
409 p_picture->p_next = NULL;
410 p_es->pp_last = &p_picture->p_next;
412 vlc_mutex_unlock( p_sys->p_lock );
415 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
418 sout_stream_sys_t *p_sys = p_stream->p_sys;
421 if ( (sout_stream_sys_t *)id != p_sys )
423 block_ChainRelease( p_buffer );
427 while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
430 picture_t *p_new_pic;
432 if ( p_sys->i_height || p_sys->i_width )
434 video_format_t fmt_out, fmt_in;
436 memset( &fmt_in, 0, sizeof(video_format_t) );
437 memset( &fmt_out, 0, sizeof(video_format_t) );
438 fmt_in = p_sys->p_decoder->fmt_out.video;
440 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
442 if ( !p_sys->i_height )
444 fmt_out.i_width = p_sys->i_width;
445 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
446 * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
449 else if ( !p_sys->i_width )
451 fmt_out.i_height = p_sys->i_height;
452 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
453 * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
458 fmt_out.i_width = p_sys->i_width;
459 fmt_out.i_height = p_sys->i_height;
461 fmt_out.i_visible_width = fmt_out.i_width;
462 fmt_out.i_visible_height = fmt_out.i_height;
464 p_new_pic = image_Convert( p_sys->p_image,
465 p_pic, &fmt_in, &fmt_out );
466 if ( p_new_pic == NULL )
468 msg_Err( p_stream, "image conversion failed" );
474 p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
475 vout_AllocatePicture( p_stream, p_new_pic, p_pic->format.i_chroma,
476 p_pic->format.i_width, p_pic->format.i_height,
477 p_sys->p_decoder->fmt_out.video.i_aspect );
479 vout_CopyPicture( p_stream, p_new_pic, p_pic );
482 p_new_pic->i_refcount = 1;
483 p_new_pic->i_status = DESTROYED_PICTURE;
484 p_new_pic->i_type = DIRECT_PICTURE;
485 p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
486 p_new_pic->pf_release = ReleasePicture;
487 p_new_pic->date = p_pic->date;
489 p_pic->pf_release( p_pic );
490 PushPicture( p_stream, p_new_pic );
498 vlc_object_t *p_owner;
502 static void video_release_buffer( picture_t *p_pic )
504 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
506 video_del_buffer( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
508 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
511 static picture_t *video_new_buffer( decoder_t *p_dec )
513 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
514 picture_t **pp_ring = p_dec->p_owner->pp_pics;
518 if( p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
519 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
520 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
521 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
523 if( !p_dec->fmt_out.video.i_sar_num ||
524 !p_dec->fmt_out.video.i_sar_den )
526 p_dec->fmt_out.video.i_sar_num =
527 p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
529 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
530 p_dec->fmt_out.video.i_width;
533 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
534 &p_dec->fmt_out.video.i_sar_den,
535 p_dec->fmt_out.video.i_sar_num,
536 p_dec->fmt_out.video.i_sar_den, 0 );
538 if( !p_dec->fmt_out.video.i_visible_width ||
539 !p_dec->fmt_out.video.i_visible_height )
541 p_dec->fmt_out.video.i_visible_width =
542 p_dec->fmt_out.video.i_width;
543 p_dec->fmt_out.video.i_visible_height =
544 p_dec->fmt_out.video.i_height;
547 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
548 p_sys->video = p_dec->fmt_out.video;
550 for( i = 0; i < PICTURE_RING_SIZE; i++ )
552 if ( pp_ring[i] != NULL )
554 if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
556 if ( pp_ring[i]->p_data_orig != NULL )
557 free( pp_ring[i]->p_data_orig );
558 free( pp_ring[i]->p_sys );
563 pp_ring[i]->p_sys->b_dead = VLC_TRUE;
570 /* Find an empty space in the picture ring buffer */
571 for( i = 0; i < PICTURE_RING_SIZE; i++ )
573 if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
575 pp_ring[i]->i_status = RESERVED_PICTURE;
579 for( i = 0; i < PICTURE_RING_SIZE; i++ )
581 if( pp_ring[i] == NULL ) break;
584 if( i == PICTURE_RING_SIZE )
586 msg_Err( p_dec, "decoder/filter is leaking pictures, "
587 "resetting its ring buffer" );
589 for( i = 0; i < PICTURE_RING_SIZE; i++ )
591 pp_ring[i]->pf_release( pp_ring[i] );
597 p_pic = malloc( sizeof(picture_t) );
598 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
599 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
600 p_dec->fmt_out.video.i_chroma,
601 p_dec->fmt_out.video.i_width,
602 p_dec->fmt_out.video.i_height,
603 p_dec->fmt_out.video.i_aspect );
605 if( !p_pic->i_planes )
611 p_pic->pf_release = video_release_buffer;
612 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
613 p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
614 p_pic->p_sys->b_dead = VLC_FALSE;
615 p_pic->i_status = RESERVED_PICTURE;
622 static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
624 p_pic->i_refcount = 0;
625 p_pic->i_status = DESTROYED_PICTURE;
626 if ( p_pic->p_sys->b_dead )
628 if ( p_pic->p_data_orig != NULL )
629 free( p_pic->p_data_orig );
630 free( p_pic->p_sys );
635 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
640 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
642 video_release_buffer( p_pic );