int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
                               enum AVSampleFormat sample_fmt, int align)
{
    int line_size;
    int sample_size = av_get_bytes_per_sample(sample_fmt);
    int planar      = av_sample_fmt_is_planar(sample_fmt);
    if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
        return AVERROR(EINVAL);
    if (!align) {
        if (nb_samples > INT_MAX - 31)
            return AVERROR(EINVAL);
        align = 1;
        nb_samples = FFALIGN(nb_samples, 32);
    }
    if (nb_channels > INT_MAX / align ||
        (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)
        return AVERROR(EINVAL);
    line_size = planar ? FFALIGN(nb_samples * sample_size,               align) :
                         FFALIGN(nb_samples * sample_size * nb_channels, align);
    if (linesize)
        *linesize = line_size;
    return planar ? line_size * nb_channels : line_size;
}
['static int tak_decode_frame(AVCodecContext *avctx, void *data,\n                            int *got_frame_ptr, AVPacket *pkt)\n{\n    TAKDecContext *s  = avctx->priv_data;\n    AVFrame *frame    = data;\n    BitstreamContext *bc = &s->bc;\n    int chan, i, ret, hsize;\n    if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)\n        return AVERROR_INVALIDDATA;\n    bitstream_init8(bc, pkt->data, pkt->size);\n    if ((ret = ff_tak_decode_frame_header(avctx, bc, &s->ti, 0)) < 0)\n        return ret;\n    if (s->ti.flags & TAK_FRAME_FLAG_HAS_METADATA) {\n        avpriv_request_sample(avctx, "Frame metadata");\n        return AVERROR_PATCHWELCOME;\n    }\n    hsize = bitstream_tell(bc) / 8;\n    if (avctx->err_recognition & AV_EF_CRCCHECK) {\n        if (ff_tak_check_crc(pkt->data, hsize)) {\n            av_log(avctx, AV_LOG_ERROR, "CRC error\\n");\n            if (avctx->err_recognition & AV_EF_EXPLODE)\n                return AVERROR_INVALIDDATA;\n        }\n    }\n    if (s->ti.codec != TAK_CODEC_MONO_STEREO &&\n        s->ti.codec != TAK_CODEC_MULTICHANNEL) {\n        avpriv_report_missing_feature(avctx, "TAK codec type %d", s->ti.codec);\n        return AVERROR_PATCHWELCOME;\n    }\n    if (s->ti.data_type) {\n        av_log(avctx, AV_LOG_ERROR,\n               "unsupported data type: %d\\n", s->ti.data_type);\n        return AVERROR_INVALIDDATA;\n    }\n    if (s->ti.codec == TAK_CODEC_MONO_STEREO && s->ti.channels > 2) {\n        av_log(avctx, AV_LOG_ERROR,\n               "invalid number of channels: %d\\n", s->ti.channels);\n        return AVERROR_INVALIDDATA;\n    }\n    if (s->ti.channels > 6) {\n        av_log(avctx, AV_LOG_ERROR,\n               "unsupported number of channels: %d\\n", s->ti.channels);\n        return AVERROR_INVALIDDATA;\n    }\n    if (s->ti.frame_samples <= 0) {\n        av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of samples\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (s->ti.bps != avctx->bits_per_coded_sample) {\n        avctx->bits_per_coded_sample = s->ti.bps;\n        if ((ret = set_bps_params(avctx)) < 0)\n            return ret;\n    }\n    if (s->ti.sample_rate != avctx->sample_rate) {\n        avctx->sample_rate = s->ti.sample_rate;\n        set_sample_rate_params(avctx);\n    }\n    if (s->ti.ch_layout)\n        avctx->channel_layout = s->ti.ch_layout;\n    avctx->channels = s->ti.channels;\n    s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples\n                                             : s->ti.frame_samples;\n    frame->nb_samples = s->nb_samples;\n    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)\n        return ret;\n    if (avctx->bits_per_coded_sample <= 16) {\n        int buf_size = av_samples_get_buffer_size(NULL, avctx->channels,\n                                                  s->nb_samples,\n                                                  AV_SAMPLE_FMT_S32P, 0);\n        if (buf_size < 0)\n            return buf_size;\n        av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size, buf_size);\n        if (!s->decode_buffer)\n            return AVERROR(ENOMEM);\n        ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,\n                                     s->decode_buffer, avctx->channels,\n                                     s->nb_samples, AV_SAMPLE_FMT_S32P, 0);\n        if (ret < 0)\n            return ret;\n    } else {\n        for (chan = 0; chan < avctx->channels; chan++)\n            s->decoded[chan] = (int32_t *)frame->extended_data[chan];\n    }\n    if (s->nb_samples < 16) {\n        for (chan = 0; chan < avctx->channels; chan++) {\n            int32_t *decoded = s->decoded[chan];\n            for (i = 0; i < s->nb_samples; i++)\n                decoded[i] = bitstream_read_signed(bc, avctx->bits_per_coded_sample);\n        }\n    } else {\n        if (s->ti.codec == TAK_CODEC_MONO_STEREO) {\n            for (chan = 0; chan < avctx->channels; chan++)\n                if (ret = decode_channel(s, chan))\n                    return ret;\n            if (avctx->channels == 2) {\n                if (bitstream_read_bit(bc)) {\n                    bitstream_skip(bc, 6);\n                }\n                s->dmode = bitstream_read(bc, 3);\n                if (ret = decorrelate(s, 0, 1, s->nb_samples - 1))\n                    return ret;\n            }\n        } else if (s->ti.codec == TAK_CODEC_MULTICHANNEL) {\n            if (bitstream_read_bit(bc)) {\n                int ch_mask = 0;\n                chan = bitstream_read(bc, 4) + 1;\n                if (chan > avctx->channels)\n                    return AVERROR_INVALIDDATA;\n                for (i = 0; i < chan; i++) {\n                    int nbit = bitstream_read(bc, 4);\n                    if (nbit >= avctx->channels)\n                        return AVERROR_INVALIDDATA;\n                    if (ch_mask & 1 << nbit)\n                        return AVERROR_INVALIDDATA;\n                    s->mcdparams[i].present = bitstream_read_bit(bc);\n                    if (s->mcdparams[i].present) {\n                        s->mcdparams[i].index = bitstream_read(bc, 2);\n                        s->mcdparams[i].chan2 = bitstream_read(bc, 4);\n                        if (s->mcdparams[i].chan2 >= avctx->channels) {\n                            av_log(avctx, AV_LOG_ERROR,\n                                   "invalid channel 2 (%d) for %d channel(s)\\n",\n                                   s->mcdparams[i].chan2, avctx->channels);\n                            return AVERROR_INVALIDDATA;\n                        }\n                        if (s->mcdparams[i].index == 1) {\n                            if ((nbit == s->mcdparams[i].chan2) ||\n                                (ch_mask & 1 << s->mcdparams[i].chan2))\n                                return AVERROR_INVALIDDATA;\n                            ch_mask |= 1 << s->mcdparams[i].chan2;\n                        } else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) {\n                            return AVERROR_INVALIDDATA;\n                        }\n                    }\n                    s->mcdparams[i].chan1 = nbit;\n                    ch_mask |= 1 << nbit;\n                }\n            } else {\n                chan = avctx->channels;\n                for (i = 0; i < chan; i++) {\n                    s->mcdparams[i].present = 0;\n                    s->mcdparams[i].chan1   = i;\n                }\n            }\n            for (i = 0; i < chan; i++) {\n                if (s->mcdparams[i].present && s->mcdparams[i].index == 1)\n                    if (ret = decode_channel(s, s->mcdparams[i].chan2))\n                        return ret;\n                if (ret = decode_channel(s, s->mcdparams[i].chan1))\n                    return ret;\n                if (s->mcdparams[i].present) {\n                    s->dmode = mc_dmodes[s->mcdparams[i].index];\n                    if (ret = decorrelate(s,\n                                          s->mcdparams[i].chan2,\n                                          s->mcdparams[i].chan1,\n                                          s->nb_samples - 1))\n                        return ret;\n                }\n            }\n        }\n        for (chan = 0; chan < avctx->channels; chan++) {\n            int32_t *decoded = s->decoded[chan];\n            if (s->lpc_mode[chan])\n                decode_lpc(decoded, s->lpc_mode[chan], s->nb_samples);\n            if (s->sample_shift[chan] > 0)\n                for (i = 0; i < s->nb_samples; i++)\n                    decoded[i] <<= s->sample_shift[chan];\n        }\n    }\n    bitstream_align(bc);\n    bitstream_skip(bc, 24);\n    if (bitstream_bits_left(bc) < 0)\n        av_log(avctx, AV_LOG_DEBUG, "overread\\n");\n    else if (bitstream_bits_left(bc) > 0)\n        av_log(avctx, AV_LOG_DEBUG, "underread\\n");\n    if (avctx->err_recognition & AV_EF_CRCCHECK) {\n        if (ff_tak_check_crc(pkt->data + hsize,\n                             bitstream_tell(bc) / 8 - hsize)) {\n            av_log(avctx, AV_LOG_ERROR, "CRC error\\n");\n            if (avctx->err_recognition & AV_EF_EXPLODE)\n                return AVERROR_INVALIDDATA;\n        }\n    }\n    switch (avctx->sample_fmt) {\n    case AV_SAMPLE_FMT_U8P:\n        for (chan = 0; chan < avctx->channels; chan++) {\n            uint8_t *samples = (uint8_t *)frame->extended_data[chan];\n            int32_t *decoded = s->decoded[chan];\n            for (i = 0; i < s->nb_samples; i++)\n                samples[i] = decoded[i] + 0x80;\n        }\n        break;\n    case AV_SAMPLE_FMT_S16P:\n        for (chan = 0; chan < avctx->channels; chan++) {\n            int16_t *samples = (int16_t *)frame->extended_data[chan];\n            int32_t *decoded = s->decoded[chan];\n            for (i = 0; i < s->nb_samples; i++)\n                samples[i] = decoded[i];\n        }\n        break;\n    case AV_SAMPLE_FMT_S32P:\n        for (chan = 0; chan < avctx->channels; chan++) {\n            int32_t *samples = (int32_t *)frame->extended_data[chan];\n            for (i = 0; i < s->nb_samples; i++)\n                samples[i] <<= 8;\n        }\n        break;\n    }\n    *got_frame_ptr = 1;\n    return pkt->size;\n}', 'int ff_tak_decode_frame_header(AVCodecContext *avctx, BitstreamContext *bc,\n                               TAKStreamInfo *ti, int log_level_offset)\n{\n    if (bitstream_read(bc, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {\n        av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    ti->flags     = bitstream_read(bc, TAK_FRAME_HEADER_FLAGS_BITS);\n    ti->frame_num = bitstream_read(bc, TAK_FRAME_HEADER_NO_BITS);\n    if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {\n        ti->last_frame_samples = bitstream_read(bc, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;\n        bitstream_skip(bc, 2);\n    } else {\n        ti->last_frame_samples = 0;\n    }\n    if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {\n        avpriv_tak_parse_streaminfo(bc, ti);\n        if (bitstream_read(bc, 6))\n            bitstream_skip(bc, 25);\n        bitstream_align(bc);\n    }\n    bitstream_skip(bc, 24);\n    return 0;\n}', 'int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,\n                           const uint8_t *buf, int nb_channels, int nb_samples,\n                           enum AVSampleFormat sample_fmt, int align)\n{\n    int ch, planar, buf_size, line_size;\n    planar   = av_sample_fmt_is_planar(sample_fmt);\n    buf_size = av_samples_get_buffer_size(&line_size, nb_channels, nb_samples,\n                                          sample_fmt, align);\n    if (buf_size < 0)\n        return buf_size;\n    audio_data[0] = buf;\n    for (ch = 1; planar && ch < nb_channels; ch++)\n        audio_data[ch] = audio_data[ch-1] + line_size;\n    if (linesize)\n        *linesize = line_size;\n    return 0;\n}', 'int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,\n                               enum AVSampleFormat sample_fmt, int align)\n{\n    int line_size;\n    int sample_size = av_get_bytes_per_sample(sample_fmt);\n    int planar      = av_sample_fmt_is_planar(sample_fmt);\n    if (!sample_size || nb_samples <= 0 || nb_channels <= 0)\n        return AVERROR(EINVAL);\n    if (!align) {\n        if (nb_samples > INT_MAX - 31)\n            return AVERROR(EINVAL);\n        align = 1;\n        nb_samples = FFALIGN(nb_samples, 32);\n    }\n    if (nb_channels > INT_MAX / align ||\n        (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)\n        return AVERROR(EINVAL);\n    line_size = planar ? FFALIGN(nb_samples * sample_size,               align) :\n                         FFALIGN(nb_samples * sample_size * nb_channels, align);\n    if (linesize)\n        *linesize = line_size;\n    return planar ? line_size * nb_channels : line_size;\n}']