]> git.sesse.net Git - vlc/blob - src/input/es_out.c
Documented input_clock_t interface.
[vlc] / src / input / es_out.c
1 /*****************************************************************************
2  * es_out.c: Es Out handler for input.
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <assert.h>
34 #include <vlc_common.h>
35
36 #include <vlc_input.h>
37 #include <vlc_es_out.h>
38 #include <vlc_block.h>
39 #include <vlc_aout.h>
40
41 #include "input_internal.h"
42
43 #include "../stream_output/stream_output.h"
44
45 #include <vlc_iso_lang.h>
46 /* FIXME we should find a better way than including that */
47 #include "../text/iso-639_def.h"
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 typedef struct
53 {
54     /* Program ID */
55     int i_id;
56
57     /* Number of es for this pgrm */
58     int i_es;
59
60     bool b_selected;
61
62     /* Clock for this program */
63     input_clock_t *p_clock;
64
65     char    *psz_name;
66     char    *psz_now_playing;
67     char    *psz_publisher;
68
69     vlc_epg_t *p_epg;
70 } es_out_pgrm_t;
71
72 struct es_out_id_t
73 {
74     /* ES ID */
75     int       i_id;
76     es_out_pgrm_t *p_pgrm;
77
78     /* Misc. */
79     int64_t i_preroll_end;
80
81     /* Channel in the track type */
82     int         i_channel;
83     es_format_t fmt;
84     char        *psz_language;
85     char        *psz_language_code;
86
87     decoder_t   *p_dec;
88     decoder_t   *p_dec_record;
89
90     /* Fields for Video with CC */
91     bool  pb_cc_present[4];
92     es_out_id_t  *pp_cc_es[4];
93
94     /* Field for CC track from a master video */
95     es_out_id_t *p_master;
96 };
97
98 struct es_out_sys_t
99 {
100     input_thread_t *p_input;
101
102     /* all programs */
103     int           i_pgrm;
104     es_out_pgrm_t **pgrm;
105     es_out_pgrm_t **pp_selected_pgrm; /* --programs */
106     es_out_pgrm_t *p_pgrm;  /* Master program */
107
108     /* all es */
109     int         i_id;
110     int         i_es;
111     es_out_id_t **es;
112
113     /* mode gestion */
114     bool  b_active;
115     int         i_mode;
116
117     /* es count */
118     int         i_audio;
119     int         i_video;
120     int         i_sub;
121
122     /* es to select */
123     int         i_audio_last, i_audio_id;
124     int         i_sub_last, i_sub_id;
125     int         i_default_sub_id;   /* As specified in container; if applicable */
126     char        **ppsz_audio_language;
127     char        **ppsz_sub_language;
128
129     /* current main es */
130     es_out_id_t *p_es_audio;
131     es_out_id_t *p_es_video;
132     es_out_id_t *p_es_sub;
133
134     /* delay */
135     int64_t i_audio_delay;
136     int64_t i_spu_delay;
137
138     /* Rate used to rescale ES ts */
139     int         i_rate;
140
141     /* Record */
142     sout_instance_t *p_sout_record;
143 };
144
145 static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
146 static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
147 static void         EsOutDel    ( es_out_t *, es_out_id_t * );
148 static void         EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
149 static int          EsOutControl( es_out_t *, int i_query, va_list );
150
151 static void         EsOutAddInfo( es_out_t *, es_out_id_t *es );
152
153 static bool EsIsSelected( es_out_id_t *es );
154 static void EsSelect( es_out_t *out, es_out_id_t *es );
155 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
156 static char *LanguageGetName( const char *psz_code );
157 static char *LanguageGetCode( const char *psz_lang );
158 static char **LanguageSplit( const char *psz_langs );
159 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
160
161 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
162
163 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
164     VLC_FOURCC('c', 'c', '1', ' '),
165     VLC_FOURCC('c', 'c', '2', ' '),
166     VLC_FOURCC('c', 'c', '3', ' '),
167     VLC_FOURCC('c', 'c', '4', ' '),
168 };
169 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
170 {
171     int i;
172     for( i = 0; i < 4; i++ )
173     {
174         if( fcc == EsOutFourccClosedCaptions[i] )
175             return i;
176     }
177     return -1;
178 }
179
180
181 /*****************************************************************************
182  * input_EsOutNew:
183  *****************************************************************************/
184 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
185 {
186     vlc_value_t  val;
187     int i;
188
189     es_out_t     *out = malloc( sizeof( es_out_t ) );
190     if( !out ) return NULL;
191
192     es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
193     if( !p_sys )
194     {
195         free( out );
196         return NULL;
197     }
198
199     out->pf_add     = EsOutAdd;
200     out->pf_send    = EsOutSend;
201     out->pf_del     = EsOutDel;
202     out->pf_control = EsOutControl;
203     out->p_sys      = p_sys;
204     out->b_sout     = p_input->p->p_sout != NULL;
205
206     p_sys->p_input = p_input;
207
208     p_sys->b_active = false;
209     p_sys->i_mode   = ES_OUT_MODE_AUTO;
210
211
212     TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
213     p_sys->p_pgrm   = NULL;
214
215     p_sys->i_id    = 0;
216
217     TAB_INIT( p_sys->i_es, p_sys->es );
218
219     p_sys->i_audio = 0;
220     p_sys->i_video = 0;
221     p_sys->i_sub   = 0;
222
223     /* */
224     var_Get( p_input, "audio-track", &val );
225     p_sys->i_audio_last = val.i_int;
226
227     var_Get( p_input, "sub-track", &val );
228     p_sys->i_sub_last = val.i_int;
229
230     p_sys->i_default_sub_id   = -1;
231
232     if( !p_input->b_preparsing )
233     {
234         var_Get( p_input, "audio-language", &val );
235         p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
236         if( p_sys->ppsz_audio_language )
237         {
238             for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
239                 msg_Dbg( p_input, "selected audio language[%d] %s",
240                          i, p_sys->ppsz_audio_language[i] );
241         }
242         free( val.psz_string );
243
244         var_Get( p_input, "sub-language", &val );
245         p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
246         if( p_sys->ppsz_sub_language )
247         {
248             for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
249                 msg_Dbg( p_input, "selected subtitle language[%d] %s",
250                          i, p_sys->ppsz_sub_language[i] );
251         }
252         free( val.psz_string );
253     }
254     else
255     {
256         p_sys->ppsz_sub_language = NULL;
257         p_sys->ppsz_audio_language = NULL;
258     }
259
260     var_Get( p_input, "audio-track-id", &val );
261     p_sys->i_audio_id = val.i_int;
262
263     var_Get( p_input, "sub-track-id", &val );
264     p_sys->i_sub_id = val.i_int;
265
266     p_sys->p_es_audio = NULL;
267     p_sys->p_es_video = NULL;
268     p_sys->p_es_sub   = NULL;
269
270     p_sys->i_audio_delay= 0;
271     p_sys->i_spu_delay  = 0;
272
273     p_sys->i_rate = i_rate;
274
275     p_sys->p_sout_record = NULL;
276
277     return out;
278 }
279
280 /*****************************************************************************
281  * input_EsOutDelete:
282  *****************************************************************************/
283 void input_EsOutDelete( es_out_t *out )
284 {
285     es_out_sys_t *p_sys = out->p_sys;
286     int i;
287
288     if( p_sys->p_sout_record )
289         input_EsOutSetRecord( out, false );
290
291     for( i = 0; i < p_sys->i_es; i++ )
292     {
293         if( p_sys->es[i]->p_dec )
294             input_DecoderDelete( p_sys->es[i]->p_dec );
295
296         free( p_sys->es[i]->psz_language );
297         free( p_sys->es[i]->psz_language_code );
298         es_format_Clean( &p_sys->es[i]->fmt );
299
300         free( p_sys->es[i] );
301     }
302     if( p_sys->ppsz_audio_language )
303     {
304         for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
305             free( p_sys->ppsz_audio_language[i] );
306         free( p_sys->ppsz_audio_language );
307     }
308     if( p_sys->ppsz_sub_language )
309     {
310         for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
311             free( p_sys->ppsz_sub_language[i] );
312         free( p_sys->ppsz_sub_language );
313     }
314     free( p_sys->es );
315
316     /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
317     for( i = 0; i < p_sys->i_pgrm; i++ )
318     {
319         es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
320         input_clock_Delete( p_pgrm->p_clock );
321         free( p_pgrm->psz_now_playing );
322         free( p_pgrm->psz_publisher );
323         free( p_pgrm->psz_name );
324         if( p_pgrm->p_epg )
325             vlc_epg_Delete( p_pgrm->p_epg );
326
327         free( p_pgrm );
328     }
329     TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
330
331     free( p_sys );
332     free( out );
333 }
334
335 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
336 {
337     int i;
338     if( i_id < 0 )
339     {
340         /* Special HACK, -i_id is tha cat of the stream */
341         return (es_out_id_t*)((uint8_t*)NULL-i_id);
342     }
343
344     for( i = 0; i < out->p_sys->i_es; i++ )
345     {
346         if( out->p_sys->es[i]->i_id == i_id )
347             return out->p_sys->es[i];
348     }
349     return NULL;
350 }
351
352 mtime_t input_EsOutGetWakeup( es_out_t *out )
353 {
354     es_out_sys_t   *p_sys = out->p_sys;
355     input_thread_t *p_input = p_sys->p_input;
356
357     if( !p_sys->p_pgrm )
358         return 0;
359
360     /* We do not have a wake up date if the input cannot have its speed
361      * controlled or sout is imposing its own */
362     if( !p_input->b_can_pace_control || p_input->p->b_out_pace_control )
363         return 0;
364
365     return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
366 }
367
368 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
369 {
370     es_out_sys_t      *p_sys = out->p_sys;
371     int i;
372
373     for( i = 0; i < p_sys->i_es; i++ )
374     {
375         es_out_id_t *es = p_sys->es[i];
376
377         /* Send a dummy block to let decoder know that
378          * there is a discontinuity */
379         if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
380         {
381             input_DecoderDiscontinuity( es->p_dec, b_flush );
382             if( es->p_dec_record )
383                 input_DecoderDiscontinuity( es->p_dec_record, b_flush );
384         }
385     }
386 }
387 void input_EsOutChangeRate( es_out_t *out, int i_rate )
388 {
389     es_out_sys_t      *p_sys = out->p_sys;
390     int i;
391
392     p_sys->i_rate = i_rate;
393     EsOutDiscontinuity( out, false, false );
394
395     for( i = 0; i < p_sys->i_pgrm; i++ )
396         input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, i_rate );
397 }
398
399 int input_EsOutSetRecord(  es_out_t *out, bool b_record )
400 {
401     es_out_sys_t   *p_sys = out->p_sys;
402     input_thread_t *p_input = p_sys->p_input;
403
404     assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
405
406     if( b_record )
407     {
408         char *psz_path = var_CreateGetString( p_input, "input-record-path" );
409         if( !psz_path || *psz_path == '\0' )
410         {
411             free( psz_path );
412             psz_path = strdup( config_GetHomeDir() );
413         }
414
415         char *psz_sout = NULL;  // TODO conf
416
417         if( !psz_sout && psz_path )
418         {
419             char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
420             if( psz_file )
421             {
422                 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
423                     psz_sout = NULL;
424                 free( psz_file );
425             }
426         }
427         free( psz_path );
428
429         if( !psz_sout )
430             return VLC_EGENERIC;
431
432 #ifdef ENABLE_SOUT
433         p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
434 #endif
435         free( psz_sout );
436
437         if( !p_sys->p_sout_record )
438             return VLC_EGENERIC;
439
440         for( int i = 0; i < p_sys->i_es; i++ )
441         {
442             es_out_id_t *p_es = p_sys->es[i];
443
444             if( !p_es->p_dec || p_es->p_master )
445                 continue;
446
447             p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_sys->p_sout_record );
448         }
449     }
450     else
451     {
452         for( int i = 0; i < p_sys->i_es; i++ )
453         {
454             es_out_id_t *p_es = p_sys->es[i];
455
456             if( !p_es->p_dec_record )
457                 continue;
458
459             input_DecoderDelete( p_es->p_dec_record );
460             p_es->p_dec_record = NULL;
461         }
462 #ifdef ENABLE_SOUT
463         sout_DeleteInstance( p_sys->p_sout_record );
464 #endif
465         p_sys->p_sout_record = NULL;
466     }
467
468     return VLC_SUCCESS;
469 }
470 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
471 {
472     es_out_sys_t *p_sys = out->p_sys;
473
474     if( i_cat == AUDIO_ES )
475         p_sys->i_audio_delay = i_delay;
476     else if( i_cat == SPU_ES )
477         p_sys->i_spu_delay = i_delay;
478 }
479 void input_EsOutChangeState( es_out_t *out )
480 {
481     es_out_sys_t *p_sys = out->p_sys;
482     input_thread_t *p_input = p_sys->p_input;
483
484     if( p_input->i_state  == PAUSE_S )
485     {
486         /* Send discontinuity to decoders (it will allow them to flush
487          *                  * if implemented */
488         EsOutDiscontinuity( out, false, false );
489     }
490     else
491     {
492         /* Out of pause, reset pcr */
493         es_out_Control( out, ES_OUT_RESET_PCR );
494     }
495 }
496 void input_EsOutChangePosition( es_out_t *out )
497 {
498     //es_out_sys_t *p_sys = out->p_sys;
499
500     es_out_Control( out, ES_OUT_RESET_PCR );
501     EsOutDiscontinuity( out, true, false );
502 }
503
504 bool input_EsOutDecodersEmpty( es_out_t *out )
505 {
506     es_out_sys_t      *p_sys = out->p_sys;
507     int i;
508
509     for( i = 0; i < p_sys->i_es; i++ )
510     {
511         es_out_id_t *es = p_sys->es[i];
512
513         if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
514             return false;
515         if( es->p_dec_record && !input_DecoderEmpty( es->p_dec_record ) )
516             return false;
517     }
518     return true;
519 }
520
521 /*****************************************************************************
522  *
523  *****************************************************************************/
524 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
525                                      bool b_delete )
526 {
527     es_out_sys_t      *p_sys = out->p_sys;
528     input_thread_t    *p_input = p_sys->p_input;
529     const  bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
530     vlc_value_t       val, text;
531
532     const char *psz_var;
533
534     if( fmt->i_cat == AUDIO_ES )
535         psz_var = "audio-es";
536     else if( fmt->i_cat == VIDEO_ES )
537         psz_var = "video-es";
538     else if( fmt->i_cat == SPU_ES )
539         psz_var = "spu-es";
540     else
541         return;
542
543     if( b_delete )
544     {
545         if( b_teletext )
546             var_SetInteger( p_sys->p_input, "teletext-es", -1 );
547
548         val.i_int = i_id;
549         var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
550
551         var_SetBool( p_sys->p_input, "intf-change", true );
552         return;
553     }
554
555     /* Get the number of ES already added */
556     var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
557     if( val.i_int == 0 )
558     {
559         vlc_value_t val2;
560
561         /* First one, we need to add the "Disable" choice */
562         val2.i_int = -1; text.psz_string = _("Disable");
563         var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
564         val.i_int++;
565     }
566
567     /* Take care of the ES description */
568     if( fmt->psz_description && *fmt->psz_description )
569     {
570         if( psz_language && *psz_language )
571         {
572             text.psz_string = malloc( strlen( fmt->psz_description) +
573                                       strlen( psz_language ) + 10 );
574             sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
575                                                    psz_language );
576         }
577         else text.psz_string = strdup( fmt->psz_description );
578     }
579     else
580     {
581         if( psz_language && *psz_language )
582         {
583             if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
584                 text.psz_string = NULL;
585         }
586         else
587         {
588             if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
589                 text.psz_string = NULL;
590         }
591     }
592
593     val.i_int = i_id;
594     var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
595
596     free( text.psz_string );
597
598     if( b_teletext )
599     {
600         if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
601             var_SetInteger( p_sys->p_input, "teletext-es", i_id );
602     }
603
604     var_SetBool( p_sys->p_input, "intf-change", true );
605 }
606
607 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
608                               bool b_delete )
609 {
610     EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
611 }
612
613 /* EsOutProgramSelect:
614  *  Select a program and update the object variable
615  */
616 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
617 {
618     es_out_sys_t      *p_sys = out->p_sys;
619     input_thread_t    *p_input = p_sys->p_input;
620     vlc_value_t       val;
621     int               i;
622
623     if( p_sys->p_pgrm == p_pgrm )
624         return; /* Nothing to do */
625
626     if( p_sys->p_pgrm )
627     {
628         es_out_pgrm_t *old = p_sys->p_pgrm;
629         msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
630
631         for( i = 0; i < p_sys->i_es; i++ )
632         {
633             if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
634                 p_sys->i_mode != ES_OUT_MODE_ALL )
635                 EsUnselect( out, p_sys->es[i], true );
636         }
637
638         p_sys->p_es_audio = NULL;
639         p_sys->p_es_sub = NULL;
640         p_sys->p_es_video = NULL;
641     }
642
643     msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
644
645     /* Mark it selected */
646     p_pgrm->b_selected = true;
647
648     /* Switch master stream */
649     if( p_sys->p_pgrm )
650         input_clock_ChangeMaster( p_sys->p_pgrm->p_clock, false );
651     input_clock_ChangeMaster( p_pgrm->p_clock, true );
652     p_sys->p_pgrm = p_pgrm;
653
654     /* Update "program" */
655     val.i_int = p_pgrm->i_id;
656     var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
657
658     /* Update "es-*" */
659     var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
660     var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
661     var_Change( p_input, "spu-es",   VLC_VAR_CLEARCHOICES, NULL, NULL );
662     var_SetInteger( p_input, "teletext-es", -1 );
663     for( i = 0; i < p_sys->i_es; i++ )
664     {
665         if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
666             EsOutESVarUpdate( out, p_sys->es[i], false );
667         EsOutSelect( out, p_sys->es[i], false );
668     }
669
670     /* Update now playing */
671     input_item_SetNowPlaying( p_input->p->input.p_item,
672                               p_pgrm->psz_now_playing );
673     input_item_SetPublisher( p_input->p->input.p_item,
674                              p_pgrm->psz_publisher );
675
676     var_SetBool( p_sys->p_input, "intf-change", true );
677 }
678
679 /* EsOutAddProgram:
680  *  Add a program
681  */
682 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
683 {
684     es_out_sys_t      *p_sys = out->p_sys;
685     input_thread_t    *p_input = p_sys->p_input;
686     vlc_value_t       val;
687
688     es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
689     if( !p_pgrm )
690         return NULL;
691
692     /* Init */
693     p_pgrm->i_id = i_group;
694     p_pgrm->i_es = 0;
695     p_pgrm->b_selected = false;
696     p_pgrm->psz_name = NULL;
697     p_pgrm->psz_now_playing = NULL;
698     p_pgrm->psz_publisher = NULL;
699     p_pgrm->p_epg = NULL;
700     p_pgrm->p_clock = input_clock_New( false, p_input->p->input.i_cr_average, p_sys->i_rate );
701     if( !p_pgrm->p_clock )
702     {
703         free( p_pgrm );
704         return NULL;
705     }
706
707     /* Append it */
708     TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
709
710     /* Update "program" variable */
711     val.i_int = i_group;
712     var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
713
714     if( i_group == var_GetInteger( p_input, "program" ) )
715     {
716         EsOutProgramSelect( out, p_pgrm );
717     }
718     else
719     {
720         var_SetBool( p_sys->p_input, "intf-change", true );
721     }
722     return p_pgrm;
723 }
724
725 /* EsOutDelProgram:
726  *  Delete a program
727  */
728 static int EsOutProgramDel( es_out_t *out, int i_group )
729 {
730     es_out_sys_t      *p_sys = out->p_sys;
731     input_thread_t    *p_input = p_sys->p_input;
732     es_out_pgrm_t     *p_pgrm = NULL;
733     vlc_value_t       val;
734     int               i;
735
736     for( i = 0; i < p_sys->i_pgrm; i++ )
737     {
738         if( p_sys->pgrm[i]->i_id == i_group )
739         {
740             p_pgrm = p_sys->pgrm[i];
741             break;
742         }
743     }
744
745     if( p_pgrm == NULL )
746         return VLC_EGENERIC;
747
748     if( p_pgrm->i_es )
749     {
750         msg_Dbg( p_input, "can't delete program %d which still has %i ES",
751                  i_group, p_pgrm->i_es );
752         return VLC_EGENERIC;
753     }
754
755     TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
756
757     /* If program is selected we need to unselect it */
758     if( p_sys->p_pgrm == p_pgrm )
759         p_sys->p_pgrm = NULL;
760
761     input_clock_Delete( p_pgrm->p_clock );
762
763     free( p_pgrm->psz_name );
764     free( p_pgrm->psz_now_playing );
765     free( p_pgrm->psz_publisher );
766     if( p_pgrm->p_epg )
767         vlc_epg_Delete( p_pgrm->p_epg );
768     free( p_pgrm );
769
770     /* Update "program" variable */
771     val.i_int = i_group;
772     var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
773
774     var_SetBool( p_sys->p_input, "intf-change", true );
775
776     return VLC_SUCCESS;
777 }
778
779 /* EsOutProgramMeta:
780  */
781 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
782 {
783     char *psz = NULL;
784     if( p_pgrm->psz_name )
785     {
786         if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
787             return NULL;
788     }
789     else
790     {
791         if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
792             return NULL;
793     }
794     return psz;
795 }
796
797 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
798 {
799     es_out_sys_t      *p_sys = out->p_sys;
800     es_out_pgrm_t     *p_pgrm = NULL;
801     input_thread_t    *p_input = p_sys->p_input;
802     char              *psz_cat;
803     const char        *psz_title = NULL;
804     const char        *psz_provider = NULL;
805     int i;
806
807     msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
808
809     /* Check against empty meta data (empty for what we handle) */
810     if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
811         !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
812         !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
813         vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
814     {
815         return;
816     }
817     /* Find program */
818     for( i = 0; i < p_sys->i_pgrm; i++ )
819     {
820         if( p_sys->pgrm[i]->i_id == i_group )
821         {
822             p_pgrm = p_sys->pgrm[i];
823             break;
824         }
825     }
826     if( p_pgrm == NULL )
827         p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
828
829     /* */
830     psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
831     psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
832
833     /* Update the description text of the program */
834     if( psz_title && *psz_title )
835     {
836         vlc_value_t val;
837         vlc_value_t text;
838
839         if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
840         {
841             char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
842
843             /* Remove old entries */
844             input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
845             /* TODO update epg name */
846             free( psz_cat );
847         }
848         free( p_pgrm->psz_name );
849         p_pgrm->psz_name = strdup( psz_title );
850
851         /* ugly but it works */
852         val.i_int = i_group;
853         var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
854
855         if( psz_provider && *psz_provider )
856         {
857             if( asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider ) != -1 )
858             {
859                 var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
860                 free( text.psz_string );
861             }
862         }
863         else
864         {
865             text.psz_string = (char *)psz_title;
866             var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
867         }
868     }
869
870     psz_cat = EsOutProgramGetMetaName( p_pgrm );
871     if( psz_provider )
872     {
873         if( p_sys->p_pgrm == p_pgrm )
874             input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
875         input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
876     }
877     char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
878     for( i = 0; ppsz_all_keys[i]; i++ )
879     {
880         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
881                        vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
882         free( ppsz_all_keys[i] );
883     }
884     free( ppsz_all_keys );
885
886     free( psz_cat );
887 }
888
889 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
890 {
891     int i;
892
893     /* Add new event */
894     for( i = 0; i < p_src->i_event; i++ )
895     {
896         vlc_epg_event_t *p_evt = p_src->pp_event[i];
897         bool b_add = true;
898         int j;
899
900         for( j = 0; j < p_dst->i_event; j++ )
901         {
902             if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
903             {
904                 b_add = false;
905                 break;
906             }
907             if( p_dst->pp_event[j]->i_start > p_evt->i_start )
908                 break;
909         }
910         if( b_add )
911         {
912             vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
913             if( !p_copy )
914                 break;
915             memset( p_copy, 0, sizeof(vlc_epg_event_t) );
916             p_copy->i_start = p_evt->i_start;
917             p_copy->i_duration = p_evt->i_duration;
918             p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
919             p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
920             p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
921             TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
922         }
923     }
924     /* Update current */
925     vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
926
927     /* Keep only 1 old event  */
928     if( p_dst->p_current )
929     {
930         while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
931             TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
932     }
933 }
934
935 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
936 {
937     es_out_sys_t      *p_sys = out->p_sys;
938     input_thread_t    *p_input = p_sys->p_input;
939     es_out_pgrm_t     *p_pgrm = NULL;
940     char *psz_cat;
941     int i;
942
943     /* Find program */
944     for( i = 0; i < p_sys->i_pgrm; i++ )
945     {
946         if( p_sys->pgrm[i]->i_id == i_group )
947         {
948             p_pgrm = p_sys->pgrm[i];
949             break;
950         }
951     }
952     if( p_pgrm == NULL )
953         p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
954
955     /* Merge EPG */
956     if( !p_pgrm->p_epg )
957         p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
958     vlc_epg_Merge( p_pgrm->p_epg, p_epg );
959
960     /* Update info */
961     psz_cat = EsOutProgramGetMetaName( p_pgrm );
962 #ifdef HAVE_LOCALTIME_R
963     char *psz_epg;
964     if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
965         psz_epg = NULL;
966     input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
967     msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
968     for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
969     {
970         const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
971         time_t t_start = (time_t)p_evt->i_start;
972         struct tm tm_start;
973         char psz_start[128];
974
975         localtime_r( &t_start, &tm_start );
976
977         snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
978         if( p_evt->psz_short_description || p_evt->psz_description )
979             input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
980                            p_evt->psz_name,
981                            p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
982                            p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
983         else
984             input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
985                            p_evt->psz_name,
986                            p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
987     }
988     free( psz_epg );
989 #endif
990     /* Update now playing */
991     free( p_pgrm->psz_now_playing );
992     p_pgrm->psz_now_playing = NULL;
993     if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
994         p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
995
996     if( p_pgrm == p_sys->p_pgrm )
997         input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
998
999     if( p_pgrm->psz_now_playing )
1000     {
1001         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1002             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1003             p_pgrm->psz_now_playing );
1004     }
1005     else
1006     {
1007         input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1008             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1009     }
1010
1011     free( psz_cat );
1012 }
1013
1014 /* EsOutAdd:
1015  *  Add an es_out
1016  */
1017 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
1018 {
1019     es_out_sys_t      *p_sys = out->p_sys;
1020     input_thread_t    *p_input = p_sys->p_input;
1021
1022     es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
1023     es_out_pgrm_t     *p_pgrm = NULL;
1024     int i;
1025
1026     if( !es ) return NULL;
1027
1028     if( fmt->i_group < 0 )
1029     {
1030         msg_Err( p_input, "invalid group number" );
1031         free( es );
1032         return NULL;
1033     }
1034
1035     /* Search the program */
1036     for( i = 0; i < p_sys->i_pgrm; i++ )
1037     {
1038         if( fmt->i_group == p_sys->pgrm[i]->i_id )
1039         {
1040             p_pgrm = p_sys->pgrm[i];
1041             break;
1042         }
1043     }
1044     if( p_pgrm == NULL )
1045     {
1046         /* Create a new one */
1047         p_pgrm = EsOutProgramAdd( out, fmt->i_group );
1048     }
1049
1050     /* Increase ref count for program */
1051     p_pgrm->i_es++;
1052
1053     /* Set up ES */
1054     if( fmt->i_id < 0 )
1055         fmt->i_id = out->p_sys->i_id;
1056     es->i_id = fmt->i_id;
1057     es->p_pgrm = p_pgrm;
1058     es_format_Copy( &es->fmt, fmt );
1059     es->i_preroll_end = -1;
1060
1061     switch( fmt->i_cat )
1062     {
1063     case AUDIO_ES:
1064     {
1065         audio_replay_gain_t rg;
1066
1067         es->i_channel = p_sys->i_audio;
1068
1069         memset( &rg, 0, sizeof(rg) );
1070         vlc_mutex_lock( &p_input->p->input.p_item->lock );
1071         vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
1072         vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1073
1074         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1075         {
1076             if( !es->fmt.audio_replay_gain.pb_peak[i] )
1077             {
1078                 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1079                 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1080             }
1081             if( !es->fmt.audio_replay_gain.pb_gain[i] )
1082             {
1083                 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1084                 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1085             }
1086         }
1087         break;
1088     }
1089
1090     case VIDEO_ES:
1091         es->i_channel = p_sys->i_video;
1092         if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
1093             vlc_ureduce( &es->fmt.video.i_frame_rate,
1094                          &es->fmt.video.i_frame_rate_base,
1095                          fmt->video.i_frame_rate,
1096                          fmt->video.i_frame_rate_base, 0 );
1097         break;
1098
1099     case SPU_ES:
1100         es->i_channel = p_sys->i_sub;
1101         break;
1102
1103     default:
1104         es->i_channel = 0;
1105         break;
1106     }
1107     es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
1108     es->psz_language_code = LanguageGetCode( fmt->psz_language );
1109     es->p_dec = NULL;
1110     es->p_dec_record = NULL;
1111     for( i = 0; i < 4; i++ )
1112         es->pb_cc_present[i] = false;
1113     es->p_master = NULL;
1114
1115     if( es->p_pgrm == p_sys->p_pgrm )
1116         EsOutESVarUpdate( out, es, false );
1117
1118     /* Select it if needed */
1119     EsOutSelect( out, es, false );
1120
1121
1122     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1123     p_sys->i_id++;  /* always incremented */
1124     switch( fmt->i_cat )
1125     {
1126         case AUDIO_ES:
1127             p_sys->i_audio++;
1128             break;
1129         case SPU_ES:
1130             p_sys->i_sub++;
1131             break;
1132         case VIDEO_ES:
1133             p_sys->i_video++;
1134             break;
1135     }
1136
1137     EsOutAddInfo( out, es );
1138
1139     return es;
1140 }
1141
1142 static bool EsIsSelected( es_out_id_t *es )
1143 {
1144     if( es->p_master )
1145     {
1146         bool b_decode = false;
1147         if( es->p_master->p_dec )
1148         {
1149             int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1150             if( i_channel != -1 )
1151                 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1152         }
1153         return b_decode;
1154     }
1155     else
1156     {
1157         return es->p_dec != NULL;
1158     }
1159 }
1160 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1161 {
1162     es_out_sys_t   *p_sys = out->p_sys;
1163     input_thread_t *p_input = p_sys->p_input;
1164
1165     p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_input->p->p_sout );
1166     if( p_es->p_dec && !p_es->p_master && p_sys->p_sout_record )
1167         p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_sys->p_sout_record );
1168 }
1169 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1170 {
1171     VLC_UNUSED(out);
1172
1173     if( !p_es->p_dec )
1174         return;
1175
1176     input_DecoderDelete( p_es->p_dec );
1177     p_es->p_dec = NULL;
1178
1179     if( p_es->p_dec_record )
1180     {
1181         input_DecoderDelete( p_es->p_dec_record );
1182         p_es->p_dec_record = NULL;
1183     }
1184 }
1185
1186 static void EsSelect( es_out_t *out, es_out_id_t *es )
1187 {
1188     es_out_sys_t   *p_sys = out->p_sys;
1189     input_thread_t *p_input = p_sys->p_input;
1190     vlc_value_t    val;
1191     const char     *psz_var;
1192
1193     if( EsIsSelected( es ) )
1194     {
1195         msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1196         return;
1197     }
1198
1199     if( es->p_master )
1200     {
1201         int i_channel;
1202         if( !es->p_master->p_dec )
1203             return;
1204
1205         i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1206         if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1207             return;
1208     }
1209     else
1210     {
1211         if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1212         {
1213             if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1214             {
1215                 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1216                          es->i_id );
1217                 return;
1218             }
1219         }
1220         else if( es->fmt.i_cat == AUDIO_ES )
1221         {
1222             var_Get( p_input, "audio", &val );
1223             if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1224             {
1225                 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1226                          es->i_id );
1227                 return;
1228             }
1229         }
1230         if( es->fmt.i_cat == SPU_ES )
1231         {
1232             var_Get( p_input, "spu", &val );
1233             if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1234             {
1235                 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1236                          es->i_id );
1237                 return;
1238             }
1239         }
1240
1241         es->i_preroll_end = -1;
1242         EsCreateDecoder( out, es );
1243
1244         if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1245             return;
1246     }
1247
1248     if( es->fmt.i_cat == VIDEO_ES )
1249         psz_var = "video-es";
1250     else if( es->fmt.i_cat == AUDIO_ES )
1251         psz_var = "audio-es";
1252     else if( es->fmt.i_cat == SPU_ES )
1253         psz_var = "spu-es";
1254     else
1255         return;
1256
1257     /* Mark it as selected */
1258     val.i_int = es->i_id;
1259     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1260
1261     var_SetBool( p_sys->p_input, "intf-change", true );
1262 }
1263
1264 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1265 {
1266     es_out_sys_t   *p_sys = out->p_sys;
1267     input_thread_t *p_input = p_sys->p_input;
1268     vlc_value_t    val;
1269     const char     *psz_var;
1270
1271     if( !EsIsSelected( es ) )
1272     {
1273         msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1274         return;
1275     }
1276
1277     if( es->p_master )
1278     {
1279         if( es->p_master->p_dec )
1280         {
1281             int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1282             if( i_channel != -1 )
1283                 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1284         }
1285     }
1286     else
1287     {
1288         const int i_spu_id = var_GetInteger( p_input, "spu-es");
1289         int i;
1290         for( i = 0; i < 4; i++ )
1291         {
1292             if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1293                 continue;
1294
1295             if( i_spu_id == es->pp_cc_es[i]->i_id )
1296             {
1297                 /* Force unselection of the CC */
1298                 val.i_int = -1;
1299                 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1300                 if( !b_update )
1301                     var_SetBool( p_sys->p_input, "intf-change", true );
1302             }
1303             EsOutDel( out, es->pp_cc_es[i] );
1304
1305             es->pb_cc_present[i] = false;
1306         }
1307         EsDestroyDecoder( out, es );
1308     }
1309
1310     if( !b_update )
1311         return;
1312
1313     /* Update var */
1314     if( es->p_dec == NULL )
1315         return;
1316     if( es->fmt.i_cat == VIDEO_ES )
1317         psz_var = "video-es";
1318     else if( es->fmt.i_cat == AUDIO_ES )
1319         psz_var = "audio-es";
1320     else if( es->fmt.i_cat == SPU_ES )
1321         psz_var = "spu-es";
1322     else
1323         return;
1324
1325     /* Mark it as unselected */
1326     val.i_int = -1;
1327     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1328
1329     var_SetBool( p_sys->p_input, "intf-change", true );
1330 }
1331
1332 /**
1333  * Select an ES given the current mode
1334  * XXX: you need to take a the lock before (stream.stream_lock)
1335  *
1336  * \param out The es_out structure
1337  * \param es es_out_id structure
1338  * \param b_force ...
1339  * \return nothing
1340  */
1341 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1342 {
1343     es_out_sys_t      *p_sys = out->p_sys;
1344
1345     int i_cat = es->fmt.i_cat;
1346
1347     if( !p_sys->b_active ||
1348         ( !b_force && es->fmt.i_priority < 0 ) )
1349     {
1350         return;
1351     }
1352
1353     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1354     {
1355         if( !EsIsSelected( es ) )
1356             EsSelect( out, es );
1357     }
1358     else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1359     {
1360         vlc_value_t val;
1361         int i;
1362         var_Get( p_sys->p_input, "programs", &val );
1363         for ( i = 0; i < val.p_list->i_count; i++ )
1364         {
1365             if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1366             {
1367                 if( !EsIsSelected( es ) )
1368                     EsSelect( out, es );
1369                 break;
1370             }
1371         }
1372         var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1373     }
1374     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1375     {
1376         int i_wanted  = -1;
1377
1378         if( es->p_pgrm != p_sys->p_pgrm )
1379             return;
1380
1381         if( i_cat == AUDIO_ES )
1382         {
1383             int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1384                                      es->psz_language_code );
1385
1386             if( p_sys->p_es_audio &&
1387                 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1388             {
1389                 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1390                                          p_sys->p_es_audio->psz_language_code );
1391
1392                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1393                     return;
1394                 i_wanted = es->i_channel;
1395             }
1396             else
1397             {
1398                 /* Select audio if (no audio selected yet)
1399                  * - no audio-language
1400                  * - no audio code for the ES
1401                  * - audio code in the requested list */
1402                 if( idx1 >= 0 ||
1403                     !strcmp( es->psz_language_code, "??" ) ||
1404                     !p_sys->ppsz_audio_language )
1405                     i_wanted = es->i_channel;
1406             }
1407
1408             if( p_sys->i_audio_last >= 0 )
1409                 i_wanted = p_sys->i_audio_last;
1410
1411             if( p_sys->i_audio_id >= 0 )
1412             {
1413                 if( es->i_id == p_sys->i_audio_id )
1414                     i_wanted = es->i_channel;
1415                 else
1416                     return;
1417             }
1418         }
1419         else if( i_cat == SPU_ES )
1420         {
1421             int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1422                                      es->psz_language_code );
1423
1424             if( p_sys->p_es_sub &&
1425                 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1426             {
1427                 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1428                                          p_sys->p_es_sub->psz_language_code );
1429
1430                 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1431                         idx1, es->psz_language_code, idx2,
1432                         p_sys->p_es_sub->psz_language_code );
1433
1434                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1435                     return;
1436                 /* We found a SPU that matches our language request */
1437                 i_wanted  = es->i_channel;
1438             }
1439             else if( idx1 >= 0 )
1440             {
1441                 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1442                         idx1, es->psz_language_code );
1443
1444                 i_wanted  = es->i_channel;
1445             }
1446             else if( p_sys->i_default_sub_id >= 0 )
1447             {
1448                 if( es->i_id == p_sys->i_default_sub_id )
1449                     i_wanted = es->i_channel;
1450             }
1451
1452             if( p_sys->i_sub_last >= 0 )
1453                 i_wanted  = p_sys->i_sub_last;
1454
1455             if( p_sys->i_sub_id >= 0 )
1456             {
1457                 if( es->i_id == p_sys->i_sub_id )
1458                     i_wanted = es->i_channel;
1459                 else
1460                     return;
1461             }
1462         }
1463         else if( i_cat == VIDEO_ES )
1464         {
1465             i_wanted  = es->i_channel;
1466         }
1467
1468         if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1469             EsSelect( out, es );
1470     }
1471
1472     /* FIXME TODO handle priority here */
1473     if( EsIsSelected( es ) )
1474     {
1475         if( i_cat == AUDIO_ES )
1476         {
1477             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1478                 p_sys->p_es_audio &&
1479                 p_sys->p_es_audio != es &&
1480                 EsIsSelected( p_sys->p_es_audio ) )
1481             {
1482                 EsUnselect( out, p_sys->p_es_audio, false );
1483             }
1484             p_sys->p_es_audio = es;
1485         }
1486         else if( i_cat == SPU_ES )
1487         {
1488             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1489                 p_sys->p_es_sub &&
1490                 p_sys->p_es_sub != es &&
1491                 EsIsSelected( p_sys->p_es_sub ) )
1492             {
1493                 EsUnselect( out, p_sys->p_es_sub, false );
1494             }
1495             p_sys->p_es_sub = es;
1496         }
1497         else if( i_cat == VIDEO_ES )
1498         {
1499             p_sys->p_es_video = es;
1500         }
1501     }
1502 }
1503
1504 /**
1505  * Send a block for the given es_out
1506  *
1507  * \param out the es_out to send from
1508  * \param es the es_out_id
1509  * \param p_block the data block to send
1510  */
1511 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1512 {
1513     es_out_sys_t *p_sys = out->p_sys;
1514     input_thread_t    *p_input = p_sys->p_input;
1515     es_out_pgrm_t *p_pgrm = es->p_pgrm;
1516     int64_t i_delay;
1517     int i_total=0;
1518
1519     if( es->fmt.i_cat == AUDIO_ES )
1520         i_delay = p_sys->i_audio_delay;
1521     else if( es->fmt.i_cat == SPU_ES )
1522         i_delay = p_sys->i_spu_delay;
1523     else
1524         i_delay = 0;
1525
1526     if( libvlc_stats (p_input) )
1527     {
1528         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1529         stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1530                              p_block->i_buffer, &i_total );
1531         stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1532                            (float)i_total, NULL );
1533         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1534     }
1535
1536     /* Mark preroll blocks */
1537     if( es->i_preroll_end >= 0 )
1538     {
1539         int64_t i_date = p_block->i_pts;
1540         if( i_date <= 0 )
1541             i_date = p_block->i_dts;
1542
1543         if( i_date < es->i_preroll_end )
1544             p_block->i_flags |= BLOCK_FLAG_PREROLL;
1545         else
1546             es->i_preroll_end = -1;
1547     }
1548
1549     if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1550     {
1551         p_block->i_dts += i_delay;
1552     }
1553     else if( p_block->i_dts > 0 )
1554     {
1555         p_block->i_dts =
1556             input_clock_GetTS( p_pgrm->p_clock, p_input->i_pts_delay, p_block->i_dts ) + i_delay;
1557     }
1558     if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1559     {
1560         p_block->i_pts += i_delay;
1561     }
1562     else if( p_block->i_pts > 0 )
1563     {
1564         p_block->i_pts =
1565             input_clock_GetTS( p_pgrm->p_clock, p_input->i_pts_delay, p_block->i_pts ) + i_delay;
1566     }
1567     if ( p_block->i_rate == INPUT_RATE_DEFAULT &&
1568          es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1569     {
1570         mtime_t current_date = mdate();
1571         if( !p_block->i_pts
1572                || p_block->i_pts > current_date + 10000000
1573                || current_date > p_block->i_pts )
1574         {
1575             /* ETSI EN 300 472 Annex A : do not take into account the PTS
1576              * for teletext streams. */
1577             p_block->i_pts = current_date + 400000
1578                                + p_input->i_pts_delay + i_delay;
1579         }
1580     }
1581
1582     p_block->i_rate = p_sys->i_rate;
1583
1584     /* TODO handle mute */
1585     if( es->p_dec &&
1586         ( es->fmt.i_cat != AUDIO_ES ||
1587           ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1588             p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1589     {
1590         bool pb_cc[4];
1591         bool b_cc_new = false;
1592         int i;
1593         if( es->p_dec_record )
1594         {
1595             block_t *p_dup = block_Duplicate( p_block );
1596             if( p_dup )
1597                 input_DecoderDecode( es->p_dec_record, p_dup );
1598         }
1599         input_DecoderDecode( es->p_dec, p_block );
1600
1601         /* Check CC status */
1602         input_DecoderIsCcPresent( es->p_dec, pb_cc );
1603         for( i = 0; i < 4; i++ )
1604         {
1605             static const vlc_fourcc_t fcc[4] = {
1606                 VLC_FOURCC('c', 'c', '1', ' '),
1607                 VLC_FOURCC('c', 'c', '2', ' '),
1608                 VLC_FOURCC('c', 'c', '3', ' '),
1609                 VLC_FOURCC('c', 'c', '4', ' '),
1610             };
1611             es_format_t fmt;
1612
1613             if(  es->pb_cc_present[i] || !pb_cc[i] )
1614                 continue;
1615             msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1616
1617             es_format_Init( &fmt, SPU_ES, fcc[i] );
1618             fmt.i_group = es->fmt.i_group;
1619             if( asprintf( &fmt.psz_description,
1620                           _("Closed captions %u"), 1 + i ) == -1 )
1621                 fmt.psz_description = NULL;
1622             es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1623             es->pp_cc_es[i]->p_master = es;
1624             es_format_Clean( &fmt );
1625
1626             /* */
1627             es->pb_cc_present[i] = true;
1628             b_cc_new = true;
1629         }
1630         if( b_cc_new )
1631             var_SetBool( p_sys->p_input, "intf-change", true );
1632     }
1633     else
1634     {
1635         block_Release( p_block );
1636     }
1637
1638     return VLC_SUCCESS;
1639 }
1640
1641 /*****************************************************************************
1642  * EsOutDel:
1643  *****************************************************************************/
1644 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1645 {
1646     es_out_sys_t *p_sys = out->p_sys;
1647     bool b_reselect = false;
1648     int i;
1649
1650     /* We don't try to reselect */
1651     if( es->p_dec )
1652     {
1653         while( !out->p_sys->p_input->b_die && es->p_dec )
1654         {
1655             if( input_DecoderEmpty( es->p_dec ) &&
1656                 ( !es->p_dec_record || input_DecoderEmpty( es->p_dec_record ) ))
1657                 break;
1658             msleep( 20*1000 );
1659         }
1660         EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1661     }
1662
1663     if( es->p_pgrm == p_sys->p_pgrm )
1664         EsOutESVarUpdate( out, es, true );
1665
1666     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1667
1668     es->p_pgrm->i_es--;
1669     if( es->p_pgrm->i_es == 0 )
1670     {
1671         msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1672     }
1673
1674     if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1675         p_sys->p_es_sub == es ) b_reselect = true;
1676
1677     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1678     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1679     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
1680
1681     switch( es->fmt.i_cat )
1682     {
1683         case AUDIO_ES:
1684             p_sys->i_audio--;
1685             break;
1686         case SPU_ES:
1687             p_sys->i_sub--;
1688             break;
1689         case VIDEO_ES:
1690             p_sys->i_video--;
1691             break;
1692     }
1693
1694     /* Re-select another track when needed */
1695     if( b_reselect )
1696         for( i = 0; i < p_sys->i_es; i++ )
1697         {
1698             if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1699                 EsOutSelect( out, p_sys->es[i], false );
1700         }
1701
1702     free( es->psz_language );
1703     free( es->psz_language_code );
1704
1705     es_format_Clean( &es->fmt );
1706
1707     free( es );
1708 }
1709
1710 /**
1711  * Control query handler
1712  *
1713  * \param out the es_out to control
1714  * \param i_query A es_out query as defined in include/ninput.h
1715  * \param args a variable list of arguments for the query
1716  * \return VLC_SUCCESS or an error code
1717  */
1718 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1719 {
1720     es_out_sys_t *p_sys = out->p_sys;
1721     bool  b, *pb;
1722     int         i, *pi;
1723
1724     es_out_id_t *es;
1725
1726     switch( i_query )
1727     {
1728         case ES_OUT_SET_ES_STATE:
1729             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1730             b = (bool) va_arg( args, int );
1731             if( b && !EsIsSelected( es ) )
1732             {
1733                 EsSelect( out, es );
1734                 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1735             }
1736             else if( !b && EsIsSelected( es ) )
1737             {
1738                 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1739                 return VLC_SUCCESS;
1740             }
1741             return VLC_SUCCESS;
1742
1743         case ES_OUT_GET_ES_STATE:
1744             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1745             pb = (bool*) va_arg( args, bool * );
1746
1747             *pb = EsIsSelected( es );
1748             return VLC_SUCCESS;
1749
1750         case ES_OUT_SET_ACTIVE:
1751         {
1752             b = (bool) va_arg( args, int );
1753             p_sys->b_active = b;
1754             /* Needed ? */
1755             if( b )
1756                 var_SetBool( p_sys->p_input, "intf-change", true );
1757             return VLC_SUCCESS;
1758         }
1759
1760         case ES_OUT_GET_ACTIVE:
1761             pb = (bool*) va_arg( args, bool * );
1762             *pb = p_sys->b_active;
1763             return VLC_SUCCESS;
1764
1765         case ES_OUT_SET_MODE:
1766             i = (int) va_arg( args, int );
1767             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1768                 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1769             {
1770                 p_sys->i_mode = i;
1771
1772                 /* Reapply policy mode */
1773                 for( i = 0; i < p_sys->i_es; i++ )
1774                 {
1775                     if( EsIsSelected( p_sys->es[i] ) )
1776                     {
1777                         EsUnselect( out, p_sys->es[i],
1778                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1779                     }
1780                 }
1781                 for( i = 0; i < p_sys->i_es; i++ )
1782                 {
1783                     EsOutSelect( out, p_sys->es[i], false );
1784                 }
1785                 return VLC_SUCCESS;
1786             }
1787             return VLC_EGENERIC;
1788
1789         case ES_OUT_GET_MODE:
1790             pi = (int*) va_arg( args, int* );
1791             *pi = p_sys->i_mode;
1792             return VLC_SUCCESS;
1793
1794         case ES_OUT_SET_ES:
1795         case ES_OUT_RESTART_ES:
1796         {
1797             int i_cat;
1798
1799             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1800
1801             if( es == NULL )
1802                 i_cat = UNKNOWN_ES;
1803             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1804                 i_cat = AUDIO_ES;
1805             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1806                 i_cat = VIDEO_ES;
1807             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1808                 i_cat = SPU_ES;
1809             else
1810                 i_cat = -1;
1811
1812             for( i = 0; i < p_sys->i_es; i++ )
1813             {
1814                 if( i_cat == -1 )
1815                 {
1816                     if( es == p_sys->es[i] )
1817                     {
1818                         EsOutSelect( out, es, true );
1819                         break;
1820                     }
1821                 }
1822                 else
1823                 {
1824                     if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
1825                     {
1826                         if( EsIsSelected( p_sys->es[i] ) )
1827                         {
1828                             if( i_query == ES_OUT_RESTART_ES )
1829                             {
1830                                 if( p_sys->es[i]->p_dec )
1831                                 {
1832                                     EsDestroyDecoder( out, p_sys->es[i] );
1833                                     EsCreateDecoder( out, p_sys->es[i] );
1834                                 }
1835                             }
1836                             else
1837                             {
1838                                 EsUnselect( out, p_sys->es[i],
1839                                             p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1840                             }
1841                         }
1842                     }
1843                 }
1844             }
1845             if( i_query == ES_OUT_SET_ES )
1846             {
1847                 vlc_event_t event;
1848                 event.type = vlc_InputSelectedStreamChanged;
1849                 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
1850             }
1851             return VLC_SUCCESS;
1852         }
1853  
1854         case ES_OUT_SET_DEFAULT:
1855         {
1856             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1857
1858             if( es == NULL )
1859             {
1860                 /*p_sys->i_default_video_id = -1;*/
1861                 /*p_sys->i_default_audio_id = -1;*/
1862                 p_sys->i_default_sub_id = -1;
1863             }
1864             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1865             {
1866                 /*p_sys->i_default_video_id = -1;*/
1867             }
1868             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1869             {
1870                 /*p_sys->i_default_audio_id = -1;*/
1871             }
1872             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1873             {
1874                 p_sys->i_default_sub_id = -1;
1875             }
1876             else
1877             {
1878                 /*if( es->fmt.i_cat == VIDEO_ES )
1879                     p_sys->i_default_video_id = es->i_id;
1880                 else
1881                 if( es->fmt.i_cat == AUDIO_ES )
1882                     p_sys->i_default_audio_id = es->i_id;
1883                 else*/
1884                 if( es->fmt.i_cat == SPU_ES )
1885                     p_sys->i_default_sub_id = es->i_id;
1886             }
1887             return VLC_SUCCESS;
1888         }
1889
1890         case ES_OUT_SET_PCR:
1891         case ES_OUT_SET_GROUP_PCR:
1892         {
1893             es_out_pgrm_t *p_pgrm = NULL;
1894             int            i_group = 0;
1895             int64_t        i_pcr;
1896
1897             if( i_query == ES_OUT_SET_PCR )
1898             {
1899                 p_pgrm = p_sys->p_pgrm;
1900             }
1901             else
1902             {
1903                 int i;
1904                 i_group = (int)va_arg( args, int );
1905                 for( i = 0; i < p_sys->i_pgrm; i++ )
1906                 {
1907                     if( p_sys->pgrm[i]->i_id == i_group )
1908                     {
1909                         p_pgrm = p_sys->pgrm[i];
1910                         break;
1911                     }
1912                 }
1913             }
1914             if( p_pgrm == NULL )
1915                 p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
1916
1917             i_pcr = (int64_t)va_arg( args, int64_t );
1918             /* search program
1919              * TODO do not use mdate() but proper stream acquisition date */
1920             input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
1921                                 p_sys->p_input->b_can_pace_control, i_pcr, mdate() );
1922             return VLC_SUCCESS;
1923         }
1924
1925         case ES_OUT_RESET_PCR:
1926             for( i = 0; i < p_sys->i_pgrm; i++ )
1927                 input_clock_Reset( p_sys->pgrm[i]->p_clock );
1928             return VLC_SUCCESS;
1929
1930         case ES_OUT_GET_TS:
1931             if( p_sys->p_pgrm )
1932             {
1933                 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1934                 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1935                 *pi_ts = input_clock_GetTS( p_sys->p_pgrm->p_clock,
1936                                             p_sys->p_input->i_pts_delay, i_ts );
1937                 return VLC_SUCCESS;
1938             }
1939             return VLC_EGENERIC;
1940
1941         case ES_OUT_GET_GROUP:
1942             pi = (int*) va_arg( args, int* );
1943             if( p_sys->p_pgrm )
1944                 *pi = p_sys->p_pgrm->i_id;
1945             else
1946                 *pi = -1;    /* FIXME */
1947             return VLC_SUCCESS;
1948
1949         case ES_OUT_SET_GROUP:
1950         {
1951             int j;
1952             i = (int) va_arg( args, int );
1953             for( j = 0; j < p_sys->i_pgrm; j++ )
1954             {
1955                 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1956                 if( p_pgrm->i_id == i )
1957                 {
1958                     EsOutProgramSelect( out, p_pgrm );
1959                     return VLC_SUCCESS;
1960                 }
1961             }
1962             return VLC_EGENERIC;
1963         }
1964
1965         case ES_OUT_SET_FMT:
1966         {
1967             /* This ain't pretty but is need by some demuxers (eg. Ogg )
1968              * to update the p_extra data */
1969             es_format_t *p_fmt;
1970             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1971             p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1972             if( es == NULL )
1973                 return VLC_EGENERIC;
1974
1975             if( p_fmt->i_extra )
1976             {
1977                 es->fmt.i_extra = p_fmt->i_extra;
1978                 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1979                 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1980
1981                 if( !es->p_dec )
1982                     return VLC_SUCCESS;
1983 #if 1
1984                 EsDestroyDecoder( out, es );
1985
1986                 EsCreateDecoder( out, es );
1987 #else
1988                 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1989                 es->p_dec->fmt_in.p_extra =
1990                     realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1991                 memcpy( es->p_dec->fmt_in.p_extra,
1992                         p_fmt->p_extra, p_fmt->i_extra );
1993 #endif
1994             }
1995
1996             return VLC_SUCCESS;
1997         }
1998
1999         case ES_OUT_SET_NEXT_DISPLAY_TIME:
2000         {
2001             int64_t i_date;
2002
2003             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2004             i_date = (int64_t)va_arg( args, int64_t );
2005
2006             if( !es || !es->p_dec )
2007                 return VLC_EGENERIC;
2008
2009             /* XXX We should call input_clock_GetTS but PCR has been reseted
2010              * and it will return 0, so we won't call input_clock_GetTS on all preroll samples
2011              * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
2012             es->i_preroll_end = i_date;
2013
2014             return VLC_SUCCESS;
2015         }
2016         case ES_OUT_SET_GROUP_META:
2017         {
2018             int i_group = (int)va_arg( args, int );
2019             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2020
2021             EsOutProgramMeta( out, i_group, p_meta );
2022             return VLC_SUCCESS;
2023         }
2024         case ES_OUT_SET_GROUP_EPG:
2025         {
2026             int i_group = (int)va_arg( args, int );
2027             vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2028
2029             EsOutProgramEpg( out, i_group, p_epg );
2030             return VLC_SUCCESS;
2031         }
2032         case ES_OUT_DEL_GROUP:
2033         {
2034             int i_group = (int)va_arg( args, int );
2035
2036             return EsOutProgramDel( out, i_group );
2037         }
2038
2039         default:
2040             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2041             return VLC_EGENERIC;
2042     }
2043 }
2044
2045 /****************************************************************************
2046  * LanguageGetName: try to expend iso639 into plain name
2047  ****************************************************************************/
2048 static char *LanguageGetName( const char *psz_code )
2049 {
2050     const iso639_lang_t *pl;
2051
2052     if( psz_code == NULL )
2053     {
2054         return strdup( "" );
2055     }
2056
2057     if( strlen( psz_code ) == 2 )
2058     {
2059         pl = GetLang_1( psz_code );
2060     }
2061     else if( strlen( psz_code ) == 3 )
2062     {
2063         pl = GetLang_2B( psz_code );
2064         if( !strcmp( pl->psz_iso639_1, "??" ) )
2065         {
2066             pl = GetLang_2T( psz_code );
2067         }
2068     }
2069     else
2070     {
2071         return strdup( psz_code );
2072     }
2073
2074     if( !strcmp( pl->psz_iso639_1, "??" ) )
2075     {
2076        return strdup( psz_code );
2077     }
2078     else
2079     {
2080         if( *pl->psz_native_name )
2081         {
2082             return strdup( pl->psz_native_name );
2083         }
2084         return strdup( pl->psz_eng_name );
2085     }
2086 }
2087
2088 /* Get a 2 char code */
2089 static char *LanguageGetCode( const char *psz_lang )
2090 {
2091     const iso639_lang_t *pl;
2092
2093     if( psz_lang == NULL || *psz_lang == '\0' )
2094         return strdup("??");
2095
2096     for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2097     {
2098         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2099             !strcasecmp( pl->psz_native_name, psz_lang ) ||
2100             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2101             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2102             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2103             break;
2104     }
2105
2106     if( pl->psz_iso639_1 != NULL )
2107         return strdup( pl->psz_iso639_1 );
2108
2109     return strdup("??");
2110 }
2111
2112 static char **LanguageSplit( const char *psz_langs )
2113 {
2114     char *psz_dup;
2115     char *psz_parser;
2116     char **ppsz = NULL;
2117     int i_psz = 0;
2118
2119     if( psz_langs == NULL ) return NULL;
2120
2121     psz_parser = psz_dup = strdup(psz_langs);
2122
2123     while( psz_parser && *psz_parser )
2124     {
2125         char *psz;
2126         char *psz_code;
2127
2128         psz = strchr(psz_parser, ',' );
2129         if( psz ) *psz++ = '\0';
2130
2131         if( !strcmp( psz_parser, "any" ) )
2132         {
2133             TAB_APPEND( i_psz, ppsz, strdup("any") );
2134         }
2135         else
2136         {
2137             psz_code = LanguageGetCode( psz_parser );
2138             if( strcmp( psz_code, "??" ) )
2139             {
2140                 TAB_APPEND( i_psz, ppsz, psz_code );
2141             }
2142             else
2143             {
2144                 free( psz_code );
2145             }
2146         }
2147
2148         psz_parser = psz;
2149     }
2150
2151     if( i_psz )
2152     {
2153         TAB_APPEND( i_psz, ppsz, NULL );
2154     }
2155
2156     free( psz_dup );
2157     return ppsz;
2158 }
2159
2160 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2161 {
2162     int i;
2163
2164     if( !ppsz_langs || !psz_lang ) return -1;
2165
2166     for( i = 0; ppsz_langs[i]; i++ )
2167     {
2168         if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2169             !strcasecmp( ppsz_langs[i], "any" ) )
2170         {
2171             return i;
2172         }
2173     }
2174
2175     return -1;
2176 }
2177
2178 /****************************************************************************
2179  * EsOutAddInfo:
2180  * - add meta info to the playlist item
2181  ****************************************************************************/
2182 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2183 {
2184     es_out_sys_t   *p_sys = out->p_sys;
2185     input_thread_t *p_input = p_sys->p_input;
2186     es_format_t    *fmt = &es->fmt;
2187     char           *psz_cat;
2188     lldiv_t         div;
2189
2190     /* Add stream info */
2191     if( asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 ) == -1 )
2192         return;
2193
2194     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2195                    "%.4s", (char*)&fmt->i_codec );
2196
2197     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2198                    "%s", es->psz_language );
2199
2200     /* Add information */
2201     switch( fmt->i_cat )
2202     {
2203     case AUDIO_ES:
2204         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2205                        _("Type"), _("Audio") );
2206
2207         if( fmt->audio.i_channels > 0 )
2208             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2209                            "%u", fmt->audio.i_channels );
2210
2211         if( fmt->audio.i_rate > 0 )
2212         {
2213             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2214                            _("%u Hz"), fmt->audio.i_rate );
2215             var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2216         }
2217
2218         if( fmt->audio.i_bitspersample > 0 )
2219             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2220                            _("Bits per sample"), "%u",
2221                            fmt->audio.i_bitspersample );
2222
2223         if( fmt->i_bitrate > 0 )
2224         {
2225             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2226                            _("%u kb/s"), fmt->i_bitrate / 1000 );
2227             var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2228         }
2229         break;
2230
2231     case VIDEO_ES:
2232         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2233                        _("Type"), _("Video") );
2234
2235         if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2236             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2237                            _("Resolution"), "%ux%u",
2238                            fmt->video.i_width, fmt->video.i_height );
2239
2240         if( fmt->video.i_visible_width > 0 &&
2241             fmt->video.i_visible_height > 0 )
2242             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2243                            _("Display resolution"), "%ux%u",
2244                            fmt->video.i_visible_width,
2245                            fmt->video.i_visible_height);
2246        if( fmt->video.i_frame_rate > 0 &&
2247            fmt->video.i_frame_rate_base > 0 )
2248        {
2249            div = lldiv( (float)fmt->video.i_frame_rate /
2250                                fmt->video.i_frame_rate_base * 1000000,
2251                                1000000 );
2252            input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2253                           _("Frame rate"), "%"PRId64".%06u",
2254                           div.quot, (unsigned int )div.rem );
2255        }
2256        break;
2257
2258     case SPU_ES:
2259         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2260                        _("Type"), _("Subtitle") );
2261         break;
2262
2263     default:
2264         break;
2265     }
2266
2267     free( psz_cat );
2268 }