ts.c 110 KB
Newer Older
1
2
3
/*****************************************************************************
 * ts.c: Transport Stream input module for VLC.
 *****************************************************************************
4
 * Copyright (C) 2004-2005 VideoLAN (Centrale Réseaux) and its contributors
5
 * $Id$
6
7
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Jean-Paul Saman <jpsaman #_at_# m2x.nl>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 * 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() */
Laurent Aimar's avatar
Laurent Aimar committed
29
#include <ctype.h>
30
31
32
33
34

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

#include "iso_lang.h"
35
#include "network.h"
36

37
38
#include "../mux/mpeg/csa.h"

39
40
41
/* Include dvbpsi headers */
#ifdef HAVE_DVBPSI_DR_H
#   include <dvbpsi/dvbpsi.h>
42
#   include <dvbpsi/demux.h>
43
44
45
#   include <dvbpsi/descriptor.h>
#   include <dvbpsi/pat.h>
#   include <dvbpsi/pmt.h>
46
#   include <dvbpsi/sdt.h>
47
48
49
50
#   include <dvbpsi/dr.h>
#   include <dvbpsi/psi.h>
#else
#   include "dvbpsi.h"
51
#   include "demux.h"
52
53
54
#   include "descriptor.h"
#   include "tables/pat.h"
#   include "tables/pmt.h"
55
#   include "tables/sdt.h"
56
57
58
59
#   include "descriptors/dr.h"
#   include "psi.h"
#endif

60
61
62
63
64
65
66
67
68
/* EIT support */
#ifdef _DVBPSI_DR_4D_H_
#   define TS_USE_DVB_SI 1
#   ifdef HAVE_DVBPSI_DR_H
#       include <dvbpsi/eit.h>
#   else
#       include "tables/eit.h"
#   endif
#endif
69

70
/* TODO:
71
 *  - XXX: do not mark options message to be translated, they are too osbcure for now ...
72
73
74
75
76
77
78
 *  - test it
 *  - ...
 */

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
79
80
static int  Open  ( vlc_object_t * );
static void Close ( vlc_object_t * );
81

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#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")

102
103
104
#define CAPMT_SYSID_TEXT N_("CAPMT System ID")
#define CAPMT_SYSID_LONGTEXT N_("only forward descriptors from this SysID to the CAM")

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#define CPKT_TEXT N_("Packet size in bytes to decrypt")
#define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
    "The decryption routines subtract the TS-header from the value before " \
    "decrypting. " )

#define TSDUMP_TEXT N_("Filename of dump")
#define TSDUMP_LONGTEXT N_("Specify a filename where to dump the TS in")

#define APPEND_TEXT N_("Append")
#define APPEND_LONGTEXT N_( \
    "If the file exists and this option is selected, the existing file " \
    "will not be overwritten." )

#define DUMPSIZE_TEXT N_("Dump buffer size")
#define DUMPSIZE_LONGTEXT N_( \
    "Tweak the buffer size for reading and writing an integer number of packets." \
    "Specify the size of the buffer here and not the number of packets." )

123
vlc_module_begin();
124
    set_description( _("MPEG Transport Stream demuxer") );
125
    set_shortname ( "MPEG-TS" );
zorglub's avatar
zorglub committed
126
127
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
128
129
130
131
132
133
134

    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 );
135
    add_integer( "ts-csa-pkt", 188, NULL, CPKT_TEXT, CPKT_LONGTEXT, VLC_TRUE );    
136
137
    add_bool( "ts-silent", 0, NULL, SILENT_TEXT, SILENT_LONGTEXT, VLC_TRUE );

138
139
140
141
142
    add_file( "ts-dump-file", NULL, NULL, TSDUMP_TEXT, TSDUMP_LONGTEXT, VLC_FALSE );
    add_bool( "ts-dump-append", 0, NULL, APPEND_TEXT, APPEND_LONGTEXT, VLC_FALSE );
    add_integer( "ts-dump-size", 16384, NULL, DUMPSIZE_TEXT,
                 DUMPSIZE_LONGTEXT, VLC_TRUE );

