]> git.sesse.net Git - vlc/blob - src/input/input_programs.c
* Bug fixes and enhancements in the Gtk+/Gnome interfaces.
[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.41 2001/03/15 01:42:20 sam 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     p_es->b_spu = 0;
375
376     if( i_data_len )
377     {
378         p_es->p_demux_data = malloc( i_data_len );
379         if( p_es->p_demux_data == NULL )
380         {
381             intf_ErrMsg( "Unable to allocate memory in input_AddES" );
382             return( NULL );
383         }
384         memset( p_es->p_demux_data, 0, i_data_len );
385     }
386     else
387     {
388         p_es->p_demux_data = NULL;
389     }
390
391     /* Add this ES to the program definition if one is given */
392     if( p_pgrm )
393     {
394         p_pgrm->i_es_number++;
395         p_pgrm->pp_es = realloc( p_pgrm->pp_es,
396                                  p_pgrm->i_es_number
397                                   * sizeof(es_descriptor_t *) );
398         if( p_pgrm->pp_es == NULL )
399         {
400             intf_ErrMsg( "Unable to realloc memory in input_AddES" );
401             return( NULL );
402         }
403         p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
404         p_es->p_pgrm = p_pgrm;
405     }
406     else
407     {
408         p_es->p_pgrm = NULL;
409     }
410
411     return p_es;
412 }
413
414 /*****************************************************************************
415  * input_DelES:
416  *****************************************************************************/
417 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
418 {
419     int                     i_index, i_es_index;
420     pgrm_descriptor_t *     p_pgrm;
421
422     ASSERT( p_es );
423     p_pgrm = p_es->p_pgrm;
424
425     /* Kill associated decoder, if any. */
426     if( p_es->p_decoder_fifo != NULL )
427     {
428         input_EndDecoder( p_input, p_es );
429     }
430
431     /* Remove this ES from the description of the program if it is associated to
432      * one */
433     if( p_pgrm )
434     {
435         for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
436         {
437             if( p_pgrm->pp_es[i_index] == p_es )
438             {
439                 p_pgrm->i_es_number--;
440                 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
441                 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
442                                          p_pgrm->i_es_number
443                                           * sizeof(es_descriptor_t *));
444                 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
445                 {
446                     intf_ErrMsg( "Unable to realloc memory in input_DelES" );
447                 }
448                 break;
449             }
450         }
451     }
452
453     /* Free the demux data */
454     if( p_es->p_demux_data != NULL )
455     {
456         free( p_es->p_demux_data );
457     }
458
459     /* Find the ES in the ES table */
460     for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
461          i_es_index++ )
462     {
463         if( p_input->stream.pp_es[i_es_index] == p_es )
464             break;
465     }
466
467     /* Free the ES */
468     free( p_es );
469     p_input->stream.i_es_number--;
470     p_input->stream.pp_es[i_es_index] =
471                     p_input->stream.pp_es[p_input->stream.i_es_number];
472     p_input->stream.pp_es = realloc( p_input->stream.pp_es,
473                                      p_input->stream.i_es_number
474                                       * sizeof(es_descriptor_t *));
475     if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
476     {
477         intf_ErrMsg( "Unable to realloc memory in input_DelES" );
478     }
479     
480 }
481
482 /*****************************************************************************
483  * InitDecConfig: initializes a decoder_config_t
484  *****************************************************************************/
485 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
486                           decoder_config_t * p_config )
487 {
488     p_config->i_id = p_es->i_id;
489     p_config->i_type = p_es->i_type;
490     p_config->p_stream_ctrl =
491         &p_input->stream.control;
492
493     /* Decoder FIFO */
494     if( (p_config->p_decoder_fifo =
495             (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
496     {
497         intf_ErrMsg( "Out of memory" );
498         return( -1 );
499     }
500
501     vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
502     vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
503     p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
504     p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
505     p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
506     p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
507     p_es->p_decoder_fifo = p_config->p_decoder_fifo;
508
509     p_config->pf_init_bit_stream = InitBitstream;
510
511     p_input->stream.i_selected_es_number++;
512
513     p_input->stream.pp_selected_es = realloc(
514                                        p_input->stream.pp_selected_es,
515                                        p_input->stream.i_selected_es_number
516                                         * sizeof(es_descriptor_t *) );
517     if( p_input->stream.pp_selected_es == NULL )
518     {
519         intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
520         return(-1);
521     }
522     p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
523             = p_es;
524
525     return( 0 );
526 }
527
528 /*****************************************************************************
529  * GetVdecConfig: returns a valid vdec_config_t
530  *****************************************************************************/
531 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
532                                       es_descriptor_t * p_es )
533 {
534     vdec_config_t *     p_config;
535
536     p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
537     if( p_config == NULL )
538     {
539         intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
540         return( NULL );
541     }
542     p_config->p_vout = p_input->p_default_vout;
543     if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
544     {
545         free( p_config );
546         return( NULL );
547     }
548
549     return( p_config );
550 }
551
552 /*****************************************************************************
553  * GetAdecConfig: returns a valid adec_config_t
554  *****************************************************************************/
555 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
556                                       es_descriptor_t * p_es )
557 {
558     adec_config_t *     p_config;
559
560     p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
561     if( p_config == NULL )
562     {
563         intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
564         return( NULL );
565     }
566     p_config->p_aout = p_input->p_default_aout;
567     if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
568     {
569         free( p_config );
570         return( NULL );
571     }
572
573     return( p_config );
574 }
575
576 /*****************************************************************************
577  * input_SelectES: selects an ES and spawns the associated decoder
578  *****************************************************************************
579  * Remember we are still supposed to have stream_lock when entering this
580  * function ?
581  *****************************************************************************/
582 /* FIXME */
583 vlc_thread_t adec_CreateThread( void * );
584 vlc_thread_t ac3dec_CreateThread( void * );
585 vlc_thread_t vpar_CreateThread( void * );
586 vlc_thread_t spudec_CreateThread( void * );
587
588 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
589 {
590     /* FIXME ! */
591     decoder_capabilities_t  decoder;
592     void *                  p_config;
593
594 #ifdef DEBUG_INPUT
595     intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
596 #endif
597
598     if( p_es->p_decoder_fifo != NULL )
599     {
600         intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
601         return( -1 );
602     }
603
604     switch( p_es->i_type )
605     {
606     case MPEG1_AUDIO_ES:
607     case MPEG2_AUDIO_ES:
608         if( p_main->b_audio )
609         {
610             decoder.pf_create_thread = adec_CreateThread;
611             p_config = (void *)GetAdecConfig( p_input, p_es );
612
613             /* Release the lock, not to block the input thread during
614              * the creation of the thread. */
615             vlc_mutex_unlock( &p_input->stream.stream_lock );
616             p_es->thread_id = input_RunDecoder( &decoder, p_config );
617             vlc_mutex_lock( &p_input->stream.stream_lock );
618         }
619         break;
620
621     case MPEG1_VIDEO_ES:
622     case MPEG2_VIDEO_ES:
623         if( p_main->b_video )
624         {
625             decoder.pf_create_thread = vpar_CreateThread;
626             p_config = (void *)GetVdecConfig( p_input, p_es );
627
628             /* Release the lock, not to block the input thread during
629              * the creation of the thread. */
630             vlc_mutex_unlock( &p_input->stream.stream_lock );
631             p_es->thread_id = input_RunDecoder( &decoder, p_config );
632             vlc_mutex_lock( &p_input->stream.stream_lock );
633         }
634         break;
635
636     case AC3_AUDIO_ES:
637         if( p_main->b_audio )
638         {
639             decoder.pf_create_thread = ac3dec_CreateThread;
640             p_config = (void *)GetAdecConfig( p_input, p_es );
641
642             /* Release the lock, not to block the input thread during
643              * the creation of the thread. */
644             vlc_mutex_unlock( &p_input->stream.stream_lock );
645             p_es->thread_id = input_RunDecoder( &decoder, p_config );
646             vlc_mutex_lock( &p_input->stream.stream_lock );
647         }
648         break;
649
650     case DVD_SPU_ES:
651         if( p_main->b_video )
652         {
653             decoder.pf_create_thread = spudec_CreateThread;
654             p_config = (void *)GetVdecConfig( p_input, p_es );
655
656             /* Release the lock, not to block the input thread during
657              * the creation of the thread. */
658             vlc_mutex_unlock( &p_input->stream.stream_lock );
659             p_es->thread_id = input_RunDecoder( &decoder, p_config );
660             vlc_mutex_lock( &p_input->stream.stream_lock );
661         }
662         break;
663
664     default:
665         intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
666         return( -1 );
667         break;
668     }
669
670     if( p_es->thread_id == 0 )
671     {
672         return( -1 );
673     }
674
675     return( 0 );
676 }
677
678 /*****************************************************************************
679  * input_UnselectES: removes an ES from the list of selected ES
680  *****************************************************************************/
681 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
682 {
683
684     int     i_index = 0;
685
686 #ifdef DEBUG_INPUT
687     intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
688 #endif
689
690     if( p_es->p_decoder_fifo == NULL )
691     {
692         intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
693         return( -1 );
694     }
695
696     /* Release lock, not to block the input thread. */
697     vlc_mutex_unlock( &p_input->stream.stream_lock );
698     input_EndDecoder( p_input, p_es );
699     vlc_mutex_lock( &p_input->stream.stream_lock );
700
701     if( p_es->p_decoder_fifo == NULL )
702     {
703         p_input->stream.i_selected_es_number--;
704
705         while( ( i_index < p_input->stream.i_selected_es_number ) &&
706                ( p_input->stream.pp_selected_es[i_index] != p_es ) )
707         {
708             i_index++;
709         }
710
711         p_input->stream.pp_selected_es[i_index] = 
712           p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
713
714         p_input->stream.pp_selected_es = realloc(
715                                            p_input->stream.pp_selected_es,
716                                            p_input->stream.i_selected_es_number
717                                             * sizeof(es_descriptor_t *) );
718         if( p_input->stream.pp_selected_es == NULL )
719         {
720             intf_ErrMsg( "Unable to realloc memory in input_UnSelectES" );
721             return(-1);
722         }
723     }
724     return( 0 );
725 }