ts.c 93.1 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * ts.c: Transport Stream input module for VLC.
 *****************************************************************************
 * Copyright (C) 2004 VideoLAN
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 *
 * Authors: Laurent Aimar <fenrir@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.
 *
 * 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>                                      /* malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/input.h>

#include "iso_lang.h"
33
#include "network.h"
34

35
36
#include "../mux/mpeg/csa.h"

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Include dvbpsi headers */
#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>
#   include <dvbpsi/psi.h>
#else
#   include "dvbpsi.h"
#   include "descriptor.h"
#   include "tables/pat.h"
#   include "tables/pmt.h"
#   include "descriptors/dr.h"
#   include "psi.h"
#endif

/* TODO:
55
 *  - XXX: do not mark options message to be translated, they are too osbcure for now ...
56
57
58
59
60
61
62
 *  - test it
 *  - ...
 */

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
63
64
static int  Open  ( vlc_object_t * );
static void Close ( vlc_object_t * );
65

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#define PMT_TEXT N_("Extra PMT")
#define PMT_LONGTEXT N_( \
  "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])" )

#define PID_TEXT N_("Set id of ES to PID")
#define PID_LONGTEXT N_("set id of es to pid")

#define TSOUT_TEXT N_("Fast udp streaming")
#define TSOUT_LONGTEXT N_( \
  "Sends TS to specific ip:port by udp (you must know what you are doing)")

#define MTUOUT_TEXT N_("MTU for out mode")
#define MTUOUT_LONGTEXT N_("MTU for out mode")

#define CSA_TEXT N_("CSA ck")
#define CSA_LONGTEXT N_("CSA ck")

#define SILENT_TEXT N_("Silent mode")
#define SILENT_LONGTEXT N_("do not complain on encrypted PES")

86
87
vlc_module_begin();
    set_description( _("ISO 13818-1 MPEG Transport Stream input - new" ) );
88
89
90
91
92
93
94
95
96

    add_string( "ts-extra-pmt", NULL, NULL, PMT_TEXT, PMT_LONGTEXT, VLC_TRUE );
    add_bool( "ts-es-id-pid", 0, NULL, PID_TEXT, PID_LONGTEXT, VLC_TRUE );
    add_string( "ts-out", NULL, NULL, TSOUT_TEXT, TSOUT_LONGTEXT, VLC_TRUE );
    add_integer( "ts-out-mtu", 1500, NULL, MTUOUT_TEXT,
                 MTUOUT_LONGTEXT, VLC_TRUE );
    add_string( "ts-csa-ck", NULL, NULL, CSA_TEXT, CSA_LONGTEXT, VLC_TRUE );
    add_bool( "ts-silent", 0, NULL, SILENT_TEXT, SILENT_LONGTEXT, VLC_TRUE );

97
98
    set_capability( "demux2", 10 );
    set_callbacks( Open, Close );
Laurent Aimar's avatar
Laurent Aimar committed
99
    add_shortcut( "ts" );
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
vlc_module_end();

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

typedef struct
{
    uint8_t                 i_objectTypeIndication;
    uint8_t                 i_streamType;
    vlc_bool_t              b_upStream;
    uint32_t                i_bufferSizeDB;
    uint32_t                i_maxBitrate;
    uint32_t                i_avgBitrate;

    int                     i_decoder_specific_info_len;
    uint8_t                 *p_decoder_specific_info;

} decoder_config_descriptor_t;

typedef struct
{
    vlc_bool_t              b_useAccessUnitStartFlag;
    vlc_bool_t              b_useAccessUnitEndFlag;
    vlc_bool_t              b_useRandomAccessPointFlag;
    vlc_bool_t              b_useRandomAccessUnitsOnlyFlag;
    vlc_bool_t              b_usePaddingFlag;
    vlc_bool_t              b_useTimeStampsFlags;
    vlc_bool_t              b_useIdleFlag;
    vlc_bool_t              b_durationFlag;
    uint32_t                i_timeStampResolution;
    uint32_t                i_OCRResolution;
    uint8_t                 i_timeStampLength;
    uint8_t                 i_OCRLength;
    uint8_t                 i_AU_Length;
    uint8_t                 i_instantBitrateLength;
    uint8_t                 i_degradationPriorityLength;
    uint8_t                 i_AU_seqNumLength;
    uint8_t                 i_packetSeqNumLength;

    uint32_t                i_timeScale;
    uint16_t                i_accessUnitDuration;
    uint16_t                i_compositionUnitDuration;

    uint64_t                i_startDecodingTimeStamp;
    uint64_t                i_startCompositionTimeStamp;

} sl_config_descriptor_t;

typedef struct
{
    vlc_bool_t              b_ok;
    uint16_t                i_es_id;

    vlc_bool_t              b_streamDependenceFlag;
    vlc_bool_t              b_OCRStreamFlag;
    uint8_t                 i_streamPriority;

    char                    *psz_url;

    uint16_t                i_dependOn_es_id;
    uint16_t                i_OCR_es_id;

    decoder_config_descriptor_t    dec_descr;
    sl_config_descriptor_t         sl_descr;
165

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
} es_mpeg4_descriptor_t;