143
144
    set_capability( "demux2", 10 );
    set_callbacks( Open, Close );
Laurent Aimar's avatar
Laurent Aimar committed
145
    add_shortcut( "ts" );
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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;
211

212
213
214
215
} es_mpeg4_descriptor_t;

typedef struct
{
216
    uint8_t                i_iod_label, i_iod_label_scope;
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

    /* 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
{
234
235
    dvbpsi_handle   handle;

236
237
238
    int             i_version;
    int             i_number;
    int             i_pid_pcr;
239
    int             i_pid_pmt;
240
241
    /* IOD stuff (mpeg4) */
    iod_descriptor_t *iod;
242

243
244
} ts_prg_psi_t;

245
246
247
248
typedef struct
{
    /* for special PAT/SDT case */
    dvbpsi_handle   handle; /* PAT/SDT/EIT */
249
    int             i_pat_version;
250
    int             i_sdt_version;
251
252
253
254

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

256
257
258
259
260
261
} ts_psi_t;

typedef struct
{
    es_format_t  fmt;
    es_out_id_t *id;
262
263
    int         i_pes_size;
    int         i_pes_gathered;
264
    block_t     *p_pes;
265
    block_t     **pp_last;
266
267

    es_mpeg4_descriptor_t *p_mpeg4desc;
268
    int         b_gather;
269

270
271
272
273
274
275
276
277
278
279
280
281
} 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;
282
    int         i_owner_number;
283
284
285
286
287

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

288
289
290
291
    /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
    ts_es_t     **extra_es;
    int         i_extra_es;

292
293
294
295
} ts_pid_t;

struct demux_sys_t
{
296
297
298
    /* TS packet size (188, 192, 204) */
    int         i_packet_size;

299
300
301
    /* how many TS packet we read at once */
    int         i_ts_read;

302
303
304
305
306
307
    /* All pid */
    ts_pid_t    pid[8192];

    /* All PMT */
    int         i_pmt;
    ts_pid_t    **pmt;
308
309
310

    /* */
    vlc_bool_t  b_es_id_pid;
311
    csa_t       *csa;
312
    int         i_csa_pkt_size;
313
    vlc_bool_t  b_silent;
314
315
316
317

    vlc_bool_t  b_udp_out;
    int         fd; /* udp socket */
    uint8_t     *buffer;
318
319
320

    vlc_bool_t  b_dvb_control;
    int         i_dvb_program;
321
    vlc_list_t  *p_programs_list;
322

323
324
325
326
327
328
    /* TS dump */
    char        *psz_file;  /* file to dump data in */
    FILE        *p_file;    /* filehandle */
    uint64_t    i_write;    /* bytes written */
    vlc_bool_t  b_file_out; /* dump mode enabled */
    
329
330
    /* */
    vlc_bool_t  b_meta;
331
332
};

333
334
static int Demux    ( demux_t *p_demux );
static int DemuxFile( demux_t *p_demux );
335
336
static int Control( demux_t *p_demux, int i_query, va_list args );

337
338
339
340
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 );

341
342
static void PATCallBack( demux_t *, dvbpsi_pat_t * );
static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
343
#ifdef TS_USE_DVB_SI
344
345
static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
                                 uint8_t  i_table_id, uint16_t i_extension );
346
#endif
347

348
349
350
351
352
353
354
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 );

355
356
static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );

357
static iod_descriptor_t *IODNew( int , uint8_t * );
358
static void              IODFree( iod_descriptor_t * );
359

360
361
362
363
#define TS_PACKET_SIZE_188 188
#define TS_PACKET_SIZE_192 192
#define TS_PACKET_SIZE_204 204
#define TS_PACKET_SIZE_MAX 204
364
365
366
367
368
369
370
371
372
373

