input_programs.c 24.2 KB
Newer Older
1
2
3
/*****************************************************************************
 * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
 *****************************************************************************
4
 * Copyright (C) 1999-2001 VideoLAN
Johan Bilien's avatar
   
Johan Bilien committed
5
 * $Id: input_programs.c,v 1.78 2002/04/08 14:53:05 jobi Exp $
6
 *
7
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27
#include <stdlib.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
28
#include <string.h>                                    /* memcpy(), memset() */
Sam Hocevar's avatar
   
Sam Hocevar committed
29
#include <sys/types.h>                                              /* off_t */
30

Sam Hocevar's avatar
   
Sam Hocevar committed
31
#include <videolan/vlc.h>
32
33
34
35

#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
36
#include "input_ext-plugins.h"
37

Sam Hocevar's avatar
   
Sam Hocevar committed
38
39
#include "debug.h"

40
41
42
43
/*
 * NOTICE : all of these functions expect you to have taken the lock on
 * p_input->stream.lock
 */
44
45
46
47

/*****************************************************************************
 * input_InitStream: init the stream descriptor of the given input
 *****************************************************************************/
48
int input_InitStream( input_thread_t * p_input, size_t i_data_len )
49
{
50

51
    p_input->stream.i_stream_id = 0;
52
53
54
55

    /* initialized to 0 since we don't give the signal to the interface
     * before the end of input initialization */
    p_input->stream.b_changed = 0;
56
57
    p_input->stream.pp_es = NULL;
    p_input->stream.pp_selected_es = NULL;
58
59
    p_input->stream.p_removed_es = NULL;
    p_input->stream.p_newly_selected_es = NULL;
60
    p_input->stream.pp_programs = NULL;
61
62
63
    p_input->stream.p_selected_program = NULL;
    p_input->stream.p_new_program = NULL;
    
64
65
    if( i_data_len )
    {
Henri Fallon's avatar
   
Henri Fallon committed
66
67
68
        if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
        {
            intf_ErrMsg( "Unable to allocate memory in input_InitStream");
69
            return 1;
Henri Fallon's avatar
   
Henri Fallon committed
70
        }
71
72
        memset( p_input->stream.p_demux_data, 0, i_data_len );
    }
73
74
75
76
    else
    {
        p_input->stream.p_demux_data = NULL;
    }
77
78

    return 0;
79
80
}

81
82
83
84
85
/*****************************************************************************
 * input_EndStream: free all stream descriptors
 *****************************************************************************/
void input_EndStream( input_thread_t * p_input )
{
86
    /* Free all programs and associated ES, and associated decoders. */
87
    while( p_input->stream.i_pgrm_number )
88
    {
89
90
        input_DelProgram( p_input, p_input->stream.pp_programs[0] );
    }
91

92
    /* Free standalone ES */
93
    while( p_input->stream.i_es_number )
94
95
96
    {
        input_DelES( p_input, p_input->stream.pp_es[0] );
    }
97

98
99
100
101
102
103
    /* Free all areas */
    while( p_input->stream.i_area_nb )
    {
        input_DelArea( p_input, p_input->stream.pp_areas[0] );
    }

104
105
106
107
108
109
    /* Free selected ES */
    if( p_input->stream.pp_selected_es != NULL )
    {
        free( p_input->stream.pp_selected_es );
    }
    
110
111
112
113
    if( p_input->stream.p_demux_data != NULL )
    {
        free( p_input->stream.p_demux_data );
    }
114
115
116
117
118
119
120
121
122
123
124
125
}

/*****************************************************************************
 * input_FindProgram: returns a pointer to a program described by its ID
 *****************************************************************************/
pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
{
    int     i;

    for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
    {
        if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
126
        {
127
            return p_input->stream.pp_programs[i];
128
129
        }
    }
130
131

    return( NULL );
132
133
}

134
135
136
137
138
139
/*****************************************************************************
 * input_AddProgram: add and init a program descriptor
 *****************************************************************************
 * This program descriptor will be referenced in the given stream descriptor
 *****************************************************************************/
pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
140
                                      u16 i_pgrm_id, size_t i_data_len )
