]> git.sesse.net Git - vlc/blob - src/input/input_programs.c
* More consistency in the p_input->stream management.
[vlc] / src / input / input_programs.c
1 /*****************************************************************************
2  * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: input_programs.c,v 1.39 2001/03/02 15:51:22 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@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 "defs.h"
28
29 #include <stdlib.h>
30
31 #include "config.h"
32 #include "common.h"
33 #include "threads.h"
34 #include "mtime.h"
35 #include "debug.h"
36
37 #include "intf_msg.h"
38
39 #include "stream_control.h"
40 #include "input_ext-intf.h"
41 #include "input_ext-dec.h"
42 #include "input.h"
43
44 #include "main.h"                                     /* --noaudio --novideo */
45
46 /*
47  * NOTICE : all of these functions expect you to have taken the lock on
48  * p_input->stream.lock
49  */
50
51 /*****************************************************************************
52  * input_InitStream: init the stream descriptor of the given input
53  *****************************************************************************/
54 int input_InitStream( input_thread_t * p_input, size_t i_data_len )
55 {
56     p_input->stream.i_stream_id = 0;
57     p_input->stream.pp_es = NULL;
58     p_input->stream.pp_selected_es = NULL;
59     p_input->stream.pp_programs = NULL;
60
61     if( i_data_len )
62     {
63         if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
64         {
65             intf_ErrMsg( "Unable to allocate memory in input_InitStream");
66             return 1;
67         }
68         memset( p_input->stream.p_demux_data, 0, i_data_len );
69     }
70
71     return 0;
72 }
73
74 /*****************************************************************************
75  * input_EndStream: free all stream descriptors
76  *****************************************************************************/
77 void input_EndStream( input_thread_t * p_input )
78 {
79     /* Free all programs and associated ES, and associated decoders. */
80     while( p_input->stream.i_pgrm_number )
81     {
82         input_DelProgram( p_input, p_input->stream.pp_programs[0] );
83     }
84
85     /* Free standalone ES */
86     while( p_input->stream.i_es_number )
87     {
88         input_DelES( p_input, p_input->stream.pp_es[0] );
89     }
90
91     if( p_input->stream.p_demux_data != NULL )
92     {
93         free( p_input->stream.p_demux_data );
94     }
95 }
96
97 /*****************************************************************************
98  * input_FindProgram: returns a pointer to a program described by its ID
99  *****************************************************************************/
100 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
101 {
102     int     i;
103
104     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
105     {
106         if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
107         {
108             return p_input->stream.pp_programs[i];
109         }
110     }
111
112     return( NULL );
113 }
114
115 /*****************************************************************************
116  * input_AddProgram: add and init a program descriptor
117  *****************************************************************************
118  * This program descriptor will be referenced in the given stream descriptor
119  *****************************************************************************/
120 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
121                                       u16 i_pgrm_id, size_t i_data_len )
122 {
123     /* Where to add the pgrm */
124     int i_pgrm_index = p_input->stream.i_pgrm_number;
125
126     intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
127
128     /* Add an entry to the list of program associated with the stream */
129     p_input->stream.i_pgrm_number++;
130     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
131                                            p_input->stream.i_pgrm_number
132                                             * sizeof(pgrm_descriptor_t *) );
133     if( p_input->stream.pp_programs == NULL )
134     {
135         intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
136         return( NULL );
137     }
138     
139     /* Allocate the structure to store this description */
140     p_input->stream.pp_programs[i_pgrm_index] =
141                                         malloc( sizeof(pgrm_descriptor_t) );
142     if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
143     {
144         intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
145         return( NULL );
146     }
147     
148     /* Init this entry */
149     p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
150     p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
151     p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
152
153     p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
154     p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
155
156     input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
157
158     p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
159                                                 = SYNCHRO_START;
160
161     p_input->stream.pp_programs[i_pgrm_index]->p_vout
162                                             = p_input->p_default_vout;
163     p_input->stream.pp_programs[i_pgrm_index]->p_aout
164                                             = p_input->p_default_aout;
165
166     if( i_data_len )
167     {
168         p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
169             malloc( i_data_len );
170         if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
171         {
172             intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
173             return( NULL );
174         }
175         memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
176                 i_data_len );
177     }
178
179     return p_input->stream.pp_programs[i_pgrm_index];
180 }
181
182 /*****************************************************************************
183  * input_DelProgram: destroy a program descriptor
184  *****************************************************************************
185  * All ES descriptions referenced in the descriptor will be deleted.
186  *****************************************************************************/
187 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
188 {
189     int i_pgrm_index;
190
191     ASSERT( p_pgrm );
192
193     intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
194
195     /* Free the structures that describe the es that belongs to that program */
196     while( p_pgrm->i_es_number )
197     {
198         input_DelES( p_input, p_pgrm->pp_es[0] );
199     }
200
201     /* Free the demux data */
202     if( p_pgrm->p_demux_data != NULL )
203     {
204         free( p_pgrm->p_demux_data );
205     }
206
207     /* Find the program in the programs table */
208     for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
209          i_pgrm_index++ )
210     {
211         if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
212             break;
213     }
214
215     /* Remove this program from the stream's list of programs */
216     p_input->stream.i_pgrm_number--;
217
218     p_input->stream.pp_programs[i_pgrm_index] =
219         p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
220     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
221                                            p_input->stream.i_pgrm_number
222                                             * sizeof(pgrm_descriptor_t *) );
223
224     if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
225     {
226         intf_ErrMsg( "input error: unable to realloc program list"
227                      " in input_DelProgram" );
228     }
229
230     /* Free the description of this program */
231     free( p_pgrm );
232 }
233
234 /*****************************************************************************
235  * input_AddArea: add and init an area descriptor
236  *****************************************************************************
237  * This area descriptor will be referenced in the given stream descriptor
238  *****************************************************************************/
239 input_area_t * input_AddArea( input_thread_t * p_input )
240 {
241     /* Where to add the pgrm */
242     int i_area_index = p_input->stream.i_area_nb;
243
244     intf_DbgMsg("Adding description for area %d", i_area_index );
245
246     /* Add an entry to the list of program associated with the stream */
247     p_input->stream.i_area_nb++;
248     p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
249                                         p_input->stream.i_area_nb
250                                             * sizeof(input_area_t *) );
251     if( p_input->stream.pp_areas == NULL )
252     {
253         intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
254         return( NULL );
255     }
256     
257     /* Allocate the structure to store this description */
258     p_input->stream.pp_areas[i_area_index] =
259                                         malloc( sizeof(input_area_t) );
260     if( p_input->stream.pp_areas[i_area_index] == NULL )
261     {
262         intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
263         return( NULL );
264     }
265     
266     /* Init this entry */
267     p_input->stream.pp_areas[i_area_index]->i_id = 0;
268     p_input->stream.pp_areas[i_area_index]->i_start = 0;
269     p_input->stream.pp_areas[i_area_index]->i_size = 0;
270     p_input->stream.pp_areas[i_area_index]->i_tell = 0;
271     p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
272     p_input->stream.pp_areas[i_area_index]->i_part_nb = 0;
273     p_input->stream.pp_areas[i_area_index]->i_part= 0;
274
275     return p_input->stream.pp_areas[i_area_index];
276 }
277
278 /*****************************************************************************
279  * input_DelArea: destroy a area descriptor
280  *****************************************************************************
281  * All ES descriptions referenced in the descriptor will be deleted.
282  *****************************************************************************/
283 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
284 {
285     int i_area_index;
286
287     ASSERT( p_area );
288
289     intf_DbgMsg("Deleting description for area %d", p_area->i_id );
290
291     /* Find the area in the areas table */
292     for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
293          i_area_index++ )
294     {
295         if( p_input->stream.pp_areas[i_area_index] == p_area )
296             break;
297     }
298
299     /* Remove this area from the stream's list of areas */
300     p_input->stream.i_area_nb--;
301
302     p_input->stream.pp_areas[i_area_index] =
303         p_input->stream.pp_areas[p_input->stream.i_area_nb];
304     p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
305                                            p_input->stream.i_area_nb
306                                             * sizeof(input_area_t *) );
307
308     if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
309     {
310         intf_ErrMsg( "input error: unable to realloc area list"
311                      " in input_DelArea" );
312     }
313
314     /* Free the description of this area */
315     free( p_area );
316 }
317
318
319 /*****************************************************************************
320  * input_FindES: returns a pointer to an ES described by its ID
321  *****************************************************************************/
322 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
323 {
324     int     i;
325
326     for( i = 0; i < p_input->stream.i_es_number; i++ )
327     {
328         if( p_input->stream.pp_es[i]->i_id == i_es_id )
329         {
330             return p_input->stream.pp_es[i];
331         }
332     }
333
334     return( NULL );
335 }
336
337 /*****************************************************************************
338  * input_AddES:
339  *****************************************************************************
340  * Reserve a slot in the table of ES descriptors for the ES and add it to the
341  * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
342  * alone (PSI ?)
343  *****************************************************************************/
344 es_descriptor_t * input_AddES( input_thread_t * p_input,
345                                pgrm_descriptor_t * p_pgrm, u16 i_es_id,
346                                size_t i_data_len )
347 {
348     es_descriptor_t * p_es;
349
350     intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
351
352     p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
353     if( p_es == NULL )
354     {
355         intf_ErrMsg( "Unable to allocate memory in input_AddES" );
356         return( NULL);
357     }
358     p_input->stream.i_es_number++;
359     p_input->stream.pp_es = realloc( p_input->stream.pp_es,
360                                      p_input->stream.i_es_number
361                                       * sizeof(es_descriptor_t *) );
362     if( p_input->stream.pp_es == NULL )
363     {
364         intf_ErrMsg( "Unable to realloc memory in input_AddES" );
365         return( NULL );
366     }
367     p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
368
369     /* Init its values */
370     p_es->i_id = i_es_id;
371     p_es->p_pes = NULL;
372     p_es->p_decoder_fifo = NULL;
373     p_es->b_audio = 0;
374
375     if( i_data_len )
376     {
377         p_es->p_demux_data = malloc( i_data_len );
378         if( p_es->p_demux_data == NULL )
379         {
380             intf_ErrMsg( "Unable to allocate memory in input_AddES" );
381             return( NULL );
382         }
383         memset( p_es->p_demux_data, 0, i_data_len );
384     }
385     else
386     {
387         p_es->p_demux_data = NULL;
388     }
389
390     /* Add this ES to the program definition if one is given */
391     if( p_pgrm )
392     {
393         p_pgrm->i_es_number++;
394         p_pgrm->pp_es = realloc( p_pgrm->pp_es,
395                                  p_pgrm->i_es_number
396                                   * sizeof(es_descriptor_t *) );
397         if( p_pgrm->pp_es == NULL )
398         {
399             intf_ErrMsg( "Unable to realloc memory in input_AddES" );
400             return( NULL );
401         }
402         p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
403         p_es->p_pgrm = p_pgrm;
404     }
405     else
406     {
407         p_es->p_pgrm = NULL;
408     }
409
410     return p_es;
411 }
412
413 /*****************************************************************************
414  * input_DelES:
415  *****************************************************************************/
416 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
417 {
418     int                     i_index, i_es_index;
419     pgrm_descriptor_t *     p_pgrm;
420
421     ASSERT( p_es );
422     p_pgrm = p_es->p_pgrm;
423
424     /* Kill associated decoder, if any. */
425     if( p_es->p_decoder_fifo != NULL )
426     {
427         input_EndDecoder( p_input, p_es );
428     }
429
430     /* Remove this ES from the description of the program if it is associated to
431      * one */
432     if( p_pgrm )
433     {
434         for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
435         {
436             if( p_pgrm->pp_es[i_index] == p_es )
437             {
438                 p_pgrm->i_es_number--;
439                 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
440                 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
441                                          p_pgrm->i_es_number
442                                           * sizeof(es_descriptor_t *));
443                 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
444                 {
445                     intf_ErrMsg( "Unable to realloc memory in input_DelES" );
446                 }
447                 break;
448             }
449         }
450     }
451
452     /* Free the demux data */
453     if( p_es->p_demux_data != NULL )
454     {
455         free( p_es->p_demux_data );
456     }
457
458     /* Find the ES in the ES table */
459     for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
460          i_es_index++ )
461     {
462         if( p_input->stream.pp_es[i_es_index] == p_es )
463             break;
464     }
465
466     /* Free the ES */
467     free( p_es );
468     p_input->stream.i_es_number--;
469     p_input->stream.pp_es[i_es_index] =
470                     p_input->stream.pp_es[p_input->stream.i_es_number];
471     p_input->stream.pp_es = realloc( p_input->stream.pp_es,
472                                      p_input->stream.i_es_number
473                                       * sizeof(es_descriptor_t *));
474     if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
475     {
476         intf_ErrMsg( "Unable to realloc memory in input_DelES" );
477     }
478     
479 }
480
481 /*****************************************************************************
482  * InitDecConfig: initializes a decoder_config_t
483  *****************************************************************************/
484 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
485                           decoder_config_t * p_config )
486 {
487     p_config->i_id = p_es->i_id;
488     p_config->i_type = p_es->i_type;
489     p_config->p_stream_ctrl =
490         &p_input->stream.control;
491
492     /* Decoder FIFO */
493     if( (p_config->p_decoder_fifo =
494             (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
495     {
496         intf_ErrMsg( "Out of memory" );
497         return( -1 );
498     }
499
500     vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
501     vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
502     p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
503     p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
504     p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
505     p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
506     p_es->p_decoder_fifo = p_config->p_decoder_fifo;
507
508     p_config->pf_init_bit_stream = InitBitstream;
509
510     p_input->stream.i_selected_es_number++;
511     p_input->stream.pp_selected_es = realloc(
512                                        p_input->stream.pp_selected_es,
513                                        p_input->stream.i_selected_es_number
514                                         * sizeof(es_descriptor_t *) );
515     if( p_input->stream.pp_selected_es == NULL )
516     {
517         intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
518         return(-1);
519     }
520     p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
521             = p_es;
522
523     return( 0 );
524 }
525
526 /*****************************************************************************
527  * GetVdecConfig: returns a valid vdec_config_t
528  *****************************************************************************/
529 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
530                                       es_descriptor_t * p_es )
531 {
532     vdec_config_t *     p_config;
533
534     p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
535     if( p_config == NULL )
536     {
537         intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
538         return( NULL );
539     }
540     p_config->p_vout = p_input->p_default_vout;
541     if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
542     {
543         free( p_config );
544         return( NULL );
545     }
546
547     return( p_config );
548 }
549
550 /*****************************************************************************
551  * GetAdecConfig: returns a valid adec_config_t
552  *****************************************************************************/
553 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
554                                       es_descriptor_t * p_es )
555 {
556     adec_config_t *     p_config;
557
558     p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
559     if( p_config == NULL )
560     {
561         intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
562         return( NULL );
563     }
564     p_config->p_aout = p_input->p_default_aout;
565     if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
566     {
567         free( p_config );
568         return( NULL );
569     }
570
571     return( p_config );
572 }
573
574 /*****************************************************************************
575  * input_SelectES: selects an ES and spawns the associated decoder
576  *****************************************************************************
577  * Remember we are still supposed to have stream_lock when entering this
578  * function ?
579  *****************************************************************************/
580 /* FIXME */
581 vlc_thread_t adec_CreateThread( void * );
582 vlc_thread_t ac3dec_CreateThread( void * );
583 vlc_thread_t vpar_CreateThread( void * );
584 vlc_thread_t spudec_CreateThread( void * );
585
586 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
587 {
588     /* FIXME ! */
589     decoder_capabilities_t  decoder;
590     void *                  p_config;
591
592 #ifdef DEBUG_INPUT
593     intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
594 #endif
595
596     if( p_es->p_decoder_fifo != NULL )
597     {
598         intf_ErrMsg( "ES %d is already selected", p_es->i_id );
599         return( -1 );
600     }
601
602     switch( p_es->i_type )
603     {
604     case MPEG1_AUDIO_ES:
605     case MPEG2_AUDIO_ES:
606         if( p_main->b_audio )
607         {
608             decoder.pf_create_thread = adec_CreateThread;
609             p_config = (void *)GetAdecConfig( p_input, p_es );
610
611             /* Release the lock, not to block the input thread during
612              * the creation of the thread. */
613             vlc_mutex_unlock( &p_input->stream.stream_lock );
614             p_es->thread_id = input_RunDecoder( &decoder, p_config );
615             vlc_mutex_lock( &p_input->stream.stream_lock );
616         }
617         break;
618
619     case MPEG1_VIDEO_ES:
620     case MPEG2_VIDEO_ES:
621         if( p_main->b_video )
622         {
623             decoder.pf_create_thread = vpar_CreateThread;
624             p_config = (void *)GetVdecConfig( p_input, p_es );
625
626             /* Release the lock, not to block the input thread during
627              * the creation of the thread. */
628             vlc_mutex_unlock( &p_input->stream.stream_lock );
629             p_es->thread_id = input_RunDecoder( &decoder, p_config );
630             vlc_mutex_lock( &p_input->stream.stream_lock );
631         }
632         break;
633
634     case AC3_AUDIO_ES:
635         if( p_main->b_audio )
636         {
637             decoder.pf_create_thread = ac3dec_CreateThread;
638             p_config = (void *)GetAdecConfig( p_input, p_es );
639
640             /* Release the lock, not to block the input thread during
641              * the creation of the thread. */
642             vlc_mutex_unlock( &p_input->stream.stream_lock );
643             p_es->thread_id = input_RunDecoder( &decoder, p_config );
644             vlc_mutex_lock( &p_input->stream.stream_lock );
645         }
646         break;
647
648     case DVD_SPU_ES:
649         if( p_main->b_video )
650         {
651             decoder.pf_create_thread = spudec_CreateThread;
652             p_config = (void *)GetVdecConfig( p_input, p_es );
653
654             /* Release the lock, not to block the input thread during
655              * the creation of the thread. */
656             vlc_mutex_unlock( &p_input->stream.stream_lock );
657             p_es->thread_id = input_RunDecoder( &decoder, p_config );
658             vlc_mutex_lock( &p_input->stream.stream_lock );
659         }
660         break;
661
662     default:
663         intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
664         return( -1 );
665         break;
666     }
667
668     if( p_es->thread_id == 0 )
669     {
670         return( -1 );
671     }
672
673     return( 0 );
674 }
675
676 /*****************************************************************************
677  * input_UnselectES: removes an ES from the list of selected ES
678  *****************************************************************************/
679 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
680 {
681
682     int     i_index = 0;
683
684 #ifdef DEBUG_INPUT
685     intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
686 #endif
687
688     if( p_es->p_decoder_fifo == NULL )
689     {
690         intf_ErrMsg( "ES %d is not selected", p_es->i_id );
691         return( -1 );
692     }
693
694     /* Release lock, not to block the input thread. */
695     vlc_mutex_unlock( &p_input->stream.stream_lock );
696     input_EndDecoder( p_input, p_es );
697     vlc_mutex_lock( &p_input->stream.stream_lock );
698
699     if( p_es->p_decoder_fifo == NULL )
700     {
701         p_input->stream.i_selected_es_number--;
702
703         while( ( i_index < p_input->stream.i_selected_es_number ) &&
704                ( p_input->stream.pp_selected_es[i_index] != p_es ) )
705         {
706             i_index++;
707         }
708
709         p_input->stream.pp_selected_es[i_index] = 
710           p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
711
712         p_input->stream.pp_selected_es = realloc(
713                                            p_input->stream.pp_selected_es,
714                                            p_input->stream.i_selected_es_number
715                                             * sizeof(es_descriptor_t *) );
716         if( p_input->stream.pp_selected_es == NULL )
717         {
718             intf_ErrMsg( "Unable to realloc memory in input_UnSelectES" );
719             return(-1);
720         }
721     }
722     return( 0 );
723 }