static int ogg_read_page(AVFormatContext *s, int *str)

{

    AVIOContext *bc = s->pb;

    struct ogg *ogg = s->priv_data;

    struct ogg_stream *os;

    int ret, i = 0;

    int flags, nsegs;

    uint64_t gp;

    uint32_t serial;

    int size, idx;

    uint8_t sync[4];

    int sp = 0;



    ret = avio_read(bc, sync, 4);

    if (ret < 4)

        return ret < 0 ? ret : AVERROR_EOF;



    do{

        int c;



        if (sync[sp & 3] == 'O' &&

            sync[(sp + 1) & 3] == 'g' &&

            sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')

            break;



        c = avio_r8(bc);

        if (bc->eof_reached)

            return AVERROR_EOF;

        sync[sp++ & 3] = c;

    }while (i++ < MAX_PAGE_SIZE);



    if (i >= MAX_PAGE_SIZE){

        av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");

        return AVERROR_INVALIDDATA;

    }



    if (avio_r8(bc) != 0)      /* version */

        return AVERROR_INVALIDDATA;



    flags = avio_r8(bc);

    gp = avio_rl64 (bc);

    serial = avio_rl32 (bc);

    avio_skip(bc, 8); /* seq, crc */

    nsegs = avio_r8(bc);



    idx = ogg_find_stream (ogg, serial);

    if (idx < 0){

        if (ogg->headers) {

            int n;



            for (n = 0; n < ogg->nstreams; n++) {

                av_freep(&ogg->streams[n].buf);

                if (!ogg->state || ogg->state->streams[n].private != ogg->streams[n].private)

                    av_freep(&ogg->streams[n].private);

            }

            ogg->curidx   = -1;

            ogg->nstreams = 0;

            idx = ogg_new_stream(s, serial, 0);

        } else {

            idx = ogg_new_stream(s, serial, 1);

        }

        if (idx < 0)

            return idx;

    }



    os = ogg->streams + idx;

    os->page_pos = avio_tell(bc) - 27;



    if(os->psize > 0)

        ogg_new_buf(ogg, idx);



    ret = avio_read(bc, os->segments, nsegs);

    if (ret < nsegs)

        return ret < 0 ? ret : AVERROR_EOF;



    os->nsegs = nsegs;

    os->segp = 0;



    size = 0;

    for (i = 0; i < nsegs; i++)

        size += os->segments[i];



    if (flags & OGG_FLAG_CONT || os->incomplete){

        if (!os->psize){

            while (os->segp < os->nsegs){

                int seg = os->segments[os->segp++];

                os->pstart += seg;

                if (seg < 255)

                    break;

            }

            os->sync_pos = os->page_pos;

        }

    }else{

        os->psize = 0;

        os->sync_pos = os->page_pos;

    }



    if (os->bufsize - os->bufpos < size){

        uint8_t *nb = av_malloc (os->bufsize *= 2);

        memcpy (nb, os->buf, os->bufpos);

        av_free (os->buf);

        os->buf = nb;

    }



    ret = avio_read(bc, os->buf + os->bufpos, size);

    if (ret < size)

        return ret < 0 ? ret : AVERROR_EOF;



    os->bufpos += size;

    os->granule = gp;

    os->flags = flags;



    if (str)

        *str = idx;



    return 0;

}

static int ogg_read_page(AVFormatContext *s, int *str)

{

    AVIOContext *bc = s->pb;

    struct ogg *ogg = s->priv_data;

    struct ogg_stream *os;

    int ret, i = 0;

    int flags, nsegs;

    uint64_t gp;

    uint32_t serial;

    int size, idx;

    uint8_t sync[4];

    int sp = 0;



    ret = avio_read(bc, sync, 4);

    if (ret < 4)

        return ret < 0 ? ret : AVERROR_EOF;



    do{

        int c;



        if (sync[sp & 3] == 'O' &&

            sync[(sp + 1) & 3] == 'g' &&

            sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')

            break;



        c = avio_r8(bc);

        if (bc->eof_reached)

            return AVERROR_EOF;

        sync[sp++ & 3] = c;

    }while (i++ < MAX_PAGE_SIZE);



    if (i >= MAX_PAGE_SIZE){

        av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");

        return AVERROR_INVALIDDATA;

    }



    if (avio_r8(bc) != 0)      

        return AVERROR_INVALIDDATA;



    flags = avio_r8(bc);

    gp = avio_rl64 (bc);

    serial = avio_rl32 (bc);

    avio_skip(bc, 8); 

    nsegs = avio_r8(bc);



    idx = ogg_find_stream (ogg, serial);

    if (idx < 0){

        if (ogg->headers) {

            int n;



            for (n = 0; n < ogg->nstreams; n++) {

                av_freep(&ogg->streams[n].buf);

                if (!ogg->state || ogg->state->streams[n].private != ogg->streams[n].private)

                    av_freep(&ogg->streams[n].private);

            }

            ogg->curidx   = -1;

            ogg->nstreams = 0;

            idx = ogg_new_stream(s, serial, 0);

        } else {

            idx = ogg_new_stream(s, serial, 1);

        }

        if (idx < 0)

            return idx;

    }



    os = ogg->streams + idx;

    os->page_pos = avio_tell(bc) - 27;



    if(os->psize > 0)

        ogg_new_buf(ogg, idx);



    ret = avio_read(bc, os->segments, nsegs);

    if (ret < nsegs)

        return ret < 0 ? ret : AVERROR_EOF;



    os->nsegs = nsegs;

    os->segp = 0;



    size = 0;

    for (i = 0; i < nsegs; i++)

        size += os->segments[i];



    if (flags & OGG_FLAG_CONT || os->incomplete){

        if (!os->psize){

            while (os->segp < os->nsegs){

                int seg = os->segments[os->segp++];

                os->pstart += seg;

                if (seg < 255)

                    break;

            }

            os->sync_pos = os->page_pos;

        }

    }else{

        os->psize = 0;

        os->sync_pos = os->page_pos;

    }



    if (os->bufsize - os->bufpos < size){

        uint8_t *nb = av_malloc (os->bufsize *= 2);

        memcpy (nb, os->buf, os->bufpos);

        av_free (os->buf);

        os->buf = nb;

    }



    ret = avio_read(bc, os->buf + os->bufpos, size);

    if (ret < size)

        return ret < 0 ? ret : AVERROR_EOF;



    os->bufpos += size;

    os->granule = gp;

    os->flags = flags;



    if (str)

        *str = idx;



    return 0;

}

        uint8_t *nb = av_malloc (os->bufsize *= 2);
static int FUNC_0(AVFormatContext *VAR_0, int *VAR_1)

{

    AVIOContext *bc = VAR_0->pb;

    struct VAR_2 *VAR_2 = VAR_0->priv_data;

    struct ogg_stream *VAR_3;

    int VAR_4, VAR_5 = 0;

    int VAR_6, VAR_7;

    uint64_t gp;

    uint32_t serial;

    int VAR_8, VAR_9;

    uint8_t sync[4];

    int VAR_10 = 0;



    VAR_4 = avio_read(bc, sync, 4);

    if (VAR_4 < 4)

        return VAR_4 < 0 ? VAR_4 : AVERROR_EOF;



    do{

        int VAR_11;



        if (sync[VAR_10 & 3] == 'O' &&

            sync[(VAR_10 + 1) & 3] == 'g' &&

            sync[(VAR_10 + 2) & 3] == 'g' && sync[(VAR_10 + 3) & 3] == 'S')

            break;



        VAR_11 = avio_r8(bc);

        if (bc->eof_reached)

            return AVERROR_EOF;

        sync[VAR_10++ & 3] = VAR_11;

    }while (VAR_5++ < MAX_PAGE_SIZE);



    if (VAR_5 >= MAX_PAGE_SIZE){

        av_log (VAR_0, AV_LOG_INFO, "VAR_2, can't find sync word\VAR_12");

        return AVERROR_INVALIDDATA;

    }



    if (avio_r8(bc) != 0)      

        return AVERROR_INVALIDDATA;



    VAR_6 = avio_r8(bc);

    gp = avio_rl64 (bc);

    serial = avio_rl32 (bc);

    avio_skip(bc, 8); 

    VAR_7 = avio_r8(bc);



    VAR_9 = ogg_find_stream (VAR_2, serial);

    if (VAR_9 < 0){

        if (VAR_2->headers) {

            int VAR_12;



            for (VAR_12 = 0; VAR_12 < VAR_2->nstreams; VAR_12++) {

                av_freep(&VAR_2->streams[VAR_12].buf);

                if (!VAR_2->state || VAR_2->state->streams[VAR_12].private != VAR_2->streams[VAR_12].private)

                    av_freep(&VAR_2->streams[VAR_12].private);

            }

            VAR_2->curidx   = -1;

            VAR_2->nstreams = 0;

            VAR_9 = ogg_new_stream(VAR_0, serial, 0);

        } else {

            VAR_9 = ogg_new_stream(VAR_0, serial, 1);

        }

        if (VAR_9 < 0)

            return VAR_9;

    }



    VAR_3 = VAR_2->streams + VAR_9;

    VAR_3->page_pos = avio_tell(bc) - 27;



    if(VAR_3->psize > 0)

        ogg_new_buf(VAR_2, VAR_9);



    VAR_4 = avio_read(bc, VAR_3->segments, VAR_7);

    if (VAR_4 < VAR_7)

        return VAR_4 < 0 ? VAR_4 : AVERROR_EOF;



    VAR_3->VAR_7 = VAR_7;

    VAR_3->segp = 0;



    VAR_8 = 0;

    for (VAR_5 = 0; VAR_5 < VAR_7; VAR_5++)

        VAR_8 += VAR_3->segments[VAR_5];



    if (VAR_6 & OGG_FLAG_CONT || VAR_3->incomplete){

        if (!VAR_3->psize){

            while (VAR_3->segp < VAR_3->VAR_7){

                int VAR_13 = VAR_3->segments[VAR_3->segp++];

                VAR_3->pstart += VAR_13;

                if (VAR_13 < 255)

                    break;

            }

            VAR_3->sync_pos = VAR_3->page_pos;

        }

    }else{

        VAR_3->psize = 0;

        VAR_3->sync_pos = VAR_3->page_pos;

    }



    if (VAR_3->bufsize - VAR_3->bufpos < VAR_8){

        uint8_t *nb = av_malloc (VAR_3->bufsize *= 2);

        memcpy (nb, VAR_3->buf, VAR_3->bufpos);

        av_free (VAR_3->buf);

        VAR_3->buf = nb;

    }



    VAR_4 = avio_read(bc, VAR_3->buf + VAR_3->bufpos, VAR_8);

    if (VAR_4 < VAR_8)

        return VAR_4 < 0 ? VAR_4 : AVERROR_EOF;



    VAR_3->bufpos += VAR_8;

    VAR_3->granule = gp;

    VAR_3->VAR_6 = VAR_6;



    if (VAR_1)

        *VAR_1 = VAR_9;



    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, int *VAR_1)
{ AVIOContext *bc = VAR_0->pb; struct VAR_2 *VAR_2 = VAR_0->priv_data; struct ogg_stream *VAR_3; int VAR_4, VAR_5 = 0; int VAR_6, VAR_7; uint64_t gp; uint32_t serial; int VAR_8, VAR_9; uint8_t sync[4]; int VAR_10 = 0; VAR_4 = avio_read(bc, sync, 4); if (VAR_4 < 4)
return VAR_4 < 0 ? VAR_4 : AVERROR_EOF; do{ int VAR_11; if (sync[VAR_10 & 3] == 'O' &&
sync[(VAR_10 + 1) & 3] == 'g' &&
sync[(VAR_10 + 2) & 3] == 'g' && sync[(VAR_10 + 3) & 3] == 'S')
break; VAR_11 = avio_r8(bc); if (bc->eof_reached)
return AVERROR_EOF; sync[VAR_10++ & 3] = VAR_11; }while (VAR_5++ < MAX_PAGE_SIZE); if (VAR_5 >= MAX_PAGE_SIZE){ av_log (VAR_0, AV_LOG_INFO, "VAR_2, can't find sync word\VAR_12"); return AVERROR_INVALIDDATA; } if (avio_r8(bc) != 0)
return AVERROR_INVALIDDATA; VAR_6 = avio_r8(bc); gp = avio_rl64 (bc); serial = avio_rl32 (bc); avio_skip(bc, 8); VAR_7 = avio_r8(bc); VAR_9 = ogg_find_stream (VAR_2, serial); if (VAR_9 < 0){ if (VAR_2->headers) { int VAR_12; for (VAR_12 = 0; VAR_12 < VAR_2->nstreams; VAR_12++) { av_freep(&VAR_2->streams[VAR_12].buf); if (!VAR_2->state || VAR_2->state->streams[VAR_12].private != VAR_2->streams[VAR_12].private)
av_freep(&VAR_2->streams[VAR_12].private); } VAR_2->curidx   = -1; VAR_2->nstreams = 0; VAR_9 = ogg_new_stream(VAR_0, serial, 0); } else { VAR_9 = ogg_new_stream(VAR_0, serial, 1); } if (VAR_9 < 0)
return VAR_9; } VAR_3 = VAR_2->streams + VAR_9; VAR_3->page_pos = avio_tell(bc) - 27; if(VAR_3->psize > 0)
ogg_new_buf(VAR_2, VAR_9); VAR_4 = avio_read(bc, VAR_3->segments, VAR_7); if (VAR_4 < VAR_7)
return VAR_4 < 0 ? VAR_4 : AVERROR_EOF; VAR_3->VAR_7 = VAR_7; VAR_3->segp = 0; VAR_8 = 0; for (VAR_5 = 0; VAR_5 < VAR_7; VAR_5++) VAR_8 += VAR_3->segments[VAR_5]; if (VAR_6 & OGG_FLAG_CONT || VAR_3->incomplete){ if (!VAR_3->psize){ while (VAR_3->segp < VAR_3->VAR_7){ int VAR_13 = VAR_3->segments[VAR_3->segp++]; VAR_3->pstart += VAR_13; if (VAR_13 < 255)
break; } VAR_3->sync_pos = VAR_3->page_pos; } }else{ VAR_3->psize = 0; VAR_3->sync_pos = VAR_3->page_pos; } if (VAR_3->bufsize - VAR_3->bufpos < VAR_8){ uint8_t *nb = av_malloc (VAR_3->bufsize *= 2); memcpy (nb, VAR_3->buf, VAR_3->bufpos); av_free (VAR_3->buf); VAR_3->buf = nb; } VAR_4 = avio_read(bc, VAR_3->buf + VAR_3->bufpos, VAR_8); if (VAR_4 < VAR_8)
return VAR_4 < 0 ? VAR_4 : AVERROR_EOF; VAR_3->bufpos += VAR_8; VAR_3->granule = gp; VAR_3->VAR_6 = VAR_6; if (VAR_1)
*VAR_1 = VAR_9; return 0; }