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