]> git.sesse.net Git - vlc/blob - src/input/es_out.c
Check asprintf return value and fix a potential memleak.
[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 <vlc_common.h>
33
34 #include <stdio.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
43 #include <vlc_iso_lang.h>
44 /* FIXME we should find a better way than including that */
45 #include "../text/iso-639_def.h"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 typedef struct
51 {
52     /* Program ID */
53     int i_id;
54
55     /* Number of es for this pgrm */
56     int i_es;
57
58     bool b_selected;
59
60     /* Clock for this program */
61     input_clock_t clock;
62
63     char    *psz_name;
64     char    *psz_now_playing;
65     char    *psz_publisher;
66
67     vlc_epg_t *p_epg;
68 } es_out_pgrm_t;
69
70 struct es_out_id_t
71 {
72     /* ES ID */
73     int       i_id;
74     es_out_pgrm_t *p_pgrm;
75
76     /* Misc. */
77     int64_t i_preroll_end;
78
79     /* Channel in the track type */
80     int         i_channel;
81     es_format_t fmt;
82     char        *psz_language;
83     char        *psz_language_code;
84
85     decoder_t   *p_dec;
86
87     /* Fields for Video with CC */
88     bool  pb_cc_present[4];
89     es_out_id_t  *pp_cc_es[4];
90
91     /* Field for CC track from a master video */
92     es_out_id_t *p_master;
93 };
94
95 struct es_out_sys_t
96 {
97     input_thread_t *p_input;
98
99     /* all programs */
100     int           i_pgrm;
101     es_out_pgrm_t **pgrm;
102     es_out_pgrm_t **pp_selected_pgrm; /* --programs */
103     es_out_pgrm_t *p_pgrm;  /* Master program */
104
105     /* all es */
106     int         i_id;
107     int         i_es;
108     es_out_id_t **es;
109
110     /* mode gestion */
111     bool  b_active;
112     int         i_mode;
113
114     /* es count */
115     int         i_audio;
116     int         i_video;
117     int         i_sub;
118
119     /* es to select */
120     int         i_audio_last, i_audio_id;
121     int         i_sub_last, i_sub_id;
122     int         i_default_sub_id;   /* As specified in container; if applicable */
123     char        **ppsz_audio_language;
124     char        **ppsz_sub_language;
125
126     /* current main es */
127     es_out_id_t *p_es_audio;
128     es_out_id_t *p_es_video;
129     es_out_id_t *p_es_sub;
130
131     /* delay */
132     int64_t i_audio_delay;
133     int64_t i_spu_delay;
134
135     /* Rate used to rescale ES ts */
136     int         i_rate;
137 };
138
139 static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
140 static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
141 static void         EsOutDel    ( es_out_t *, es_out_id_t * );
142 static void         EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force );
143 static int          EsOutControl( es_out_t *, int i_query, va_list );
144
145 static void         EsOutAddInfo( es_out_t *, es_out_id_t *es );
146
147 static bool EsIsSelected( es_out_id_t *es );
148 static void EsSelect( es_out_t *out, es_out_id_t *es );
149 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update );
150 static char *LanguageGetName( const char *psz_code );
151 static char *LanguageGetCode( const char *psz_lang );
152 static char **LanguageSplit( const char *psz_langs );
153 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
154
155 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
156
157 static const vlc_fourcc_t EsOutFourccClosedCaptions[4] = {
158     VLC_FOURCC('c', 'c', '1', ' '),
159     VLC_FOURCC('c', 'c', '2', ' '),
160     VLC_FOURCC('c', 'c', '3', ' '),
161     VLC_FOURCC('c', 'c', '4', ' '),
162 };
163 static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
164 {
165     int i;
166     for( i = 0; i < 4; i++ )
167     {
168         if( fcc == EsOutFourccClosedCaptions[i] )
169             return i;
170     }
171     return -1;
172 }
173
174
175 /*****************************************************************************
176  * input_EsOutNew:
177  *****************************************************************************/
178 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
179 {
180     vlc_value_t  val;
181     int i;
182
183     es_out_t     *out = malloc( sizeof( es_out_t ) );
184     if( !out ) return NULL;
185
186     es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
187     if( !p_sys )
188     {
189         free( out );
190         return NULL;
191     }
192
193     out->pf_add     = EsOutAdd;
194     out->pf_send    = EsOutSend;
195     out->pf_del     = EsOutDel;
196     out->pf_control = EsOutControl;
197     out->p_sys      = p_sys;
198     out->b_sout     = p_input->p->p_sout != NULL;
199
200     p_sys->p_input = p_input;
201
202     p_sys->b_active = false;
203     p_sys->i_mode   = ES_OUT_MODE_AUTO;
204
205
206     TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
207     p_sys->p_pgrm   = NULL;
208
209     p_sys->i_id    = 0;
210
211     TAB_INIT( p_sys->i_es, p_sys->es );
212
213     p_sys->i_audio = 0;
214     p_sys->i_video = 0;
215     p_sys->i_sub   = 0;
216
217     /* */
218     var_Get( p_input, "audio-track", &val );
219     p_sys->i_audio_last = val.i_int;
220
221     var_Get( p_input, "sub-track", &val );
222     p_sys->i_sub_last = val.i_int;
223
224     p_sys->i_default_sub_id   = -1;
225
226     if( !p_input->b_preparsing )
227     {
228         var_Get( p_input, "audio-language", &val );
229         p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
230         if( p_sys->ppsz_audio_language )
231         {
232             for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
233                 msg_Dbg( p_input, "selected audio language[%d] %s",
234                          i, p_sys->ppsz_audio_language[i] );
235         }
236         free( val.psz_string );
237
238         var_Get( p_input, "sub-language", &val );
239         p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
240         if( p_sys->ppsz_sub_language )
241         {
242             for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
243                 msg_Dbg( p_input, "selected subtitle language[%d] %s",
244                          i, p_sys->ppsz_sub_language[i] );
245         }
246         free( val.psz_string );
247     }
248     else
249     {
250         p_sys->ppsz_sub_language = NULL;
251         p_sys->ppsz_audio_language = NULL;
252     }
253
254     var_Get( p_input, "audio-track-id", &val );
255     p_sys->i_audio_id = val.i_int;
256
257     var_Get( p_input, "sub-track-id", &val );
258     p_sys->i_sub_id = val.i_int;
259
260     p_sys->p_es_audio = NULL;
261     p_sys->p_es_video = NULL;
262     p_sys->p_es_sub   = NULL;
263
264     p_sys->i_audio_delay= 0;
265     p_sys->i_spu_delay  = 0;
266
267     p_sys->i_rate = i_rate;
268
269     return out;
270 }
271
272 /*****************************************************************************
273  * input_EsOutDelete:
274  *****************************************************************************/
275 void input_EsOutDelete( es_out_t *out )
276 {
277     es_out_sys_t *p_sys = out->p_sys;
278     int i;
279
280     for( i = 0; i < p_sys->i_es; i++ )
281     {
282         if( p_sys->es[i]->p_dec )
283         {
284             input_DecoderDelete( p_sys->es[i]->p_dec );
285         }
286         free( p_sys->es[i]->psz_language );
287         free( p_sys->es[i]->psz_language_code );
288         es_format_Clean( &p_sys->es[i]->fmt );
289
290         free( p_sys->es[i] );
291     }
292     if( p_sys->ppsz_audio_language )
293     {
294         for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
295             free( p_sys->ppsz_audio_language[i] );
296         free( p_sys->ppsz_audio_language );
297     }
298     if( p_sys->ppsz_sub_language )
299     {
300         for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
301             free( p_sys->ppsz_sub_language[i] );
302         free( p_sys->ppsz_sub_language );
303     }
304
305     free( p_sys->es );
306
307     /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
308     for( i = 0; i < p_sys->i_pgrm; i++ )
309     {
310         es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
311         free( p_pgrm->psz_now_playing );
312         free( p_pgrm->psz_publisher );
313         free( p_pgrm->psz_name );
314         if( p_pgrm->p_epg )
315             vlc_epg_Delete( p_pgrm->p_epg );
316
317         free( p_pgrm );
318     }
319     TAB_CLEAN( p_sys->i_pgrm, p_sys->pgrm );
320
321     free( p_sys );
322     free( out );
323 }
324
325 es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
326 {
327     int i;
328     if( i_id < 0 )
329     {
330         /* Special HACK, -i_id is tha cat of the stream */
331         return (es_out_id_t*)((uint8_t*)NULL-i_id);
332     }
333
334     for( i = 0; i < out->p_sys->i_es; i++ )
335     {
336         if( out->p_sys->es[i]->i_id == i_id )
337             return out->p_sys->es[i];
338     }
339     return NULL;
340 }
341
342 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
343 {
344     es_out_sys_t      *p_sys = out->p_sys;
345     int i;
346
347     for( i = 0; i < p_sys->i_es; i++ )
348     {
349         es_out_id_t *es = p_sys->es[i];
350
351         /* Send a dummy block to let decoder know that
352          * there is a discontinuity */
353         if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
354             input_DecoderDiscontinuity( es->p_dec, b_flush );
355     }
356 }
357 void input_EsOutChangeRate( es_out_t *out, int i_rate )
358 {
359     es_out_sys_t      *p_sys = out->p_sys;
360     int i;
361
362     p_sys->i_rate = i_rate;
363     EsOutDiscontinuity( out, false, false );
364
365     for( i = 0; i < p_sys->i_pgrm; i++ )
366         input_ClockSetRate( &p_sys->pgrm[i]->clock, i_rate );
367 }
368
369 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
370 {
371     es_out_sys_t *p_sys = out->p_sys;
372
373     if( i_cat == AUDIO_ES )
374         p_sys->i_audio_delay = i_delay;
375     else if( i_cat == SPU_ES )
376         p_sys->i_spu_delay = i_delay;
377 }
378 void input_EsOutChangeState( es_out_t *out )
379 {
380     es_out_sys_t *p_sys = out->p_sys;
381     input_thread_t *p_input = p_sys->p_input;
382
383     if( p_input->i_state  == PAUSE_S )
384     {
385         /* Send discontinuity to decoders (it will allow them to flush
386          *                  * if implemented */
387         EsOutDiscontinuity( out, false, false );
388     }
389     else
390     {
391         /* Out of pause, reset pcr */
392         es_out_Control( out, ES_OUT_RESET_PCR );
393     }
394 }
395 void input_EsOutChangePosition( es_out_t *out )
396 {
397     //es_out_sys_t *p_sys = out->p_sys;
398
399     es_out_Control( out, ES_OUT_RESET_PCR );
400     EsOutDiscontinuity( out, true, false );
401 }
402
403 bool input_EsOutDecodersEmpty( es_out_t *out )
404 {
405     es_out_sys_t      *p_sys = out->p_sys;
406     int i;
407
408     for( i = 0; i < p_sys->i_es; i++ )
409     {
410         es_out_id_t *es = p_sys->es[i];
411
412         if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
413             return false;
414     }
415     return true;
416 }
417
418 /*****************************************************************************
419  *
420  *****************************************************************************/
421 static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt, const char *psz_language,
422                                      bool b_delete )
423 {
424     es_out_sys_t      *p_sys = out->p_sys;
425     input_thread_t    *p_input = p_sys->p_input;
426     const  bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
427     vlc_value_t       val, text;
428
429     const char *psz_var;
430
431     if( fmt->i_cat == AUDIO_ES )
432         psz_var = "audio-es";
433     else if( fmt->i_cat == VIDEO_ES )
434         psz_var = "video-es";
435     else if( fmt->i_cat == SPU_ES )
436         psz_var = "spu-es";
437     else
438         return;
439
440     if( b_delete )
441     {
442         if( b_teletext )
443             var_SetInteger( p_sys->p_input, "teletext-es", -1 );
444
445         val.i_int = i_id;
446         var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
447
448         var_SetBool( p_sys->p_input, "intf-change", true );
449         return;
450     }
451
452     /* Get the number of ES already added */
453     var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
454     if( val.i_int == 0 )
455     {
456         vlc_value_t val2;
457
458         /* First one, we need to add the "Disable" choice */
459         val2.i_int = -1; text.psz_string = _("Disable");
460         var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
461         val.i_int++;
462     }
463
464     /* Take care of the ES description */
465     if( fmt->psz_description && *fmt->psz_description )
466     {
467         if( psz_language && *psz_language )
468         {
469             text.psz_string = malloc( strlen( fmt->psz_description) +
470                                       strlen( psz_language ) + 10 );
471             sprintf( text.psz_string, "%s - [%s]", fmt->psz_description,
472                                                    psz_language );
473         }
474         else text.psz_string = strdup( fmt->psz_description );
475     }
476     else
477     {
478         if( psz_language && *psz_language )
479         {
480             if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
481                 text.psz_string = NULL;
482         }
483         else
484         {
485             if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
486                 text.psz_string = NULL;
487         }
488     }
489
490     val.i_int = i_id;
491     var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
492
493     free( text.psz_string );
494
495     if( b_teletext )
496         var_SetInteger( p_sys->p_input, "teletext-es", i_id );
497
498     var_SetBool( p_sys->p_input, "intf-change", true );
499 }
500
501 static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
502                               bool b_delete )
503 {
504     EsOutESVarUpdateGeneric( out, es->i_id, &es->fmt, es->psz_language, b_delete );
505 }
506
507 /* EsOutProgramSelect:
508  *  Select a program and update the object variable
509  */
510 static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
511 {
512     es_out_sys_t      *p_sys = out->p_sys;
513     input_thread_t    *p_input = p_sys->p_input;
514     vlc_value_t       val;
515     int               i;
516
517     if( p_sys->p_pgrm == p_pgrm )
518         return; /* Nothing to do */
519
520     if( p_sys->p_pgrm )
521     {
522         es_out_pgrm_t *old = p_sys->p_pgrm;
523         msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
524
525         for( i = 0; i < p_sys->i_es; i++ )
526         {
527             if( p_sys->es[i]->p_pgrm == old && EsIsSelected( p_sys->es[i] ) &&
528                 p_sys->i_mode != ES_OUT_MODE_ALL )
529                 EsUnselect( out, p_sys->es[i], true );
530         }
531
532         p_sys->p_es_audio = NULL;
533         p_sys->p_es_sub = NULL;
534         p_sys->p_es_video = NULL;
535     }
536
537     msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
538
539     /* Mark it selected */
540     p_pgrm->b_selected = true;
541
542     /* Switch master stream */
543     if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
544     {
545         p_sys->p_pgrm->clock.b_master = false;
546     }
547     p_pgrm->clock.b_master = true;
548     p_sys->p_pgrm = p_pgrm;
549
550     /* Update "program" */
551     val.i_int = p_pgrm->i_id;
552     var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
553
554     /* Update "es-*" */
555     var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
556     var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
557     var_Change( p_input, "spu-es",   VLC_VAR_CLEARCHOICES, NULL, NULL );
558     var_SetInteger( p_input, "teletext-es", -1 );
559     for( i = 0; i < p_sys->i_es; i++ )
560     {
561         if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
562             EsOutESVarUpdate( out, p_sys->es[i], false );
563         EsOutSelect( out, p_sys->es[i], false );
564     }
565
566     /* Update now playing */
567     input_item_SetNowPlaying( p_input->p->input.p_item,
568                               p_pgrm->psz_now_playing );
569     input_item_SetPublisher( p_input->p->input.p_item,
570                              p_pgrm->psz_publisher );
571
572     var_SetBool( p_sys->p_input, "intf-change", true );
573 }
574
575 /* EsOutAddProgram:
576  *  Add a program
577  */
578 static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
579 {
580     es_out_sys_t      *p_sys = out->p_sys;
581     input_thread_t    *p_input = p_sys->p_input;
582     vlc_value_t       val;
583
584     es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
585     if( !p_pgrm ) return NULL;
586
587     /* Init */
588     p_pgrm->i_id = i_group;
589     p_pgrm->i_es = 0;
590     p_pgrm->b_selected = false;
591     p_pgrm->psz_name = NULL;
592     p_pgrm->psz_now_playing = NULL;
593     p_pgrm->psz_publisher = NULL;
594     p_pgrm->p_epg = NULL;
595     input_ClockInit( &p_pgrm->clock, false, p_input->p->input.i_cr_average, p_sys->i_rate );
596
597     /* Append it */
598     TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
599
600     /* Update "program" variable */
601     val.i_int = i_group;
602     var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );
603
604     if( i_group == var_GetInteger( p_input, "program" ) )
605     {
606         EsOutProgramSelect( out, p_pgrm );
607     }
608     else
609     {
610         var_SetBool( p_sys->p_input, "intf-change", true );
611     }
612     return p_pgrm;
613 }
614
615 /* EsOutDelProgram:
616  *  Delete a program
617  */
618 static int EsOutProgramDel( es_out_t *out, int i_group )
619 {
620     es_out_sys_t      *p_sys = out->p_sys;
621     input_thread_t    *p_input = p_sys->p_input;
622     es_out_pgrm_t     *p_pgrm = NULL;
623     vlc_value_t       val;
624     int               i;
625
626     for( i = 0; i < p_sys->i_pgrm; i++ )
627     {
628         if( p_sys->pgrm[i]->i_id == i_group )
629         {
630             p_pgrm = p_sys->pgrm[i];
631             break;
632         }
633     }
634
635     if( p_pgrm == NULL )
636         return VLC_EGENERIC;
637
638     if( p_pgrm->i_es )
639     {
640         msg_Dbg( p_input, "can't delete program %d which still has %i ES",
641                  i_group, p_pgrm->i_es );
642         return VLC_EGENERIC;
643     }
644
645     TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
646
647     /* If program is selected we need to unselect it */
648     if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
649
650     free( p_pgrm->psz_name );
651     free( p_pgrm->psz_now_playing );
652     free( p_pgrm->psz_publisher );
653     if( p_pgrm->p_epg )
654         vlc_epg_Delete( p_pgrm->p_epg );
655     free( p_pgrm );
656
657     /* Update "program" variable */
658     val.i_int = i_group;
659     var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
660
661     var_SetBool( p_sys->p_input, "intf-change", true );
662
663     return VLC_SUCCESS;
664 }
665
666 /* EsOutProgramMeta:
667  */
668 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
669 {
670     char *psz = NULL;
671     if( p_pgrm->psz_name )
672         asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
673     else
674         asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
675     return psz;
676 }
677
678 static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
679 {
680     es_out_sys_t      *p_sys = out->p_sys;
681     es_out_pgrm_t     *p_pgrm = NULL;
682     input_thread_t    *p_input = p_sys->p_input;
683     char              *psz_cat;
684     const char        *psz_title = NULL;
685     const char        *psz_provider = NULL;
686     int i;
687
688     msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
689
690     /* Check against empty meta data (empty for what we handle) */
691     if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
692         !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
693         !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
694         vlc_dictionary_keys_count( &p_meta->extra_tags ) <= 0 )
695     {
696         return;
697     }
698     /* Find program */
699     for( i = 0; i < p_sys->i_pgrm; i++ )
700     {
701         if( p_sys->pgrm[i]->i_id == i_group )
702         {
703             p_pgrm = p_sys->pgrm[i];
704             break;
705         }
706     }
707     if( p_pgrm == NULL )
708         p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
709
710     /* */
711     psz_title = vlc_meta_Get( p_meta, vlc_meta_Title);
712     psz_provider = vlc_meta_Get( p_meta, vlc_meta_Publisher);
713
714     /* Update the description text of the program */
715     if( psz_title && *psz_title )
716     {
717         vlc_value_t val;
718         vlc_value_t text;
719
720         if( !p_pgrm->psz_name || strcmp( p_pgrm->psz_name, psz_title ) )
721         {
722             char *psz_cat = EsOutProgramGetMetaName( p_pgrm );
723
724             /* Remove old entries */
725             input_Control( p_input, INPUT_DEL_INFO, psz_cat, NULL );
726             /* TODO update epg name */
727             free( psz_cat );
728         }
729         free( p_pgrm->psz_name );
730         p_pgrm->psz_name = strdup( psz_title );
731
732         /* ugly but it works */
733         val.i_int = i_group;
734         var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );
735
736         if( psz_provider && *psz_provider )
737         {
738             asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
739             var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
740             free( text.psz_string );
741         }
742         else
743         {
744             text.psz_string = (char *)psz_title;
745             var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
746         }
747     }
748
749     psz_cat = EsOutProgramGetMetaName( p_pgrm );
750     if( psz_provider )
751     {
752         if( p_sys->p_pgrm == p_pgrm )
753             input_item_SetPublisher( p_input->p->input.p_item, psz_provider );
754         input_Control( p_input, INPUT_ADD_INFO, psz_cat, input_MetaTypeToLocalizedString(vlc_meta_Publisher), psz_provider );
755     }
756     char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
757     for( i = 0; ppsz_all_keys[i]; i++ )
758     {
759         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _(ppsz_all_keys[i]),
760                        vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
761         free( ppsz_all_keys[i] );
762     }
763     free( ppsz_all_keys );
764
765     free( psz_cat );
766 }
767
768 static void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
769 {
770     int i;
771
772     /* Add new event */
773     for( i = 0; i < p_src->i_event; i++ )
774     {
775         vlc_epg_event_t *p_evt = p_src->pp_event[i];
776         bool b_add = true;
777         int j;
778
779         for( j = 0; j < p_dst->i_event; j++ )
780         {
781             if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
782             {
783                 b_add = false;
784                 break;
785             }
786             if( p_dst->pp_event[j]->i_start > p_evt->i_start )
787                 break;
788         }
789         if( b_add )
790         {
791             vlc_epg_event_t *p_copy = malloc( sizeof(vlc_epg_event_t) );
792             if( !p_copy )
793                 break;
794             memset( p_copy, 0, sizeof(vlc_epg_event_t) );
795             p_copy->i_start = p_evt->i_start;
796             p_copy->i_duration = p_evt->i_duration;
797             p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
798             p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
799             p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
800             TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
801         }
802     }
803     /* Update current */
804     vlc_epg_SetCurrent( p_dst, p_src->p_current ? p_src->p_current->i_start : -1 );
805
806     /* Keep only 1 old event  */
807     if( p_dst->p_current )
808     {
809         while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
810             TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
811     }
812 }
813
814 static void EsOutProgramEpg( es_out_t *out, int i_group, vlc_epg_t *p_epg )
815 {
816     es_out_sys_t      *p_sys = out->p_sys;
817     input_thread_t    *p_input = p_sys->p_input;
818     es_out_pgrm_t     *p_pgrm = NULL;
819     char *psz_cat;
820     int i;
821
822     /* Find program */
823     for( i = 0; i < p_sys->i_pgrm; i++ )
824     {
825         if( p_sys->pgrm[i]->i_id == i_group )
826         {
827             p_pgrm = p_sys->pgrm[i];
828             break;
829         }
830     }
831     if( p_pgrm == NULL )
832         p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
833
834     /* Merge EPG */
835     if( !p_pgrm->p_epg )
836         p_pgrm->p_epg = vlc_epg_New( p_pgrm->psz_name );
837     vlc_epg_Merge( p_pgrm->p_epg, p_epg );
838
839     /* Update info */
840     psz_cat = EsOutProgramGetMetaName( p_pgrm );
841 #ifdef HAVE_LOCALTIME_R
842     char *psz_epg;
843     if( asprintf( &psz_epg, "EPG %s", psz_cat ) == -1 )
844         psz_epg = NULL;
845     input_Control( p_input, INPUT_DEL_INFO, psz_epg, NULL );
846     msg_Dbg( p_input, "EsOutProgramEpg: number=%d name=%s", i_group, p_pgrm->p_epg->psz_name );
847     for( i = 0; i < p_pgrm->p_epg->i_event; i++ )
848     {
849         const vlc_epg_event_t *p_evt = p_pgrm->p_epg->pp_event[i];
850         time_t t_start = (time_t)p_evt->i_start;
851         struct tm tm_start;
852         char psz_start[128];
853
854         localtime_r( &t_start, &tm_start );
855
856         snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d:%2.2d", tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
857         if( p_evt->psz_short_description || p_evt->psz_description )
858             input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
859                            p_evt->psz_name,
860                            p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
861                            p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
862         else
863             input_Control( p_input, INPUT_ADD_INFO, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
864                            p_evt->psz_name,
865                            p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
866     }
867     free( psz_epg );
868 #endif
869     /* Update now playing */
870     free( p_pgrm->psz_now_playing );
871     p_pgrm->psz_now_playing = NULL;
872     if( p_epg->p_current && p_epg->p_current->psz_name && *p_epg->p_current->psz_name )
873         p_pgrm->psz_now_playing = strdup( p_epg->p_current->psz_name );
874
875     if( p_pgrm == p_sys->p_pgrm )
876         input_item_SetNowPlaying( p_input->p->input.p_item, p_pgrm->psz_now_playing );
877
878     if( p_pgrm->psz_now_playing )
879     {
880         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
881             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying),
882             p_pgrm->psz_now_playing );
883     }
884     else
885     {
886         input_Control( p_input, INPUT_DEL_INFO, psz_cat,
887             input_MetaTypeToLocalizedString(vlc_meta_NowPlaying) );
888     }
889
890     free( psz_cat );
891 }
892
893 /* EsOutAdd:
894  *  Add an es_out
895  */
896 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
897 {
898     es_out_sys_t      *p_sys = out->p_sys;
899     input_thread_t    *p_input = p_sys->p_input;
900
901     es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
902     es_out_pgrm_t     *p_pgrm = NULL;
903     int i;
904
905     if( !es ) return NULL;
906
907     if( fmt->i_group < 0 )
908     {
909         msg_Err( p_input, "invalid group number" );
910         free( es );
911         return NULL;
912     }
913
914     /* Search the program */
915     for( i = 0; i < p_sys->i_pgrm; i++ )
916     {
917         if( fmt->i_group == p_sys->pgrm[i]->i_id )
918         {
919             p_pgrm = p_sys->pgrm[i];
920             break;
921         }
922     }
923     if( p_pgrm == NULL )
924     {
925         /* Create a new one */
926         p_pgrm = EsOutProgramAdd( out, fmt->i_group );
927     }
928
929     /* Increase ref count for program */
930     p_pgrm->i_es++;
931
932     /* Set up ES */
933     if( fmt->i_id < 0 )
934         fmt->i_id = out->p_sys->i_id;
935     es->i_id = fmt->i_id;
936     es->p_pgrm = p_pgrm;
937     es_format_Copy( &es->fmt, fmt );
938     es->i_preroll_end = -1;
939
940     switch( fmt->i_cat )
941     {
942     case AUDIO_ES:
943     {
944         audio_replay_gain_t rg;
945
946         es->i_channel = p_sys->i_audio;
947
948         vlc_mutex_lock( &p_input->p->input.p_item->lock );
949         memset( &rg, 0, sizeof(rg) );
950         vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
951         vlc_mutex_unlock( &p_input->p->input.p_item->lock );
952
953         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
954         {
955             if( !es->fmt.audio_replay_gain.pb_peak[i] )
956             {
957                 es->fmt.audio_replay_gain.pb_peak[i] = rg.pb_peak[i];
958                 es->fmt.audio_replay_gain.pf_peak[i] = rg.pf_peak[i];
959             }
960             if( !es->fmt.audio_replay_gain.pb_gain[i] )
961             {
962                 es->fmt.audio_replay_gain.pb_gain[i] = rg.pb_gain[i];
963                 es->fmt.audio_replay_gain.pf_gain[i] = rg.pf_gain[i];
964             }
965         }
966         break;
967     }
968
969     case VIDEO_ES:
970         es->i_channel = p_sys->i_video;
971         if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
972             vlc_ureduce( &es->fmt.video.i_frame_rate,
973                          &es->fmt.video.i_frame_rate_base,
974                          fmt->video.i_frame_rate,
975                          fmt->video.i_frame_rate_base, 0 );
976         break;
977
978     case SPU_ES:
979         es->i_channel = p_sys->i_sub;
980         break;
981
982     default:
983         es->i_channel = 0;
984         break;
985     }
986     es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
987     es->psz_language_code = LanguageGetCode( fmt->psz_language );
988     es->p_dec = NULL;
989     for( i = 0; i < 4; i++ )
990         es->pb_cc_present[i] = false;
991     es->p_master = false;
992
993     if( es->p_pgrm == p_sys->p_pgrm )
994         EsOutESVarUpdate( out, es, false );
995
996     /* Select it if needed */
997     EsOutSelect( out, es, false );
998
999
1000     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
1001     p_sys->i_id++;  /* always incremented */
1002     switch( fmt->i_cat )
1003     {
1004         case AUDIO_ES:
1005             p_sys->i_audio++;
1006             break;
1007         case SPU_ES:
1008             p_sys->i_sub++;
1009             break;
1010         case VIDEO_ES:
1011             p_sys->i_video++;
1012             break;
1013     }
1014
1015     EsOutAddInfo( out, es );
1016
1017     return es;
1018 }
1019
1020 static bool EsIsSelected( es_out_id_t *es )
1021 {
1022     if( es->p_master )
1023     {
1024         bool b_decode = false;
1025         if( es->p_master->p_dec )
1026         {
1027             int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1028             if( i_channel != -1 )
1029                 input_DecoderGetCcState( es->p_master->p_dec, &b_decode, i_channel );
1030         }
1031         return b_decode;
1032     }
1033     else
1034     {
1035         return es->p_dec != NULL;
1036     }
1037 }
1038 static void EsSelect( es_out_t *out, es_out_id_t *es )
1039 {
1040     es_out_sys_t   *p_sys = out->p_sys;
1041     input_thread_t *p_input = p_sys->p_input;
1042     vlc_value_t    val;
1043     const char     *psz_var;
1044
1045     if( EsIsSelected( es ) )
1046     {
1047         msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
1048         return;
1049     }
1050
1051     if( es->p_master )
1052     {
1053         int i_channel;
1054         if( !es->p_master->p_dec )
1055             return;
1056
1057         i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1058         if( i_channel == -1 || input_DecoderSetCcState( es->p_master->p_dec, true, i_channel ) )
1059             return;
1060     }
1061     else
1062     {
1063         if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
1064         {
1065             if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
1066             {
1067                 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
1068                          es->i_id );
1069                 return;
1070             }
1071         }
1072         else if( es->fmt.i_cat == AUDIO_ES )
1073         {
1074             var_Get( p_input, "audio", &val );
1075             if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
1076             {
1077                 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
1078                          es->i_id );
1079                 return;
1080             }
1081         }
1082         if( es->fmt.i_cat == SPU_ES )
1083         {
1084             var_Get( p_input, "spu", &val );
1085             if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
1086             {
1087                 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
1088                          es->i_id );
1089                 return;
1090             }
1091         }
1092
1093         es->i_preroll_end = -1;
1094         es->p_dec = input_DecoderNew( p_input, &es->fmt, false );
1095         if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
1096             return;
1097     }
1098
1099     if( es->fmt.i_cat == VIDEO_ES )
1100         psz_var = "video-es";
1101     else if( es->fmt.i_cat == AUDIO_ES )
1102         psz_var = "audio-es";
1103     else if( es->fmt.i_cat == SPU_ES )
1104         psz_var = "spu-es";
1105     else
1106         return;
1107
1108     /* Mark it as selected */
1109     val.i_int = es->i_id;
1110     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1111
1112     var_SetBool( p_sys->p_input, "intf-change", true );
1113 }
1114
1115 static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
1116 {
1117     es_out_sys_t   *p_sys = out->p_sys;
1118     input_thread_t *p_input = p_sys->p_input;
1119     vlc_value_t    val;
1120     const char     *psz_var;
1121
1122     if( !EsIsSelected( es ) )
1123     {
1124         msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
1125         return;
1126     }
1127
1128     if( es->p_master )
1129     {
1130         if( es->p_master->p_dec )
1131         {
1132             int i_channel = EsOutGetClosedCaptionsChannel( es->fmt.i_codec );
1133             if( i_channel != -1 )
1134                 input_DecoderSetCcState( es->p_master->p_dec, false, i_channel );
1135         }
1136     }
1137     else
1138     {
1139         const int i_spu_id = var_GetInteger( p_input, "spu-es");
1140         int i;
1141         for( i = 0; i < 4; i++ )
1142         {
1143             if( !es->pb_cc_present[i] || !es->pp_cc_es[i] )
1144                 continue;
1145
1146             if( i_spu_id == es->pp_cc_es[i]->i_id )
1147             {
1148                 /* Force unselection of the CC */
1149                 val.i_int = -1;
1150                 var_Change( p_input, "spu-es", VLC_VAR_SETVALUE, &val, NULL );
1151                 if( !b_update )
1152                     var_SetBool( p_sys->p_input, "intf-change", true );
1153             }
1154             EsOutDel( out, es->pp_cc_es[i] );
1155
1156             es->pb_cc_present[i] = false;
1157         }
1158         input_DecoderDelete( es->p_dec );
1159         es->p_dec = NULL;
1160     }
1161
1162     if( !b_update )
1163         return;
1164
1165     /* Update var */
1166     if( es->p_dec == NULL )
1167         return;
1168     if( es->fmt.i_cat == VIDEO_ES )
1169         psz_var = "video-es";
1170     else if( es->fmt.i_cat == AUDIO_ES )
1171         psz_var = "audio-es";
1172     else if( es->fmt.i_cat == SPU_ES )
1173         psz_var = "spu-es";
1174     else
1175         return;
1176
1177     /* Mark it as unselected */
1178     val.i_int = -1;
1179     var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );
1180
1181     var_SetBool( p_sys->p_input, "intf-change", true );
1182 }
1183
1184 /**
1185  * Select an ES given the current mode
1186  * XXX: you need to take a the lock before (stream.stream_lock)
1187  *
1188  * \param out The es_out structure
1189  * \param es es_out_id structure
1190  * \param b_force ...
1191  * \return nothing
1192  */
1193 static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
1194 {
1195     es_out_sys_t      *p_sys = out->p_sys;
1196
1197     int i_cat = es->fmt.i_cat;
1198
1199     if( !p_sys->b_active ||
1200         ( !b_force && es->fmt.i_priority < 0 ) )
1201     {
1202         return;
1203     }
1204
1205     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
1206     {
1207         if( !EsIsSelected( es ) )
1208             EsSelect( out, es );
1209     }
1210     else if( p_sys->i_mode == ES_OUT_MODE_PARTIAL )
1211     {
1212         vlc_value_t val;
1213         int i;
1214         var_Get( p_sys->p_input, "programs", &val );
1215         for ( i = 0; i < val.p_list->i_count; i++ )
1216         {
1217             if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
1218             {
1219                 if( !EsIsSelected( es ) )
1220                     EsSelect( out, es );
1221                 break;
1222             }
1223         }
1224         var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
1225     }
1226     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
1227     {
1228         int i_wanted  = -1;
1229
1230         if( es->p_pgrm != p_sys->p_pgrm )
1231             return;
1232
1233         if( i_cat == AUDIO_ES )
1234         {
1235             int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1236                                      es->psz_language_code );
1237
1238             if( p_sys->p_es_audio &&
1239                 p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
1240             {
1241                 int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
1242                                          p_sys->p_es_audio->psz_language_code );
1243
1244                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1245                     return;
1246                 i_wanted = es->i_channel;
1247             }
1248             else
1249             {
1250                 /* Select audio if (no audio selected yet)
1251                  * - no audio-language
1252                  * - no audio code for the ES
1253                  * - audio code in the requested list */
1254                 if( idx1 >= 0 ||
1255                     !strcmp( es->psz_language_code, "??" ) ||
1256                     !p_sys->ppsz_audio_language )
1257                     i_wanted = es->i_channel;
1258             }
1259
1260             if( p_sys->i_audio_last >= 0 )
1261                 i_wanted = p_sys->i_audio_last;
1262
1263             if( p_sys->i_audio_id >= 0 )
1264             {
1265                 if( es->i_id == p_sys->i_audio_id )
1266                     i_wanted = es->i_channel;
1267                 else
1268                     return;
1269             }
1270         }
1271         else if( i_cat == SPU_ES )
1272         {
1273             int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1274                                      es->psz_language_code );
1275
1276             if( p_sys->p_es_sub &&
1277                 p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
1278             {
1279                 int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
1280                                          p_sys->p_es_sub->psz_language_code );
1281
1282                 msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
1283                         idx1, es->psz_language_code, idx2,
1284                         p_sys->p_es_sub->psz_language_code );
1285
1286                 if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
1287                     return;
1288                 /* We found a SPU that matches our language request */
1289                 i_wanted  = es->i_channel;
1290             }
1291             else if( idx1 >= 0 )
1292             {
1293                 msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
1294                         idx1, es->psz_language_code );
1295
1296                 i_wanted  = es->i_channel;
1297             }
1298             else if( p_sys->i_default_sub_id >= 0 )
1299             {
1300                 if( es->i_id == p_sys->i_default_sub_id )
1301                     i_wanted = es->i_channel;
1302             }
1303
1304             if( p_sys->i_sub_last >= 0 )
1305                 i_wanted  = p_sys->i_sub_last;
1306
1307             if( p_sys->i_sub_id >= 0 )
1308             {
1309                 if( es->i_id == p_sys->i_sub_id )
1310                     i_wanted = es->i_channel;
1311                 else
1312                     return;
1313             }
1314         }
1315         else if( i_cat == VIDEO_ES )
1316         {
1317             i_wanted  = es->i_channel;
1318         }
1319
1320         if( i_wanted == es->i_channel && !EsIsSelected( es ) )
1321             EsSelect( out, es );
1322     }
1323
1324     /* FIXME TODO handle priority here */
1325     if( EsIsSelected( es ) )
1326     {
1327         if( i_cat == AUDIO_ES )
1328         {
1329             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1330                 p_sys->p_es_audio &&
1331                 p_sys->p_es_audio != es &&
1332                 EsIsSelected( p_sys->p_es_audio ) )
1333             {
1334                 EsUnselect( out, p_sys->p_es_audio, false );
1335             }
1336             p_sys->p_es_audio = es;
1337         }
1338         else if( i_cat == SPU_ES )
1339         {
1340             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
1341                 p_sys->p_es_sub &&
1342                 p_sys->p_es_sub != es &&
1343                 EsIsSelected( p_sys->p_es_sub ) )
1344             {
1345                 EsUnselect( out, p_sys->p_es_sub, false );
1346             }
1347             p_sys->p_es_sub = es;
1348         }
1349         else if( i_cat == VIDEO_ES )
1350         {
1351             p_sys->p_es_video = es;
1352         }
1353     }
1354 }
1355
1356 /**
1357  * Send a block for the given es_out
1358  *
1359  * \param out the es_out to send from
1360  * \param es the es_out_id
1361  * \param p_block the data block to send
1362  */
1363 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
1364 {
1365     es_out_sys_t *p_sys = out->p_sys;
1366     input_thread_t    *p_input = p_sys->p_input;
1367     es_out_pgrm_t *p_pgrm = es->p_pgrm;
1368     int64_t i_delay;
1369     int i_total=0;
1370
1371     if( es->fmt.i_cat == AUDIO_ES )
1372         i_delay = p_sys->i_audio_delay;
1373     else if( es->fmt.i_cat == SPU_ES )
1374         i_delay = p_sys->i_spu_delay;
1375     else
1376         i_delay = 0;
1377
1378     if( libvlc_stats (p_input) )
1379     {
1380         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1381         stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1382                              p_block->i_buffer, &i_total );
1383         stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1384                            (float)i_total, NULL );
1385         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1386     }
1387
1388     /* Mark preroll blocks */
1389     if( es->i_preroll_end >= 0 )
1390     {
1391         int64_t i_date = p_block->i_pts;
1392         if( i_date <= 0 )
1393             i_date = p_block->i_dts;
1394
1395         if( i_date < es->i_preroll_end )
1396             p_block->i_flags |= BLOCK_FLAG_PREROLL;
1397         else
1398             es->i_preroll_end = -1;
1399     }
1400
1401     if( p_block->i_dts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1402     {
1403         p_block->i_dts += i_delay;
1404     }
1405     else if( p_block->i_dts > 0 )
1406     {
1407         p_block->i_dts =
1408             input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
1409     }
1410     if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
1411     {
1412         p_block->i_pts += i_delay;
1413     }
1414     else if( p_block->i_pts > 0 )
1415     {
1416         p_block->i_pts =
1417             input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
1418     }
1419     if ( p_block->i_rate == INPUT_RATE_DEFAULT &&
1420          es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
1421     {
1422         mtime_t current_date = mdate();
1423         if( !p_block->i_pts
1424                || p_block->i_pts > current_date + 10000000
1425                || current_date > p_block->i_pts )
1426         {
1427             /* ETSI EN 300 472 Annex A : do not take into account the PTS
1428              * for teletext streams. */
1429             p_block->i_pts = current_date + 400000
1430                                + p_input->i_pts_delay + i_delay;
1431         }
1432     }
1433
1434     p_block->i_rate = p_sys->i_rate;
1435
1436     /* TODO handle mute */
1437     if( es->p_dec &&
1438         ( es->fmt.i_cat != AUDIO_ES ||
1439           ( p_sys->i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
1440             p_sys->i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE ) ) )
1441     {
1442         bool pb_cc[4];
1443         bool b_cc_new = false;
1444         int i;
1445         input_DecoderDecode( es->p_dec, p_block );
1446
1447         /* Check CC status */
1448         input_DecoderIsCcPresent( es->p_dec, pb_cc );
1449         for( i = 0; i < 4; i++ )
1450         {
1451             static const vlc_fourcc_t fcc[4] = {
1452                 VLC_FOURCC('c', 'c', '1', ' '),
1453                 VLC_FOURCC('c', 'c', '2', ' '),
1454                 VLC_FOURCC('c', 'c', '3', ' '),
1455                 VLC_FOURCC('c', 'c', '4', ' '),
1456             };
1457             static const char ppsz_description[4][18] = {
1458                 N_("Closed captions 1"),
1459                 N_("Closed captions 2"),
1460                 N_("Closed captions 3"),
1461                 N_("Closed captions 4"),
1462             };
1463             es_format_t fmt;
1464
1465             if(  es->pb_cc_present[i] || !pb_cc[i] )
1466                 continue;
1467             msg_Dbg( p_input, "Adding CC track %d for es[%d]", 1+i, es->i_id );
1468
1469             es_format_Init( &fmt, SPU_ES, fcc[i] );
1470             fmt.i_group = es->fmt.i_group;
1471             fmt.psz_description = strdup( _(ppsz_description[i] ) );
1472             es->pp_cc_es[i] = EsOutAdd( out, &fmt );
1473             es->pp_cc_es[i]->p_master = es;
1474             es_format_Clean( &fmt );
1475
1476             /* */
1477             es->pb_cc_present[i] = true;
1478             b_cc_new = true;
1479         }
1480         if( b_cc_new )
1481             var_SetBool( p_sys->p_input, "intf-change", true );
1482     }
1483     else
1484     {
1485         block_Release( p_block );
1486     }
1487
1488     return VLC_SUCCESS;
1489 }
1490
1491 /*****************************************************************************
1492  * EsOutDel:
1493  *****************************************************************************/
1494 static void EsOutDel( es_out_t *out, es_out_id_t *es )
1495 {
1496     es_out_sys_t *p_sys = out->p_sys;
1497     bool b_reselect = false;
1498     int i;
1499
1500     /* We don't try to reselect */
1501     if( es->p_dec )
1502     {
1503         while( !out->p_sys->p_input->b_die && es->p_dec )
1504         {
1505             if( input_DecoderEmpty( es->p_dec ) )
1506                 break;
1507             msleep( 20*1000 );
1508         }
1509         EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1510     }
1511
1512     if( es->p_pgrm == p_sys->p_pgrm )
1513         EsOutESVarUpdate( out, es, true );
1514
1515     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
1516
1517     es->p_pgrm->i_es--;
1518     if( es->p_pgrm->i_es == 0 )
1519     {
1520         msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1521     }
1522
1523     if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
1524         p_sys->p_es_sub == es ) b_reselect = true;
1525
1526     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
1527     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
1528     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
1529
1530     switch( es->fmt.i_cat )
1531     {
1532         case AUDIO_ES:
1533             p_sys->i_audio--;
1534             break;
1535         case SPU_ES:
1536             p_sys->i_sub--;
1537             break;
1538         case VIDEO_ES:
1539             p_sys->i_video--;
1540             break;
1541     }
1542
1543     /* Re-select another track when needed */
1544     if( b_reselect )
1545         for( i = 0; i < p_sys->i_es; i++ )
1546         {
1547             if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
1548                 EsOutSelect( out, p_sys->es[i], false );
1549         }
1550
1551     free( es->psz_language );
1552     free( es->psz_language_code );
1553
1554     es_format_Clean( &es->fmt );
1555
1556     free( es );
1557 }
1558
1559 /**
1560  * Control query handler
1561  *
1562  * \param out the es_out to control
1563  * \param i_query A es_out query as defined in include/ninput.h
1564  * \param args a variable list of arguments for the query
1565  * \return VLC_SUCCESS or an error code
1566  */
1567 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1568 {
1569     es_out_sys_t *p_sys = out->p_sys;
1570     bool  b, *pb;
1571     int         i, *pi;
1572
1573     es_out_id_t *es;
1574
1575     switch( i_query )
1576     {
1577         case ES_OUT_SET_ES_STATE:
1578             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1579             b = (bool) va_arg( args, int );
1580             if( b && !EsIsSelected( es ) )
1581             {
1582                 EsSelect( out, es );
1583                 return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
1584             }
1585             else if( !b && EsIsSelected( es ) )
1586             {
1587                 EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
1588                 return VLC_SUCCESS;
1589             }
1590             return VLC_SUCCESS;
1591
1592         case ES_OUT_GET_ES_STATE:
1593             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1594             pb = (bool*) va_arg( args, bool * );
1595
1596             *pb = EsIsSelected( es );
1597             return VLC_SUCCESS;
1598
1599         case ES_OUT_SET_ACTIVE:
1600         {
1601             b = (bool) va_arg( args, int );
1602             p_sys->b_active = b;
1603             /* Needed ? */
1604             if( b )
1605                 var_SetBool( p_sys->p_input, "intf-change", true );
1606             return VLC_SUCCESS;
1607         }
1608
1609         case ES_OUT_GET_ACTIVE:
1610             pb = (bool*) va_arg( args, bool * );
1611             *pb = p_sys->b_active;
1612             return VLC_SUCCESS;
1613
1614         case ES_OUT_SET_MODE:
1615             i = (int) va_arg( args, int );
1616             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
1617                 i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
1618             {
1619                 p_sys->i_mode = i;
1620
1621                 /* Reapply policy mode */
1622                 for( i = 0; i < p_sys->i_es; i++ )
1623                 {
1624                     if( EsIsSelected( p_sys->es[i] ) )
1625                     {
1626                         EsUnselect( out, p_sys->es[i],
1627                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1628                     }
1629                 }
1630                 for( i = 0; i < p_sys->i_es; i++ )
1631                 {
1632                     EsOutSelect( out, p_sys->es[i], false );
1633                 }
1634                 return VLC_SUCCESS;
1635             }
1636             return VLC_EGENERIC;
1637
1638         case ES_OUT_GET_MODE:
1639             pi = (int*) va_arg( args, int* );
1640             *pi = p_sys->i_mode;
1641             return VLC_SUCCESS;
1642
1643         case ES_OUT_SET_ES:
1644             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1645             /* Special case NULL, NULL+i_cat */
1646             if( es == NULL )
1647             {
1648                 for( i = 0; i < p_sys->i_es; i++ )
1649                 {
1650                     if( EsIsSelected( p_sys->es[i] ) )
1651                         EsUnselect( out, p_sys->es[i],
1652                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1653                 }
1654             }
1655             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1656             {
1657                 for( i = 0; i < p_sys->i_es; i++ )
1658                 {
1659                     if( p_sys->es[i]->fmt.i_cat == AUDIO_ES &&
1660                         EsIsSelected( p_sys->es[i] ) )
1661                         EsUnselect( out, p_sys->es[i],
1662                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1663                 }
1664             }
1665             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1666             {
1667                 for( i = 0; i < p_sys->i_es; i++ )
1668                 {
1669                     if( p_sys->es[i]->fmt.i_cat == VIDEO_ES &&
1670                         EsIsSelected( p_sys->es[i] ) )
1671                         EsUnselect( out, p_sys->es[i],
1672                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1673                 }
1674             }
1675             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1676             {
1677                 for( i = 0; i < p_sys->i_es; i++ )
1678                 {
1679                     if( p_sys->es[i]->fmt.i_cat == SPU_ES &&
1680                         EsIsSelected( p_sys->es[i] ) )
1681                         EsUnselect( out, p_sys->es[i],
1682                                     p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
1683                 }
1684             }
1685             else
1686             {
1687                 for( i = 0; i < p_sys->i_es; i++ )
1688                 {
1689                     if( es == p_sys->es[i] )
1690                     {
1691                         EsOutSelect( out, es, true );
1692                         break;
1693                     }
1694                 }
1695             }
1696             {
1697                 vlc_event_t event;
1698                 event.type = vlc_InputSelectedStreamChanged;
1699                 vlc_event_send( &p_sys->p_input->p->event_manager, &event );
1700             }
1701             return VLC_SUCCESS;
1702  
1703         case ES_OUT_SET_DEFAULT:
1704         {
1705             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1706
1707             if( es == NULL )
1708             {
1709                 /*p_sys->i_default_video_id = -1;*/
1710                 /*p_sys->i_default_audio_id = -1;*/
1711                 p_sys->i_default_sub_id = -1;
1712             }
1713             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
1714             {
1715                 /*p_sys->i_default_video_id = -1;*/
1716             }
1717             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
1718             {
1719                 /*p_sys->i_default_audio_id = -1;*/
1720             }
1721             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
1722             {
1723                 p_sys->i_default_sub_id = -1;
1724             }
1725             else
1726             {
1727                 /*if( es->fmt.i_cat == VIDEO_ES )
1728                     p_sys->i_default_video_id = es->i_id;
1729                 else
1730                 if( es->fmt.i_cat == AUDIO_ES )
1731                     p_sys->i_default_audio_id = es->i_id;
1732                 else*/
1733                 if( es->fmt.i_cat == SPU_ES )
1734                     p_sys->i_default_sub_id = es->i_id;
1735             }
1736             return VLC_SUCCESS;
1737         }
1738
1739         case ES_OUT_SET_PCR:
1740         case ES_OUT_SET_GROUP_PCR:
1741         {
1742             es_out_pgrm_t *p_pgrm = NULL;
1743             int            i_group = 0;
1744             int64_t        i_pcr;
1745
1746             if( i_query == ES_OUT_SET_PCR )
1747             {
1748                 p_pgrm = p_sys->p_pgrm;
1749             }
1750             else
1751             {
1752                 int i;
1753                 i_group = (int)va_arg( args, int );
1754                 for( i = 0; i < p_sys->i_pgrm; i++ )
1755                 {
1756                     if( p_sys->pgrm[i]->i_id == i_group )
1757                     {
1758                         p_pgrm = p_sys->pgrm[i];
1759                         break;
1760                     }
1761                 }
1762             }
1763             if( p_pgrm == NULL )
1764                 p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
1765
1766             i_pcr = (int64_t)va_arg( args, int64_t );
1767             /* search program */
1768             input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
1769             return VLC_SUCCESS;
1770         }
1771
1772         case ES_OUT_RESET_PCR:
1773             for( i = 0; i < p_sys->i_pgrm; i++ )
1774                 input_ClockResetPCR( &p_sys->pgrm[i]->clock );
1775             return VLC_SUCCESS;
1776
1777         case ES_OUT_GET_TS:
1778             if( p_sys->p_pgrm )
1779             {
1780                 int64_t i_ts = (int64_t)va_arg( args, int64_t );
1781                 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
1782                 *pi_ts = input_ClockGetTS( p_sys->p_input,
1783                                            &p_sys->p_pgrm->clock, i_ts );
1784                 return VLC_SUCCESS;
1785             }
1786             return VLC_EGENERIC;
1787
1788         case ES_OUT_GET_GROUP:
1789             pi = (int*) va_arg( args, int* );
1790             if( p_sys->p_pgrm )
1791                 *pi = p_sys->p_pgrm->i_id;
1792             else
1793                 *pi = -1;    /* FIXME */
1794             return VLC_SUCCESS;
1795
1796         case ES_OUT_SET_GROUP:
1797         {
1798             int j;
1799             i = (int) va_arg( args, int );
1800             for( j = 0; j < p_sys->i_pgrm; j++ )
1801             {
1802                 es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
1803                 if( p_pgrm->i_id == i )
1804                 {
1805                     EsOutProgramSelect( out, p_pgrm );
1806                     return VLC_SUCCESS;
1807                 }
1808             }
1809             return VLC_EGENERIC;
1810         }
1811
1812         case ES_OUT_SET_FMT:
1813         {
1814             /* This ain't pretty but is need by some demuxers (eg. Ogg )
1815              * to update the p_extra data */
1816             es_format_t *p_fmt;
1817             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1818             p_fmt = (es_format_t*) va_arg( args, es_format_t * );
1819             if( es == NULL ) return VLC_EGENERIC;
1820
1821             if( p_fmt->i_extra )
1822             {
1823                 es->fmt.i_extra = p_fmt->i_extra;
1824                 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
1825                 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
1826
1827                 if( !es->p_dec ) return VLC_SUCCESS;
1828
1829 #if 1
1830                 input_DecoderDelete( es->p_dec );
1831                 es->p_dec = input_DecoderNew( p_sys->p_input,
1832                                               &es->fmt, false );
1833
1834 #else
1835                 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
1836                 es->p_dec->fmt_in.p_extra =
1837                     realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
1838                 memcpy( es->p_dec->fmt_in.p_extra,
1839                         p_fmt->p_extra, p_fmt->i_extra );
1840 #endif
1841             }
1842
1843             return VLC_SUCCESS;
1844         }
1845
1846         case ES_OUT_SET_NEXT_DISPLAY_TIME:
1847         {
1848             int64_t i_date;
1849
1850             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
1851             i_date = (int64_t)va_arg( args, int64_t );
1852
1853             if( !es || !es->p_dec )
1854                 return VLC_EGENERIC;
1855
1856             /* XXX We should call input_ClockGetTS but PCR has been reseted
1857              * and it will return 0, so we won't call input_ClockGetTS on all preroll samples
1858              * but that's ugly(more time discontinuity), it need to be improved -- fenrir */
1859             es->i_preroll_end = i_date;
1860
1861             return VLC_SUCCESS;
1862         }
1863         case ES_OUT_SET_GROUP_META:
1864         {
1865             int i_group = (int)va_arg( args, int );
1866             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1867
1868             EsOutProgramMeta( out, i_group, p_meta );
1869             return VLC_SUCCESS;
1870         }
1871         case ES_OUT_SET_GROUP_EPG:
1872         {
1873             int i_group = (int)va_arg( args, int );
1874             vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1875
1876             EsOutProgramEpg( out, i_group, p_epg );
1877             return VLC_SUCCESS;
1878         }
1879         case ES_OUT_DEL_GROUP:
1880         {
1881             int i_group = (int)va_arg( args, int );
1882
1883             return EsOutProgramDel( out, i_group );
1884         }
1885
1886         default:
1887             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1888             return VLC_EGENERIC;
1889     }
1890 }
1891
1892 /****************************************************************************
1893  * LanguageGetName: try to expend iso639 into plain name
1894  ****************************************************************************/
1895 static char *LanguageGetName( const char *psz_code )
1896 {
1897     const iso639_lang_t *pl;
1898
1899     if( psz_code == NULL )
1900     {
1901         return strdup( "" );
1902     }
1903
1904     if( strlen( psz_code ) == 2 )
1905     {
1906         pl = GetLang_1( psz_code );
1907     }
1908     else if( strlen( psz_code ) == 3 )
1909     {
1910         pl = GetLang_2B( psz_code );
1911         if( !strcmp( pl->psz_iso639_1, "??" ) )
1912         {
1913             pl = GetLang_2T( psz_code );
1914         }
1915     }
1916     else
1917     {
1918         return strdup( psz_code );
1919     }
1920
1921     if( !strcmp( pl->psz_iso639_1, "??" ) )
1922     {
1923        return strdup( psz_code );
1924     }
1925     else
1926     {
1927         if( *pl->psz_native_name )
1928         {
1929             return strdup( pl->psz_native_name );
1930         }
1931         return strdup( pl->psz_eng_name );
1932     }
1933 }
1934
1935 /* Get a 2 char code */
1936 static char *LanguageGetCode( const char *psz_lang )
1937 {
1938     const iso639_lang_t *pl;
1939
1940     if( psz_lang == NULL || *psz_lang == '\0' )
1941         return strdup("??");
1942
1943     for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
1944     {
1945         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1946             !strcasecmp( pl->psz_native_name, psz_lang ) ||
1947             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1948             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1949             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1950             break;
1951     }
1952
1953     if( pl->psz_iso639_1 != NULL )
1954         return strdup( pl->psz_iso639_1 );
1955
1956     return strdup("??");
1957 }
1958
1959 static char **LanguageSplit( const char *psz_langs )
1960 {
1961     char *psz_dup;
1962     char *psz_parser;
1963     char **ppsz = NULL;
1964     int i_psz = 0;
1965
1966     if( psz_langs == NULL ) return NULL;
1967
1968     psz_parser = psz_dup = strdup(psz_langs);
1969
1970     while( psz_parser && *psz_parser )
1971     {
1972         char *psz;
1973         char *psz_code;
1974
1975         psz = strchr(psz_parser, ',' );
1976         if( psz ) *psz++ = '\0';
1977
1978         if( !strcmp( psz_parser, "any" ) )
1979         {
1980             TAB_APPEND( i_psz, ppsz, strdup("any") );
1981         }
1982         else
1983         {
1984             psz_code = LanguageGetCode( psz_parser );
1985             if( strcmp( psz_code, "??" ) )
1986             {
1987                 TAB_APPEND( i_psz, ppsz, psz_code );
1988             }
1989             else
1990             {
1991                 free( psz_code );
1992             }
1993         }
1994
1995         psz_parser = psz;
1996     }
1997
1998     if( i_psz )
1999     {
2000         TAB_APPEND( i_psz, ppsz, NULL );
2001     }
2002
2003     free( psz_dup );
2004     return ppsz;
2005 }
2006
2007 static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
2008 {
2009     int i;
2010
2011     if( !ppsz_langs || !psz_lang ) return -1;
2012
2013     for( i = 0; ppsz_langs[i]; i++ )
2014     {
2015         if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
2016             !strcasecmp( ppsz_langs[i], "any" ) )
2017         {
2018             return i;
2019         }
2020     }
2021
2022     return -1;
2023 }
2024
2025 /****************************************************************************
2026  * EsOutAddInfo:
2027  * - add meta info to the playlist item
2028  ****************************************************************************/
2029 static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
2030 {
2031     es_out_sys_t   *p_sys = out->p_sys;
2032     input_thread_t *p_input = p_sys->p_input;
2033     es_format_t    *fmt = &es->fmt;
2034     char           *psz_cat;
2035     lldiv_t         div;
2036
2037     /* Add stream info */
2038     asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
2039
2040     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
2041                    "%.4s", (char*)&fmt->i_codec );
2042
2043     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
2044                    "%s", es->psz_language );
2045
2046     /* Add information */
2047     switch( fmt->i_cat )
2048     {
2049     case AUDIO_ES:
2050         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2051                        _("Type"), _("Audio") );
2052
2053         if( fmt->audio.i_channels > 0 )
2054             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
2055                            "%u", fmt->audio.i_channels );
2056
2057         if( fmt->audio.i_rate > 0 )
2058         {
2059             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
2060                            _("%u Hz"), fmt->audio.i_rate );
2061             var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
2062         }
2063
2064         if( fmt->audio.i_bitspersample > 0 )
2065             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2066                            _("Bits per sample"), "%u",
2067                            fmt->audio.i_bitspersample );
2068
2069         if( fmt->i_bitrate > 0 )
2070         {
2071             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
2072                            _("%u kb/s"), fmt->i_bitrate / 1000 );
2073             var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
2074         }
2075         break;
2076
2077     case VIDEO_ES:
2078         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2079                        _("Type"), _("Video") );
2080
2081         if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
2082             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2083                            _("Resolution"), "%ux%u",
2084                            fmt->video.i_width, fmt->video.i_height );
2085
2086         if( fmt->video.i_visible_width > 0 &&
2087             fmt->video.i_visible_height > 0 )
2088             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2089                            _("Display resolution"), "%ux%u",
2090                            fmt->video.i_visible_width,
2091                            fmt->video.i_visible_height);
2092        if( fmt->video.i_frame_rate > 0 &&
2093            fmt->video.i_frame_rate_base > 0 )
2094        {
2095            div = lldiv( (float)fmt->video.i_frame_rate /
2096                                fmt->video.i_frame_rate_base * 1000000,
2097                                1000000 );
2098            input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2099                           _("Frame rate"), "%"PRId64".%06u",
2100                           div.quot, (unsigned int )div.rem );
2101        }
2102        break;
2103
2104     case SPU_ES:
2105         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
2106                        _("Type"), _("Subtitle") );
2107         break;
2108
2109     default:
2110         break;
2111     }
2112
2113     free( psz_cat );
2114 }