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