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