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