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