Index: libs/libavformat/mpegts.c
===================================================================
--- libs/libavformat/mpegts.c	(revision 22779)
+++ libs/libavformat/mpegts.c	(working copy)
@@ -1,6 +1,6 @@
 /*
  * MPEG2 transport stream (aka DVB) demuxer
- * Copyright (c) 2002-2003 Fabrice Bellard
+ * Copyright (c) 2002-2003 Fabrice Bellard.
  *
  * This file is part of FFmpeg.
  *
@@ -19,17 +19,15 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-//#define DEBUG
-
 #include "libavutil/crc.h"
-#include "libavutil/intreadwrite.h"
-#include "libavcodec/bytestream.h"
 #include "avformat.h"
-#include "libmythdb/compat.h"  // for uint on MinGW (Windows)
 #include <pthread.h>
+#include "libmythdb/compat.h"
 #include "mpegts.h"
-#include "internal.h"
 
+//#define DEBUG_SI
+//#define DEBUG_SEEK
+
 /* 1.0 second at 24Mbit/s */
 #define MAX_SCAN_PACKETS 32000
 
@@ -37,7 +35,6 @@
    synchronisation is lost */
 #define MAX_RESYNC_SIZE 4096
 
-#define MAX_PES_PAYLOAD 200*1024
 #define PMT_NOT_YET_FOUND 0
 #define PMT_NOT_IN_PAT    1
 #define PMT_FOUND         2
@@ -45,13 +42,13 @@
 typedef struct PESContext PESContext;
 typedef struct SectionContext SectionContext;
 
-static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type);
+static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int stream_type);
+static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code);
 static AVStream *new_section_av_stream(SectionContext *sect, uint32_t code);
 static SectionContext *add_section_stream(MpegTSContext *ts, int pid, int stream_type);
 static void mpegts_cleanup_streams(MpegTSContext *ts);
 static int is_desired_stream(int type);
 static int find_in_list(const int *pids, int pid);
-static void mpegts_push_section(void *opaque, const uint8_t *section, int section_len);
 
 enum MpegTSFilterType {
     MPEGTS_PES,
@@ -85,12 +82,12 @@
 static int is_pat_same(MpegTSContext *mpegts_ctx,
                        int *pmt_pnums, int *pmts_pids, uint pmt_count);
 
-static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item, uint32_t prog_reg_desc, int pcr_pid);
+static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item);
 static int is_pmt_same(MpegTSContext *mpegts_ctx, pmt_entry_t* items,
                        int item_cnt);
 
-typedef int PESCallback(void *opaque, const uint8_t *buf, int len, 
-                        int is_start, int64_t startpos);
+typedef void PESCallback(void *opaque, const uint8_t *buf, int len, 
+                         int is_start, int64_t startpos);
 
 typedef struct MpegTSPESFilter {
     PESCallback *pes_cb;
@@ -172,8 +169,6 @@
 
     /** packet containing Audio/Video data                   */
     AVPacket *pkt;
-    /** to detect seek                                       */
-    int64_t last_pos;
 
     /******************************************/
     /* private mpegts data */
@@ -214,20 +209,17 @@
 
 enum MpegTSState {
     MPEGTS_HEADER = 0,
-    MPEGTS_PESHEADER,
     MPEGTS_PESHEADER_FILL,
     MPEGTS_PAYLOAD,
     MPEGTS_SKIP,
 };
 
 /* enough for PES header + length */
-#define PES_START_SIZE  6
-#define PES_HEADER_SIZE 9
+#define PES_START_SIZE 9
 #define MAX_PES_HEADER_SIZE (9 + 255)
 
 struct PESContext {
     int pid;
-    int pcr_pid; /**< if -1 then all packets containing PCR are considered */ 
     int stream_type;
     MpegTSContext *ts;
     AVFormatContext *stream;
@@ -238,9 +230,8 @@
     int total_size;
     int pes_header_size;
     int64_t pts, dts;
-    int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
     uint8_t header[MAX_PES_HEADER_SIZE];
-    uint8_t *buffer;
+    int64_t startpos;
 };
 
 extern AVInputFormat mpegts_demuxer;
@@ -249,7 +240,6 @@
 struct SectionContext {
     int pid;
     int stream_type;
-    int new_packet;
     MpegTSContext *ts;
     AVFormatContext *stream;
     AVStream *st;
@@ -284,25 +274,19 @@
         tss->section_index += len;
     }
 
-    if (tss->section_cb == mpegts_push_section) {
-        SectionContext *sect = tss->opaque;
-        sect->new_packet = 1;
-    }
-    while (!tss->end_of_section_reached) {
+    while (1) { /* There may be several tables in this data. */
         /* compute section length if possible */
         if (tss->section_h_size == -1 && tss->section_index >= 3) {
-            len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
+            len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
             if (len > 4096)
-                return;
+                break;
             tss->section_h_size = len;
         }
 
         if (tss->section_h_size == -1 || tss->section_index < tss->section_h_size)
             break;
 
-        if (!tss->check_crc ||
-            av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
-                   tss->section_buf, tss->section_h_size) == 0)
+        if (!tss->check_crc || av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, tss->section_buf, tss->section_h_size) == 0)
             tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size);
 
         if (tss->section_index > tss->section_h_size) {
@@ -312,6 +296,7 @@
             tss->section_h_size = -1;
         } else {
             tss->end_of_section_reached = 1;
+            break;
         }
     }
 }
@@ -324,11 +309,12 @@
     MpegTSFilter *filter = ts->pids[pid];
     MpegTSSectionFilter *sec;
     
-    dprintf(ts->stream, "Filter: pid=0x%x\n", pid);
+#ifdef DEBUG_SI
+    av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid);
+#endif
 
-
     if (NULL!=filter) {
-#ifdef DEBUG
+#ifdef DEBUG_SI
 	av_log(ts->stream, AV_LOG_DEBUG, "Filter Already Exists\n");
 #endif
         mpegts_close_filter(ts, filter);
@@ -355,7 +341,7 @@
     return filter;
 }
 
-static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
+MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
                                      PESCallback *pes_cb,
                                      void *opaque)
 {
@@ -377,7 +363,7 @@
     return filter;
 }
 
-static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
+void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
 {
     int pid;
 
@@ -386,7 +372,7 @@
 
     pid = filter->pid;
 
-#ifdef DEBUG
+#ifdef DEBUG_SI
     av_log(NULL, AV_LOG_DEBUG, "Closing Filter: pid=0x%x\n", pid);
 #endif
     if (filter == ts->pmt_filter)
@@ -402,15 +388,6 @@
 
     if (filter->type == MPEGTS_SECTION)
         av_freep(&filter->u.section_filter.section_buf);
-    else if (filter->type == MPEGTS_PES) {
-        PESContext *pes = filter->u.pes_filter.opaque;
-        av_freep(&pes->buffer);
-        /* referenced private data will be freed later in
-         * av_close_input_stream */
-        if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
-            av_freep(&filter->u.pes_filter.opaque);
-        }
-    }
 
     av_free(filter);
     ts->pids[pid] = NULL;
