]> git.sesse.net Git - vlc/blob - src/input/es_out.c
Forgot these files
[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  *
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 /* EsOutDelProgram:
500  *  Delete a program
501  */
502 static int EsOutProgramDel( es_out_t *out, int i_group )
503 {
504     es_out_sys_t      *p_sys = out->p_sys;
505     input_thread_t    *p_input = p_sys->p_input;
506     es_out_pgrm_t     *p_pgrm = NULL;
507     vlc_value_t       val;
508     int               i;
509
510     for( i = 0; i < p_sys->i_pgrm; i++ )
511     {
512         if( p_sys->pgrm[i]->i_id == i_group )
513         {
514             p_pgrm = p_sys->pgrm[i];
515             break;
516         }
517     }
518
519     if( p_pgrm == NULL )
520         return VLC_EGENERIC;
521
522     if( p_pgrm->i_es )
523     {
524         msg_Dbg( p_input, "can't delete program %d which still has %i ES",
525                  i_group, p_pgrm->i_es );
526         return VLC_EGENERIC;
527     }
528
529     TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
530
531     /* If program is selected we need to unselect it */
532     if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = 0;
533
534     if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
535     free( p_pgrm );
536
537     /* Update "program" variable */
538     val.i_int = i_group;
539     var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
540
541     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
542
543     return VLC_SUCCESS;
544 }
545
546 /* EsOutProgramMeta:
547  */
548 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
549 {
550     es_out_sys_t      *p_sys = out->p_sys;
551     es_out_pgrm_t     *p_pgrm = NULL;
552     input_thread_t    *p_input = p_sys->p_input;
553     char              *psz_cat = malloc( strlen(_("Program")) + 10 );
554     char              *psz_name = NULL;
555     char              *psz_now_playing = NULL;
556     char              *psz_provider = NULL;
557     int i;
558
559     msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
560     sprintf( psz_cat, "%s %d", _("Program"), i_group );
561
562     for( i = 0; i < p_meta->i_meta; i++ )
563     {
564         msg_Dbg( p_input, "  - %s = %s", p_meta->name[i], p_meta->value[i] );
565
566         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
567                       _(p_meta->name[i]), "%s", p_meta->value[i] );
568         if( !strcasecmp( p_meta->name[i], "Name" ) )
569             psz_name = p_meta->value[i];
570         else if( !strcasecmp( p_meta->name[i], "Provider" ) )
571             psz_provider = p_meta->value[i];
572         else if( !strcasecmp( p_meta->name[i], VLC_META_NOW_PLAYING ) )
573             psz_now_playing = p_meta->value[i];
574     }
575
576     if( !psz_name && !psz_now_playing )
577     {
578         free( psz_cat );
579         return;
580     }
581
582     for( i = 0; i < p_sys->i_pgrm; i++ )
583     {
584         if( p_sys->pgrm[i]->i_id == i_group )
585         {
586             p_pgrm = p_sys->pgrm[i];
587             break;
588         }
589     }
590
591     if( p_pgrm == NULL )
592         p_pgrm = EsOutProgramAdd( out, i_group );
593
594     /* Update the description text of the program */
595     if( psz_name && *psz_name )
596     {
597         vlc_value_t val;
598         vlc_value_t text;
599
600         /* ugly but it works */
601         val.i_int = i_group;
602         var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
603
604         if( psz_provider && *psz_provider )
605         {
606             asprintf( &text.psz_string, "%s [%s]", psz_name, psz_provider );
607             var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
608             free( text.psz_string );
609         }
610         else
611         {
612             text.psz_string = psz_name;
613             var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
614         }
615     }
616     if( psz_now_playing )
617     {
618         p_pgrm->psz_now_playing = strdup(psz_now_playing);
619
620         if( p_sys->p_pgrm == p_pgrm )
621         {
622             input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
623                            VLC_META_NOW_PLAYING, "%s", psz_now_playing );
624         }
625     }
626     free( psz_cat );
627 }
628
629 /* EsOutAdd:
630  *  Add an es_out
631  */
632 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
633 {
634     es_out_sys_t      *p_sys = out->p_sys;
635     input_thread_t    *p_input = p_sys->p_input;
636
637     es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
638     es_out_pgrm_t     *p_pgrm = NULL;
639     int i;
640
641     if( fmt->i_group < 0 )
642     {
643         msg_Err( p_input, "invalid group number" );
644         return NULL;
645     }
646
647     /* Search the program */
648     for( i = 0; i < p_sys->i_pgrm; i++ )
649     {
650         if( fmt->i_group == p_sys->pgrm[i]->i_id )
651         {
652             p_pgrm = p_sys->pgrm[i];
653             break;
654         }
655     }
656     if( p_pgrm == NULL )
657     {
658         /* Create a new one */
659         p_pgrm = EsOutProgramAdd( out, fmt->i_group );
660     }
661
662     /* Increase ref count for program */
663     p_pgrm->i_es++;
664
665     /* Set up ES */
666     if( fmt->i_id < 0 )
667         fmt->i_id = out->p_sys->i_id;
668     es->i_id = fmt->i_id;
669     es->p_pgrm = p_pgrm;
670     es_format_Copy( &es->fmt, fmt );
671     es->i_preroll_end = -1;
672
673     switch( fmt->i_cat )
674     {
675     case AUDIO_ES:
676         es->i_channel = p_sys->i_audio;
677         break;
678
679     case VIDEO_ES:
680         es->i_channel = p_sys->i_video;
681         if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
682             vlc_ureduce( &es->fmt.video.i_frame_rate,
683                          &es->fmt.video.i_frame_rate_base,
684                          fmt->video.i_frame_rate,
685                          fmt->video.i_frame_rate_base, 0 );
686         break;
687
688     case SPU_ES:
689         es->i_channel = p_sys->i_sub;
690         break;
691
692     default:
693         es->i_channel = 0;
694         break;
695     }
696     es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
697     es->psz_language_code = LanguageGetCode( fmt->psz_language );
698     es->p_dec = NULL;
699
700     if( es->p_pgrm == p_sys->p_pgrm )
701         EsOutESVarUpdate( out, es, VLC_FALSE );
702
703     /* Select it if needed */
704     EsOutSelect( out, es, VLC_FALSE );
705
706
707     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
708     p_sys->i_id++;  /* always incremented */
709     switch( fmt->i_cat )
710     {
711         case AUDIO_ES:
712             p_sys->i_audio++;
713             break;
714         case SPU_ES:
715             p_sys->i_sub++;
716             break;
717         case VIDEO_ES:
718             p_sys->i_video++;
719             break;
720     }
721
722     EsOutAddInfo( out, es );
723
724     return es;
725 }
726
727 static void EsSelect( es_out_t *out, es_out_id_t *es )
728 {
729     es_out_sys_t   *p_sys = out->p_sys;
730     input_thread_t *p_input = p_sys->p_input;
731     vlc_value_t    val;
732     char           *psz_var;
733
734     if( es->p_dec )
735     {
736         msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
737         return;
738     }
739
740     if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
741     {
742         if( !var_GetBool( p_input, "video" ) ||
743             ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
744         {
745             msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
746                      es->i_id );
747             return;
748         }
749     }
750     else if( es->fmt.i_cat == AUDIO_ES )
751     {
752         var_Get( p_input, "audio", &val );
753         if( !var_GetBool( p_input, "audio" ) ||
754             ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
755         {
756             msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
757                      es->i_id );
758             return;
759         }
760     }
761
762     es->i_preroll_end = -1;
763     es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
764     if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
765         return;
766
767     if( es->fmt.i_cat == VIDEO_ES )
768         psz_var = "video-es";
769     else if( es->fmt.i_cat == AUDIO_ES )
770         psz_var = "audio-es";
771     else if( es->fmt.i_cat == SPU_ES )
772         psz_var = "spu-es";
773     else
774         return;
775
776     /* Mark it as selected */
777     val.i_int = es->i_id;
778     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
779
780
781     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
782 }
783
784 static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
785 {
786     es_out_sys_t   *p_sys = out->p_sys;
787     input_thread_t *p_input = p_sys->p_input;
788     vlc_value_t    val;
789     char           *psz_var;
790
791     if( es->p_dec == NULL )
792     {
793         msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
794         return;
795     }
796
797     input_DecoderDelete( es->p_dec );
798     es->p_dec = NULL;
799
800     if( !b_update )
801         return;
802
803     /* Update var */
804     if( es->p_dec == NULL )
805         return;
806     if( es->fmt.i_cat == VIDEO_ES )
807         psz_var = "video-es";
808     else if( es->fmt.i_cat == AUDIO_ES )
809         psz_var = "audio-es";
810     else if( es->fmt.i_cat == SPU_ES )
811         psz_var = "spu-es";
812     else
813         return;
814
815     /* Mark it as selected */
816     val.i_int = -1;
817     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
818
819     var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
820 }
821
822 /**
823  * Select an ES given the current mode
824  * XXX: you need to take a the lock before (stream.stream_lock)
825  *
826  * \param out The es_out structure
827  * \param es es_out_id structure
828  * \param b_force ...
829  * \return nothing
830  */
831 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
832 {
833     es_out_sys_t      *p_sys = out->p_sys;
834
835     int i_cat = es->fmt.i_cat;
836
837     if( !p_sys->b_active ||
838         ( !b_force && es->fmt.i_priority < 0 ) )
839     {
840         return;
841     }
842
843     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
844     {
845         if( !es->p_dec )
846             EsSelect( out, es );
847     }
848     else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
849     {
850         vlc_value_t val;
851         int i;
852         var_Get( p_sys->p_input, "programs", &val );
853         for ( i = 0; i < val.p_list->i_count; i++ )
854         {
855             if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
856             {
857                 if( !es->p_dec )
858                     EsSelect( out, es );
859                 break;
860             }
861         }
862         var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
863     }
864     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
865     {
866         int i_wanted  = -1;
867
868         if( es->p_pgrm != p_sys->p_pgrm )
869             return;
870
871         if( i_cat == AUDIO_ES )
872         {
873             int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
874                                      es->psz_language_code );
875
876             if( p_sys->p_es_audio &&
877                 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
878             {
879                 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
880                                          p_sys->p_es_audio->psz_language_code );
881
882                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
883                     return;
884                 i_wanted = es->i_channel;
885             }
886             else
887             {
888                 /* Select audio if (no audio selected yet)
889                  * - no audio-language
890                  * - no audio code for the ES
891                  * - audio code in the requested list */
892                 if( idx1 >= 0 ||
893                     !strcmp( es->psz_language_code, "??" ) ||
894                     !p_sys->ppsz_audio_language )
895                     i_wanted = es->i_channel;
896             }
897
898             if( p_sys->i_audio_last >= 0 )
899                 i_wanted = p_sys->i_audio_last;
900         }
901         else if( i_cat == SPU_ES )
902         {
903             int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
904                                      es->psz_language_code );
905
906             if( p_sys->p_es_sub &&
907                 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
908             {
909                 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
910                                          p_sys->p_es_sub->psz_language_code );
911
912                 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
913                         idx1, es->psz_language_code, idx2,
914                         p_sys->p_es_sub->psz_language_code );
915
916                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
917                     return;
918                 /* We found a SPU that matches our language request */
919                 i_wanted  = es->i_channel;
920             }
921             else if( idx1 >= 0 )
922             {
923                 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
924                         idx1, es->psz_language_code );
925
926                 i_wanted  = es->i_channel;
927             }
928             if( p_sys->i_sub_last >= 0 )
929                 i_wanted  = p_sys->i_sub_last;
930         }
931         else if( i_cat == VIDEO_ES )
932         {
933             i_wanted  = es->i_channel;
934         }
935
936         if( i_wanted == es->i_channel && es->p_dec == NULL )
937             EsSelect( out, es );
938     }
939
940     /* FIXME TODO handle priority here */
941     if( es->p_dec )
942     {
943         if( i_cat == AUDIO_ES )
944         {
945             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
946                 p_sys->p_es_audio &&
947                 p_sys->p_es_audio != es &&
948                 p_sys->p_es_audio->p_dec )
949             {
950                 EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
951             }
952             p_sys->p_es_audio = es;
953         }
954         else if( i_cat == SPU_ES )
955         {
956             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
957                 p_sys->p_es_sub &&
958                 p_sys->p_es_sub != es &&
959                 p_sys->p_es_sub->p_dec )
960             {
961                 EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
962             }
963             p_sys->p_es_sub = es;
964         }
965         else if( i_cat == VIDEO_ES )
966         {
967             p_sys->p_es_video = es;
968         }
969     }
970 }
971
972 /**
973  * Send a block for the given es_out
974  *
975  * \param out the es_out to send from
976  * \param es the es_out_id
977  * \param p_block the data block to send
978  */
979 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
980 {
981     es_out_sys_t *p_sys = out->p_sys;
982     input_thread_t    *p_input = p_sys->p_input;
983     es_out_pgrm_t *p_pgrm = es->p_pgrm;
984     int64_t i_delay;
985
986     if( es->fmt.i_cat == AUDIO_ES )
987         i_delay = p_sys->i_audio_delay;
988     else if( es->fmt.i_cat == SPU_ES )
989         i_delay = p_sys->i_spu_delay;
990     else
991         i_delay = 0;
992
993     /* Mark preroll blocks */
994     if( es->i_preroll_end >= 0 )
995     {
996         int64_t i_date = p_block->i_pts;
997         if( i_date <= 0 )
998             i_date = p_block->i_dts;
999
1000         if( i_date < es->i_preroll_end )
1001             p_block->i_flags |= BLOCK_FLAG_PREROLL;
1002         else
1003             es->i_preroll_end = -1;
1004     }
1005
1006     /* +11 -> avoid null value with non null dts/pts */
1007     if( p_block->i_dts > 0 )
1008     {
1009         p_block->i_dts =
1010             input_ClockGetTS( p_input, &p_pgrm->clock,
1011                               ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
1012     }
1013     if( p_block->i_pts > 0 )
1014     {
1015         p_block->i_pts =
1016             input_ClockGetTS( p_input, &p_pgrm->clock,
1017                               ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
1018     }
1019     if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1020     {
1021         mtime_t current_date = mdate();
1022         if( !p_block->i_pts
1023                || p_block->i_pts > current_date + 10000000
1024                || current_date > p_block->i_pts )
1025         {
1026             /* ETSI EN 300 472 Annex A : do not take into account the PTS
1027              * for teletext streams. */
1028             p_block->i_pts = current_date + 400000
1029                                + p_input->i_pts_delay + i_delay;
1030         }
1031     }
1032
1033     p_block->i_rate = p_input->i_rate;
1034
1035     /* TODO handle mute */
1036     if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
1037         p_input->i_rate == INPUT_RATE_DEFAULT ) )
1038     {
1039         input_DecoderDecode( es->p_dec, p_block );
1040     }
1041     else
1042     {
1043         block_Release( p_block );
1044     }
1045
1046     return VLC_SUCCESS;
1047 }
1048
1049 /*****************************************************************************
1050  * EsOutDel:
1051  *****************************************************************************/
1052 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1053 {
1054     es_out_sys_t *p_sys = out->p_sys;
1055
1056     /* We don't try to reselect */
1057     if( es->p_dec )
1058         EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1059
1060     if( es->p_pgrm == p_sys->p_pgrm )
1061         EsOutESVarUpdate( out, es, VLC_TRUE );
1062
1063     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1064
1065     es->p_pgrm->i_es--;
1066     if( es->p_pgrm->i_es == 0 )
1067     {
1068         msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1069     }
1070
1071     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1072     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1073     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
1074
1075     switch( es->fmt.i_cat )
1076     {
1077         case AUDIO_ES:
1078             p_sys->i_audio--;
1079             break;
1080         case SPU_ES:
1081             p_sys->i_sub--;
1082             break;
1083         case VIDEO_ES:
1084             p_sys->i_video--;
1085             break;
1086     }
1087
1088     if( es->psz_language )
1089         free( es->psz_language );
1090     if( es->psz_language_code )
1091         free( es->psz_language_code );
1092
1093     es_format_Clean( &es->fmt );
1094
1095     free( es );
1096 }
1097
1098 /**
1099  * Control query handler
1100  *
1101  * \param out the es_out to control
1102  * \param i_query A es_out query as defined in include/ninput.h
1103  * \param args a variable list of arguments for the query
1104  * \return VLC_SUCCESS or an error code
1105  */
1106 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1107 {
1108     es_out_sys_t *p_sys = out->p_sys;
1109     vlc_bool_t  b, *pb;
1110     int         i, *pi;
1111
1112     es_out_id_t *es;
1113
1114     switch( i_query )
1115     {
1116         case ES_OUT_SET_ES_STATE:
1117             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1118             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1119             if( b && es->p_dec == NULL )
1120             {
1121                 EsSelect( out, es );
1122                 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
1123             }
1124             else if( !b && es->p_dec )
1125             {
1126                 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1127                 return VLC_SUCCESS;
1128             }
1129             return VLC_SUCCESS;
1130
1131         case ES_OUT_GET_ES_STATE:
1132             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1133             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1134
1135             *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
1136             return VLC_SUCCESS;
1137
1138         case ES_OUT_SET_ACTIVE:
1139         {
1140             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1141             p_sys->b_active = b;
1142             /* Needed ? */
1143             if( b )
1144                 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
1145             return VLC_SUCCESS;
1146         }
1147
1148         case ES_OUT_GET_ACTIVE:
1149             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1150             *pb = p_sys->b_active;
1151             return VLC_SUCCESS;
1152
1153         case ES_OUT_SET_MODE:
1154             i = (int) va_arg( args, int );
1155             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1156                 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1157             {
1158                 p_sys->i_mode = i;
1159
1160                 /* Reapply policy mode */
1161                 for( i = 0; i < p_sys->i_es; i++ )
1162                 {
1163                     if( p_sys->es[i]->p_dec )
1164                     {
1165                         EsUnselect( out, p_sys->es[i],
1166                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1167                     }
1168                 }
1169                 for( i = 0; i < p_sys->i_es; i++ )
1170                 {
1171                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
1172                 }
1173                 return VLC_SUCCESS;
1174             }
1175             return VLC_EGENERIC;
1176
1177         case ES_OUT_GET_MODE:
1178             pi = (int*) va_arg( args, int* );
1179             *pi = p_sys->i_mode;
1180             return VLC_SUCCESS;
1181
1182         case ES_OUT_SET_ES:
1183             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1184             /* Special case NULL, NULL+i_cat */
1185             if( es == NULL )
1186             {
1187                 for( i = 0; i < p_sys->i_es; i++ )
1188                 {
1189                     if( p_sys->es[i]->p_dec )
1190                         EsUnselect( out, p_sys->es[i],
1191                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1192                 }
1193             }
1194             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1195             {
1196                 for( i = 0; i < p_sys->i_es; i++ )
1197                 {
1198                     if( p_sys->es[i]->p_dec &&
1199                         p_sys->es[i]->fmt.i_cat == AUDIO_ES )
1200                         EsUnselect( out, p_sys->es[i],
1201                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1202                 }
1203             }
1204             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1205             {
1206                 for( i = 0; i < p_sys->i_es; i++ )
1207                 {
1208                     if( p_sys->es[i]->p_dec &&
1209                         p_sys->es[i]->fmt.i_cat == VIDEO_ES )
1210                         EsUnselect( out, p_sys->es[i],
1211                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1212                 }
1213             }
1214             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1215             {
1216                 for( i = 0; i < p_sys->i_es; i++ )
1217                 {
1218                     if( p_sys->es[i]->p_dec &&
1219                         p_sys->es[i]->fmt.i_cat == SPU_ES )
1220                         EsUnselect( out, p_sys->es[i],
1221                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1222                 }
1223             }
1224             else
1225             {
1226                 for( i = 0; i < p_sys->i_es; i++ )
1227                 {
1228                     if( es == p_sys->es[i] )
1229                     {
1230                         EsOutSelect( out, es, VLC_TRUE );
1231                         break;
1232                     }
1233                 }
1234             }
1235             return VLC_SUCCESS;
1236
1237         case ES_OUT_SET_PCR:
1238         case ES_OUT_SET_GROUP_PCR:
1239         {
1240             es_out_pgrm_t *p_pgrm = NULL;
1241             int            i_group = 0;
1242             int64_t        i_pcr;
1243
1244             if( i_query == ES_OUT_SET_PCR )
1245             {
1246                 p_pgrm = p_sys->p_pgrm;
1247             }
1248             else
1249             {
1250                 int i;
1251                 i_group = (int)va_arg( args, int );
1252                 for( i = 0; i < p_sys->i_pgrm; i++ )
1253                 {
1254                     if( p_sys->pgrm[i]->i_id == i_group )
1255                     {
1256                         p_pgrm = p_sys->pgrm[i];
1257                         break;
1258                     }
1259                 }
1260             }
1261             if( p_pgrm == NULL )
1262                 p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
1263
1264             i_pcr = (int64_t)va_arg( args, int64_t );
1265             /* search program */
1266             /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1267             input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1268                                (i_pcr + 11 ) * 9 / 100);
1269             return VLC_SUCCESS;
1270         }
1271
1272         case ES_OUT_RESET_PCR:
1273             for( i = 0; i < p_sys->i_pgrm; i++ )
1274             {
1275                 p_sys->pgrm[i]->clock.i_synchro_state =  SYNCHRO_REINIT;
1276                 p_sys->pgrm[i]->clock.last_pts = 0;
1277             }
1278             return VLC_SUCCESS;
1279
1280         case ES_OUT_GET_TS:
1281             if( p_sys->p_pgrm )
1282             {
1283                 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1284                 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1285                 *pi_ts = input_ClockGetTS( p_sys->p_input,
1286                                            &p_sys->p_pgrm->clock,
1287                                            ( i_ts + 11 ) * 9 / 100 );
1288                 return VLC_SUCCESS;
1289             }
1290             return VLC_EGENERIC;
1291
1292         case ES_OUT_GET_GROUP:
1293             pi = (int*) va_arg( args, int* );
1294             if( p_sys->p_pgrm )
1295                 *pi = p_sys->p_pgrm->i_id;
1296             else
1297                 *pi = -1;    /* FIXME */
1298             return VLC_SUCCESS;
1299
1300         case ES_OUT_SET_GROUP:
1301         {
1302             int j;
1303             i = (int) va_arg( args, int );
1304             for( j = 0; j < p_sys->i_pgrm; j++ )
1305             {
1306                 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1307                 if( p_pgrm->i_id == i )
1308                 {
1309                     EsOutProgramSelect( out, p_pgrm );
1310                     return VLC_SUCCESS;
1311                 }
1312             }
1313             return VLC_EGENERIC;
1314         }
1315
1316         case ES_OUT_SET_FMT:
1317         {
1318             /* This ain't pretty but is need by some demuxers (eg. Ogg )
1319              * to update the p_extra data */
1320             es_format_t *p_fmt;
1321             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1322             p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1323             if( es == NULL ) return VLC_EGENERIC;
1324
1325             if( p_fmt->i_extra )
1326             {
1327                 es->fmt.i_extra = p_fmt->i_extra;
1328                 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1329                 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1330
1331                 if( !es->p_dec ) return VLC_SUCCESS;
1332
1333 #if 1
1334                 input_DecoderDelete( es->p_dec );
1335                 es->p_dec = input_DecoderNew( p_sys->p_input,
1336                                               &es->fmt, VLC_FALSE );
1337
1338 #else
1339                 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1340                 es->p_dec->fmt_in.p_extra =
1341                     realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1342                 memcpy( es->p_dec->fmt_in.p_extra,
1343                         p_fmt->p_extra, p_fmt->i_extra );
1344 #endif
1345             }
1346
1347             return VLC_SUCCESS;
1348         }
1349
1350         case ES_OUT_SET_NEXT_DISPLAY_TIME:
1351         {
1352             int64_t i_date;
1353
1354             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1355             i_date = (int64_t)va_arg( args, int64_t );
1356
1357             if( !es || !es->p_dec )
1358                 return VLC_EGENERIC;
1359
1360             es->i_preroll_end = i_date;
1361             input_DecoderPreroll( es->p_dec, i_date );
1362
1363             return VLC_SUCCESS;
1364         }
1365         case ES_OUT_SET_GROUP_META:
1366         {
1367             int i_group = (int)va_arg( args, int );
1368             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1369
1370             EsOutProgramMeta( out, i_group, p_meta );
1371             return VLC_SUCCESS;
1372         }
1373         case ES_OUT_DEL_GROUP:
1374         {
1375             int i_group = (int)va_arg( args, int );
1376
1377             return EsOutProgramDel( out, i_group );
1378         }
1379
1380         default:
1381             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1382             return VLC_EGENERIC;
1383     }
1384 }
1385
1386 /****************************************************************************
1387  * LanguageGetName: try to expend iso639 into plain name
1388  ****************************************************************************/
1389 static char *LanguageGetName( const char *psz_code )
1390 {
1391     const iso639_lang_t *pl;
1392
1393     if( psz_code == NULL )
1394     {
1395         return strdup( "" );
1396     }
1397
1398     if( strlen( psz_code ) == 2 )
1399     {
1400         pl = GetLang_1( psz_code );
1401     }
1402     else if( strlen( psz_code ) == 3 )
1403     {
1404         pl = GetLang_2B( psz_code );
1405         if( !strcmp( pl->psz_iso639_1, "??" ) )
1406         {
1407             pl = GetLang_2T( psz_code );
1408         }
1409     }
1410     else
1411     {
1412         return strdup( psz_code );
1413     }
1414
1415     if( !strcmp( pl->psz_iso639_1, "??" ) )
1416     {
1417        return strdup( psz_code );
1418     }
1419     else
1420     {
1421         if( *pl->psz_native_name )
1422         {
1423             return strdup( pl->psz_native_name );
1424         }
1425         return strdup( pl->psz_eng_name );
1426     }
1427 }
1428
1429 /* Get a 2 char code */
1430 static char *LanguageGetCode( const char *psz_lang )
1431 {
1432     const iso639_lang_t *pl;
1433
1434     if( psz_lang == NULL || *psz_lang == '\0' )
1435         return strdup("??");
1436
1437     for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1438     {
1439         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1440             !strcasecmp( pl->psz_native_name, psz_lang ) ||
1441             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1442             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1443             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1444             break;
1445     }
1446
1447     if( pl->psz_iso639_1 != NULL )
1448         return strdup( pl->psz_iso639_1 );
1449
1450     return strdup("??");
1451 }
1452
1453 static char **LanguageSplit( const char *psz_langs )
1454 {
1455     char *psz_dup;
1456     char *psz_parser;
1457     char **ppsz = NULL;
1458     int i_psz = 0;
1459
1460     if( psz_langs == NULL ) return NULL;
1461
1462     psz_parser = psz_dup = strdup(psz_langs);
1463
1464     while( psz_parser && *psz_parser )
1465     {
1466         char *psz;
1467         char *psz_code;
1468
1469         psz = strchr(psz_parser, ',' );
1470         if( psz ) *psz++ = '\0';
1471
1472         psz_code = LanguageGetCode( psz_parser );
1473         if( strcmp( psz_code, "??" ) )
1474         {
1475             TAB_APPEND( i_psz, ppsz, psz_code );
1476         }
1477
1478         psz_parser = psz;
1479     }
1480
1481     if( i_psz )
1482     {
1483         TAB_APPEND( i_psz, ppsz, NULL );
1484     }
1485
1486     free( psz_dup );
1487     return ppsz;
1488 }
1489
1490 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
1491 {
1492     int i;
1493
1494     if( !ppsz_langs || !psz_lang ) return -1;
1495
1496     for( i = 0; ppsz_langs[i]; i++ )
1497         if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1498
1499     return -1;
1500 }
1501
1502 /****************************************************************************
1503  * EsOutAddInfo:
1504  * - add meta info to the playlist item
1505  ****************************************************************************/
1506 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1507 {
1508     es_out_sys_t   *p_sys = out->p_sys;
1509     input_thread_t *p_input = p_sys->p_input;
1510     es_format_t    *fmt = &es->fmt;
1511     char           *psz_cat;
1512
1513     /* Add stream info */
1514     asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1515
1516     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1517                    "%.4s", (char*)&fmt->i_codec );
1518
1519     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1520                    "%s", es->psz_language );
1521
1522     /* Add information */
1523     switch( fmt->i_cat )
1524     {
1525     case AUDIO_ES:
1526         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1527                        _("Type"), _("Audio") );
1528
1529         if( fmt->audio.i_channels > 0 )
1530             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1531                            "%d", fmt->audio.i_channels );
1532
1533         if( fmt->audio.i_rate > 0 )
1534             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1535                            _("%d Hz"), fmt->audio.i_rate );
1536
1537         if( fmt->audio.i_bitspersample > 0 )
1538             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1539                            _("Bits per sample"), "%d",
1540                            fmt->audio.i_bitspersample );
1541
1542         if( fmt->i_bitrate > 0 )
1543             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1544                            _("%d kb/s"), fmt->i_bitrate / 1000 );
1545         break;
1546
1547     case VIDEO_ES:
1548         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1549                        _("Type"), _("Video") );
1550
1551         if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1552             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1553                            _("Resolution"), "%dx%d",
1554                            fmt->video.i_width, fmt->video.i_height );
1555
1556         if( fmt->video.i_visible_width > 0 &&
1557             fmt->video.i_visible_height > 0 )
1558             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1559                            _("Display resolution"), "%dx%d",
1560                            fmt->video.i_visible_width,
1561                            fmt->video.i_visible_height);
1562        if( fmt->video.i_frame_rate > 0 &&
1563            fmt->video.i_frame_rate_base > 0 )
1564            input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1565                           _("Frame rate"), "%f",
1566                           (float)fmt->video.i_frame_rate / 
1567                           fmt->video.i_frame_rate_base );
1568         break;
1569
1570     case SPU_ES:
1571         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1572                        _("Type"), _("Subtitle") );
1573         break;
1574
1575     default:
1576         break;
1577     }
1578
1579     free( psz_cat );
1580 }