141
{
142
143
    /* Where to add the pgrm */
    int i_pgrm_index = p_input->stream.i_pgrm_number;
144
145
146
147
148
149

    /* Add an entry to the list of program associated with the stream */
    p_input->stream.i_pgrm_number++;
    p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
                                           p_input->stream.i_pgrm_number
                                            * sizeof(pgrm_descriptor_t *) );
Henri Fallon's avatar
   
Henri Fallon committed
150
151
152
    if( p_input->stream.pp_programs == NULL )
    {
        intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
Henri Fallon's avatar
   
Henri Fallon committed
153
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
154
155
    }
    
156
157
158
    /* Allocate the structure to store this description */
    p_input->stream.pp_programs[i_pgrm_index] =
                                        malloc( sizeof(pgrm_descriptor_t) );
Henri Fallon's avatar
   
Henri Fallon committed
159
160
161
    if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
    {
        intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
Henri Fallon's avatar
   
Henri Fallon committed
162
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
163
164
    }
    
165
166
167
    /* Init this entry */
    p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
    p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
168
    p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
169
170
171
172

    p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
    p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;

173
174
    input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );

175
    p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
176
                                                = SYNCHRO_START;
177

178
179
180
181
    if( i_data_len )
    {
        p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
            malloc( i_data_len );
Henri Fallon's avatar
   
Henri Fallon committed
182
183
184
        if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
        {
            intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
Henri Fallon's avatar
   
Henri Fallon committed
185
            return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
186
        }
187
188
189
        memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
                i_data_len );
    }
190
191
192
193
    else
    {
        p_input->stream.pp_programs[i_pgrm_index]->p_demux_data = NULL;
    }
194

195
196
197
198
199
200
201
202
    return p_input->stream.pp_programs[i_pgrm_index];
}

/*****************************************************************************
 * input_DelProgram: destroy a program descriptor
 *****************************************************************************
 * All ES descriptions referenced in the descriptor will be deleted.
 *****************************************************************************/
203
void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
204
{
205
    int i_pgrm_index;
206

207
    ASSERT( p_pgrm );
208
209

    /* Free the structures that describe the es that belongs to that program */
210
    while( p_pgrm->i_es_number )
211
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
212
        input_DelES( p_input, p_pgrm->pp_es[0] );
213
214
    }

215
216
217
218
219
220
    /* Free the demux data */
    if( p_pgrm->p_demux_data != NULL )
    {
        free( p_pgrm->p_demux_data );
    }

221
222
223
224
225
226
227
    /* Find the program in the programs table */
    for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
         i_pgrm_index++ )
    {
        if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
            break;
    }
228
229
230

    /* Remove this program from the stream's list of programs */
    p_input->stream.i_pgrm_number--;
231

232
233
234
235
236
    p_input->stream.pp_programs[i_pgrm_index] =
        p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
    p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
                                           p_input->stream.i_pgrm_number
                                            * sizeof(pgrm_descriptor_t *) );
237

238
    if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
Henri Fallon's avatar
   
Henri Fallon committed
239
    {
240
241
        intf_ErrMsg( "input error: unable to realloc program list"
                     " in input_DelProgram" );
Henri Fallon's avatar
   
Henri Fallon committed
242
    }
243

244
245
246
247
    /* Free the description of this program */
    free( p_pgrm );
}

Stéphane Borel's avatar
   
Stéphane Borel committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*****************************************************************************
 * input_AddArea: add and init an area descriptor
 *****************************************************************************
 * This area descriptor will be referenced in the given stream descriptor
 *****************************************************************************/
input_area_t * input_AddArea( input_thread_t * p_input )
{
    /* Where to add the pgrm */
    int i_area_index = p_input->stream.i_area_nb;

    /* Add an entry to the list of program associated with the stream */
    p_input->stream.i_area_nb++;
    p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
                                        p_input->stream.i_area_nb
                                            * sizeof(input_area_t *) );
    if( p_input->stream.pp_areas == NULL )
    {
        intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
        return( NULL );
    }
    
    /* Allocate the structure to store this description */
    p_input->stream.pp_areas[i_area_index] =
                                        malloc( sizeof(input_area_t) );
    if( p_input->stream.pp_areas[i_area_index] == NULL )
    {
        intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
        return( NULL );
    }
    
    /* Init this entry */
    p_input->stream.pp_areas[i_area_index]->i_id = 0;
    p_input->stream.pp_areas[i_area_index]->i_start = 0;
    p_input->stream.pp_areas[i_area_index]->i_size = 0;
    p_input->stream.pp_areas[i_area_index]->i_tell = 0;
