ts.c 62.6 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * mpeg_ts.c : Transport Stream input module for vlc
 *****************************************************************************
 * Copyright (C) 2000-2001 VideoLAN
5
 * $Id: ts.c,v 1.31 2003/08/01 18:49:51 fenrir Exp $
6
7
8
9
10
11
12
13
 *
 * Authors: Henri Fallon <henri@via.ecp.fr>
 *          Johan Bilien <jobi@via.ecp.fr>
 *
 * 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.
14
 *
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * 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
 *****************************************************************************/
#include <stdlib.h>
#include <string.h>

gbazin's avatar
   
gbazin committed
31
32
33
34
#ifdef HAVE_STDINT_H
#   include <stdint.h>                                            /* uint8_t */
#endif

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <vlc/vlc.h>
#include <vlc/input.h>

#include "iso_lang.h"

#if defined MODULE_NAME_IS_ts_dvbpsi
#   ifdef HAVE_DVBPSI_DR_H
#       include <dvbpsi/dvbpsi.h>
#       include <dvbpsi/descriptor.h>
#       include <dvbpsi/pat.h>
#       include <dvbpsi/pmt.h>
#       include <dvbpsi/dr.h>
#   else
#       include "dvbpsi.h"
#       include "descriptor.h"
#       include "tables/pat.h"
#       include "tables/pmt.h"
#       include "descriptors/dr.h"
#   endif
#endif

56
#include "system.h"
57
#include "codecs.h"
58

59
60
61
62
63
/*****************************************************************************
 * Constants
 *****************************************************************************/
#define TS_READ_ONCE 200

64
65
66
67
68
69
70
71
72
/*****************************************************************************
 * Private structure
 *****************************************************************************/
struct demux_sys_t
{
    module_t *   p_module;
    mpeg_demux_t mpeg;
};

73
74
75
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
76
77
78
static int  Activate   ( vlc_object_t * );
static void Deactivate ( vlc_object_t * );
static int  Demux      ( input_thread_t * );
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

#if defined MODULE_NAME_IS_ts
static void TSDemuxPSI ( input_thread_t *, data_packet_t *,
                          es_descriptor_t *, vlc_bool_t );
static void TSDecodePAT( input_thread_t *, es_descriptor_t *);
static void TSDecodePMT( input_thread_t *, es_descriptor_t *);
#define PSI_CALLBACK TSDemuxPSI
#elif defined MODULE_NAME_IS_ts_dvbpsi
static void TS_DVBPSI_DemuxPSI  ( input_thread_t *, data_packet_t *,
                                  es_descriptor_t *, vlc_bool_t );
static void TS_DVBPSI_HandlePAT ( input_thread_t *, dvbpsi_pat_t * );
static void TS_DVBPSI_HandlePMT ( input_thread_t *, dvbpsi_pmt_t * );
#define PSI_CALLBACK TS_DVBPSI_DemuxPSI
#endif

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Christophe Massiot's avatar
Christophe Massiot committed
97
#define VLS_BACKWARDS_COMPAT_TEXT N_("Compatibility with pre-0.4 VLS")
98
99
100
101
102
#define VLS_BACKWARDS_COMPAT_LONGTEXT N_( \
    "The protocol for transmitting A/52 audio streams changed between VLC " \
    "0.3.x and 0.4. By default VLC assumes you have the latest VLS. In case " \
    "you're using an old version, select this option.")

Christophe Massiot's avatar
Christophe Massiot committed
103
#define BUGGY_PSI_TEXT N_("Buggy PSI")
104
105
106
107
#define BUGGY_PSI_LONGTEXT N_( \
    "If you have a stream whose PSI packets do not feature incremented " \
    "continuity counters, select this option.")

108
109
110
111
112
113
114
115
116
117
vlc_module_begin();
#if defined MODULE_NAME_IS_ts
    set_description( _("ISO 13818-1 MPEG Transport Stream input") );
    set_capability( "demux", 160 );
    add_shortcut( "ts" );
#elif defined MODULE_NAME_IS_ts_dvbpsi
    set_description( _("ISO 13818-1 MPEG Transport Stream input (libdvbpsi)") );
    set_capability( "demux", 170 );
    add_shortcut( "ts_dvbpsi" );
#endif
118
    add_category_hint( N_("Miscellaneous"), NULL, VLC_TRUE );
119
    add_bool( "vls-backwards-compat", 0, NULL,
120
121
              VLS_BACKWARDS_COMPAT_TEXT, VLS_BACKWARDS_COMPAT_LONGTEXT, VLC_TRUE );
    add_bool( "buggy-psi", 0, NULL, BUGGY_PSI_TEXT, BUGGY_PSI_LONGTEXT, VLC_TRUE );
122
    set_callbacks( Activate, Deactivate );
123
124
125
vlc_module_end();

/*****************************************************************************
126
 * Activate: initialize TS structures
127
128
129
130
 *****************************************************************************/
static int Activate( vlc_object_t * p_this )
{
    input_thread_t *    p_input = (input_thread_t *)p_this;
131
132
133
134
135
    demux_sys_t *       p_demux;
    es_descriptor_t *   p_pat_es;
    es_ts_data_t *      p_demux_data;
    stream_ts_data_t *  p_stream_data;
    byte_t *            p_peek;
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

    /* Set the demux function */
    p_input->pf_demux = Demux;

    /* Initialize access plug-in structures. */
    if( p_input->i_mtu == 0 )
    {
        /* Improve speed. */
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
    }

    /* Have a peep at the show. */
    if( input_Peek( p_input, &p_peek, 1 ) < 1 )
    {
        msg_Err( p_input, "cannot peek()" );
151
        return -1;
152
153
154
155
    }

    if( *p_peek != TS_SYNC_CODE )
    {
156
157
        if( *p_input->psz_demux && ( !strncmp( p_input->psz_demux, "ts", 3 )
                         || !strncmp( p_input->psz_demux, "ts_dvbpsi", 10 ) ) )
158
159
160
161
162
163
164
        {
            /* User forced */
            msg_Err( p_input, "this does not look like a TS stream, continuing" );
        }
        else
        {
            msg_Warn( p_input, "TS module discarded (no sync)" );
165
            return -1;
166
167
168
169
170
171
172
173
174
175
        }
    }

    /* Adapt the bufsize for our only use. */
    if( p_input->i_mtu != 0 )
    {
        /* Have minimum granularity to avoid bottlenecks at the input level. */
        p_input->i_bufsize = (p_input->i_mtu / TS_PACKET_SIZE) * TS_PACKET_SIZE;
    }

176
177
178
179
180
181
182
183
184
185
186
187
188
189
    p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t ) );
    if( p_demux == NULL )
    {
        return -1;
    }

    p_input->p_private = (void*)&p_demux->mpeg;
    p_demux->p_module = module_Need( p_input, "mpeg-system", NULL );
    if( p_demux->p_module == NULL )
    {
        free( p_input->p_demux_data );
        return -1;
    }

