static int mpegps_read_pes_header(AVFormatContext *s,

                                  int64_t *ppos, int *pstart_code,

                                  int64_t *ppts, int64_t *pdts)

{

    MpegDemuxContext *m = s->priv_data;

    int len, size, startcode, c, flags, header_len;

    int64_t pts, dts, last_pos;



    last_pos = -1;

 redo:

        /* next start code (should be immediately after) */

        m->header_state = 0xff;

        size = MAX_SYNC_SIZE;

        startcode = find_next_start_code(&s->pb, &size, &m->header_state);

    //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, url_ftell(&s->pb));

    if (startcode < 0)

        return AVERROR_IO;

    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;

    }

    if (startcode == PROGRAM_STREAM_MAP) {

        mpegps_psm_parse(m, &s->pb);

        goto redo;

    }



    /* find matching stream */

    if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||

          (startcode >= 0x1e0 && startcode <= 0x1ef) ||

          (startcode == 0x1bd)))

        goto redo;

    if (ppos) {

        *ppos = url_ftell(&s->pb) - 4;

    }

    len = get_be16(&s->pb);

    pts = AV_NOPTS_VALUE;

    dts = AV_NOPTS_VALUE;

    /* stuffing */

    for(;;) {

        if (len < 1)

            goto redo;

        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 */

        if (len < 2)

            goto redo;

        get_byte(&s->pb);

        c = get_byte(&s->pb);

        len -= 2;

    }

    if ((c & 0xf0) == 0x20) {

        if (len < 4)

            goto redo;

        dts = pts = get_pts(&s->pb, c);

        len -= 4;

    } else if ((c & 0xf0) == 0x30) {

        if (len < 9)

            goto redo;

        pts = get_pts(&s->pb, c);

        dts = get_pts(&s->pb, -1);

        len -= 9;

    } else if ((c & 0xc0) == 0x80) {

        /* mpeg 2 PES */

#if 0 /* some streams have this field set for no apparent reason */

        if ((c & 0x30) != 0) {

            /* Encrypted multiplex not handled */

            goto redo;

        }

#endif

        flags = get_byte(&s->pb);

        header_len = get_byte(&s->pb);

        len -= 2;

        if (header_len > len)

            goto redo;

        if ((flags & 0xc0) == 0x80) {

            dts = pts = get_pts(&s->pb, -1);

            if (header_len < 5)

                goto redo;

            header_len -= 5;

            len -= 5;

        } if ((flags & 0xc0) == 0xc0) {

            pts = get_pts(&s->pb, -1);

            dts = get_pts(&s->pb, -1);

            if (header_len < 10)

                goto redo;

            header_len -= 10;

            len -= 10;

        }

        len -= header_len;

        while (header_len > 0) {

            get_byte(&s->pb);

            header_len--;

        }

    }

    else if( c!= 0xf )

        goto redo;



    if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {

        if (len < 1)

            goto redo;

        startcode = get_byte(&s->pb);

        len--;

        if (startcode >= 0x80 && startcode <= 0xbf) {

            /* audio: skip header */

            if (len < 3)

                goto redo;

            get_byte(&s->pb);

            get_byte(&s->pb);

            get_byte(&s->pb);

            len -= 3;

        }

    }

    if(dts != AV_NOPTS_VALUE && ppos){

        int i;

        for(i=0; i<s->nb_streams; i++){

            if(startcode == s->streams[i]->id) {

                av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);

            }

        }

    }



    *pstart_code = startcode;

    *ppts = pts;

    *pdts = dts;

    return len;

}

static int mpegps_read_pes_header(AVFormatContext *s,

                                  int64_t *ppos, int *pstart_code,

                                  int64_t *ppts, int64_t *pdts)

