]> git.sesse.net Git - vlc/blob - src/input/es_out.c
Do not delay rate change after end of pause.
[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_CODEC_TELETEXT;
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     EsOutProgramsChangeRate( out );
577 }
578
579 static void EsOutChangePosition( es_out_t *out )
580 {
581     es_out_sys_t      *p_sys = out->p_sys;
582
583     input_SendEventCache( p_sys->p_input, 0.0 );
584
585     for( int i = 0; i < p_sys->i_es; i++ )
586     {
587         es_out_id_t *p_es = p_sys->es[i];
588
589         if( !p_es->p_dec )
590             continue;
591
592         input_DecoderStartBuffering( p_es->p_dec );
593
594         if( p_es->p_dec_record )
595             input_DecoderStartBuffering( p_es->p_dec_record );
596     }
597
598     for( int i = 0; i < p_sys->i_pgrm; i++ )
599         input_clock_Reset( p_sys->pgrm[i]->p_clock );
600
601     p_sys->b_buffering = true;
602     p_sys->i_buffering_extra_initial = 0;
603     p_sys->i_buffering_extra_stream = 0;
604     p_sys->i_buffering_extra_system = 0;
605     p_sys->i_preroll_end = -1;
606 }
607
608
609
610 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
611 {
612     es_out_sys_t *p_sys = out->p_sys;
613     int i_ret;
614
615     mtime_t i_stream_start;
616     mtime_t i_system_start;
617     mtime_t i_stream_duration;
618     mtime_t i_system_duration;
619     i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
620                                   &i_stream_start, &i_system_start,
621                                   &i_stream_duration, &i_system_duration );
622     assert( !i_ret || b_forced );
623     if( i_ret )
624         return;
625
626     mtime_t i_preroll_duration = 0;
627     if( p_sys->i_preroll_end >= 0 )
628         i_preroll_duration = __MAX( p_sys->i_preroll_end - i_stream_start, 0 );
629
630     const mtime_t i_buffering_duration = p_sys->i_pts_delay +
631                                          i_preroll_duration +
632                                          p_sys->i_buffering_extra_stream - p_sys->i_buffering_extra_initial;
633
634     if( i_stream_duration <= i_buffering_duration && !b_forced )
635     {
636         const double f_level = (double)i_stream_duration / i_buffering_duration;
637         input_SendEventCache( p_sys->p_input, f_level );
638
639         msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
640         return;
641     }
642     input_SendEventCache( p_sys->p_input, 1.0 );
643
644     msg_Dbg( p_sys->p_input, "Stream buffering done (%d ms in %d ms)",
645               (int)(i_stream_duration/1000), (int)(i_system_duration/1000) );
646     p_sys->b_buffering = false;
647     p_sys->i_preroll_end = -1;
648
649     if( p_sys->i_buffering_extra_initial > 0 )
650     {
651         /* FIXME wrong ? */
652         return;
653     }
654
655     const mtime_t i_decoder_buffering_start = mdate();
656     for( int i = 0; i < p_sys->i_es; i++ )
657     {
658         es_out_id_t *p_es = p_sys->es[i];
659
660         if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
661             continue;
662         input_DecoderWaitBuffering( p_es->p_dec );
663         if( p_es->p_dec_record )
664             input_DecoderWaitBuffering( p_es->p_dec_record );
665     }
666
667     msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
668               (int)(mdate() - i_decoder_buffering_start)/1000 );
669
670     /* Here is a good place to destroy unused vout with every demuxer */
671     input_resource_TerminateVout( p_sys->p_input->p->p_resource );
672
673     /* */
674     const mtime_t i_wakeup_delay = 10*1000; /* FIXME CLEANUP thread wake up time*/
675     const mtime_t i_current_date = p_sys->b_paused ? p_sys->i_pause_date : mdate();
676
677     input_clock_ChangeSystemOrigin( p_sys->p_pgrm->p_clock, i_current_date + i_wakeup_delay - i_buffering_duration );
678
679     for( int i = 0; i < p_sys->i_es; i++ )
680     {
681         es_out_id_t *p_es = p_sys->es[i];
682
683         if( !p_es->p_dec )
684             continue;
685
686         input_DecoderStopBuffering( p_es->p_dec );
687         if( p_es->p_dec_record )
688             input_DecoderStopBuffering( p_es->p_dec_record );
689     }
690 }
691 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
692 {
693     es_out_sys_t *p_sys = out->p_sys;
694
695     /* Pause decoders first */
696     for( int i = 0; i < p_sys->i_es; i++ )
697     {
698         es_out_id_t *es = p_sys->es[i];
699
700         if( es->p_dec )
701         {
702             input_DecoderChangePause( es->p_dec, b_paused, i_date );
703             if( es->p_dec_record )
704                 input_DecoderChangePause( es->p_dec_record, b_paused, i_date );
705         }
706     }
707 }
708 static void EsOutProgramChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
709 {
710     es_out_sys_t *p_sys = out->p_sys;
711
712     for( int i = 0; i < p_sys->i_pgrm; i++ )
713         input_clock_ChangePause( p_sys->pgrm[i]->p_clock, b_paused, i_date );
714 }
715
716 static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
717 {
718     es_out_sys_t *p_sys = out->p_sys;
719
720     mtime_t i_delay = 0;
721     if( p_es->fmt.i_cat == AUDIO_ES )
722         i_delay = p_sys->i_audio_delay;
723     else if( p_es->fmt.i_cat == SPU_ES )
724         i_delay = p_sys->i_spu_delay;
725
726     if( i_delay != 0 )
727     {
728         if( p_es->p_dec )
729             input_DecoderChangeDelay( p_es->p_dec, i_delay );
730         if( p_es->p_dec_record )
731             input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
732     }
733 }
734 static void EsOutProgramsChangeRate( es_out_t *out )
735 {
736     es_out_sys_t      *p_sys = out->p_sys;
737
738     for( int i = 0; i < p_sys->i_pgrm; i++ )
739         input_clock_ChangeRate( p_sys->pgrm[i]->p_clock, p_sys->i_rate );
740 }
741
742 static void EsOutFrameNext( es_out_t *out )
743 {
744     es_out_sys_t *p_sys = out->p_sys;
745     es_out_id_t *p_es_video = NULL;
746
747     if( p_sys->b_buffering )
748     {
749         msg_Warn( p_sys->p_input, "buffering, ignoring 'frame next'" );
750         return;
751     }
752
753     assert( p_sys->b_paused );
754
755     for( int i = 0; i < p_sys->i_es; i++ )
756     {
757         es_out_id_t *p_es = p_sys->es[i];
758
759         if( p_es->fmt.i_cat == VIDEO_ES && p_es->p_dec )
760         {
761             p_es_video = p_es;
762             break;
763         }
764     }
765
766     if( !p_es_video )
767     {
768         msg_Warn( p_sys->p_input, "No video track selected, ignoring 'frame next'" );
769         return;
770     }
771
772     mtime_t i_duration;
773     input_DecoderFrameNext( p_es_video->p_dec, &i_duration );
774
775     msg_Dbg( out->p_sys->p_input, "EsOutFrameNext consummed %d ms", (int)(i_duration/1000) );
776
777     if( i_duration <= 0 )
778         i_duration = 40*1000;
779
780     /* FIXME it is not a clean way ? */
781     if( p_sys->i_buffering_extra_initial <= 0 )
782     {
783         mtime_t i_stream_start;
784         mtime_t i_system_start;
785         mtime_t i_stream_duration;
786         mtime_t i_system_duration;
787         int i_ret;
788
789         i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
790                                       &i_stream_start, &i_system_start,
791                                       &i_stream_duration, &i_system_duration );
792         if( i_ret )
793             return;
794
795         p_sys->i_buffering_extra_initial = 1 + i_stream_duration - p_sys->i_pts_delay; /* FIXME < 0 ? */
796         p_sys->i_buffering_extra_system =
797         p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial;
798     }
799
800     const int i_rate = input_clock_GetRate( p_sys->p_pgrm->p_clock );
801
802     p_sys->b_buffering = true;
803     p_sys->i_buffering_extra_system += i_duration;
804     p_sys->i_buffering_extra_stream = p_sys->i_buffering_extra_initial +
805                                       ( p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial ) *
806                                                 INPUT_RATE_DEFAULT / i_rate;
807
808     p_sys->i_preroll_end = -1;
809 }
810 static mtime_t EsOutGetBuffering( es_out_t *out )
811 {
812     es_out_sys_t *p_sys = out->p_sys;
813
814     if( !p_sys->p_pgrm )
815         return 0;
816
817     int i_ret;
818     mtime_t i_stream_start;
819     mtime_t i_system_start;
820     mtime_t i_stream_duration;
821     mtime_t i_system_duration;
822     i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
823                                   &i_stream_start, &i_system_start,
824                                   &i_stream_duration, &i_system_duration );
825
826     if( i_ret )
827         return 0;
828
829     mtime_t i_delay;
830
831     if( p_sys->b_buffering && p_sys->i_buffering_extra_initial <= 0 )
832     {
833         i_delay = i_stream_duration;
834     }
835     else
836     {
837         mtime_t i_system_duration;
838         if( p_sys->b_paused )
839         {
840             i_system_duration = p_sys->i_pause_date  - i_system_start;
841             if( p_sys->i_buffering_extra_initial > 0 )
842                 i_system_duration += p_sys->i_buffering_extra_system - p_sys->i_buffering_extra_initial;
843         }
844         else
845         {
846             i_system_duration = mdate() - i_system_start;
847         }
848
849         const mtime_t i_consumed = i_system_duration * INPUT_RATE_DEFAULT / p_sys->i_rate - i_stream_duration;
850         i_delay = p_sys->i_pts_delay - i_consumed;
851     }
852     if( i_delay < 0 )
853         return 0;
854     return i_delay;
855 }
856
857 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id,
858                                      const es_format_t *fmt, const char *psz_language,
859                                      bool b_delete )
860 {
861     es_out_sys_t      *p_sys = out->p_sys;
862     input_thread_t    *p_input = p_sys->p_input;
863     vlc_value_t       val, text;
864
865     if( b_delete )
866     {
867         if( EsFmtIsTeletext( fmt ) )
868             input_SendEventTeletextDel( p_sys->p_input, i_id );
869
870         input_SendEventEsDel( p_input, fmt->i_cat, i_id );
871         return;
872     }
873
874     /* Get the number of ES already added */
875     const char *psz_var;
876     if( fmt->i_cat == AUDIO_ES )
877         psz_var = "audio-es";
878     else if( fmt->i_cat == VIDEO_ES )
879         psz_var = "video-es";
880     else
881         psz_var = "spu-es";
882
883     var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
884     if( val.i_int == 0 )
885     {
886         vlc_value_t val2;
887
888         /* First one, we need to add the "Disable" choice */
889         val2.i_int = -1; text.psz_string = _("Disable");
890         var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
891         val.i_int++;
892     }
893
894     /* Take care of the ES description */
895     if( fmt->psz_description && *fmt->psz_description )
896     {
897         if( psz_language && *psz_language )
898         {
899             text.psz_string = malloc( strlen( fmt->psz_description) +
900                                       strlen( psz_language ) + 10 );
901             sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
902                                                    psz_language );
903         }
904         else text.psz_string = strdup( fmt->psz_description );
905     }
906     else
907     {
908         if( psz_language && *psz_language )
909         {
910             if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
911                 text.psz_string = NULL;
912         }
913         else
914         {
915             if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
916                 text.psz_string = NULL;
917         }
918     }
919
920     input_SendEventEsAdd( p_input, fmt->i_cat, i_id, text.psz_string );
921     if( EsFmtIsTeletext( fmt ) )
922     {
923         char psz_page[3+1];
924         snprintf( psz_page, sizeof(psz_page), "%d%2.2x",
925                   fmt->subs.teletext.i_magazine,
926                   fmt->subs.teletext.i_page );
927         input_SendEventTeletextAdd( p_sys->p_input,
928                                     i_id, fmt->subs.teletext.i_magazine >= 0 ? psz_page : NULL );
929     }
930
931     free( text.psz_string );
932 }
933
934 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
935                               bool b_delete )
936 {
937     EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
938 }
939
940 /* EsOutProgramSelect:
941  *  Select a program and update the object variable
942  */
943 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
944 {
945     es_out_sys_t      *p_sys = out->p_sys;
946     input_thread_t    *p_input = p_sys->p_input;
947     int               i;
948
949     if( p_sys->p_pgrm == p_pgrm )
950         return; /* Nothing to do */
951
952     if( p_sys->p_pgrm )
953     {
954         es_out_pgrm_t *old = p_sys->p_pgrm;
955         msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
956
957         for( i = 0; i < p_sys->i_es; i++ )
958         {
959             if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
960                 p_sys->i_mode != ES_OUT_MODE_ALL )
961                 EsUnselect( out, p_sys->es[i], true );
962         }
963
964         p_sys->p_es_audio = NULL;
965         p_sys->p_es_sub = NULL;
966         p_sys->p_es_video = NULL;
967     }
968
969     msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
970
971     /* Mark it selected */
972     p_pgrm->b_selected = true;
973
974     /* Switch master stream */
975     p_sys->p_pgrm = p_pgrm;
976
977     /* Update "program" */
978     input_SendEventProgramSelect( p_input, p_pgrm->i_id );
979
980     /* Update "es-*" */
981     input_SendEventEsDel( p_input, AUDIO_ES, -1 );
982     input_SendEventEsDel( p_input, VIDEO_ES, -1 );
983     input_SendEventEsDel( p_input, SPU_ES, -1 );
984     input_SendEventTeletextDel( p_input, -1 );
985     input_SendEventProgramScrambled( p_input, p_pgrm->i_id, p_pgrm->b_scrambled );
986
987     /* TODO event */
988     var_SetInteger( p_input, "teletext-es", -1 );
989
990     for( i = 0; i < p_sys->i_es; i++ )
991     {
992         if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
993             EsOutESVarUpdate( out, p_sys->es[i], false );
994         EsOutSelect( out, p_sys->es[i], false );
995     }
996
997     /* Update now playing */
998     input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
999     input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
1000
1001     input_SendEventMeta( p_input );
1002 }
1003
1004 /* EsOutAddProgram:
1005  *  Add a program
1006  */
1007 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
1008 {
1009     es_out_sys_t      *p_sys = out->p_sys;
1010     input_thread_t    *p_input = p_sys->p_input;
1011
1012     es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
1013     if( !p_pgrm )
1014         return NULL;
1015
1016     /* Init */
1017     p_pgrm->i_id = i_group;
1018     p_pgrm->i_es = 0;
1019     p_pgrm->b_selected = false;
1020     p_pgrm->b_scrambled = false;
1021     p_pgrm->psz_name = NULL;
1022     p_pgrm->psz_now_playing = NULL;
1023     p_pgrm->psz_publisher = NULL;
1024     p_pgrm->p_epg = NULL;
1025     p_pgrm->p_clock = input_clock_New( p_sys->i_rate );
1026     if( !p_pgrm->p_clock )
1027     {
1028         free( p_pgrm );
1029         return NULL;
1030     }
1031     if( p_sys->b_paused )
1032         input_clock_ChangePause( p_pgrm->p_clock, p_sys->b_paused, p_sys->i_pause_date );
1033     input_clock_SetJitter( p_pgrm->p_clock, p_sys->i_pts_delay, p_sys->i_cr_average );
1034
1035     /* Append it */
1036     TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1037
1038     /* Update "program" variable */
1039     input_SendEventProgramAdd( p_input, i_group, NULL );
1040
1041     if( i_group == var_GetInteger( p_input, "program" ) )
1042         EsOutProgramSelect( out, p_pgrm );
1043
1044     return p_pgrm;
1045 }
1046
1047 /* EsOutDelProgram:
1048  *  Delete a program
1049  */
1050 static int EsOutProgramDel( es_out_t *out, int i_group )
1051 {
1052     es_out_sys_t      *p_sys = out->p_sys;
1053     input_thread_t    *p_input = p_sys->p_input;
1054     es_out_pgrm_t     *p_pgrm = NULL;
1055     int               i;
1056
1057     for( i = 0; i < p_sys->i_pgrm; i++ )
1058     {
1059         if( p_sys->pgrm[i]->i_id == i_group )
1060         {
1061             p_pgrm = p_sys->pgrm[i];
1062             break;
1063         }
1064     }
1065
1066     if( p_pgrm == NULL )
1067         return VLC_EGENERIC;
1068
1069     if( p_pgrm->i_es )
1070     {
1071         msg_Dbg( p_input, "can't delete program %d which still has %i ES",
1072                  i_group, p_pgrm->i_es );
1073         return VLC_EGENERIC;
1074     }
1075
1076     TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
1077
1078     /* If program is selected we need to unselect it */
1079     if( p_sys->p_pgrm == p_pgrm )
1080         p_sys->p_pgrm = NULL;
1081
1082     input_clock_Delete( p_pgrm->p_clock );
1083
1084     free( p_pgrm->psz_name );
1085     free( p_pgrm->psz_now_playing );
1086     free( p_pgrm->psz_publisher );
1087     if( p_pgrm->p_epg )
1088         vlc_epg_Delete( p_pgrm->p_epg );
1089     free( p_pgrm );
1090
1091     /* Update "program" variable */
1092     input_SendEventProgramDel( p_input, i_group );
1093
1094     return VLC_SUCCESS;
1095 }
1096
1097 /* EsOutProgramFind
1098  */
1099 static es_out_pgrm_t *EsOutProgramFind( es_out_t *p_out, int i_group )
1100 {
1101     es_out_sys_t *p_sys = p_out->p_sys;
1102
1103     for( int i = 0; i < p_sys->i_pgrm; i++ )
1104     {
1105         if( p_sys->pgrm[i]->i_id == i_group )
1106             return p_sys->pgrm[i];
1107     }
1108     return EsOutProgramAdd( p_out, i_group );
1109 }
1110
1111 /* EsOutProgramMeta:
1112  */
1113 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
1114 {
1115     char *psz = NULL;
1116     if( p_pgrm->psz_name )
1117     {
1118         if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
1119             return NULL;
1120     }
1121     else
1122     {
1123         if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
1124             return NULL;
1125     }
1126     return psz;
1127 }
1128
1129 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
1130 {
1131     es_out_sys_t      *p_sys = out->p_sys;
1132     es_out_pgrm_t     *p_pgrm;
1133     input_thread_t    *p_input = p_sys->p_input;
1134     char              *psz_cat;
1135     const char        *psz_title = NULL;
1136     const char        *psz_provider = NULL;
1137     int i;
1138
1139     msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
1140
1141     /* Check against empty meta data (empty for what we handle) */
1142     if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
1143         !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
1144         !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
1145         vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
1146     {
1147         return;
1148     }
1149     /* Find program */
1150     p_pgrm = EsOutProgramFind( out, i_group );
1151     if( !p_pgrm )
1152         return;
1153
1154     /* */
1155     psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
1156     psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
1157
1158     /* Update the description text of the program */
1159     if( psz_title && *psz_title )
1160     {
1161         if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
1162         {
1163             char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1164
1165             /* Remove old entries */
1166             input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
1167             /* TODO update epg name ?
1168              * TODO update scrambled info name ? */
1169             free( psz_cat );
1170         }
1171         free( p_pgrm->psz_name );
1172         p_pgrm->psz_name = strdup( psz_title );
1173
1174         char *psz_text;
1175         if( psz_provider && *psz_provider )
1176         {
1177             if( asprintf( &psz_text, "%s [%s]", psz_title, psz_provider ) < 0 )
1178                 psz_text = NULL;
1179         }
1180         else
1181         {
1182             psz_text = strdup( psz_title );
1183         }
1184
1185         /* ugly but it works */
1186         if( psz_text )
1187         {
1188             input_SendEventProgramDel( p_input, i_group );
1189             input_SendEventProgramAdd( p_input, i_group, psz_text );
1190
1191             free( psz_text );
1192         }
1193     }
1194
1195     psz_cat = EsOutProgramGetMetaName( p_pgrm );
1196     if( psz_provider )
1197     {
1198         if( p_sys->p_pgrm == p_pgrm )
1199         {
1200             input_item_SetPublisher( p_input->p->p_item, psz_provider );
1201             input_SendEventMeta( p_input );
1202         }
1203         input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
1204     }
1205     char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
1206     for( i = 0; ppsz_all_keys[i]; i++ )
1207     {
1208         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1209                        vlc_gettext(ppsz_all_keys[i]),
1210                        vlc_dictionary_value_for_key( &p_meta->extra_tags,
1211                        ppsz_all_keys[i] ) );
1212         free( ppsz_all_keys[i] );
1213     }
1214     free( ppsz_all_keys );
1215
1216     free( psz_cat );
1217 }
1218
1219 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
1220 {
1221     int i;
1222
1223     /* Add new event */
1224     for( i = 0; i < p_src->i_event; i++ )
1225     {
1226         vlc_epg_event_t *p_evt = p_src->pp_event[i];
1227         bool b_add = true;
1228         int j;
1229
1230         for( j = 0; j < p_dst->i_event; j++ )
1231         {
1232             if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
1233             {
1234                 b_add = false;
1235                 break;
1236             }
1237             if( p_dst->pp_event[j]->i_start > p_evt->i_start )
1238                 break;
1239         }
1240         if( b_add )
1241         {
1242             vlc_epg_event_t *p_copy = calloc( 1, sizeof(vlc_epg_event_t) );
1243             if( !p_copy )
1244                 break;
1245             p_copy->i_start = p_evt->i_start;
1246             p_copy->i_duration = p_evt->i_duration;
1247             p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
1248             p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
1249             p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
1250             TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
1251         }
1252     }
1253     /* Update current */
1254     if( p_src->p_current )
1255         vlc_epg_SetCurrent( p_dst, p_src->p_current->i_start );
1256
1257     /* Keep only 1 old event  */
1258     if( p_dst->p_current )
1259     {
1260         while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
1261             TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
1262     }
1263 }
1264
1265 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
1266 {
1267     es_out_sys_t      *p_sys = out->p_sys;
1268     input_thread_t    *p_input = p_sys->p_input;
1269     es_out_pgrm_t     *p_pgrm;
1270     char *psz_cat;
1271
1272     /* Find program */
1273     p_pgrm = EsOutProgramFind( out, i_group );
1274     if( !p_pgrm )
1275         return;
1276
1277     /* Merge EPG */
1278     if( !p_pgrm->p_epg )
1279         p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
1280     vlc_epg_Merge( p_pgrm->p_epg, p_epg );
1281
1282     /* Update info */
1283     msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
1284
1285     psz_cat = EsOutProgramGetMetaName( p_pgrm );
1286
1287     char *psz_epg;
1288     if( asprintf( &psz_epg, "EPG %s", psz_cat ) >= 0 )
1289     {
1290         input_item_SetEpg( p_input->p->p_item, psz_epg, p_pgrm->p_epg );
1291         free( psz_epg );
1292     }
1293
1294     /* Update now playing */
1295     free( p_pgrm->psz_now_playing );
1296     p_pgrm->psz_now_playing = NULL;
1297     if( p_pgrm->p_epg->p_current && p_pgrm->p_epg->p_current->psz_name && *p_pgrm->p_epg->p_current->psz_name )
1298         p_pgrm->psz_now_playing = strdup( p_pgrm->p_epg->p_current->psz_name );
1299
1300     if( p_pgrm == p_sys->p_pgrm )
1301     {
1302         input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
1303         input_SendEventMeta( p_input );
1304     }
1305
1306     if( p_pgrm->psz_now_playing )
1307     {
1308         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1309             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
1310             p_pgrm->psz_now_playing );
1311     }
1312     else
1313     {
1314         input_Control( p_input, INPUT_DEL_INFO, psz_cat,
1315             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
1316     }
1317
1318     free( psz_cat );
1319 }
1320
1321 static void EsOutProgramUpdateScrambled( es_out_t *p_out, es_out_pgrm_t *p_pgrm )
1322 {
1323     es_out_sys_t    *p_sys = p_out->p_sys;
1324     input_thread_t  *p_input = p_sys->p_input;
1325     bool b_scrambled = false;
1326
1327     for( int i = 0; i < p_sys->i_es; i++ )
1328     {
1329         if( p_sys->es[i]->p_pgrm == p_pgrm && p_sys->es[i]->b_scrambled )
1330         {
1331             b_scrambled = true;
1332             break;
1333         }
1334     }
1335     if( !p_pgrm->b_scrambled == !b_scrambled )
1336         return;
1337
1338     p_pgrm->b_scrambled = b_scrambled;
1339     char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
1340
1341     if( b_scrambled )
1342         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Scrambled"), _("Yes") );
1343     else
1344         input_Control( p_input, INPUT_DEL_INFO, psz_cat, _("Scrambled") );
1345
1346     input_SendEventProgramScrambled( p_input, p_pgrm->i_id, b_scrambled );
1347 }
1348
1349 /* EsOutAdd:
1350  *  Add an es_out
1351  */
1352 static es_out_id_t *EsOutAdd( es_out_t *out, const es_format_t *fmt )
1353 {
1354     es_out_sys_t      *p_sys = out->p_sys;
1355     input_thread_t    *p_input = p_sys->p_input;
1356
1357     if( fmt->i_group < 0 )
1358     {
1359         msg_Err( p_input, "invalid group number" );
1360         return NULL;
1361     }
1362
1363     es_out_id_t   *es = malloc( sizeof( *es ) );
1364     es_out_pgrm_t *p_pgrm;
1365     int i;
1366
1367     if( !es )
1368         return NULL;
1369
1370     vlc_mutex_lock( &p_sys->lock );
1371
1372     /* Search the program */
1373     p_pgrm = EsOutProgramFind( out, fmt->i_group );
1374     if( !p_pgrm )
1375     {
1376         vlc_mutex_unlock( &p_sys->lock );
1377         free( es );
1378         return NULL;
1379     }
1380
1381     /* Increase ref count for program */
1382     p_pgrm->i_es++;
1383
1384     /* Set up ES */
1385     es->p_pgrm = p_pgrm;
1386     es_format_Copy( &es->fmt, fmt );
1387     if( es->fmt.i_id < 0 )
1388         es->fmt.i_id = out->p_sys->i_id;
1389     if( !es->fmt.i_original_fourcc )
1390         es->fmt.i_original_fourcc = es->fmt.i_codec;
1391     es->fmt.i_codec = vlc_fourcc_GetCodec( es->fmt.i_cat, es->fmt.i_codec );
1392
1393     es->i_id = es->fmt.i_id;
1394     es->i_meta_id = out->p_sys->i_id;
1395     es->b_scrambled = false;
1396
1397     switch( es->fmt.i_cat )
1398     {
1399     case AUDIO_ES:
1400     {
1401         audio_replay_gain_t rg;
1402
1403         es->i_channel = p_sys->i_audio;
1404
1405         memset( &rg, 0, sizeof(rg) );
1406         vlc_mutex_lock( &p_input->p->p_item->lock );
1407         vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->p_item->p_meta );
1408         vlc_mutex_unlock( &p_input->p->p_item->lock );
1409
1410         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1411         {
1412             if( !es->fmt.audio_replay_gain.pb_peak[i] )
1413             {
1414                 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
1415                 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
1416             }
1417             if( !es->fmt.audio_replay_gain.pb_gain[i] )
1418             {
1419                 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
1420                 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
1421             }
1422         }
1423         break;
1424     }
1425
1426     case VIDEO_ES:
1427         es->i_channel = p_sys->i_video;
1428         if( es->fmt.video.i_frame_rate && es->fmt.video.i_frame_rate_base )
1429             vlc_ureduce( &es->fmt.video.i_frame_rate,
1430                          &es->fmt.video.i_frame_rate_base,
1431                          es->fmt.video.i_frame_rate,
1432                          es->fmt.video.i_frame_rate_base, 0 );
1433         break;
1434
1435     case SPU_ES:
1436         es->i_channel = p_sys->i_sub;
1437         break;
1438
1439     default:
1440         es->i_channel = 0;
1441         break;
1442     }
1443     es->psz_language = LanguageGetName( es->fmt.psz_language ); /* remember so we only need to do it once */
1444     es->psz_language_code = LanguageGetCode( es->fmt.psz_language );
1445     es->p_dec = NULL;
1446     es->p_dec_record = NULL;
1447     for( i = 0; i < 4; i++ )
1448         es->pb_cc_present[i] = false;
1449     es->p_master = NULL;
1450
1451     if( es->p_pgrm == p_sys->p_pgrm )
1452         EsOutESVarUpdate( out, es, false );
1453
1454     /* Select it if needed */
1455     EsOutSelect( out, es, false );
1456
1457
1458     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1459     p_sys->i_id++;  /* always incremented */
1460     switch( es->fmt.i_cat )
1461     {
1462         case AUDIO_ES:
1463             p_sys->i_audio++;
1464             break;
1465         case SPU_ES:
1466             p_sys->i_sub++;
1467             break;
1468         case VIDEO_ES:
1469             p_sys->i_video++;
1470             break;
1471     }
1472
1473     EsOutUpdateInfo( out, es, &es->fmt, NULL );
1474
1475     if( es->b_scrambled )
1476         EsOutProgramUpdateScrambled( out, es->p_pgrm );
1477
1478     vlc_mutex_unlock( &p_sys->lock );
1479
1480     return es;
1481 }
1482
1483 static bool EsIsSelected( es_out_id_t *es )
1484 {
1485     if( es->p_master )
1486     {
1487         bool b_decode = false;
1488         if( es->p_master->p_dec )
1489         {
1490             int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1491             if( i_channel != -1 )
1492                 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1493         }
1494         return b_decode;
1495     }
1496     else
1497     {
1498         return es->p_dec != NULL;
1499     }
1500 }
1501 static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
1502 {
1503     es_out_sys_t   *p_sys = out->p_sys;
1504     input_thread_t *p_input = p_sys->p_input;
1505
1506     p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_input->p->p_sout );
1507     if( p_es->p_dec )
1508     {
1509         if( p_sys->b_buffering )
1510             input_DecoderStartBuffering( p_es->p_dec );
1511
1512         if( !p_es->p_master && p_sys->p_sout_record )
1513         {
1514             p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
1515             if( p_es->p_dec_record && p_sys->b_buffering )
1516                 input_DecoderStartBuffering( p_es->p_dec_record );
1517         }
1518     }
1519
1520     EsOutDecoderChangeDelay( out, p_es );
1521 }
1522 static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
1523 {
1524     VLC_UNUSED(out);
1525
1526     if( !p_es->p_dec )
1527         return;
1528
1529     input_DecoderDelete( p_es->p_dec );
1530     p_es->p_dec = NULL;
1531
1532     if( p_es->p_dec_record )
1533     {
1534         input_DecoderDelete( p_es->p_dec_record );
1535         p_es->p_dec_record = NULL;
1536     }
1537 }
1538
1539 static void EsSelect( es_out_t *out, es_out_id_t *es )
1540 {
1541     es_out_sys_t   *p_sys = out->p_sys;
1542     input_thread_t *p_input = p_sys->p_input;
1543
1544     if( EsIsSelected( es ) )
1545     {
1546         msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1547         return;
1548     }
1549
1550     if( es->p_master )
1551     {
1552         int i_channel;
1553         if( !es->p_master->p_dec )
1554             return;
1555
1556         i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1557         if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1558             return;
1559     }
1560     else
1561     {
1562         if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1563         {
1564             if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1565             {
1566                 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1567                          es->i_id );
1568                 return;
1569             }
1570         }
1571         else if( es->fmt.i_cat == AUDIO_ES )
1572         {
1573             if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1574             {
1575                 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1576                          es->i_id );
1577                 return;
1578             }
1579         }
1580         if( es->fmt.i_cat == SPU_ES )
1581         {
1582             if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1583             {
1584                 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1585                          es->i_id );
1586                 return;
1587             }
1588         }
1589
1590         EsCreateDecoder( out, es );
1591
1592         if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1593             return;
1594     }
1595
1596     /* Mark it as selected */
1597     input_SendEventEsSelect( p_input, es->fmt.i_cat, es->i_id );
1598     input_SendEventTeletextSelect( p_input, EsFmtIsTeletext( &es->fmt ) ? es->i_id : -1 );
1599 }
1600
1601 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1602 {
1603     es_out_sys_t   *p_sys = out->p_sys;
1604     input_thread_t *p_input = p_sys->p_input;
1605
1606     if( !EsIsSelected( es ) )
1607     {
1608         msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1609         return;
1610     }
1611
1612     if( es->p_master )
1613     {
1614         if( es->p_master->p_dec )
1615         {
1616             int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1617             if( i_channel != -1 )
1618                 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1619         }
1620     }
1621     else
1622     {
1623         const int i_spu_id = var_GetInteger( p_input, "spu-es");
1624         int i;
1625         for( i = 0; i < 4; i++ )
1626         {
1627             if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1628                 continue;
1629
1630             if( i_spu_id == es->pp_cc_es[i]->i_id )
1631             {
1632                 /* Force unselection of the CC */
1633                 input_SendEventEsSelect( p_input, SPU_ES, -1 );
1634             }
1635             EsOutDel( out, es->pp_cc_es[i] );
1636
1637             es->pb_cc_present[i] = false;
1638         }
1639         EsDestroyDecoder( out, es );
1640     }
1641
1642     if( !b_update )
1643         return;
1644
1645     /* Mark it as unselected */
1646     input_SendEventEsSelect( p_input, es->fmt.i_cat, -1 );
1647     if( EsFmtIsTeletext( &es->fmt ) )
1648         input_SendEventTeletextSelect( p_input, -1 );
1649 }
1650
1651 /**
1652  * Select an ES given the current mode
1653  * XXX: you need to take a the lock before (stream.stream_lock)
1654  *
1655  * \param out The es_out structure
1656  * \param es es_out_id structure
1657  * \param b_force ...
1658  * \return nothing
1659  */
1660 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1661 {
1662     es_out_sys_t      *p_sys = out->p_sys;
1663
1664     int i_cat = es->fmt.i_cat;
1665
1666     if( !p_sys->b_active ||
1667         ( !b_force && es->fmt.i_priority < 0 ) )
1668     {
1669         return;
1670     }
1671
1672     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1673     {
1674         if( !EsIsSelected( es ) )
1675             EsSelect( out, es );
1676     }
1677     else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1678     {
1679         vlc_value_t val;
1680         int i;
1681         var_Get( p_sys->p_input, "programs", &val );
1682         for ( i = 0; i < val.p_list->i_count; i++ )
1683         {
1684             if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1685             {
1686                 if( !EsIsSelected( es ) )
1687                     EsSelect( out, es );
1688                 break;
1689             }
1690         }
1691         var_FreeList( &val, NULL );
1692     }
1693     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1694     {
1695         int i_wanted  = -1;
1696
1697         if( es->p_pgrm != p_sys->p_pgrm )
1698             return;
1699
1700         if( i_cat == AUDIO_ES )
1701         {
1702             int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1703                                      es->psz_language_code );
1704
1705             if( p_sys->p_es_audio &&
1706                 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1707             {
1708                 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1709                                          p_sys->p_es_audio->psz_language_code );
1710
1711                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1712                     return;
1713                 i_wanted = es->i_channel;
1714             }
1715             else
1716             {
1717                 /* Select audio if (no audio selected yet)
1718                  * - no audio-language
1719                  * - no audio code for the ES
1720                  * - audio code in the requested list */
1721                 if( idx1 >= 0 ||
1722                     !strcmp( es->psz_language_code, "??" ) ||
1723                     !p_sys->ppsz_audio_language )
1724                     i_wanted = es->i_channel;
1725             }
1726
1727             if( p_sys->i_audio_last >= 0 )
1728                 i_wanted = p_sys->i_audio_last;
1729
1730             if( p_sys->i_audio_id >= 0 )
1731             {
1732                 if( es->i_id == p_sys->i_audio_id )
1733                     i_wanted = es->i_channel;
1734                 else
1735                     return;
1736             }
1737         }
1738         else if( i_cat == SPU_ES )
1739         {
1740             int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1741                                      es->psz_language_code );
1742
1743             if( p_sys->p_es_sub &&
1744                 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1745             {
1746                 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1747                                          p_sys->p_es_sub->psz_language_code );
1748
1749                 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1750                         idx1, es->psz_language_code, idx2,
1751                         p_sys->p_es_sub->psz_language_code );
1752
1753                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1754                     return;
1755                 /* We found a SPU that matches our language request */
1756                 i_wanted  = es->i_channel;
1757             }
1758             else if( idx1 >= 0 )
1759             {
1760                 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1761                         idx1, es->psz_language_code );
1762
1763                 i_wanted  = es->i_channel;
1764             }
1765             else if( p_sys->i_default_sub_id >= 0 )
1766             {
1767                 if( es->i_id == p_sys->i_default_sub_id )
1768                     i_wanted = es->i_channel;
1769             }
1770
1771             if( p_sys->i_sub_last >= 0 )
1772                 i_wanted  = p_sys->i_sub_last;
1773
1774             if( p_sys->i_sub_id >= 0 )
1775             {
1776                 if( es->i_id == p_sys->i_sub_id )
1777                     i_wanted = es->i_channel;
1778                 else
1779                     return;
1780             }
1781         }
1782         else if( i_cat == VIDEO_ES )
1783         {
1784             i_wanted  = es->i_channel;
1785         }
1786
1787         if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1788             EsSelect( out, es );
1789     }
1790
1791     /* FIXME TODO handle priority here */
1792     if( EsIsSelected( es ) )
1793     {
1794         if( i_cat == AUDIO_ES )
1795         {
1796             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1797                 p_sys->p_es_audio &&
1798                 p_sys->p_es_audio != es &&
1799                 EsIsSelected( p_sys->p_es_audio ) )
1800             {
1801                 EsUnselect( out, p_sys->p_es_audio, false );
1802             }
1803             p_sys->p_es_audio = es;
1804         }
1805         else if( i_cat == SPU_ES )
1806         {
1807             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1808                 p_sys->p_es_sub &&
1809                 p_sys->p_es_sub != es &&
1810                 EsIsSelected( p_sys->p_es_sub ) )
1811             {
1812                 EsUnselect( out, p_sys->p_es_sub, false );
1813             }
1814             p_sys->p_es_sub = es;
1815         }
1816         else if( i_cat == VIDEO_ES )
1817         {
1818             p_sys->p_es_video = es;
1819         }
1820     }
1821 }
1822
1823 /**
1824  * Send a block for the given es_out
1825  *
1826  * \param out the es_out to send from
1827  * \param es the es_out_id
1828  * \param p_block the data block to send
1829  */
1830 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1831 {
1832     es_out_sys_t   *p_sys = out->p_sys;
1833     input_thread_t *p_input = p_sys->p_input;
1834     int i_total = 0;
1835
1836     if( libvlc_stats( p_input ) )
1837     {
1838         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1839         stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1840                              p_block->i_buffer, &i_total );
1841         stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1842                            (float)i_total, NULL );
1843
1844         /* Update number of corrupted data packats */
1845         if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
1846         {
1847             stats_UpdateInteger( p_input, p_input->p->counters.p_demux_corrupted,
1848                                  1, NULL );
1849         }
1850         /* Update number of discontinuities */
1851         if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
1852         {
1853             stats_UpdateInteger( p_input, p_input->p->counters.p_demux_discontinuity,
1854                                  1, NULL );
1855         }
1856         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1857     }
1858
1859     vlc_mutex_lock( &p_sys->lock );
1860
1861     /* Mark preroll blocks */
1862     if( p_sys->i_preroll_end >= 0 )
1863     {
1864         int64_t i_date = p_block->i_pts;
1865         if( i_date <= 0 )
1866             i_date = p_block->i_dts;
1867
1868         if( i_date < p_sys->i_preroll_end )
1869             p_block->i_flags |= BLOCK_FLAG_PREROLL;
1870     }
1871
1872     p_block->i_rate = 0;
1873
1874     if( !es->p_dec )
1875     {
1876         block_Release( p_block );
1877         vlc_mutex_unlock( &p_sys->lock );
1878         return VLC_SUCCESS;
1879     }
1880
1881     /* Check for sout mode */
1882     if( out->b_sout )
1883     {
1884         /* FIXME review this, proper lock may be missing */
1885         if( p_input->p->p_sout->i_out_pace_nocontrol > 0 &&
1886             p_input->p->b_out_pace_control )
1887         {
1888             msg_Dbg( p_input, "switching to sync mode" );
1889             p_input->p->b_out_pace_control = false;
1890         }
1891         else if( p_input->p->p_sout->i_out_pace_nocontrol <= 0 &&
1892                  !p_input->p->b_out_pace_control )
1893         {
1894             msg_Dbg( p_input, "switching to async mode" );
1895             p_input->p->b_out_pace_control = true;
1896         }
1897     }
1898
1899     /* Decode */
1900     if( es->p_dec_record )
1901     {
1902         block_t *p_dup = block_Duplicate( p_block );
1903         if( p_dup )
1904             input_DecoderDecode( es->p_dec_record, p_dup,
1905                                  p_input->p->b_out_pace_control );
1906     }
1907     input_DecoderDecode( es->p_dec, p_block,
1908                          p_input->p->b_out_pace_control );
1909
1910     es_format_t fmt_dsc;
1911     vlc_meta_t  *p_meta_dsc;
1912     if( input_DecoderHasFormatChanged( es->p_dec, &fmt_dsc, &p_meta_dsc ) )
1913     {
1914         EsOutUpdateInfo( out, es, &fmt_dsc, p_meta_dsc );
1915
1916         es_format_Clean( &fmt_dsc );
1917         if( p_meta_dsc )
1918             vlc_meta_Delete( p_meta_dsc );
1919     }
1920
1921     /* Check CC status */
1922     bool pb_cc[4];
1923
1924     input_DecoderIsCcPresent( es->p_dec, pb_cc );
1925     for( int i = 0; i < 4; i++ )
1926     {
1927         es_format_t fmt;
1928
1929         if(  es->pb_cc_present[i] || !pb_cc[i] )
1930             continue;
1931         msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1932
1933         es_format_Init( &fmt, SPU_ES, EsOutFourccClosedCaptions[i] );
1934         fmt.i_group = es->fmt.i_group;
1935         if( asprintf( &fmt.psz_description,
1936                       _("Closed captions %u"), 1 + i ) == -1 )
1937             fmt.psz_description = NULL;
1938         es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1939         es->pp_cc_es[i]->p_master = es;
1940         es_format_Clean( &fmt );
1941
1942         /* */
1943         es->pb_cc_present[i] = true;
1944     }
1945
1946     vlc_mutex_unlock( &p_sys->lock );
1947
1948     return VLC_SUCCESS;
1949 }
1950
1951 /*****************************************************************************
1952  * EsOutDel:
1953  *****************************************************************************/
1954 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1955 {
1956     es_out_sys_t *p_sys = out->p_sys;
1957     bool b_reselect = false;
1958     int i;
1959
1960     vlc_mutex_lock( &p_sys->lock );
1961
1962     /* We don't try to reselect */
1963     if( es->p_dec )
1964     {
1965         while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
1966         {
1967             if( input_DecoderIsEmpty( es->p_dec ) &&
1968                 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
1969                 break;
1970             /* FIXME there should be a way to have auto deleted es, but there will be
1971              * a problem when another codec of the same type is created (mainly video) */
1972             msleep( 20*1000 );
1973         }
1974         EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1975     }
1976
1977     if( es->p_pgrm == p_sys->p_pgrm )
1978         EsOutESVarUpdate( out, es, true );
1979
1980     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1981
1982     /* Update program */
1983     es->p_pgrm->i_es--;
1984     if( es->p_pgrm->i_es == 0 )
1985         msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1986
1987     if( es->b_scrambled )
1988         EsOutProgramUpdateScrambled( out, es->p_pgrm );
1989
1990     /* */
1991     if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1992         p_sys->p_es_sub == es ) b_reselect = true;
1993
1994     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1995     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1996     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
1997
1998     switch( es->fmt.i_cat )
1999     {
2000         case AUDIO_ES:
2001             p_sys->i_audio--;
2002             break;
2003         case SPU_ES:
2004             p_sys->i_sub--;
2005             break;
2006         case VIDEO_ES:
2007             p_sys->i_video--;
2008             break;
2009     }
2010
2011     /* Re-select another track when needed */
2012     if( b_reselect )
2013     {
2014         for( i = 0; i < p_sys->i_es; i++ )
2015         {
2016             if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
2017                 EsOutSelect( out, p_sys->es[i], false );
2018         }
2019     }
2020
2021     free( es->psz_language );
2022     free( es->psz_language_code );
2023
2024     es_format_Clean( &es->fmt );
2025
2026     vlc_mutex_unlock( &p_sys->lock );
2027
2028     free( es );
2029 }
2030
2031 /**
2032  * Control query handler
2033  *
2034  * \param out the es_out to control
2035  * \param i_query A es_out query as defined in include/ninput.h
2036  * \param args a variable list of arguments for the query
2037  * \return VLC_SUCCESS or an error code
2038  */
2039 static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
2040 {
2041     es_out_sys_t *p_sys = out->p_sys;
2042     bool  b, *pb;
2043     int         i;
2044
2045     es_out_id_t *es;
2046
2047     switch( i_query )
2048     {
2049         case ES_OUT_SET_ES_STATE:
2050             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2051             b = (bool) va_arg( args, int );
2052             if( b && !EsIsSelected( es ) )
2053             {
2054                 EsSelect( out, es );
2055                 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
2056             }
2057             else if( !b && EsIsSelected( es ) )
2058             {
2059                 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
2060                 return VLC_SUCCESS;
2061             }
2062             return VLC_SUCCESS;
2063
2064         case ES_OUT_GET_ES_STATE:
2065             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2066             pb = (bool*) va_arg( args, bool * );
2067
2068             *pb = EsIsSelected( es );
2069             return VLC_SUCCESS;
2070
2071         case ES_OUT_SET_ACTIVE:
2072         {
2073             b = (bool) va_arg( args, int );
2074             if( b && !p_sys->b_active && p_sys->i_es > 0 )
2075             {
2076                 /* XXX Terminate vout if there are tracks but no video one.
2077                  * This one is not mandatory but is he earliest place where it
2078                  * can be done */
2079                 for( i = 0; i < p_sys->i_es; i++ )
2080                 {
2081                     es_out_id_t *p_es = p_sys->es[i];
2082                     if( p_es->fmt.i_cat == VIDEO_ES )
2083                         break;
2084                 }
2085                 if( i >= p_sys->i_es )
2086                     input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2087             }
2088             p_sys->b_active = b;
2089             return VLC_SUCCESS;
2090         }
2091
2092         case ES_OUT_SET_MODE:
2093             i = (int) va_arg( args, int );
2094             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
2095                 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
2096             {
2097                 p_sys->i_mode = i;
2098
2099                 /* Reapply policy mode */
2100                 for( i = 0; i < p_sys->i_es; i++ )
2101                 {
2102                     if( EsIsSelected( p_sys->es[i] ) )
2103                     {
2104                         EsUnselect( out, p_sys->es[i],
2105                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2106                     }
2107                 }
2108                 for( i = 0; i < p_sys->i_es; i++ )
2109                 {
2110                     EsOutSelect( out, p_sys->es[i], false );
2111                 }
2112                 return VLC_SUCCESS;
2113             }
2114             return VLC_EGENERIC;
2115
2116         case ES_OUT_SET_ES:
2117         case ES_OUT_RESTART_ES:
2118         {
2119             int i_cat;
2120
2121             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2122
2123             if( es == NULL )
2124                 i_cat = UNKNOWN_ES;
2125             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2126                 i_cat = AUDIO_ES;
2127             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2128                 i_cat = VIDEO_ES;
2129             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2130                 i_cat = SPU_ES;
2131             else
2132                 i_cat = -1;
2133
2134             for( i = 0; i < p_sys->i_es; i++ )
2135             {
2136                 if( i_cat == -1 )
2137                 {
2138                     if( es == p_sys->es[i] )
2139                     {
2140                         EsOutSelect( out, es, true );
2141                         break;
2142                     }
2143                 }
2144                 else
2145                 {
2146                     if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
2147                     {
2148                         if( EsIsSelected( p_sys->es[i] ) )
2149                         {
2150                             if( i_query == ES_OUT_RESTART_ES )
2151                             {
2152                                 if( p_sys->es[i]->p_dec )
2153                                 {
2154                                     EsDestroyDecoder( out, p_sys->es[i] );
2155                                     EsCreateDecoder( out, p_sys->es[i] );
2156                                 }
2157                             }
2158                             else
2159                             {
2160                                 EsUnselect( out, p_sys->es[i],
2161                                             p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
2162                             }
2163                         }
2164                     }
2165                 }
2166             }
2167             return VLC_SUCCESS;
2168         }
2169  
2170         case ES_OUT_SET_ES_DEFAULT:
2171         {
2172             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2173
2174             if( es == NULL )
2175             {
2176                 /*p_sys->i_default_video_id = -1;*/
2177                 /*p_sys->i_default_audio_id = -1;*/
2178                 p_sys->i_default_sub_id = -1;
2179             }
2180             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
2181             {
2182                 /*p_sys->i_default_video_id = -1;*/
2183             }
2184             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
2185             {
2186                 /*p_sys->i_default_audio_id = -1;*/
2187             }
2188             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
2189             {
2190                 p_sys->i_default_sub_id = -1;
2191             }
2192             else
2193             {
2194                 /*if( es->fmt.i_cat == VIDEO_ES )
2195                     p_sys->i_default_video_id = es->i_id;
2196                 else
2197                 if( es->fmt.i_cat == AUDIO_ES )
2198                     p_sys->i_default_audio_id = es->i_id;
2199                 else*/
2200                 if( es->fmt.i_cat == SPU_ES )
2201                     p_sys->i_default_sub_id = es->i_id;
2202             }
2203             return VLC_SUCCESS;
2204         }
2205
2206         case ES_OUT_SET_PCR:
2207         case ES_OUT_SET_GROUP_PCR:
2208         {
2209             es_out_pgrm_t *p_pgrm = NULL;
2210             int            i_group = 0;
2211             int64_t        i_pcr;
2212
2213             /* Search program */
2214             if( i_query == ES_OUT_SET_PCR )
2215             {
2216                 p_pgrm = p_sys->p_pgrm;
2217                 if( !p_pgrm )
2218                     p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
2219             }
2220             else
2221             {
2222                 i_group = (int)va_arg( args, int );
2223                 p_pgrm = EsOutProgramFind( out, i_group );
2224             }
2225             if( !p_pgrm )
2226                 return VLC_EGENERIC;
2227
2228             i_pcr = (int64_t)va_arg( args, int64_t );
2229             if( i_pcr <= VLC_TS_INVALID )
2230             {
2231                 msg_Err( p_sys->p_input, "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" );
2232                 return VLC_EGENERIC;
2233             }
2234
2235             /* TODO do not use mdate() but proper stream acquisition date */
2236             bool b_late;
2237             input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
2238                                 &b_late,
2239                                 p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering, i_pcr, mdate() );
2240
2241             if( p_pgrm == p_sys->p_pgrm )
2242             {
2243                 if( p_sys->b_buffering )
2244                 {
2245                     /* Check buffering state on master clock update */
2246                     EsOutDecodersStopBuffering( out, false );
2247                 }
2248                 else if( b_late )
2249                 {
2250                     mtime_t i_pts_delay = input_clock_GetJitter( p_pgrm->p_clock );
2251
2252                     /* Avoid dangerously high value */
2253                     const mtime_t i_pts_delay_max = 30000000;
2254                     if( i_pts_delay > i_pts_delay_max )
2255                         i_pts_delay = __MAX( i_pts_delay_max, p_sys->i_pts_delay );
2256
2257                     /* Force a rebufferization when we are too late */
2258                     msg_Err( p_sys->p_input,
2259                              "ES_OUT_SET_(GROUP_)PCR  is called too late, increasing pts_delay to %d ms",
2260                              (int)(i_pts_delay/1000) );
2261
2262                     /* It is not really good, as we throw away already buffered data
2263                      * TODO have a mean to correctly reenter bufferization */
2264                     es_out_Control( out, ES_OUT_RESET_PCR );
2265
2266                     es_out_Control( out, ES_OUT_SET_JITTER, i_pts_delay, p_sys->i_cr_average );
2267                 }
2268             }
2269             return VLC_SUCCESS;
2270         }
2271
2272         case ES_OUT_RESET_PCR:
2273             msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
2274             EsOutChangePosition( out );
2275             return VLC_SUCCESS;
2276
2277         case ES_OUT_SET_GROUP:
2278         {
2279             int j;
2280             i = (int) va_arg( args, int );
2281             for( j = 0; j < p_sys->i_pgrm; j++ )
2282             {
2283                 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
2284                 if( p_pgrm->i_id == i )
2285                 {
2286                     EsOutProgramSelect( out, p_pgrm );
2287                     return VLC_SUCCESS;
2288                 }
2289             }
2290             return VLC_EGENERIC;
2291         }
2292
2293         case ES_OUT_SET_ES_FMT:
2294         {
2295             /* This ain't pretty but is need by some demuxers (eg. Ogg )
2296              * to update the p_extra data */
2297             es_format_t *p_fmt;
2298             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2299             p_fmt = (es_format_t*) va_arg( args, es_format_t * );
2300             if( es == NULL )
2301                 return VLC_EGENERIC;
2302
2303             if( p_fmt->i_extra )
2304             {
2305                 es->fmt.i_extra = p_fmt->i_extra;
2306                 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
2307                 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
2308
2309                 if( !es->p_dec )
2310                     return VLC_SUCCESS;
2311 #if 1
2312                 EsDestroyDecoder( out, es );
2313
2314                 EsCreateDecoder( out, es );
2315 #else
2316                 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
2317                 es->p_dec->fmt_in.p_extra =
2318                     realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
2319                 memcpy( es->p_dec->fmt_in.p_extra,
2320                         p_fmt->p_extra, p_fmt->i_extra );
2321 #endif
2322             }
2323
2324             return VLC_SUCCESS;
2325         }
2326
2327         case ES_OUT_SET_ES_SCRAMBLED_STATE:
2328         {
2329             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
2330             bool b_scrambled = (bool)va_arg( args, int );
2331
2332             if( !es->b_scrambled != !b_scrambled )
2333             {
2334                 es->b_scrambled = b_scrambled;
2335                 EsOutProgramUpdateScrambled( out, es->p_pgrm );
2336             }
2337             return VLC_SUCCESS;
2338         }
2339
2340         case ES_OUT_SET_NEXT_DISPLAY_TIME:
2341         {
2342             const int64_t i_date = (int64_t)va_arg( args, int64_t );
2343
2344             if( i_date < 0 )
2345                 return VLC_EGENERIC;
2346
2347             p_sys->i_preroll_end = i_date;
2348
2349             return VLC_SUCCESS;
2350         }
2351         case ES_OUT_SET_GROUP_META:
2352         {
2353             int i_group = (int)va_arg( args, int );
2354             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
2355
2356             EsOutProgramMeta( out, i_group, p_meta );
2357             return VLC_SUCCESS;
2358         }
2359         case ES_OUT_SET_GROUP_EPG:
2360         {
2361             int i_group = (int)va_arg( args, int );
2362             vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
2363
2364             EsOutProgramEpg( out, i_group, p_epg );
2365             return VLC_SUCCESS;
2366         }
2367
2368         case ES_OUT_DEL_GROUP:
2369         {
2370             int i_group = (int)va_arg( args, int );
2371
2372             return EsOutProgramDel( out, i_group );
2373         }
2374
2375         case ES_OUT_GET_WAKE_UP:
2376         {
2377             mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
2378             *pi_wakeup = EsOutGetWakeup( out );
2379             return VLC_SUCCESS;
2380         }
2381
2382         case ES_OUT_SET_ES_BY_ID:
2383         case ES_OUT_RESTART_ES_BY_ID:
2384         case ES_OUT_SET_ES_DEFAULT_BY_ID:
2385         {
2386             const int i_id = (int)va_arg( args, int );
2387             es_out_id_t *p_es = EsOutGetFromID( out, i_id );
2388             int i_new_query;
2389
2390             switch( i_query )
2391             {
2392             case ES_OUT_SET_ES_BY_ID:         i_new_query = ES_OUT_SET_ES; break;
2393             case ES_OUT_RESTART_ES_BY_ID:     i_new_query = ES_OUT_RESTART_ES; break;
2394             case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
2395             default:
2396               assert(0);
2397             }
2398             /* TODO if the lock is made non recursive it should be changed */
2399             int i_ret = es_out_Control( out, i_new_query, p_es );
2400
2401             /* Clean up vout after user action (in active mode only).
2402              * FIXME it does not work well with multiple video windows */
2403             if( p_sys->b_active )
2404                 input_resource_TerminateVout( p_sys->p_input->p->p_resource );
2405             return i_ret;
2406         }
2407
2408         case ES_OUT_GET_BUFFERING:
2409             pb = (bool *)va_arg( args, bool* );
2410             *pb = p_sys->b_buffering;
2411             return VLC_SUCCESS;
2412
2413         case ES_OUT_GET_EMPTY:
2414             pb = (bool *)va_arg( args, bool* );
2415             *pb = EsOutDecodersIsEmpty( out );
2416             return VLC_SUCCESS;
2417
2418         case ES_OUT_SET_DELAY:
2419         {
2420             const int i_cat = (int)va_arg( args, int );
2421             const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
2422             EsOutSetDelay( out, i_cat, i_delay );
2423             return VLC_SUCCESS;
2424         }
2425
2426         case ES_OUT_SET_RECORD_STATE:
2427             b = (bool) va_arg( args, int );
2428             return EsOutSetRecord( out, b );
2429
2430         case ES_OUT_SET_PAUSE_STATE:
2431         {
2432             const bool b_source_paused = (bool)va_arg( args, int );
2433             const bool b_paused = (bool)va_arg( args, int );
2434             const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
2435
2436             assert( !b_source_paused == !b_paused );
2437             EsOutChangePause( out, b_paused, i_date );
2438
2439             return VLC_SUCCESS;
2440         }
2441
2442         case ES_OUT_SET_RATE:
2443         {
2444             const int i_src_rate = (int)va_arg( args, int );
2445             const int i_rate = (int)va_arg( args, int );
2446
2447             assert( i_src_rate == i_rate );
2448             EsOutChangeRate( out, i_rate );
2449
2450             return VLC_SUCCESS;
2451         }
2452
2453         case ES_OUT_SET_TIME:
2454         {
2455             const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
2456
2457             assert( i_date == -1 );
2458             EsOutChangePosition( out );
2459
2460             return VLC_SUCCESS;
2461         }
2462
2463         case ES_OUT_SET_FRAME_NEXT:
2464             EsOutFrameNext( out );
2465             return VLC_SUCCESS;
2466
2467         case ES_OUT_SET_TIMES:
2468         {
2469             double f_position = (double)va_arg( args, double );
2470             mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
2471             mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
2472
2473             input_SendEventLength( p_sys->p_input, i_length );
2474
2475             if( !p_sys->b_buffering )
2476             {
2477                 /* Fix for buffering delay */
2478                 const mtime_t i_delay = EsOutGetBuffering( out );
2479
2480                 i_time -= i_delay;
2481                 if( i_time < 0 )
2482                     i_time = 0;
2483
2484                 if( i_length > 0 )
2485                     f_position -= (double)i_delay / i_length;
2486                 if( f_position < 0 )
2487                     f_position = 0;
2488
2489                 input_SendEventPosition( p_sys->p_input, f_position, i_time );
2490             }
2491             return VLC_SUCCESS;
2492         }
2493         case ES_OUT_SET_JITTER:
2494         {
2495             mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
2496             int     i_cr_average = (int)va_arg( args, int );
2497
2498             if( i_pts_delay == p_sys->i_pts_delay &&
2499                 i_cr_average == p_sys->i_cr_average )
2500                 return VLC_SUCCESS;
2501
2502             p_sys->i_pts_delay = i_pts_delay;
2503             p_sys->i_cr_average = i_cr_average;
2504
2505             for( int i = 0; i < p_sys->i_pgrm; i++ )
2506                 input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
2507                                        i_pts_delay, i_cr_average );
2508             return VLC_SUCCESS;
2509         }
2510
2511         default:
2512             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
2513             return VLC_EGENERIC;
2514     }
2515 }
2516 static int EsOutControl( es_out_t *out, int i_query, va_list args )
2517 {
2518     es_out_sys_t *p_sys = out->p_sys;
2519     int i_ret;
2520
2521     vlc_mutex_lock( &p_sys->lock );
2522     i_ret = EsOutControlLocked( out, i_query, args );
2523     vlc_mutex_unlock( &p_sys->lock );
2524
2525     return i_ret;
2526 }
2527
2528 /****************************************************************************
2529  * LanguageGetName: try to expend iso639 into plain name
2530  ****************************************************************************/
2531 static char *LanguageGetName( const char *psz_code )
2532 {
2533     const iso639_lang_t *pl;
2534
2535     if( psz_code == NULL )
2536     {
2537         return strdup( "" );
2538     }
2539
2540     if( strlen( psz_code ) == 2 )
2541     {
2542         pl = GetLang_1( psz_code );
2543     }
2544     else if( strlen( psz_code ) == 3 )
2545     {
2546         pl = GetLang_2B( psz_code );
2547         if( !strcmp( pl->psz_iso639_1, "??" ) )
2548         {
2549             pl = GetLang_2T( psz_code );
2550         }
2551     }
2552     else
2553     {
2554         return strdup( psz_code );
2555     }
2556
2557     if( !strcmp( pl->psz_iso639_1, "??" ) )
2558     {
2559        return strdup( psz_code );
2560     }
2561     else
2562     {
2563         if( *pl->psz_native_name )
2564         {
2565             return strdup( pl->psz_native_name );
2566         }
2567         return strdup( pl->psz_eng_name );
2568     }
2569 }
2570
2571 /* Get a 2 char code */
2572 static char *LanguageGetCode( const char *psz_lang )
2573 {
2574     const iso639_lang_t *pl;
2575
2576     if( psz_lang == NULL || *psz_lang == '\0' )
2577         return strdup("??");
2578
2579     for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
2580     {
2581         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
2582             !strcasecmp( pl->psz_native_name, psz_lang ) ||
2583             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
2584             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
2585             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
2586             break;
2587     }
2588
2589     if( pl->psz_eng_name != NULL )
2590         return strdup( pl->psz_iso639_1 );
2591
2592     return strdup("??");
2593 }
2594
2595 static char **LanguageSplit( const char *psz_langs )
2596 {
2597     char *psz_dup;
2598     char *psz_parser;
2599     char **ppsz = NULL;
2600     int i_psz = 0;
2601
2602     if( psz_langs == NULL ) return NULL;
2603
2604     psz_parser = psz_dup = strdup(psz_langs);
2605
2606     while( psz_parser && *psz_parser )
2607     {
2608         char *psz;
2609         char *psz_code;
2610
2611         psz = strchr(psz_parser, ',' );
2612         if( psz ) *psz++ = '\0';
2613
2614         if( !strcmp( psz_parser, "any" ) )
2615         {
2616             TAB_APPEND( i_psz, ppsz, strdup("any") );
2617         }
2618         else
2619         {
2620             psz_code = LanguageGetCode( psz_parser );
2621             if( strcmp( psz_code, "??" ) )
2622             {
2623                 TAB_APPEND( i_psz, ppsz, psz_code );
2624             }
2625             else
2626             {
2627                 free( psz_code );
2628             }
2629         }
2630
2631         psz_parser = psz;
2632     }
2633
2634     if( i_psz )
2635     {
2636         TAB_APPEND( i_psz, ppsz, NULL );
2637     }
2638
2639     free( psz_dup );
2640     return ppsz;
2641 }
2642
2643 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2644 {
2645     int i;
2646
2647     if( !ppsz_langs || !psz_lang ) return -1;
2648
2649     for( i = 0; ppsz_langs[i]; i++ )
2650     {
2651         if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2652             !strcasecmp( ppsz_langs[i], "any" ) )
2653         {
2654             return i;
2655         }
2656     }
2657
2658     return -1;
2659 }
2660
2661 /****************************************************************************
2662  * EsOutUpdateInfo:
2663  * - add meta info to the playlist item
2664  ****************************************************************************/
2665 static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *fmt, const vlc_meta_t *p_meta )
2666 {
2667     es_out_sys_t   *p_sys = out->p_sys;
2668     input_thread_t *p_input = p_sys->p_input;
2669     const es_format_t *p_fmt_es = &es->fmt;
2670     char           *psz_cat;
2671     lldiv_t         div;
2672
2673     /* Create category name */
2674     if( asprintf( &psz_cat, _("Stream %d"), es->i_meta_id ) == -1 )
2675         return;
2676
2677     /* Remove previous information */
2678     input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
2679
2680     /* Add informations */
2681     const char *psz_type;
2682     switch( fmt->i_cat )
2683     {
2684     case AUDIO_ES:
2685         psz_type = _("Audio");
2686         break;
2687     case VIDEO_ES:
2688         psz_type = _("Video");
2689         break;
2690     case SPU_ES:
2691         psz_type = _("Subtitle");
2692         break;
2693     default:
2694         psz_type = NULL;
2695         break;
2696     }
2697
2698     if( psz_type )
2699         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Type"), psz_type );
2700
2701     if( es->i_meta_id != es->i_id )
2702         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Original ID"),
2703                        "%d", es->i_id );
2704
2705     const char *psz_codec_description =
2706         vlc_fourcc_GetDescription( p_fmt_es->i_cat, p_fmt_es->i_codec );
2707     const vlc_fourcc_t i_codec_fourcc = p_fmt_es->i_original_fourcc ?: p_fmt_es->i_codec;
2708     if( psz_codec_description )
2709         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2710                        "%s (%.4s)", psz_codec_description, (char*)&i_codec_fourcc );
2711     else
2712         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2713                        "%.4s", (char*)&i_codec_fourcc );
2714
2715     if( es->psz_language && *es->psz_language )
2716         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2717                        "%s", es->psz_language );
2718     if( fmt->psz_description && *fmt->psz_description )
2719         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Description"),
2720                        "%s", fmt->psz_description );
2721
2722     switch( fmt->i_cat )
2723     {
2724     case AUDIO_ES:
2725         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2726                        _("Type"), _("Audio") );
2727
2728         if( fmt->audio.i_physical_channels & AOUT_CHAN_PHYSMASK )
2729             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2730                            "%s", _( aout_FormatPrintChannels( &fmt->audio ) ) );
2731         else if( fmt->audio.i_channels > 0 )
2732             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2733                            "%u", fmt->audio.i_channels );
2734
2735         if( fmt->audio.i_rate > 0 )
2736         {
2737             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2738                            _("%u Hz"), fmt->audio.i_rate );
2739             /* FIXME that should be removed or improved ! (used by text/strings.c) */
2740             var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2741         }
2742
2743         unsigned int i_bitspersample = fmt->audio.i_bitspersample;
2744         if( i_bitspersample <= 0 )
2745             i_bitspersample = aout_BitsPerSample( p_fmt_es->i_codec );
2746         if( i_bitspersample > 0 )
2747             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2748                            _("Bits per sample"), "%u",
2749                            i_bitspersample );
2750
2751         if( fmt->i_bitrate > 0 )
2752         {
2753             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2754                            _("%u kb/s"), fmt->i_bitrate / 1000 );
2755             /* FIXME that should be removed or improved ! (used by text/strings.c) */
2756             var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2757         }
2758         for( int i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
2759         {
2760             const audio_replay_gain_t *p_rg = &fmt->audio_replay_gain;
2761             if( !p_rg->pb_gain[i] )
2762                 continue;
2763             const char *psz_name;
2764             if( i == AUDIO_REPLAY_GAIN_TRACK )
2765                 psz_name = _("Track replay gain");
2766             else
2767                 psz_name = _("Album replay gain");
2768             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2769                            psz_name, _("%.2f dB"), p_rg->pf_gain[i] );
2770         }
2771         break;
2772
2773     case VIDEO_ES:
2774         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2775                        _("Type"), _("Video") );
2776
2777         if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2778             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2779                            _("Resolution"), "%ux%u",
2780                            fmt->video.i_width, fmt->video.i_height );
2781
2782         if( fmt->video.i_visible_width > 0 &&
2783             fmt->video.i_visible_height > 0 )
2784             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2785                            _("Display resolution"), "%ux%u",
2786                            fmt->video.i_visible_width,
2787                            fmt->video.i_visible_height);
2788        if( fmt->video.i_frame_rate > 0 &&
2789            fmt->video.i_frame_rate_base > 0 )
2790        {
2791            div = lldiv( (float)fmt->video.i_frame_rate /
2792                                fmt->video.i_frame_rate_base * 1000000,
2793                                1000000 );
2794            if( div.rem > 0 )
2795                input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2796                               _("Frame rate"), "%"PRId64".%06u",
2797                               div.quot, (unsigned int )div.rem );
2798            else
2799                input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2800                               _("Frame rate"), "%"PRId64, div.quot );
2801        }
2802        break;
2803
2804     case SPU_ES:
2805         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2806                        _("Type"), _("Subtitle") );
2807         break;
2808
2809     default:
2810         break;
2811     }
2812
2813     /* Append generic meta */
2814     if( p_meta )
2815     {
2816         char **ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2817         for( int i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
2818         {
2819             char *psz_key = ppsz_all_keys[i];
2820             char *psz_value = vlc_dictionary_value_for_key( &p_meta->extra_tags, psz_key );
2821
2822             if( psz_value )
2823                 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2824                                vlc_gettext(psz_key), vlc_gettext(psz_value) );
2825             free( psz_key );
2826         }
2827         free( ppsz_all_keys );
2828     }
2829
2830     free( psz_cat );
2831 }