/*****************************************************************************
 * 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;
374
375
    int          i_sync, i_peek, i;
    int          i_packet_size;
376

377
378
379
    ts_pid_t    *pat;
    char        *psz_mode;
    vlc_bool_t   b_append;
380
381
382

    vlc_value_t  val;

383
    if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
384
        TS_PACKET_SIZE_MAX ) return VLC_EGENERIC;
385

386
387
    /* Search first sync byte */
    for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
388
389
390
    {
        if( p_peek[i_sync] == 0x47 ) break;
    }
391
    if( i_sync >= TS_PACKET_SIZE_MAX )
392
    {
393
        if( strcmp( p_demux->psz_demux, "ts" ) ) return VLC_EGENERIC;
394
395
        msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
    }
396
397
398
399

    /* 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 )
400
    {
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
        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;
430
    }
431

432
433
    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
    memset( p_sys, 0, sizeof( demux_sys_t ) );
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
    p_sys->i_packet_size = i_packet_size;

    /* Fill dump mode fields */
    p_sys->i_write = 0;
    p_sys->p_file = NULL;
    p_sys->b_file_out = VLC_FALSE;
    p_sys->psz_file = var_CreateGetString( p_demux, "ts-dump-file" );
    if( *p_sys->psz_file != '\0' )
    {
        p_sys->b_file_out = VLC_TRUE;

        var_Create( p_demux, "ts-dump-append", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
        var_Get( p_demux, "ts-dump-append", &val );
        b_append = val.b_bool;
        if ( b_append )
            psz_mode = "ab";
        else
            psz_mode = "wb";

        if( !strcmp( p_sys->psz_file, "-" ) )
        {
            msg_Info( p_demux, "dumping raw stream to standard output" );
            p_sys->p_file = stdout;
        }
        else if( ( p_sys->p_file = fopen( p_sys->psz_file, psz_mode ) ) == NULL )
        {
            msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
            p_sys->b_file_out = VLC_FALSE;
        }

        if( p_sys->b_file_out )
        {
            vlc_value_t bufsize;

            /* Determine how many packets to read. */
            var_Create( p_demux, "ts-dump-size",
                        VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
            var_Get( p_demux, "ts-dump-size", &bufsize );
            p_sys->i_ts_read = (int) (bufsize.i_int / p_sys->i_packet_size);
            if( p_sys->i_ts_read <= 0 )
            {
                p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
            }
            p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
            msg_Info( p_demux, "%s raw stream to file `%s' reading packets %d",
                      b_append ? "appending" : "dumping", p_sys->psz_file,
                      p_sys->i_ts_read );
        }
    }

484
    /* Fill p_demux field */
485
486
487
488
    if( p_sys->b_file_out )
        p_demux->pf_demux = DemuxFile;
    else
        p_demux->pf_demux = Demux;
489
490
491
    p_demux->pf_control = Control;

    /* Init p_sys field */
492
    p_sys->b_meta = VLC_TRUE;
493
494
    p_sys->b_dvb_control = VLC_TRUE;
    p_sys->i_dvb_program = 0;
495
496
497
498
499
500
501
502
    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;
    }
503
    p_sys->i_packet_size = i_packet_size;
504
505
    p_sys->b_udp_out = VLC_FALSE;
    p_sys->i_ts_read = 50;
506
    p_sys->csa = NULL;
507
508
509
510

    /* Init PAT handler */
    pat = &p_sys->pid[0];
    PIDInit( pat, VLC_TRUE, NULL );
511
512
    pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
                                         p_demux );
513
#ifdef TS_USE_DVB_SI
514
515
516
517
518
519
    if( p_sys->b_meta )
    {
        ts_pid_t *sdt = &p_sys->pid[0x11];
        ts_pid_t *eit = &p_sys->pid[0x12];

        PIDInit( sdt, VLC_TRUE, NULL );
520
521
522
        sdt->psi->handle =
            dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
                                p_demux );
523
        PIDInit( eit, VLC_TRUE, NULL );
524
525
526
        eit->psi->handle =
            dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
                                p_demux );
