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