1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
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>
33 #include "vlc_playlist.h"
37 /*****************************************************************************
39 *****************************************************************************/
43 es_descriptor_t *p_es;
48 input_thread_t *p_input;
70 es_out_id_t *p_es_audio;
71 es_out_id_t *p_es_video;
72 es_out_id_t *p_es_sub;
75 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
76 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
77 static void EsOutDel ( es_out_t *, es_out_id_t * );
78 static int EsOutControl( es_out_t *, int i_query, va_list );
80 static char *LanguageGetName( const char *psz_code );
83 * Create a new es_out structure
85 * \param p_input The related input thread
86 * \return the new es_out_t
88 es_out_t *input_EsOutNew( input_thread_t *p_input )
90 es_out_t *out = malloc( sizeof( es_out_t ) );
91 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
94 out->pf_add = EsOutAdd;
95 out->pf_send = EsOutSend;
96 out->pf_del = EsOutDel;
97 out->pf_control = EsOutControl;
100 p_sys->p_input = p_input;
102 p_sys->b_active = VLC_FALSE;
103 p_sys->i_mode = ES_OUT_MODE_AUTO;
114 var_Get( p_input, "audio-channel", &val );
115 p_sys->i_audio_last = val.i_int;
117 var_Get( p_input, "spu-channel", &val );
118 p_sys->i_sub_last = val.i_int;
120 p_sys->p_es_audio = NULL;
121 p_sys->p_es_video = NULL;
122 p_sys->p_es_sub = NULL;
128 * Deletes an es_out structure
130 * \param out the es_out structure to destroy
133 void input_EsOutDelete( es_out_t *out )
135 es_out_sys_t *p_sys = out->p_sys;
138 for( i = 0; i < p_sys->i_es; i++ )
140 free( p_sys->es[i] );
153 * \param out the es_out
155 * \return a program descriptor for the new program
157 static pgrm_descriptor_t *EsOutAddProgram( es_out_t *out, int i_group )
159 input_thread_t *p_input = out->p_sys->p_input;
160 pgrm_descriptor_t *p_pgrm;
161 es_descriptor_t *p_pmt;
163 /* FIXME we should use a object variable but a lot of place in src/input
164 * have to be changed */
165 int i_select = config_GetInt( p_input, "program" );
168 p_pgrm = input_AddProgram( p_input, i_group, 0 );
170 /* XXX welcome to kludge, add a dummy es, if you want to understand
171 * why have a look at input_SetProgram. Basicaly, it assume the first
172 * es to be the PMT, how that is stupid, nevertheless it is needed for
173 * the old ts demuxer */
174 p_pmt = input_AddES( p_input, p_pgrm, 0, UNKNOWN_ES, NULL, 0 );
175 p_pmt->i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
177 /* Select i_select or the first by default */
178 if( p_input->stream.p_selected_program == NULL &&
179 ( i_select <= 0 || i_select == i_group ) )
181 p_input->stream.p_selected_program = p_pgrm;
188 * Select an ES given the current mode
189 * XXX: you need to take a the lock before (stream.stream_lock)
191 * \param out The es_out structure
192 * \param es es_out_id structure
196 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
198 es_out_sys_t *p_sys = out->p_sys;
199 input_thread_t *p_input = p_sys->p_input;
201 int i_cat = es->p_es->i_cat;
203 if( !p_sys->b_active ||
204 ( !b_force && es->p_es->fmt.i_priority < 0 ) )
209 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
211 if( !es->p_es->p_dec )
213 input_SelectES( p_input, es->p_es );
216 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
220 if( es->p_es->p_pgrm != NULL &&
221 es->p_es->p_pgrm != p_input->stream.p_selected_program )
226 if( i_cat == AUDIO_ES )
228 if( p_sys->p_es_audio &&
229 p_sys->p_es_audio->p_es->fmt.i_priority >=
230 es->p_es->fmt.i_priority )
234 i_wanted = p_sys->i_audio_last >= 0 ?
235 p_sys->i_audio_last : es->i_channel;
237 else if( i_cat == SPU_ES )
239 if( p_sys->p_es_sub &&
240 p_sys->p_es_sub->p_es->fmt.i_priority >=
241 es->p_es->fmt.i_priority )
245 i_wanted = p_sys->i_sub_last;
247 else if( i_cat == VIDEO_ES )
249 i_wanted = es->i_channel;
252 if( i_wanted == es->i_channel && es->p_es->p_dec == NULL )
254 input_SelectES( p_input, es->p_es );
258 /* FIXME TODO handle priority here */
259 if( es->p_es->p_dec )
261 if( i_cat == AUDIO_ES )
263 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
264 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
266 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
268 p_sys->p_es_audio = es;
270 else if( i_cat == SPU_ES )
272 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
273 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
275 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
277 p_sys->p_es_sub = es;
279 else if( i_cat == VIDEO_ES )
281 p_sys->p_es_video = es;
289 * \param out the es_out to add
290 * \param fmt the es_format of the es_out
291 * \return an es_out id
293 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
295 es_out_sys_t *p_sys = out->p_sys;
296 input_thread_t *p_input = p_sys->p_input;
297 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
298 pgrm_descriptor_t *p_pgrm = NULL;
299 char psz_cat[sizeof( _("Stream ") ) + 10];
300 char *psz_description;
302 vlc_mutex_lock( &p_input->stream.stream_lock );
303 if( fmt->i_group >= 0 )
306 p_pgrm = input_FindProgram( p_input, fmt->i_group );
311 p_pgrm = EsOutAddProgram( out, fmt->i_group );
316 fmt->i_id = out->p_sys->i_id - 1;
319 psz_description = LanguageGetName( fmt->psz_language );
320 es->p_es = input_AddES( p_input, p_pgrm, fmt->i_id + 1,
321 fmt->i_cat, psz_description, 0 );
322 es->p_es->i_stream_id = fmt->i_id;
323 es->p_es->i_fourcc = fmt->i_codec;
328 es->i_channel = p_sys->i_audio;
332 es->i_channel = p_sys->i_video;
336 es->i_channel = p_sys->i_sub;
344 /* Add stream info */
345 vlc_mutex_unlock( &p_input->stream.stream_lock );
346 sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
348 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
349 "%.4s", (char*)&fmt->i_codec );
351 if( *psz_description )
352 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
353 "%s", psz_description );
355 if( fmt->psz_description && *fmt->psz_description )
356 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Description"),
357 "%s", fmt->psz_description );
359 /* Add information */
363 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
364 _("Type"), _("Audio") );
366 if( fmt->audio.i_channels > 0 )
367 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
368 "%d", fmt->audio.i_channels );
370 if( fmt->audio.i_rate > 0 )
371 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
372 _("%d Hz"), fmt->audio.i_rate );
374 if( fmt->audio.i_bitspersample > 0 )
375 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
376 _("Bits per sample"), "%d",
377 fmt->audio.i_bitspersample );
379 if( fmt->i_bitrate > 0 )
380 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
381 _("%d bps"), fmt->i_bitrate );
385 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
386 _("Type"), _("Video") );
388 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
389 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
390 _("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 )
395 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
396 _("Display resolution"), "%dx%d",
397 fmt->video.i_visible_width,
398 fmt->video.i_visible_height);
402 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
403 _("Type"), _("Subtitle") );
410 vlc_mutex_lock( &p_input->stream.stream_lock );
411 free( psz_description );
413 es_format_Copy( &es->p_es->fmt, fmt );
416 * XXX change that when we do group too */
419 EsOutSelect( out, es, VLC_FALSE );
421 vlc_mutex_unlock( &p_input->stream.stream_lock );
423 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
424 p_sys->i_id++; /* always incremented */
442 * Send a block for the given es_out
444 * \param out the es_out to send from
445 * \param es the es_out_id
446 * \param p_block the data block to send
448 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
450 es_out_sys_t *p_sys = out->p_sys;
451 pgrm_descriptor_t *p_pgrm = es->p_es->p_pgrm;
452 input_thread_t *p_input = p_sys->p_input;
456 p_pgrm = p_sys->p_input->stream.p_selected_program;
459 /* +11 -> avoid null value with non null dts/pts */
460 if( p_block->i_dts > 0 && p_pgrm )
463 input_ClockGetTS( p_input, p_pgrm, ( p_block->i_dts + 11 ) * 9 / 100 );
465 if( p_block->i_pts > 0 && p_pgrm )
468 input_ClockGetTS( p_input, p_pgrm, ( p_block->i_pts + 11 )* 9 / 100 );
471 vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
472 p_block->i_rate = out->p_sys->p_input->stream.control.i_rate;
473 if( es->p_es->p_dec &&
474 (es->p_es->i_cat!=AUDIO_ES || !p_sys->p_input->stream.control.b_mute) )
476 vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
477 input_DecodeBlock( es->p_es->p_dec, p_block );
481 vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
482 block_Release( p_block );
488 /*****************************************************************************
490 *****************************************************************************/
491 static void EsOutDel( es_out_t *out, es_out_id_t *es )
493 es_out_sys_t *p_sys = out->p_sys;
495 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
497 switch( es->p_es->i_cat )
510 /* We don't try to reselect */
511 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
512 if( es->p_es->p_dec )
514 input_UnselectES( p_sys->p_input, es->p_es );
517 if( es->p_es->p_waveformatex )
519 free( es->p_es->p_waveformatex );
520 es->p_es->p_waveformatex = NULL;
522 if( es->p_es->p_bitmapinfoheader )
524 free( es->p_es->p_bitmapinfoheader );
525 es->p_es->p_bitmapinfoheader = NULL;
527 input_DelES( p_sys->p_input, es->p_es );
529 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
530 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
531 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
533 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
539 * Control query handler
541 * \param out the es_out to control
542 * \param i_query A es_out query as defined in include/ninput.h
543 * \param args a variable list of arguments for the query
544 * \return VLC_SUCCESS or an error code
546 static int EsOutControl( es_out_t *out, int i_query, va_list args )
548 es_out_sys_t *p_sys = out->p_sys;
556 case ES_OUT_SET_ES_STATE:
557 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
558 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
559 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
560 if( b && es->p_es->p_dec == NULL )
562 input_SelectES( p_sys->p_input, es->p_es );
563 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
564 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
566 else if( !b && es->p_es->p_dec )
568 input_UnselectES( p_sys->p_input, es->p_es );
569 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
572 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
575 case ES_OUT_GET_ES_STATE:
576 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
577 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
579 *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
582 case ES_OUT_SET_ACTIVE:
584 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
590 val.b_bool = VLC_TRUE;
591 var_Set( p_sys->p_input, "intf-change", val );
596 case ES_OUT_GET_ACTIVE:
597 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
598 *pb = p_sys->b_active;
601 case ES_OUT_SET_MODE:
602 i = (int) va_arg( args, int );
603 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
604 i == ES_OUT_MODE_AUTO )
610 /* Reapply policy mode */
611 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
612 for( i = 0; i < p_sys->i_es; i++ )
614 if( p_sys->es[i]->p_es->p_dec )
616 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
619 for( i = 0; i < p_sys->i_es; i++ )
621 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
623 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
625 val.b_bool = VLC_TRUE;
626 var_Set( p_sys->p_input, "intf-change", val );
632 case ES_OUT_GET_MODE:
633 pi = (int*) va_arg( args, int* );
638 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
641 for( i = 0; i < p_sys->i_es; i++ )
643 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
644 if( p_sys->es[i]->p_es->p_dec )
646 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
648 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
653 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
654 EsOutSelect( out, es, VLC_TRUE );
655 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
660 case ES_OUT_SET_GROUP_PCR:
662 pgrm_descriptor_t *p_pgrm = NULL;
665 if( i_query == ES_OUT_SET_PCR )
667 p_pgrm = p_sys->p_input->stream.p_selected_program;
671 int i_group = (int)va_arg( args, int );
672 p_pgrm = input_FindProgram( p_sys->p_input, i_group );
675 /* we create the requested program */
676 p_pgrm = EsOutAddProgram( out, i_group );
679 i_pcr = (int64_t)va_arg( args, int64_t );
683 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
684 input_ClockManageRef( p_sys->p_input, p_pgrm, (i_pcr + 11 ) * 9 / 100);
689 case ES_OUT_RESET_PCR:
690 for( i = 0; i < p_sys->p_input->stream.i_pgrm_number; i++ )
692 p_sys->p_input->stream.pp_programs[i]->i_synchro_state =
694 p_sys->p_input->stream.pp_programs[i]->last_pts = 0;
699 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
704 /****************************************************************************
705 * LanguageGetName: try to expend iso639 into plain name
706 ****************************************************************************/
707 static char *LanguageGetName( const char *psz_code )
709 const iso639_lang_t *pl;
711 if( psz_code == NULL )
716 if( strlen( psz_code ) == 2 )
718 pl = GetLang_1( psz_code );
720 else if( strlen( psz_code ) == 3 )
722 pl = GetLang_2B( psz_code );
723 if( !strcmp( pl->psz_iso639_1, "??" ) )
725 pl = GetLang_2T( psz_code );
730 return strdup( psz_code );
733 if( !strcmp( pl->psz_iso639_1, "??" ) )
735 return strdup( psz_code );
739 if( *pl->psz_native_name )
741 return strdup( pl->psz_native_name );
743 return strdup( pl->psz_eng_name );