{

    MpegDemuxContext *m = s->priv_data;

    int len, size, startcode, c, flags, header_len;

    int64_t pts, dts, last_pos;



    last_pos = -1;

 redo:

        

        m->header_state = 0xff;

        size = MAX_SYNC_SIZE;

        startcode = find_next_start_code(&s->pb, &size, &m->header_state);

    

    if (startcode < 0)

        return AVERROR_IO;

    if (startcode == PACK_START_CODE)

        goto redo;

    if (startcode == SYSTEM_HEADER_START_CODE)

        goto redo;

    if (startcode == PADDING_STREAM ||

        startcode == PRIVATE_STREAM_2) {

        

        len = get_be16(&s->pb);

        url_fskip(&s->pb, len);

        goto redo;

    }

    if (startcode == PROGRAM_STREAM_MAP) {

        mpegps_psm_parse(m, &s->pb);

        goto redo;

    }



    

    if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||

          (startcode >= 0x1e0 && startcode <= 0x1ef) ||

          (startcode == 0x1bd)))

        goto redo;

    if (ppos) {

        *ppos = url_ftell(&s->pb) - 4;

    }

    len = get_be16(&s->pb);

    pts = AV_NOPTS_VALUE;

    dts = AV_NOPTS_VALUE;

    

    for(;;) {

        if (len < 1)

            goto redo;

        c = get_byte(&s->pb);

        len--;

        

        if (c != 0xff)

            break;

    }

    if ((c & 0xc0) == 0x40) {

        

        if (len < 2)

            goto redo;

        get_byte(&s->pb);

        c = get_byte(&s->pb);

        len -= 2;

    }

    if ((c & 0xf0) == 0x20) {

        if (len < 4)

            goto redo;

        dts = pts = get_pts(&s->pb, c);

        len -= 4;

    } else if ((c & 0xf0) == 0x30) {

        if (len < 9)

            goto redo;

        pts = get_pts(&s->pb, c);

        dts = get_pts(&s->pb, -1);

        len -= 9;

    } else if ((c & 0xc0) == 0x80) {

        

#if 0 

        if ((c & 0x30) != 0) {

            

            goto redo;

        }

#endif

        flags = get_byte(&s->pb);

        header_len = get_byte(&s->pb);

        len -= 2;

        if (header_len > len)

            goto redo;

        if ((flags & 0xc0) == 0x80) {

            dts = pts = get_pts(&s->pb, -1);

            if (header_len < 5)

                goto redo;

            header_len -= 5;

            len -= 5;

        } if ((flags & 0xc0) == 0xc0) {

            pts = get_pts(&s->pb, -1);

            dts = get_pts(&s->pb, -1);

            if (header_len < 10)

                goto redo;

            header_len -= 10;

            len -= 10;

        }

        len -= header_len;

        while (header_len > 0) {

            get_byte(&s->pb);

            header_len--;

        }

    }

    else if( c!= 0xf )

        goto redo;



    if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {

        if (len < 1)

            goto redo;

        startcode = get_byte(&s->pb);

        len--;

        if (startcode >= 0x80 && startcode <= 0xbf) {

            

            if (len < 3)

                goto redo;

            get_byte(&s->pb);

            get_byte(&s->pb);

            get_byte(&s->pb);

            len -= 3;

        }

    }

    if(dts != AV_NOPTS_VALUE && ppos){

        int i;

        for(i=0; i<s->nb_streams; i++){

            if(startcode == s->streams[i]->id) {

                av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME );

            }

        }

    }



    *pstart_code = startcode;

    *ppts = pts;

    *pdts = dts;

    return len;

}

static int FUNC_0(AVFormatContext *VAR_0,

                                  int64_t *VAR_1, int *VAR_2,

                                  int64_t *VAR_3, int64_t *VAR_4)

