Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpegts.c @ 8537591a

History | View | Annotate | Download (45.2 KB)

1 fe9cf0d4 Fabrice Bellard
/*
2 7fbde343 Aurelien Jacobs
 * MPEG2 transport stream (aka DVB) demuxer
3 406792e7 Diego Biurrun
 * Copyright (c) 2002-2003 Fabrice Bellard
4 fe9cf0d4 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 fe9cf0d4 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 fe9cf0d4 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 fe9cf0d4 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 fe9cf0d4 Fabrice Bellard
 */
21 245976da Diego Biurrun
22
#include "libavutil/crc.h"
23 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
24 fe9cf0d4 Fabrice Bellard
#include "avformat.h"
25 5dbafeb7 Fabrice Bellard
#include "mpegts.h"
26 ecfb51d1 Diego Biurrun
#include "internal.h"
27 fe9cf0d4 Fabrice Bellard
28 5dbafeb7 Fabrice Bellard
//#define DEBUG_SI
29 27f388aa Fabrice Bellard
//#define DEBUG_SEEK
30 5dbafeb7 Fabrice Bellard
31
/* 1.0 second at 24Mbit/s */
32 fc48fe84 Fabrice Bellard
#define MAX_SCAN_PACKETS 32000
33
34
/* maximum size in which we look for synchronisation if
35
   synchronisation is lost */
36
#define MAX_RESYNC_SIZE 4096
37 a45b40c4 Nico Sabbi
#define REGISTRATION_DESCRIPTOR 5
38 5dbafeb7 Fabrice Bellard
39 c6ec28b1 Michael Niedermayer
typedef struct PESContext PESContext;
40
41 dbf6b678 Michael Niedermayer
static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type);
42 c6ec28b1 Michael Niedermayer
static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code);
43 5dbafeb7 Fabrice Bellard
44
enum MpegTSFilterType {
45
    MPEGTS_PES,
46
    MPEGTS_SECTION,
47 fe9cf0d4 Fabrice Bellard
};
48
49 b474d1f3 Måns Rullgård
typedef struct MpegTSFilter MpegTSFilter;
50
51 4c6b49bf Ivan Schreter
typedef void PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
52 fe9cf0d4 Fabrice Bellard
53 5dbafeb7 Fabrice Bellard
typedef struct MpegTSPESFilter {
54
    PESCallback *pes_cb;
55
    void *opaque;
56
} MpegTSPESFilter;
57
58 b474d1f3 Måns Rullgård
typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
59 5dbafeb7 Fabrice Bellard
60
typedef void SetServiceCallback(void *opaque, int ret);
61
62
typedef struct MpegTSSectionFilter {
63
    int section_index;
64
    int section_h_size;
65
    uint8_t *section_buf;
66 72e043dd Erik Hovland
    unsigned int check_crc:1;
67
    unsigned int end_of_section_reached:1;
68 5dbafeb7 Fabrice Bellard
    SectionCallback *section_cb;
69
    void *opaque;
70
} MpegTSSectionFilter;
71
72 b474d1f3 Måns Rullgård
struct MpegTSFilter {
73 fe9cf0d4 Fabrice Bellard
    int pid;
74
    int last_cc; /* last cc code (-1 if first packet) */
75 5dbafeb7 Fabrice Bellard
    enum MpegTSFilterType type;
76
    union {
77
        MpegTSPESFilter pes_filter;
78
        MpegTSSectionFilter section_filter;
79
    } u;
80 b474d1f3 Måns Rullgård
};
81 5dbafeb7 Fabrice Bellard
82 172d1171 Nico Sabbi
#define MAX_PIDS_PER_PROGRAM 64
83 8c39a758 Måns Rullgård
struct Program {
84 172d1171 Nico Sabbi
    unsigned int id; //program id/service id
85
    unsigned int nb_pids;
86
    unsigned int pids[MAX_PIDS_PER_PROGRAM];
87 8c39a758 Måns Rullgård
};
88 172d1171 Nico Sabbi
89 b45a7a18 Fabrice Bellard
struct MpegTSContext {
90 5dbafeb7 Fabrice Bellard
    /* user data */
91
    AVFormatContext *stream;
92 e995cfca Daniel Kristjansson
    /** raw packet size, including FEC if present            */
93
    int raw_packet_size;
94 5fd63f3c Michael Niedermayer
95
    int pos47;
96
97 e995cfca Daniel Kristjansson
    /** if true, all pids are analyzed to find streams       */
98
    int auto_guess;
99 5dbafeb7 Fabrice Bellard
100 e995cfca Daniel Kristjansson
    /** compute exact PCR for each transport stream packet   */
101
    int mpeg2ts_compute_pcr;
102 b45a7a18 Fabrice Bellard
103 e995cfca Daniel Kristjansson
    int64_t cur_pcr;    /**< used to estimate the exact PCR  */
104
    int pcr_incr;       /**< used to estimate the exact PCR  */
105 115329f1 Diego Biurrun
106 5dbafeb7 Fabrice Bellard
    /* data needed to handle file based ts */
107 e995cfca Daniel Kristjansson
    /** stop parsing loop                                    */
108
    int stop_parse;
109
    /** packet containing Audio/Video data                   */
110
    AVPacket *pkt;
111 5dbafeb7 Fabrice Bellard
112
    /******************************************/
113
    /* private mpegts data */
114
    /* scan context */
115 172d1171 Nico Sabbi
    /** structure to keep track of Program->pids mapping     */
116
    unsigned int nb_prg;
117 8c39a758 Måns Rullgård
    struct Program *prg;
118 115329f1 Diego Biurrun
119 5dbafeb7 Fabrice Bellard
120 e995cfca Daniel Kristjansson
    /** filters for various streams specified by PMT + for the PAT and PMT */
121 5dbafeb7 Fabrice Bellard
    MpegTSFilter *pids[NB_PID_MAX];
122 b45a7a18 Fabrice Bellard
};
123 fe9cf0d4 Fabrice Bellard
124 ecc31d1d Michael Niedermayer
/* TS stream handling */
125
126
enum MpegTSState {
127
    MPEGTS_HEADER = 0,
128
    MPEGTS_PESHEADER_FILL,
129
    MPEGTS_PAYLOAD,
130
    MPEGTS_SKIP,
131
};
132
133
/* enough for PES header + length */
134
#define PES_START_SIZE 9
135
#define MAX_PES_HEADER_SIZE (9 + 255)
136
137
struct PESContext {
138
    int pid;
139 dbf6b678 Michael Niedermayer
    int pcr_pid; /**< if -1 then all packets containing PCR are considered */
140 ecc31d1d Michael Niedermayer
    int stream_type;
141
    MpegTSContext *ts;
142
    AVFormatContext *stream;
143
    AVStream *st;
144
    enum MpegTSState state;
145
    /* used to get the format */
146
    int data_index;
147
    int total_size;
148
    int pes_header_size;
149
    int64_t pts, dts;
150 4c6b49bf Ivan Schreter
    int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
151 ecc31d1d Michael Niedermayer
    uint8_t header[MAX_PES_HEADER_SIZE];
152
};
153
154 6369638b Aurelien Jacobs
extern AVInputFormat mpegts_demuxer;
155
156 172d1171 Nico Sabbi
static void clear_program(MpegTSContext *ts, unsigned int programid)
157
{
158
    int i;
159
160
    for(i=0; i<ts->nb_prg; i++)
161
        if(ts->prg[i].id == programid)
162
            ts->prg[i].nb_pids = 0;
163
}
164
165
static void clear_programs(MpegTSContext *ts)
166
{
167
    av_freep(&ts->prg);
168
    ts->nb_prg=0;
169
}
170
171
static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
172
{
173 8c39a758 Måns Rullgård
    struct Program *p;
174
    void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
175 172d1171 Nico Sabbi
    if(!tmp)
176
        return;
177
    ts->prg = tmp;
178
    p = &ts->prg[ts->nb_prg];
179
    p->id = programid;
180
    p->nb_pids = 0;
181
    ts->nb_prg++;
182
}
183
184
static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
185
{
186
    int i;
187 8c39a758 Måns Rullgård
    struct Program *p = NULL;
188 172d1171 Nico Sabbi
    for(i=0; i<ts->nb_prg; i++) {
189
        if(ts->prg[i].id == programid) {
190
            p = &ts->prg[i];
191
            break;
192
        }
193
    }
194
    if(!p)
195
        return;
196
197
    if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
198
        return;
199
    p->pids[p->nb_pids++] = pid;
200
}
201
202
/**
203
 * \brief discard_pid() decides if the pid is to be discarded according
204
 *                      to caller's programs selection
205
 * \param ts    : - TS context
206
 * \param pid   : - pid
207
 * \return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
208
 *         0 otherwise
209
 */
