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