input_programs.c 26 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
 *****************************************************************************
 * Copyright (C) 1999, 2000 VideoLAN
5
 * $Id: input_programs.c,v 1.57 2001/05/23 17:47:34 stef 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
28
29
#include "defs.h"

#include <stdlib.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
30
#include <string.h>                                    /* memcpy(), memset() */
Sam Hocevar's avatar
   
Sam Hocevar committed
31
#include <sys/types.h>                                              /* off_t */
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include "config.h"
#include "common.h"
#include "threads.h"
#include "mtime.h"
#include "debug.h"

#include "intf_msg.h"

#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
#include "input.h"

46
47
#include "main.h"                                     /* --noaudio --novideo */

48
49
50
51
/*
 * NOTICE : all of these functions expect you to have taken the lock on
 * p_input->stream.lock
 */
52
53
54
55

/*****************************************************************************
 * input_InitStream: init the stream descriptor of the given input
 *****************************************************************************/
56
int input_InitStream( input_thread_t * p_input, size_t i_data_len )
57
{
58
59

    p_input->stream.i_method = INPUT_METHOD_NONE;
60
    p_input->stream.i_stream_id = 0;
61
62
63
64

    /* 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;
65
66
    p_input->stream.pp_es = NULL;
    p_input->stream.pp_selected_es = NULL;
67
68
    p_input->stream.p_removed_es = NULL;
    p_input->stream.p_newly_selected_es = NULL;
69
70
71
72
    p_input->stream.pp_programs = NULL;

    if( i_data_len )
    {
Henri Fallon's avatar
   
Henri Fallon committed
73
74
75
        if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
        {
            intf_ErrMsg( "Unable to allocate memory in input_InitStream");
76
            return 1;
Henri Fallon's avatar
   
Henri Fallon committed
77
        }
78
79
        memset( p_input->stream.p_demux_data, 0, i_data_len );
    }
80
81

    return 0;
82
83
}

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

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

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

107
108
109
110
    if( p_input->stream.p_demux_data != NULL )
    {
        free( p_input->stream.p_demux_data );
    }
111
112
113
114
115
116
117
118
119
120
121
122
}

/*****************************************************************************
 * 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 )
123
        {
124
            return p_input->stream.pp_programs[i];
125
126
        }
    }
127
128

    return( NULL );
129
130
}

131
132
133
134
135
136
/*****************************************************************************
 * 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,
137
                                      u16 i_pgrm_id, size_t i_data_len )
138
{
139
140
    /* Where to add the pgrm */
    int i_pgrm_index = p_input->stream.i_pgrm_number;
141
142
143
144
145
146
147
148

    intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);

    /* 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
149
150
151
    if( p_input->stream.pp_programs == NULL )
    {
        intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
Henri Fallon's avatar
   
Henri Fallon committed
152
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
153
154
    }
    
155
156
157
    /* 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
158
159
160
    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
161
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
162
163
    }
    
164
165
166
    /* 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;
167
    p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
168
169
170
171

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

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

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

177
178
179
180
    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
181
182
183
        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
184
            return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
185
        }
186
187
188
189
        memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
                i_data_len );
    }

190
191
192
193
194
195
196
197
    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.
 *****************************************************************************/
198
void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
199
{
200
    int i_pgrm_index;
201

202
    ASSERT( p_pgrm );
203

204
    intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
205
206

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

212
213
214
215
216
217
    /* Free the demux data */
    if( p_pgrm->p_demux_data != NULL )
    {
        free( p_pgrm->p_demux_data );
    }

218
219
220
221
222
223
224
    /* 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;
    }
225
226
227

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

229
230
231
232
233
    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 *) );
234

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

241
242
243
244
    /* Free the description of this program */
    free( p_pgrm );
}

Stéphane Borel's avatar
   
