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