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