static int vp3_decode_frame(AVCodecContext *avctx,
                            void *data, int *data_size,
                            AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
    Vp3DecodeContext *s = avctx->priv_data;
    GetBitContext gb;
    static int counter = 0;
    int i;
    init_get_bits(&gb, buf, buf_size * 8);
    if (s->theora && get_bits1(&gb))
    {
        av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
        return -1;
    }
    s->keyframe = !get_bits1(&gb);
    if (!s->theora)
        skip_bits(&gb, 1);
    for (i = 0; i < 3; i++)
        s->last_qps[i] = s->qps[i];
    s->nqps=0;
    do{
        s->qps[s->nqps++]= get_bits(&gb, 6);
    } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb));
    for (i = s->nqps; i < 3; i++)
        s->qps[i] = -1;
    if (s->avctx->debug & FF_DEBUG_PICT_INFO)
        av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
            s->keyframe?"key":"", counter, s->qps[0]);
    counter++;
    if (s->qps[0] != s->last_qps[0])
        init_loop_filter(s);
    for (i = 0; i < s->nqps; i++)
        if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
            init_dequantizer(s, i);
    if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
        return buf_size;
    s->current_frame.reference = 3;
    s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
    if (avctx->get_buffer(avctx, &s->current_frame) < 0) {
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        goto error;
    }
    if (s->keyframe) {
        if (!s->theora)
        {
            skip_bits(&gb, 4);
            skip_bits(&gb, 4);
            if (s->version)
            {
                s->version = get_bits(&gb, 5);
                if (counter == 1)
                    av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
            }
        }
        if (s->version || s->theora)
        {
                if (get_bits1(&gb))
                    av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
            skip_bits(&gb, 2);
        }
    } else {
        if (!s->golden_frame.data[0]) {
            av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n");
            s->golden_frame.reference = 3;
            s->golden_frame.pict_type = FF_I_TYPE;
            if (avctx->get_buffer(avctx, &s->golden_frame) < 0) {
                av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
                goto error;
            }
            s->last_frame = s->golden_frame;
            s->last_frame.type = FF_BUFFER_TYPE_COPY;
        }
    }
    s->current_frame.qscale_table= s->qscale_table;
    s->current_frame.qstride= 0;
    memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
    if (unpack_superblocks(s, &gb)){
        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
        goto error;
    }
    if (unpack_modes(s, &gb)){
        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
        goto error;
    }
    if (unpack_vectors(s, &gb)){
        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
        goto error;
    }
    if (unpack_block_qpis(s, &gb)){
        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
        goto error;
    }
    if (unpack_dct_coeffs(s, &gb)){
        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
        goto error;
    }
    for (i = 0; i < 3; i++) {
        int height = s->height >> (i && s->chroma_y_shift);
        if (s->flipped_image)
            s->data_offset[i] = 0;
        else
            s->data_offset[i] = (height-1) * s->current_frame.linesize[i];
    }
    s->last_slice_end = 0;
    for (i = 0; i < s->c_superblock_height; i++)
        render_slice(s, i);
    for (i = 0; i < 3; i++) {
        int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;
        apply_loop_filter(s, i, row, row+1);
    }
    vp3_draw_horiz_band(s, s->height);
    *data_size=sizeof(AVFrame);
    *(AVFrame*)data= s->current_frame;
    if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
        avctx->release_buffer(avctx, &s->last_frame);
    s->last_frame= s->current_frame;
    if (s->keyframe) {
        if (s->golden_frame.data[0])
            avctx->release_buffer(avctx, &s->golden_frame);
        s->golden_frame = s->current_frame;
        s->last_frame.type = FF_BUFFER_TYPE_COPY;
    }
    s->current_frame.data[0]= NULL;
    return buf_size;
