pts = 0; dts = 0;
pts = AV_NOPTS_VALUE; dts = AV_NOPTS_VALUE;
static int mpegps_read_packet(AVFormatContext *s,                                  AVPacket *pkt){    MpegDemuxContext *m = s->priv_data;    AVStream *st;    int len, size, startcode, i, c, flags, header_len, type, codec_id;    INT64 pts, dts;    /* next start code (should be immediately after */ redo:    m->header_state = 0xff;    size = MAX_SYNC_SIZE;    startcode = find_start_code(&s->pb, &size, &m->header_state);    //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));    if (startcode < 0)        return -EIO;    if (startcode == PACK_START_CODE)        goto redo;    if (startcode == SYSTEM_HEADER_START_CODE)        goto redo;    if (startcode == PADDING_STREAM ||        startcode == PRIVATE_STREAM_2) {        /* skip them */        len = get_be16(&s->pb);        url_fskip(&s->pb, len);        goto redo;    }    /* find matching stream */    if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||          (startcode >= 0x1e0 && startcode <= 0x1ef) ||          (startcode == 0x1bd)))        goto redo;    len = get_be16(&s->pb);    pts = 0;    dts = 0;    /* stuffing */    for(;;) {        c = get_byte(&s->pb);        len--;        /* XXX: for mpeg1, should test only bit 7 */        if (c != 0xff)             break;    }    if ((c & 0xc0) == 0x40) {        /* buffer scale & size */        get_byte(&s->pb);        c = get_byte(&s->pb);        len -= 2;    }    if ((c & 0xf0) == 0x20) {        pts = get_pts(&s->pb, c);        len -= 4;        dts = pts;    } else if ((c & 0xf0) == 0x30) {        pts = get_pts(&s->pb, c);        dts = get_pts(&s->pb, -1);        len -= 9;    } else if ((c & 0xc0) == 0x80) {        /* mpeg 2 PES */        if ((c & 0x30) != 0) {            fprintf(stderr, "Encrypted multiplex not handled\n");            return -EIO;        }        flags = get_byte(&s->pb);        header_len = get_byte(&s->pb);        len -= 2;        if (header_len > len)            goto redo;        if ((flags & 0xc0) == 0x80) {            pts = get_pts(&s->pb, -1);            dts = pts;            header_len -= 5;            len -= 5;        } if ((flags & 0xc0) == 0xc0) {            pts = get_pts(&s->pb, -1);            dts = get_pts(&s->pb, -1);            header_len -= 10;            len -= 10;        }        len -= header_len;        while (header_len > 0) {            get_byte(&s->pb);            header_len--;        }    }    if (startcode == 0x1bd) {        startcode = get_byte(&s->pb);        len--;        if (startcode >= 0x80 && startcode <= 0xbf) {            /* audio: skip header */            get_byte(&s->pb);            get_byte(&s->pb);            get_byte(&s->pb);            len -= 3;        }    }    /* now find stream */    for(i=0;i<s->nb_streams;i++) {        st = s->streams[i];        if (st->id == startcode)            goto found;    }    if (startcode >= 0x1e0 && startcode <= 0x1ef) {        type = CODEC_TYPE_VIDEO;        codec_id = CODEC_ID_MPEG1VIDEO;    } else if (startcode >= 0x1c0 && startcode <= 0x1df) {        type = CODEC_TYPE_AUDIO;        codec_id = CODEC_ID_MP2;    } else if (startcode >= 0x80 && startcode <= 0x9f) {        type = CODEC_TYPE_AUDIO;        codec_id = CODEC_ID_AC3;    } else {    skip:        /* skip packet */        url_fskip(&s->pb, len);        goto redo;    }    /* no stream found: add a new stream */    st = av_new_stream(s, startcode);    if (!st)         goto skip;    st->codec.codec_type = type;    st->codec.codec_id = codec_id; found:    av_new_packet(pkt, len);    //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode,    //       (float)pts/90000, len);    get_buffer(&s->pb, pkt->data, pkt->size);    pkt->pts = pts;    pkt->stream_index = st->index;    return 0;}