210
static int discard_pid(MpegTSContext *ts, unsigned int pid)
211
{
212
    int i, j, k;
213
    int used = 0, discarded = 0;
214 8c39a758 Måns Rullgård
    struct Program *p;
215 172d1171 Nico Sabbi
    for(i=0; i<ts->nb_prg; i++) {
216
        p = &ts->prg[i];
217
        for(j=0; j<p->nb_pids; j++) {
218
            if(p->pids[j] != pid)
219
                continue;
220
            //is program with id p->id set to be discarded?
221
            for(k=0; k<ts->stream->nb_programs; k++) {
222
                if(ts->stream->programs[k]->id == p->id) {
223
                    if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
224
                        discarded++;
225
                    else
226
                        used++;
227
                }
228
            }
229
        }
230
    }
231
232 ccd425e7 Diego Biurrun
    return !used && discarded;
233 172d1171 Nico Sabbi
}
234
235 e995cfca Daniel Kristjansson
/**
236
 *  Assembles PES packets out of TS packets, and then calls the "section_cb"
237
 *  function when they are complete.
238
 */
239 5dbafeb7 Fabrice Bellard
static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
240
                               const uint8_t *buf, int buf_size, int is_start)
241
{
242
    MpegTSSectionFilter *tss = &tss1->u.section_filter;
243
    int len;
244 115329f1 Diego Biurrun
245 5dbafeb7 Fabrice Bellard
    if (is_start) {
246
        memcpy(tss->section_buf, buf, buf_size);
247
        tss->section_index = buf_size;
248
        tss->section_h_size = -1;
249
        tss->end_of_section_reached = 0;
250
    } else {
251
        if (tss->end_of_section_reached)
252
            return;
253
        len = 4096 - tss->section_index;
254
        if (buf_size < len)
255
            len = buf_size;
256
        memcpy(tss->section_buf + tss->section_index, buf, len);
257
        tss->section_index += len;
258
    }
259
260
    /* compute section length if possible */
261
    if (tss->section_h_size == -1 && tss->section_index >= 3) {
262 80fb8234 Ronald S. Bultje
        len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
263 5dbafeb7 Fabrice Bellard
        if (len > 4096)
264
            return;
265
        tss->section_h_size = len;
266
    }
267
268
    if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
269
        tss->end_of_section_reached = 1;
270 27bb1ed3 Wolfram Gloger
        if (!tss->check_crc ||
271 3abe5fbd Aurelien Jacobs
            av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
272
                   tss->section_buf, tss->section_h_size) == 0)
273 4d9a577e Michael Niedermayer
            tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
274 5dbafeb7 Fabrice Bellard
    }
275
}
276
277 7b49ce2e Stefan Huehner
static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
278 5dbafeb7 Fabrice Bellard
                                         SectionCallback *section_cb, void *opaque,
279
                                         int check_crc)
280
281
{
282
    MpegTSFilter *filter;
283
    MpegTSSectionFilter *sec;
284 115329f1 Diego Biurrun
285 b45a7a18 Fabrice Bellard
#ifdef DEBUG_SI
286 f0ee3ea6 Michel Bardiaux
    av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid);
287 b45a7a18 Fabrice Bellard
#endif
288 5dbafeb7 Fabrice Bellard
    if (pid >= NB_PID_MAX || ts->pids[pid])
289
        return NULL;
290
    filter = av_mallocz(sizeof(MpegTSFilter));
291 115329f1 Diego Biurrun
    if (!filter)
292 5dbafeb7 Fabrice Bellard
        return NULL;
293
    ts->pids[pid] = filter;
294
    filter->type = MPEGTS_SECTION;
295
    filter->pid = pid;
296
    filter->last_cc = -1;
297
    sec = &filter->u.section_filter;
298
    sec->section_cb = section_cb;
299
    sec->opaque = opaque;
300
    sec->section_buf = av_malloc(MAX_SECTION_SIZE);
301
    sec->check_crc = check_crc;
302
    if (!sec->section_buf) {
303
        av_free(filter);
304
        return NULL;
305
    }
306
    return filter;
307
}
308
309 7b49ce2e Stefan Huehner
static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
310 5dbafeb7 Fabrice Bellard
                                     PESCallback *pes_cb,
311
                                     void *opaque)
312
{
313
    MpegTSFilter *filter;
314
    MpegTSPESFilter *pes;
315
316
    if (pid >= NB_PID_MAX || ts->pids[pid])
317
        return NULL;
318
    filter = av_mallocz(sizeof(MpegTSFilter));
319 115329f1 Diego Biurrun
    if (!filter)
320 5dbafeb7 Fabrice Bellard
        return NULL;
321
    ts->pids[pid] = filter;
322
    filter->type = MPEGTS_PES;
323
    filter->pid = pid;
324
    filter->last_cc = -1;
325
    pes = &filter->u.pes_filter;
326
    pes->pes_cb = pes_cb;
327
    pes->opaque = opaque;
328
    return filter;
329
}
330
331 7b49ce2e Stefan Huehner
static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
332 5dbafeb7 Fabrice Bellard
{
333
    int pid;
334
335
    pid = filter->pid;
336
    if (filter->type == MPEGTS_SECTION)
337
        av_freep(&filter->u.section_filter.section_buf);
338 dcd913d9 Jindřich Makovička
    else if (filter->type == MPEGTS_PES) {
339
        /* referenced private data will be freed later in
340
         * av_close_input_stream */
341
        if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
342
            av_freep(&filter->u.pes_filter.opaque);
343
        }
344
    }
345 ec7d0d2e Gildas Bazin
346 5dbafeb7 Fabrice Bellard
    av_free(filter);
347
    ts->pids[pid] = NULL;
348
}
349
350 a0b8f70c Michael Niedermayer
static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
351
    int stat[packet_size];
352
    int i;
353
    int x=0;
354
    int best_score=0;
355
356
    memset(stat, 0, packet_size*sizeof(int));
357
358 c6002ea1 Måns Rullgård
    for(x=i=0; i<size-3; i++){
359
        if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
360 a0b8f70c Michael Niedermayer
            stat[x]++;
361
            if(stat[x] > best_score){
362
                best_score= stat[x];
363
                if(index) *index= x;
364
            }
365
        }
366
367
        x++;
368
        if(x == packet_size) x= 0;
369
    }
370
371
    return best_score;
