1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: es_out.c,v 1.5 2003/11/30 17:29:56 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
31 #include <vlc/decoder.h>
35 /*****************************************************************************
37 *****************************************************************************/
41 es_descriptor_t *p_es;
46 input_thread_t *p_input;
69 es_out_id_t *p_es_audio;
70 es_out_id_t *p_es_video;
71 es_out_id_t *p_es_sub;
74 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
75 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
76 static void EsOutDel ( es_out_t *, es_out_id_t * );
77 static int EsOutControl( es_out_t *, int i_query, va_list );
80 /*****************************************************************************
82 *****************************************************************************/
83 es_out_t *input_EsOutNew( input_thread_t *p_input )
85 es_out_t *out = malloc( sizeof( es_out_t ) );
86 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
89 out->pf_add = EsOutAdd;
90 out->pf_send = EsOutSend;
91 out->pf_del = EsOutDel;
92 out->pf_control = EsOutControl;
95 p_sys->p_input = p_input;
96 p_sys->b_pcr_set = VLC_FALSE;
98 p_sys->b_active = VLC_FALSE;
99 p_sys->i_mode = ES_OUT_MODE_AUTO;
110 var_Get( p_input, "audio-channel", &val );
111 p_sys->i_audio_last = val.i_int;
113 var_Get( p_input, "spu-channel", &val );
114 p_sys->i_sub_last = val.i_int;
116 p_sys->p_es_audio = NULL;
117 p_sys->p_es_video = NULL;
118 p_sys->p_es_sub = NULL;
123 /*****************************************************************************
125 *****************************************************************************/
126 void input_EsOutDelete( es_out_t *out )
128 es_out_sys_t *p_sys = out->p_sys;
131 for( i = 0; i < p_sys->i_es; i++ )
133 free( p_sys->es[i] );
143 /*****************************************************************************
144 * EsOutSelect: Select an ES given the current mode
145 * XXX: you need to take a the lock before (stream.stream_lock)
146 *****************************************************************************/
147 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
149 es_out_sys_t *p_sys = out->p_sys;
150 input_thread_t *p_input = p_sys->p_input;
152 int i_cat = es->p_es->i_cat;
154 if( !p_sys->b_active || ( !b_force && es->p_es->fmt.i_priority < 0 ) )
159 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
161 input_SelectES( p_input, es->p_es );
163 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
167 if( i_cat == AUDIO_ES )
169 if( p_sys->p_es_audio && p_sys->p_es_audio->p_es->fmt.i_priority >= es->p_es->fmt.i_priority )
173 i_wanted = p_sys->i_audio_last >= 0 ? p_sys->i_audio_last : es->i_channel;
175 else if( i_cat == SPU_ES )
177 if( p_sys->p_es_sub && p_sys->p_es_sub->p_es->fmt.i_priority >= es->p_es->fmt.i_priority )
181 i_wanted = p_sys->i_sub_last;
183 else if( i_cat == VIDEO_ES )
185 i_wanted = es->i_channel;
188 if( i_wanted == es->i_channel && es->p_es->p_dec == NULL )
190 input_SelectES( p_input, es->p_es );
194 /* FIXME TODO handle priority here */
195 if( es->p_es->p_dec )
197 if( i_cat == AUDIO_ES )
199 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
200 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
202 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
204 p_sys->p_es_audio = es;
206 else if( i_cat == SPU_ES )
208 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
209 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
211 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
213 p_sys->p_es_sub = es;
215 else if( i_cat == VIDEO_ES )
217 p_sys->p_es_video = es;
222 /*****************************************************************************
224 *****************************************************************************/
225 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
227 es_out_sys_t *p_sys = out->p_sys;
228 input_thread_t *p_input = p_sys->p_input;
229 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
230 pgrm_descriptor_t *p_prgm = NULL;
231 char psz_cat[strlen( "Stream " ) + 10];
232 input_info_category_t *p_cat;
234 vlc_mutex_lock( &p_input->stream.stream_lock );
235 if( fmt->i_group >= 0 )
238 p_prgm = input_FindProgram( p_input, fmt->i_group );
243 p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
245 /* Select the first by default */
246 if( p_input->stream.p_selected_program == NULL )
248 p_input->stream.p_selected_program = p_prgm;
253 es->p_es = input_AddES( p_input,
257 fmt->psz_language, 0 );
258 es->p_es->i_stream_id = out->p_sys->i_id;
259 es->p_es->i_fourcc = fmt->i_codec;
266 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
268 p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
269 p_wf->nChannels = fmt->audio.i_channels;
270 p_wf->nSamplesPerSec = fmt->audio.i_rate;
271 p_wf->nAvgBytesPerSec = fmt->i_bitrate / 8;
272 p_wf->nBlockAlign = fmt->audio.i_blockalign;
273 p_wf->wBitsPerSample = fmt->audio.i_bitspersample;
274 p_wf->cbSize = fmt->i_extra;
275 if( fmt->i_extra > 0 )
277 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
279 es->p_es->p_waveformatex = p_wf;
281 es->i_channel = p_sys->i_audio;
286 BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
288 p_bih->biSize = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
289 p_bih->biWidth = fmt->video.i_width;
290 p_bih->biHeight = fmt->video.i_height;
292 p_bih->biBitCount = 24;
293 p_bih->biCompression = fmt->i_codec;
294 p_bih->biSizeImage = fmt->video.i_width *
296 p_bih->biXPelsPerMeter = 0;
297 p_bih->biYPelsPerMeter = 0;
298 p_bih->biClrUsed = 0;
299 p_bih->biClrImportant = 0;
301 if( fmt->i_extra > 0 )
303 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
305 es->p_es->p_bitmapinfoheader = p_bih;
307 es->i_channel = p_sys->i_video;
312 subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
313 memset( p_sub, 0, sizeof( subtitle_data_t ) );
314 if( fmt->i_extra > 0 )
316 p_sub->psz_header = malloc( fmt->i_extra + 1 );
317 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
318 /* just to be sure */
319 ((uint8_t*)fmt->p_extra)[fmt->i_extra] = '\0';
321 /* FIXME beuuuuuurk */
322 es->p_es->p_demux_data = p_sub;
324 es->i_channel = p_sys->i_sub;
333 sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
334 if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) )
336 /* Add information */
340 input_AddInfo( p_cat, _("Type"), _("Audio") );
341 input_AddInfo( p_cat, _("Codec"), "%.4s",
342 (char*)&fmt->i_codec );
343 if( fmt->audio.i_channels > 0 )
345 input_AddInfo( p_cat, _("Channels"), "%d",
346 fmt->audio.i_channels );
348 if( fmt->audio.i_rate > 0 )
350 input_AddInfo( p_cat, _("Sample Rate"), "%d",
353 if( fmt->i_bitrate > 0 )
355 input_AddInfo( p_cat, _("Bitrate"), "%d",
358 if( fmt->audio.i_bitspersample )
360 input_AddInfo( p_cat, _("Bits Per Sample"), "%d",
361 fmt->audio.i_bitspersample );
365 input_AddInfo( p_cat, _("Type"), _("Video") );
366 input_AddInfo( p_cat, _("Codec"), "%.4s",
367 (char*)&fmt->i_codec );
368 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
370 input_AddInfo( p_cat, _("Resolution"), "%dx%d",
371 fmt->video.i_width, fmt->video.i_height );
373 if( fmt->video.i_visible_width > 0 &&
374 fmt->video.i_visible_height > 0 )
376 input_AddInfo( p_cat, _("Display Resolution"), "%dx%d",
377 fmt->video.i_visible_width,
378 fmt->video.i_visible_height);
382 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
383 input_AddInfo( p_cat, _("Codec"), "%.4s",
384 (char*)&fmt->i_codec );
394 * XXX change that when we do group too */
397 EsOutSelect( out, es, VLC_FALSE );
400 vlc_mutex_unlock( &p_input->stream.stream_lock );
402 es->p_es->fmt = *fmt;
404 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
405 p_sys->i_id++; /* always incremented */
422 /*****************************************************************************
424 *****************************************************************************/
425 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
427 es_out_sys_t *p_sys = out->p_sys;
429 if( p_sys->b_pcr_set && p_sys->p_input->stream.p_selected_program )
431 input_thread_t *p_input = p_sys->p_input;
433 if( p_block->i_dts > 0 )
435 p_block->i_dts = input_ClockGetTS( p_input,
436 p_input->stream.p_selected_program,
437 p_block->i_dts * 9 / 100 );
439 if( p_block->i_pts > 0 )
441 p_block->i_pts = input_ClockGetTS( p_input,
442 p_input->stream.p_selected_program,
443 p_block->i_pts * 9 / 100 );
446 vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
447 if( es->p_es->p_dec )
449 input_DecodeBlock( es->p_es->p_dec, p_block );
453 block_Release( p_block );
455 vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
459 /*****************************************************************************
461 *****************************************************************************/
462 static void EsOutDel( es_out_t *out, es_out_id_t *es )
464 es_out_sys_t *p_sys = out->p_sys;
466 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
468 switch( es->p_es->i_cat )
481 /* We don't try to reselect */
482 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
483 if( es->p_es->p_dec )
485 input_UnselectES( p_sys->p_input, es->p_es );
488 if( es->p_es->p_waveformatex )
490 free( es->p_es->p_waveformatex );
491 es->p_es->p_waveformatex = NULL;
493 if( es->p_es->p_bitmapinfoheader )
495 free( es->p_es->p_bitmapinfoheader );
496 es->p_es->p_bitmapinfoheader = NULL;
498 input_DelES( p_sys->p_input, es->p_es );
500 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
505 /*****************************************************************************
507 *****************************************************************************/
508 static int EsOutControl( es_out_t *out, int i_query, va_list args )
510 es_out_sys_t *p_sys = out->p_sys;
520 case ES_OUT_SET_ES_STATE:
521 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
522 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
523 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
524 if( b && es->p_es->p_dec == NULL )
526 input_SelectES( p_sys->p_input, es->p_es );
527 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
528 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
530 else if( !b && es->p_es->p_dec )
532 input_UnselectES( p_sys->p_input, es->p_es );
533 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
536 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
539 case ES_OUT_GET_ES_STATE:
540 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
541 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
543 *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
546 case ES_OUT_SET_ACTIVE:
548 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
554 val.b_bool = VLC_TRUE;
555 var_Set( p_sys->p_input, "intf-change", val );
560 case ES_OUT_GET_ACTIVE:
561 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
562 *pb = p_sys->b_active;
565 case ES_OUT_SET_MODE:
566 i = (int) va_arg( args, int );
567 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL || i == ES_OUT_MODE_AUTO )
573 /* Reapply policy mode */
574 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
575 for( i = 0; i < p_sys->i_es; i++ )
577 if( p_sys->es[i]->p_es->p_dec )
579 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
582 for( i = 0; i < p_sys->i_es; i++ )
584 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
586 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
588 val.b_bool = VLC_TRUE;
589 var_Set( p_sys->p_input, "intf-change", val );
595 case ES_OUT_GET_MODE:
596 pi = (int*) va_arg( args, int* );
601 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
604 for( i = 0; i < p_sys->i_es; i++ )
606 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
607 if( p_sys->es[i]->p_es->p_dec )
609 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
611 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
616 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
617 EsOutSelect( out, es, VLC_TRUE );
618 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
624 pgrm_descriptor_t *p_prgm = NULL;
626 i_group = (int)va_arg( args, int );
627 i_pcr = (int64_t)va_arg( args, int64_t );
630 if( ( p_prgm = input_FindProgram( p_sys->p_input, i_group ) ) )
632 input_ClockManageRef( p_sys->p_input, p_prgm, i_pcr );
634 p_sys->b_pcr_set = VLC_TRUE;
638 case ES_OUT_RESET_PCR:
639 /* FIXME do it for all program */
640 if( p_sys->p_input->stream.p_selected_program )
642 p_sys->p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_REINIT;
644 p_sys->b_pcr_set = VLC_TRUE;
648 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );