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 *****************************************************************************/
32 #include <vlc_common.h>
36 #include <vlc_input.h>
37 #include <vlc_es_out.h>
38 #include <vlc_block.h>
41 #include "input_internal.h"
43 #include <vlc_iso_lang.h>
44 /* FIXME we should find a better way than including that */
45 #include "../text/iso-639_def.h"
47 /*****************************************************************************
49 *****************************************************************************/
55 /* Number of es for this pgrm */
60 /* Clock for this program */
64 char *psz_now_playing;
74 es_out_pgrm_t *p_pgrm;
77 int64_t i_preroll_end;
79 /* Channel in the track type */
83 char *psz_language_code;
87 /* Fields for Video with CC */
88 bool pb_cc_present[4];
89 es_out_id_t *pp_cc_es[4];
91 /* Field for CC track from a master video */
92 es_out_id_t *p_master;
97 input_thread_t *p_input;
101 es_out_pgrm_t **pgrm;
102 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
103 es_out_pgrm_t *p_pgrm; /* Master program */
120 int i_audio_last, i_audio_id;
121 int i_sub_last, i_sub_id;
122 int i_default_sub_id; /* As specified in container; if applicable */
123 char **ppsz_audio_language;
124 char **ppsz_sub_language;
126 /* current main es */
127 es_out_id_t *p_es_audio;
128 es_out_id_t *p_es_video;
129 es_out_id_t *p_es_sub;
132 int64_t i_audio_delay;
135 /* Rate used to rescale ES ts */
139 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
140 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
141 static void EsOutDel ( es_out_t *, es_out_id_t * );
142 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
143 static int EsOutControl( es_out_t *, int i_query, va_list );
145 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
147 static bool EsIsSelected( es_out_id_t *es );
148 static void EsSelect( es_out_t *out, es_out_id_t *es );
149 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
150 static char *LanguageGetName( const char *psz_code );
151 static char *LanguageGetCode( const char *psz_lang );
152 static char **LanguageSplit( const char *psz_langs );
153 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
155 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
157 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
158 VLC_FOURCC('c', 'c', '1', ' '),
159 VLC_FOURCC('c', 'c', '2', ' '),
160 VLC_FOURCC('c', 'c', '3', ' '),
161 VLC_FOURCC('c', 'c', '4', ' '),
163 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
166 for( i = 0; i < 4; i++ )
168 if( fcc == EsOutFourccClosedCaptions[i] )
175 /*****************************************************************************
177 *****************************************************************************/
178 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
180 es_out_t *out = malloc( sizeof( es_out_t ) );
181 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
185 if( !out ) return NULL;
192 out->pf_add = EsOutAdd;
193 out->pf_send = EsOutSend;
194 out->pf_del = EsOutDel;
195 out->pf_control = EsOutControl;
197 out->b_sout = p_input->p->p_sout != NULL;
199 p_sys->p_input = p_input;
201 p_sys->b_active = false;
202 p_sys->i_mode = ES_OUT_MODE_AUTO;
205 TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
206 p_sys->p_pgrm = NULL;
210 TAB_INIT( p_sys->i_es, p_sys->es );
217 var_Get( p_input, "audio-track", &val );
218 p_sys->i_audio_last = val.i_int;
220 var_Get( p_input, "sub-track", &val );
221 p_sys->i_sub_last = val.i_int;
223 p_sys->i_default_sub_id = -1;
225 if( !p_input->b_preparsing )
227 var_Get( p_input, "audio-language", &val );
228 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
229 if( p_sys->ppsz_audio_language )
231 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
232 msg_Dbg( p_input, "selected audio language[%d] %s",
233 i, p_sys->ppsz_audio_language[i] );
235 free( val.psz_string );
237 var_Get( p_input, "sub-language", &val );
238 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
239 if( p_sys->ppsz_sub_language )
241 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
242 msg_Dbg( p_input, "selected subtitle language[%d] %s",
243 i, p_sys->ppsz_sub_language[i] );
245 free( val.psz_string );
249 p_sys->ppsz_sub_language = NULL;
250 p_sys->ppsz_audio_language = NULL;
253 var_Get( p_input, "audio-track-id", &val );
254 p_sys->i_audio_id = val.i_int;
256 var_Get( p_input, "sub-track-id", &val );
257 p_sys->i_sub_id = val.i_int;
259 p_sys->p_es_audio = NULL;
260 p_sys->p_es_video = NULL;
261 p_sys->p_es_sub = NULL;
263 p_sys->i_audio_delay= 0;
264 p_sys->i_spu_delay = 0;
266 p_sys->i_rate = i_rate;
271 /*****************************************************************************
273 *****************************************************************************/
274 void input_EsOutDelete( es_out_t *out )
276 es_out_sys_t *p_sys = out->p_sys;
279 for( i = 0; i < p_sys->i_es; i++ )
281 if( p_sys->es[i]->p_dec )
283 input_DecoderDelete( p_sys->es[i]->p_dec );
285 free( p_sys->es[i]->psz_language );
286 free( p_sys->es[i]->psz_language_code );
287 es_format_Clean( &p_sys->es[i]->fmt );
289 free( p_sys->es[i] );
291 if( p_sys->ppsz_audio_language )
293 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
294 free( p_sys->ppsz_audio_language[i] );
295 free( p_sys->ppsz_audio_language );
297 if( p_sys->ppsz_sub_language )
299 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
300 free( p_sys->ppsz_sub_language[i] );
301 free( p_sys->ppsz_sub_language );
306 /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
307 for( i = 0; i < p_sys->i_pgrm; i++ )
309 es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
310 free( p_pgrm->psz_now_playing );
311 free( p_pgrm->psz_publisher );
312 free( p_pgrm->psz_name );
314 vlc_epg_Delete( p_pgrm->p_epg );
318 TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
324 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
329 /* Special HACK, -i_id is tha cat of the stream */
330 return (es_out_id_t*)((uint8_t*)NULL-i_id);
333 for( i = 0; i < out->p_sys->i_es; i++ )
335 if( out->p_sys->es[i]->i_id == i_id )
336 return out->p_sys->es[i];
341 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
343 es_out_sys_t *p_sys = out->p_sys;
346 for( i = 0; i < p_sys->i_es; i++ )
348 es_out_id_t *es = p_sys->es[i];
350 /* Send a dummy block to let decoder know that
351 * there is a discontinuity */
352 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
353 input_DecoderDiscontinuity( es->p_dec, b_flush );
356 void input_EsOutChangeRate( es_out_t *out, int i_rate )
358 es_out_sys_t *p_sys = out->p_sys;
361 p_sys->i_rate = i_rate;
362 EsOutDiscontinuity( out, false, false );
364 for( i = 0; i < p_sys->i_pgrm; i++ )
365 input_ClockSetRate( &p_sys->pgrm[i]->clock, i_rate );
368 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
370 es_out_sys_t *p_sys = out->p_sys;
372 if( i_cat == AUDIO_ES )
373 p_sys->i_audio_delay = i_delay;
374 else if( i_cat == SPU_ES )
375 p_sys->i_spu_delay = i_delay;
377 void input_EsOutChangeState( es_out_t *out )
379 es_out_sys_t *p_sys = out->p_sys;
380 input_thread_t *p_input = p_sys->p_input;
382 if( p_input->i_state == PAUSE_S )
384 /* Send discontinuity to decoders (it will allow them to flush
385 * * if implemented */
386 EsOutDiscontinuity( out, false, false );
390 /* Out of pause, reset pcr */
391 es_out_Control( out, ES_OUT_RESET_PCR );
394 void input_EsOutChangePosition( es_out_t *out )
396 //es_out_sys_t *p_sys = out->p_sys;
398 es_out_Control( out, ES_OUT_RESET_PCR );
399 EsOutDiscontinuity( out, true, false );
402 bool input_EsOutDecodersEmpty( es_out_t *out )
404 es_out_sys_t *p_sys = out->p_sys;
407 for( i = 0; i < p_sys->i_es; i++ )
409 es_out_id_t *es = p_sys->es[i];
411 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
417 /*****************************************************************************
419 *****************************************************************************/
420 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
423 es_out_sys_t *p_sys = out->p_sys;
424 input_thread_t *p_input = p_sys->p_input;
425 vlc_value_t val, text;
429 if( fmt->i_cat == AUDIO_ES )
430 psz_var = "audio-es";
431 else if( fmt->i_cat == VIDEO_ES )
432 psz_var = "video-es";
433 else if( fmt->i_cat == SPU_ES )
441 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
442 var_SetBool( p_sys->p_input, "intf-change", true );
446 /* Get the number of ES already added */
447 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
452 /* First one, we need to add the "Disable" choice */
453 val2.i_int = -1; text.psz_string = _("Disable");
454 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
458 /* Take care of the ES description */
459 if( fmt->psz_description && *fmt->psz_description )
461 if( psz_language && *psz_language )
463 text.psz_string = malloc( strlen( fmt->psz_description) +
464 strlen( psz_language ) + 10 );
465 sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
468 else text.psz_string = strdup( fmt->psz_description );
472 if( psz_language && *psz_language )
475 text.psz_string = malloc( strlen( _("Track %i") )+
476 strlen( psz_language ) + 30 );
477 asprintf( &temp, _("Track %i"), val.i_int );
478 sprintf( text.psz_string, "%s - [%s]", temp, psz_language );
483 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
484 sprintf( text.psz_string, _("Track %i"), val.i_int );
489 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
491 free( text.psz_string );
493 var_SetBool( p_sys->p_input, "intf-change", true );
496 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
499 EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
502 /* EsOutProgramSelect:
503 * Select a program and update the object variable
505 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
507 es_out_sys_t *p_sys = out->p_sys;
508 input_thread_t *p_input = p_sys->p_input;
512 if( p_sys->p_pgrm == p_pgrm )
513 return; /* Nothing to do */
517 es_out_pgrm_t *old = p_sys->p_pgrm;
518 msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
520 for( i = 0; i < p_sys->i_es; i++ )
522 if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
523 p_sys->i_mode != ES_OUT_MODE_ALL )
524 EsUnselect( out, p_sys->es[i], true );
527 p_sys->p_es_audio = NULL;
528 p_sys->p_es_sub = NULL;
529 p_sys->p_es_video = NULL;
532 msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
534 /* Mark it selected */
535 p_pgrm->b_selected = true;
537 /* Switch master stream */
538 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
540 p_sys->p_pgrm->clock.b_master = false;
542 p_pgrm->clock.b_master = true;
543 p_sys->p_pgrm = p_pgrm;
545 /* Update "program" */
546 val.i_int = p_pgrm->i_id;
547 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
550 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
551 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
552 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
553 for( i = 0; i < p_sys->i_es; i++ )
555 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
556 EsOutESVarUpdate( out, p_sys->es[i], false );
557 EsOutSelect( out, p_sys->es[i], false );
560 /* Update now playing */
561 input_item_SetNowPlaying( p_input->p->input.p_item,
562 p_pgrm->psz_now_playing );
563 input_item_SetPublisher( p_input->p->input.p_item,
564 p_pgrm->psz_publisher );
566 var_SetBool( p_sys->p_input, "intf-change", true );
572 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
574 es_out_sys_t *p_sys = out->p_sys;
575 input_thread_t *p_input = p_sys->p_input;
578 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
579 if( !p_pgrm ) return NULL;
582 p_pgrm->i_id = i_group;
584 p_pgrm->b_selected = false;
585 p_pgrm->psz_name = NULL;
586 p_pgrm->psz_now_playing = NULL;
587 p_pgrm->psz_publisher = NULL;
588 p_pgrm->p_epg = NULL;
589 input_ClockInit( &p_pgrm->clock, false, p_input->p->input.i_cr_average, p_sys->i_rate );
592 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
594 /* Update "program" variable */
596 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
598 if( i_group == var_GetInteger( p_input, "program" ) )
600 EsOutProgramSelect( out, p_pgrm );
604 var_SetBool( p_sys->p_input, "intf-change", true );
612 static int EsOutProgramDel( es_out_t *out, int i_group )
614 es_out_sys_t *p_sys = out->p_sys;
615 input_thread_t *p_input = p_sys->p_input;
616 es_out_pgrm_t *p_pgrm = NULL;
620 for( i = 0; i < p_sys->i_pgrm; i++ )
622 if( p_sys->pgrm[i]->i_id == i_group )
624 p_pgrm = p_sys->pgrm[i];
634 msg_Dbg( p_input, "can't delete program %d which still has %i ES",
635 i_group, p_pgrm->i_es );
639 TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
641 /* If program is selected we need to unselect it */
642 if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
644 free( p_pgrm->psz_name );
645 free( p_pgrm->psz_now_playing );
646 free( p_pgrm->psz_publisher );
648 vlc_epg_Delete( p_pgrm->p_epg );
651 /* Update "program" variable */
653 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
655 var_SetBool( p_sys->p_input, "intf-change", true );
662 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
665 if( p_pgrm->psz_name )
666 asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
668 asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
672 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
674 es_out_sys_t *p_sys = out->p_sys;
675 es_out_pgrm_t *p_pgrm = NULL;
676 input_thread_t *p_input = p_sys->p_input;
678 const char *psz_title = NULL;
679 const char *psz_provider = NULL;
682 msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
684 /* Check against empty meta data (empty for what we handle) */
685 if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
686 !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
687 !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
688 vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
693 for( i = 0; i < p_sys->i_pgrm; i++ )
695 if( p_sys->pgrm[i]->i_id == i_group )
697 p_pgrm = p_sys->pgrm[i];
702 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
705 psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
706 psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
708 /* Update the description text of the program */
709 if( psz_title && *psz_title )
714 if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
716 char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
718 /* Remove old entries */
719 input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
720 /* TODO update epg name */
723 free( p_pgrm->psz_name );
724 p_pgrm->psz_name = strdup( psz_title );
726 /* ugly but it works */
728 var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
730 if( psz_provider && *psz_provider )
732 asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
733 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
734 free( text.psz_string );
738 text.psz_string = (char *)psz_title;
739 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
743 psz_cat = EsOutProgramGetMetaName( p_pgrm );
746 if( p_sys->p_pgrm == p_pgrm )
747 input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
748 input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
750 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
751 for( i = 0; ppsz_all_keys[i]; i++ )
753 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
754 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
755 free( ppsz_all_keys[i] );
757 free( ppsz_all_keys );
762 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
767 for( i = 0; i < p_src->i_event; i++ )
769 vlc_epg_event_t *p_evt = p_src->pp_event[i];
773 for( j = 0; j < p_dst->i_event; j++ )
775 if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
780 if( p_dst->pp_event[j]->i_start > p_evt->i_start )
785 vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
788 memset( p_copy, 0, sizeof(vlc_epg_event_t) );
789 p_copy->i_start = p_evt->i_start;
790 p_copy->i_duration = p_evt->i_duration;
791 p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
792 p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
793 p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
794 TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
798 vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
800 /* Keep only 1 old event */
801 if( p_dst->p_current )
803 while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
804 TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
808 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
810 es_out_sys_t *p_sys = out->p_sys;
811 input_thread_t *p_input = p_sys->p_input;
812 es_out_pgrm_t *p_pgrm = NULL;
817 for( i = 0; i < p_sys->i_pgrm; i++ )
819 if( p_sys->pgrm[i]->i_id == i_group )
821 p_pgrm = p_sys->pgrm[i];
826 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
830 p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
831 vlc_epg_Merge( p_pgrm->p_epg, p_epg );
834 psz_cat = EsOutProgramGetMetaName( p_pgrm );
835 #ifdef HAVE_LOCALTIME_R
837 if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
839 input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
840 msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
841 for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
843 const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
844 time_t t_start = (time_t)p_evt->i_start;
848 localtime_r( &t_start, &tm_start );
850 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
851 if( p_evt->psz_short_description || p_evt->psz_description )
852 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
854 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
855 p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
857 input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
859 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
863 /* Update now playing */
864 free( p_pgrm->psz_now_playing );
865 p_pgrm->psz_now_playing = NULL;
866 if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
867 p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
869 if( p_pgrm == p_sys->p_pgrm )
870 input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
872 if( p_pgrm->psz_now_playing )
874 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
875 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
876 p_pgrm->psz_now_playing );
880 input_Control( p_input, INPUT_DEL_INFO, psz_cat,
881 input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
890 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
892 es_out_sys_t *p_sys = out->p_sys;
893 input_thread_t *p_input = p_sys->p_input;
895 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
896 es_out_pgrm_t *p_pgrm = NULL;
899 if( !es ) return NULL;
901 if( fmt->i_group < 0 )
903 msg_Err( p_input, "invalid group number" );
908 /* Search the program */
909 for( i = 0; i < p_sys->i_pgrm; i++ )
911 if( fmt->i_group == p_sys->pgrm[i]->i_id )
913 p_pgrm = p_sys->pgrm[i];
919 /* Create a new one */
920 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
923 /* Increase ref count for program */
928 fmt->i_id = out->p_sys->i_id;
929 es->i_id = fmt->i_id;
931 es_format_Copy( &es->fmt, fmt );
932 es->i_preroll_end = -1;
938 audio_replay_gain_t rg;
940 es->i_channel = p_sys->i_audio;
942 vlc_mutex_lock( &p_input->p->input.p_item->lock );
943 memset( &rg, 0, sizeof(rg) );
944 vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
945 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
947 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
949 if( !es->fmt.audio_replay_gain.pb_peak[i] )
951 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
952 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
954 if( !es->fmt.audio_replay_gain.pb_gain[i] )
956 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
957 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
964 es->i_channel = p_sys->i_video;
965 if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
966 vlc_ureduce( &es->fmt.video.i_frame_rate,
967 &es->fmt.video.i_frame_rate_base,
968 fmt->video.i_frame_rate,
969 fmt->video.i_frame_rate_base, 0 );
973 es->i_channel = p_sys->i_sub;
980 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
981 es->psz_language_code = LanguageGetCode( fmt->psz_language );
983 for( i = 0; i < 4; i++ )
984 es->pb_cc_present[i] = false;
985 es->p_master = false;
987 if( es->p_pgrm == p_sys->p_pgrm )
988 EsOutESVarUpdate( out, es, false );
990 /* Select it if needed */
991 EsOutSelect( out, es, false );
994 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
995 p_sys->i_id++; /* always incremented */
1009 EsOutAddInfo( out, es );
1014 static bool EsIsSelected( es_out_id_t *es )
1018 bool b_decode = false;
1019 if( es->p_master->p_dec )
1021 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1022 if( i_channel != -1 )
1023 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1029 return es->p_dec != NULL;
1032 static void EsSelect( es_out_t *out, es_out_id_t *es )
1034 es_out_sys_t *p_sys = out->p_sys;
1035 input_thread_t *p_input = p_sys->p_input;
1037 const char *psz_var;
1039 if( EsIsSelected( es ) )
1041 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1048 if( !es->p_master->p_dec )
1051 i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1052 if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1057 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1059 if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1061 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1066 else if( es->fmt.i_cat == AUDIO_ES )
1068 var_Get( p_input, "audio", &val );
1069 if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1071 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1076 if( es->fmt.i_cat == SPU_ES )
1078 var_Get( p_input, "spu", &val );
1079 if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1081 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1087 es->i_preroll_end = -1;
1088 es->p_dec = input_DecoderNew( p_input, &es->fmt, false );
1089 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1093 if( es->fmt.i_cat == VIDEO_ES )
1094 psz_var = "video-es";
1095 else if( es->fmt.i_cat == AUDIO_ES )
1096 psz_var = "audio-es";
1097 else if( es->fmt.i_cat == SPU_ES )
1102 /* Mark it as selected */
1103 val.i_int = es->i_id;
1104 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1106 var_SetBool( p_sys->p_input, "intf-change", true );
1109 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1111 es_out_sys_t *p_sys = out->p_sys;
1112 input_thread_t *p_input = p_sys->p_input;
1114 const char *psz_var;
1116 if( !EsIsSelected( es ) )
1118 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1124 if( es->p_master->p_dec )
1126 int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1127 if( i_channel != -1 )
1128 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1133 const int i_spu_id = var_GetInteger( p_input, "spu-es");
1135 for( i = 0; i < 4; i++ )
1137 if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1140 if( i_spu_id == es->pp_cc_es[i]->i_id )
1142 /* Force unselection of the CC */
1144 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1146 var_SetBool( p_sys->p_input, "intf-change", true );
1148 EsOutDel( out, es->pp_cc_es[i] );
1150 es->pb_cc_present[i] = false;
1152 input_DecoderDelete( es->p_dec );
1160 if( es->p_dec == NULL )
1162 if( es->fmt.i_cat == VIDEO_ES )
1163 psz_var = "video-es";
1164 else if( es->fmt.i_cat == AUDIO_ES )
1165 psz_var = "audio-es";
1166 else if( es->fmt.i_cat == SPU_ES )
1171 /* Mark it as unselected */
1173 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1175 var_SetBool( p_sys->p_input, "intf-change", true );
1179 * Select an ES given the current mode
1180 * XXX: you need to take a the lock before (stream.stream_lock)
1182 * \param out The es_out structure
1183 * \param es es_out_id structure
1184 * \param b_force ...
1187 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1189 es_out_sys_t *p_sys = out->p_sys;
1191 int i_cat = es->fmt.i_cat;
1193 if( !p_sys->b_active ||
1194 ( !b_force && es->fmt.i_priority < 0 ) )
1199 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1201 if( !EsIsSelected( es ) )
1202 EsSelect( out, es );
1204 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1208 var_Get( p_sys->p_input, "programs", &val );
1209 for ( i = 0; i < val.p_list->i_count; i++ )
1211 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1213 if( !EsIsSelected( es ) )
1214 EsSelect( out, es );
1218 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1220 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1224 if( es->p_pgrm != p_sys->p_pgrm )
1227 if( i_cat == AUDIO_ES )
1229 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1230 es->psz_language_code );
1232 if( p_sys->p_es_audio &&
1233 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1235 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1236 p_sys->p_es_audio->psz_language_code );
1238 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1240 i_wanted = es->i_channel;
1244 /* Select audio if (no audio selected yet)
1245 * - no audio-language
1246 * - no audio code for the ES
1247 * - audio code in the requested list */
1249 !strcmp( es->psz_language_code, "??" ) ||
1250 !p_sys->ppsz_audio_language )
1251 i_wanted = es->i_channel;
1254 if( p_sys->i_audio_last >= 0 )
1255 i_wanted = p_sys->i_audio_last;
1257 if( p_sys->i_audio_id >= 0 )
1259 if( es->i_id == p_sys->i_audio_id )
1260 i_wanted = es->i_channel;
1265 else if( i_cat == SPU_ES )
1267 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1268 es->psz_language_code );
1270 if( p_sys->p_es_sub &&
1271 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1273 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1274 p_sys->p_es_sub->psz_language_code );
1276 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1277 idx1, es->psz_language_code, idx2,
1278 p_sys->p_es_sub->psz_language_code );
1280 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1282 /* We found a SPU that matches our language request */
1283 i_wanted = es->i_channel;
1285 else if( idx1 >= 0 )
1287 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1288 idx1, es->psz_language_code );
1290 i_wanted = es->i_channel;
1292 else if( p_sys->i_default_sub_id >= 0 )
1294 if( es->i_id == p_sys->i_default_sub_id )
1295 i_wanted = es->i_channel;
1298 if( p_sys->i_sub_last >= 0 )
1299 i_wanted = p_sys->i_sub_last;
1301 if( p_sys->i_sub_id >= 0 )
1303 if( es->i_id == p_sys->i_sub_id )
1304 i_wanted = es->i_channel;
1309 else if( i_cat == VIDEO_ES )
1311 i_wanted = es->i_channel;
1314 if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1315 EsSelect( out, es );
1318 /* FIXME TODO handle priority here */
1319 if( EsIsSelected( es ) )
1321 if( i_cat == AUDIO_ES )
1323 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1324 p_sys->p_es_audio &&
1325 p_sys->p_es_audio != es &&
1326 EsIsSelected( p_sys->p_es_audio ) )
1328 EsUnselect( out, p_sys->p_es_audio, false );
1330 p_sys->p_es_audio = es;
1332 else if( i_cat == SPU_ES )
1334 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1336 p_sys->p_es_sub != es &&
1337 EsIsSelected( p_sys->p_es_sub ) )
1339 EsUnselect( out, p_sys->p_es_sub, false );
1341 p_sys->p_es_sub = es;
1343 else if( i_cat == VIDEO_ES )
1345 p_sys->p_es_video = es;
1351 * Send a block for the given es_out
1353 * \param out the es_out to send from
1354 * \param es the es_out_id
1355 * \param p_block the data block to send
1357 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1359 es_out_sys_t *p_sys = out->p_sys;
1360 input_thread_t *p_input = p_sys->p_input;
1361 es_out_pgrm_t *p_pgrm = es->p_pgrm;
1365 if( es->fmt.i_cat == AUDIO_ES )
1366 i_delay = p_sys->i_audio_delay;
1367 else if( es->fmt.i_cat == SPU_ES )
1368 i_delay = p_sys->i_spu_delay;
1372 if( libvlc_stats (p_input) )
1374 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1375 stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1376 p_block->i_buffer, &i_total );
1377 stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1378 (float)i_total, NULL );
1379 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1382 /* Mark preroll blocks */
1383 if( es->i_preroll_end >= 0 )
1385 int64_t i_date = p_block->i_pts;
1387 i_date = p_block->i_dts;
1389 if( i_date < es->i_preroll_end )
1390 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1392 es->i_preroll_end = -1;
1395 if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1397 p_block->i_dts += i_delay;
1399 else if( p_block->i_dts > 0 )
1402 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1404 if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1406 p_block->i_pts += i_delay;
1408 else if( p_block->i_pts > 0 )
1411 input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1413 if ( p_block->i_rate == INPUT_RATE_DEFAULT &&
1414 es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1416 mtime_t current_date = mdate();
1418 || p_block->i_pts > current_date + 10000000
1419 || current_date > p_block->i_pts )
1421 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1422 * for teletext streams. */
1423 p_block->i_pts = current_date + 400000
1424 + p_input->i_pts_delay + i_delay;
1428 p_block->i_rate = p_sys->i_rate;
1430 /* TODO handle mute */
1432 ( es->fmt.i_cat != AUDIO_ES ||
1433 ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1434 p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1437 bool b_cc_new = false;
1439 input_DecoderDecode( es->p_dec, p_block );
1441 /* Check CC status */
1442 input_DecoderIsCcPresent( es->p_dec, pb_cc );
1443 for( i = 0; i < 4; i++ )
1445 static const vlc_fourcc_t fcc[4] = {
1446 VLC_FOURCC('c', 'c', '1', ' '),
1447 VLC_FOURCC('c', 'c', '2', ' '),
1448 VLC_FOURCC('c', 'c', '3', ' '),
1449 VLC_FOURCC('c', 'c', '4', ' '),
1451 static const char ppsz_description[4][18] = {
1452 N_("Closed captions 1"),
1453 N_("Closed captions 2"),
1454 N_("Closed captions 3"),
1455 N_("Closed captions 4"),
1459 if( es->pb_cc_present[i] || !pb_cc[i] )
1461 msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1463 es_format_Init( &fmt, SPU_ES, fcc[i] );
1464 fmt.i_group = es->fmt.i_group;
1465 fmt.psz_description = strdup( _(ppsz_description[i] ) );
1466 es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1467 es->pp_cc_es[i]->p_master = es;
1468 es_format_Clean( &fmt );
1471 es->pb_cc_present[i] = true;
1475 var_SetBool( p_sys->p_input, "intf-change", true );
1479 block_Release( p_block );
1485 /*****************************************************************************
1487 *****************************************************************************/
1488 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1490 es_out_sys_t *p_sys = out->p_sys;
1491 bool b_reselect = false;
1494 /* We don't try to reselect */
1497 while( !out->p_sys->p_input->b_die && es->p_dec )
1499 if( input_DecoderEmpty( es->p_dec ) )
1503 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1506 if( es->p_pgrm == p_sys->p_pgrm )
1507 EsOutESVarUpdate( out, es, true );
1509 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1512 if( es->p_pgrm->i_es == 0 )
1514 msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1517 if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1518 p_sys->p_es_sub == es ) b_reselect = true;
1520 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1521 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1522 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
1524 switch( es->fmt.i_cat )
1537 /* Re-select another track when needed */
1539 for( i = 0; i < p_sys->i_es; i++ )
1541 if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1542 EsOutSelect( out, p_sys->es[i], false );
1545 free( es->psz_language );
1546 free( es->psz_language_code );
1548 es_format_Clean( &es->fmt );
1554 * Control query handler
1556 * \param out the es_out to control
1557 * \param i_query A es_out query as defined in include/ninput.h
1558 * \param args a variable list of arguments for the query
1559 * \return VLC_SUCCESS or an error code
1561 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1563 es_out_sys_t *p_sys = out->p_sys;
1571 case ES_OUT_SET_ES_STATE:
1572 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1573 b = (bool) va_arg( args, int );
1574 if( b && !EsIsSelected( es ) )
1576 EsSelect( out, es );
1577 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1579 else if( !b && EsIsSelected( es ) )
1581 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1586 case ES_OUT_GET_ES_STATE:
1587 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1588 pb = (bool*) va_arg( args, bool * );
1590 *pb = EsIsSelected( es );
1593 case ES_OUT_SET_ACTIVE:
1595 b = (bool) va_arg( args, int );
1596 p_sys->b_active = b;
1599 var_SetBool( p_sys->p_input, "intf-change", true );
1603 case ES_OUT_GET_ACTIVE:
1604 pb = (bool*) va_arg( args, bool * );
1605 *pb = p_sys->b_active;
1608 case ES_OUT_SET_MODE:
1609 i = (int) va_arg( args, int );
1610 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1611 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1615 /* Reapply policy mode */
1616 for( i = 0; i < p_sys->i_es; i++ )
1618 if( EsIsSelected( p_sys->es[i] ) )
1620 EsUnselect( out, p_sys->es[i],
1621 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1624 for( i = 0; i < p_sys->i_es; i++ )
1626 EsOutSelect( out, p_sys->es[i], false );
1630 return VLC_EGENERIC;
1632 case ES_OUT_GET_MODE:
1633 pi = (int*) va_arg( args, int* );
1634 *pi = p_sys->i_mode;
1638 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1639 /* Special case NULL, NULL+i_cat */
1642 for( i = 0; i < p_sys->i_es; i++ )
1644 if( EsIsSelected( p_sys->es[i] ) )
1645 EsUnselect( out, p_sys->es[i],
1646 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1649 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1651 for( i = 0; i < p_sys->i_es; i++ )
1653 if( p_sys->es[i]->fmt.i_cat == AUDIO_ES &&
1654 EsIsSelected( p_sys->es[i] ) )
1655 EsUnselect( out, p_sys->es[i],
1656 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1659 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1661 for( i = 0; i < p_sys->i_es; i++ )
1663 if( p_sys->es[i]->fmt.i_cat == VIDEO_ES &&
1664 EsIsSelected( p_sys->es[i] ) )
1665 EsUnselect( out, p_sys->es[i],
1666 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1669 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1671 for( i = 0; i < p_sys->i_es; i++ )
1673 if( p_sys->es[i]->fmt.i_cat == SPU_ES &&
1674 EsIsSelected( p_sys->es[i] ) )
1675 EsUnselect( out, p_sys->es[i],
1676 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1681 for( i = 0; i < p_sys->i_es; i++ )
1683 if( es == p_sys->es[i] )
1685 EsOutSelect( out, es, true );
1692 event.type = vlc_InputSelectedStreamChanged;
1693 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
1697 case ES_OUT_SET_DEFAULT:
1699 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1703 /*p_sys->i_default_video_id = -1;*/
1704 /*p_sys->i_default_audio_id = -1;*/
1705 p_sys->i_default_sub_id = -1;
1707 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1709 /*p_sys->i_default_video_id = -1;*/
1711 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1713 /*p_sys->i_default_audio_id = -1;*/
1715 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1717 p_sys->i_default_sub_id = -1;
1721 /*if( es->fmt.i_cat == VIDEO_ES )
1722 p_sys->i_default_video_id = es->i_id;
1724 if( es->fmt.i_cat == AUDIO_ES )
1725 p_sys->i_default_audio_id = es->i_id;
1727 if( es->fmt.i_cat == SPU_ES )
1728 p_sys->i_default_sub_id = es->i_id;
1733 case ES_OUT_SET_PCR:
1734 case ES_OUT_SET_GROUP_PCR:
1736 es_out_pgrm_t *p_pgrm = NULL;
1740 if( i_query == ES_OUT_SET_PCR )
1742 p_pgrm = p_sys->p_pgrm;
1747 i_group = (int)va_arg( args, int );
1748 for( i = 0; i < p_sys->i_pgrm; i++ )
1750 if( p_sys->pgrm[i]->i_id == i_group )
1752 p_pgrm = p_sys->pgrm[i];
1757 if( p_pgrm == NULL )
1758 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1760 i_pcr = (int64_t)va_arg( args, int64_t );
1761 /* search program */
1762 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1766 case ES_OUT_RESET_PCR:
1767 for( i = 0; i < p_sys->i_pgrm; i++ )
1768 input_ClockResetPCR( &p_sys->pgrm[i]->clock );
1774 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1775 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1776 *pi_ts = input_ClockGetTS( p_sys->p_input,
1777 &p_sys->p_pgrm->clock, i_ts );
1780 return VLC_EGENERIC;
1782 case ES_OUT_GET_GROUP:
1783 pi = (int*) va_arg( args, int* );
1785 *pi = p_sys->p_pgrm->i_id;
1787 *pi = -1; /* FIXME */
1790 case ES_OUT_SET_GROUP:
1793 i = (int) va_arg( args, int );
1794 for( j = 0; j < p_sys->i_pgrm; j++ )
1796 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1797 if( p_pgrm->i_id == i )
1799 EsOutProgramSelect( out, p_pgrm );
1803 return VLC_EGENERIC;
1806 case ES_OUT_SET_FMT:
1808 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1809 * to update the p_extra data */
1811 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1812 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1813 if( es == NULL ) return VLC_EGENERIC;
1815 if( p_fmt->i_extra )
1817 es->fmt.i_extra = p_fmt->i_extra;
1818 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1819 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1821 if( !es->p_dec ) return VLC_SUCCESS;
1824 input_DecoderDelete( es->p_dec );
1825 es->p_dec = input_DecoderNew( p_sys->p_input,
1829 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1830 es->p_dec->fmt_in.p_extra =
1831 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1832 memcpy( es->p_dec->fmt_in.p_extra,
1833 p_fmt->p_extra, p_fmt->i_extra );
1840 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1844 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1845 i_date = (int64_t)va_arg( args, int64_t );
1847 if( !es || !es->p_dec )
1848 return VLC_EGENERIC;
1850 /* XXX We should call input_ClockGetTS but PCR has been reseted
1851 * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1852 * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1853 es->i_preroll_end = i_date;
1857 case ES_OUT_SET_GROUP_META:
1859 int i_group = (int)va_arg( args, int );
1860 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1862 EsOutProgramMeta( out, i_group, p_meta );
1865 case ES_OUT_SET_GROUP_EPG:
1867 int i_group = (int)va_arg( args, int );
1868 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1870 EsOutProgramEpg( out, i_group, p_epg );
1873 case ES_OUT_DEL_GROUP:
1875 int i_group = (int)va_arg( args, int );
1877 return EsOutProgramDel( out, i_group );
1881 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1882 return VLC_EGENERIC;
1886 /****************************************************************************
1887 * LanguageGetName: try to expend iso639 into plain name
1888 ****************************************************************************/
1889 static char *LanguageGetName( const char *psz_code )
1891 const iso639_lang_t *pl;
1893 if( psz_code == NULL )
1895 return strdup( "" );
1898 if( strlen( psz_code ) == 2 )
1900 pl = GetLang_1( psz_code );
1902 else if( strlen( psz_code ) == 3 )
1904 pl = GetLang_2B( psz_code );
1905 if( !strcmp( pl->psz_iso639_1, "??" ) )
1907 pl = GetLang_2T( psz_code );
1912 return strdup( psz_code );
1915 if( !strcmp( pl->psz_iso639_1, "??" ) )
1917 return strdup( psz_code );
1921 if( *pl->psz_native_name )
1923 return strdup( pl->psz_native_name );
1925 return strdup( pl->psz_eng_name );
1929 /* Get a 2 char code */
1930 static char *LanguageGetCode( const char *psz_lang )
1932 const iso639_lang_t *pl;
1934 if( psz_lang == NULL || *psz_lang == '\0' )
1935 return strdup("??");
1937 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1939 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1940 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1941 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1942 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1943 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1947 if( pl->psz_iso639_1 != NULL )
1948 return strdup( pl->psz_iso639_1 );
1950 return strdup("??");
1953 static char **LanguageSplit( const char *psz_langs )
1960 if( psz_langs == NULL ) return NULL;
1962 psz_parser = psz_dup = strdup(psz_langs);
1964 while( psz_parser && *psz_parser )
1969 psz = strchr(psz_parser, ',' );
1970 if( psz ) *psz++ = '\0';
1972 if( !strcmp( psz_parser, "any" ) )
1974 TAB_APPEND( i_psz, ppsz, strdup("any") );
1978 psz_code = LanguageGetCode( psz_parser );
1979 if( strcmp( psz_code, "??" ) )
1981 TAB_APPEND( i_psz, ppsz, psz_code );
1994 TAB_APPEND( i_psz, ppsz, NULL );
2001 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2005 if( !ppsz_langs || !psz_lang ) return -1;
2007 for( i = 0; ppsz_langs[i]; i++ )
2009 if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2010 !strcasecmp( ppsz_langs[i], "any" ) )
2019 /****************************************************************************
2021 * - add meta info to the playlist item
2022 ****************************************************************************/
2023 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2025 es_out_sys_t *p_sys = out->p_sys;
2026 input_thread_t *p_input = p_sys->p_input;
2027 es_format_t *fmt = &es->fmt;
2031 /* Add stream info */
2032 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
2034 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2035 "%.4s", (char*)&fmt->i_codec );
2037 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2038 "%s", es->psz_language );
2040 /* Add information */
2041 switch( fmt->i_cat )
2044 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2045 _("Type"), _("Audio") );
2047 if( fmt->audio.i_channels > 0 )
2048 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2049 "%u", fmt->audio.i_channels );
2051 if( fmt->audio.i_rate > 0 )
2053 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2054 _("%u Hz"), fmt->audio.i_rate );
2055 var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2058 if( fmt->audio.i_bitspersample > 0 )
2059 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2060 _("Bits per sample"), "%u",
2061 fmt->audio.i_bitspersample );
2063 if( fmt->i_bitrate > 0 )
2065 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2066 _("%u kb/s"), fmt->i_bitrate / 1000 );
2067 var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2072 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2073 _("Type"), _("Video") );
2075 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2076 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2077 _("Resolution"), "%ux%u",
2078 fmt->video.i_width, fmt->video.i_height );
2080 if( fmt->video.i_visible_width > 0 &&
2081 fmt->video.i_visible_height > 0 )
2082 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2083 _("Display resolution"), "%ux%u",
2084 fmt->video.i_visible_width,
2085 fmt->video.i_visible_height);
2086 if( fmt->video.i_frame_rate > 0 &&
2087 fmt->video.i_frame_rate_base > 0 )
2089 div = lldiv( (float)fmt->video.i_frame_rate /
2090 fmt->video.i_frame_rate_base * 1000000,
2092 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2093 _("Frame rate"), "%"PRId64".%06u",
2094 div.quot, (unsigned int )div.rem );
2099 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2100 _("Type"), _("Subtitle") );