]> git.sesse.net Git - vlc/blob - src/input/es_out.c
* es_out.c: better support for multiple program. (bugfixes and a dirty
[vlc] / 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.17 2004/01/18 04:53:57 fenrir 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  * input_EsOutNew:
83  *****************************************************************************/
84 es_out_t *input_EsOutNew( input_thread_t *p_input )
85 {
86     es_out_t     *out = malloc( sizeof( es_out_t ) );
87     es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
88     vlc_value_t  val;
89
90     out->pf_add     = EsOutAdd;
91     out->pf_send    = EsOutSend;
92     out->pf_del     = EsOutDel;
93     out->pf_control = EsOutControl;
94     out->p_sys      = p_sys;
95
96     p_sys->p_input = p_input;
97     p_sys->b_pcr_set = VLC_FALSE;
98
99     p_sys->b_active = VLC_FALSE;
100     p_sys->i_mode   = ES_OUT_MODE_AUTO;
101
102     p_sys->i_id    = 1;
103
104     p_sys->i_es    = 0;
105     p_sys->es      = NULL;
106
107     p_sys->i_audio = 0;
108     p_sys->i_video = 0;
109     p_sys->i_sub   = 0;
110
111     var_Get( p_input, "audio-channel", &val );
112     p_sys->i_audio_last = val.i_int;
113
114     var_Get( p_input, "spu-channel", &val );
115     p_sys->i_sub_last = val.i_int;
116
117     p_sys->p_es_audio = NULL;
118     p_sys->p_es_video = NULL;
119     p_sys->p_es_sub   = NULL;
120
121     return out;
122 }
123
124 /*****************************************************************************
125  * input_EsOutDelete:
126  *****************************************************************************/
127 void input_EsOutDelete( es_out_t *out )
128 {
129     es_out_sys_t *p_sys = out->p_sys;
130     int i;
131
132     for( i = 0; i < p_sys->i_es; i++ )
133     {
134         free( p_sys->es[i] );
135     }
136     if( p_sys->es )
137     {
138         free( p_sys->es );
139     }
140     free( p_sys );
141     free( out );
142 }
143
144 /*****************************************************************************
145  * EsOutSelect: Select an ES given the current mode
146  * XXX: you need to take a the lock before (stream.stream_lock)
147  *****************************************************************************/
148 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
149 {
150     es_out_sys_t      *p_sys = out->p_sys;
151     input_thread_t    *p_input = p_sys->p_input;
152
153     int i_cat = es->p_es->i_cat;
154
155     if( !p_sys->b_active ||
156         ( !b_force && es->p_es->fmt.i_priority < 0 ) )
157     {
158         return;
159     }
160
161     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
162     {
163         if( !es->p_es->p_dec )
164         {
165             input_SelectES( p_input, es->p_es );
166         }
167     }
168     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
169     {
170         int i_wanted  = -1;
171
172         if( es->p_es->p_pgrm != NULL &&
173             es->p_es->p_pgrm != p_input->stream.p_selected_program )
174         {
175             return;
176         }
177
178         if( i_cat == AUDIO_ES )
179         {
180             if( p_sys->p_es_audio &&
181                 p_sys->p_es_audio->p_es->fmt.i_priority >=
182                     es->p_es->fmt.i_priority )
183             {
184                 return;
185             }
186             i_wanted  = p_sys->i_audio_last >= 0 ?
187                             p_sys->i_audio_last : es->i_channel;
188         }
189         else if( i_cat == SPU_ES )
190         {
191             if( p_sys->p_es_sub &&
192                 p_sys->p_es_sub->p_es->fmt.i_priority >=
193                     es->p_es->fmt.i_priority )
194             {
195                 return;
196             }
197             i_wanted  = p_sys->i_sub_last;
198         }
199         else if( i_cat == VIDEO_ES )
200         {
201             i_wanted  = es->i_channel;
202         }
203
204         if( i_wanted == es->i_channel && es->p_es->p_dec == NULL )
205         {
206             input_SelectES( p_input, es->p_es );
207         }
208     }
209
210     /* FIXME TODO handle priority here */
211     if( es->p_es->p_dec )
212     {
213         if( i_cat == AUDIO_ES )
214         {
215             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
216                 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
217             {
218                 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
219             }
220             p_sys->p_es_audio = es;
221         }
222         else if( i_cat == SPU_ES )
223         {
224             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
225                 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
226             {
227                 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
228             }
229             p_sys->p_es_sub = es;
230         }
231         else if( i_cat == VIDEO_ES )
232         {
233             p_sys->p_es_video = es;
234         }
235     }
236 }
237
238 /*****************************************************************************
239  * EsOutAdd:
240  *****************************************************************************/
241 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
242 {
243     es_out_sys_t      *p_sys = out->p_sys;
244     playlist_t        *p_playlist = NULL;
245     input_thread_t    *p_input = p_sys->p_input;
246     es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
247     pgrm_descriptor_t *p_prgm = NULL;
248     char              psz_cat[sizeof( "Stream " ) + 10];
249     input_info_category_t *p_cat;
250
251     vlc_mutex_lock( &p_input->stream.stream_lock );
252     if( fmt->i_group >= 0 )
253     {
254         /* search program */
255         p_prgm = input_FindProgram( p_input, fmt->i_group );
256
257         if( p_prgm == NULL )
258         {
259             /* create it */
260             p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
261
262             /* XXX welcome to kludge, add a dummy es, if you want to understand
263              * why have a look at input_SetProgram. Basicaly, it assume the first
264              * es to be the PMT, how that is stupide, nevertheless it is needed for
265              * the old ts demuxer */
266             input_AddES( p_input, p_prgm, 0, UNKNOWN_ES, NULL, 0 );
267
268             /* Select the first by default */
269             if( p_input->stream.p_selected_program == NULL )
270             {
271                 p_input->stream.p_selected_program = p_prgm;
272             }
273         }
274     }
275
276     es->p_es = input_AddES( p_input,
277                             p_prgm,
278                             out->p_sys->i_id,
279                             fmt->i_cat,
280                             fmt->psz_language, 0 );
281     es->p_es->i_stream_id = out->p_sys->i_id;
282     es->p_es->i_fourcc = fmt->i_codec;
283
284     switch( fmt->i_cat )
285     {
286         case AUDIO_ES:
287         {
288             WAVEFORMATEX *p_wf =
289                 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
290
291             p_wf->wFormatTag        = WAVE_FORMAT_UNKNOWN;
292             p_wf->nChannels         = fmt->audio.i_channels;
293             p_wf->nSamplesPerSec    = fmt->audio.i_rate;
294             p_wf->nAvgBytesPerSec   = fmt->i_bitrate / 8;
295             p_wf->nBlockAlign       = fmt->audio.i_blockalign;
296             p_wf->wBitsPerSample    = fmt->audio.i_bitspersample;
297             p_wf->cbSize            = fmt->i_extra;
298             if( fmt->i_extra > 0 )
299             {
300                 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
301             }
302             es->p_es->p_waveformatex = p_wf;
303
304             es->i_channel = p_sys->i_audio;
305             break;
306         }
307         case VIDEO_ES:
308         {
309             BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
310                                               fmt->i_extra );
311             p_bih->biSize           = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
312             p_bih->biWidth          = fmt->video.i_width;
313             p_bih->biHeight         = fmt->video.i_height;
314             p_bih->biPlanes         = 1;
315             p_bih->biBitCount       = 24;
316             p_bih->biCompression    = fmt->i_codec;
317             p_bih->biSizeImage      = fmt->video.i_width *
318                                           fmt->video.i_height;
319             p_bih->biXPelsPerMeter  = 0;
320             p_bih->biYPelsPerMeter  = 0;
321             p_bih->biClrUsed        = 0;
322             p_bih->biClrImportant   = 0;
323
324             if( fmt->i_extra > 0 )
325             {
326                 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
327             }
328             es->p_es->p_bitmapinfoheader = p_bih;
329
330             es->i_channel = p_sys->i_video;
331             break;
332         }
333         case SPU_ES:
334         {
335             subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
336             memset( p_sub, 0, sizeof( subtitle_data_t ) );
337             if( fmt->i_extra > 0 )
338             {
339                 p_sub->psz_header = malloc( fmt->i_extra  + 1 );
340                 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
341                 /* just to be sure */
342                 ((uint8_t*)fmt->p_extra)[fmt->i_extra] = '\0';
343             }
344             /* FIXME beuuuuuurk */
345             es->p_es->p_demux_data = p_sub;
346
347             es->i_channel = p_sys->i_sub;
348             break;
349         }
350
351         default:
352             es->i_channel = 0;
353             break;
354     }
355
356     sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
357     /* Get a category and the playlist */
358     if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) &&
359         ( p_playlist = (playlist_t *)vlc_object_find( p_input,
360                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE ) ) )
361     {
362         /* Add information */
363         switch( fmt->i_cat )
364         {
365             case AUDIO_ES:
366                 if( fmt->psz_description )
367                 {
368                     input_AddInfo( p_cat, _("Description"), "%s",
369                                    fmt->psz_description );
370                     playlist_AddInfo( p_playlist, -1, psz_cat,
371                                      _("Description"), "%s",
372                                      fmt->psz_description );
373                 }
374                 input_AddInfo( p_cat, _("Codec"), "%.4s",
375                                (char*)&fmt->i_codec );
376                 playlist_AddInfo( p_playlist, -1, psz_cat,
377                                   _("Codec"),"%.4s",(char*)&fmt->i_codec );
378
379                 input_AddInfo( p_cat, _("Type"), _("Audio") );
380                 playlist_AddInfo( p_playlist, -1, psz_cat,
381                                  _("Type"), _("Audio") );
382
383                 if( fmt->audio.i_channels > 0 )
384                 {
385                     input_AddInfo( p_cat, _("Channels"), "%d",
386                                    fmt->audio.i_channels );
387                     playlist_AddInfo( p_playlist, -1, psz_cat,
388                                       _("Channels"), "%d",                                                            fmt->audio.i_channels );
389                 }
390                 if( fmt->psz_language )
391                 {
392                     input_AddInfo( p_cat, _("Language"), "%s",
393                                    fmt->psz_language );
394                     playlist_AddInfo( p_playlist, -1, psz_cat,
395                                      _("Language"), "%s",
396                                      fmt->psz_language );
397                 }
398                 if( fmt->audio.i_rate > 0 )
399                 {
400                     input_AddInfo( p_cat, _("Sample rate"), _("%d Hz"),
401                                    fmt->audio.i_rate );
402                     playlist_AddInfo( p_playlist, -1, psz_cat,
403                                      _("Sample rate"), _("%d Hz"),
404                                       fmt->audio.i_rate );
405                 }
406                 if( fmt->i_bitrate > 0 )
407                 {
408                     input_AddInfo( p_cat, _("Bitrate"), _("%d bps"),
409                                    fmt->i_bitrate );
410                     playlist_AddInfo( p_playlist, -1, psz_cat,
411                                     _("Bitrate"), _("%d bps"),
412                                      fmt->i_bitrate );
413                 }
414                 if( fmt->audio.i_bitspersample )
415                 {
416                     input_AddInfo( p_cat, _("Bits per sample"), "%d",
417                                    fmt->audio.i_bitspersample );
418                     playlist_AddInfo( p_playlist, -1, psz_cat,
419                                      _("Bits per sample"), "%d",
420                                      fmt->audio.i_bitspersample );
421                 }
422                 break;
423             case VIDEO_ES:
424                 if( fmt->psz_description )
425                 {
426                     input_AddInfo( p_cat, _("Description"), "%s",
427                                    fmt->psz_description );
428                     playlist_AddInfo( p_playlist, -1, psz_cat,
429                                      _("Description"), "%s",
430                                      fmt->psz_description );
431                 }
432                 input_AddInfo( p_cat, _("Type"), _("Video") );
433                 playlist_AddInfo( p_playlist, -1, psz_cat,
434                                 _("Type"), _("Video") );
435
436                 input_AddInfo( p_cat, _("Codec"), "%.4s",
437                                (char*)&fmt->i_codec );
438                 playlist_AddInfo( p_playlist, -1, psz_cat,
439                                  _("Codec"), "%.4s",
440                                  (char*)&fmt->i_codec );
441
442                 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
443                 {
444                     input_AddInfo( p_cat, _("Resolution"), "%dx%d",
445                                    fmt->video.i_width, fmt->video.i_height );
446                     playlist_AddInfo( p_playlist, -1, psz_cat,
447                                     _("Resolution"), "%dx%d",
448                                     fmt->video.i_width, fmt->video.i_height );
449                 }
450                 if( fmt->video.i_visible_width > 0 &&
451                     fmt->video.i_visible_height > 0 )
452                 {
453                     input_AddInfo( p_cat, _("Display resolution"), "%dx%d",
454                                    fmt->video.i_visible_width,
455                                    fmt->video.i_visible_height);
456                      playlist_AddInfo( p_playlist, -1, psz_cat,
457                                        _("Display resolution"), "%dx%d",
458                                        fmt->video.i_visible_width,
459                                        fmt->video.i_visible_height);
460                 }
461                 break;
462             case SPU_ES:
463                 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
464                 playlist_AddInfo( p_playlist, -1, psz_cat,
465                                    _("Type"), _("Subtitle") );
466                 input_AddInfo( p_cat, _("Codec"), "%.4s",
467                                (char*)&fmt->i_codec );
468                 playlist_AddInfo( p_playlist, -1, psz_cat,
469                                  _("Codec"), "%.4s",
470                                  (char*)&fmt->i_codec );
471                 break;
472             default:
473
474                 break;
475         }
476         if( p_playlist ) vlc_object_release( p_playlist );
477     }
478
479     /* Apply mode
480      * XXX change that when we do group too */
481     if( 1 )
482     {
483         EsOutSelect( out, es, VLC_FALSE );
484     }
485
486     vlc_mutex_unlock( &p_input->stream.stream_lock );
487
488     es->p_es->fmt = *fmt;
489
490     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
491     p_sys->i_id++;  /* always incremented */
492     switch( fmt->i_cat )
493     {
494         case AUDIO_ES:
495             p_sys->i_audio++;
496             break;
497         case SPU_ES:
498             p_sys->i_sub++;
499             break;
500         case VIDEO_ES:
501             p_sys->i_video++;
502             break;
503     }
504
505     return es;
506 }
507
508 /*****************************************************************************
509  * EsOutSend:
510  *****************************************************************************/
511 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
512 {
513     es_out_sys_t *p_sys = out->p_sys;
514
515     if( p_sys->b_pcr_set )
516     {
517         pgrm_descriptor_t *p_pgrm = es->p_es->p_pgrm;
518         input_thread_t    *p_input = p_sys->p_input;
519
520         if( p_pgrm == NULL )
521         {
522             p_pgrm = p_sys->p_input->stream.p_selected_program;
523         }
524
525         if( p_block->i_dts > 0 && p_pgrm )
526         {
527             p_block->i_dts =
528                 input_ClockGetTS( p_input, p_pgrm, p_block->i_dts * 9 / 100 );
529         }
530         if( p_block->i_pts > 0 && p_pgrm )
531         {
532             p_block->i_pts =
533                 input_ClockGetTS( p_input, p_pgrm, p_block->i_pts * 9 / 100 );
534         }
535     }
536
537     vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
538     p_block->i_rate = out->p_sys->p_input->stream.control.i_rate;
539     if( es->p_es->p_dec &&
540         (es->p_es->i_cat!=AUDIO_ES || !p_sys->p_input->stream.control.b_mute) )
541     {
542         input_DecodeBlock( es->p_es->p_dec, p_block );
543     }
544     else
545     {
546         block_Release( p_block );
547     }
548     vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
549
550     return VLC_SUCCESS;
551 }
552
553 /*****************************************************************************
554  * EsOutDel:
555  *****************************************************************************/
556 static void EsOutDel( es_out_t *out, es_out_id_t *es )
557 {
558     es_out_sys_t *p_sys = out->p_sys;
559
560     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
561
562     switch( es->p_es->i_cat )
563     {
564         case AUDIO_ES:
565             p_sys->i_audio--;
566             break;
567         case SPU_ES:
568             p_sys->i_sub--;
569             break;
570         case VIDEO_ES:
571             p_sys->i_video--;
572             break;
573     }
574
575     /* We don't try to reselect */
576     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
577     if( es->p_es->p_dec )
578     {
579         input_UnselectES( p_sys->p_input, es->p_es );
580     }
581
582     if( es->p_es->p_waveformatex )
583     {
584         free( es->p_es->p_waveformatex );
585         es->p_es->p_waveformatex = NULL;
586     }
587     if( es->p_es->p_bitmapinfoheader )
588     {
589         free( es->p_es->p_bitmapinfoheader );
590         es->p_es->p_bitmapinfoheader = NULL;
591     }
592     input_DelES( p_sys->p_input, es->p_es );
593
594     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
595     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
596     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
597
598     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
599
600     free( es );
601 }
602
603 /*****************************************************************************
604  * EsOutControl:
605  *****************************************************************************/
606 static int EsOutControl( es_out_t *out, int i_query, va_list args )
607 {
608     es_out_sys_t *p_sys = out->p_sys;
609     vlc_bool_t  b, *pb;
610     int         i, *pi;
611
612     es_out_id_t *es;
613
614     switch( i_query )
615     {
616         case ES_OUT_SET_ES_STATE:
617             vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
618             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
619             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
620             if( b && es->p_es->p_dec == NULL )
621             {
622                 input_SelectES( p_sys->p_input, es->p_es );
623                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
624                 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
625             }
626             else if( !b && es->p_es->p_dec )
627             {
628                 input_UnselectES( p_sys->p_input, es->p_es );
629                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
630                 return VLC_SUCCESS;
631             }
632             vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
633             return VLC_SUCCESS;
634
635         case ES_OUT_GET_ES_STATE:
636             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
637             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
638
639             *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
640             return VLC_SUCCESS;
641
642         case ES_OUT_SET_ACTIVE:
643         {
644             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
645             p_sys->b_active = b;
646
647             if( b )
648             {
649                 vlc_value_t val;
650                 val.b_bool = VLC_TRUE;
651                 var_Set( p_sys->p_input, "intf-change", val );
652             }
653             return VLC_SUCCESS;
654         }
655
656         case ES_OUT_GET_ACTIVE:
657             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
658             *pb = p_sys->b_active;
659             return VLC_SUCCESS;
660
661         case ES_OUT_SET_MODE:
662             i = (int) va_arg( args, int );
663             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
664                 i == ES_OUT_MODE_AUTO )
665             {
666                 vlc_value_t val;
667
668                 p_sys->i_mode = i;
669
670                 /* Reapply policy mode */
671                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
672                 for( i = 0; i < p_sys->i_es; i++ )
673                 {
674                     if( p_sys->es[i]->p_es->p_dec )
675                     {
676                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
677                     }
678                 }
679                 for( i = 0; i < p_sys->i_es; i++ )
680                 {
681                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
682                 }
683                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
684
685                 val.b_bool = VLC_TRUE;
686                 var_Set( p_sys->p_input, "intf-change", val );
687
688                 return VLC_SUCCESS;
689             }
690             return VLC_EGENERIC;
691
692         case ES_OUT_GET_MODE:
693             pi = (int*) va_arg( args, int* );
694             *pi = p_sys->i_mode;
695             return VLC_SUCCESS;
696
697         case ES_OUT_SET_ES:
698             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
699             if( es == NULL )
700             {
701                 for( i = 0; i < p_sys->i_es; i++ )
702                 {
703                     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
704                     if( p_sys->es[i]->p_es->p_dec )
705                     {
706                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
707                     }
708                     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
709                 }
710             }
711             else
712             {
713                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
714                 EsOutSelect( out, es, VLC_TRUE );
715                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
716             }
717             return VLC_SUCCESS;
718
719         case ES_OUT_SET_PCR:
720         case ES_OUT_SET_GROUP_PCR:
721         {
722             pgrm_descriptor_t *p_prgm = NULL;
723             int64_t           i_pcr;
724
725             if( i_query == ES_OUT_SET_PCR )
726             {
727                 p_prgm = p_sys->p_input->stream.p_selected_program;
728             }
729             else
730             {
731                 int i_group = (int)va_arg( args, int );
732                 p_prgm = input_FindProgram( p_sys->p_input, i_group );
733             }
734             i_pcr   = (int64_t)va_arg( args, int64_t );
735
736             /* search program */
737             if( p_prgm )
738             {
739                 input_ClockManageRef( p_sys->p_input, p_prgm, i_pcr * 9 / 100);
740             }
741             p_sys->b_pcr_set = VLC_TRUE;
742             return VLC_SUCCESS;
743         }
744
745         case ES_OUT_RESET_PCR:
746             for( i = 0; i < p_sys->p_input->stream.i_pgrm_number; i++ )
747             {
748                 p_sys->p_input->stream.pp_programs[i]->i_synchro_state = SYNCHRO_REINIT;
749             }
750             p_sys->b_pcr_set = VLC_TRUE;
751             return VLC_SUCCESS;
752
753         default:
754             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
755             return VLC_EGENERIC;
756     }
757 }