error:
    if (s->current_frame.data[0])
        avctx->release_buffer(avctx, &s->current_frame);
    return -1;
}
['static int vp3_decode_frame(AVCodecContext *avctx,\n                            void *data, int *data_size,\n                            AVPacket *avpkt)\n{\n    const uint8_t *buf = avpkt->data;\n    int buf_size = avpkt->size;\n    Vp3DecodeContext *s = avctx->priv_data;\n    GetBitContext gb;\n    static int counter = 0;\n    int i;\n    init_get_bits(&gb, buf, buf_size * 8);\n    if (s->theora && get_bits1(&gb))\n    {\n        av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\\n");\n        return -1;\n    }\n    s->keyframe = !get_bits1(&gb);\n    if (!s->theora)\n        skip_bits(&gb, 1);\n    for (i = 0; i < 3; i++)\n        s->last_qps[i] = s->qps[i];\n    s->nqps=0;\n    do{\n        s->qps[s->nqps++]= get_bits(&gb, 6);\n    } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb));\n    for (i = s->nqps; i < 3; i++)\n        s->qps[i] = -1;\n    if (s->avctx->debug & FF_DEBUG_PICT_INFO)\n        av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\\n",\n            s->keyframe?"key":"", counter, s->qps[0]);\n    counter++;\n    if (s->qps[0] != s->last_qps[0])\n        init_loop_filter(s);\n    for (i = 0; i < s->nqps; i++)\n        if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])\n            init_dequantizer(s, i);\n    if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)\n        return buf_size;\n    s->current_frame.reference = 3;\n    s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;\n    if (avctx->get_buffer(avctx, &s->current_frame) < 0) {\n        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n        goto error;\n    }\n    if (s->keyframe) {\n        if (!s->theora)\n        {\n            skip_bits(&gb, 4);\n            skip_bits(&gb, 4);\n            if (s->version)\n            {\n                s->version = get_bits(&gb, 5);\n                if (counter == 1)\n                    av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\\n", s->version);\n            }\n        }\n        if (s->version || s->theora)\n        {\n                if (get_bits1(&gb))\n                    av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\\n");\n            skip_bits(&gb, 2);\n        }\n    } else {\n        if (!s->golden_frame.data[0]) {\n            av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\\n");\n            s->golden_frame.reference = 3;\n            s->golden_frame.pict_type = FF_I_TYPE;\n            if (avctx->get_buffer(avctx, &s->golden_frame) < 0) {\n                av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n                goto error;\n            }\n            s->last_frame = s->golden_frame;\n            s->last_frame.type = FF_BUFFER_TYPE_COPY;\n        }\n    }\n    s->current_frame.qscale_table= s->qscale_table;\n    s->current_frame.qstride= 0;\n    memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));\n    if (unpack_superblocks(s, &gb)){\n        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\\n");\n        goto error;\n    }\n    if (unpack_modes(s, &gb)){\n        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\\n");\n        goto error;\n    }\n    if (unpack_vectors(s, &gb)){\n        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\\n");\n        goto error;\n    }\n    if (unpack_block_qpis(s, &gb)){\n        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\\n");\n        goto error;\n    }\n    if (unpack_dct_coeffs(s, &gb)){\n        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\\n");\n        goto error;\n    }\n    for (i = 0; i < 3; i++) {\n        int height = s->height >> (i && s->chroma_y_shift);\n        if (s->flipped_image)\n            s->data_offset[i] = 0;\n        else\n            s->data_offset[i] = (height-1) * s->current_frame.linesize[i];\n    }\n    s->last_slice_end = 0;\n    for (i = 0; i < s->c_superblock_height; i++)\n        render_slice(s, i);\n    for (i = 0; i < 3; i++) {\n        int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;\n        apply_loop_filter(s, i, row, row+1);\n    }\n    vp3_draw_horiz_band(s, s->height);\n    *data_size=sizeof(AVFrame);\n    *(AVFrame*)data= s->current_frame;\n    if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)\n        avctx->release_buffer(avctx, &s->last_frame);\n    s->last_frame= s->current_frame;\n    if (s->keyframe) {\n        if (s->golden_frame.data[0])\n            avctx->release_buffer(avctx, &s->golden_frame);\n        s->golden_frame = s->current_frame;\n        s->last_frame.type = FF_BUFFER_TYPE_COPY;\n    }\n    s->current_frame.data[0]= NULL;\n    return buf_size;\nerror:\n    if (s->current_frame.data[0])\n        avctx->release_buffer(avctx, &s->current_frame);\n    return -1;\n}', 'static inline void init_get_bits(GetBitContext *s,\n                   const uint8_t *buffer, int bit_size)\n{\n    int buffer_size= (bit_size+7)>>3;\n    if(buffer_size < 0 || bit_size < 0) {\n        buffer_size = bit_size = 0;\n        buffer = NULL;\n    }\n    s->buffer= buffer;\n    s->size_in_bits= bit_size;\n    s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n    s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n    s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n    s->bit_count = 16 + 8*((intptr_t)buffer&1);\n    skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n    s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n    s->bit_count = 32 + 8*((intptr_t)buffer&3);\n    skip_bits_long(s, 0);\n#endif\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n    unsigned int index= s->index;\n    uint8_t result= s->buffer[ index>>3 ];\n#ifdef ALT_BITSTREAM_READER_LE\n    result>>= (index&0x07);\n    result&= 1;\n#else\n    result<<= (index&0x07);\n    result>>= 8 - 1;\n#endif\n    index++;\n    s->index= index;\n    return result;\n#else\n    return get_bits(s, 1);\n#endif\n}']