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 attribute_align_arg avresample_convert(AVAudioResampleContext *avr,\n                                           uint8_t **output, int out_plane_size,\n                                           int out_samples, uint8_t **input,\n                                           int in_plane_size, int in_samples)\n{\n    AudioData input_buffer;\n    AudioData output_buffer;\n    AudioData *current_buffer;\n    int ret, direct_output;\n    if (avr->in_buffer) {\n        avr->in_buffer->nb_samples = 0;\n        ff_audio_data_set_channels(avr->in_buffer,\n                                   avr->in_buffer->allocated_channels);\n    }\n    if (avr->resample_out_buffer) {\n        avr->resample_out_buffer->nb_samples = 0;\n        ff_audio_data_set_channels(avr->resample_out_buffer,\n                                   avr->resample_out_buffer->allocated_channels);\n    }\n    if (avr->out_buffer) {\n        avr->out_buffer->nb_samples = 0;\n        ff_audio_data_set_channels(avr->out_buffer,\n                                   avr->out_buffer->allocated_channels);\n    }\n    av_dlog(avr, "[start conversion]\\n");\n    direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;\n    if (output) {\n        ret = ff_audio_data_init(&output_buffer, output, out_plane_size,\n                                 avr->out_channels, out_samples,\n                                 avr->out_sample_fmt, 0, "output");\n        if (ret < 0)\n            return ret;\n        output_buffer.nb_samples = 0;\n    }\n    if (input) {\n        ret = ff_audio_data_init(&input_buffer, input, in_plane_size,\n                                 avr->in_channels, in_samples,\n                                 avr->in_sample_fmt, 1, "input");\n        if (ret < 0)\n            return ret;\n        current_buffer = &input_buffer;\n        if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&\n            !avr->out_convert_needed && direct_output && out_samples >= in_samples) {\n            av_dlog(avr, "[copy] %s to output\\n", current_buffer->name);\n            ret = ff_audio_data_copy(&output_buffer, current_buffer,\n                                     avr->remap_point == REMAP_OUT_COPY ?\n                                     &avr->ch_map_info : NULL);\n            if (ret < 0)\n                return ret;\n            current_buffer = &output_buffer;\n        } else if (avr->remap_point == REMAP_OUT_COPY &&\n                   (!direct_output || out_samples < in_samples)) {\n            av_dlog(avr, "[copy] %s to out_buffer\\n", current_buffer->name);\n            ret = ff_audio_data_copy(avr->out_buffer, current_buffer,\n                                     &avr->ch_map_info);\n            if (ret < 0)\n                return ret;\n            current_buffer = avr->out_buffer;\n        } else if (avr->in_copy_needed || avr->in_convert_needed) {\n            if (avr->in_convert_needed) {\n                ret = ff_audio_data_realloc(avr->in_buffer,\n                                            current_buffer->nb_samples);\n                if (ret < 0)\n                    return ret;\n                av_dlog(avr, "[convert] %s to in_buffer\\n", current_buffer->name);\n                ret = ff_audio_convert(avr->ac_in, avr->in_buffer,\n                                       current_buffer);\n                if (ret < 0)\n                    return ret;\n            } else {\n                av_dlog(avr, "[copy] %s to in_buffer\\n", current_buffer->name);\n                ret = ff_audio_data_copy(avr->in_buffer, current_buffer,\n                                         avr->remap_point == REMAP_IN_COPY ?\n                                         &avr->ch_map_info : NULL);\n                if (ret < 0)\n                    return ret;\n            }\n            ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);\n            if (avr->downmix_needed) {\n                av_dlog(avr, "[downmix] in_buffer\\n");\n                ret = ff_audio_mix(avr->am, avr->in_buffer);\n                if (ret < 0)\n                    return ret;\n            }\n            current_buffer = avr->in_buffer;\n        }\n    } else {\n        if (!avr->resample_needed)\n            return handle_buffered_output(avr, output ? &output_buffer : NULL,\n                                          NULL);\n        current_buffer = NULL;\n    }\n    if (avr->resample_needed) {\n        AudioData *resample_out;\n        if (!avr->out_convert_needed && direct_output && out_samples > 0)\n            resample_out = &output_buffer;\n        else\n            resample_out = avr->resample_out_buffer;\n        av_dlog(avr, "[resample] %s to %s\\n",\n                current_buffer ? current_buffer->name : "null",\n                resample_out->name);\n        ret = ff_audio_resample(avr->resample, resample_out,\n                                current_buffer);\n        if (ret < 0)\n            return ret;\n        if (resample_out->nb_samples == 0) {\n            av_dlog(avr, "[end conversion]\\n");\n            return 0;\n        }\n        current_buffer = resample_out;\n    }\n    if (avr->upmix_needed) {\n        av_dlog(avr, "[upmix] %s\\n", current_buffer->name);\n        ret = ff_audio_mix(avr->am, current_buffer);\n        if (ret < 0)\n            return ret;\n    }\n    if (current_buffer == &output_buffer) {\n        av_dlog(avr, "[end conversion]\\n");\n        return current_buffer->nb_samples;\n    }\n    if (avr->out_convert_needed) {\n        if (direct_output && out_samples >= current_buffer->nb_samples) {\n            av_dlog(avr, "[convert] %s to output\\n", current_buffer->name);\n            ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);\n            if (ret < 0)\n                return ret;\n            av_dlog(avr, "[end conversion]\\n");\n            return output_buffer.nb_samples;\n        } else {\n            ret = ff_audio_data_realloc(avr->out_buffer,\n                                        current_buffer->nb_samples);\n            if (ret < 0)\n                return ret;\n            av_dlog(avr, "[convert] %s to out_buffer\\n", current_buffer->name);\n            ret = ff_audio_convert(avr->ac_out, avr->out_buffer,\n                                   current_buffer);\n            if (ret < 0)\n                return ret;\n            current_buffer = avr->out_buffer;\n        }\n    }\n    return handle_buffered_output(avr, output ? &output_buffer : NULL,\n                                  current_buffer);\n}', 'int ff_audio_data_init(AudioData *a, uint8_t **src, int plane_size, int channels,\n                       int nb_samples, enum AVSampleFormat sample_fmt,\n                       int read_only, const char *name)\n{\n    int p;\n    memset(a, 0, sizeof(*a));\n    a->class = &audio_data_class;\n    if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS) {\n        av_log(a, AV_LOG_ERROR, "invalid channel count: %d\\n", channels);\n        return AVERROR(EINVAL);\n    }\n    a->sample_size = av_get_bytes_per_sample(sample_fmt);\n    if (!a->sample_size) {\n        av_log(a, AV_LOG_ERROR, "invalid sample format\\n");\n        return AVERROR(EINVAL);\n    }\n    a->is_planar = av_sample_fmt_is_planar(sample_fmt);\n    a->planes    = a->is_planar ? channels : 1;\n    a->stride    = a->sample_size * (a->is_planar ? 1 : channels);\n    for (p = 0; p < (a->is_planar ? channels : 1); p++) {\n        if (!src[p]) {\n            av_log(a, AV_LOG_ERROR, "invalid NULL pointer for src[%d]\\n", p);\n            return AVERROR(EINVAL);\n        }\n        a->data[p] = src[p];\n    }\n    a->allocated_samples  = nb_samples * !read_only;\n    a->nb_samples         = nb_samples;\n    a->sample_fmt         = sample_fmt;\n    a->channels           = channels;\n    a->allocated_channels = channels;\n    a->read_only          = read_only;\n    a->allow_realloc      = 0;\n    a->name               = name ? name : "{no name}";\n    calc_ptr_alignment(a);\n    a->samples_align = plane_size / a->stride;\n    return 0;\n}', 'int ff_audio_data_copy(AudioData *dst, AudioData *src, ChannelMapInfo *map)\n{\n    int ret, p;\n    if (dst->sample_fmt != src->sample_fmt || dst->channels < src->channels)\n        return AVERROR(EINVAL);\n    if (map && !src->is_planar) {\n        av_log(src, AV_LOG_ERROR, "cannot remap packed format during copy\\n");\n        return AVERROR(EINVAL);\n    }\n    if (!src->nb_samples) {\n        dst->nb_samples = 0;\n        return 0;\n    }\n    ret = ff_audio_data_realloc(dst, src->nb_samples);\n    if (ret < 0)\n        return ret;\n    if (map) {\n        if (map->do_remap) {\n            for (p = 0; p < src->planes; p++) {\n                if (map->channel_map[p] >= 0)\n                    memcpy(dst->data[p], src->data[map->channel_map[p]],\n                           src->nb_samples * src->stride);\n            }\n        }\n        if (map->do_copy || map->do_zero) {\n            for (p = 0; p < src->planes; p++) {\n                if (map->channel_copy[p])\n                    memcpy(dst->data[p], dst->data[map->channel_copy[p]],\n                           src->nb_samples * src->stride);\n                else if (map->channel_zero[p])\n                    av_samples_set_silence(&dst->data[p], 0, src->nb_samples,\n                                           1, dst->sample_fmt);\n            }\n        }\n    } else {\n        for (p = 0; p < src->planes; p++)\n            memcpy(dst->data[p], src->data[p], src->nb_samples * src->stride);\n    }\n    dst->nb_samples = src->nb_samples;\n    return 0;\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_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}']