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/mathematics.h"
00029 #include "libavutil/opt.h"
00030 #include "libavutil/avassert.h"
00031 #include "libavcodec/bytestream.h"
00032 #include "libavcodec/get_bits.h"
00033 #include "avformat.h"
00034 #include "mpegts.h"
00035 #include "internal.h"
00036 #include "avio_internal.h"
00037 #include "seek.h"
00038 #include "mpeg.h"
00039 #include "isom.h"
00040
00041
00042
00043 #define MAX_RESYNC_SIZE 65536
00044
00045 #define MAX_PES_PAYLOAD 200*1024
00046
00047 #define MAX_MP4_DESCR_COUNT 16
00048
00049 enum MpegTSFilterType {
00050 MPEGTS_PES,
00051 MPEGTS_SECTION,
00052 };
00053
00054 typedef struct MpegTSFilter MpegTSFilter;
00055
00056 typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
00057
00058 typedef struct MpegTSPESFilter {
00059 PESCallback *pes_cb;
00060 void *opaque;
00061 } MpegTSPESFilter;
00062
00063 typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
00064
00065 typedef void SetServiceCallback(void *opaque, int ret);
00066
00067 typedef struct MpegTSSectionFilter {
00068 int section_index;
00069 int section_h_size;
00070 uint8_t *section_buf;
00071 unsigned int check_crc:1;
00072 unsigned int end_of_section_reached:1;
00073 SectionCallback *section_cb;
00074 void *opaque;
00075 } MpegTSSectionFilter;
00076
00077 struct MpegTSFilter {
00078 int pid;
00079 int es_id;
00080 int last_cc;
00081 enum MpegTSFilterType type;
00082 union {
00083 MpegTSPESFilter pes_filter;
00084 MpegTSSectionFilter section_filter;
00085 } u;
00086 };
00087
00088 #define MAX_PIDS_PER_PROGRAM 64
00089 struct Program {
00090 unsigned int id;
00091 unsigned int nb_pids;
00092 unsigned int pids[MAX_PIDS_PER_PROGRAM];
00093 };
00094
00095 struct MpegTSContext {
00096 const AVClass *class;
00097
00098 AVFormatContext *stream;
00100 int raw_packet_size;
00101
00102 int pos47;
00103
00105 int auto_guess;
00106
00108 int mpeg2ts_compute_pcr;
00109
00110 int64_t cur_pcr;
00111 int pcr_incr;
00113
00115 int stop_parse;
00117 AVPacket *pkt;
00119 int64_t last_pos;
00120
00121
00122
00123
00125 unsigned int nb_prg;
00126 struct Program *prg;
00127
00128
00130 MpegTSFilter *pids[NB_PID_MAX];
00131 };
00132
00133 static const AVOption options[] = {
00134 {"compute_pcr", "Compute exact PCR for each transport stream packet.", offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_INT,
00135 {.dbl = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
00136 { NULL },
00137 };
00138
00139 static const AVClass mpegtsraw_class = {
00140 .class_name = "mpegtsraw demuxer",
00141 .item_name = av_default_item_name,
00142 .option = options,
00143 .version = LIBAVUTIL_VERSION_INT,
00144 };
00145
00146
00147
00148 enum MpegTSState {
00149 MPEGTS_HEADER = 0,
00150 MPEGTS_PESHEADER,
00151 MPEGTS_PESHEADER_FILL,
00152 MPEGTS_PAYLOAD,
00153 MPEGTS_SKIP,
00154 };
00155
00156
00157 #define PES_START_SIZE 6
00158 #define PES_HEADER_SIZE 9
00159 #define MAX_PES_HEADER_SIZE (9 + 255)
00160
00161 typedef struct PESContext {
00162 int pid;
00163 int pcr_pid;
00164 int stream_type;
00165 MpegTSContext *ts;
00166 AVFormatContext *stream;
00167 AVStream *st;
00168 AVStream *sub_st;
00169 enum MpegTSState state;
00170
00171 int data_index;
00172 int flags;
00173 int total_size;
00174 int pes_header_size;
00175 int extended_stream_id;
00176 int64_t pts, dts;
00177 int64_t ts_packet_pos;
00178 uint8_t header[MAX_PES_HEADER_SIZE];
00179 uint8_t *buffer;
00180 SLConfigDescr sl;
00181 } PESContext;
00182
00183 extern AVInputFormat ff_mpegts_demuxer;
00184
00185 static void clear_program(MpegTSContext *ts, unsigned int programid)
00186 {
00187 int i;
00188
00189 for(i=0; i<ts->nb_prg; i++)
00190 if(ts->prg[i].id == programid)
00191 ts->prg[i].nb_pids = 0;
00192 }
00193
00194 static void clear_programs(MpegTSContext *ts)
00195 {
00196 av_freep(&ts->prg);
00197 ts->nb_prg=0;
00198 }
00199
00200 static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
00201 {
00202 struct Program *p;
00203 void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
00204 if(!tmp)
00205 return;
00206 ts->prg = tmp;
00207 p = &ts->prg[ts->nb_prg];
00208 p->id = programid;
00209 p->nb_pids = 0;
00210 ts->nb_prg++;
00211 }
00212
00213 static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
00214 {
00215 int i;
00216 struct Program *p = NULL;
00217 for(i=0; i<ts->nb_prg; i++) {
00218 if(ts->prg[i].id == programid) {
00219 p = &ts->prg[i];
00220 break;
00221 }
00222 }
00223 if(!p)
00224 return;
00225
00226 if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
00227 return;
00228 p->pids[p->nb_pids++] = pid;
00229 }
00230
00231 static void set_pcr_pid(AVFormatContext *s, unsigned int programid, unsigned int pid)
00232 {
00233 int i;
00234 for(i=0; i<s->nb_programs; i++) {
00235 if(s->programs[i]->id == programid) {
00236 s->programs[i]->pcr_pid = pid;
00237 break;
00238 }
00239 }
00240 }
00241
00250 static int discard_pid(MpegTSContext *ts, unsigned int pid)
00251 {
00252 int i, j, k;
00253 int used = 0, discarded = 0;
00254 struct Program *p;
00255 for(i=0; i<ts->nb_prg; i++) {
00256 p = &ts->prg[i];
00257 for(j=0; j<p->nb_pids; j++) {
00258 if(p->pids[j] != pid)
00259 continue;
00260
00261 for(k=0; k<ts->stream->nb_programs; k++) {
00262 if(ts->stream->programs[k]->id == p->id) {
00263 if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
00264 discarded++;
00265 else
00266 used++;
00267 }
00268 }
00269 }
00270 }
00271
00272 return !used && discarded;
00273 }
00274
00279 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
00280 const uint8_t *buf, int buf_size, int is_start)
00281 {
00282 MpegTSSectionFilter *tss = &tss1->u.section_filter;
00283 int len;
00284
00285 if (is_start) {
00286 memcpy(tss->section_buf, buf, buf_size);
00287 tss->section_index = buf_size;
00288 tss->section_h_size = -1;
00289 tss->end_of_section_reached = 0;
00290 } else {
00291 if (tss->end_of_section_reached)
00292 return;
00293 len = 4096 - tss->section_index;
00294 if (buf_size < len)
00295 len = buf_size;
00296 memcpy(tss->section_buf + tss->section_index, buf, len);
00297 tss->section_index += len;
00298 }
00299
00300
00301 if (tss->section_h_size == -1 && tss->section_index >= 3) {
00302 len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
00303 if (len > 4096)
00304 return;
00305 tss->section_h_size = len;
00306 }
00307
00308 if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
00309 tss->end_of_section_reached = 1;
00310 if (!tss->check_crc ||
00311 av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
00312 tss->section_buf, tss->section_h_size) == 0)
00313 tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
00314 }
00315 }
00316
00317 static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
00318 SectionCallback *section_cb, void *opaque,
00319 int check_crc)
00320
00321 {
00322 MpegTSFilter *filter;
00323 MpegTSSectionFilter *sec;
00324
00325 av_dlog(ts->stream, "Filter: pid=0x%x\n", pid);
00326
00327 if (pid >= NB_PID_MAX || ts->pids[pid])
00328 return NULL;
00329 filter = av_mallocz(sizeof(MpegTSFilter));
00330 if (!filter)
00331 return NULL;
00332 ts->pids[pid] = filter;
00333 filter->type = MPEGTS_SECTION;
00334 filter->pid = pid;
00335 filter->es_id = -1;
00336 filter->last_cc = -1;
00337 sec = &filter->u.section_filter;
00338 sec->section_cb = section_cb;
00339 sec->opaque = opaque;
00340 sec->section_buf = av_malloc(MAX_SECTION_SIZE);
00341 sec->check_crc = check_crc;
00342 if (!sec->section_buf) {
00343 av_free(filter);
00344 return NULL;
00345 }
00346 return filter;
00347 }
00348
00349 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
00350 PESCallback *pes_cb,
00351 void *opaque)
00352 {
00353 MpegTSFilter *filter;
00354 MpegTSPESFilter *pes;
00355
00356 if (pid >= NB_PID_MAX || ts->pids[pid])
00357 return NULL;
00358 filter = av_mallocz(sizeof(MpegTSFilter));
00359 if (!filter)
00360 return NULL;
00361 ts->pids[pid] = filter;
00362 filter->type = MPEGTS_PES;
00363 filter->pid = pid;
00364 filter->es_id = -1;
00365 filter->last_cc = -1;
00366 pes = &filter->u.pes_filter;
00367 pes->pes_cb = pes_cb;
00368 pes->opaque = opaque;
00369 return filter;
00370 }
00371
00372 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
00373 {
00374 int pid;
00375
00376 pid = filter->pid;
00377 if (filter->type == MPEGTS_SECTION)
00378 av_freep(&filter->u.section_filter.section_buf);
00379 else if (filter->type == MPEGTS_PES) {
00380 PESContext *pes = filter->u.pes_filter.opaque;
00381 av_freep(&pes->buffer);
00382
00383
00384 if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
00385 av_freep(&filter->u.pes_filter.opaque);
00386 }
00387 }
00388
00389 av_free(filter);
00390 ts->pids[pid] = NULL;
00391 }
00392
00393 static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
00394 int stat[TS_MAX_PACKET_SIZE];
00395 int i;
00396 int x=0;
00397 int best_score=0;
00398
00399 memset(stat, 0, packet_size*sizeof(int));
00400
00401 for(x=i=0; i<size-3; i++){
00402 if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && buf[i+3] != 0x47){
00403 stat[x]++;
00404 if(stat[x] > best_score){
00405 best_score= stat[x];
00406 if(index) *index= x;
00407 }
00408 }
00409
00410 x++;
00411 if(x == packet_size) x= 0;
00412 }
00413
00414 return best_score;
00415 }
00416
00417
00418 static int get_packet_size(const uint8_t *buf, int size)
00419 {
00420 int score, fec_score, dvhs_score;
00421
00422 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
00423 return -1;
00424
00425 score = analyze(buf, size, TS_PACKET_SIZE, NULL);
00426 dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
00427 fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
00428
00429
00430 if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
00431 else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
00432 else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
00433 else return -1;
00434 }
00435
00436 typedef struct SectionHeader {
00437 uint8_t tid;
00438 uint16_t id;
00439 uint8_t version;
00440 uint8_t sec_num;
00441 uint8_t last_sec_num;
00442 } SectionHeader;
00443
00444 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
00445 {
00446 const uint8_t *p;
00447 int c;
00448
00449 p = *pp;
00450 if (p >= p_end)
00451 return -1;
00452 c = *p++;
00453 *pp = p;
00454 return c;
00455 }
00456
00457 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
00458 {
00459 const uint8_t *p;
00460 int c;
00461
00462 p = *pp;
00463 if ((p + 1) >= p_end)
00464 return -1;
00465 c = AV_RB16(p);
00466 p += 2;
00467 *pp = p;
00468 return c;
00469 }
00470
00471
00472 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
00473 {
00474 int len;
00475 const uint8_t *p;
00476 char *str;
00477
00478 p = *pp;
00479 len = get8(&p, p_end);
00480 if (len < 0)
00481 return NULL;
00482 if ((p + len) > p_end)
00483 return NULL;
00484 str = av_malloc(len + 1);
00485 if (!str)
00486 return NULL;
00487 memcpy(str, p, len);
00488 str[len] = '\0';
00489 p += len;
00490 *pp = p;
00491 return str;
00492 }
00493
00494 static int parse_section_header(SectionHeader *h,
00495 const uint8_t **pp, const uint8_t *p_end)
00496 {
00497 int val;
00498
00499 val = get8(pp, p_end);
00500 if (val < 0)
00501 return -1;
00502 h->tid = val;
00503 *pp += 2;
00504 val = get16(pp, p_end);
00505 if (val < 0)
00506 return -1;
00507 h->id = val;
00508 val = get8(pp, p_end);
00509 if (val < 0)
00510 return -1;
00511 h->version = (val >> 1) & 0x1f;
00512 val = get8(pp, p_end);
00513 if (val < 0)
00514 return -1;
00515 h->sec_num = val;
00516 val = get8(pp, p_end);
00517 if (val < 0)
00518 return -1;
00519 h->last_sec_num = val;
00520 return 0;
00521 }
00522
00523 typedef struct {
00524 uint32_t stream_type;
00525 enum AVMediaType codec_type;
00526 enum CodecID codec_id;
00527 } StreamType;
00528
00529 static const StreamType ISO_types[] = {
00530 { 0x01, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
00531 { 0x02, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
00532 { 0x03, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
00533 { 0x04, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
00534 { 0x0f, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC },
00535 { 0x10, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG4 },
00536
00537
00538
00539 { 0x1b, AVMEDIA_TYPE_VIDEO, CODEC_ID_H264 },
00540 { 0xd1, AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
00541 { 0xea, AVMEDIA_TYPE_VIDEO, CODEC_ID_VC1 },
00542 { 0 },
00543 };
00544
00545 static const StreamType HDMV_types[] = {
00546 { 0x80, AVMEDIA_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
00547 { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
00548 { 0x82, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00549 { 0x83, AVMEDIA_TYPE_AUDIO, CODEC_ID_TRUEHD },
00550 { 0x84, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
00551 { 0x85, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00552 { 0x86, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00553 { 0xa1, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
00554 { 0xa2, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00555 { 0x90, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
00556 { 0 },
00557 };
00558
00559
00560 static const StreamType MISC_types[] = {
00561 { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
00562 { 0x8a, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00563 { 0 },
00564 };
00565
00566 static const StreamType REGD_types[] = {
00567 { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
00568 { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
00569 { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, CODEC_ID_S302M },
00570 { MKTAG('D','T','S','1'), AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00571 { MKTAG('D','T','S','2'), AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00572 { MKTAG('D','T','S','3'), AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00573 { MKTAG('V','C','-','1'), AVMEDIA_TYPE_VIDEO, CODEC_ID_VC1 },
00574 { 0 },
00575 };
00576
00577
00578 static const StreamType DESC_types[] = {
00579 { 0x6a, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
00580 { 0x7a, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
00581 { 0x7b, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00582 { 0x56, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
00583 { 0x59, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE },
00584 { 0 },
00585 };
00586
00587 static void mpegts_find_stream_type(AVStream *st,
00588 uint32_t stream_type, const StreamType *types)
00589 {
00590 for (; types->stream_type; types++) {
00591 if (stream_type == types->stream_type) {
00592 st->codec->codec_type = types->codec_type;
00593 st->codec->codec_id = types->codec_id;
00594 st->request_probe = 0;
00595 return;
00596 }
00597 }
00598 }
00599
00600 static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
00601 uint32_t stream_type, uint32_t prog_reg_desc)
00602 {
00603 int old_codec_type= st->codec->codec_type;
00604 int old_codec_id = st->codec->codec_id;
00605 avpriv_set_pts_info(st, 33, 1, 90000);
00606 st->priv_data = pes;
00607 st->codec->codec_type = AVMEDIA_TYPE_DATA;
00608 st->codec->codec_id = CODEC_ID_NONE;
00609 st->need_parsing = AVSTREAM_PARSE_FULL;
00610 pes->st = st;
00611 pes->stream_type = stream_type;
00612
00613 av_log(pes->stream, AV_LOG_DEBUG,
00614 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
00615 st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
00616
00617 st->codec->codec_tag = pes->stream_type;
00618
00619 mpegts_find_stream_type(st, pes->stream_type, ISO_types);
00620 if ((prog_reg_desc == AV_RL32("HDMV") ||
00621 prog_reg_desc == AV_RL32("HDPR")) &&
00622 st->codec->codec_id == CODEC_ID_NONE) {
00623 mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
00624 if (pes->stream_type == 0x83) {
00625
00626
00627 AVStream *sub_st;
00628
00629 PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
00630 if (!sub_pes)
00631 return AVERROR(ENOMEM);
00632 memcpy(sub_pes, pes, sizeof(*sub_pes));
00633
00634 sub_st = avformat_new_stream(pes->stream, NULL);
00635 if (!sub_st) {
00636 av_free(sub_pes);
00637 return AVERROR(ENOMEM);
00638 }
00639
00640 sub_st->id = pes->pid;
00641 avpriv_set_pts_info(sub_st, 33, 1, 90000);
00642 sub_st->priv_data = sub_pes;
00643 sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00644 sub_st->codec->codec_id = CODEC_ID_AC3;
00645 sub_st->need_parsing = AVSTREAM_PARSE_FULL;
00646 sub_pes->sub_st = pes->sub_st = sub_st;
00647 }
00648 }
00649 if (st->codec->codec_id == CODEC_ID_NONE)
00650 mpegts_find_stream_type(st, pes->stream_type, MISC_types);
00651 if (st->codec->codec_id == CODEC_ID_NONE){
00652 st->codec->codec_id = old_codec_id;
00653 st->codec->codec_type= old_codec_type;
00654 }
00655
00656 return 0;
00657 }
00658
00659 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
00660 {
00661 av_init_packet(pkt);
00662
00663 pkt->destruct = av_destruct_packet;
00664 pkt->data = pes->buffer;
00665 pkt->size = pes->data_index;
00666
00667 if(pes->total_size != MAX_PES_PAYLOAD &&
00668 pes->pes_header_size + pes->data_index != pes->total_size + PES_START_SIZE) {
00669 av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
00670 pes->flags |= AV_PKT_FLAG_CORRUPT;
00671 }
00672 memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00673
00674
00675 if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
00676 pkt->stream_index = pes->sub_st->index;
00677 else
00678 pkt->stream_index = pes->st->index;
00679 pkt->pts = pes->pts;
00680 pkt->dts = pes->dts;
00681
00682 pkt->pos = pes->ts_packet_pos;
00683 pkt->flags = pes->flags;
00684
00685
00686 pes->pts = AV_NOPTS_VALUE;
00687 pes->dts = AV_NOPTS_VALUE;
00688 pes->buffer = NULL;
00689 pes->data_index = 0;
00690 pes->flags = 0;
00691 }
00692
00693 static uint64_t get_bits64(GetBitContext *gb, int bits)
00694 {
00695 uint64_t ret = 0;
00696
00697 if (get_bits_left(gb) < bits)
00698 return AV_NOPTS_VALUE;
00699 while (bits > 17) {
00700 ret <<= 17;
00701 ret |= get_bits(gb, 17);
00702 bits -= 17;
00703 }
00704 ret <<= bits;
00705 ret |= get_bits(gb, bits);
00706 return ret;
00707 }
00708
00709 static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
00710 {
00711 GetBitContext gb;
00712 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
00713 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
00714 int dts_flag = -1, cts_flag = -1;
00715 int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
00716
00717 init_get_bits(&gb, buf, buf_size*8);
00718
00719 if (sl->use_au_start)
00720 au_start_flag = get_bits1(&gb);
00721 if (sl->use_au_end)
00722 au_end_flag = get_bits1(&gb);
00723 if (!sl->use_au_start && !sl->use_au_end)
00724 au_start_flag = au_end_flag = 1;
00725 if (sl->ocr_len > 0)
00726 ocr_flag = get_bits1(&gb);
00727 if (sl->use_idle)
00728 idle_flag = get_bits1(&gb);
00729 if (sl->use_padding)
00730 padding_flag = get_bits1(&gb);
00731 if (padding_flag)
00732 padding_bits = get_bits(&gb, 3);
00733
00734 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
00735 if (sl->packet_seq_num_len)
00736 skip_bits_long(&gb, sl->packet_seq_num_len);
00737 if (sl->degr_prior_len)
00738 if (get_bits1(&gb))
00739 skip_bits(&gb, sl->degr_prior_len);
00740 if (ocr_flag)
00741 skip_bits_long(&gb, sl->ocr_len);
00742 if (au_start_flag) {
00743 if (sl->use_rand_acc_pt)
00744 get_bits1(&gb);
00745 if (sl->au_seq_num_len > 0)
00746 skip_bits_long(&gb, sl->au_seq_num_len);
00747 if (sl->use_timestamps) {
00748 dts_flag = get_bits1(&gb);
00749 cts_flag = get_bits1(&gb);
00750 }
00751 }
00752 if (sl->inst_bitrate_len)
00753 inst_bitrate_flag = get_bits1(&gb);
00754 if (dts_flag == 1)
00755 dts = get_bits64(&gb, sl->timestamp_len);
00756 if (cts_flag == 1)
00757 cts = get_bits64(&gb, sl->timestamp_len);
00758 if (sl->au_len > 0)
00759 skip_bits_long(&gb, sl->au_len);
00760 if (inst_bitrate_flag)
00761 skip_bits_long(&gb, sl->inst_bitrate_len);
00762 }
00763
00764 if (dts != AV_NOPTS_VALUE)
00765 pes->dts = dts;
00766 if (cts != AV_NOPTS_VALUE)
00767 pes->pts = cts;
00768
00769 if (sl->timestamp_len && sl->timestamp_res)
00770 avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
00771
00772 return (get_bits_count(&gb) + 7) >> 3;
00773 }
00774
00775
00776 static int mpegts_push_data(MpegTSFilter *filter,
00777 const uint8_t *buf, int buf_size, int is_start,
00778 int64_t pos)
00779 {
00780 PESContext *pes = filter->u.pes_filter.opaque;
00781 MpegTSContext *ts = pes->ts;
00782 const uint8_t *p;
00783 int len, code;
00784
00785 if(!ts->pkt)
00786 return 0;
00787
00788 if (is_start) {
00789 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
00790 new_pes_packet(pes, ts->pkt);
00791 ts->stop_parse = 1;
00792 }
00793 pes->state = MPEGTS_HEADER;
00794 pes->data_index = 0;
00795 pes->ts_packet_pos = pos;
00796 }
00797 p = buf;
00798 while (buf_size > 0) {
00799 switch(pes->state) {
00800 case MPEGTS_HEADER:
00801 len = PES_START_SIZE - pes->data_index;
00802 if (len > buf_size)
00803 len = buf_size;
00804 memcpy(pes->header + pes->data_index, p, len);
00805 pes->data_index += len;
00806 p += len;
00807 buf_size -= len;
00808 if (pes->data_index == PES_START_SIZE) {
00809
00810
00811 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
00812 pes->header[2] == 0x01) {
00813
00814 code = pes->header[3] | 0x100;
00815 av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
00816
00817 if ((pes->st && pes->st->discard == AVDISCARD_ALL) ||
00818 code == 0x1be)
00819 goto skip;
00820
00821
00822 if (!pes->st) {
00823 pes->st = avformat_new_stream(ts->stream, NULL);
00824 if (!pes->st)
00825 return AVERROR(ENOMEM);
00826 pes->st->id = pes->pid;
00827 mpegts_set_stream_info(pes->st, pes, 0, 0);
00828 }
00829
00830 pes->total_size = AV_RB16(pes->header + 4);
00831
00832
00833 if (!pes->total_size)
00834 pes->total_size = MAX_PES_PAYLOAD;
00835
00836
00837 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
00838 if (!pes->buffer)
00839 return AVERROR(ENOMEM);
00840
00841 if (code != 0x1bc && code != 0x1bf &&
00842 code != 0x1f0 && code != 0x1f1 &&
00843 code != 0x1ff && code != 0x1f2 &&
00844 code != 0x1f8) {
00845 pes->state = MPEGTS_PESHEADER;
00846 if (pes->st->codec->codec_id == CODEC_ID_NONE && !pes->st->request_probe) {
00847 av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
00848 pes->pid, pes->stream_type);
00849 pes->st->request_probe= 1;
00850 }
00851 } else {
00852 pes->state = MPEGTS_PAYLOAD;
00853 pes->data_index = 0;
00854 }
00855 } else {
00856
00857
00858 skip:
00859 pes->state = MPEGTS_SKIP;
00860 continue;
00861 }
00862 }
00863 break;
00864
00865
00866 case MPEGTS_PESHEADER:
00867 len = PES_HEADER_SIZE - pes->data_index;
00868 if (len < 0)
00869 return -1;
00870 if (len > buf_size)
00871 len = buf_size;
00872 memcpy(pes->header + pes->data_index, p, len);
00873 pes->data_index += len;
00874 p += len;
00875 buf_size -= len;
00876 if (pes->data_index == PES_HEADER_SIZE) {
00877 pes->pes_header_size = pes->header[8] + 9;
00878 pes->state = MPEGTS_PESHEADER_FILL;
00879 }
00880 break;
00881 case MPEGTS_PESHEADER_FILL:
00882 len = pes->pes_header_size - pes->data_index;
00883 if (len < 0)
00884 return -1;
00885 if (len > buf_size)
00886 len = buf_size;
00887 memcpy(pes->header + pes->data_index, p, len);
00888 pes->data_index += len;
00889 p += len;
00890 buf_size -= len;
00891 if (pes->data_index == pes->pes_header_size) {
00892 const uint8_t *r;
00893 unsigned int flags, pes_ext, skip;
00894
00895 flags = pes->header[7];
00896 r = pes->header + 9;
00897 pes->pts = AV_NOPTS_VALUE;
00898 pes->dts = AV_NOPTS_VALUE;
00899 if ((flags & 0xc0) == 0x80) {
00900 pes->dts = pes->pts = ff_parse_pes_pts(r);
00901 r += 5;
00902 } else if ((flags & 0xc0) == 0xc0) {
00903 pes->pts = ff_parse_pes_pts(r);
00904 r += 5;
00905 pes->dts = ff_parse_pes_pts(r);
00906 r += 5;
00907 }
00908 pes->extended_stream_id = -1;
00909 if (flags & 0x01) {
00910 pes_ext = *r++;
00911
00912 skip = (pes_ext >> 4) & 0xb;
00913 skip += skip & 0x9;
00914 r += skip;
00915 if ((pes_ext & 0x41) == 0x01 &&
00916 (r + 2) <= (pes->header + pes->pes_header_size)) {
00917
00918 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
00919 pes->extended_stream_id = r[1];
00920 }
00921 }
00922
00923
00924 pes->state = MPEGTS_PAYLOAD;
00925 pes->data_index = 0;
00926 if (pes->stream_type == 0x12 && buf_size > 0) {
00927 int sl_header_bytes = read_sl_header(pes, &pes->sl, p, buf_size);
00928 pes->pes_header_size += sl_header_bytes;
00929 p += sl_header_bytes;
00930 buf_size -= sl_header_bytes;
00931 }
00932 }
00933 break;
00934 case MPEGTS_PAYLOAD:
00935 if (buf_size > 0 && pes->buffer) {
00936 if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
00937 new_pes_packet(pes, ts->pkt);
00938 pes->total_size = MAX_PES_PAYLOAD;
00939 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
00940 if (!pes->buffer)
00941 return AVERROR(ENOMEM);
00942 ts->stop_parse = 1;
00943 } else if (pes->data_index == 0 && buf_size > pes->total_size) {
00944
00945
00946 buf_size = pes->total_size;
00947 }
00948 memcpy(pes->buffer+pes->data_index, p, buf_size);
00949 pes->data_index += buf_size;
00950 }
00951 buf_size = 0;
00952
00953
00954
00955
00956
00957 if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
00958 pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
00959 ts->stop_parse = 1;
00960 new_pes_packet(pes, ts->pkt);
00961 }
00962 break;
00963 case MPEGTS_SKIP:
00964 buf_size = 0;
00965 break;
00966 }
00967 }
00968
00969 return 0;
00970 }
00971
00972 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
00973 {
00974 MpegTSFilter *tss;
00975 PESContext *pes;
00976
00977
00978 pes = av_mallocz(sizeof(PESContext));
00979 if (!pes)
00980 return 0;
00981 pes->ts = ts;
00982 pes->stream = ts->stream;
00983 pes->pid = pid;
00984 pes->pcr_pid = pcr_pid;
00985 pes->state = MPEGTS_SKIP;
00986 pes->pts = AV_NOPTS_VALUE;
00987 pes->dts = AV_NOPTS_VALUE;
00988 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
00989 if (!tss) {
00990 av_free(pes);
00991 return 0;
00992 }
00993 return pes;
00994 }
00995
00996 #define MAX_LEVEL 4
00997 typedef struct {
00998 AVFormatContext *s;
00999 AVIOContext pb;
01000 Mp4Descr *descr;
01001 Mp4Descr *active_descr;
01002 int descr_count;
01003 int max_descr_count;
01004 int level;
01005 } MP4DescrParseContext;
01006
01007 static int init_MP4DescrParseContext(
01008 MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf,
01009 unsigned size, Mp4Descr *descr, int max_descr_count)
01010 {
01011 int ret;
01012 if (size > (1<<30))
01013 return AVERROR_INVALIDDATA;
01014
01015 if ((ret = ffio_init_context(&d->pb, (unsigned char*)buf, size, 0,
01016 NULL, NULL, NULL, NULL)) < 0)
01017 return ret;
01018
01019 d->s = s;
01020 d->level = 0;
01021 d->descr_count = 0;
01022 d->descr = descr;
01023 d->active_descr = NULL;
01024 d->max_descr_count = max_descr_count;
01025
01026 return 0;
01027 }
01028
01029 static void update_offsets(AVIOContext *pb, int64_t *off, int *len) {
01030 int64_t new_off = avio_tell(pb);
01031 (*len) -= new_off - *off;
01032 *off = new_off;
01033 }
01034
01035 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
01036 int target_tag);
01037
01038 static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
01039 {
01040 while (len > 0) {
01041 if (parse_mp4_descr(d, off, len, 0) < 0)
01042 return -1;
01043 update_offsets(&d->pb, &off, &len);
01044 }
01045 return 0;
01046 }
01047
01048 static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
01049 {
01050 avio_rb16(&d->pb);
01051 avio_r8(&d->pb);
01052 avio_r8(&d->pb);
01053 avio_r8(&d->pb);
01054 avio_r8(&d->pb);
01055 avio_r8(&d->pb);
01056 update_offsets(&d->pb, &off, &len);
01057 return parse_mp4_descr_arr(d, off, len);
01058 }
01059
01060 static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
01061 {
01062 int id_flags;
01063 if (len < 2)
01064 return 0;
01065 id_flags = avio_rb16(&d->pb);
01066 if (!(id_flags & 0x0020)) {
01067 update_offsets(&d->pb, &off, &len);
01068 return parse_mp4_descr_arr(d, off, len);
01069 } else {
01070 return 0;
01071 }
01072 }
01073
01074 static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
01075 {
01076 int es_id = 0;
01077 if (d->descr_count >= d->max_descr_count)
01078 return -1;
01079 ff_mp4_parse_es_descr(&d->pb, &es_id);
01080 d->active_descr = d->descr + (d->descr_count++);
01081
01082 d->active_descr->es_id = es_id;
01083 update_offsets(&d->pb, &off, &len);
01084 parse_mp4_descr(d, off, len, MP4DecConfigDescrTag);
01085 update_offsets(&d->pb, &off, &len);
01086 if (len > 0)
01087 parse_mp4_descr(d, off, len, MP4SLDescrTag);
01088 d->active_descr = NULL;
01089 return 0;
01090 }
01091
01092 static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len)
01093 {
01094 Mp4Descr *descr = d->active_descr;
01095 if (!descr)
01096 return -1;
01097 d->active_descr->dec_config_descr = av_malloc(len);
01098 if (!descr->dec_config_descr)
01099 return AVERROR(ENOMEM);
01100 descr->dec_config_descr_len = len;
01101 avio_read(&d->pb, descr->dec_config_descr, len);
01102 return 0;
01103 }
01104
01105 static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
01106 {
01107 Mp4Descr *descr = d->active_descr;
01108 int predefined;
01109 if (!descr)
01110 return -1;
01111
01112 predefined = avio_r8(&d->pb);
01113 if (!predefined) {
01114 int lengths;
01115 int flags = avio_r8(&d->pb);
01116 descr->sl.use_au_start = !!(flags & 0x80);
01117 descr->sl.use_au_end = !!(flags & 0x40);
01118 descr->sl.use_rand_acc_pt = !!(flags & 0x20);
01119 descr->sl.use_padding = !!(flags & 0x08);
01120 descr->sl.use_timestamps = !!(flags & 0x04);
01121 descr->sl.use_idle = !!(flags & 0x02);
01122 descr->sl.timestamp_res = avio_rb32(&d->pb);
01123 avio_rb32(&d->pb);
01124 descr->sl.timestamp_len = avio_r8(&d->pb);
01125 descr->sl.ocr_len = avio_r8(&d->pb);
01126 descr->sl.au_len = avio_r8(&d->pb);
01127 descr->sl.inst_bitrate_len = avio_r8(&d->pb);
01128 lengths = avio_rb16(&d->pb);
01129 descr->sl.degr_prior_len = lengths >> 12;
01130 descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
01131 descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
01132 } else {
01133 av_log_missing_feature(d->s, "Predefined SLConfigDescriptor", 0);
01134 }
01135 return 0;
01136 }
01137
01138 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
01139 int target_tag) {
01140 int tag;
01141 int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
01142 update_offsets(&d->pb, &off, &len);
01143 if (len < 0 || len1 > len || len1 <= 0) {
01144 av_log(d->s, AV_LOG_ERROR, "Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
01145 return -1;
01146 }
01147
01148 if (d->level++ >= MAX_LEVEL) {
01149 av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
01150 goto done;
01151 }
01152
01153 if (target_tag && tag != target_tag) {
01154 av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag, target_tag);
01155 goto done;
01156 }
01157
01158 switch (tag) {
01159 case MP4IODescrTag:
01160 parse_MP4IODescrTag(d, off, len1);
01161 break;
01162 case MP4ODescrTag:
01163 parse_MP4ODescrTag(d, off, len1);
01164 break;
01165 case MP4ESDescrTag:
01166 parse_MP4ESDescrTag(d, off, len1);
01167 break;
01168 case MP4DecConfigDescrTag:
01169 parse_MP4DecConfigDescrTag(d, off, len1);
01170 break;
01171 case MP4SLDescrTag:
01172 parse_MP4SLDescrTag(d, off, len1);
01173 break;
01174 }
01175
01176 done:
01177 d->level--;
01178 avio_seek(&d->pb, off + len1, SEEK_SET);
01179 return 0;
01180 }
01181
01182 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
01183 Mp4Descr *descr, int *descr_count, int max_descr_count)
01184 {
01185 MP4DescrParseContext d;
01186 if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
01187 return -1;
01188
01189 parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
01190
01191 *descr_count = d.descr_count;
01192 return 0;
01193 }
01194
01195 static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
01196 Mp4Descr *descr, int *descr_count, int max_descr_count)
01197 {
01198 MP4DescrParseContext d;
01199 if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
01200 return -1;
01201
01202 parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
01203
01204 *descr_count = d.descr_count;
01205 return 0;
01206 }
01207
01208 static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01209 {
01210 MpegTSContext *ts = filter->u.section_filter.opaque;
01211 SectionHeader h;
01212 const uint8_t *p, *p_end;
01213 AVIOContext pb;
01214 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
01215 int mp4_descr_count = 0;
01216 int i, pid;
01217 AVFormatContext *s = ts->stream;
01218
01219 p_end = section + section_len - 4;
01220 p = section;
01221 if (parse_section_header(&h, &p, p_end) < 0)
01222 return;
01223 if (h.tid != M4OD_TID)
01224 return;
01225
01226 mp4_read_od(s, p, (unsigned)(p_end - p), mp4_descr, &mp4_descr_count, MAX_MP4_DESCR_COUNT);
01227
01228 for (pid = 0; pid < NB_PID_MAX; pid++) {
01229 if (!ts->pids[pid])
01230 continue;
01231 for (i = 0; i < mp4_descr_count; i++) {
01232 PESContext *pes;
01233 AVStream *st;
01234 if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
01235 continue;
01236 if (!(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES)) {
01237 av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
01238 continue;
01239 }
01240 pes = ts->pids[pid]->u.pes_filter.opaque;
01241 st = pes->st;
01242 if (!st) {
01243 continue;
01244 }
01245
01246 pes->sl = mp4_descr[i].sl;
01247
01248 ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
01249 mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
01250 ff_mp4_read_dec_config_descr(s, st, &pb);
01251 if (st->codec->codec_id == CODEC_ID_AAC &&
01252 st->codec->extradata_size > 0)
01253 st->need_parsing = 0;
01254 if (st->codec->codec_id == CODEC_ID_H264 &&
01255 st->codec->extradata_size > 0)
01256 st->need_parsing = 0;
01257
01258 if (st->codec->codec_id <= CODEC_ID_NONE) {
01259 } else if (st->codec->codec_id < CODEC_ID_FIRST_AUDIO) {
01260 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01261 } else if (st->codec->codec_id < CODEC_ID_FIRST_SUBTITLE) {
01262 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01263 } else if (st->codec->codec_id < CODEC_ID_FIRST_UNKNOWN) {
01264 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
01265 }
01266 }
01267 }
01268 for (i = 0; i < mp4_descr_count; i++)
01269 av_free(mp4_descr[i].dec_config_descr);
01270 }
01271
01272 int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
01273 const uint8_t **pp, const uint8_t *desc_list_end,
01274 Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
01275 MpegTSContext *ts)
01276 {
01277 const uint8_t *desc_end;
01278 int desc_len, desc_tag, desc_es_id;
01279 char language[252];
01280 int i;
01281
01282 desc_tag = get8(pp, desc_list_end);
01283 if (desc_tag < 0)
01284 return -1;
01285 desc_len = get8(pp, desc_list_end);
01286 if (desc_len < 0)
01287 return -1;
01288 desc_end = *pp + desc_len;
01289 if (desc_end > desc_list_end)
01290 return -1;
01291
01292 av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
01293
01294 if (st->codec->codec_id == CODEC_ID_NONE &&
01295 stream_type == STREAM_TYPE_PRIVATE_DATA)
01296 mpegts_find_stream_type(st, desc_tag, DESC_types);
01297
01298 switch(desc_tag) {
01299 case 0x1E:
01300 desc_es_id = get16(pp, desc_end);
01301 if (ts && ts->pids[pid])
01302 ts->pids[pid]->es_id = desc_es_id;
01303 for (i = 0; i < mp4_descr_count; i++)
01304 if (mp4_descr[i].dec_config_descr_len &&
01305 mp4_descr[i].es_id == desc_es_id) {
01306 AVIOContext pb;
01307 ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
01308 mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
01309 ff_mp4_read_dec_config_descr(fc, st, &pb);
01310 if (st->codec->codec_id == CODEC_ID_AAC &&
01311 st->codec->extradata_size > 0)
01312 st->need_parsing = 0;
01313 if (st->codec->codec_id == CODEC_ID_MPEG4SYSTEMS)
01314 mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
01315 }
01316 break;
01317 case 0x1F:
01318 get16(pp, desc_end);
01319 if (mp4_descr_count > 0 && (st->codec->codec_id == CODEC_ID_AAC_LATM || st->request_probe>0) &&
01320 mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
01321 AVIOContext pb;
01322 ffio_init_context(&pb, mp4_descr->dec_config_descr,
01323 mp4_descr->dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
01324 ff_mp4_read_dec_config_descr(fc, st, &pb);
01325 if (st->codec->codec_id == CODEC_ID_AAC &&
01326 st->codec->extradata_size > 0){
01327 st->request_probe= st->need_parsing = 0;
01328 st->codec->codec_type= AVMEDIA_TYPE_AUDIO;
01329 }
01330 }
01331 break;
01332 case 0x56:
01333 language[0] = get8(pp, desc_end);
01334 language[1] = get8(pp, desc_end);
01335 language[2] = get8(pp, desc_end);
01336 language[3] = 0;
01337 av_dict_set(&st->metadata, "language", language, 0);
01338 break;
01339 case 0x59:
01340 language[0] = get8(pp, desc_end);
01341 language[1] = get8(pp, desc_end);
01342 language[2] = get8(pp, desc_end);
01343 language[3] = 0;
01344
01345 switch(get8(pp, desc_end)) {
01346 case 0x20:
01347 case 0x21:
01348 case 0x22:
01349 case 0x23:
01350 case 0x24:
01351 case 0x25:
01352 st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
01353 break;
01354 }
01355 if (st->codec->extradata) {
01356 if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
01357 av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
01358 } else {
01359 st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
01360 if (st->codec->extradata) {
01361 st->codec->extradata_size = 4;
01362 memcpy(st->codec->extradata, *pp, 4);
01363 }
01364 }
01365 *pp += 4;
01366 av_dict_set(&st->metadata, "language", language, 0);
01367 break;
01368 case 0x0a:
01369 for (i = 0; i + 4 <= desc_len; i += 4) {
01370 language[i + 0] = get8(pp, desc_end);
01371 language[i + 1] = get8(pp, desc_end);
01372 language[i + 2] = get8(pp, desc_end);
01373 language[i + 3] = ',';
01374 switch (get8(pp, desc_end)) {
01375 case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
01376 case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
01377 case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
01378 }
01379 }
01380 if (i) {
01381 language[i - 1] = 0;
01382 av_dict_set(&st->metadata, "language", language, 0);
01383 }
01384 break;
01385 case 0x05:
01386 st->codec->codec_tag = bytestream_get_le32(pp);
01387 av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
01388 if (st->codec->codec_id == CODEC_ID_NONE)
01389 mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
01390 break;
01391 case 0x52:
01392 st->stream_identifier = 1 + get8(pp, desc_end);
01393 break;
01394 default:
01395 break;
01396 }
01397 *pp = desc_end;
01398 return 0;
01399 }
01400
01401 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01402 {
01403 MpegTSContext *ts = filter->u.section_filter.opaque;
01404 SectionHeader h1, *h = &h1;
01405 PESContext *pes;
01406 AVStream *st;
01407 const uint8_t *p, *p_end, *desc_list_end;
01408 int program_info_length, pcr_pid, pid, stream_type;
01409 int desc_list_len;
01410 uint32_t prog_reg_desc = 0;
01411
01412 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
01413 int mp4_descr_count = 0;
01414 int i;
01415
01416 av_dlog(ts->stream, "PMT: len %i\n", section_len);
01417 hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
01418
01419 p_end = section + section_len - 4;
01420 p = section;
01421 if (parse_section_header(h, &p, p_end) < 0)
01422 return;
01423
01424 av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
01425 h->id, h->sec_num, h->last_sec_num);
01426
01427 if (h->tid != PMT_TID)
01428 return;
01429
01430 clear_program(ts, h->id);
01431 pcr_pid = get16(&p, p_end);
01432 if (pcr_pid < 0)
01433 return;
01434 pcr_pid &= 0x1fff;
01435 add_pid_to_pmt(ts, h->id, pcr_pid);
01436 set_pcr_pid(ts->stream, h->id, pcr_pid);
01437
01438 av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
01439
01440 program_info_length = get16(&p, p_end);
01441 if (program_info_length < 0)
01442 return;
01443 program_info_length &= 0xfff;
01444 while(program_info_length >= 2) {
01445 uint8_t tag, len;
01446 tag = get8(&p, p_end);
01447 len = get8(&p, p_end);
01448
01449 av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
01450
01451 if(len > program_info_length - 2)
01452
01453 break;
01454 program_info_length -= len + 2;
01455 if (tag == 0x1d) {
01456 get8(&p, p_end);
01457 get8(&p, p_end);
01458 len -= 2;
01459 mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
01460 &mp4_descr_count, MAX_MP4_DESCR_COUNT);
01461 } else if (tag == 0x05 && len >= 4) {
01462 prog_reg_desc = bytestream_get_le32(&p);
01463 len -= 4;
01464 }
01465 p += len;
01466 }
01467 p += program_info_length;
01468 if (p >= p_end)
01469 goto out;
01470
01471
01472 if (!ts->stream->nb_streams)
01473 ts->stop_parse = 2;
01474
01475 for(;;) {
01476 st = 0;
01477 pes = NULL;
01478 stream_type = get8(&p, p_end);
01479 if (stream_type < 0)
01480 break;
01481 pid = get16(&p, p_end);
01482 if (pid < 0)
01483 break;
01484 pid &= 0x1fff;
01485
01486
01487 if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
01488 pes = ts->pids[pid]->u.pes_filter.opaque;
01489 if (!pes->st) {
01490 pes->st = avformat_new_stream(pes->stream, NULL);
01491 pes->st->id = pes->pid;
01492 }
01493 st = pes->st;
01494 } else if (stream_type != 0x13) {
01495 if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]);
01496 pes = add_pes_stream(ts, pid, pcr_pid);
01497 if (pes) {
01498 st = avformat_new_stream(pes->stream, NULL);
01499 st->id = pes->pid;
01500 }
01501 } else {
01502 int idx = ff_find_stream_index(ts->stream, pid);
01503 if (idx >= 0) {
01504 st = ts->stream->streams[idx];
01505 } else {
01506 st = avformat_new_stream(ts->stream, NULL);
01507 st->id = pid;
01508 st->codec->codec_type = AVMEDIA_TYPE_DATA;
01509 }
01510 }
01511
01512 if (!st)
01513 goto out;
01514
01515 if (pes && !pes->stream_type)
01516 mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
01517
01518 add_pid_to_pmt(ts, h->id, pid);
01519
01520 ff_program_add_stream_index(ts->stream, h->id, st->index);
01521
01522 desc_list_len = get16(&p, p_end);
01523 if (desc_list_len < 0)
01524 break;
01525 desc_list_len &= 0xfff;
01526 desc_list_end = p + desc_list_len;
01527 if (desc_list_end > p_end)
01528 break;
01529 for(;;) {
01530 if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
01531 mp4_descr, mp4_descr_count, pid, ts) < 0)
01532 break;
01533
01534 if (pes && prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
01535 ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
01536 pes->sub_st->codec->codec_tag = st->codec->codec_tag;
01537 }
01538 }
01539 p = desc_list_end;
01540 }
01541
01542 out:
01543 for (i = 0; i < mp4_descr_count; i++)
01544 av_free(mp4_descr[i].dec_config_descr);
01545 }
01546
01547 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01548 {
01549 MpegTSContext *ts = filter->u.section_filter.opaque;
01550 SectionHeader h1, *h = &h1;
01551 const uint8_t *p, *p_end;
01552 int sid, pmt_pid;
01553 AVProgram *program;
01554
01555 av_dlog(ts->stream, "PAT:\n");
01556 hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
01557
01558 p_end = section + section_len - 4;
01559 p = section;
01560 if (parse_section_header(h, &p, p_end) < 0)
01561 return;
01562 if (h->tid != PAT_TID)
01563 return;
01564
01565 ts->stream->ts_id = h->id;
01566
01567 clear_programs(ts);
01568 for(;;) {
01569 sid = get16(&p, p_end);
01570 if (sid < 0)
01571 break;
01572 pmt_pid = get16(&p, p_end);
01573 if (pmt_pid < 0)
01574 break;
01575 pmt_pid &= 0x1fff;
01576
01577 av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
01578
01579 if (sid == 0x0000) {
01580
01581 } else {
01582 program = av_new_program(ts->stream, sid);
01583 program->program_num = sid;
01584 program->pmt_pid = pmt_pid;
01585 if (ts->pids[pmt_pid])
01586 mpegts_close_filter(ts, ts->pids[pmt_pid]);
01587 mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
01588 add_pat_entry(ts, sid);
01589 add_pid_to_pmt(ts, sid, 0);
01590 add_pid_to_pmt(ts, sid, pmt_pid);
01591 }
01592 }
01593 }
01594
01595 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01596 {
01597 MpegTSContext *ts = filter->u.section_filter.opaque;
01598 SectionHeader h1, *h = &h1;
01599 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
01600 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
01601 char *name, *provider_name;
01602
01603 av_dlog(ts->stream, "SDT:\n");
01604 hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
01605
01606 p_end = section + section_len - 4;
01607 p = section;
01608 if (parse_section_header(h, &p, p_end) < 0)
01609 return;
01610 if (h->tid != SDT_TID)
01611 return;
01612 onid = get16(&p, p_end);
01613 if (onid < 0)
01614 return;
01615 val = get8(&p, p_end);
01616 if (val < 0)
01617 return;
01618 for(;;) {
01619 sid = get16(&p, p_end);
01620 if (sid < 0)
01621 break;
01622 val = get8(&p, p_end);
01623 if (val < 0)
01624 break;
01625 desc_list_len = get16(&p, p_end);
01626 if (desc_list_len < 0)
01627 break;
01628 desc_list_len &= 0xfff;
01629 desc_list_end = p + desc_list_len;
01630 if (desc_list_end > p_end)
01631 break;
01632 for(;;) {
01633 desc_tag = get8(&p, desc_list_end);
01634 if (desc_tag < 0)
01635 break;
01636 desc_len = get8(&p, desc_list_end);
01637 desc_end = p + desc_len;
01638 if (desc_end > desc_list_end)
01639 break;
01640
01641 av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
01642 desc_tag, desc_len);
01643
01644 switch(desc_tag) {
01645 case 0x48:
01646 service_type = get8(&p, p_end);
01647 if (service_type < 0)
01648 break;
01649 provider_name = getstr8(&p, p_end);
01650 if (!provider_name)
01651 break;
01652 name = getstr8(&p, p_end);
01653 if (name) {
01654 AVProgram *program = av_new_program(ts->stream, sid);
01655 if(program) {
01656 av_dict_set(&program->metadata, "service_name", name, 0);
01657 av_dict_set(&program->metadata, "service_provider", provider_name, 0);
01658 }
01659 }
01660 av_free(name);
01661 av_free(provider_name);
01662 break;
01663 default:
01664 break;
01665 }
01666 p = desc_end;
01667 }
01668 p = desc_list_end;
01669 }
01670 }
01671
01672
01673 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
01674 {
01675 AVFormatContext *s = ts->stream;
01676 MpegTSFilter *tss;
01677 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
01678 has_adaptation, has_payload;
01679 const uint8_t *p, *p_end;
01680 int64_t pos;
01681
01682 pid = AV_RB16(packet + 1) & 0x1fff;
01683 if(pid && discard_pid(ts, pid))
01684 return 0;
01685 is_start = packet[1] & 0x40;
01686 tss = ts->pids[pid];
01687 if (ts->auto_guess && tss == NULL && is_start) {
01688 add_pes_stream(ts, pid, -1);
01689 tss = ts->pids[pid];
01690 }
01691 if (!tss)
01692 return 0;
01693
01694 afc = (packet[3] >> 4) & 3;
01695 if (afc == 0)
01696 return 0;
01697 has_adaptation = afc & 2;
01698 has_payload = afc & 1;
01699 is_discontinuity = has_adaptation
01700 && packet[4] != 0
01701 && (packet[5] & 0x80);
01702
01703
01704 cc = (packet[3] & 0xf);
01705 expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
01706 cc_ok = pid == 0x1FFF
01707 || is_discontinuity
01708 || tss->last_cc < 0
01709 || expected_cc == cc;
01710
01711 tss->last_cc = cc;
01712 if (!cc_ok) {
01713 av_log(ts->stream, AV_LOG_DEBUG,
01714 "Continuity check failed for pid %d expected %d got %d\n",
01715 pid, expected_cc, cc);
01716 if(tss->type == MPEGTS_PES) {
01717 PESContext *pc = tss->u.pes_filter.opaque;
01718 pc->flags |= AV_PKT_FLAG_CORRUPT;
01719 }
01720 }
01721
01722 if (!has_payload)
01723 return 0;
01724 p = packet + 4;
01725 if (has_adaptation) {
01726
01727 p += p[0] + 1;
01728 }
01729
01730 p_end = packet + TS_PACKET_SIZE;
01731 if (p >= p_end)
01732 return 0;
01733
01734 pos = avio_tell(ts->stream->pb);
01735 ts->pos47= pos % ts->raw_packet_size;
01736
01737 if (tss->type == MPEGTS_SECTION) {
01738 if (is_start) {
01739
01740 len = *p++;
01741 if (p + len > p_end)
01742 return 0;
01743 if (len && cc_ok) {
01744
01745 write_section_data(s, tss,
01746 p, len, 0);
01747
01748 if (!ts->pids[pid])
01749 return 0;
01750 }
01751 p += len;
01752 if (p < p_end) {
01753 write_section_data(s, tss,
01754 p, p_end - p, 1);
01755 }
01756 } else {
01757 if (cc_ok) {
01758 write_section_data(s, tss,
01759 p, p_end - p, 0);
01760 }
01761 }
01762 } else {
01763 int ret;
01764
01765 if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
01766 pos - ts->raw_packet_size)) < 0)
01767 return ret;
01768 }
01769
01770 return 0;
01771 }
01772
01773
01774
01775 static int mpegts_resync(AVFormatContext *s)
01776 {
01777 AVIOContext *pb = s->pb;
01778 int c, i;
01779
01780 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
01781 c = avio_r8(pb);
01782 if (url_feof(pb))
01783 return -1;
01784 if (c == 0x47) {
01785 avio_seek(pb, -1, SEEK_CUR);
01786 return 0;
01787 }
01788 }
01789 av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
01790
01791 return -1;
01792 }
01793
01794
01795 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
01796 {
01797 AVIOContext *pb = s->pb;
01798 int skip, len;
01799
01800 for(;;) {
01801 len = avio_read(pb, buf, TS_PACKET_SIZE);
01802 if (len != TS_PACKET_SIZE)
01803 return len < 0 ? len : AVERROR_EOF;
01804
01805 if (buf[0] != 0x47) {
01806
01807 avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
01808 if (mpegts_resync(s) < 0)
01809 return AVERROR(EAGAIN);
01810 else
01811 continue;
01812 } else {
01813 skip = raw_packet_size - TS_PACKET_SIZE;
01814 if (skip > 0)
01815 avio_skip(pb, skip);
01816 break;
01817 }
01818 }
01819 return 0;
01820 }
01821
01822 static int handle_packets(MpegTSContext *ts, int nb_packets)
01823 {
01824 AVFormatContext *s = ts->stream;
01825 uint8_t packet[TS_PACKET_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
01826 int packet_num, ret = 0;
01827
01828 if (avio_tell(s->pb) != ts->last_pos) {
01829 int i;
01830 av_dlog(ts->stream, "Skipping after seek\n");
01831
01832 for (i = 0; i < NB_PID_MAX; i++) {
01833 if (ts->pids[i]) {
01834 if (ts->pids[i]->type == MPEGTS_PES) {
01835 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
01836 av_freep(&pes->buffer);
01837 pes->data_index = 0;
01838 pes->state = MPEGTS_SKIP;
01839 }
01840 ts->pids[i]->last_cc = -1;
01841 }
01842 }
01843 }
01844
01845 ts->stop_parse = 0;
01846 packet_num = 0;
01847 memset(packet + TS_PACKET_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
01848 for(;;) {
01849 packet_num++;
01850 if (nb_packets != 0 && packet_num >= nb_packets ||
01851 ts->stop_parse > 1) {
01852 ret = AVERROR(EAGAIN);
01853 break;
01854 }
01855 if (ts->stop_parse > 0)
01856 break;
01857
01858 ret = read_packet(s, packet, ts->raw_packet_size);
01859 if (ret != 0)
01860 break;
01861 ret = handle_packet(ts, packet);
01862 if (ret != 0)
01863 break;
01864 }
01865 ts->last_pos = avio_tell(s->pb);
01866 return ret;
01867 }
01868
01869 static int mpegts_probe(AVProbeData *p)
01870 {
01871 #if 1
01872 const int size= p->buf_size;
01873 int score, fec_score, dvhs_score;
01874 int check_count= size / TS_FEC_PACKET_SIZE;
01875 #define CHECK_COUNT 10
01876
01877 if (check_count < CHECK_COUNT)
01878 return -1;
01879
01880 score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
01881 dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
01882 fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
01883
01884
01885
01886 if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
01887 else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
01888 else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
01889 else return -1;
01890 #else
01891
01892 if (av_match_ext(p->filename, "ts"))
01893 return AVPROBE_SCORE_MAX;
01894 else
01895 return 0;
01896 #endif
01897 }
01898
01899
01900
01901 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
01902 const uint8_t *packet)
01903 {
01904 int afc, len, flags;
01905 const uint8_t *p;
01906 unsigned int v;
01907
01908 afc = (packet[3] >> 4) & 3;
01909 if (afc <= 1)
01910 return -1;
01911 p = packet + 4;
01912 len = p[0];
01913 p++;
01914 if (len == 0)
01915 return -1;
01916 flags = *p++;
01917 len--;
01918 if (!(flags & 0x10))
01919 return -1;
01920 if (len < 6)
01921 return -1;
01922 v = AV_RB32(p);
01923 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
01924 *ppcr_low = ((p[4] & 1) << 8) | p[5];
01925 return 0;
01926 }
01927
01928 static int mpegts_read_header(AVFormatContext *s)
01929 {
01930 MpegTSContext *ts = s->priv_data;
01931 AVIOContext *pb = s->pb;
01932 uint8_t buf[8*1024]={0};
01933 int len;
01934 int64_t pos;
01935
01936
01937 pos = avio_tell(pb);
01938 len = avio_read(pb, buf, sizeof(buf));
01939 ts->raw_packet_size = get_packet_size(buf, len);
01940 if (ts->raw_packet_size <= 0) {
01941 av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n");
01942 ts->raw_packet_size = TS_PACKET_SIZE;
01943 }
01944 ts->stream = s;
01945 ts->auto_guess = 0;
01946
01947 if (s->iformat == &ff_mpegts_demuxer) {
01948
01949
01950
01951
01952
01953
01954 if (avio_seek(pb, pos, SEEK_SET) < 0)
01955 av_log(s, pb->seekable ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n");
01956
01957 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
01958
01959 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
01960
01961 handle_packets(ts, s->probesize / ts->raw_packet_size);
01962
01963
01964 ts->auto_guess = 1;
01965
01966 av_dlog(ts->stream, "tuning done\n");
01967
01968 s->ctx_flags |= AVFMTCTX_NOHEADER;
01969 } else {
01970 AVStream *st;
01971 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
01972 int64_t pcrs[2], pcr_h;
01973 int packet_count[2];
01974 uint8_t packet[TS_PACKET_SIZE];
01975
01976
01977
01978 st = avformat_new_stream(s, NULL);
01979 if (!st)
01980 goto fail;
01981 avpriv_set_pts_info(st, 60, 1, 27000000);
01982 st->codec->codec_type = AVMEDIA_TYPE_DATA;
01983 st->codec->codec_id = CODEC_ID_MPEG2TS;
01984
01985
01986 pcr_pid = -1;
01987 nb_pcrs = 0;
01988 nb_packets = 0;
01989 for(;;) {
01990 ret = read_packet(s, packet, ts->raw_packet_size);
01991 if (ret < 0)
01992 return -1;
01993 pid = AV_RB16(packet + 1) & 0x1fff;
01994 if ((pcr_pid == -1 || pcr_pid == pid) &&
01995 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
01996 pcr_pid = pid;
01997 packet_count[nb_pcrs] = nb_packets;
01998 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
01999 nb_pcrs++;
02000 if (nb_pcrs >= 2)
02001 break;
02002 }
02003 nb_packets++;
02004 }
02005
02006
02007
02008 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
02009 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
02010 s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
02011 st->codec->bit_rate = s->bit_rate;
02012 st->start_time = ts->cur_pcr;
02013 av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n",
02014 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
02015 }
02016
02017 avio_seek(pb, pos, SEEK_SET);
02018 return 0;
02019 fail:
02020 return -1;
02021 }
02022
02023 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
02024
02025 static int mpegts_raw_read_packet(AVFormatContext *s,
02026 AVPacket *pkt)
02027 {
02028 MpegTSContext *ts = s->priv_data;
02029 int ret, i;
02030 int64_t pcr_h, next_pcr_h, pos;
02031 int pcr_l, next_pcr_l;
02032 uint8_t pcr_buf[12];
02033
02034 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
02035 return AVERROR(ENOMEM);
02036 pkt->pos= avio_tell(s->pb);
02037 ret = read_packet(s, pkt->data, ts->raw_packet_size);
02038 if (ret < 0) {
02039 av_free_packet(pkt);
02040 return ret;
02041 }
02042 if (ts->mpeg2ts_compute_pcr) {
02043
02044 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
02045
02046 pos = avio_tell(s->pb);
02047 for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
02048 avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
02049 avio_read(s->pb, pcr_buf, 12);
02050 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
02051
02052 ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
02053 (i + 1);
02054 break;
02055 }
02056 }
02057 avio_seek(s->pb, pos, SEEK_SET);
02058
02059 ts->cur_pcr = pcr_h * 300 + pcr_l;
02060 }
02061 pkt->pts = ts->cur_pcr;
02062 pkt->duration = ts->pcr_incr;
02063 ts->cur_pcr += ts->pcr_incr;
02064 }
02065 pkt->stream_index = 0;
02066 return 0;
02067 }
02068
02069 static int mpegts_read_packet(AVFormatContext *s,
02070 AVPacket *pkt)
02071 {
02072 MpegTSContext *ts = s->priv_data;
02073 int ret, i;
02074
02075 pkt->size = -1;
02076 ts->pkt = pkt;
02077 ret = handle_packets(ts, 0);
02078 if (ret < 0) {
02079
02080 for (i = 0; i < NB_PID_MAX; i++) {
02081 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
02082 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
02083 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
02084 new_pes_packet(pes, pkt);
02085 pes->state = MPEGTS_SKIP;
02086 ret = 0;
02087 break;
02088 }
02089 }
02090 }
02091 }
02092
02093 if (!ret && pkt->size < 0)
02094 ret = AVERROR(EINTR);
02095 return ret;
02096 }
02097
02098 static int mpegts_read_close(AVFormatContext *s)
02099 {
02100 MpegTSContext *ts = s->priv_data;
02101 int i;
02102
02103 clear_programs(ts);
02104
02105 for(i=0;i<NB_PID_MAX;i++)
02106 if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
02107
02108 return 0;
02109 }
02110
02111 static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
02112 int64_t *ppos, int64_t pos_limit)
02113 {
02114 MpegTSContext *ts = s->priv_data;
02115 int64_t pos, timestamp;
02116 uint8_t buf[TS_PACKET_SIZE];
02117 int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
02118 pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
02119 while(pos < pos_limit) {
02120 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
02121 return AV_NOPTS_VALUE;
02122 if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
02123 return AV_NOPTS_VALUE;
02124 if (buf[0] != 0x47) {
02125 if (mpegts_resync(s) < 0)
02126 return AV_NOPTS_VALUE;
02127 pos = avio_tell(s->pb);
02128 continue;
02129 }
02130 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
02131 parse_pcr(×tamp, &pcr_l, buf) == 0) {
02132 *ppos = pos;
02133 return timestamp;
02134 }
02135 pos += ts->raw_packet_size;
02136 }
02137
02138 return AV_NOPTS_VALUE;
02139 }
02140
02141 static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index,
02142 int64_t *ppos, int64_t pos_limit)
02143 {
02144 MpegTSContext *ts = s->priv_data;
02145 int64_t pos;
02146 pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
02147 ff_read_frame_flush(s);
02148 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
02149 return AV_NOPTS_VALUE;
02150 while(pos < pos_limit) {
02151 int ret;
02152 AVPacket pkt;
02153 av_init_packet(&pkt);
02154 ret= av_read_frame(s, &pkt);
02155 if(ret < 0)
02156 return AV_NOPTS_VALUE;
02157 av_free_packet(&pkt);
02158 if(pkt.dts != AV_NOPTS_VALUE && pkt.pos >= 0){
02159 ff_reduce_index(s, pkt.stream_index);
02160 av_add_index_entry(s->streams[pkt.stream_index], pkt.pos, pkt.dts, 0, 0, AVINDEX_KEYFRAME );
02161 if(pkt.stream_index == stream_index){
02162 *ppos= pkt.pos;
02163 return pkt.dts;
02164 }
02165 }
02166 pos = pkt.pos;
02167 }
02168
02169 return AV_NOPTS_VALUE;
02170 }
02171
02172 #ifdef USE_SYNCPOINT_SEARCH
02173
02174 static int read_seek2(AVFormatContext *s,
02175 int stream_index,
02176 int64_t min_ts,
02177 int64_t target_ts,
02178 int64_t max_ts,
02179 int flags)
02180 {
02181 int64_t pos;
02182
02183 int64_t ts_ret, ts_adj;
02184 int stream_index_gen_search;
02185 AVStream *st;
02186 AVParserState *backup;
02187
02188 backup = ff_store_parser_state(s);
02189
02190
02191 flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
02192 AVSEEK_FLAG_BACKWARD : 0;
02193
02194 if (flags & AVSEEK_FLAG_BYTE) {
02195
02196 pos = target_ts;
02197 } else {
02198
02199 if (stream_index < 0) {
02200 stream_index_gen_search = av_find_default_stream_index(s);
02201 if (stream_index_gen_search < 0) {
02202 ff_restore_parser_state(s, backup);
02203 return -1;
02204 }
02205
02206 st = s->streams[stream_index_gen_search];
02207
02208 ts_adj = av_rescale(target_ts,
02209 st->time_base.den,
02210 AV_TIME_BASE * (int64_t)st->time_base.num);
02211 } else {
02212 ts_adj = target_ts;
02213 stream_index_gen_search = stream_index;
02214 }
02215 pos = ff_gen_search(s, stream_index_gen_search, ts_adj,
02216 0, INT64_MAX, -1,
02217 AV_NOPTS_VALUE,
02218 AV_NOPTS_VALUE,
02219 flags, &ts_ret, mpegts_get_pcr);
02220 if (pos < 0) {
02221 ff_restore_parser_state(s, backup);
02222 return -1;
02223 }
02224 }
02225
02226
02227 if (ff_gen_syncpoint_search(s, stream_index, pos,
02228 min_ts, target_ts, max_ts,
02229 flags) < 0) {
02230 ff_restore_parser_state(s, backup);
02231 return -1;
02232 }
02233
02234 ff_free_parser_state(s, backup);
02235 return 0;
02236 }
02237
02238 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
02239 {
02240 int ret;
02241 if (flags & AVSEEK_FLAG_BACKWARD) {
02242 flags &= ~AVSEEK_FLAG_BACKWARD;
02243 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
02244 if (ret < 0)
02245
02246 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
02247 } else {
02248 ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
02249 if (ret < 0)
02250
02251 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
02252 }
02253 return ret;
02254 }
02255
02256 #endif
02257
02258
02259
02260
02261 MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
02262 {
02263 MpegTSContext *ts;
02264
02265 ts = av_mallocz(sizeof(MpegTSContext));
02266 if (!ts)
02267 return NULL;
02268
02269 ts->raw_packet_size = TS_PACKET_SIZE;
02270 ts->stream = s;
02271 ts->auto_guess = 1;
02272 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
02273 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
02274
02275 return ts;
02276 }
02277
02278
02279
02280 int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
02281 const uint8_t *buf, int len)
02282 {
02283 int len1;
02284
02285 len1 = len;
02286 ts->pkt = pkt;
02287 for(;;) {
02288 ts->stop_parse = 0;
02289 if (len < TS_PACKET_SIZE)
02290 return -1;
02291 if (buf[0] != 0x47) {
02292 buf++;
02293 len--;
02294 } else {
02295 handle_packet(ts, buf);
02296 buf += TS_PACKET_SIZE;
02297 len -= TS_PACKET_SIZE;
02298 if (ts->stop_parse == 1)
02299 break;
02300 }
02301 }
02302 return len1 - len;
02303 }
02304
02305 void ff_mpegts_parse_close(MpegTSContext *ts)
02306 {
02307 int i;
02308
02309 for(i=0;i<NB_PID_MAX;i++)
02310 av_free(ts->pids[i]);
02311 av_free(ts);
02312 }
02313
02314 AVInputFormat ff_mpegts_demuxer = {
02315 .name = "mpegts",
02316 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
02317 .priv_data_size = sizeof(MpegTSContext),
02318 .read_probe = mpegts_probe,
02319 .read_header = mpegts_read_header,
02320 .read_packet = mpegts_read_packet,
02321 .read_close = mpegts_read_close,
02322 .read_timestamp = mpegts_get_dts,
02323 .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
02324 #ifdef USE_SYNCPOINT_SEARCH
02325 .read_seek2 = read_seek2,
02326 #endif
02327 };
02328
02329 AVInputFormat ff_mpegtsraw_demuxer = {
02330 .name = "mpegtsraw",
02331 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
02332 .priv_data_size = sizeof(MpegTSContext),
02333 .read_header = mpegts_read_header,
02334 .read_packet = mpegts_raw_read_packet,
02335 .read_close = mpegts_read_close,
02336 .read_timestamp = mpegts_get_dts,
02337 .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
02338 #ifdef USE_SYNCPOINT_SEARCH
02339 .read_seek2 = read_seek2,
02340 #endif
02341 .priv_class = &mpegtsraw_class,
02342 };