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