static int flac_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame_ptr, AVPacket *avpkt)
{
    AVFrame *frame     = data;
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
    FLACContext *s = avctx->priv_data;
    int bytes_read = 0;
    int ret;
    *got_frame_ptr = 0;
    if (s->max_framesize == 0) {
        s->max_framesize =
            ff_flac_get_max_frame_size(s->max_blocksize ? s->max_blocksize : FLAC_MAX_BLOCKSIZE,
                                       FLAC_MAX_CHANNELS, 32);
    }
    if (buf_size < FLAC_MIN_FRAME_SIZE)
        return buf_size;
    if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
        if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) {
            av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
            return ret;
        }
        return get_metadata_size(buf, buf_size);
    }
    init_get_bits(&s->gb, buf, buf_size*8);
    if ((ret = decode_frame(s)) < 0) {
        av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
        return ret;
    }
    bytes_read = (get_bits_count(&s->gb)+7)/8;
    frame->nb_samples = s->blocksize;
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        return ret;
    }
    s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded, s->channels,
                                   s->blocksize, s->sample_shift);
    if (bytes_read > buf_size) {
        av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
        return AVERROR_INVALIDDATA;
    }
    if (bytes_read < buf_size) {
        av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
               buf_size - bytes_read, buf_size);
    }
    *got_frame_ptr = 1;
    return bytes_read;
}
['static int flac_decode_frame(AVCodecContext *avctx, void *data,\n                             int *got_frame_ptr, AVPacket *avpkt)\n{\n    AVFrame *frame     = data;\n    const uint8_t *buf = avpkt->data;\n    int buf_size = avpkt->size;\n    FLACContext *s = avctx->priv_data;\n    int bytes_read = 0;\n    int ret;\n    *got_frame_ptr = 0;\n    if (s->max_framesize == 0) {\n        s->max_framesize =\n            ff_flac_get_max_frame_size(s->max_blocksize ? s->max_blocksize : FLAC_MAX_BLOCKSIZE,\n                                       FLAC_MAX_CHANNELS, 32);\n    }\n    if (buf_size < FLAC_MIN_FRAME_SIZE)\n        return buf_size;\n    if (AV_RB32(buf) == MKBETAG(\'f\',\'L\',\'a\',\'C\')) {\n        if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) {\n            av_log(s->avctx, AV_LOG_ERROR, "invalid header\\n");\n            return ret;\n        }\n        return get_metadata_size(buf, buf_size);\n    }\n    init_get_bits(&s->gb, buf, buf_size*8);\n    if ((ret = decode_frame(s)) < 0) {\n        av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\\n");\n        return ret;\n    }\n    bytes_read = (get_bits_count(&s->gb)+7)/8;\n    frame->nb_samples = s->blocksize;\n    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {\n        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n        return ret;\n    }\n    s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded, s->channels,\n                                   s->blocksize, s->sample_shift);\n    if (bytes_read > buf_size) {\n        av_log(s->avctx, AV_LOG_ERROR, "overread: %d\\n", bytes_read - buf_size);\n        return AVERROR_INVALIDDATA;\n    }\n    if (bytes_read < buf_size) {\n        av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\\n",\n               buf_size - bytes_read, buf_size);\n    }\n    *got_frame_ptr = 1;\n    return bytes_read;\n}', 'int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)\n{\n    int count;\n    count = 16;\n    count += ch * ((7+bps+7)/8);\n    if (ch == 2) {\n        count += (( 2*bps+1) * blocksize + 7) / 8;\n    } else {\n        count += ( ch*bps    * blocksize + 7) / 8;\n    }\n    count += 2;\n    return count;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n                                int bit_size)\n{\n    int buffer_size;\n    int ret = 0;\n    if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n        bit_size    = 0;\n        buffer      = NULL;\n        ret         = AVERROR_INVALIDDATA;\n    }\n    buffer_size = (bit_size + 7) >> 3;\n    s->buffer             = buffer;\n    s->size_in_bits       = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n    s->size_in_bits_plus8 = bit_size + 8;\n#endif\n    s->buffer_end         = buffer + buffer_size;\n    s->index              = 0;\n    return ret;\n}', 'static int decode_frame(FLACContext *s)\n{\n    int i, ret;\n    GetBitContext *gb = &s->gb;\n    FLACFrameInfo fi;\n    if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) {\n        av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\\n");\n        return ret;\n    }\n    if (s->channels && fi.channels != s->channels && s->got_streaminfo) {\n        s->channels = s->avctx->channels = fi.channels;\n        ff_flac_set_channel_layout(s->avctx);\n        ret = allocate_buffers(s);\n        if (ret < 0)\n            return ret;\n    }\n    s->channels = s->avctx->channels = fi.channels;\n    if (!s->avctx->channel_layout)\n        ff_flac_set_channel_layout(s->avctx);\n    s->ch_mode = fi.ch_mode;\n    if (!s->bps && !fi.bps) {\n        av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (!fi.bps) {\n        fi.bps = s->bps;\n    } else if (s->bps && fi.bps != s->bps) {\n        av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "\n                                       "supported\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (!s->bps) {\n        s->bps = s->avctx->bits_per_raw_sample = fi.bps;\n        flac_set_bps(s);\n    }\n    if (!s->max_blocksize)\n        s->max_blocksize = FLAC_MAX_BLOCKSIZE;\n    if (fi.blocksize > s->max_blocksize) {\n        av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\\n", fi.blocksize,\n               s->max_blocksize);\n        return AVERROR_INVALIDDATA;\n    }\n    s->blocksize = fi.blocksize;\n    if (!s->samplerate && !fi.samplerate) {\n        av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"\n                                        " or frame header\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (fi.samplerate == 0)\n        fi.samplerate = s->samplerate;\n    s->samplerate = s->avctx->sample_rate = fi.samplerate;\n    if (!s->got_streaminfo) {\n        ret = allocate_buffers(s);\n        if (ret < 0)\n            return ret;\n        ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps);\n        s->got_streaminfo = 1;\n        dump_headers(s->avctx, (FLACStreaminfo *)s);\n    }\n    for (i = 0; i < s->channels; i++) {\n        if ((ret = decode_subframe(s, i)) < 0)\n            return ret;\n    }\n    align_get_bits(gb);\n    skip_bits(gb, 16);\n    return 0;\n}', 'int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,\n                                FLACFrameInfo *fi, int log_level_offset)\n{\n    int bs_code, sr_code, bps_code;\n    if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {\n        av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    fi->is_var_size = get_bits1(gb);\n    bs_code = get_bits(gb, 4);\n    sr_code = get_bits(gb, 4);\n    fi->ch_mode = get_bits(gb, 4);\n    if (fi->ch_mode < FLAC_MAX_CHANNELS) {\n        fi->channels = fi->ch_mode + 1;\n        fi->ch_mode = FLAC_CHMODE_INDEPENDENT;\n    } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {\n        fi->channels = 2;\n        fi->ch_mode -= FLAC_MAX_CHANNELS - 1;\n    } else {\n        av_log(avctx, AV_LOG_ERROR + log_level_offset,\n               "invalid channel mode: %d\\n", fi->ch_mode);\n        return AVERROR_INVALIDDATA;\n    }\n    bps_code = get_bits(gb, 3);\n    if (bps_code == 3 || bps_code == 7) {\n        av_log(avctx, AV_LOG_ERROR + log_level_offset,\n               "invalid sample size code (%d)\\n",\n               bps_code);\n        return AVERROR_INVALIDDATA;\n    }\n    fi->bps = sample_size_table[bps_code];\n    if (get_bits1(gb)) {\n        av_log(avctx, AV_LOG_ERROR + log_level_offset,\n               "broken stream, invalid padding\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    fi->frame_or_sample_num = get_utf8(gb);\n    if (fi->frame_or_sample_num < 0) {\n        av_log(avctx, AV_LOG_ERROR + log_level_offset,\n               "sample/frame number invalid; utf8 fscked\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (bs_code == 0) {\n        av_log(avctx, AV_LOG_ERROR + log_level_offset,\n               "reserved blocksize code: 0\\n");\n        return AVERROR_INVALIDDATA;\n    } else if (bs_code == 6) {\n        fi->blocksize = get_bits(gb, 8) + 1;\n    } else if (bs_code == 7) {\n        fi->blocksize = get_bits(gb, 16) + 1;\n    } else {\n        fi->blocksize = ff_flac_blocksize_table[bs_code];\n    }\n    if (sr_code < 12) {\n        fi->samplerate = ff_flac_sample_rate_table[sr_code];\n    } else if (sr_code == 12) {\n        fi->samplerate = get_bits(gb, 8) * 1000;\n    } else if (sr_code == 13) {\n        fi->samplerate = get_bits(gb, 16);\n    } else if (sr_code == 14) {\n        fi->samplerate = get_bits(gb, 16) * 10;\n    } else {\n        av_log(avctx, AV_LOG_ERROR + log_level_offset,\n               "illegal sample rate code %d\\n",\n               sr_code);\n        return AVERROR_INVALIDDATA;\n    }\n    skip_bits(gb, 8);\n    if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,\n               get_bits_count(gb)/8)) {\n        av_log(avctx, AV_LOG_ERROR + log_level_offset,\n               "header crc mismatch\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    return 0;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n    unsigned int index = s->index;\n    uint8_t result     = s->buffer[index >> 3];\n#ifdef BITSTREAM_READER_LE\n    result >>= index & 7;\n    result  &= 1;\n#else\n    result <<= index & 7;\n    result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n    if (s->index < s->size_in_bits_plus8)\n#endif\n        index++;\n    s->index = index;\n    return result;\n}']