{

    MpegDemuxContext *m = VAR_0->priv_data;

    int VAR_5, VAR_6, VAR_7, VAR_8, VAR_9, VAR_10;

    int64_t pts, dts, last_pos;



    last_pos = -1;

 redo:

        

        m->header_state = 0xff;

        VAR_6 = MAX_SYNC_SIZE;

        VAR_7 = find_next_start_code(&VAR_0->pb, &VAR_6, &m->header_state);

    

    if (VAR_7 < 0)

        return AVERROR_IO;

    if (VAR_7 == PACK_START_CODE)

        goto redo;

    if (VAR_7 == SYSTEM_HEADER_START_CODE)

        goto redo;

    if (VAR_7 == PADDING_STREAM ||

        VAR_7 == PRIVATE_STREAM_2) {

        

        VAR_5 = get_be16(&VAR_0->pb);

        url_fskip(&VAR_0->pb, VAR_5);

        goto redo;

    }

    if (VAR_7 == PROGRAM_STREAM_MAP) {

        mpegps_psm_parse(m, &VAR_0->pb);

        goto redo;

    }



    

    if (!((VAR_7 >= 0x1c0 && VAR_7 <= 0x1df) ||

          (VAR_7 >= 0x1e0 && VAR_7 <= 0x1ef) ||

          (VAR_7 == 0x1bd)))

        goto redo;

    if (VAR_1) {

        *VAR_1 = url_ftell(&VAR_0->pb) - 4;

    }

    VAR_5 = get_be16(&VAR_0->pb);

    pts = AV_NOPTS_VALUE;

    dts = AV_NOPTS_VALUE;

    

    for(;;) {

        if (VAR_5 < 1)

            goto redo;

        VAR_8 = get_byte(&VAR_0->pb);

        VAR_5--;

        

        if (VAR_8 != 0xff)

            break;

    }

    if ((VAR_8 & 0xc0) == 0x40) {

        

        if (VAR_5 < 2)

            goto redo;

        get_byte(&VAR_0->pb);

        VAR_8 = get_byte(&VAR_0->pb);

        VAR_5 -= 2;

    }

    if ((VAR_8 & 0xf0) == 0x20) {

        if (VAR_5 < 4)

            goto redo;

        dts = pts = get_pts(&VAR_0->pb, VAR_8);

        VAR_5 -= 4;

    } else if ((VAR_8 & 0xf0) == 0x30) {

        if (VAR_5 < 9)

            goto redo;

        pts = get_pts(&VAR_0->pb, VAR_8);

        dts = get_pts(&VAR_0->pb, -1);

        VAR_5 -= 9;

    } else if ((VAR_8 & 0xc0) == 0x80) {

        

#if 0 

        if ((VAR_8 & 0x30) != 0) {

            

            goto redo;

        }

#endif

        VAR_9 = get_byte(&VAR_0->pb);

        VAR_10 = get_byte(&VAR_0->pb);

        VAR_5 -= 2;

        if (VAR_10 > VAR_5)

            goto redo;

        if ((VAR_9 & 0xc0) == 0x80) {

            dts = pts = get_pts(&VAR_0->pb, -1);

            if (VAR_10 < 5)

                goto redo;

            VAR_10 -= 5;

            VAR_5 -= 5;

        } if ((VAR_9 & 0xc0) == 0xc0) {

            pts = get_pts(&VAR_0->pb, -1);

            dts = get_pts(&VAR_0->pb, -1);

            if (VAR_10 < 10)

                goto redo;

            VAR_10 -= 10;

            VAR_5 -= 10;

        }

        VAR_5 -= VAR_10;

        while (VAR_10 > 0) {

            get_byte(&VAR_0->pb);

            VAR_10--;

        }

    }

    else if( VAR_8!= 0xf )

        goto redo;



    if (VAR_7 == PRIVATE_STREAM_1 && !m->psm_es_type[VAR_7 & 0xff]) {

        if (VAR_5 < 1)

            goto redo;

        VAR_7 = get_byte(&VAR_0->pb);

        VAR_5--;

        if (VAR_7 >= 0x80 && VAR_7 <= 0xbf) {

            

            if (VAR_5 < 3)

                goto redo;

            get_byte(&VAR_0->pb);

            get_byte(&VAR_0->pb);

            get_byte(&VAR_0->pb);

            VAR_5 -= 3;

        }

    }

    if(dts != AV_NOPTS_VALUE && VAR_1){

        int VAR_11;

        for(VAR_11=0; VAR_11<VAR_0->nb_streams; VAR_11++){

            if(VAR_7 == VAR_0->streams[VAR_11]->id) {

                av_add_index_entry(VAR_0->streams[VAR_11], *VAR_1, dts, 0, 0, AVINDEX_KEYFRAME );

            }

        }

    }



    *VAR_2 = VAR_7;

    *VAR_3 = pts;

    *VAR_4 = dts;

    return VAR_5;

}

