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 decode_frame(AVCodecContext *avctx,\n                        void *data, int *got_frame,\n                        AVPacket *avpkt)\n{\n    AnmContext *s = avctx->priv_data;\n    const int buf_size = avpkt->size;\n    uint8_t *dst, *dst_end;\n    int count, ret;\n    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0){\n        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n        return ret;\n    }\n    dst     = s->frame->data[0];\n    dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height;\n    bytestream2_init(&s->gb, avpkt->data, buf_size);\n    if (bytestream2_get_byte(&s->gb) != 0x42) {\n        avpriv_request_sample(avctx, "Unknown record type");\n        return buf_size;\n    }\n    if (bytestream2_get_byte(&s->gb)) {\n        avpriv_request_sample(avctx, "Padding bytes");\n        return buf_size;\n    }\n    bytestream2_skip(&s->gb, 2);\n    s->x = 0;\n    do {\n#define OP(gb, pixel, count) \\\n    op(&dst, dst_end, (gb), (pixel), (count), &s->x, avctx->width, s->frame->linesize[0])\n        int type = bytestream2_get_byte(&s->gb);\n        count = type & 0x7F;\n        type >>= 7;\n        if (count) {\n            if (OP(type ? NULL : &s->gb, -1, count)) break;\n        } else if (!type) {\n            int pixel;\n            count = bytestream2_get_byte(&s->gb);\n            pixel = bytestream2_get_byte(&s->gb);\n            if (OP(NULL, pixel, count)) break;\n        } else {\n            int pixel;\n            type = bytestream2_get_le16(&s->gb);\n            count = type & 0x3FFF;\n            type >>= 14;\n            if (!count) {\n                if (type == 0)\n                    break;\n                if (type == 2) {\n                    avpriv_request_sample(avctx, "Unknown opcode");\n                    return AVERROR_PATCHWELCOME;\n                }\n                continue;\n            }\n            pixel = type == 3 ? bytestream2_get_byte(&s->gb) : -1;\n            if (type == 1) count += 0x4000;\n            if (OP(type == 2 ? &s->gb : NULL, pixel, count)) break;\n        }\n    } while (bytestream2_get_bytes_left(&s->gb) > 0);\n    memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);\n    *got_frame = 1;\n    if ((ret = av_frame_ref(data, s->frame)) < 0)\n        return ret;\n    return buf_size;\n}', 'int av_frame_ref(AVFrame *dst, const AVFrame *src)\n{\n    int i, ret = 0;\n    dst->format         = src->format;\n    dst->width          = src->width;\n    dst->height         = src->height;\n    dst->channel_layout = src->channel_layout;\n    dst->nb_samples     = src->nb_samples;\n    ret = av_frame_copy_props(dst, src);\n    if (ret < 0)\n        return ret;\n    if (!src->buf[0]) {\n        ret = av_frame_get_buffer(dst, 32);\n        if (ret < 0)\n            return ret;\n        if (src->nb_samples) {\n            int ch = av_get_channel_layout_nb_channels(src->channel_layout);\n            av_samples_copy(dst->extended_data, src->extended_data, 0, 0,\n                            dst->nb_samples, ch, dst->format);\n        } else {\n            av_image_copy(dst->data, dst->linesize, src->data, src->linesize,\n                          dst->format, dst->width, dst->height);\n        }\n        return 0;\n    }\n    for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {\n        dst->buf[i] = av_buffer_ref(src->buf[i]);\n        if (!dst->buf[i]) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n    }\n    if (src->extended_buf) {\n        dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *\n                                       src->nb_extended_buf);\n        if (!dst->extended_buf) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n        dst->nb_extended_buf = src->nb_extended_buf;\n        for (i = 0; i < src->nb_extended_buf; i++) {\n            dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);\n            if (!dst->extended_buf[i]) {\n                ret = AVERROR(ENOMEM);\n                goto fail;\n            }\n        }\n    }\n    if (src->extended_data != src->data) {\n        int ch = av_get_channel_layout_nb_channels(src->channel_layout);\n        if (!ch) {\n            ret = AVERROR(EINVAL);\n            goto fail;\n        }\n        dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);\n        if (!dst->extended_data) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n        memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);\n    } else\n        dst->extended_data = dst->data;\n    memcpy(dst->data,     src->data,     sizeof(src->data));\n    memcpy(dst->linesize, src->linesize, sizeof(src->linesize));\n    return 0;\nfail:\n    av_frame_unref(dst);\n    return ret;\n}', 'int av_frame_get_buffer(AVFrame *frame, int align)\n{\n    if (frame->format < 0)\n        return AVERROR(EINVAL);\n    if (frame->width > 0 && frame->height > 0)\n        return get_video_buffer(frame, align);\n    else if (frame->nb_samples > 0 && frame->channel_layout)\n        return get_audio_buffer(frame, align);\n    return AVERROR(EINVAL);\n}', 'static int get_audio_buffer(AVFrame *frame, int align)\n{\n    int channels = av_get_channel_layout_nb_channels(frame->channel_layout);\n    int planar   = av_sample_fmt_is_planar(frame->format);\n    int planes   = planar ? channels : 1;\n    int ret, i;\n    if (!frame->linesize[0]) {\n        ret = av_samples_get_buffer_size(&frame->linesize[0], channels,\n                                         frame->nb_samples, frame->format,\n                                         align);\n        if (ret < 0)\n            return ret;\n    }\n    if (planes > AV_NUM_DATA_POINTERS) {\n        frame->extended_data = av_mallocz(planes *\n                                          sizeof(*frame->extended_data));\n        frame->extended_buf  = av_mallocz((planes - AV_NUM_DATA_POINTERS) *\n                                          sizeof(*frame->extended_buf));\n        if (!frame->extended_data || !frame->extended_buf) {\n            av_freep(&frame->extended_data);\n            av_freep(&frame->extended_buf);\n            return AVERROR(ENOMEM);\n        }\n        frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;\n    } else\n        frame->extended_data = frame->data;\n    for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {\n        frame->buf[i] = av_buffer_alloc(frame->linesize[0]);\n        if (!frame->buf[i]) {\n            av_frame_unref(frame);\n            return AVERROR(ENOMEM);\n        }\n        frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;\n    }\n    for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {\n        frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);\n        if (!frame->extended_buf[i]) {\n            av_frame_unref(frame);\n            return AVERROR(ENOMEM);\n        }\n        frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;\n    }\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}']