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, "selected audio 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, "selected subtitle 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) +
372 strlen( es->psz_language ) + 10 );
373 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description,
376 else text.psz_string = strdup( es->fmt.psz_description );
380 if( es->psz_language && *es->psz_language )
383 text.psz_string = malloc( strlen( _("Track %i") )+
384 strlen( es->psz_language ) + 30 );
385 asprintf( &temp, _("Track %i"), val.i_int );
386 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
391 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
392 sprintf( text.psz_string, _("Track %i"), val.i_int );
396 val.i_int = es->i_id;
397 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
399 free( text.psz_string );
401 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
404 /* EsOutProgramSelect:
405 * Select a program and update the object variable
407 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
409 es_out_sys_t *p_sys = out->p_sys;
410 input_thread_t *p_input = p_sys->p_input;
414 if( p_sys->p_pgrm == p_pgrm )
415 return; /* Nothing to do */
419 es_out_pgrm_t *old = p_sys->p_pgrm;
420 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
422 for( i = 0; i < p_sys->i_es; i++ )
424 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
425 p_sys->i_mode != ES_OUT_MODE_ALL )
426 EsUnselect( out, p_sys->es[i], VLC_TRUE );
429 p_sys->p_es_audio = NULL;
430 p_sys->p_es_sub = NULL;
431 p_sys->p_es_video = NULL;
434 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
436 /* Mark it selected */
437 p_pgrm->b_selected = VLC_TRUE;
439 /* Switch master stream */
440 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
442 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
444 p_pgrm->clock.b_master = VLC_TRUE;
445 p_sys->p_pgrm = p_pgrm;
447 /* Update "program" */
448 val.i_int = p_pgrm->i_id;
449 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
452 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
453 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
454 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
455 for( i = 0; i < p_sys->i_es; i++ )
457 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
458 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
459 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
462 /* Update now playing if defined per program */
463 if( p_pgrm->psz_now_playing )
465 char *psz_cat = malloc( strlen(_("Program")) + 10 );
467 sprintf( psz_cat, "%s %d", _("Program"), p_pgrm->i_id );
468 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT),
469 _(VLC_META_NOW_PLAYING), "%s", p_pgrm->psz_now_playing );
474 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
480 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
482 es_out_sys_t *p_sys = out->p_sys;
483 input_thread_t *p_input = p_sys->p_input;
486 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
489 p_pgrm->i_id = i_group;
491 p_pgrm->b_selected = VLC_FALSE;
492 p_pgrm->psz_now_playing = NULL;
493 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->input.i_cr_average );
496 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
498 /* Update "program" variable */
500 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
502 if( i_group == var_GetInteger( p_input, "program" ) )
504 EsOutProgramSelect( out, p_pgrm );
508 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
516 static int EsOutProgramDel( es_out_t *out, int i_group )
518 es_out_sys_t *p_sys = out->p_sys;
519 input_thread_t *p_input = p_sys->p_input;
520 es_out_pgrm_t *p_pgrm = NULL;
524 for( i = 0; i < p_sys->i_pgrm; i++ )
526 if( p_sys->pgrm[i]->i_id == i_group )
528 p_pgrm = p_sys->pgrm[i];
538 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
539 i_group, p_pgrm->i_es );
543 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
545 /* If program is selected we need to unselect it */
546 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = 0;
548 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
551 /* Update "program" variable */
553 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
555 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
562 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
564 es_out_sys_t *p_sys = out->p_sys;
565 es_out_pgrm_t *p_pgrm = NULL;
566 input_thread_t *p_input = p_sys->p_input;
567 char *psz_cat = malloc( strlen(_("Program")) + 10 );
568 char *psz_name = NULL;
569 char *psz_now_playing = NULL;
570 char *psz_provider = NULL;
573 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
574 sprintf( psz_cat, "%s %d", _("Program"), i_group );
576 for( i = 0; i < p_meta->i_meta; i++ )
578 msg_Dbg( p_input, " - %s = %s", p_meta->name[i], p_meta->value[i] );
580 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
581 _(p_meta->name[i]), "%s", p_meta->value[i] );
582 if( !strcasecmp( p_meta->name[i], "Name" ) )
583 psz_name = p_meta->value[i];
584 else if( !strcasecmp( p_meta->name[i], "Provider" ) )
585 psz_provider = p_meta->value[i];
586 else if( !strcasecmp( p_meta->name[i], VLC_META_NOW_PLAYING ) )
587 psz_now_playing = p_meta->value[i];
590 if( !psz_name && !psz_now_playing )
596 for( i = 0; i < p_sys->i_pgrm; i++ )
598 if( p_sys->pgrm[i]->i_id == i_group )
600 p_pgrm = p_sys->pgrm[i];
606 p_pgrm = EsOutProgramAdd( out, i_group );
608 /* Update the description text of the program */
609 if( psz_name && *psz_name )
614 /* ugly but it works */
616 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
618 if( psz_provider && *psz_provider )
620 asprintf( &text.psz_string, "%s [%s]", psz_name, psz_provider );
621 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
622 free( text.psz_string );
626 text.psz_string = psz_name;
627 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
630 if( psz_now_playing )
632 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
633 p_pgrm->psz_now_playing = strdup(psz_now_playing);
635 if( p_sys->p_pgrm == p_pgrm )
637 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT),
638 _(VLC_META_NOW_PLAYING), "%s", psz_now_playing );
647 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
649 es_out_sys_t *p_sys = out->p_sys;
650 input_thread_t *p_input = p_sys->p_input;
652 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
653 es_out_pgrm_t *p_pgrm = NULL;
656 if( fmt->i_group < 0 )
658 msg_Err( p_input, "invalid group number" );
662 /* Search the program */
663 for( i = 0; i < p_sys->i_pgrm; i++ )
665 if( fmt->i_group == p_sys->pgrm[i]->i_id )
667 p_pgrm = p_sys->pgrm[i];
673 /* Create a new one */
674 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
677 /* Increase ref count for program */
682 fmt->i_id = out->p_sys->i_id;
683 es->i_id = fmt->i_id;
685 es_format_Copy( &es->fmt, fmt );
686 es->i_preroll_end = -1;
687 es->b_discontinuity = VLC_FALSE;
692 es->i_channel = p_sys->i_audio;
696 es->i_channel = p_sys->i_video;
697 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
698 vlc_ureduce( &es->fmt.video.i_frame_rate,
699 &es->fmt.video.i_frame_rate_base,
700 fmt->video.i_frame_rate,
701 fmt->video.i_frame_rate_base, 0 );
705 es->i_channel = p_sys->i_sub;
712 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
713 es->psz_language_code = LanguageGetCode( fmt->psz_language );
716 if( es->p_pgrm == p_sys->p_pgrm )
717 EsOutESVarUpdate( out, es, VLC_FALSE );
719 /* Select it if needed */
720 EsOutSelect( out, es, VLC_FALSE );
723 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
724 p_sys->i_id++; /* always incremented */
738 EsOutAddInfo( out, es );
743 static void EsSelect( es_out_t *out, es_out_id_t *es )
745 es_out_sys_t *p_sys = out->p_sys;
746 input_thread_t *p_input = p_sys->p_input;
752 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
756 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
758 if( !var_GetBool( p_input, "video" ) ||
759 ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
761 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
766 else if( es->fmt.i_cat == AUDIO_ES )
768 var_Get( p_input, "audio", &val );
769 if( !var_GetBool( p_input, "audio" ) ||
770 ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
772 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
777 if( es->fmt.i_cat == SPU_ES )
779 var_Get( p_input, "spu", &val );
780 if( !var_GetBool( p_input, "spu" ) ||
781 ( p_input->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
783 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
789 es->i_preroll_end = -1;
790 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
791 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
794 if( es->fmt.i_cat == VIDEO_ES )
795 psz_var = "video-es";
796 else if( es->fmt.i_cat == AUDIO_ES )
797 psz_var = "audio-es";
798 else if( es->fmt.i_cat == SPU_ES )
803 /* Mark it as selected */
804 val.i_int = es->i_id;
805 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
808 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
811 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
813 es_out_sys_t *p_sys = out->p_sys;
814 input_thread_t *p_input = p_sys->p_input;
818 if( es->p_dec == NULL )
820 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
824 input_DecoderDelete( es->p_dec );
831 if( es->p_dec == NULL )
833 if( es->fmt.i_cat == VIDEO_ES )
834 psz_var = "video-es";
835 else if( es->fmt.i_cat == AUDIO_ES )
836 psz_var = "audio-es";
837 else if( es->fmt.i_cat == SPU_ES )
842 /* Mark it as selected */
844 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
846 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
850 * Select an ES given the current mode
851 * XXX: you need to take a the lock before (stream.stream_lock)
853 * \param out The es_out structure
854 * \param es es_out_id structure
858 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
860 es_out_sys_t *p_sys = out->p_sys;
862 int i_cat = es->fmt.i_cat;
864 if( !p_sys->b_active ||
865 ( !b_force && es->fmt.i_priority < 0 ) )
870 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
875 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
879 var_Get( p_sys->p_input, "programs", &val );
880 for ( i = 0; i < val.p_list->i_count; i++ )
882 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
889 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
891 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
895 if( es->p_pgrm != p_sys->p_pgrm )
898 if( i_cat == AUDIO_ES )
900 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
901 es->psz_language_code );
903 if( p_sys->p_es_audio &&
904 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
906 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
907 p_sys->p_es_audio->psz_language_code );
909 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
911 i_wanted = es->i_channel;
915 /* Select audio if (no audio selected yet)
916 * - no audio-language
917 * - no audio code for the ES
918 * - audio code in the requested list */
920 !strcmp( es->psz_language_code, "??" ) ||
921 !p_sys->ppsz_audio_language )
922 i_wanted = es->i_channel;
925 if( p_sys->i_audio_last >= 0 )
926 i_wanted = p_sys->i_audio_last;
928 if( p_sys->i_audio_id >= 0 )
930 if( es->i_id == p_sys->i_audio_id )
931 i_wanted = es->i_channel;
936 else if( i_cat == SPU_ES )
938 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
939 es->psz_language_code );
941 if( p_sys->p_es_sub &&
942 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
944 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
945 p_sys->p_es_sub->psz_language_code );
947 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
948 idx1, es->psz_language_code, idx2,
949 p_sys->p_es_sub->psz_language_code );
951 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
953 /* We found a SPU that matches our language request */
954 i_wanted = es->i_channel;
958 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
959 idx1, es->psz_language_code );
961 i_wanted = es->i_channel;
963 if( p_sys->i_sub_last >= 0 )
964 i_wanted = p_sys->i_sub_last;
966 if( p_sys->i_sub_id >= 0 )
968 if( es->i_id == p_sys->i_sub_id )
969 i_wanted = es->i_channel;
974 else if( i_cat == VIDEO_ES )
976 i_wanted = es->i_channel;
979 if( i_wanted == es->i_channel && es->p_dec == NULL )
983 /* FIXME TODO handle priority here */
986 if( i_cat == AUDIO_ES )
988 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
990 p_sys->p_es_audio != es &&
991 p_sys->p_es_audio->p_dec )
993 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
995 p_sys->p_es_audio = es;
997 else if( i_cat == SPU_ES )
999 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1001 p_sys->p_es_sub != es &&
1002 p_sys->p_es_sub->p_dec )
1004 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
1006 p_sys->p_es_sub = es;
1008 else if( i_cat == VIDEO_ES )
1010 p_sys->p_es_video = es;
1016 * Send a block for the given es_out
1018 * \param out the es_out to send from
1019 * \param es the es_out_id
1020 * \param p_block the data block to send
1022 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1024 es_out_sys_t *p_sys = out->p_sys;
1025 input_thread_t *p_input = p_sys->p_input;
1026 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1030 if( es->fmt.i_cat == AUDIO_ES )
1031 i_delay = p_sys->i_audio_delay;
1032 else if( es->fmt.i_cat == SPU_ES )
1033 i_delay = p_sys->i_spu_delay;
1037 if( p_input->p_libvlc->b_stats )
1039 stats_UpdateInteger( p_input, STATS_DEMUX_READ, p_block->i_buffer,
1041 stats_UpdateFloat( p_input , STATS_DEMUX_BITRATE, (float)i_total, NULL );
1044 /* Mark preroll blocks */
1045 if( es->i_preroll_end >= 0 )
1047 int64_t i_date = p_block->i_pts;
1049 i_date = p_block->i_dts;
1051 if( i_date < es->i_preroll_end )
1052 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1054 es->i_preroll_end = -1;
1057 /* +11 -> avoid null value with non null dts/pts */
1058 if( p_block->i_dts > 0 )
1061 input_ClockGetTS( p_input, &p_pgrm->clock,
1062 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
1064 if( p_block->i_pts > 0 )
1067 input_ClockGetTS( p_input, &p_pgrm->clock,
1068 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
1070 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1072 mtime_t current_date = mdate();
1074 || p_block->i_pts > current_date + 10000000
1075 || current_date > p_block->i_pts )
1077 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1078 * for teletext streams. */
1079 p_block->i_pts = current_date + 400000
1080 + p_input->i_pts_delay + i_delay;
1084 p_block->i_rate = p_input->i_rate;
1086 /* TODO handle mute */
1087 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
1088 p_input->i_rate == INPUT_RATE_DEFAULT ) )
1090 input_DecoderDecode( es->p_dec, p_block );
1094 block_Release( p_block );
1100 /*****************************************************************************
1102 *****************************************************************************/
1103 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1105 es_out_sys_t *p_sys = out->p_sys;
1106 vlc_bool_t b_reselect = VLC_FALSE;
1109 /* We don't try to reselect */
1111 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1113 if( es->p_pgrm == p_sys->p_pgrm )
1114 EsOutESVarUpdate( out, es, VLC_TRUE );
1116 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1119 if( es->p_pgrm->i_es == 0 )
1121 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1124 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1125 p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1127 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1128 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1129 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1131 switch( es->fmt.i_cat )
1144 /* Re-select another track when needed */
1146 for( i = 0; i < p_sys->i_es; i++ )
1148 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1149 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1152 if( es->psz_language )
1153 free( es->psz_language );
1154 if( es->psz_language_code )
1155 free( es->psz_language_code );
1157 es_format_Clean( &es->fmt );
1163 * Control query handler
1165 * \param out the es_out to control
1166 * \param i_query A es_out query as defined in include/ninput.h
1167 * \param args a variable list of arguments for the query
1168 * \return VLC_SUCCESS or an error code
1170 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1172 es_out_sys_t *p_sys = out->p_sys;
1180 case ES_OUT_SET_ES_STATE:
1181 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1182 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1183 if( b && es->p_dec == NULL )
1185 EsSelect( out, es );
1186 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1188 else if( !b && es->p_dec )
1190 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1195 case ES_OUT_GET_ES_STATE:
1196 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1197 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1199 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1202 case ES_OUT_SET_ACTIVE:
1204 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1205 p_sys->b_active = b;
1208 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1212 case ES_OUT_GET_ACTIVE:
1213 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1214 *pb = p_sys->b_active;
1217 case ES_OUT_SET_MODE:
1218 i = (int) va_arg( args, int );
1219 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1220 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1224 /* Reapply policy mode */
1225 for( i = 0; i < p_sys->i_es; i++ )
1227 if( p_sys->es[i]->p_dec )
1229 EsUnselect( out, p_sys->es[i],
1230 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1233 for( i = 0; i < p_sys->i_es; i++ )
1235 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1239 return VLC_EGENERIC;
1241 case ES_OUT_GET_MODE:
1242 pi = (int*) va_arg( args, int* );
1243 *pi = p_sys->i_mode;
1247 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1248 /* Special case NULL, NULL+i_cat */
1251 for( i = 0; i < p_sys->i_es; i++ )
1253 if( p_sys->es[i]->p_dec )
1254 EsUnselect( out, p_sys->es[i],
1255 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1258 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1260 for( i = 0; i < p_sys->i_es; i++ )
1262 if( p_sys->es[i]->p_dec &&
1263 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1264 EsUnselect( out, p_sys->es[i],
1265 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1268 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1270 for( i = 0; i < p_sys->i_es; i++ )
1272 if( p_sys->es[i]->p_dec &&
1273 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1274 EsUnselect( out, p_sys->es[i],
1275 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1278 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1280 for( i = 0; i < p_sys->i_es; i++ )
1282 if( p_sys->es[i]->p_dec &&
1283 p_sys->es[i]->fmt.i_cat == SPU_ES )
1284 EsUnselect( out, p_sys->es[i],
1285 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1290 for( i = 0; i < p_sys->i_es; i++ )
1292 if( es == p_sys->es[i] )
1294 EsOutSelect( out, es, VLC_TRUE );
1301 case ES_OUT_SET_PCR:
1302 case ES_OUT_SET_GROUP_PCR:
1304 es_out_pgrm_t *p_pgrm = NULL;
1308 if( i_query == ES_OUT_SET_PCR )
1310 p_pgrm = p_sys->p_pgrm;
1315 i_group = (int)va_arg( args, int );
1316 for( i = 0; i < p_sys->i_pgrm; i++ )
1318 if( p_sys->pgrm[i]->i_id == i_group )
1320 p_pgrm = p_sys->pgrm[i];
1325 if( p_pgrm == NULL )
1326 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1328 i_pcr = (int64_t)va_arg( args, int64_t );
1329 /* search program */
1330 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1331 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1332 (i_pcr + 11 ) * 9 / 100);
1336 case ES_OUT_RESET_PCR:
1337 for( i = 0; i < p_sys->i_pgrm; i++ )
1339 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1340 p_sys->pgrm[i]->clock.last_pts = 0;
1347 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1348 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1349 *pi_ts = input_ClockGetTS( p_sys->p_input,
1350 &p_sys->p_pgrm->clock,
1351 ( i_ts + 11 ) * 9 / 100 );
1354 return VLC_EGENERIC;
1356 case ES_OUT_GET_GROUP:
1357 pi = (int*) va_arg( args, int* );
1359 *pi = p_sys->p_pgrm->i_id;
1361 *pi = -1; /* FIXME */
1364 case ES_OUT_SET_GROUP:
1367 i = (int) va_arg( args, int );
1368 for( j = 0; j < p_sys->i_pgrm; j++ )
1370 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1371 if( p_pgrm->i_id == i )
1373 EsOutProgramSelect( out, p_pgrm );
1377 return VLC_EGENERIC;
1380 case ES_OUT_SET_FMT:
1382 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1383 * to update the p_extra data */
1385 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1386 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1387 if( es == NULL ) return VLC_EGENERIC;
1389 if( p_fmt->i_extra )
1391 es->fmt.i_extra = p_fmt->i_extra;
1392 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1393 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1395 if( !es->p_dec ) return VLC_SUCCESS;
1398 input_DecoderDelete( es->p_dec );
1399 es->p_dec = input_DecoderNew( p_sys->p_input,
1400 &es->fmt, VLC_FALSE );
1403 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1404 es->p_dec->fmt_in.p_extra =
1405 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1406 memcpy( es->p_dec->fmt_in.p_extra,
1407 p_fmt->p_extra, p_fmt->i_extra );
1414 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1418 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1419 i_date = (int64_t)va_arg( args, int64_t );
1421 if( !es || !es->p_dec )
1422 return VLC_EGENERIC;
1424 es->i_preroll_end = i_date;
1425 input_DecoderPreroll( es->p_dec, i_date );
1429 case ES_OUT_SET_GROUP_META:
1431 int i_group = (int)va_arg( args, int );
1432 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1434 EsOutProgramMeta( out, i_group, p_meta );
1437 case ES_OUT_DEL_GROUP:
1439 int i_group = (int)va_arg( args, int );
1441 return EsOutProgramDel( out, i_group );
1445 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1446 return VLC_EGENERIC;
1450 /****************************************************************************
1451 * LanguageGetName: try to expend iso639 into plain name
1452 ****************************************************************************/
1453 static char *LanguageGetName( const char *psz_code )
1455 const iso639_lang_t *pl;
1457 if( psz_code == NULL )
1459 return strdup( "" );
1462 if( strlen( psz_code ) == 2 )
1464 pl = GetLang_1( psz_code );
1466 else if( strlen( psz_code ) == 3 )
1468 pl = GetLang_2B( psz_code );
1469 if( !strcmp( pl->psz_iso639_1, "??" ) )
1471 pl = GetLang_2T( psz_code );
1476 return strdup( psz_code );
1479 if( !strcmp( pl->psz_iso639_1, "??" ) )
1481 return strdup( psz_code );
1485 if( *pl->psz_native_name )
1487 return strdup( pl->psz_native_name );
1489 return strdup( pl->psz_eng_name );
1493 /* Get a 2 char code */
1494 static char *LanguageGetCode( const char *psz_lang )
1496 const iso639_lang_t *pl;
1498 if( psz_lang == NULL || *psz_lang == '\0' )
1499 return strdup("??");
1501 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1503 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1504 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1505 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1506 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1507 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1511 if( pl->psz_iso639_1 != NULL )
1512 return strdup( pl->psz_iso639_1 );
1514 return strdup("??");
1517 static char **LanguageSplit( const char *psz_langs )
1524 if( psz_langs == NULL ) return NULL;
1526 psz_parser = psz_dup = strdup(psz_langs);
1528 while( psz_parser && *psz_parser )
1533 psz = strchr(psz_parser, ',' );
1534 if( psz ) *psz++ = '\0';
1536 psz_code = LanguageGetCode( psz_parser );
1537 if( strcmp( psz_code, "??" ) )
1539 TAB_APPEND( i_psz, ppsz, psz_code );
1547 TAB_APPEND( i_psz, ppsz, NULL );
1554 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1558 if( !ppsz_langs || !psz_lang ) return -1;
1560 for( i = 0; ppsz_langs[i]; i++ )
1561 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1566 /****************************************************************************
1568 * - add meta info to the playlist item
1569 ****************************************************************************/
1570 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1572 es_out_sys_t *p_sys = out->p_sys;
1573 input_thread_t *p_input = p_sys->p_input;
1574 es_format_t *fmt = &es->fmt;
1578 /* Add stream info */
1579 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1581 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1582 "%.4s", (char*)&fmt->i_codec );
1584 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1585 "%s", es->psz_language );
1587 /* Add information */
1588 switch( fmt->i_cat )
1591 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1592 _("Type"), _("Audio") );
1594 if( fmt->audio.i_channels > 0 )
1595 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1596 "%d", fmt->audio.i_channels );
1598 if( fmt->audio.i_rate > 0 )
1599 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1600 _("%d Hz"), fmt->audio.i_rate );
1602 if( fmt->audio.i_bitspersample > 0 )
1603 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1604 _("Bits per sample"), "%d",
1605 fmt->audio.i_bitspersample );
1607 if( fmt->i_bitrate > 0 )
1608 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1609 _("%d kb/s"), fmt->i_bitrate / 1000 );
1613 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1614 _("Type"), _("Video") );
1616 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1617 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1618 _("Resolution"), "%dx%d",
1619 fmt->video.i_width, fmt->video.i_height );
1621 if( fmt->video.i_visible_width > 0 &&
1622 fmt->video.i_visible_height > 0 )
1623 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1624 _("Display resolution"), "%dx%d",
1625 fmt->video.i_visible_width,
1626 fmt->video.i_visible_height);
1627 if( fmt->video.i_frame_rate > 0 &&
1628 fmt->video.i_frame_rate_base > 0 )
1630 div = lldiv( (float)fmt->video.i_frame_rate /
1631 fmt->video.i_frame_rate_base * 1000000,
1633 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1634 _("Frame rate"), I64Fd".%06u",
1635 div.quot, (unsigned int )div.rem );
1640 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1641 _("Type"), _("Subtitle") );