Ticket #6753: dvb_subtiles_workaround.patch
File dvb_subtiles_workaround.patch, 49.1 KB (added by , 14 years ago) |
---|
-
libs/libavformat/mpegts.c
1 1 /* 2 2 * MPEG2 transport stream (aka DVB) demuxer 3 * Copyright (c) 2002-2003 Fabrice Bellard 3 * Copyright (c) 2002-2003 Fabrice Bellard. 4 4 * 5 5 * This file is part of FFmpeg. 6 6 * … … 19 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 20 */ 21 21 22 //#define DEBUG23 24 22 #include "libavutil/crc.h" 25 #include "libavutil/intreadwrite.h"26 #include "libavcodec/bytestream.h"27 23 #include "avformat.h" 28 #include "libmythdb/compat.h" // for uint on MinGW (Windows)29 24 #include <pthread.h> 25 #include "libmythdb/compat.h" 30 26 #include "mpegts.h" 31 #include "internal.h"32 27 28 //#define DEBUG_SI 29 //#define DEBUG_SEEK 30 33 31 /* 1.0 second at 24Mbit/s */ 34 32 #define MAX_SCAN_PACKETS 32000 35 33 … … 37 35 synchronisation is lost */ 38 36 #define MAX_RESYNC_SIZE 4096 39 37 40 #define MAX_PES_PAYLOAD 200*102441 38 #define PMT_NOT_YET_FOUND 0 42 39 #define PMT_NOT_IN_PAT 1 43 40 #define PMT_FOUND 2 … … 45 42 typedef struct PESContext PESContext; 46 43 typedef struct SectionContext SectionContext; 47 44 48 static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type); 45 static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int stream_type); 46 static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code); 49 47 static AVStream *new_section_av_stream(SectionContext *sect, uint32_t code); 50 48 static SectionContext *add_section_stream(MpegTSContext *ts, int pid, int stream_type); 51 49 static void mpegts_cleanup_streams(MpegTSContext *ts); 52 50 static int is_desired_stream(int type); 53 51 static int find_in_list(const int *pids, int pid); 54 static void mpegts_push_section(void *opaque, const uint8_t *section, int section_len);55 52 56 53 enum MpegTSFilterType { 57 54 MPEGTS_PES, … … 85 82 static int is_pat_same(MpegTSContext *mpegts_ctx, 86 83 int *pmt_pnums, int *pmts_pids, uint pmt_count); 87 84 88 static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item , uint32_t prog_reg_desc, int pcr_pid);85 static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item); 89 86 static int is_pmt_same(MpegTSContext *mpegts_ctx, pmt_entry_t* items, 90 87 int item_cnt); 91 88 92 typedef intPESCallback(void *opaque, const uint8_t *buf, int len,93 int is_start, int64_t startpos);89 typedef void PESCallback(void *opaque, const uint8_t *buf, int len, 90 int is_start, int64_t startpos); 94 91 95 92 typedef struct MpegTSPESFilter { 96 93 PESCallback *pes_cb; … … 172 169 173 170 /** packet containing Audio/Video data */ 174 171 AVPacket *pkt; 175 /** to detect seek */176 int64_t last_pos;177 172 178 173 /******************************************/ 179 174 /* private mpegts data */ … … 214 209 215 210 enum MpegTSState { 216 211 MPEGTS_HEADER = 0, 217 MPEGTS_PESHEADER,218 212 MPEGTS_PESHEADER_FILL, 219 213 MPEGTS_PAYLOAD, 220 214 MPEGTS_SKIP, 221 215 }; 222 216 223 217 /* enough for PES header + length */ 224 #define PES_START_SIZE 6 225 #define PES_HEADER_SIZE 9 218 #define PES_START_SIZE 9 226 219 #define MAX_PES_HEADER_SIZE (9 + 255) 227 220 228 221 struct PESContext { 229 222 int pid; 230 int pcr_pid; /**< if -1 then all packets containing PCR are considered */231 223 int stream_type; 232 224 MpegTSContext *ts; 233 225 AVFormatContext *stream; … … 238 230 int total_size; 239 231 int pes_header_size; 240 232 int64_t pts, dts; 241 int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */242 233 uint8_t header[MAX_PES_HEADER_SIZE]; 243 uint8_t *buffer;234 int64_t startpos; 244 235 }; 245 236 246 237 extern AVInputFormat mpegts_demuxer; … … 249 240 struct SectionContext { 250 241 int pid; 251 242 int stream_type; 252 int new_packet;253 243 MpegTSContext *ts; 254 244 AVFormatContext *stream; 255 245 AVStream *st; … … 284 274 tss->section_index += len; 285 275 } 286 276 287 if (tss->section_cb == mpegts_push_section) { 288 SectionContext *sect = tss->opaque; 289 sect->new_packet = 1; 290 } 291 while (!tss->end_of_section_reached) { 277 while (1) { /* There may be several tables in this data. */ 292 278 /* compute section length if possible */ 293 279 if (tss->section_h_size == -1 && tss->section_index >= 3) { 294 len = ( AV_RB16(tss->section_buf + 1) & 0xfff) + 3;280 len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3; 295 281 if (len > 4096) 296 return;282 break; 297 283 tss->section_h_size = len; 298 284 } 299 285 300 286 if (tss->section_h_size == -1 || tss->section_index < tss->section_h_size) 301 287 break; 302 288 303 if (!tss->check_crc || 304 av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, 305 tss->section_buf, tss->section_h_size) == 0) 289 if (!tss->check_crc || av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, tss->section_buf, tss->section_h_size) == 0) 306 290 tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size); 307 291 308 292 if (tss->section_index > tss->section_h_size) { … … 312 296 tss->section_h_size = -1; 313 297 } else { 314 298 tss->end_of_section_reached = 1; 299 break; 315 300 } 316 301 } 317 302 } … … 324 309 MpegTSFilter *filter = ts->pids[pid]; 325 310 MpegTSSectionFilter *sec; 326 311 327 dprintf(ts->stream, "Filter: pid=0x%x\n", pid); 312 #ifdef DEBUG_SI 313 av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid); 314 #endif 328 315 329 330 316 if (NULL!=filter) { 331 #ifdef DEBUG 317 #ifdef DEBUG_SI 332 318 av_log(ts->stream, AV_LOG_DEBUG, "Filter Already Exists\n"); 333 319 #endif 334 320 mpegts_close_filter(ts, filter); … … 355 341 return filter; 356 342 } 357 343 358 staticMpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,344 MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid, 359 345 PESCallback *pes_cb, 360 346 void *opaque) 361 347 { … … 377 363 return filter; 378 364 } 379 365 380 staticvoid mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)366 void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter) 381 367 { 382 368 int pid; 383 369 … … 386 372 387 373 pid = filter->pid; 388 374 389 #ifdef DEBUG 375 #ifdef DEBUG_SI 390 376 av_log(NULL, AV_LOG_DEBUG, "Closing Filter: pid=0x%x\n", pid); 391 377 #endif 392 378 if (filter == ts->pmt_filter) … … 402 388 403 389 if (filter->type == MPEGTS_SECTION) 404 390 av_freep(&filter->u.section_filter.section_buf); 405 else if (filter->type == MPEGTS_PES) {406 PESContext *pes = filter->u.pes_filter.opaque;407 av_freep(&pes->buffer);408 /* referenced private data will be freed later in409 * av_close_input_stream */410 if (!((PESContext *)filter->u.pes_filter.opaque)->st) {411 av_freep(&filter->u.pes_filter.opaque);412 }413 }414 391 415 392 av_free(filter); 416 393 ts->pids[pid] = NULL; … … 451 428 score = analyze(buf, size, TS_PACKET_SIZE, NULL); 452 429 dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL); 453 430 fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL); 454 // av_log(NULL, AV_LOG_ERROR, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score); 455 431 /* av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", 432 score, dvhs_score, fec_score);*/ 433 456 434 if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE; 457 435 else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE; 458 436 else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE; … … 544 522 return -1; 545 523 h->last_sec_num = val; 546 524 547 #ifdef DEBUG 525 #ifdef DEBUG_SI 548 526 av_log(NULL, AV_LOG_DEBUG, "sid=0x%x sec_num=%d/%d\n", 549 527 h->id, h->sec_num, h->last_sec_num); 550 528 #endif 551 529 return 0; 552 530 } 553 531 554 typedef struct {555 uint32_t stream_type;556 enum CodecType codec_type;557 enum CodecID codec_id;558 } StreamType;559 560 static const StreamType ISO_types[] = {561 { 0x01, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },562 { 0x02, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },563 { 0x03, CODEC_TYPE_AUDIO, CODEC_ID_MP3 },564 { 0x04, CODEC_TYPE_AUDIO, CODEC_ID_MP3 },565 { 0x0f, CODEC_TYPE_AUDIO, CODEC_ID_AAC },566 { 0x10, CODEC_TYPE_VIDEO, CODEC_ID_MPEG4 },567 { 0x11, CODEC_TYPE_AUDIO, CODEC_ID_AAC_LATM },568 { 0x1b, CODEC_TYPE_VIDEO, CODEC_ID_H264 },569 { 0xd1, CODEC_TYPE_VIDEO, CODEC_ID_DIRAC },570 { 0xea, CODEC_TYPE_VIDEO, CODEC_ID_VC1 },571 { 0 },572 };573 574 static const StreamType HDMV_types[] = {575 { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },576 { 0x82, CODEC_TYPE_AUDIO, CODEC_ID_DTS },577 { 0x83, CODEC_TYPE_AUDIO, CODEC_ID_TRUEHD },578 { 0x84, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },579 { 0x85, CODEC_TYPE_AUDIO, CODEC_ID_DTS },580 { 0x86, CODEC_TYPE_AUDIO, CODEC_ID_DTS },581 { 0 },582 };583 584 /* ATSC ? */585 static const StreamType MISC_types[] = {586 { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },587 { 0x8a, CODEC_TYPE_AUDIO, CODEC_ID_DTS },588 { 0x100, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE },589 { 0x101, CODEC_TYPE_DATA, CODEC_ID_DVB_VBI },590 { 0 },591 };592 593 static const StreamType REGD_types[] = {594 { MKTAG('d','r','a','c'), CODEC_TYPE_VIDEO, CODEC_ID_DIRAC },595 { MKTAG('A','C','-','3'), CODEC_TYPE_AUDIO, CODEC_ID_AC3 },596 { 0 },597 };598 599 /* descriptor present */600 static const StreamType DESC_types[] = {601 { 0x6a, CODEC_TYPE_AUDIO, CODEC_ID_AC3 }, /* AC-3 descriptor */602 { 0x7a, CODEC_TYPE_AUDIO, CODEC_ID_EAC3 }, /* E-AC-3 descriptor */603 { 0x7b, CODEC_TYPE_AUDIO, CODEC_ID_DTS },604 { 0x59, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */605 { 0 },606 };607 608 static void mpegts_find_stream_type(AVStream *st,609 uint32_t stream_type, const StreamType *types)610 {611 for (; types->stream_type; types++) {612 if (stream_type == types->stream_type) {613 st->codec->codec_type = types->codec_type;614 st->codec->codec_id = types->codec_id;615 return;616 }617 }618 }619 620 static AVStream *new_pes_av_stream(PESContext *pes, uint32_t prog_reg_desc, uint32_t code)621 {622 AVStream *st = av_new_stream(pes->stream, pes->pid);623 624 if (!st)625 return NULL;626 627 av_set_pts_info(st, 33, 1, 90000);628 st->priv_data = pes;629 st->codec->codec_type = CODEC_TYPE_DATA;630 st->codec->codec_id = CODEC_ID_NONE;631 st->need_parsing = AVSTREAM_PARSE_FULL;632 pes->st = st;633 634 dprintf(pes->stream, "stream_type=%x pid=%x prog_reg_desc=%.4s\n",635 pes->stream_type, pes->pid, (char*)&prog_reg_desc);636 637 st->codec->codec_tag = pes->stream_type;638 639 mpegts_find_stream_type(st, pes->stream_type, ISO_types);640 if (prog_reg_desc == AV_RL32("HDMV") &&641 st->codec->codec_id == CODEC_ID_NONE)642 mpegts_find_stream_type(st, pes->stream_type, HDMV_types);643 if (st->codec->codec_id == CODEC_ID_NONE)644 mpegts_find_stream_type(st, pes->stream_type, MISC_types);645 646 /* stream was not present in PMT, guess based on PES start code */647 if (st->codec->codec_id == CODEC_ID_NONE) {648 if (code >= 0x1c0 && code <= 0x1df) {649 st->codec->codec_type = CODEC_TYPE_AUDIO;650 st->codec->codec_id = CODEC_ID_MP2;651 } else if (code == 0x1bd) {652 st->codec->codec_type = CODEC_TYPE_AUDIO;653 st->codec->codec_id = CODEC_ID_AC3;654 }655 }656 657 return st;658 }659 660 532 static MpegTSService *new_service(MpegTSContext *ts, int sid, int pid, 661 533 char *provider_name, char *name) 662 534 { 663 535 MpegTSService *service; 664 536 665 #ifdef DEBUG 537 #ifdef DEBUG_SI 666 538 av_log(ts->stream, AV_LOG_DEBUG, "new_service: " 667 539 "sid=0x%04x provider='%s' name='%s'\n", 668 540 sid, provider_name, name); … … 679 551 return service; 680 552 } 681 553 554 static int mpegts_parse_program_info_length(uint8_t **p, uint8_t *p_end) 555 { 556 int program_info_length = get16(p, p_end); 557 if (program_info_length < 0) 558 return -1; 559 program_info_length &= 0xfff; 560 *p += program_info_length; 561 if (*p >= p_end) 562 return -1; 563 return program_info_length; 564 } 565 682 566 static int find_in_list(const int *pids, int pid) { 683 567 int i; 684 568 for (i=0; i<PMT_PIDS_MAX; i++) … … 695 579 return -1; 696 580 pcr_pid &= 0x1fff; 697 581 mpegts_ctx->pcr_pid = pcr_pid; 698 #ifdef DEBUG 582 #ifdef DEBUG_SI 699 583 av_log(NULL, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid); 700 584 #endif 701 585 return pcr_pid; … … 716 600 717 601 int last_item = 0; 718 602 int desc_count = 0; 719 int program_info_length, pcr_pid, pid, stream_type;720 uint32_t prog_reg_desc = 0; /* registration descriptor */721 603 pmt_entry_t items[PMT_PIDS_MAX]; 722 604 bzero(&items, sizeof(pmt_entry_t) * PMT_PIDS_MAX); 723 605 724 606 mpegts_cleanup_streams(mpegts_ctx); /* in case someone else removed streams.. */ 725 607 726 #ifdef DEBUG 608 #ifdef DEBUG_SI 727 609 av_log(mpegts_ctx->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len); 728 610 av_hex_dump_log(mpegts_ctx->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len); 729 611 #endif … … 734 616 /* if we require a specific PMT, and this isn't it return silently */ 735 617 if (mpegts_ctx->req_sid >= 0 && header.id != mpegts_ctx->req_sid) 736 618 { 737 #ifdef DEBUG 619 #ifdef DEBUG_SI 738 620 av_log(NULL, AV_LOG_DEBUG, "We are looking for program 0x%x, not 0x%x", 739 621 mpegts_ctx->req_sid, header.id); 740 622 #endif … … 748 630 /* Extract the Program Clock Reference PID */ 749 631 if (mpegts_parse_pcrpid(mpegts_ctx, &p, p_end) < 0) 750 632 HANDLE_PMT_PARSE_ERROR("PCR PID"); 751 pcr_pid = mpegts_ctx->pcr_pid;752 633 753 program_info_length = get16(&p, p_end) & 0xfff; 754 if (program_info_length < 0) 755 return; 756 while(program_info_length >= 2) { 757 uint8_t tag, len; 758 tag = get8(&p, p_end); 759 len = get8(&p, p_end); 760 if(len > program_info_length - 2) 761 //something else is broken, exit the program_descriptors_loop 762 break; 763 program_info_length -= len + 2; 764 if(tag == 0x05 && len >= 4) { // registration descriptor 765 prog_reg_desc = bytestream_get_le32(&p); 766 len -= 4; 767 } 768 p += len; 769 } 770 p += program_info_length; 771 if (p >= p_end) 772 return; 634 /* Extract program info length, just so we can skip it */ 635 if (mpegts_parse_program_info_length(&p, p_end) < 0) 636 HANDLE_PMT_PARSE_ERROR("program info"); 773 637 774 638 /* parse new streams */ 775 639 while (p < p_end) 776 640 { 777 641 dvb_caption_info_t dvbci; 778 stream_type = get8(&p, p_end);779 pid = get16(&p, p_end);642 int stream_type = get8(&p, p_end); 643 int pid = get16(&p, p_end); 780 644 int desc_ok = mpegts_parse_desc(&dvbci, &p, p_end, &stream_type); 781 645 if ((stream_type < 0) || (pid < 0) || (desc_ok < 0)) 782 646 { … … 807 671 stream_type = STREAM_TYPE_VIDEO_MPEG2; 808 672 } 809 673 810 #ifdef DEBUG 674 #ifdef DEBUG_SI 811 675 av_log(mpegts_ctx->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", mpegts_ctx->pcr_pid); 812 676 #endif 813 677 … … 847 711 848 712 /* create new streams */ 849 713 for (idx = 0; idx < last_item; idx++) 850 mpegts_add_stream(mpegts_ctx, &items[idx] , prog_reg_desc, pcr_pid);714 mpegts_add_stream(mpegts_ctx, &items[idx]); 851 715 852 716 /* cache pmt */ 853 717 void *tmp0 = avctx->cur_pmt_sect; … … 896 760 int idx; 897 761 if (mpegts_ctx->pid_cnt != item_cnt) 898 762 { 899 #ifdef DEBUG 763 #ifdef DEBUG_SI 900 764 av_log(NULL, AV_LOG_DEBUG, "mpegts_ctx->pid_cnt=%d != item_cnt=%d\n", 901 765 mpegts_ctx->pid_cnt, item_cnt); 902 766 #endif … … 908 772 int loc = find_in_list(mpegts_ctx->pmt_pids, items[idx].pid); 909 773 if (loc < 0) 910 774 { 911 #ifdef DEBUG 775 #ifdef DEBUG_SI 912 776 av_log(NULL, AV_LOG_DEBUG, 913 777 "find_in_list(..,[%d].pid=%d) => -1\n" 914 778 "is_pmt_same() => false\n", … … 921 785 MpegTSFilter *tss = mpegts_ctx->pids[items[idx].pid]; 922 786 if (!tss) 923 787 { 924 #ifdef DEBUG 788 #ifdef DEBUG_SI 925 789 av_log(NULL, AV_LOG_DEBUG, 926 790 "mpegts_ctx->pids[items[%d].pid=%d] => null\n" 927 791 "is_pmt_same() => false\n", … … 934 798 PESContext *pes = (PESContext*) tss->u.pes_filter.opaque; 935 799 if (!pes) 936 800 { 937 #ifdef DEBUG 801 #ifdef DEBUG_SI 938 802 av_log(NULL, AV_LOG_DEBUG, "pes == null, where idx %d\n" 939 803 "is_pmt_same() => false\n", idx); 940 804 #endif … … 942 806 } 943 807 if (pes->stream_type != items[idx].type) 944 808 { 945 #ifdef DEBUG 809 #ifdef DEBUG_SI 946 810 av_log(NULL, AV_LOG_DEBUG, 947 811 "pes->stream_type != items[%d].type\n" 948 812 "is_pmt_same() => false\n", idx); … … 955 819 SectionContext *sect = (SectionContext*) tss->u.section_filter.opaque; 956 820 if (!sect) 957 821 { 958 #ifdef DEBUG 822 #ifdef DEBUG_SI 959 823 av_log(NULL, AV_LOG_DEBUG, "sect == null, where idx %d\n" 960 824 "is_pmt_same() => false\n", idx); 961 825 #endif … … 963 827 } 964 828 if (sect->stream_type != items[idx].type) 965 829 { 966 #ifdef DEBUG 830 #ifdef DEBUG_SI 967 831 av_log(NULL, AV_LOG_DEBUG, 968 832 "sect->stream_type != items[%d].type\n" 969 833 "is_pmt_same() => false\n", idx); … … 973 837 } 974 838 else 975 839 { 976 #ifdef DEBUG 840 #ifdef DEBUG_SI 977 841 av_log(NULL, AV_LOG_DEBUG, 978 842 "tss->type != MPEGTS_PES, where idx %d\n" 979 843 "is_pmt_same() => false\n", idx); … … 981 845 return 0; 982 846 } 983 847 } 984 #ifdef DEBUG 848 #ifdef DEBUG_SI 985 849 av_log(NULL, AV_LOG_DEBUG, "is_pmt_same() => true\n", idx); 986 850 #endif 987 851 return 1; … … 1003 867 case STREAM_TYPE_AUDIO_AAC_LATM: 1004 868 case STREAM_TYPE_AUDIO_AC3: 1005 869 case STREAM_TYPE_AUDIO_DTS: 1006 case STREAM_TYPE_AUDIO_HDMV_AC3_PLUS:1007 case STREAM_TYPE_AUDIO_HDMV_AC3_TRUE_HD:1008 case STREAM_TYPE_AUDIO_HDMV_DTS:1009 case STREAM_TYPE_AUDIO_HDMV_DTS_HD:1010 case STREAM_TYPE_AUDIO_HDMV_DTS_HD_MASTER:1011 870 //case STREAM_TYPE_PRIVATE_DATA: 1012 871 case STREAM_TYPE_VBI_DVB: 1013 872 case STREAM_TYPE_SUBTITLE_DVB: … … 1055 914 desc_end = *p + desc_len; 1056 915 if (desc_end > desc_list_end) 1057 916 break; 1058 1059 /* dprintf(ts->stream, "tag: 0x%02x len=%d\n", */ 1060 /* desc_tag, desc_len); */ 1061 1062 switch(desc_tag){917 #ifdef DEBUG_SI 918 av_log(NULL, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n", desc_tag, desc_len); 919 #endif 920 switch (desc_tag) 921 { 1063 922 case DVB_SUBT_DESCID: 1064 923 dvbci->language[0] = get8(p, desc_end); 1065 924 dvbci->language[1] = get8(p, desc_end); … … 1130 989 } 1131 990 } 1132 991 1133 static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item , uint32_t prog_reg_desc, int pcr_pid)992 static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item) 1134 993 { 1135 994 1136 995 av_log(NULL, AV_LOG_DEBUG, … … 1174 1033 } else { 1175 1034 PESContext *pes = NULL; 1176 1035 AVStream *st = NULL; 1177 pes = add_pes_stream(ts, item->pid, pcr_pid,item->type);1036 pes = add_pes_stream(ts, item->pid, item->type); 1178 1037 if (!pes) 1179 1038 { 1180 1039 av_log(NULL, AV_LOG_ERROR, "mpegts_add_stream: " … … 1184 1043 } 1185 1044 1186 1045 /* Pretend it's audio if we have a language. */ 1187 st = new_pes_av_stream(pes, prog_reg_desc,0);1046 st = new_pes_av_stream(pes, item->dvbci.language[0] ? 0x1c0 : 0); 1188 1047 if (!st) 1189 1048 { 1190 1049 av_log(NULL, AV_LOG_ERROR, "mpegts_add_stream: " … … 1251 1110 uint pmt_count = 0; 1252 1111 int i; 1253 1112 1254 #ifdef DEBUG 1113 #ifdef DEBUG_SI 1255 1114 av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n"); 1256 1115 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len); 1257 1116 #endif … … 1282 1141 1283 1142 pmt_count++; 1284 1143 1285 #ifdef DEBUG 1144 #ifdef DEBUG_SI 1286 1145 av_log(ts->stream, AV_LOG_DEBUG, 1287 1146 "MPEG Program Number=0x%x pid=0x%x req_sid=0x%x\n", 1288 1147 pmt_pnums[i], pmt_pids[i], ts->req_sid); … … 1291 1150 1292 1151 if (!is_pat_same(ts, pmt_pnums, pmt_pids, pmt_count)) 1293 1152 { 1294 #ifdef DEBUG 1153 #ifdef DEBUG_SI 1295 1154 av_log(NULL, AV_LOG_DEBUG, "New PAT!\n"); 1296 1155 #endif 1297 1156 /* if there were services, get rid of them */ … … 1324 1183 * add a filter for the PMT. */ 1325 1184 if (ts->req_sid == pmt_pnums[i]) 1326 1185 { 1327 #ifdef DEBUG 1186 #ifdef DEBUG_SI 1328 1187 av_log(NULL, AV_LOG_DEBUG, "Found program number!\n"); 1329 1188 #endif 1330 1189 /* close old filter if it doesn't match */ … … 1359 1218 * tell parser it is safe to quit. */ 1360 1219 if (ts->req_sid < 0 && ts->scanning) 1361 1220 { 1362 #ifdef DEBUG 1221 #ifdef DEBUG_SI 1363 1222 av_log(NULL, AV_LOG_DEBUG, "Found PAT, ending scan\n"); 1364 1223 #endif 1365 1224 ts->stop_parse = 1; … … 1370 1229 * and tell parser it is safe to quit. */ 1371 1230 if (ts->req_sid >= 0 && !found) 1372 1231 { 1373 #ifdef DEBUG 1232 #ifdef DEBUG_SI 1374 1233 av_log(NULL, AV_LOG_DEBUG, "Program 0x%x is not in PAT, ending scan\n", 1375 1234 ts->req_sid); 1376 1235 #endif … … 1390 1249 char *provider_name, *name; 1391 1250 char buf[256]; 1392 1251 1393 #ifdef DEBUG 1394 dprintf(ts->stream, "PAT:\n");1252 #ifdef DEBUG_SI 1253 av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n"); 1395 1254 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len); 1396 1255 #endif 1397 1256 p_end = section + section_len - 4; … … 1408 1267 pmt_pid = get16(&p, p_end) & 0x1fff; 1409 1268 if (pmt_pid < 0) 1410 1269 break; 1411 1412 dprintf(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);1413 1270 #ifdef DEBUG_SI 1271 av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid); 1272 #endif 1414 1273 if (sid == 0x0000) { 1415 1274 /* NIT info */ 1416 1275 } else { … … 1431 1290 /* remove filter */ 1432 1291 mpegts_close_filter(ts, ts->pat_filter); 1433 1292 ts->pat_filter = NULL; 1434 #ifdef DEBUG 1293 #ifdef DEBUG_SI 1435 1294 av_log(NULL, AV_LOG_DEBUG, "end of scan PAT\n"); 1436 1295 #endif 1437 1296 } … … 1446 1305 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type; 1447 1306 char *name, *provider_name; 1448 1307 1449 #ifdef DEBUG 1450 dprintf(ts->stream, "SDT:\n");1308 #ifdef DEBUG_SI 1309 av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n"); 1451 1310 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len); 1452 1311 #endif 1453 1312 … … 1484 1343 desc_end = p + desc_len; 1485 1344 if (desc_end > desc_list_end) 1486 1345 break; 1487 1488 dprintf(ts->stream, "tag: 0x%02x len=%d\n",1346 #ifdef DEBUG_SI 1347 av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n", 1489 1348 desc_tag, desc_len); 1490 1349 #endif 1491 1350 switch(desc_tag) { 1492 1351 case 0x48: 1493 1352 service_type = get8(&p, p_end); … … 1518 1377 } 1519 1378 #endif 1520 1379 1521 static int64_t get_pts(const uint8_t *p) 1380 #if 0 1381 /* scan services in a transport stream by looking at the SDT */ 1382 void mpegts_scan_sdt(MpegTSContext *ts) 1522 1383 { 1523 int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30; 1524 pts |= (AV_RB16(p + 1) >> 1) << 15; 1525 pts |= AV_RB16(p + 3) >> 1; 1526 return pts; 1384 ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID, 1385 sdt_cb, ts, 1); 1527 1386 } 1387 #endif 1528 1388 1529 static void new_pes_packet(PESContext *pes, AVPacket *pkt) 1389 #if 0 1390 /* scan services in a transport stream by looking at the PAT (better 1391 than nothing !) */ 1392 void mpegts_scan_pat(MpegTSContext *ts) 1530 1393 { 1531 av_init_packet(pkt); 1394 ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID, 1395 pat_scan_cb, ts, 1); 1396 } 1397 #endif 1532 1398 1533 pkt->destruct = av_destruct_packet; 1534 pkt->data = pes->buffer; 1535 pkt->size = pes->data_index; 1536 if (pkt->data && pkt->size) 1537 memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); 1399 static int64_t get_pts(const uint8_t *p) 1400 { 1401 int64_t pts; 1402 int val; 1538 1403 1539 pkt->stream_index = pes->st->index; 1540 pkt->pts = pes->pts; 1541 pkt->dts = pes->dts; 1542 /* store position of first TS packet of this PES packet */ 1543 pkt->pos = pes->ts_packet_pos; 1544 1545 /* reset pts values */ 1546 pes->pts = AV_NOPTS_VALUE; 1547 pes->dts = AV_NOPTS_VALUE; 1548 pes->buffer = NULL; 1549 pes->data_index = 0; 1550 pes->total_size = 0; 1404 pts = (int64_t)((p[0] >> 1) & 0x07) << 30; 1405 val = (p[1] << 8) | p[2]; 1406 pts |= (int64_t)(val >> 1) << 15; 1407 val = (p[3] << 8) | p[4]; 1408 pts |= (int64_t)(val >> 1); 1409 return pts; 1551 1410 } 1552 1411 1412 1553 1413 static void init_stream(AVStream *st, int stream_type, int code) 1554 1414 { 1555 1415 int codec_type=-1, codec_id=-1; … … 1589 1449 codec_id = CODEC_ID_AC3; 1590 1450 break; 1591 1451 case STREAM_TYPE_AUDIO_DTS: 1592 codec_type = CODEC_TYPE_AUDIO;1593 codec_id = CODEC_ID_DTS;1452 codec_type = CODEC_TYPE_AUDIO; 1453 codec_id = CODEC_ID_DTS; 1594 1454 break; 1595 1455 case STREAM_TYPE_VBI_DVB: 1596 1456 codec_type = CODEC_TYPE_DATA; … … 1623 1483 av_set_pts_info(st, 33, 1, 90000); 1624 1484 } 1625 1485 1486 static AVStream *new_pes_av_stream(PESContext *pes, uint32_t code) 1487 { 1488 CHECKED_ALLOCZ(pes->st, sizeof(AVStream)); 1489 pes->st->codec = avcodec_alloc_context(); 1490 init_stream(pes->st, pes->stream_type, code); /* sets codec type and id */ 1491 pes->st->priv_data = pes; 1492 pes->st->need_parsing = AVSTREAM_PARSE_FULL; 1493 1494 pes->st = av_add_stream(pes->stream, pes->st, pes->pid); 1495 fail: /*for the CHECKED_ALLOCZ macro*/ 1496 return pes->st; 1497 } 1498 1626 1499 static AVStream *new_section_av_stream(SectionContext *sect, uint32_t code) 1627 1500 { 1628 1501 CHECKED_ALLOCZ(sect->st, sizeof(AVStream)); … … 1638 1511 1639 1512 1640 1513 /* return non zero if a packet could be constructed */ 1641 static intmpegts_push_data(void *opaque,1642 const uint8_t *buf, int buf_size, int is_start,1643 int64_t pos)1514 static void mpegts_push_data(void *opaque, 1515 const uint8_t *buf, int buf_size, int is_start, 1516 int64_t position) 1644 1517 { 1645 1518 PESContext *pes = opaque; 1646 1519 MpegTSContext *ts = pes->ts; 1647 1520 const uint8_t *p; 1648 1521 int len, code; 1649 1522 1650 if(!ts->pkt)1651 return 0;1652 1653 1523 if (is_start) { 1654 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) { 1655 new_pes_packet(pes, ts->pkt); 1656 ts->stop_parse = 1; 1657 } 1524 pes->startpos = position; 1658 1525 pes->state = MPEGTS_HEADER; 1659 1526 pes->data_index = 0; 1660 pes->ts_packet_pos = pos;1661 1527 } 1662 1528 p = buf; 1663 1529 while (buf_size > 0) { … … 1680 1546 pes->header[2] == 0x01) { 1681 1547 /* it must be an mpeg2 PES stream */ 1682 1548 code = pes->header[3] | 0x100; 1683 dprintf(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code); 1684 1685 if ((!pes->st && pes->stream->nb_streams == MAX_STREAMS) || 1686 (pes->st && pes->st->discard == AVDISCARD_ALL) || 1687 /* !((code >= 0x1c0 && code <= 0x1df) || */ 1688 /* (code >= 0x1e0 && code <= 0x1ef) || */ 1689 /* (code == 0x1bd) || (code == 0x1fd)) || */ 1690 code == 0x1be) /* padding_stream */ 1549 if (!((code >= 0x1c0 && code <= 0x1df) || 1550 (code >= 0x1e0 && code <= 0x1ef) || 1551 (code == 0x1bd) || (code == 0x1fd))) 1691 1552 goto skip; 1692 1693 /* stream not present in PMT */1694 if (!pes->st)1695 pes->st = new_pes_av_stream(pes, 0, code);1696 if (!pes->st)1697 return AVERROR(ENOMEM);1698 1699 pes->total_size = AV_RB16(pes->header + 4);1553 if (!pes->st && 0 == new_pes_av_stream(pes, code)) { 1554 av_log(NULL, AV_LOG_ERROR, 1555 "Error: new_pes_av_stream() " 1556 "failed in mpegts_push_data\n"); 1557 goto skip; 1558 } 1559 pes->state = MPEGTS_PESHEADER_FILL; 1560 pes->total_size = (pes->header[4] << 8) | pes->header[5]; 1700 1561 /* NOTE: a zero total size means the PES size is 1701 1562 unbounded */ 1702 if (!pes->total_size) 1703 pes->total_size = MAX_PES_PAYLOAD; 1704 1705 /* allocate pes buffer */ 1706 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE); 1707 if (!pes->buffer) 1708 return AVERROR(ENOMEM); 1709 1710 if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */ 1711 code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */ 1712 code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */ 1713 code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */ 1714 pes->state = MPEGTS_PESHEADER; 1715 if (pes->st->codec->codec_id == CODEC_ID_NONE) { 1716 dprintf(pes->stream, "pid=%x stream_type=%x probing\n", 1717 pes->pid, pes->stream_type); 1718 pes->st->codec->codec_id = CODEC_ID_PROBE; 1719 } 1720 } else { 1721 pes->state = MPEGTS_PAYLOAD; 1722 pes->data_index = 0; 1723 } 1563 if (pes->total_size) 1564 pes->total_size += 6; 1565 pes->pes_header_size = pes->header[8] + 9; 1724 1566 } else { 1725 1567 /* otherwise, it should be a table */ 1726 1568 /* skip packet */ … … 1732 1574 break; 1733 1575 /**********************************************/ 1734 1576 /* PES packing parsing */ 1735 case MPEGTS_PESHEADER:1736 len = PES_HEADER_SIZE - pes->data_index;1737 if (len < 0)1738 return -1;1739 if (len > buf_size)1740 len = buf_size;1741 memcpy(pes->header + pes->data_index, p, len);1742 pes->data_index += len;1743 p += len;1744 buf_size -= len;1745 if (pes->data_index == PES_HEADER_SIZE) {1746 pes->pes_header_size = pes->header[8] + 9;1747 pes->state = MPEGTS_PESHEADER_FILL;1748 }1749 break;1750 1577 case MPEGTS_PESHEADER_FILL: 1751 1578 len = pes->pes_header_size - pes->data_index; 1752 if (len < 0)1753 return -1;1754 1579 if (len > buf_size) 1755 1580 len = buf_size; 1756 1581 memcpy(pes->header + pes->data_index, p, len); … … 1766 1591 pes->pts = AV_NOPTS_VALUE; 1767 1592 pes->dts = AV_NOPTS_VALUE; 1768 1593 if ((flags & 0xc0) == 0x80) { 1769 pes-> dts = pes->pts = get_pts(r);1594 pes->pts = get_pts(r); 1770 1595 r += 5; 1771 1596 } else if ((flags & 0xc0) == 0xc0) { 1772 1597 pes->pts = get_pts(r); … … 1774 1599 pes->dts = get_pts(r); 1775 1600 r += 5; 1776 1601 } 1777 1778 1602 /* we got the full header. We parse it and get the payload */ 1779 1603 pes->state = MPEGTS_PAYLOAD; 1780 pes->data_index = 0;1781 1604 } 1782 1605 break; 1783 1606 case MPEGTS_PAYLOAD: 1784 if (buf_size > 0) { 1785 if (pes->data_index+buf_size > pes->total_size) { 1786 new_pes_packet(pes, ts->pkt); 1787 pes->total_size = MAX_PES_PAYLOAD; 1788 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE); 1789 if (!pes->buffer) 1790 return AVERROR(ENOMEM); 1607 if (pes->total_size) { 1608 len = pes->total_size - pes->data_index; 1609 if (len > buf_size) 1610 len = buf_size; 1611 } else { 1612 len = buf_size; 1613 } 1614 if (len > 0) { 1615 AVPacket *pkt = ts->pkt; 1616 if (pkt && pes->st && av_new_packet(pkt, len) == 0) { 1617 memcpy(pkt->data, p, len); 1618 pkt->stream_index = pes->st->index; 1619 pkt->pts = pes->pts; 1620 pkt->dts = pes->dts; 1621 pkt->pos = pes->startpos; 1622 /* reset pts values */ 1623 pes->pts = AV_NOPTS_VALUE; 1624 pes->dts = AV_NOPTS_VALUE; 1791 1625 ts->stop_parse = 1; 1626 return; 1792 1627 } 1793 memcpy(pes->buffer+pes->data_index, p, buf_size);1794 pes->data_index += buf_size;1795 1628 } 1796 1629 buf_size = 0; 1797 1630 break; … … 1800 1633 break; 1801 1634 } 1802 1635 } 1803 1804 return 0;1805 1636 } 1806 1637 1807 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, intstream_type)1638 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int stream_type) 1808 1639 { 1809 1640 MpegTSFilter *tss = ts->pids[pid]; 1810 1641 PESContext *pes = 0; … … 1826 1657 pes->ts = ts; 1827 1658 pes->stream = ts->stream; 1828 1659 pes->pid = pid; 1829 pes->pcr_pid = pcr_pid;1830 1660 pes->stream_type = stream_type; 1831 pes->state = MPEGTS_SKIP;1832 1661 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes); 1833 1662 if (!tss) { 1834 1663 av_free(pes); … … 1852 1681 SectionHeader header; 1853 1682 AVPacket *pkt = ts->pkt; 1854 1683 const uint8_t *p = section, *p_end = section + section_len - 4; 1855 1856 1684 if (parse_section_header(&header, &p, p_end) < 0) 1857 1685 { 1858 1686 av_log(NULL, AV_LOG_DEBUG, "Unable to parse header\n"); 1859 1687 return; 1860 1688 } 1861 1862 if (sect->new_packet && pkt && sect->st) { 1863 int pktLen = section_len + 184; /* Add enough for a complete TS payload. */ 1864 sect->new_packet = 0; 1865 if (av_new_packet(pkt, pktLen) == 0) { 1866 memcpy(pkt->data, section, section_len); 1867 memset(pkt->data+section_len, 0xff, pktLen-section_len); 1868 pkt->stream_index = sect->st->index; 1869 ts->stop_parse = 1; 1870 } 1871 } else if (pkt->data) { /* We've already added at least one table. */ 1689 if (pkt->data) { /* We've already added at least one table. */ 1872 1690 uint8_t *data = pkt->data; 1873 1691 int space = pkt->size; 1874 1692 int table_size = 0; … … 1884 1702 return; 1885 1703 } 1886 1704 memcpy(data, section, section_len); 1705 } else if (pkt && sect->st) { 1706 int pktLen = section_len + 184; /* Add enough for a complete TS payload. */ 1707 if (av_new_packet(pkt, pktLen) == 0) { 1708 memcpy(pkt->data, section, section_len); 1709 memset(pkt->data+section_len, 0xff, pktLen-section_len); 1710 pkt->stream_index = sect->st->index; 1711 pkt->pts = AV_NOPTS_VALUE; 1712 pkt->dts = AV_NOPTS_VALUE; 1713 pkt->pos = 0; 1714 ts->stop_parse = 1; 1715 } 1887 1716 } 1888 1717 } 1889 1718 … … 1923 1752 1924 1753 1925 1754 /* handle one TS packet */ 1926 static int handle_packet(MpegTSContext *ts, const uint8_t *packet) 1755 static void handle_packet(MpegTSContext *ts, const uint8_t *packet, 1756 int64_t position) 1927 1757 { 1928 1758 AVFormatContext *s = ts->stream; 1929 1759 MpegTSFilter *tss; 1930 1760 int len, pid, cc, cc_ok, afc, is_start; 1931 1761 const uint8_t *p, *p_end; 1932 int64_t pos;1933 1762 1934 pid = AV_RB16(packet + 1) & 0x1fff;1935 1936 1763 if (!ts->pids[0]) { 1937 1764 /* make sure we're always scanning for new PAT's */ 1938 1765 ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1); 1939 1766 } 1940 1767 1768 pid = ((packet[1] & 0x1f) << 8) | packet[2]; 1941 1769 is_start = packet[1] & 0x40; 1942 1943 1770 tss = ts->pids[pid]; 1944 1771 if (ts->auto_guess && tss == NULL && is_start) { 1945 add_pes_stream(ts, pid, -1,0);1772 add_pes_stream(ts, pid, 0); 1946 1773 tss = ts->pids[pid]; 1947 1774 } 1948 1775 if (!tss) 1949 return 0;1776 return; 1950 1777 1951 1778 /* continuity check (currently not used) */ 1952 1779 cc = (packet[3] & 0xf); … … 1957 1784 afc = (packet[3] >> 4) & 3; 1958 1785 p = packet + 4; 1959 1786 if (afc == 0) /* reserved value */ 1960 return 0;1787 return; 1961 1788 if (afc == 2) /* adaptation field only */ 1962 return 0;1789 return; 1963 1790 if (afc == 3) { 1964 1791 /* skip adapation field */ 1965 1792 p += p[0] + 1; … … 1967 1794 /* if past the end of packet, ignore */ 1968 1795 p_end = packet + TS_PACKET_SIZE; 1969 1796 if (p >= p_end) 1970 return 0;1797 return; 1971 1798 1972 pos = url_ftell(ts->stream->pb); 1973 ts->pos47= pos % ts->raw_packet_size; 1799 ts->pos47= url_ftell(ts->stream->pb) % ts->raw_packet_size; 1974 1800 1975 1801 if (tss->type == MPEGTS_SECTION) { 1976 1802 if (is_start) { 1977 1803 /* pointer field present */ 1978 1804 len = *p++; 1979 1805 if (p + len > p_end) 1980 return 0;1806 return; 1981 1807 if (len && cc_ok) { 1982 1808 /* write remaining section bytes */ 1983 1809 write_section_data(s, tss, 1984 1810 p, len, 0); 1985 1811 /* check whether filter has been closed */ 1986 1812 if (!ts->pids[pid]) 1987 return 0;1813 return; 1988 1814 } 1989 1815 p += len; 1990 1816 if (p < p_end) { … … 1998 1824 } 1999 1825 } 2000 1826 } else { 2001 int ret; 2002 // Note: The position here points actually behind the current packet. 2003 if ((ret = tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque, 2004 p, p_end - p, is_start, 2005 pos - ts->raw_packet_size)) < 0) 2006 return ret; 1827 tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque, 1828 p, p_end - p, is_start, position); 2007 1829 } 2008 2009 return 0;2010 1830 } 2011 1831 2012 1832 /* XXX: try to find a better synchro over several packets (use … … 2029 1849 } 2030 1850 2031 1851 /* return -1 if error or EOF. Return 0 if OK. */ 2032 static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size) 1852 static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size, 1853 int64_t *position) 2033 1854 { 2034 1855 int skip, len; 2035 1856 2036 1857 for(;;) { 1858 *position = url_ftell(pb); 2037 1859 len = get_buffer(pb, buf, TS_PACKET_SIZE); 2038 1860 if (len != TS_PACKET_SIZE) 2039 1861 return AVERROR(EIO); … … 2061 1883 ByteIOContext *pb = s->pb; 2062 1884 uint8_t packet[TS_PACKET_SIZE]; 2063 1885 int packet_num, ret; 1886 int64_t pos; 2064 1887 2065 1888 ts->stop_parse = 0; 2066 1889 packet_num = 0; 2067 1890 for(;;) { 2068 if (ts->stop_parse >0)1891 if (ts->stop_parse) 2069 1892 break; 2070 1893 packet_num++; 2071 if (nb_packets != 0 && packet_num * ts->raw_packet_size>= nb_packets)1894 if (nb_packets != 0 && packet_num >= nb_packets) 2072 1895 break; 2073 ret = read_packet(pb, packet, ts->raw_packet_size );1896 ret = read_packet(pb, packet, ts->raw_packet_size, &pos); 2074 1897 if (ret != 0) 2075 1898 return ret; 2076 ret = handle_packet(ts, packet); 2077 if (ret != 0) 2078 return ret; 1899 handle_packet(ts, packet, pos); 2079 1900 } 2080 1901 return 0; 2081 1902 } … … 2085 1906 #if 1 2086 1907 const int size= p->buf_size; 2087 1908 int score, fec_score, dvhs_score; 2088 int check_count= size / TS_FEC_PACKET_SIZE;2089 1909 #define CHECK_COUNT 10 2090 1910 2091 if ( check_count < CHECK_COUNT)1911 if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT)) 2092 1912 return -1; 2093 1913 2094 score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count; 2095 dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count; 2096 fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count; 2097 // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score); 2098 2099 // we need a clear definition for the returned score otherwise things will become messy sooner or later 1914 score = analyze(p->buf, TS_PACKET_SIZE *CHECK_COUNT, TS_PACKET_SIZE, NULL); 1915 dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE * CHECK_COUNT, 1916 TS_DVHS_PACKET_SIZE, NULL); 1917 fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL); 1918 #if 0 1919 av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n", 1920 score, fec_score); 1921 #endif 1922 1923 /* we need a clear definition for the returned score 1924 otherwise things will become messy sooner or later */ 2100 1925 if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT; 2101 1926 else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT; 2102 1927 else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT; … … 2110 1935 #endif 2111 1936 } 2112 1937 2113 /* return the 90kHz PCR and the extension for the 27MHz PCR. return 1938 #if 0 1939 static void set_service_cb(void *opaque, int ret) 1940 { 1941 MpegTSContext *ts = opaque; 1942 ts->set_service_ret = ret; 1943 ts->stop_parse = 1; 1944 } 1945 #endif 1946 1947 /* return the 90 kHz PCR and the extension for the 27 MHz PCR. return 2114 1948 (-1) if not available */ 2115 1949 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, 2116 1950 const uint8_t *packet) … … 2144 1978 { 2145 1979 MpegTSContext *ts = s->priv_data; 2146 1980 ByteIOContext *pb = s->pb; 2147 uint8_t buf[ 5*1024];1981 uint8_t buf[1024]; 2148 1982 int len, sid, i; 2149 1983 int64_t pos; 2150 1984 MpegTSService *service; 2151 1985 2152 1986 if (ap) { 1987 ts->mpeg2ts_raw = ap->mpeg2ts_raw; 2153 1988 ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr; 2154 if(ap->mpeg2ts_raw){2155 av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");2156 return -1;2157 }2158 1989 } 2159 1990 2160 1991 /* read the first 1024 bytes to get packet size */ 2161 1992 pos = url_ftell(pb); 2162 1993 len = get_buffer(pb, buf, sizeof(buf)); 2163 if (len != sizeof(buf)) 2164 { 1994 if (len != sizeof(buf)) { 2165 1995 av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: " 2166 1996 "unable to read first 1024 bytes\n"); 2167 1997 goto fail; 2168 1998 } 2169 1999 ts->raw_packet_size = get_packet_size(buf, sizeof(buf)); 2170 av_log(NULL, AV_LOG_DEBUG, "mpegts_read_header: TS packet size = %d\n", ts->raw_packet_size); 2171 if (ts->raw_packet_size <= 0) 2172 { 2000 if (ts->raw_packet_size <= 0) { 2173 2001 av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: " 2174 2002 "packet size incorrect\n"); 2175 2003 goto fail; … … 2177 2005 ts->stream = s; 2178 2006 ts->auto_guess = 0; 2179 2007 2180 if ( s->iformat == &mpegts_demuxer) {2008 if (!ts->mpeg2ts_raw) { 2181 2009 /* normal demux */ 2182 2010 2183 2011 if (!ts->auto_guess) { 2184 /* first do a scaning to get all the services */2185 url_fseek(pb, pos, SEEK_SET);2186 2187 2012 /* SDT Scan Removed here. It caused startup delays in TS files 2188 SDT will not exist in a stripped TS file created by myth. 2189 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1); 2190 */ 2013 SDT will not exist in a stripped TS file created by myth. */ 2191 2014 2192 2015 /* we don't want any PMT pid filters created on first pass */ 2193 2016 ts->req_sid = -1; 2194 2017 2195 2018 ts->scanning = 1; 2196 ts->pat_filter = 2197 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);2198 2199 handle_packets(ts, s->probesize);2019 ts->pat_filter = mpegts_open_section_filter( 2020 ts, PAT_PID, pat_cb, ts, 1); 2021 url_fseek(pb, pos, SEEK_SET); 2022 handle_packets(ts, s->probesize); 2200 2023 ts->scanning = 0; 2201 2024 2202 2025 if (ts->nb_services <= 0) { … … 2213 2036 (ts->pmt_scan_state == PMT_NOT_YET_FOUND)); i++) 2214 2037 { 2215 2038 service = ts->services[i]; 2216 #ifdef DEBUG 2039 #ifdef DEBUG_SI 2217 2040 av_log(ts->stream, AV_LOG_DEBUG, "Tuning to '%s' pnum: 0x%x\n", 2218 2041 service->name, service->sid); 2219 2042 #endif … … 2252 2075 goto fail; 2253 2076 } 2254 2077 2255 dprintf(ts->stream, "tuning done\n"); 2256 2078 #ifdef DEBUG_SI 2079 av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n"); 2080 #endif 2257 2081 } 2258 2082 s->ctx_flags |= AVFMTCTX_NOHEADER; 2259 2083 } else { 2260 2084 AVStream *st; 2261 2085 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l; 2262 int64_t pcrs[2], pcr_h ;2086 int64_t pcrs[2], pcr_h, position; 2263 2087 int packet_count[2]; 2264 2088 uint8_t packet[TS_PACKET_SIZE]; 2265 2089 … … 2267 2091 2268 2092 do_pcr: 2269 2093 st = av_new_stream(s, 0); 2270 if (!st) 2271 { 2094 if (!st) { 2272 2095 av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: " 2273 2096 "av_new_stream() failed\n"); 2274 2097 goto fail; 2275 2098 } 2276 av_set_pts_info(st, 60, 1, 27000000);2099 av_set_pts_info(st, 33, 1, 27000000); 2277 2100 st->codec->codec_type = CODEC_TYPE_DATA; 2278 2101 st->codec->codec_id = CODEC_ID_MPEG2TS; 2279 2102 … … 2282 2105 nb_pcrs = 0; 2283 2106 nb_packets = 0; 2284 2107 for(;;) { 2285 ret = read_packet(s->pb, packet, ts->raw_packet_size); 2286 if (ret < 0) 2287 { 2108 ret = read_packet(s->pb, packet, ts->raw_packet_size, &position); 2109 if (ret < 0) { 2288 2110 av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: " 2289 2111 "read_packet() failed\n"); 2290 return -1;2112 goto fail; 2291 2113 } 2292 pid = AV_RB16(packet + 1) & 0x1fff;2114 pid = ((packet[1] & 0x1f) << 8) | packet[2]; 2293 2115 if ((pcr_pid == -1 || pcr_pid == pid) && 2294 2116 parse_pcr(&pcr_h, &pcr_l, packet) == 0) { 2295 2117 pcr_pid = pid; … … 2329 2151 { 2330 2152 MpegTSContext *ts = s->priv_data; 2331 2153 int ret, i; 2332 int64_t pcr_h, next_pcr_h, pos ;2154 int64_t pcr_h, next_pcr_h, pos, position; 2333 2155 int pcr_l, next_pcr_l; 2334 2156 uint8_t pcr_buf[12]; 2335 2157 2336 2158 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0) 2337 2159 return AVERROR(ENOMEM); 2338 pkt->pos= url_ftell(s->pb); 2339 ret = read_packet(s->pb, pkt->data, ts->raw_packet_size); 2160 ret = read_packet(s->pb, pkt->data, ts->raw_packet_size, &position); 2340 2161 if (ret < 0) { 2341 2162 av_free_packet(pkt); 2342 2163 return ret; … … 2372 2193 AVPacket *pkt) 2373 2194 { 2374 2195 MpegTSContext *ts = s->priv_data; 2375 int ret, i;2376 2196 2377 if (url_ftell(s->pb) != ts->last_pos) { 2378 /* seek detected, flush pes buffer */ 2379 for (i = 0; i < NB_PID_MAX; i++) { 2380 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) { 2381 PESContext *pes = ts->pids[i]->u.pes_filter.opaque; 2382 av_freep(&pes->buffer); 2383 pes->data_index = 0; 2384 pes->state = MPEGTS_SKIP; /* skip until pes header */ 2385 } 2386 } 2197 if (!ts->mpeg2ts_raw) { 2198 ts->pkt = pkt; 2199 return handle_packets(ts, 0); 2200 } else { 2201 return mpegts_raw_read_packet(s, pkt); 2387 2202 } 2388 2389 ts->pkt = pkt;2390 ret = handle_packets(ts, 0);2391 if (ret < 0) {2392 /* flush pes data left */2393 for (i = 0; i < NB_PID_MAX; i++) {2394 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {2395 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;2396 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {2397 new_pes_packet(pes, pkt);2398 ret = 0;2399 break;2400 }2401 }2402 }2403 }2404 2405 ts->last_pos = url_ftell(s->pb);2406 2407 return ret;2408 2203 } 2409 2204 2410 2205 static int mpegts_read_close(AVFormatContext *s) … … 2512 2307 const uint8_t *buf, int len) 2513 2308 { 2514 2309 int len1; 2310 int64_t position = 0; 2515 2311 2516 2312 len1 = len; 2517 2313 ts->pkt = pkt; 2518 2314 ts->stop_parse = 0; 2519 2315 for(;;) { 2520 if (ts->stop_parse >0)2316 if (ts->stop_parse) 2521 2317 break; 2522 2318 if (len < TS_PACKET_SIZE) 2523 2319 return -1; … … 2525 2321 buf++; 2526 2322 len--; 2527 2323 } else { 2528 handle_packet(ts, buf );2324 handle_packet(ts, buf, position); 2529 2325 buf += TS_PACKET_SIZE; 2530 2326 len -= TS_PACKET_SIZE; 2531 2327 } -
libs/libavformat/mpegts.h
1 1 /* 2 2 * MPEG2 transport stream defines 3 * Copyright (c) 2003 Fabrice Bellard 3 * Copyright (c) 2003 Fabrice Bellard. 4 4 * 5 5 * This file is part of FFmpeg. 6 6 * … … 39 39 #define PMT_TID 0x02 40 40 #define SDT_TID 0x42 41 41 42 /* descriptor ids */ 42 43 #define DVB_CAROUSEL_ID 0x13 43 44 #define DVB_VBI_DATA_ID 0x45 44 45 #define DVB_VBI_TELETEXT_ID 0x46 … … 46 47 #define DVB_SUBT_DESCID 0x59 47 48 #define DVB_BROADCAST_ID 0x66 48 49 #define DVB_DATA_STREAM 0x52 50 49 51 #define STREAM_TYPE_VIDEO_MPEG1 0x01 50 52 #define STREAM_TYPE_VIDEO_MPEG2 0x02 51 53 #define STREAM_TYPE_AUDIO_MPEG1 0x03 … … 62 64 63 65 #define STREAM_TYPE_AUDIO_AC3 0x81 64 66 #define STREAM_TYPE_AUDIO_DTS 0x8a 65 #define STREAM_TYPE_AUDIO_HDMV_AC3_PLUS 0x8466 #define STREAM_TYPE_AUDIO_HDMV_AC3_TRUE_HD 0x8367 #define STREAM_TYPE_AUDIO_HDMV_DTS 0x8268 #define STREAM_TYPE_AUDIO_HDMV_DTS_HD 0x8569 #define STREAM_TYPE_AUDIO_HDMV_DTS_HD_MASTER 0x8670 67 71 68 #define STREAM_TYPE_SUBTITLE_DVB 0x100 72 69 #define STREAM_TYPE_VBI_DVB 0x101