2
* MPEG2 transport stream (aka DVB) demux
3
* Copyright (c) 2002-2003 Fabrice Bellard.
5
* 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.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26
/* 1.0 second at 24Mbit/s */
27
#define MAX_SCAN_PACKETS 32000
29
/* maximum size in which we look for synchronisation if
30
synchronisation is lost */
31
#define MAX_RESYNC_SIZE 4096
33
typedef struct PESContext PESContext;
35
static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int stream_type);
36
static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code);
38
enum MpegTSFilterType {
43
typedef void PESCallback(void *opaque, const uint8_t *buf, int len, int is_start);
45
typedef struct MpegTSPESFilter {
50
typedef void SectionCallback(void *opaque, const uint8_t *buf, int len);
52
typedef void SetServiceCallback(void *opaque, int ret);
54
typedef struct MpegTSSectionFilter {
59
int end_of_section_reached:1;
60
SectionCallback *section_cb;
62
} MpegTSSectionFilter;
64
typedef struct MpegTSFilter {
66
int last_cc; /* last cc code (-1 if first packet) */
67
enum MpegTSFilterType type;
69
MpegTSPESFilter pes_filter;
70
MpegTSSectionFilter section_filter;
74
typedef struct MpegTSService {
81
struct MpegTSContext {
83
AVFormatContext *stream;
84
int raw_packet_size; /* raw packet size, including FEC if present */
85
int auto_guess; /* if true, all pids are analized to find streams */
88
int mpeg2ts_raw; /* force raw MPEG2 transport stream output, if possible */
89
int mpeg2ts_compute_pcr; /* compute exact PCR for each transport stream packet */
91
/* used to estimate the exact PCR */
96
/* data needed to handle file based ts */
97
int stop_parse; /* stop parsing loop */
98
AVPacket *pkt; /* packet containing av data */
100
/******************************************/
101
/* private mpegts data */
103
MpegTSFilter *sdt_filter;
105
MpegTSService **services;
107
/* set service context (XXX: allocated it ?) */
108
SetServiceCallback *set_service_cb;
109
void *set_service_opaque;
110
MpegTSFilter *pat_filter;
111
MpegTSFilter *pmt_filter;
114
MpegTSFilter *pids[NB_PID_MAX];
117
static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
118
const uint8_t *buf, int buf_size, int is_start)
120
MpegTSSectionFilter *tss = &tss1->u.section_filter;
124
memcpy(tss->section_buf, buf, buf_size);
125
tss->section_index = buf_size;
126
tss->section_h_size = -1;
127
tss->end_of_section_reached = 0;
129
if (tss->end_of_section_reached)
131
len = 4096 - tss->section_index;
134
memcpy(tss->section_buf + tss->section_index, buf, len);
135
tss->section_index += len;
138
/* compute section length if possible */
139
if (tss->section_h_size == -1 && tss->section_index >= 3) {
140
len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
143
tss->section_h_size = len;
146
if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
147
tss->end_of_section_reached = 1;
148
if (!tss->check_crc ||
149
av_crc(av_crc04C11DB7, -1, tss->section_buf, tss->section_h_size) == 0)
150
tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size);
154
MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
155
SectionCallback *section_cb, void *opaque,
159
MpegTSFilter *filter;
160
MpegTSSectionFilter *sec;
163
printf("Filter: pid=0x%x\n", pid);
165
if (pid >= NB_PID_MAX || ts->pids[pid])
167
filter = av_mallocz(sizeof(MpegTSFilter));
170
ts->pids[pid] = filter;
171
filter->type = MPEGTS_SECTION;
173
filter->last_cc = -1;
174
sec = &filter->u.section_filter;
175
sec->section_cb = section_cb;
176
sec->opaque = opaque;
177
sec->section_buf = av_malloc(MAX_SECTION_SIZE);
178
sec->check_crc = check_crc;
179
if (!sec->section_buf) {
186
MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
190
MpegTSFilter *filter;
191
MpegTSPESFilter *pes;
193
if (pid >= NB_PID_MAX || ts->pids[pid])
195
filter = av_mallocz(sizeof(MpegTSFilter));
198
ts->pids[pid] = filter;
199
filter->type = MPEGTS_PES;
201
filter->last_cc = -1;
202
pes = &filter->u.pes_filter;
203
pes->pes_cb = pes_cb;
204
pes->opaque = opaque;
208
void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
213
if (filter->type == MPEGTS_SECTION)
214
av_freep(&filter->u.section_filter.section_buf);
215
else if (filter->type == MPEGTS_PES)
216
av_freep(&filter->u.pes_filter.opaque);
219
ts->pids[pid] = NULL;
222
static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
223
int stat[packet_size];
228
memset(stat, 0, packet_size*sizeof(int));
230
for(x=i=0; i<size; i++){
233
if(stat[x] > best_score){
240
if(x == packet_size) x= 0;
246
/* autodetect fec presence. Must have at least 1024 bytes */
247
static int get_packet_size(const uint8_t *buf, int size)
249
int score, fec_score, dvhs_score;
251
if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
254
score = analyze(buf, size, TS_PACKET_SIZE, NULL);
255
dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
256
fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
257
// av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
259
if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
260
else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
261
else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
265
typedef struct SectionHeader {
270
uint8_t last_sec_num;
273
static inline int get8(const uint8_t **pp, const uint8_t *p_end)
286
static inline int get16(const uint8_t **pp, const uint8_t *p_end)
292
if ((p + 1) >= p_end)
294
c = (p[0] << 8) | p[1];
300
/* read and allocate a DVB string preceeded by its length */
301
static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
308
len = get8(&p, p_end);
311
if ((p + len) > p_end)
313
str = av_malloc(len + 1);
323
static int parse_section_header(SectionHeader *h,
324
const uint8_t **pp, const uint8_t *p_end)
328
val = get8(pp, p_end);
333
val = get16(pp, p_end);
337
val = get8(pp, p_end);
340
h->version = (val >> 1) & 0x1f;
341
val = get8(pp, p_end);
345
val = get8(pp, p_end);
348
h->last_sec_num = val;
352
static MpegTSService *new_service(MpegTSContext *ts, int sid,
353
char *provider_name, char *name)
355
MpegTSService *service;
358
printf("new_service: sid=0x%04x provider='%s' name='%s'\n",
359
sid, provider_name, name);
362
service = av_mallocz(sizeof(MpegTSService));
366
service->provider_name = provider_name;
367
service->name = name;
368
dynarray_add(&ts->services, &ts->nb_services, service);
372
static void pmt_cb(void *opaque, const uint8_t *section, int section_len)
374
MpegTSContext *ts = opaque;
375
SectionHeader h1, *h = &h1;
378
const uint8_t *p, *p_end, *desc_list_end, *desc_end;
379
int program_info_length, pcr_pid, pid, stream_type;
380
int desc_list_len, desc_len, desc_tag;
381
int comp_page = 0, anc_page = 0; /* initialize to kill warnings */
386
av_hex_dump(stdout, (uint8_t *)section, section_len);
388
p_end = section + section_len - 4;
390
if (parse_section_header(h, &p, p_end) < 0)
393
printf("sid=0x%x sec_num=%d/%d\n", h->id, h->sec_num, h->last_sec_num);
395
if (h->tid != PMT_TID || (ts->req_sid >= 0 && h->id != ts->req_sid) )
398
pcr_pid = get16(&p, p_end) & 0x1fff;
401
ts->pcr_pid = pcr_pid;
403
printf("pcr_pid=0x%x\n", pcr_pid);
405
program_info_length = get16(&p, p_end) & 0xfff;
406
if (program_info_length < 0)
408
p += program_info_length;
414
stream_type = get8(&p, p_end);
417
pid = get16(&p, p_end) & 0x1fff;
420
desc_list_len = get16(&p, p_end) & 0xfff;
421
if (desc_list_len < 0)
423
desc_list_end = p + desc_list_len;
424
if (desc_list_end > p_end)
427
desc_tag = get8(&p, desc_list_end);
430
if (stream_type == STREAM_TYPE_PRIVATE_DATA &&
431
((desc_tag == 0x6A) || (desc_tag == 0x7A))) {
432
/*assume DVB AC-3 Audio*/
433
stream_type = STREAM_TYPE_AUDIO_AC3;
435
desc_len = get8(&p, desc_list_end);
436
desc_end = p + desc_len;
437
if (desc_end > desc_list_end)
440
printf("tag: 0x%02x len=%d\n", desc_tag, desc_len);
443
case DVB_SUBT_DESCID:
444
if (stream_type == STREAM_TYPE_PRIVATE_DATA)
445
stream_type = STREAM_TYPE_SUBTITLE_DVB;
447
language[0] = get8(&p, desc_end);
448
language[1] = get8(&p, desc_end);
449
language[2] = get8(&p, desc_end);
452
comp_page = get16(&p, desc_end);
453
anc_page = get16(&p, desc_end);
456
case 0x0a: /* ISO 639 language descriptor */
457
language[0] = get8(&p, desc_end);
458
language[1] = get8(&p, desc_end);
459
language[2] = get8(&p, desc_end);
470
printf("stream_type=%d pid=0x%x\n", stream_type, pid);
473
/* now create ffmpeg stream */
474
switch(stream_type) {
475
case STREAM_TYPE_AUDIO_MPEG1:
476
case STREAM_TYPE_AUDIO_MPEG2:
477
case STREAM_TYPE_VIDEO_MPEG1:
478
case STREAM_TYPE_VIDEO_MPEG2:
479
case STREAM_TYPE_VIDEO_MPEG4:
480
case STREAM_TYPE_VIDEO_H264:
481
case STREAM_TYPE_AUDIO_AAC:
482
case STREAM_TYPE_AUDIO_AC3:
483
case STREAM_TYPE_AUDIO_DTS:
484
case STREAM_TYPE_SUBTITLE_DVB:
485
pes = add_pes_stream(ts, pid, stream_type);
487
st = new_pes_av_stream(pes, 0);
490
/* we ignore the other streams */
495
if (language[0] != 0) {
496
st->language[0] = language[0];
497
st->language[1] = language[1];
498
st->language[2] = language[2];
499
st->language[3] = language[3];
502
if (stream_type == STREAM_TYPE_SUBTITLE_DVB) {
503
st->codec->sub_id = (anc_page << 16) | comp_page;
507
/* all parameters are there */
508
ts->set_service_cb(ts->set_service_opaque, 0);
509
mpegts_close_filter(ts, ts->pmt_filter);
510
ts->pmt_filter = NULL;
513
static void pat_cb(void *opaque, const uint8_t *section, int section_len)
515
MpegTSContext *ts = opaque;
516
SectionHeader h1, *h = &h1;
517
const uint8_t *p, *p_end;
522
av_hex_dump(stdout, (uint8_t *)section, section_len);
524
p_end = section + section_len - 4;
526
if (parse_section_header(h, &p, p_end) < 0)
528
if (h->tid != PAT_TID)
532
sid = get16(&p, p_end);
535
pmt_pid = get16(&p, p_end) & 0x1fff;
539
printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
544
if (ts->req_sid == sid) {
545
ts->pmt_filter = mpegts_open_section_filter(ts, pmt_pid,
552
ts->set_service_cb(ts->set_service_opaque, -1);
555
mpegts_close_filter(ts, ts->pat_filter);
556
ts->pat_filter = NULL;
559
/* add all services found in the PAT */
560
static void pat_scan_cb(void *opaque, const uint8_t *section, int section_len)
562
MpegTSContext *ts = opaque;
563
SectionHeader h1, *h = &h1;
564
const uint8_t *p, *p_end;
566
char *provider_name, *name;
571
av_hex_dump(stdout, (uint8_t *)section, section_len);
573
p_end = section + section_len - 4;
575
if (parse_section_header(h, &p, p_end) < 0)
577
if (h->tid != PAT_TID)
581
sid = get16(&p, p_end);
584
pmt_pid = get16(&p, p_end) & 0x1fff;
588
printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
593
/* add the service with a dummy name */
594
snprintf(buf, sizeof(buf), "Service %x\n", sid);
595
name = av_strdup(buf);
596
provider_name = av_strdup("");
597
if (name && provider_name) {
598
new_service(ts, sid, provider_name, name);
601
av_freep(&provider_name);
608
mpegts_close_filter(ts, ts->pat_filter);
609
ts->pat_filter = NULL;
612
void mpegts_set_service(MpegTSContext *ts, int sid,
613
SetServiceCallback *set_service_cb, void *opaque)
615
ts->set_service_cb = set_service_cb;
616
ts->set_service_opaque = opaque;
618
ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
622
static void sdt_cb(void *opaque, const uint8_t *section, int section_len)
624
MpegTSContext *ts = opaque;
625
SectionHeader h1, *h = &h1;
626
const uint8_t *p, *p_end, *desc_list_end, *desc_end;
627
int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
628
char *name, *provider_name;
632
av_hex_dump(stdout, (uint8_t *)section, section_len);
635
p_end = section + section_len - 4;
637
if (parse_section_header(h, &p, p_end) < 0)
639
if (h->tid != SDT_TID)
641
onid = get16(&p, p_end);
644
val = get8(&p, p_end);
648
sid = get16(&p, p_end);
651
val = get8(&p, p_end);
654
desc_list_len = get16(&p, p_end) & 0xfff;
655
if (desc_list_len < 0)
657
desc_list_end = p + desc_list_len;
658
if (desc_list_end > p_end)
661
desc_tag = get8(&p, desc_list_end);
664
desc_len = get8(&p, desc_list_end);
665
desc_end = p + desc_len;
666
if (desc_end > desc_list_end)
669
printf("tag: 0x%02x len=%d\n", desc_tag, desc_len);
673
service_type = get8(&p, p_end);
674
if (service_type < 0)
676
provider_name = getstr8(&p, p_end);
679
name = getstr8(&p, p_end);
682
new_service(ts, sid, provider_name, name);
694
mpegts_close_filter(ts, ts->sdt_filter);
695
ts->sdt_filter = NULL;
698
/* scan services in a transport stream by looking at the SDT */
699
void mpegts_scan_sdt(MpegTSContext *ts)
701
ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,
705
/* scan services in a transport stream by looking at the PAT (better
707
void mpegts_scan_pat(MpegTSContext *ts)
709
ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
713
/* TS stream handling */
717
MPEGTS_PESHEADER_FILL,
722
/* enough for PES header + length */
723
#define PES_START_SIZE 9
724
#define MAX_PES_HEADER_SIZE (9 + 255)
730
AVFormatContext *stream;
732
enum MpegTSState state;
733
/* used to get the format */
738
uint8_t header[MAX_PES_HEADER_SIZE];
741
static int64_t get_pts(const uint8_t *p)
746
pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
747
val = (p[1] << 8) | p[2];
748
pts |= (int64_t)(val >> 1) << 15;
749
val = (p[3] << 8) | p[4];
750
pts |= (int64_t)(val >> 1);
754
/* return non zero if a packet could be constructed */
755
static void mpegts_push_data(void *opaque,
756
const uint8_t *buf, int buf_size, int is_start)
758
PESContext *pes = opaque;
759
MpegTSContext *ts = pes->ts;
764
pes->state = MPEGTS_HEADER;
768
while (buf_size > 0) {
771
len = PES_START_SIZE - pes->data_index;
774
memcpy(pes->header + pes->data_index, p, len);
775
pes->data_index += len;
778
if (pes->data_index == PES_START_SIZE) {
779
/* we got all the PES or section header. We can now
782
av_hex_dump(pes->header, pes->data_index);
784
if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
785
pes->header[2] == 0x01) {
786
/* it must be an mpeg2 PES stream */
787
code = pes->header[3] | 0x100;
788
if (!((code >= 0x1c0 && code <= 0x1df) ||
789
(code >= 0x1e0 && code <= 0x1ef) ||
793
/* allocate stream */
794
new_pes_av_stream(pes, code);
796
pes->state = MPEGTS_PESHEADER_FILL;
797
pes->total_size = (pes->header[4] << 8) | pes->header[5];
798
/* NOTE: a zero total size means the PES size is
801
pes->total_size += 6;
802
pes->pes_header_size = pes->header[8] + 9;
804
/* otherwise, it should be a table */
807
pes->state = MPEGTS_SKIP;
812
/**********************************************/
813
/* PES packing parsing */
814
case MPEGTS_PESHEADER_FILL:
815
len = pes->pes_header_size - pes->data_index;
818
memcpy(pes->header + pes->data_index, p, len);
819
pes->data_index += len;
822
if (pes->data_index == pes->pes_header_size) {
826
flags = pes->header[7];
828
pes->pts = AV_NOPTS_VALUE;
829
pes->dts = AV_NOPTS_VALUE;
830
if ((flags & 0xc0) == 0x80) {
831
pes->pts = get_pts(r);
833
} else if ((flags & 0xc0) == 0xc0) {
834
pes->pts = get_pts(r);
836
pes->dts = get_pts(r);
839
/* we got the full header. We parse it and get the payload */
840
pes->state = MPEGTS_PAYLOAD;
844
if (pes->total_size) {
845
len = pes->total_size - pes->data_index;
852
AVPacket *pkt = ts->pkt;
853
if (pes->st && av_new_packet(pkt, len) == 0) {
854
memcpy(pkt->data, p, len);
855
pkt->stream_index = pes->st->index;
858
/* reset pts values */
859
pes->pts = AV_NOPTS_VALUE;
860
pes->dts = AV_NOPTS_VALUE;
874
static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code)
877
int codec_type, codec_id;
879
switch(pes->stream_type){
880
case STREAM_TYPE_AUDIO_MPEG1:
881
case STREAM_TYPE_AUDIO_MPEG2:
882
codec_type = CODEC_TYPE_AUDIO;
883
codec_id = CODEC_ID_MP3;
885
case STREAM_TYPE_VIDEO_MPEG1:
886
case STREAM_TYPE_VIDEO_MPEG2:
887
codec_type = CODEC_TYPE_VIDEO;
888
codec_id = CODEC_ID_MPEG2VIDEO;
890
case STREAM_TYPE_VIDEO_MPEG4:
891
codec_type = CODEC_TYPE_VIDEO;
892
codec_id = CODEC_ID_MPEG4;
894
case STREAM_TYPE_VIDEO_H264:
895
codec_type = CODEC_TYPE_VIDEO;
896
codec_id = CODEC_ID_H264;
898
case STREAM_TYPE_AUDIO_AAC:
899
codec_type = CODEC_TYPE_AUDIO;
900
codec_id = CODEC_ID_AAC;
902
case STREAM_TYPE_AUDIO_AC3:
903
codec_type = CODEC_TYPE_AUDIO;
904
codec_id = CODEC_ID_AC3;
906
case STREAM_TYPE_AUDIO_DTS:
907
codec_type = CODEC_TYPE_AUDIO;
908
codec_id = CODEC_ID_DTS;
910
case STREAM_TYPE_SUBTITLE_DVB:
911
codec_type = CODEC_TYPE_SUBTITLE;
912
codec_id = CODEC_ID_DVB_SUBTITLE;
915
if (code >= 0x1c0 && code <= 0x1df) {
916
codec_type = CODEC_TYPE_AUDIO;
917
codec_id = CODEC_ID_MP2;
918
} else if (code == 0x1bd) {
919
codec_type = CODEC_TYPE_AUDIO;
920
codec_id = CODEC_ID_AC3;
922
codec_type = CODEC_TYPE_VIDEO;
923
codec_id = CODEC_ID_MPEG1VIDEO;
927
st = av_new_stream(pes->stream, pes->pid);
929
av_set_pts_info(st, 33, 1, 90000);
931
st->codec->codec_type = codec_type;
932
st->codec->codec_id = codec_id;
933
st->need_parsing = 1;
940
static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int stream_type)
945
/* if no pid found, then add a pid context */
946
pes = av_mallocz(sizeof(PESContext));
950
pes->stream = ts->stream;
952
pes->stream_type = stream_type;
953
tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
961
/* handle one TS packet */
962
static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
964
AVFormatContext *s = ts->stream;
966
int len, pid, cc, cc_ok, afc, is_start;
967
const uint8_t *p, *p_end;
969
pid = ((packet[1] & 0x1f) << 8) | packet[2];
970
is_start = packet[1] & 0x40;
972
if (ts->auto_guess && tss == NULL && is_start) {
973
add_pes_stream(ts, pid, 0);
979
/* continuity check (currently not used) */
980
cc = (packet[3] & 0xf);
981
cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
984
/* skip adaptation field */
985
afc = (packet[3] >> 4) & 3;
987
if (afc == 0) /* reserved value */
989
if (afc == 2) /* adaptation field only */
992
/* skip adapation field */
995
/* if past the end of packet, ignore */
996
p_end = packet + TS_PACKET_SIZE;
1000
if (tss->type == MPEGTS_SECTION) {
1002
/* pointer field present */
1004
if (p + len > p_end)
1007
/* write remaining section bytes */
1008
write_section_data(s, tss,
1010
/* check whether filter has been closed */
1016
write_section_data(s, tss,
1021
write_section_data(s, tss,
1026
tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,
1027
p, p_end - p, is_start);
1031
/* XXX: try to find a better synchro over several packets (use
1032
get_packet_size() ?) */
1033
static int mpegts_resync(ByteIOContext *pb)
1037
for(i = 0;i < MAX_RESYNC_SIZE; i++) {
1042
url_fseek(pb, -1, SEEK_CUR);
1050
/* return -1 if error or EOF. Return 0 if OK. */
1051
static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
1056
len = get_buffer(pb, buf, TS_PACKET_SIZE);
1057
if (len != TS_PACKET_SIZE)
1059
/* check paquet sync byte */
1060
if (buf[0] != 0x47) {
1061
/* find a new packet start */
1062
url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
1063
if (mpegts_resync(pb) < 0)
1064
return AVERROR_INVALIDDATA;
1068
skip = raw_packet_size - TS_PACKET_SIZE;
1070
url_fskip(pb, skip);
1077
static int handle_packets(MpegTSContext *ts, int nb_packets)
1079
AVFormatContext *s = ts->stream;
1080
ByteIOContext *pb = &s->pb;
1081
uint8_t packet[TS_PACKET_SIZE];
1082
int packet_num, ret;
1090
if (nb_packets != 0 && packet_num >= nb_packets)
1092
ret = read_packet(pb, packet, ts->raw_packet_size);
1095
handle_packet(ts, packet);
1100
static int mpegts_probe(AVProbeData *p)
1103
const int size= p->buf_size;
1104
int score, fec_score, dvhs_score;
1105
#define CHECK_COUNT 10
1107
if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
1110
score = analyze(p->buf, TS_PACKET_SIZE *CHECK_COUNT, TS_PACKET_SIZE, NULL);
1111
dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE *CHECK_COUNT, TS_DVHS_PACKET_SIZE, NULL);
1112
fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
1113
// av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
1115
// we need a clear definition for the returned score otherwise things will become messy sooner or later
1116
if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
1117
else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
1118
else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
1121
/* only use the extension for safer guess */
1122
if (match_ext(p->filename, "ts"))
1123
return AVPROBE_SCORE_MAX;
1129
void set_service_cb(void *opaque, int ret)
1131
MpegTSContext *ts = opaque;
1132
ts->set_service_ret = ret;
1136
/* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
1137
(-1) if not available */
1138
static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
1139
const uint8_t *packet)
1141
int afc, len, flags;
1145
afc = (packet[3] >> 4) & 3;
1155
if (!(flags & 0x10))
1159
v = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1160
*ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1161
*ppcr_low = ((p[4] & 1) << 8) | p[5];
1165
static int mpegts_read_header(AVFormatContext *s,
1166
AVFormatParameters *ap)
1168
MpegTSContext *ts = s->priv_data;
1169
ByteIOContext *pb = &s->pb;
1173
MpegTSService *service;
1176
ts->mpeg2ts_raw = ap->mpeg2ts_raw;
1177
ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
1180
/* read the first 1024 bytes to get packet size */
1181
pos = url_ftell(pb);
1182
len = get_buffer(pb, buf, sizeof(buf));
1183
if (len != sizeof(buf))
1185
ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1186
if (ts->raw_packet_size <= 0)
1192
if (!ts->mpeg2ts_raw) {
1195
if (!ts->auto_guess) {
1196
ts->set_service_ret = -1;
1198
/* first do a scaning to get all the services */
1199
url_fseek(pb, pos, SEEK_SET);
1200
mpegts_scan_sdt(ts);
1202
handle_packets(ts, MAX_SCAN_PACKETS);
1204
if (ts->nb_services <= 0) {
1205
/* no SDT found, we try to look at the PAT */
1207
/* First remove the SDT filters from each PID */
1209
for (i=0; i < NB_PID_MAX; i++) {
1211
mpegts_close_filter(ts, ts->pids[i]);
1213
url_fseek(pb, pos, SEEK_SET);
1214
mpegts_scan_pat(ts);
1216
handle_packets(ts, MAX_SCAN_PACKETS);
1219
if (ts->nb_services <= 0) {
1220
/* raw transport stream */
1222
s->ctx_flags |= AVFMTCTX_NOHEADER;
1226
/* tune to first service found */
1227
for(i=0; i<ts->nb_services && ts->set_service_ret; i++){
1228
service = ts->services[i];
1231
printf("tuning to '%s'\n", service->name);
1234
/* now find the info for the first service if we found any,
1235
otherwise try to filter all PATs */
1237
url_fseek(pb, pos, SEEK_SET);
1238
mpegts_set_service(ts, sid, set_service_cb, ts);
1240
handle_packets(ts, MAX_SCAN_PACKETS);
1242
/* if could not find service, exit */
1244
if (ts->set_service_ret != 0) {
1248
//let's retry with auto_guess set
1250
goto goto_auto_guess;
1255
printf("tuning done\n");
1258
s->ctx_flags |= AVFMTCTX_NOHEADER;
1261
int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
1262
int64_t pcrs[2], pcr_h;
1263
int packet_count[2];
1264
uint8_t packet[TS_PACKET_SIZE];
1266
/* only read packets */
1269
st = av_new_stream(s, 0);
1272
av_set_pts_info(st, 60, 1, 27000000);
1273
st->codec->codec_type = CODEC_TYPE_DATA;
1274
st->codec->codec_id = CODEC_ID_MPEG2TS;
1276
/* we iterate until we find two PCRs to estimate the bitrate */
1281
ret = read_packet(&s->pb, packet, ts->raw_packet_size);
1284
pid = ((packet[1] & 0x1f) << 8) | packet[2];
1285
if ((pcr_pid == -1 || pcr_pid == pid) &&
1286
parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
1288
packet_count[nb_pcrs] = nb_packets;
1289
pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1296
ts->pcr_pid = pcr_pid;
1298
/* NOTE1: the bitrate is computed without the FEC */
1299
/* NOTE2: it is only the bitrate of the start of the stream */
1300
ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
1301
ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
1302
s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
1303
st->codec->bit_rate = s->bit_rate;
1304
st->start_time = ts->cur_pcr;
1306
printf("start=%0.3f pcr=%0.3f incr=%d\n",
1307
st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
1311
url_fseek(pb, pos, SEEK_SET);
1317
#define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1319
static int mpegts_raw_read_packet(AVFormatContext *s,
1322
MpegTSContext *ts = s->priv_data;
1324
int64_t pcr_h, next_pcr_h, pos;
1325
int pcr_l, next_pcr_l;
1326
uint8_t pcr_buf[12];
1328
if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
1330
pkt->pos= url_ftell(&s->pb);
1331
ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
1333
av_free_packet(pkt);
1336
if (ts->mpeg2ts_compute_pcr) {
1337
/* compute exact PCR for each packet */
1338
if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
1339
/* we read the next PCR (XXX: optimize it by using a bigger buffer */
1340
pos = url_ftell(&s->pb);
1341
for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
1342
url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
1343
get_buffer(&s->pb, pcr_buf, 12);
1344
if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
1345
/* XXX: not precise enough */
1346
ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
1351
url_fseek(&s->pb, pos, SEEK_SET);
1352
/* no next PCR found: we use previous increment */
1353
ts->cur_pcr = pcr_h * 300 + pcr_l;
1355
pkt->pts = ts->cur_pcr;
1356
pkt->duration = ts->pcr_incr;
1357
ts->cur_pcr += ts->pcr_incr;
1359
pkt->stream_index = 0;
1363
static int mpegts_read_packet(AVFormatContext *s,
1366
MpegTSContext *ts = s->priv_data;
1368
if (!ts->mpeg2ts_raw) {
1370
return handle_packets(ts, 0);
1372
return mpegts_raw_read_packet(s, pkt);
1376
static int mpegts_read_close(AVFormatContext *s)
1378
MpegTSContext *ts = s->priv_data;
1380
for(i=0;i<NB_PID_MAX;i++)
1381
if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
1383
for(i = 0; i < ts->nb_services; i++){
1384
av_free(ts->services[i]->provider_name);
1385
av_free(ts->services[i]->name);
1386
av_free(ts->services[i]);
1388
av_freep(&ts->services);
1393
static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
1394
int64_t *ppos, int64_t pos_limit)
1396
MpegTSContext *ts = s->priv_data;
1397
int64_t pos, timestamp;
1398
uint8_t buf[TS_PACKET_SIZE];
1400
const int find_next= 1;
1401
pos = ((*ppos + ts->raw_packet_size - 1) / ts->raw_packet_size) * ts->raw_packet_size;
1404
url_fseek(&s->pb, pos, SEEK_SET);
1405
if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1406
return AV_NOPTS_VALUE;
1407
pid = ((buf[1] & 0x1f) << 8) | buf[2];
1408
if (pid == ts->pcr_pid &&
1409
parse_pcr(×tamp, &pcr_l, buf) == 0) {
1412
pos += ts->raw_packet_size;
1416
pos -= ts->raw_packet_size;
1418
return AV_NOPTS_VALUE;
1419
url_fseek(&s->pb, pos, SEEK_SET);
1420
if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1421
return AV_NOPTS_VALUE;
1422
pid = ((buf[1] & 0x1f) << 8) | buf[2];
1423
if (pid == ts->pcr_pid &&
1424
parse_pcr(×tamp, &pcr_l, buf) == 0) {
1434
static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
1435
MpegTSContext *ts = s->priv_data;
1436
uint8_t buf[TS_PACKET_SIZE];
1439
if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
1442
pos= url_ftell(&s->pb);
1445
url_fseek(&s->pb, pos, SEEK_SET);
1446
if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1448
// pid = ((buf[1] & 0x1f) << 8) | buf[2];
1449
if(buf[1] & 0x40) break;
1450
pos += ts->raw_packet_size;
1452
url_fseek(&s->pb, pos, SEEK_SET);
1457
/**************************************************************/
1458
/* parsing functions - called from other demuxers such as RTP */
1460
MpegTSContext *mpegts_parse_open(AVFormatContext *s)
1464
ts = av_mallocz(sizeof(MpegTSContext));
1467
/* no stream case, currently used by RTP */
1468
ts->raw_packet_size = TS_PACKET_SIZE;
1474
/* return the consumed length if a packet was output, or -1 if no
1476
int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
1477
const uint8_t *buf, int len)
1487
if (len < TS_PACKET_SIZE)
1489
if (buf[0] != 0x47) {
1493
handle_packet(ts, buf);
1494
buf += TS_PACKET_SIZE;
1495
len -= TS_PACKET_SIZE;
1501
void mpegts_parse_close(MpegTSContext *ts)
1505
for(i=0;i<NB_PID_MAX;i++)
1506
av_free(ts->pids[i]);
1510
AVInputFormat mpegts_demux = {
1512
"MPEG2 transport stream format",
1513
sizeof(MpegTSContext),
1520
.flags = AVFMT_SHOW_IDS,
1523
int mpegts_init(void)
1525
av_register_input_format(&mpegts_demux);
1526
#ifdef CONFIG_MUXERS
1527
av_register_output_format(&mpegts_mux);