]> git.sesse.net Git - vlc/blob - src/input/es_out.c
* all: added a i_id field in es_format_t.
[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.19 2004/01/19 18:15:29 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             es_descriptor_t *p_pmt;
260             /* create it */
261             p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
262
263             /* XXX welcome to kludge, add a dummy es, if you want to understand
264              * why have a look at input_SetProgram. Basicaly, it assume the first
265              * es to be the PMT, how that is stupide, nevertheless it is needed for
266              * the old ts demuxer */
267             p_pmt = input_AddES( p_input, p_prgm, 0, UNKNOWN_ES, NULL, 0 );
268             p_pmt->i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
269
270             /* Select the first by default */
271             if( p_input->stream.p_selected_program == NULL )
272             {
273                 p_input->stream.p_selected_program = p_prgm;
274             }
275         }
276     }
277     if( fmt->i_id < 0 )
278     {
279         fmt->i_id = out->p_sys->i_id - 1;
280     }
281
282     es->p_es = input_AddES( p_input,
283                             p_prgm,
284                             fmt->i_id + 1,
285                             fmt->i_cat,
286                             fmt->psz_language, 0 );
287     es->p_es->i_stream_id = fmt->i_id;
288     es->p_es->i_fourcc = fmt->i_codec;
289
290     switch( fmt->i_cat )
291     {
292         case AUDIO_ES:
293         {
294             WAVEFORMATEX *p_wf =
295                 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
296
297             p_wf->wFormatTag        = WAVE_FORMAT_UNKNOWN;
298             p_wf->nChannels         = fmt->audio.i_channels;
299             p_wf->nSamplesPerSec    = fmt->audio.i_rate;
300             p_wf->nAvgBytesPerSec   = fmt->i_bitrate / 8;
301             p_wf->nBlockAlign       = fmt->audio.i_blockalign;
302             p_wf->wBitsPerSample    = fmt->audio.i_bitspersample;
303             p_wf->cbSize            = fmt->i_extra;
304             if( fmt->i_extra > 0 )
305             {
306                 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
307             }
308             es->p_es->p_waveformatex = p_wf;
309
310             es->i_channel = p_sys->i_audio;
311             break;
312         }
313         case VIDEO_ES:
314         {
315             BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
316                                               fmt->i_extra );
317             p_bih->biSize           = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
318             p_bih->biWidth          = fmt->video.i_width;
319             p_bih->biHeight         = fmt->video.i_height;
320             p_bih->biPlanes         = 1;
321             p_bih->biBitCount       = 24;
322             p_bih->biCompression    = fmt->i_codec;
323             p_bih->biSizeImage      = fmt->video.i_width *
324                                           fmt->video.i_height;
325             p_bih->biXPelsPerMeter  = 0;
326             p_bih->biYPelsPerMeter  = 0;
327             p_bih->biClrUsed        = 0;
328             p_bih->biClrImportant   = 0;
329
330             if( fmt->i_extra > 0 )
331             {
332                 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
333             }
334             es->p_es->p_bitmapinfoheader = p_bih;
335
336             es->i_channel = p_sys->i_video;
337             break;
338         }
339         case SPU_ES:
340         {
341             subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
342             memset( p_sub, 0, sizeof( subtitle_data_t ) );
343             if( fmt->i_extra > 0 )
344             {
345                 p_sub->psz_header = malloc( fmt->i_extra  + 1 );
346                 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
347                 /* just to be sure */
348                 ((uint8_t*)fmt->p_extra)[fmt->i_extra] = '\0';
349             }
350             /* FIXME beuuuuuurk */
351             es->p_es->p_demux_data = p_sub;
352
353             es->i_channel = p_sys->i_sub;
354             break;
355         }
356
357         default:
358             es->i_channel = 0;
359             break;
360     }
361
362     sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
363     /* Get a category and the playlist */
364     if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) &&
365         ( p_playlist = (playlist_t *)vlc_object_find( p_input,
366                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE ) ) )
367     {
368         /* Add information */
369         switch( fmt->i_cat )
370         {
371             case AUDIO_ES:
372                 if( fmt->psz_description )
373                 {
374                     input_AddInfo( p_cat, _("Description"), "%s",
375                                    fmt->psz_description );
376                     playlist_AddInfo( p_playlist, -1, psz_cat,
377                                      _("Description"), "%s",
378                                      fmt->psz_description );
379                 }
380                 input_AddInfo( p_cat, _("Codec"), "%.4s",
381                                (char*)&fmt->i_codec );
382                 playlist_AddInfo( p_playlist, -1, psz_cat,
383                                   _("Codec"),"%.4s",(char*)&fmt->i_codec );
384
385                 input_AddInfo( p_cat, _("Type"), _("Audio") );
386                 playlist_AddInfo( p_playlist, -1, psz_cat,
387                                  _("Type"), _("Audio") );
388
389                 if( fmt->audio.i_channels > 0 )
390                 {
391                     input_AddInfo( p_cat, _("Channels"), "%d",
392                                    fmt->audio.i_channels );
393                     playlist_AddInfo( p_playlist, -1, psz_cat,
394                                       _("Channels"), "%d",                                                            fmt->audio.i_channels );
395                 }
396                 if( fmt->psz_language )
397                 {
398                     input_AddInfo( p_cat, _("Language"), "%s",
399                                    fmt->psz_language );
400                     playlist_AddInfo( p_playlist, -1, psz_cat,
401                                      _("Language"), "%s",
402                                      fmt->psz_language );
403                 }
404                 if( fmt->audio.i_rate > 0 )
405                 {
406                     input_AddInfo( p_cat, _("Sample rate"), _("%d Hz"),
407                                    fmt->audio.i_rate );
408                     playlist_AddInfo( p_playlist, -1, psz_cat,
409                                      _("Sample rate"), _("%d Hz"),
410                                       fmt->audio.i_rate );
411                 }
412                 if( fmt->i_bitrate > 0 )
413                 {
414                     input_AddInfo( p_cat, _("Bitrate"), _("%d bps"),
415                                    fmt->i_bitrate );
416                     playlist_AddInfo( p_playlist, -1, psz_cat,
417                                     _("Bitrate"), _("%d bps"),
418                                      fmt->i_bitrate );
419                 }
420                 if( fmt->audio.i_bitspersample )
421                 {
422                     input_AddInfo( p_cat, _("Bits per sample"), "%d",
423                                    fmt->audio.i_bitspersample );
424                     playlist_AddInfo( p_playlist, -1, psz_cat,
425                                      _("Bits per sample"), "%d",
426                                      fmt->audio.i_bitspersample );
427                 }
428                 break;
429             case VIDEO_ES:
430                 if( fmt->psz_description )
431                 {
432                     input_AddInfo( p_cat, _("Description"), "%s",
433                                    fmt->psz_description );
434                     playlist_AddInfo( p_playlist, -1, psz_cat,
435                                      _("Description"), "%s",
436                                      fmt->psz_description );
437                 }
438                 input_AddInfo( p_cat, _("Type"), _("Video") );
439                 playlist_AddInfo( p_playlist, -1, psz_cat,
440                                 _("Type"), _("Video") );
441
442                 input_AddInfo( p_cat, _("Codec"), "%.4s",
443                                (char*)&fmt->i_codec );
444                 playlist_AddInfo( p_playlist, -1, psz_cat,
445                                  _("Codec"), "%.4s",
446                                  (char*)&fmt->i_codec );
447
448                 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
449                 {
450                     input_AddInfo( p_cat, _("Resolution"), "%dx%d",
451                                    fmt->video.i_width, fmt->video.i_height );
452                     playlist_AddInfo( p_playlist, -1, psz_cat,
453                                     _("Resolution"), "%dx%d",
454                                     fmt->video.i_width, fmt->video.i_height );
455                 }
456                 if( fmt->video.i_visible_width > 0 &&
457                     fmt->video.i_visible_height > 0 )
458                 {
459                     input_AddInfo( p_cat, _("Display resolution"), "%dx%d",
460                                    fmt->video.i_visible_width,
461                                    fmt->video.i_visible_height);
462                      playlist_AddInfo( p_playlist, -1, psz_cat,
463                                        _("Display resolution"), "%dx%d",
464                                        fmt->video.i_visible_width,
465                                        fmt->video.i_visible_height);
466                 }
467                 break;
468             case SPU_ES:
469                 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
470                 playlist_AddInfo( p_playlist, -1, psz_cat,
471                                    _("Type"), _("Subtitle") );
472                 input_AddInfo( p_cat, _("Codec"), "%.4s",
473                                (char*)&fmt->i_codec );
474                 playlist_AddInfo( p_playlist, -1, psz_cat,
475                                  _("Codec"), "%.4s",
476                                  (char*)&fmt->i_codec );
477                 break;
478             default:
479
480                 break;
481         }
482         if( p_playlist ) vlc_object_release( p_playlist );
483     }
484
485     /* Apply mode
486      * XXX change that when we do group too */
487     if( 1 )
488     {
489         EsOutSelect( out, es, VLC_FALSE );
490     }
491
492     vlc_mutex_unlock( &p_input->stream.stream_lock );
493
494     es->p_es->fmt = *fmt;
495
496     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
497     p_sys->i_id++;  /* always incremented */
498     switch( fmt->i_cat )
499     {
500         case AUDIO_ES:
501             p_sys->i_audio++;
502             break;
503         case SPU_ES:
504             p_sys->i_sub++;
505             break;
506         case VIDEO_ES:
507             p_sys->i_video++;
508             break;
509     }
510
511     return es;
512 }
513
514 /*****************************************************************************
515  * EsOutSend:
516  *****************************************************************************/
517 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
518 {
519     es_out_sys_t *p_sys = out->p_sys;
520
521     if( p_sys->b_pcr_set )
522     {
523         pgrm_descriptor_t *p_pgrm = es->p_es->p_pgrm;
524         input_thread_t    *p_input = p_sys->p_input;
525
526         if( p_pgrm == NULL )
527         {
528             p_pgrm = p_sys->p_input->stream.p_selected_program;
529         }
530
531         if( p_block->i_dts > 0 && p_pgrm )
532         {
533             p_block->i_dts =
534                 input_ClockGetTS( p_input, p_pgrm, p_block->i_dts * 9 / 100 );
535         }
536         if( p_block->i_pts > 0 && p_pgrm )
537         {
538             p_block->i_pts =
539                 input_ClockGetTS( p_input, p_pgrm, p_block->i_pts * 9 / 100 );
540         }
541     }
542
543     vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
544     p_block->i_rate = out->p_sys->p_input->stream.control.i_rate;
545     if( es->p_es->p_dec &&
546         (es->p_es->i_cat!=AUDIO_ES || !p_sys->p_input->stream.control.b_mute) )
547     {
548         input_DecodeBlock( es->p_es->p_dec, p_block );
549     }
550     else
551     {
552         block_Release( p_block );
553     }
554     vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
555
556     return VLC_SUCCESS;
557 }
558
559 /*****************************************************************************
560  * EsOutDel:
561  *****************************************************************************/
562 static void EsOutDel( es_out_t *out, es_out_id_t *es )
563 {
564     es_out_sys_t *p_sys = out->p_sys;
565
566     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
567
568     switch( es->p_es->i_cat )
569     {
570         case AUDIO_ES:
571             p_sys->i_audio--;
572             break;
573         case SPU_ES:
574             p_sys->i_sub--;
575             break;
576         case VIDEO_ES:
577             p_sys->i_video--;
578             break;
579     }
580
581     /* We don't try to reselect */
582     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
583     if( es->p_es->p_dec )
584     {
585         input_UnselectES( p_sys->p_input, es->p_es );
586     }
587
588     if( es->p_es->p_waveformatex )
589     {
590         free( es->p_es->p_waveformatex );
591         es->p_es->p_waveformatex = NULL;
592     }
593     if( es->p_es->p_bitmapinfoheader )
594     {
595         free( es->p_es->p_bitmapinfoheader );
596         es->p_es->p_bitmapinfoheader = NULL;
597     }
598     input_DelES( p_sys->p_input, es->p_es );
599
600     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
601     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
602     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
603
604     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
605
606     free( es );
607 }
608
609 /*****************************************************************************
610  * EsOutControl:
611  *****************************************************************************/
612 static int EsOutControl( es_out_t *out, int i_query, va_list args )
613 {
614     es_out_sys_t *p_sys = out->p_sys;
615     vlc_bool_t  b, *pb;
616     int         i, *pi;
617
618     es_out_id_t *es;
619
620     switch( i_query )
621     {
622         case ES_OUT_SET_ES_STATE:
623             vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
624             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
625             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
626             if( b && es->p_es->p_dec == NULL )
627             {
628                 input_SelectES( p_sys->p_input, es->p_es );
629                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
630                 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
631             }
632             else if( !b && es->p_es->p_dec )
633             {
634                 input_UnselectES( p_sys->p_input, es->p_es );
635                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
636                 return VLC_SUCCESS;
637             }
638             vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
639             return VLC_SUCCESS;
640
641         case ES_OUT_GET_ES_STATE:
642             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
643             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
644
645             *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
646             return VLC_SUCCESS;
647
648         case ES_OUT_SET_ACTIVE:
649         {
650             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
651             p_sys->b_active = b;
652
653             if( b )
654             {
655                 vlc_value_t val;
656                 val.b_bool = VLC_TRUE;
657                 var_Set( p_sys->p_input, "intf-change", val );
658             }
659             return VLC_SUCCESS;
660         }
661
662         case ES_OUT_GET_ACTIVE:
663             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
664             *pb = p_sys->b_active;
665             return VLC_SUCCESS;
666
667         case ES_OUT_SET_MODE:
668             i = (int) va_arg( args, int );
669             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
670                 i == ES_OUT_MODE_AUTO )
671             {
672                 vlc_value_t val;
673
674                 p_sys->i_mode = i;
675
676                 /* Reapply policy mode */
677                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
678                 for( i = 0; i < p_sys->i_es; i++ )
679                 {
680                     if( p_sys->es[i]->p_es->p_dec )
681                     {
682                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
683                     }
684                 }
685                 for( i = 0; i < p_sys->i_es; i++ )
686                 {
687                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
688                 }
689                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
690
691                 val.b_bool = VLC_TRUE;
692                 var_Set( p_sys->p_input, "intf-change", val );
693
694                 return VLC_SUCCESS;
695             }
696             return VLC_EGENERIC;
697
698         case ES_OUT_GET_MODE:
699             pi = (int*) va_arg( args, int* );
700             *pi = p_sys->i_mode;
701             return VLC_SUCCESS;
702
703         case ES_OUT_SET_ES:
704             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
705             if( es == NULL )
706             {
707                 for( i = 0; i < p_sys->i_es; i++ )
708                 {
709                     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
710                     if( p_sys->es[i]->p_es->p_dec )
711                     {
712                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
713                     }
714                     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
715                 }
716             }
717             else
718             {
719                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
720                 EsOutSelect( out, es, VLC_TRUE );
721                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
722             }
723             return VLC_SUCCESS;
724
725         case ES_OUT_SET_PCR:
726         case ES_OUT_SET_GROUP_PCR:
727         {
728             pgrm_descriptor_t *p_prgm = NULL;
729             int64_t           i_pcr;
730
731             if( i_query == ES_OUT_SET_PCR )
732             {
733                 p_prgm = p_sys->p_input->stream.p_selected_program;
734             }
735             else
736             {
737                 int i_group = (int)va_arg( args, int );
738                 p_prgm = input_FindProgram( p_sys->p_input, i_group );
739             }
740             i_pcr   = (int64_t)va_arg( args, int64_t );
741
742             /* search program */
743             if( p_prgm )
744             {
745                 input_ClockManageRef( p_sys->p_input, p_prgm, i_pcr * 9 / 100);
746             }
747             p_sys->b_pcr_set = VLC_TRUE;
748             return VLC_SUCCESS;
749         }
750
751         case ES_OUT_RESET_PCR:
752             for( i = 0; i < p_sys->p_input->stream.i_pgrm_number; i++ )
753             {
754                 p_sys->p_input->stream.pp_programs[i]->i_synchro_state = SYNCHRO_REINIT;
755             }
756             p_sys->b_pcr_set = VLC_TRUE;
757             return VLC_SUCCESS;
758
759         default:
760             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
761             return VLC_EGENERIC;
762     }
763 }