1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
31 #include <vlc/decoder.h>
33 #include "input_internal.h"
35 #include "vlc_playlist.h"
37 /* FIXME we should find a better way than including that */
38 #include "../misc/iso-639_def.h"
40 /*****************************************************************************
42 *****************************************************************************/
48 /* Number of es for this pgrm */
51 vlc_bool_t b_selected;
53 /* Clock for this program */
62 es_out_pgrm_t *p_pgrm;
65 int64_t i_preroll_end;
67 /* Channel in the track type */
71 char *psz_language_code;
77 input_thread_t *p_input;
82 es_out_pgrm_t **pp_selected_pgrm; /* --programs */
83 es_out_pgrm_t *p_pgrm; /* Master program */
102 char **ppsz_audio_language;
103 char **ppsz_sub_language;
105 /* current main es */
106 es_out_id_t *p_es_audio;
107 es_out_id_t *p_es_video;
108 es_out_id_t *p_es_sub;
111 int64_t i_audio_delay;
115 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
116 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
117 static void EsOutDel ( es_out_t *, es_out_id_t * );
118 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
119 static int EsOutControl( es_out_t *, int i_query, va_list );
121 static void EsOutAddInfo( es_out_t *, es_out_id_t *es );
123 static void EsSelect( es_out_t *out, es_out_id_t *es );
124 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
125 static char *LanguageGetName( const char *psz_code );
126 static char *LanguageGetCode( const char *psz_lang );
127 static char **LanguageSplit( const char *psz_langs );
128 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
130 /*****************************************************************************
132 *****************************************************************************/
133 es_out_t *input_EsOutNew( input_thread_t *p_input )
135 es_out_t *out = malloc( sizeof( es_out_t ) );
136 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
140 out->pf_add = EsOutAdd;
141 out->pf_send = EsOutSend;
142 out->pf_del = EsOutDel;
143 out->pf_control = EsOutControl;
146 p_sys->p_input = p_input;
148 p_sys->b_active = VLC_FALSE;
149 p_sys->i_mode = ES_OUT_MODE_AUTO;
154 p_sys->p_pgrm = NULL;
165 var_Get( p_input, "audio-track", &val );
166 p_sys->i_audio_last = val.i_int;
168 var_Get( p_input, "sub-track", &val );
169 p_sys->i_sub_last = val.i_int;
171 var_Get( p_input, "audio-language", &val );
172 p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
173 if( p_sys->ppsz_audio_language )
175 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
176 msg_Dbg( p_input, "Select audio in language[%d] %s",
177 i, p_sys->ppsz_audio_language[i] );
180 var_Get( p_input, "sub-language", &val );
181 p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
182 if( p_sys->ppsz_sub_language )
184 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
185 msg_Dbg( p_input, "Select subtitle in language[%d] %s",
186 i, p_sys->ppsz_sub_language[i] );
190 p_sys->p_es_audio = NULL;
191 p_sys->p_es_video = NULL;
192 p_sys->p_es_sub = NULL;
194 p_sys->i_audio_delay= 0;
195 p_sys->i_spu_delay = 0;
200 /*****************************************************************************
202 *****************************************************************************/
203 void input_EsOutDelete( es_out_t *out )
205 es_out_sys_t *p_sys = out->p_sys;
208 for( i = 0; i < p_sys->i_es; i++ )
210 if( p_sys->es[i]->p_dec )
212 input_DecoderDelete( p_sys->es[i]->p_dec );
214 if( p_sys->es[i]->psz_language )
215 free( p_sys->es[i]->psz_language );
216 if( p_sys->es[i]->psz_language_code )
217 free( p_sys->es[i]->psz_language_code );
218 es_format_Clean( &p_sys->es[i]->fmt );
220 free( p_sys->es[i] );
222 if( p_sys->ppsz_audio_language )
224 for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
225 free( p_sys->ppsz_audio_language[i] );
226 free( p_sys->ppsz_audio_language );
228 if( p_sys->ppsz_sub_language )
230 for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
231 free( p_sys->ppsz_sub_language[i] );
232 free( p_sys->ppsz_sub_language );
238 for( i = 0; i < p_sys->i_pgrm; i++ )
240 free( p_sys->pgrm[i] );
249 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
254 /* Special HACK, -i_id is tha cat of the stream */
255 return (es_out_id_t*)((uint8_t*)NULL-i_id);
258 for( i = 0; i < out->p_sys->i_es; i++ )
260 if( out->p_sys->es[i]->i_id == i_id )
261 return out->p_sys->es[i];
266 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
268 es_out_sys_t *p_sys = out->p_sys;
271 for( i = 0; i < p_sys->i_es; i++ )
273 es_out_id_t *es = p_sys->es[i];
275 /* Send a dummy block to let decoder know that
276 * there is a discontinuity */
277 if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
279 input_DecoderDiscontinuity( es->p_dec );
284 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
286 es_out_sys_t *p_sys = out->p_sys;
288 if( i_cat == AUDIO_ES )
289 p_sys->i_audio_delay = i_delay;
290 else if( i_cat == SPU_ES )
291 p_sys->i_spu_delay = i_delay;
294 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
296 es_out_sys_t *p_sys = out->p_sys;
299 for( i = 0; i < p_sys->i_es; i++ )
301 es_out_id_t *es = p_sys->es[i];
303 if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
309 /*****************************************************************************
311 *****************************************************************************/
312 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
313 vlc_bool_t b_delete )
315 es_out_sys_t *p_sys = out->p_sys;
316 input_thread_t *p_input = p_sys->p_input;
317 vlc_value_t val, text;
321 if( es->fmt.i_cat == AUDIO_ES )
322 psz_var = "audio-es";
323 else if( es->fmt.i_cat == VIDEO_ES )
324 psz_var = "video-es";
325 else if( es->fmt.i_cat == SPU_ES )
332 val.i_int = es->i_id;
333 var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
334 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
338 /* Get the number of ES already added */
339 var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
344 /* First one, we need to add the "Disable" choice */
345 val2.i_int = -1; text.psz_string = _("Disable");
346 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
350 /* Take care of the ES description */
351 if( es->fmt.psz_description && *es->fmt.psz_description )
353 if( es->psz_language && *es->psz_language )
355 text.psz_string = malloc( strlen( es->fmt.psz_description) + strlen( es->psz_language ) + 10 );
356 sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description, es->psz_language );
358 else text.psz_string = strdup( es->fmt.psz_description );
362 if( es->psz_language && *es->psz_language )
365 text.psz_string = malloc( strlen( _("Track %i") )+ strlen( es->psz_language ) + 30 );
366 asprintf( &temp, _("Track %i"), val.i_int );
367 sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
372 text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
373 sprintf( text.psz_string, _("Track %i"), val.i_int );
377 val.i_int = es->i_id;
378 var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
380 free( text.psz_string );
382 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
385 /* EsOutProgramSelect:
386 * Select a program and update the object variable
388 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
390 es_out_sys_t *p_sys = out->p_sys;
391 input_thread_t *p_input = p_sys->p_input;
395 if( p_sys->p_pgrm == p_pgrm )
396 return; /* Nothing to do */
400 es_out_pgrm_t *old = p_sys->p_pgrm;
401 msg_Dbg( p_input, "Unselecting program id=%d", old->i_id );
403 for( i = 0; i < p_sys->i_es; i++ )
405 if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
406 p_sys->i_mode != ES_OUT_MODE_ALL )
407 EsUnselect( out, p_sys->es[i], VLC_TRUE );
410 p_sys->p_es_audio = NULL;
411 p_sys->p_es_sub = NULL;
412 p_sys->p_es_video = NULL;
415 msg_Dbg( p_input, "Selecting program id=%d", p_pgrm->i_id );
417 /* Mark it selected */
418 p_pgrm->b_selected = VLC_TRUE;
420 /* Switch master stream */
421 if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
423 p_sys->p_pgrm->clock.b_master = VLC_FALSE;
425 p_pgrm->clock.b_master = VLC_TRUE;
426 p_sys->p_pgrm = p_pgrm;
428 /* Update "program" */
429 val.i_int = p_pgrm->i_id;
430 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
433 var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
434 var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
435 var_Change( p_input, "spu-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
436 for( i = 0; i < p_sys->i_es; i++ )
438 if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
439 EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
440 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
443 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
449 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
451 es_out_sys_t *p_sys = out->p_sys;
452 input_thread_t *p_input = p_sys->p_input;
455 es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
458 p_pgrm->i_id = i_group;
460 p_pgrm->b_selected = VLC_FALSE;
461 input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->input.i_cr_average );
464 TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
466 /* Update "program" variable */
468 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
470 if( i_group == var_GetInteger( p_input, "program" ) )
472 EsOutProgramSelect( out, p_pgrm );
476 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
484 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
486 es_out_sys_t *p_sys = out->p_sys;
487 input_thread_t *p_input = p_sys->p_input;
489 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
490 es_out_pgrm_t *p_pgrm = NULL;
493 if( fmt->i_group < 0 )
495 msg_Err( p_input, "invalid group number" );
499 /* Search the program */
500 for( i = 0; i < p_sys->i_pgrm; i++ )
502 if( fmt->i_group == p_sys->pgrm[i]->i_id )
504 p_pgrm = p_sys->pgrm[i];
510 /* Create a new one */
511 p_pgrm = EsOutProgramAdd( out, fmt->i_group );
514 /* Increase ref count for program */
519 fmt->i_id = out->p_sys->i_id;
520 es->i_id = fmt->i_id;
522 es_format_Copy( &es->fmt, fmt );
523 es->i_preroll_end = -1;
528 es->i_channel = p_sys->i_audio;
532 es->i_channel = p_sys->i_video;
536 es->i_channel = p_sys->i_sub;
543 es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
544 es->psz_language_code = LanguageGetCode( fmt->psz_language );
547 if( es->p_pgrm == p_sys->p_pgrm )
548 EsOutESVarUpdate( out, es, VLC_FALSE );
550 /* Select it if needed */
551 EsOutSelect( out, es, VLC_FALSE );
554 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
555 p_sys->i_id++; /* always incremented */
569 EsOutAddInfo( out, es );
574 static void EsSelect( es_out_t *out, es_out_id_t *es )
576 es_out_sys_t *p_sys = out->p_sys;
577 input_thread_t *p_input = p_sys->p_input;
583 msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
587 if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
589 if( !var_GetBool( p_input, "video" ) ||
590 ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
592 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
597 else if( es->fmt.i_cat == AUDIO_ES )
599 var_Get( p_input, "audio", &val );
600 if( !var_GetBool( p_input, "audio" ) ||
601 ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
603 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
609 es->i_preroll_end = -1;
610 es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
611 if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
614 if( es->fmt.i_cat == VIDEO_ES )
615 psz_var = "video-es";
616 else if( es->fmt.i_cat == AUDIO_ES )
617 psz_var = "audio-es";
618 else if( es->fmt.i_cat == SPU_ES )
623 /* Mark it as selected */
624 val.i_int = es->i_id;
625 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
628 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
631 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
633 es_out_sys_t *p_sys = out->p_sys;
634 input_thread_t *p_input = p_sys->p_input;
638 if( es->p_dec == NULL )
640 msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
644 input_DecoderDelete( es->p_dec );
651 if( es->p_dec == NULL )
653 if( es->fmt.i_cat == VIDEO_ES )
654 psz_var = "video-es";
655 else if( es->fmt.i_cat == AUDIO_ES )
656 psz_var = "audio-es";
657 else if( es->fmt.i_cat == SPU_ES )
662 /* Mark it as selected */
664 var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
666 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
670 * Select an ES given the current mode
671 * XXX: you need to take a the lock before (stream.stream_lock)
673 * \param out The es_out structure
674 * \param es es_out_id structure
678 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
680 es_out_sys_t *p_sys = out->p_sys;
682 int i_cat = es->fmt.i_cat;
684 if( !p_sys->b_active ||
685 ( !b_force && es->fmt.i_priority < 0 ) )
690 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
695 else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
699 var_Get( p_sys->p_input, "programs", &val );
700 for ( i = 0; i < val.p_list->i_count; i++ )
702 if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
709 var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
711 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
715 if( es->p_pgrm != p_sys->p_pgrm )
718 if( i_cat == AUDIO_ES )
720 int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
721 es->psz_language_code );
723 if( p_sys->p_es_audio &&
724 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
726 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
727 p_sys->p_es_audio->psz_language_code );
729 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
731 i_wanted = es->i_channel;
735 /* Select audio if (no audio selected yet)
736 * - no audio-language
737 * - no audio code for the ES
738 * - audio code in the requested list */
740 !strcmp( es->psz_language_code, "??" ) ||
741 !p_sys->ppsz_audio_language )
742 i_wanted = es->i_channel;
745 if( p_sys->i_audio_last >= 0 )
746 i_wanted = p_sys->i_audio_last;
748 else if( i_cat == SPU_ES )
750 int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
751 es->psz_language_code );
753 if( p_sys->p_es_sub &&
754 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
756 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
757 p_sys->p_es_sub->psz_language_code );
759 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
760 idx1, es->psz_language_code, idx2,
761 p_sys->p_es_sub->psz_language_code );
763 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
765 /* We found a SPU that matches our language request */
766 i_wanted = es->i_channel;
770 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
771 idx1, es->psz_language_code );
773 i_wanted = es->i_channel;
775 if( p_sys->i_sub_last >= 0 )
776 i_wanted = p_sys->i_sub_last;
778 else if( i_cat == VIDEO_ES )
780 i_wanted = es->i_channel;
783 if( i_wanted == es->i_channel && es->p_dec == NULL )
787 /* FIXME TODO handle priority here */
790 if( i_cat == AUDIO_ES )
792 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
794 p_sys->p_es_audio != es &&
795 p_sys->p_es_audio->p_dec )
797 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
799 p_sys->p_es_audio = es;
801 else if( i_cat == SPU_ES )
803 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
805 p_sys->p_es_sub != es &&
806 p_sys->p_es_sub->p_dec )
808 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
810 p_sys->p_es_sub = es;
812 else if( i_cat == VIDEO_ES )
814 p_sys->p_es_video = es;
820 * Send a block for the given es_out
822 * \param out the es_out to send from
823 * \param es the es_out_id
824 * \param p_block the data block to send
826 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
828 es_out_sys_t *p_sys = out->p_sys;
829 input_thread_t *p_input = p_sys->p_input;
830 es_out_pgrm_t *p_pgrm = es->p_pgrm;
833 if( es->fmt.i_cat == AUDIO_ES )
834 i_delay = p_sys->i_audio_delay;
835 else if( es->fmt.i_cat == SPU_ES )
836 i_delay = p_sys->i_spu_delay;
840 /* Mark preroll blocks */
841 if( es->i_preroll_end >= 0 )
843 int64_t i_date = p_block->i_pts;
845 i_date = p_block->i_dts;
847 if( i_date < es->i_preroll_end )
848 p_block->i_flags |= BLOCK_FLAG_PREROLL;
850 es->i_preroll_end = -1;
853 /* +11 -> avoid null value with non null dts/pts */
854 if( p_block->i_dts > 0 )
857 input_ClockGetTS( p_input, &p_pgrm->clock,
858 ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
860 if( p_block->i_pts > 0 )
863 input_ClockGetTS( p_input, &p_pgrm->clock,
864 ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
866 if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
868 mtime_t current_date = mdate();
870 || p_block->i_pts > current_date + 10000000
871 || current_date > p_block->i_pts )
873 /* ETSI EN 300 472 Annex A : do not take into account the PTS
874 * for teletext streams. */
875 p_block->i_pts = current_date + 400000
876 + p_input->i_pts_delay + i_delay;
880 p_block->i_rate = p_input->i_rate;
882 /* TODO handle mute */
883 if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
884 p_input->i_rate == INPUT_RATE_DEFAULT ) )
886 input_DecoderDecode( es->p_dec, p_block );
890 block_Release( p_block );
896 /*****************************************************************************
898 *****************************************************************************/
899 static void EsOutDel( es_out_t *out, es_out_id_t *es )
901 es_out_sys_t *p_sys = out->p_sys;
903 /* We don't try to reselect */
905 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
907 if( es->p_pgrm == p_sys->p_pgrm )
908 EsOutESVarUpdate( out, es, VLC_TRUE );
910 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
913 if( es->p_pgrm->i_es == 0 )
915 msg_Warn( p_sys->p_input, "Program doesn't contain anymore ES, "
919 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
920 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
921 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
923 switch( es->fmt.i_cat )
936 if( es->psz_language )
937 free( es->psz_language );
938 if( es->psz_language_code )
939 free( es->psz_language_code );
941 es_format_Clean( &es->fmt );
947 * Control query handler
949 * \param out the es_out to control
950 * \param i_query A es_out query as defined in include/ninput.h
951 * \param args a variable list of arguments for the query
952 * \return VLC_SUCCESS or an error code
954 static int EsOutControl( es_out_t *out, int i_query, va_list args )
956 es_out_sys_t *p_sys = out->p_sys;
964 case ES_OUT_SET_ES_STATE:
965 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
966 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
967 if( b && es->p_dec == NULL )
970 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
972 else if( !b && es->p_dec )
974 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
979 case ES_OUT_GET_ES_STATE:
980 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
981 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
983 *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
986 case ES_OUT_SET_ACTIVE:
988 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
992 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
996 case ES_OUT_GET_ACTIVE:
997 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
998 *pb = p_sys->b_active;
1001 case ES_OUT_SET_MODE:
1002 i = (int) va_arg( args, int );
1003 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1004 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1008 /* Reapply policy mode */
1009 for( i = 0; i < p_sys->i_es; i++ )
1011 if( p_sys->es[i]->p_dec )
1013 EsUnselect( out, p_sys->es[i],
1014 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1017 for( i = 0; i < p_sys->i_es; i++ )
1019 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1023 return VLC_EGENERIC;
1025 case ES_OUT_GET_MODE:
1026 pi = (int*) va_arg( args, int* );
1027 *pi = p_sys->i_mode;
1031 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1032 /* Special case NULL, NULL+i_cat */
1035 for( i = 0; i < p_sys->i_es; i++ )
1037 if( p_sys->es[i]->p_dec )
1038 EsUnselect( out, p_sys->es[i],
1039 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1042 else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1044 for( i = 0; i < p_sys->i_es; i++ )
1046 if( p_sys->es[i]->p_dec &&
1047 p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1048 EsUnselect( out, p_sys->es[i],
1049 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1052 else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1054 for( i = 0; i < p_sys->i_es; i++ )
1056 if( p_sys->es[i]->p_dec &&
1057 p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1058 EsUnselect( out, p_sys->es[i],
1059 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1062 else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1064 for( i = 0; i < p_sys->i_es; i++ )
1066 if( p_sys->es[i]->p_dec &&
1067 p_sys->es[i]->fmt.i_cat == SPU_ES )
1068 EsUnselect( out, p_sys->es[i],
1069 p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1074 for( i = 0; i < p_sys->i_es; i++ )
1076 if( es == p_sys->es[i] )
1078 EsOutSelect( out, es, VLC_TRUE );
1085 case ES_OUT_SET_PCR:
1086 case ES_OUT_SET_GROUP_PCR:
1088 es_out_pgrm_t *p_pgrm = NULL;
1092 if( i_query == ES_OUT_SET_PCR )
1094 p_pgrm = p_sys->p_pgrm;
1099 i_group = (int)va_arg( args, int );
1100 for( i = 0; i < p_sys->i_pgrm; i++ )
1102 if( p_sys->pgrm[i]->i_id == i_group )
1104 p_pgrm = p_sys->pgrm[i];
1109 if( p_pgrm == NULL )
1110 p_pgrm = EsOutProgramAdd( out, i_group ); /* Create it */
1112 i_pcr = (int64_t)va_arg( args, int64_t );
1113 /* search program */
1114 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1115 input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1116 (i_pcr + 11 ) * 9 / 100);
1120 case ES_OUT_RESET_PCR:
1121 for( i = 0; i < p_sys->i_pgrm; i++ )
1123 p_sys->pgrm[i]->clock.i_synchro_state = SYNCHRO_REINIT;
1124 p_sys->pgrm[i]->clock.last_pts = 0;
1131 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1132 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1133 *pi_ts = input_ClockGetTS( p_sys->p_input,
1134 &p_sys->p_pgrm->clock,
1135 ( i_ts + 11 ) * 9 / 100 );
1138 return VLC_EGENERIC;
1140 case ES_OUT_GET_GROUP:
1141 pi = (int*) va_arg( args, int* );
1143 *pi = p_sys->p_pgrm->i_id;
1145 *pi = -1; /* FIXME */
1148 case ES_OUT_SET_GROUP:
1151 i = (int) va_arg( args, int );
1152 for( j = 0; j < p_sys->i_pgrm; j++ )
1154 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1155 if( p_pgrm->i_id == i )
1157 EsOutProgramSelect( out, p_pgrm );
1161 return VLC_EGENERIC;
1164 case ES_OUT_SET_FMT:
1166 /* This ain't pretty but is need by some demuxers (eg. Ogg )
1167 * to update the p_extra data */
1169 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1170 p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1171 if( es == NULL ) return VLC_EGENERIC;
1173 if( p_fmt->i_extra )
1175 es->fmt.i_extra = p_fmt->i_extra;
1176 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1177 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1179 if( !es->p_dec ) return VLC_SUCCESS;
1181 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1182 es->p_dec->fmt_in.p_extra =
1183 realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1184 memcpy( es->p_dec->fmt_in.p_extra,
1185 p_fmt->p_extra, p_fmt->i_extra );
1191 case ES_OUT_SET_NEXT_DISPLAY_TIME:
1195 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1196 i_date = (int64_t)va_arg( args, int64_t );
1198 if( !es || !es->p_dec )
1199 return VLC_EGENERIC;
1201 es->i_preroll_end = i_date;
1202 input_DecoderPreroll( es->p_dec, i_date );
1208 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1209 return VLC_EGENERIC;
1213 /****************************************************************************
1214 * LanguageGetName: try to expend iso639 into plain name
1215 ****************************************************************************/
1216 static char *LanguageGetName( const char *psz_code )
1218 const iso639_lang_t *pl;
1220 if( psz_code == NULL )
1222 return strdup( "" );
1225 if( strlen( psz_code ) == 2 )
1227 pl = GetLang_1( psz_code );
1229 else if( strlen( psz_code ) == 3 )
1231 pl = GetLang_2B( psz_code );
1232 if( !strcmp( pl->psz_iso639_1, "??" ) )
1234 pl = GetLang_2T( psz_code );
1239 return strdup( psz_code );
1242 if( !strcmp( pl->psz_iso639_1, "??" ) )
1244 return strdup( psz_code );
1248 if( *pl->psz_native_name )
1250 return strdup( pl->psz_native_name );
1252 return strdup( pl->psz_eng_name );
1256 /* Get a 2 char code */
1257 static char *LanguageGetCode( const char *psz_lang )
1259 const iso639_lang_t *pl;
1261 if( psz_lang == NULL || *psz_lang == '\0' )
1262 return strdup("??");
1264 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1266 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1267 !strcasecmp( pl->psz_native_name, psz_lang ) ||
1268 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1269 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1270 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1274 if( pl->psz_iso639_1 != NULL )
1275 return strdup( pl->psz_iso639_1 );
1277 return strdup("??");
1280 static char **LanguageSplit( const char *psz_langs )
1287 if( psz_langs == NULL )
1290 psz_parser = psz_dup = strdup(psz_langs);
1292 while( psz_parser && *psz_parser )
1297 psz = strchr(psz_parser, ',' );
1303 psz_code = LanguageGetCode( psz_parser );
1304 if( strcmp( psz_code, "??" ) )
1306 TAB_APPEND( i_psz, ppsz, psz_code );
1314 TAB_APPEND( i_psz, ppsz, NULL );
1320 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1324 if( !ppsz_langs || !psz_lang )
1327 for( i = 0; ppsz_langs[i]; i++ )
1328 if( !strcasecmp( ppsz_langs[i], psz_lang ) )
1334 /****************************************************************************
1336 * - add meta info to the playlist item
1337 ****************************************************************************/
1338 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1340 es_out_sys_t *p_sys = out->p_sys;
1341 input_thread_t *p_input = p_sys->p_input;
1342 es_format_t *fmt = &es->fmt;
1345 /* Add stream info */
1346 asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1348 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1349 "%.4s", (char*)&fmt->i_codec );
1351 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1352 "%s", es->psz_language );
1354 /* Add information */
1355 switch( fmt->i_cat )
1358 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1359 _("Type"), _("Audio") );
1361 if( fmt->audio.i_channels > 0 )
1362 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1363 "%d", fmt->audio.i_channels );
1365 if( fmt->audio.i_rate > 0 )
1366 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1367 _("%d Hz"), fmt->audio.i_rate );
1369 if( fmt->audio.i_bitspersample > 0 )
1370 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1371 _("Bits per sample"), "%d",
1372 fmt->audio.i_bitspersample );
1374 if( fmt->i_bitrate > 0 )
1375 input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1376 _("%d kb/s"), fmt->i_bitrate / 1000 );
1380 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1381 _("Type"), _("Video") );
1383 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1384 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1385 _("Resolution"), "%dx%d",
1386 fmt->video.i_width, fmt->video.i_height );
1388 if( fmt->video.i_visible_width > 0 &&
1389 fmt->video.i_visible_height > 0 )
1390 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1391 _("Display resolution"), "%dx%d",
1392 fmt->video.i_visible_width,
1393 fmt->video.i_visible_height);
1397 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1398 _("Type"), _("Subtitle") );