527
528
529
530
531
532
533
534
535
        if( p_sys->b_dvb_control )
        {
            stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
                            ACCESS_SET_PRIVATE_ID_STATE, 0x11, VLC_TRUE );
            stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
                            ACCESS_SET_PRIVATE_ID_STATE, 0x12, VLC_TRUE );
        }
    }
#endif
536
537
538
539
540

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

541
    /* Read config */
542
543
    var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "ts-es-id-pid", &val );
544
    p_sys->b_es_id_pid = val.b_bool;
545

546
547
    var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "ts-out", &val );
548
    if( val.psz_string && *val.psz_string && !p_sys->b_file_out )
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
    {
        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
        {
572
573
            var_Create( p_demux, "ts-out-mtu",
                        VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
574
            var_Get( p_demux, "ts-out-mtu", &mtu );
575
            p_sys->i_ts_read = mtu.i_int / p_sys->i_packet_size;
576
577
            if( p_sys->i_ts_read <= 0 )
            {
578
                p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
579
            }
580
            p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
581
582
583
584
585
586
587
        }
    }
    if( val.psz_string )
    {
        free( val.psz_string );
    }

588
589
590
591
592
593
594
595
596
597
598
599
    /* 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];

600
            msg_Dbg( p_demux, "extra pmt specified (pid=%d)", i_pid );
601
            PIDInit( pmt, VLC_TRUE, NULL );
Laurent Aimar's avatar
Laurent Aimar committed
602
603
            pmt->psi->i_prg = 1;
            pmt->psi->prg = malloc( sizeof(ts_prg_psi_t) );
604
            /* FIXME we should also ask for a number */
605
            pmt->psi->prg[0]->handle =
606
607
                dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
                                  p_demux );
608
            pmt->psi->prg[0]->i_number = 0; /* special one */
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624

            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 );
625
626
                    if( i_pid >= 2 && i_pid < 8192 &&
                        !p_sys->pid[i_pid].b_valid )
627
628
629
630
                    {
                        ts_pid_t *pid = &p_sys->pid[i_pid];

                        PIDInit( pid, VLC_FALSE, pmt->psi);
631
                        if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
632
                        {
633
                            pmt->psi->prg[0]->i_pid_pcr = i_pid;
634
                        }
635
                        PIDFillFormat( pid, i_stream_type);
636
637
                        if( pid->es->fmt.i_cat != UNKNOWN_ES )
                        {
638
639
640
641
                            if( p_sys->b_es_id_pid )
                            {
                                pid->es->fmt.i_id = i_pid;
                            }
642
                            msg_Dbg( p_demux, "  * es pid=%d type=%d "
643
644
645
646
                                     "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 );
647
648
649
650
651
652
653
654
655
656
657
658
                        }
                    }
                }
                psz = psz_next;
            }
        }
    }
    if( val.psz_string )
    {
        free( val.psz_string );
    }

659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
    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
        {
gbazin's avatar
gbazin committed
674
#ifndef UNDER_CE
675
            uint64_t i_ck = strtoull( psz, NULL, 16 );
gbazin's avatar
gbazin committed
676
677
678
#else
            uint64_t i_ck = strtoll( psz, NULL, 16 );
#endif
679
680
681
682
683
684
            uint8_t ck[8];
            int     i;
            for( i = 0; i < 8; i++ )
            {
                ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
            }
685
#ifndef TS_NO_CSA_CK_MSG
686
687
            msg_Dbg( p_demux, "using CSA scrambling with "
                     "ck=%x:%x:%x:%x:%x:%x:%x:%x",
688
                     ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
689
#endif
690
            p_sys->csa = csa_New();
691
692
693
            
            if( p_sys->csa )
            {
694
695
                vlc_value_t pkt_val;
                
696
697
698
                csa_SetCW( p_sys->csa, ck, ck );
            
                var_Create( p_demux, "ts-csa-pkt", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
699
700
                var_Get( p_demux, "ts-csa-pkt", &pkt_val );
                if( pkt_val.i_int < 4 || pkt_val.i_int > 188 )
701
                {
702
                    msg_Err( p_demux, "wrong packet size %d specified.", pkt_val.i_int );
703
704
705
                    msg_Warn( p_demux, "using default packet size of 188 bytes" );
                    p_sys->i_csa_pkt_size = 188;
                }
706
                else p_sys->i_csa_pkt_size = pkt_val.i_int;
707
708
                msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
            }
709
710
711
712
713
714
715
        }
    }
    if( val.psz_string )
    {
        free( val.psz_string );
    }

716
717
718
    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;
719

720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
    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 );
