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