Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpegts.c @ 5a757507

History | View | Annotate | Download (43 KB)

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