372
}
373
374 fe9cf0d4 Fabrice Bellard
/* autodetect fec presence. Must have at least 1024 bytes  */
375 5dbafeb7 Fabrice Bellard
static int get_packet_size(const uint8_t *buf, int size)
376 fe9cf0d4 Fabrice Bellard
{
377 5d4d67e0 Nico Sabbi
    int score, fec_score, dvhs_score;
378 fe9cf0d4 Fabrice Bellard
379
    if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
380
        return -1;
381 115329f1 Diego Biurrun
382 a0b8f70c Michael Niedermayer
    score    = analyze(buf, size, TS_PACKET_SIZE, NULL);
383 5d4d67e0 Nico Sabbi
    dvhs_score    = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
384 a0b8f70c Michael Niedermayer
    fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
385 5d4d67e0 Nico Sabbi
//    av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
386 115329f1 Diego Biurrun
387 5d4d67e0 Nico Sabbi
    if     (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
388
    else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
389
    else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
390 a0b8f70c Michael Niedermayer
    else                       return -1;
391 fe9cf0d4 Fabrice Bellard
}
392
393 5dbafeb7 Fabrice Bellard
typedef struct SectionHeader {
394
    uint8_t tid;
395
    uint16_t id;
396
    uint8_t version;
397
    uint8_t sec_num;
398
    uint8_t last_sec_num;
399
} SectionHeader;
400
401
static inline int get8(const uint8_t **pp, const uint8_t *p_end)
402 fe9cf0d4 Fabrice Bellard
{
403 5dbafeb7 Fabrice Bellard
    const uint8_t *p;
404
    int c;
405
406
    p = *pp;
407
    if (p >= p_end)
408
        return -1;
409
    c = *p++;
410
    *pp = p;
411
    return c;
412 fe9cf0d4 Fabrice Bellard
}
413
414 5dbafeb7 Fabrice Bellard
static inline int get16(const uint8_t **pp, const uint8_t *p_end)
415
{
416
    const uint8_t *p;
417
    int c;
418
419
    p = *pp;
420
    if ((p + 1) >= p_end)
421
        return -1;
422 80fb8234 Ronald S. Bultje
    c = AV_RB16(p);
423 5dbafeb7 Fabrice Bellard
    p += 2;
424
    *pp = p;
425
    return c;
426
}
427
428
/* read and allocate a DVB string preceeded by its length */
429
static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
430 fe9cf0d4 Fabrice Bellard
{
431
    int len;
432 5dbafeb7 Fabrice Bellard
    const uint8_t *p;
433
    char *str;
434 fe9cf0d4 Fabrice Bellard
435 5dbafeb7 Fabrice Bellard
    p = *pp;
436
    len = get8(&p, p_end);
437
    if (len < 0)
438
        return NULL;
439
    if ((p + len) > p_end)
440
        return NULL;
441
    str = av_malloc(len + 1);
442
    if (!str)
443
        return NULL;
444
    memcpy(str, p, len);
445
    str[len] = '\0';
446
    p += len;
447
    *pp = p;
448
    return str;
449
}
450
451 115329f1 Diego Biurrun
static int parse_section_header(SectionHeader *h,
452 5dbafeb7 Fabrice Bellard
                                const uint8_t **pp, const uint8_t *p_end)
453
{
454
    int val;
455
456
    val = get8(pp, p_end);
457
    if (val < 0)
458
        return -1;
459
    h->tid = val;
460
    *pp += 2;
461
    val = get16(pp, p_end);
462
    if (val < 0)
463
        return -1;
464
    h->id = val;
465
    val = get8(pp, p_end);
466
    if (val < 0)
467
        return -1;
468
    h->version = (val >> 1) & 0x1f;
469
    val = get8(pp, p_end);
470
    if (val < 0)
471
        return -1;
472
    h->sec_num = val;
473
    val = get8(pp, p_end);
474
    if (val < 0)
475
        return -1;
476
    h->last_sec_num = val;
477 fe9cf0d4 Fabrice Bellard
    return 0;
478 5dbafeb7 Fabrice Bellard
}
479
480
481 4d9a577e Michael Niedermayer
static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
482 5dbafeb7 Fabrice Bellard
{
483 4d9a577e Michael Niedermayer
    MpegTSContext *ts = filter->u.section_filter.opaque;
484 5dbafeb7 Fabrice Bellard
    SectionHeader h1, *h = &h1;
485 c6ec28b1 Michael Niedermayer
    PESContext *pes;
486
    AVStream *st;
487
    const uint8_t *p, *p_end, *desc_list_end, *desc_end;
488
    int program_info_length, pcr_pid, pid, stream_type;
489
    int desc_list_len, desc_len, desc_tag;
490 79396ac6 Måns Rullgård
    int comp_page = 0, anc_page = 0; /* initialize to kill warnings */
491 828bb5f1 Glenn A. Serre
    char language[4] = {0}; /* initialize to kill warnings */
492 a45b40c4 Nico Sabbi
    int has_hdmv_descr = 0;
493 f4bba201 Anuradha Suraparaju
    int has_dirac_descr = 0;
494 115329f1 Diego Biurrun
495 5dbafeb7 Fabrice Bellard
#ifdef DEBUG_SI
496 f0ee3ea6 Michel Bardiaux
    av_log(ts->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len);
497 750f0e1f Panagiotis Issaris
    av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
498 5dbafeb7 Fabrice Bellard
#endif
499
    p_end = section + section_len - 4;
500
    p = section;
501
    if (parse_section_header(h, &p, p_end) < 0)
502
        return;
503
#ifdef DEBUG_SI
504 f0ee3ea6 Michel Bardiaux
    av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x sec_num=%d/%d\n",
505 2ddb79be Daniel Kristjansson
           h->id, h->sec_num, h->last_sec_num);
506 5dbafeb7 Fabrice Bellard
#endif
507 90d13e30 Michael Niedermayer
    if (h->tid != PMT_TID)
508 5dbafeb7 Fabrice Bellard
        return;
509
510 172d1171 Nico Sabbi
    clear_program(ts, h->id);
511 5dbafeb7 Fabrice Bellard
    pcr_pid = get16(&p, p_end) & 0x1fff;
512
    if (pcr_pid < 0)
513
        return;
514 172d1171 Nico Sabbi
    add_pid_to_pmt(ts, h->id, pcr_pid);
515 5dbafeb7 Fabrice Bellard
#ifdef DEBUG_SI
516 f0ee3ea6 Michel Bardiaux
    av_log(ts->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid);
517 5dbafeb7 Fabrice Bellard
#endif
518
    program_info_length = get16(&p, p_end) & 0xfff;
519
    if (program_info_length < 0)
520
        return;
521 a45b40c4 Nico Sabbi
    while(program_info_length >= 2) {
522
        uint8_t tag, len;
523
        tag = get8(&p, p_end);
524
        len = get8(&p, p_end);
525
        if(len > program_info_length - 2)
526
            //something else is broken, exit the program_descriptors_loop
527
            break;
528
        program_info_length -= len + 2;
529
        if(tag == REGISTRATION_DESCRIPTOR && len >= 4) {
530
            uint8_t bytes[4];
531
            bytes[0] = get8(&p, p_end);
532
            bytes[1] = get8(&p, p_end);
533
            bytes[2] = get8(&p, p_end);
534
            bytes[3] = get8(&p, p_end);
535
            len -= 4;
536
            if(bytes[0] == 'H' && bytes[1] == 'D' &&
537
               bytes[2] == 'M' && bytes[3] == 'V')
538
                has_hdmv_descr = 1;
539
        }
540
        p += len;
541
    }
542 5dbafeb7 Fabrice Bellard
    p += program_info_length;
543
    if (p >= p_end)
544
        return;
545
    for(;;) {
546 c6ec28b1 Michael Niedermayer
        language[0] = 0;
547
        st = 0;
548 5dbafeb7 Fabrice Bellard
        stream_type = get8(&p, p_end);
549
        if (stream_type < 0)
550
            break;
551
        pid = get16(&p, p_end) & 0x1fff;
552
        if (pid < 0)
553
            break;
554 c6ec28b1 Michael Niedermayer
        desc_list_len = get16(&p, p_end) & 0xfff;
555
        if (desc_list_len < 0)
556
            break;
557
        desc_list_end = p + desc_list_len;
558
        if (desc_list_end > p_end)
559 5dbafeb7 Fabrice Bellard
            break;
560 c6ec28b1 Michael Niedermayer
        for(;;) {
561
            desc_tag = get8(&p, desc_list_end);
562
            if (desc_tag < 0)
563
                break;
564 2eb1a65d Nico Sabbi
            if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
565
                if((desc_tag == 0x6A) || (desc_tag == 0x7A)) {
566 9d9b432a Måns Rullgård
                    /*assume DVB AC-3 Audio*/
567
                    stream_type = STREAM_TYPE_AUDIO_AC3;
568 2eb1a65d Nico Sabbi
                } else if(desc_tag == 0x7B) {
569
                    /* DVB DTS audio */
570
                    stream_type = STREAM_TYPE_AUDIO_DTS;
571
                }
572 9d9b432a Måns Rullgård
            }
573 c6ec28b1 Michael Niedermayer
            desc_len = get8(&p, desc_list_end);
574
            desc_end = p + desc_len;
575
            if (desc_end > desc_list_end)
576
                break;
577
#ifdef DEBUG_SI
578 f0ee3ea6 Michel Bardiaux
            av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
579 2ddb79be Daniel Kristjansson
                   desc_tag, desc_len);
580 c6ec28b1 Michael Niedermayer
#endif
581
            switch(desc_tag) {
582
            case DVB_SUBT_DESCID:
583
                if (stream_type == STREAM_TYPE_PRIVATE_DATA)
584
                    stream_type = STREAM_TYPE_SUBTITLE_DVB;
585
586
                language[0] = get8(&p, desc_end);
587
                language[1] = get8(&p, desc_end);
588
                language[2] = get8(&p, desc_end);
589
                language[3] = 0;
590
                get8(&p, desc_end);
591
                comp_page = get16(&p, desc_end);
592
                anc_page = get16(&p, desc_end);
593
594
                break;
595
            case 0x0a: /* ISO 639 language descriptor */
596
                language[0] = get8(&p, desc_end);
597
                language[1] = get8(&p, desc_end);
598
                language[2] = get8(&p, desc_end);
599
                language[3] = 0;
600
                break;
601 f4bba201 Anuradha Suraparaju
            case REGISTRATION_DESCRIPTOR: /*MPEG-2 Registration descriptor */
602
                {
603
                    uint8_t bytes[4];
604
                    bytes[0] = get8(&p, desc_end);
605
                    bytes[1] = get8(&p, desc_end);
606
                    bytes[2] = get8(&p, desc_end);
607
                    bytes[3] = get8(&p, desc_end);
608
                    if(bytes[0] == 'd' && bytes[1] == 'r' &&
609
                       bytes[2] == 'a' && bytes[3] == 'c')
610
                        has_dirac_descr = 1;
611
                    break;
612
                }
613 c6ec28b1 Michael Niedermayer
            default:
614
                break;
615
            }
616
            p = desc_end;
617
        }
618
        p = desc_list_end;
619 5dbafeb7 Fabrice Bellard
620
#ifdef DEBUG_SI
621 f0ee3ea6 Michel Bardiaux
        av_log(ts->stream, AV_LOG_DEBUG, "stream_type=%d pid=0x%x\n",
622 2ddb79be Daniel Kristjansson
               stream_type, pid);
623 5dbafeb7 Fabrice Bellard
#endif
624
625
        /* now create ffmpeg stream */