static int FUNC_0(AVFormatContext *VAR_0,
int64_t *VAR_1, int *VAR_2,
int64_t *VAR_3, int64_t *VAR_4)
{ MpegDemuxContext *m = VAR_0->priv_data; int VAR_5, VAR_6, VAR_7, VAR_8, VAR_9, VAR_10; int64_t pts, dts, last_pos; last_pos = -1; redo:
m->header_state = 0xff; VAR_6 = MAX_SYNC_SIZE; VAR_7 = find_next_start_code(&VAR_0->pb, &VAR_6, &m->header_state); if (VAR_7 < 0)
return AVERROR_IO; if (VAR_7 == PACK_START_CODE)
goto redo; if (VAR_7 == SYSTEM_HEADER_START_CODE)
goto redo; if (VAR_7 == PADDING_STREAM ||
VAR_7 == PRIVATE_STREAM_2) { VAR_5 = get_be16(&VAR_0->pb); url_fskip(&VAR_0->pb, VAR_5); goto redo; } if (VAR_7 == PROGRAM_STREAM_MAP) { mpegps_psm_parse(m, &VAR_0->pb); goto redo; } if (!((VAR_7 >= 0x1c0 && VAR_7 <= 0x1df) ||
(VAR_7 >= 0x1e0 && VAR_7 <= 0x1ef) ||
(VAR_7 == 0x1bd)))
goto redo; if (VAR_1) { *VAR_1 = url_ftell(&VAR_0->pb) - 4; } VAR_5 = get_be16(&VAR_0->pb); pts = AV_NOPTS_VALUE; dts = AV_NOPTS_VALUE; for(;;) { if (VAR_5 < 1)
goto redo; VAR_8 = get_byte(&VAR_0->pb); VAR_5--; if (VAR_8 != 0xff)
break; } if ((VAR_8 & 0xc0) == 0x40) { if (VAR_5 < 2)
goto redo; get_byte(&VAR_0->pb); VAR_8 = get_byte(&VAR_0->pb); VAR_5 -= 2; } if ((VAR_8 & 0xf0) == 0x20) { if (VAR_5 < 4)
goto redo; dts = pts = get_pts(&VAR_0->pb, VAR_8); VAR_5 -= 4; } else if ((VAR_8 & 0xf0) == 0x30) { if (VAR_5 < 9)
goto redo; pts = get_pts(&VAR_0->pb, VAR_8); dts = get_pts(&VAR_0->pb, -1); VAR_5 -= 9; } else if ((VAR_8 & 0xc0) == 0x80) { #if 0
if ((VAR_8 & 0x30) != 0) { goto redo; } #endif
VAR_9 = get_byte(&VAR_0->pb); VAR_10 = get_byte(&VAR_0->pb); VAR_5 -= 2; if (VAR_10 > VAR_5)
goto redo; if ((VAR_9 & 0xc0) == 0x80) { dts = pts = get_pts(&VAR_0->pb, -1); if (VAR_10 < 5)
goto redo; VAR_10 -= 5; VAR_5 -= 5; } if ((VAR_9 & 0xc0) == 0xc0) { pts = get_pts(&VAR_0->pb, -1); dts = get_pts(&VAR_0->pb, -1); if (VAR_10 < 10)
goto redo; VAR_10 -= 10; VAR_5 -= 10; } VAR_5 -= VAR_10; while (VAR_10 > 0) { get_byte(&VAR_0->pb); VAR_10--; } } else if( VAR_8!= 0xf )
goto redo; if (VAR_7 == PRIVATE_STREAM_1 && !m->psm_es_type[VAR_7 & 0xff]) { if (VAR_5 < 1)
goto redo; VAR_7 = get_byte(&VAR_0->pb); VAR_5--; if (VAR_7 >= 0x80 && VAR_7 <= 0xbf) { if (VAR_5 < 3)
goto redo; get_byte(&VAR_0->pb); get_byte(&VAR_0->pb); get_byte(&VAR_0->pb); VAR_5 -= 3; } } if(dts != AV_NOPTS_VALUE && VAR_1){ int VAR_11; for(VAR_11=0; VAR_11<VAR_0->nb_streams; VAR_11++){ if(VAR_7 == VAR_0->streams[VAR_11]->id) { av_add_index_entry(VAR_0->streams[VAR_11], *VAR_1, dts, 0, 0, AVINDEX_KEYFRAME ); } } } *VAR_2 = VAR_7; *VAR_3 = pts; *VAR_4 = dts; return VAR_5; }