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 while (bits > 17) {
00697 ret <<= 17;
00698 ret |= get_bits(gb, 17);
00699 bits -= 17;
00700 }
00701 ret <<= bits;
00702 ret |= get_bits(gb, bits);
00703 return ret;
00704 }
00705
00706 static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
00707 {
00708 GetBitContext gb;
00709 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
00710 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
00711 int dts_flag = -1, cts_flag = -1;
00712 int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
00713 init_get_bits(&gb, buf, buf_size*8);
00714
00715 if (sl->use_au_start)
00716 au_start_flag = get_bits1(&gb);
00717 if (sl->use_au_end)
00718 au_end_flag = get_bits1(&gb);
00719 if (!sl->use_au_start && !sl->use_au_end)
00720 au_start_flag = au_end_flag = 1;
00721 if (sl->ocr_len > 0)
00722 ocr_flag = get_bits1(&gb);
00723 if (sl->use_idle)
00724 idle_flag = get_bits1(&gb);
00725 if (sl->use_padding)
00726 padding_flag = get_bits1(&gb);
00727 if (padding_flag)
00728 padding_bits = get_bits(&gb, 3);
00729
00730 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
00731 if (sl->packet_seq_num_len)
00732 skip_bits_long(&gb, sl->packet_seq_num_len);
00733 if (sl->degr_prior_len)
00734 if (get_bits1(&gb))
00735 skip_bits(&gb, sl->degr_prior_len);
00736 if (ocr_flag)
00737 skip_bits_long(&gb, sl->ocr_len);
00738 if (au_start_flag) {
00739 if (sl->use_rand_acc_pt)
00740 get_bits1(&gb);
00741 if (sl->au_seq_num_len > 0)
00742 skip_bits_long(&gb, sl->au_seq_num_len);
00743 if (sl->use_timestamps) {
00744 dts_flag = get_bits1(&gb);
00745 cts_flag = get_bits1(&gb);
00746 }
00747 }
00748 if (sl->inst_bitrate_len)
00749 inst_bitrate_flag = get_bits1(&gb);
00750 if (dts_flag == 1)
00751 dts = get_bits64(&gb, sl->timestamp_len);
00752 if (cts_flag == 1)
00753 cts = get_bits64(&gb, sl->timestamp_len);
00754 if (sl->au_len > 0)
00755 skip_bits_long(&gb, sl->au_len);
00756 if (inst_bitrate_flag)
00757 skip_bits_long(&gb, sl->inst_bitrate_len);
00758 }
00759
00760 if (dts != AV_NOPTS_VALUE)
00761 pes->dts = dts;
00762 if (cts != AV_NOPTS_VALUE)
00763 pes->pts = cts;
00764
00765 if (sl->timestamp_len && sl->timestamp_res)
00766 avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
00767
00768 return (get_bits_count(&gb) + 7) >> 3;
00769 }
00770
00771
00772 static int mpegts_push_data(MpegTSFilter *filter,
00773 const uint8_t *buf, int buf_size, int is_start,
00774 int64_t pos)
00775 {
00776 PESContext *pes = filter->u.pes_filter.opaque;
00777 MpegTSContext *ts = pes->ts;
00778 const uint8_t *p;
00779 int len, code;
00780
00781 if(!ts->pkt)
00782 return 0;
00783
00784 if (is_start) {
00785 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
00786 new_pes_packet(pes, ts->pkt);
00787 ts->stop_parse = 1;
00788 }
00789 pes->state = MPEGTS_HEADER;
00790 pes->data_index = 0;
00791 pes->ts_packet_pos = pos;
00792 }
00793 p = buf;
00794 while (buf_size > 0) {
00795 switch(pes->state) {
00796 case MPEGTS_HEADER:
00797 len = PES_START_SIZE - pes->data_index;
00798 if (len > buf_size)
00799 len = buf_size;
00800 memcpy(pes->header + pes->data_index, p, len);
00801 pes->data_index += len;
00802 p += len;
00803 buf_size -= len;
00804 if (pes->data_index == PES_START_SIZE) {
00805
00806
00807 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
00808 pes->header[2] == 0x01) {
00809
00810 code = pes->header[3] | 0x100;
00811 av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
00812
00813 if ((pes->st && pes->st->discard == AVDISCARD_ALL) ||
00814 code == 0x1be)
00815 goto skip;
00816
00817
00818 if (!pes->st) {
00819 pes->st = avformat_new_stream(ts->stream, NULL);
00820 if (!pes->st)
00821 return AVERROR(ENOMEM);
00822 pes->st->id = pes->pid;
00823 mpegts_set_stream_info(pes->st, pes, 0, 0);
00824 }
00825
00826 pes->total_size = AV_RB16(pes->header + 4);
00827
00828
00829 if (!pes->total_size)
00830 pes->total_size = MAX_PES_PAYLOAD;
00831
00832
00833 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
00834 if (!pes->buffer)
00835 return AVERROR(ENOMEM);
00836
00837 if (code != 0x1bc && code != 0x1bf &&
00838 code != 0x1f0 && code != 0x1f1 &&
00839 code != 0x1ff && code != 0x1f2 &&
00840 code != 0x1f8) {
00841 pes->state = MPEGTS_PESHEADER;
00842 if (pes->st->codec->codec_id == CODEC_ID_NONE && !pes->st->request_probe) {
00843 av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
00844 pes->pid, pes->stream_type);
00845 pes->st->request_probe= 1;
00846 }
00847 } else {
00848 pes->state = MPEGTS_PAYLOAD;
00849 pes->data_index = 0;
00850 }
00851 } else {
00852
00853
00854 skip:
00855 pes->state = MPEGTS_SKIP;
00856 continue;
00857 }
00858 }
00859 break;
00860
00861
00862 case MPEGTS_PESHEADER:
00863 len = PES_HEADER_SIZE - pes->data_index;
00864 if (len < 0)
00865 return -1;
00866 if (len > buf_size)
00867 len = buf_size;
00868 memcpy(pes->header + pes->data_index, p, len);
00869 pes->data_index += len;
00870 p += len;
00871 buf_size -= len;
00872 if (pes->data_index == PES_HEADER_SIZE) {
00873 pes->pes_header_size = pes->header[8] + 9;
00874 pes->state = MPEGTS_PESHEADER_FILL;
00875 }
00876 break;
00877 case MPEGTS_PESHEADER_FILL:
00878 len = pes->pes_header_size - pes->data_index;
00879 if (len < 0)
00880 return -1;
00881 if (len > buf_size)
00882 len = buf_size;
00883 memcpy(pes->header + pes->data_index, p, len);
00884 pes->data_index += len;
00885 p += len;
00886 buf_size -= len;
00887 if (pes->data_index == pes->pes_header_size) {
00888 const uint8_t *r;
00889 unsigned int flags, pes_ext, skip;
00890
00891 flags = pes->header[7];
00892 r = pes->header + 9;
00893 pes->pts = AV_NOPTS_VALUE;
00894 pes->dts = AV_NOPTS_VALUE;
00895 if ((flags & 0xc0) == 0x80) {
00896 pes->dts = pes->pts = ff_parse_pes_pts(r);
00897 r += 5;
00898 } else if ((flags & 0xc0) == 0xc0) {
00899 pes->pts = ff_parse_pes_pts(r);
00900 r += 5;
00901 pes->dts = ff_parse_pes_pts(r);
00902 r += 5;
00903 }
00904 pes->extended_stream_id = -1;
00905 if (flags & 0x01) {
00906 pes_ext = *r++;
00907
00908 skip = (pes_ext >> 4) & 0xb;
00909 skip += skip & 0x9;
00910 r += skip;
00911 if ((pes_ext & 0x41) == 0x01 &&
00912 (r + 2) <= (pes->header + pes->pes_header_size)) {
00913
00914 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
00915 pes->extended_stream_id = r[1];
00916 }
00917 }
00918
00919
00920 pes->state = MPEGTS_PAYLOAD;
00921 pes->data_index = 0;
00922 if (pes->stream_type == 0x12 && buf_size > 0) {
00923 int sl_header_bytes = read_sl_header(pes, &pes->sl, p, buf_size);
00924 pes->pes_header_size += sl_header_bytes;
00925 p += sl_header_bytes;
00926 buf_size -= sl_header_bytes;
00927 }
00928 }
00929 break;
00930 case MPEGTS_PAYLOAD:
00931 if (buf_size > 0 && pes->buffer) {
00932 if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
00933 new_pes_packet(pes, ts->pkt);
00934 pes->total_size = MAX_PES_PAYLOAD;
00935 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
00936 if (!pes->buffer)
00937 return AVERROR(ENOMEM);
00938 ts->stop_parse = 1;
00939 } else if (pes->data_index == 0 && buf_size > pes->total_size) {
00940
00941
00942 buf_size = pes->total_size;
00943 }
00944 memcpy(pes->buffer+pes->data_index, p, buf_size);
00945 pes->data_index += buf_size;
00946 }
00947 buf_size = 0;
00948
00949
00950
00951
00952
00953 if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
00954 pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
00955 ts->stop_parse = 1;
00956 new_pes_packet(pes, ts->pkt);
00957 }
00958 break;
00959 case MPEGTS_SKIP:
00960 buf_size = 0;
00961 break;
00962 }
00963 }
00964
00965 return 0;
00966 }
00967
00968 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
00969 {
00970 MpegTSFilter *tss;
00971 PESContext *pes;
00972
00973
00974 pes = av_mallocz(sizeof(PESContext));
00975 if (!pes)
00976 return 0;
00977 pes->ts = ts;
00978 pes->stream = ts->stream;
00979 pes->pid = pid;
00980 pes->pcr_pid = pcr_pid;
00981 pes->state = MPEGTS_SKIP;
00982 pes->pts = AV_NOPTS_VALUE;
00983 pes->dts = AV_NOPTS_VALUE;
00984 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
00985 if (!tss) {
00986 av_free(pes);
00987 return 0;
00988 }
00989 return pes;
00990 }
00991
00992 #define MAX_LEVEL 4
00993 typedef struct {
00994 AVFormatContext *s;
00995 AVIOContext pb;
00996 Mp4Descr *descr;
00997 Mp4Descr *active_descr;
00998 int descr_count;
00999 int max_descr_count;
01000 int level;
01001 } MP4DescrParseContext;
01002
01003 static int init_MP4DescrParseContext(
01004 MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf,
01005 unsigned size, Mp4Descr *descr, int max_descr_count)
01006 {
01007 int ret;
01008 if (size > (1<<30))
01009 return AVERROR_INVALIDDATA;
01010
01011 if ((ret = ffio_init_context(&d->pb, (unsigned char*)buf, size, 0,
01012 NULL, NULL, NULL, NULL)) < 0)
01013 return ret;
01014
01015 d->s = s;
01016 d->level = 0;
01017 d->descr_count = 0;
01018 d->descr = descr;
01019 d->active_descr = NULL;
01020 d->max_descr_count = max_descr_count;
01021
01022 return 0;
01023 }
01024
01025 static void update_offsets(AVIOContext *pb, int64_t *off, int *len) {
01026 int64_t new_off = avio_tell(pb);
01027 (*len) -= new_off - *off;
01028 *off = new_off;
01029 }
01030
01031 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
01032 int target_tag);
01033
01034 static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
01035 {
01036 while (len > 0) {
01037 if (parse_mp4_descr(d, off, len, 0) < 0)
01038 return -1;
01039 update_offsets(&d->pb, &off, &len);
01040 }
01041 return 0;
01042 }
01043
01044 static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
01045 {
01046 avio_rb16(&d->pb);
01047 avio_r8(&d->pb);
01048 avio_r8(&d->pb);
01049 avio_r8(&d->pb);
01050 avio_r8(&d->pb);
01051 avio_r8(&d->pb);
01052 update_offsets(&d->pb, &off, &len);
01053 return parse_mp4_descr_arr(d, off, len);
01054 }
01055
01056 static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
01057 {
01058 int id_flags;
01059 if (len < 2)
01060 return 0;
01061 id_flags = avio_rb16(&d->pb);
01062 if (!(id_flags & 0x0020)) {
01063 update_offsets(&d->pb, &off, &len);
01064 return parse_mp4_descr_arr(d, off, len);
01065 } else {
01066 return 0;
01067 }
01068 }
01069
01070 static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
01071 {
01072 int es_id = 0;
01073 if (d->descr_count >= d->max_descr_count)
01074 return -1;
01075 ff_mp4_parse_es_descr(&d->pb, &es_id);
01076 d->active_descr = d->descr + (d->descr_count++);
01077
01078 d->active_descr->es_id = es_id;
01079 update_offsets(&d->pb, &off, &len);
01080 parse_mp4_descr(d, off, len, MP4DecConfigDescrTag);
01081 update_offsets(&d->pb, &off, &len);
01082 if (len > 0)
01083 parse_mp4_descr(d, off, len, MP4SLDescrTag);
01084 d->active_descr = NULL;
01085 return 0;
01086 }
01087
01088 static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len)
01089 {
01090 Mp4Descr *descr = d->active_descr;
01091 if (!descr)
01092 return -1;
01093 d->active_descr->dec_config_descr = av_malloc(len);
01094 if (!descr->dec_config_descr)
01095 return AVERROR(ENOMEM);
01096 descr->dec_config_descr_len = len;
01097 avio_read(&d->pb, descr->dec_config_descr, len);
01098 return 0;
01099 }
01100
01101 static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
01102 {
01103 Mp4Descr *descr = d->active_descr;
01104 int predefined;
01105 if (!descr)
01106 return -1;
01107
01108 predefined = avio_r8(&d->pb);
01109 if (!predefined) {
01110 int lengths;
01111 int flags = avio_r8(&d->pb);
01112 descr->sl.use_au_start = !!(flags & 0x80);
01113 descr->sl.use_au_end = !!(flags & 0x40);
01114 descr->sl.use_rand_acc_pt = !!(flags & 0x20);
01115 descr->sl.use_padding = !!(flags & 0x08);
01116 descr->sl.use_timestamps = !!(flags & 0x04);
01117 descr->sl.use_idle = !!(flags & 0x02);
01118 descr->sl.timestamp_res = avio_rb32(&d->pb);
01119 avio_rb32(&d->pb);
01120 descr->sl.timestamp_len = avio_r8(&d->pb);
01121 descr->sl.ocr_len = avio_r8(&d->pb);
01122 descr->sl.au_len = avio_r8(&d->pb);
01123 descr->sl.inst_bitrate_len = avio_r8(&d->pb);
01124 lengths = avio_rb16(&d->pb);
01125 descr->sl.degr_prior_len = lengths >> 12;
01126 descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
01127 descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
01128 } else {
01129 av_log_missing_feature(d->s, "Predefined SLConfigDescriptor\n", 0);
01130 }
01131 return 0;
01132 }
01133
01134 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
01135 int target_tag) {
01136 int tag;
01137 int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
01138 update_offsets(&d->pb, &off, &len);
01139 if (len < 0 || len1 > len || len1 <= 0) {
01140 av_log(d->s, AV_LOG_ERROR, "Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
01141 return -1;
01142 }
01143
01144 if (d->level++ >= MAX_LEVEL) {
01145 av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
01146 goto done;
01147 }
01148
01149 if (target_tag && tag != target_tag) {
01150 av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag, target_tag);
01151 goto done;
01152 }
01153
01154 switch (tag) {
01155 case MP4IODescrTag:
01156 parse_MP4IODescrTag(d, off, len1);
01157 break;
01158 case MP4ODescrTag:
01159 parse_MP4ODescrTag(d, off, len1);
01160 break;
01161 case MP4ESDescrTag:
01162 parse_MP4ESDescrTag(d, off, len1);
01163 break;
01164 case MP4DecConfigDescrTag:
01165 parse_MP4DecConfigDescrTag(d, off, len1);
01166 break;
01167 case MP4SLDescrTag:
01168 parse_MP4SLDescrTag(d, off, len1);
01169 break;
01170 }
01171
01172 done:
01173 d->level--;
01174 avio_seek(&d->pb, off + len1, SEEK_SET);
01175 return 0;
01176 }
01177
01178 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
01179 Mp4Descr *descr, int *descr_count, int max_descr_count)
01180 {
01181 MP4DescrParseContext d;
01182 if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
01183 return -1;
01184
01185 parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
01186
01187 *descr_count = d.descr_count;
01188 return 0;
01189 }
01190
01191 static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
01192 Mp4Descr *descr, int *descr_count, int max_descr_count)
01193 {
01194 MP4DescrParseContext d;
01195 if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
01196 return -1;
01197
01198 parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
01199
01200 *descr_count = d.descr_count;
01201 return 0;
01202 }
01203
01204 static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01205 {
01206 MpegTSContext *ts = filter->u.section_filter.opaque;
01207 SectionHeader h;
01208 const uint8_t *p, *p_end;
01209 AVIOContext pb;
01210 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
01211 int mp4_descr_count = 0;
01212 int i, pid;
01213 AVFormatContext *s = ts->stream;
01214
01215 p_end = section + section_len - 4;
01216 p = section;
01217 if (parse_section_header(&h, &p, p_end) < 0)
01218 return;
01219 if (h.tid != M4OD_TID)
01220 return;
01221
01222 mp4_read_od(s, p, (unsigned)(p_end - p), mp4_descr, &mp4_descr_count, MAX_MP4_DESCR_COUNT);
01223
01224 for (pid = 0; pid < NB_PID_MAX; pid++) {
01225 if (!ts->pids[pid])
01226 continue;
01227 for (i = 0; i < mp4_descr_count; i++) {
01228 PESContext *pes;
01229 AVStream *st;
01230 if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
01231 continue;
01232 if (!(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES)) {
01233 av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
01234 continue;
01235 }
01236 pes = ts->pids[pid]->u.pes_filter.opaque;
01237 st = pes->st;
01238 if (!st) {
01239 continue;
01240 }
01241
01242 pes->sl = mp4_descr[i].sl;
01243
01244 ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
01245 mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
01246 ff_mp4_read_dec_config_descr(s, st, &pb);
01247 if (st->codec->codec_id == CODEC_ID_AAC &&
01248 st->codec->extradata_size > 0)
01249 st->need_parsing = 0;
01250 if (st->codec->codec_id == CODEC_ID_H264 &&
01251 st->codec->extradata_size > 0)
01252 st->need_parsing = 0;
01253
01254 if (st->codec->codec_id <= CODEC_ID_NONE) {
01255 } else if (st->codec->codec_id < CODEC_ID_FIRST_AUDIO) {
01256 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01257 } else if (st->codec->codec_id < CODEC_ID_FIRST_SUBTITLE) {
01258 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01259 } else if (st->codec->codec_id < CODEC_ID_FIRST_UNKNOWN) {
01260 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
01261 }
01262 }
01263 }
01264 for (i = 0; i < mp4_descr_count; i++)
01265 av_free(mp4_descr[i].dec_config_descr);
01266 }
01267
01268 int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
01269 const uint8_t **pp, const uint8_t *desc_list_end,
01270 Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
01271 MpegTSContext *ts)
01272 {
01273 const uint8_t *desc_end;
01274 int desc_len, desc_tag, desc_es_id;
01275 char language[252];
01276 int i;
01277
01278 desc_tag = get8(pp, desc_list_end);
01279 if (desc_tag < 0)
01280 return -1;
01281 desc_len = get8(pp, desc_list_end);
01282 if (desc_len < 0)
01283 return -1;
01284 desc_end = *pp + desc_len;
01285 if (desc_end > desc_list_end)
01286 return -1;
01287
01288 av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
01289
01290 if (st->codec->codec_id == CODEC_ID_NONE &&
01291 stream_type == STREAM_TYPE_PRIVATE_DATA)
01292 mpegts_find_stream_type(st, desc_tag, DESC_types);
01293
01294 switch(desc_tag) {
01295 case 0x1E:
01296 desc_es_id = get16(pp, desc_end);
01297 if (ts && ts->pids[pid])
01298 ts->pids[pid]->es_id = desc_es_id;
01299 for (i = 0; i < mp4_descr_count; i++)
01300 if (mp4_descr[i].dec_config_descr_len &&
01301 mp4_descr[i].es_id == desc_es_id) {
01302 AVIOContext pb;
01303 ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
01304 mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
01305 ff_mp4_read_dec_config_descr(fc, st, &pb);
01306 if (st->codec->codec_id == CODEC_ID_AAC &&
01307 st->codec->extradata_size > 0)
01308 st->need_parsing = 0;
01309 if (st->codec->codec_id == CODEC_ID_MPEG4SYSTEMS)
01310 mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
01311 }
01312 break;
01313 case 0x1F:
01314 get16(pp, desc_end);
01315 if (mp4_descr_count > 0 && (st->codec->codec_id == CODEC_ID_AAC_LATM || st->request_probe>0) &&
01316 mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
01317 AVIOContext pb;
01318 ffio_init_context(&pb, mp4_descr->dec_config_descr,
01319 mp4_descr->dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
01320 ff_mp4_read_dec_config_descr(fc, st, &pb);
01321 if (st->codec->codec_id == CODEC_ID_AAC &&
01322 st->codec->extradata_size > 0){
01323 st->request_probe= st->need_parsing = 0;
01324 st->codec->codec_type= AVMEDIA_TYPE_AUDIO;
01325 }
01326 }
01327 break;
01328 case 0x56:
01329 language[0] = get8(pp, desc_end);
01330 language[1] = get8(pp, desc_end);
01331 language[2] = get8(pp, desc_end);
01332 language[3] = 0;
01333 av_dict_set(&st->metadata, "language", language, 0);
01334 break;
01335 case 0x59:
01336 language[0] = get8(pp, desc_end);
01337 language[1] = get8(pp, desc_end);
01338 language[2] = get8(pp, desc_end);
01339 language[3] = 0;
01340
01341 switch(get8(pp, desc_end)) {
01342 case 0x20:
01343 case 0x21:
01344 case 0x22:
01345 case 0x23:
01346 case 0x24:
01347 case 0x25:
01348 st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
01349 break;
01350 }
01351 if (st->codec->extradata) {
01352 if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
01353 av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
01354 } else {
01355 st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
01356 if (st->codec->extradata) {
01357 st->codec->extradata_size = 4;
01358 memcpy(st->codec->extradata, *pp, 4);
01359 }
01360 }
01361 *pp += 4;
01362 av_dict_set(&st->metadata, "language", language, 0);
01363 break;
01364 case 0x0a:
01365 for (i = 0; i + 4 <= desc_len; i += 4) {
01366 language[i + 0] = get8(pp, desc_end);
01367 language[i + 1] = get8(pp, desc_end);
01368 language[i + 2] = get8(pp, desc_end);
01369 language[i + 3] = ',';
01370 switch (get8(pp, desc_end)) {
01371 case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
01372 case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
01373 case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
01374 }
01375 }
01376 if (i) {
01377 language[i - 1] = 0;
01378 av_dict_set(&st->metadata, "language", language, 0);
01379 }
01380 break;
01381 case 0x05:
01382 st->codec->codec_tag = bytestream_get_le32(pp);
01383 av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
01384 if (st->codec->codec_id == CODEC_ID_NONE &&
01385 stream_type == STREAM_TYPE_PRIVATE_DATA)
01386 mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
01387 break;
01388 case 0x52:
01389 st->stream_identifier = 1 + get8(pp, desc_end);
01390 break;
01391 default:
01392 break;
01393 }
01394 *pp = desc_end;
01395 return 0;
01396 }
01397
01398 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01399 {
01400 MpegTSContext *ts = filter->u.section_filter.opaque;
01401 SectionHeader h1, *h = &h1;
01402 PESContext *pes;
01403 AVStream *st;
01404 const uint8_t *p, *p_end, *desc_list_end;
01405 int program_info_length, pcr_pid, pid, stream_type;
01406 int desc_list_len;
01407 uint32_t prog_reg_desc = 0;
01408
01409 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
01410 int mp4_descr_count = 0;
01411 int i;
01412
01413 av_dlog(ts->stream, "PMT: len %i\n", section_len);
01414 hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
01415
01416 p_end = section + section_len - 4;
01417 p = section;
01418 if (parse_section_header(h, &p, p_end) < 0)
01419 return;
01420
01421 av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
01422 h->id, h->sec_num, h->last_sec_num);
01423
01424 if (h->tid != PMT_TID)
01425 return;
01426
01427 clear_program(ts, h->id);
01428 pcr_pid = get16(&p, p_end) & 0x1fff;
01429 if (pcr_pid < 0)
01430 return;
01431 add_pid_to_pmt(ts, h->id, pcr_pid);
01432 set_pcr_pid(ts->stream, h->id, pcr_pid);
01433
01434 av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
01435
01436 program_info_length = get16(&p, p_end) & 0xfff;
01437 if (program_info_length < 0)
01438 return;
01439 while(program_info_length >= 2) {
01440 uint8_t tag, len;
01441 tag = get8(&p, p_end);
01442 len = get8(&p, p_end);
01443
01444 av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
01445
01446 if(len > program_info_length - 2)
01447
01448 break;
01449 program_info_length -= len + 2;
01450 if (tag == 0x1d) {
01451 get8(&p, p_end);
01452 get8(&p, p_end);
01453 len -= 2;
01454 mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
01455 &mp4_descr_count, MAX_MP4_DESCR_COUNT);
01456 } else if (tag == 0x05 && len >= 4) {
01457 prog_reg_desc = bytestream_get_le32(&p);
01458 len -= 4;
01459 }
01460 p += len;
01461 }
01462 p += program_info_length;
01463 if (p >= p_end)
01464 goto out;
01465
01466
01467 if (!ts->stream->nb_streams)
01468 ts->stop_parse = 2;
01469
01470 for(;;) {
01471 st = 0;
01472 pes = NULL;
01473 stream_type = get8(&p, p_end);
01474 if (stream_type < 0)
01475 break;
01476 pid = get16(&p, p_end) & 0x1fff;
01477 if (pid < 0)
01478 break;
01479
01480
01481 if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
01482 pes = ts->pids[pid]->u.pes_filter.opaque;
01483 if (!pes->st) {
01484 pes->st = avformat_new_stream(pes->stream, NULL);
01485 pes->st->id = pes->pid;
01486 }
01487 st = pes->st;
01488 } else if (stream_type != 0x13) {
01489 if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]);
01490 pes = add_pes_stream(ts, pid, pcr_pid);
01491 if (pes) {
01492 st = avformat_new_stream(pes->stream, NULL);
01493 st->id = pes->pid;
01494 }
01495 } else {
01496 int idx = ff_find_stream_index(ts->stream, pid);
01497 if (idx >= 0) {
01498 st = ts->stream->streams[idx];
01499 } else {
01500 st = avformat_new_stream(ts->stream, NULL);
01501 st->id = pid;
01502 st->codec->codec_type = AVMEDIA_TYPE_DATA;
01503 }
01504 }
01505
01506 if (!st)
01507 goto out;
01508
01509 if (pes && !pes->stream_type)
01510 mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
01511
01512 add_pid_to_pmt(ts, h->id, pid);
01513
01514 ff_program_add_stream_index(ts->stream, h->id, st->index);
01515
01516 desc_list_len = get16(&p, p_end) & 0xfff;
01517 if (desc_list_len < 0)
01518 break;
01519 desc_list_end = p + desc_list_len;
01520 if (desc_list_end > p_end)
01521 break;
01522 for(;;) {
01523 if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
01524 mp4_descr, mp4_descr_count, pid, ts) < 0)
01525 break;
01526
01527 if (pes && prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
01528 ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
01529 pes->sub_st->codec->codec_tag = st->codec->codec_tag;
01530 }
01531 }
01532 p = desc_list_end;
01533 }
01534
01535 out:
01536 for (i = 0; i < mp4_descr_count; i++)
01537 av_free(mp4_descr[i].dec_config_descr);
01538 }
01539
01540 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01541 {
01542 MpegTSContext *ts = filter->u.section_filter.opaque;
01543 SectionHeader h1, *h = &h1;
01544 const uint8_t *p, *p_end;
01545 int sid, pmt_pid;
01546 AVProgram *program;
01547
01548 av_dlog(ts->stream, "PAT:\n");
01549 hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
01550
01551 p_end = section + section_len - 4;
01552 p = section;
01553 if (parse_section_header(h, &p, p_end) < 0)
01554 return;
01555 if (h->tid != PAT_TID)
01556 return;
01557
01558 ts->stream->ts_id = h->id;
01559
01560 clear_programs(ts);
01561 for(;;) {
01562 sid = get16(&p, p_end);
01563 if (sid < 0)
01564 break;
01565 pmt_pid = get16(&p, p_end) & 0x1fff;
01566 if (pmt_pid < 0)
01567 break;
01568
01569 av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
01570
01571 if (sid == 0x0000) {
01572
01573 } else {
01574 program = av_new_program(ts->stream, sid);
01575 program->program_num = sid;
01576 program->pmt_pid = pmt_pid;
01577 if (ts->pids[pmt_pid])
01578 mpegts_close_filter(ts, ts->pids[pmt_pid]);
01579 mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
01580 add_pat_entry(ts, sid);
01581 add_pid_to_pmt(ts, sid, 0);
01582 add_pid_to_pmt(ts, sid, pmt_pid);
01583 }
01584 }
01585 }
01586
01587 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01588 {
01589 MpegTSContext *ts = filter->u.section_filter.opaque;
01590 SectionHeader h1, *h = &h1;
01591 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
01592 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
01593 char *name, *provider_name;
01594
01595 av_dlog(ts->stream, "SDT:\n");
01596 hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
01597
01598 p_end = section + section_len - 4;
01599 p = section;
01600 if (parse_section_header(h, &p, p_end) < 0)
01601 return;
01602 if (h->tid != SDT_TID)
01603 return;
01604 onid = get16(&p, p_end);
01605 if (onid < 0)
01606 return;
01607 val = get8(&p, p_end);
01608 if (val < 0)
01609 return;
01610 for(;;) {
01611 sid = get16(&p, p_end);
01612 if (sid < 0)
01613 break;
01614 val = get8(&p, p_end);
01615 if (val < 0)
01616 break;
01617 desc_list_len = get16(&p, p_end) & 0xfff;
01618 if (desc_list_len < 0)
01619 break;
01620 desc_list_end = p + desc_list_len;
01621 if (desc_list_end > p_end)
01622 break;
01623 for(;;) {
01624 desc_tag = get8(&p, desc_list_end);
01625 if (desc_tag < 0)
01626 break;
01627 desc_len = get8(&p, desc_list_end);
01628 desc_end = p + desc_len;
01629 if (desc_end > desc_list_end)
01630 break;
01631
01632 av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
01633 desc_tag, desc_len);
01634
01635 switch(desc_tag) {
01636 case 0x48:
01637 service_type = get8(&p, p_end);
01638 if (service_type < 0)
01639 break;
01640 provider_name = getstr8(&p, p_end);
01641 if (!provider_name)
01642 break;
01643 name = getstr8(&p, p_end);
01644 if (name) {
01645 AVProgram *program = av_new_program(ts->stream, sid);
01646 if(program) {
01647 av_dict_set(&program->metadata, "service_name", name, 0);
01648 av_dict_set(&program->metadata, "service_provider", provider_name, 0);
01649 }
01650 }
01651 av_free(name);
01652 av_free(provider_name);
01653 break;
01654 default:
01655 break;
01656 }
01657 p = desc_end;
01658 }
01659 p = desc_list_end;
01660 }
01661 }
01662
01663
01664 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
01665 {
01666 AVFormatContext *s = ts->stream;
01667 MpegTSFilter *tss;
01668 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
01669 has_adaptation, has_payload;
01670 const uint8_t *p, *p_end;
01671 int64_t pos;
01672
01673 pid = AV_RB16(packet + 1) & 0x1fff;
01674 if(pid && discard_pid(ts, pid))
01675 return 0;
01676 is_start = packet[1] & 0x40;
01677 tss = ts->pids[pid];
01678 if (ts->auto_guess && tss == NULL && is_start) {
01679 add_pes_stream(ts, pid, -1);
01680 tss = ts->pids[pid];
01681 }
01682 if (!tss)
01683 return 0;
01684
01685 afc = (packet[3] >> 4) & 3;
01686 if (afc == 0)
01687 return 0;
01688 has_adaptation = afc & 2;
01689 has_payload = afc & 1;
01690 is_discontinuity = has_adaptation
01691 && packet[4] != 0
01692 && (packet[5] & 0x80);
01693
01694
01695 cc = (packet[3] & 0xf);
01696 expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
01697 cc_ok = pid == 0x1FFF
01698 || is_discontinuity
01699 || tss->last_cc < 0
01700 || expected_cc == cc;
01701
01702 tss->last_cc = cc;
01703 if (!cc_ok) {
01704 av_log(ts->stream, AV_LOG_DEBUG,
01705 "Continuity check failed for pid %d expected %d got %d\n",
01706 pid, expected_cc, cc);
01707 if(tss->type == MPEGTS_PES) {
01708 PESContext *pc = tss->u.pes_filter.opaque;
01709 pc->flags |= AV_PKT_FLAG_CORRUPT;
01710 }
01711 }
01712
01713 if (!has_payload)
01714 return 0;
01715 p = packet + 4;
01716 if (has_adaptation) {
01717
01718 p += p[0] + 1;
01719 }
01720
01721 p_end = packet + TS_PACKET_SIZE;
01722 if (p >= p_end)
01723 return 0;
01724
01725 pos = avio_tell(ts->stream->pb);
01726 ts->pos47= pos % ts->raw_packet_size;
01727
01728 if (tss->type == MPEGTS_SECTION) {
01729 if (is_start) {
01730
01731 len = *p++;
01732 if (p + len > p_end)
01733 return 0;
01734 if (len && cc_ok) {
01735
01736 write_section_data(s, tss,
01737 p, len, 0);
01738
01739 if (!ts->pids[pid])
01740 return 0;
01741 }
01742 p += len;
01743 if (p < p_end) {
01744 write_section_data(s, tss,
01745 p, p_end - p, 1);
01746 }
01747 } else {
01748 if (cc_ok) {
01749 write_section_data(s, tss,
01750 p, p_end - p, 0);
01751 }
01752 }
01753 } else {
01754 int ret;
01755
01756 if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
01757 pos - ts->raw_packet_size)) < 0)
01758 return ret;
01759 }
01760
01761 return 0;
01762 }
01763
01764
01765
01766 static int mpegts_resync(AVFormatContext *s)
01767 {
01768 AVIOContext *pb = s->pb;
01769 int c, i;
01770
01771 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
01772 c = avio_r8(pb);
01773 if (url_feof(pb))
01774 return -1;
01775 if (c == 0x47) {
01776 avio_seek(pb, -1, SEEK_CUR);
01777 return 0;
01778 }
01779 }
01780 av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
01781
01782 return -1;
01783 }
01784
01785
01786 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
01787 {
01788 AVIOContext *pb = s->pb;
01789 int skip, len;
01790
01791 for(;;) {
01792 len = avio_read(pb, buf, TS_PACKET_SIZE);
01793 if (len != TS_PACKET_SIZE)
01794 return len < 0 ? len : AVERROR_EOF;
01795
01796 if (buf[0] != 0x47) {
01797
01798 avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
01799 if (mpegts_resync(s) < 0)
01800 return AVERROR(EAGAIN);
01801 else
01802 continue;
01803 } else {
01804 skip = raw_packet_size - TS_PACKET_SIZE;
01805 if (skip > 0)
01806 avio_skip(pb, skip);
01807 break;
01808 }
01809 }
01810 return 0;
01811 }
01812
01813 static int handle_packets(MpegTSContext *ts, int nb_packets)
01814 {
01815 AVFormatContext *s = ts->stream;
01816 uint8_t packet[TS_PACKET_SIZE+FF_INPUT_BUFFER_PADDING_SIZE];
01817 int packet_num, ret = 0;
01818
01819 if (avio_tell(s->pb) != ts->last_pos) {
01820 int i;
01821 av_dlog(ts->stream, "Skipping after seek\n");
01822
01823 for (i = 0; i < NB_PID_MAX; i++) {
01824 if (ts->pids[i]) {
01825 if (ts->pids[i]->type == MPEGTS_PES) {
01826 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
01827 av_freep(&pes->buffer);
01828 pes->data_index = 0;
01829 pes->state = MPEGTS_SKIP;
01830 }
01831 ts->pids[i]->last_cc = -1;
01832 }
01833 }
01834 }
01835
01836 ts->stop_parse = 0;
01837 packet_num = 0;
01838 memset(packet + TS_PACKET_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
01839 for(;;) {
01840 packet_num++;
01841 if (nb_packets != 0 && packet_num >= nb_packets ||
01842 ts->stop_parse > 1) {
01843 ret = AVERROR(EAGAIN);
01844 break;
01845 }
01846 if (ts->stop_parse > 0)
01847 break;
01848
01849 ret = read_packet(s, packet, ts->raw_packet_size);
01850 if (ret != 0)
01851 break;
01852 ret = handle_packet(ts, packet);
01853 if (ret != 0)
01854 break;
01855 }
01856 ts->last_pos = avio_tell(s->pb);
01857 return ret;
01858 }
01859
01860 static int mpegts_probe(AVProbeData *p)
01861 {
01862 #if 1
01863 const int size= p->buf_size;
01864 int score, fec_score, dvhs_score;
01865 int check_count= size / TS_FEC_PACKET_SIZE;
01866 #define CHECK_COUNT 10
01867
01868 if (check_count < CHECK_COUNT)
01869 return -1;
01870
01871 score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
01872 dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
01873 fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
01874
01875
01876
01877 if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
01878 else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
01879 else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
01880 else return -1;
01881 #else
01882
01883 if (av_match_ext(p->filename, "ts"))
01884 return AVPROBE_SCORE_MAX;
01885 else
01886 return 0;
01887 #endif
01888 }
01889
01890
01891
01892 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
01893 const uint8_t *packet)
01894 {
01895 int afc, len, flags;
01896 const uint8_t *p;
01897 unsigned int v;
01898
01899 afc = (packet[3] >> 4) & 3;
01900 if (afc <= 1)
01901 return -1;
01902 p = packet + 4;
01903 len = p[0];
01904 p++;
01905 if (len == 0)
01906 return -1;
01907 flags = *p++;
01908 len--;
01909 if (!(flags & 0x10))
01910 return -1;
01911 if (len < 6)
01912 return -1;
01913 v = AV_RB32(p);
01914 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
01915 *ppcr_low = ((p[4] & 1) << 8) | p[5];
01916 return 0;
01917 }
01918
01919 static int mpegts_read_header(AVFormatContext *s,
01920 AVFormatParameters *ap)
01921 {
01922 MpegTSContext *ts = s->priv_data;
01923 AVIOContext *pb = s->pb;
01924 uint8_t buf[8*1024];
01925 int len;
01926 int64_t pos;
01927
01928
01929 pos = avio_tell(pb);
01930 len = avio_read(pb, buf, sizeof(buf));
01931 if (len != sizeof(buf))
01932 goto fail;
01933 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
01934 if (ts->raw_packet_size <= 0) {
01935 av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n");
01936 ts->raw_packet_size = TS_PACKET_SIZE;
01937 }
01938 ts->stream = s;
01939 ts->auto_guess = 0;
01940
01941 if (s->iformat == &ff_mpegts_demuxer) {
01942
01943
01944
01945
01946
01947
01948 if (avio_seek(pb, pos, SEEK_SET) < 0)
01949 av_log(s, pb->seekable ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n");
01950
01951 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
01952
01953 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
01954
01955 handle_packets(ts, s->probesize / ts->raw_packet_size);
01956
01957
01958 ts->auto_guess = 1;
01959
01960 av_dlog(ts->stream, "tuning done\n");
01961
01962 s->ctx_flags |= AVFMTCTX_NOHEADER;
01963 } else {
01964 AVStream *st;
01965 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
01966 int64_t pcrs[2], pcr_h;
01967 int packet_count[2];
01968 uint8_t packet[TS_PACKET_SIZE];
01969
01970
01971
01972 st = avformat_new_stream(s, NULL);
01973 if (!st)
01974 goto fail;
01975 avpriv_set_pts_info(st, 60, 1, 27000000);
01976 st->codec->codec_type = AVMEDIA_TYPE_DATA;
01977 st->codec->codec_id = CODEC_ID_MPEG2TS;
01978
01979
01980 pcr_pid = -1;
01981 nb_pcrs = 0;
01982 nb_packets = 0;
01983 for(;;) {
01984 ret = read_packet(s, packet, ts->raw_packet_size);
01985 if (ret < 0)
01986 return -1;
01987 pid = AV_RB16(packet + 1) & 0x1fff;
01988 if ((pcr_pid == -1 || pcr_pid == pid) &&
01989 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
01990 pcr_pid = pid;
01991 packet_count[nb_pcrs] = nb_packets;
01992 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
01993 nb_pcrs++;
01994 if (nb_pcrs >= 2)
01995 break;
01996 }
01997 nb_packets++;
01998 }
01999
02000
02001
02002 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
02003 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
02004 s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
02005 st->codec->bit_rate = s->bit_rate;
02006 st->start_time = ts->cur_pcr;
02007 av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n",
02008 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
02009 }
02010
02011 avio_seek(pb, pos, SEEK_SET);
02012 return 0;
02013 fail:
02014 return -1;
02015 }
02016
02017 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
02018
02019 static int mpegts_raw_read_packet(AVFormatContext *s,
02020 AVPacket *pkt)
02021 {
02022 MpegTSContext *ts = s->priv_data;
02023 int ret, i;
02024 int64_t pcr_h, next_pcr_h, pos;
02025 int pcr_l, next_pcr_l;
02026 uint8_t pcr_buf[12];
02027
02028 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
02029 return AVERROR(ENOMEM);
02030 pkt->pos= avio_tell(s->pb);
02031 ret = read_packet(s, pkt->data, ts->raw_packet_size);
02032 if (ret < 0) {
02033 av_free_packet(pkt);
02034 return ret;
02035 }
02036 if (ts->mpeg2ts_compute_pcr) {
02037
02038 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
02039
02040 pos = avio_tell(s->pb);
02041 for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
02042 avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
02043 avio_read(s->pb, pcr_buf, 12);
02044 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
02045
02046 ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
02047 (i + 1);
02048 break;
02049 }
02050 }
02051 avio_seek(s->pb, pos, SEEK_SET);
02052
02053 ts->cur_pcr = pcr_h * 300 + pcr_l;
02054 }
02055 pkt->pts = ts->cur_pcr;
02056 pkt->duration = ts->pcr_incr;
02057 ts->cur_pcr += ts->pcr_incr;
02058 }
02059 pkt->stream_index = 0;
02060 return 0;
02061 }
02062
02063 static int mpegts_read_packet(AVFormatContext *s,
02064 AVPacket *pkt)
02065 {
02066 MpegTSContext *ts = s->priv_data;
02067 int ret, i;
02068
02069 ts->pkt = pkt;
02070 ret = handle_packets(ts, 0);
02071 if (ret < 0) {
02072
02073 for (i = 0; i < NB_PID_MAX; i++) {
02074 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
02075 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
02076 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
02077 new_pes_packet(pes, pkt);
02078 pes->state = MPEGTS_SKIP;
02079 ret = 0;
02080 break;
02081 }
02082 }
02083 }
02084 }
02085
02086 return ret;
02087 }
02088
02089 static int mpegts_read_close(AVFormatContext *s)
02090 {
02091 MpegTSContext *ts = s->priv_data;
02092 int i;
02093
02094 clear_programs(ts);
02095
02096 for(i=0;i<NB_PID_MAX;i++)
02097 if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
02098
02099 return 0;
02100 }
02101
02102 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
02103 int64_t *ppos, int64_t pos_limit)
02104 {
02105 MpegTSContext *ts = s->priv_data;
02106 int64_t pos, timestamp;
02107 uint8_t buf[TS_PACKET_SIZE];
02108 int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
02109 pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
02110 while(pos < pos_limit) {
02111 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
02112 return AV_NOPTS_VALUE;
02113 if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
02114 return AV_NOPTS_VALUE;
02115 if (buf[0] != 0x47) {
02116 if (mpegts_resync(s) < 0)
02117 return AV_NOPTS_VALUE;
02118 pos = avio_tell(s->pb);
02119 continue;
02120 }
02121 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
02122 parse_pcr(×tamp, &pcr_l, buf) == 0) {
02123 *ppos = pos;
02124 return timestamp;
02125 }
02126 pos += ts->raw_packet_size;
02127 }
02128
02129 return AV_NOPTS_VALUE;
02130 }
02131
02132 static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index,
02133 int64_t *ppos, int64_t pos_limit)
02134 {
02135 MpegTSContext *ts = s->priv_data;
02136 int64_t pos;
02137 pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
02138 ff_read_frame_flush(s);
02139 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
02140 return AV_NOPTS_VALUE;
02141 while(pos < pos_limit) {
02142 int ret;
02143 AVPacket pkt;
02144 av_init_packet(&pkt);
02145 ret= av_read_frame(s, &pkt);
02146 if(ret < 0)
02147 return AV_NOPTS_VALUE;
02148 av_free_packet(&pkt);
02149 if(pkt.dts != AV_NOPTS_VALUE && pkt.pos >= 0){
02150 ff_reduce_index(s, pkt.stream_index);
02151 av_add_index_entry(s->streams[pkt.stream_index], pkt.pos, pkt.dts, 0, 0, AVINDEX_KEYFRAME );
02152 if(pkt.stream_index == stream_index){
02153 *ppos= pkt.pos;
02154 return pkt.dts;
02155 }
02156 }
02157 pos = pkt.pos;
02158 }
02159
02160 return AV_NOPTS_VALUE;
02161 }
02162
02163 #ifdef USE_SYNCPOINT_SEARCH
02164
02165 static int read_seek2(AVFormatContext *s,
02166 int stream_index,
02167 int64_t min_ts,
02168 int64_t target_ts,
02169 int64_t max_ts,
02170 int flags)
02171 {
02172 int64_t pos;
02173
02174 int64_t ts_ret, ts_adj;
02175 int stream_index_gen_search;
02176 AVStream *st;
02177 AVParserState *backup;
02178
02179 backup = ff_store_parser_state(s);
02180
02181
02182 flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
02183 AVSEEK_FLAG_BACKWARD : 0;
02184
02185 if (flags & AVSEEK_FLAG_BYTE) {
02186
02187 pos = target_ts;
02188 } else {
02189
02190 if (stream_index < 0) {
02191 stream_index_gen_search = av_find_default_stream_index(s);
02192 if (stream_index_gen_search < 0) {
02193 ff_restore_parser_state(s, backup);
02194 return -1;
02195 }
02196
02197 st = s->streams[stream_index_gen_search];
02198
02199 ts_adj = av_rescale(target_ts,
02200 st->time_base.den,
02201 AV_TIME_BASE * (int64_t)st->time_base.num);
02202 } else {
02203 ts_adj = target_ts;
02204 stream_index_gen_search = stream_index;
02205 }
02206 pos = ff_gen_search(s, stream_index_gen_search, ts_adj,
02207 0, INT64_MAX, -1,
02208 AV_NOPTS_VALUE,
02209 AV_NOPTS_VALUE,
02210 flags, &ts_ret, mpegts_get_pcr);
02211 if (pos < 0) {
02212 ff_restore_parser_state(s, backup);
02213 return -1;
02214 }
02215 }
02216
02217
02218 if (ff_gen_syncpoint_search(s, stream_index, pos,
02219 min_ts, target_ts, max_ts,
02220 flags) < 0) {
02221 ff_restore_parser_state(s, backup);
02222 return -1;
02223 }
02224
02225 ff_free_parser_state(s, backup);
02226 return 0;
02227 }
02228
02229 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
02230 {
02231 int ret;
02232 if (flags & AVSEEK_FLAG_BACKWARD) {
02233 flags &= ~AVSEEK_FLAG_BACKWARD;
02234 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
02235 if (ret < 0)
02236
02237 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
02238 } else {
02239 ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
02240 if (ret < 0)
02241
02242 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
02243 }
02244 return ret;
02245 }
02246
02247 #endif
02248
02249
02250
02251
02252 MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
02253 {
02254 MpegTSContext *ts;
02255
02256 ts = av_mallocz(sizeof(MpegTSContext));
02257 if (!ts)
02258 return NULL;
02259
02260 ts->raw_packet_size = TS_PACKET_SIZE;
02261 ts->stream = s;
02262 ts->auto_guess = 1;
02263 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
02264 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
02265
02266 return ts;
02267 }
02268
02269
02270
02271 int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
02272 const uint8_t *buf, int len)
02273 {
02274 int len1;
02275
02276 len1 = len;
02277 ts->pkt = pkt;
02278 for(;;) {
02279 ts->stop_parse = 0;
02280 if (len < TS_PACKET_SIZE)
02281 return -1;
02282 if (buf[0] != 0x47) {
02283 buf++;
02284 len--;
02285 } else {
02286 handle_packet(ts, buf);
02287 buf += TS_PACKET_SIZE;
02288 len -= TS_PACKET_SIZE;
02289 if (ts->stop_parse == 1)
02290 break;
02291 }
02292 }
02293 return len1 - len;
02294 }
02295
02296 void ff_mpegts_parse_close(MpegTSContext *ts)
02297 {
02298 int i;
02299
02300 for(i=0;i<NB_PID_MAX;i++)
02301 av_free(ts->pids[i]);
02302 av_free(ts);
02303 }
02304
02305 AVInputFormat ff_mpegts_demuxer = {
02306 .name = "mpegts",
02307 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
02308 .priv_data_size = sizeof(MpegTSContext),
02309 .read_probe = mpegts_probe,
02310 .read_header = mpegts_read_header,
02311 .read_packet = mpegts_read_packet,
02312 .read_close = mpegts_read_close,
02313 .read_timestamp = mpegts_get_dts,
02314 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
02315 #ifdef USE_SYNCPOINT_SEARCH
02316 .read_seek2 = read_seek2,
02317 #endif
02318 };
02319
02320 AVInputFormat ff_mpegtsraw_demuxer = {
02321 .name = "mpegtsraw",
02322 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
02323 .priv_data_size = sizeof(MpegTSContext),
02324 .read_header = mpegts_read_header,
02325 .read_packet = mpegts_raw_read_packet,
02326 .read_close = mpegts_read_close,
02327 .read_timestamp = mpegts_get_dts,
02328 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
02329 #ifdef USE_SYNCPOINT_SEARCH
02330 .read_seek2 = read_seek2,
02331 #endif
02332 .priv_class = &mpegtsraw_class,
02333 };