typedef struct
{
    uint8_t                i_iod_label;

    /* IOD */
    uint16_t                i_od_id;
    char                    *psz_url;

    uint8_t                 i_ODProfileLevelIndication;
    uint8_t                 i_sceneProfileLevelIndication;
    uint8_t                 i_audioProfileLevelIndication;
    uint8_t                 i_visualProfileLevelIndication;
    uint8_t                 i_graphicsProfileLevelIndication;

    es_mpeg4_descriptor_t   es_descr[255];

} iod_descriptor_t;

typedef struct
{
188
189
    dvbpsi_handle   handle;

190
191
192
    int             i_version;
    int             i_number;
    int             i_pid_pcr;
193
    int             i_pid_pmt;
194
195
    /* IOD stuff (mpeg4) */
    iod_descriptor_t *iod;
196

197
198
199
    /* Conditional Access PMT (EN 50 221) */
    uint8_t         *p_capmt;
    int             i_capmt_size;
200

201
202
203
204
205
} ts_prg_psi_t;

typedef struct
{
    /* for special PAT case */
206
    dvbpsi_handle   handle;
207
208
209
210
211
    int             i_pat_version;

    /* For PMT */
    int             i_prg;
    ts_prg_psi_t    **prg;
212

213
214
215
216
217
218
} ts_psi_t;

typedef struct
{
    es_format_t  fmt;
    es_out_id_t *id;
219
220
    int         i_pes_size;
    int         i_pes_gathered;
221
    block_t     *p_pes;
222
    block_t     **pp_last;
223
224

    es_mpeg4_descriptor_t *p_mpeg4desc;
225
    int         b_gather;
226

227
228
229
230
231
232
233
234
235
236
237
238
} ts_es_t;

typedef struct
{
    int         i_pid;

    vlc_bool_t  b_seen;
    vlc_bool_t  b_valid;
    int         i_cc;   /* countinuity counter */

    /* PSI owner (ie PMT -> PAT, ES -> PMT */
    ts_psi_t   *p_owner;
239
    int         i_owner_number;
240
241
242
243
244

    /* */
    ts_psi_t    *psi;
    ts_es_t     *es;

245
246
247
248
    /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
    ts_es_t     **extra_es;
    int         i_extra_es;

249
250
251
252
} ts_pid_t;

struct demux_sys_t
{
253
254
255
    /* TS packet size (188, 192, 204) */
    int         i_packet_size;

256
257
258
    /* how many TS packet we read at once */
    int         i_ts_read;

259
260
261
262
263
264
    /* All pid */
    ts_pid_t    pid[8192];

    /* All PMT */
    int         i_pmt;
    ts_pid_t    **pmt;
265
266
267

    /* */
    vlc_bool_t  b_es_id_pid;
268
    csa_t       *csa;
269
    vlc_bool_t  b_silent;
270
271
272
273

    vlc_bool_t  b_udp_out;
    int         fd; /* udp socket */
    uint8_t     *buffer;
274
275
276

    vlc_bool_t  b_dvb_control;
    int         i_dvb_program;
277
    vlc_list_t  *p_programs_list;
278
279
280
281
282
};

static int Demux  ( demux_t *p_demux );
static int Control( demux_t *p_demux, int i_query, va_list args );

283
284
285
286
287

static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
static void PIDClean( es_out_t *out, ts_pid_t *pid );
static int  PIDFillFormat( ts_pid_t *pid, int i_stream_type );

288
289
290
291
292
293
294
295
296
297
static void PATCallBack( demux_t *, dvbpsi_pat_t * );
static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );

static inline int PIDGet( block_t *p )
{
    return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
}

static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );

298
299
static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );

300
static iod_descriptor_t *IODNew( int , uint8_t * );
301
static void              IODFree( iod_descriptor_t * );
302

303
304
static void DVBCAPMTSend( demux_t *p_demux );

305
306
307
308
#define TS_PACKET_SIZE_188 188
#define TS_PACKET_SIZE_192 192
#define TS_PACKET_SIZE_204 204
#define TS_PACKET_SIZE_MAX 204
309
310
311
312
313
314
315
316
317
318

/*****************************************************************************
 * Open
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;

    uint8_t     *p_peek;
319
320
    int          i_sync, i_peek, i;
    int          i_packet_size;
321
322
323
324
325

    ts_pid_t     *pat;

    vlc_value_t  val;

326
327
    if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
        TS_PACKET_SIZE_MAX )
328
329
330
331
    {
        msg_Err( p_demux, "cannot peek" );
        return VLC_EGENERIC;
    }
332

333
334
    /* Search first sync byte */
    for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
335
336
337
    {
        if( p_peek[i_sync] == 0x47 ) break;
    }
338
    if( i_sync >= TS_PACKET_SIZE_MAX )
339
    {
340
        if( strcmp( p_demux->psz_demux, "ts" ) )
341
342
343
344
345
346
        {
            msg_Warn( p_demux, "TS module discarded" );
            return VLC_EGENERIC;
        }
        msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
    }
