1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 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
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 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];
62 typedef void (* pf_release_t)( picture_t * );
63 static void ReleasePicture( picture_t *p_pic )
67 if ( p_pic->i_refcount <= 0 )
69 if ( p_pic->p_sys != NULL )
71 pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
77 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
78 if( p_pic ) free( p_pic );
83 /*****************************************************************************
85 *****************************************************************************/
86 static int Open ( vlc_object_t * );
87 static void Close ( vlc_object_t * );
88 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
89 static int Del ( sout_stream_t *, sout_stream_id_t * );
90 static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
92 static void video_del_buffer( decoder_t *, picture_t * );
93 static picture_t *video_new_buffer( decoder_t * );
94 static void video_link_picture_decoder( decoder_t *, picture_t * );
95 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
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 "Allows you to specify the output video width." )
107 #define HEIGHT_TEXT N_("Video height")
108 #define HEIGHT_LONGTEXT N_( \
109 "Allows you to specify the 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 SOUT_CFG_PREFIX "sout-mosaic-bridge-"
117 set_shortname( _( "Mosaic bridge" ) );
118 set_description(_("Mosaic bridge stream output") );
119 set_capability( "sout stream", 0 );
120 add_shortcut( "mosaic-bridge" );
122 add_string( SOUT_CFG_PREFIX "id", "Id", NULL, ID_TEXT, ID_LONGTEXT,
124 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
125 WIDTH_LONGTEXT, VLC_TRUE );
126 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
127 HEIGHT_LONGTEXT, VLC_TRUE );
128 add_string( SOUT_CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
129 RATIO_LONGTEXT, VLC_FALSE );
131 set_callbacks( Open, Close );
133 var_Create( p_module->p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
136 static const char *ppsz_sout_options[] = {
137 "id", "width", "height", "sar", NULL
140 /*****************************************************************************
142 *****************************************************************************/
143 static int Open( vlc_object_t *p_this )
145 sout_stream_t *p_stream = (sout_stream_t *)p_this;
146 sout_stream_sys_t *p_sys;
147 libvlc_t *p_libvlc = p_this->p_libvlc;
150 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
153 p_sys = malloc( sizeof( sout_stream_sys_t ) );
154 p_stream->p_sys = p_sys;
155 p_sys->b_inited = VLC_FALSE;
157 var_Get( p_libvlc, "mosaic-lock", &val );
158 p_sys->p_lock = val.p_address;
160 var_Get( p_stream, SOUT_CFG_PREFIX "id", &val );
161 p_sys->psz_id = val.psz_string;
163 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
164 p_sys->i_height = val.i_int;
166 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
167 p_sys->i_width = val.i_int;
169 var_Get( p_stream, SOUT_CFG_PREFIX "sar", &val );
170 if ( val.psz_string )
172 char *psz_parser = strchr( val.psz_string, ':' );
176 *psz_parser++ = '\0';
177 p_sys->i_sar_num = atoi( val.psz_string );
178 p_sys->i_sar_den = atoi( psz_parser );
179 vlc_reduce( &p_sys->i_sar_num, &p_sys->i_sar_den,
180 p_sys->i_sar_num, p_sys->i_sar_den, 0 );
184 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
185 p_sys->i_sar_num = p_sys->i_sar_den = 1;
188 free( val.psz_string );
192 p_sys->i_sar_num = p_sys->i_sar_den = 1;
195 p_stream->pf_add = Add;
196 p_stream->pf_del = Del;
197 p_stream->pf_send = Send;
199 p_stream->p_sout->i_out_pace_nocontrol++;
204 /*****************************************************************************
206 *****************************************************************************/
207 static void Close( vlc_object_t * p_this )
209 sout_stream_t *p_stream = (sout_stream_t*)p_this;
210 sout_stream_sys_t *p_sys = p_stream->p_sys;
212 p_stream->p_sout->i_out_pace_nocontrol--;
215 free( p_sys->psz_id );
220 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
222 sout_stream_sys_t *p_sys = p_stream->p_sys;
227 if ( p_sys->b_inited )
232 /* Create decoder object */
233 p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
234 vlc_object_attach( p_sys->p_decoder, p_stream );
235 p_sys->p_decoder->p_module = NULL;
236 p_sys->p_decoder->fmt_in = *p_fmt;
237 p_sys->p_decoder->b_pace_control = VLC_FALSE;
238 p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
239 p_sys->p_decoder->fmt_out.i_extra = 0;
240 p_sys->p_decoder->fmt_out.p_extra = 0;
241 p_sys->p_decoder->pf_decode_video = 0;
242 p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer;
243 p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer;
244 p_sys->p_decoder->pf_picture_link = video_link_picture_decoder;
245 p_sys->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
246 p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
247 for( i = 0; i < PICTURE_RING_SIZE; i++ )
248 p_sys->p_decoder->p_owner->pp_pics[i] = 0;
249 //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
251 p_sys->p_decoder->p_module =
252 module_Need( p_sys->p_decoder, "decoder", "$codec", 0 );
254 if( !p_sys->p_decoder->p_module )
256 msg_Err( p_stream, "cannot find decoder" );
257 vlc_object_detach( p_sys->p_decoder );
258 vlc_object_destroy( p_sys->p_decoder );
262 p_sys->b_inited = VLC_TRUE;
263 vlc_mutex_lock( p_sys->p_lock );
265 p_bridge = GetBridge( p_stream );
266 if ( p_bridge == NULL )
268 libvlc_t *p_libvlc = p_stream->p_libvlc;
271 p_bridge = malloc( sizeof( bridge_t ) );
273 var_Create( p_libvlc, "mosaic-struct", VLC_VAR_ADDRESS );
274 val.p_address = p_bridge;
275 var_Set( p_libvlc, "mosaic-struct", val );
277 p_bridge->i_es_num = 0;
278 p_bridge->pp_es = NULL;
281 for ( i = 0; i < p_bridge->i_es_num; i++ )
283 if ( p_bridge->pp_es[i]->b_empty )
287 if ( i == p_bridge->i_es_num )
289 p_bridge->pp_es = realloc( p_bridge->pp_es,
290 (p_bridge->i_es_num + 1)
291 * sizeof(bridged_es_t *) );
292 p_bridge->i_es_num++;
293 p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
296 p_sys->p_es = p_es = p_bridge->pp_es[i];
298 //p_es->fmt = *p_fmt;
299 p_es->psz_id = p_sys->psz_id;
300 p_es->p_picture = NULL;
301 p_es->pp_last = &p_es->p_picture;
302 p_es->b_empty = VLC_FALSE;
304 vlc_mutex_unlock( p_sys->p_lock );
306 if ( p_sys->i_height || p_sys->i_width )
308 p_sys->p_image = image_HandlerCreate( p_stream );
311 msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
313 return (sout_stream_id_t *)p_sys;
316 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
318 sout_stream_sys_t *p_sys = p_stream->p_sys;
321 vlc_bool_t b_last_es = VLC_TRUE;
324 if ( !p_sys->b_inited )
329 if ( p_sys->p_decoder )
331 if( p_sys->p_decoder->p_module )
332 module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
333 vlc_object_detach( p_sys->p_decoder );
334 vlc_object_destroy( p_sys->p_decoder );
337 vlc_mutex_lock( p_sys->p_lock );
339 p_bridge = GetBridge( p_stream );
342 p_es->b_empty = VLC_TRUE;
343 while ( p_es->p_picture )
345 picture_t *p_next = p_es->p_picture->p_next;
346 p_es->p_picture->pf_release( p_es->p_picture );
347 p_es->p_picture = p_next;
350 for ( i = 0; i < p_bridge->i_es_num; i++ )
352 if ( !p_bridge->pp_es[i]->b_empty )
354 b_last_es = VLC_FALSE;
361 libvlc_t *p_libvlc = p_stream->p_libvlc;
362 for ( i = 0; i < p_bridge->i_es_num; i++ )
363 free( p_bridge->pp_es[i] );
364 free( p_bridge->pp_es );
366 var_Destroy( p_libvlc, "mosaic-struct" );
369 vlc_mutex_unlock( p_sys->p_lock );
371 if ( p_sys->i_height || p_sys->i_width )
373 image_HandlerDelete( p_sys->p_image );
376 p_sys->b_inited = VLC_FALSE;
381 /*****************************************************************************
382 * PushPicture : push a picture in the mosaic-struct structure
383 *****************************************************************************/
384 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
386 sout_stream_sys_t *p_sys = p_stream->p_sys;
387 bridged_es_t *p_es = p_sys->p_es;
389 vlc_mutex_lock( p_sys->p_lock );
391 *p_es->pp_last = p_picture;
392 p_picture->p_next = NULL;
393 p_es->pp_last = &p_picture->p_next;
395 vlc_mutex_unlock( p_sys->p_lock );
398 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
401 sout_stream_sys_t *p_sys = p_stream->p_sys;
404 if ( (sout_stream_sys_t *)id != p_sys )
406 block_ChainRelease( p_buffer );
410 while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
413 picture_t *p_new_pic;
415 if ( p_sys->i_height || p_sys->i_width )
417 video_format_t fmt_out = {0}, fmt_in = {0};
418 fmt_in = p_sys->p_decoder->fmt_out.video;
420 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
422 if ( !p_sys->i_height )
424 fmt_out.i_width = p_sys->i_width;
425 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
426 * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
429 else if ( !p_sys->i_width )
431 fmt_out.i_height = p_sys->i_height;
432 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
433 * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
438 fmt_out.i_width = p_sys->i_width;
439 fmt_out.i_height = p_sys->i_height;
441 fmt_out.i_visible_width = fmt_out.i_width;
442 fmt_out.i_visible_height = fmt_out.i_height;
444 p_new_pic = image_Convert( p_sys->p_image,
445 p_pic, &fmt_in, &fmt_out );
446 if ( p_new_pic == NULL )
448 msg_Err( p_stream, "image conversion failed" );
454 p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
455 vout_AllocatePicture( p_stream, p_new_pic, p_pic->format.i_chroma,
456 p_pic->format.i_width, p_pic->format.i_height,
457 p_sys->p_decoder->fmt_out.video.i_aspect );
459 vout_CopyPicture( p_stream, p_new_pic, p_pic );
462 p_new_pic->i_refcount = 1;
463 p_new_pic->i_status = DESTROYED_PICTURE;
464 p_new_pic->i_type = DIRECT_PICTURE;
465 p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
466 p_new_pic->pf_release = ReleasePicture;
467 p_new_pic->date = p_pic->date;
469 p_pic->pf_release( p_pic );
470 PushPicture( p_stream, p_new_pic );
478 vlc_object_t *p_owner;
481 static void video_release_buffer( picture_t *p_pic )
483 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
485 video_del_buffer( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
487 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
490 static picture_t *video_new_buffer( decoder_t *p_dec )
492 picture_t **pp_ring = p_dec->p_owner->pp_pics;
496 /* Find an empty space in the picture ring buffer */
497 for( i = 0; i < PICTURE_RING_SIZE; i++ )
499 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
501 pp_ring[i]->i_status = RESERVED_PICTURE;
505 for( i = 0; i < PICTURE_RING_SIZE; i++ )
507 if( pp_ring[i] == 0 ) break;
510 if( i == PICTURE_RING_SIZE )
512 msg_Err( p_dec, "decoder/filter is leaking pictures, "
513 "resetting its ring buffer" );
515 for( i = 0; i < PICTURE_RING_SIZE; i++ )
517 pp_ring[i]->pf_release( pp_ring[i] );
523 p_pic = malloc( sizeof(picture_t) );
524 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
525 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
526 p_dec->fmt_out.video.i_chroma,
527 p_dec->fmt_out.video.i_width,
528 p_dec->fmt_out.video.i_height,
529 p_dec->fmt_out.video.i_aspect );
531 if( !p_pic->i_planes )
537 p_pic->pf_release = video_release_buffer;
538 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
539 p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
540 p_pic->i_status = RESERVED_PICTURE;
547 static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
549 p_pic->i_refcount = 0;
550 p_pic->i_status = DESTROYED_PICTURE;
553 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
558 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
560 video_release_buffer( p_pic );