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