347
348
349
350

    /* Check next 3 sync bytes */
    i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
    if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
351
    {
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
        msg_Err( p_demux, "cannot peek" );
        return VLC_EGENERIC;
    }
    if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
        p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
        p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
    {
        i_packet_size = TS_PACKET_SIZE_188;
    }
    else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
             p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
             p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
    {
        i_packet_size = TS_PACKET_SIZE_192;
    }
    else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
             p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
             p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
    {
        i_packet_size = TS_PACKET_SIZE_204;
    }
    else if( !strcmp( p_demux->psz_demux, "ts" ) )
    {
        i_packet_size = TS_PACKET_SIZE_188;
    }
    else
    {
        msg_Warn( p_demux, "TS module discarded (lost sync)" );
        return VLC_EGENERIC;
381
    }
382
383
384
385
386
387
388
389

    /* Fill p_demux field */
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
    memset( p_sys, 0, sizeof( demux_sys_t ) );

    /* Init p_sys field */
390
391
    p_sys->b_dvb_control = VLC_TRUE;
    p_sys->i_dvb_program = 0;
392
393
394
395
396
397
398
399
    for( i = 0; i < 8192; i++ )
    {
        ts_pid_t *pid = &p_sys->pid[i];

        pid->i_pid      = i;
        pid->b_seen     = VLC_FALSE;
        pid->b_valid    = VLC_FALSE;
    }
400
    p_sys->i_packet_size = i_packet_size;
401
402
    p_sys->b_udp_out = VLC_FALSE;
    p_sys->i_ts_read = 50;
403
    p_sys->csa = NULL;
404
405
406
407

    /* Init PAT handler */
    pat = &p_sys->pid[0];
    PIDInit( pat, VLC_TRUE, NULL );
408
409
    pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
                                         p_demux );
410
411
412
413
414

    /* Init PMT array */
    p_sys->i_pmt = 0;
    p_sys->pmt   = NULL;

415
    /* Read config */
416
417
418
419
    var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "ts-es-id-pid", &val );
    p_sys->b_es_id_pid = val.b_bool,

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
    var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "ts-out", &val );
    if( val.psz_string && *val.psz_string )
    {
        vlc_value_t mtu;
        char *psz = strchr( val.psz_string, ':' );
        int   i_port = 0;

        p_sys->b_udp_out = VLC_TRUE;

        if( psz )
        {
            *psz++ = '\0';
            i_port = atoi( psz );
        }
        if( i_port <= 0 ) i_port  = 1234;
        msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );

        p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
        if( p_sys->fd < 0 )
        {
            msg_Err( p_demux, "failed to open udp socket, send disabled" );
            p_sys->b_udp_out = VLC_FALSE;
        }
        else
        {
446
447
            var_Create( p_demux, "ts-out-mtu",
                        VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
448
            var_Get( p_demux, "ts-out-mtu", &mtu );
449
            p_sys->i_ts_read = mtu.i_int / p_sys->i_packet_size;
450
451
            if( p_sys->i_ts_read <= 0 )
            {
452
                p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
453
            }
454
            p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
455
456
457
458
459
460
461
        }
    }
    if( val.psz_string )
    {
        free( val.psz_string );
    }

462

463
464
465
466
467
468
469
470
471
472
473
474
    /* We handle description of an extra PMT */
    var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "ts-extra-pmt", &val );
    if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
    {
        char *psz = val.psz_string;
        int  i_pid = strtol( psz, &psz, 0 );

        if( i_pid >= 2 && i_pid < 8192 )
        {
            ts_pid_t *pmt = &p_sys->pid[i_pid];

475
            msg_Dbg( p_demux, "extra pmt specified (pid=%d)", i_pid );
476
477
            PIDInit( pmt, VLC_TRUE, NULL );
            /* FIXME we should also ask for a number */
478
            pmt->psi->prg[0]->handle =
479
480
                dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
                                  p_demux );
481
            pmt->psi->prg[0]->i_number = 0; /* special one */
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497

            psz = strchr( psz, '=' ) + 1;   /* can't failed */
            while( psz && *psz )
            {
                char *psz_next = strchr( psz, ',' );
                int i_pid, i_stream_type;

                if( psz_next )
                {
                    *psz_next++ = '\0';
                }

                i_pid = strtol( psz, &psz, 0 );
                if( *psz == ':' )
                {
                    i_stream_type = strtol( psz + 1, &psz, 0 );
498
499
                    if( i_pid >= 2 && i_pid < 8192 &&
                        !p_sys->pid[i_pid].b_valid )
500
501
502
503
                    {
                        ts_pid_t *pid = &p_sys->pid[i_pid];

                        PIDInit( pid, VLC_FALSE, pmt->psi);
504
                        if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
505
                        {
506
                            pmt->psi->prg[0]->i_pid_pcr = i_pid;
507
                        }
508
                        PIDFillFormat( pid, i_stream_type);
509
510
                        if( pid->es->fmt.i_cat != UNKNOWN_ES )
                        {
511
512
513
514
                            if( p_sys->b_es_id_pid )
                            {
                                pid->es->fmt.i_id = i_pid;
                            }
515
                            msg_Dbg( p_demux, "  * es pid=%d type=%d "
516
517
518
519
                                     "fcc=%4.4s", i_pid, i_stream_type,
                                     (char*)&pid->es->fmt.i_codec );
                            pid->es->id = es_out_Add( p_demux->out,
                                                      &pid->es->fmt );
520
521
522
523
524
525
526
527
528
529
530
531
                        }
                    }
                }
                psz = psz_next;
            }
        }
    }
    if( val.psz_string )
    {
        free( val.psz_string );
    }