Stéphane Borel committed
245
246
247
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
/*****************************************************************************
 * 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;

    intf_DbgMsg("Adding description for area %d", i_area_index );

    /* 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;
282
    p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
283
    p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
Stéphane Borel's avatar
   
Stéphane Borel committed
284
    p_input->stream.pp_areas[i_area_index]->i_part= 0;
285
286
    p_input->stream.pp_areas[i_area_index]->i_angle_nb = 1;
    p_input->stream.pp_areas[i_area_index]->i_angle = 0;
Stéphane Borel's avatar
   
Stéphane Borel committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

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

/*****************************************************************************
 * 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 );

302
    intf_DbgMsg("Deleting description for area %d", p_area->i_id );
Stéphane Borel's avatar
   
Stéphane Borel committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323

    /* 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"
324
                     " in input_DelArea" );
Stéphane Borel's avatar
   
Stéphane Borel committed
325
326
327
328
329
330
331
    }

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


332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*****************************************************************************
 * 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 );
348
349
350
351
352
353
354
355
356
357
}

/*****************************************************************************
 * 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,
358
359
                               pgrm_descriptor_t * p_pgrm, u16 i_es_id,
                               size_t i_data_len )
360
{
361
    es_descriptor_t * p_es;
362

363
    intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
364

365
    p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
Henri Fallon's avatar
   
Henri Fallon committed
366
367
368
    if( p_es == NULL )
    {
        intf_ErrMsg( "Unable to allocate memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
369
        return( NULL);
Henri Fallon's avatar
   
Henri Fallon committed
370
    }
371
372
373
374
    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
375
376
377
    if( p_input->stream.pp_es == NULL )
    {
        intf_ErrMsg( "Unable to realloc memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
378
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
379
    }
380
    p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
381

382
    /* Init its values */
383
    p_es->i_id = i_es_id;
384
    p_es->psz_desc[0] = '\0';
385
386
    p_es->p_pes = NULL;
    p_es->p_decoder_fifo = NULL;
387
    p_es->b_audio = 0;
Stéphane Borel's avatar
Stéphane Borel committed
388
    p_es->i_cat = UNKNOWN_ES;
389
390

    if( i_data_len )
391
    {
392
        p_es->p_demux_data = malloc( i_data_len );
Henri Fallon's avatar
   
Henri Fallon committed
393
394
395
        if( p_es->p_demux_data == NULL )
        {
            intf_ErrMsg( "Unable to allocate memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
396
            return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
397
        }
398
399
        memset( p_es->p_demux_data, 0, i_data_len );
    }
400
401
402
403
    else
    {
        p_es->p_demux_data = NULL;
    }
404
405
406
407
408
409
410
411

    /* 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
412
413
414
        if( p_pgrm->pp_es == NULL )
        {
            intf_ErrMsg( "Unable to realloc memory in input_AddES" );
Henri Fallon's avatar
   
Henri Fallon committed
415
            return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
416
        }
417
418
        p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
        p_es->p_pgrm = p_pgrm;
419
420
421
    }
    else
    {
422
        p_es->p_pgrm = NULL;
423
424
425
426
427
428
429
430
    }

    return p_es;
}

/*****************************************************************************
 * input_DelES:
 *****************************************************************************/
431
void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
432
{
433
434
    int                     i_index, i_es_index;
    pgrm_descriptor_t *     p_pgrm;
435

436
437
438
439
440
    ASSERT( p_es );
    p_pgrm = p_es->p_pgrm;

    /* Kill associated decoder, if any. */
    if( p_es->p_decoder_fifo != NULL )
441
    {
442
        input_EndDecoder( p_input, p_es );
443
444
445
446
447
448
    }

    /* Remove this ES from the description of the program if it is associated to
     * one */
    if( p_pgrm )
    {
449
        for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
450
        {
451
            if( p_pgrm->pp_es[i_index] == p_es )
452
453
            {
                p_pgrm->i_es_number--;
454
                p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
455
456
                p_pgrm->pp_es = realloc( p_pgrm->pp_es,
                                         p_pgrm->i_es_number
457
                                          * sizeof(es_descriptor_t *));
458
                if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
Henri Fallon's avatar
   
Henri Fallon committed
459
460
461
                {
                    intf_ErrMsg( "Unable to realloc memory in input_DelES" );
                }
462
463
464
465
466
                break;
            }
        }
    }

467
468
469
470
471
    /* Free the demux data */
    if( p_es->p_demux_data != NULL )
    {
        free( p_es->p_demux_data );
    }
472

473
474
475
476
477
478
479
480
    /* 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;
    }

481
482
    /* Free the ES */
    free( p_es );
483
484
485
486
487
488
    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 *));
489
    if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
Henri Fallon's avatar
   
Henri Fallon committed
490
491
492
493
    {
        intf_ErrMsg( "Unable to realloc memory in input_DelES" );
    }
    
494
495
496
497
498
499
500
501
}

