1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: es_out.c,v 1.11 2003/12/24 09:46:08 gbazin 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 &&
170 p_sys->p_es_audio->p_es->fmt.i_priority >=
171 es->p_es->fmt.i_priority )
175 i_wanted = p_sys->i_audio_last >= 0 ?
176 p_sys->i_audio_last : es->i_channel;
178 else if( i_cat == SPU_ES )
180 if( p_sys->p_es_sub &&
181 p_sys->p_es_sub->p_es->fmt.i_priority >=
182 es->p_es->fmt.i_priority )
186 i_wanted = p_sys->i_sub_last;
188 else if( i_cat == VIDEO_ES )
190 i_wanted = es->i_channel;
193 if( i_wanted == es->i_channel && es->p_es->p_dec == NULL )
195 input_SelectES( p_input, es->p_es );
199 /* FIXME TODO handle priority here */
200 if( es->p_es->p_dec )
202 if( i_cat == AUDIO_ES )
204 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
205 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
207 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
209 p_sys->p_es_audio = es;
211 else if( i_cat == SPU_ES )
213 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
214 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
216 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
218 p_sys->p_es_sub = es;
220 else if( i_cat == VIDEO_ES )
222 p_sys->p_es_video = es;
227 /*****************************************************************************
229 *****************************************************************************/
230 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
232 es_out_sys_t *p_sys = out->p_sys;
233 input_thread_t *p_input = p_sys->p_input;
234 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
235 pgrm_descriptor_t *p_prgm = NULL;
236 char psz_cat[sizeof( "Stream " ) + 10];
237 input_info_category_t *p_cat;
239 vlc_mutex_lock( &p_input->stream.stream_lock );
240 if( fmt->i_group >= 0 )
243 p_prgm = input_FindProgram( p_input, fmt->i_group );
248 p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
250 /* Select the first by default */
251 if( p_input->stream.p_selected_program == NULL )
253 p_input->stream.p_selected_program = p_prgm;
258 es->p_es = input_AddES( p_input,
262 fmt->psz_language, 0 );
263 es->p_es->i_stream_id = out->p_sys->i_id;
264 es->p_es->i_fourcc = fmt->i_codec;
271 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
273 p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
274 p_wf->nChannels = fmt->audio.i_channels;
275 p_wf->nSamplesPerSec = fmt->audio.i_rate;
276 p_wf->nAvgBytesPerSec = fmt->i_bitrate / 8;
277 p_wf->nBlockAlign = fmt->audio.i_blockalign;
278 p_wf->wBitsPerSample = fmt->audio.i_bitspersample;
279 p_wf->cbSize = fmt->i_extra;
280 if( fmt->i_extra > 0 )
282 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
284 es->p_es->p_waveformatex = p_wf;
286 es->i_channel = p_sys->i_audio;
291 BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
293 p_bih->biSize = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
294 p_bih->biWidth = fmt->video.i_width;
295 p_bih->biHeight = fmt->video.i_height;
297 p_bih->biBitCount = 24;
298 p_bih->biCompression = fmt->i_codec;
299 p_bih->biSizeImage = fmt->video.i_width *
301 p_bih->biXPelsPerMeter = 0;
302 p_bih->biYPelsPerMeter = 0;
303 p_bih->biClrUsed = 0;
304 p_bih->biClrImportant = 0;
306 if( fmt->i_extra > 0 )
308 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
310 es->p_es->p_bitmapinfoheader = p_bih;
312 es->i_channel = p_sys->i_video;
317 subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
318 memset( p_sub, 0, sizeof( subtitle_data_t ) );
319 if( fmt->i_extra > 0 )
321 p_sub->psz_header = malloc( fmt->i_extra + 1 );
322 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
323 /* just to be sure */
324 ((uint8_t*)fmt->p_extra)[fmt->i_extra] = '\0';
326 /* FIXME beuuuuuurk */
327 es->p_es->p_demux_data = p_sub;
329 es->i_channel = p_sys->i_sub;
338 sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
339 if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) )
341 /* Add information */
345 if( fmt->psz_description )
347 input_AddInfo( p_cat, _("Description"), "%s",
348 fmt->psz_description );
350 input_AddInfo( p_cat, _("Codec"), "%.4s",
351 (char*)&fmt->i_codec );
352 input_AddInfo( p_cat, _("Type"), _("Audio") );
353 if( fmt->audio.i_channels > 0 )
355 input_AddInfo( p_cat, _("Channels"), "%d",
356 fmt->audio.i_channels );
358 if( fmt->psz_language )
360 input_AddInfo( p_cat, _("Language"), "%s",
363 if( fmt->audio.i_rate > 0 )
365 input_AddInfo( p_cat, _("Sample rate"), _("%d Hz"),
368 if( fmt->i_bitrate > 0 )
370 input_AddInfo( p_cat, _("Bitrate"), _("%d bps"),
373 if( fmt->audio.i_bitspersample )
375 input_AddInfo( p_cat, _("Bits per sample"), "%d",
376 fmt->audio.i_bitspersample );
380 if( fmt->psz_description )
382 input_AddInfo( p_cat, _("Description"), "%s",
383 fmt->psz_description );
385 input_AddInfo( p_cat, _("Type"), _("Video") );
386 input_AddInfo( p_cat, _("Codec"), "%.4s",
387 (char*)&fmt->i_codec );
388 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
390 input_AddInfo( p_cat, _("Resolution"), "%dx%d",
391 fmt->video.i_width, fmt->video.i_height );
393 if( fmt->video.i_visible_width > 0 &&
394 fmt->video.i_visible_height > 0 )
396 input_AddInfo( p_cat, _("Display resolution"), "%dx%d",
397 fmt->video.i_visible_width,
398 fmt->video.i_visible_height);
402 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
403 input_AddInfo( p_cat, _("Codec"), "%.4s",
404 (char*)&fmt->i_codec );
414 * XXX change that when we do group too */
417 EsOutSelect( out, es, VLC_FALSE );
420 vlc_mutex_unlock( &p_input->stream.stream_lock );
422 es->p_es->fmt = *fmt;
424 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
425 p_sys->i_id++; /* always incremented */
442 /*****************************************************************************
444 *****************************************************************************/
445 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
447 es_out_sys_t *p_sys = out->p_sys;
449 if( p_sys->b_pcr_set && p_sys->p_input->stream.p_selected_program )
451 input_thread_t *p_input = p_sys->p_input;
453 if( p_block->i_dts > 0 )
456 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
457 p_block->i_dts * 9 / 100 );
459 if( p_block->i_pts > 0 )
462 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
463 p_block->i_pts * 9 / 100 );
467 vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
468 p_block->i_rate = out->p_sys->p_input->stream.control.i_rate;
469 if( es->p_es->p_dec &&
470 (es->p_es->i_cat!=AUDIO_ES || !p_sys->p_input->stream.control.b_mute) )
472 input_DecodeBlock( es->p_es->p_dec, p_block );
476 block_Release( p_block );
478 vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
483 /*****************************************************************************
485 *****************************************************************************/
486 static void EsOutDel( es_out_t *out, es_out_id_t *es )
488 es_out_sys_t *p_sys = out->p_sys;
490 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
492 switch( es->p_es->i_cat )
505 /* We don't try to reselect */
506 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
507 if( es->p_es->p_dec )
509 input_UnselectES( p_sys->p_input, es->p_es );
512 if( es->p_es->p_waveformatex )
514 free( es->p_es->p_waveformatex );
515 es->p_es->p_waveformatex = NULL;
517 if( es->p_es->p_bitmapinfoheader )
519 free( es->p_es->p_bitmapinfoheader );
520 es->p_es->p_bitmapinfoheader = NULL;
522 input_DelES( p_sys->p_input, es->p_es );
524 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
525 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
526 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
528 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
533 /*****************************************************************************
535 *****************************************************************************/
536 static int EsOutControl( es_out_t *out, int i_query, va_list args )
538 es_out_sys_t *p_sys = out->p_sys;
548 case ES_OUT_SET_ES_STATE:
549 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
550 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
551 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
552 if( b && es->p_es->p_dec == NULL )
554 input_SelectES( p_sys->p_input, es->p_es );
555 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
556 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
558 else if( !b && es->p_es->p_dec )
560 input_UnselectES( p_sys->p_input, es->p_es );
561 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
564 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
567 case ES_OUT_GET_ES_STATE:
568 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
569 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
571 *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
574 case ES_OUT_SET_ACTIVE:
576 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
582 val.b_bool = VLC_TRUE;
583 var_Set( p_sys->p_input, "intf-change", val );
588 case ES_OUT_GET_ACTIVE:
589 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
590 *pb = p_sys->b_active;
593 case ES_OUT_SET_MODE:
594 i = (int) va_arg( args, int );
595 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
596 i == ES_OUT_MODE_AUTO )
602 /* Reapply policy mode */
603 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
604 for( i = 0; i < p_sys->i_es; i++ )
606 if( p_sys->es[i]->p_es->p_dec )
608 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
611 for( i = 0; i < p_sys->i_es; i++ )
613 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
615 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
617 val.b_bool = VLC_TRUE;
618 var_Set( p_sys->p_input, "intf-change", val );
624 case ES_OUT_GET_MODE:
625 pi = (int*) va_arg( args, int* );
630 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
633 for( i = 0; i < p_sys->i_es; i++ )
635 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
636 if( p_sys->es[i]->p_es->p_dec )
638 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
640 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
645 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
646 EsOutSelect( out, es, VLC_TRUE );
647 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
653 pgrm_descriptor_t *p_prgm = NULL;
655 i_group = (int)va_arg( args, int );
656 i_pcr = (int64_t)va_arg( args, int64_t );
659 if( ( p_prgm = input_FindProgram( p_sys->p_input, i_group ) ) )
661 input_ClockManageRef( p_sys->p_input, p_prgm, i_pcr );
663 p_sys->b_pcr_set = VLC_TRUE;
667 case ES_OUT_RESET_PCR:
668 /* FIXME do it for all program */
669 if( p_sys->p_input->stream.p_selected_program )
671 p_sys->p_input->stream.p_selected_program->i_synchro_state =
674 p_sys->b_pcr_set = VLC_TRUE;
678 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );