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

{

    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;



        if(!i && bc->seekable && ogg->page_pos > 0) {

            memset(sync, 0, 4);

            avio_seek(bc, ogg->page_pos+4, SEEK_SET);

            ogg->page_pos = -1;

        }



        c = avio_r8(bc);



        if (avio_feof(bc))

            return AVERROR_EOF;



        sync[sp++ & 3] = c;

    } while (i++ < MAX_PAGE_SIZE);



    if (i >= MAX_PAGE_SIZE) {

        av_log(s, AV_LOG_INFO, "cannot find sync word\n");

        return AVERROR_INVALIDDATA;

    }



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

        av_log (s, AV_LOG_ERROR, "ogg page, unsupported version\n");

        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 (data_packets_seen(ogg))

            idx = ogg_replace_stream(s, serial, nsegs);

        else

            idx = ogg_new_stream(s, serial);



        if (idx < 0) {

            av_log(s, AV_LOG_ERROR, "failed to create or replace stream\n");

            return idx;

        }

    }



    os = ogg->streams + idx;

    ogg->page_pos =

    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_BOS))

        os->got_data = 1;



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

        if (!os->psize) {

            // If this is the very first segment we started

            // playback in the middle of a continuation packet.

            // Discard it since we missed the start of it.

            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) + FF_INPUT_BUFFER_PADDING_SIZE);

        if (!nb)

            return AVERROR(ENOMEM);

        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;



    memset(os->buf + os->bufpos, 0, FF_INPUT_BUFFER_PADDING_SIZE);

    if (sid)

        *sid = idx;



    return 0;

}

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

{

    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;



        if(!i && bc->seekable && ogg->page_pos > 0) {

            memset(sync, 0, 4);

            avio_seek(bc, ogg->page_pos+4, SEEK_SET);

            ogg->page_pos = -1;

        }



        c = avio_r8(bc);



        if (avio_feof(bc))

            return AVERROR_EOF;



        sync[sp++ & 3] = c;

    } while (i++ < MAX_PAGE_SIZE);



    if (i >= MAX_PAGE_SIZE) {

        av_log(s, AV_LOG_INFO, "cannot find sync word\n");

        return AVERROR_INVALIDDATA;

    }



    if (avio_r8(bc) != 0) {      

        av_log (s, AV_LOG_ERROR, "ogg page, unsupported version\n");

        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 (data_packets_seen(ogg))

            idx = ogg_replace_stream(s, serial, nsegs);

        else

            idx = ogg_new_stream(s, serial);



        if (idx < 0) {

            av_log(s, AV_LOG_ERROR, "failed to create or replace stream\n");

            return idx;

        }

    }



    os = ogg->streams + idx;

    ogg->page_pos =

    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_BOS))

        os->got_data = 1;



    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) + FF_INPUT_BUFFER_PADDING_SIZE);

        if (!nb)

            return AVERROR(ENOMEM);

        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;



    memset(os->buf + os->bufpos, 0, FF_INPUT_BUFFER_PADDING_SIZE);

    if (sid)

        *sid = idx;



    return 0;

}

    if (os->psize > 0)         ogg_new_buf(ogg, idx);
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;



        if(!VAR_5 && bc->seekable && VAR_2->page_pos > 0) {

            memset(sync, 0, 4);

            avio_seek(bc, VAR_2->page_pos+4, SEEK_SET);

            VAR_2->page_pos = -1;

        }



        VAR_11 = avio_r8(bc);



        if (avio_feof(bc))

            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, "cannot find sync word\n");

        return AVERROR_INVALIDDATA;

    }



    if (avio_r8(bc) != 0) {      

        av_log (VAR_0, AV_LOG_ERROR, "VAR_2 page, unsupported version\n");

        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 (data_packets_seen(VAR_2))

            VAR_9 = ogg_replace_stream(VAR_0, serial, VAR_7);

        else

            VAR_9 = ogg_new_stream(VAR_0, serial);



        if (VAR_9 < 0) {

            av_log(VAR_0, AV_LOG_ERROR, "failed to create or replace stream\n");

            return VAR_9;

        }

    }



    VAR_3 = VAR_2->streams + VAR_9;

    VAR_2->page_pos =

    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_BOS))

        VAR_3->got_data = 1;



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

        if (!VAR_3->psize) {

            

            

            

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

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

                VAR_3->pstart += VAR_12;

                if (VAR_12 < 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) + FF_INPUT_BUFFER_PADDING_SIZE);

        if (!nb)

            return AVERROR(ENOMEM);

        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;



    memset(VAR_3->buf + VAR_3->bufpos, 0, FF_INPUT_BUFFER_PADDING_SIZE);

    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; if(!VAR_5 && bc->seekable && VAR_2->page_pos > 0) { memset(sync, 0, 4); avio_seek(bc, VAR_2->page_pos+4, SEEK_SET); VAR_2->page_pos = -1; } VAR_11 = avio_r8(bc); if (avio_feof(bc))
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, "cannot find sync word\n"); return AVERROR_INVALIDDATA; } if (avio_r8(bc) != 0) { av_log (VAR_0, AV_LOG_ERROR, "VAR_2 page, unsupported version\n"); 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 (data_packets_seen(VAR_2))
VAR_9 = ogg_replace_stream(VAR_0, serial, VAR_7); else
VAR_9 = ogg_new_stream(VAR_0, serial); if (VAR_9 < 0) { av_log(VAR_0, AV_LOG_ERROR, "failed to create or replace stream\n"); return VAR_9; } } VAR_3 = VAR_2->streams + VAR_9; VAR_2->page_pos =
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_BOS))
VAR_3->got_data = 1; if (VAR_6 & OGG_FLAG_CONT || VAR_3->incomplete) { if (!VAR_3->psize) { while (VAR_3->segp < VAR_3->VAR_7) { int VAR_12 = VAR_3->segments[VAR_3->segp++]; VAR_3->pstart += VAR_12; if (VAR_12 < 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) + FF_INPUT_BUFFER_PADDING_SIZE); if (!nb)
return AVERROR(ENOMEM); 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; memset(VAR_3->buf + VAR_3->bufpos, 0, FF_INPUT_BUFFER_PADDING_SIZE); if (VAR_1)
*VAR_1 = VAR_9; return 0; }