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