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