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 *****************************************************************************/
33 #include <vlc_input.h>
34 #include <vlc_es_out.h>
35 #include <vlc_block.h>
37 #include "input_internal.h"
39 #include "vlc_playlist.h"
41 /* FIXME we should find a better way than including that */
42 #include "../text/iso-639_def.h"
44 /*****************************************************************************
46 *****************************************************************************/
52 /* Number of es for this pgrm */
55 vlc_bool_t b_selected;
57 /* Clock for this program */
60 char *psz_now_playing;
68 es_out_pgrm_t *p_pgrm;
70 /* Signal a discontinuity in the timeline for every PID */
71 vlc_bool_t b_discontinuity;
74 int64_t i_preroll_end;
76 /* Channel in the track type */
80 char *psz_language_code;
86 input_thread_t *p_input;
91 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
92 es_out_pgrm_t *p_pgrm; /* Master program */
109 int i_audio_last, i_audio_id;
110 int i_sub_last, i_sub_id;
111 char **ppsz_audio_language;
112 char **ppsz_sub_language;
114 /* current main es */
115 es_out_id_t *p_es_audio;
116 es_out_id_t *p_es_video;
117 es_out_id_t *p_es_sub;
120 int64_t i_audio_delay;
124 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
125 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
126 static void EsOutDel ( es_out_t *, es_out_id_t * );
127 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
128 static int EsOutControl( es_out_t *, int i_query, va_list );
130 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
132 static void EsSelect( es_out_t *out, es_out_id_t *es );
133 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
134 static char *LanguageGetName( const char *psz_code );
135 static char *LanguageGetCode( const char *psz_lang );
136 static char **LanguageSplit( const char *psz_langs );
137 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
139 /*****************************************************************************
141 *****************************************************************************/
142 es_out_t *input_EsOutNew( input_thread_t *p_input )
144 es_out_t *out = malloc( sizeof( es_out_t ) );
145 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
149 out->pf_add = EsOutAdd;
150 out->pf_send = EsOutSend;
151 out->pf_del = EsOutDel;
152 out->pf_control = EsOutControl;
155 p_sys->p_input = p_input;
157 p_sys->b_active = VLC_FALSE;
158 p_sys->i_mode = ES_OUT_MODE_AUTO;
163 p_sys->p_pgrm = NULL;
174 var_Get( p_input, "audio-track", &val );
175 p_sys->i_audio_last = val.i_int;
177 var_Get( p_input, "sub-track", &val );
178 p_sys->i_sub_last = val.i_int;
180 if( !p_input->b_preparsing )
182 var_Get( p_input, "audio-language", &val );
183 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
184 if( p_sys->ppsz_audio_language )
186 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
187 msg_Dbg( p_input, "selected audio language[%d] %s",
188 i, p_sys->ppsz_audio_language[i] );
190 if( val.psz_string ) free( val.psz_string );
192 var_Get( p_input, "sub-language", &val );
193 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
194 if( p_sys->ppsz_sub_language )
196 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
197 msg_Dbg( p_input, "selected subtitle language[%d] %s",
198 i, p_sys->ppsz_sub_language[i] );
200 if( val.psz_string ) free( val.psz_string );
204 p_sys->ppsz_sub_language = NULL;
205 p_sys->ppsz_audio_language = NULL;
208 var_Get( p_input, "audio-track-id", &val );
209 p_sys->i_audio_id = val.i_int;
211 var_Get( p_input, "sub-track-id", &val );
212 p_sys->i_sub_id = val.i_int;
214 p_sys->p_es_audio = NULL;
215 p_sys->p_es_video = NULL;
216 p_sys->p_es_sub = NULL;
218 p_sys->i_audio_delay= 0;
219 p_sys->i_spu_delay = 0;
224 /*****************************************************************************
226 *****************************************************************************/
227 void input_EsOutDelete( es_out_t *out )
229 es_out_sys_t *p_sys = out->p_sys;
232 for( i = 0; i < p_sys->i_es; i++ )
234 if( p_sys->es[i]->p_dec )
236 input_DecoderDelete( p_sys->es[i]->p_dec );
238 if( p_sys->es[i]->psz_language )
239 free( p_sys->es[i]->psz_language );
240 if( p_sys->es[i]->psz_language_code )
241 free( p_sys->es[i]->psz_language_code );
242 es_format_Clean( &p_sys->es[i]->fmt );
244 free( p_sys->es[i] );
246 if( p_sys->ppsz_audio_language )
248 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
249 free( p_sys->ppsz_audio_language[i] );
250 free( p_sys->ppsz_audio_language );
252 if( p_sys->ppsz_sub_language )
254 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
255 free( p_sys->ppsz_sub_language[i] );
256 free( p_sys->ppsz_sub_language );
262 for( i = 0; i < p_sys->i_pgrm; i++ )
264 if( p_sys->pgrm[i]->psz_now_playing )
265 free( p_sys->pgrm[i]->psz_now_playing );
266 free( p_sys->pgrm[i] );
275 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
280 /* Special HACK, -i_id is tha cat of the stream */
281 return (es_out_id_t*)((uint8_t*)NULL-i_id);
284 for( i = 0; i < out->p_sys->i_es; i++ )
286 if( out->p_sys->es[i]->i_id == i_id )
287 return out->p_sys->es[i];
292 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
294 es_out_sys_t *p_sys = out->p_sys;
297 for( i = 0; i < p_sys->i_es; i++ )
299 es_out_id_t *es = p_sys->es[i];
300 es->b_discontinuity = VLC_TRUE; /* signal discontinuity */
302 /* Send a dummy block to let decoder know that
303 * there is a discontinuity */
304 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
306 input_DecoderDiscontinuity( es->p_dec );
311 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
313 es_out_sys_t *p_sys = out->p_sys;
315 if( i_cat == AUDIO_ES )
316 p_sys->i_audio_delay = i_delay;
317 else if( i_cat == SPU_ES )
318 p_sys->i_spu_delay = i_delay;
321 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
323 es_out_sys_t *p_sys = out->p_sys;
326 for( i = 0; i < p_sys->i_es; i++ )
328 es_out_id_t *es = p_sys->es[i];
330 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
336 /*****************************************************************************
338 *****************************************************************************/
339 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
340 vlc_bool_t b_delete )
342 es_out_sys_t *p_sys = out->p_sys;
343 input_thread_t *p_input = p_sys->p_input;
344 vlc_value_t val, text;
348 if( es->fmt.i_cat == AUDIO_ES )
349 psz_var = "audio-es";
350 else if( es->fmt.i_cat == VIDEO_ES )
351 psz_var = "video-es";
352 else if( es->fmt.i_cat == SPU_ES )
359 val.i_int = es->i_id;
360 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
361 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
365 /* Get the number of ES already added */
366 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
371 /* First one, we need to add the "Disable" choice */
372 val2.i_int = -1; text.psz_string = _("Disable");
373 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
377 /* Take care of the ES description */
378 if( es->fmt.psz_description && *es->fmt.psz_description )
380 if( es->psz_language && *es->psz_language )
382 text.psz_string = malloc( strlen( es->fmt.psz_description) +
383 strlen( es->psz_language ) + 10 );
384 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description,
387 else text.psz_string = strdup( es->fmt.psz_description );
391 if( es->psz_language && *es->psz_language )
394 text.psz_string = malloc( strlen( _("Track %i") )+
395 strlen( es->psz_language ) + 30 );
396 asprintf( &temp, _("Track %i"), val.i_int );
397 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
402 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
403 sprintf( text.psz_string, _("Track %i"), val.i_int );
407 val.i_int = es->i_id;
408 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
410 free( text.psz_string );
412 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
415 /* EsOutProgramSelect:
416 * Select a program and update the object variable
418 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
420 es_out_sys_t *p_sys = out->p_sys;
421 input_thread_t *p_input = p_sys->p_input;
425 if( p_sys->p_pgrm == p_pgrm )
426 return; /* Nothing to do */
430 es_out_pgrm_t *old = p_sys->p_pgrm;
431 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
433 for( i = 0; i < p_sys->i_es; i++ )
435 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
436 p_sys->i_mode != ES_OUT_MODE_ALL )
437 EsUnselect( out, p_sys->es[i], VLC_TRUE );
440 p_sys->p_es_audio = NULL;
441 p_sys->p_es_sub = NULL;
442 p_sys->p_es_video = NULL;
445 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
447 /* Mark it selected */
448 p_pgrm->b_selected = VLC_TRUE;
450 /* Switch master stream */
451 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
453 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
455 p_pgrm->clock.b_master = VLC_TRUE;
456 p_sys->p_pgrm = p_pgrm;
458 /* Update "program" */
459 val.i_int = p_pgrm->i_id;
460 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
463 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
464 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
465 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
466 for( i = 0; i < p_sys->i_es; i++ )
468 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
469 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
470 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
473 /* Update now playing if defined per program */
474 if( p_pgrm->psz_now_playing )
476 char *psz_cat = malloc( strlen(_("Program")) + 10 );
478 sprintf( psz_cat, "%s %d", _("Program"), p_pgrm->i_id );
479 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT),
480 _(VLC_META_NOW_PLAYING), "%s", p_pgrm->psz_now_playing );
485 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
491 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
493 es_out_sys_t *p_sys = out->p_sys;
494 input_thread_t *p_input = p_sys->p_input;
497 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
500 p_pgrm->i_id = i_group;
502 p_pgrm->b_selected = VLC_FALSE;
503 p_pgrm->psz_now_playing = NULL;
504 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average );
507 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
509 /* Update "program" variable */
511 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
513 if( i_group == var_GetInteger( p_input, "program" ) )
515 EsOutProgramSelect( out, p_pgrm );
519 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
527 static int EsOutProgramDel( es_out_t *out, int i_group )
529 es_out_sys_t *p_sys = out->p_sys;
530 input_thread_t *p_input = p_sys->p_input;
531 es_out_pgrm_t *p_pgrm = NULL;
535 for( i = 0; i < p_sys->i_pgrm; i++ )
537 if( p_sys->pgrm[i]->i_id == i_group )
539 p_pgrm = p_sys->pgrm[i];
549 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
550 i_group, p_pgrm->i_es );
554 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
556 /* If program is selected we need to unselect it */
557 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = 0;
559 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
562 /* Update "program" variable */
564 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
566 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
573 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
575 es_out_sys_t *p_sys = out->p_sys;
576 es_out_pgrm_t *p_pgrm = NULL;
577 input_thread_t *p_input = p_sys->p_input;
578 char *psz_cat = malloc( strlen(_("Program")) + 10 );
579 char *psz_title = NULL;
580 char *psz_now_playing = NULL;
581 char *psz_provider = NULL;
584 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
585 sprintf( psz_cat, "%s %d", _("Program"), i_group );
587 if( p_meta->psz_title ) psz_title = p_meta->psz_title;
588 if( p_meta->psz_publisher ) psz_provider = p_meta->psz_publisher;
589 if( p_meta->psz_nowplaying ) psz_now_playing = p_meta->psz_nowplaying;
591 if( !psz_title && !psz_now_playing )
597 for( i = 0; i < p_sys->i_pgrm; i++ )
599 if( p_sys->pgrm[i]->i_id == i_group )
601 p_pgrm = p_sys->pgrm[i];
606 if( p_pgrm == NULL ) {
608 msg_Dbg( p_input, "Trying to add meta for non-existing program" );
612 /* Update the description text of the program */
613 if( psz_title && *psz_title )
618 /* ugly but it works */
620 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
622 if( psz_provider && *psz_provider )
624 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
625 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
626 free( text.psz_string );
630 text.psz_string = psz_title;
631 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
634 if( psz_now_playing )
636 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
637 p_pgrm->psz_now_playing = strdup(psz_now_playing);
639 if( p_sys->p_pgrm == p_pgrm )
641 vlc_meta_SetNowPlaying( p_input->p->input.p_item->p_meta,
651 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
653 es_out_sys_t *p_sys = out->p_sys;
654 input_thread_t *p_input = p_sys->p_input;
656 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
657 es_out_pgrm_t *p_pgrm = NULL;
660 if( fmt->i_group < 0 )
662 msg_Err( p_input, "invalid group number" );
666 /* Search the program */
667 for( i = 0; i < p_sys->i_pgrm; i++ )
669 if( fmt->i_group == p_sys->pgrm[i]->i_id )
671 p_pgrm = p_sys->pgrm[i];
677 /* Create a new one */
678 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
681 /* Increase ref count for program */
686 fmt->i_id = out->p_sys->i_id;
687 es->i_id = fmt->i_id;
689 es_format_Copy( &es->fmt, fmt );
690 es->i_preroll_end = -1;
691 es->b_discontinuity = VLC_FALSE;
696 es->i_channel = p_sys->i_audio;
700 es->i_channel = p_sys->i_video;
701 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
702 vlc_ureduce( &es->fmt.video.i_frame_rate,
703 &es->fmt.video.i_frame_rate_base,
704 fmt->video.i_frame_rate,
705 fmt->video.i_frame_rate_base, 0 );
709 es->i_channel = p_sys->i_sub;
716 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
717 es->psz_language_code = LanguageGetCode( fmt->psz_language );
720 if( es->p_pgrm == p_sys->p_pgrm )
721 EsOutESVarUpdate( out, es, VLC_FALSE );
723 /* Select it if needed */
724 EsOutSelect( out, es, VLC_FALSE );
727 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
728 p_sys->i_id++; /* always incremented */
742 EsOutAddInfo( out, es );
747 static void EsSelect( es_out_t *out, es_out_id_t *es )
749 es_out_sys_t *p_sys = out->p_sys;
750 input_thread_t *p_input = p_sys->p_input;
756 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
760 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
762 if( !var_GetBool( p_input, "video" ) ||
763 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
765 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
770 else if( es->fmt.i_cat == AUDIO_ES )
772 var_Get( p_input, "audio", &val );
773 if( !var_GetBool( p_input, "audio" ) ||
774 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
776 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
781 if( es->fmt.i_cat == SPU_ES )
783 var_Get( p_input, "spu", &val );
784 if( !var_GetBool( p_input, "spu" ) ||
785 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
787 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
793 es->i_preroll_end = -1;
794 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
795 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
798 if( es->fmt.i_cat == VIDEO_ES )
799 psz_var = "video-es";
800 else if( es->fmt.i_cat == AUDIO_ES )
801 psz_var = "audio-es";
802 else if( es->fmt.i_cat == SPU_ES )
807 /* Mark it as selected */
808 val.i_int = es->i_id;
809 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
812 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
815 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
817 es_out_sys_t *p_sys = out->p_sys;
818 input_thread_t *p_input = p_sys->p_input;
822 if( es->p_dec == NULL )
824 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
828 input_DecoderDelete( es->p_dec );
835 if( es->p_dec == NULL )
837 if( es->fmt.i_cat == VIDEO_ES )
838 psz_var = "video-es";
839 else if( es->fmt.i_cat == AUDIO_ES )
840 psz_var = "audio-es";
841 else if( es->fmt.i_cat == SPU_ES )
846 /* Mark it as selected */
848 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
850 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
854 * Select an ES given the current mode
855 * XXX: you need to take a the lock before (stream.stream_lock)
857 * \param out The es_out structure
858 * \param es es_out_id structure
862 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
864 es_out_sys_t *p_sys = out->p_sys;
866 int i_cat = es->fmt.i_cat;
868 if( !p_sys->b_active ||
869 ( !b_force && es->fmt.i_priority < 0 ) )
874 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
879 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
883 var_Get( p_sys->p_input, "programs", &val );
884 for ( i = 0; i < val.p_list->i_count; i++ )
886 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
893 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
895 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
899 if( es->p_pgrm != p_sys->p_pgrm )
902 if( i_cat == AUDIO_ES )
904 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
905 es->psz_language_code );
907 if( p_sys->p_es_audio &&
908 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
910 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
911 p_sys->p_es_audio->psz_language_code );
913 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
915 i_wanted = es->i_channel;
919 /* Select audio if (no audio selected yet)
920 * - no audio-language
921 * - no audio code for the ES
922 * - audio code in the requested list */
924 !strcmp( es->psz_language_code, "??" ) ||
925 !p_sys->ppsz_audio_language )
926 i_wanted = es->i_channel;
929 if( p_sys->i_audio_last >= 0 )
930 i_wanted = p_sys->i_audio_last;
932 if( p_sys->i_audio_id >= 0 )
934 if( es->i_id == p_sys->i_audio_id )
935 i_wanted = es->i_channel;
940 else if( i_cat == SPU_ES )
942 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
943 es->psz_language_code );
945 if( p_sys->p_es_sub &&
946 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
948 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
949 p_sys->p_es_sub->psz_language_code );
951 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
952 idx1, es->psz_language_code, idx2,
953 p_sys->p_es_sub->psz_language_code );
955 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
957 /* We found a SPU that matches our language request */
958 i_wanted = es->i_channel;
962 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
963 idx1, es->psz_language_code );
965 i_wanted = es->i_channel;
967 if( p_sys->i_sub_last >= 0 )
968 i_wanted = p_sys->i_sub_last;
970 if( p_sys->i_sub_id >= 0 )
972 if( es->i_id == p_sys->i_sub_id )
973 i_wanted = es->i_channel;
978 else if( i_cat == VIDEO_ES )
980 i_wanted = es->i_channel;
983 if( i_wanted == es->i_channel && es->p_dec == NULL )
987 /* FIXME TODO handle priority here */
990 if( i_cat == AUDIO_ES )
992 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
994 p_sys->p_es_audio != es &&
995 p_sys->p_es_audio->p_dec )
997 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
999 p_sys->p_es_audio = es;
1001 else if( i_cat == SPU_ES )
1003 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1005 p_sys->p_es_sub != es &&
1006 p_sys->p_es_sub->p_dec )
1008 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
1010 p_sys->p_es_sub = es;
1012 else if( i_cat == VIDEO_ES )
1014 p_sys->p_es_video = es;
1020 * Send a block for the given es_out
1022 * \param out the es_out to send from
1023 * \param es the es_out_id
1024 * \param p_block the data block to send
1026 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1028 es_out_sys_t *p_sys = out->p_sys;
1029 input_thread_t *p_input = p_sys->p_input;
1030 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1034 if( es->fmt.i_cat == AUDIO_ES )
1035 i_delay = p_sys->i_audio_delay;
1036 else if( es->fmt.i_cat == SPU_ES )
1037 i_delay = p_sys->i_spu_delay;
1041 if( p_input->p_libvlc->b_stats )
1043 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1044 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1045 p_block->i_buffer, &i_total );
1046 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1047 (float)i_total, NULL );
1048 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1051 /* Mark preroll blocks */
1052 if( es->i_preroll_end >= 0 )
1054 int64_t i_date = p_block->i_pts;
1056 i_date = p_block->i_dts;
1058 if( i_date < es->i_preroll_end )
1059 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1061 es->i_preroll_end = -1;
1064 /* +11 -> avoid null value with non null dts/pts */
1065 if( p_block->i_dts > 0 )
1068 input_ClockGetTS( p_input, &p_pgrm->clock,
1069 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
1071 if( p_block->i_pts > 0 )
1074 input_ClockGetTS( p_input, &p_pgrm->clock,
1075 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
1077 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1079 mtime_t current_date = mdate();
1081 || p_block->i_pts > current_date + 10000000
1082 || current_date > p_block->i_pts )
1084 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1085 * for teletext streams. */
1086 p_block->i_pts = current_date + 400000
1087 + p_input->i_pts_delay + i_delay;
1091 p_block->i_rate = p_input->p->i_rate;
1093 /* TODO handle mute */
1094 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
1095 p_input->p->i_rate == INPUT_RATE_DEFAULT ) )
1097 input_DecoderDecode( es->p_dec, p_block );
1101 block_Release( p_block );
1107 /*****************************************************************************
1109 *****************************************************************************/
1110 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1112 es_out_sys_t *p_sys = out->p_sys;
1113 vlc_bool_t b_reselect = VLC_FALSE;
1116 /* We don't try to reselect */
1118 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1120 if( es->p_pgrm == p_sys->p_pgrm )
1121 EsOutESVarUpdate( out, es, VLC_TRUE );
1123 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1126 if( es->p_pgrm->i_es == 0 )
1128 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1131 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1132 p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1134 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1135 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1136 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1138 switch( es->fmt.i_cat )
1151 /* Re-select another track when needed */
1153 for( i = 0; i < p_sys->i_es; i++ )
1155 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1156 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1159 if( es->psz_language )
1160 free( es->psz_language );
1161 if( es->psz_language_code )
1162 free( es->psz_language_code );
1164 es_format_Clean( &es->fmt );
1170 * Control query handler
1172 * \param out the es_out to control
1173 * \param i_query A es_out query as defined in include/ninput.h
1174 * \param args a variable list of arguments for the query
1175 * \return VLC_SUCCESS or an error code
1177 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1179 es_out_sys_t *p_sys = out->p_sys;
1187 case ES_OUT_SET_ES_STATE:
1188 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1189 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1190 if( b && es->p_dec == NULL )
1192 EsSelect( out, es );
1193 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1195 else if( !b && es->p_dec )
1197 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1202 case ES_OUT_GET_ES_STATE:
1203 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1204 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1206 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1209 case ES_OUT_SET_ACTIVE:
1211 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1212 p_sys->b_active = b;
1215 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1219 case ES_OUT_GET_ACTIVE:
1220 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1221 *pb = p_sys->b_active;
1224 case ES_OUT_SET_MODE:
1225 i = (int) va_arg( args, int );
1226 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1227 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1231 /* Reapply policy mode */
1232 for( i = 0; i < p_sys->i_es; i++ )
1234 if( p_sys->es[i]->p_dec )
1236 EsUnselect( out, p_sys->es[i],
1237 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1240 for( i = 0; i < p_sys->i_es; i++ )
1242 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1246 return VLC_EGENERIC;
1248 case ES_OUT_GET_MODE:
1249 pi = (int*) va_arg( args, int* );
1250 *pi = p_sys->i_mode;
1254 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1255 /* Special case NULL, NULL+i_cat */
1258 for( i = 0; i < p_sys->i_es; i++ )
1260 if( p_sys->es[i]->p_dec )
1261 EsUnselect( out, p_sys->es[i],
1262 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1265 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1267 for( i = 0; i < p_sys->i_es; i++ )
1269 if( p_sys->es[i]->p_dec &&
1270 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1271 EsUnselect( out, p_sys->es[i],
1272 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1275 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1277 for( i = 0; i < p_sys->i_es; i++ )
1279 if( p_sys->es[i]->p_dec &&
1280 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1281 EsUnselect( out, p_sys->es[i],
1282 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1285 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1287 for( i = 0; i < p_sys->i_es; i++ )
1289 if( p_sys->es[i]->p_dec &&
1290 p_sys->es[i]->fmt.i_cat == SPU_ES )
1291 EsUnselect( out, p_sys->es[i],
1292 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1297 for( i = 0; i < p_sys->i_es; i++ )
1299 if( es == p_sys->es[i] )
1301 EsOutSelect( out, es, VLC_TRUE );
1308 case ES_OUT_SET_PCR:
1309 case ES_OUT_SET_GROUP_PCR:
1311 es_out_pgrm_t *p_pgrm = NULL;
1315 if( i_query == ES_OUT_SET_PCR )
1317 p_pgrm = p_sys->p_pgrm;
1322 i_group = (int)va_arg( args, int );
1323 for( i = 0; i < p_sys->i_pgrm; i++ )
1325 if( p_sys->pgrm[i]->i_id == i_group )
1327 p_pgrm = p_sys->pgrm[i];
1332 if( p_pgrm == NULL )
1333 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1335 i_pcr = (int64_t)va_arg( args, int64_t );
1336 /* search program */
1337 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1338 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1339 (i_pcr + 11 ) * 9 / 100);
1343 case ES_OUT_RESET_PCR:
1344 for( i = 0; i < p_sys->i_pgrm; i++ )
1346 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1347 p_sys->pgrm[i]->clock.last_pts = 0;
1354 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1355 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1356 *pi_ts = input_ClockGetTS( p_sys->p_input,
1357 &p_sys->p_pgrm->clock,
1358 ( i_ts + 11 ) * 9 / 100 );
1361 return VLC_EGENERIC;
1363 case ES_OUT_GET_GROUP:
1364 pi = (int*) va_arg( args, int* );
1366 *pi = p_sys->p_pgrm->i_id;
1368 *pi = -1; /* FIXME */
1371 case ES_OUT_SET_GROUP:
1374 i = (int) va_arg( args, int );
1375 for( j = 0; j < p_sys->i_pgrm; j++ )
1377 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1378 if( p_pgrm->i_id == i )
1380 EsOutProgramSelect( out, p_pgrm );
1384 return VLC_EGENERIC;
1387 case ES_OUT_SET_FMT:
1389 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1390 * to update the p_extra data */
1392 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1393 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1394 if( es == NULL ) return VLC_EGENERIC;
1396 if( p_fmt->i_extra )
1398 es->fmt.i_extra = p_fmt->i_extra;
1399 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1400 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1402 if( !es->p_dec ) return VLC_SUCCESS;
1405 input_DecoderDelete( es->p_dec );
1406 es->p_dec = input_DecoderNew( p_sys->p_input,
1407 &es->fmt, VLC_FALSE );
1410 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1411 es->p_dec->fmt_in.p_extra =
1412 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1413 memcpy( es->p_dec->fmt_in.p_extra,
1414 p_fmt->p_extra, p_fmt->i_extra );
1421 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1425 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1426 i_date = (int64_t)va_arg( args, int64_t );
1428 if( !es || !es->p_dec )
1429 return VLC_EGENERIC;
1431 es->i_preroll_end = i_date;
1432 input_DecoderPreroll( es->p_dec, i_date );
1436 case ES_OUT_SET_GROUP_META:
1438 int i_group = (int)va_arg( args, int );
1439 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1441 EsOutProgramMeta( out, i_group, p_meta );
1444 case ES_OUT_DEL_GROUP:
1446 int i_group = (int)va_arg( args, int );
1448 return EsOutProgramDel( out, i_group );
1452 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1453 return VLC_EGENERIC;
1457 /****************************************************************************
1458 * LanguageGetName: try to expend iso639 into plain name
1459 ****************************************************************************/
1460 static char *LanguageGetName( const char *psz_code )
1462 const iso639_lang_t *pl;
1464 if( psz_code == NULL )
1466 return strdup( "" );
1469 if( strlen( psz_code ) == 2 )
1471 pl = GetLang_1( psz_code );
1473 else if( strlen( psz_code ) == 3 )
1475 pl = GetLang_2B( psz_code );
1476 if( !strcmp( pl->psz_iso639_1, "??" ) )
1478 pl = GetLang_2T( psz_code );
1483 return strdup( psz_code );
1486 if( !strcmp( pl->psz_iso639_1, "??" ) )
1488 return strdup( psz_code );
1492 if( *pl->psz_native_name )
1494 return strdup( pl->psz_native_name );
1496 return strdup( pl->psz_eng_name );
1500 /* Get a 2 char code */
1501 static char *LanguageGetCode( const char *psz_lang )
1503 const iso639_lang_t *pl;
1505 if( psz_lang == NULL || *psz_lang == '\0' )
1506 return strdup("??");
1508 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1510 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1511 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1512 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1513 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1514 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1518 if( pl->psz_iso639_1 != NULL )
1519 return strdup( pl->psz_iso639_1 );
1521 return strdup("??");
1524 static char **LanguageSplit( const char *psz_langs )
1531 if( psz_langs == NULL ) return NULL;
1533 psz_parser = psz_dup = strdup(psz_langs);
1535 while( psz_parser && *psz_parser )
1540 psz = strchr(psz_parser, ',' );
1541 if( psz ) *psz++ = '\0';
1543 psz_code = LanguageGetCode( psz_parser );
1544 if( strcmp( psz_code, "??" ) )
1546 TAB_APPEND( i_psz, ppsz, psz_code );
1554 TAB_APPEND( i_psz, ppsz, NULL );
1561 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1565 if( !ppsz_langs || !psz_lang ) return -1;
1567 for( i = 0; ppsz_langs[i]; i++ )
1568 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1573 /****************************************************************************
1575 * - add meta info to the playlist item
1576 ****************************************************************************/
1577 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1579 es_out_sys_t *p_sys = out->p_sys;
1580 input_thread_t *p_input = p_sys->p_input;
1581 es_format_t *fmt = &es->fmt;
1585 /* Add stream info */
1586 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1588 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1589 "%.4s", (char*)&fmt->i_codec );
1591 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1592 "%s", es->psz_language );
1594 /* Add information */
1595 switch( fmt->i_cat )
1598 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1599 _("Type"), _("Audio") );
1601 if( fmt->audio.i_channels > 0 )
1602 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1603 "%d", fmt->audio.i_channels );
1605 if( fmt->audio.i_rate > 0 )
1607 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1608 _("%d Hz"), fmt->audio.i_rate );
1609 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
1612 if( fmt->audio.i_bitspersample > 0 )
1613 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1614 _("Bits per sample"), "%d",
1615 fmt->audio.i_bitspersample );
1617 if( fmt->i_bitrate > 0 )
1619 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1620 _("%d kb/s"), fmt->i_bitrate / 1000 );
1621 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
1626 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1627 _("Type"), _("Video") );
1629 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1630 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1631 _("Resolution"), "%dx%d",
1632 fmt->video.i_width, fmt->video.i_height );
1634 if( fmt->video.i_visible_width > 0 &&
1635 fmt->video.i_visible_height > 0 )
1636 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1637 _("Display resolution"), "%dx%d",
1638 fmt->video.i_visible_width,
1639 fmt->video.i_visible_height);
1640 if( fmt->video.i_frame_rate > 0 &&
1641 fmt->video.i_frame_rate_base > 0 )
1643 div = lldiv( (float)fmt->video.i_frame_rate /
1644 fmt->video.i_frame_rate_base * 1000000,
1646 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1647 _("Frame rate"), I64Fd".%06u",
1648 div.quot, (unsigned int )div.rem );
1653 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1654 _("Type"), _("Subtitle") );