744
                    free( pid->psi );
745
746
                    break;
                case 1: /* CAT */
747
                    free( pid->psi );
748
                    break;
749
750
751
752
753
                case 0x11: /* SDT */
                case 0x12: /* EIT */
                    dvbpsi_DetachDemux( pid->psi->handle );
                    free( pid->psi );
                    break;
754
                default:
755
                    PIDClean( p_demux->out, pid );
756
757
758
759
760
                    break;
            }
        }
        else if( pid->b_valid && pid->es )
        {
761
            PIDClean( p_demux->out, pid );
762
        }
763

764
765
        if( pid->b_seen )
        {
766
            msg_Dbg( p_demux, "  - pid[%d] seen", pid->i_pid );
767
        }
768
769
770
771
772
773
774

        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 );
        }

775
776
    }

777
778
779
780
781
    if( p_sys->b_udp_out )
    {
        net_Close( p_sys->fd );
        free( p_sys->buffer );
    }
782
783
784
785
    if( p_sys->csa )
    {
        csa_Delete( p_sys->csa );
    }
786

gbazin's avatar
   
gbazin committed
787
    if( p_sys->i_pmt ) free( p_sys->pmt );
788
789
790
791
792
793
794
795

    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 );
    }

796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
    /* If in dump mode, then close the file */
    if( p_sys->b_file_out )
    {
        msg_Info( p_demux ,"closing %s ("I64Fd" Kbytes dumped)", p_sys->psz_file,
                  p_sys->i_write / 1024 );

        if( p_sys->p_file != stdout )
        {
            fclose( p_sys->p_file );
            p_sys->p_file = NULL;
        }
        free( p_sys->psz_file );
        p_sys->psz_file = NULL;

        free( p_sys->buffer );
    }

813
814
815
    free( p_sys );
}

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
/*****************************************************************************
 * DemuxFile:
 *****************************************************************************/
