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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 */
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 p_sys->p_es_audio = NULL;
198 p_sys->p_es_video = NULL;
199 p_sys->p_es_sub = NULL;
201 p_sys->i_audio_delay= 0;
202 p_sys->i_spu_delay = 0;
207 /*****************************************************************************
209 *****************************************************************************/
210 void input_EsOutDelete( es_out_t *out )
212 es_out_sys_t *p_sys = out->p_sys;
215 for( i = 0; i < p_sys->i_es; i++ )
217 if( p_sys->es[i]->p_dec )
219 input_DecoderDelete( p_sys->es[i]->p_dec );
221 if( p_sys->es[i]->psz_language )
222 free( p_sys->es[i]->psz_language );
223 if( p_sys->es[i]->psz_language_code )
224 free( p_sys->es[i]->psz_language_code );
225 es_format_Clean( &p_sys->es[i]->fmt );
227 free( p_sys->es[i] );
229 if( p_sys->ppsz_audio_language )
231 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
232 free( p_sys->ppsz_audio_language[i] );
233 free( p_sys->ppsz_audio_language );
235 if( p_sys->ppsz_sub_language )
237 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
238 free( p_sys->ppsz_sub_language[i] );
239 free( p_sys->ppsz_sub_language );
245 for( i = 0; i < p_sys->i_pgrm; i++ )
247 if( p_sys->pgrm[i]->psz_now_playing )
248 free( p_sys->pgrm[i]->psz_now_playing );
249 free( p_sys->pgrm[i] );
258 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
263 /* Special HACK, -i_id is tha cat of the stream */
264 return (es_out_id_t*)((uint8_t*)NULL-i_id);
267 for( i = 0; i < out->p_sys->i_es; i++ )
269 if( out->p_sys->es[i]->i_id == i_id )
270 return out->p_sys->es[i];
275 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
277 es_out_sys_t *p_sys = out->p_sys;
280 for( i = 0; i < p_sys->i_es; i++ )
282 es_out_id_t *es = p_sys->es[i];
283 es->b_discontinuity = VLC_TRUE; /* signal discontinuity */
285 /* Send a dummy block to let decoder know that
286 * there is a discontinuity */
287 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
289 input_DecoderDiscontinuity( es->p_dec );
294 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
296 es_out_sys_t *p_sys = out->p_sys;
298 if( i_cat == AUDIO_ES )
299 p_sys->i_audio_delay = i_delay;
300 else if( i_cat == SPU_ES )
301 p_sys->i_spu_delay = i_delay;
304 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
306 es_out_sys_t *p_sys = out->p_sys;
309 for( i = 0; i < p_sys->i_es; i++ )
311 es_out_id_t *es = p_sys->es[i];
313 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
319 /*****************************************************************************
321 *****************************************************************************/
322 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
323 vlc_bool_t b_delete )
325 es_out_sys_t *p_sys = out->p_sys;
326 input_thread_t *p_input = p_sys->p_input;
327 vlc_value_t val, text;
331 if( es->fmt.i_cat == AUDIO_ES )
332 psz_var = "audio-es";
333 else if( es->fmt.i_cat == VIDEO_ES )
334 psz_var = "video-es";
335 else if( es->fmt.i_cat == SPU_ES )
342 val.i_int = es->i_id;
343 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
344 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
348 /* Get the number of ES already added */
349 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
354 /* First one, we need to add the "Disable" choice */
355 val2.i_int = -1; text.psz_string = _("Disable");
356 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
360 /* Take care of the ES description */
361 if( es->fmt.psz_description && *es->fmt.psz_description )
363 if( es->psz_language && *es->psz_language )
365 text.psz_string = malloc( strlen( es->fmt.psz_description) + strlen( es->psz_language ) + 10 );
366 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description, es->psz_language );
368 else text.psz_string = strdup( es->fmt.psz_description );
372 if( es->psz_language && *es->psz_language )
375 text.psz_string = malloc( strlen( _("Track %i") )+ strlen( es->psz_language ) + 30 );
376 asprintf( &temp, _("Track %i"), val.i_int );
377 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
382 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
383 sprintf( text.psz_string, _("Track %i"), val.i_int );
387 val.i_int = es->i_id;
388 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
390 free( text.psz_string );
392 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
395 /* EsOutProgramSelect:
396 * Select a program and update the object variable
398 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
400 es_out_sys_t *p_sys = out->p_sys;
401 input_thread_t *p_input = p_sys->p_input;
405 if( p_sys->p_pgrm == p_pgrm )
406 return; /* Nothing to do */
410 es_out_pgrm_t *old = p_sys->p_pgrm;
411 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
413 for( i = 0; i < p_sys->i_es; i++ )
415 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
416 p_sys->i_mode != ES_OUT_MODE_ALL )
417 EsUnselect( out, p_sys->es[i], VLC_TRUE );
420 p_sys->p_es_audio = NULL;
421 p_sys->p_es_sub = NULL;
422 p_sys->p_es_video = NULL;
425 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
427 /* Mark it selected */
428 p_pgrm->b_selected = VLC_TRUE;
430 /* Switch master stream */
431 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
433 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
435 p_pgrm->clock.b_master = VLC_TRUE;
436 p_sys->p_pgrm = p_pgrm;
438 /* Update "program" */
439 val.i_int = p_pgrm->i_id;
440 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
443 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
444 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
445 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
446 for( i = 0; i < p_sys->i_es; i++ )
448 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
449 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
450 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
453 /* Update now playing if defined per program */
454 if( p_pgrm->psz_now_playing )
456 char *psz_cat = malloc( strlen(_("Program")) + 10 );
458 sprintf( psz_cat, "%s %d", _("Program"), p_pgrm->i_id );
459 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
460 VLC_META_NOW_PLAYING, "%s", p_pgrm->psz_now_playing );
465 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
471 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
473 es_out_sys_t *p_sys = out->p_sys;
474 input_thread_t *p_input = p_sys->p_input;
477 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
480 p_pgrm->i_id = i_group;
482 p_pgrm->b_selected = VLC_FALSE;
483 p_pgrm->psz_now_playing = NULL;
484 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->input.i_cr_average );
487 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
489 /* Update "program" variable */
491 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
493 if( i_group == var_GetInteger( p_input, "program" ) )
495 EsOutProgramSelect( out, p_pgrm );
499 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
507 static int EsOutProgramDel( es_out_t *out, int i_group )
509 es_out_sys_t *p_sys = out->p_sys;
510 input_thread_t *p_input = p_sys->p_input;
511 es_out_pgrm_t *p_pgrm = NULL;
515 for( i = 0; i < p_sys->i_pgrm; i++ )
517 if( p_sys->pgrm[i]->i_id == i_group )
519 p_pgrm = p_sys->pgrm[i];
529 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
530 i_group, p_pgrm->i_es );
534 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
536 /* If program is selected we need to unselect it */
537 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = 0;
539 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
542 /* Update "program" variable */
544 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
546 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
553 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
555 es_out_sys_t *p_sys = out->p_sys;
556 es_out_pgrm_t *p_pgrm = NULL;
557 input_thread_t *p_input = p_sys->p_input;
558 char *psz_cat = malloc( strlen(_("Program")) + 10 );
559 char *psz_name = NULL;
560 char *psz_now_playing = NULL;
561 char *psz_provider = NULL;
564 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
565 sprintf( psz_cat, "%s %d", _("Program"), i_group );
567 for( i = 0; i < p_meta->i_meta; i++ )
569 msg_Dbg( p_input, " - %s = %s", p_meta->name[i], p_meta->value[i] );
571 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
572 _(p_meta->name[i]), "%s", p_meta->value[i] );
573 if( !strcasecmp( p_meta->name[i], "Name" ) )
574 psz_name = p_meta->value[i];
575 else if( !strcasecmp( p_meta->name[i], "Provider" ) )
576 psz_provider = p_meta->value[i];
577 else if( !strcasecmp( p_meta->name[i], VLC_META_NOW_PLAYING ) )
578 psz_now_playing = p_meta->value[i];
581 if( !psz_name && !psz_now_playing )
587 for( i = 0; i < p_sys->i_pgrm; i++ )
589 if( p_sys->pgrm[i]->i_id == i_group )
591 p_pgrm = p_sys->pgrm[i];
597 p_pgrm = EsOutProgramAdd( out, i_group );
599 /* Update the description text of the program */
600 if( psz_name && *psz_name )
605 /* ugly but it works */
607 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
609 if( psz_provider && *psz_provider )
611 asprintf( &text.psz_string, "%s [%s]", psz_name, psz_provider );
612 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
613 free( text.psz_string );
617 text.psz_string = psz_name;
618 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
621 if( psz_now_playing )
623 p_pgrm->psz_now_playing = strdup(psz_now_playing);
625 if( p_sys->p_pgrm == p_pgrm )
627 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
628 VLC_META_NOW_PLAYING, "%s", psz_now_playing );
637 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
639 es_out_sys_t *p_sys = out->p_sys;
640 input_thread_t *p_input = p_sys->p_input;
642 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
643 es_out_pgrm_t *p_pgrm = NULL;
646 if( fmt->i_group < 0 )
648 msg_Err( p_input, "invalid group number" );
652 /* Search the program */
653 for( i = 0; i < p_sys->i_pgrm; i++ )
655 if( fmt->i_group == p_sys->pgrm[i]->i_id )
657 p_pgrm = p_sys->pgrm[i];
663 /* Create a new one */
664 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
667 /* Increase ref count for program */
672 fmt->i_id = out->p_sys->i_id;
673 es->i_id = fmt->i_id;
675 es_format_Copy( &es->fmt, fmt );
676 es->i_preroll_end = -1;
677 es->b_discontinuity = VLC_FALSE;
682 es->i_channel = p_sys->i_audio;
686 es->i_channel = p_sys->i_video;
687 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
688 vlc_ureduce( &es->fmt.video.i_frame_rate,
689 &es->fmt.video.i_frame_rate_base,
690 fmt->video.i_frame_rate,
691 fmt->video.i_frame_rate_base, 0 );
695 es->i_channel = p_sys->i_sub;
702 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
703 es->psz_language_code = LanguageGetCode( fmt->psz_language );
706 if( es->p_pgrm == p_sys->p_pgrm )
707 EsOutESVarUpdate( out, es, VLC_FALSE );
709 /* Select it if needed */
710 EsOutSelect( out, es, VLC_FALSE );
713 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
714 p_sys->i_id++; /* always incremented */
728 EsOutAddInfo( out, es );
733 static void EsSelect( es_out_t *out, es_out_id_t *es )
735 es_out_sys_t *p_sys = out->p_sys;
736 input_thread_t *p_input = p_sys->p_input;
742 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
746 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
748 if( !var_GetBool( p_input, "video" ) ||
749 ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
751 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
756 else if( es->fmt.i_cat == AUDIO_ES )
758 var_Get( p_input, "audio", &val );
759 if( !var_GetBool( p_input, "audio" ) ||
760 ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
762 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
768 es->i_preroll_end = -1;
769 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
770 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
773 if( es->fmt.i_cat == VIDEO_ES )
774 psz_var = "video-es";
775 else if( es->fmt.i_cat == AUDIO_ES )
776 psz_var = "audio-es";
777 else if( es->fmt.i_cat == SPU_ES )
782 /* Mark it as selected */
783 val.i_int = es->i_id;
784 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
787 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
790 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
792 es_out_sys_t *p_sys = out->p_sys;
793 input_thread_t *p_input = p_sys->p_input;
797 if( es->p_dec == NULL )
799 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
803 input_DecoderDelete( es->p_dec );
810 if( es->p_dec == NULL )
812 if( es->fmt.i_cat == VIDEO_ES )
813 psz_var = "video-es";
814 else if( es->fmt.i_cat == AUDIO_ES )
815 psz_var = "audio-es";
816 else if( es->fmt.i_cat == SPU_ES )
821 /* Mark it as selected */
823 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
825 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
829 * Select an ES given the current mode
830 * XXX: you need to take a the lock before (stream.stream_lock)
832 * \param out The es_out structure
833 * \param es es_out_id structure
837 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
839 es_out_sys_t *p_sys = out->p_sys;
841 int i_cat = es->fmt.i_cat;
843 if( !p_sys->b_active ||
844 ( !b_force && es->fmt.i_priority < 0 ) )
849 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
854 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
858 var_Get( p_sys->p_input, "programs", &val );
859 for ( i = 0; i < val.p_list->i_count; i++ )
861 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
868 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
870 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
874 if( es->p_pgrm != p_sys->p_pgrm )
877 if( i_cat == AUDIO_ES )
879 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
880 es->psz_language_code );
882 if( p_sys->p_es_audio &&
883 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
885 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
886 p_sys->p_es_audio->psz_language_code );
888 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
890 i_wanted = es->i_channel;
894 /* Select audio if (no audio selected yet)
895 * - no audio-language
896 * - no audio code for the ES
897 * - audio code in the requested list */
899 !strcmp( es->psz_language_code, "??" ) ||
900 !p_sys->ppsz_audio_language )
901 i_wanted = es->i_channel;
904 if( p_sys->i_audio_last >= 0 )
905 i_wanted = p_sys->i_audio_last;
907 else if( i_cat == SPU_ES )
909 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
910 es->psz_language_code );
912 if( p_sys->p_es_sub &&
913 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
915 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
916 p_sys->p_es_sub->psz_language_code );
918 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
919 idx1, es->psz_language_code, idx2,
920 p_sys->p_es_sub->psz_language_code );
922 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
924 /* We found a SPU that matches our language request */
925 i_wanted = es->i_channel;
929 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
930 idx1, es->psz_language_code );
932 i_wanted = es->i_channel;
934 if( p_sys->i_sub_last >= 0 )
935 i_wanted = p_sys->i_sub_last;
937 else if( i_cat == VIDEO_ES )
939 i_wanted = es->i_channel;
942 if( i_wanted == es->i_channel && es->p_dec == NULL )
946 /* FIXME TODO handle priority here */
949 if( i_cat == AUDIO_ES )
951 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
953 p_sys->p_es_audio != es &&
954 p_sys->p_es_audio->p_dec )
956 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
958 p_sys->p_es_audio = es;
960 else if( i_cat == SPU_ES )
962 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
964 p_sys->p_es_sub != es &&
965 p_sys->p_es_sub->p_dec )
967 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
969 p_sys->p_es_sub = es;
971 else if( i_cat == VIDEO_ES )
973 p_sys->p_es_video = es;
979 * Send a block for the given es_out
981 * \param out the es_out to send from
982 * \param es the es_out_id
983 * \param p_block the data block to send
985 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
987 es_out_sys_t *p_sys = out->p_sys;
988 input_thread_t *p_input = p_sys->p_input;
989 es_out_pgrm_t *p_pgrm = es->p_pgrm;
992 if( es->fmt.i_cat == AUDIO_ES )
993 i_delay = p_sys->i_audio_delay;
994 else if( es->fmt.i_cat == SPU_ES )
995 i_delay = p_sys->i_spu_delay;
999 /* Mark preroll blocks */
1000 if( es->i_preroll_end >= 0 )
1002 int64_t i_date = p_block->i_pts;
1004 i_date = p_block->i_dts;
1006 if( i_date < es->i_preroll_end )
1007 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1009 es->i_preroll_end = -1;
1012 /* +11 -> avoid null value with non null dts/pts */
1013 if( p_block->i_dts > 0 )
1016 input_ClockGetTS( p_input, &p_pgrm->clock,
1017 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
1019 if( p_block->i_pts > 0 )
1022 input_ClockGetTS( p_input, &p_pgrm->clock,
1023 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
1025 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1027 mtime_t current_date = mdate();
1029 || p_block->i_pts > current_date + 10000000
1030 || current_date > p_block->i_pts )
1032 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1033 * for teletext streams. */
1034 p_block->i_pts = current_date + 400000
1035 + p_input->i_pts_delay + i_delay;
1039 p_block->i_rate = p_input->i_rate;
1041 /* TODO handle mute */
1042 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
1043 p_input->i_rate == INPUT_RATE_DEFAULT ) )
1045 input_DecoderDecode( es->p_dec, p_block );
1049 block_Release( p_block );
1055 /*****************************************************************************
1057 *****************************************************************************/
1058 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1060 es_out_sys_t *p_sys = out->p_sys;
1062 /* We don't try to reselect */
1064 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1066 if( es->p_pgrm == p_sys->p_pgrm )
1067 EsOutESVarUpdate( out, es, VLC_TRUE );
1069 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1072 if( es->p_pgrm->i_es == 0 )
1074 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1077 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1078 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1079 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1081 switch( es->fmt.i_cat )
1094 if( es->psz_language )
1095 free( es->psz_language );
1096 if( es->psz_language_code )
1097 free( es->psz_language_code );
1099 es_format_Clean( &es->fmt );
1105 * Control query handler
1107 * \param out the es_out to control
1108 * \param i_query A es_out query as defined in include/ninput.h
1109 * \param args a variable list of arguments for the query
1110 * \return VLC_SUCCESS or an error code
1112 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1114 es_out_sys_t *p_sys = out->p_sys;
1122 case ES_OUT_SET_ES_STATE:
1123 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1124 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1125 if( b && es->p_dec == NULL )
1127 EsSelect( out, es );
1128 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1130 else if( !b && es->p_dec )
1132 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1137 case ES_OUT_GET_ES_STATE:
1138 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1139 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1141 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1144 case ES_OUT_SET_ACTIVE:
1146 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1147 p_sys->b_active = b;
1150 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1154 case ES_OUT_GET_ACTIVE:
1155 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1156 *pb = p_sys->b_active;
1159 case ES_OUT_SET_MODE:
1160 i = (int) va_arg( args, int );
1161 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1162 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1166 /* Reapply policy mode */
1167 for( i = 0; i < p_sys->i_es; i++ )
1169 if( p_sys->es[i]->p_dec )
1171 EsUnselect( out, p_sys->es[i],
1172 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1175 for( i = 0; i < p_sys->i_es; i++ )
1177 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1181 return VLC_EGENERIC;
1183 case ES_OUT_GET_MODE:
1184 pi = (int*) va_arg( args, int* );
1185 *pi = p_sys->i_mode;
1189 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1190 /* Special case NULL, NULL+i_cat */
1193 for( i = 0; i < p_sys->i_es; i++ )
1195 if( p_sys->es[i]->p_dec )
1196 EsUnselect( out, p_sys->es[i],
1197 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1200 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1202 for( i = 0; i < p_sys->i_es; i++ )
1204 if( p_sys->es[i]->p_dec &&
1205 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1206 EsUnselect( out, p_sys->es[i],
1207 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1210 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1212 for( i = 0; i < p_sys->i_es; i++ )
1214 if( p_sys->es[i]->p_dec &&
1215 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1216 EsUnselect( out, p_sys->es[i],
1217 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1220 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1222 for( i = 0; i < p_sys->i_es; i++ )
1224 if( p_sys->es[i]->p_dec &&
1225 p_sys->es[i]->fmt.i_cat == SPU_ES )
1226 EsUnselect( out, p_sys->es[i],
1227 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1232 for( i = 0; i < p_sys->i_es; i++ )
1234 if( es == p_sys->es[i] )
1236 EsOutSelect( out, es, VLC_TRUE );
1243 case ES_OUT_SET_PCR:
1244 case ES_OUT_SET_GROUP_PCR:
1246 es_out_pgrm_t *p_pgrm = NULL;
1250 if( i_query == ES_OUT_SET_PCR )
1252 p_pgrm = p_sys->p_pgrm;
1257 i_group = (int)va_arg( args, int );
1258 for( i = 0; i < p_sys->i_pgrm; i++ )
1260 if( p_sys->pgrm[i]->i_id == i_group )
1262 p_pgrm = p_sys->pgrm[i];
1267 if( p_pgrm == NULL )
1268 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1270 i_pcr = (int64_t)va_arg( args, int64_t );
1271 /* search program */
1272 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1273 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1274 (i_pcr + 11 ) * 9 / 100);
1278 case ES_OUT_RESET_PCR:
1279 for( i = 0; i < p_sys->i_pgrm; i++ )
1281 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1282 p_sys->pgrm[i]->clock.last_pts = 0;
1289 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1290 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1291 *pi_ts = input_ClockGetTS( p_sys->p_input,
1292 &p_sys->p_pgrm->clock,
1293 ( i_ts + 11 ) * 9 / 100 );
1296 return VLC_EGENERIC;
1298 case ES_OUT_GET_GROUP:
1299 pi = (int*) va_arg( args, int* );
1301 *pi = p_sys->p_pgrm->i_id;
1303 *pi = -1; /* FIXME */
1306 case ES_OUT_SET_GROUP:
1309 i = (int) va_arg( args, int );
1310 for( j = 0; j < p_sys->i_pgrm; j++ )
1312 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1313 if( p_pgrm->i_id == i )
1315 EsOutProgramSelect( out, p_pgrm );
1319 return VLC_EGENERIC;
1322 case ES_OUT_SET_FMT:
1324 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1325 * to update the p_extra data */
1327 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1328 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1329 if( es == NULL ) return VLC_EGENERIC;
1331 if( p_fmt->i_extra )
1333 es->fmt.i_extra = p_fmt->i_extra;
1334 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1335 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1337 if( !es->p_dec ) return VLC_SUCCESS;
1340 input_DecoderDelete( es->p_dec );
1341 es->p_dec = input_DecoderNew( p_sys->p_input,
1342 &es->fmt, VLC_FALSE );
1345 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1346 es->p_dec->fmt_in.p_extra =
1347 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1348 memcpy( es->p_dec->fmt_in.p_extra,
1349 p_fmt->p_extra, p_fmt->i_extra );
1356 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1360 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1361 i_date = (int64_t)va_arg( args, int64_t );
1363 if( !es || !es->p_dec )
1364 return VLC_EGENERIC;
1366 es->i_preroll_end = i_date;
1367 input_DecoderPreroll( es->p_dec, i_date );
1371 case ES_OUT_SET_GROUP_META:
1373 int i_group = (int)va_arg( args, int );
1374 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1376 EsOutProgramMeta( out, i_group, p_meta );
1379 case ES_OUT_DEL_GROUP:
1381 int i_group = (int)va_arg( args, int );
1383 return EsOutProgramDel( out, i_group );
1387 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1388 return VLC_EGENERIC;
1392 /****************************************************************************
1393 * LanguageGetName: try to expend iso639 into plain name
1394 ****************************************************************************/
1395 static char *LanguageGetName( const char *psz_code )
1397 const iso639_lang_t *pl;
1399 if( psz_code == NULL )
1401 return strdup( "" );
1404 if( strlen( psz_code ) == 2 )
1406 pl = GetLang_1( psz_code );
1408 else if( strlen( psz_code ) == 3 )
1410 pl = GetLang_2B( psz_code );
1411 if( !strcmp( pl->psz_iso639_1, "??" ) )
1413 pl = GetLang_2T( psz_code );
1418 return strdup( psz_code );
1421 if( !strcmp( pl->psz_iso639_1, "??" ) )
1423 return strdup( psz_code );
1427 if( *pl->psz_native_name )
1429 return strdup( pl->psz_native_name );
1431 return strdup( pl->psz_eng_name );
1435 /* Get a 2 char code */
1436 static char *LanguageGetCode( const char *psz_lang )
1438 const iso639_lang_t *pl;
1440 if( psz_lang == NULL || *psz_lang == '\0' )
1441 return strdup("??");
1443 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1445 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1446 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1447 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1448 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1449 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1453 if( pl->psz_iso639_1 != NULL )
1454 return strdup( pl->psz_iso639_1 );
1456 return strdup("??");
1459 static char **LanguageSplit( const char *psz_langs )
1466 if( psz_langs == NULL ) return NULL;
1468 psz_parser = psz_dup = strdup(psz_langs);
1470 while( psz_parser && *psz_parser )
1475 psz = strchr(psz_parser, ',' );
1476 if( psz ) *psz++ = '\0';
1478 psz_code = LanguageGetCode( psz_parser );
1479 if( strcmp( psz_code, "??" ) )
1481 TAB_APPEND( i_psz, ppsz, psz_code );
1489 TAB_APPEND( i_psz, ppsz, NULL );
1496 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1500 if( !ppsz_langs || !psz_lang ) return -1;
1502 for( i = 0; ppsz_langs[i]; i++ )
1503 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1508 /****************************************************************************
1510 * - add meta info to the playlist item
1511 ****************************************************************************/
1512 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1514 es_out_sys_t *p_sys = out->p_sys;
1515 input_thread_t *p_input = p_sys->p_input;
1516 es_format_t *fmt = &es->fmt;
1519 /* Add stream info */
1520 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1522 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1523 "%.4s", (char*)&fmt->i_codec );
1525 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1526 "%s", es->psz_language );
1528 /* Add information */
1529 switch( fmt->i_cat )
1532 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1533 _("Type"), _("Audio") );
1535 if( fmt->audio.i_channels > 0 )
1536 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1537 "%d", fmt->audio.i_channels );
1539 if( fmt->audio.i_rate > 0 )
1540 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1541 _("%d Hz"), fmt->audio.i_rate );
1543 if( fmt->audio.i_bitspersample > 0 )
1544 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1545 _("Bits per sample"), "%d",
1546 fmt->audio.i_bitspersample );
1548 if( fmt->i_bitrate > 0 )
1549 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1550 _("%d kb/s"), fmt->i_bitrate / 1000 );
1554 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1555 _("Type"), _("Video") );
1557 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1558 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1559 _("Resolution"), "%dx%d",
1560 fmt->video.i_width, fmt->video.i_height );
1562 if( fmt->video.i_visible_width > 0 &&
1563 fmt->video.i_visible_height > 0 )
1564 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1565 _("Display resolution"), "%dx%d",
1566 fmt->video.i_visible_width,
1567 fmt->video.i_visible_height);
1568 if( fmt->video.i_frame_rate > 0 &&
1569 fmt->video.i_frame_rate_base > 0 )
1570 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1571 _("Frame rate"), "%f",
1572 (float)fmt->video.i_frame_rate /
1573 fmt->video.i_frame_rate_base );
1577 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1578 _("Type"), _("Subtitle") );