532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
    var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "ts-csa-ck", &val );
    if( val.psz_string && *val.psz_string )
    {
        char *psz = val.psz_string;
        if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
        {
            psz += 2;
        }
        if( strlen( psz ) != 16 )
        {
            msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
        }
        else
        {
547
            uint64_t i_ck = strtoull( psz, NULL, 16 );
548
549
550
551
552
553
554
            uint8_t ck[8];
            int     i;
            for( i = 0; i < 8; i++ )
            {
                ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
            }

555
556
            msg_Dbg( p_demux, "using CSA scrambling with "
                     "ck=%x:%x:%x:%x:%x:%x:%x:%x",
557
558
559
560
561
562
563
564
565
566
567
                     ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );

            p_sys->csa = csa_New();
            csa_SetCW( p_sys->csa, ck, ck );
        }
    }
    if( val.psz_string )
    {
        free( val.psz_string );
    }

568
569
570
    var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "ts-silent", &val );
    p_sys->b_silent = val.b_bool;
571

572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;

    int          i;

    msg_Dbg( p_demux, "pid list:" );
    for( i = 0; i < 8192; i++ )
    {
        ts_pid_t *pid = &p_sys->pid[i];

        if( pid->b_valid && pid->psi )
        {
            switch( pid->i_pid )
            {
                case 0: /* PAT */
                    dvbpsi_DetachPAT( pid->psi->handle );
596
                    free( pid->psi );
597
598
                    break;
                case 1: /* CAT */
599
                    free( pid->psi );
600
601
                    break;
                default:
602
                    PIDClean( p_demux->out, pid );
603
604
605
606
607
                    break;
            }
        }
        else if( pid->b_valid && pid->es )
        {
608
            PIDClean( p_demux->out, pid );
609
        }
610

611
612
        if( pid->b_seen )
        {
613
            msg_Dbg( p_demux, "  - pid[%d] seen", pid->i_pid );
614
        }
615
616
617
618
619
620
621

        if( p_sys->b_dvb_control && pid->i_pid > 0 )
        {
            /* too much */
            stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, pid->i_pid, VLC_FALSE );
        }

622
623
    }

624
625
626
627
628
    if( p_sys->b_udp_out )
    {
        net_Close( p_sys->fd );
        free( p_sys->buffer );
    }
629
630
631
632
    if( p_sys->csa )
    {
        csa_Delete( p_sys->csa );
    }
633

gbazin's avatar
   
gbazin committed
634
    if( p_sys->i_pmt ) free( p_sys->pmt );
635
636
637
638
639
640
641
642

    if ( p_sys->p_programs_list )
    {
        vlc_value_t val;
        val.p_list = p_sys->p_programs_list;
        var_Change( p_demux, "programs", VLC_VAR_FREELIST, &val, NULL );
    }

643
644
645
646
647
648
649
650
651
652
653
654
    free( p_sys );
}

/*****************************************************************************
 * Demux:
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    int          i_pkt;

    /* We read at most 100 TS packet or until a frame is completed */
655
    for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
