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