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;
57 #define PICTURE_RING_SIZE 4
58 struct decoder_owner_sys_t
60 picture_t *pp_pics[PICTURE_RING_SIZE];
62 /* Current format in use by the output */
66 typedef void (* pf_release_t)( picture_t * );
67 static void ReleasePicture( picture_t *p_pic )
71 if ( p_pic->i_refcount <= 0 )
73 if ( p_pic->p_sys != NULL )
75 pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
81 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
82 if( p_pic ) free( p_pic );
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open ( vlc_object_t * );
91 static void Close ( vlc_object_t * );
92 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
93 static int Del ( sout_stream_t *, sout_stream_id_t * );
94 static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
96 static void video_del_buffer( decoder_t *, picture_t * );
97 static picture_t *video_new_buffer( decoder_t * );
98 static void video_link_picture_decoder( decoder_t *, picture_t * );
99 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
101 /*****************************************************************************
103 *****************************************************************************/
104 #define ID_TEXT N_("ID")
105 #define ID_LONGTEXT N_( \
106 "Specify an identifier string for this subpicture" )
108 #define WIDTH_TEXT N_("Video width")
109 #define WIDTH_LONGTEXT N_( \
110 "Output video width." )
111 #define HEIGHT_TEXT N_("Video height")
112 #define HEIGHT_LONGTEXT N_( \
113 "Output video height." )
114 #define RATIO_TEXT N_("Sample aspect ratio")
115 #define RATIO_LONGTEXT N_( \
116 "Sample aspect ratio of the destination (1:1, 3:4, 2:3)." )
118 #define SOUT_CFG_PREFIX "sout-mosaic-bridge-"
121 set_shortname( _( "Mosaic bridge" ) );
122 set_description(_("Mosaic bridge stream output") );
123 set_capability( "sout stream", 0 );
124 add_shortcut( "mosaic-bridge" );
126 add_string( SOUT_CFG_PREFIX "id", "Id", NULL, ID_TEXT, ID_LONGTEXT,
128 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
129 WIDTH_LONGTEXT, VLC_TRUE );
130 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
131 HEIGHT_LONGTEXT, VLC_TRUE );
132 add_string( SOUT_CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
133 RATIO_LONGTEXT, VLC_FALSE );
135 set_callbacks( Open, Close );
137 var_Create( p_module->p_libvlc_global, "mosaic-lock", VLC_VAR_MUTEX );
140 static const char *ppsz_sout_options[] = {
141 "id", "width", "height", "sar", NULL
144 /*****************************************************************************
146 *****************************************************************************/
147 static int Open( vlc_object_t *p_this )
149 sout_stream_t *p_stream = (sout_stream_t *)p_this;
150 sout_stream_sys_t *p_sys;
151 libvlc_global_data_t *p_libvlc_global = p_this->p_libvlc_global;
154 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
157 p_sys = malloc( sizeof( sout_stream_sys_t ) );
158 p_stream->p_sys = p_sys;
159 p_sys->b_inited = VLC_FALSE;
161 var_Get( p_libvlc_global, "mosaic-lock", &val );
162 p_sys->p_lock = val.p_address;
164 var_Get( p_stream, SOUT_CFG_PREFIX "id", &val );
165 p_sys->psz_id = val.psz_string;
167 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
168 p_sys->i_height = val.i_int;
170 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
171 p_sys->i_width = val.i_int;
173 var_Get( p_stream, SOUT_CFG_PREFIX "sar", &val );
174 if ( val.psz_string )
176 char *psz_parser = strchr( val.psz_string, ':' );
180 *psz_parser++ = '\0';
181 p_sys->i_sar_num = atoi( val.psz_string );
182 p_sys->i_sar_den = atoi( psz_parser );
183 vlc_ureduce( &p_sys->i_sar_num, &p_sys->i_sar_den,
184 p_sys->i_sar_num, p_sys->i_sar_den, 0 );
188 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
189 p_sys->i_sar_num = p_sys->i_sar_den = 1;
192 free( val.psz_string );
196 p_sys->i_sar_num = p_sys->i_sar_den = 1;
199 p_stream->pf_add = Add;
200 p_stream->pf_del = Del;
201 p_stream->pf_send = Send;
203 p_stream->p_sout->i_out_pace_nocontrol++;
208 /*****************************************************************************
210 *****************************************************************************/
211 static void Close( vlc_object_t * p_this )
213 sout_stream_t *p_stream = (sout_stream_t*)p_this;
214 sout_stream_sys_t *p_sys = p_stream->p_sys;
216 p_stream->p_sout->i_out_pace_nocontrol--;
219 free( p_sys->psz_id );
224 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
226 sout_stream_sys_t *p_sys = p_stream->p_sys;
231 if ( p_sys->b_inited )
236 /* Create decoder object */
237 p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
238 vlc_object_attach( p_sys->p_decoder, p_stream );
239 p_sys->p_decoder->p_module = NULL;
240 p_sys->p_decoder->fmt_in = *p_fmt;
241 p_sys->p_decoder->b_pace_control = VLC_FALSE;
242 p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
243 p_sys->p_decoder->fmt_out.i_extra = 0;
244 p_sys->p_decoder->fmt_out.p_extra = 0;
245 p_sys->p_decoder->pf_decode_video = 0;
246 p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer;
247 p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer;
248 p_sys->p_decoder->pf_picture_link = video_link_picture_decoder;
249 p_sys->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
250 p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
251 for( i = 0; i < PICTURE_RING_SIZE; i++ )
252 p_sys->p_decoder->p_owner->pp_pics[i] = 0;
253 p_sys->p_decoder->p_owner->video = p_fmt->video;
254 //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
256 p_sys->p_decoder->p_module =
257 module_Need( p_sys->p_decoder, "decoder", "$codec", 0 );
259 if( !p_sys->p_decoder->p_module )
261 msg_Err( p_stream, "cannot find decoder" );
262 vlc_object_detach( p_sys->p_decoder );
263 vlc_object_destroy( p_sys->p_decoder );
267 p_sys->b_inited = VLC_TRUE;
268 vlc_mutex_lock( p_sys->p_lock );
270 p_bridge = GetBridge( p_stream );
271 if ( p_bridge == NULL )
273 libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
276 p_bridge = malloc( sizeof( bridge_t ) );
278 var_Create( p_libvlc_global, "mosaic-struct", VLC_VAR_ADDRESS );
279 val.p_address = p_bridge;
280 var_Set( p_libvlc_global, "mosaic-struct", val );
282 p_bridge->i_es_num = 0;
283 p_bridge->pp_es = NULL;
286 for ( i = 0; i < p_bridge->i_es_num; i++ )
288 if ( p_bridge->pp_es[i]->b_empty )
292 if ( i == p_bridge->i_es_num )
294 p_bridge->pp_es = realloc( p_bridge->pp_es,
295 (p_bridge->i_es_num + 1)
296 * sizeof(bridged_es_t *) );
297 p_bridge->i_es_num++;
298 p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
301 p_sys->p_es = p_es = p_bridge->pp_es[i];
303 //p_es->fmt = *p_fmt;
304 p_es->psz_id = p_sys->psz_id;
305 p_es->p_picture = NULL;
306 p_es->pp_last = &p_es->p_picture;
307 p_es->b_empty = VLC_FALSE;
309 vlc_mutex_unlock( p_sys->p_lock );
311 if ( p_sys->i_height || p_sys->i_width )
313 p_sys->p_image = image_HandlerCreate( p_stream );
316 msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
318 return (sout_stream_id_t *)p_sys;
321 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
323 sout_stream_sys_t *p_sys = p_stream->p_sys;
326 vlc_bool_t b_last_es = VLC_TRUE;
329 if ( !p_sys->b_inited )
334 if ( p_sys->p_decoder != NULL )
336 picture_t **pp_ring = p_sys->p_decoder->p_owner->pp_pics;
338 if( p_sys->p_decoder->p_module )
339 module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
340 vlc_object_detach( p_sys->p_decoder );
341 vlc_object_destroy( p_sys->p_decoder );
343 for( i = 0; i < PICTURE_RING_SIZE; i++ )
345 if ( pp_ring[i] != NULL )
347 if ( pp_ring[i]->p_data_orig != NULL )
348 free( pp_ring[i]->p_data_orig );
349 free( pp_ring[i]->p_sys );
355 vlc_mutex_lock( p_sys->p_lock );
357 p_bridge = GetBridge( p_stream );
360 p_es->b_empty = VLC_TRUE;
361 while ( p_es->p_picture )
363 picture_t *p_next = p_es->p_picture->p_next;
364 p_es->p_picture->pf_release( p_es->p_picture );
365 p_es->p_picture = p_next;
368 for ( i = 0; i < p_bridge->i_es_num; i++ )
370 if ( !p_bridge->pp_es[i]->b_empty )
372 b_last_es = VLC_FALSE;
379 libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
380 for ( i = 0; i < p_bridge->i_es_num; i++ )
381 free( p_bridge->pp_es[i] );
382 free( p_bridge->pp_es );
384 var_Destroy( p_libvlc_global, "mosaic-struct" );
387 vlc_mutex_unlock( p_sys->p_lock );
389 if ( p_sys->i_height || p_sys->i_width )
391 image_HandlerDelete( p_sys->p_image );
394 p_sys->b_inited = VLC_FALSE;
399 /*****************************************************************************
400 * PushPicture : push a picture in the mosaic-struct structure
401 *****************************************************************************/
402 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
404 sout_stream_sys_t *p_sys = p_stream->p_sys;
405 bridged_es_t *p_es = p_sys->p_es;
407 vlc_mutex_lock( p_sys->p_lock );
409 *p_es->pp_last = p_picture;
410 p_picture->p_next = NULL;
411 p_es->pp_last = &p_picture->p_next;
413 vlc_mutex_unlock( p_sys->p_lock );
416 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
419 sout_stream_sys_t *p_sys = p_stream->p_sys;
422 if ( (sout_stream_sys_t *)id != p_sys )
424 block_ChainRelease( p_buffer );
428 while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
431 picture_t *p_new_pic;
433 if ( p_sys->i_height || p_sys->i_width )
435 video_format_t fmt_out, fmt_in;
437 memset( &fmt_in, 0, sizeof(video_format_t) );
438 memset( &fmt_out, 0, sizeof(video_format_t) );
439 fmt_in = p_sys->p_decoder->fmt_out.video;
441 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
443 if ( !p_sys->i_height )
445 fmt_out.i_width = p_sys->i_width;
446 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
447 * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
450 else if ( !p_sys->i_width )
452 fmt_out.i_height = p_sys->i_height;
453 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
454 * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
459 fmt_out.i_width = p_sys->i_width;
460 fmt_out.i_height = p_sys->i_height;
462 fmt_out.i_visible_width = fmt_out.i_width;
463 fmt_out.i_visible_height = fmt_out.i_height;
465 p_new_pic = image_Convert( p_sys->p_image,
466 p_pic, &fmt_in, &fmt_out );
467 if ( p_new_pic == NULL )
469 msg_Err( p_stream, "image conversion failed" );
475 p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
476 vout_AllocatePicture( p_stream, p_new_pic, p_pic->format.i_chroma,
477 p_pic->format.i_width, p_pic->format.i_height,
478 p_sys->p_decoder->fmt_out.video.i_aspect );
480 vout_CopyPicture( p_stream, p_new_pic, p_pic );
483 p_new_pic->i_refcount = 1;
484 p_new_pic->i_status = DESTROYED_PICTURE;
485 p_new_pic->i_type = DIRECT_PICTURE;
486 p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
487 p_new_pic->pf_release = ReleasePicture;
488 p_new_pic->date = p_pic->date;
490 p_pic->pf_release( p_pic );
491 PushPicture( p_stream, p_new_pic );
499 vlc_object_t *p_owner;
503 static void video_release_buffer( picture_t *p_pic )
505 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
507 video_del_buffer( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
509 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
512 static picture_t *video_new_buffer( decoder_t *p_dec )
514 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
515 picture_t **pp_ring = p_dec->p_owner->pp_pics;
519 if( p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
520 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
521 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
522 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
524 if( !p_dec->fmt_out.video.i_sar_num ||
525 !p_dec->fmt_out.video.i_sar_den )
527 p_dec->fmt_out.video.i_sar_num =
528 p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
530 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
531 p_dec->fmt_out.video.i_width;
534 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
535 &p_dec->fmt_out.video.i_sar_den,
536 p_dec->fmt_out.video.i_sar_num,
537 p_dec->fmt_out.video.i_sar_den, 0 );
539 if( !p_dec->fmt_out.video.i_visible_width ||
540 !p_dec->fmt_out.video.i_visible_height )
542 p_dec->fmt_out.video.i_visible_width =
543 p_dec->fmt_out.video.i_width;
544 p_dec->fmt_out.video.i_visible_height =
545 p_dec->fmt_out.video.i_height;
548 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
549 p_sys->video = p_dec->fmt_out.video;
551 for( i = 0; i < PICTURE_RING_SIZE; i++ )
553 if ( pp_ring[i] != NULL )
555 if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
557 if ( pp_ring[i]->p_data_orig != NULL )
558 free( pp_ring[i]->p_data_orig );
559 free( pp_ring[i]->p_sys );
564 pp_ring[i]->p_sys->b_dead = VLC_TRUE;
571 /* Find an empty space in the picture ring buffer */
572 for( i = 0; i < PICTURE_RING_SIZE; i++ )
574 if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
576 pp_ring[i]->i_status = RESERVED_PICTURE;
580 for( i = 0; i < PICTURE_RING_SIZE; i++ )
582 if( pp_ring[i] == NULL ) break;
585 if( i == PICTURE_RING_SIZE )
587 msg_Err( p_dec, "decoder/filter is leaking pictures, "
588 "resetting its ring buffer" );
590 for( i = 0; i < PICTURE_RING_SIZE; i++ )
592 pp_ring[i]->pf_release( pp_ring[i] );
598 p_pic = malloc( sizeof(picture_t) );
599 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
600 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
601 p_dec->fmt_out.video.i_chroma,
602 p_dec->fmt_out.video.i_width,
603 p_dec->fmt_out.video.i_height,
604 p_dec->fmt_out.video.i_aspect );
606 if( !p_pic->i_planes )
612 p_pic->pf_release = video_release_buffer;
613 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
614 p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
615 p_pic->p_sys->b_dead = VLC_FALSE;
616 p_pic->i_status = RESERVED_PICTURE;
623 static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
625 p_pic->i_refcount = 0;
626 p_pic->i_status = DESTROYED_PICTURE;
627 if ( p_pic->p_sys->b_dead )
629 if ( p_pic->p_data_orig != NULL )
630 free( p_pic->p_data_orig );
631 free( p_pic->p_sys );
636 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
641 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
643 video_release_buffer( p_pic );