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