656
657
658
659
660
661
    {
        vlc_bool_t  b_frame = VLC_FALSE;
        block_t     *p_pkt;
        ts_pid_t    *p_pid;

        /* Get a new TS packet */
662
        if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
663
664
665
666
        {
            msg_Dbg( p_demux, "eof ?" );
            return 0;
        }
667
668

        /* Check sync byte and re-sync if needed */
669
670
671
672
673
        if( p_pkt->p_buffer[0] != 0x47 )
        {
            msg_Warn( p_demux, "lost synchro" );
            block_Release( p_pkt );

674
            while( !p_demux->b_die )
675
            {
676
677
                uint8_t *p_peek;
                int i_peek, i_skip = 0;
678

679
680
681
                i_peek = stream_Peek( p_demux->s, &p_peek,
                                      p_sys->i_packet_size * 10 );
                if( i_peek < p_sys->i_packet_size + 1 )
682
683
684
685
686
                {
                    msg_Dbg( p_demux, "eof ?" );
                    return 0;
                }

687
                while( i_skip < i_peek - p_sys->i_packet_size )
688
                {
689
690
                    if( p_peek[i_skip] == 0x47 &&
                        p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
691
692
693
694
695
                    {
                        break;
                    }
                    i_skip++;
                }
696
697

                msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
698
                stream_Read( p_demux->s, NULL, i_skip );
699
700

                if( i_skip < i_peek - p_sys->i_packet_size )
701
702
703
704
                {
                    break;
                }
            }
705
706

            if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
707
708
709
710
711
712
            {
                msg_Dbg( p_demux, "eof ?" );
                return 0;
            }
        }

713
714
        if( p_sys->b_udp_out )
        {
715
716
            memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
                    p_pkt->p_buffer, p_sys->i_packet_size );
717
718
        }

719
720
721
722
723
724
725
        /* Parse the TS packet */
        p_pid = &p_sys->pid[PIDGet( p_pkt )];

        if( p_pid->b_valid )
        {
            if( p_pid->psi )
            {
726
727
728
729
730
731
732
733
734
                if( p_pid->i_pid == 0 )
                {
                    dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
                }
                else
                {
                    int i_prg;
                    for( i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
                    {
735
736
                        dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
                                           p_pkt->p_buffer );
737
738
                    }
                }
739
740
                block_Release( p_pkt );
            }
741
            else if( !p_sys->b_udp_out )
742
743
744
            {
                b_frame = GatherPES( p_demux, p_pid, p_pkt );
            }
745
746
            else
            {
Laurent Aimar's avatar
Laurent Aimar committed
747
                PCRHandle( p_demux, p_pid, p_pkt );
748
749
                block_Release( p_pkt );
            }
750
751
752
753
754
        }
        else
        {
            if( !p_pid->b_seen )
            {
755
                msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
756
            }
757
758
            /* We have to handle PCR if present */
            PCRHandle( p_demux, p_pid, p_pkt );
759
760
761
762
763
764
765
766
767
768
            block_Release( p_pkt );
        }
        p_pid->b_seen = VLC_TRUE;

        if( b_frame )
        {
            break;
        }
    }

769
770
771
    if( p_sys->b_udp_out )
    {
        /* Send the complete block */
772
773
        net_Write( p_demux, p_sys->fd, p_sys->buffer,
                   p_sys->i_ts_read * p_sys->i_packet_size );
774
775
    }

776
777
778
779
780
781
782
783
    return 1;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
784
    demux_sys_t *p_sys = p_demux->p_sys;
785
    double f, *pf;
786
    int64_t i64;
787
    int i_int;
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828

    switch( i_query )
    {
        case DEMUX_GET_POSITION:
            pf = (double*) va_arg( args, double* );
            i64 = stream_Size( p_demux->s );
            if( i64 > 0 )
            {
                *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
            }
            else
            {
                *pf = 0.0;
            }
            return VLC_SUCCESS;
        case DEMUX_SET_POSITION:
            f = (double) va_arg( args, double );
            i64 = stream_Size( p_demux->s );

            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
            if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
            {
                return VLC_EGENERIC;
            }
            return VLC_SUCCESS;
#if 0

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( p_sys->i_time < 0 )
            {
                *pi64 = 0;
                return VLC_EGENERIC;
            }
            *pi64 = p_sys->i_time;
            return VLC_SUCCESS;

        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( p_sys->i_mux_rate > 0 )
            {
829
830
                *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
                        p_sys->i_mux_rate;
831
832
833
834
835
                return VLC_SUCCESS;
            }
            *pi64 = 0;
            return VLC_EGENERIC;
#endif
836
        case DEMUX_SET_GROUP:
837
838
839
        {
            uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
            ts_prg_psi_t *p_prg = NULL;
840
            vlc_list_t *p_list;
841

842
            i_int = (int)va_arg( args, int );
843
844
            p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
            msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
845

846
847
848
849
850
851
852
853
854
855
856
857
858
            if( p_sys->b_dvb_control && i_int > 0 && i_int != p_sys->i_dvb_program )
            {
                int i_pmt_pid = -1;
                int i;

                /* Search pmt to be unselected */
                for( i = 0; i < p_sys->i_pmt; i++ )
                {
                    ts_pid_t *pmt = p_sys->pmt[i];
                    int i_prg;

                    for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
                    {
859
                        if( pmt->psi->prg[i_prg]->i_number == p_sys->i_dvb_program )
860
861
862
863
864
865
866
867
868
869
                        {
                            i_pmt_pid = p_sys->pmt[i]->i_pid;
                            break;
                        }
                    }
                    if( i_pmt_pid > 0 ) break;
                }

                if( i_pmt_pid > 0 )
                {
870
871
872
                    stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
                                    ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
                                    VLC_FALSE );
873
874
875
876
                    /* All ES */
                    for( i = 2; i < 8192; i++ )
                    {
                        ts_pid_t *pid = &p_sys->pid[i];
Eric Petit's avatar
Eric Petit committed
877
                        int i_prg;
878
879
880
881
882

                        if( !pid->b_valid || pid->psi ) continue;

                        for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
                        {
883
                            if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
884
885
                            {
                                /* We only remove es that aren't defined by extra pmt */
886
887
888
889
                                stream_Control( p_demux->s,
                                                STREAM_CONTROL_ACCESS,
                                                ACCESS_SET_PRIVATE_ID_STATE,
                                                i, VLC_FALSE );
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
                                break;
                            }
                        }
                    }
                }

                /* select new program */
                p_sys->i_dvb_program = i_int;
                i_pmt_pid = -1;
                for( i = 0; i < p_sys->i_pmt; i++ )
                {
                    ts_pid_t *pmt = p_sys->pmt[i];
                    int i_prg;

                    for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
                    {
                        if( pmt->psi->prg[i_prg]->i_number == i_int )
                        {
                            i_pmt_pid = p_sys->pmt[i]->i_pid;
909
                            p_prg = p_sys->pmt[i]->psi->prg[i_prg];
910
911
912
913
914
915
916
                            break;
                        }
                    }
                    if( i_pmt_pid > 0 ) break;
                }
                if( i_pmt_pid > 0 )
                {
917
918
919
                    stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
                                    ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
                                    VLC_TRUE );
920
921
922
923
                    stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
                                    ACCESS_SET_PRIVATE_ID_STATE, p_prg->i_pid_pcr,
                                    VLC_TRUE );

924
925
926
                    for( i = 2; i < 8192; i++ )
                    {
                        ts_pid_t *pid = &p_sys->pid[i];
Eric Petit's avatar
Eric Petit committed
927
                        int i_prg;
928
929
930
931
932

                        if( !pid->b_valid || pid->psi ) continue;

                        for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
                        {
933
                            if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
934
                            {
935
936
937
938
939
940
941
942
943
944
945
946
947
                                if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
                                    i_vpid = i;
                                if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
                                    i_apid1 = i;
                                else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
                                    i_apid2 = i;
                                else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
                                    i_apid3 = i;

                                stream_Control( p_demux->s,
                                                STREAM_CONTROL_ACCESS,
                                                ACCESS_SET_PRIVATE_ID_STATE,
                                                i, VLC_TRUE );
948
949
950
951
                                break;
                            }
                        }
                    }
952
953

                    /* Set CAM descrambling */
954
                    DVBCAPMTSend( p_demux );
955
956
                }
            }