static int DemuxFile( demux_t *p_demux )
{    
    demux_sys_t *p_sys = p_demux->p_sys;
    uint8_t     *p_buffer = p_sys->buffer; /* Put first on sync byte */
    int i_diff= 0;
    int i_data= 0;
    int i_pos = 0;
    int i_bufsize = p_sys->i_packet_size * p_sys->i_ts_read;
    
    i_data = stream_Read( p_demux->s, p_sys->buffer, i_bufsize );
    if( (i_data <= 0) && (i_data < p_sys->i_packet_size) )
    {
        msg_Dbg( p_demux, "Error reading malformed packets" );
        return i_data;
    }

    /* Test continuity counter */
    while( i_pos < i_data )
    {
        ts_pid_t    *p_pid;   /* point to a PID structure */
        vlc_bool_t b_payload; /* indicates a packet with payload */
        vlc_bool_t b_adaptation; /* adaptation field */
        int i_cc  = 0;        /* continuity counter */
    
        if( p_sys->buffer[i_pos] != 0x47 )
        {
            msg_Warn( p_demux, "lost sync" );
            while( !p_demux->b_die && (i_pos < i_data) )
            {
                i_pos++;
                if( p_sys->buffer[i_pos] == 0x47 )
                    break;
            }
            if( !p_demux->b_die )
                msg_Warn( p_demux, "sync found" );
        }
        
        /* continuous when (one of this):
         * diff == 1
         * diff == 0 and payload == 0
         * diff == 0 and duplicate packet (playload != 0) <- should we
         *   test the content ?
         */
        i_cc  = p_buffer[i_pos+3]&0x0f;
        b_payload = p_buffer[i_pos+3]&0x10;
        b_adaptation = p_buffer[i_pos+3]&0x20;
        
        /* Get the PID */
        p_pid = &p_sys->pid[ ((p_buffer[i_pos+1]&0x1f)<<8)|p_buffer[i_pos+2] ];
        
        /* Detect discontinuity indicator in adaptation field */
        if( b_adaptation )
        {
            if( p_buffer[i_pos+5]&0x80 )
                msg_Warn( p_demux, "discontinuity indicator (pid=%d) ", p_pid->i_pid );
            if( p_buffer[i_pos+5]&0x40 )
                msg_Warn( p_demux, "random access indicator (pid=%d) ", p_pid->i_pid );
        }
 
        i_diff = ( i_cc - p_pid->i_cc )&0x0f;
        if( b_payload && i_diff == 1 )
        {
            p_pid->i_cc++;
        }
        else
        {
            if( p_pid->i_cc == 0xff )
            {
                msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
                        p_pid->i_pid, i_cc );
                p_pid->i_cc = i_cc;
            }
            else if( i_diff != 0 )
            {
                /* FIXME what to do when discontinuity_indicator is set ? */
                msg_Warn( p_demux, "transport error detected 0x%x instead of 0x%x",
                          i_cc, ( p_pid->i_cc + 1 )&0x0f );
    
                p_pid->i_cc = i_cc;
                /* Mark transport error in the TS packet. */
                p_buffer[i_pos+1] |= 0x80;
            }
        }
        
        /* Test if user wants to decrypt it first */
        if( p_sys->csa )
            csa_Decrypt( p_demux->p_sys->csa, &p_buffer[i_pos], p_demux->p_sys->i_csa_pkt_size );
        
        i_pos += p_sys->i_packet_size;
    }

    /* Then write */
    i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
    if( i_data < 0 )
    {
        msg_Err( p_demux, "failed to write data" );
        return -1;
    }
#if 0
    msg_Dbg( p_demux, "dumped %d bytes", i_data );
#endif

    p_sys->i_write += i_data;
    return 1;
}

925
926
927
928
929
930
931
932
933
/*****************************************************************************
 * 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 */
934
    for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
935
936
937
938
939
940
    {
        vlc_bool_t  b_frame = VLC_FALSE;
        block_t     *p_pkt;
        ts_pid_t    *p_pid;

        /* Get a new TS packet */
941
        if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
942
943
944
945
        {
            msg_Dbg( p_demux, "eof ?" );
            return 0;
        }
946
947

        /* Check sync byte and re-sync if needed */
948
949
950
951
952
        if( p_pkt->p_buffer[0] != 0x47 )
        {
            msg_Warn( p_demux, "lost synchro" );
            block_Release( p_pkt );

953
            while( !p_demux->b_die )
954
            {
955
956
                uint8_t *p_peek;
                int i_peek, i_skip = 0;
957

958
959
960
                i_peek = stream_Peek( p_demux->s, &p_peek,
                                      p_sys->i_packet_size * 10 );
                if( i_peek < p_sys->i_packet_size + 1 )
961
962
963
964
965
                {
                    msg_Dbg( p_demux, "eof ?" );
                    return 0;
                }

966
                while( i_skip < i_peek - p_sys->i_packet_size )
967
                {
968
969
                    if( p_peek[i_skip] == 0x47 &&
                        p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
970
971
972
973
974
                    {
                        break;
                    }
                    i_skip++;
                }
975
976

                msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
977
                stream_Read( p_demux->s, NULL, i_skip );
978
979

                if( i_skip < i_peek - p_sys->i_packet_size )
980
981
982
983
                {
                    break;
                }
            }
984
985

            if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
986
987
988
989
990
991
            {
                msg_Dbg( p_demux, "eof ?" );
                return 0;
            }
        }

992
993
        if( p_sys->b_udp_out )
        {
994
995
            memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
                    p_pkt->p_buffer, p_sys->i_packet_size );
996
997
        }

998
999
1000
1001
1002
1003
1004
        /* Parse the TS packet */
        p_pid = &p_sys->pid[PIDGet( p_pkt )];

        if( p_pid->b_valid )
        {
            if( p_pid->psi )
            {
1005
                if( p_pid->i_pid == 0 || p_pid->i_pid == 0x11 || p_pid->i_pid == 0x12 )
1006
1007
1008
1009
1010
1011
1012
1013
                {
                    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++ )
                    {
1014
1015
                        dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
                                           p_pkt->p_buffer );
1016
1017
                    }
                }
1018
1019
                block_Release( p_pkt );
            }