283
    p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
284
    p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
Stéphane Borel's avatar
   
Stéphane Borel committed
285
286
287
288
289
    p_input->stream.pp_areas[i_area_index]->i_part= 0;

    return p_input->stream.pp_areas[i_area_index];
}

290
291
292
293
294
295
/*****************************************************************************
 * input_SetProgram: changes the current program
 *****************************************************************************/
int input_SetProgram( input_thread_t * p_input, pgrm_descriptor_t * p_new_prg )
{
    int i_es_index;
Johan Bilien's avatar
   
Johan Bilien committed
296
297
298
299
300
    int i_required_audio_es;
    int i_required_spu_es;
    int i_audio_es = 0;
    int i_spu_es = 0;

Johan Bilien's avatar
   
Johan Bilien committed
301
    if ( p_input->stream.p_selected_program )
302
    {
Johan Bilien's avatar
   
Johan Bilien committed
303
304
305
        for ( i_es_index = 1 ; /* 0 should be the PMT */
                i_es_index < p_input->stream.p_selected_program->
                i_es_number ;
Johan Bilien's avatar
   
Johan Bilien committed
306
307
                i_es_index ++ )
        {
Johan Bilien's avatar
   
Johan Bilien committed
308
309
#define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
            if ( p_es->p_decoder_fifo )
Johan Bilien's avatar
   
Johan Bilien committed
310
            {
Johan Bilien's avatar
   
Johan Bilien committed
311
                input_UnselectES( p_input , p_es );
Johan Bilien's avatar
   
Johan Bilien committed
312
            }
Johan Bilien's avatar
   
Johan Bilien committed
313
#undef p_es
Johan Bilien's avatar
   
Johan Bilien committed
314
        }
Johan Bilien's avatar
   
Johan Bilien committed
315
316
317
318
319
320
321
    }
    /* Get the number of the required audio stream */
    if( p_main->b_audio )
    {
        /* Default is the first one */
        i_required_audio_es = config_GetIntVariable( "input_channel" );
        if( i_required_audio_es < 0 )
Johan Bilien's avatar
   
Johan Bilien committed
322
        {
Johan Bilien's avatar
   
Johan Bilien committed
323
            i_required_audio_es = 1;
Johan Bilien's avatar
   
Johan Bilien committed
324
        }
Johan Bilien's avatar
   
Johan Bilien committed
325
326
327
328
329
    }
    else
    {
        i_required_audio_es = 0;
    }
Johan Bilien's avatar
   
Johan Bilien committed
330

Johan Bilien's avatar
   
Johan Bilien committed
331
332
333
334
335
336
    /* Same thing for subtitles */
    if( p_main->b_video )
    {
        /* for spu, default is none */
        i_required_spu_es = config_GetIntVariable( "input_subtitle" );
        if( i_required_spu_es < 0 )
Johan Bilien's avatar
   
Johan Bilien committed
337
338
339
        {
            i_required_spu_es = 0;
        }
Johan Bilien's avatar
   
Johan Bilien committed
340
341
342
343
344
345
346
347
    }
    else
    {
        i_required_spu_es = 0;
    }

    for (i_es_index = 0 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
    {
Johan Bilien's avatar
   
Johan Bilien committed
348
349
        switch( p_new_prg->pp_es[i_es_index]->i_cat )
                    {
Johan Bilien's avatar
   
Johan Bilien committed
350
351
352
                       case MPEG1_VIDEO_ES:
                       case MPEG2_VIDEO_ES:
                           intf_WarnMsg( 4, "Selecting ES %x",
Johan Bilien's avatar
   
Johan Bilien committed
353
                                    p_new_prg->pp_es[i_es_index]->i_id );
Johan Bilien's avatar
   
Johan Bilien committed
354
                            input_SelectES( p_input,
Johan Bilien's avatar
   
Johan Bilien committed
355
356
357
358
359
360
361
                                   p_new_prg->pp_es[i_es_index] );
                            break;
                        case MPEG1_AUDIO_ES:
                        case MPEG2_AUDIO_ES:
                            i_audio_es += 1;
                            if( i_audio_es == i_required_audio_es )
                            {
Johan Bilien's avatar
   
Johan Bilien committed
362
363
364
                                intf_WarnMsg( 4, "Selecting ES %x",
                                    p_new_prg->pp_es[i_es_index]->i_id );
                                input_SelectES( p_input,
Johan Bilien's avatar
   
Johan Bilien committed
365
366
367
368
369
370
371
372
                                    p_new_prg->pp_es[i_es_index]);
                            }
                            break;
                        case LPCM_AUDIO_ES :
                        case AC3_AUDIO_ES :
                            i_audio_es += 1;
                            if( i_audio_es == i_required_audio_es )
                            {
Johan Bilien's avatar
   
Johan Bilien committed
373
                                intf_WarnMsg( 4, "Selecting ES %x",
Johan Bilien's avatar
   
Johan Bilien committed
374
                                    p_new_prg->pp_es[i_es_index]->i_id );
Johan Bilien's avatar
   
Johan Bilien committed
375
                                input_SelectES( p_input,
Johan Bilien's avatar
   
Johan Bilien committed
376
377
378
379
380
381
382
383
                                    p_new_prg->pp_es[i_es_index] );
                            }
                            break;
                        /* Not sure this one is fully specification-compliant */
                        case DVD_SPU_ES :
                            i_spu_es += 1;
                            if( i_spu_es == i_required_spu_es )
                            {
Johan Bilien's avatar
   
Johan Bilien committed
384
                                intf_WarnMsg( 4, "Selecting ES %x",
Johan Bilien's avatar
   
Johan Bilien committed
385
                                    p_new_prg->pp_es[i_es_index]->i_id );
Johan Bilien's avatar
   
Johan Bilien committed
386
                                input_SelectES( p_input,
Johan Bilien's avatar
   
Johan Bilien committed
387
388
389
390
391
392
393
                                    p_new_prg->pp_es[i_es_index] );
                            }
                            break;
                        default :
                            break;
                    }

394
    }
Johan Bilien's avatar
   
Johan Bilien committed
395

396
397
398
399
400
401
402

    p_input->stream.p_selected_program = p_new_prg;

    return( 0 );
}


Stéphane Borel's avatar
   
Stéphane Borel committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*****************************************************************************
 * input_DelArea: destroy a area descriptor
 *****************************************************************************
 * All ES descriptions referenced in the descriptor will be deleted.
 *****************************************************************************/
void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
{
    int i_area_index;

    ASSERT( p_area );

    /* Find the area in the areas table */
    for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
         i_area_index++ )
    {
        if( p_input->stream.pp_areas[i_area_index] == p_area )
            break;
    }

    /* Remove this area from the stream's list of areas */
    p_input->stream.i_area_nb--;

    p_input->stream.pp_areas[i_area_index] =
        p_input->stream.pp_areas[p_input->stream.i_area_nb];
    p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
                                           p_input->stream.i_area_nb
                                            * sizeof(input_area_t *) );

    if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
    {
        intf_ErrMsg( "input error: unable to realloc area list"
434
                     " in input_DelArea" );
Stéphane Borel's avatar
   
Stéphane Borel committed
435
436
437
438
439
440
441
    }

    /* Free the description of this area */
    free( p_area );
}


