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