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