1020
            else if( !p_sys->b_udp_out )
1021
1022
1023
            {
                b_frame = GatherPES( p_demux, p_pid, p_pkt );
            }
1024
1025
            else
            {
Laurent Aimar's avatar
Laurent Aimar committed
1026
                PCRHandle( p_demux, p_pid, p_pkt );
1027
1028
                block_Release( p_pkt );
            }
1029
1030
1031
1032
1033
        }
        else
        {
            if( !p_pid->b_seen )
            {
1034
                msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1035
            }
1036
1037
            /* We have to handle PCR if present */
            PCRHandle( p_demux, p_pid, p_pkt );
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
            block_Release( p_pkt );
        }
        p_pid->b_seen = VLC_TRUE;

        if( b_frame )
        {
            break;
        }
    }

1048
1049
1050
    if( p_sys->b_udp_out )
    {
        /* Send the complete block */
1051
        net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
1052
                   p_sys->i_ts_read * p_sys->i_packet_size );
1053
1054
    }

1055
1056
1057
1058
1059
1060
1061
1062
    return 1;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
1063
    demux_sys_t *p_sys = p_demux->p_sys;
1064
    double f, *pf;
1065
    int64_t i64;
1066
    int i_int;
1067

1068
1069
1070
    if( p_sys->b_file_out )
        return demux2_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );

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
    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 )
            {
1111
1112
                *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
                        p_sys->i_mux_rate;
1113
1114
1115
1116
1117
                return VLC_SUCCESS;
            }
            *pi64 = 0;
            return VLC_EGENERIC;
#endif
1118
        case DEMUX_SET_GROUP:
1119
1120
1121
        {
            uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
            ts_prg_psi_t *p_prg = NULL;
1122
            vlc_list_t *p_list;
1123

1124
            i_int = (int)va_arg( args, int );
1125
1126
            p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
            msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1127

1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
            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++ )
                    {
1141
                        if( pmt->psi->prg[i_prg]->i_number == p_sys->i_dvb_program )
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
                        {
                            i_pmt_pid = p_sys->pmt[i]->i_pid;
                            break;
                        }
                    }
                    if( i_pmt_pid > 0 ) break;
                }

                if( i_pmt_pid > 0 )
                {
1152
1153
1154
                    stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
                                    ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
                                    VLC_FALSE );
1155
1156
1157
1158
                    /* All ES */
                    for( i = 2; i < 8192; i++ )
                    {
                        ts_pid_t *pid = &p_sys->pid[i];
Eric Petit's avatar
Eric Petit committed
1159
                        int i_prg;
1160
1161
1162
1163
1164

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

                        for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
                        {
1165
                            if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1166
1167
                            {
                                /* We only remove es that aren't defined by extra pmt */
1168
1169
1170
1171
                                stream_Control( p_demux->s,
                                                STREAM_CONTROL_ACCESS,
                                                ACCESS_SET_PRIVATE_ID_STATE,
                                                i, VLC_FALSE );
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
                                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;
1191
                            p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1192
1193
1194
1195
1196
1197
1198
                            break;
                        }
                    }
                    if( i_pmt_pid > 0 ) break;
                }
                if( i_pmt_pid > 0 )
                {
1199
1200
1201
                    stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
                                    ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
                                    VLC_TRUE );
1202
1203
1204
1205
                    stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
                                    ACCESS_SET_PRIVATE_ID_STATE, p_prg->i_pid_pcr,
                                    VLC_TRUE );