626
        switch(stream_type) {
627 ce34182d Michael Niedermayer
        case STREAM_TYPE_AUDIO_MPEG1:
628
        case STREAM_TYPE_AUDIO_MPEG2:
629
        case STREAM_TYPE_VIDEO_MPEG1:
630
        case STREAM_TYPE_VIDEO_MPEG2:
631 278de475 Måns Rullgård
        case STREAM_TYPE_VIDEO_MPEG4:
632
        case STREAM_TYPE_VIDEO_H264:
633 cf10ee18 Nico Sabbi
        case STREAM_TYPE_VIDEO_VC1:
634 f4bba201 Anuradha Suraparaju
        case STREAM_TYPE_VIDEO_DIRAC:
635 278de475 Måns Rullgård
        case STREAM_TYPE_AUDIO_AAC:
636 ec23a472 Isaac Richards
        case STREAM_TYPE_AUDIO_AC3:
637 23c99253 Michael Niedermayer
        case STREAM_TYPE_AUDIO_DTS:
638 a45b40c4 Nico Sabbi
        case STREAM_TYPE_AUDIO_HDMV_DTS:
639 c6ec28b1 Michael Niedermayer
        case STREAM_TYPE_SUBTITLE_DVB:
640 f4bba201 Anuradha Suraparaju
            if((stream_type == STREAM_TYPE_AUDIO_HDMV_DTS && !has_hdmv_descr)
641
            || (stream_type == STREAM_TYPE_VIDEO_DIRAC    && !has_dirac_descr))
642 a45b40c4 Nico Sabbi
                break;
643 28af284c Jeff Downs
            if(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES){
644 90d13e30 Michael Niedermayer
                pes= ts->pids[pid]->u.pes_filter.opaque;
645
                st= pes->st;
646
            }else{
647 28af284c Jeff Downs
                if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
648 dbf6b678 Michael Niedermayer
                pes = add_pes_stream(ts, pid, pcr_pid, stream_type);
649 28f03d04 Michael Niedermayer
                if (pes)
650
                    st = new_pes_av_stream(pes, 0);
651 90d13e30 Michael Niedermayer
            }
652 172d1171 Nico Sabbi
            add_pid_to_pmt(ts, h->id, pid);
653 aa237f45 Nico Sabbi
            if(st)
654
                av_program_add_stream_index(ts->stream, h->id, st->index);
655 04ec18b8 Michael Niedermayer
            break;
656 5dbafeb7 Fabrice Bellard
        default:
657
            /* we ignore the other streams */
658
            break;
659
        }
660 c6ec28b1 Michael Niedermayer
661
        if (st) {
662
            if (language[0] != 0) {
663 08681191 Aurelien Jacobs
                av_metadata_set(&st->metadata, "language", language);
664 c6ec28b1 Michael Niedermayer
            }
665
666
            if (stream_type == STREAM_TYPE_SUBTITLE_DVB) {
667 01f4895c Michael Niedermayer
                st->codec->sub_id = (anc_page << 16) | comp_page;
668 c6ec28b1 Michael Niedermayer
            }
669
        }
670 5dbafeb7 Fabrice Bellard
    }
671
    /* all parameters are there */
672 90d13e30 Michael Niedermayer
    ts->stop_parse++;
673 4d9a577e Michael Niedermayer
    mpegts_close_filter(ts, filter);
674 5dbafeb7 Fabrice Bellard
}
675
676 4d9a577e Michael Niedermayer
static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
677 5dbafeb7 Fabrice Bellard
{
678 4d9a577e Michael Niedermayer
    MpegTSContext *ts = filter->u.section_filter.opaque;
679 5dbafeb7 Fabrice Bellard
    SectionHeader h1, *h = &h1;
680
    const uint8_t *p, *p_end;
681
    int sid, pmt_pid;
682
683
#ifdef DEBUG_SI
684 f0ee3ea6 Michel Bardiaux
    av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
685 750f0e1f Panagiotis Issaris
    av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
686 5dbafeb7 Fabrice Bellard
#endif
687
    p_end = section + section_len - 4;
688
    p = section;
689
    if (parse_section_header(h, &p, p_end) < 0)
690
        return;
691
    if (h->tid != PAT_TID)
692
        return;
693
694 172d1171 Nico Sabbi
    clear_programs(ts);
695 5dbafeb7 Fabrice Bellard
    for(;;) {
696
        sid = get16(&p, p_end);
697
        if (sid < 0)
698
            break;
699
        pmt_pid = get16(&p, p_end) & 0x1fff;
700
        if (pmt_pid < 0)
701
            break;
702
#ifdef DEBUG_SI
703 f0ee3ea6 Michel Bardiaux
        av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
704 5dbafeb7 Fabrice Bellard
#endif
705
        if (sid == 0x0000) {
706
            /* NIT info */
707
        } else {
708 1d257254 Nico Sabbi
            av_new_program(ts->stream, sid);
709 90d13e30 Michael Niedermayer
            ts->stop_parse--;
710 28f03d04 Michael Niedermayer
            mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
711 172d1171 Nico Sabbi
            add_pat_entry(ts, sid);
712
            add_pid_to_pmt(ts, sid, 0); //add pat pid to program
713
            add_pid_to_pmt(ts, sid, pmt_pid);
714 5dbafeb7 Fabrice Bellard
        }
715
    }
716
    /* not found */
717 90d13e30 Michael Niedermayer
    ts->stop_parse++;
718 5dbafeb7 Fabrice Bellard
719 4d9a577e Michael Niedermayer
    mpegts_close_filter(ts, filter);
720 5dbafeb7 Fabrice Bellard
}
721
722 90d13e30 Michael Niedermayer
static void mpegts_set_service(MpegTSContext *ts)
723 5dbafeb7 Fabrice Bellard
{
724 4d9a577e Michael Niedermayer
    mpegts_open_section_filter(ts, PAT_PID,
725 5dbafeb7 Fabrice Bellard
                                                pat_cb, ts, 1);
726
}
727
728 4d9a577e Michael Niedermayer
static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
729 5dbafeb7 Fabrice Bellard
{
730 4d9a577e Michael Niedermayer
    MpegTSContext *ts = filter->u.section_filter.opaque;
731 5dbafeb7 Fabrice Bellard
    SectionHeader h1, *h = &h1;
732
    const uint8_t *p, *p_end, *desc_list_end, *desc_end;
733
    int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
734
    char *name, *provider_name;
735
736
#ifdef DEBUG_SI
737 f0ee3ea6 Michel Bardiaux
    av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n");
738 750f0e1f Panagiotis Issaris
    av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
739 5dbafeb7 Fabrice Bellard
#endif
740
741
    p_end = section + section_len - 4;
742
    p = section;
743
    if (parse_section_header(h, &p, p_end) < 0)
744
        return;
745
    if (h->tid != SDT_TID)
746
        return;
747
    onid = get16(&p, p_end);
748
    if (onid < 0)
749
        return;
750
    val = get8(&p, p_end);
751
    if (val < 0)
752
        return;
753
    for(;;) {
754
        sid = get16(&p, p_end);
755
        if (sid < 0)
756
            break;
757
        val = get8(&p, p_end);
758
        if (val < 0)
759
            break;
760
        desc_list_len = get16(&p, p_end) & 0xfff;
761
        if (desc_list_len < 0)
762
            break;
763
        desc_list_end = p + desc_list_len;
764
        if (desc_list_end > p_end)
765
            break;
766
        for(;;) {
767
            desc_tag = get8(&p, desc_list_end);
768
            if (desc_tag < 0)
769
                break;
770
            desc_len = get8(&p, desc_list_end);
771
            desc_end = p + desc_len;
772
            if (desc_end > desc_list_end)
773
                break;
774
#ifdef DEBUG_SI
775 f0ee3ea6 Michel Bardiaux
            av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
776 2ddb79be Daniel Kristjansson
                   desc_tag, desc_len);
777 5dbafeb7 Fabrice Bellard
#endif
778
            switch(desc_tag) {
779
            case 0x48:
780
                service_type = get8(&p, p_end);
781
                if (service_type < 0)
782
                    break;
783
                provider_name = getstr8(&p, p_end);
784
                if (!provider_name)
785
                    break;
786
                name = getstr8(&p, p_end);
787 1d257254 Nico Sabbi
                if (name) {
788
                    AVProgram *program = av_new_program(ts->stream, sid);
789 08681191 Aurelien Jacobs
                    if(program) {
790
                        av_metadata_set(&program->metadata, "name", name);
791
                        av_metadata_set(&program->metadata, "provider_name", provider_name);
792
                    }
793 1d257254 Nico Sabbi
                }
794 81d5ae6d Michael Niedermayer
                av_free(name);
795
                av_free(provider_name);
796 5dbafeb7 Fabrice Bellard
                break;
797
            default:
798
                break;
799
            }
800
            p = desc_end;
801
        }
802
        p = desc_list_end;
803
    }
804
}
805
806 fc48fe84 Fabrice Bellard
/* scan services in a transport stream by looking at the SDT */
807 7b49ce2e Stefan Huehner
static void mpegts_scan_sdt(MpegTSContext *ts)
808 5dbafeb7 Fabrice Bellard
{
809 37dfd633 Michael Niedermayer
    mpegts_open_section_filter(ts, SDT_PID,
810 5dbafeb7 Fabrice Bellard
                                                sdt_cb, ts, 1);
811
}
812
813
static int64_t get_pts(const uint8_t *p)
814
{
815 80fb8234 Ronald S. Bultje
    int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
816
    pts |= (AV_RB16(p + 1) >> 1) << 15;
817
    pts |=  AV_RB16(p + 3) >> 1;
818 5dbafeb7 Fabrice Bellard
    return pts;
819 fe9cf0d4 Fabrice Bellard
}
820
821
/* return non zero if a packet could be constructed */
822 4d9a577e Michael Niedermayer
static void mpegts_push_data(MpegTSFilter *filter,
823 4c6b49bf Ivan Schreter
                             const uint8_t *buf, int buf_size, int is_start,
824
                             int64_t pos)
