]> git.sesse.net Git - vlc/blob - src/input/es_out.c
* control: added INPUT_DEL_INFO.
[vlc] / src / input / es_out.c
1 /*****************************************************************************
2  * es_out.c: Es Out handler for input.
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/decoder.h>
32
33 #include "input_internal.h"
34
35 #include "vlc_playlist.h"
36 #include "iso_lang.h"
37 /* FIXME we should find a better way than including that */
38 #include "../misc/iso-639_def.h"
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 typedef struct
44 {
45     /* Program ID */
46     int i_id;
47
48     /* Number of es for this pgrm */
49     int i_es;
50
51     vlc_bool_t b_selected;
52
53     /* Clock for this program */
54     input_clock_t clock;
55
56     char    *psz_now_playing;
57
58 } es_out_pgrm_t;
59
60 struct es_out_id_t
61 {
62     /* ES ID */
63     int       i_id;
64     es_out_pgrm_t *p_pgrm;
65
66     /* */
67     int64_t i_preroll_end;
68
69     /* Channel in the track type */
70     int         i_channel;
71     es_format_t fmt;
72     char        *psz_language;
73     char        *psz_language_code;
74     decoder_t   *p_dec;
75 };
76
77 struct es_out_sys_t
78 {
79     input_thread_t *p_input;
80
81     /* all programs */
82     int           i_pgrm;
83     es_out_pgrm_t **pgrm;
84     es_out_pgrm_t **pp_selected_pgrm; /* --programs */
85     es_out_pgrm_t *p_pgrm;  /* Master program */
86
87     /* all es */
88     int         i_id;
89     int         i_es;
90     es_out_id_t **es;
91
92     /* mode gestion */
93     vlc_bool_t  b_active;
94     int         i_mode;
95
96     /* es count */
97     int         i_audio;
98     int         i_video;
99     int         i_sub;
100
101     /* es to select */
102     int         i_audio_last;
103     int         i_sub_last;
104     char        **ppsz_audio_language;
105     char        **ppsz_sub_language;
106
107     /* current main es */
108     es_out_id_t *p_es_audio;
109     es_out_id_t *p_es_video;
110     es_out_id_t *p_es_sub;
111
112     /* delay */
113     int64_t i_audio_delay;
114     int64_t i_spu_delay;
115 };
116
117 static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
118 static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
119 static void         EsOutDel    ( es_out_t *, es_out_id_t * );
120 static void         EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
121 static int          EsOutControl( es_out_t *, int i_query, va_list );
122
123 static void         EsOutAddInfo( es_out_t *, es_out_id_t *es );
124
125 static void EsSelect( es_out_t *out, es_out_id_t *es );
126 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
127 static char *LanguageGetName( const char *psz_code );
128 static char *LanguageGetCode( const char *psz_lang );
129 static char **LanguageSplit( const char *psz_langs );
130 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
131
132 /*****************************************************************************
133  * input_EsOutNew:
134  *****************************************************************************/
135 es_out_t *input_EsOutNew( input_thread_t *p_input )
136 {
137     es_out_t     *out = malloc( sizeof( es_out_t ) );
138     es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
139     vlc_value_t  val;
140     int i;
141
142     out->pf_add     = EsOutAdd;
143     out->pf_send    = EsOutSend;
144     out->pf_del     = EsOutDel;
145     out->pf_control = EsOutControl;
146     out->p_sys      = p_sys;
147
148     p_sys->p_input = p_input;
149
150     p_sys->b_active = VLC_FALSE;
151     p_sys->i_mode   = ES_OUT_MODE_AUTO;
152
153
154     p_sys->i_pgrm   = 0;
155     p_sys->pgrm     = NULL;
156     p_sys->p_pgrm   = NULL;
157
158     p_sys->i_id    = 0;
159     p_sys->i_es    = 0;
160     p_sys->es      = NULL;
161
162     p_sys->i_audio = 0;
163     p_sys->i_video = 0;
164     p_sys->i_sub   = 0;
165
166     /* */
167     var_Get( p_input, "audio-track", &val );
168     p_sys->i_audio_last = val.i_int;
169
170     var_Get( p_input, "sub-track", &val );
171     p_sys->i_sub_last = val.i_int;
172
173     var_Get( p_input, "audio-language", &val );
174     p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
175     if( p_sys->ppsz_audio_language )
176     {
177         for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
178             msg_Dbg( p_input, "select audio in language[%d] %s",
179                      i, p_sys->ppsz_audio_language[i] );
180     }
181     if( val.psz_string ) free( val.psz_string );
182
183     var_Get( p_input, "sub-language", &val );
184     p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
185     if( p_sys->ppsz_sub_language )
186     {
187         for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
188             msg_Dbg( p_input, "select subtitle in language[%d] %s",
189                      i, p_sys->ppsz_sub_language[i] );
190     }
191     if( val.psz_string ) free( val.psz_string );
192
193     p_sys->p_es_audio = NULL;
194     p_sys->p_es_video = NULL;
195     p_sys->p_es_sub   = NULL;
196
197     p_sys->i_audio_delay= 0;
198     p_sys->i_spu_delay  = 0;
199
200     return out;
201 }
202
203 /*****************************************************************************
204  * input_EsOutDelete:
205  *****************************************************************************/
206 void input_EsOutDelete( es_out_t *out )
207 {
208     es_out_sys_t *p_sys = out->p_sys;
209     int i;
210
211     for( i = 0; i < p_sys->i_es; i++ )
212     {
213         if( p_sys->es[i]->p_dec )
214         {
215             input_DecoderDelete( p_sys->es[i]->p_dec );
216         }
217         if( p_sys->es[i]->psz_language )
218             free( p_sys->es[i]->psz_language );
219         if( p_sys->es[i]->psz_language_code )
220             free( p_sys->es[i]->psz_language_code );
221         es_format_Clean( &p_sys->es[i]->fmt );
222
223         free( p_sys->es[i] );
224     }
225     if( p_sys->ppsz_audio_language )
226     {
227         for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
228             free( p_sys->ppsz_audio_language[i] );
229         free( p_sys->ppsz_audio_language );
230     }
231     if( p_sys->ppsz_sub_language )
232     {
233         for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
234             free( p_sys->ppsz_sub_language[i] );
235         free( p_sys->ppsz_sub_language );
236     }
237
238     if( p_sys->es )
239         free( p_sys->es );
240
241     for( i = 0; i < p_sys->i_pgrm; i++ )
242     {
243         if( p_sys->pgrm[i]->psz_now_playing )
244             free( p_sys->pgrm[i]->psz_now_playing );
245         free( p_sys->pgrm[i] );
246     }
247     if( p_sys->pgrm )
248         free( p_sys->pgrm );
249
250     free( p_sys );
251     free( out );
252 }
253
254 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
255 {
256     int i;
257     if( i_id < 0 )
258     {
259         /* Special HACK, -i_id is tha cat of the stream */
260         return (es_out_id_t*)((uint8_t*)NULL-i_id);
261     }
262
263     for( i = 0; i < out->p_sys->i_es; i++ )
264     {
265         if( out->p_sys->es[i]->i_id == i_id )
266             return out->p_sys->es[i];
267     }
268     return NULL;
269 }
270
271 void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
272 {
273     es_out_sys_t      *p_sys = out->p_sys;
274     int i;
275
276     for( i = 0; i < p_sys->i_es; i++ )
277     {
278         es_out_id_t *es = p_sys->es[i];
279
280         /* Send a dummy block to let decoder know that
281          * there is a discontinuity */
282         if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
283         {
284             input_DecoderDiscontinuity( es->p_dec );
285         }
286     }
287 }
288
289 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
290 {
291     es_out_sys_t *p_sys = out->p_sys;
292
293     if( i_cat == AUDIO_ES )
294         p_sys->i_audio_delay = i_delay;
295     else if( i_cat == SPU_ES )
296         p_sys->i_spu_delay = i_delay;
297 }
298
299 vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
300 {
301     es_out_sys_t      *p_sys = out->p_sys;
302     int i;
303
304     for( i = 0; i < p_sys->i_es; i++ )
305     {
306         es_out_id_t *es = p_sys->es[i];
307
308         if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
309             return VLC_FALSE;
310     }
311     return VLC_TRUE;
312 }
313
314 /*****************************************************************************
315  *
316  *****************************************************************************/
317 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
318                               vlc_bool_t b_delete )
319 {
320     es_out_sys_t      *p_sys = out->p_sys;
321     input_thread_t    *p_input = p_sys->p_input;
322     vlc_value_t       val, text;
323
324     char *psz_var;
325
326     if( es->fmt.i_cat == AUDIO_ES )
327         psz_var = "audio-es";
328     else if( es->fmt.i_cat == VIDEO_ES )
329         psz_var = "video-es";
330     else if( es->fmt.i_cat == SPU_ES )
331         psz_var = "spu-es";
332     else
333         return;
334
335     if( b_delete )
336     {
337         val.i_int = es->i_id;
338         var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
339         var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
340         return;
341     }
342
343     /* Get the number of ES already added */
344     var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
345     if( val.i_int == 0 )
346     {
347         vlc_value_t val2;
348
349         /* First one, we need to add the "Disable" choice */
350         val2.i_int = -1; text.psz_string = _("Disable");
351         var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
352         val.i_int++;
353     }
354
355     /* Take care of the ES description */
356     if( es->fmt.psz_description && *es->fmt.psz_description )
357     {
358         if( es->psz_language && *es->psz_language )
359         {
360             text.psz_string = malloc( strlen( es->fmt.psz_description) + strlen( es->psz_language ) + 10 );
361             sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description, es->psz_language );
362         }
363         else text.psz_string = strdup( es->fmt.psz_description );
364     }
365     else
366     {
367         if( es->psz_language && *es->psz_language )
368         {
369             char *temp;
370             text.psz_string = malloc( strlen( _("Track %i") )+ strlen( es->psz_language ) + 30 );
371             asprintf( &temp,  _("Track %i"), val.i_int );
372             sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
373             free( temp );
374         }
375         else
376         {
377             text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
378             sprintf( text.psz_string, _("Track %i"), val.i_int );
379         }
380     }
381
382     val.i_int = es->i_id;
383     var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
384
385     free( text.psz_string );
386
387     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
388 }
389
390 /* EsOutProgramSelect:
391  *  Select a program and update the object variable
392  */
393 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
394 {
395     es_out_sys_t      *p_sys = out->p_sys;
396     input_thread_t    *p_input = p_sys->p_input;
397     vlc_value_t       val;
398     int               i;
399
400     if( p_sys->p_pgrm == p_pgrm )
401         return; /* Nothing to do */
402
403     if( p_sys->p_pgrm )
404     {
405         es_out_pgrm_t *old = p_sys->p_pgrm;
406         msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
407
408         for( i = 0; i < p_sys->i_es; i++ )
409         {
410             if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
411                 p_sys->i_mode != ES_OUT_MODE_ALL )
412                 EsUnselect( out, p_sys->es[i], VLC_TRUE );
413         }
414
415         p_sys->p_es_audio = NULL;
416         p_sys->p_es_sub = NULL;
417         p_sys->p_es_video = NULL;
418     }
419
420     msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
421
422     /* Mark it selected */
423     p_pgrm->b_selected = VLC_TRUE;
424
425     /* Switch master stream */
426     if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
427     {
428         p_sys->p_pgrm->clock.b_master = VLC_FALSE;
429     }
430     p_pgrm->clock.b_master = VLC_TRUE;
431     p_sys->p_pgrm = p_pgrm;
432
433     /* Update "program" */
434     val.i_int = p_pgrm->i_id;
435     var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
436
437     /* Update "es-*" */
438     var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
439     var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
440     var_Change( p_input, "spu-es",   VLC_VAR_CLEARCHOICES, NULL, NULL );
441     for( i = 0; i < p_sys->i_es; i++ )
442     {
443         if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
444             EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
445         EsOutSelect( out, p_sys->es[i], VLC_FALSE );
446     }
447
448     /* Update now playing if defined per program */
449     if( p_pgrm->psz_now_playing )
450     {
451         char *psz_cat = malloc( strlen(_("Program")) + 10 );
452
453         sprintf( psz_cat, "%s %d", _("Program"), p_pgrm->i_id );
454         input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
455                        VLC_META_NOW_PLAYING, "%s", p_pgrm->psz_now_playing );
456         free( psz_cat );
457     }
458
459
460     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
461 }
462
463 /* EsOutAddProgram:
464  *  Add a program
465  */
466 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
467 {
468     es_out_sys_t      *p_sys = out->p_sys;
469     input_thread_t    *p_input = p_sys->p_input;
470     vlc_value_t       val;
471
472     es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
473
474     /* Init */
475     p_pgrm->i_id = i_group;
476     p_pgrm->i_es = 0;
477     p_pgrm->b_selected = VLC_FALSE;
478     p_pgrm->psz_now_playing = NULL;
479     input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->input.i_cr_average );
480
481     /* Append it */
482     TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
483
484     /* Update "program" variable */
485     val.i_int = i_group;
486     var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
487
488     if( i_group == var_GetInteger( p_input, "program" ) )
489     {
490         EsOutProgramSelect( out, p_pgrm );
491     }
492     else
493     {
494         var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
495     }
496     return p_pgrm;
497 }
498
499 /* EsOutProgramMeta:
500  */
501 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
502 {
503     es_out_sys_t      *p_sys = out->p_sys;
504     es_out_pgrm_t     *p_pgrm = NULL;
505     input_thread_t    *p_input = p_sys->p_input;
506     char              *psz_cat = malloc( strlen(_("Program")) + 10 );
507     char              *psz_name = NULL;
508     char              *psz_now_playing = NULL;
509     char              *psz_provider = NULL;
510     int i;
511
512     msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
513     sprintf( psz_cat, "%s %d", _("Program"), i_group );
514
515     for( i = 0; i < p_meta->i_meta; i++ )
516     {
517         msg_Dbg( p_input, "  - %s = %s", p_meta->name[i], p_meta->value[i] );
518
519         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
520                       _(p_meta->name[i]), "%s", p_meta->value[i] );
521         if( !strcasecmp( p_meta->name[i], "Name" ) )
522             psz_name = p_meta->value[i];
523         else if( !strcasecmp( p_meta->name[i], "Provider" ) )
524             psz_provider = p_meta->value[i];
525         else if( !strcasecmp( p_meta->name[i], VLC_META_NOW_PLAYING ) )
526             psz_now_playing = p_meta->value[i];
527     }
528
529     if( !psz_name && !psz_now_playing )
530     {
531         free( psz_cat );
532         return;
533     }
534
535     for( i = 0; i < p_sys->i_pgrm; i++ )
536     {
537         if( p_sys->pgrm[i]->i_id == i_group )
538         {
539             p_pgrm = p_sys->pgrm[i];
540             break;
541         }
542     }
543
544     if( p_pgrm == NULL )
545         p_pgrm = EsOutProgramAdd( out, i_group );
546
547     /* Update the description text of the program */
548     if( psz_name && *psz_name )
549     {
550         vlc_value_t val;
551         vlc_value_t text;
552
553         /* ugly but it works */
554         val.i_int = i_group;
555         var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
556
557         if( psz_provider && *psz_provider )
558         {
559             asprintf( &text.psz_string, "%s [%s]", psz_name, psz_provider );
560             var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
561             free( text.psz_string );
562         }
563         else
564         {
565             text.psz_string = psz_name;
566             var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
567         }
568     }
569     if( psz_now_playing )
570     {
571         p_pgrm->psz_now_playing = strdup(psz_now_playing);
572
573         if( p_sys->p_pgrm == p_pgrm )
574         {
575             input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
576                            VLC_META_NOW_PLAYING, "%s", psz_now_playing );
577         }
578     }
579     free( psz_cat );
580 }
581
582 /* EsOutAdd:
583  *  Add an es_out
584  */
585 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
586 {
587     es_out_sys_t      *p_sys = out->p_sys;
588     input_thread_t    *p_input = p_sys->p_input;
589
590     es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
591     es_out_pgrm_t     *p_pgrm = NULL;
592     int i;
593
594     if( fmt->i_group < 0 )
595     {
596         msg_Err( p_input, "invalid group number" );
597         return NULL;
598     }
599
600     /* Search the program */
601     for( i = 0; i < p_sys->i_pgrm; i++ )
602     {
603         if( fmt->i_group == p_sys->pgrm[i]->i_id )
604         {
605             p_pgrm = p_sys->pgrm[i];
606             break;
607         }
608     }
609     if( p_pgrm == NULL )
610     {
611         /* Create a new one */
612         p_pgrm = EsOutProgramAdd( out, fmt->i_group );
613     }
614
615     /* Increase ref count for program */
616     p_pgrm->i_es++;
617
618     /* Set up ES */
619     if( fmt->i_id < 0 )
620         fmt->i_id = out->p_sys->i_id;
621     es->i_id = fmt->i_id;
622     es->p_pgrm = p_pgrm;
623     es_format_Copy( &es->fmt, fmt );
624     es->i_preroll_end = -1;
625
626     switch( fmt->i_cat )
627     {
628     case AUDIO_ES:
629         es->i_channel = p_sys->i_audio;
630         break;
631
632     case VIDEO_ES:
633         es->i_channel = p_sys->i_video;
634         break;
635
636     case SPU_ES:
637         es->i_channel = p_sys->i_sub;
638         break;
639
640     default:
641         es->i_channel = 0;
642         break;
643     }
644     es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
645     es->psz_language_code = LanguageGetCode( fmt->psz_language );
646     es->p_dec = NULL;
647
648     if( es->p_pgrm == p_sys->p_pgrm )
649         EsOutESVarUpdate( out, es, VLC_FALSE );
650
651     /* Select it if needed */
652     EsOutSelect( out, es, VLC_FALSE );
653
654
655     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
656     p_sys->i_id++;  /* always incremented */
657     switch( fmt->i_cat )
658     {
659         case AUDIO_ES:
660             p_sys->i_audio++;
661             break;
662         case SPU_ES:
663             p_sys->i_sub++;
664             break;
665         case VIDEO_ES:
666             p_sys->i_video++;
667             break;
668     }
669
670     EsOutAddInfo( out, es );
671
672     return es;
673 }
674
675 static void EsSelect( es_out_t *out, es_out_id_t *es )
676 {
677     es_out_sys_t   *p_sys = out->p_sys;
678     input_thread_t *p_input = p_sys->p_input;
679     vlc_value_t    val;
680     char           *psz_var;
681
682     if( es->p_dec )
683     {
684         msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
685         return;
686     }
687
688     if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
689     {
690         if( !var_GetBool( p_input, "video" ) ||
691             ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
692         {
693             msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
694                      es->i_id );
695             return;
696         }
697     }
698     else if( es->fmt.i_cat == AUDIO_ES )
699     {
700         var_Get( p_input, "audio", &val );
701         if( !var_GetBool( p_input, "audio" ) ||
702             ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
703         {
704             msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
705                      es->i_id );
706             return;
707         }
708     }
709
710     es->i_preroll_end = -1;
711     es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
712     if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
713         return;
714
715     if( es->fmt.i_cat == VIDEO_ES )
716         psz_var = "video-es";
717     else if( es->fmt.i_cat == AUDIO_ES )
718         psz_var = "audio-es";
719     else if( es->fmt.i_cat == SPU_ES )
720         psz_var = "spu-es";
721     else
722         return;
723
724     /* Mark it as selected */
725     val.i_int = es->i_id;
726     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
727
728
729     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
730 }
731
732 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
733 {
734     es_out_sys_t   *p_sys = out->p_sys;
735     input_thread_t *p_input = p_sys->p_input;
736     vlc_value_t    val;
737     char           *psz_var;
738
739     if( es->p_dec == NULL )
740     {
741         msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
742         return;
743     }
744
745     input_DecoderDelete( es->p_dec );
746     es->p_dec = NULL;
747
748     if( !b_update )
749         return;
750
751     /* Update var */
752     if( es->p_dec == NULL )
753         return;
754     if( es->fmt.i_cat == VIDEO_ES )
755         psz_var = "video-es";
756     else if( es->fmt.i_cat == AUDIO_ES )
757         psz_var = "audio-es";
758     else if( es->fmt.i_cat == SPU_ES )
759         psz_var = "spu-es";
760     else
761         return;
762
763     /* Mark it as selected */
764     val.i_int = -1;
765     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
766
767     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
768 }
769
770 /**
771  * Select an ES given the current mode
772  * XXX: you need to take a the lock before (stream.stream_lock)
773  *
774  * \param out The es_out structure
775  * \param es es_out_id structure
776  * \param b_force ...
777  * \return nothing
778  */
779 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
780 {
781     es_out_sys_t      *p_sys = out->p_sys;
782
783     int i_cat = es->fmt.i_cat;
784
785     if( !p_sys->b_active ||
786         ( !b_force && es->fmt.i_priority < 0 ) )
787     {
788         return;
789     }
790
791     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
792     {
793         if( !es->p_dec )
794             EsSelect( out, es );
795     }
796     else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
797     {
798         vlc_value_t val;
799         int i;
800         var_Get( p_sys->p_input, "programs", &val );
801         for ( i = 0; i < val.p_list->i_count; i++ )
802         {
803             if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
804             {
805                 if( !es->p_dec )
806                     EsSelect( out, es );
807                 break;
808             }
809         }
810         var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
811     }
812     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
813     {
814         int i_wanted  = -1;
815
816         if( es->p_pgrm != p_sys->p_pgrm )
817             return;
818
819         if( i_cat == AUDIO_ES )
820         {
821             int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
822                                      es->psz_language_code );
823
824             if( p_sys->p_es_audio &&
825                 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
826             {
827                 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
828                                          p_sys->p_es_audio->psz_language_code );
829
830                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
831                     return;
832                 i_wanted = es->i_channel;
833             }
834             else
835             {
836                 /* Select audio if (no audio selected yet)
837                  * - no audio-language
838                  * - no audio code for the ES
839                  * - audio code in the requested list */
840                 if( idx1 >= 0 ||
841                     !strcmp( es->psz_language_code, "??" ) ||
842                     !p_sys->ppsz_audio_language )
843                     i_wanted = es->i_channel;
844             }
845
846             if( p_sys->i_audio_last >= 0 )
847                 i_wanted = p_sys->i_audio_last;
848         }
849         else if( i_cat == SPU_ES )
850         {
851             int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
852                                      es->psz_language_code );
853
854             if( p_sys->p_es_sub &&
855                 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
856             {
857                 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
858                                          p_sys->p_es_sub->psz_language_code );
859
860                 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
861                         idx1, es->psz_language_code, idx2,
862                         p_sys->p_es_sub->psz_language_code );
863
864                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
865                     return;
866                 /* We found a SPU that matches our language request */
867                 i_wanted  = es->i_channel;
868             }
869             else if( idx1 >= 0 )
870             {
871                 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
872                         idx1, es->psz_language_code );
873
874                 i_wanted  = es->i_channel;
875             }
876             if( p_sys->i_sub_last >= 0 )
877                 i_wanted  = p_sys->i_sub_last;
878         }
879         else if( i_cat == VIDEO_ES )
880         {
881             i_wanted  = es->i_channel;
882         }
883
884         if( i_wanted == es->i_channel && es->p_dec == NULL )
885             EsSelect( out, es );
886     }
887
888     /* FIXME TODO handle priority here */
889     if( es->p_dec )
890     {
891         if( i_cat == AUDIO_ES )
892         {
893             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
894                 p_sys->p_es_audio &&
895                 p_sys->p_es_audio != es &&
896                 p_sys->p_es_audio->p_dec )
897             {
898                 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
899             }
900             p_sys->p_es_audio = es;
901         }
902         else if( i_cat == SPU_ES )
903         {
904             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
905                 p_sys->p_es_sub &&
906                 p_sys->p_es_sub != es &&
907                 p_sys->p_es_sub->p_dec )
908             {
909                 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
910             }
911             p_sys->p_es_sub = es;
912         }
913         else if( i_cat == VIDEO_ES )
914         {
915             p_sys->p_es_video = es;
916         }
917     }
918 }
919
920 /**
921  * Send a block for the given es_out
922  *
923  * \param out the es_out to send from
924  * \param es the es_out_id
925  * \param p_block the data block to send
926  */
927 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
928 {
929     es_out_sys_t *p_sys = out->p_sys;
930     input_thread_t    *p_input = p_sys->p_input;
931     es_out_pgrm_t *p_pgrm = es->p_pgrm;
932     int64_t i_delay;
933
934     if( es->fmt.i_cat == AUDIO_ES )
935         i_delay = p_sys->i_audio_delay;
936     else if( es->fmt.i_cat == SPU_ES )
937         i_delay = p_sys->i_spu_delay;
938     else
939         i_delay = 0;
940
941     /* Mark preroll blocks */
942     if( es->i_preroll_end >= 0 )
943     {
944         int64_t i_date = p_block->i_pts;
945         if( i_date <= 0 )
946             i_date = p_block->i_dts;
947
948         if( i_date < es->i_preroll_end )
949             p_block->i_flags |= BLOCK_FLAG_PREROLL;
950         else
951             es->i_preroll_end = -1;
952     }
953
954     /* +11 -> avoid null value with non null dts/pts */
955     if( p_block->i_dts > 0 )
956     {
957         p_block->i_dts =
958             input_ClockGetTS( p_input, &p_pgrm->clock,
959                               ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
960     }
961     if( p_block->i_pts > 0 )
962     {
963         p_block->i_pts =
964             input_ClockGetTS( p_input, &p_pgrm->clock,
965                               ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
966     }
967     if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
968     {
969         mtime_t current_date = mdate();
970         if( !p_block->i_pts
971                || p_block->i_pts > current_date + 10000000
972                || current_date > p_block->i_pts )
973         {
974             /* ETSI EN 300 472 Annex A : do not take into account the PTS
975              * for teletext streams. */
976             p_block->i_pts = current_date + 400000
977                                + p_input->i_pts_delay + i_delay;
978         }
979     }
980
981     p_block->i_rate = p_input->i_rate;
982
983     /* TODO handle mute */
984     if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
985         p_input->i_rate == INPUT_RATE_DEFAULT ) )
986     {
987         input_DecoderDecode( es->p_dec, p_block );
988     }
989     else
990     {
991         block_Release( p_block );
992     }
993
994     return VLC_SUCCESS;
995 }
996
997 /*****************************************************************************
998  * EsOutDel:
999  *****************************************************************************/
1000 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1001 {
1002     es_out_sys_t *p_sys = out->p_sys;
1003
1004     /* We don't try to reselect */
1005     if( es->p_dec )
1006         EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1007
1008     if( es->p_pgrm == p_sys->p_pgrm )
1009         EsOutESVarUpdate( out, es, VLC_TRUE );
1010
1011     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1012
1013     es->p_pgrm->i_es--;
1014     if( es->p_pgrm->i_es == 0 )
1015     {
1016         msg_Warn( p_sys->p_input, "Program doesn't contain anymore ES, "
1017                   "TODO cleaning ?" );
1018     }
1019
1020     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1021     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1022     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
1023
1024     switch( es->fmt.i_cat )
1025     {
1026         case AUDIO_ES:
1027             p_sys->i_audio--;
1028             break;
1029         case SPU_ES:
1030             p_sys->i_sub--;
1031             break;
1032         case VIDEO_ES:
1033             p_sys->i_video--;
1034             break;
1035     }
1036
1037     if( es->psz_language )
1038         free( es->psz_language );
1039     if( es->psz_language_code )
1040         free( es->psz_language_code );
1041
1042     es_format_Clean( &es->fmt );
1043
1044     free( es );
1045 }
1046
1047 /**
1048  * Control query handler
1049  *
1050  * \param out the es_out to control
1051  * \param i_query A es_out query as defined in include/ninput.h
1052  * \param args a variable list of arguments for the query
1053  * \return VLC_SUCCESS or an error code
1054  */
1055 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1056 {
1057     es_out_sys_t *p_sys = out->p_sys;
1058     vlc_bool_t  b, *pb;
1059     int         i, *pi;
1060
1061     es_out_id_t *es;
1062
1063     switch( i_query )
1064     {
1065         case ES_OUT_SET_ES_STATE:
1066             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1067             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1068             if( b && es->p_dec == NULL )
1069             {
1070                 EsSelect( out, es );
1071                 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1072             }
1073             else if( !b && es->p_dec )
1074             {
1075                 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1076                 return VLC_SUCCESS;
1077             }
1078             return VLC_SUCCESS;
1079
1080         case ES_OUT_GET_ES_STATE:
1081             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1082             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1083
1084             *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1085             return VLC_SUCCESS;
1086
1087         case ES_OUT_SET_ACTIVE:
1088         {
1089             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1090             p_sys->b_active = b;
1091             /* Needed ? */
1092             if( b )
1093                 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1094             return VLC_SUCCESS;
1095         }
1096
1097         case ES_OUT_GET_ACTIVE:
1098             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1099             *pb = p_sys->b_active;
1100             return VLC_SUCCESS;
1101
1102         case ES_OUT_SET_MODE:
1103             i = (int) va_arg( args, int );
1104             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1105                 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1106             {
1107                 p_sys->i_mode = i;
1108
1109                 /* Reapply policy mode */
1110                 for( i = 0; i < p_sys->i_es; i++ )
1111                 {
1112                     if( p_sys->es[i]->p_dec )
1113                     {
1114                         EsUnselect( out, p_sys->es[i],
1115                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1116                     }
1117                 }
1118                 for( i = 0; i < p_sys->i_es; i++ )
1119                 {
1120                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1121                 }
1122                 return VLC_SUCCESS;
1123             }
1124             return VLC_EGENERIC;
1125
1126         case ES_OUT_GET_MODE:
1127             pi = (int*) va_arg( args, int* );
1128             *pi = p_sys->i_mode;
1129             return VLC_SUCCESS;
1130
1131         case ES_OUT_SET_ES:
1132             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1133             /* Special case NULL, NULL+i_cat */
1134             if( es == NULL )
1135             {
1136                 for( i = 0; i < p_sys->i_es; i++ )
1137                 {
1138                     if( p_sys->es[i]->p_dec )
1139                         EsUnselect( out, p_sys->es[i],
1140                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1141                 }
1142             }
1143             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1144             {
1145                 for( i = 0; i < p_sys->i_es; i++ )
1146                 {
1147                     if( p_sys->es[i]->p_dec &&
1148                         p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1149                         EsUnselect( out, p_sys->es[i],
1150                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1151                 }
1152             }
1153             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1154             {
1155                 for( i = 0; i < p_sys->i_es; i++ )
1156                 {
1157                     if( p_sys->es[i]->p_dec &&
1158                         p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1159                         EsUnselect( out, p_sys->es[i],
1160                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1161                 }
1162             }
1163             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1164             {
1165                 for( i = 0; i < p_sys->i_es; i++ )
1166                 {
1167                     if( p_sys->es[i]->p_dec &&
1168                         p_sys->es[i]->fmt.i_cat == SPU_ES )
1169                         EsUnselect( out, p_sys->es[i],
1170                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1171                 }
1172             }
1173             else
1174             {
1175                 for( i = 0; i < p_sys->i_es; i++ )
1176                 {
1177                     if( es == p_sys->es[i] )
1178                     {
1179                         EsOutSelect( out, es, VLC_TRUE );
1180                         break;
1181                     }
1182                 }
1183             }
1184             return VLC_SUCCESS;
1185
1186         case ES_OUT_SET_PCR:
1187         case ES_OUT_SET_GROUP_PCR:
1188         {
1189             es_out_pgrm_t *p_pgrm = NULL;
1190             int            i_group = 0;
1191             int64_t        i_pcr;
1192
1193             if( i_query == ES_OUT_SET_PCR )
1194             {
1195                 p_pgrm = p_sys->p_pgrm;
1196             }
1197             else
1198             {
1199                 int i;
1200                 i_group = (int)va_arg( args, int );
1201                 for( i = 0; i < p_sys->i_pgrm; i++ )
1202                 {
1203                     if( p_sys->pgrm[i]->i_id == i_group )
1204                     {
1205                         p_pgrm = p_sys->pgrm[i];
1206                         break;
1207                     }
1208                 }
1209             }
1210             if( p_pgrm == NULL )
1211                 p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
1212
1213             i_pcr = (int64_t)va_arg( args, int64_t );
1214             /* search program */
1215             /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1216             input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1217                                (i_pcr + 11 ) * 9 / 100);
1218             return VLC_SUCCESS;
1219         }
1220
1221         case ES_OUT_RESET_PCR:
1222             for( i = 0; i < p_sys->i_pgrm; i++ )
1223             {
1224                 p_sys->pgrm[i]->clock.i_synchro_state =  SYNCHRO_REINIT;
1225                 p_sys->pgrm[i]->clock.last_pts = 0;
1226             }
1227             return VLC_SUCCESS;
1228
1229         case ES_OUT_GET_TS:
1230             if( p_sys->p_pgrm )
1231             {
1232                 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1233                 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1234                 *pi_ts = input_ClockGetTS( p_sys->p_input,
1235                                            &p_sys->p_pgrm->clock,
1236                                            ( i_ts + 11 ) * 9 / 100 );
1237                 return VLC_SUCCESS;
1238             }
1239             return VLC_EGENERIC;
1240
1241         case ES_OUT_GET_GROUP:
1242             pi = (int*) va_arg( args, int* );
1243             if( p_sys->p_pgrm )
1244                 *pi = p_sys->p_pgrm->i_id;
1245             else
1246                 *pi = -1;    /* FIXME */
1247             return VLC_SUCCESS;
1248
1249         case ES_OUT_SET_GROUP:
1250         {
1251             int j;
1252             i = (int) va_arg( args, int );
1253             for( j = 0; j < p_sys->i_pgrm; j++ )
1254             {
1255                 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1256                 if( p_pgrm->i_id == i )
1257                 {
1258                     EsOutProgramSelect( out, p_pgrm );
1259                     return VLC_SUCCESS;
1260                 }
1261             }
1262             return VLC_EGENERIC;
1263         }
1264
1265         case ES_OUT_SET_FMT:
1266         {
1267             /* This ain't pretty but is need by some demuxers (eg. Ogg )
1268              * to update the p_extra data */
1269             es_format_t *p_fmt;
1270             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1271             p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1272             if( es == NULL ) return VLC_EGENERIC;
1273
1274             if( p_fmt->i_extra )
1275             {
1276                 es->fmt.i_extra = p_fmt->i_extra;
1277                 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1278                 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1279
1280                 if( !es->p_dec ) return VLC_SUCCESS;
1281
1282 #if 1
1283                 input_DecoderDelete( es->p_dec );
1284                 es->p_dec = input_DecoderNew( p_sys->p_input,
1285                                               &es->fmt, VLC_FALSE );
1286
1287 #else
1288                 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1289                 es->p_dec->fmt_in.p_extra =
1290                     realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1291                 memcpy( es->p_dec->fmt_in.p_extra,
1292                         p_fmt->p_extra, p_fmt->i_extra );
1293 #endif
1294             }
1295
1296             return VLC_SUCCESS;
1297         }
1298
1299         case ES_OUT_SET_NEXT_DISPLAY_TIME:
1300         {
1301             int64_t i_date;
1302
1303             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1304             i_date = (int64_t)va_arg( args, int64_t );
1305
1306             if( !es || !es->p_dec )
1307                 return VLC_EGENERIC;
1308
1309             es->i_preroll_end = i_date;
1310             input_DecoderPreroll( es->p_dec, i_date );
1311
1312             return VLC_SUCCESS;
1313         }
1314         case ES_OUT_SET_GROUP_META:
1315         {
1316             int i_group = (int)va_arg( args, int );
1317             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1318
1319             EsOutProgramMeta( out, i_group, p_meta );
1320             return VLC_SUCCESS;
1321         }
1322
1323         default:
1324             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1325             return VLC_EGENERIC;
1326     }
1327 }
1328
1329 /****************************************************************************
1330  * LanguageGetName: try to expend iso639 into plain name
1331  ****************************************************************************/
1332 static char *LanguageGetName( const char *psz_code )
1333 {
1334     const iso639_lang_t *pl;
1335
1336     if( psz_code == NULL )
1337     {
1338         return strdup( "" );
1339     }
1340
1341     if( strlen( psz_code ) == 2 )
1342     {
1343         pl = GetLang_1( psz_code );
1344     }
1345     else if( strlen( psz_code ) == 3 )
1346     {
1347         pl = GetLang_2B( psz_code );
1348         if( !strcmp( pl->psz_iso639_1, "??" ) )
1349         {
1350             pl = GetLang_2T( psz_code );
1351         }
1352     }
1353     else
1354     {
1355         return strdup( psz_code );
1356     }
1357
1358     if( !strcmp( pl->psz_iso639_1, "??" ) )
1359     {
1360        return strdup( psz_code );
1361     }
1362     else
1363     {
1364         if( *pl->psz_native_name )
1365         {
1366             return strdup( pl->psz_native_name );
1367         }
1368         return strdup( pl->psz_eng_name );
1369     }
1370 }
1371
1372 /* Get a 2 char code */
1373 static char *LanguageGetCode( const char *psz_lang )
1374 {
1375     const iso639_lang_t *pl;
1376
1377     if( psz_lang == NULL || *psz_lang == '\0' )
1378         return strdup("??");
1379
1380     for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1381     {
1382         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1383             !strcasecmp( pl->psz_native_name, psz_lang ) ||
1384             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1385             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1386             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1387             break;
1388     }
1389
1390     if( pl->psz_iso639_1 != NULL )
1391         return strdup( pl->psz_iso639_1 );
1392
1393     return strdup("??");
1394 }
1395
1396 static char **LanguageSplit( const char *psz_langs )
1397 {
1398     char *psz_dup;
1399     char *psz_parser;
1400     char **ppsz = NULL;
1401     int i_psz = 0;
1402
1403     if( psz_langs == NULL ) return NULL;
1404
1405     psz_parser = psz_dup = strdup(psz_langs);
1406
1407     while( psz_parser && *psz_parser )
1408     {
1409         char *psz;
1410         char *psz_code;
1411
1412         psz = strchr(psz_parser, ',' );
1413         if( psz ) *psz++ = '\0';
1414
1415         psz_code = LanguageGetCode( psz_parser );
1416         if( strcmp( psz_code, "??" ) )
1417         {
1418             TAB_APPEND( i_psz, ppsz, psz_code );
1419         }
1420
1421         psz_parser = psz;
1422     }
1423
1424     if( i_psz )
1425     {
1426         TAB_APPEND( i_psz, ppsz, NULL );
1427     }
1428
1429     free( psz_dup );
1430     return ppsz;
1431 }
1432
1433 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1434 {
1435     int i;
1436
1437     if( !ppsz_langs || !psz_lang ) return -1;
1438
1439     for( i = 0; ppsz_langs[i]; i++ )
1440         if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1441
1442     return -1;
1443 }
1444
1445 /****************************************************************************
1446  * EsOutAddInfo:
1447  * - add meta info to the playlist item
1448  ****************************************************************************/
1449 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1450 {
1451     es_out_sys_t   *p_sys = out->p_sys;
1452     input_thread_t *p_input = p_sys->p_input;
1453     es_format_t    *fmt = &es->fmt;
1454     char           *psz_cat;
1455
1456     /* Add stream info */
1457     asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1458
1459     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1460                    "%.4s", (char*)&fmt->i_codec );
1461
1462     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1463                    "%s", es->psz_language );
1464
1465     /* Add information */
1466     switch( fmt->i_cat )
1467     {
1468     case AUDIO_ES:
1469         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1470                        _("Type"), _("Audio") );
1471
1472         if( fmt->audio.i_channels > 0 )
1473             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1474                            "%d", fmt->audio.i_channels );
1475
1476         if( fmt->audio.i_rate > 0 )
1477             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1478                            _("%d Hz"), fmt->audio.i_rate );
1479
1480         if( fmt->audio.i_bitspersample > 0 )
1481             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1482                            _("Bits per sample"), "%d",
1483                            fmt->audio.i_bitspersample );
1484
1485         if( fmt->i_bitrate > 0 )
1486             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1487                            _("%d kb/s"), fmt->i_bitrate / 1000 );
1488         break;
1489
1490     case VIDEO_ES:
1491         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1492                        _("Type"), _("Video") );
1493
1494         if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1495             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1496                            _("Resolution"), "%dx%d",
1497                            fmt->video.i_width, fmt->video.i_height );
1498
1499         if( fmt->video.i_visible_width > 0 &&
1500             fmt->video.i_visible_height > 0 )
1501             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1502                            _("Display resolution"), "%dx%d",
1503                            fmt->video.i_visible_width,
1504                            fmt->video.i_visible_height);
1505        if( fmt->video.i_frame_rate > 0 &&
1506            fmt->video.i_frame_rate_base > 0 )
1507            input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1508                           _("Frame rate"), "%f",
1509                           (float)fmt->video.i_frame_rate / 
1510                           fmt->video.i_frame_rate_base );
1511         break;
1512
1513     case SPU_ES:
1514         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1515                        _("Type"), _("Subtitle") );
1516         break;
1517
1518     default:
1519         break;
1520     }
1521
1522     free( psz_cat );
1523 }