957
958
959
960
961
            else
            {
                p_sys->i_dvb_program = -1;
                p_sys->p_programs_list = p_list;
            }
962
            return VLC_SUCCESS;
963
        }
964

965
        case DEMUX_GET_FPS:
966
        case DEMUX_SET_TIME:
967
968
969
970
971
972
973
        default:
            return VLC_EGENERIC;
    }
}

static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
{
974
975
    vlc_bool_t b_old_valid = pid->b_valid;

976
977
978
    pid->b_valid    = VLC_TRUE;
    pid->i_cc       = 0xff;
    pid->p_owner    = p_owner;
979
    pid->i_owner_number = 0;
980

981
982
983
    pid->extra_es   = NULL;
    pid->i_extra_es = 0;

984
985
986
987
    if( b_psi )
    {
        pid->es  = NULL;

988
989
990
991
992
        if( !b_old_valid )
        {
            pid->psi = malloc( sizeof( ts_psi_t ) );
            pid->psi->i_prg = 0;
            pid->psi->prg   = NULL;
993
            pid->psi->handle= NULL;
994
995
996
997
998
999
1000
1001
1002
        }
        pid->psi->i_pat_version  = -1;
        if( p_owner )
        {
            ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
            /* PMT */
            prg->i_version  = -1;
            prg->i_number   = -1;
            prg->i_pid_pcr  = -1;
1003
            prg->i_pid_pmt  = -1;
1004
            prg->iod        = NULL;
1005
1006
            prg->p_capmt    = NULL;
            prg->i_capmt_size = 0;
1007
            prg->handle     = NULL;
1008
1009
1010

            TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
        }
1011
1012
1013
1014
1015
1016
1017
1018
1019
    }
    else
    {
        pid->psi = NULL;
        pid->es  = malloc( sizeof( ts_es_t ) );

        es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
        pid->es->id      = NULL;
        pid->es->p_pes   = NULL;
1020
1021
        pid->es->i_pes_size= 0;
        pid->es->i_pes_gathered= 0;
1022
        pid->es->pp_last = &pid->es->p_pes;
1023
        pid->es->p_mpeg4desc = NULL;
1024
        pid->es->b_gather = VLC_FALSE;
1025
1026
1027
    }
}

1028
1029
1030
1031
static void PIDClean( es_out_t *out, ts_pid_t *pid )
{
    if( pid->psi )
    {
1032
1033
        int i;

1034
        if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
1035
1036
        for( i = 0; i < pid->psi->i_prg; i++ )
        {
1037
1038
            if( pid->psi->prg[i]->iod )
                IODFree( pid->psi->prg[i]->iod );
1039
1040
            if ( pid->psi->prg[i]->i_capmt_size )
                free( pid->psi->prg[i]->p_capmt );
1041
1042
            if( pid->psi->prg[i]->handle )
                dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1043
1044
1045
            free( pid->psi->prg[i] );
        }
        if( pid->psi->prg ) free( pid->psi->prg );
1046
1047
1048
1049
        free( pid->psi );
    }
    else
    {
1050
1051
        int i;

1052
1053
        if( pid->es->id )
            es_out_Del( out, pid->es->id );
1054

1055
1056
        if( pid->es->p_pes )
            block_ChainRelease( pid->es->p_pes );
1057
1058
1059

        es_format_Clean( &pid->es->fmt );

1060
        free( pid->es );
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074

        for( i = 0; i < pid->i_extra_es; i++ )
        {
            if( pid->extra_es[i]->id )
                es_out_Del( out, pid->extra_es[i]->id );

            if( pid->extra_es[i]->p_pes )
                block_ChainRelease( pid->extra_es[i]->p_pes );

            es_format_Clean( &pid->extra_es[i]->fmt );

            free( pid->extra_es[i] );
        }
        if( pid->i_extra_es ) free( pid->extra_es );
1075
    }
1076

1077
1078
1079
    pid->b_valid = VLC_FALSE;
}