825 fe9cf0d4 Fabrice Bellard
{
826 4d9a577e Michael Niedermayer
    PESContext *pes = filter->u.pes_filter.opaque;
827 b45a7a18 Fabrice Bellard
    MpegTSContext *ts = pes->ts;
828 5dbafeb7 Fabrice Bellard
    const uint8_t *p;
829 c6ec28b1 Michael Niedermayer
    int len, code;
830 115329f1 Diego Biurrun
831 b2292194 Michael Niedermayer
    if(!ts->pkt)
832
        return;
833
834 fe9cf0d4 Fabrice Bellard
    if (is_start) {
835 5dbafeb7 Fabrice Bellard
        pes->state = MPEGTS_HEADER;
836
        pes->data_index = 0;
837 4c6b49bf Ivan Schreter
        pes->ts_packet_pos = pos;
838 fe9cf0d4 Fabrice Bellard
    }
839
    p = buf;
840
    while (buf_size > 0) {
841 5dbafeb7 Fabrice Bellard
        switch(pes->state) {
842 fe9cf0d4 Fabrice Bellard
        case MPEGTS_HEADER:
843 5dbafeb7 Fabrice Bellard
            len = PES_START_SIZE - pes->data_index;
844
            if (len > buf_size)
845
                len = buf_size;
846
            memcpy(pes->header + pes->data_index, p, len);
847
            pes->data_index += len;
848 fe9cf0d4 Fabrice Bellard
            p += len;
849
            buf_size -= len;
850 5dbafeb7 Fabrice Bellard
            if (pes->data_index == PES_START_SIZE) {
851 fe9cf0d4 Fabrice Bellard
                /* we got all the PES or section header. We can now
852
                   decide */
853
#if 0
854 750f0e1f Panagiotis Issaris
                av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
855 fe9cf0d4 Fabrice Bellard
#endif
856 5dbafeb7 Fabrice Bellard
                if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
857
                    pes->header[2] == 0x01) {
858 fe9cf0d4 Fabrice Bellard
                    /* it must be an mpeg2 PES stream */
859 5dbafeb7 Fabrice Bellard
                    code = pes->header[3] | 0x100;
860 fe9cf0d4 Fabrice Bellard
                    if (!((code >= 0x1c0 && code <= 0x1df) ||
861 ec23a472 Isaac Richards
                          (code >= 0x1e0 && code <= 0x1ef) ||
862 cf10ee18 Nico Sabbi
                          (code == 0x1bd) || (code == 0x1fd)))
863 fe9cf0d4 Fabrice Bellard
                        goto skip;
864 5dbafeb7 Fabrice Bellard
                    if (!pes->st) {
865 fe9cf0d4 Fabrice Bellard
                        /* allocate stream */
866 c6ec28b1 Michael Niedermayer
                        new_pes_av_stream(pes, code);
867 fe9cf0d4 Fabrice Bellard
                    }
868 5dbafeb7 Fabrice Bellard
                    pes->state = MPEGTS_PESHEADER_FILL;
869 80fb8234 Ronald S. Bultje
                    pes->total_size = AV_RB16(pes->header + 4);
870 5dbafeb7 Fabrice Bellard
                    /* NOTE: a zero total size means the PES size is
871
                       unbounded */
872
                    if (pes->total_size)
873
                        pes->total_size += 6;
874
                    pes->pes_header_size = pes->header[8] + 9;
875 fe9cf0d4 Fabrice Bellard
                } else {
876
                    /* otherwise, it should be a table */
877
                    /* skip packet */
878
                skip:
879 5dbafeb7 Fabrice Bellard
                    pes->state = MPEGTS_SKIP;
880 fe9cf0d4 Fabrice Bellard
                    continue;
881
                }
882
            }
883
            break;
884
            /**********************************************/
885
            /* PES packing parsing */
886
        case MPEGTS_PESHEADER_FILL:
887 5dbafeb7 Fabrice Bellard
            len = pes->pes_header_size - pes->data_index;
888
            if (len > buf_size)
889
                len = buf_size;
890
            memcpy(pes->header + pes->data_index, p, len);
891
            pes->data_index += len;
892 fe9cf0d4 Fabrice Bellard
            p += len;
893
            buf_size -= len;
894 5dbafeb7 Fabrice Bellard
            if (pes->data_index == pes->pes_header_size) {
895
                const uint8_t *r;
896
                unsigned int flags;
897
898
                flags = pes->header[7];
899
                r = pes->header + 9;
900
                pes->pts = AV_NOPTS_VALUE;
901
                pes->dts = AV_NOPTS_VALUE;
902
                if ((flags & 0xc0) == 0x80) {
903 14120c95 Baptiste Coudurier
                    pes->dts = pes->pts = get_pts(r);
904 5dbafeb7 Fabrice Bellard
                    r += 5;
905
                } else if ((flags & 0xc0) == 0xc0) {
906
                    pes->pts = get_pts(r);
907
                    r += 5;
908
                    pes->dts = get_pts(r);
909
                    r += 5;
910
                }
911
                /* we got the full header. We parse it and get the payload */
912
                pes->state = MPEGTS_PAYLOAD;
913
            }
914 fe9cf0d4 Fabrice Bellard
            break;
915
        case MPEGTS_PAYLOAD:
916 5dbafeb7 Fabrice Bellard
            if (pes->total_size) {
917
                len = pes->total_size - pes->data_index;
918
                if (len > buf_size)
919
                    len = buf_size;
920
            } else {
921
                len = buf_size;
922
            }
923 fe9cf0d4 Fabrice Bellard
            if (len > 0) {
924 5dbafeb7 Fabrice Bellard
                AVPacket *pkt = ts->pkt;
925
                if (pes->st && av_new_packet(pkt, len) == 0) {
926
                    memcpy(pkt->data, p, len);
927
                    pkt->stream_index = pes->st->index;
928
                    pkt->pts = pes->pts;
929 2092bd75 Gildas Bazin
                    pkt->dts = pes->dts;
930 4c6b49bf Ivan Schreter
                    /* store position of first TS packet of this PES packet */
931
                    pkt->pos = pes->ts_packet_pos;
932 5dbafeb7 Fabrice Bellard
                    /* reset pts values */
933
                    pes->pts = AV_NOPTS_VALUE;
934
                    pes->dts = AV_NOPTS_VALUE;
935
                    ts->stop_parse = 1;
936
                    return;
937 fe9cf0d4 Fabrice Bellard
                }
938
            }
939
            buf_size = 0;
940
            break;
941
        case MPEGTS_SKIP:
942
            buf_size = 0;
943
            break;
944
        }
945
    }
