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