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