]> git.sesse.net Git - vlc/blob - src/input/input_programs.c
* ALL: the first libvlc commit.
[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.89 2002/06/01 12:32:01 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 <vlc/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             msg_Err( p_input, "out of memory" );
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         msg_Err( p_input, "out of memory" );
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         msg_Err( p_input, "out of memory" );
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             msg_Err( p_input, "out of memory" );
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         msg_Err( p_input, "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             msg_Err( p_input, "cannot realloc memory" );
245         }
246     }
247     else
248     {
249         free( p_input->stream.pp_programs );
250         p_input->stream.pp_programs = NULL;
251     }
252
253     /* Free the description of this program */
254     free( p_pgrm );
255 }
256
257 /*****************************************************************************
258  * input_AddArea: add and init an area descriptor
259  *****************************************************************************
260  * This area descriptor will be referenced in the given stream descriptor
261  *****************************************************************************/
262 input_area_t * input_AddArea( input_thread_t * p_input )
263 {
264     /* Where to add the pgrm */
265     int i_area_index = p_input->stream.i_area_nb;
266
267     /* Add an entry to the list of program associated with the stream */
268     p_input->stream.i_area_nb++;
269     p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
270                                         p_input->stream.i_area_nb
271                                         * sizeof(input_area_t *) );
272     if( p_input->stream.pp_areas == NULL )
273     {
274         msg_Err( p_input, "out of memory" );
275         return( NULL );
276     }
277
278     /* Allocate the structure to store this description */
279     p_input->stream.pp_areas[i_area_index] =
280                                         malloc( sizeof(input_area_t) );
281     if( p_input->stream.pp_areas[i_area_index] == NULL )
282     {
283         msg_Err( p_input, "out of memory" );
284         return( NULL );
285     }
286     
287     /* Init this entry */
288     p_input->stream.pp_areas[i_area_index]->i_id = 0;
289     p_input->stream.pp_areas[i_area_index]->i_start = 0;
290     p_input->stream.pp_areas[i_area_index]->i_size = 0;
291     p_input->stream.pp_areas[i_area_index]->i_tell = 0;
292     p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
293     p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
294     p_input->stream.pp_areas[i_area_index]->i_part= 0;
295
296     return p_input->stream.pp_areas[i_area_index];
297 }
298
299 /*****************************************************************************
300  * input_SetProgram: changes the current program
301  *****************************************************************************/
302 int input_SetProgram( input_thread_t * p_input, pgrm_descriptor_t * p_new_prg )
303 {
304     int i_es_index;
305     int i_required_audio_es;
306     int i_required_spu_es;
307     int i_audio_es = 0;
308     int i_spu_es = 0;
309
310     if ( p_input->stream.p_selected_program )
311     {
312         for ( i_es_index = 1 ; /* 0 should be the PMT */
313                 i_es_index < p_input->stream.p_selected_program->
314                 i_es_number ;
315                 i_es_index ++ )
316         {
317 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
318             if ( p_es->p_decoder_fifo ) /* if the ES was selected */
319             {
320                 input_UnselectES( p_input , p_es );
321             }
322 #undef p_es
323         }
324     }
325     /* Get the number of the required audio stream */
326     if( config_GetInt( p_input, "audio" ) )
327     {
328         /* Default is the first one */
329         i_required_audio_es = config_GetInt( p_input, "audio-channel" );
330         if( i_required_audio_es < 0 )
331         {
332             i_required_audio_es = 1;
333         }
334     }
335     else
336     {
337         i_required_audio_es = 0;
338     }
339
340     /* Same thing for subtitles */
341     if( config_GetInt( p_input, "video" ) )
342     {
343         /* for spu, default is none */
344         i_required_spu_es = config_GetInt( p_input, "spu-channel" );
345         if( i_required_spu_es < 0 )
346         {
347             i_required_spu_es = 0;
348         }
349     }
350     else
351     {
352         i_required_spu_es = 0;
353     }
354
355     for (i_es_index = 0 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
356     {
357         switch( p_new_prg->pp_es[i_es_index]->i_cat )
358         {
359             case VIDEO_ES:
360                 msg_Dbg( p_input, "selecting ES %x",
361                          p_new_prg->pp_es[i_es_index]->i_id );
362                 input_SelectES( p_input, p_new_prg->pp_es[i_es_index] );
363                 break;
364             case AUDIO_ES:
365                 i_audio_es += 1;
366                 if( i_audio_es <= i_required_audio_es )
367                 {
368                     msg_Dbg( p_input, "selecting ES %x",
369                              p_new_prg->pp_es[i_es_index]->i_id );
370                     input_SelectES( p_input, p_new_prg->pp_es[i_es_index]);
371                 }
372                 break;
373             /* Not sure this one is fully specification-compliant */
374             case SPU_ES :
375                 i_spu_es += 1;
376                 if( i_spu_es <= i_required_spu_es )
377                 {
378                     msg_Dbg( p_input, "selecting ES %x",
379                              p_new_prg->pp_es[i_es_index]->i_id );
380                     input_SelectES( p_input, p_new_prg->pp_es[i_es_index] );
381                 }
382             break;
383             default :
384                 msg_Dbg( p_input, "ES %x has unknown type",
385                          p_new_prg->pp_es[i_es_index]->i_id );
386                 break;
387         }
388
389     }
390
391
392     p_input->stream.p_selected_program = p_new_prg;
393
394     return( 0 );
395 }
396
397
398 /*****************************************************************************
399  * input_DelArea: destroy a area descriptor
400  *****************************************************************************
401  * All ES descriptions referenced in the descriptor will be deleted.
402  *****************************************************************************/
403 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
404 {
405     int i_area_index;
406
407     /* Find the area in the areas table */
408     for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
409          i_area_index++ )
410     {
411         if( p_input->stream.pp_areas[i_area_index] == p_area )
412             break;
413     }
414
415     /* If the area wasn't found, do nothing */
416     if( i_area_index == p_input->stream.i_area_nb )
417     {
418         msg_Err( p_input, "area does not belong to this input" );
419         return;
420     }
421
422     /* Remove this area from the stream's list of areas */
423     p_input->stream.i_area_nb--;
424
425     p_input->stream.pp_areas[i_area_index] =
426         p_input->stream.pp_areas[p_input->stream.i_area_nb];
427     if( p_input->stream.i_area_nb )
428     {
429         p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
430                                             p_input->stream.i_area_nb
431                                             * sizeof(input_area_t *) );
432
433         if( p_input->stream.pp_areas == NULL )
434         {
435             msg_Err( p_input, "cannot realloc memory" );
436         }
437     }
438     else
439     {
440         free( p_input->stream.pp_areas );
441         p_input->stream.pp_areas = NULL;
442     }
443
444     /* Free the description of this area */
445     free( p_area );
446 }
447
448
449 /*****************************************************************************
450  * input_FindES: returns a pointer to an ES described by its ID
451  *****************************************************************************/
452 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
453 {
454     int     i;
455
456     for( i = 0; i < p_input->stream.i_es_number; i++ )
457     {
458         if( p_input->stream.pp_es[i]->i_id == i_es_id )
459         {
460             return p_input->stream.pp_es[i];
461         }
462     }
463
464     return NULL;
465 }
466
467 /*****************************************************************************
468  * input_AddES:
469  *****************************************************************************
470  * Reserve a slot in the table of ES descriptors for the ES and add it to the
471  * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
472  * alone (PSI ?)
473  *****************************************************************************/
474 es_descriptor_t * input_AddES( input_thread_t * p_input,
475                                pgrm_descriptor_t * p_pgrm, u16 i_es_id,
476                                size_t i_data_len )
477 {
478     es_descriptor_t * p_es;
479
480     p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
481     if( p_es == NULL )
482     {
483         msg_Err( p_input, "out of memory" );
484         return( NULL);
485     }
486     p_input->stream.i_es_number++;
487     p_input->stream.pp_es = realloc( p_input->stream.pp_es,
488                                      p_input->stream.i_es_number
489                                       * sizeof(es_descriptor_t *) );
490     if( p_input->stream.pp_es == NULL )
491     {
492         msg_Err( p_input, "out of memory" );
493         return( NULL );
494     }
495
496     p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
497
498     /* Init its values */
499     p_es->i_id = i_es_id;
500     p_es->psz_desc[0] = '\0';
501     p_es->p_pes = NULL;
502     p_es->p_decoder_fifo = NULL;
503     p_es->b_audio = 0;
504     p_es->i_cat = UNKNOWN_ES;
505     p_es->i_demux_fd = 0;
506
507     if( i_data_len )
508     {
509         p_es->p_demux_data = malloc( i_data_len );
510         if( p_es->p_demux_data == NULL )
511         {
512             msg_Err( p_input, "out of memory" );
513             return( NULL );
514         }
515         memset( p_es->p_demux_data, 0, i_data_len );
516     }
517     else
518     {
519         p_es->p_demux_data = NULL;
520     }
521
522     /* Add this ES to the program definition if one is given */
523     if( p_pgrm )
524     {
525         p_pgrm->i_es_number++;
526         p_pgrm->pp_es = realloc( p_pgrm->pp_es,
527                                  p_pgrm->i_es_number
528                                   * sizeof(es_descriptor_t *) );
529         if( p_pgrm->pp_es == NULL )
530         {
531             msg_Err( p_input, "out of memory" );
532             return( NULL );
533         }
534
535         p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
536         p_es->p_pgrm = p_pgrm;
537     }
538     else
539     {
540         p_es->p_pgrm = NULL;
541     }
542
543     return p_es;
544 }
545
546 /*****************************************************************************
547  * input_DelES:
548  *****************************************************************************/
549 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
550 {
551     int                     i_index, i_es_index;
552     pgrm_descriptor_t *     p_pgrm;
553
554     /* Find the ES in the ES table */
555     for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
556          i_es_index++ )
557     {
558         if( p_input->stream.pp_es[i_es_index] == p_es )
559             break;
560     }
561
562     /* If the ES wasn't found, do nothing */
563     if( i_es_index == p_input->stream.i_es_number )
564     {
565         msg_Err( p_input, "ES does not belong to this input" );
566         return;
567     }
568
569     p_pgrm = p_es->p_pgrm;
570
571     /* Kill associated decoder, if any. */
572     if( p_es->p_decoder_fifo != NULL )
573     {
574         input_EndDecoder( p_input, p_es );
575     }
576
577     /* Remove this ES from the description of the program if it is associated to
578      * one */
579     if( p_pgrm )
580     {
581         for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
582         {
583             if( p_pgrm->pp_es[i_index] == p_es )
584             {
585                 p_pgrm->i_es_number--;
586                 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
587                 if( p_pgrm->i_es_number )
588                 {
589                     p_pgrm->pp_es = realloc( p_pgrm->pp_es,
590                                              p_pgrm->i_es_number
591                                               * sizeof(es_descriptor_t *));
592                     if( p_pgrm->pp_es == NULL )
593                     {
594                         msg_Err( p_input, "cannot realloc memory" );
595                     }
596                 }
597                 else
598                 {
599                     free( p_pgrm->pp_es );
600                     p_pgrm->pp_es = NULL;
601                 }
602                 break;
603             }
604         }
605     }
606
607     /* Free the demux data */
608     if( p_es->p_demux_data != NULL )
609     {
610         free( p_es->p_demux_data );
611     }
612
613     /* Find the ES in the ES table */
614     for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
615          i_es_index++ )
616     {
617         if( p_input->stream.pp_es[i_es_index] == p_es )
618             break;
619     }
620
621     /* Remove this ES from the stream's list of ES */
622     p_input->stream.i_es_number--;
623     p_input->stream.pp_es[i_es_index] =
624                     p_input->stream.pp_es[p_input->stream.i_es_number];
625     if( p_input->stream.i_es_number )
626     {
627         p_input->stream.pp_es = realloc( p_input->stream.pp_es,
628                                          p_input->stream.i_es_number
629                                           * sizeof(es_descriptor_t *));
630         if( p_input->stream.pp_es == NULL )
631         {
632             msg_Err( p_input, "cannot realloc memory" );
633         }
634     }
635     else
636     {
637         free( p_input->stream.pp_es );
638         p_input->stream.pp_es = NULL;
639     }
640     
641     /* Free the ES */
642     free( p_es );
643 }
644
645 /*****************************************************************************
646  * input_SelectES: selects an ES and spawns the associated decoder
647  *****************************************************************************
648  * Remember we are still supposed to have stream_lock when entering this
649  * function ?
650  *****************************************************************************/
651 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
652 {
653     if( p_es == NULL )
654     {
655         msg_Err( p_input, "nothing to do in input_SelectES" );
656         return -1;
657     }
658
659     msg_Dbg( p_input, "selecting ES 0x%x", p_es->i_id );
660
661     if( p_es->p_decoder_fifo != NULL )
662     {
663         msg_Err( p_input, "ES 0x%x is already selected", p_es->i_id );
664         return( -1 );
665     }
666
667     p_es->p_decoder_fifo = NULL;
668
669     switch( p_es->i_type )
670     {
671     case AC3_AUDIO_ES:
672     case MPEG1_AUDIO_ES:
673     case MPEG2_AUDIO_ES:
674     case LPCM_AUDIO_ES:
675         if( config_GetInt( p_input, "audio" ) )
676         {
677             /* Release the lock, not to block the input thread during
678              * the creation of the thread. */
679             vlc_mutex_unlock( &p_input->stream.stream_lock );
680             p_es->p_decoder_fifo = input_RunDecoder( p_input, p_es );
681             vlc_mutex_lock( &p_input->stream.stream_lock );
682         }
683         break;
684
685     case MPEG1_VIDEO_ES:
686     case MPEG2_VIDEO_ES:
687     case MPEG4_VIDEO_ES:
688     case MSMPEG4v1_VIDEO_ES:
689     case MSMPEG4v2_VIDEO_ES:
690     case MSMPEG4v3_VIDEO_ES:
691     case DVD_SPU_ES:
692         if( config_GetInt( p_input, "video" ) )
693         {
694             /* Release the lock, not to block the input thread during
695              * the creation of the thread. */
696             vlc_mutex_unlock( &p_input->stream.stream_lock );
697             p_es->p_decoder_fifo = input_RunDecoder( p_input, p_es );
698             vlc_mutex_lock( &p_input->stream.stream_lock );
699         }
700         break;
701
702     default:
703         msg_Err( p_input, "unknown stream type 0x%x", p_es->i_type );
704         return( -1 );
705         break;
706     }
707
708     if( p_es->p_decoder_fifo == NULL )
709     {
710         return( -1 );
711     }
712
713     return( 0 );
714 }
715
716 /*****************************************************************************
717  * input_UnselectES: removes an ES from the list of selected ES
718  *****************************************************************************/
719 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
720 {
721
722     int     i_index = 0;
723
724     if( p_es == NULL )
725     {
726         msg_Err( p_input, "nothing to do in input_UnselectES" );
727         return -1;
728     }
729
730     msg_Dbg( p_input, "unselecting ES 0x%x", p_es->i_id );
731
732     if( p_es->p_decoder_fifo == NULL )
733     {
734         msg_Err( p_input, "ES 0x%x is not selected", p_es->i_id );
735         return( -1 );
736     }
737
738     input_EndDecoder( p_input, p_es );
739     p_es->p_pes = NULL;
740
741     if( ( p_es->p_decoder_fifo == NULL ) &&
742         ( p_input->stream.i_selected_es_number > 0 ) )
743     {
744         p_input->stream.i_selected_es_number--;
745
746         while( ( i_index < p_input->stream.i_selected_es_number ) &&
747                ( p_input->stream.pp_selected_es[i_index] != p_es ) )
748         {
749             i_index++;
750         }
751
752         p_input->stream.pp_selected_es[i_index] = 
753           p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
754
755         if( p_input->stream.i_selected_es_number )
756         {
757             p_input->stream.pp_selected_es = realloc(
758                                            p_input->stream.pp_selected_es,
759                                            p_input->stream.i_selected_es_number
760                                            * sizeof(es_descriptor_t *) );
761             if( p_input->stream.pp_selected_es == NULL )
762             {
763                 msg_Err( p_input, "cannot realloc memory" );
764                 return( -1 );
765             }
766         }
767         else
768         {
769             free( p_input->stream.pp_selected_es );   
770             p_input->stream.pp_selected_es = NULL;
771             msg_Dbg( p_input, "no more selected ES" );
772             return( 1 );
773         }
774     }
775
776     return( 0 );
777 }