190
191
192
193
    vlc_mutex_lock( &p_input->stream.stream_lock );

    if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
    {
194
195
196
        module_Unneed( p_input, p_demux->p_module );
        free( p_input->p_demux_data );
        return -1;
197
    }
198

199
200
    p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
    p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
201
    p_stream_data->b_buggy_psi = config_GetInt( p_input, "buggy-psi" );
202
203
204

#ifdef MODULE_NAME_IS_ts_dvbpsi
    p_stream_data->p_pat_handle = (dvbpsi_handle *)
205
      dvbpsi_AttachPAT( (dvbpsi_pat_callback) &TS_DVBPSI_HandlePAT, p_input );
206
207
208
209

    if( p_stream_data->p_pat_handle == NULL )
    {
        msg_Err( p_input, "could not create PAT decoder" );
210
211
212
        module_Unneed( p_input, p_demux->p_module );
        free( p_input->p_demux_data );
        return -1;
213
214
    }
#endif
215

216
217
218
    /* We'll have to catch the PAT in order to continue
     * Then the input will catch the PMT and then the others ES
     * The PAT es is indepedent of any program. */
gbazin's avatar
   
gbazin committed
219
220
    p_pat_es = input_AddES( p_input, NULL, 0x00,
                            UNKNOWN_ES, NULL, sizeof( es_ts_data_t ) );
221
    p_pat_es->i_fourcc = VLC_FOURCC( 'p', 'a', 't', ' ' );
222
223
224
225
226
    p_demux_data = (es_ts_data_t *)p_pat_es->p_demux_data;
    p_demux_data->b_psi = 1;
    p_demux_data->i_psi_type = PSI_IS_PAT;
    p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
    p_demux_data->p_psi_section->b_is_complete = 1;
227
    p_demux_data->i_continuity_counter = 0xFF;
228
229

    vlc_mutex_unlock( &p_input->stream.stream_lock );
230

231
232
233
234
235
236
237
238
239
240
241
242
    return 0;
}

/*****************************************************************************
 * Deactivate: deinitialize TS structures
 *****************************************************************************/