@@ -451,8 +428,9 @@
     score    = analyze(buf, size, TS_PACKET_SIZE, NULL);
     dvhs_score    = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
     fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
-//    av_log(NULL, AV_LOG_ERROR, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
-
+/*  av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n",
+           score, dvhs_score, fec_score);*/
+    
     if     (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
     else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
     else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
@@ -544,125 +522,19 @@
         return -1;
     h->last_sec_num = val;
 
-#ifdef DEBUG
+#ifdef DEBUG_SI
     av_log(NULL, AV_LOG_DEBUG, "sid=0x%x sec_num=%d/%d\n",
            h->id, h->sec_num, h->last_sec_num);
 #endif
     return 0;
 }
 
-typedef struct {
-    uint32_t stream_type;
-    enum CodecType codec_type;
-    enum CodecID codec_id;
-} StreamType;
-
-static const StreamType ISO_types[] = {
-    { 0x01, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
-    { 0x02, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
-    { 0x03, CODEC_TYPE_AUDIO,        CODEC_ID_MP3 },
-    { 0x04, CODEC_TYPE_AUDIO,        CODEC_ID_MP3 },
-    { 0x0f, CODEC_TYPE_AUDIO,        CODEC_ID_AAC },
-    { 0x10, CODEC_TYPE_VIDEO,      CODEC_ID_MPEG4 },
-    { 0x11, CODEC_TYPE_AUDIO,   CODEC_ID_AAC_LATM },
-    { 0x1b, CODEC_TYPE_VIDEO,       CODEC_ID_H264 },
-    { 0xd1, CODEC_TYPE_VIDEO,      CODEC_ID_DIRAC },
-    { 0xea, CODEC_TYPE_VIDEO,        CODEC_ID_VC1 },
-    { 0 },
-};
-
-static const StreamType HDMV_types[] = {
-    { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
-    { 0x82, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
-    { 0x83, CODEC_TYPE_AUDIO, CODEC_ID_TRUEHD },
-    { 0x84, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
-    { 0x85, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
-    { 0x86, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
-    { 0 },
-};
-
-/* ATSC ? */
-static const StreamType MISC_types[] = {
-    { 0x81, CODEC_TYPE_AUDIO,   CODEC_ID_AC3 },
-    { 0x8a, CODEC_TYPE_AUDIO,   CODEC_ID_DTS },
-    { 0x100, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE },
-    { 0x101, CODEC_TYPE_DATA,     CODEC_ID_DVB_VBI },
-    { 0 },
-};
-
-static const StreamType REGD_types[] = {
-    { MKTAG('d','r','a','c'), CODEC_TYPE_VIDEO, CODEC_ID_DIRAC },
-    { MKTAG('A','C','-','3'), CODEC_TYPE_AUDIO,   CODEC_ID_AC3 },
-    { 0 },
-};
-
-/* descriptor present */
-static const StreamType DESC_types[] = {
-    { 0x6a, CODEC_TYPE_AUDIO,             CODEC_ID_AC3 }, /* AC-3 descriptor */
-    { 0x7a, CODEC_TYPE_AUDIO,            CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
-    { 0x7b, CODEC_TYPE_AUDIO,             CODEC_ID_DTS },
-    { 0x59, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
-    { 0 },
-};
-
-static void mpegts_find_stream_type(AVStream *st,
-                                    uint32_t stream_type, const StreamType *types)
-{
-    for (; types->stream_type; types++) {
-        if (stream_type == types->stream_type) {
-            st->codec->codec_type = types->codec_type;
-            st->codec->codec_id   = types->codec_id;
-            return;
-        }
-    }
-}
-
-static AVStream *new_pes_av_stream(PESContext *pes, uint32_t prog_reg_desc, uint32_t code)
-{
-    AVStream *st = av_new_stream(pes->stream, pes->pid);
-
-    if (!st)
-        return NULL;
-
-    av_set_pts_info(st, 33, 1, 90000);
-    st->priv_data = pes;
-    st->codec->codec_type = CODEC_TYPE_DATA;
-    st->codec->codec_id   = CODEC_ID_NONE;
-    st->need_parsing = AVSTREAM_PARSE_FULL;
-    pes->st = st;
-
-    dprintf(pes->stream, "stream_type=%x pid=%x prog_reg_desc=%.4s\n",
-            pes->stream_type, pes->pid, (char*)&prog_reg_desc);
-
-    st->codec->codec_tag = pes->stream_type;
-
-    mpegts_find_stream_type(st, pes->stream_type, ISO_types);
-    if (prog_reg_desc == AV_RL32("HDMV") &&
-        st->codec->codec_id == CODEC_ID_NONE)
-        mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
-    if (st->codec->codec_id == CODEC_ID_NONE)
-        mpegts_find_stream_type(st, pes->stream_type, MISC_types);
-
-    /* stream was not present in PMT, guess based on PES start code */
-    if (st->codec->codec_id == CODEC_ID_NONE) {
-        if (code >= 0x1c0 && code <= 0x1df) {
-            st->codec->codec_type = CODEC_TYPE_AUDIO;
-            st->codec->codec_id = CODEC_ID_MP2;
-        } else if (code == 0x1bd) {
-            st->codec->codec_type = CODEC_TYPE_AUDIO;
-            st->codec->codec_id = CODEC_ID_AC3;
-        }
-    }
-
-    return st;
-}
-
 static MpegTSService *new_service(MpegTSContext *ts, int sid, int pid,
                                   char *provider_name, char *name)
 {
     MpegTSService *service;
 
-#ifdef DEBUG
+#ifdef DEBUG_SI
     av_log(ts->stream, AV_LOG_DEBUG, "new_service: "
            "sid=0x%04x provider='%s' name='%s'\n",
            sid, provider_name, name);
@@ -679,6 +551,18 @@
     return service;
 }
 
+static int mpegts_parse_program_info_length(uint8_t **p, uint8_t *p_end)
+{
+    int program_info_length = get16(p, p_end);
+    if (program_info_length < 0)
+        return -1;
+    program_info_length &= 0xfff;
+    *p += program_info_length;
+    if (*p >= p_end)
+        return -1;
+    return program_info_length;
+}
+
 static int find_in_list(const int *pids, int pid) {
     int i;
     for (i=0; i<PMT_PIDS_MAX; i++)
@@ -695,7 +579,7 @@
         return -1;
     pcr_pid &= 0x1fff;
     mpegts_ctx->pcr_pid = pcr_pid;
-#ifdef DEBUG
+#ifdef DEBUG_SI
     av_log(NULL, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid);
 #endif
     return pcr_pid;
@@ -716,14 +600,12 @@
 
     int last_item = 0;
     int desc_count = 0;
-    int program_info_length, pcr_pid, pid, stream_type;
-    uint32_t prog_reg_desc = 0; /* registration descriptor */
     pmt_entry_t items[PMT_PIDS_MAX];
     bzero(&items, sizeof(pmt_entry_t) * PMT_PIDS_MAX);
 
     mpegts_cleanup_streams(mpegts_ctx); /* in case someone else removed streams.. */
 
-#ifdef DEBUG
+#ifdef DEBUG_SI
     av_log(mpegts_ctx->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len);
     av_hex_dump_log(mpegts_ctx->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
 #endif
@@ -734,7 +616,7 @@
     /* if we require a specific PMT, and this isn't it return silently */
     if (mpegts_ctx->req_sid >= 0 && header.id != mpegts_ctx->req_sid)
     {
-#ifdef DEBUG
+#ifdef DEBUG_SI
         av_log(NULL, AV_LOG_DEBUG, "We are looking for program 0x%x, not 0x%x",
                mpegts_ctx->req_sid, header.id);
 #endif
@@ -748,35 +630,17 @@
     /* Extract the Program Clock Reference PID */
     if (mpegts_parse_pcrpid(mpegts_ctx, &p, p_end) < 0)
         HANDLE_PMT_PARSE_ERROR("PCR PID");
-    pcr_pid = mpegts_ctx->pcr_pid;
 
-    program_info_length = get16(&p, p_end) & 0xfff;
-    if (program_info_length < 0)
-        return;
-    while(program_info_length >= 2) {
-        uint8_t tag, len;
-        tag = get8(&p, p_end);
-        len = get8(&p, p_end);
-        if(len > program_info_length - 2)
-            //something else is broken, exit the program_descriptors_loop
-            break;
-        program_info_length -= len + 2;
-        if(tag == 0x05 && len >= 4) { // registration descriptor
-            prog_reg_desc = bytestream_get_le32(&p);
-            len -= 4;
-        }
-        p += len;
-    }
-    p += program_info_length;
-    if (p >= p_end)
-        return;
+    /* Extract program info length, just so we can skip it */
+    if (mpegts_parse_program_info_length(&p, p_end) < 0)
+        HANDLE_PMT_PARSE_ERROR("program info");
 
     /* parse new streams */
     while (p < p_end)
     {
         dvb_caption_info_t dvbci;
-        stream_type = get8(&p, p_end);
-        pid = get16(&p, p_end);
+        int stream_type = get8(&p, p_end);
+        int pid = get16(&p, p_end);
         int desc_ok = mpegts_parse_desc(&dvbci, &p, p_end, &stream_type);
         if ((stream_type < 0) || (pid < 0) || (desc_ok < 0))
         {
@@ -807,7 +671,7 @@
                 stream_type = STREAM_TYPE_VIDEO_MPEG2;
         }
 
-#ifdef DEBUG
+#ifdef DEBUG_SI
     av_log(mpegts_ctx->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", mpegts_ctx->pcr_pid);
 #endif
 
@@ -847,7 +711,7 @@
 
         /* create new streams */
         for (idx = 0; idx < last_item; idx++)
-            mpegts_add_stream(mpegts_ctx, &items[idx], prog_reg_desc, pcr_pid);
+            mpegts_add_stream(mpegts_ctx, &items[idx]);
 
         /* cache pmt */
         void *tmp0 = avctx->cur_pmt_sect;
@@ -896,7 +760,7 @@
     int idx;
     if (mpegts_ctx->pid_cnt != item_cnt)
     {
-#ifdef DEBUG
+#ifdef DEBUG_SI
         av_log(NULL, AV_LOG_DEBUG, "mpegts_ctx->pid_cnt=%d != item_cnt=%d\n",
                mpegts_ctx->pid_cnt, item_cnt);
 #endif
@@ -908,7 +772,7 @@
         int loc = find_in_list(mpegts_ctx->pmt_pids, items[idx].pid);
         if (loc < 0)
         {
-#ifdef DEBUG
+#ifdef DEBUG_SI
             av_log(NULL, AV_LOG_DEBUG,
                    "find_in_list(..,[%d].pid=%d) => -1\n"
                    "is_pmt_same() => false\n",
@@ -921,7 +785,7 @@
         MpegTSFilter *tss = mpegts_ctx->pids[items[idx].pid];
         if (!tss)
         {
-#ifdef DEBUG
+#ifdef DEBUG_SI
             av_log(NULL, AV_LOG_DEBUG,
                    "mpegts_ctx->pids[items[%d].pid=%d] => null\n"
                    "is_pmt_same() => false\n",
@@ -934,7 +798,7 @@
             PESContext *pes = (PESContext*) tss->u.pes_filter.opaque;
             if (!pes)
             {
-#ifdef DEBUG
+#ifdef DEBUG_SI
                 av_log(NULL, AV_LOG_DEBUG, "pes == null, where idx %d\n"
                        "is_pmt_same() => false\n", idx);
 #endif
@@ -942,7 +806,7 @@
             }
             if (pes->stream_type != items[idx].type)
             {
-#ifdef DEBUG
+#ifdef DEBUG_SI
                 av_log(NULL, AV_LOG_DEBUG,
                        "pes->stream_type != items[%d].type\n"
                        "is_pmt_same() => false\n", idx);
@@ -955,7 +819,7 @@
             SectionContext *sect = (SectionContext*) tss->u.section_filter.opaque;
             if (!sect)
             {
-#ifdef DEBUG
+#ifdef DEBUG_SI
                 av_log(NULL, AV_LOG_DEBUG, "sect == null, where idx %d\n"
                        "is_pmt_same() => false\n", idx);
 #endif
@@ -963,7 +827,7 @@
             }
             if (sect->stream_type != items[idx].type)
             {
-#ifdef DEBUG
+#ifdef DEBUG_SI
                 av_log(NULL, AV_LOG_DEBUG,
                        "sect->stream_type != items[%d].type\n"
                        "is_pmt_same() => false\n", idx);
@@ -973,7 +837,7 @@
         }
         else
         {
-#ifdef DEBUG
+#ifdef DEBUG_SI
             av_log(NULL, AV_LOG_DEBUG,
                    "tss->type != MPEGTS_PES, where idx %d\n"
                    "is_pmt_same() => false\n", idx);
@@ -981,7 +845,7 @@
             return 0;
         }
     }
-#ifdef DEBUG
+#ifdef DEBUG_SI
     av_log(NULL, AV_LOG_DEBUG, "is_pmt_same() => true\n", idx);
 #endif
     return 1;
@@ -1003,11 +867,6 @@
         case STREAM_TYPE_AUDIO_AAC_LATM:
         case STREAM_TYPE_AUDIO_AC3:
         case STREAM_TYPE_AUDIO_DTS:
-        case STREAM_TYPE_AUDIO_HDMV_AC3_PLUS:
-        case STREAM_TYPE_AUDIO_HDMV_AC3_TRUE_HD:
-        case STREAM_TYPE_AUDIO_HDMV_DTS:
-        case STREAM_TYPE_AUDIO_HDMV_DTS_HD:
-        case STREAM_TYPE_AUDIO_HDMV_DTS_HD_MASTER:
             //case STREAM_TYPE_PRIVATE_DATA:
         case STREAM_TYPE_VBI_DVB:
         case STREAM_TYPE_SUBTITLE_DVB:
@@ -1055,11 +914,11 @@
         desc_end = *p + desc_len;
         if (desc_end > desc_list_end)
             break;
-
-/*             dprintf(ts->stream, "tag: 0x%02x len=%d\n", */
-/*                    desc_tag, desc_len); */
-
-            switch(desc_tag) {
+#ifdef DEBUG_SI
+        av_log(NULL, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
+#endif
+        switch (desc_tag)
+        {
             case DVB_SUBT_DESCID:
                 dvbci->language[0] = get8(p, desc_end);
                 dvbci->language[1] = get8(p, desc_end);
@@ -1130,7 +989,7 @@
     }
 }
 
-static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item, uint32_t prog_reg_desc, int pcr_pid)
+static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item)
 {
 
     av_log(NULL, AV_LOG_DEBUG,
@@ -1174,7 +1033,7 @@
         } else {
             PESContext *pes = NULL;
             AVStream *st = NULL;
-            pes = add_pes_stream(ts, item->pid, pcr_pid, item->type);
+            pes = add_pes_stream(ts, item->pid, item->type);
             if (!pes)
             {
                 av_log(NULL, AV_LOG_ERROR, "mpegts_add_stream: "
@@ -1184,7 +1043,7 @@
             }
 
             /* Pretend it's audio if we have a language. */
-            st = new_pes_av_stream(pes, prog_reg_desc, 0);
+            st = new_pes_av_stream(pes, item->dvbci.language[0] ? 0x1c0 : 0);
             if (!st)
             {
                 av_log(NULL, AV_LOG_ERROR, "mpegts_add_stream: "
@@ -1251,7 +1110,7 @@
     uint pmt_count = 0;
     int i;
 
-#ifdef DEBUG
+#ifdef DEBUG_SI
     av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
     av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
 #endif
@@ -1282,7 +1141,7 @@
 
         pmt_count++;
 
-#ifdef DEBUG
+#ifdef DEBUG_SI
         av_log(ts->stream, AV_LOG_DEBUG,
                "MPEG Program Number=0x%x pid=0x%x req_sid=0x%x\n",
                pmt_pnums[i], pmt_pids[i], ts->req_sid);
@@ -1291,7 +1150,7 @@
 
     if (!is_pat_same(ts, pmt_pnums, pmt_pids, pmt_count))
     {
-#ifdef DEBUG
+#ifdef DEBUG_SI
         av_log(NULL, AV_LOG_DEBUG, "New PAT!\n");
 #endif
         /* if there were services, get rid of them */
@@ -1324,7 +1183,7 @@
          * add a filter for the PMT. */
         if (ts->req_sid == pmt_pnums[i])
         {
-#ifdef DEBUG
+#ifdef DEBUG_SI
             av_log(NULL, AV_LOG_DEBUG, "Found program number!\n");
 #endif
             /* close old filter if it doesn't match */
@@ -1359,7 +1218,7 @@
      * tell parser it is safe to quit. */
     if (ts->req_sid < 0 && ts->scanning)
     {
-#ifdef DEBUG
+#ifdef DEBUG_SI
         av_log(NULL, AV_LOG_DEBUG, "Found PAT, ending scan\n");
 #endif
         ts->stop_parse = 1;
@@ -1370,7 +1229,7 @@
      * and tell parser it is safe to quit. */ 
     if (ts->req_sid >= 0 && !found)
     {
-#ifdef DEBUG
+#ifdef DEBUG_SI
         av_log(NULL, AV_LOG_DEBUG, "Program 0x%x is not in PAT, ending scan\n",
                ts->req_sid);
 #endif
@@ -1390,8 +1249,8 @@
     char *provider_name, *name;
     char buf[256];
 
-#ifdef DEBUG
-    dprintf(ts->stream, "PAT:\n");
+#ifdef DEBUG_SI
+    av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
     av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
 #endif
     p_end = section + section_len - 4;
@@ -1408,9 +1267,9 @@
         pmt_pid = get16(&p, p_end) & 0x1fff;
         if (pmt_pid < 0)
             break;
-
-        dprintf(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
-
+#ifdef DEBUG_SI
+        av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
+#endif
         if (sid == 0x0000) {
             /* NIT info */
         } else {
@@ -1431,7 +1290,7 @@
     /* remove filter */
     mpegts_close_filter(ts, ts->pat_filter);
     ts->pat_filter = NULL;
-#ifdef DEBUG
+#ifdef DEBUG_SI
     av_log(NULL, AV_LOG_DEBUG, "end of scan PAT\n");
 #endif    
 }
@@ -1446,8 +1305,8 @@
     int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
     char *name, *provider_name;
 
-#ifdef DEBUG
-    dprintf(ts->stream, "SDT:\n");
+#ifdef DEBUG_SI
+    av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n");
     av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
 #endif
 
@@ -1484,10 +1343,10 @@
             desc_end = p + desc_len;
             if (desc_end > desc_list_end)
                 break;
-
-            dprintf(ts->stream, "tag: 0x%02x len=%d\n",
+#ifdef DEBUG_SI
+            av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
                    desc_tag, desc_len);
-
+#endif
             switch(desc_tag) {
             case 0x48:
                 service_type = get8(&p, p_end);
@@ -1518,38 +1377,39 @@
 }
 #endif
 
-static int64_t get_pts(const uint8_t *p)
+#if 0
+/* scan services in a transport stream by looking at the SDT */
+void mpegts_scan_sdt(MpegTSContext *ts)
 {
-    int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
-    pts |= (AV_RB16(p + 1) >> 1) << 15;
-    pts |=  AV_RB16(p + 3) >> 1;
-    return pts;
+    ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,
+                                                sdt_cb, ts, 1);
 }
+#endif
 
-static void new_pes_packet(PESContext *pes, AVPacket *pkt)
+#if 0
+/* scan services in a transport stream by looking at the PAT (better
+   than nothing !) */
+void mpegts_scan_pat(MpegTSContext *ts)
 {
-    av_init_packet(pkt);
+    ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
+                                                pat_scan_cb, ts, 1);
+}
+#endif
 
-    pkt->destruct = av_destruct_packet;
-    pkt->data = pes->buffer;
-    pkt->size = pes->data_index;
-    if (pkt->data && pkt->size)
-        memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+static int64_t get_pts(const uint8_t *p)
+{
+    int64_t pts;
+    int val;
 
-    pkt->stream_index = pes->st->index;
-    pkt->pts = pes->pts;
-    pkt->dts = pes->dts;
-    /* store position of first TS packet of this PES packet */
-    pkt->pos = pes->ts_packet_pos;
-
-    /* reset pts values */
-    pes->pts = AV_NOPTS_VALUE;
-    pes->dts = AV_NOPTS_VALUE;
-    pes->buffer = NULL;
-    pes->data_index = 0;
-    pes->total_size = 0;
+    pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
+    val = (p[1] << 8) | p[2];
+    pts |= (int64_t)(val >> 1) << 15;
+    val = (p[3] << 8) | p[4];
+    pts |= (int64_t)(val >> 1);
+    return pts;
 }
 
+
 static void init_stream(AVStream *st, int stream_type, int code)
 {
     int codec_type=-1, codec_id=-1;
@@ -1589,8 +1449,8 @@
             codec_id = CODEC_ID_AC3;
             break;
         case STREAM_TYPE_AUDIO_DTS:
-           codec_type = CODEC_TYPE_AUDIO;
-           codec_id = CODEC_ID_DTS;
+            codec_type = CODEC_TYPE_AUDIO;
+            codec_id = CODEC_ID_DTS;
             break;
         case STREAM_TYPE_VBI_DVB:
             codec_type = CODEC_TYPE_DATA;
@@ -1623,6 +1483,19 @@
     av_set_pts_info(st, 33, 1, 90000);
 }
 
+static AVStream *new_pes_av_stream(PESContext *pes, uint32_t code)
+{
+    CHECKED_ALLOCZ(pes->st, sizeof(AVStream));
+    pes->st->codec = avcodec_alloc_context();
+    init_stream(pes->st, pes->stream_type, code);  /* sets codec type and id */
+    pes->st->priv_data = pes;
+    pes->st->need_parsing = AVSTREAM_PARSE_FULL;
+
+    pes->st = av_add_stream(pes->stream, pes->st, pes->pid);
+fail: /*for the CHECKED_ALLOCZ macro*/
+    return pes->st;
+}
+
 static AVStream *new_section_av_stream(SectionContext *sect, uint32_t code)
 {
     CHECKED_ALLOCZ(sect->st, sizeof(AVStream));
@@ -1638,26 +1511,19 @@
 
 
 /* return non zero if a packet could be constructed */
-static int mpegts_push_data(void *opaque,
-                            const uint8_t *buf, int buf_size, int is_start,
-                            int64_t pos)
+static void mpegts_push_data(void *opaque,
+                             const uint8_t *buf, int buf_size, int is_start,
+                             int64_t position)
 {
     PESContext *pes = opaque;
     MpegTSContext *ts = pes->ts;
     const uint8_t *p;
     int len, code;
 
-    if(!ts->pkt)
-        return 0;
-
     if (is_start) {
-        if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
-            new_pes_packet(pes, ts->pkt);
-            ts->stop_parse = 1;
-        }
+        pes->startpos = position;
         pes->state = MPEGTS_HEADER;
         pes->data_index = 0;
-        pes->ts_packet_pos = pos;
     }
     p = buf;
     while (buf_size > 0) {
@@ -1680,47 +1546,23 @@
                     pes->header[2] == 0x01) {
                     /* it must be an mpeg2 PES stream */
                     code = pes->header[3] | 0x100;
-                    dprintf(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
-
-                    if ((!pes->st && pes->stream->nb_streams == MAX_STREAMS) ||
-                        (pes->st && pes->st->discard == AVDISCARD_ALL) ||
-/*                         !((code >= 0x1c0 && code <= 0x1df) || */
-/*                           (code >= 0x1e0 && code <= 0x1ef) || */
-/*                           (code == 0x1bd) || (code == 0x1fd)) || */
-                        code == 0x1be) /* padding_stream */
+                    if (!((code >= 0x1c0 && code <= 0x1df) ||
+                          (code >= 0x1e0 && code <= 0x1ef) ||
+                          (code == 0x1bd) || (code == 0x1fd)))
                         goto skip;
-
-                    /* stream not present in PMT */
-                    if (!pes->st)
-                        pes->st = new_pes_av_stream(pes, 0, code);
-                    if (!pes->st)
-                        return AVERROR(ENOMEM);
-
-                    pes->total_size = AV_RB16(pes->header + 4);
+                    if (!pes->st && 0 == new_pes_av_stream(pes, code)) {
+                        av_log(NULL, AV_LOG_ERROR,
+                               "Error: new_pes_av_stream() "
+                               "failed in mpegts_push_data\n");
+                        goto skip;
+                    }
+                    pes->state = MPEGTS_PESHEADER_FILL;
+                    pes->total_size = (pes->header[4] << 8) | pes->header[5];
                     /* NOTE: a zero total size means the PES size is
                        unbounded */
-                    if (!pes->total_size)
-                        pes->total_size = MAX_PES_PAYLOAD;
-
-                    /* allocate pes buffer */
-                    pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
-                    if (!pes->buffer)
-                        return AVERROR(ENOMEM);
-
-                    if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
-                        code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
-                        code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
-                        code != 0x1f8) {                  /* ITU-T Rec. H.222.1 type E stream */
-                        pes->state = MPEGTS_PESHEADER;
-                        if (pes->st->codec->codec_id == CODEC_ID_NONE) {
-                            dprintf(pes->stream, "pid=%x stream_type=%x probing\n",
-                                    pes->pid, pes->stream_type);
-                            pes->st->codec->codec_id = CODEC_ID_PROBE;
-                        }
-                    } else {
-                        pes->state = MPEGTS_PAYLOAD;
-                        pes->data_index = 0;
-                    }
+                    if (pes->total_size)
+                        pes->total_size += 6;
+                    pes->pes_header_size = pes->header[8] + 9;
                 } else {
                     /* otherwise, it should be a table */
                     /* skip packet */
@@ -1732,25 +1574,8 @@
             break;
             /**********************************************/
             /* PES packing parsing */
-        case MPEGTS_PESHEADER:
-            len = PES_HEADER_SIZE - pes->data_index;
-            if (len < 0)
-                return -1;
-            if (len > buf_size)
-                len = buf_size;
-            memcpy(pes->header + pes->data_index, p, len);
-            pes->data_index += len;
-            p += len;
-            buf_size -= len;
-            if (pes->data_index == PES_HEADER_SIZE) {
-                pes->pes_header_size = pes->header[8] + 9;
-                pes->state = MPEGTS_PESHEADER_FILL;
-            }
-            break;
         case MPEGTS_PESHEADER_FILL:
             len = pes->pes_header_size - pes->data_index;
-            if (len < 0)
-                return -1;
             if (len > buf_size)
                 len = buf_size;
             memcpy(pes->header + pes->data_index, p, len);
@@ -1766,7 +1591,7 @@
                 pes->pts = AV_NOPTS_VALUE;
                 pes->dts = AV_NOPTS_VALUE;
                 if ((flags & 0xc0) == 0x80) {
-                    pes->dts = pes->pts = get_pts(r);
+                    pes->pts = get_pts(r);
                     r += 5;
                 } else if ((flags & 0xc0) == 0xc0) {
                     pes->pts = get_pts(r);
@@ -1774,24 +1599,32 @@
                     pes->dts = get_pts(r);
                     r += 5;
                 }
-
                 /* we got the full header. We parse it and get the payload */
                 pes->state = MPEGTS_PAYLOAD;
-                pes->data_index = 0;
             }
             break;
         case MPEGTS_PAYLOAD:
-            if (buf_size > 0) {
-                if (pes->data_index+buf_size > pes->total_size) {
-                    new_pes_packet(pes, ts->pkt);
-                    pes->total_size = MAX_PES_PAYLOAD;
-                    pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
-                    if (!pes->buffer)
-                        return AVERROR(ENOMEM);
+            if (pes->total_size) {
+                len = pes->total_size - pes->data_index;
+                if (len > buf_size)
+                    len = buf_size;
+            } else {
+                len = buf_size;
+            }
+            if (len > 0) {
+                AVPacket *pkt = ts->pkt;
+                if (pkt && pes->st && av_new_packet(pkt, len) == 0) {
+                    memcpy(pkt->data, p, len);
+                    pkt->stream_index = pes->st->index;
+                    pkt->pts = pes->pts;
+                    pkt->dts = pes->dts;
+                    pkt->pos = pes->startpos;
+                    /* reset pts values */
+                    pes->pts = AV_NOPTS_VALUE;
+                    pes->dts = AV_NOPTS_VALUE;
                     ts->stop_parse = 1;
+                    return;
                 }
-                memcpy(pes->buffer+pes->data_index, p, buf_size);
-                pes->data_index += buf_size;
             }
             buf_size = 0;
             break;
@@ -1800,11 +1633,9 @@
             break;
         }
     }
-
-    return 0;
 }
 
-static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type)
+static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int stream_type)
 {
     MpegTSFilter *tss = ts->pids[pid];
     PESContext *pes = 0;
@@ -1826,9 +1657,7 @@
     pes->ts = ts;
     pes->stream = ts->stream;
     pes->pid = pid;
-    pes->pcr_pid = pcr_pid;
     pes->stream_type = stream_type;
-    pes->state = MPEGTS_SKIP;
     tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
     if (!tss) {
         av_free(pes);
@@ -1852,23 +1681,12 @@
     SectionHeader header;
     AVPacket *pkt = ts->pkt;
     const uint8_t *p = section, *p_end = section + section_len - 4;
-
     if (parse_section_header(&header, &p, p_end) < 0)
     {
         av_log(NULL, AV_LOG_DEBUG, "Unable to parse header\n");
         return;
     }
-
-    if (sect->new_packet && pkt && sect->st) {
-        int pktLen = section_len + 184; /* Add enough for a complete TS payload. */
-        sect->new_packet = 0;
-        if (av_new_packet(pkt, pktLen) == 0) {
-            memcpy(pkt->data, section, section_len);
-            memset(pkt->data+section_len, 0xff, pktLen-section_len);
-            pkt->stream_index = sect->st->index;
-            ts->stop_parse = 1;
-        }
-    } else if (pkt->data) { /* We've already added at least one table. */
+    if (pkt->data) { /* We've already added at least one table. */
         uint8_t *data = pkt->data;
         int space = pkt->size;
         int table_size = 0;
@@ -1884,6 +1702,17 @@
             return;
         }
         memcpy(data, section, section_len);
+    } else if (pkt && sect->st) {
+        int pktLen = section_len + 184; /* Add enough for a complete TS payload. */
+        if (av_new_packet(pkt, pktLen) == 0) {
+            memcpy(pkt->data, section, section_len);
+            memset(pkt->data+section_len, 0xff, pktLen-section_len);
+            pkt->stream_index = sect->st->index;
+            pkt->pts = AV_NOPTS_VALUE;
+            pkt->dts = AV_NOPTS_VALUE;
+            pkt->pos = 0;
+            ts->stop_parse = 1;
+        }
    }
 }
 
@@ -1923,30 +1752,28 @@
 
 
 /* handle one TS packet */
-static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
+static void handle_packet(MpegTSContext *ts, const uint8_t *packet,
+                          int64_t position)
 {
     AVFormatContext *s = ts->stream;
     MpegTSFilter *tss;
     int len, pid, cc, cc_ok, afc, is_start;
     const uint8_t *p, *p_end;
-    int64_t pos;
 
-    pid = AV_RB16(packet + 1) & 0x1fff;
-
     if (!ts->pids[0]) {
         /* make sure we're always scanning for new PAT's */
         ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
     }
 
+    pid = ((packet[1] & 0x1f) << 8) | packet[2];
     is_start = packet[1] & 0x40;
-
     tss = ts->pids[pid];
     if (ts->auto_guess && tss == NULL && is_start) {
-        add_pes_stream(ts, pid, -1, 0);
+        add_pes_stream(ts, pid, 0);
         tss = ts->pids[pid];
     }
     if (!tss)
-        return 0;
+        return;
 
     /* continuity check (currently not used) */
     cc = (packet[3] & 0xf);
@@ -1957,9 +1784,9 @@
     afc = (packet[3] >> 4) & 3;
     p = packet + 4;
     if (afc == 0) /* reserved value */
-        return 0;
+        return;
     if (afc == 2) /* adaptation field only */
-        return 0;
+        return;
     if (afc == 3) {
         /* skip adapation field */
         p += p[0] + 1;
@@ -1967,24 +1794,23 @@
     /* if past the end of packet, ignore */
     p_end = packet + TS_PACKET_SIZE;
     if (p >= p_end)
-        return 0;
+        return;
 
-    pos = url_ftell(ts->stream->pb);
-    ts->pos47= pos % ts->raw_packet_size;
+    ts->pos47= url_ftell(ts->stream->pb) % ts->raw_packet_size;
 
     if (tss->type == MPEGTS_SECTION) {
         if (is_start) {
             /* pointer field present */
             len = *p++;
             if (p + len > p_end)
-                return 0;
+                return;
             if (len && cc_ok) {
                 /* write remaining section bytes */
                 write_section_data(s, tss,
                                    p, len, 0);
                 /* check whether filter has been closed */
                 if (!ts->pids[pid])
-                    return 0;
+                    return;
             }
             p += len;
             if (p < p_end) {
@@ -1998,15 +1824,9 @@
             }
         }
     } else {
-        int ret;
-        // Note: The position here points actually behind the current packet.
-        if ((ret = tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque, 
-                                            p, p_end - p, is_start,
-                                            pos - ts->raw_packet_size)) < 0)
-            return ret;
+        tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque, 
+                                 p, p_end - p, is_start, position);
     }
-
-    return 0;
 }
 
 /* XXX: try to find a better synchro over several packets (use
@@ -2029,11 +1849,13 @@
 }
 
 /* return -1 if error or EOF. Return 0 if OK. */
-static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
+static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size,
+                       int64_t *position)
 {
     int skip, len;
 
     for(;;) {
+        *position = url_ftell(pb);
         len = get_buffer(pb, buf, TS_PACKET_SIZE);
         if (len != TS_PACKET_SIZE)
             return AVERROR(EIO);
@@ -2061,21 +1883,20 @@
     ByteIOContext *pb = s->pb;
     uint8_t packet[TS_PACKET_SIZE];
     int packet_num, ret;
+    int64_t pos;
 
     ts->stop_parse = 0;
     packet_num = 0;
     for(;;) {
-        if (ts->stop_parse>0)
+        if (ts->stop_parse)
             break;
         packet_num++;
-        if (nb_packets != 0 && packet_num * ts->raw_packet_size >= nb_packets)
+        if (nb_packets != 0 && packet_num >= nb_packets)
             break;
-        ret = read_packet(pb, packet, ts->raw_packet_size);
+        ret = read_packet(pb, packet, ts->raw_packet_size, &pos);
         if (ret != 0)
             return ret;
-        ret = handle_packet(ts, packet);
-        if (ret != 0)
-            return ret;
+        handle_packet(ts, packet, pos);
     }
     return 0;
 }
@@ -2085,18 +1906,22 @@
 #if 1
     const int size= p->buf_size;
     int score, fec_score, dvhs_score;
-    int check_count= size / TS_FEC_PACKET_SIZE;
 #define CHECK_COUNT 10
 
-    if (check_count < CHECK_COUNT)
+    if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
         return -1;
 
-    score     = analyze(p->buf, TS_PACKET_SIZE     *check_count, TS_PACKET_SIZE     , NULL)*CHECK_COUNT/check_count;
-    dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
-    fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
-//    av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
-
-// we need a clear definition for the returned score otherwise things will become messy sooner or later
+    score    = analyze(p->buf, TS_PACKET_SIZE    *CHECK_COUNT, TS_PACKET_SIZE, NULL);
+    dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE * CHECK_COUNT,
+                         TS_DVHS_PACKET_SIZE, NULL);
+    fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
+#if 0
+    av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n",
+           score, fec_score);
+#endif
+ 
+    /* we need a clear definition for the returned score 
+       otherwise things will become messy sooner or later */
     if     (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score     - CHECK_COUNT;
     else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score  - CHECK_COUNT;
     else if(                 fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
@@ -2110,7 +1935,16 @@
 #endif
 }
 
-/* return the 90kHz PCR and the extension for the 27MHz PCR. return
+#if 0
+static void set_service_cb(void *opaque, int ret)
+{
+    MpegTSContext *ts = opaque;
+    ts->set_service_ret = ret;
+    ts->stop_parse = 1;
+}
+#endif
+
+/* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
    (-1) if not available */
 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
                      const uint8_t *packet)
@@ -2144,32 +1978,26 @@
 {
     MpegTSContext *ts = s->priv_data;
     ByteIOContext *pb = s->pb;
-    uint8_t buf[5*1024];
+    uint8_t buf[1024];
     int len, sid, i;
     int64_t pos;
     MpegTSService *service;
 
     if (ap) {
+        ts->mpeg2ts_raw = ap->mpeg2ts_raw;
         ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
-        if(ap->mpeg2ts_raw){
-            av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
-            return -1;
-        }
     }
 
     /* read the first 1024 bytes to get packet size */
     pos = url_ftell(pb);
     len = get_buffer(pb, buf, sizeof(buf));
-    if (len != sizeof(buf))
-    {
+    if (len != sizeof(buf)) {
         av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: "
                "unable to read first 1024 bytes\n");
         goto fail;
     }
     ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
-    av_log(NULL, AV_LOG_DEBUG, "mpegts_read_header: TS packet size = %d\n", ts->raw_packet_size);
-    if (ts->raw_packet_size <= 0)
-    {
+    if (ts->raw_packet_size <= 0) {
         av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: "
                "packet size incorrect\n");
         goto fail;
@@ -2177,26 +2005,21 @@
     ts->stream = s;
     ts->auto_guess = 0;
 
-    if (s->iformat == &mpegts_demuxer) {
+    if (!ts->mpeg2ts_raw) {
         /* normal demux */
 
         if (!ts->auto_guess) {
-            /* first do a scaning to get all the services */
-            url_fseek(pb, pos, SEEK_SET);
-
             /* SDT Scan Removed here. It caused startup delays in TS files
-               SDT will not exist in a stripped TS file created by myth.
-        mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
-            */
+               SDT will not exist in a stripped TS file created by myth. */
 
             /* we don't want any PMT pid filters created on first pass */
             ts->req_sid = -1;
 
             ts->scanning = 1;
-            ts->pat_filter =
-        mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
-
-        handle_packets(ts, s->probesize);
+            ts->pat_filter = mpegts_open_section_filter(
+                ts, PAT_PID, pat_cb, ts, 1);
+            url_fseek(pb, pos, SEEK_SET);
+            handle_packets(ts, s->probesize);
             ts->scanning = 0;
 
             if (ts->nb_services <= 0) {
@@ -2213,7 +2036,7 @@
                          (ts->pmt_scan_state == PMT_NOT_YET_FOUND)); i++)
             {
                 service = ts->services[i];
-#ifdef DEBUG
+#ifdef DEBUG_SI
                 av_log(ts->stream, AV_LOG_DEBUG, "Tuning to '%s' pnum: 0x%x\n",
                        service->name, service->sid);
 #endif
@@ -2252,14 +2075,15 @@
                 goto fail;
             } 
             
-        dprintf(ts->stream, "tuning done\n");
-
+#ifdef DEBUG_SI
+            av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");
+#endif
         }
         s->ctx_flags |= AVFMTCTX_NOHEADER;
     } else {
         AVStream *st;
         int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
-        int64_t pcrs[2], pcr_h;
+        int64_t pcrs[2], pcr_h, position;
         int packet_count[2];
         uint8_t packet[TS_PACKET_SIZE];
 
@@ -2267,13 +2091,12 @@
 
     do_pcr:
         st = av_new_stream(s, 0);
-        if (!st)
-        {
+        if (!st) {
             av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: "
                    "av_new_stream() failed\n");
             goto fail;
         }
-        av_set_pts_info(st, 60, 1, 27000000);
+        av_set_pts_info(st, 33, 1, 27000000);
         st->codec->codec_type = CODEC_TYPE_DATA;
         st->codec->codec_id = CODEC_ID_MPEG2TS;
 
@@ -2282,14 +2105,13 @@
         nb_pcrs = 0;
         nb_packets = 0;
         for(;;) {
-            ret = read_packet(s->pb, packet, ts->raw_packet_size);
-            if (ret < 0)
-            {
+            ret = read_packet(s->pb, packet, ts->raw_packet_size, &position);
+            if (ret < 0) {
                 av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: "
                        "read_packet() failed\n");
-                return -1;
+                goto fail;
             }
-            pid = AV_RB16(packet + 1) & 0x1fff;
+            pid = ((packet[1] & 0x1f) << 8) | packet[2];
             if ((pcr_pid == -1 || pcr_pid == pid) &&
                 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
                 pcr_pid = pid;
@@ -2329,14 +2151,13 @@
 {
     MpegTSContext *ts = s->priv_data;
     int ret, i;
-    int64_t pcr_h, next_pcr_h, pos;
+    int64_t pcr_h, next_pcr_h, pos, position;
     int pcr_l, next_pcr_l;
     uint8_t pcr_buf[12];
 
     if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
         return AVERROR(ENOMEM);
-    pkt->pos= url_ftell(s->pb);
-    ret = read_packet(s->pb, pkt->data, ts->raw_packet_size);
+    ret = read_packet(s->pb, pkt->data, ts->raw_packet_size, &position);
     if (ret < 0) {
         av_free_packet(pkt);
         return ret;
@@ -2372,39 +2193,13 @@
                               AVPacket *pkt)
 {
     MpegTSContext *ts = s->priv_data;
-    int ret, i;
 
-    if (url_ftell(s->pb) != ts->last_pos) {
-        /* seek detected, flush pes buffer */
-        for (i = 0; i < NB_PID_MAX; i++) {
-            if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
-                PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
-                av_freep(&pes->buffer);
-                pes->data_index = 0;
-                pes->state = MPEGTS_SKIP; /* skip until pes header */
-            }
-        }
+    if (!ts->mpeg2ts_raw) {
+        ts->pkt = pkt;
+        return handle_packets(ts, 0);
+    } else {
+        return mpegts_raw_read_packet(s, pkt);
     }
-
-    ts->pkt = pkt;
-    ret = handle_packets(ts, 0);
-    if (ret < 0) {
-        /* flush pes data left */
-        for (i = 0; i < NB_PID_MAX; i++) {
-            if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
-                PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
-                if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
-                    new_pes_packet(pes, pkt);
-                    ret = 0;
-                    break;
-                }
-            }
-        }
-    }
-
-    ts->last_pos = url_ftell(s->pb);
-
-    return ret;
 }
 
 static int mpegts_read_close(AVFormatContext *s)
@@ -2512,12 +2307,13 @@
                         const uint8_t *buf, int len)
 {
     int len1;
+    int64_t position = 0;
 
     len1 = len;
     ts->pkt = pkt;
     ts->stop_parse = 0;
     for(;;) {
-        if (ts->stop_parse>0)
+        if (ts->stop_parse)
             break;
         if (len < TS_PACKET_SIZE)
             return -1;
@@ -2525,7 +2321,7 @@
             buf++;
             len--;
         } else {
-            handle_packet(ts, buf);
+            handle_packet(ts, buf, position);
             buf += TS_PACKET_SIZE;
             len -= TS_PACKET_SIZE;
         }
Index: libs/libavformat/mpegts.h
===================================================================
--- libs/libavformat/mpegts.h	(revision 22779)
+++ libs/libavformat/mpegts.h	(working copy)
@@ -1,6 +1,6 @@
 /*
  * MPEG2 transport stream defines
- * Copyright (c) 2003 Fabrice Bellard
+ * Copyright (c) 2003 Fabrice Bellard.
  *
  * This file is part of FFmpeg.
  *
@@ -39,6 +39,7 @@
 #define PMT_TID   0x02
 #define SDT_TID   0x42
 
+/* descriptor ids */
 #define DVB_CAROUSEL_ID             0x13
 #define DVB_VBI_DATA_ID             0x45
 #define DVB_VBI_TELETEXT_ID         0x46
@@ -46,6 +47,7 @@
 #define DVB_SUBT_DESCID             0x59
 #define DVB_BROADCAST_ID            0x66
 #define DVB_DATA_STREAM             0x52
+
 #define STREAM_TYPE_VIDEO_MPEG1     0x01
 #define STREAM_TYPE_VIDEO_MPEG2     0x02
 #define STREAM_TYPE_AUDIO_MPEG1     0x03
@@ -62,11 +64,6 @@
 
 #define STREAM_TYPE_AUDIO_AC3       0x81
 #define STREAM_TYPE_AUDIO_DTS       0x8a
-#define STREAM_TYPE_AUDIO_HDMV_AC3_PLUS      0x84
-#define STREAM_TYPE_AUDIO_HDMV_AC3_TRUE_HD   0x83
-#define STREAM_TYPE_AUDIO_HDMV_DTS           0x82
-#define STREAM_TYPE_AUDIO_HDMV_DTS_HD        0x85
-#define STREAM_TYPE_AUDIO_HDMV_DTS_HD_MASTER 0x86
 
 #define STREAM_TYPE_SUBTITLE_DVB    0x100
 #define STREAM_TYPE_VBI_DVB         0x101