/*****************************************************************************
 * InitDecConfig: initializes a decoder_config_t
 *****************************************************************************/
static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
                          decoder_config_t * p_config )
{
502
    p_config->i_id = p_es->i_id;
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
    p_config->i_type = p_es->i_type;
    p_config->p_stream_ctrl =
        &p_input->stream.control;

    /* Decoder FIFO */
    if( (p_config->p_decoder_fifo =
            (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        return( -1 );
    }

    vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
    vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
    p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
518
    p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
519
    p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
Sam Hocevar's avatar
   
Sam Hocevar committed
520
    p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
521
522
523
524
    p_es->p_decoder_fifo = p_config->p_decoder_fifo;

    p_config->pf_init_bit_stream = InitBitstream;

525
    p_input->stream.i_selected_es_number++;
526

527
528
529
530
531
532
533
534
535
536
537
538
    p_input->stream.pp_selected_es = realloc(
                                       p_input->stream.pp_selected_es,
                                       p_input->stream.i_selected_es_number
                                        * sizeof(es_descriptor_t *) );
    if( p_input->stream.pp_selected_es == NULL )
    {
        intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
        return(-1);
    }
    p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
            = p_es;

539
540
541
542
543
544
545
546
547
548
549
    return( 0 );
}

/*****************************************************************************
 * GetVdecConfig: returns a valid vdec_config_t
 *****************************************************************************/
static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
                                      es_descriptor_t * p_es )
{
    vdec_config_t *     p_config;

Henri Fallon's avatar
   
Henri Fallon committed
550
551
    p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
    if( p_config == NULL )
Henri Fallon's avatar
   
Henri Fallon committed
552
553
    {
        intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
Henri Fallon's avatar
   
Henri Fallon committed
554
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
555
    }
556
557
558
    if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
    {
        free( p_config );
Henri Fallon's avatar
   
Henri Fallon committed
559
        return( NULL );
560
561
562
563
564
565
566
567
568
569
570
571
572
    }

    return( p_config );
}

/*****************************************************************************
 * GetAdecConfig: returns a valid adec_config_t
 *****************************************************************************/
static adec_config_t * GetAdecConfig( input_thread_t * p_input,
                                      es_descriptor_t * p_es )
{
    adec_config_t *     p_config;

Henri Fallon's avatar
   
Henri Fallon committed
573
574
    p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
    if( p_config == NULL )
Henri Fallon's avatar
   
Henri Fallon committed
575
576
    {
        intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
Henri Fallon's avatar
   
Henri Fallon committed
577
        return( NULL );
Henri Fallon's avatar
   
Henri Fallon committed
578
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
579

580
581
582
    if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
    {
        free( p_config );
Henri Fallon's avatar
   
Henri Fallon committed
583
        return( NULL );
584
585
586
587
588
589
590
    }

    return( p_config );
}

/*****************************************************************************
 * input_SelectES: selects an ES and spawns the associated decoder
591
592
593
 *****************************************************************************
 * Remember we are still supposed to have stream_lock when entering this
 * function ?
594
 *****************************************************************************/
595
596
597
/* FIXME */
vlc_thread_t adec_CreateThread( void * );
vlc_thread_t ac3dec_CreateThread( void * );
598
599
vlc_thread_t ac3spdif_CreateThread( void * );
vlc_thread_t spdif_CreateThread( void * );
600
601
vlc_thread_t vpar_CreateThread( void * );
vlc_thread_t spudec_CreateThread( void * );
602
vlc_thread_t lpcmdec_CreateThread( void * );
603

604
605
int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
{
606
607
    /* FIXME ! */
    decoder_capabilities_t  decoder;
608
    void *                  p_config;
609

610
611
612
613
614
615
    if( p_es == NULL )
    {
        intf_ErrMsg( "Nothing to do in input_SelectES" );
        return -1;
    }

616
#ifdef TRACE_INPUT
617
    intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
618
619
620
621
#endif

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

    switch( p_es->i_type )
    {
    case MPEG1_AUDIO_ES:
    case MPEG2_AUDIO_ES:
630
631
        if( p_main->b_audio )
        {
632
            decoder.pf_create_thread = adec_CreateThread;
633
634
635
636
637
638
639
            p_config = (void *)GetAdecConfig( p_input, p_es );

            /* Release the lock, not to block the input thread during
             * the creation of the thread. */
            vlc_mutex_unlock( &p_input->stream.stream_lock );
            p_es->thread_id = input_RunDecoder( &decoder, p_config );
            vlc_mutex_lock( &p_input->stream.stream_lock );
640
        }
641
642
643
644
        break;

    case MPEG1_VIDEO_ES:
    case MPEG2_VIDEO_ES:
645
646
        if( p_main->b_video )
        {
647
            decoder.pf_create_thread = vpar_CreateThread;
648
649
650
651
652
653
654
            p_config = (void *)GetVdecConfig( p_input, p_es );

            /* Release the lock, not to block the input thread during
             * the creation of the thread. */
            vlc_mutex_unlock( &p_input->stream.stream_lock );
            p_es->thread_id = input_RunDecoder( &decoder, p_config );
            vlc_mutex_lock( &p_input->stream.stream_lock );
655
        }
656
657
        break;

658
    case AC3_AUDIO_ES:
659
660
        if( p_main->b_audio )
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
661
            if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) )
662
            {
Sam Hocevar's avatar
   
Sam Hocevar committed
663
                decoder.pf_create_thread = spdif_CreateThread;
664
665
666
            }
            else
            {
Sam Hocevar's avatar
   
Sam Hocevar committed
667
                decoder.pf_create_thread = ac3dec_CreateThread;
668
669
            }

670
671
672
673
674
675
676
            p_config = (void *)GetAdecConfig( p_input, p_es );

            /* Release the lock, not to block the input thread during
             * the creation of the thread. */
            vlc_mutex_unlock( &p_input->stream.stream_lock );
            p_es->thread_id = input_RunDecoder( &decoder, p_config );
            vlc_mutex_lock( &p_input->stream.stream_lock );
677
        }
678
        break;
679
680
681
    case LPCM_AUDIO_ES:
        if( p_main->b_audio )
        {
682
            decoder.pf_create_thread = lpcmdec_CreateThread;
683
            p_config = (void *)GetAdecConfig( p_input, p_es );
684

685
686
687
688
689
690
691
            /* Release the lock, not to block the input thread during
             * the creation of the thread. */
            vlc_mutex_unlock( &p_input->stream.stream_lock );
            p_es->thread_id = input_RunDecoder( &decoder, p_config );
            vlc_mutex_lock( &p_input->stream.stream_lock );
        }
        break;
692
    case DVD_SPU_ES:
693
694
        if( p_main->b_video )
        {
695
            decoder.pf_create_thread = spudec_CreateThread;
696
697
698
699
700
701
702
            p_config = (void *)GetVdecConfig( p_input, p_es );

            /* Release the lock, not to block the input thread during
             * the creation of the thread. */
            vlc_mutex_unlock( &p_input->stream.stream_lock );
            p_es->thread_id = input_RunDecoder( &decoder, p_config );
            vlc_mutex_lock( &p_input->stream.stream_lock );
703
        }
704
705
        break;

706
    default:
Sam Hocevar's avatar
   
Sam Hocevar committed
707
        intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
708
709
710
711
        return( -1 );
        break;
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
712
713
714
715
716
    if( p_es->thread_id == 0 )
    {
        return( -1 );
    }

717
    return( 0 );
718
}
719
720

/*****************************************************************************
721
 * input_UnselectES: removes an ES from the list of selected ES
722
 *****************************************************************************/
723
int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
724
725
726
727
{

    int     i_index = 0;

728
729
730
731
732
733
    if( p_es == NULL )
    {
        intf_ErrMsg( "Nothing to do in input_UnselectES" );
        return -1;
    }

734
#ifdef TRACE_INPUT
735
    intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
736
737
738
739
#endif

    if( p_es->p_decoder_fifo == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
740
        intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
741
742
743
744
745
        return( -1 );
    }

    input_EndDecoder( p_input, p_es );

746
747
    if( ( p_es->p_decoder_fifo == NULL ) &&
        ( p_input->stream.i_selected_es_number > 0 ) )
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
    {
        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 *) );
764

765
766
        if( p_input->stream.pp_selected_es == NULL )
        {
767
#ifdef TRACE_INPUT
Stéphane Borel's avatar
Stéphane Borel committed
768
769
            intf_DbgMsg( "No more selected ES in input_UnselectES" );
#endif
770
            return( 1 );
771
772
        }
    }
773

774
775
    return( 0 );
}