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