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;
}
['int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src)\n{\n    int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;\n    int ret = AVERROR(EINVAL);\n    int nearest_neighbour = (c->compensation_distance == 0 &&\n                             c->filter_length == 1 &&\n                             c->phase_shift == 0);\n    in_samples  = src ? src->nb_samples : 0;\n    in_leftover = c->buffer->nb_samples;\n    if (src) {\n        ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);\n        if (ret < 0)\n            return ret;\n    } else if (!in_leftover) {\n        return 0;\n    } else {\n    }\n    if (!c->initial_padding_filled) {\n        int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);\n        int i;\n        if (c->buffer->nb_samples < 2 * c->padding_size)\n            return 0;\n        for (i = 0; i < c->padding_size; i++)\n            for (ch = 0; ch < c->buffer->channels; ch++)\n                memcpy(c->buffer->data[ch] + bps * i,\n                       c->buffer->data[ch] + bps * (2 * c->padding_size - i), bps);\n        c->initial_padding_filled = 1;\n    }\n    if (!dst->read_only && dst->allow_realloc) {\n        out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples,\n                               INT_MAX, 0, nearest_neighbour);\n        ret = ff_audio_data_realloc(dst, out_samples);\n        if (ret < 0) {\n            av_log(c->avr, AV_LOG_ERROR, "error reallocating output\\n");\n            return ret;\n        }\n    }\n    for (ch = 0; ch < c->buffer->channels; ch++) {\n        out_samples = resample(c, (void *)dst->data[ch],\n                               (const void *)c->buffer->data[ch], &consumed,\n                               c->buffer->nb_samples, dst->allocated_samples,\n                               ch + 1 == c->buffer->channels, nearest_neighbour);\n    }\n    if (out_samples < 0) {\n        av_log(c->avr, AV_LOG_ERROR, "error during resampling\\n");\n        return out_samples;\n    }\n    ff_audio_data_drain(c->buffer, consumed);\n    c->initial_padding_samples = FFMAX(c->initial_padding_samples - consumed, 0);\n    av_dlog(c->avr, "resampled %d in + %d leftover to %d out + %d leftover\\n",\n            in_samples, in_leftover, out_samples, c->buffer->nb_samples);\n    dst->nb_samples = out_samples;\n    return 0;\n}', 'static int resample(ResampleContext *c, void *dst, const void *src,\n                    int *consumed, int src_size, int dst_size, int update_ctx,\n                    int nearest_neighbour)\n{\n    int dst_index;\n    unsigned int index = c->index;\n    int frac          = c->frac;\n    int dst_incr_frac = c->dst_incr % c->src_incr;\n    int dst_incr      = c->dst_incr / c->src_incr;\n    int compensation_distance = c->compensation_distance;\n    if (!dst != !src)\n        return AVERROR(EINVAL);\n    if (nearest_neighbour) {\n        uint64_t index2 = ((uint64_t)index) << 32;\n        int64_t incr   = (1LL << 32) * c->dst_incr / c->src_incr;\n        dst_size       = FFMIN(dst_size,\n                               (src_size-1-index) * (int64_t)c->src_incr /\n                               c->dst_incr);\n        if (dst) {\n            for(dst_index = 0; dst_index < dst_size; dst_index++) {\n                c->resample_nearest(dst, dst_index, src, index2 >> 32);\n                index2 += incr;\n            }\n        } else {\n            dst_index = dst_size;\n        }\n        index += dst_index * dst_incr;\n        index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;\n        frac   = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;\n    } else {\n        for (dst_index = 0; dst_index < dst_size; dst_index++) {\n            int sample_index = index >> c->phase_shift;\n            if (sample_index + c->filter_length > src_size)\n                break;\n            if (dst)\n                c->resample_one(c, dst, dst_index, src, index, frac);\n            frac  += dst_incr_frac;\n            index += dst_incr;\n            if (frac >= c->src_incr) {\n                frac -= c->src_incr;\n                index++;\n            }\n            if (dst_index + 1 == compensation_distance) {\n                compensation_distance = 0;\n                dst_incr_frac = c->ideal_dst_incr % c->src_incr;\n                dst_incr      = c->ideal_dst_incr / c->src_incr;\n            }\n        }\n    }\n    if (consumed)\n        *consumed = index >> c->phase_shift;\n    if (update_ctx) {\n        index &= c->phase_mask;\n        if (compensation_distance) {\n            compensation_distance -= dst_index;\n            if (compensation_distance <= 0)\n                return AVERROR_BUG;\n        }\n        c->frac     = frac;\n        c->index    = index;\n        c->dst_incr = dst_incr_frac + c->src_incr*dst_incr;\n        c->compensation_distance = compensation_distance;\n    }\n    return dst_index;\n}', 'int ff_audio_data_realloc(AudioData *a, int nb_samples)\n{\n    int ret, new_buf_size, plane_size, p;\n    if (a->allocated_samples >= nb_samples)\n        return 0;\n    if (a->read_only || !a->allow_realloc)\n        return AVERROR(EINVAL);\n    new_buf_size = av_samples_get_buffer_size(&plane_size,\n                                              a->allocated_channels, nb_samples,\n                                              a->sample_fmt, 0);\n    if (new_buf_size < 0)\n        return new_buf_size;\n    if (a->nb_samples > 0 && a->is_planar) {\n        uint8_t *new_data[AVRESAMPLE_MAX_CHANNELS] = { NULL };\n        ret = av_samples_alloc(new_data, &plane_size, a->allocated_channels,\n                               nb_samples, a->sample_fmt, 0);\n        if (ret < 0)\n            return ret;\n        for (p = 0; p < a->planes; p++)\n            memcpy(new_data[p], a->data[p], a->nb_samples * a->stride);\n        av_freep(&a->buffer);\n        memcpy(a->data, new_data, sizeof(new_data));\n        a->buffer = a->data[0];\n    } else {\n        av_freep(&a->buffer);\n        a->buffer = av_malloc(new_buf_size);\n        if (!a->buffer)\n            return AVERROR(ENOMEM);\n        ret = av_samples_fill_arrays(a->data, &plane_size, a->buffer,\n                                     a->allocated_channels, nb_samples,\n                                     a->sample_fmt, 0);\n        if (ret < 0)\n            return ret;\n    }\n    a->buffer_size       = new_buf_size;\n    a->allocated_samples = nb_samples;\n    calc_ptr_alignment(a);\n    a->samples_align = plane_size / a->stride;\n    return 0;\n}', 'int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,\n                     int nb_samples, enum AVSampleFormat sample_fmt, int align)\n{\n    uint8_t *buf;\n    int size = av_samples_get_buffer_size(NULL, nb_channels, nb_samples,\n                                          sample_fmt, align);\n    if (size < 0)\n        return size;\n    buf = av_malloc(size);\n    if (!buf)\n        return AVERROR(ENOMEM);\n    size = av_samples_fill_arrays(audio_data, linesize, buf, nb_channels,\n                                  nb_samples, sample_fmt, align);\n    if (size < 0) {\n        av_free(buf);\n        return size;\n    }\n    av_samples_set_silence(audio_data, 0, nb_samples, nb_channels, sample_fmt);\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}']