1080
1081
1082
/****************************************************************************
 * gathering stuff
 ****************************************************************************/
1083
static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1084
1085
1086
{
    block_t *p_pes = pid->es->p_pes;
    uint8_t header[30];
1087
    int     i_pes_size = 0;
1088
1089
1090
    int     i_skip = 0;
    mtime_t i_dts = -1;
    mtime_t i_pts = -1;
1091
    mtime_t i_length = 0;
1092
1093
1094
1095
    int i_max;

    /* remove the pes from pid */
    pid->es->p_pes = NULL;
1096
1097
    pid->es->i_pes_size= 0;
    pid->es->i_pes_gathered= 0;
1098
    pid->es->pp_last = &pid->es->p_pes;
1099
1100
1101
1102

    /* FIXME find real max size */
    i_max = block_ChainExtract( p_pes, header, 30 );

1103

1104
1105
    if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
    {
1106
        if( !p_demux->p_sys->b_silent )
1107
            msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: %d)",
1108
                      header[0], header[1],header[2],header[3], pid->i_pid );
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
        block_ChainRelease( p_pes );
        return;
    }

    /* TODO check size */
    switch( header[3] )
    {
        case 0xBC:  /* Program stream map */
        case 0xBE:  /* Padding */
        case 0xBF:  /* Private stream 2 */
        case 0xB0:  /* ECM */
        case 0xB1:  /* EMM */
        case 0xFF:  /* Program stream directory */
        case 0xF2:  /* DSMCC stream */
        case 0xF8:  /* ITU-T H.222.1 type E stream */
            i_skip = 6;
            break;
        default:
            if( ( header[6]&0xC0 ) == 0x80 )
            {
                /* mpeg2 PES */
                i_skip = header[8] + 9;

                if( header[7]&0x80 )    /* has pts */
                {
                    i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
                             (mtime_t)(header[10] << 22)|
                            ((mtime_t)(header[11]&0xfe) << 14)|
                             (mtime_t)(header[12] << 7)|
1138
                             (mtime_t)(header[13] >> 1);
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169

                    if( header[7]&0x40 )    /* has dts */
                    {
                         i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
                                 (mtime_t)(header[15] << 22)|
                                ((mtime_t)(header[16]&0xfe) << 14)|
                                 (mtime_t)(header[17] << 7)|
                                 (mtime_t)(header[18] >> 1);
                    }
                }
            }
            else
            {
                i_skip = 6;
                while( i_skip < 23 && header[i_skip] == 0xff )
                {
                    i_skip++;
                }
                if( i_skip == 23 )
                {
                    msg_Err( p_demux, "too much MPEG-1 stuffing" );
                    block_ChainRelease( p_pes );
                    return;
                }
                if( ( header[i_skip] & 0xC0 ) == 0x40 )
                {
                    i_skip += 2;
                }

                if(  header[i_skip]&0x20 )
                {
1170
1171
1172
1173
1174
                     i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
                              (mtime_t)(header[i_skip+1] << 22)|
                             ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
                              (mtime_t)(header[i_skip+3] << 7)|
                              (mtime_t)(header[i_skip+4] >> 1);
1175
1176
1177

                    if( header[i_skip]&0x10 )    /* has dts */
                    {
1178
1179
1180
1181
1182
                         i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
                                  (mtime_t)(header[i_skip+6] << 22)|
                                 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
                                  (mtime_t)(header[i_skip+8] << 7)|
                                  (mtime_t)(header[i_skip+9] >> 1);
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
                         i_skip += 10;
                    }
                    else
                    {
                        i_skip += 5;
                    }
                }
                else
                {
                    i_skip += 1;
                }
            }
            break;
    }

    if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
        pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
    {
        i_skip += 4;
    }
    else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
             pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
             pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
    {
        i_skip += 1;
    }
1209
1210
    else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
             pid->es->p_mpeg4desc )
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
    {
        decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;

        if( dcd->i_decoder_specific_info_len > 2 &&
            dcd->p_decoder_specific_info[0] == 0x10 &&
            ( dcd->p_decoder_specific_info[1]&0x10 ) )
        {
            /* display length */
            if( p_pes->i_buffer + 2 <= i_skip )
            {
                i_length = GetWBE( &p_pes->p_buffer[i_skip] );
            }

            i_skip += 2;
        }
        if( p_pes->i_buffer + 2 <= i_skip )
        {
            i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
        }
        /* */
        i_skip += 2;
    }
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251

    /* skip header */
    while( p_pes && i_skip > 0 )
    {
        if( p_pes->i_buffer <= i_skip )
        {
            block_t *p_next = p_pes->p_next;

            i_skip -= p_pes->i_buffer;
            block_Release( p_pes );
            p_pes = p_next;
        }
        else
        {
            p_pes->i_buffer -= i_skip;
            p_pes->p_buffer += i_skip;
            break;
        }
    }
