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