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