]> git.sesse.net Git - vlc/blob - src/input/es_out.c
* src/input/es_out.c: Improved the kludge for teletext streams without
[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     }
758     if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
759     {
760         mtime_t current_date = mdate();
761         if( !p_block->i_pts
762                || p_block->i_pts > current_date + 10000000
763                || current_date > p_block->i_pts )
764         {
765             /* ETSI EN 300 472 Annex A : do not take into account the PTS
766              * for teletext streams. */
767             p_block->i_pts = current_date + 400000
768                                + p_input->i_pts_delay + i_delay;
769         }
770     }
771
772     p_block->i_rate = p_input->i_rate;
773
774     /* TODO handle mute */
775     if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
776         p_input->i_rate == INPUT_RATE_DEFAULT ) )
777     {
778         input_DecoderDecode( es->p_dec, p_block );
779     }
780     else
781     {
782         block_Release( p_block );
783     }
784
785     return VLC_SUCCESS;
786 }
787
788 /*****************************************************************************
789  * EsOutDel:
790  *****************************************************************************/
791 static void EsOutDel( es_out_t *out, es_out_id_t *es )
792 {
793     es_out_sys_t *p_sys = out->p_sys;
794
795     /* We don't try to reselect */
796     if( es->p_dec )
797         EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
798
799     if( es->p_pgrm == p_sys->p_pgrm )
800         EsOutESVarUpdate( out, es, VLC_TRUE );
801
802     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
803
804     es->p_pgrm->i_es--;
805     if( es->p_pgrm->i_es == 0 )
806     {
807         msg_Warn( p_sys->p_input, "Program doesn't contain anymore ES, "
808                   "TODO cleaning ?" );
809     }
810
811     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
812     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
813     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
814
815     switch( es->fmt.i_cat )
816     {
817         case AUDIO_ES:
818             p_sys->i_audio--;
819             break;
820         case SPU_ES:
821             p_sys->i_sub--;
822             break;
823         case VIDEO_ES:
824             p_sys->i_video--;
825             break;
826     }
827
828     if( es->psz_language )
829         free( es->psz_language );
830
831     es_format_Clean( &es->fmt );
832
833     free( es );
834 }
835
836 /**
837  * Control query handler
838  *
839  * \param out the es_out to control
840  * \param i_query A es_out query as defined in include/ninput.h
841  * \param args a variable list of arguments for the query
842  * \return VLC_SUCCESS or an error code
843  */
844 static int EsOutControl( es_out_t *out, int i_query, va_list args )
845 {
846     es_out_sys_t *p_sys = out->p_sys;
847     vlc_bool_t  b, *pb;
848     int         i, *pi;
849
850     es_out_id_t *es;
851
852     switch( i_query )
853     {
854         case ES_OUT_SET_ES_STATE:
855             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
856             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
857             if( b && es->p_dec == NULL )
858             {
859                 EsSelect( out, es );
860                 return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
861             }
862             else if( !b && es->p_dec )
863             {
864                 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
865                 return VLC_SUCCESS;
866             }
867             return VLC_SUCCESS;
868
869         case ES_OUT_GET_ES_STATE:
870             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
871             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
872
873             *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
874             return VLC_SUCCESS;
875
876         case ES_OUT_SET_ACTIVE:
877         {
878             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
879             p_sys->b_active = b;
880             /* Needed ? */
881             if( b )
882                 var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
883             return VLC_SUCCESS;
884         }
885
886         case ES_OUT_GET_ACTIVE:
887             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
888             *pb = p_sys->b_active;
889             return VLC_SUCCESS;
890
891         case ES_OUT_SET_MODE:
892             i = (int) va_arg( args, int );
893             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
894                 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
895             {
896                 p_sys->i_mode = i;
897
898                 /* Reapply policy mode */
899                 for( i = 0; i < p_sys->i_es; i++ )
900                 {
901                     if( p_sys->es[i]->p_dec )
902                     {
903                         EsUnselect( out, p_sys->es[i],
904                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
905                     }
906                 }
907                 for( i = 0; i < p_sys->i_es; i++ )
908                 {
909                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
910                 }
911                 return VLC_SUCCESS;
912             }
913             return VLC_EGENERIC;
914
915         case ES_OUT_GET_MODE:
916             pi = (int*) va_arg( args, int* );
917             *pi = p_sys->i_mode;
918             return VLC_SUCCESS;
919
920         case ES_OUT_SET_ES:
921             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
922             /* Special case NULL, NULL+i_cat */
923             if( es == NULL )
924             {
925                 for( i = 0; i < p_sys->i_es; i++ )
926                 {
927                     if( p_sys->es[i]->p_dec )
928                         EsUnselect( out, p_sys->es[i],
929                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
930                 }
931             }
932             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
933             {
934                 for( i = 0; i < p_sys->i_es; i++ )
935                 {
936                     if( p_sys->es[i]->p_dec &&
937                         p_sys->es[i]->fmt.i_cat == AUDIO_ES )
938                         EsUnselect( out, p_sys->es[i],
939                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
940                 }
941             }
942             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
943             {
944                 for( i = 0; i < p_sys->i_es; i++ )
945                 {
946                     if( p_sys->es[i]->p_dec &&
947                         p_sys->es[i]->fmt.i_cat == VIDEO_ES )
948                         EsUnselect( out, p_sys->es[i],
949                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
950                 }
951             }
952             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
953             {
954                 for( i = 0; i < p_sys->i_es; i++ )
955                 {
956                     if( p_sys->es[i]->p_dec &&
957                         p_sys->es[i]->fmt.i_cat == SPU_ES )
958                         EsUnselect( out, p_sys->es[i],
959                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
960                 }
961             }
962             else
963             {
964                 for( i = 0; i < p_sys->i_es; i++ )
965                 {
966                     if( es == p_sys->es[i] )
967                     {
968                         EsOutSelect( out, es, VLC_TRUE );
969                         break;
970                     }
971                 }
972             }
973             return VLC_SUCCESS;
974
975         case ES_OUT_SET_PCR:
976         case ES_OUT_SET_GROUP_PCR:
977         {
978             es_out_pgrm_t *p_pgrm = NULL;
979             int            i_group = 0;
980             int64_t        i_pcr;
981
982             if( i_query == ES_OUT_SET_PCR )
983             {
984                 p_pgrm = p_sys->p_pgrm;
985             }
986             else
987             {
988                 int i;
989                 i_group = (int)va_arg( args, int );
990                 for( i = 0; i < p_sys->i_pgrm; i++ )
991                 {
992                     if( p_sys->pgrm[i]->i_id == i_group )
993                     {
994                         p_pgrm = p_sys->pgrm[i];
995                         break;
996                     }
997                 }
998             }
999             if( p_pgrm == NULL )
1000                 p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
1001
1002             i_pcr = (int64_t)va_arg( args, int64_t );
1003             /* search program */
1004             /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
1005             input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
1006                                (i_pcr + 11 ) * 9 / 100);
1007             return VLC_SUCCESS;
1008         }
1009
1010         case ES_OUT_RESET_PCR:
1011             for( i = 0; i < p_sys->i_pgrm; i++ )
1012             {
1013                 p_sys->pgrm[i]->clock.i_synchro_state =  SYNCHRO_REINIT;
1014                 p_sys->pgrm[i]->clock.last_pts = 0;
1015             }
1016             return VLC_SUCCESS;
1017
1018         case ES_OUT_GET_TS:
1019             if( p_sys->p_pgrm )
1020             {
1021                 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1022                 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1023                 *pi_ts = input_ClockGetTS( p_sys->p_input,
1024                                            &p_sys->p_pgrm->clock,
1025                                            ( i_ts + 11 ) * 9 / 100 );
1026                 return VLC_SUCCESS;
1027             }
1028             return VLC_EGENERIC;
1029
1030         case ES_OUT_GET_GROUP:
1031             pi = (int*) va_arg( args, int* );
1032             if( p_sys->p_pgrm )
1033                 *pi = p_sys->p_pgrm->i_id;
1034             else
1035                 *pi = -1;    /* FIXME */
1036             return VLC_SUCCESS;
1037
1038         case ES_OUT_SET_GROUP:
1039         {
1040             int j;
1041             i = (int) va_arg( args, int );
1042             for( j = 0; j < p_sys->i_pgrm; j++ )
1043             {
1044                 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1045                 if( p_pgrm->i_id == i )
1046                 {
1047                     EsOutProgramSelect( out, p_pgrm );
1048                     return VLC_SUCCESS;
1049                 }
1050             }
1051             return VLC_EGENERIC;
1052         }
1053
1054         case ES_OUT_SET_FMT:
1055         {
1056             /* This ain't pretty but is need by some demuxers (eg. Ogg )
1057              * to update the p_extra data */
1058             es_format_t *p_fmt;
1059             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1060             p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1061             if( es == NULL ) return VLC_EGENERIC;
1062
1063             if( p_fmt->i_extra )
1064             {
1065                 es->fmt.i_extra = p_fmt->i_extra;
1066                 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1067                 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1068
1069                 if( !es->p_dec ) return VLC_SUCCESS;
1070
1071                 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1072                 es->p_dec->fmt_in.p_extra =
1073                     realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1074                 memcpy( es->p_dec->fmt_in.p_extra,
1075                         p_fmt->p_extra, p_fmt->i_extra );
1076             }
1077
1078             return VLC_SUCCESS;
1079         }
1080
1081         default:
1082             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1083             return VLC_EGENERIC;
1084     }
1085 }
1086
1087 /****************************************************************************
1088  * LanguageGetName: try to expend iso639 into plain name
1089  ****************************************************************************/
1090 static char *LanguageGetName( const char *psz_code )
1091 {
1092     const iso639_lang_t *pl;
1093
1094     if( psz_code == NULL )
1095     {
1096         return strdup( "" );
1097     }
1098
1099     if( strlen( psz_code ) == 2 )
1100     {
1101         pl = GetLang_1( psz_code );
1102     }
1103     else if( strlen( psz_code ) == 3 )
1104     {
1105         pl = GetLang_2B( psz_code );
1106         if( !strcmp( pl->psz_iso639_1, "??" ) )
1107         {
1108             pl = GetLang_2T( psz_code );
1109         }
1110     }
1111     else
1112     {
1113         return strdup( psz_code );
1114     }
1115
1116     if( !strcmp( pl->psz_iso639_1, "??" ) )
1117     {
1118        return strdup( psz_code );
1119     }
1120     else
1121     {
1122         if( *pl->psz_native_name )
1123         {
1124             return strdup( pl->psz_native_name );
1125         }
1126         return strdup( pl->psz_eng_name );
1127     }
1128 }
1129
1130 /****************************************************************************
1131  * EsOutAddInfo:
1132  * - add meta info to the playlist item
1133  ****************************************************************************/
1134 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
1135 {
1136     es_out_sys_t   *p_sys = out->p_sys;
1137     input_thread_t *p_input = p_sys->p_input;
1138     es_format_t    *fmt = &es->fmt;
1139     char           *psz_cat;
1140
1141     /* Add stream info */
1142     asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
1143
1144     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
1145                    "%.4s", (char*)&fmt->i_codec );
1146
1147     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
1148                    "%s", es->psz_language );
1149
1150     /* Add information */
1151     switch( fmt->i_cat )
1152     {
1153     case AUDIO_ES:
1154         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1155                        _("Type"), _("Audio") );
1156
1157         if( fmt->audio.i_channels > 0 )
1158             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
1159                            "%d", fmt->audio.i_channels );
1160
1161         if( fmt->audio.i_rate > 0 )
1162             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
1163                            _("%d Hz"), fmt->audio.i_rate );
1164
1165         if( fmt->audio.i_bitspersample > 0 )
1166             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1167                            _("Bits per sample"), "%d",
1168                            fmt->audio.i_bitspersample );
1169
1170         if( fmt->i_bitrate > 0 )
1171             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
1172                            _("%d kb/s"), fmt->i_bitrate / 1000 );
1173         break;
1174
1175     case VIDEO_ES:
1176         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1177                        _("Type"), _("Video") );
1178
1179         if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1180             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1181                            _("Resolution"), "%dx%d",
1182                            fmt->video.i_width, fmt->video.i_height );
1183
1184         if( fmt->video.i_visible_width > 0 &&
1185             fmt->video.i_visible_height > 0 )
1186             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1187                            _("Display resolution"), "%dx%d",
1188                            fmt->video.i_visible_width,
1189                            fmt->video.i_visible_height);
1190         break;
1191
1192     case SPU_ES:
1193         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1194                        _("Type"), _("Subtitle") );
1195         break;
1196
1197     default:
1198         break;
1199     }
1200
1201     free( psz_cat );
1202 }