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