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