00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "libavutil/crc.h"
00027 #include "libavutil/intreadwrite.h"
00028 #include "libavcodec/bytestream.h"
00029 #include "avformat.h"
00030 #include "mpegts.h"
00031 #include "internal.h"
00032 #include "seek.h"
00033 #include "isom.h"
00034
00035
00036 #define MAX_SCAN_PACKETS 32000
00037
00038
00039
00040 #define MAX_RESYNC_SIZE 65536
00041
00042 #define MAX_PES_PAYLOAD 200*1024
00043
00044 enum MpegTSFilterType {
00045 MPEGTS_PES,
00046 MPEGTS_SECTION,
00047 };
00048
00049 typedef struct MpegTSFilter MpegTSFilter;
00050
00051 typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
00052
00053 typedef struct MpegTSPESFilter {
00054 PESCallback *pes_cb;
00055 void *opaque;
00056 } MpegTSPESFilter;
00057
00058 typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
00059
00060 typedef void SetServiceCallback(void *opaque, int ret);
00061
00062 typedef struct MpegTSSectionFilter {
00063 int section_index;
00064 int section_h_size;
00065 uint8_t *section_buf;
00066 unsigned int check_crc:1;
00067 unsigned int end_of_section_reached:1;
00068 SectionCallback *section_cb;
00069 void *opaque;
00070 } MpegTSSectionFilter;
00071
00072 struct MpegTSFilter {
00073 int pid;
00074 int last_cc;
00075 enum MpegTSFilterType type;
00076 union {
00077 MpegTSPESFilter pes_filter;
00078 MpegTSSectionFilter section_filter;
00079 } u;
00080 };
00081
00082 #define MAX_PIDS_PER_PROGRAM 64
00083 struct Program {
00084 unsigned int id;
00085 unsigned int nb_pids;
00086 unsigned int pids[MAX_PIDS_PER_PROGRAM];
00087 };
00088
00089 struct MpegTSContext {
00090
00091 AVFormatContext *stream;
00093 int raw_packet_size;
00094
00095 int pos47;
00096
00098 int auto_guess;
00099
00101 int mpeg2ts_compute_pcr;
00102
00103 int64_t cur_pcr;
00104 int pcr_incr;
00106
00108 int stop_parse;
00110 AVPacket *pkt;
00112 int64_t last_pos;
00113
00114
00115
00116
00118 unsigned int nb_prg;
00119 struct Program *prg;
00120
00121
00123 MpegTSFilter *pids[NB_PID_MAX];
00124 };
00125
00126
00127
00128 enum MpegTSState {
00129 MPEGTS_HEADER = 0,
00130 MPEGTS_PESHEADER,
00131 MPEGTS_PESHEADER_FILL,
00132 MPEGTS_PAYLOAD,
00133 MPEGTS_SKIP,
00134 };
00135
00136
00137 #define PES_START_SIZE 6
00138 #define PES_HEADER_SIZE 9
00139 #define MAX_PES_HEADER_SIZE (9 + 255)
00140
00141 typedef struct PESContext {
00142 int pid;
00143 int pcr_pid;
00144 int stream_type;
00145 MpegTSContext *ts;
00146 AVFormatContext *stream;
00147 AVStream *st;
00148 AVStream *sub_st;
00149 enum MpegTSState state;
00150
00151 int data_index;
00152 int total_size;
00153 int pes_header_size;
00154 int extended_stream_id;
00155 int64_t pts, dts;
00156 int64_t ts_packet_pos;
00157 uint8_t header[MAX_PES_HEADER_SIZE];
00158 uint8_t *buffer;
00159 } PESContext;
00160
00161 extern AVInputFormat mpegts_demuxer;
00162
00163 static void clear_program(MpegTSContext *ts, unsigned int programid)
00164 {
00165 int i;
00166
00167 for(i=0; i<ts->nb_prg; i++)
00168 if(ts->prg[i].id == programid)
00169 ts->prg[i].nb_pids = 0;
00170 }
00171
00172 static void clear_programs(MpegTSContext *ts)
00173 {
00174 av_freep(&ts->prg);
00175 ts->nb_prg=0;
00176 }
00177
00178 static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
00179 {
00180 struct Program *p;
00181 void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
00182 if(!tmp)
00183 return;
00184 ts->prg = tmp;
00185 p = &ts->prg[ts->nb_prg];
00186 p->id = programid;
00187 p->nb_pids = 0;
00188 ts->nb_prg++;
00189 }
00190
00191 static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
00192 {
00193 int i;
00194 struct Program *p = NULL;
00195 for(i=0; i<ts->nb_prg; i++) {
00196 if(ts->prg[i].id == programid) {
00197 p = &ts->prg[i];
00198 break;
00199 }
00200 }
00201 if(!p)
00202 return;
00203
00204 if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
00205 return;
00206 p->pids[p->nb_pids++] = pid;
00207 }
00208
00217 static int discard_pid(MpegTSContext *ts, unsigned int pid)
00218 {
00219 int i, j, k;
00220 int used = 0, discarded = 0;
00221 struct Program *p;
00222 for(i=0; i<ts->nb_prg; i++) {
00223 p = &ts->prg[i];
00224 for(j=0; j<p->nb_pids; j++) {
00225 if(p->pids[j] != pid)
00226 continue;
00227
00228 for(k=0; k<ts->stream->nb_programs; k++) {
00229 if(ts->stream->programs[k]->id == p->id) {
00230 if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
00231 discarded++;
00232 else
00233 used++;
00234 }
00235 }
00236 }
00237 }
00238
00239 return !used && discarded;
00240 }
00241
00246 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
00247 const uint8_t *buf, int buf_size, int is_start)
00248 {
00249 MpegTSSectionFilter *tss = &tss1->u.section_filter;
00250 int len;
00251
00252 if (is_start) {
00253 memcpy(tss->section_buf, buf, buf_size);
00254 tss->section_index = buf_size;
00255 tss->section_h_size = -1;
00256 tss->end_of_section_reached = 0;
00257 } else {
00258 if (tss->end_of_section_reached)
00259 return;
00260 len = 4096 - tss->section_index;
00261 if (buf_size < len)
00262 len = buf_size;
00263 memcpy(tss->section_buf + tss->section_index, buf, len);
00264 tss->section_index += len;
00265 }
00266
00267
00268 if (tss->section_h_size == -1 && tss->section_index >= 3) {
00269 len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
00270 if (len > 4096)
00271 return;
00272 tss->section_h_size = len;
00273 }
00274
00275 if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
00276 tss->end_of_section_reached = 1;
00277 if (!tss->check_crc ||
00278 av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
00279 tss->section_buf, tss->section_h_size) == 0)
00280 tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
00281 }
00282 }
00283
00284 static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
00285 SectionCallback *section_cb, void *opaque,
00286 int check_crc)
00287
00288 {
00289 MpegTSFilter *filter;
00290 MpegTSSectionFilter *sec;
00291
00292 dprintf(ts->stream, "Filter: pid=0x%x\n", pid);
00293
00294 if (pid >= NB_PID_MAX || ts->pids[pid])
00295 return NULL;
00296 filter = av_mallocz(sizeof(MpegTSFilter));
00297 if (!filter)
00298 return NULL;
00299 ts->pids[pid] = filter;
00300 filter->type = MPEGTS_SECTION;
00301 filter->pid = pid;
00302 filter->last_cc = -1;
00303 sec = &filter->u.section_filter;
00304 sec->section_cb = section_cb;
00305 sec->opaque = opaque;
00306 sec->section_buf = av_malloc(MAX_SECTION_SIZE);
00307 sec->check_crc = check_crc;
00308 if (!sec->section_buf) {
00309 av_free(filter);
00310 return NULL;
00311 }
00312 return filter;
00313 }
00314
00315 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
00316 PESCallback *pes_cb,
00317 void *opaque)
00318 {
00319 MpegTSFilter *filter;
00320 MpegTSPESFilter *pes;
00321
00322 if (pid >= NB_PID_MAX || ts->pids[pid])
00323 return NULL;
00324 filter = av_mallocz(sizeof(MpegTSFilter));
00325 if (!filter)
00326 return NULL;
00327 ts->pids[pid] = filter;
00328 filter->type = MPEGTS_PES;
00329 filter->pid = pid;
00330 filter->last_cc = -1;
00331 pes = &filter->u.pes_filter;
00332 pes->pes_cb = pes_cb;
00333 pes->opaque = opaque;
00334 return filter;
00335 }
00336
00337 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
00338 {
00339 int pid;
00340
00341 pid = filter->pid;
00342 if (filter->type == MPEGTS_SECTION)
00343 av_freep(&filter->u.section_filter.section_buf);
00344 else if (filter->type == MPEGTS_PES) {
00345 PESContext *pes = filter->u.pes_filter.opaque;
00346 av_freep(&pes->buffer);
00347
00348
00349 if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
00350 av_freep(&filter->u.pes_filter.opaque);
00351 }
00352 }
00353
00354 av_free(filter);
00355 ts->pids[pid] = NULL;
00356 }
00357
00358 static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
00359 int stat[TS_MAX_PACKET_SIZE];
00360 int i;
00361 int x=0;
00362 int best_score=0;
00363
00364 memset(stat, 0, packet_size*sizeof(int));
00365
00366 for(x=i=0; i<size-3; i++){
00367 if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
00368 stat[x]++;
00369 if(stat[x] > best_score){
00370 best_score= stat[x];
00371 if(index) *index= x;
00372 }
00373 }
00374
00375 x++;
00376 if(x == packet_size) x= 0;
00377 }
00378
00379 return best_score;
00380 }
00381
00382
00383 static int get_packet_size(const uint8_t *buf, int size)
00384 {
00385 int score, fec_score, dvhs_score;
00386
00387 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
00388 return -1;
00389
00390 score = analyze(buf, size, TS_PACKET_SIZE, NULL);
00391 dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
00392 fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
00393
00394
00395 if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
00396 else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
00397 else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
00398 else return -1;
00399 }
00400
00401 typedef struct SectionHeader {
00402 uint8_t tid;
00403 uint16_t id;
00404 uint8_t version;
00405 uint8_t sec_num;
00406 uint8_t last_sec_num;
00407 } SectionHeader;
00408
00409 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
00410 {
00411 const uint8_t *p;
00412 int c;
00413
00414 p = *pp;
00415 if (p >= p_end)
00416 return -1;
00417 c = *p++;
00418 *pp = p;
00419 return c;
00420 }
00421
00422 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
00423 {
00424 const uint8_t *p;
00425 int c;
00426
00427 p = *pp;
00428 if ((p + 1) >= p_end)
00429 return -1;
00430 c = AV_RB16(p);
00431 p += 2;
00432 *pp = p;
00433 return c;
00434 }
00435
00436
00437 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
00438 {
00439 int len;
00440 const uint8_t *p;
00441 char *str;
00442
00443 p = *pp;
00444 len = get8(&p, p_end);
00445 if (len < 0)
00446 return NULL;
00447 if ((p + len) > p_end)
00448 return NULL;
00449 str = av_malloc(len + 1);
00450 if (!str)
00451 return NULL;
00452 memcpy(str, p, len);
00453 str[len] = '\0';
00454 p += len;
00455 *pp = p;
00456 return str;
00457 }
00458
00459 static int parse_section_header(SectionHeader *h,
00460 const uint8_t **pp, const uint8_t *p_end)
00461 {
00462 int val;
00463
00464 val = get8(pp, p_end);
00465 if (val < 0)
00466 return -1;
00467 h->tid = val;
00468 *pp += 2;
00469 val = get16(pp, p_end);
00470 if (val < 0)
00471 return -1;
00472 h->id = val;
00473 val = get8(pp, p_end);
00474 if (val < 0)
00475 return -1;
00476 h->version = (val >> 1) & 0x1f;
00477 val = get8(pp, p_end);
00478 if (val < 0)
00479 return -1;
00480 h->sec_num = val;
00481 val = get8(pp, p_end);
00482 if (val < 0)
00483 return -1;
00484 h->last_sec_num = val;
00485 return 0;
00486 }
00487
00488 typedef struct {
00489 uint32_t stream_type;
00490 enum AVMediaType codec_type;
00491 enum CodecID codec_id;
00492 } StreamType;
00493
00494 static const StreamType ISO_types[] = {
00495 { 0x01, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
00496 { 0x02, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
00497 { 0x03, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
00498 { 0x04, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
00499 { 0x0f, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC },
00500 { 0x10, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG4 },
00501 { 0x11, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC_LATM },
00502 { 0x1b, AVMEDIA_TYPE_VIDEO, CODEC_ID_H264 },
00503 { 0xd1, AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
00504 { 0xea, AVMEDIA_TYPE_VIDEO, CODEC_ID_VC1 },
00505 { 0 },
00506 };
00507
00508 static const StreamType HDMV_types[] = {
00509 { 0x80, AVMEDIA_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
00510 { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
00511 { 0x82, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00512 { 0x83, AVMEDIA_TYPE_AUDIO, CODEC_ID_TRUEHD },
00513 { 0x84, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
00514 { 0x90, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
00515 { 0 },
00516 };
00517
00518
00519 static const StreamType MISC_types[] = {
00520 { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
00521 { 0x8a, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00522 { 0 },
00523 };
00524
00525 static const StreamType REGD_types[] = {
00526 { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
00527 { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
00528 { 0 },
00529 };
00530
00531
00532 static const StreamType DESC_types[] = {
00533 { 0x6a, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
00534 { 0x7a, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
00535 { 0x7b, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00536 { 0x56, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
00537 { 0x59, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE },
00538 { 0 },
00539 };
00540
00541 static void mpegts_find_stream_type(AVStream *st,
00542 uint32_t stream_type, const StreamType *types)
00543 {
00544 for (; types->stream_type; types++) {
00545 if (stream_type == types->stream_type) {
00546 st->codec->codec_type = types->codec_type;
00547 st->codec->codec_id = types->codec_id;
00548 return;
00549 }
00550 }
00551 }
00552
00553 static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
00554 uint32_t stream_type, uint32_t prog_reg_desc)
00555 {
00556 av_set_pts_info(st, 33, 1, 90000);
00557 st->priv_data = pes;
00558 st->codec->codec_type = AVMEDIA_TYPE_DATA;
00559 st->codec->codec_id = CODEC_ID_NONE;
00560 st->need_parsing = AVSTREAM_PARSE_FULL;
00561 pes->st = st;
00562 pes->stream_type = stream_type;
00563
00564 av_log(pes->stream, AV_LOG_DEBUG,
00565 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
00566 st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
00567
00568 st->codec->codec_tag = pes->stream_type;
00569
00570 mpegts_find_stream_type(st, pes->stream_type, ISO_types);
00571 if (prog_reg_desc == AV_RL32("HDMV") &&
00572 st->codec->codec_id == CODEC_ID_NONE) {
00573 mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
00574 if (pes->stream_type == 0x83) {
00575
00576
00577 AVStream *sub_st;
00578
00579 PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
00580 if (!sub_pes)
00581 return AVERROR(ENOMEM);
00582 memcpy(sub_pes, pes, sizeof(*sub_pes));
00583
00584 sub_st = av_new_stream(pes->stream, pes->pid);
00585 if (!sub_st) {
00586 av_free(sub_pes);
00587 return AVERROR(ENOMEM);
00588 }
00589
00590 av_set_pts_info(sub_st, 33, 1, 90000);
00591 sub_st->priv_data = sub_pes;
00592 sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00593 sub_st->codec->codec_id = CODEC_ID_AC3;
00594 sub_st->need_parsing = AVSTREAM_PARSE_FULL;
00595 sub_pes->sub_st = pes->sub_st = sub_st;
00596 }
00597 }
00598 if (st->codec->codec_id == CODEC_ID_NONE)
00599 mpegts_find_stream_type(st, pes->stream_type, MISC_types);
00600
00601 return 0;
00602 }
00603
00604 static int64_t get_pts(const uint8_t *p)
00605 {
00606 int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
00607 pts |= (AV_RB16(p + 1) >> 1) << 15;
00608 pts |= AV_RB16(p + 3) >> 1;
00609 return pts;
00610 }
00611
00612 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
00613 {
00614 av_init_packet(pkt);
00615
00616 pkt->destruct = av_destruct_packet;
00617 pkt->data = pes->buffer;
00618 pkt->size = pes->data_index;
00619 memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00620
00621
00622 if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
00623 pkt->stream_index = pes->sub_st->index;
00624 else
00625 pkt->stream_index = pes->st->index;
00626 pkt->pts = pes->pts;
00627 pkt->dts = pes->dts;
00628
00629 pkt->pos = pes->ts_packet_pos;
00630
00631
00632 pes->pts = AV_NOPTS_VALUE;
00633 pes->dts = AV_NOPTS_VALUE;
00634 pes->buffer = NULL;
00635 pes->data_index = 0;
00636 }
00637
00638
00639 static int mpegts_push_data(MpegTSFilter *filter,
00640 const uint8_t *buf, int buf_size, int is_start,
00641 int64_t pos)
00642 {
00643 PESContext *pes = filter->u.pes_filter.opaque;
00644 MpegTSContext *ts = pes->ts;
00645 const uint8_t *p;
00646 int len, code;
00647
00648 if(!ts->pkt)
00649 return 0;
00650
00651 if (is_start) {
00652 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
00653 new_pes_packet(pes, ts->pkt);
00654 ts->stop_parse = 1;
00655 }
00656 pes->state = MPEGTS_HEADER;
00657 pes->data_index = 0;
00658 pes->ts_packet_pos = pos;
00659 }
00660 p = buf;
00661 while (buf_size > 0) {
00662 switch(pes->state) {
00663 case MPEGTS_HEADER:
00664 len = PES_START_SIZE - pes->data_index;
00665 if (len > buf_size)
00666 len = buf_size;
00667 memcpy(pes->header + pes->data_index, p, len);
00668 pes->data_index += len;
00669 p += len;
00670 buf_size -= len;
00671 if (pes->data_index == PES_START_SIZE) {
00672
00673
00674 #if 0
00675 av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
00676 #endif
00677 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
00678 pes->header[2] == 0x01) {
00679
00680 code = pes->header[3] | 0x100;
00681 dprintf(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
00682
00683 if ((pes->st && pes->st->discard == AVDISCARD_ALL) ||
00684 code == 0x1be)
00685 goto skip;
00686
00687 #if FF_API_MAX_STREAMS
00688 if (!pes->st && pes->stream->nb_streams == MAX_STREAMS)
00689 goto skip;
00690 #endif
00691
00692
00693 if (!pes->st) {
00694 pes->st = av_new_stream(ts->stream, pes->pid);
00695 if (!pes->st)
00696 return AVERROR(ENOMEM);
00697 mpegts_set_stream_info(pes->st, pes, 0, 0);
00698 }
00699
00700 pes->total_size = AV_RB16(pes->header + 4);
00701
00702
00703 if (!pes->total_size)
00704 pes->total_size = MAX_PES_PAYLOAD;
00705
00706
00707 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
00708 if (!pes->buffer)
00709 return AVERROR(ENOMEM);
00710
00711 if (code != 0x1bc && code != 0x1bf &&
00712 code != 0x1f0 && code != 0x1f1 &&
00713 code != 0x1ff && code != 0x1f2 &&
00714 code != 0x1f8) {
00715 pes->state = MPEGTS_PESHEADER;
00716 if (pes->st->codec->codec_id == CODEC_ID_NONE) {
00717 dprintf(pes->stream, "pid=%x stream_type=%x probing\n",
00718 pes->pid, pes->stream_type);
00719 pes->st->codec->codec_id = CODEC_ID_PROBE;
00720 }
00721 } else {
00722 pes->state = MPEGTS_PAYLOAD;
00723 pes->data_index = 0;
00724 }
00725 } else {
00726
00727
00728 skip:
00729 pes->state = MPEGTS_SKIP;
00730 continue;
00731 }
00732 }
00733 break;
00734
00735
00736 case MPEGTS_PESHEADER:
00737 len = PES_HEADER_SIZE - pes->data_index;
00738 if (len < 0)
00739 return -1;
00740 if (len > buf_size)
00741 len = buf_size;
00742 memcpy(pes->header + pes->data_index, p, len);
00743 pes->data_index += len;
00744 p += len;
00745 buf_size -= len;
00746 if (pes->data_index == PES_HEADER_SIZE) {
00747 pes->pes_header_size = pes->header[8] + 9;
00748 pes->state = MPEGTS_PESHEADER_FILL;
00749 }
00750 break;
00751 case MPEGTS_PESHEADER_FILL:
00752 len = pes->pes_header_size - pes->data_index;
00753 if (len < 0)
00754 return -1;
00755 if (len > buf_size)
00756 len = buf_size;
00757 memcpy(pes->header + pes->data_index, p, len);
00758 pes->data_index += len;
00759 p += len;
00760 buf_size -= len;
00761 if (pes->data_index == pes->pes_header_size) {
00762 const uint8_t *r;
00763 unsigned int flags, pes_ext, skip;
00764
00765 flags = pes->header[7];
00766 r = pes->header + 9;
00767 pes->pts = AV_NOPTS_VALUE;
00768 pes->dts = AV_NOPTS_VALUE;
00769 if ((flags & 0xc0) == 0x80) {
00770 pes->dts = pes->pts = get_pts(r);
00771 r += 5;
00772 } else if ((flags & 0xc0) == 0xc0) {
00773 pes->pts = get_pts(r);
00774 r += 5;
00775 pes->dts = get_pts(r);
00776 r += 5;
00777 }
00778 pes->extended_stream_id = -1;
00779 if (flags & 0x01) {
00780 pes_ext = *r++;
00781
00782 skip = (pes_ext >> 4) & 0xb;
00783 skip += skip & 0x9;
00784 r += skip;
00785 if ((pes_ext & 0x41) == 0x01 &&
00786 (r + 2) <= (pes->header + pes->pes_header_size)) {
00787
00788 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
00789 pes->extended_stream_id = r[1];
00790 }
00791 }
00792
00793
00794 pes->state = MPEGTS_PAYLOAD;
00795 pes->data_index = 0;
00796 }
00797 break;
00798 case MPEGTS_PAYLOAD:
00799 if (buf_size > 0 && pes->buffer) {
00800 if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
00801 new_pes_packet(pes, ts->pkt);
00802 pes->total_size = MAX_PES_PAYLOAD;
00803 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
00804 if (!pes->buffer)
00805 return AVERROR(ENOMEM);
00806 ts->stop_parse = 1;
00807 } else if (pes->data_index == 0 && buf_size > pes->total_size) {
00808
00809
00810 buf_size = pes->total_size;
00811 }
00812 memcpy(pes->buffer+pes->data_index, p, buf_size);
00813 pes->data_index += buf_size;
00814 }
00815 buf_size = 0;
00816
00817
00818
00819
00820
00821 if (pes->total_size < MAX_PES_PAYLOAD &&
00822 pes->pes_header_size + pes->data_index == pes->total_size + 6) {
00823 ts->stop_parse = 1;
00824 new_pes_packet(pes, ts->pkt);
00825 }
00826 break;
00827 case MPEGTS_SKIP:
00828 buf_size = 0;
00829 break;
00830 }
00831 }
00832
00833 return 0;
00834 }
00835
00836 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
00837 {
00838 MpegTSFilter *tss;
00839 PESContext *pes;
00840
00841
00842 pes = av_mallocz(sizeof(PESContext));
00843 if (!pes)
00844 return 0;
00845 pes->ts = ts;
00846 pes->stream = ts->stream;
00847 pes->pid = pid;
00848 pes->pcr_pid = pcr_pid;
00849 pes->state = MPEGTS_SKIP;
00850 pes->pts = AV_NOPTS_VALUE;
00851 pes->dts = AV_NOPTS_VALUE;
00852 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
00853 if (!tss) {
00854 av_free(pes);
00855 return 0;
00856 }
00857 return pes;
00858 }
00859
00860 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
00861 int *es_id, uint8_t **dec_config_descr,
00862 int *dec_config_descr_size)
00863 {
00864 ByteIOContext pb;
00865 int tag;
00866 unsigned len;
00867
00868 init_put_byte(&pb, buf, size, 0, NULL, NULL, NULL, NULL);
00869
00870 len = ff_mp4_read_descr(s, &pb, &tag);
00871 if (tag == MP4IODescrTag) {
00872 get_be16(&pb);
00873 get_byte(&pb);
00874 get_byte(&pb);
00875 get_byte(&pb);
00876 get_byte(&pb);
00877 get_byte(&pb);
00878 len = ff_mp4_read_descr(s, &pb, &tag);
00879 if (tag == MP4ESDescrTag) {
00880 *es_id = get_be16(&pb);
00881 dprintf(s, "ES_ID %#x\n", *es_id);
00882 get_byte(&pb);
00883 len = ff_mp4_read_descr(s, &pb, &tag);
00884 if (tag == MP4DecConfigDescrTag) {
00885 *dec_config_descr = av_malloc(len);
00886 if (!*dec_config_descr)
00887 return AVERROR(ENOMEM);
00888 *dec_config_descr_size = len;
00889 get_buffer(&pb, *dec_config_descr, len);
00890 }
00891 }
00892 }
00893 return 0;
00894 }
00895
00896 int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
00897 const uint8_t **pp, const uint8_t *desc_list_end,
00898 int mp4_dec_config_descr_len, int mp4_es_id, int pid,
00899 uint8_t *mp4_dec_config_descr)
00900 {
00901 const uint8_t *desc_end;
00902 int desc_len, desc_tag;
00903 char language[4];
00904
00905 desc_tag = get8(pp, desc_list_end);
00906 if (desc_tag < 0)
00907 return -1;
00908 desc_len = get8(pp, desc_list_end);
00909 if (desc_len < 0)
00910 return -1;
00911 desc_end = *pp + desc_len;
00912 if (desc_end > desc_list_end)
00913 return -1;
00914
00915 dprintf(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
00916
00917 if (st->codec->codec_id == CODEC_ID_NONE &&
00918 stream_type == STREAM_TYPE_PRIVATE_DATA)
00919 mpegts_find_stream_type(st, desc_tag, DESC_types);
00920
00921 switch(desc_tag) {
00922 case 0x1F:
00923 get16(pp, desc_end);
00924 if (st->codec->codec_id == CODEC_ID_AAC_LATM &&
00925 mp4_dec_config_descr_len && mp4_es_id == pid) {
00926 ByteIOContext pb;
00927 init_put_byte(&pb, mp4_dec_config_descr,
00928 mp4_dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
00929 ff_mp4_read_dec_config_descr(fc, st, &pb);
00930 if (st->codec->codec_id == CODEC_ID_AAC &&
00931 st->codec->extradata_size > 0)
00932 st->need_parsing = 0;
00933 }
00934 break;
00935 case 0x56:
00936 language[0] = get8(pp, desc_end);
00937 language[1] = get8(pp, desc_end);
00938 language[2] = get8(pp, desc_end);
00939 language[3] = 0;
00940 av_metadata_set2(&st->metadata, "language", language, 0);
00941 break;
00942 case 0x59:
00943 language[0] = get8(pp, desc_end);
00944 language[1] = get8(pp, desc_end);
00945 language[2] = get8(pp, desc_end);
00946 language[3] = 0;
00947 get8(pp, desc_end);
00948 if (st->codec->extradata) {
00949 if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
00950 av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
00951 } else {
00952 st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
00953 if (st->codec->extradata) {
00954 st->codec->extradata_size = 4;
00955 memcpy(st->codec->extradata, *pp, 4);
00956 }
00957 }
00958 *pp += 4;
00959 av_metadata_set2(&st->metadata, "language", language, 0);
00960 break;
00961 case 0x0a:
00962 language[0] = get8(pp, desc_end);
00963 language[1] = get8(pp, desc_end);
00964 language[2] = get8(pp, desc_end);
00965 language[3] = 0;
00966 av_metadata_set2(&st->metadata, "language", language, 0);
00967 break;
00968 case 0x05:
00969 st->codec->codec_tag = bytestream_get_le32(pp);
00970 dprintf(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
00971 if (st->codec->codec_id == CODEC_ID_NONE &&
00972 stream_type == STREAM_TYPE_PRIVATE_DATA)
00973 mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
00974 break;
00975 default:
00976 break;
00977 }
00978 *pp = desc_end;
00979 return 0;
00980 }
00981
00982 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
00983 {
00984 MpegTSContext *ts = filter->u.section_filter.opaque;
00985 SectionHeader h1, *h = &h1;
00986 PESContext *pes;
00987 AVStream *st;
00988 const uint8_t *p, *p_end, *desc_list_end;
00989 int program_info_length, pcr_pid, pid, stream_type;
00990 int desc_list_len;
00991 uint32_t prog_reg_desc = 0;
00992 uint8_t *mp4_dec_config_descr = NULL;
00993 int mp4_dec_config_descr_len = 0;
00994 int mp4_es_id = 0;
00995
00996 #ifdef DEBUG
00997 dprintf(ts->stream, "PMT: len %i\n", section_len);
00998 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
00999 #endif
01000
01001 p_end = section + section_len - 4;
01002 p = section;
01003 if (parse_section_header(h, &p, p_end) < 0)
01004 return;
01005
01006 dprintf(ts->stream, "sid=0x%x sec_num=%d/%d\n",
01007 h->id, h->sec_num, h->last_sec_num);
01008
01009 if (h->tid != PMT_TID)
01010 return;
01011
01012 clear_program(ts, h->id);
01013 pcr_pid = get16(&p, p_end) & 0x1fff;
01014 if (pcr_pid < 0)
01015 return;
01016 add_pid_to_pmt(ts, h->id, pcr_pid);
01017
01018 dprintf(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
01019
01020 program_info_length = get16(&p, p_end) & 0xfff;
01021 if (program_info_length < 0)
01022 return;
01023 while(program_info_length >= 2) {
01024 uint8_t tag, len;
01025 tag = get8(&p, p_end);
01026 len = get8(&p, p_end);
01027
01028 dprintf(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
01029
01030 if(len > program_info_length - 2)
01031
01032 break;
01033 program_info_length -= len + 2;
01034 if (tag == 0x1d) {
01035 get8(&p, p_end);
01036 get8(&p, p_end);
01037 len -= 2;
01038 mp4_read_iods(ts->stream, p, len, &mp4_es_id,
01039 &mp4_dec_config_descr, &mp4_dec_config_descr_len);
01040 } else if (tag == 0x05 && len >= 4) {
01041 prog_reg_desc = bytestream_get_le32(&p);
01042 len -= 4;
01043 }
01044 p += len;
01045 }
01046 p += program_info_length;
01047 if (p >= p_end)
01048 goto out;
01049
01050
01051 if (!ts->stream->nb_streams)
01052 ts->stop_parse = 1;
01053
01054 for(;;) {
01055 st = 0;
01056 stream_type = get8(&p, p_end);
01057 if (stream_type < 0)
01058 break;
01059 pid = get16(&p, p_end) & 0x1fff;
01060 if (pid < 0)
01061 break;
01062
01063
01064 if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
01065 pes = ts->pids[pid]->u.pes_filter.opaque;
01066 if (!pes->st)
01067 pes->st = av_new_stream(pes->stream, pes->pid);
01068 st = pes->st;
01069 } else {
01070 if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]);
01071 pes = add_pes_stream(ts, pid, pcr_pid);
01072 if (pes)
01073 st = av_new_stream(pes->stream, pes->pid);
01074 }
01075
01076 if (!st)
01077 goto out;
01078
01079 if (!pes->stream_type)
01080 mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
01081
01082 add_pid_to_pmt(ts, h->id, pid);
01083
01084 ff_program_add_stream_index(ts->stream, h->id, st->index);
01085
01086 desc_list_len = get16(&p, p_end) & 0xfff;
01087 if (desc_list_len < 0)
01088 break;
01089 desc_list_end = p + desc_list_len;
01090 if (desc_list_end > p_end)
01091 break;
01092 for(;;) {
01093 if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
01094 mp4_dec_config_descr_len, mp4_es_id, pid, mp4_dec_config_descr) < 0)
01095 break;
01096
01097 if (prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
01098 ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
01099 pes->sub_st->codec->codec_tag = st->codec->codec_tag;
01100 }
01101 }
01102 p = desc_list_end;
01103 }
01104
01105 out:
01106 av_free(mp4_dec_config_descr);
01107 }
01108
01109 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01110 {
01111 MpegTSContext *ts = filter->u.section_filter.opaque;
01112 SectionHeader h1, *h = &h1;
01113 const uint8_t *p, *p_end;
01114 int sid, pmt_pid;
01115
01116 #ifdef DEBUG
01117 dprintf(ts->stream, "PAT:\n");
01118 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
01119 #endif
01120 p_end = section + section_len - 4;
01121 p = section;
01122 if (parse_section_header(h, &p, p_end) < 0)
01123 return;
01124 if (h->tid != PAT_TID)
01125 return;
01126
01127 clear_programs(ts);
01128 for(;;) {
01129 sid = get16(&p, p_end);
01130 if (sid < 0)
01131 break;
01132 pmt_pid = get16(&p, p_end) & 0x1fff;
01133 if (pmt_pid < 0)
01134 break;
01135
01136 dprintf(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
01137
01138 if (sid == 0x0000) {
01139
01140 } else {
01141 av_new_program(ts->stream, sid);
01142 if (ts->pids[pmt_pid])
01143 mpegts_close_filter(ts, ts->pids[pmt_pid]);
01144 mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
01145 add_pat_entry(ts, sid);
01146 add_pid_to_pmt(ts, sid, 0);
01147 add_pid_to_pmt(ts, sid, pmt_pid);
01148 }
01149 }
01150 }
01151
01152 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01153 {
01154 MpegTSContext *ts = filter->u.section_filter.opaque;
01155 SectionHeader h1, *h = &h1;
01156 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
01157 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
01158 char *name, *provider_name;
01159
01160 #ifdef DEBUG
01161 dprintf(ts->stream, "SDT:\n");
01162 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
01163 #endif
01164
01165 p_end = section + section_len - 4;
01166 p = section;
01167 if (parse_section_header(h, &p, p_end) < 0)
01168 return;
01169 if (h->tid != SDT_TID)
01170 return;
01171 onid = get16(&p, p_end);
01172 if (onid < 0)
01173 return;
01174 val = get8(&p, p_end);
01175 if (val < 0)
01176 return;
01177 for(;;) {
01178 sid = get16(&p, p_end);
01179 if (sid < 0)
01180 break;
01181 val = get8(&p, p_end);
01182 if (val < 0)
01183 break;
01184 desc_list_len = get16(&p, p_end) & 0xfff;
01185 if (desc_list_len < 0)
01186 break;
01187 desc_list_end = p + desc_list_len;
01188 if (desc_list_end > p_end)
01189 break;
01190 for(;;) {
01191 desc_tag = get8(&p, desc_list_end);
01192 if (desc_tag < 0)
01193 break;
01194 desc_len = get8(&p, desc_list_end);
01195 desc_end = p + desc_len;
01196 if (desc_end > desc_list_end)
01197 break;
01198
01199 dprintf(ts->stream, "tag: 0x%02x len=%d\n",
01200 desc_tag, desc_len);
01201
01202 switch(desc_tag) {
01203 case 0x48:
01204 service_type = get8(&p, p_end);
01205 if (service_type < 0)
01206 break;
01207 provider_name = getstr8(&p, p_end);
01208 if (!provider_name)
01209 break;
01210 name = getstr8(&p, p_end);
01211 if (name) {
01212 AVProgram *program = av_new_program(ts->stream, sid);
01213 if(program) {
01214 av_metadata_set2(&program->metadata, "name", name, 0);
01215 av_metadata_set2(&program->metadata, "provider_name", provider_name, 0);
01216 }
01217 }
01218 av_free(name);
01219 av_free(provider_name);
01220 break;
01221 default:
01222 break;
01223 }
01224 p = desc_end;
01225 }
01226 p = desc_list_end;
01227 }
01228 }
01229
01230
01231 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
01232 {
01233 AVFormatContext *s = ts->stream;
01234 MpegTSFilter *tss;
01235 int len, pid, cc, cc_ok, afc, is_start;
01236 const uint8_t *p, *p_end;
01237 int64_t pos;
01238
01239 pid = AV_RB16(packet + 1) & 0x1fff;
01240 if(pid && discard_pid(ts, pid))
01241 return 0;
01242 is_start = packet[1] & 0x40;
01243 tss = ts->pids[pid];
01244 if (ts->auto_guess && tss == NULL && is_start) {
01245 add_pes_stream(ts, pid, -1);
01246 tss = ts->pids[pid];
01247 }
01248 if (!tss)
01249 return 0;
01250
01251
01252 cc = (packet[3] & 0xf);
01253 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
01254 tss->last_cc = cc;
01255
01256
01257 afc = (packet[3] >> 4) & 3;
01258 p = packet + 4;
01259 if (afc == 0)
01260 return 0;
01261 if (afc == 2)
01262 return 0;
01263 if (afc == 3) {
01264
01265 p += p[0] + 1;
01266 }
01267
01268 p_end = packet + TS_PACKET_SIZE;
01269 if (p >= p_end)
01270 return 0;
01271
01272 pos = url_ftell(ts->stream->pb);
01273 ts->pos47= pos % ts->raw_packet_size;
01274
01275 if (tss->type == MPEGTS_SECTION) {
01276 if (is_start) {
01277
01278 len = *p++;
01279 if (p + len > p_end)
01280 return 0;
01281 if (len && cc_ok) {
01282
01283 write_section_data(s, tss,
01284 p, len, 0);
01285
01286 if (!ts->pids[pid])
01287 return 0;
01288 }
01289 p += len;
01290 if (p < p_end) {
01291 write_section_data(s, tss,
01292 p, p_end - p, 1);
01293 }
01294 } else {
01295 if (cc_ok) {
01296 write_section_data(s, tss,
01297 p, p_end - p, 0);
01298 }
01299 }
01300 } else {
01301 int ret;
01302
01303 if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
01304 pos - ts->raw_packet_size)) < 0)
01305 return ret;
01306 }
01307
01308 return 0;
01309 }
01310
01311
01312
01313 static int mpegts_resync(AVFormatContext *s)
01314 {
01315 ByteIOContext *pb = s->pb;
01316 int c, i;
01317
01318 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
01319 c = url_fgetc(pb);
01320 if (c < 0)
01321 return -1;
01322 if (c == 0x47) {
01323 url_fseek(pb, -1, SEEK_CUR);
01324 return 0;
01325 }
01326 }
01327 av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
01328
01329 return -1;
01330 }
01331
01332
01333 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
01334 {
01335 ByteIOContext *pb = s->pb;
01336 int skip, len;
01337
01338 for(;;) {
01339 len = get_buffer(pb, buf, TS_PACKET_SIZE);
01340 if (len != TS_PACKET_SIZE)
01341 return AVERROR(EIO);
01342
01343 if (buf[0] != 0x47) {
01344
01345 url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
01346 if (mpegts_resync(s) < 0)
01347 return AVERROR(EAGAIN);
01348 else
01349 continue;
01350 } else {
01351 skip = raw_packet_size - TS_PACKET_SIZE;
01352 if (skip > 0)
01353 url_fskip(pb, skip);
01354 break;
01355 }
01356 }
01357 return 0;
01358 }
01359
01360 static int handle_packets(MpegTSContext *ts, int nb_packets)
01361 {
01362 AVFormatContext *s = ts->stream;
01363 uint8_t packet[TS_PACKET_SIZE];
01364 int packet_num, ret;
01365
01366 ts->stop_parse = 0;
01367 packet_num = 0;
01368 for(;;) {
01369 if (ts->stop_parse>0)
01370 break;
01371 packet_num++;
01372 if (nb_packets != 0 && packet_num >= nb_packets)
01373 break;
01374 ret = read_packet(s, packet, ts->raw_packet_size);
01375 if (ret != 0)
01376 return ret;
01377 ret = handle_packet(ts, packet);
01378 if (ret != 0)
01379 return ret;
01380 }
01381 return 0;
01382 }
01383
01384 static int mpegts_probe(AVProbeData *p)
01385 {
01386 #if 1
01387 const int size= p->buf_size;
01388 int score, fec_score, dvhs_score;
01389 int check_count= size / TS_FEC_PACKET_SIZE;
01390 #define CHECK_COUNT 10
01391
01392 if (check_count < CHECK_COUNT)
01393 return -1;
01394
01395 score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
01396 dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
01397 fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
01398
01399
01400
01401 if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
01402 else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
01403 else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
01404 else return -1;
01405 #else
01406
01407 if (av_match_ext(p->filename, "ts"))
01408 return AVPROBE_SCORE_MAX;
01409 else
01410 return 0;
01411 #endif
01412 }
01413
01414
01415
01416 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
01417 const uint8_t *packet)
01418 {
01419 int afc, len, flags;
01420 const uint8_t *p;
01421 unsigned int v;
01422
01423 afc = (packet[3] >> 4) & 3;
01424 if (afc <= 1)
01425 return -1;
01426 p = packet + 4;
01427 len = p[0];
01428 p++;
01429 if (len == 0)
01430 return -1;
01431 flags = *p++;
01432 len--;
01433 if (!(flags & 0x10))
01434 return -1;
01435 if (len < 6)
01436 return -1;
01437 v = AV_RB32(p);
01438 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
01439 *ppcr_low = ((p[4] & 1) << 8) | p[5];
01440 return 0;
01441 }
01442
01443 static int mpegts_read_header(AVFormatContext *s,
01444 AVFormatParameters *ap)
01445 {
01446 MpegTSContext *ts = s->priv_data;
01447 ByteIOContext *pb = s->pb;
01448 uint8_t buf[5*1024];
01449 int len;
01450 int64_t pos;
01451
01452 if (ap) {
01453 ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
01454 if(ap->mpeg2ts_raw){
01455 av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
01456 return -1;
01457 }
01458 }
01459
01460
01461 pos = url_ftell(pb);
01462 len = get_buffer(pb, buf, sizeof(buf));
01463 if (len != sizeof(buf))
01464 goto fail;
01465 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
01466 if (ts->raw_packet_size <= 0)
01467 goto fail;
01468 ts->stream = s;
01469 ts->auto_guess = 0;
01470
01471 if (s->iformat == &mpegts_demuxer) {
01472
01473
01474
01475 if (url_fseek(pb, pos, SEEK_SET) < 0)
01476 av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
01477
01478 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
01479
01480 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
01481
01482 handle_packets(ts, s->probesize / ts->raw_packet_size);
01483
01484
01485 ts->auto_guess = 1;
01486
01487 dprintf(ts->stream, "tuning done\n");
01488
01489 s->ctx_flags |= AVFMTCTX_NOHEADER;
01490 } else {
01491 AVStream *st;
01492 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
01493 int64_t pcrs[2], pcr_h;
01494 int packet_count[2];
01495 uint8_t packet[TS_PACKET_SIZE];
01496
01497
01498
01499 st = av_new_stream(s, 0);
01500 if (!st)
01501 goto fail;
01502 av_set_pts_info(st, 60, 1, 27000000);
01503 st->codec->codec_type = AVMEDIA_TYPE_DATA;
01504 st->codec->codec_id = CODEC_ID_MPEG2TS;
01505
01506
01507 pcr_pid = -1;
01508 nb_pcrs = 0;
01509 nb_packets = 0;
01510 for(;;) {
01511 ret = read_packet(s, packet, ts->raw_packet_size);
01512 if (ret < 0)
01513 return -1;
01514 pid = AV_RB16(packet + 1) & 0x1fff;
01515 if ((pcr_pid == -1 || pcr_pid == pid) &&
01516 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
01517 pcr_pid = pid;
01518 packet_count[nb_pcrs] = nb_packets;
01519 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
01520 nb_pcrs++;
01521 if (nb_pcrs >= 2)
01522 break;
01523 }
01524 nb_packets++;
01525 }
01526
01527
01528
01529 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
01530 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
01531 s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
01532 st->codec->bit_rate = s->bit_rate;
01533 st->start_time = ts->cur_pcr;
01534 #if 0
01535 av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
01536 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
01537 #endif
01538 }
01539
01540 url_fseek(pb, pos, SEEK_SET);
01541 return 0;
01542 fail:
01543 return -1;
01544 }
01545
01546 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
01547
01548 static int mpegts_raw_read_packet(AVFormatContext *s,
01549 AVPacket *pkt)
01550 {
01551 MpegTSContext *ts = s->priv_data;
01552 int ret, i;
01553 int64_t pcr_h, next_pcr_h, pos;
01554 int pcr_l, next_pcr_l;
01555 uint8_t pcr_buf[12];
01556
01557 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
01558 return AVERROR(ENOMEM);
01559 pkt->pos= url_ftell(s->pb);
01560 ret = read_packet(s, pkt->data, ts->raw_packet_size);
01561 if (ret < 0) {
01562 av_free_packet(pkt);
01563 return ret;
01564 }
01565 if (ts->mpeg2ts_compute_pcr) {
01566
01567 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
01568
01569 pos = url_ftell(s->pb);
01570 for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
01571 url_fseek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
01572 get_buffer(s->pb, pcr_buf, 12);
01573 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
01574
01575 ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
01576 (i + 1);
01577 break;
01578 }
01579 }
01580 url_fseek(s->pb, pos, SEEK_SET);
01581
01582 ts->cur_pcr = pcr_h * 300 + pcr_l;
01583 }
01584 pkt->pts = ts->cur_pcr;
01585 pkt->duration = ts->pcr_incr;
01586 ts->cur_pcr += ts->pcr_incr;
01587 }
01588 pkt->stream_index = 0;
01589 return 0;
01590 }
01591
01592 static int mpegts_read_packet(AVFormatContext *s,
01593 AVPacket *pkt)
01594 {
01595 MpegTSContext *ts = s->priv_data;
01596 int ret, i;
01597
01598 if (url_ftell(s->pb) != ts->last_pos) {
01599
01600 for (i = 0; i < NB_PID_MAX; i++) {
01601 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
01602 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
01603 av_freep(&pes->buffer);
01604 pes->data_index = 0;
01605 pes->state = MPEGTS_SKIP;
01606 }
01607 }
01608 }
01609
01610 ts->pkt = pkt;
01611 ret = handle_packets(ts, 0);
01612 if (ret < 0) {
01613
01614 for (i = 0; i < NB_PID_MAX; i++) {
01615 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
01616 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
01617 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
01618 new_pes_packet(pes, pkt);
01619 pes->state = MPEGTS_SKIP;
01620 ret = 0;
01621 break;
01622 }
01623 }
01624 }
01625 }
01626
01627 ts->last_pos = url_ftell(s->pb);
01628
01629 return ret;
01630 }
01631
01632 static int mpegts_read_close(AVFormatContext *s)
01633 {
01634 MpegTSContext *ts = s->priv_data;
01635 int i;
01636
01637 clear_programs(ts);
01638
01639 for(i=0;i<NB_PID_MAX;i++)
01640 if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
01641
01642 return 0;
01643 }
01644
01645 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
01646 int64_t *ppos, int64_t pos_limit)
01647 {
01648 MpegTSContext *ts = s->priv_data;
01649 int64_t pos, timestamp;
01650 uint8_t buf[TS_PACKET_SIZE];
01651 int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
01652 const int find_next= 1;
01653 pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
01654 if (find_next) {
01655 for(;;) {
01656 url_fseek(s->pb, pos, SEEK_SET);
01657 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
01658 return AV_NOPTS_VALUE;
01659 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
01660 parse_pcr(×tamp, &pcr_l, buf) == 0) {
01661 break;
01662 }
01663 pos += ts->raw_packet_size;
01664 }
01665 } else {
01666 for(;;) {
01667 pos -= ts->raw_packet_size;
01668 if (pos < 0)
01669 return AV_NOPTS_VALUE;
01670 url_fseek(s->pb, pos, SEEK_SET);
01671 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
01672 return AV_NOPTS_VALUE;
01673 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
01674 parse_pcr(×tamp, &pcr_l, buf) == 0) {
01675 break;
01676 }
01677 }
01678 }
01679 *ppos = pos;
01680
01681 return timestamp;
01682 }
01683
01684 #ifdef USE_SYNCPOINT_SEARCH
01685
01686 static int read_seek2(AVFormatContext *s,
01687 int stream_index,
01688 int64_t min_ts,
01689 int64_t target_ts,
01690 int64_t max_ts,
01691 int flags)
01692 {
01693 int64_t pos;
01694
01695 int64_t ts_ret, ts_adj;
01696 int stream_index_gen_search;
01697 AVStream *st;
01698 AVParserState *backup;
01699
01700 backup = ff_store_parser_state(s);
01701
01702
01703 flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
01704 AVSEEK_FLAG_BACKWARD : 0;
01705
01706 if (flags & AVSEEK_FLAG_BYTE) {
01707
01708 pos = target_ts;
01709 } else {
01710
01711 if (stream_index < 0) {
01712 stream_index_gen_search = av_find_default_stream_index(s);
01713 if (stream_index_gen_search < 0) {
01714 ff_restore_parser_state(s, backup);
01715 return -1;
01716 }
01717
01718 st = s->streams[stream_index_gen_search];
01719
01720 ts_adj = av_rescale(target_ts,
01721 st->time_base.den,
01722 AV_TIME_BASE * (int64_t)st->time_base.num);
01723 } else {
01724 ts_adj = target_ts;
01725 stream_index_gen_search = stream_index;
01726 }
01727 pos = av_gen_search(s, stream_index_gen_search, ts_adj,
01728 0, INT64_MAX, -1,
01729 AV_NOPTS_VALUE,
01730 AV_NOPTS_VALUE,
01731 flags, &ts_ret, mpegts_get_pcr);
01732 if (pos < 0) {
01733 ff_restore_parser_state(s, backup);
01734 return -1;
01735 }
01736 }
01737
01738
01739 if (ff_gen_syncpoint_search(s, stream_index, pos,
01740 min_ts, target_ts, max_ts,
01741 flags) < 0) {
01742 ff_restore_parser_state(s, backup);
01743 return -1;
01744 }
01745
01746 ff_free_parser_state(s, backup);
01747 return 0;
01748 }
01749
01750 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
01751 {
01752 int ret;
01753 if (flags & AVSEEK_FLAG_BACKWARD) {
01754 flags &= ~AVSEEK_FLAG_BACKWARD;
01755 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
01756 if (ret < 0)
01757
01758 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
01759 } else {
01760 ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
01761 if (ret < 0)
01762
01763 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
01764 }
01765 return ret;
01766 }
01767
01768 #else
01769
01770 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
01771 MpegTSContext *ts = s->priv_data;
01772 uint8_t buf[TS_PACKET_SIZE];
01773 int64_t pos;
01774
01775 if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
01776 return -1;
01777
01778 pos= url_ftell(s->pb);
01779
01780 for(;;) {
01781 url_fseek(s->pb, pos, SEEK_SET);
01782 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
01783 return -1;
01784
01785 if(buf[1] & 0x40) break;
01786 pos += ts->raw_packet_size;
01787 }
01788 url_fseek(s->pb, pos, SEEK_SET);
01789
01790 return 0;
01791 }
01792
01793 #endif
01794
01795
01796
01797
01798 MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
01799 {
01800 MpegTSContext *ts;
01801
01802 ts = av_mallocz(sizeof(MpegTSContext));
01803 if (!ts)
01804 return NULL;
01805
01806 ts->raw_packet_size = TS_PACKET_SIZE;
01807 ts->stream = s;
01808 ts->auto_guess = 1;
01809 return ts;
01810 }
01811
01812
01813
01814 int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
01815 const uint8_t *buf, int len)
01816 {
01817 int len1;
01818
01819 len1 = len;
01820 ts->pkt = pkt;
01821 ts->stop_parse = 0;
01822 for(;;) {
01823 if (ts->stop_parse>0)
01824 break;
01825 if (len < TS_PACKET_SIZE)
01826 return -1;
01827 if (buf[0] != 0x47) {
01828 buf++;
01829 len--;
01830 } else {
01831 handle_packet(ts, buf);
01832 buf += TS_PACKET_SIZE;
01833 len -= TS_PACKET_SIZE;
01834 }
01835 }
01836 return len1 - len;
01837 }
01838
01839 void ff_mpegts_parse_close(MpegTSContext *ts)
01840 {
01841 int i;
01842
01843 for(i=0;i<NB_PID_MAX;i++)
01844 av_free(ts->pids[i]);
01845 av_free(ts);
01846 }
01847
01848 AVInputFormat mpegts_demuxer = {
01849 "mpegts",
01850 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
01851 sizeof(MpegTSContext),
01852 mpegts_probe,
01853 mpegts_read_header,
01854 mpegts_read_packet,
01855 mpegts_read_close,
01856 read_seek,
01857 mpegts_get_pcr,
01858 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
01859 #ifdef USE_SYNCPOINT_SEARCH
01860 .read_seek2 = read_seek2,
01861 #endif
01862 };
01863
01864 AVInputFormat mpegtsraw_demuxer = {
01865 "mpegtsraw",
01866 NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
01867 sizeof(MpegTSContext),
01868 NULL,
01869 mpegts_read_header,
01870 mpegts_raw_read_packet,
01871 mpegts_read_close,
01872 read_seek,
01873 mpegts_get_pcr,
01874 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
01875 #ifdef USE_SYNCPOINT_SEARCH
01876 .read_seek2 = read_seek2,
01877 #endif
01878 };