946 5dbafeb7 Fabrice Bellard
}
947
948 c6ec28b1 Michael Niedermayer
static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code)
949
{
950
    AVStream *st;
951 fb65d2ca Diego Pettenò
    enum CodecID codec_id;
952
    enum CodecType codec_type;
953 c6ec28b1 Michael Niedermayer
954
    switch(pes->stream_type){
955
    case STREAM_TYPE_AUDIO_MPEG1:
956
    case STREAM_TYPE_AUDIO_MPEG2:
957
        codec_type = CODEC_TYPE_AUDIO;
958
        codec_id = CODEC_ID_MP3;
959
        break;
960
    case STREAM_TYPE_VIDEO_MPEG1:
961
    case STREAM_TYPE_VIDEO_MPEG2:
962
        codec_type = CODEC_TYPE_VIDEO;
963
        codec_id = CODEC_ID_MPEG2VIDEO;
964
        break;
965
    case STREAM_TYPE_VIDEO_MPEG4:
966
        codec_type = CODEC_TYPE_VIDEO;
967
        codec_id = CODEC_ID_MPEG4;
968
        break;
969
    case STREAM_TYPE_VIDEO_H264:
970
        codec_type = CODEC_TYPE_VIDEO;
971
        codec_id = CODEC_ID_H264;
972
        break;
973 cf10ee18 Nico Sabbi
    case STREAM_TYPE_VIDEO_VC1:
974
        codec_type = CODEC_TYPE_VIDEO;
975
        codec_id = CODEC_ID_VC1;
976
        break;
977 f4bba201 Anuradha Suraparaju
    case STREAM_TYPE_VIDEO_DIRAC:
978
        codec_type = CODEC_TYPE_VIDEO;
979
        codec_id = CODEC_ID_DIRAC;
980
        break;
981 c6ec28b1 Michael Niedermayer
    case STREAM_TYPE_AUDIO_AAC:
982
        codec_type = CODEC_TYPE_AUDIO;
983
        codec_id = CODEC_ID_AAC;
984
        break;
985
    case STREAM_TYPE_AUDIO_AC3:
986
        codec_type = CODEC_TYPE_AUDIO;
987
        codec_id = CODEC_ID_AC3;
988
        break;
989
    case STREAM_TYPE_AUDIO_DTS:
990 a45b40c4 Nico Sabbi
    case STREAM_TYPE_AUDIO_HDMV_DTS:
991 c6ec28b1 Michael Niedermayer
        codec_type = CODEC_TYPE_AUDIO;
992
        codec_id = CODEC_ID_DTS;
993
        break;
994
    case STREAM_TYPE_SUBTITLE_DVB:
995
        codec_type = CODEC_TYPE_SUBTITLE;
996
        codec_id = CODEC_ID_DVB_SUBTITLE;
997
        break;
998
    default:
999
        if (code >= 0x1c0 && code <= 0x1df) {
1000
            codec_type = CODEC_TYPE_AUDIO;
1001
            codec_id = CODEC_ID_MP2;
1002
        } else if (code == 0x1bd) {
1003
            codec_type = CODEC_TYPE_AUDIO;
1004
            codec_id = CODEC_ID_AC3;
1005
        } else {
1006
            codec_type = CODEC_TYPE_VIDEO;
1007 452a398f Michael Niedermayer
            codec_id = CODEC_ID_PROBE;
1008 c6ec28b1 Michael Niedermayer
        }
1009
        break;
1010
    }
1011
    st = av_new_stream(pes->stream, pes->pid);
1012
    if (st) {
1013
        av_set_pts_info(st, 33, 1, 90000);
1014
        st->priv_data = pes;
1015 01f4895c Michael Niedermayer
        st->codec->codec_type = codec_type;
1016
        st->codec->codec_id = codec_id;
1017 57004ff1 Aurelien Jacobs
        st->need_parsing = AVSTREAM_PARSE_FULL;
1018 c6ec28b1 Michael Niedermayer
        pes->st = st;
1019
    }
1020
    return st;
1021
}
1022
1023
1024 dbf6b678 Michael Niedermayer
static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type)
1025 5dbafeb7 Fabrice Bellard
{
1026
    MpegTSFilter *tss;
1027
    PESContext *pes;
1028
1029
    /* if no pid found, then add a pid context */
1030
    pes = av_mallocz(sizeof(PESContext));
1031
    if (!pes)
1032 c6ec28b1 Michael Niedermayer
        return 0;
1033 b45a7a18 Fabrice Bellard
    pes->ts = ts;
1034
    pes->stream = ts->stream;
1035 5dbafeb7 Fabrice Bellard
    pes->pid = pid;
1036 dbf6b678 Michael Niedermayer
    pes->pcr_pid = pcr_pid;
1037 278de475 Måns Rullgård
    pes->stream_type = stream_type;
1038 5dbafeb7 Fabrice Bellard
    tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
1039
    if (!tss) {
1040
        av_free(pes);
1041 c6ec28b1 Michael Niedermayer
        return 0;
1042 5dbafeb7 Fabrice Bellard
    }
1043 c6ec28b1 Michael Niedermayer
    return pes;
1044 fe9cf0d4 Fabrice Bellard
}
1045
1046 5dbafeb7 Fabrice Bellard
/* handle one TS packet */
1047 b45a7a18 Fabrice Bellard
static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
1048 fe9cf0d4 Fabrice Bellard
{
1049 b45a7a18 Fabrice Bellard
    AVFormatContext *s = ts->stream;
1050 5dbafeb7 Fabrice Bellard
    MpegTSFilter *tss;
1051
    int len, pid, cc, cc_ok, afc, is_start;
1052
    const uint8_t *p, *p_end;
1053 4c6b49bf Ivan Schreter
    int64_t pos;
1054 5dbafeb7 Fabrice Bellard
1055 80fb8234 Ronald S. Bultje
    pid = AV_RB16(packet + 1) & 0x1fff;
1056 172d1171 Nico Sabbi
    if(pid && discard_pid(ts, pid))
1057
        return;
1058 5dbafeb7 Fabrice Bellard
    is_start = packet[1] & 0x40;
1059
    tss = ts->pids[pid];
1060
    if (ts->auto_guess && tss == NULL && is_start) {
1061 dbf6b678 Michael Niedermayer
        add_pes_stream(ts, pid, -1, 0);
1062 5dbafeb7 Fabrice Bellard
        tss = ts->pids[pid];
1063
    }
1064
    if (!tss)
1065
        return;
1066
1067
    /* continuity check (currently not used) */
1068
    cc = (packet[3] & 0xf);
1069
    cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
1070
    tss->last_cc = cc;
1071 115329f1 Diego Biurrun
1072 5dbafeb7 Fabrice Bellard
    /* skip adaptation field */
1073
    afc = (packet[3] >> 4) & 3;
1074
    p = packet + 4;
1075
    if (afc == 0) /* reserved value */
1076
        return;
1077
    if (afc == 2) /* adaptation field only */
1078
        return;
1079
    if (afc == 3) {
1080
        /* skip adapation field */
1081
        p += p[0] + 1;
1082
    }
1083
    /* if past the end of packet, ignore */
1084
    p_end = packet + TS_PACKET_SIZE;
1085
    if (p >= p_end)
1086
        return;
1087 115329f1 Diego Biurrun
1088 4c6b49bf Ivan Schreter
    pos = url_ftell(ts->stream->pb);
1089
    ts->pos47= pos % ts->raw_packet_size;
1090 5fd63f3c Michael Niedermayer
1091 5dbafeb7 Fabrice Bellard
    if (tss->type == MPEGTS_SECTION) {
1092
        if (is_start) {
1093
            /* pointer field present */
1094
            len = *p++;
1095
            if (p + len > p_end)
1096
                return;
1097
            if (len && cc_ok) {
1098 27bb1ed3 Wolfram Gloger
                /* write remaining section bytes */
1099 115329f1 Diego Biurrun
                write_section_data(s, tss,
1100 5dbafeb7 Fabrice Bellard
                                   p, len, 0);
1101 27bb1ed3 Wolfram Gloger
                /* check whether filter has been closed */
1102
                if (!ts->pids[pid])
1103
                    return;
1104 5dbafeb7 Fabrice Bellard
            }
1105
            p += len;
1106
            if (p < p_end) {
1107 115329f1 Diego Biurrun
                write_section_data(s, tss,
1108 5dbafeb7 Fabrice Bellard
                                   p, p_end - p, 1);
1109
            }
1110
        } else {
1111
            if (cc_ok) {
1112 115329f1 Diego Biurrun
                write_section_data(s, tss,
1113 5dbafeb7 Fabrice Bellard
                                   p, p_end - p, 0);
1114 fc48fe84 Fabrice Bellard
            }
1115 5dbafeb7 Fabrice Bellard
        }
1116
    } else {
1117 4c6b49bf Ivan Schreter
        // Note: The position here points actually behind the current packet.
1118 4d9a577e Michael Niedermayer
        tss->u.pes_filter.pes_cb(tss,
1119 4c6b49bf Ivan Schreter
                                 p, p_end - p, is_start, pos - ts->raw_packet_size);
1120 5dbafeb7 Fabrice Bellard
    }
1121
}
1122
1123 fc48fe84 Fabrice Bellard
/* XXX: try to find a better synchro over several packets (use
1124
   get_packet_size() ?) */
