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