1206
1207
1208
                    for( i = 2; i < 8192; i++ )
                    {
                        ts_pid_t *pid = &p_sys->pid[i];
Eric Petit's avatar
Eric Petit committed
1209
                        int i_prg;
1210
1211
1212
1213
1214

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

                        for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
                        {
1215
                            if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1216
                            {
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
                                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 );
1230
1231
1232
1233
1234
1235
                                break;
                            }
                        }
                    }
                }
            }
1236
1237
1238
1239
1240
            else
            {
                p_sys->i_dvb_program = -1;
                p_sys->p_programs_list = p_list;
            }
1241
            return VLC_SUCCESS;
1242
        }
1243

1244
        case DEMUX_GET_FPS:
1245
        case DEMUX_SET_TIME:
1246
1247
1248
1249
1250
1251
1252
        default:
            return VLC_EGENERIC;
    }
}

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

1255
1256
1257
    pid->b_valid    = VLC_TRUE;
    pid->i_cc       = 0xff;
    pid->p_owner    = p_owner;
1258
    pid->i_owner_number = 0;
1259

1260
1261
1262
    pid->extra_es   = NULL;
    pid->i_extra_es = 0;

1263
1264
1265
1266
    if( b_psi )
    {
        pid->es  = NULL;

1267
1268
1269
        if( !b_old_valid )
        {
            pid->psi = malloc( sizeof( ts_psi_t ) );
1270
            pid->psi->handle= NULL;
1271
1272
1273
1274
            pid->psi->i_prg = 0;
            pid->psi->prg   = NULL;
        }
        pid->psi->i_pat_version  = -1;
1275
        pid->psi->i_sdt_version  = -1;
1276
1277
1278
1279
1280
1281
1282
        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;
1283
            prg->i_pid_pmt  = -1;
1284
            prg->iod        = NULL;
1285
            prg->handle     = NULL;
1286
1287
1288

            TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
        }
1289
1290
1291
1292
1293
1294
1295
1296
1297
    }
    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;
1298
1299
        pid->es->i_pes_size= 0;
        pid->es->i_pes_gathered= 0;
1300
        pid->es->pp_last = &pid->es->p_pes;
1301
        pid->es->p_mpeg4desc = NULL;
1302
        pid->es->b_gather = VLC_FALSE;
1303
1304
1305
    }
}

1306
1307
1308
1309
static void PIDClean( es_out_t *out, ts_pid_t *pid )
{
    if( pid->psi )
    {
1310
1311
        int i;

1312
        if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
1313
1314
        for( i = 0; i < pid->psi->i_prg; i++ )
        {
1315
1316
1317
1318
            if( pid->psi->prg[i]->iod )
                IODFree( pid->psi->prg[i]->iod );
            if( pid->psi->prg[i]->handle )
                dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1319
1320
1321
            free( pid->psi->prg[i] );
        }
        if( pid->psi->prg ) free( pid->psi->prg );
1322
1323
1324
1325
        free( pid->psi );
    }
    else
    {
1326
1327
        int i;

1328
1329
        if( pid->es->id )
            es_out_Del( out, pid->es->id );
1330

1331
1332
        if( pid->es->p_pes )
            block_ChainRelease( pid->es->p_pes );
1333
1334
1335

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

1336
        free( pid->es );
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350

        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 );
1351
    }
1352

1353
1354
1355
    pid->b_valid = VLC_FALSE;
}

1356
1357
1358
/****************************************************************************
 * gathering stuff
 ****************************************************************************/
1359
static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1360
1361
1362
{
    block_t *p_pes = pid->es->p_pes;
    uint8_t header[30];
1363
    int     i_pes_size = 0;
1364
1365
1366
    int     i_skip = 0;
    mtime_t i_dts = -1;
    mtime_t i_pts = -1;
1367
    mtime_t i_length = 0;
1368
1369
1370
1371
    int i_max;

    /* remove the pes from pid */
    pid->es->p_pes = NULL;
Laurent Aimar's avatar