* String review, round one
[vlc.git] / src / input / es_out.c
1 /*****************************************************************************
2  * es_out.c: Es Out handler for input.
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id: es_out.c,v 1.21 2004/01/25 17:16:05 zorglub Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/decoder.h>
32
33 #include "vlc_playlist.h"
34 #include "codecs.h"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 struct es_out_id_t
40 {
41     int             i_channel;
42     es_descriptor_t *p_es;
43 };
44
45 struct es_out_sys_t
46 {
47     input_thread_t *p_input;
48     vlc_bool_t      b_pcr_set;
49
50     /* all es */
51     int         i_id;
52
53     int         i_es;
54     es_out_id_t **es;
55
56     /* mode gestion */
57     vlc_bool_t  b_active;
58     int         i_mode;
59
60     /* es count */
61     int         i_audio;
62     int         i_video;
63     int         i_sub;
64
65     /* es to select */
66     int         i_audio_last;
67     int         i_sub_last;
68
69     /* current main es */
70     es_out_id_t *p_es_audio;
71     es_out_id_t *p_es_video;
72     es_out_id_t *p_es_sub;
73 };
74
75 static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
76 static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
77 static void         EsOutDel    ( es_out_t *, es_out_id_t * );
78 static int          EsOutControl( es_out_t *, int i_query, va_list );
79
80
81 /**
82  * Create a new es_out structure
83  *
84  * \param p_input The related input thread
85  * \return the new es_out_t
86  */
87 es_out_t *input_EsOutNew( input_thread_t *p_input )
88 {
89     es_out_t     *out = malloc( sizeof( es_out_t ) );
90     es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
91     vlc_value_t  val;
92
93     out->pf_add     = EsOutAdd;
94     out->pf_send    = EsOutSend;
95     out->pf_del     = EsOutDel;
96     out->pf_control = EsOutControl;
97     out->p_sys      = p_sys;
98
99     p_sys->p_input = p_input;
100     p_sys->b_pcr_set = VLC_FALSE;
101
102     p_sys->b_active = VLC_FALSE;
103     p_sys->i_mode   = ES_OUT_MODE_AUTO;
104
105     p_sys->i_id    = 1;
106
107     p_sys->i_es    = 0;
108     p_sys->es      = NULL;
109
110     p_sys->i_audio = 0;
111     p_sys->i_video = 0;
112     p_sys->i_sub   = 0;
113
114     var_Get( p_input, "audio-channel", &val );
115     p_sys->i_audio_last = val.i_int;
116
117     var_Get( p_input, "spu-channel", &val );
118     p_sys->i_sub_last = val.i_int;
119
120     p_sys->p_es_audio = NULL;
121     p_sys->p_es_video = NULL;
122     p_sys->p_es_sub   = NULL;
123
124     return out;
125 }
126
127 /**
128  * Deletes an es_out structure
129  *
130  * \param out  the es_out structure to destroy
131  * \return nothing
132  */
133 void input_EsOutDelete( es_out_t *out )
134 {
135     es_out_sys_t *p_sys = out->p_sys;
136     int i;
137
138     for( i = 0; i < p_sys->i_es; i++ )
139     {
140         free( p_sys->es[i] );
141     }
142     if( p_sys->es )
143     {
144         free( p_sys->es );
145     }
146     free( p_sys );
147     free( out );
148 }
149
150 /**
151  * Add a program
152  *
153  * \param out the es_out
154  * \param i_group ...
155  * \return a program descriptor for the new program
156  */
157 static pgrm_descriptor_t *EsOutAddProgram( es_out_t *out, int i_group )
158 {
159     input_thread_t    *p_input = out->p_sys->p_input;
160     pgrm_descriptor_t *p_prgm;
161     es_descriptor_t   *p_pmt;
162
163     /* FIXME we should use a object variable but a lot of place in src/input
164      * have to be changed */
165     int               i_select = config_GetInt( p_input, "program" );
166
167     /* create it */
168     p_prgm = input_AddProgram( p_input, i_group, 0 );
169
170     /* XXX welcome to kludge, add a dummy es, if you want to understand
171      * why have a look at input_SetProgram. Basicaly, it assume the first
172      * es to be the PMT, how that is stupid, nevertheless it is needed for
173      * the old ts demuxer */
174     p_pmt = input_AddES( p_input, p_prgm, 0, UNKNOWN_ES, NULL, 0 );
175     p_pmt->i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
176
177     /* Select i_select or the first by default */
178     if( p_input->stream.p_selected_program == NULL &&
179         ( i_select <= 0 || i_select == i_group ) )
180     {
181         p_input->stream.p_selected_program = p_prgm;
182     }
183
184     return p_prgm;
185 }
186
187 /**
188  * Select an ES given the current mode
189  * XXX: you need to take a the lock before (stream.stream_lock)
190  *
191  * \param out The es_out structure
192  * \param es es_out_id structure
193  * \param b_force ...
194  * \return nothing
195  */
196 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
197 {
198     es_out_sys_t      *p_sys = out->p_sys;
199     input_thread_t    *p_input = p_sys->p_input;
200
201     int i_cat = es->p_es->i_cat;
202
203     if( !p_sys->b_active ||
204         ( !b_force && es->p_es->fmt.i_priority < 0 ) )
205     {
206         return;
207     }
208
209     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
210     {
211         if( !es->p_es->p_dec )
212         {
213             input_SelectES( p_input, es->p_es );
214         }
215     }
216     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
217     {
218         int i_wanted  = -1;
219
220         if( es->p_es->p_pgrm != NULL &&
221             es->p_es->p_pgrm != p_input->stream.p_selected_program )
222         {
223             return;
224         }
225
226         if( i_cat == AUDIO_ES )
227         {
228             if( p_sys->p_es_audio &&
229                 p_sys->p_es_audio->p_es->fmt.i_priority >=
230                     es->p_es->fmt.i_priority )
231             {
232                 return;
233             }
234             i_wanted  = p_sys->i_audio_last >= 0 ?
235                             p_sys->i_audio_last : es->i_channel;
236         }
237         else if( i_cat == SPU_ES )
238         {
239             if( p_sys->p_es_sub &&
240                 p_sys->p_es_sub->p_es->fmt.i_priority >=
241                     es->p_es->fmt.i_priority )
242             {
243                 return;
244             }
245             i_wanted  = p_sys->i_sub_last;
246         }
247         else if( i_cat == VIDEO_ES )
248         {
249             i_wanted  = es->i_channel;
250         }
251
252         if( i_wanted == es->i_channel && es->p_es->p_dec == NULL )
253         {
254             input_SelectES( p_input, es->p_es );
255         }
256     }
257
258     /* FIXME TODO handle priority here */
259     if( es->p_es->p_dec )
260     {
261         if( i_cat == AUDIO_ES )
262         {
263             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
264                 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
265             {
266                 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
267             }
268             p_sys->p_es_audio = es;
269         }
270         else if( i_cat == SPU_ES )
271         {
272             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
273                 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
274             {
275                 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
276             }
277             p_sys->p_es_sub = es;
278         }
279         else if( i_cat == VIDEO_ES )
280         {
281             p_sys->p_es_video = es;
282         }
283     }
284 }
285
286 /**
287  * Add an es_out
288  *
289  * \param out the es_out to add
290  * \param fmt the es_format of the es_out
291  * \return an es_out id
292  */
293 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
294 {
295     es_out_sys_t      *p_sys = out->p_sys;
296     playlist_t        *p_playlist = NULL;
297     input_thread_t    *p_input = p_sys->p_input;
298     es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
299     pgrm_descriptor_t *p_prgm = NULL;
300     char              psz_cat[sizeof( _("Stream ") ) + 10];
301     input_info_category_t *p_cat;
302
303     vlc_mutex_lock( &p_input->stream.stream_lock );
304     if( fmt->i_group >= 0 )
305     {
306         /* search program */
307         p_prgm = input_FindProgram( p_input, fmt->i_group );
308
309         if( p_prgm == NULL )
310         {
311             /* Create it */
312             p_prgm = EsOutAddProgram( out, fmt->i_group );
313         }
314     }
315     if( fmt->i_id < 0 )
316     {
317         fmt->i_id = out->p_sys->i_id - 1;
318     }
319
320     es->p_es = input_AddES( p_input,
321                             p_prgm,
322                             fmt->i_id + 1,
323                             fmt->i_cat,
324                             fmt->psz_language, 0 );
325     es->p_es->i_stream_id = fmt->i_id;
326     es->p_es->i_fourcc = fmt->i_codec;
327
328     switch( fmt->i_cat )
329     {
330         case AUDIO_ES:
331         {
332             WAVEFORMATEX *p_wf =
333                 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
334
335             p_wf->wFormatTag        = WAVE_FORMAT_UNKNOWN;
336             p_wf->nChannels         = fmt->audio.i_channels;
337             p_wf->nSamplesPerSec    = fmt->audio.i_rate;
338             p_wf->nAvgBytesPerSec   = fmt->i_bitrate / 8;
339             p_wf->nBlockAlign       = fmt->audio.i_blockalign;
340             p_wf->wBitsPerSample    = fmt->audio.i_bitspersample;
341             p_wf->cbSize            = fmt->i_extra;
342             if( fmt->i_extra > 0 )
343             {
344                 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
345             }
346             es->p_es->p_waveformatex = p_wf;
347
348             es->i_channel = p_sys->i_audio;
349             break;
350         }
351         case VIDEO_ES:
352         {
353             BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
354                                               fmt->i_extra );
355             p_bih->biSize           = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
356             p_bih->biWidth          = fmt->video.i_width;
357             p_bih->biHeight         = fmt->video.i_height;
358             p_bih->biPlanes         = 1;
359             p_bih->biBitCount       = 24;
360             p_bih->biCompression    = fmt->i_codec;
361             p_bih->biSizeImage      = fmt->video.i_width *
362                                           fmt->video.i_height;
363             p_bih->biXPelsPerMeter  = 0;
364             p_bih->biYPelsPerMeter  = 0;
365             p_bih->biClrUsed        = 0;
366             p_bih->biClrImportant   = 0;
367
368             if( fmt->i_extra > 0 )
369             {
370                 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
371             }
372             es->p_es->p_bitmapinfoheader = p_bih;
373
374             es->i_channel = p_sys->i_video;
375             break;
376         }
377         case SPU_ES:
378         {
379             subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
380             memset( p_sub, 0, sizeof( subtitle_data_t ) );
381             if( fmt->i_extra > 0 )
382             {
383                 p_sub->psz_header = malloc( fmt->i_extra  + 1 );
384                 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
385                 /* just to be sure */
386                 ((uint8_t*)fmt->p_extra)[fmt->i_extra] = '\0';
387             }
388             /* FIXME beuuuuuurk */
389             es->p_es->p_demux_data = p_sub;
390
391             es->i_channel = p_sys->i_sub;
392             break;
393         }
394
395         default:
396             es->i_channel = 0;
397             break;
398     }
399
400     sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
401     /* Get a category and the playlist */
402     if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) &&
403         ( p_playlist = (playlist_t *)vlc_object_find( p_input,
404                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE ) ) )
405     {
406         /* Add information */
407         switch( fmt->i_cat )
408         {
409             case AUDIO_ES:
410                 if( fmt->psz_description )
411                 {
412                     input_AddInfo( p_cat, _("Description"), "%s",
413                                    fmt->psz_description );
414                     playlist_AddInfo( p_playlist, -1, psz_cat,
415                                      _("Description"), "%s",
416                                      fmt->psz_description );
417                 }
418                 input_AddInfo( p_cat, _("Codec"), "%.4s",
419                                (char*)&fmt->i_codec );
420                 playlist_AddInfo( p_playlist, -1, psz_cat,
421                                   _("Codec"),"%.4s",(char*)&fmt->i_codec );
422
423                 input_AddInfo( p_cat, _("Type"), _("Audio") );
424                 playlist_AddInfo( p_playlist, -1, psz_cat,
425                                  _("Type"), _("Audio") );
426
427                 if( fmt->audio.i_channels > 0 )
428                 {
429                     input_AddInfo( p_cat, _("Channels"), "%d",
430                                    fmt->audio.i_channels );
431                     playlist_AddInfo( p_playlist, -1, psz_cat,
432                                       _("Channels"), "%d",                                                            fmt->audio.i_channels );
433                 }
434                 if( fmt->psz_language )
435                 {
436                     input_AddInfo( p_cat, _("Language"), "%s",
437                                    fmt->psz_language );
438                     playlist_AddInfo( p_playlist, -1, psz_cat,
439                                      _("Language"), "%s",
440                                      fmt->psz_language );
441                 }
442                 if( fmt->audio.i_rate > 0 )
443                 {
444                     input_AddInfo( p_cat, _("Sample rate"), _("%d Hz"),
445                                    fmt->audio.i_rate );
446                     playlist_AddInfo( p_playlist, -1, psz_cat,
447                                      _("Sample rate"), _("%d Hz"),
448                                       fmt->audio.i_rate );
449                 }
450                 if( fmt->i_bitrate > 0 )
451                 {
452                     input_AddInfo( p_cat, _("Bitrate"), _("%d bps"),
453                                    fmt->i_bitrate );
454                     playlist_AddInfo( p_playlist, -1, psz_cat,
455                                     _("Bitrate"), _("%d bps"),
456                                      fmt->i_bitrate );
457                 }
458                 if( fmt->audio.i_bitspersample )
459                 {
460                     input_AddInfo( p_cat, _("Bits per sample"), "%d",
461                                    fmt->audio.i_bitspersample );
462                     playlist_AddInfo( p_playlist, -1, psz_cat,
463                                      _("Bits per sample"), "%d",
464                                      fmt->audio.i_bitspersample );
465                 }
466                 break;
467             case VIDEO_ES:
468                 if( fmt->psz_description )
469                 {
470                     input_AddInfo( p_cat, _("Description"), "%s",
471                                    fmt->psz_description );
472                     playlist_AddInfo( p_playlist, -1, psz_cat,
473                                      _("Description"), "%s",
474                                      fmt->psz_description );
475                 }
476                 input_AddInfo( p_cat, _("Type"), _("Video") );
477                 playlist_AddInfo( p_playlist, -1, psz_cat,
478                                 _("Type"), _("Video") );
479
480                 input_AddInfo( p_cat, _("Codec"), "%.4s",
481                                (char*)&fmt->i_codec );
482                 playlist_AddInfo( p_playlist, -1, psz_cat,
483                                  _("Codec"), "%.4s",
484                                  (char*)&fmt->i_codec );
485
486                 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
487                 {
488                     input_AddInfo( p_cat, _("Resolution"), "%dx%d",
489                                    fmt->video.i_width, fmt->video.i_height );
490                     playlist_AddInfo( p_playlist, -1, psz_cat,
491                                     _("Resolution"), "%dx%d",
492                                     fmt->video.i_width, fmt->video.i_height );
493                 }
494                 if( fmt->video.i_visible_width > 0 &&
495                     fmt->video.i_visible_height > 0 )
496                 {
497                     input_AddInfo( p_cat, _("Display resolution"), "%dx%d",
498                                    fmt->video.i_visible_width,
499                                    fmt->video.i_visible_height);
500                      playlist_AddInfo( p_playlist, -1, psz_cat,
501                                        _("Display resolution"), "%dx%d",
502                                        fmt->video.i_visible_width,
503                                        fmt->video.i_visible_height);
504                 }
505                 break;
506             case SPU_ES:
507                 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
508                 playlist_AddInfo( p_playlist, -1, psz_cat,
509                                    _("Type"), _("Subtitle") );
510                 input_AddInfo( p_cat, _("Codec"), "%.4s",
511                                (char*)&fmt->i_codec );
512                 playlist_AddInfo( p_playlist, -1, psz_cat,
513                                  _("Codec"), "%.4s",
514                                  (char*)&fmt->i_codec );
515                 break;
516             default:
517
518                 break;
519         }
520         if( p_playlist ) vlc_object_release( p_playlist );
521     }
522
523     /* Apply mode
524      * XXX change that when we do group too */
525     if( 1 )
526     {
527         EsOutSelect( out, es, VLC_FALSE );
528     }
529
530     vlc_mutex_unlock( &p_input->stream.stream_lock );
531
532     es->p_es->fmt = *fmt;
533
534     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
535     p_sys->i_id++;  /* always incremented */
536     switch( fmt->i_cat )
537     {
538         case AUDIO_ES:
539             p_sys->i_audio++;
540             break;
541         case SPU_ES:
542             p_sys->i_sub++;
543             break;
544         case VIDEO_ES:
545             p_sys->i_video++;
546             break;
547     }
548
549     return es;
550 }
551
552 /**
553  * Send a block for the given es_out
554  *
555  * \param out the es_out to send from
556  * \param es the es_out_id
557  * \param p_block the data block to send
558  */
559 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
560 {
561     es_out_sys_t *p_sys = out->p_sys;
562
563     if( p_sys->b_pcr_set )
564     {
565         pgrm_descriptor_t *p_pgrm = es->p_es->p_pgrm;
566         input_thread_t    *p_input = p_sys->p_input;
567
568         if( p_pgrm == NULL )
569         {
570             p_pgrm = p_sys->p_input->stream.p_selected_program;
571         }
572
573         if( p_block->i_dts > 0 && p_pgrm )
574         {
575             p_block->i_dts =
576                 input_ClockGetTS( p_input, p_pgrm, p_block->i_dts * 9 / 100 );
577         }
578         if( p_block->i_pts > 0 && p_pgrm )
579         {
580             p_block->i_pts =
581                 input_ClockGetTS( p_input, p_pgrm, p_block->i_pts * 9 / 100 );
582         }
583     }
584
585     vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
586     p_block->i_rate = out->p_sys->p_input->stream.control.i_rate;
587     if( es->p_es->p_dec &&
588         (es->p_es->i_cat!=AUDIO_ES || !p_sys->p_input->stream.control.b_mute) )
589     {
590         input_DecodeBlock( es->p_es->p_dec, p_block );
591     }
592     else
593     {
594         block_Release( p_block );
595     }
596     vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
597
598     return VLC_SUCCESS;
599 }
600
601 /*****************************************************************************
602  * EsOutDel:
603  *****************************************************************************/
604 static void EsOutDel( es_out_t *out, es_out_id_t *es )
605 {
606     es_out_sys_t *p_sys = out->p_sys;
607
608     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
609
610     switch( es->p_es->i_cat )
611     {
612         case AUDIO_ES:
613             p_sys->i_audio--;
614             break;
615         case SPU_ES:
616             p_sys->i_sub--;
617             break;
618         case VIDEO_ES:
619             p_sys->i_video--;
620             break;
621     }
622
623     /* We don't try to reselect */
624     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
625     if( es->p_es->p_dec )
626     {
627         input_UnselectES( p_sys->p_input, es->p_es );
628     }
629
630     if( es->p_es->p_waveformatex )
631     {
632         free( es->p_es->p_waveformatex );
633         es->p_es->p_waveformatex = NULL;
634     }
635     if( es->p_es->p_bitmapinfoheader )
636     {
637         free( es->p_es->p_bitmapinfoheader );
638         es->p_es->p_bitmapinfoheader = NULL;
639     }
640     input_DelES( p_sys->p_input, es->p_es );
641
642     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
643     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
644     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
645
646     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
647
648     free( es );
649 }
650
651 /**
652  * Control query handler
653  *
654  * \param out the es_out to control
655  * \param i_query A es_out query as defined in include/ninput.h
656  * \param args a variable list of arguments for the query
657  * \return VLC_SUCCESS or an error code
658  */
659 static int EsOutControl( es_out_t *out, int i_query, va_list args )
660 {
661     es_out_sys_t *p_sys = out->p_sys;
662     vlc_bool_t  b, *pb;
663     int         i, *pi;
664
665     es_out_id_t *es;
666
667     switch( i_query )
668     {
669         case ES_OUT_SET_ES_STATE:
670             vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
671             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
672             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
673             if( b && es->p_es->p_dec == NULL )
674             {
675                 input_SelectES( p_sys->p_input, es->p_es );
676                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
677                 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
678             }
679             else if( !b && es->p_es->p_dec )
680             {
681                 input_UnselectES( p_sys->p_input, es->p_es );
682                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
683                 return VLC_SUCCESS;
684             }
685             vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
686             return VLC_SUCCESS;
687
688         case ES_OUT_GET_ES_STATE:
689             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
690             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
691
692             *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
693             return VLC_SUCCESS;
694
695         case ES_OUT_SET_ACTIVE:
696         {
697             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
698             p_sys->b_active = b;
699
700             if( b )
701             {
702                 vlc_value_t val;
703                 val.b_bool = VLC_TRUE;
704                 var_Set( p_sys->p_input, "intf-change", val );
705             }
706             return VLC_SUCCESS;
707         }
708
709         case ES_OUT_GET_ACTIVE:
710             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
711             *pb = p_sys->b_active;
712             return VLC_SUCCESS;
713
714         case ES_OUT_SET_MODE:
715             i = (int) va_arg( args, int );
716             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
717                 i == ES_OUT_MODE_AUTO )
718             {
719                 vlc_value_t val;
720
721                 p_sys->i_mode = i;
722
723                 /* Reapply policy mode */
724                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
725                 for( i = 0; i < p_sys->i_es; i++ )
726                 {
727                     if( p_sys->es[i]->p_es->p_dec )
728                     {
729                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
730                     }
731                 }
732                 for( i = 0; i < p_sys->i_es; i++ )
733                 {
734                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
735                 }
736                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
737
738                 val.b_bool = VLC_TRUE;
739                 var_Set( p_sys->p_input, "intf-change", val );
740
741                 return VLC_SUCCESS;
742             }
743             return VLC_EGENERIC;
744
745         case ES_OUT_GET_MODE:
746             pi = (int*) va_arg( args, int* );
747             *pi = p_sys->i_mode;
748             return VLC_SUCCESS;
749
750         case ES_OUT_SET_ES:
751             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
752             if( es == NULL )
753             {
754                 for( i = 0; i < p_sys->i_es; i++ )
755                 {
756                     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
757                     if( p_sys->es[i]->p_es->p_dec )
758                     {
759                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
760                     }
761                     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
762                 }
763             }
764             else
765             {
766                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
767                 EsOutSelect( out, es, VLC_TRUE );
768                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
769             }
770             return VLC_SUCCESS;
771
772         case ES_OUT_SET_PCR:
773         case ES_OUT_SET_GROUP_PCR:
774         {
775             pgrm_descriptor_t *p_prgm = NULL;
776             int64_t           i_pcr;
777
778             if( i_query == ES_OUT_SET_PCR )
779             {
780                 p_prgm = p_sys->p_input->stream.p_selected_program;
781             }
782             else
783             {
784                 int i_group = (int)va_arg( args, int );
785                 p_prgm = input_FindProgram( p_sys->p_input, i_group );
786                 if( p_prgm == NULL )
787                 {
788                     /* we create the requested program */
789                     p_prgm = EsOutAddProgram( out, i_group );
790                 }
791             }
792             i_pcr   = (int64_t)va_arg( args, int64_t );
793
794             /* search program */
795             if( p_prgm )
796             {
797                 input_ClockManageRef( p_sys->p_input, p_prgm, i_pcr * 9 / 100);
798             }
799             p_sys->b_pcr_set = VLC_TRUE;
800             return VLC_SUCCESS;
801         }
802
803         case ES_OUT_RESET_PCR:
804             for( i = 0; i < p_sys->p_input->stream.i_pgrm_number; i++ )
805             {
806                 p_sys->p_input->stream.pp_programs[i]->i_synchro_state = SYNCHRO_REINIT;
807             }
808             p_sys->b_pcr_set = VLC_TRUE;
809             return VLC_SUCCESS;
810
811         default:
812             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
813             return VLC_EGENERIC;
814     }
815 }