1252

1253
1254
    if( p_pes )
    {
1255
1256
1257
        block_t *p_block;
        int i;

1258
1259
1260
1261
1262
1263
1264
1265
        if( i_dts >= 0 )
        {
            p_pes->i_dts = i_dts * 100 / 9;
        }
        if( i_pts >= 0 )
        {
            p_pes->i_pts = i_pts * 100 / 9;
        }
1266
        p_pes->i_length = i_length * 100 / 9;
1267

1268
1269
        p_block = block_ChainGather( p_pes );
        if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1270
        {
1271
1272
1273
1274
1275
1276
1277
            if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
            {
                p_block->i_buffer = i_pes_size;
            }
            /* Append a \0 */
            p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
            p_block->p_buffer[p_block->i_buffer -1] = '\0';
1278
        }
1279
1280
1281
1282
1283
1284
1285
1286

        for( i = 0; i < pid->i_extra_es; i++ )
        {
            es_out_Send( p_demux->out, pid->extra_es[i]->id,
                         block_Duplicate( p_block ) );
        }

        es_out_Send( p_demux->out, pid->es->id, p_block );
1287
1288
1289
1290
1291
    }
    else
    {
        msg_Warn( p_demux, "empty pes" );
    }
gbazin's avatar
   
gbazin committed
1292
}
1293

1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
{
    demux_sys_t   *p_sys = p_demux->p_sys;
    const uint8_t *p = p_bk->p_buffer;

    if( ( p[3]&0x20 ) && /* adaptation */
        ( p[5]&0x10 ) &&
        ( p[4] >= 7 ) )
    {
        int i;
        mtime_t i_pcr;  /* 33 bits */

        i_pcr = ( (mtime_t)p[6] << 25 ) |
                ( (mtime_t)p[7] << 17 ) |
                ( (mtime_t)p[8] << 9 ) |
                ( (mtime_t)p[9] << 1 ) |
                ( (mtime_t)p[10] >> 7 );

        /* Search program and set the PCR */
        for( i = 0; i < p_sys->i_pmt; i++ )
        {
1315
1316
            int i_prg;
            for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1317
            {
1318
1319
1320
1321
1322
1323
                if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
                {
                    es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
                                    (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
                                    (int64_t)(i_pcr * 100 / 9) );
                }
1324
1325
1326
1327
1328
            }
        }
    }
}

1329
1330
static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
{
1331
    const uint8_t    *p = p_bk->p_buffer;
1332
1333
1334
1335
    const vlc_bool_t b_unit_start = p[1]&0x40;
    const vlc_bool_t b_adaptation = p[3]&0x20;
    const vlc_bool_t b_payload    = p[3]&0x10;
    const int        i_cc         = p[3]&0x0f;   /* continuity counter */
1336

1337
1338
1339
    /* transport_scrambling_control is ignored */

    int         i_skip = 0;
1340
    vlc_bool_t  i_ret  = VLC_FALSE;
1341
1342
    int         i_diff;

1343
#if 0
1344
    msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
1345
1346
             "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
             b_payload, i_cc );
1347
1348
#endif

1349
1350
1351
1352
    /* For now, ignore additional error correction
     * TODO: handle Reed-Solomon 204,188 error correction */
    p_bk->i_buffer = TS_PACKET_SIZE_188;

1353
1354
    if( p[1]&0x80 )
    {
1355
        msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
1356
                 pid->i_pid );
1357
1358
    }

1359
1360
1361
1362
1363
    if( p_demux->p_sys->csa )
    {
        csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer );
    }

1364
1365
    if( !b_adaptation )
    {
1366
1367
        /* We don't have any adaptation_field, so payload starts
         * immediately after the 4 byte TS header */
1368
1369
1370
1371
        i_skip = 4;
    }
    else
    {
1372
        /* p[4] is adaptation_field_length minus one */
1373
1374
1375
1376
1377
        i_skip = 5 + p[4];
        if( p[4] > 0 )
        {
            if( p[5]&0x80 )
            {
1378
                msg_Warn( p_demux, "discontinuity_indicator (pid=%d) "
1379
                          "ignored", pid->i_pid );
1380
1381
1382
            }
        }
    }
1383

1384
    /* Test continuity counter */
1385
1386
1387
    /* continuous when (one of this):
        * diff == 1
        * diff == 0 and payload == 0
1388
1389
        * diff == 0 and duplicate packet (playload != 0) <- should we
        *   test the content ?
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
     */

    i_diff = ( i_cc - pid->i_cc )&0x0f;
    if( b_payload && i_diff == 1 )
    {
        pid->i_cc