1125 b45a7a18 Fabrice Bellard
static int mpegts_resync(ByteIOContext *pb)
1126 fc48fe84 Fabrice Bellard
{
1127
    int c, i;
1128
1129
    for(i = 0;i < MAX_RESYNC_SIZE; i++) {
1130
        c = url_fgetc(pb);
1131
        if (c < 0)
1132
            return -1;
1133
        if (c == 0x47) {
1134
            url_fseek(pb, -1, SEEK_CUR);
1135
            return 0;
1136
        }
1137
    }
1138
    /* no sync found */
1139
    return -1;
1140
}
1141
1142 b45a7a18 Fabrice Bellard
/* return -1 if error or EOF. Return 0 if OK. */
1143
static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
1144 5dbafeb7 Fabrice Bellard
{
1145 b45a7a18 Fabrice Bellard
    int skip, len;
1146
1147
    for(;;) {
1148
        len = get_buffer(pb, buf, TS_PACKET_SIZE);
1149
        if (len != TS_PACKET_SIZE)
1150 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
1151 b45a7a18 Fabrice Bellard
        /* check paquet sync byte */
1152
        if (buf[0] != 0x47) {
1153
            /* find a new packet start */
1154
            url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
1155
            if (mpegts_resync(pb) < 0)
1156
                return AVERROR_INVALIDDATA;
1157
            else
1158
                continue;
1159
        } else {
1160
            skip = raw_packet_size - TS_PACKET_SIZE;
1161
            if (skip > 0)
1162
                url_fskip(pb, skip);
1163
            break;
1164
        }
1165
    }
1166
    return 0;
1167
}
1168
1169
static int handle_packets(MpegTSContext *ts, int nb_packets)
1170
{
1171
    AVFormatContext *s = ts->stream;
1172 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
1173 b45a7a18 Fabrice Bellard
    uint8_t packet[TS_PACKET_SIZE];
1174
    int packet_num, ret;
1175 5dbafeb7 Fabrice Bellard
1176
    ts->stop_parse = 0;
1177
    packet_num = 0;
1178 fe9cf0d4 Fabrice Bellard
    for(;;) {
1179 90d13e30 Michael Niedermayer
        if (ts->stop_parse>0)
1180 5dbafeb7 Fabrice Bellard
            break;
1181
        packet_num++;
1182
        if (nb_packets != 0 && packet_num >= nb_packets)
1183
            break;
1184 b45a7a18 Fabrice Bellard
        ret = read_packet(pb, packet, ts->raw_packet_size);
1185
        if (ret != 0)
1186
            return ret;
1187
        handle_packet(ts, packet);
1188 5dbafeb7 Fabrice Bellard
    }
1189
    return 0;
1190
}
1191 fe9cf0d4 Fabrice Bellard
1192 5dbafeb7 Fabrice Bellard
static int mpegts_probe(AVProbeData *p)
1193
{
1194 fc48fe84 Fabrice Bellard
#if 1
1195 a0b8f70c Michael Niedermayer
    const int size= p->buf_size;
1196 5d4d67e0 Nico Sabbi
    int score, fec_score, dvhs_score;
1197 1558d0b9 Michael Niedermayer
    int check_count= size / TS_FEC_PACKET_SIZE;
1198 a0b8f70c Michael Niedermayer
#define CHECK_COUNT 10
1199 115329f1 Diego Biurrun
1200 1558d0b9 Michael Niedermayer
    if (check_count < CHECK_COUNT)
1201 a0b8f70c Michael Niedermayer
        return -1;
1202 115329f1 Diego Biurrun
1203 1558d0b9 Michael Niedermayer
    score     = analyze(p->buf, TS_PACKET_SIZE     *check_count, TS_PACKET_SIZE     , NULL)*CHECK_COUNT/check_count;
1204
    dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
1205
    fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
1206 5d4d67e0 Nico Sabbi
//    av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
1207 115329f1 Diego Biurrun
1208 a0b8f70c Michael Niedermayer
// we need a clear definition for the returned score otherwise things will become messy sooner or later
1209 5d4d67e0 Nico Sabbi
    if     (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score     - CHECK_COUNT;
1210
    else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score  - CHECK_COUNT;
1211 a0b8f70c Michael Niedermayer
    else if(                 fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
1212
    else                                    return -1;
1213 fc48fe84 Fabrice Bellard
#else
1214
    /* only use the extension for safer guess */
1215
    if (match_ext(p->filename, "ts"))
1216
        return AVPROBE_SCORE_MAX;
1217
    else
1218
        return 0;
1219
#endif
1220 5dbafeb7 Fabrice Bellard
}
1221
1222 910f02a0 Diego Biurrun
/* return the 90kHz PCR and the extension for the 27MHz PCR. return
1223 b45a7a18 Fabrice Bellard
   (-1) if not available */
1224 115329f1 Diego Biurrun
static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
1225 b45a7a18 Fabrice Bellard
                     const uint8_t *packet)
1226
{
1227
    int afc, len, flags;
1228
    const uint8_t *p;
1229
    unsigned int v;
1230
1231
    afc = (packet[3] >> 4) & 3;
1232
    if (afc <= 1)
1233
        return -1;
1234
    p = packet + 4;
1235
    len = p[0];
1236
    p++;
1237
    if (len == 0)
1238
        return -1;
1239
    flags = *p++;
1240
    len--;
1241
    if (!(flags & 0x10))
1242
        return -1;
1243
    if (len < 6)
1244
        return -1;
1245 80fb8234 Ronald S. Bultje
    v = AV_RB32(p);
1246 b45a7a18 Fabrice Bellard
    *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1247
    *ppcr_low = ((p[4] & 1) << 8) | p[5];
1248
    return 0;
1249
}
1250
1251 5dbafeb7 Fabrice Bellard
static int mpegts_read_header(AVFormatContext *s,
1252
                              AVFormatParameters *ap)
1253
{
1254
    MpegTSContext *ts = s->priv_data;
1255 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
1256 1558d0b9 Michael Niedermayer
    uint8_t buf[5*1024];
1257 4dc8a963 Michael Niedermayer
    int len;
1258 5dbafeb7 Fabrice Bellard
    int64_t pos;
1259 e05655fb Michael Niedermayer
1260 b45a7a18 Fabrice Bellard
    if (ap) {
1261
        ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
1262 38c48be2 Michael Niedermayer
        if(ap->mpeg2ts_raw){
1263
            av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
1264
            return -1;
1265
        }
1266 b45a7a18 Fabrice Bellard
    }
1267
1268 5dbafeb7 Fabrice Bellard
    /* read the first 1024 bytes to get packet size */
1269
    pos = url_ftell(pb);
1270
    len = get_buffer(pb, buf, sizeof(buf));
1271
    if (len != sizeof(buf))
1272
        goto fail;
1273
    ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1274
    if (ts->raw_packet_size <= 0)
1275
        goto fail;
1276 b45a7a18 Fabrice Bellard
    ts->stream = s;
1277 5dbafeb7 Fabrice Bellard
    ts->auto_guess = 0;
1278
1279 38c48be2 Michael Niedermayer
    if (s->iformat == &mpegts_demuxer) {
1280 b45a7a18 Fabrice Bellard
        /* normal demux */
1281 fc48fe84 Fabrice Bellard
1282 a45ec414 Benoit Fouet
        /* first do a scaning to get all the services */
1283
        url_fseek(pb, pos, SEEK_SET);
1284
        mpegts_scan_sdt(ts);
1285 115329f1 Diego Biurrun
1286 28f03d04 Michael Niedermayer
        mpegts_set_service(ts);
1287 115329f1 Diego Biurrun
1288 28f03d04 Michael Niedermayer
        handle_packets(ts, s->probesize);
1289 a45ec414 Benoit Fouet
        /* if could not find service, enable auto_guess */
1290 115329f1 Diego Biurrun
1291 a45ec414 Benoit Fouet
        ts->auto_guess = 1;
1292 115329f1 Diego Biurrun
1293 b45a7a18 Fabrice Bellard
#ifdef DEBUG_SI
1294 a45ec414 Benoit Fouet
        av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");
1295 b45a7a18 Fabrice Bellard
#endif
1296
        s->ctx_flags |= AVFMTCTX_NOHEADER;
1297
    } else {
1298
        AVStream *st;
1299
        int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
1300
        int64_t pcrs[2], pcr_h;
1301
        int packet_count[2];
1302
        uint8_t packet[TS_PACKET_SIZE];
1303 115329f1 Diego Biurrun
1304 b45a7a18 Fabrice Bellard
        /* only read packets */
1305 115329f1 Diego Biurrun
1306 b45a7a18 Fabrice Bellard
        st = av_new_stream(s, 0);
1307
        if (!st)
1308
            goto fail;
1309 9ee91c2f Michael Niedermayer
        av_set_pts_info(st, 60, 1, 27000000);
1310 01f4895c Michael Niedermayer
        st->codec->codec_type = CODEC_TYPE_DATA;
1311
        st->codec->codec_id = CODEC_ID_MPEG2TS;
1312 115329f1 Diego Biurrun
1313 b45a7a18 Fabrice Bellard
        /* we iterate until we find two PCRs to estimate the bitrate */
1314
        pcr_pid = -1;
1315
        nb_pcrs = 0;
1316
        nb_packets = 0;
1317
        for(;;) {
1318 899681cd Björn Axelsson
            ret = read_packet(s->pb, packet, ts->raw_packet_size);
1319 b45a7a18 Fabrice Bellard
            if (ret < 0)
1320
                return -1;
1321 80fb8234 Ronald S. Bultje
            pid = AV_RB16(packet + 1) & 0x1fff;
1322 b45a7a18 Fabrice Bellard
            if ((pcr_pid == -1 || pcr_pid == pid) &&
1323
                parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
1324
                pcr_pid = pid;
1325
                packet_count[nb_pcrs] = nb_packets;
1326
                pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1327
                nb_pcrs++;
1328
                if (nb_pcrs >= 2)
1329
                    break;
1330
            }
1331
            nb_packets++;
1332
        }
1333 5dbafeb7 Fabrice Bellard
1334 b45a7a18 Fabrice Bellard
        /* NOTE1: the bitrate is computed without the FEC */
1335
        /* NOTE2: it is only the bitrate of the start of the stream */
1336
        ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
1337
        ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
1338
        s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
1339 01f4895c Michael Niedermayer
        st->codec->bit_rate = s->bit_rate;
1340 c0df9d75 Michael Niedermayer
        st->start_time = ts->cur_pcr;
1341 b45a7a18 Fabrice Bellard
#if 0
1342 f0ee3ea6 Michel Bardiaux
        av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
1343 b45a7a18 Fabrice Bellard
               st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
1344 5dbafeb7 Fabrice Bellard
#endif
1345 fe9cf0d4 Fabrice Bellard
    }
1346 5dbafeb7 Fabrice Bellard
1347
    url_fseek(pb, pos, SEEK_SET);
1348 fe9cf0d4 Fabrice Bellard
    return 0;
1349 5dbafeb7 Fabrice Bellard
 fail:
1350
    return -1;
1351
}
1352
1353 b45a7a18 Fabrice Bellard
#define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1354
1355
static int mpegts_raw_read_packet(AVFormatContext *s,
1356
                                  AVPacket *pkt)
1357
{
1358
    MpegTSContext *ts = s->priv_data;
1359
    int ret, i;
1360
    int64_t pcr_h, next_pcr_h, pos;
1361
    int pcr_l, next_pcr_l;
1362
    uint8_t pcr_buf[12];
1363
1364
    if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
1365 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
1366 899681cd Björn Axelsson
    pkt->pos= url_ftell(s->pb);
1367
    ret = read_packet(s->pb, pkt->data, ts->raw_packet_size);
1368 b45a7a18 Fabrice Bellard
    if (ret < 0) {
1369
        av_free_packet(pkt);
1370
        return ret;
1371
    }
1372
    if (ts->mpeg2ts_compute_pcr) {
1373
        /* compute exact PCR for each packet */
1374
        if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
1375
            /* we read the next PCR (XXX: optimize it by using a bigger buffer */
1376 899681cd Björn Axelsson
            pos = url_ftell(s->pb);
1377 b45a7a18 Fabrice Bellard
            for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
1378 899681cd Björn Axelsson
                url_fseek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
1379
                get_buffer(s->pb, pcr_buf, 12);
1380 b45a7a18 Fabrice Bellard
                if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
1381
                    /* XXX: not precise enough */
1382 115329f1 Diego Biurrun
                    ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
1383 b45a7a18 Fabrice Bellard
                        (i + 1);
1384
                    break;
1385
                }
1386
            }
1387 899681cd Björn Axelsson
            url_fseek(s->pb, pos, SEEK_SET);
1388 b45a7a18 Fabrice Bellard
            /* no next PCR found: we use previous increment */
1389
            ts->cur_pcr = pcr_h * 300 + pcr_l;
1390
        }
