1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
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 *****************************************************************************/
31 #include <vlc/input.h>
32 #include <vlc/decoder.h>
34 #include "input_internal.h"
36 #include "vlc_playlist.h"
38 /* FIXME we should find a better way than including that */
39 #include "../misc/iso-639_def.h"
41 /*****************************************************************************
43 *****************************************************************************/
49 /* Number of es for this pgrm */
52 vlc_bool_t b_selected;
54 /* Clock for this program */
57 char *psz_now_playing;
65 es_out_pgrm_t *p_pgrm;
67 /* Signal a discontinuity in the timeline for every PID */
68 vlc_bool_t b_discontinuity;
71 int64_t i_preroll_end;
73 /* Channel in the track type */
77 char *psz_language_code;
83 input_thread_t *p_input;
88 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
89 es_out_pgrm_t *p_pgrm; /* Master program */
106 int i_audio_last, i_audio_id;
107 int i_sub_last, i_sub_id;
108 char **ppsz_audio_language;
109 char **ppsz_sub_language;
111 /* current main es */
112 es_out_id_t *p_es_audio;
113 es_out_id_t *p_es_video;
114 es_out_id_t *p_es_sub;
117 int64_t i_audio_delay;
121 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
122 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
123 static void EsOutDel ( es_out_t *, es_out_id_t * );
124 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
125 static int EsOutControl( es_out_t *, int i_query, va_list );
127 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
129 static void EsSelect( es_out_t *out, es_out_id_t *es );
130 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
131 static char *LanguageGetName( const char *psz_code );
132 static char *LanguageGetCode( const char *psz_lang );
133 static char **LanguageSplit( const char *psz_langs );
134 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
136 /*****************************************************************************
138 *****************************************************************************/
139 es_out_t *input_EsOutNew( input_thread_t *p_input )
141 es_out_t *out = malloc( sizeof( es_out_t ) );
142 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
146 out->pf_add = EsOutAdd;
147 out->pf_send = EsOutSend;
148 out->pf_del = EsOutDel;
149 out->pf_control = EsOutControl;
152 p_sys->p_input = p_input;
154 p_sys->b_active = VLC_FALSE;
155 p_sys->i_mode = ES_OUT_MODE_AUTO;
160 p_sys->p_pgrm = NULL;
171 var_Get( p_input, "audio-track", &val );
172 p_sys->i_audio_last = val.i_int;
174 var_Get( p_input, "sub-track", &val );
175 p_sys->i_sub_last = val.i_int;
177 var_Get( p_input, "audio-language", &val );
178 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
179 if( p_sys->ppsz_audio_language )
181 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
182 msg_Dbg( p_input, "select audio in language[%d] %s",
183 i, p_sys->ppsz_audio_language[i] );
185 if( val.psz_string ) free( val.psz_string );
187 var_Get( p_input, "sub-language", &val );
188 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
189 if( p_sys->ppsz_sub_language )
191 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
192 msg_Dbg( p_input, "select subtitle in language[%d] %s",
193 i, p_sys->ppsz_sub_language[i] );
195 if( val.psz_string ) free( val.psz_string );
197 var_Get( p_input, "audio-track-id", &val );
198 p_sys->i_audio_id = val.i_int;
200 var_Get( p_input, "sub-track-id", &val );
201 p_sys->i_sub_id = val.i_int;
203 p_sys->p_es_audio = NULL;
204 p_sys->p_es_video = NULL;
205 p_sys->p_es_sub = NULL;
207 p_sys->i_audio_delay= 0;
208 p_sys->i_spu_delay = 0;
213 /*****************************************************************************
215 *****************************************************************************/
216 void input_EsOutDelete( es_out_t *out )
218 es_out_sys_t *p_sys = out->p_sys;
221 for( i = 0; i < p_sys->i_es; i++ )
223 if( p_sys->es[i]->p_dec )
225 input_DecoderDelete( p_sys->es[i]->p_dec );
227 if( p_sys->es[i]->psz_language )
228 free( p_sys->es[i]->psz_language );
229 if( p_sys->es[i]->psz_language_code )
230 free( p_sys->es[i]->psz_language_code );
231 es_format_Clean( &p_sys->es[i]->fmt );
233 free( p_sys->es[i] );
235 if( p_sys->ppsz_audio_language )
237 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
238 free( p_sys->ppsz_audio_language[i] );
239 free( p_sys->ppsz_audio_language );
241 if( p_sys->ppsz_sub_language )
243 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
244 free( p_sys->ppsz_sub_language[i] );
245 free( p_sys->ppsz_sub_language );
251 for( i = 0; i < p_sys->i_pgrm; i++ )
253 if( p_sys->pgrm[i]->psz_now_playing )
254 free( p_sys->pgrm[i]->psz_now_playing );
255 free( p_sys->pgrm[i] );
264 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
269 /* Special HACK, -i_id is tha cat of the stream */
270 return (es_out_id_t*)((uint8_t*)NULL-i_id);
273 for( i = 0; i < out->p_sys->i_es; i++ )
275 if( out->p_sys->es[i]->i_id == i_id )
276 return out->p_sys->es[i];
281 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
283 es_out_sys_t *p_sys = out->p_sys;
286 for( i = 0; i < p_sys->i_es; i++ )
288 es_out_id_t *es = p_sys->es[i];
289 es->b_discontinuity = VLC_TRUE; /* signal discontinuity */
291 /* Send a dummy block to let decoder know that
292 * there is a discontinuity */
293 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
295 input_DecoderDiscontinuity( es->p_dec );
300 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
302 es_out_sys_t *p_sys = out->p_sys;
304 if( i_cat == AUDIO_ES )
305 p_sys->i_audio_delay = i_delay;
306 else if( i_cat == SPU_ES )
307 p_sys->i_spu_delay = i_delay;
310 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
312 es_out_sys_t *p_sys = out->p_sys;
315 for( i = 0; i < p_sys->i_es; i++ )
317 es_out_id_t *es = p_sys->es[i];
319 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
325 /*****************************************************************************
327 *****************************************************************************/
328 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
329 vlc_bool_t b_delete )
331 es_out_sys_t *p_sys = out->p_sys;
332 input_thread_t *p_input = p_sys->p_input;
333 vlc_value_t val, text;
337 if( es->fmt.i_cat == AUDIO_ES )
338 psz_var = "audio-es";
339 else if( es->fmt.i_cat == VIDEO_ES )
340 psz_var = "video-es";
341 else if( es->fmt.i_cat == SPU_ES )
348 val.i_int = es->i_id;
349 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
350 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
354 /* Get the number of ES already added */
355 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
360 /* First one, we need to add the "Disable" choice */
361 val2.i_int = -1; text.psz_string = _("Disable");
362 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
366 /* Take care of the ES description */
367 if( es->fmt.psz_description && *es->fmt.psz_description )
369 if( es->psz_language && *es->psz_language )
371 text.psz_string = malloc( strlen( es->fmt.psz_description) + strlen( es->psz_language ) + 10 );
372 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description, es->psz_language );
374 else text.psz_string = strdup( es->fmt.psz_description );
378 if( es->psz_language && *es->psz_language )
381 text.psz_string = malloc( strlen( _("Track %i") )+ strlen( es->psz_language ) + 30 );
382 asprintf( &temp, _("Track %i"), val.i_int );
383 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
388 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
389 sprintf( text.psz_string, _("Track %i"), val.i_int );
393 val.i_int = es->i_id;
394 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
396 free( text.psz_string );
398 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
401 /* EsOutProgramSelect:
402 * Select a program and update the object variable
404 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
406 es_out_sys_t *p_sys = out->p_sys;
407 input_thread_t *p_input = p_sys->p_input;
411 if( p_sys->p_pgrm == p_pgrm )
412 return; /* Nothing to do */
416 es_out_pgrm_t *old = p_sys->p_pgrm;
417 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
419 for( i = 0; i < p_sys->i_es; i++ )
421 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
422 p_sys->i_mode != ES_OUT_MODE_ALL )
423 EsUnselect( out, p_sys->es[i], VLC_TRUE );
426 p_sys->p_es_audio = NULL;
427 p_sys->p_es_sub = NULL;
428 p_sys->p_es_video = NULL;
431 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
433 /* Mark it selected */
434 p_pgrm->b_selected = VLC_TRUE;
436 /* Switch master stream */
437 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
439 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
441 p_pgrm->clock.b_master = VLC_TRUE;
442 p_sys->p_pgrm = p_pgrm;
444 /* Update "program" */
445 val.i_int = p_pgrm->i_id;
446 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
449 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
450 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
451 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
452 for( i = 0; i < p_sys->i_es; i++ )
454 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
455 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
456 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
459 /* Update now playing if defined per program */
460 if( p_pgrm->psz_now_playing )
462 char *psz_cat = malloc( strlen(_("Program")) + 10 );
464 sprintf( psz_cat, "%s %d", _("Program"), p_pgrm->i_id );
465 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
466 VLC_META_NOW_PLAYING, "%s", p_pgrm->psz_now_playing );
471 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
477 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
479 es_out_sys_t *p_sys = out->p_sys;
480 input_thread_t *p_input = p_sys->p_input;
483 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
486 p_pgrm->i_id = i_group;
488 p_pgrm->b_selected = VLC_FALSE;
489 p_pgrm->psz_now_playing = NULL;
490 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->input.i_cr_average );
493 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
495 /* Update "program" variable */
497 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
499 if( i_group == var_GetInteger( p_input, "program" ) )
501 EsOutProgramSelect( out, p_pgrm );
505 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
513 static int EsOutProgramDel( es_out_t *out, int i_group )
515 es_out_sys_t *p_sys = out->p_sys;
516 input_thread_t *p_input = p_sys->p_input;
517 es_out_pgrm_t *p_pgrm = NULL;
521 for( i = 0; i < p_sys->i_pgrm; i++ )
523 if( p_sys->pgrm[i]->i_id == i_group )
525 p_pgrm = p_sys->pgrm[i];
535 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
536 i_group, p_pgrm->i_es );
540 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
542 /* If program is selected we need to unselect it */
543 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = 0;
545 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
548 /* Update "program" variable */
550 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
552 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
559 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
561 es_out_sys_t *p_sys = out->p_sys;
562 es_out_pgrm_t *p_pgrm = NULL;
563 input_thread_t *p_input = p_sys->p_input;
564 char *psz_cat = malloc( strlen(_("Program")) + 10 );
565 char *psz_name = NULL;
566 char *psz_now_playing = NULL;
567 char *psz_provider = NULL;
570 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
571 sprintf( psz_cat, "%s %d", _("Program"), i_group );
573 for( i = 0; i < p_meta->i_meta; i++ )
575 msg_Dbg( p_input, " - %s = %s", p_meta->name[i], p_meta->value[i] );
577 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
578 _(p_meta->name[i]), "%s", p_meta->value[i] );
579 if( !strcasecmp( p_meta->name[i], "Name" ) )
580 psz_name = p_meta->value[i];
581 else if( !strcasecmp( p_meta->name[i], "Provider" ) )
582 psz_provider = p_meta->value[i];
583 else if( !strcasecmp( p_meta->name[i], VLC_META_NOW_PLAYING ) )
584 psz_now_playing = p_meta->value[i];
587 if( !psz_name && !psz_now_playing )
593 for( i = 0; i < p_sys->i_pgrm; i++ )
595 if( p_sys->pgrm[i]->i_id == i_group )
597 p_pgrm = p_sys->pgrm[i];
603 p_pgrm = EsOutProgramAdd( out, i_group );
605 /* Update the description text of the program */
606 if( psz_name && *psz_name )
611 /* ugly but it works */
613 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
615 if( psz_provider && *psz_provider )
617 asprintf( &text.psz_string, "%s [%s]", psz_name, psz_provider );
618 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
619 free( text.psz_string );
623 text.psz_string = psz_name;
624 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
627 if( psz_now_playing )
629 p_pgrm->psz_now_playing = strdup(psz_now_playing);
631 if( p_sys->p_pgrm == p_pgrm )
633 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
634 VLC_META_NOW_PLAYING, "%s", psz_now_playing );
643 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
645 es_out_sys_t *p_sys = out->p_sys;
646 input_thread_t *p_input = p_sys->p_input;
648 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
649 es_out_pgrm_t *p_pgrm = NULL;
652 if( fmt->i_group < 0 )
654 msg_Err( p_input, "invalid group number" );
658 /* Search the program */
659 for( i = 0; i < p_sys->i_pgrm; i++ )
661 if( fmt->i_group == p_sys->pgrm[i]->i_id )
663 p_pgrm = p_sys->pgrm[i];
669 /* Create a new one */
670 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
673 /* Increase ref count for program */
678 fmt->i_id = out->p_sys->i_id;
679 es->i_id = fmt->i_id;
681 es_format_Copy( &es->fmt, fmt );
682 es->i_preroll_end = -1;
683 es->b_discontinuity = VLC_FALSE;
688 es->i_channel = p_sys->i_audio;
692 es->i_channel = p_sys->i_video;
693 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
694 vlc_ureduce( &es->fmt.video.i_frame_rate,
695 &es->fmt.video.i_frame_rate_base,
696 fmt->video.i_frame_rate,
697 fmt->video.i_frame_rate_base, 0 );
701 es->i_channel = p_sys->i_sub;
708 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
709 es->psz_language_code = LanguageGetCode( fmt->psz_language );
712 if( es->p_pgrm == p_sys->p_pgrm )
713 EsOutESVarUpdate( out, es, VLC_FALSE );
715 /* Select it if needed */
716 EsOutSelect( out, es, VLC_FALSE );
719 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
720 p_sys->i_id++; /* always incremented */
734 EsOutAddInfo( out, es );
739 static void EsSelect( es_out_t *out, es_out_id_t *es )
741 es_out_sys_t *p_sys = out->p_sys;
742 input_thread_t *p_input = p_sys->p_input;
748 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
752 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
754 if( !var_GetBool( p_input, "video" ) ||
755 ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
757 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
762 else if( es->fmt.i_cat == AUDIO_ES )
764 var_Get( p_input, "audio", &val );
765 if( !var_GetBool( p_input, "audio" ) ||
766 ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
768 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
773 if( es->fmt.i_cat == SPU_ES )
775 var_Get( p_input, "spu", &val );
776 if( !var_GetBool( p_input, "spu" ) ||
777 ( p_input->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
779 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
785 es->i_preroll_end = -1;
786 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
787 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
790 if( es->fmt.i_cat == VIDEO_ES )
791 psz_var = "video-es";
792 else if( es->fmt.i_cat == AUDIO_ES )
793 psz_var = "audio-es";
794 else if( es->fmt.i_cat == SPU_ES )
799 /* Mark it as selected */
800 val.i_int = es->i_id;
801 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
804 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
807 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
809 es_out_sys_t *p_sys = out->p_sys;
810 input_thread_t *p_input = p_sys->p_input;
814 if( es->p_dec == NULL )
816 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
820 input_DecoderDelete( es->p_dec );
827 if( es->p_dec == NULL )
829 if( es->fmt.i_cat == VIDEO_ES )
830 psz_var = "video-es";
831 else if( es->fmt.i_cat == AUDIO_ES )
832 psz_var = "audio-es";
833 else if( es->fmt.i_cat == SPU_ES )
838 /* Mark it as selected */
840 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
842 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
846 * Select an ES given the current mode
847 * XXX: you need to take a the lock before (stream.stream_lock)
849 * \param out The es_out structure
850 * \param es es_out_id structure
854 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
856 es_out_sys_t *p_sys = out->p_sys;
858 int i_cat = es->fmt.i_cat;
860 if( !p_sys->b_active ||
861 ( !b_force && es->fmt.i_priority < 0 ) )
866 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
871 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
875 var_Get( p_sys->p_input, "programs", &val );
876 for ( i = 0; i < val.p_list->i_count; i++ )
878 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
885 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
887 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
891 if( es->p_pgrm != p_sys->p_pgrm )
894 if( i_cat == AUDIO_ES )
896 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
897 es->psz_language_code );
899 if( p_sys->p_es_audio &&
900 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
902 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
903 p_sys->p_es_audio->psz_language_code );
905 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
907 i_wanted = es->i_channel;
911 /* Select audio if (no audio selected yet)
912 * - no audio-language
913 * - no audio code for the ES
914 * - audio code in the requested list */
916 !strcmp( es->psz_language_code, "??" ) ||
917 !p_sys->ppsz_audio_language )
918 i_wanted = es->i_channel;
921 if( p_sys->i_audio_last >= 0 )
922 i_wanted = p_sys->i_audio_last;
924 if( p_sys->i_audio_id >= 0 )
926 if( es->i_id == p_sys->i_audio_id )
927 i_wanted = es->i_channel;
932 else if( i_cat == SPU_ES )
934 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
935 es->psz_language_code );
937 if( p_sys->p_es_sub &&
938 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
940 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
941 p_sys->p_es_sub->psz_language_code );
943 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
944 idx1, es->psz_language_code, idx2,
945 p_sys->p_es_sub->psz_language_code );
947 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
949 /* We found a SPU that matches our language request */
950 i_wanted = es->i_channel;
954 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
955 idx1, es->psz_language_code );
957 i_wanted = es->i_channel;
959 if( p_sys->i_sub_last >= 0 )
960 i_wanted = p_sys->i_sub_last;
962 if( p_sys->i_sub_id >= 0 )
964 if( es->i_id == p_sys->i_sub_id )
965 i_wanted = es->i_channel;
970 else if( i_cat == VIDEO_ES )
972 i_wanted = es->i_channel;
975 if( i_wanted == es->i_channel && es->p_dec == NULL )
979 /* FIXME TODO handle priority here */
982 if( i_cat == AUDIO_ES )
984 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
986 p_sys->p_es_audio != es &&
987 p_sys->p_es_audio->p_dec )
989 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
991 p_sys->p_es_audio = es;
993 else if( i_cat == SPU_ES )
995 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
997 p_sys->p_es_sub != es &&
998 p_sys->p_es_sub->p_dec )
1000 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
1002 p_sys->p_es_sub = es;
1004 else if( i_cat == VIDEO_ES )
1006 p_sys->p_es_video = es;
1012 * Send a block for the given es_out
1014 * \param out the es_out to send from
1015 * \param es the es_out_id
1016 * \param p_block the data block to send
1018 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1020 es_out_sys_t *p_sys = out->p_sys;
1021 input_thread_t *p_input = p_sys->p_input;
1022 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1026 if( es->fmt.i_cat == AUDIO_ES )
1027 i_delay = p_sys->i_audio_delay;
1028 else if( es->fmt.i_cat == SPU_ES )
1029 i_delay = p_sys->i_spu_delay;
1033 stats_UpdateInteger( p_input, "demux_read", p_block->i_buffer );
1034 stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
1036 stats_UpdateFloat( p_input , "demux_bitrate", (float)i_total );
1038 /* Mark preroll blocks */
1039 if( es->i_preroll_end >= 0 )
1041 int64_t i_date = p_block->i_pts;
1043 i_date = p_block->i_dts;
1045 if( i_date < es->i_preroll_end )
1046 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1048 es->i_preroll_end = -1;
1051 /* +11 -> avoid null value with non null dts/pts */
1052 if( p_block->i_dts > 0 )
1055 input_ClockGetTS( p_input, &p_pgrm->clock,
1056 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
1058 if( p_block->i_pts > 0 )
1061 input_ClockGetTS( p_input, &p_pgrm->clock,
1062 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
1064 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1066 mtime_t current_date = mdate();
1068 || p_block->i_pts > current_date + 10000000
1069 || current_date > p_block->i_pts )
1071 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1072 * for teletext streams. */
1073 p_block->i_pts = current_date + 400000
1074 + p_input->i_pts_delay + i_delay;
1078 p_block->i_rate = p_input->i_rate;
1080 /* TODO handle mute */
1081 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
1082 p_input->i_rate == INPUT_RATE_DEFAULT ) )
1084 input_DecoderDecode( es->p_dec, p_block );
1088 block_Release( p_block );
1094 /*****************************************************************************
1096 *****************************************************************************/
1097 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1099 es_out_sys_t *p_sys = out->p_sys;
1100 vlc_bool_t b_reselect = VLC_FALSE;
1103 /* We don't try to reselect */
1105 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1107 if( es->p_pgrm == p_sys->p_pgrm )
1108 EsOutESVarUpdate( out, es, VLC_TRUE );
1110 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1113 if( es->p_pgrm->i_es == 0 )
1115 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1118 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1119 p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1121 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1122 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1123 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1125 switch( es->fmt.i_cat )
1138 /* Re-select another track when needed */
1140 for( i = 0; i < p_sys->i_es; i++ )
1142 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1143 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1146 if( es->psz_language )
1147 free( es->psz_language );
1148 if( es->psz_language_code )
1149 free( es->psz_language_code );
1151 es_format_Clean( &es->fmt );
1157 * Control query handler
1159 * \param out the es_out to control
1160 * \param i_query A es_out query as defined in include/ninput.h
1161 * \param args a variable list of arguments for the query
1162 * \return VLC_SUCCESS or an error code
1164 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1166 es_out_sys_t *p_sys = out->p_sys;
1174 case ES_OUT_SET_ES_STATE:
1175 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1176 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1177 if( b && es->p_dec == NULL )
1179 EsSelect( out, es );
1180 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1182 else if( !b && es->p_dec )
1184 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1189 case ES_OUT_GET_ES_STATE:
1190 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1191 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1193 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1196 case ES_OUT_SET_ACTIVE:
1198 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1199 p_sys->b_active = b;
1202 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1206 case ES_OUT_GET_ACTIVE:
1207 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1208 *pb = p_sys->b_active;
1211 case ES_OUT_SET_MODE:
1212 i = (int) va_arg( args, int );
1213 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1214 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1218 /* Reapply policy mode */
1219 for( i = 0; i < p_sys->i_es; i++ )
1221 if( p_sys->es[i]->p_dec )
1223 EsUnselect( out, p_sys->es[i],
1224 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1227 for( i = 0; i < p_sys->i_es; i++ )
1229 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1233 return VLC_EGENERIC;
1235 case ES_OUT_GET_MODE:
1236 pi = (int*) va_arg( args, int* );
1237 *pi = p_sys->i_mode;
1241 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1242 /* Special case NULL, NULL+i_cat */
1245 for( i = 0; i < p_sys->i_es; i++ )
1247 if( p_sys->es[i]->p_dec )
1248 EsUnselect( out, p_sys->es[i],
1249 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1252 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1254 for( i = 0; i < p_sys->i_es; i++ )
1256 if( p_sys->es[i]->p_dec &&
1257 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1258 EsUnselect( out, p_sys->es[i],
1259 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1262 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1264 for( i = 0; i < p_sys->i_es; i++ )
1266 if( p_sys->es[i]->p_dec &&
1267 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1268 EsUnselect( out, p_sys->es[i],
1269 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1272 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1274 for( i = 0; i < p_sys->i_es; i++ )
1276 if( p_sys->es[i]->p_dec &&
1277 p_sys->es[i]->fmt.i_cat == SPU_ES )
1278 EsUnselect( out, p_sys->es[i],
1279 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1284 for( i = 0; i < p_sys->i_es; i++ )
1286 if( es == p_sys->es[i] )
1288 EsOutSelect( out, es, VLC_TRUE );
1295 case ES_OUT_SET_PCR:
1296 case ES_OUT_SET_GROUP_PCR:
1298 es_out_pgrm_t *p_pgrm = NULL;
1302 if( i_query == ES_OUT_SET_PCR )
1304 p_pgrm = p_sys->p_pgrm;
1309 i_group = (int)va_arg( args, int );
1310 for( i = 0; i < p_sys->i_pgrm; i++ )
1312 if( p_sys->pgrm[i]->i_id == i_group )
1314 p_pgrm = p_sys->pgrm[i];
1319 if( p_pgrm == NULL )
1320 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1322 i_pcr = (int64_t)va_arg( args, int64_t );
1323 /* search program */
1324 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1325 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1326 (i_pcr + 11 ) * 9 / 100);
1330 case ES_OUT_RESET_PCR:
1331 for( i = 0; i < p_sys->i_pgrm; i++ )
1333 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1334 p_sys->pgrm[i]->clock.last_pts = 0;
1341 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1342 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1343 *pi_ts = input_ClockGetTS( p_sys->p_input,
1344 &p_sys->p_pgrm->clock,
1345 ( i_ts + 11 ) * 9 / 100 );
1348 return VLC_EGENERIC;
1350 case ES_OUT_GET_GROUP:
1351 pi = (int*) va_arg( args, int* );
1353 *pi = p_sys->p_pgrm->i_id;
1355 *pi = -1; /* FIXME */
1358 case ES_OUT_SET_GROUP:
1361 i = (int) va_arg( args, int );
1362 for( j = 0; j < p_sys->i_pgrm; j++ )
1364 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1365 if( p_pgrm->i_id == i )
1367 EsOutProgramSelect( out, p_pgrm );
1371 return VLC_EGENERIC;
1374 case ES_OUT_SET_FMT:
1376 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1377 * to update the p_extra data */
1379 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1380 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1381 if( es == NULL ) return VLC_EGENERIC;
1383 if( p_fmt->i_extra )
1385 es->fmt.i_extra = p_fmt->i_extra;
1386 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1387 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1389 if( !es->p_dec ) return VLC_SUCCESS;
1392 input_DecoderDelete( es->p_dec );
1393 es->p_dec = input_DecoderNew( p_sys->p_input,
1394 &es->fmt, VLC_FALSE );
1397 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1398 es->p_dec->fmt_in.p_extra =
1399 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1400 memcpy( es->p_dec->fmt_in.p_extra,
1401 p_fmt->p_extra, p_fmt->i_extra );
1408 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1412 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1413 i_date = (int64_t)va_arg( args, int64_t );
1415 if( !es || !es->p_dec )
1416 return VLC_EGENERIC;
1418 es->i_preroll_end = i_date;
1419 input_DecoderPreroll( es->p_dec, i_date );
1423 case ES_OUT_SET_GROUP_META:
1425 int i_group = (int)va_arg( args, int );
1426 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1428 EsOutProgramMeta( out, i_group, p_meta );
1431 case ES_OUT_DEL_GROUP:
1433 int i_group = (int)va_arg( args, int );
1435 return EsOutProgramDel( out, i_group );
1439 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1440 return VLC_EGENERIC;
1444 /****************************************************************************
1445 * LanguageGetName: try to expend iso639 into plain name
1446 ****************************************************************************/
1447 static char *LanguageGetName( const char *psz_code )
1449 const iso639_lang_t *pl;
1451 if( psz_code == NULL )
1453 return strdup( "" );
1456 if( strlen( psz_code ) == 2 )
1458 pl = GetLang_1( psz_code );
1460 else if( strlen( psz_code ) == 3 )
1462 pl = GetLang_2B( psz_code );
1463 if( !strcmp( pl->psz_iso639_1, "??" ) )
1465 pl = GetLang_2T( psz_code );
1470 return strdup( psz_code );
1473 if( !strcmp( pl->psz_iso639_1, "??" ) )
1475 return strdup( psz_code );
1479 if( *pl->psz_native_name )
1481 return strdup( pl->psz_native_name );
1483 return strdup( pl->psz_eng_name );
1487 /* Get a 2 char code */
1488 static char *LanguageGetCode( const char *psz_lang )
1490 const iso639_lang_t *pl;
1492 if( psz_lang == NULL || *psz_lang == '\0' )
1493 return strdup("??");
1495 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1497 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1498 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1499 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1500 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1501 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1505 if( pl->psz_iso639_1 != NULL )
1506 return strdup( pl->psz_iso639_1 );
1508 return strdup("??");
1511 static char **LanguageSplit( const char *psz_langs )
1518 if( psz_langs == NULL ) return NULL;
1520 psz_parser = psz_dup = strdup(psz_langs);
1522 while( psz_parser && *psz_parser )
1527 psz = strchr(psz_parser, ',' );
1528 if( psz ) *psz++ = '\0';
1530 psz_code = LanguageGetCode( psz_parser );
1531 if( strcmp( psz_code, "??" ) )
1533 TAB_APPEND( i_psz, ppsz, psz_code );
1541 TAB_APPEND( i_psz, ppsz, NULL );
1548 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1552 if( !ppsz_langs || !psz_lang ) return -1;
1554 for( i = 0; ppsz_langs[i]; i++ )
1555 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1560 /****************************************************************************
1562 * - add meta info to the playlist item
1563 ****************************************************************************/
1564 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1566 es_out_sys_t *p_sys = out->p_sys;
1567 input_thread_t *p_input = p_sys->p_input;
1568 es_format_t *fmt = &es->fmt;
1571 /* Add stream info */
1572 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1574 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1575 "%.4s", (char*)&fmt->i_codec );
1577 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1578 "%s", es->psz_language );
1580 /* Add information */
1581 switch( fmt->i_cat )
1584 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1585 _("Type"), _("Audio") );
1587 if( fmt->audio.i_channels > 0 )
1588 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1589 "%d", fmt->audio.i_channels );
1591 if( fmt->audio.i_rate > 0 )
1592 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1593 _("%d Hz"), fmt->audio.i_rate );
1595 if( fmt->audio.i_bitspersample > 0 )
1596 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1597 _("Bits per sample"), "%d",
1598 fmt->audio.i_bitspersample );
1600 if( fmt->i_bitrate > 0 )
1601 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1602 _("%d kb/s"), fmt->i_bitrate / 1000 );
1606 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1607 _("Type"), _("Video") );
1609 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1610 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1611 _("Resolution"), "%dx%d",
1612 fmt->video.i_width, fmt->video.i_height );
1614 if( fmt->video.i_visible_width > 0 &&
1615 fmt->video.i_visible_height > 0 )
1616 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1617 _("Display resolution"), "%dx%d",
1618 fmt->video.i_visible_width,
1619 fmt->video.i_visible_height);
1620 if( fmt->video.i_frame_rate > 0 &&
1621 fmt->video.i_frame_rate_base > 0 )
1622 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1623 _("Frame rate"), "%f",
1624 (float)fmt->video.i_frame_rate /
1625 fmt->video.i_frame_rate_base );
1629 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1630 _("Type"), _("Subtitle") );