442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*****************************************************************************
 * input_FindES: returns a pointer to an ES described by its ID
 *****************************************************************************/
es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
{
    int     i;

    for( i = 0; i < p_input->stream.i_es_number; i++ )
    {
        if( p_input->stream.pp_es[i]->i_id == i_es_id )
        {
            return p_input->stream.pp_es[i];
        }
    }

    return( NULL );
458
459
460
461
462
463
464
465
466
467
}

/*****************************************************************************
 * input_AddES:
 *****************************************************************************
 * Reserve a slot in the table of ES descriptors for the ES and add it to the
 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
 * alone (PSI ?)
 *****************************************************************************/
es_descriptor_t * input_AddES( input_thread_t * p_input,
468
469
                               pgrm_descriptor_t * p_pgrm, u16 i_es_id,
                               size_t i_data_len )
470
{
471
    es_descriptor_t * p_es;
472

473
    p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
Henri Fallon's avatar
   
Henri Fallon committed
474
475
476
    if( p_es == NULL )
    {
        intf_ErrMsg( "Unable to allocate memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
477
        return( NULL);
Henri Fallon's avatar
   
Henri Fallon committed
478
    }
479
480
481
482
    p_input->stream.i_es_number++;
    p_input->stream.pp_es = realloc( p_input->stream.pp_es,
                                     p_input->stream.i_es_number
                                      * sizeof(es_descriptor_t *) );
Henri Fallon's avatar
   
Henri Fallon committed
483
484
485
    if( p_input->stream.pp_es == NULL )
    {
        intf_ErrMsg( "Unable to realloc memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
486
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
487
    }
488
    p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
489

490
    /* Init its values */
491
    p_es->i_id = i_es_id;
492
    p_es->psz_desc[0] = '\0';
493
494
    p_es->p_pes = NULL;
    p_es->p_decoder_fifo = NULL;
495
    p_es->b_audio = 0;
Stéphane Borel's avatar
Stéphane Borel committed
496
    p_es->i_cat = UNKNOWN_ES;
Johan Bilien's avatar
Johan Bilien committed
497
498
499
#ifdef HAVE_SATELLITE
    p_es->i_dmx_fd = 0;
#endif
500
501

    if( i_data_len )
502
    {
503
        p_es->p_demux_data = malloc( i_data_len );
Henri Fallon's avatar
   
Henri Fallon committed
504
505
506
        if( p_es->p_demux_data == NULL )
        {
            intf_ErrMsg( "Unable to allocate memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
507
            return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
508
        }
509
510
        memset( p_es->p_demux_data, 0, i_data_len );
    }
511
512
513
514
    else
    {
        p_es->p_demux_data = NULL;
    }
515
516
517
518
519
520
521
522

    /* Add this ES to the program definition if one is given */
    if( p_pgrm )
    {
        p_pgrm->i_es_number++;
        p_pgrm->pp_es = realloc( p_pgrm->pp_es,
                                 p_pgrm->i_es_number
                                  * sizeof(es_descriptor_t *) );
Henri Fallon's avatar
   
Henri Fallon committed
523
524
525
        if( p_pgrm->pp_es == NULL )
        {
            intf_ErrMsg( "Unable to realloc memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
526
            return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
527
        }
528
529
        p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
        p_es->p_pgrm = p_pgrm;
530
531
532
    }
    else
    {
533
        p_es->p_pgrm = NULL;
534
535
536
537
538
539
540
541
    }

    return p_es;
}

/*****************************************************************************
 * input_DelES:
 *****************************************************************************/
542
void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
543
{
544
545
    int                     i_index, i_es_index;
    pgrm_descriptor_t *     p_pgrm;
546

547
548
549
550
551
    ASSERT( p_es );
    p_pgrm = p_es->p_pgrm;

    /* Kill associated decoder, if any. */
    if( p_es->p_decoder_fifo != NULL )
552
    {
553
        input_EndDecoder( p_input, p_es );
554
555
556
557
558
559
    }

    /* Remove this ES from the description of the program if it is associated to
     * one */
    if( p_pgrm )
    {
560
        for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
561
        {
562
            if( p_pgrm->pp_es[i_index] == p_es )
563
564
            {
                p_pgrm->i_es_number--;
565
                p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
566
567
                p_pgrm->pp_es = realloc( p_pgrm->pp_es,
                                         p_pgrm->i_es_number
568
                                          * sizeof(es_descriptor_t *));
569
                if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
Henri Fallon's avatar
   
Henri Fallon committed
570
571
572
                {
                    intf_ErrMsg( "Unable to realloc memory in input_DelES" );
                }
573
574
575
576
577
                break;
            }
        }
    }

578
579
580
581
582
    /* Free the demux data */
    if( p_es->p_demux_data != NULL )
    {
        free( p_es->p_demux_data );
    }
583

584
585
586
587
588
589
590
591
    /* Find the ES in the ES table */
    for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
         i_es_index++ )
    {
        if( p_input->stream.pp_es[i_es_index] == p_es )
            break;
    }

592
593
    /* Free the ES */
    free( p_es );
594
595
596
597
598
599
    p_input->stream.i_es_number--;
    p_input->stream.pp_es[i_es_index] =
                    p_input->stream.pp_es[p_input->stream.i_es_number];
    p_input->stream.pp_es = realloc( p_input->stream.pp_es,
                                     p_input->stream.i_es_number
                                      * sizeof(es_descriptor_t *));
600
    if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
Henri Fallon's avatar
   
Henri Fallon committed
601
602
603
604
    {
        intf_ErrMsg( "Unable to realloc memory in input_DelES" );
    }
    
605
606
607
608
}

/*****************************************************************************
 * input_SelectES: selects an ES and spawns the associated decoder
609
610
611
 *****************************************************************************
 * Remember we are still supposed to have stream_lock when entering this
 * function ?
612
613
614
 *****************************************************************************/
int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
{
615
616
    if( p_es == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
617
        intf_ErrMsg( "input error: nothing to do in input_SelectES" );
618
619
620
        return -1;
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
621
    intf_WarnMsg( 4, "input: selecting ES 0x%x", p_es->i_id );
622
623
624

    if( p_es->p_decoder_fifo != NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
625
        intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
626
627
628
        return( -1 );
    }

gbazin's avatar
   
gbazin committed
629
630
    p_es->thread_id = 0;

631
632
    switch( p_es->i_type )
    {
Henri Fallon's avatar
   
Henri Fallon committed
633
    case AC3_AUDIO_ES:
634
635
    case MPEG1_AUDIO_ES:
    case MPEG2_AUDIO_ES:
Henri Fallon's avatar
   
Henri Fallon committed
636
    case LPCM_AUDIO_ES:
637
638
        if( p_main->b_audio )
        {
639
640
641
            /* Release the lock, not to block the input thread during
             * the creation of the thread. */
            vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
642
            p_es->thread_id = input_RunDecoder( p_input, p_es );
643
            vlc_mutex_lock( &p_input->stream.stream_lock );
644
        }
645
646
647
648
        break;

    case MPEG1_VIDEO_ES:
    case MPEG2_VIDEO_ES:
649
    case DVD_SPU_ES:
650
651
        if( p_main->b_video )
        {
652
653
654
            /* Release the lock, not to block the input thread during
             * the creation of the thread. */
            vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
655
            p_es->thread_id = input_RunDecoder( p_input, p_es );
656
            vlc_mutex_lock( &p_input->stream.stream_lock );
657
        }
658
659
        break;

660
    default:
Sam Hocevar's avatar
   
Sam Hocevar committed
661
        intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
662
663
664
665
        return( -1 );
        break;
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
666
667
668
669
670
    if( p_es->thread_id == 0 )
    {
        return( -1 );
    }

671
    return( 0 );
672
}
673
674

/*****************************************************************************
675
 * input_UnselectES: removes an ES from the list of selected ES
676
 *****************************************************************************/
677
int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
678
679
680
681
{

    int     i_index = 0;

682
683
684
685
686
687
    if( p_es == NULL )
    {
        intf_ErrMsg( "Nothing to do in input_UnselectES" );
        return -1;
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
688
    intf_WarnMsg( 4, "input: unselecting ES 0x%x", p_es->i_id );
689
690
691

    if( p_es->p_decoder_fifo == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
692
        intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
693
694
695
696
        return( -1 );
    }

    input_EndDecoder( p_input, p_es );
Johan Bilien's avatar
   
Johan Bilien committed
697
    p_es->p_pes = NULL;
698

699
700
    if( ( p_es->p_decoder_fifo == NULL ) &&
        ( p_input->stream.i_selected_es_number > 0 ) )
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
    {
        p_input->stream.i_selected_es_number--;

        while( ( i_index < p_input->stream.i_selected_es_number ) &&
               ( p_input->stream.pp_selected_es[i_index] != p_es ) )
        {
            i_index++;
        }

        p_input->stream.pp_selected_es[i_index] = 
          p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];

        p_input->stream.pp_selected_es = realloc(
                                           p_input->stream.pp_selected_es,
                                           p_input->stream.i_selected_es_number
                                            * sizeof(es_descriptor_t *) );
717

718
719
        if( p_input->stream.pp_selected_es == NULL )
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
720
            intf_WarnMsg( 4, "input: no more selected ES in input_UnselectES" );
721
            return( 1 );
722
723
        }
    }
724

725
726
    return( 0 );
}