]> git.sesse.net Git - vlc/blob - src/input/es_out.c
Use vlc_fourcc_GetDescription for item infos.
[vlc] / src / input / es_out.c
1 /*****************************************************************************
2  * es_out.c: Es Out handler for input.
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <assert.h>
34 #include <vlc_common.h>
35
36 #include <vlc_input.h>
37 #include <vlc_es_out.h>
38 #include <vlc_block.h>
39 #include <vlc_aout.h>
40 #include <vlc_fourcc.h>
41
42 #include "input_internal.h"
43 #include "clock.h"
44 #include "decoder.h"
45 #include "es_out.h"
46 #include "event.h"
47
48 #include "../stream_output/stream_output.h"
49
50 #include <vlc_iso_lang.h>
51 /* FIXME we should find a better way than including that */
52 #include "../text/iso-639_def.h"
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 typedef struct
58 {
59     /* Program ID */
60     int i_id;
61
62     /* Number of es for this pgrm */
63     int i_es;
64
65     bool b_selected;
66     bool b_scrambled;
67
68     /* Clock for this program */
69     input_clock_t *p_clock;
70
71     char    *psz_name;
72     char    *psz_now_playing;
73     char    *psz_publisher;
74
75     vlc_epg_t *p_epg;
76 } es_out_pgrm_t;
77
78 struct es_out_id_t
79 {
80     /* ES ID */
81     int       i_id;
82     es_out_pgrm_t *p_pgrm;
83
84     /* */
85     bool b_scrambled;
86
87     /* Channel in the track type */
88     int         i_channel;
89     es_format_t fmt;
90     char        *psz_language;
91     char        *psz_language_code;
92
93     decoder_t   *p_dec;
94     decoder_t   *p_dec_record;
95
96     /* Fields for Video with CC */
97     bool  pb_cc_present[4];
98     es_out_id_t  *pp_cc_es[4];
99
100     /* Field for CC track from a master video */
101     es_out_id_t *p_master;
102
103     /* ID for the meta data */
104     int         i_meta_id;
105 };
106
107 struct es_out_sys_t
108 {
109     input_thread_t *p_input;
110
111     /* */
112     vlc_mutex_t   lock;
113
114     /* all programs */
115     int           i_pgrm;
116     es_out_pgrm_t **pgrm;
117     es_out_pgrm_t **pp_selected_pgrm; /* --programs */
118     es_out_pgrm_t *p_pgrm;  /* Master program */
119
120     /* all es */
121     int         i_id;
122     int         i_es;
123     es_out_id_t **es;
124
125     /* mode gestion */
126     bool  b_active;
127     int         i_mode;
128
129     /* es count */
130     int         i_audio;
131     int         i_video;
132     int         i_sub;
133
134     /* es to select */
135     int         i_audio_last, i_audio_id;
136     int         i_sub_last, i_sub_id;
137     int         i_default_sub_id;   /* As specified in container; if applicable */
138     char        **ppsz_audio_language;
139     char        **ppsz_sub_language;
140
141     /* current main es */
142     es_out_id_t *p_es_audio;
143     es_out_id_t *p_es_video;
144     es_out_id_t *p_es_sub;
145
146     /* delay */
147     int64_t i_audio_delay;
148     int64_t i_spu_delay;
149
150     /* Clock configuration */
151     mtime_t     i_pts_delay;
152     int         i_cr_average;
153     int         i_rate;
154
155     /* */
156     bool        b_paused;
157     mtime_t     i_pause_date;
158
159     /* Current preroll */
160     mtime_t     i_preroll_end;
161
162     /* Used for buffering */
163     bool        b_buffering;
164     mtime_t     i_buffering_extra_initial;
165     mtime_t     i_buffering_extra_stream;
166     mtime_t     i_buffering_extra_system;
167
168     /* Record */
169     sout_instance_t *p_sout_record;
170 };
171
172 static es_out_id_t *EsOutAdd    ( es_out_t *, const es_format_t * );
173 static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
174 static void         EsOutDel    ( es_out_t *, es_out_id_t * );
175 static int          EsOutControl( es_out_t *, int i_query, va_list );
176 static void         EsOutDelete ( es_out_t * );
177
178 static void         EsOutSelect( es_out_t *, es_out_id_t *es, bool b_force );
179 static void         EsOutUpdateInfo( es_out_t *, es_out_id_t *es, const es_format_t *, const vlc_meta_t * );
180 static int          EsOutSetRecord(  es_out_t *, bool b_record );
181
182 static bool EsIsSelected( es_out_id_t *es );
183 static void EsSelect( es_out_t *out, es_out_id_t *es );
184 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
185 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es );
186 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
187 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date );
188 static void EsOutProgramsChangeRate( es_out_t *out );
189 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
190 static char *LanguageGetName( const char *psz_code );
191 static char *LanguageGetCode( const char *psz_lang );
192 static char **LanguageSplit( const char *psz_langs );
193 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
194
195 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
196
197 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
198     VLC_FOURCC('c', 'c', '1', ' '),
199     VLC_FOURCC('c', 'c', '2', ' '),
200     VLC_FOURCC('c', 'c', '3', ' '),
201     VLC_FOURCC('c', 'c', '4', ' '),
202 };
203 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
204 {
205     int i;
206     for( i = 0; i < 4; i++ )
207     {
208         if( fcc == EsOutFourccClosedCaptions[i] )
209             return i;
210     }
211     return -1;
212 }
213 static inline bool EsFmtIsTeletext( const es_format_t *p_fmt )
214 {
215     return p_fmt->i_cat == SPU_ES && p_fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
216 }
217
218 /*****************************************************************************
219  * input_EsOutNew:
220  *****************************************************************************/
221 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
222 {
223     es_out_t     *out = malloc( sizeof( *out ) );
224     if( !out )
225         return NULL;
226
227     es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
228     if( !p_sys )
229     {
230         free( out );
231         return NULL;
232     }
233
234     out->pf_add     = EsOutAdd;
235     out->pf_send    = EsOutSend;
236     out->pf_del     = EsOutDel;
237     out->pf_control = EsOutControl;
238     out->pf_destroy = EsOutDelete;
239     out->p_sys      = p_sys;
240     out->b_sout     = p_input->p->p_sout != NULL;
241
242
243     vlc_mutex_init_recursive( &p_sys->lock );
244     p_sys->p_input = p_input;
245
246     p_sys->b_active = false;
247     p_sys->i_mode   = ES_OUT_MODE_AUTO;
248
249
250     TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
251     p_sys->p_pgrm   = NULL;
252
253     p_sys->i_id    = 0;
254
255     TAB_INIT( p_sys->i_es, p_sys->es );
256
257     p_sys->i_audio = 0;
258     p_sys->i_video = 0;
259     p_sys->i_sub   = 0;
260
261     /* */
262     p_sys->i_audio_last = var_GetInteger( p_input, "audio-track" );
263
264     p_sys->i_sub_last = var_GetInteger( p_input, "sub-track" );
265
266     p_sys->i_default_sub_id   = -1;
267
268     if( !p_input->b_preparsing )
269     {
270         char *psz_string;
271
272         psz_string = var_GetString( p_input, "audio-language" );
273         p_sys->ppsz_audio_language = LanguageSplit( psz_string );
274         if( p_sys->ppsz_audio_language )
275         {
276             for( int i = 0; p_sys->ppsz_audio_language[i]; i++ )
277                 msg_Dbg( p_input, "selected audio language[%d] %s",
278                          i, p_sys->ppsz_audio_language[i] );
279         }
280         free( psz_string );
281
282         psz_string = var_GetString( p_input, "sub-language" );
283         p_sys->ppsz_sub_language = LanguageSplit( psz_string );
284         if( p_sys->ppsz_sub_language )
285         {
286             for( int i = 0; p_sys->ppsz_sub_language[i]; i++ )
287                 msg_Dbg( p_input, "selected subtitle language[%d] %s",
288                          i, p_sys->ppsz_sub_language[i] );
289         }
290         free( psz_string );
291     }
292     else
293     {
294         p_sys->ppsz_sub_language = NULL;
295         p_sys->ppsz_audio_language = NULL;
296     }
297
298     p_sys->i_audio_id = var_GetInteger( p_input, "audio-track-id" );
299
300     p_sys->i_sub_id = var_GetInteger( p_input, "sub-track-id" );
301
302     p_sys->p_es_audio = NULL;
303     p_sys->p_es_video = NULL;
304     p_sys->p_es_sub   = NULL;
305
306     p_sys->i_audio_delay= 0;
307     p_sys->i_spu_delay  = 0;
308
309     p_sys->b_paused = false;
310     p_sys->i_pause_date = -1;
311
312     p_sys->i_rate = i_rate;
313     p_sys->i_pts_delay = 0;
314     p_sys->i_cr_average = 0;
315
316     p_sys->b_buffering = true;
317     p_sys->i_buffering_extra_initial = 0;
318     p_sys->i_buffering_extra_stream = 0;
319     p_sys->i_buffering_extra_system = 0;
320     p_sys->i_preroll_end = -1;
321
322     p_sys->p_sout_record = NULL;
323
324     return out;
325 }
326
327 /*****************************************************************************
328  *
329  *****************************************************************************/
330 static void EsOutDelete( es_out_t *out )
331 {
332     es_out_sys_t *p_sys = out->p_sys;
333     int i;
334
335     if( p_sys->p_sout_record )
336         EsOutSetRecord( out, false );
337
338     for( i = 0; i < p_sys->i_es; i++ )
339     {
340         if( p_sys->es[i]->p_dec )
341             input_DecoderDelete( p_sys->es[i]->p_dec );
342
343         free( p_sys->es[i]->psz_language );
344         free( p_sys->es[i]->psz_language_code );
345         es_format_Clean( &p_sys->es[i]->fmt );
346
347         free( p_sys->es[i] );
348     }
349     if( p_sys->ppsz_audio_language )
350     {
351         for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
352             free( p_sys->ppsz_audio_language[i] );
353         free( p_sys->ppsz_audio_language );
354     }
355     if( p_sys->ppsz_sub_language )
356     {
357         for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
358             free( p_sys->ppsz_sub_language[i] );
359         free( p_sys->ppsz_sub_language );
360     }
361     free( p_sys->es );
362
363     /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
364     for( i = 0; i < p_sys->i_pgrm; i++ )
365     {
366         es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
367         input_clock_Delete( p_pgrm->p_clock );
368         free( p_pgrm->psz_now_playing );
369         free( p_pgrm->psz_publisher );
370         free( p_pgrm->psz_name );
371         if( p_pgrm->p_epg )
372             vlc_epg_Delete( p_pgrm->p_epg );
373
374         free( p_pgrm );
375     }
376     TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
377     vlc_mutex_destroy( &p_sys->lock );
378
379     free( p_sys );
380     free( out );
381 }
382
383 static mtime_t EsOutGetWakeup( es_out_t *out )
384 {
385     es_out_sys_t   *p_sys = out->p_sys;
386     input_thread_t *p_input = p_sys->p_input;
387
388     if( !p_sys->p_pgrm )
389         return 0;
390
391     /* We do not have a wake up date if the input cannot have its speed
392      * controlled or sout is imposing its own or while buffering
393      *
394      * FIXME for !p_input->p->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
395     if( !p_input->p->b_can_pace_control ||
396         p_input->p->b_out_pace_control ||
397         p_sys->b_buffering )
398         return 0;
399
400     return input_clock_GetWakeup( p_sys->p_pgrm->p_clock );
401 }
402
403 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
404 {
405     int i;
406     if( i_id < 0 )
407     {
408         /* Special HACK, -i_id is the cat of the stream */
409         return (es_out_id_t*)((uint8_t*)NULL-i_id);
410     }
411
412     for( i = 0; i < out->p_sys->i_es; i++ )
413     {
414         if( out->p_sys->es[i]->i_id == i_id )
415             return out->p_sys->es[i];
416     }
417     return NULL;
418 }
419
420 static bool EsOutDecodersIsEmpty( es_out_t *out )
421 {
422     es_out_sys_t      *p_sys = out->p_sys;
423     int i;
424
425     if( p_sys->b_buffering && p_sys->p_pgrm )
426     {
427         EsOutDecodersStopBuffering( out, true );
428         if( p_sys->b_buffering )
429             return true;
430     }
431
432     for( i = 0; i < p_sys->i_es; i++ )
433     {
434         es_out_id_t *es = p_sys->es[i];
435
436         if( es->p_dec && !input_DecoderIsEmpty( es->p_dec ) )
437             return false;
438         if( es->p_dec_record && !input_DecoderIsEmpty( es->p_dec_record ) )
439             return false;
440     }
441     return true;
442 }
443
444 static void EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
445 {
446     es_out_sys_t *p_sys = out->p_sys;
447
448     if( i_cat == AUDIO_ES )
449         p_sys->i_audio_delay = i_delay;
450     else if( i_cat == SPU_ES )
451         p_sys->i_spu_delay = i_delay;
452
453     for( int i = 0; i < p_sys->i_es; i++ )
454         EsOutDecoderChangeDelay( out, p_sys->es[i] );
455 }
456
457 static int EsOutSetRecord(  es_out_t *out, bool b_record )
458 {
459     es_out_sys_t   *p_sys = out->p_sys;
460     input_thread_t *p_input = p_sys->p_input;
461
462     assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
463
464     if( b_record )
465     {
466         char *psz_path = var_CreateGetString( p_input, "input-record-path" );
467         if( !psz_path || *psz_path == '\0' )
468         {
469             free( psz_path );
470             psz_path = strdup( config_GetHomeDir() );
471         }
472
473         char *psz_sout = NULL;  // TODO conf
474
475         if( !psz_sout && psz_path )
476         {
477             char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
478             if( psz_file )
479             {
480                 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
481                     psz_sout = NULL;
482                 free( psz_file );
483             }
484         }
485         free( psz_path );
486
487         if( !psz_sout )
488             return VLC_EGENERIC;
489
490 #ifdef ENABLE_SOUT
491         p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
492 #endif
493         free( psz_sout );
494
495         if( !p_sys->p_sout_record )
496             return VLC_EGENERIC;
497
498         for( int i = 0; i < p_sys->i_es; i++ )
499         {
500             es_out_id_t *p_es = p_sys->es[i];
501
502             if( !p_es->p_dec || p_es->p_master )
503                 continue;
504
505             p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
506             if( p_es->p_dec_record && p_sys->b_buffering )
507                 input_DecoderStartBuffering( p_es->p_dec_record );
508         }
509     }
510     else
511     {
512         for( int i = 0; i < p_sys->i_es; i++ )
513         {
514             es_out_id_t *p_es = p_sys->es[i];
515
516             if( !p_es->p_dec_record )
517                 continue;
518
519             input_DecoderDelete( p_es->p_dec_record );
520             p_es->p_dec_record = NULL;
521         }
522 #ifdef ENABLE_SOUT
523         sout_DeleteInstance( p_sys->p_sout_record );
524 #endif
525         p_sys->p_sout_record = NULL;
526     }
527
528     return VLC_SUCCESS;
529 }
530 static void EsOutChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
531 {
532     es_out_sys_t *p_sys = out->p_sys;
533
534     /* XXX the order is important */
535     if( b_paused )
536     {
537         EsOutDecodersChangePause( out, true, i_date );
538         EsOutProgramChangePause( out, true, i_date );
539     }
540     else
541     {
542         if( p_sys->i_buffering_extra_initial > 0 )
543         {
544             mtime_t i_stream_start;
545             mtime_t i_system_start;
546             mtime_t i_stream_duration;
547             mtime_t i_system_duration;
548             int i_ret;
549             i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
550                                           &i_stream_start, &i_system_start,
551                                           &i_stream_duration, &i_system_duration );
552             if( !i_ret )
553             {
554                 /* FIXME pcr != exactly what wanted */
555                 const mtime_t i_used = /*(i_stream_duration - p_sys->p_input->p->i_pts_delay)*/ p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
556                 i_date -= i_used;
557             }
558             p_sys->i_buffering_extra_initial = 0;
559             p_sys->i_buffering_extra_stream = 0;
560             p_sys->i_buffering_extra_system = 0;
561         }
562         EsOutProgramChangePause( out, false, i_date );
563         EsOutDecodersChangePause( out, false, i_date );
564
565         EsOutProgramsChangeRate( out );
566     }
567     p_sys->b_paused = b_paused;
568     p_sys->i_pause_date = i_date;
569 }
570
571 static void EsOutChangeRate( es_out_t *out, int i_rate )
572 {
573     es_out_sys_t      *p_sys = out->p_sys;
574
575     p_sys->i_rate = i_rate;
576
577     if( !p_sys->b_paused )
578         EsOutProgramsChangeRate( out );
579 }
580
581 static void EsOutChangePosition( es_out_t *out )
582 {
583     es_out_sys_t      *p_sys = out->p_sys;
584
585     input_SendEventCache( p_sys->p_input, 0.0 );
586
587     for( int i = 0; i < p_sys->i_es; i++ )
588     {
589         es_out_id_t *p_es = p_sys->es[i];
590
591         if( !p_es->p_dec )
592             continue;
593
594         input_DecoderStartBuffering( p_es->p_dec );
595
596         if( p_es->p_dec_record )
597             input_DecoderStartBuffering( p_es->p_dec_record );
598     }
599
600     for( int i = 0; i < p_sys->i_pgrm; i++ )
601         input_clock_Reset( p_sys->pgrm[i]->p_clock );
602
603     p_sys->b_buffering = true;
604     p_sys->i_buffering_extra_initial = 0;
605     p_sys->i_buffering_extra_stream = 0;
606     p_sys->i_buffering_extra_system = 0;
607     p_sys->i_preroll_end = -1;
608 }
609
610
611
612 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
613 {
614     es_out_sys_t *p_sys = out->p_sys;
615     int i_ret;
616
617     mtime_t i_stream_start;
618     mtime_t i_system_start;
619     mtime_t i_stream_duration;
620     mtime_t i_system_duration;
621     i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
622                                   &i_stream_start, &i_system_start,
623                                   &i_stream_duration, &i_system_duration );
624     assert( !i_ret || b_forced );
625     if( i_ret )
626         return;
627
628     mtime_t i_preroll_duration = 0;
629     if( p_sys->i_preroll_end >= 0 )
630         i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
631
632     const mtime_t i_buffering_duration = p_sys->i_pts_delay +
633                                          i_preroll_duration +
634                                          p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
635
636     if( i_stream_duration <= i_buffering_duration && !b_forced )
637     {
638         const double f_level = (double)i_stream_duration / i_buffering_duration;
639         input_SendEventCache( p_sys->p_input, f_level );
640
641         msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
642         return;
643     }
644     input_SendEventCache( p_sys->p_input, 1.0 );
645
646     msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
647               (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
648     p_sys->b_buffering = false;
649     p_sys->i_preroll_end = -1;
650
651     if( p_sys->i_buffering_extra_initial > 0 )
652     {
653         /* FIXME wrong ? */
654         return;
655     }
656
657     const mtime_t i_decoder_buffering_start = mdate();
658     for( int i = 0; i < p_sys->i_es; i++ )
659     {
660         es_out_id_t *p_es = p_sys->es[i];
661
662         if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
663             continue;
664         input_DecoderWaitBuffering( p_es->p_dec );
665         if( p_es->p_dec_record )
666             input_DecoderWaitBuffering( p_es->p_dec_record );
667     }
668
669     msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
670               (int)(mdate() - i_decoder_buffering_start)/1000 );
671
672     /* Here is a good place to destroy unused vout with every demuxer */
673     input_resource_TerminateVout( p_sys->p_input->p->p_resource );
674
675     /* */
676     const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
677     const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
678
679     input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_current_date + i_wakeup_delay - i_buffering_duration );
680
681     for( int i = 0; i < p_sys->i_es; i++ )
682     {
683         es_out_id_t *p_es = p_sys->es[i];
684
685         if( !p_es->p_dec )
686             continue;
687
688         input_DecoderStopBuffering( p_es->p_dec );
689         if( p_es->p_dec_record )
690             input_DecoderStopBuffering( p_es->p_dec_record );
691     }
692 }
693 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
694 {
695     es_out_sys_t *p_sys = out->p_sys;
696
697     /* Pause decoders first */
698     for( int i = 0; i < p_sys->i_es; i++ )
699     {
700         es_out_id_t *es = p_sys->es[i];
701
702         if( es->p_dec )
703         {
704             input_DecoderChangePause( es->p_dec, b_paused, i_date );
705             if( es->p_dec_record )
706                 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
707         }
708     }
709 }
710 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
711 {
712     es_out_sys_t *p_sys = out->p_sys;
713
714     for( int i = 0; i < p_sys->i_pgrm; i++ )
715         input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
716 }
717
718 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
719 {
720     es_out_sys_t *p_sys = out->p_sys;
721
722     mtime_t i_delay = 0;
723     if( p_es->fmt.i_cat == AUDIO_ES )
724         i_delay = p_sys->i_audio_delay;
725     else if( p_es->fmt.i_cat == SPU_ES )
726         i_delay = p_sys->i_spu_delay;
727
728     if( i_delay != 0 )
729     {
730         if( p_es->p_dec )
731             input_DecoderChangeDelay( p_es->p_dec, i_delay );
732         if( p_es->p_dec_record )
733             input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
734     }
735 }
736 static void EsOutProgramsChangeRate( es_out_t *out )
737 {
738     es_out_sys_t      *p_sys = out->p_sys;
739
740     for( int i = 0; i < p_sys->i_pgrm; i++ )
741         input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
742 }
743
744 static void EsOutFrameNext( es_out_t *out )
745 {
746     es_out_sys_t *p_sys = out->p_sys;
747     es_out_id_t *p_es_video = NULL;
748
749     if( p_sys->b_buffering )
750     {
751         msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
752         return;
753     }
754
755     assert( p_sys->b_paused );
756
757     for( int i = 0; i < p_sys->i_es; i++ )
758     {
759         es_out_id_t *p_es = p_sys->es[i];
760
761         if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
762         {
763             p_es_video = p_es;
764             break;
765         }
766     }
767
768     if( !p_es_video )
769     {
770         msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
771         return;
772     }
773
774     mtime_t i_duration;
775     input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
776
777     msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
778
779     if( i_duration <= 0 )
780         i_duration = 40*1000;
781
782     /* FIXME it is not a clean way ? */
783     if( p_sys->i_buffering_extra_initial <= 0 )
784     {
785         mtime_t i_stream_start;
786         mtime_t i_system_start;
787         mtime_t i_stream_duration;
788         mtime_t i_system_duration;
789         int i_ret;
790
791         i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
792                                       &i_stream_start, &i_system_start,
793                                       &i_stream_duration, &i_system_duration );
794         if( i_ret )
795             return;
796
797         p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
798         p_sys->i_buffering_extra_system =
799         p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
800     }
801
802     const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
803
804     p_sys->b_buffering = true;
805     p_sys->i_buffering_extra_system += i_duration;
806     p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
807                                       ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
808                                                 INPUT_RATE_DEFAULT / i_rate;
809
810     p_sys->i_preroll_end = -1;
811 }
812 static mtime_t EsOutGetBuffering( es_out_t *out )
813 {
814     es_out_sys_t *p_sys = out->p_sys;
815
816     if( !p_sys->p_pgrm )
817         return 0;
818
819     int i_ret;
820     mtime_t i_stream_start;
821     mtime_t i_system_start;
822     mtime_t i_stream_duration;
823     mtime_t i_system_duration;
824     i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
825                                   &i_stream_start, &i_system_start,
826                                   &i_stream_duration, &i_system_duration );
827
828     if( i_ret )
829         return 0;
830
831     mtime_t i_delay;
832
833     if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
834     {
835         i_delay = i_stream_duration;
836     }
837     else
838     {
839         mtime_t i_system_duration;
840         if( p_sys->b_paused )
841         {
842             i_system_duration = p_sys->i_pause_date  - i_system_start;
843             if( p_sys->i_buffering_extra_initial > 0 )
844                 i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
845         }
846         else
847         {
848             i_system_duration = mdate() - i_system_start;
849         }
850
851         const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
852         i_delay = p_sys->i_pts_delay - i_consumed;
853     }
854     if( i_delay < 0 )
855         return 0;
856     return i_delay;
857 }
858
859 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
860                                      const es_format_t *fmt, const char *psz_language,
861                                      bool b_delete )
862 {
863     es_out_sys_t      *p_sys = out->p_sys;
864     input_thread_t    *p_input = p_sys->p_input;
865     vlc_value_t       val, text;
866
867     if( b_delete )
868     {
869         if( EsFmtIsTeletext( fmt ) )
870             input_SendEventTeletextDel( p_sys->p_input, i_id );
871
872         input_SendEventEsDel( p_input, fmt->i_cat, i_id );
873         return;
874     }
875
876     /* Get the number of ES already added */
877     const char *psz_var;
878     if( fmt->i_cat == AUDIO_ES )
879         psz_var = "audio-es";
880     else if( fmt->i_cat == VIDEO_ES )
881         psz_var = "video-es";
882     else
883         psz_var = "spu-es";
884
885     var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
886     if( val.i_int == 0 )
887     {
888         vlc_value_t val2;
889
890         /* First one, we need to add the "Disable" choice */
891         val2.i_int = -1; text.psz_string = _("Disable");
892         var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
893         val.i_int++;
894     }
895
896     /* Take care of the ES description */
897     if( fmt->psz_description && *fmt->psz_description )
898     {
899         if( psz_language && *psz_language )
900         {
901             text.psz_string = malloc( strlen( fmt->psz_description) +
902                                       strlen( psz_language ) + 10 );
903             sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
904                                                    psz_language );
905         }
906         else text.psz_string = strdup( fmt->psz_description );
907     }
908     else
909     {
910         if( psz_language && *psz_language )
911         {
912             if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
913                 text.psz_string = NULL;
914         }
915         else
916         {
917             if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
918                 text.psz_string = NULL;
919         }
920     }
921
922     input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
923     if( EsFmtIsTeletext( fmt ) )
924     {
925         char psz_page[3+1];
926         snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
927                   fmt->subs.teletext.i_magazine,
928                   fmt->subs.teletext.i_page );
929         input_SendEventTeletextAdd( p_sys->p_input,
930                                     i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
931     }
932
933     free( text.psz_string );
934 }
935
936 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
937                               bool b_delete )
938 {
939     EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
940 }
941
942 /* EsOutProgramSelect:
943  *  Select a program and update the object variable
944  */
945 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
946 {
947     es_out_sys_t      *p_sys = out->p_sys;
948     input_thread_t    *p_input = p_sys->p_input;
949     int               i;
950
951     if( p_sys->p_pgrm == p_pgrm )
952         return; /* Nothing to do */
953
954     if( p_sys->p_pgrm )
955     {
956         es_out_pgrm_t *old = p_sys->p_pgrm;
957         msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
958
959         for( i = 0; i < p_sys->i_es; i++ )
960         {
961             if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
962                 p_sys->i_mode != ES_OUT_MODE_ALL )
963                 EsUnselect( out, p_sys->es[i], true );
964         }
965
966         p_sys->p_es_audio = NULL;
967         p_sys->p_es_sub = NULL;
968         p_sys->p_es_video = NULL;
969     }
970
971     msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
972
973     /* Mark it selected */
974     p_pgrm->b_selected = true;
975
976     /* Switch master stream */
977     p_sys->p_pgrm = p_pgrm;
978
979     /* Update "program" */
980     input_SendEventProgramSelect( p_input, p_pgrm->i_id );
981
982     /* Update "es-*" */
983     input_SendEventEsDel( p_input, AUDIO_ES, -1 );
984     input_SendEventEsDel( p_input, VIDEO_ES, -1 );
985     input_SendEventEsDel( p_input, SPU_ES, -1 );
986     input_SendEventTeletextDel( p_input, -1 );
987     input_SendEventProgramScrambled( p_input, p_pgrm->i_id, p_pgrm->b_scrambled );
988
989     /* TODO event */
990     var_SetInteger( p_input, "teletext-es", -1 );
991
992     for( i = 0; i < p_sys->i_es; i++ )
993     {
994         if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
995             EsOutESVarUpdate( out, p_sys->es[i], false );
996         EsOutSelect( out, p_sys->es[i], false );
997     }
998
999     /* Update now playing */
1000     input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1001     input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
1002
1003     input_SendEventMeta( p_input );
1004 }
1005
1006 /* EsOutAddProgram:
1007  *  Add a program
1008  */
1009 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
1010 {
1011     es_out_sys_t      *p_sys = out->p_sys;
1012     input_thread_t    *p_input = p_sys->p_input;
1013
1014     es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
1015     if( !p_pgrm )
1016         return NULL;
1017
1018     /* Init */
1019     p_pgrm->i_id = i_group;
1020     p_pgrm->i_es = 0;
1021     p_pgrm->b_selected = false;
1022     p_pgrm->b_scrambled = false;
1023     p_pgrm->psz_name = NULL;
1024     p_pgrm->psz_now_playing = NULL;
1025     p_pgrm->psz_publisher = NULL;
1026     p_pgrm->p_epg = NULL;
1027     p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1028     if( !p_pgrm->p_clock )
1029     {
1030         free( p_pgrm );
1031         return NULL;
1032     }
1033     input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );
1034
1035
1036     /* Append it */
1037     TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1038
1039     /* Update "program" variable */
1040     input_SendEventProgramAdd( p_input, i_group, NULL );
1041
1042     if( i_group == var_GetInteger( p_input, "program" ) )
1043         EsOutProgramSelect( out, p_pgrm );
1044
1045     return p_pgrm;
1046 }
1047
1048 /* EsOutDelProgram:
1049  *  Delete a program
1050  */
1051 static int EsOutProgramDel( es_out_t *out, int i_group )
1052 {
1053     es_out_sys_t      *p_sys = out->p_sys;
1054     input_thread_t    *p_input = p_sys->p_input;
1055     es_out_pgrm_t     *p_pgrm = NULL;
1056     int               i;
1057
1058     for( i = 0; i < p_sys->i_pgrm; i++ )
1059     {
1060         if( p_sys->pgrm[i]->i_id == i_group )
1061         {
1062             p_pgrm = p_sys->pgrm[i];
1063             break;
1064         }
1065     }
1066
1067     if( p_pgrm == NULL )
1068         return VLC_EGENERIC;
1069
1070     if( p_pgrm->i_es )
1071     {
1072         msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1073                  i_group, p_pgrm->i_es );
1074         return VLC_EGENERIC;
1075     }
1076
1077     TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1078
1079     /* If program is selected we need to unselect it */
1080     if( p_sys->p_pgrm == p_pgrm )
1081         p_sys->p_pgrm = NULL;
1082
1083     input_clock_Delete( p_pgrm->p_clock );
1084
1085     free( p_pgrm->psz_name );
1086     free( p_pgrm->psz_now_playing );
1087     free( p_pgrm->psz_publisher );
1088     if( p_pgrm->p_epg )
1089         vlc_epg_Delete( p_pgrm->p_epg );
1090     free( p_pgrm );
1091
1092     /* Update "program" variable */
1093     input_SendEventProgramDel( p_input, i_group );
1094
1095     return VLC_SUCCESS;
1096 }
1097
1098 /* EsOutProgramFind
1099  */
1100 static es_out_pgrm_t *EsOutProgramFind( es_out_t *p_out, int i_group )
1101 {
1102     es_out_sys_t *p_sys = p_out->p_sys;
1103
1104     for( int i = 0; i < p_sys->i_pgrm; i++ )
1105     {
1106         if( p_sys->pgrm[i]->i_id == i_group )
1107             return p_sys->pgrm[i];
1108     }
1109     return EsOutProgramAdd( p_out, i_group );
1110 }
1111
1112 /* EsOutProgramMeta:
1113  */
1114 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1115 {
1116     char *psz = NULL;
1117     if( p_pgrm->psz_name )
1118     {
1119         if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1120             return NULL;
1121     }
1122     else
1123     {
1124         if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1125             return NULL;
1126     }
1127     return psz;
1128 }
1129
1130 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
1131 {
1132     es_out_sys_t      *p_sys = out->p_sys;
1133     es_out_pgrm_t     *p_pgrm;
1134     input_thread_t    *p_input = p_sys->p_input;
1135     char              *psz_cat;
1136     const char        *psz_title = NULL;
1137     const char        *psz_provider = NULL;
1138     int i;
1139
1140     msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1141
1142     /* Check against empty meta data (empty for what we handle) */
1143     if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1144         !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1145         !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1146         vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1147     {
1148         return;
1149     }
1150     /* Find program */
1151     p_pgrm = EsOutProgramFind( out, i_group );
1152     if( !p_pgrm )
1153         return;
1154
1155     /* */
1156     psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1157     psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1158
1159     /* Update the description text of the program */
1160     if( psz_title && *psz_title )
1161     {
1162         if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1163         {
1164             char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1165
1166             /* Remove old entries */
1167             input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1168             /* TODO update epg name ?
1169              * TODO update scrambled info name ? */
1170             free( psz_cat );
1171         }
1172         free( p_pgrm->psz_name );
1173         p_pgrm->psz_name = strdup( psz_title );
1174
1175         char *psz_text;
1176         if( psz_provider && *psz_provider )
1177         {
1178             if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1179                 psz_text = NULL;
1180         }
1181         else
1182         {
1183             psz_text = strdup( psz_title );
1184         }
1185
1186         /* ugly but it works */
1187         if( psz_text )
1188         {
1189             input_SendEventProgramDel( p_input, i_group );
1190             input_SendEventProgramAdd( p_input, i_group, psz_text );
1191
1192             free( psz_text );
1193         }
1194     }
1195
1196     psz_cat = EsOutProgramGetMetaName( p_pgrm );
1197     if( psz_provider )
1198     {
1199         if( p_sys->p_pgrm == p_pgrm )
1200         {
1201             input_item_SetPublisher( p_input->p->p_item, psz_provider );
1202             input_SendEventMeta( p_input );
1203         }
1204         input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1205     }
1206     char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1207     for( i = 0; ppsz_all_keys[i]; i++ )
1208     {
1209         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1210                        vlc_gettext(ppsz_all_keys[i]),
1211                        vlc_dictionary_value_for_key( &p_meta->extra_tags,
1212                        ppsz_all_keys[i] ) );
1213         free( ppsz_all_keys[i] );
1214     }
1215     free( ppsz_all_keys );
1216
1217     free( psz_cat );
1218 }
1219
1220 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
1221 {
1222     int i;
1223
1224     /* Add new event */
1225     for( i = 0; i < p_src->i_event; i++ )
1226     {
1227         vlc_epg_event_t *p_evt = p_src->pp_event[i];
1228         bool b_add = true;
1229         int j;
1230
1231         for( j = 0; j < p_dst->i_event; j++ )
1232         {
1233             if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
1234             {
1235                 b_add = false;
1236                 break;
1237             }
1238             if( p_dst->pp_event[j]->i_start > p_evt->i_start )
1239                 break;
1240         }
1241         if( b_add )
1242         {
1243             vlc_epg_event_t *p_copy = calloc( 1, sizeof(vlc_epg_event_t) );
1244             if( !p_copy )
1245                 break;
1246             p_copy->i_start = p_evt->i_start;
1247             p_copy->i_duration = p_evt->i_duration;
1248             p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
1249             p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
1250             p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
1251             TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
1252         }
1253     }
1254     /* Update current */
1255     if( p_src->p_current )
1256         vlc_epg_SetCurrent( p_dst, p_src->p_current->i_start );
1257
1258     /* Keep only 1 old event  */
1259     if( p_dst->p_current )
1260     {
1261         while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
1262             TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
1263     }
1264 }
1265
1266 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
1267 {
1268     es_out_sys_t      *p_sys = out->p_sys;
1269     input_thread_t    *p_input = p_sys->p_input;
1270     es_out_pgrm_t     *p_pgrm;
1271     char *psz_cat;
1272
1273     /* Find program */
1274     p_pgrm = EsOutProgramFind( out, i_group );
1275     if( !p_pgrm )
1276         return;
1277
1278     /* Merge EPG */
1279     if( !p_pgrm->p_epg )
1280         p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1281     vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1282
1283     /* Update info */
1284     msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1285
1286     psz_cat = EsOutProgramGetMetaName( p_pgrm );
1287
1288     char *psz_epg;
1289     if( asprintf( &psz_epg, "EPG %s", psz_cat ) >= 0 )
1290     {
1291         input_item_SetEpg( p_input->p->p_item, psz_epg, p_pgrm->p_epg );
1292         free( psz_epg );
1293     }
1294
1295     /* Update now playing */
1296     free( p_pgrm->psz_now_playing );
1297     p_pgrm->psz_now_playing = NULL;
1298     if( p_pgrm->p_epg->p_current && p_pgrm->p_epg->p_current->psz_name && *p_pgrm->p_epg->p_current->psz_name )
1299         p_pgrm->psz_now_playing = strdup( p_pgrm->p_epg->p_current->psz_name );
1300
1301     if( p_pgrm == p_sys->p_pgrm )
1302     {
1303         input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1304         input_SendEventMeta( p_input );
1305     }
1306
1307     if( p_pgrm->psz_now_playing )
1308     {
1309         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1310             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1311             p_pgrm->psz_now_playing );
1312     }
1313     else
1314     {
1315         input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1316             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1317     }
1318
1319     free( psz_cat );
1320 }
1321
1322 static void EsOutProgramUpdateScrambled( es_out_t *p_out, es_out_pgrm_t *p_pgrm )
1323 {
1324     es_out_sys_t    *p_sys = p_out->p_sys;
1325     input_thread_t  *p_input = p_sys->p_input;
1326     bool b_scrambled = false;
1327
1328     for( int i = 0; i < p_sys->i_es; i++ )
1329     {
1330         if( p_sys->es[i]->p_pgrm == p_pgrm && p_sys->es[i]->b_scrambled )
1331         {
1332             b_scrambled = true;
1333             break;
1334         }
1335     }
1336     if( !p_pgrm->b_scrambled == !b_scrambled )
1337         return;
1338
1339     p_pgrm->b_scrambled = b_scrambled;
1340     char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1341
1342     if( b_scrambled )
1343         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Scrambled"), _("Yes") );
1344     else
1345         input_Control( p_input, INPUT_DEL_INFO, psz_cat, _("Scrambled") );
1346
1347     input_SendEventProgramScrambled( p_input, p_pgrm->i_id, b_scrambled );
1348 }
1349
1350 /* EsOutAdd:
1351  *  Add an es_out
1352  */
1353 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1354 {
1355     es_out_sys_t      *p_sys = out->p_sys;
1356     input_thread_t    *p_input = p_sys->p_input;
1357
1358     if( fmt->i_group < 0 )
1359     {
1360         msg_Err( p_input, "invalid group number" );
1361         return NULL;
1362     }
1363
1364     es_out_id_t   *es = malloc( sizeof( *es ) );
1365     es_out_pgrm_t *p_pgrm;
1366     int i;
1367
1368     if( !es )
1369         return NULL;
1370
1371     vlc_mutex_lock( &p_sys->lock );
1372
1373     /* Search the program */
1374     p_pgrm = EsOutProgramFind( out, fmt->i_group );
1375     if( !p_pgrm )
1376     {
1377         vlc_mutex_unlock( &p_sys->lock );
1378         free( es );
1379         return NULL;
1380     }
1381
1382     /* Increase ref count for program */
1383     p_pgrm->i_es++;
1384
1385     /* Set up ES */
1386     es->p_pgrm = p_pgrm;
1387     es_format_Copy( &es->fmt, fmt );
1388     if( es->fmt.i_id < 0 )
1389         es->fmt.i_id = out->p_sys->i_id;
1390     es->i_id = es->fmt.i_id;
1391     es->i_meta_id = out->p_sys->i_id;
1392     es->b_scrambled = false;
1393
1394     switch( es->fmt.i_cat )
1395     {
1396     case AUDIO_ES:
1397     {
1398         audio_replay_gain_t rg;
1399
1400         es->i_channel = p_sys->i_audio;
1401
1402         memset( &rg, 0, sizeof(rg) );
1403         vlc_mutex_lock( &p_input->p->p_item->lock );
1404         vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
1405         vlc_mutex_unlock( &p_input->p->p_item->lock );
1406
1407         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1408         {
1409             if( !es->fmt.audio_replay_gain.pb_peak[i] )
1410             {
1411                 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1412                 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1413             }
1414             if( !es->fmt.audio_replay_gain.pb_gain[i] )
1415             {
1416                 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1417                 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1418             }
1419         }
1420         break;
1421     }
1422
1423     case VIDEO_ES:
1424         es->i_channel = p_sys->i_video;
1425         if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1426             vlc_ureduce( &es->fmt.video.i_frame_rate,
1427                          &es->fmt.video.i_frame_rate_base,
1428                          es->fmt.video.i_frame_rate,
1429                          es->fmt.video.i_frame_rate_base, 0 );
1430         break;
1431
1432     case SPU_ES:
1433         es->i_channel = p_sys->i_sub;
1434         break;
1435
1436     default:
1437         es->i_channel = 0;
1438         break;
1439     }
1440     es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1441     es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1442     es->p_dec = NULL;
1443     es->p_dec_record = NULL;
1444     for( i = 0; i < 4; i++ )
1445         es->pb_cc_present[i] = false;
1446     es->p_master = NULL;
1447
1448     if( es->p_pgrm == p_sys->p_pgrm )
1449         EsOutESVarUpdate( out, es, false );
1450
1451     /* Select it if needed */
1452     EsOutSelect( out, es, false );
1453
1454
1455     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1456     p_sys->i_id++;  /* always incremented */
1457     switch( es->fmt.i_cat )
1458     {
1459         case AUDIO_ES:
1460             p_sys->i_audio++;
1461             break;
1462         case SPU_ES:
1463             p_sys->i_sub++;
1464             break;
1465         case VIDEO_ES:
1466             p_sys->i_video++;
1467             break;
1468     }
1469
1470     EsOutUpdateInfo( out, es, &es->fmt, NULL );
1471
1472     if( es->b_scrambled )
1473         EsOutProgramUpdateScrambled( out, es->p_pgrm );
1474
1475     vlc_mutex_unlock( &p_sys->lock );
1476
1477     return es;
1478 }
1479
1480 static bool EsIsSelected( es_out_id_t *es )
1481 {
1482     if( es->p_master )
1483     {
1484         bool b_decode = false;
1485         if( es->p_master->p_dec )
1486         {
1487             int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1488             if( i_channel != -1 )
1489                 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1490         }
1491         return b_decode;
1492     }
1493     else
1494     {
1495         return es->p_dec != NULL;
1496     }
1497 }
1498 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1499 {
1500     es_out_sys_t   *p_sys = out->p_sys;
1501     input_thread_t *p_input = p_sys->p_input;
1502
1503     p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1504     if( p_es->p_dec )
1505     {
1506         if( p_sys->b_buffering )
1507             input_DecoderStartBuffering( p_es->p_dec );
1508
1509         if( !p_es->p_master && p_sys->p_sout_record )
1510         {
1511             p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1512             if( p_es->p_dec_record && p_sys->b_buffering )
1513                 input_DecoderStartBuffering( p_es->p_dec_record );
1514         }
1515     }
1516
1517     EsOutDecoderChangeDelay( out, p_es );
1518 }
1519 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1520 {
1521     VLC_UNUSED(out);
1522
1523     if( !p_es->p_dec )
1524         return;
1525
1526     input_DecoderDelete( p_es->p_dec );
1527     p_es->p_dec = NULL;
1528
1529     if( p_es->p_dec_record )
1530     {
1531         input_DecoderDelete( p_es->p_dec_record );
1532         p_es->p_dec_record = NULL;
1533     }
1534 }
1535
1536 static void EsSelect( es_out_t *out, es_out_id_t *es )
1537 {
1538     es_out_sys_t   *p_sys = out->p_sys;
1539     input_thread_t *p_input = p_sys->p_input;
1540
1541     if( EsIsSelected( es ) )
1542     {
1543         msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1544         return;
1545     }
1546
1547     if( es->p_master )
1548     {
1549         int i_channel;
1550         if( !es->p_master->p_dec )
1551             return;
1552
1553         i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1554         if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1555             return;
1556     }
1557     else
1558     {
1559         if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1560         {
1561             if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1562             {
1563                 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1564                          es->i_id );
1565                 return;
1566             }
1567         }
1568         else if( es->fmt.i_cat == AUDIO_ES )
1569         {
1570             if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1571             {
1572                 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1573                          es->i_id );
1574                 return;
1575             }
1576         }
1577         if( es->fmt.i_cat == SPU_ES )
1578         {
1579             if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1580             {
1581                 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1582                          es->i_id );
1583                 return;
1584             }
1585         }
1586
1587         EsCreateDecoder( out, es );
1588
1589         if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1590             return;
1591     }
1592
1593     /* Mark it as selected */
1594     input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1595     input_SendEventTeletextSelect( p_input, EsFmtIsTeletext( &es->fmt ) ? es->i_id : -1 );
1596 }
1597
1598 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1599 {
1600     es_out_sys_t   *p_sys = out->p_sys;
1601     input_thread_t *p_input = p_sys->p_input;
1602
1603     if( !EsIsSelected( es ) )
1604     {
1605         msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1606         return;
1607     }
1608
1609     if( es->p_master )
1610     {
1611         if( es->p_master->p_dec )
1612         {
1613             int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1614             if( i_channel != -1 )
1615                 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1616         }
1617     }
1618     else
1619     {
1620         const int i_spu_id = var_GetInteger( p_input, "spu-es");
1621         int i;
1622         for( i = 0; i < 4; i++ )
1623         {
1624             if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1625                 continue;
1626
1627             if( i_spu_id == es->pp_cc_es[i]->i_id )
1628             {
1629                 /* Force unselection of the CC */
1630                 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1631             }
1632             EsOutDel( out, es->pp_cc_es[i] );
1633
1634             es->pb_cc_present[i] = false;
1635         }
1636         EsDestroyDecoder( out, es );
1637     }
1638
1639     if( !b_update )
1640         return;
1641
1642     /* Mark it as unselected */
1643     input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1644     if( EsFmtIsTeletext( &es->fmt ) )
1645         input_SendEventTeletextSelect( p_input, -1 );
1646 }
1647
1648 /**
1649  * Select an ES given the current mode
1650  * XXX: you need to take a the lock before (stream.stream_lock)
1651  *
1652  * \param out The es_out structure
1653  * \param es es_out_id structure
1654  * \param b_force ...
1655  * \return nothing
1656  */
1657 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1658 {
1659     es_out_sys_t      *p_sys = out->p_sys;
1660
1661     int i_cat = es->fmt.i_cat;
1662
1663     if( !p_sys->b_active ||
1664         ( !b_force && es->fmt.i_priority < 0 ) )
1665     {
1666         return;
1667     }
1668
1669     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1670     {
1671         if( !EsIsSelected( es ) )
1672             EsSelect( out, es );
1673     }
1674     else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1675     {
1676         vlc_value_t val;
1677         int i;
1678         var_Get( p_sys->p_input, "programs", &val );
1679         for ( i = 0; i < val.p_list->i_count; i++ )
1680         {
1681             if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1682             {
1683                 if( !EsIsSelected( es ) )
1684                     EsSelect( out, es );
1685                 break;
1686             }
1687         }
1688         var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1689     }
1690     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1691     {
1692         int i_wanted  = -1;
1693
1694         if( es->p_pgrm != p_sys->p_pgrm )
1695             return;
1696
1697         if( i_cat == AUDIO_ES )
1698         {
1699             int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1700                                      es->psz_language_code );
1701
1702             if( p_sys->p_es_audio &&
1703                 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1704             {
1705                 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1706                                          p_sys->p_es_audio->psz_language_code );
1707
1708                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1709                     return;
1710                 i_wanted = es->i_channel;
1711             }
1712             else
1713             {
1714                 /* Select audio if (no audio selected yet)
1715                  * - no audio-language
1716                  * - no audio code for the ES
1717                  * - audio code in the requested list */
1718                 if( idx1 >= 0 ||
1719                     !strcmp( es->psz_language_code, "??" ) ||
1720                     !p_sys->ppsz_audio_language )
1721                     i_wanted = es->i_channel;
1722             }
1723
1724             if( p_sys->i_audio_last >= 0 )
1725                 i_wanted = p_sys->i_audio_last;
1726
1727             if( p_sys->i_audio_id >= 0 )
1728             {
1729                 if( es->i_id == p_sys->i_audio_id )
1730                     i_wanted = es->i_channel;
1731                 else
1732                     return;
1733             }
1734         }
1735         else if( i_cat == SPU_ES )
1736         {
1737             int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1738                                      es->psz_language_code );
1739
1740             if( p_sys->p_es_sub &&
1741                 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1742             {
1743                 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1744                                          p_sys->p_es_sub->psz_language_code );
1745
1746                 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1747                         idx1, es->psz_language_code, idx2,
1748                         p_sys->p_es_sub->psz_language_code );
1749
1750                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1751                     return;
1752                 /* We found a SPU that matches our language request */
1753                 i_wanted  = es->i_channel;
1754             }
1755             else if( idx1 >= 0 )
1756             {
1757                 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1758                         idx1, es->psz_language_code );
1759
1760                 i_wanted  = es->i_channel;
1761             }
1762             else if( p_sys->i_default_sub_id >= 0 )
1763             {
1764                 if( es->i_id == p_sys->i_default_sub_id )
1765                     i_wanted = es->i_channel;
1766             }
1767
1768             if( p_sys->i_sub_last >= 0 )
1769                 i_wanted  = p_sys->i_sub_last;
1770
1771             if( p_sys->i_sub_id >= 0 )
1772             {
1773                 if( es->i_id == p_sys->i_sub_id )
1774                     i_wanted = es->i_channel;
1775                 else
1776                     return;
1777             }
1778         }
1779         else if( i_cat == VIDEO_ES )
1780         {
1781             i_wanted  = es->i_channel;
1782         }
1783
1784         if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1785             EsSelect( out, es );
1786     }
1787
1788     /* FIXME TODO handle priority here */
1789     if( EsIsSelected( es ) )
1790     {
1791         if( i_cat == AUDIO_ES )
1792         {
1793             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1794                 p_sys->p_es_audio &&
1795                 p_sys->p_es_audio != es &&
1796                 EsIsSelected( p_sys->p_es_audio ) )
1797             {
1798                 EsUnselect( out, p_sys->p_es_audio, false );
1799             }
1800             p_sys->p_es_audio = es;
1801         }
1802         else if( i_cat == SPU_ES )
1803         {
1804             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1805                 p_sys->p_es_sub &&
1806                 p_sys->p_es_sub != es &&
1807                 EsIsSelected( p_sys->p_es_sub ) )
1808             {
1809                 EsUnselect( out, p_sys->p_es_sub, false );
1810             }
1811             p_sys->p_es_sub = es;
1812         }
1813         else if( i_cat == VIDEO_ES )
1814         {
1815             p_sys->p_es_video = es;
1816         }
1817     }
1818 }
1819
1820 /**
1821  * Send a block for the given es_out
1822  *
1823  * \param out the es_out to send from
1824  * \param es the es_out_id
1825  * \param p_block the data block to send
1826  */
1827 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1828 {
1829     es_out_sys_t   *p_sys = out->p_sys;
1830     input_thread_t *p_input = p_sys->p_input;
1831     int i_total = 0;
1832
1833     if( libvlc_stats( p_input ) )
1834     {
1835         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1836         stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1837                              p_block->i_buffer, &i_total );
1838         stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1839                            (float)i_total, NULL );
1840
1841         /* Update number of corrupted data packats */
1842         if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
1843         {
1844             stats_UpdateInteger( p_input, p_input->p->counters.p_demux_corrupted,
1845                                  1, NULL );
1846         }
1847         /* Update number of discontinuities */
1848         if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
1849         {
1850             stats_UpdateInteger( p_input, p_input->p->counters.p_demux_discontinuity,
1851                                  1, NULL );
1852         }
1853         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1854     }
1855
1856     vlc_mutex_lock( &p_sys->lock );
1857
1858     /* Mark preroll blocks */
1859     if( p_sys->i_preroll_end >= 0 )
1860     {
1861         int64_t i_date = p_block->i_pts;
1862         if( i_date <= 0 )
1863             i_date = p_block->i_dts;
1864
1865         if( i_date < p_sys->i_preroll_end )
1866             p_block->i_flags |= BLOCK_FLAG_PREROLL;
1867     }
1868
1869     p_block->i_rate = 0;
1870
1871     if( !es->p_dec )
1872     {
1873         block_Release( p_block );
1874         vlc_mutex_unlock( &p_sys->lock );
1875         return VLC_SUCCESS;
1876     }
1877
1878     /* Check for sout mode */
1879     if( out->b_sout )
1880     {
1881         /* FIXME review this, proper lock may be missing */
1882         if( p_input->p->p_sout->i_out_pace_nocontrol > 0 &&
1883             p_input->p->b_out_pace_control )
1884         {
1885             msg_Dbg( p_input, "switching to sync mode" );
1886             p_input->p->b_out_pace_control = false;
1887         }
1888         else if( p_input->p->p_sout->i_out_pace_nocontrol <= 0 &&
1889                  !p_input->p->b_out_pace_control )
1890         {
1891             msg_Dbg( p_input, "switching to async mode" );
1892             p_input->p->b_out_pace_control = true;
1893         }
1894     }
1895
1896     /* Decode */
1897     if( es->p_dec_record )
1898     {
1899         block_t *p_dup = block_Duplicate( p_block );
1900         if( p_dup )
1901             input_DecoderDecode( es->p_dec_record, p_dup,
1902                                  p_input->p->b_out_pace_control );
1903     }
1904     input_DecoderDecode( es->p_dec, p_block,
1905                          p_input->p->b_out_pace_control );
1906
1907     es_format_t fmt_dsc;
1908     vlc_meta_t  *p_meta_dsc;
1909     if( input_DecoderHasFormatChanged( es->p_dec, &fmt_dsc, &p_meta_dsc ) )
1910     {
1911         EsOutUpdateInfo( out, es, &fmt_dsc, p_meta_dsc );
1912
1913         es_format_Clean( &fmt_dsc );
1914         if( p_meta_dsc )
1915             vlc_meta_Delete( p_meta_dsc );
1916     }
1917
1918     /* Check CC status */
1919     bool pb_cc[4];
1920
1921     input_DecoderIsCcPresent( es->p_dec, pb_cc );
1922     for( int i = 0; i < 4; i++ )
1923     {
1924         static const vlc_fourcc_t fcc[4] = {
1925             VLC_FOURCC('c', 'c', '1', ' '),
1926             VLC_FOURCC('c', 'c', '2', ' '),
1927             VLC_FOURCC('c', 'c', '3', ' '),
1928             VLC_FOURCC('c', 'c', '4', ' '),
1929         };
1930         es_format_t fmt;
1931
1932         if(  es->pb_cc_present[i] || !pb_cc[i] )
1933             continue;
1934         msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1935
1936         es_format_Init( &fmt, SPU_ES, fcc[i] );
1937         fmt.i_group = es->fmt.i_group;
1938         if( asprintf( &fmt.psz_description,
1939                       _("Closed captions %u"), 1 + i ) == -1 )
1940             fmt.psz_description = NULL;
1941         es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1942         es->pp_cc_es[i]->p_master = es;
1943         es_format_Clean( &fmt );
1944
1945         /* */
1946         es->pb_cc_present[i] = true;
1947     }
1948
1949     vlc_mutex_unlock( &p_sys->lock );
1950
1951     return VLC_SUCCESS;
1952 }
1953
1954 /*****************************************************************************
1955  * EsOutDel:
1956  *****************************************************************************/
1957 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1958 {
1959     es_out_sys_t *p_sys = out->p_sys;
1960     bool b_reselect = false;
1961     int i;
1962
1963     vlc_mutex_lock( &p_sys->lock );
1964
1965     /* We don't try to reselect */
1966     if( es->p_dec )
1967     {
1968         while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
1969         {
1970             if( input_DecoderIsEmpty( es->p_dec ) &&
1971                 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
1972                 break;
1973             /* FIXME there should be a way to have auto deleted es, but there will be
1974              * a problem when another codec of the same type is created (mainly video) */
1975             msleep( 20*1000 );
1976         }
1977         EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1978     }
1979
1980     if( es->p_pgrm == p_sys->p_pgrm )
1981         EsOutESVarUpdate( out, es, true );
1982
1983     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1984
1985     /* Update program */
1986     es->p_pgrm->i_es--;
1987     if( es->p_pgrm->i_es == 0 )
1988         msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1989
1990     if( es->b_scrambled )
1991         EsOutProgramUpdateScrambled( out, es->p_pgrm );
1992
1993     /* */
1994     if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1995         p_sys->p_es_sub == es ) b_reselect = true;
1996
1997     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1998     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1999     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
2000
2001     switch( es->fmt.i_cat )
2002     {
2003         case AUDIO_ES:
2004             p_sys->i_audio--;
2005             break;
2006         case SPU_ES:
2007             p_sys->i_sub--;
2008             break;
2009         case VIDEO_ES:
2010             p_sys->i_video--;
2011             break;
2012     }
2013
2014     /* Re-select another track when needed */
2015     if( b_reselect )
2016     {
2017         for( i = 0; i < p_sys->i_es; i++ )
2018         {
2019             if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
2020                 EsOutSelect( out, p_sys->es[i], false );
2021         }
2022     }
2023
2024     free( es->psz_language );
2025     free( es->psz_language_code );
2026
2027     es_format_Clean( &es->fmt );
2028
2029     vlc_mutex_unlock( &p_sys->lock );
2030
2031     free( es );
2032 }
2033
2034 /**
2035  * Control query handler
2036  *
2037  * \param out the es_out to control
2038  * \param i_query A es_out query as defined in include/ninput.h
2039  * \param args a variable list of arguments for the query
2040  * \return VLC_SUCCESS or an error code
2041  */
2042 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
2043 {
2044     es_out_sys_t *p_sys = out->p_sys;
2045     bool  b, *pb;
2046     int         i;
2047
2048     es_out_id_t *es;
2049
2050     switch( i_query )
2051     {
2052         case ES_OUT_SET_ES_STATE:
2053             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2054             b = (bool) va_arg( args, int );
2055             if( b && !EsIsSelected( es ) )
2056             {
2057                 EsSelect( out, es );
2058                 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
2059             }
2060             else if( !b && EsIsSelected( es ) )
2061             {
2062                 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2063                 return VLC_SUCCESS;
2064             }
2065             return VLC_SUCCESS;
2066
2067         case ES_OUT_GET_ES_STATE:
2068             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2069             pb = (bool*) va_arg( args, bool * );
2070
2071             *pb = EsIsSelected( es );
2072             return VLC_SUCCESS;
2073
2074         case ES_OUT_SET_ACTIVE:
2075         {
2076             b = (bool) va_arg( args, int );
2077             if( b && !p_sys->b_active && p_sys->i_es > 0 )
2078             {
2079                 /* XXX Terminate vout if there are tracks but no video one.
2080                  * This one is not mandatory but is he earliest place where it
2081                  * can be done */
2082                 for( i = 0; i < p_sys->i_es; i++ )
2083                 {
2084                     es_out_id_t *p_es = p_sys->es[i];
2085                     if( p_es->fmt.i_cat == VIDEO_ES )
2086                         break;
2087                 }
2088                 if( i >= p_sys->i_es )
2089                     input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2090             }
2091             p_sys->b_active = b;
2092             return VLC_SUCCESS;
2093         }
2094
2095         case ES_OUT_SET_MODE:
2096             i = (int) va_arg( args, int );
2097             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
2098                 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
2099             {
2100                 p_sys->i_mode = i;
2101
2102                 /* Reapply policy mode */
2103                 for( i = 0; i < p_sys->i_es; i++ )
2104                 {
2105                     if( EsIsSelected( p_sys->es[i] ) )
2106                     {
2107                         EsUnselect( out, p_sys->es[i],
2108                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2109                     }
2110                 }
2111                 for( i = 0; i < p_sys->i_es; i++ )
2112                 {
2113                     EsOutSelect( out, p_sys->es[i], false );
2114                 }
2115                 return VLC_SUCCESS;
2116             }
2117             return VLC_EGENERIC;
2118
2119         case ES_OUT_SET_ES:
2120         case ES_OUT_RESTART_ES:
2121         {
2122             int i_cat;
2123
2124             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2125
2126             if( es == NULL )
2127                 i_cat = UNKNOWN_ES;
2128             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2129                 i_cat = AUDIO_ES;
2130             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2131                 i_cat = VIDEO_ES;
2132             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2133                 i_cat = SPU_ES;
2134             else
2135                 i_cat = -1;
2136
2137             for( i = 0; i < p_sys->i_es; i++ )
2138             {
2139                 if( i_cat == -1 )
2140                 {
2141                     if( es == p_sys->es[i] )
2142                     {
2143                         EsOutSelect( out, es, true );
2144                         break;
2145                     }
2146                 }
2147                 else
2148                 {
2149                     if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2150                     {
2151                         if( EsIsSelected( p_sys->es[i] ) )
2152                         {
2153                             if( i_query == ES_OUT_RESTART_ES )
2154                             {
2155                                 if( p_sys->es[i]->p_dec )
2156                                 {
2157                                     EsDestroyDecoder( out, p_sys->es[i] );
2158                                     EsCreateDecoder( out, p_sys->es[i] );
2159                                 }
2160                             }
2161                             else
2162                             {
2163                                 EsUnselect( out, p_sys->es[i],
2164                                             p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2165                             }
2166                         }
2167                     }
2168                 }
2169             }
2170             return VLC_SUCCESS;
2171         }
2172  
2173         case ES_OUT_SET_ES_DEFAULT:
2174         {
2175             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2176
2177             if( es == NULL )
2178             {
2179                 /*p_sys->i_default_video_id = -1;*/
2180                 /*p_sys->i_default_audio_id = -1;*/
2181                 p_sys->i_default_sub_id = -1;
2182             }
2183             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2184             {
2185                 /*p_sys->i_default_video_id = -1;*/
2186             }
2187             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2188             {
2189                 /*p_sys->i_default_audio_id = -1;*/
2190             }
2191             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2192             {
2193                 p_sys->i_default_sub_id = -1;
2194             }
2195             else
2196             {
2197                 /*if( es->fmt.i_cat == VIDEO_ES )
2198                     p_sys->i_default_video_id = es->i_id;
2199                 else
2200                 if( es->fmt.i_cat == AUDIO_ES )
2201                     p_sys->i_default_audio_id = es->i_id;
2202                 else*/
2203                 if( es->fmt.i_cat == SPU_ES )
2204                     p_sys->i_default_sub_id = es->i_id;
2205             }
2206             return VLC_SUCCESS;
2207         }
2208
2209         case ES_OUT_SET_PCR:
2210         case ES_OUT_SET_GROUP_PCR:
2211         {
2212             es_out_pgrm_t *p_pgrm = NULL;
2213             int            i_group = 0;
2214             int64_t        i_pcr;
2215
2216             if( i_query == ES_OUT_SET_PCR )
2217             {
2218                 p_pgrm = p_sys->p_pgrm;
2219                 if( !p_pgrm )
2220                     p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
2221             }
2222             else
2223             {
2224                 i_group = (int)va_arg( args, int );
2225                 p_pgrm = EsOutProgramFind( out, i_group );
2226             }
2227             if( !p_pgrm )
2228                 return VLC_EGENERIC;
2229
2230             i_pcr = (int64_t)va_arg( args, int64_t );
2231             if( i_pcr <= VLC_TS_INVALID )
2232             {
2233                 msg_Err( p_sys->p_input, "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" );
2234                 return VLC_EGENERIC;
2235             }
2236
2237             /* search program
2238              * TODO do not use mdate() but proper stream acquisition date */
2239             input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2240                                 p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering, i_pcr, mdate() );
2241             /* Check buffering state on master clock update */
2242             if( p_sys->b_buffering && p_pgrm == p_sys->p_pgrm )
2243                 EsOutDecodersStopBuffering( out, false );
2244
2245             return VLC_SUCCESS;
2246         }
2247
2248         case ES_OUT_RESET_PCR:
2249             msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2250             EsOutChangePosition( out );
2251             return VLC_SUCCESS;
2252
2253         case ES_OUT_SET_GROUP:
2254         {
2255             int j;
2256             i = (int) va_arg( args, int );
2257             for( j = 0; j < p_sys->i_pgrm; j++ )
2258             {
2259                 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2260                 if( p_pgrm->i_id == i )
2261                 {
2262                     EsOutProgramSelect( out, p_pgrm );
2263                     return VLC_SUCCESS;
2264                 }
2265             }
2266             return VLC_EGENERIC;
2267         }
2268
2269         case ES_OUT_SET_ES_FMT:
2270         {
2271             /* This ain't pretty but is need by some demuxers (eg. Ogg )
2272              * to update the p_extra data */
2273             es_format_t *p_fmt;
2274             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2275             p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2276             if( es == NULL )
2277                 return VLC_EGENERIC;
2278
2279             if( p_fmt->i_extra )
2280             {
2281                 es->fmt.i_extra = p_fmt->i_extra;
2282                 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
2283                 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2284
2285                 if( !es->p_dec )
2286                     return VLC_SUCCESS;
2287 #if 1
2288                 EsDestroyDecoder( out, es );
2289
2290                 EsCreateDecoder( out, es );
2291 #else
2292                 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2293                 es->p_dec->fmt_in.p_extra =
2294                     realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2295                 memcpy( es->p_dec->fmt_in.p_extra,
2296                         p_fmt->p_extra, p_fmt->i_extra );
2297 #endif
2298             }
2299
2300             return VLC_SUCCESS;
2301         }
2302
2303         case ES_OUT_SET_ES_SCRAMBLED_STATE:
2304         {
2305             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2306             bool b_scrambled = (bool)va_arg( args, int );
2307
2308             if( !es->b_scrambled != !b_scrambled )
2309             {
2310                 es->b_scrambled = b_scrambled;
2311                 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2312             }
2313             return VLC_SUCCESS;
2314         }
2315
2316         case ES_OUT_SET_NEXT_DISPLAY_TIME:
2317         {
2318             const int64_t i_date = (int64_t)va_arg( args, int64_t );
2319
2320             if( i_date < 0 )
2321                 return VLC_EGENERIC;
2322
2323             p_sys->i_preroll_end = i_date;
2324
2325             return VLC_SUCCESS;
2326         }
2327         case ES_OUT_SET_GROUP_META:
2328         {
2329             int i_group = (int)va_arg( args, int );
2330             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2331
2332             EsOutProgramMeta( out, i_group, p_meta );
2333             return VLC_SUCCESS;
2334         }
2335         case ES_OUT_SET_GROUP_EPG:
2336         {
2337             int i_group = (int)va_arg( args, int );
2338             vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2339
2340             EsOutProgramEpg( out, i_group, p_epg );
2341             return VLC_SUCCESS;
2342         }
2343
2344         case ES_OUT_DEL_GROUP:
2345         {
2346             int i_group = (int)va_arg( args, int );
2347
2348             return EsOutProgramDel( out, i_group );
2349         }
2350
2351         case ES_OUT_GET_WAKE_UP:
2352         {
2353             mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2354             *pi_wakeup = EsOutGetWakeup( out );
2355             return VLC_SUCCESS;
2356         }
2357
2358         case ES_OUT_SET_ES_BY_ID:
2359         case ES_OUT_RESTART_ES_BY_ID:
2360         case ES_OUT_SET_ES_DEFAULT_BY_ID:
2361         {
2362             const int i_id = (int)va_arg( args, int );
2363             es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2364             int i_new_query;
2365
2366             switch( i_query )
2367             {
2368             case ES_OUT_SET_ES_BY_ID:         i_new_query = ES_OUT_SET_ES; break;
2369             case ES_OUT_RESTART_ES_BY_ID:     i_new_query = ES_OUT_RESTART_ES; break;
2370             case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2371             default:
2372               assert(0);
2373             }
2374             /* TODO if the lock is made non recursive it should be changed */
2375             int i_ret = es_out_Control( out, i_new_query, p_es );
2376
2377             /* Clean up vout after user action (in active mode only).
2378              * FIXME it does not work well with multiple video windows */
2379             if( p_sys->b_active )
2380                 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2381             return i_ret;
2382         }
2383
2384         case ES_OUT_GET_BUFFERING:
2385             pb = (bool *)va_arg( args, bool* );
2386             *pb = p_sys->b_buffering;
2387             return VLC_SUCCESS;
2388
2389         case ES_OUT_GET_EMPTY:
2390             pb = (bool *)va_arg( args, bool* );
2391             *pb = EsOutDecodersIsEmpty( out );
2392             return VLC_SUCCESS;
2393
2394         case ES_OUT_SET_DELAY:
2395         {
2396             const int i_cat = (int)va_arg( args, int );
2397             const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2398             EsOutSetDelay( out, i_cat, i_delay );
2399             return VLC_SUCCESS;
2400         }
2401
2402         case ES_OUT_SET_RECORD_STATE:
2403             b = (bool) va_arg( args, int );
2404             return EsOutSetRecord( out, b );
2405
2406         case ES_OUT_SET_PAUSE_STATE:
2407         {
2408             const bool b_source_paused = (bool)va_arg( args, int );
2409             const bool b_paused = (bool)va_arg( args, int );
2410             const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2411
2412             assert( !b_source_paused == !b_paused );
2413             EsOutChangePause( out, b_paused, i_date );
2414
2415             return VLC_SUCCESS;
2416         }
2417
2418         case ES_OUT_SET_RATE:
2419         {
2420             const int i_src_rate = (int)va_arg( args, int );
2421             const int i_rate = (int)va_arg( args, int );
2422
2423             assert( i_src_rate == i_rate );
2424             EsOutChangeRate( out, i_rate );
2425
2426             return VLC_SUCCESS;
2427         }
2428
2429         case ES_OUT_SET_TIME:
2430         {
2431             const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2432
2433             assert( i_date == -1 );
2434             EsOutChangePosition( out );
2435
2436             return VLC_SUCCESS;
2437         }
2438
2439         case ES_OUT_SET_FRAME_NEXT:
2440             EsOutFrameNext( out );
2441             return VLC_SUCCESS;
2442
2443         case ES_OUT_SET_TIMES:
2444         {
2445             double f_position = (double)va_arg( args, double );
2446             mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
2447             mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
2448
2449             input_SendEventLength( p_sys->p_input, i_length );
2450
2451             if( !p_sys->b_buffering )
2452             {
2453                 /* Fix for buffering delay */
2454                 const mtime_t i_delay = EsOutGetBuffering( out );
2455
2456                 i_time -= i_delay;
2457                 if( i_time < 0 )
2458                     i_time = 0;
2459
2460                 if( i_length > 0 )
2461                     f_position -= (double)i_delay / i_length;
2462                 if( f_position < 0 )
2463                     f_position = 0;
2464
2465                 input_SendEventPosition( p_sys->p_input, f_position, i_time );
2466             }
2467             return VLC_SUCCESS;
2468         }
2469         case ES_OUT_SET_JITTER:
2470         {
2471             mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
2472             int     i_cr_average = (int)va_arg( args, int );
2473
2474             if( i_pts_delay == p_sys->i_pts_delay &&
2475                 i_cr_average == p_sys->i_cr_average )
2476                 return VLC_SUCCESS;
2477
2478             p_sys->i_pts_delay = i_pts_delay;
2479             p_sys->i_cr_average = i_cr_average;
2480
2481             for( int i = 0; i < p_sys->i_pgrm; i++ )
2482                 input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
2483                                        i_pts_delay, i_cr_average );
2484             return VLC_SUCCESS;
2485         }
2486
2487         default:
2488             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2489             return VLC_EGENERIC;
2490     }
2491 }
2492 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2493 {
2494     es_out_sys_t *p_sys = out->p_sys;
2495     int i_ret;
2496
2497     vlc_mutex_lock( &p_sys->lock );
2498     i_ret = EsOutControlLocked( out, i_query, args );
2499     vlc_mutex_unlock( &p_sys->lock );
2500
2501     return i_ret;
2502 }
2503
2504 /****************************************************************************
2505  * LanguageGetName: try to expend iso639 into plain name
2506  ****************************************************************************/
2507 static char *LanguageGetName( const char *psz_code )
2508 {
2509     const iso639_lang_t *pl;
2510
2511     if( psz_code == NULL )
2512     {
2513         return strdup( "" );
2514     }
2515
2516     if( strlen( psz_code ) == 2 )
2517     {
2518         pl = GetLang_1( psz_code );
2519     }
2520     else if( strlen( psz_code ) == 3 )
2521     {
2522         pl = GetLang_2B( psz_code );
2523         if( !strcmp( pl->psz_iso639_1, "??" ) )
2524         {
2525             pl = GetLang_2T( psz_code );
2526         }
2527     }
2528     else
2529     {
2530         return strdup( psz_code );
2531     }
2532
2533     if( !strcmp( pl->psz_iso639_1, "??" ) )
2534     {
2535        return strdup( psz_code );
2536     }
2537     else
2538     {
2539         if( *pl->psz_native_name )
2540         {
2541             return strdup( pl->psz_native_name );
2542         }
2543         return strdup( pl->psz_eng_name );
2544     }
2545 }
2546
2547 /* Get a 2 char code */
2548 static char *LanguageGetCode( const char *psz_lang )
2549 {
2550     const iso639_lang_t *pl;
2551
2552     if( psz_lang == NULL || *psz_lang == '\0' )
2553         return strdup("??");
2554
2555     for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
2556     {
2557         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2558             !strcasecmp( pl->psz_native_name, psz_lang ) ||
2559             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2560             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2561             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2562             break;
2563     }
2564
2565     if( pl->psz_iso639_1 != NULL )
2566         return strdup( pl->psz_iso639_1 );
2567
2568     return strdup("??");
2569 }
2570
2571 static char **LanguageSplit( const char *psz_langs )
2572 {
2573     char *psz_dup;
2574     char *psz_parser;
2575     char **ppsz = NULL;
2576     int i_psz = 0;
2577
2578     if( psz_langs == NULL ) return NULL;
2579
2580     psz_parser = psz_dup = strdup(psz_langs);
2581
2582     while( psz_parser && *psz_parser )
2583     {
2584         char *psz;
2585         char *psz_code;
2586
2587         psz = strchr(psz_parser, ',' );
2588         if( psz ) *psz++ = '\0';
2589
2590         if( !strcmp( psz_parser, "any" ) )
2591         {
2592             TAB_APPEND( i_psz, ppsz, strdup("any") );
2593         }
2594         else
2595         {
2596             psz_code = LanguageGetCode( psz_parser );
2597             if( strcmp( psz_code, "??" ) )
2598             {
2599                 TAB_APPEND( i_psz, ppsz, psz_code );
2600             }
2601             else
2602             {
2603                 free( psz_code );
2604             }
2605         }
2606
2607         psz_parser = psz;
2608     }
2609
2610     if( i_psz )
2611     {
2612         TAB_APPEND( i_psz, ppsz, NULL );
2613     }
2614
2615     free( psz_dup );
2616     return ppsz;
2617 }
2618
2619 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2620 {
2621     int i;
2622
2623     if( !ppsz_langs || !psz_lang ) return -1;
2624
2625     for( i = 0; ppsz_langs[i]; i++ )
2626     {
2627         if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2628             !strcasecmp( ppsz_langs[i], "any" ) )
2629         {
2630             return i;
2631         }
2632     }
2633
2634     return -1;
2635 }
2636
2637 /****************************************************************************
2638  * EsOutUpdateInfo:
2639  * - add meta info to the playlist item
2640  ****************************************************************************/
2641 static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *fmt, const vlc_meta_t *p_meta )
2642 {
2643     es_out_sys_t   *p_sys = out->p_sys;
2644     input_thread_t *p_input = p_sys->p_input;
2645     const es_format_t *p_fmt_es = &es->fmt;
2646     char           *psz_cat;
2647     lldiv_t         div;
2648
2649     /* Create category name */
2650     if( asprintf( &psz_cat, _("Stream %d"), es->i_meta_id ) == -1 )
2651         return;
2652
2653     /* Remove previous information */
2654     input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
2655
2656     /* Add informations */
2657     const char *psz_type;
2658     switch( fmt->i_cat )
2659     {
2660     case AUDIO_ES:
2661         psz_type = _("Audio");
2662         break;
2663     case VIDEO_ES:
2664         psz_type = _("Video");
2665         break;
2666     case SPU_ES:
2667         psz_type = _("Subtitle");
2668         break;
2669     default:
2670         psz_type = NULL;
2671         break;
2672     }
2673
2674     if( psz_type )
2675         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Type"), psz_type );
2676
2677     if( es->i_meta_id != es->i_id )
2678         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Original ID"),
2679                        "%d", es->i_id );
2680
2681     const char *psz_codec_description =
2682         vlc_fourcc_GetDescription( p_fmt_es->i_cat, p_fmt_es->i_codec );
2683     const vlc_fourcc_t i_codec_fourcc = p_fmt_es->i_original_fourcc ?: p_fmt_es->i_codec;
2684     if( psz_codec_description )
2685         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2686                        "%s (%.4s)", psz_codec_description, (char*)&i_codec_fourcc );
2687     else
2688         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2689                        "%.4s", (char*)&i_codec_fourcc );
2690
2691     if( es->psz_language && *es->psz_language )
2692         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2693                        "%s", es->psz_language );
2694     if( fmt->psz_description && *fmt->psz_description )
2695         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Description"),
2696                        "%s", fmt->psz_description );
2697
2698     switch( fmt->i_cat )
2699     {
2700     case AUDIO_ES:
2701         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2702                        _("Type"), _("Audio") );
2703
2704         if( fmt->audio.i_physical_channels & AOUT_CHAN_PHYSMASK )
2705             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2706                            "%s", _( aout_FormatPrintChannels( &fmt->audio ) ) );
2707         else if( fmt->audio.i_channels > 0 )
2708             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2709                            "%u", fmt->audio.i_channels );
2710
2711         if( fmt->audio.i_rate > 0 )
2712         {
2713             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2714                            _("%u Hz"), fmt->audio.i_rate );
2715             /* FIXME that should be removed or improved ! (used by text/strings.c) */
2716             var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2717         }
2718
2719         unsigned int i_bitspersample = fmt->audio.i_bitspersample;
2720         if( i_bitspersample <= 0 )
2721             i_bitspersample = aout_BitsPerSample( p_fmt_es->i_codec );
2722         if( i_bitspersample > 0 )
2723             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2724                            _("Bits per sample"), "%u",
2725                            i_bitspersample );
2726
2727         if( fmt->i_bitrate > 0 )
2728         {
2729             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2730                            _("%u kb/s"), fmt->i_bitrate / 1000 );
2731             /* FIXME that should be removed or improved ! (used by text/strings.c) */
2732             var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2733         }
2734         for( int i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
2735         {
2736             const audio_replay_gain_t *p_rg = &fmt->audio_replay_gain;
2737             if( !p_rg->pb_gain[i] )
2738                 continue;
2739             const char *psz_name;
2740             if( i == AUDIO_REPLAY_GAIN_TRACK )
2741                 psz_name = _("Track replay gain");
2742             else
2743                 psz_name = _("Album replay gain");
2744             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2745                            psz_name, _("%.2f dB"), p_rg->pf_gain[i] );
2746         }
2747         break;
2748
2749     case VIDEO_ES:
2750         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2751                        _("Type"), _("Video") );
2752
2753         if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2754             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2755                            _("Resolution"), "%ux%u",
2756                            fmt->video.i_width, fmt->video.i_height );
2757
2758         if( fmt->video.i_visible_width > 0 &&
2759             fmt->video.i_visible_height > 0 )
2760             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2761                            _("Display resolution"), "%ux%u",
2762                            fmt->video.i_visible_width,
2763                            fmt->video.i_visible_height);
2764        if( fmt->video.i_frame_rate > 0 &&
2765            fmt->video.i_frame_rate_base > 0 )
2766        {
2767            div = lldiv( (float)fmt->video.i_frame_rate /
2768                                fmt->video.i_frame_rate_base * 1000000,
2769                                1000000 );
2770            if( div.rem > 0 )
2771                input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2772                               _("Frame rate"), "%"PRId64".%06u",
2773                               div.quot, (unsigned int )div.rem );
2774            else
2775                input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2776                               _("Frame rate"), "%"PRId64, div.quot );
2777        }
2778        break;
2779
2780     case SPU_ES:
2781         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2782                        _("Type"), _("Subtitle") );
2783         break;
2784
2785     default:
2786         break;
2787     }
2788
2789     /* Append generic meta */
2790     if( p_meta )
2791     {
2792         char **ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2793         for( int i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
2794         {
2795             char *psz_key = ppsz_all_keys[i];
2796             char *psz_value = vlc_dictionary_value_for_key( &p_meta->extra_tags, psz_key );
2797
2798             if( psz_value )
2799                 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2800                                vlc_gettext(psz_key), vlc_gettext(psz_value) );
2801             free( psz_key );
2802         }
2803         free( ppsz_all_keys );
2804     }
2805
2806     free( psz_cat );
2807 }