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;
154 out->b_sout = (p_input->p->p_sout != NULL ? VLC_TRUE : VLC_FALSE);
156 p_sys->p_input = p_input;
158 p_sys->b_active = VLC_FALSE;
159 p_sys->i_mode = ES_OUT_MODE_AUTO;
164 p_sys->p_pgrm = NULL;
175 var_Get( p_input, "audio-track", &val );
176 p_sys->i_audio_last = val.i_int;
178 var_Get( p_input, "sub-track", &val );
179 p_sys->i_sub_last = val.i_int;
181 if( !p_input->b_preparsing )
183 var_Get( p_input, "audio-language", &val );
184 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
185 if( p_sys->ppsz_audio_language )
187 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
188 msg_Dbg( p_input, "selected audio language[%d] %s",
189 i, p_sys->ppsz_audio_language[i] );
191 if( val.psz_string ) free( val.psz_string );
193 var_Get( p_input, "sub-language", &val );
194 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
195 if( p_sys->ppsz_sub_language )
197 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
198 msg_Dbg( p_input, "selected subtitle language[%d] %s",
199 i, p_sys->ppsz_sub_language[i] );
201 if( val.psz_string ) free( val.psz_string );
205 p_sys->ppsz_sub_language = NULL;
206 p_sys->ppsz_audio_language = NULL;
209 var_Get( p_input, "audio-track-id", &val );
210 p_sys->i_audio_id = val.i_int;
212 var_Get( p_input, "sub-track-id", &val );
213 p_sys->i_sub_id = val.i_int;
215 p_sys->p_es_audio = NULL;
216 p_sys->p_es_video = NULL;
217 p_sys->p_es_sub = NULL;
219 p_sys->i_audio_delay= 0;
220 p_sys->i_spu_delay = 0;
225 /*****************************************************************************
227 *****************************************************************************/
228 void input_EsOutDelete( es_out_t *out )
230 es_out_sys_t *p_sys = out->p_sys;
233 for( i = 0; i < p_sys->i_es; i++ )
235 if( p_sys->es[i]->p_dec )
237 input_DecoderDelete( p_sys->es[i]->p_dec );
239 if( p_sys->es[i]->psz_language )
240 free( p_sys->es[i]->psz_language );
241 if( p_sys->es[i]->psz_language_code )
242 free( p_sys->es[i]->psz_language_code );
243 es_format_Clean( &p_sys->es[i]->fmt );
245 free( p_sys->es[i] );
247 if( p_sys->ppsz_audio_language )
249 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
250 free( p_sys->ppsz_audio_language[i] );
251 free( p_sys->ppsz_audio_language );
253 if( p_sys->ppsz_sub_language )
255 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
256 free( p_sys->ppsz_sub_language[i] );
257 free( p_sys->ppsz_sub_language );
263 for( i = 0; i < p_sys->i_pgrm; i++ )
265 if( p_sys->pgrm[i]->psz_now_playing )
266 free( p_sys->pgrm[i]->psz_now_playing );
267 free( p_sys->pgrm[i] );
276 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
281 /* Special HACK, -i_id is tha cat of the stream */
282 return (es_out_id_t*)((uint8_t*)NULL-i_id);
285 for( i = 0; i < out->p_sys->i_es; i++ )
287 if( out->p_sys->es[i]->i_id == i_id )
288 return out->p_sys->es[i];
293 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
295 es_out_sys_t *p_sys = out->p_sys;
298 for( i = 0; i < p_sys->i_es; i++ )
300 es_out_id_t *es = p_sys->es[i];
301 es->b_discontinuity = VLC_TRUE; /* signal discontinuity */
303 /* Send a dummy block to let decoder know that
304 * there is a discontinuity */
305 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
307 input_DecoderDiscontinuity( es->p_dec );
312 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
314 es_out_sys_t *p_sys = out->p_sys;
316 if( i_cat == AUDIO_ES )
317 p_sys->i_audio_delay = i_delay;
318 else if( i_cat == SPU_ES )
319 p_sys->i_spu_delay = i_delay;
322 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
324 es_out_sys_t *p_sys = out->p_sys;
327 for( i = 0; i < p_sys->i_es; i++ )
329 es_out_id_t *es = p_sys->es[i];
331 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
337 /*****************************************************************************
339 *****************************************************************************/
340 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
341 vlc_bool_t b_delete )
343 es_out_sys_t *p_sys = out->p_sys;
344 input_thread_t *p_input = p_sys->p_input;
345 vlc_value_t val, text;
349 if( es->fmt.i_cat == AUDIO_ES )
350 psz_var = "audio-es";
351 else if( es->fmt.i_cat == VIDEO_ES )
352 psz_var = "video-es";
353 else if( es->fmt.i_cat == SPU_ES )
360 val.i_int = es->i_id;
361 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
362 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
366 /* Get the number of ES already added */
367 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
372 /* First one, we need to add the "Disable" choice */
373 val2.i_int = -1; text.psz_string = _("Disable");
374 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
378 /* Take care of the ES description */
379 if( es->fmt.psz_description && *es->fmt.psz_description )
381 if( es->psz_language && *es->psz_language )
383 text.psz_string = malloc( strlen( es->fmt.psz_description) +
384 strlen( es->psz_language ) + 10 );
385 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description,
388 else text.psz_string = strdup( es->fmt.psz_description );
392 if( es->psz_language && *es->psz_language )
395 text.psz_string = malloc( strlen( _("Track %i") )+
396 strlen( es->psz_language ) + 30 );
397 asprintf( &temp, _("Track %i"), val.i_int );
398 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
403 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
404 sprintf( text.psz_string, _("Track %i"), val.i_int );
408 val.i_int = es->i_id;
409 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
411 free( text.psz_string );
413 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
416 /* EsOutProgramSelect:
417 * Select a program and update the object variable
419 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
421 es_out_sys_t *p_sys = out->p_sys;
422 input_thread_t *p_input = p_sys->p_input;
426 if( p_sys->p_pgrm == p_pgrm )
427 return; /* Nothing to do */
431 es_out_pgrm_t *old = p_sys->p_pgrm;
432 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
434 for( i = 0; i < p_sys->i_es; i++ )
436 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
437 p_sys->i_mode != ES_OUT_MODE_ALL )
438 EsUnselect( out, p_sys->es[i], VLC_TRUE );
441 p_sys->p_es_audio = NULL;
442 p_sys->p_es_sub = NULL;
443 p_sys->p_es_video = NULL;
446 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
448 /* Mark it selected */
449 p_pgrm->b_selected = VLC_TRUE;
451 /* Switch master stream */
452 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
454 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
456 p_pgrm->clock.b_master = VLC_TRUE;
457 p_sys->p_pgrm = p_pgrm;
459 /* Update "program" */
460 val.i_int = p_pgrm->i_id;
461 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
464 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
465 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
466 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
467 for( i = 0; i < p_sys->i_es; i++ )
469 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
470 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
471 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
474 /* Update now playing if defined per program */
475 if( p_pgrm->psz_now_playing )
477 char *psz_cat = malloc( strlen(_("Program")) + 10 );
479 sprintf( psz_cat, "%s %d", _("Program"), p_pgrm->i_id );
480 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT),
481 _(VLC_META_NOW_PLAYING), "%s", p_pgrm->psz_now_playing );
486 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
492 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
494 es_out_sys_t *p_sys = out->p_sys;
495 input_thread_t *p_input = p_sys->p_input;
498 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
501 p_pgrm->i_id = i_group;
503 p_pgrm->b_selected = VLC_FALSE;
504 p_pgrm->psz_now_playing = NULL;
505 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average );
508 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
510 /* Update "program" variable */
512 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
514 if( i_group == var_GetInteger( p_input, "program" ) )
516 EsOutProgramSelect( out, p_pgrm );
520 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
528 static int EsOutProgramDel( es_out_t *out, int i_group )
530 es_out_sys_t *p_sys = out->p_sys;
531 input_thread_t *p_input = p_sys->p_input;
532 es_out_pgrm_t *p_pgrm = NULL;
536 for( i = 0; i < p_sys->i_pgrm; i++ )
538 if( p_sys->pgrm[i]->i_id == i_group )
540 p_pgrm = p_sys->pgrm[i];
550 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
551 i_group, p_pgrm->i_es );
555 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
557 /* If program is selected we need to unselect it */
558 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = 0;
560 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
563 /* Update "program" variable */
565 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
567 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
574 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
576 es_out_sys_t *p_sys = out->p_sys;
577 es_out_pgrm_t *p_pgrm = NULL;
578 input_thread_t *p_input = p_sys->p_input;
579 char *psz_cat = malloc( strlen(_("Program")) + 10 );
580 char *psz_title = NULL;
581 char *psz_now_playing = NULL;
582 char *psz_provider = NULL;
585 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
586 sprintf( psz_cat, "%s %d", _("Program"), i_group );
588 if( p_meta->psz_title ) psz_title = p_meta->psz_title;
589 if( p_meta->psz_publisher ) psz_provider = p_meta->psz_publisher;
590 if( p_meta->psz_nowplaying ) psz_now_playing = p_meta->psz_nowplaying;
592 if( !psz_title && !psz_now_playing )
598 for( i = 0; i < p_sys->i_pgrm; i++ )
600 if( p_sys->pgrm[i]->i_id == i_group )
602 p_pgrm = p_sys->pgrm[i];
607 if( p_pgrm == NULL ) {
609 msg_Dbg( p_input, "Trying to add meta for non-existing program" );
613 /* Update the description text of the program */
614 if( psz_title && *psz_title )
619 /* ugly but it works */
621 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
623 if( psz_provider && *psz_provider )
625 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
626 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
627 free( text.psz_string );
631 text.psz_string = psz_title;
632 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
635 if( psz_now_playing )
637 if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
638 p_pgrm->psz_now_playing = strdup(psz_now_playing);
640 if( p_sys->p_pgrm == p_pgrm )
642 vlc_meta_SetNowPlaying( p_input->p->input.p_item->p_meta,
652 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
654 es_out_sys_t *p_sys = out->p_sys;
655 input_thread_t *p_input = p_sys->p_input;
657 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
658 es_out_pgrm_t *p_pgrm = NULL;
661 if( fmt->i_group < 0 )
663 msg_Err( p_input, "invalid group number" );
667 /* Search the program */
668 for( i = 0; i < p_sys->i_pgrm; i++ )
670 if( fmt->i_group == p_sys->pgrm[i]->i_id )
672 p_pgrm = p_sys->pgrm[i];
678 /* Create a new one */
679 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
682 /* Increase ref count for program */
687 fmt->i_id = out->p_sys->i_id;
688 es->i_id = fmt->i_id;
690 es_format_Copy( &es->fmt, fmt );
691 es->i_preroll_end = -1;
692 es->b_discontinuity = VLC_FALSE;
697 es->i_channel = p_sys->i_audio;
701 es->i_channel = p_sys->i_video;
702 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
703 vlc_ureduce( &es->fmt.video.i_frame_rate,
704 &es->fmt.video.i_frame_rate_base,
705 fmt->video.i_frame_rate,
706 fmt->video.i_frame_rate_base, 0 );
710 es->i_channel = p_sys->i_sub;
717 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
718 es->psz_language_code = LanguageGetCode( fmt->psz_language );
721 if( es->p_pgrm == p_sys->p_pgrm )
722 EsOutESVarUpdate( out, es, VLC_FALSE );
724 /* Select it if needed */
725 EsOutSelect( out, es, VLC_FALSE );
728 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
729 p_sys->i_id++; /* always incremented */
743 EsOutAddInfo( out, es );
748 static void EsSelect( es_out_t *out, es_out_id_t *es )
750 es_out_sys_t *p_sys = out->p_sys;
751 input_thread_t *p_input = p_sys->p_input;
757 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
761 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
763 if( !var_GetBool( p_input, "video" ) ||
764 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
766 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
771 else if( es->fmt.i_cat == AUDIO_ES )
773 var_Get( p_input, "audio", &val );
774 if( !var_GetBool( p_input, "audio" ) ||
775 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
777 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
782 if( es->fmt.i_cat == SPU_ES )
784 var_Get( p_input, "spu", &val );
785 if( !var_GetBool( p_input, "spu" ) ||
786 ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
788 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
794 es->i_preroll_end = -1;
795 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
796 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
799 if( es->fmt.i_cat == VIDEO_ES )
800 psz_var = "video-es";
801 else if( es->fmt.i_cat == AUDIO_ES )
802 psz_var = "audio-es";
803 else if( es->fmt.i_cat == SPU_ES )
808 /* Mark it as selected */
809 val.i_int = es->i_id;
810 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
813 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
816 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
818 es_out_sys_t *p_sys = out->p_sys;
819 input_thread_t *p_input = p_sys->p_input;
823 if( es->p_dec == NULL )
825 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
829 input_DecoderDelete( es->p_dec );
836 if( es->p_dec == NULL )
838 if( es->fmt.i_cat == VIDEO_ES )
839 psz_var = "video-es";
840 else if( es->fmt.i_cat == AUDIO_ES )
841 psz_var = "audio-es";
842 else if( es->fmt.i_cat == SPU_ES )
847 /* Mark it as selected */
849 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
851 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
855 * Select an ES given the current mode
856 * XXX: you need to take a the lock before (stream.stream_lock)
858 * \param out The es_out structure
859 * \param es es_out_id structure
863 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
865 es_out_sys_t *p_sys = out->p_sys;
867 int i_cat = es->fmt.i_cat;
869 if( !p_sys->b_active ||
870 ( !b_force && es->fmt.i_priority < 0 ) )
875 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
880 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
884 var_Get( p_sys->p_input, "programs", &val );
885 for ( i = 0; i < val.p_list->i_count; i++ )
887 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
894 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
896 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
900 if( es->p_pgrm != p_sys->p_pgrm )
903 if( i_cat == AUDIO_ES )
905 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
906 es->psz_language_code );
908 if( p_sys->p_es_audio &&
909 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
911 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
912 p_sys->p_es_audio->psz_language_code );
914 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
916 i_wanted = es->i_channel;
920 /* Select audio if (no audio selected yet)
921 * - no audio-language
922 * - no audio code for the ES
923 * - audio code in the requested list */
925 !strcmp( es->psz_language_code, "??" ) ||
926 !p_sys->ppsz_audio_language )
927 i_wanted = es->i_channel;
930 if( p_sys->i_audio_last >= 0 )
931 i_wanted = p_sys->i_audio_last;
933 if( p_sys->i_audio_id >= 0 )
935 if( es->i_id == p_sys->i_audio_id )
936 i_wanted = es->i_channel;
941 else if( i_cat == SPU_ES )
943 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
944 es->psz_language_code );
946 if( p_sys->p_es_sub &&
947 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
949 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
950 p_sys->p_es_sub->psz_language_code );
952 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
953 idx1, es->psz_language_code, idx2,
954 p_sys->p_es_sub->psz_language_code );
956 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
958 /* We found a SPU that matches our language request */
959 i_wanted = es->i_channel;
963 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
964 idx1, es->psz_language_code );
966 i_wanted = es->i_channel;
968 if( p_sys->i_sub_last >= 0 )
969 i_wanted = p_sys->i_sub_last;
971 if( p_sys->i_sub_id >= 0 )
973 if( es->i_id == p_sys->i_sub_id )
974 i_wanted = es->i_channel;
979 else if( i_cat == VIDEO_ES )
981 i_wanted = es->i_channel;
984 if( i_wanted == es->i_channel && es->p_dec == NULL )
988 /* FIXME TODO handle priority here */
991 if( i_cat == AUDIO_ES )
993 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
995 p_sys->p_es_audio != es &&
996 p_sys->p_es_audio->p_dec )
998 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
1000 p_sys->p_es_audio = es;
1002 else if( i_cat == SPU_ES )
1004 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1006 p_sys->p_es_sub != es &&
1007 p_sys->p_es_sub->p_dec )
1009 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
1011 p_sys->p_es_sub = es;
1013 else if( i_cat == VIDEO_ES )
1015 p_sys->p_es_video = es;
1021 * Send a block for the given es_out
1023 * \param out the es_out to send from
1024 * \param es the es_out_id
1025 * \param p_block the data block to send
1027 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1029 es_out_sys_t *p_sys = out->p_sys;
1030 input_thread_t *p_input = p_sys->p_input;
1031 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1035 if( es->fmt.i_cat == AUDIO_ES )
1036 i_delay = p_sys->i_audio_delay;
1037 else if( es->fmt.i_cat == SPU_ES )
1038 i_delay = p_sys->i_spu_delay;
1042 if( p_input->p_libvlc->b_stats )
1044 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1045 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1046 p_block->i_buffer, &i_total );
1047 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1048 (float)i_total, NULL );
1049 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1052 /* Mark preroll blocks */
1053 if( es->i_preroll_end >= 0 )
1055 int64_t i_date = p_block->i_pts;
1057 i_date = p_block->i_dts;
1059 if( i_date < es->i_preroll_end )
1060 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1062 es->i_preroll_end = -1;
1065 /* +11 -> avoid null value with non null dts/pts */
1066 if( p_block->i_dts > 0 )
1069 input_ClockGetTS( p_input, &p_pgrm->clock,
1070 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
1072 if( p_block->i_pts > 0 )
1075 input_ClockGetTS( p_input, &p_pgrm->clock,
1076 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
1078 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1080 mtime_t current_date = mdate();
1082 || p_block->i_pts > current_date + 10000000
1083 || current_date > p_block->i_pts )
1085 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1086 * for teletext streams. */
1087 p_block->i_pts = current_date + 400000
1088 + p_input->i_pts_delay + i_delay;
1092 p_block->i_rate = p_input->p->i_rate;
1094 /* TODO handle mute */
1095 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
1096 p_input->p->i_rate == INPUT_RATE_DEFAULT ) )
1098 input_DecoderDecode( es->p_dec, p_block );
1102 block_Release( p_block );
1108 /*****************************************************************************
1110 *****************************************************************************/
1111 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1113 es_out_sys_t *p_sys = out->p_sys;
1114 vlc_bool_t b_reselect = VLC_FALSE;
1117 /* We don't try to reselect */
1119 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1121 if( es->p_pgrm == p_sys->p_pgrm )
1122 EsOutESVarUpdate( out, es, VLC_TRUE );
1124 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1127 if( es->p_pgrm->i_es == 0 )
1129 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1132 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1133 p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;
1135 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1136 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1137 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1139 switch( es->fmt.i_cat )
1152 /* Re-select another track when needed */
1154 for( i = 0; i < p_sys->i_es; i++ )
1156 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1157 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1160 if( es->psz_language )
1161 free( es->psz_language );
1162 if( es->psz_language_code )
1163 free( es->psz_language_code );
1165 es_format_Clean( &es->fmt );
1171 * Control query handler
1173 * \param out the es_out to control
1174 * \param i_query A es_out query as defined in include/ninput.h
1175 * \param args a variable list of arguments for the query
1176 * \return VLC_SUCCESS or an error code
1178 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1180 es_out_sys_t *p_sys = out->p_sys;
1188 case ES_OUT_SET_ES_STATE:
1189 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1190 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1191 if( b && es->p_dec == NULL )
1193 EsSelect( out, es );
1194 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1196 else if( !b && es->p_dec )
1198 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1203 case ES_OUT_GET_ES_STATE:
1204 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1205 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1207 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1210 case ES_OUT_SET_ACTIVE:
1212 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1213 p_sys->b_active = b;
1216 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1220 case ES_OUT_GET_ACTIVE:
1221 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1222 *pb = p_sys->b_active;
1225 case ES_OUT_SET_MODE:
1226 i = (int) va_arg( args, int );
1227 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1228 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1232 /* Reapply policy mode */
1233 for( i = 0; i < p_sys->i_es; i++ )
1235 if( p_sys->es[i]->p_dec )
1237 EsUnselect( out, p_sys->es[i],
1238 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1241 for( i = 0; i < p_sys->i_es; i++ )
1243 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1247 return VLC_EGENERIC;
1249 case ES_OUT_GET_MODE:
1250 pi = (int*) va_arg( args, int* );
1251 *pi = p_sys->i_mode;
1255 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1256 /* Special case NULL, NULL+i_cat */
1259 for( i = 0; i < p_sys->i_es; i++ )
1261 if( p_sys->es[i]->p_dec )
1262 EsUnselect( out, p_sys->es[i],
1263 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1266 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1268 for( i = 0; i < p_sys->i_es; i++ )
1270 if( p_sys->es[i]->p_dec &&
1271 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1272 EsUnselect( out, p_sys->es[i],
1273 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1276 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1278 for( i = 0; i < p_sys->i_es; i++ )
1280 if( p_sys->es[i]->p_dec &&
1281 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1282 EsUnselect( out, p_sys->es[i],
1283 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1286 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1288 for( i = 0; i < p_sys->i_es; i++ )
1290 if( p_sys->es[i]->p_dec &&
1291 p_sys->es[i]->fmt.i_cat == SPU_ES )
1292 EsUnselect( out, p_sys->es[i],
1293 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1298 for( i = 0; i < p_sys->i_es; i++ )
1300 if( es == p_sys->es[i] )
1302 EsOutSelect( out, es, VLC_TRUE );
1308 playlist_t * p_playlist = pl_Yield( p_sys->p_input );
1310 p_playlist->gc_date = mdate();
1311 vlc_cond_signal( &p_playlist->object_wait );
1313 pl_Release( p_playlist );
1317 case ES_OUT_SET_PCR:
1318 case ES_OUT_SET_GROUP_PCR:
1320 es_out_pgrm_t *p_pgrm = NULL;
1324 if( i_query == ES_OUT_SET_PCR )
1326 p_pgrm = p_sys->p_pgrm;
1331 i_group = (int)va_arg( args, int );
1332 for( i = 0; i < p_sys->i_pgrm; i++ )
1334 if( p_sys->pgrm[i]->i_id == i_group )
1336 p_pgrm = p_sys->pgrm[i];
1341 if( p_pgrm == NULL )
1342 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1344 i_pcr = (int64_t)va_arg( args, int64_t );
1345 /* search program */
1346 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1347 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1348 (i_pcr + 11 ) * 9 / 100);
1352 case ES_OUT_RESET_PCR:
1353 for( i = 0; i < p_sys->i_pgrm; i++ )
1355 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1356 p_sys->pgrm[i]->clock.last_pts = 0;
1363 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1364 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1365 *pi_ts = input_ClockGetTS( p_sys->p_input,
1366 &p_sys->p_pgrm->clock,
1367 ( i_ts + 11 ) * 9 / 100 );
1370 return VLC_EGENERIC;
1372 case ES_OUT_GET_GROUP:
1373 pi = (int*) va_arg( args, int* );
1375 *pi = p_sys->p_pgrm->i_id;
1377 *pi = -1; /* FIXME */
1380 case ES_OUT_SET_GROUP:
1383 i = (int) va_arg( args, int );
1384 for( j = 0; j < p_sys->i_pgrm; j++ )
1386 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1387 if( p_pgrm->i_id == i )
1389 EsOutProgramSelect( out, p_pgrm );
1393 return VLC_EGENERIC;
1396 case ES_OUT_SET_FMT:
1398 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1399 * to update the p_extra data */
1401 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1402 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1403 if( es == NULL ) return VLC_EGENERIC;
1405 if( p_fmt->i_extra )
1407 es->fmt.i_extra = p_fmt->i_extra;
1408 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1409 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1411 if( !es->p_dec ) return VLC_SUCCESS;
1414 input_DecoderDelete( es->p_dec );
1415 es->p_dec = input_DecoderNew( p_sys->p_input,
1416 &es->fmt, VLC_FALSE );
1419 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1420 es->p_dec->fmt_in.p_extra =
1421 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1422 memcpy( es->p_dec->fmt_in.p_extra,
1423 p_fmt->p_extra, p_fmt->i_extra );
1430 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1434 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1435 i_date = (int64_t)va_arg( args, int64_t );
1437 if( !es || !es->p_dec )
1438 return VLC_EGENERIC;
1440 es->i_preroll_end = i_date;
1441 input_DecoderPreroll( es->p_dec, i_date );
1445 case ES_OUT_SET_GROUP_META:
1447 int i_group = (int)va_arg( args, int );
1448 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1450 EsOutProgramMeta( out, i_group, p_meta );
1453 case ES_OUT_DEL_GROUP:
1455 int i_group = (int)va_arg( args, int );
1457 return EsOutProgramDel( out, i_group );
1461 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1462 return VLC_EGENERIC;
1466 /****************************************************************************
1467 * LanguageGetName: try to expend iso639 into plain name
1468 ****************************************************************************/
1469 static char *LanguageGetName( const char *psz_code )
1471 const iso639_lang_t *pl;
1473 if( psz_code == NULL )
1475 return strdup( "" );
1478 if( strlen( psz_code ) == 2 )
1480 pl = GetLang_1( psz_code );
1482 else if( strlen( psz_code ) == 3 )
1484 pl = GetLang_2B( psz_code );
1485 if( !strcmp( pl->psz_iso639_1, "??" ) )
1487 pl = GetLang_2T( psz_code );
1492 return strdup( psz_code );
1495 if( !strcmp( pl->psz_iso639_1, "??" ) )
1497 return strdup( psz_code );
1501 if( *pl->psz_native_name )
1503 return strdup( pl->psz_native_name );
1505 return strdup( pl->psz_eng_name );
1509 /* Get a 2 char code */
1510 static char *LanguageGetCode( const char *psz_lang )
1512 const iso639_lang_t *pl;
1514 if( psz_lang == NULL || *psz_lang == '\0' )
1515 return strdup("??");
1517 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1519 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1520 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1521 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1522 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1523 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1527 if( pl->psz_iso639_1 != NULL )
1528 return strdup( pl->psz_iso639_1 );
1530 return strdup("??");
1533 static char **LanguageSplit( const char *psz_langs )
1540 if( psz_langs == NULL ) return NULL;
1542 psz_parser = psz_dup = strdup(psz_langs);
1544 while( psz_parser && *psz_parser )
1549 psz = strchr(psz_parser, ',' );
1550 if( psz ) *psz++ = '\0';
1552 psz_code = LanguageGetCode( psz_parser );
1553 if( strcmp( psz_code, "??" ) )
1555 TAB_APPEND( i_psz, ppsz, psz_code );
1563 TAB_APPEND( i_psz, ppsz, NULL );
1570 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1574 if( !ppsz_langs || !psz_lang ) return -1;
1576 for( i = 0; ppsz_langs[i]; i++ )
1577 if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1582 /****************************************************************************
1584 * - add meta info to the playlist item
1585 ****************************************************************************/
1586 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1588 es_out_sys_t *p_sys = out->p_sys;
1589 input_thread_t *p_input = p_sys->p_input;
1590 es_format_t *fmt = &es->fmt;
1594 /* Add stream info */
1595 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1597 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1598 "%.4s", (char*)&fmt->i_codec );
1600 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1601 "%s", es->psz_language );
1603 /* Add information */
1604 switch( fmt->i_cat )
1607 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1608 _("Type"), _("Audio") );
1610 if( fmt->audio.i_channels > 0 )
1611 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1612 "%d", fmt->audio.i_channels );
1614 if( fmt->audio.i_rate > 0 )
1616 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1617 _("%d Hz"), fmt->audio.i_rate );
1618 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
1621 if( fmt->audio.i_bitspersample > 0 )
1622 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1623 _("Bits per sample"), "%d",
1624 fmt->audio.i_bitspersample );
1626 if( fmt->i_bitrate > 0 )
1628 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1629 _("%d kb/s"), fmt->i_bitrate / 1000 );
1630 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
1635 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1636 _("Type"), _("Video") );
1638 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1639 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1640 _("Resolution"), "%dx%d",
1641 fmt->video.i_width, fmt->video.i_height );
1643 if( fmt->video.i_visible_width > 0 &&
1644 fmt->video.i_visible_height > 0 )
1645 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1646 _("Display resolution"), "%dx%d",
1647 fmt->video.i_visible_width,
1648 fmt->video.i_visible_height);
1649 if( fmt->video.i_frame_rate > 0 &&
1650 fmt->video.i_frame_rate_base > 0 )
1652 div = lldiv( (float)fmt->video.i_frame_rate /
1653 fmt->video.i_frame_rate_base * 1000000,
1655 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1656 _("Frame rate"), I64Fd".%06u",
1657 div.quot, (unsigned int )div.rem );
1662 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1663 _("Type"), _("Subtitle") );