1391
        pkt->pts = ts->cur_pcr;
1392
        pkt->duration = ts->pcr_incr;
1393
        ts->cur_pcr += ts->pcr_incr;
1394
    }
1395
    pkt->stream_index = 0;
1396
    return 0;
1397
}
1398
1399 5dbafeb7 Fabrice Bellard
static int mpegts_read_packet(AVFormatContext *s,
1400
                              AVPacket *pkt)
1401
{
1402
    MpegTSContext *ts = s->priv_data;
1403 b45a7a18 Fabrice Bellard
1404 38c48be2 Michael Niedermayer
    ts->pkt = pkt;
1405
    return handle_packets(ts, 0);
1406 fe9cf0d4 Fabrice Bellard
}
1407
1408
static int mpegts_read_close(AVFormatContext *s)
1409
{
1410
    MpegTSContext *ts = s->priv_data;
1411
    int i;
1412 81d5ae6d Michael Niedermayer
1413
    clear_programs(ts);
1414
1415 fe9cf0d4 Fabrice Bellard
    for(i=0;i<NB_PID_MAX;i++)
1416 ec7d0d2e Gildas Bazin
        if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
1417 00a6b92b Måns Rullgård
1418 fe9cf0d4 Fabrice Bellard
    return 0;
1419
}
1420
1421 115329f1 Diego Biurrun
static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
1422 cdd5034f Michael Niedermayer
                              int64_t *ppos, int64_t pos_limit)
1423 27f388aa Fabrice Bellard
{
1424
    MpegTSContext *ts = s->priv_data;
1425
    int64_t pos, timestamp;
1426
    uint8_t buf[TS_PACKET_SIZE];
1427 dbf6b678 Michael Niedermayer
    int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
1428 cdd5034f Michael Niedermayer
    const int find_next= 1;
1429 5fd63f3c Michael Niedermayer
    pos = ((*ppos  + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
1430 27f388aa Fabrice Bellard
    if (find_next) {
1431
        for(;;) {
1432 899681cd Björn Axelsson
            url_fseek(s->pb, pos, SEEK_SET);
1433
            if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1434 27f388aa Fabrice Bellard
                return AV_NOPTS_VALUE;
1435 80fb8234 Ronald S. Bultje
            if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
1436 27f388aa Fabrice Bellard
                parse_pcr(&timestamp, &pcr_l, buf) == 0) {
1437
                break;
1438
            }
1439
            pos += ts->raw_packet_size;
1440
        }
1441
    } else {
1442
        for(;;) {
1443
            pos -= ts->raw_packet_size;
1444
            if (pos < 0)
1445
                return AV_NOPTS_VALUE;
1446 899681cd Björn Axelsson
            url_fseek(s->pb, pos, SEEK_SET);
1447
            if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1448 27f388aa Fabrice Bellard
                return AV_NOPTS_VALUE;
1449 80fb8234 Ronald S. Bultje
            if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
1450 27f388aa Fabrice Bellard
                parse_pcr(&timestamp, &pcr_l, buf) == 0) {
1451
                break;
1452
            }
1453
        }
1454
    }
1455
    *ppos = pos;
1456
1457 cdd5034f Michael Niedermayer
    return timestamp;
1458 27f388aa Fabrice Bellard
}
1459
1460 3ba1438d Michael Niedermayer
static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
1461 e05655fb Michael Niedermayer
    MpegTSContext *ts = s->priv_data;
1462
    uint8_t buf[TS_PACKET_SIZE];
1463
    int64_t pos;
1464
1465 3ba1438d Michael Niedermayer
    if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
1466 e05655fb Michael Niedermayer
        return -1;
1467
1468 899681cd Björn Axelsson
    pos= url_ftell(s->pb);
1469 e05655fb Michael Niedermayer
1470
    for(;;) {
1471 899681cd Björn Axelsson
        url_fseek(s->pb, pos, SEEK_SET);
1472
        if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1473 e05655fb Michael Niedermayer
            return -1;
1474 80fb8234 Ronald S. Bultje
//        pid = AV_RB16(buf + 1) & 0x1fff;
1475 e05655fb Michael Niedermayer
        if(buf[1] & 0x40) break;
1476
        pos += ts->raw_packet_size;
1477 115329f1 Diego Biurrun
    }
1478 899681cd Björn Axelsson
    url_fseek(s->pb, pos, SEEK_SET);
1479 e05655fb Michael Niedermayer
1480
    return 0;
1481
}
1482
1483 b45a7a18 Fabrice Bellard
/**************************************************************/
1484
/* parsing functions - called from other demuxers such as RTP */
1485
1486
MpegTSContext *mpegts_parse_open(AVFormatContext *s)
1487
{
1488
    MpegTSContext *ts;
1489 115329f1 Diego Biurrun
1490 b45a7a18 Fabrice Bellard
    ts = av_mallocz(sizeof(MpegTSContext));
1491
    if (!ts)
1492
        return NULL;
1493
    /* no stream case, currently used by RTP */
1494
    ts->raw_packet_size = TS_PACKET_SIZE;
1495
    ts->stream = s;
1496
    ts->auto_guess = 1;
1497
    return ts;
1498
}
1499
1500
/* return the consumed length if a packet was output, or -1 if no
1501
   packet is output */
1502
int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
1503
                        const uint8_t *buf, int len)
1504
{
1505
    int len1;
1506
1507
    len1 = len;
1508
    ts->pkt = pkt;
1509
    ts->stop_parse = 0;
1510
    for(;;) {
1511 90d13e30 Michael Niedermayer
        if (ts->stop_parse>0)
1512 b45a7a18 Fabrice Bellard
            break;
1513
        if (len < TS_PACKET_SIZE)
1514
            return -1;
1515
        if (buf[0] != 0x47) {
1516 550f0a9b Wolfram Gloger
            buf++;
1517 b45a7a18 Fabrice Bellard
            len--;
1518
        } else {
1519
            handle_packet(ts, buf);
1520
            buf += TS_PACKET_SIZE;
1521
            len -= TS_PACKET_SIZE;
1522
        }
1523
    }
1524
    return len1 - len;
1525
}
1526
1527
void mpegts_parse_close(MpegTSContext *ts)
1528
{
1529
    int i;
1530
1531
    for(i=0;i<NB_PID_MAX;i++)
1532
        av_free(ts->pids[i]);
1533
    av_free(ts);
1534
}
1535
1536 d2a067d1 Måns Rullgård
AVInputFormat mpegts_demuxer = {
1537 fe9cf0d4 Fabrice Bellard
    "mpegts",
1538 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
1539 fe9cf0d4 Fabrice Bellard
    sizeof(MpegTSContext),
1540
    mpegts_probe,
1541
    mpegts_read_header,
1542
    mpegts_read_packet,
1543
    mpegts_read_close,
1544 e05655fb Michael Niedermayer
    read_seek,
1545 cdd5034f Michael Niedermayer
    mpegts_get_pcr,
1546 ff9c8d7c Michael Niedermayer
    .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
1547 fe9cf0d4 Fabrice Bellard
};
1548 38c48be2 Michael Niedermayer
1549
AVInputFormat mpegtsraw_demuxer = {
1550
    "mpegtsraw",
1551 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
1552 38c48be2 Michael Niedermayer
    sizeof(MpegTSContext),
1553 056f6b8f Michael Niedermayer
    NULL,
1554 38c48be2 Michael Niedermayer
    mpegts_read_header,
1555
    mpegts_raw_read_packet,
1556
    mpegts_read_close,
1557
    read_seek,
1558
    mpegts_get_pcr,
1559 ff9c8d7c Michael Niedermayer
    .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
1560 38c48be2 Michael Niedermayer
};