static void Deactivate( vlc_object_t * p_this )
{
    input_thread_t *    p_input = (input_thread_t *)p_this;

    module_Unneed( p_input, p_input->p_demux_data->p_module );
    free( p_input->p_demux_data );
243
244
245
246
247
248
249
250
251
252
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
 * packets.
 *****************************************************************************/
static int Demux( input_thread_t * p_input )
{
253
    demux_sys_t *   p_demux = p_input->p_demux_data;
254
255
256
257
258
259
260
261
262
263
    int             i_read_once = (p_input->i_mtu ?
                                   p_input->i_bufsize / TS_PACKET_SIZE :
                                   TS_READ_ONCE);
    int             i;

    for( i = 0; i < i_read_once; i++ )
    {
        data_packet_t *     p_data;
        ssize_t             i_result;

264
        i_result = p_demux->mpeg.pf_read_ts( p_input, &p_data );
265
266
267

        if( i_result <= 0 )
        {
268
            return i_result;
269
270
        }

271
272
        p_demux->mpeg.pf_demux_ts( p_input, p_data,
                                   (psi_callback_t) &PSI_CALLBACK );
273
274
    }

275
    return i_read_once;
276
277
278
279
280
281
282
283
284
285
286
}


#if defined MODULE_NAME_IS_ts
/*
 * PSI demultiplexing and decoding without libdvbpsi
 */

/*****************************************************************************
 * DemuxPSI : makes up complete PSI data
 *****************************************************************************/
287
static void TSDemuxPSI( input_thread_t * p_input, data_packet_t * p_data,
288
289
290
        es_descriptor_t * p_es, vlc_bool_t b_unit_start )
{
    es_ts_data_t  * p_demux_data;
291

292
293
294
295
296
297
298
299
300
    p_demux_data = (es_ts_data_t *)p_es->p_demux_data;

#define p_psi (p_demux_data->p_psi_section)
#define p (p_data->p_payload_start)

    if( b_unit_start )
    {
        /* unit_start set to 1 -> presence of a pointer field
         * (see ISO/IEC 13818 (2.4.4.2) which should be set to 0x00 */
301
        if( (uint8_t)p[0] != 0x00 )
302
303
304
        {
            msg_Warn( p_input,
                      "non-zero pointer field found, trying to continue" );
305
            p+=(uint8_t)p[0];
306
307
308
309
310
311
312
313
        }
        else
        {
            p++;
        }

        /* This is the begining of a new section */

314
        if( ((uint8_t)(p[1]) & 0xc0) != 0x80 )
315
316
317
318
        {
            msg_Warn( p_input, "invalid PSI packet" );
            p_psi->b_trash = 1;
        }
319
        else
320
321
322
323
        {
            p_psi->i_section_length = ((p[1] & 0xF) << 8) | p[2];
            p_psi->b_section_complete = 0;
            p_psi->i_read_in_section = 0;
324
            p_psi->i_section_number = (uint8_t)p[6];
325
326
327
328
329
330
331

            if( p_psi->b_is_complete || p_psi->i_section_number == 0 )
            {
                /* This is a new PSI packet */
                p_psi->b_is_complete = 0;
                p_psi->b_trash = 0;
                p_psi->i_version_number = ( p[5] >> 1 ) & 0x1f;
332
                p_psi->i_last_section_number = (uint8_t)p[7];
333
334
335
336
337
338
339
340
341
342

                /* We'll write at the begining of the buffer */
                p_psi->p_current = p_psi->buffer;
            }
            else
            {
                if( p_psi->b_section_complete )
                {
                    /* New Section of an already started PSI */
                    p_psi->b_section_complete = 0;
343

344
345
346
347
348
349
                    if( p_psi->i_version_number != (( p[5] >> 1 ) & 0x1f) )
                    {
                        msg_Warn( p_input,
                                  "PSI version differs inside same PAT" );
                        p_psi->b_trash = 1;
                    }
350
                    if( p_psi->i_section_number + 1 != (uint8_t)p[6] )
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
                    {
                        msg_Warn( p_input,
                                  "PSI Section discontinuity, packet lost?" );
                        p_psi->b_trash = 1;
                    }
                    else
                        p_psi->i_section_number++;
                }
                else
                {
                    msg_Warn( p_input, "got unexpected new PSI section" );
                    p_psi->b_trash = 1;
                }
            }
        }
    } /* b_unit_start */
367

368
369
370
371
372
373
374
    if( !p_psi->b_trash )
    {
        /* read */
        if( (p_data->p_payload_end - p) >=
            ( p_psi->i_section_length - p_psi->i_read_in_section ) )
        {
            /* The end of the section is in this TS packet */
375
            memcpy( p_psi->p_current, p,
376
            (p_psi->i_section_length - p_psi->i_read_in_section) );
377

378
            p_psi->b_section_complete = 1;
379
            p_psi->p_current +=
380
                (p_psi->i_section_length - p_psi->i_read_in_section);
381

382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
            if( p_psi->i_section_number == p_psi->i_last_section_number )
            {
                /* This was the last section of PSI */
                p_psi->b_is_complete = 1;

                switch( p_demux_data->i_psi_type)
                {
                case PSI_IS_PAT:
                    TSDecodePAT( p_input, p_es );
                    break;
                case PSI_IS_PMT:
                    TSDecodePMT( p_input, p_es );
                    break;
                default:
                    msg_Warn( p_input, "received unknown PSI in DemuxPSI" );
                }
            }
        }
        else
        {
            memcpy( p_psi->buffer, p, p_data->p_payload_end - p );
            p_psi->i_read_in_section += p_data->p_payload_end - p;

            p_psi->p_current += p_data->p_payload_end - p;
        }
    }

409
#undef p_psi
410
#undef p
411

412
    input_DeletePacket( p_input->p_method_data, p_data );
413

414
415
416
417
418
419
420
421
422
423
424
425
426
    return ;
}

/*****************************************************************************
 * DecodePAT : Decodes Programm association table and deal with it
 *****************************************************************************/
static void TSDecodePAT( input_thread_t * p_input, es_descriptor_t * p_es )
{
    stream_ts_data_t  * p_stream_data;
    es_ts_data_t      * p_demux_data;

    pgrm_descriptor_t * p_pgrm;
    es_descriptor_t   * p_current_es;
427
    byte_t            * p_current_data;
428
429
430
431
432
433
434
435

    int                 i_section_length, i_program_id, i_pmt_pid;
    int                 i_loop, i_current_section;

    vlc_bool_t          b_changed = 0;

    p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
    p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
436

437
438
439
440
441
442
443
444
445
446
447
448
#define p_psi (p_demux_data->p_psi_section)

    /* Not so fast, Mike ! If the PAT version has changed, we first check
     * that its content has really changed before doing anything */
    if( p_stream_data->i_pat_version != p_psi->i_version_number )
    {
        int i_programs = p_input->stream.i_pgrm_number;

        p_current_data = p_psi->buffer;

        do
        {
449
            i_section_length = ((uint32_t)(p_current_data[1] & 0xF) << 8) |
450
                                 p_current_data[2];
451
            i_current_section = (uint8_t)p_current_data[6];
452

453
454
455
456
            for( i_loop = 0;
                 ( i_loop < (i_section_length - 9) / 4 ) && !b_changed;
                 i_loop++ )
            {
457
                i_program_id = ( (uint32_t)*(p_current_data + i_loop * 4 + 8) << 8 )
458
                                 | *(p_current_data + i_loop * 4 + 9);
459
                i_pmt_pid = ( ((uint32_t)*(p_current_data + i_loop * 4 + 10) & 0x1F)
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
                                    << 8 )
                               | *(p_current_data + i_loop * 4 + 11);

                if( i_program_id )
                {
                    if( (p_pgrm = input_FindProgram( p_input, i_program_id ))
                        && (p_current_es = input_FindES( p_input, i_pmt_pid ))
                        && p_current_es->p_pgrm == p_pgrm
                        && p_current_es->i_id == i_pmt_pid
                        && ((es_ts_data_t *)p_current_es->p_demux_data)->b_psi
                        && ((es_ts_data_t *)p_current_es->p_demux_data)
                            ->i_psi_type == PSI_IS_PMT )
                    {
                        i_programs--;
                    }
                    else
                    {
                        b_changed = 1;
                    }
                }
            }
481

482
483
484
485
486
487
488
489
490
491
492
493
            p_current_data += 3 + i_section_length;

        } while( ( i_current_section < p_psi->i_last_section_number )
                  && !b_changed );

        /* If we didn't find the expected amount of programs, the PAT has
         * changed. Otherwise, it only changed if b_changed is already != 0 */
        b_changed = b_changed || i_programs;
    }

    if( b_changed )
    {
494
        /* PAT has changed. We are going to delete all programs and
495
496
497
498
499
         * create new ones. We chose not to only change what was needed
         * as a PAT change may mean the stream is radically changing and
         * this is a secure method to avoid crashes */
        es_ts_data_t      * p_es_demux;
        pgrm_ts_data_t    * p_pgrm_demux;
500

501
502
503
504
505
506
507
        p_current_data = p_psi->buffer;

        /* Delete all programs */
        while( p_input->stream.i_pgrm_number )
        {
            input_DelProgram( p_input, p_input->stream.pp_programs[0] );
        }
508

509
510
        do
        {
511
            i_section_length = ((uint32_t)(p_current_data[1] & 0xF) << 8) |
512
                                 p_current_data[2];
513
            i_current_section = (uint8_t)p_current_data[6];
514

515
516
            for( i_loop = 0; i_loop < (i_section_length - 9) / 4 ; i_loop++ )
            {
517
                i_program_id = ( (uint32_t)*(p_current_data + i_loop * 4 + 8) << 8 )
518
                                 | *(p_current_data + i_loop * 4 + 9);
519
                i_pmt_pid = ( ((uint32_t)*(p_current_data + i_loop * 4 + 10) & 0x1F)
520
521
                                    << 8 )
                               | *(p_current_data + i_loop * 4 + 11);
522

523
524
525
526
                /* If program = 0, we're having info about NIT not PMT */
                if( i_program_id )
                {
                    /* Add this program */
527
                    p_pgrm = input_AddProgram( p_input, i_program_id,
528
                                               sizeof( pgrm_ts_data_t ) );
529

530
531
532
                    /* whatis the PID of the PMT of this program */
                    p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
                    p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
533

534
                    /* Add the PMT ES to this program */
535
                    p_current_es = input_AddES( p_input, p_pgrm,(uint16_t)i_pmt_pid,
gbazin's avatar
   
gbazin committed
536
                        UNKNOWN_ES, NULL, sizeof( es_ts_data_t) );
537
                    p_current_es->i_fourcc = VLC_FOURCC( 'p', 'm', 't', ' ' );
538
539
540
                    p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
                    p_es_demux->b_psi = 1;
                    p_es_demux->i_psi_type = PSI_IS_PMT;
541
542

                    p_es_demux->p_psi_section =
543
544
                                            malloc( sizeof( psi_section_t ) );
                    p_es_demux->p_psi_section->b_is_complete = 0;
545
                    p_es_demux->i_continuity_counter = 0xFF;
546
547
                }
            }
548

549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
            p_current_data += 3 + i_section_length;

        } while( i_current_section < p_psi->i_last_section_number );

        /* Go to the beginning of the next section */
        p_stream_data->i_pat_version = p_psi->i_version_number;

    }
#undef p_psi

}

/*****************************************************************************
 * DecodePMT : decode a given Program Stream Map
 * ***************************************************************************
 * When the PMT changes, it may mean a deep change in the stream, and it is
 * careful to delete the ES and add them again. If the PMT doesn't change,
 * there no need to do anything.
 *****************************************************************************/
static void TSDecodePMT( input_thread_t * p_input, es_descriptor_t * p_es )
{

    pgrm_ts_data_t            * p_pgrm_data;
    es_ts_data_t              * p_demux_data;
573
    vlc_bool_t b_vls_compat = config_GetInt( p_input, "vls-backwards-compat" );
574
575
576

    p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
    p_pgrm_data = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
577

578
579
#define p_psi (p_demux_data->p_psi_section)

580
    if( p_psi->i_version_number != p_pgrm_data->i_pmt_version )
581
    {
582
        es_descriptor_t   * p_new_es;
583
584
585
586
587
        es_ts_data_t      * p_es_demux;
        byte_t            * p_current_data, * p_current_section;
        int                 i_section_length,i_current_section;
        int                 i_prog_info_length, i_loop;
        int                 i_es_info_length, i_pid, i_stream_type;
588

589
590
591
        p_current_section = p_psi->buffer;
        p_current_data = p_psi->buffer;

592
        p_pgrm_data->i_pcr_pid = ( ((uint32_t)*(p_current_section + 8) & 0x1F) << 8 ) |
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
                                    *(p_current_section + 9);


        /* Lock stream information */
        vlc_mutex_lock( &p_input->stream.stream_lock );

        /* Delete all ES in this program  except the PSI. We start from the
         * end because i_es_number gets decremented after each deletion. */
        for( i_loop = p_es->p_pgrm->i_es_number ; i_loop ; )
        {
            i_loop--;
            p_es_demux = (es_ts_data_t *)
                         p_es->p_pgrm->pp_es[i_loop]->p_demux_data;
            if ( ! p_es_demux->b_psi )
            {
                input_DelES( p_input, p_es->p_pgrm->pp_es[i_loop] );
            }
        }

        /* Then add what we received in this PMT */
        do
        {
615
            i_section_length = ( ((uint32_t)*(p_current_data + 1) & 0xF) << 8 ) |
616
                                  *(p_current_data + 2);
617
618
            i_current_section = (uint8_t)p_current_data[6];
            i_prog_info_length = ( ((uint32_t)*(p_current_data + 10) & 0xF) << 8 ) |
619
620
621
622
                                    *(p_current_data + 11);

            /* For the moment we ignore program descriptors */
            p_current_data += 12 + i_prog_info_length;
623
624

            /* The end of the section, before the CRC is at
625
626
627
628
             * p_current_section + i_section_length -1 */
            while( p_current_data < p_current_section + i_section_length -1 )
            {
                i_stream_type = (int)p_current_data[0];
629
                i_pid = ( ((uint32_t)*(p_current_data + 1) & 0x1F) << 8 ) |
630
                           *(p_current_data + 2);
631
                i_es_info_length = ( ((uint32_t)*(p_current_data + 3) & 0xF) << 8 ) |
632
                                      *(p_current_data + 4);
633
634

                /* Tell the interface what kind of stream it is and select
635
636
                 * the required ones */
                {
gbazin's avatar
   
gbazin committed
637
638
                    int i_fourcc, i_cat, i_stream_id;

639
640
641
642
                    switch( i_stream_type )
                    {
                        case MPEG1_VIDEO_ES:
                        case MPEG2_VIDEO_ES:
643
                        case MPEG2_MOTO_VIDEO_ES:
644
645
                            /* This isn't real, but we don't actually use
                             * it. */
gbazin's avatar
   
gbazin committed
646
647
648
                            i_stream_id = 0xE0;
                            i_fourcc = VLC_FOURCC('m','p','g','v');
                            i_cat = VIDEO_ES;
649
650
651
                            break;
                        case MPEG1_AUDIO_ES:
                        case MPEG2_AUDIO_ES:
652
653
                            /* This isn't real, but we don't actually use
                             * it. */
gbazin's avatar
   
gbazin committed
654
655
656
                            i_stream_id = 0xC0;
                            i_fourcc = VLC_FOURCC('m','p','g','a');
                            i_cat = AUDIO_ES;
657
                            break;
658
                        case A52_AUDIO_ES:
659
                            if ( !b_vls_compat )
gbazin's avatar
   
gbazin committed
660
                                i_fourcc = VLC_FOURCC('a','5','2',' ');
661
                            else
gbazin's avatar
   
gbazin committed
662
663
664
                                i_fourcc = VLC_FOURCC('a','5','2','b');
                            i_stream_id = 0xBD;
                            i_cat = AUDIO_ES;
665
                            break;
666
                        case LPCM_AUDIO_ES:
gbazin's avatar
   
gbazin committed
667
668
669
                            i_fourcc = VLC_FOURCC('l','p','c','m');
                            i_stream_id = 0xBD;
                            i_cat = AUDIO_ES;
670
671
                            break;
                        case DVD_SPU_ES:
672
                            if ( !b_vls_compat )
gbazin's avatar
   
gbazin committed
673
                                i_fourcc = VLC_FOURCC('s','p','u',' ');
674
                            else
gbazin's avatar
   
gbazin committed
675
676
677
                                i_fourcc = VLC_FOURCC('s','p','u','b');
                            i_stream_id = 0xBD;
                            i_cat = SPU_ES;
678
                            break;
679
                        case SDDS_AUDIO_ES:
gbazin's avatar
   
gbazin committed
680
681
682
                            i_fourcc = VLC_FOURCC('s','d','d','s');
                            i_stream_id = 0xBD;
                            i_cat = AUDIO_ES;
683
684
                            break;
                        case DTS_AUDIO_ES:
gbazin's avatar
   
gbazin committed
685
686
687
                            i_fourcc = VLC_FOURCC('d','t','s',' ');
                            i_stream_id = 0xBD;
                            i_cat = AUDIO_ES;
688
689
690
                            break;
                        /* 'b' stands for 'buggy' */
                        case A52B_AUDIO_ES:
gbazin's avatar
   
gbazin committed
691
692
693
                            i_fourcc = VLC_FOURCC('a','5','2','b');
                            i_stream_id = 0xBD;
                            i_cat = AUDIO_ES;
694
695
                            break;
                        case LPCMB_AUDIO_ES:
gbazin's avatar
   
gbazin committed
696
697
698
                            i_fourcc = VLC_FOURCC('l','p','c','b');
                            i_stream_id = 0xBD;
                            i_cat = AUDIO_ES;
699
700
                            break;
                        case DVDB_SPU_ES:
gbazin's avatar
   
gbazin committed
701
702
703
                            i_fourcc = VLC_FOURCC('s','p','u','b');
                            i_stream_id = 0xBD;
                            i_cat = SPU_ES;
704
                            break;
705

706
                        default :
gbazin's avatar
   
gbazin committed
707
708
709
                            i_stream_id = 0;
                            i_fourcc = 0;
                            i_cat = UNKNOWN_ES;
710
711
                            break;
                    }
gbazin's avatar
   
gbazin committed
712
713

                    /* Add this ES to the program */
714
                    p_new_es = input_AddES( p_input, p_es->p_pgrm, (uint16_t)i_pid,
gbazin's avatar
   
gbazin committed
715
716
717
718
719
720
721
                                   i_cat, NULL, sizeof( es_ts_data_t ) );

                    ((es_ts_data_t *)p_new_es->p_demux_data)->i_continuity_counter = 0xFF;

                    p_new_es->i_stream_id = i_stream_id;
                    p_new_es->i_fourcc = i_fourcc;

722
                }
723

724
725
726
727
728
                p_current_data += 5 + i_es_info_length;
            }

            /* Go to the beginning of the next section*/
            p_current_data += 3 + i_section_length;
729

730
            p_current_section++;
731

732
733
734
735
736
737
738
739
        } while( i_current_section < p_psi->i_last_section_number );

        p_pgrm_data->i_pmt_version = p_psi->i_version_number;

        /* if no program is selected :*/
        if( !p_input->stream.p_selected_program )
        {
            pgrm_descriptor_t *     p_pgrm_to_select;
740
            uint16_t i_id = (uint16_t)config_GetInt( p_input, "program" );
741
742
743
744
745
746
747
748
749
750
751

            if( i_id != 0 ) /* if user specified a program */
            {
                p_pgrm_to_select = input_FindProgram( p_input, i_id );

                if( p_pgrm_to_select && p_pgrm_to_select == p_es->p_pgrm )
                    p_input->pf_set_program( p_input, p_pgrm_to_select );
            }
            else
                    p_input->pf_set_program( p_input, p_es->p_pgrm );
        }
752

753
754
755
756
757
758
759
        /* if the pmt belongs to the currently selected program, we
         * reselect it to update its ES */
        else if( p_es->p_pgrm == p_input->stream.p_selected_program )
        {
            p_input->pf_set_program( p_input, p_es->p_pgrm );
        }

760
761
762
763
764
        /* inform interface that stream has changed */
        p_input->stream.b_changed = 1;
        /*  Remove lock */
        vlc_mutex_unlock( &p_input->stream.stream_lock );
    }
765

766
767
768
769
770
#undef p_psi
}

#elif defined MODULE_NAME_IS_ts_dvbpsi
/*
771
 * PSI Decoding using libdvbpsi
772
773
774
775
776
 */

/*****************************************************************************
 * DemuxPSI : send the PSI to the right libdvbpsi decoder
 *****************************************************************************/
777
778
779
static void TS_DVBPSI_DemuxPSI( input_thread_t  * p_input,
                                data_packet_t   * p_data,
                                es_descriptor_t * p_es,
780
781
782
783
784
785
                                vlc_bool_t        b_unit_start )
{
    es_ts_data_t        * p_es_demux_data;
    pgrm_ts_data_t      * p_pgrm_demux_data;
    stream_ts_data_t    * p_stream_demux_data;

786
787
    p_es_demux_data = (es_ts_data_t *)p_es->p_demux_data;
    p_stream_demux_data = (stream_ts_data_t *) p_input->stream.p_demux_data;
788
789
790
791

    switch( p_es_demux_data->i_psi_type)
    {
        case PSI_IS_PAT:
792
            dvbpsi_PushPacket(
793
                    (dvbpsi_handle)p_stream_demux_data->p_pat_handle,
794
795
796
797
                    p_data->p_demux_start );
            break;
        case PSI_IS_PMT:
            p_pgrm_demux_data = ( pgrm_ts_data_t * )p_es->p_pgrm->p_demux_data;
798
            dvbpsi_PushPacket(
799
                    (dvbpsi_handle)p_pgrm_demux_data->p_pmt_handle,
800
801
802
803
804
                    p_data->p_demux_start );
            break;
        default:
            msg_Warn( p_input, "received unknown PSI in DemuxPSI" );
    }
805

806
807
    input_DeletePacket( p_input->p_method_data, p_data );
}
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
/*****************************************************************************
 * MP4 specific functions
 *****************************************************************************/
static int  MP4_DescriptorLength( int *pi_data, uint8_t **pp_data )
{
    unsigned int i_b;
    unsigned int i_len = 0;
    do
    {
        i_b = **pp_data;
        (*pp_data)++;
        (*pi_data)--;
        i_len = ( i_len << 7 ) + ( i_b&0x7f );

    } while( i_b&0x80 );

    return( i_len );
}
static int MP4_GetByte( int *pi_data, uint8_t **pp_data )
{
    if( *pi_data > 0 )
    {
        int i_b = **pp_data;
        (*pp_data)++;
        (*pi_data)--;
        return( i_b );
    }
    else
    {
        return( 0 );
    }
}

static int MP4_GetWord( int *pi_data, uint8_t **pp_data )
{
    int i1, i2;
    i1 = MP4_GetByte( pi_data, pp_data );
    i2 = MP4_GetByte( pi_data, pp_data );
    return( ( i1 << 8 ) | i2 );
}
static int MP4_Get3Bytes( int *pi_data, uint8_t **pp_data )
{
    int i1, i2, i3;
    i1 = MP4_GetByte( pi_data, pp_data );
    i2 = MP4_GetByte( pi_data, pp_data );
    i3 = MP4_GetByte( pi_data, pp_data );
    return( ( i1 << 16 ) | ( i2 << 8) | i3 );
}

static uint32_t MP4_GetDWord( int *pi_data, uint8_t **pp_data )
{
    uint32_t i1, i2;
    i1 = MP4_GetWord( pi_data, pp_data );
    i2 = MP4_GetWord( pi_data, pp_data );
    return( ( i1 << 16 ) | i2 );
}

static char* MP4_GetURL( int *pi_data, uint8_t **pp_data )
{
    char *url;
    int i_url_len, i;

    i_url_len = MP4_GetByte( pi_data, pp_data );
    url = malloc( i_url_len + 1 );
    for( i = 0; i < i_url_len; i++ )
    {
        url[i] = MP4_GetByte( pi_data, pp_data );
    }
    url[i_url_len] = '\0';
    return( url );
}

static void MP4_IODParse( iod_descriptor_t *p_iod, int i_data, uint8_t *p_data )
{
    int i;
    int i_es_index;
    uint8_t     i_flags;
    vlc_bool_t  b_url;
    int         i_iod_length;

    fprintf( stderr, "\n************ IOD ************" );
    for( i = 0; i < 255; i++ )
    {
        p_iod->es_descr[i].b_ok = 0;
    }
    i_es_index = 0;

    if( i_data < 3 )
    {
        return;
    }

    p_iod->i_iod_label = MP4_GetByte( &i_data, &p_data );
    fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
    fprintf( stderr, "\n* ===========" );
    fprintf( stderr, "\n* tag:0x%x", p_data[0] );

    if( MP4_GetByte( &i_data, &p_data ) != 0x02 )
    {
        fprintf( stderr, "\n ERR: tag != 0x02" );
        return;
    }

    i_iod_length = MP4_DescriptorLength( &i_data, &p_data );
    fprintf( stderr, "\n* length:%d", i_iod_length );
    if( i_iod_length > i_data )
    {
        i_iod_length = i_data;
    }

    p_iod->i_od_id = ( MP4_GetByte( &i_data, &p_data ) << 2 );
    i_flags = MP4_GetByte( &i_data, &p_data );
    p_iod->i_od_id |= i_flags >> 6;
    b_url = ( i_flags >> 5  )&0x01;

    fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
    fprintf( stderr, "\n* url flag:%d", b_url );
    fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );

    if( b_url )
    {
        p_iod->psz_url = MP4_GetURL( &i_data, &p_data );
        fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
        fprintf( stderr, "\n*****************************\n" );
        return;
    }
    else
    {
        p_iod->psz_url = NULL;
    }

    p_iod->i_ODProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
    p_iod->i_sceneProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
    p_iod->i_audioProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
    p_iod->i_visualProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
    p_iod->i_graphicsProfileLevelIndication = MP4_GetByte( &i_data, &p_data );

    fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
    fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
    fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
    fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
    fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );


    while( i_data > 0 && i_es_index < 255)
    {
        int i_tag, i_length;
        int     i_data_sav;
        uint8_t *p_data_sav;

        i_tag = MP4_GetByte( &i_data, &p_data );
        i_length = MP4_DescriptorLength( &i_data, &p_data );

        i_data_sav = i_data;
        p_data_sav = p_data;

        i_data = i_length;

        switch( i_tag )
        {
            case 0x03:
                {
#define es_descr    p_iod->es_descr[i_es_index]
                    int i_decoderConfigDescr_length;
                    fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
                    es_descr.b_ok = 1;

                    es_descr.i_es_id = MP4_GetWord( &i_data, &p_data );
                    i_flags = MP4_GetByte( &i_data, &p_data );
                    es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
                    b_url = ( i_flags >> 6 )&0x01;
                    es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
                    es_descr.i_streamPriority = i_flags & 0x1f;
                    fprintf( stderr, "\n*   * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
                    fprintf( stderr, "\n*   * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
                    fprintf( stderr, "\n*   * streamPriority:%d", es_descr.i_streamPriority );

                    if( es_descr.b_streamDependenceFlag )
                    {
                        es_descr.i_dependOn_es_id = MP4_GetWord( &i_data, &p_data );
                        fprintf( stderr, "\n*   * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
                    }

                    if( b_url )
                    {
                        es_descr.psz_url = MP4_GetURL( &i_data, &p_data );
                        fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
                    }
                    else
                    {
                        es_descr.psz_url = NULL;
                    }

                    if( es_descr.b_OCRStreamFlag )
                    {
                        es_descr.i_OCR_es_id = MP4_GetWord( &i_data, &p_data );
                        fprintf( stderr, "\n*   * OCR_es_id:%d", es_descr.i_OCR_es_id );
                    }

                    if( MP4_GetByte( &i_data, &p_data ) != 0x04 )
                    {
                        fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
                        es_descr.b_ok = 0;
                        break;
                    }
                    i_decoderConfigDescr_length = MP4_DescriptorLength( &i_data, &p_data );

                    fprintf( stderr, "\n*   - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
#define dec_descr   es_descr.dec_descr
                    dec_descr.i_objectTypeIndication = MP4_GetByte( &i_data, &p_data );
                    i_flags = MP4_GetByte( &i_data, &p_data );
                    dec_descr.i_streamType = i_flags >> 2;
                    dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
                    dec_descr.i_bufferSizeDB = MP4_Get3Bytes( &i_data, &p_data );
                    dec_descr.i_maxBitrate = MP4_GetDWord( &i_data, &p_data );
                    dec_descr.i_avgBitrate = MP4_GetDWord( &i_data, &p_data );
                    fprintf( stderr, "\n*     * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication  );
                    fprintf( stderr, "\n*     * streamType:0x%x", dec_descr.i_streamType );
                    fprintf( stderr, "\n*     * upStream:%d", dec_descr.b_upStream );
                    fprintf( stderr, "\n*     * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
                    fprintf( stderr, "\n*     * maxBitrate:%d", dec_descr.i_maxBitrate );
                    fprintf( stderr, "\n*     * avgBitrate:%d", dec_descr.i_avgBitrate );
                    if( i_decoderConfigDescr_length > 13 && MP4_GetByte( &i_data, &p_data ) == 0x05 )
                    {
                        int i;
                        dec_descr.i_decoder_specific_info_len =
                            MP4_DescriptorLength( &i_data, &p_data );
                        if( dec_descr.i_decoder_specific_info_len > 0 )
                        {
                            dec_descr.p_decoder_specific_info =
                                malloc( dec_descr.i_decoder_specific_info_len );
                        }
                        for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
                        {
                            dec_descr.p_decoder_specific_info[i] = MP4_GetByte( &i_data, &p_data );
                        }
                    }
                    else
                    {
                        dec_descr.i_decoder_specific_info_len = 0;
                        dec_descr.p_decoder_specific_info = NULL;
                    }
                }
#undef  dec_descr
#define sl_descr    es_descr.sl_descr
                {
                    int i_SLConfigDescr_length;
                    int i_predefined;

                    if( MP4_GetByte( &i_data, &p_data ) != 0x06 )
                    {
                        fprintf( stderr, "\n* ERR missing SLConfigDescr" );
                        es_descr.b_ok = 0;
                        break;
                    }
                    i_SLConfigDescr_length = MP4_DescriptorLength( &i_data, &p_data );

                    fprintf( stderr, "\n*   - SLConfigDescr length:%d", i_SLConfigDescr_length );
                    i_predefined = MP4_GetByte( &i_data, &p_data );
                    fprintf( stderr, "\n*     * i_predefined:0x%x", i_predefined  );
                    switch( i_predefined )
                    {
                        case 0x01:
                            {
                                sl_descr.b_useAccessUnitStartFlag   = 0;
                                sl_descr.b_useAccessUnitEndFlag     = 0;
                                sl_descr.b_useRandomAccessPointFlag = 0;
                                //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
                                sl_descr.b_usePaddingFlag           = 0;
                                sl_descr.b_useTimeStampsFlags       = 0;
                                sl_descr.b_useIdleFlag              = 0;
                                sl_descr.b_durationFlag     = 0;    // FIXME FIXME
                                sl_descr.i_timeStampResolution      = 1000;
                                sl_descr.i_OCRResolution    = 0;    // FIXME FIXME
                                sl_descr.i_timeStampLength          = 32;
                                sl_descr.i_OCRLength        = 0;    // FIXME FIXME
                                sl_descr.i_AU_Length                = 0;
                                sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
                                sl_descr.i_degradationPriorityLength= 0;
                                sl_descr.i_AU_seqNumLength          = 0;
                                sl_descr.i_packetSeqNumLength       = 0;
                                if( sl_descr.b_durationFlag )
                                {
                                    sl_descr.i_timeScale            = 0;    // FIXME FIXME
                                    sl_descr.i_accessUnitDuration   = 0;    // FIXME FIXME
                                    sl_descr.i_compositionUnitDuration= 0;    // FIXME FIXME
                                }
                                if( !sl_descr.b_useTimeStampsFlags )
                                {
                                    sl_descr.i_startDecodingTimeStamp   = 0;    // FIXME FIXME
                                    sl_descr.i_startCompositionTimeStamp= 0;    // FIXME FIXME
                                }
                            }
                            break;
                        default:
                            fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
                            es_descr.b_ok = 0;
                            break;
                    }
                }
                break;
#undef  sl_descr
#undef  es_descr
            default:
                fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
                break;
        }

        p_data = p_data_sav + i_length;
        i_data = i_data_sav - i_length;
        i_es_index++;
    }


    fprintf( stderr, "\n*****************************\n" );
}

static void MP4_IODClean( iod_descriptor_t *p_iod )
{
    int i;

    if( p_iod->psz_url )
    {
        free( p_iod->psz_url );
        p_iod->psz_url = NULL;
        return;
    }

    for( i = 0; i < 255; i++ )
    {
#define es_descr p_iod->es_descr[i]
        if( es_descr.b_ok )
        {
            if( es_descr.psz_url )
            {
                free( es_descr.psz_url );
                es_descr.psz_url = NULL;
            }
            else
            {
                if( es_descr.dec_descr.p_decoder_specific_info != NULL )
                {
                    free( es_descr.dec_descr.p_decoder_specific_info );
                    es_descr.dec_descr.p_decoder_specific_info = NULL;
                    es_descr.dec_descr.i_decoder_specific_info_len = 0;
                }
            }
        }
        es_descr.b_ok = 0;
#undef  es_descr
    }
}
1160
1161
1162
1163

/*****************************************************************************
 * HandlePAT: will treat a PAT returned by dvbpsi
 *****************************************************************************/
1164
1165
static void TS_DVBPSI_HandlePAT( input_thread_t * p_input,
                                 dvbpsi_pat_t * p_new_pat )
1166
1167
1168
1169
1170
1171
1172
1173
1174
{
    dvbpsi_pat_program_t *      p_pgrm;
    pgrm_descriptor_t *         p_new_pgrm;
    pgrm_ts_data_t *            p_pgrm_demux;
    es_descriptor_t *           p_current_es;
    es_ts_data_t *              p_es_demux;
    stream_ts_data_t *          p_stream_data;

    vlc_mutex_lock( &p_input->stream.stream_lock );
1175

1176
    p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
1177

1178
    if ( ( p_new_pat->b_current_next && ( p_new_pat->i_version != p_stream_data->i_pat_version ) ) ||
1179
1180
1181
1182
1183
            p_stream_data->i_pat_version == PAT_UNINITIALIZED  )
    {
        /* Delete all programs */
        while( p_input->stream.i_pgrm_number )
        {
1184
1185
1186
1187
1188
1189
1190
1191
            pgrm_ts_data_t *p_pgrm_demux_old =
                (pgrm_ts_data_t *)p_input->stream.pp_programs[0]->p_demux_data;

            if( p_pgrm_demux_old->b_mpeg4 )
            {
                MP4_IODClean( &p_pgrm_demux_old->iod );
            }

1192
1193
            input_DelProgram( p_input, p_input->stream.pp_programs[0] );
        }
1194

1195
1196
        /* treat the new programs list */
        p_pgrm = p_new_pat->p_first_program;
1197

1198
1199
1200
1201
1202
1203
        while( p_pgrm )
        {
            /* If program = 0, we're having info about NIT not PMT */
            if( p_pgrm->i_number )
            {
                /* Add this program */
1204
                p_new_pgrm = input_AddProgram( p_input, p_pgrm->i_number,
1205
1206
1207
1208
                                            sizeof( pgrm_ts_data_t ) );

                p_pgrm_demux = (pgrm_ts_data_t *)p_new_pgrm->p_demux_data;
                p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
1209

1210
1211
                /* Add the PMT ES to this program */
                p_current_es = input_AddES( p_input, p_new_pgrm,
1212
                                            (uint16_t)p_pgrm->i_pid, UNKNOWN_ES,
gbazin's avatar
   
gbazin committed
1213
                                            NULL, sizeof(es_ts_data_t) );
1214
                p_current_es->i_fourcc = VLC_FOURCC( 'p', 'm', 't', ' ' );
1215
1216
1217
                p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
                p_es_demux->b_psi = 1;
                p_es_demux->i_psi_type = PSI_IS_PMT;
1218
                p_es_demux->p_psi_section = NULL;
1219
                p_es_demux->i_continuity_counter = 0xFF;
1220

1221
1222
1223
                /* Create a PMT decoder */
                p_pgrm_demux->p_pmt_handle = (dvbpsi_handle *)
                    dvbpsi_AttachPMT( p_pgrm->i_number,
1224
                            (dvbpsi_pmt_callback) &TS_DVBPSI_HandlePMT,
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
                            p_input );

                if( p_pgrm_demux->p_pmt_handle == NULL )
                {
                    msg_Err( p_input, "could not create PMT decoder" );
                    p_input->b_error = 1;
                    return;
                }

            }
1235
            p_pgrm = p_pgrm->p_next;
1236
        }
1237

1238
1239
1240
1241
1242
        p_stream_data->i_pat_version = p_new_pat->i_version;
    }
    vlc_mutex_unlock( &p_input->stream.stream_lock );
}

1243

1244
1245
1246
/*****************************************************************************
 * HandlePMT: will treat a PMT returned by dvbpsi
 *****************************************************************************/
1247
1248
static void TS_DVBPSI_HandlePMT( input_thread_t * p_input,
                                 dvbpsi_pmt_t * p_new_pmt )
1249
1250
1251
1252
1253
{
    dvbpsi_pmt_es_t *       p_es;
    pgrm_descriptor_t *     p_pgrm;
    es_descriptor_t *       p_new_es;
    pgrm_ts_data_t *        p_pgrm_demux;
1254
    vlc_bool_t b_vls_compat = config_GetInt( p_input, "vls-backwards-compat" );
1255

1256
    vlc_mutex_lock( &p_input->stream.stream_lock );
1257

1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
    p_pgrm = input_FindProgram( p_input, p_new_pmt->i_program_number );

    if( p_pgrm == NULL )
    {
        msg_Warn( p_input, "PMT of unreferenced program found" );
        return;
    }

    p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
    p_pgrm_demux->i_pcr_pid = p_new_pmt->i_pcr_pid;

gbazin's avatar
   
gbazin committed
1269
1270
1271
    if( ( p_new_pmt->b_current_next &&
          ( p_new_pmt->i_version != p_pgrm_demux->i_pmt_version ) ) ||
          p_pgrm_demux->i_pmt_version == PMT_UNINITIALIZED )
1272
    {
1273
        dvbpsi_descriptor_t *p_dr = p_new_pmt->p_first_descriptor;
1274
1275
1276
1277
1278
1279
1280
        int i_loop;

        msg_Dbg( p_input, "Processing PMT for program %d version %d",
                 p_new_pmt->i_program_number, p_new_pmt->i_version );

        /* Delete all ES in this program  except the PSI. We start from the
         * end because i_es_number gets decremented after each deletion. */
1281
        for( i_loop = p_pgrm->i_es_number ; i_loop > 0 ; )
1282
1283
1284
1285
1286
        {
            es_ts_data_t *              p_es_demux;

            i_loop--;
            p_es_demux = (es_ts_data_t *)
1287
                         p_pgrm->pp_es[i_loop]->p_demux_data;
1288
1289
            if ( !p_es_demux->b_psi )
            {
1290
                input_DelES( p_input, p_pgrm->pp_es[i_loop] );
1291
1292
1293
            }
        }

1294
1295
1296
1297
1298
1299
1300
1301
1302
        /* IOD */
        while( p_dr && ( p_dr->i_tag != 0x1d ) )
            p_dr = p_dr->p_next;
        if( p_dr)
        {
            msg_Warn( p_input, "found IOD descriptor" );
            MP4_IODParse( &p_pgrm_demux->iod, p_dr->i_length, p_dr->p_data );
        }

1303
1304
1305
        p_es = p_new_pmt->p_first_es;
        while( p_es )
        {
gbazin's avatar
   
gbazin committed
1306
1307
1308
1309
1310
1311
1312
1313
1314
            vlc_fourcc_t i_fourcc;
            int i_size, i_cat, i_stream_id = 0;
            es_ts_data_t demux_data;
            BITMAPINFOHEADER *p_bih = NULL;
            WAVEFORMATEX *p_wf = NULL;
            char psz_desc[30];

            memset( &demux_data, 0, sizeof(es_ts_data_t) );
            *psz_desc = 0;
1315

1316
1317
1318
1319
            switch( p_es->i_type )
            {
                case MPEG1_VIDEO_ES:
                case MPEG2_VIDEO_ES:
1320
                case MPEG2_MOTO_VIDEO_ES:
gbazin's avatar
   
gbazin committed
1321
1322
                    i_fourcc = VLC_FOURCC('m','p','g','v');
                    i_cat = VIDEO_ES;
1323
1324
1325
                    break;
                case MPEG1_AUDIO_ES:
                case MPEG2_AUDIO_ES:
gbazin's avatar
   
gbazin committed
1326
1327
                    i_fourcc = VLC_FOURCC('m','p','g','a');
                    i_cat = AUDIO_ES;
1328
                    break;
1329
                case A52_AUDIO_ES:
1330
                    if ( !b_vls_compat )
gbazin's avatar
   
gbazin committed
1331
                        i_fourcc = VLC_FOURCC('a','5','2',' ');
1332
                    else
gbazin's avatar
   
gbazin committed
1333
1334
1335
                        i_fourcc = VLC_FOURCC('a','5','2','b');
                    i_cat = AUDIO_ES;
                    i_stream_id = 0xBD;
1336
1337
                    break;
                case DVD_SPU_ES:
1338
                    if ( !b_vls_compat )
gbazin's avatar
   
gbazin committed
1339
                        i_fourcc = VLC_FOURCC('s','p','u',' ');
1340
                    else
gbazin's avatar
   
gbazin committed
1341
1342
1343
                        i_fourcc = VLC_FOURCC('s','p','u','b');
                    i_cat = SPU_ES;
                    i_stream_id = 0xBD;
1344
                    break;
1345
                case LPCM_AUDIO_ES:
gbazin's avatar
   
gbazin committed
1346
1347
1348
                    i_fourcc = VLC_FOURCC('l','p','c','m');
                    i_cat = AUDIO_ES;
                    i_stream_id = 0xBD;
1349
                    break;
1350
                case SDDS_AUDIO_ES:
gbazin's avatar
   
gbazin committed
1351
1352
1353
                    i_fourcc = VLC_FOURCC('s','d','d','s');
                    i_stream_id = 0xBD;
                    i_cat = AUDIO_ES;
1354
1355
                    break;
                case DTS_AUDIO_ES:
gbazin's avatar
   
gbazin committed
1356
1357
1358
                    i_fourcc = VLC_FOURCC('d','t','s',' ');
                    i_stream_id = 0xBD;
                    i_cat = AUDIO_ES;
1359
                    break;
1360
                case A52B_AUDIO_ES:
gbazin's avatar
   
gbazin committed
1361
1362
1363
                    i_fourcc = VLC_FOURCC('a','5','2','b');
                    i_cat = AUDIO_ES;
                    i_stream_id = 0xBD;
1364
1365
                    break;
                case DVDB_SPU_ES:
gbazin's avatar
   
gbazin committed
1366
1367
1368
                    i_fourcc = VLC_FOURCC('s','p','u','b');
                    i_cat = SPU_ES;
                    i_stream_id = 0xBD;
1369
                    break;
1370
                case LPCMB_AUDIO_ES:
gbazin's avatar
   
gbazin committed
1371
1372
1373
                    i_fourcc = VLC_FOURCC('l','p','c','b');
                    i_cat = AUDIO_ES;
                    i_stream_id = 0xBD;
1374
                    break;
1375
                case MPEG4_VIDEO_ES:
gbazin's avatar
   
gbazin committed
1376
1377
1378
                    i_fourcc = VLC_FOURCC('m','p','4','v');
                    i_cat = VIDEO_ES;
                    i_stream_id = 0xfa;
1379
1380
                    break;
                case MPEG4_AUDIO_ES:
gbazin's avatar
   
gbazin committed
1381
1382
1383
                    i_fourcc = VLC_FOURCC('m','p','4','a');
                    i_cat = AUDIO_ES;
                    i_stream_id = 0xfa;
1384
                    break;
1385
                case MSCODEC_VIDEO_ES:
gbazin's avatar
   
gbazin committed
1386
1387
1388
                    i_fourcc = VLC_FOURCC(0,0,0,0);   // fixed later
                    i_cat = VIDEO_ES;
                    i_stream_id = 0xa0;
1389
                    break;
1390
                default:
gbazin's avatar
   
gbazin committed
1391
1392
                    i_fourcc = 0;
                    i_cat = UNKNOWN_ES;
gbazin's avatar
   
gbazin committed
1393
                    i_stream_id = 0;
gbazin's avatar
   
gbazin committed
1394
1395
            }

gbazin's avatar
   
gbazin committed
1396
1397
            if( p_es->i_type == MPEG4_VIDEO_ES ||
                p_es->i_type == MPEG4_AUDIO_ES )
1398
1399
1400
1401
            {
                /* mpeg4 stream, search sl_descriptor */
                dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;

gbazin's avatar