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 mp_decode_frame(AVCodecContext *avctx,\n                                 void *data, int *got_frame,\n                                 AVPacket *avpkt)\n{\n    const uint8_t *buf = avpkt->data;\n    int buf_size = avpkt->size;\n    MotionPixelsContext *mp = avctx->priv_data;\n    GetBitContext gb;\n    int i, count1, count2, sz, ret;\n    if ((ret = ff_reget_buffer(avctx, mp->frame)) < 0) {\n        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\\n");\n        return ret;\n    }\n    av_fast_malloc(&mp->bswapbuf, &mp->bswapbuf_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);\n    if (!mp->bswapbuf)\n        return AVERROR(ENOMEM);\n    mp->dsp.bswap_buf((uint32_t *)mp->bswapbuf, (const uint32_t *)buf, buf_size / 4);\n    if (buf_size & 3)\n        memcpy(mp->bswapbuf + (buf_size & ~3), buf + (buf_size & ~3), buf_size & 3);\n    memset(mp->bswapbuf + buf_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);\n    init_get_bits(&gb, mp->bswapbuf, buf_size * 8);\n    memset(mp->changes_map, 0, avctx->width * avctx->height);\n    for (i = !(avctx->extradata[1] & 2); i < 2; ++i) {\n        count1 = get_bits(&gb, 12);\n        count2 = get_bits(&gb, 12);\n        mp_read_changes_map(mp, &gb, count1, 8, i);\n        mp_read_changes_map(mp, &gb, count2, 4, i);\n    }\n    mp->codes_count = get_bits(&gb, 4);\n    if (mp->codes_count == 0)\n        goto end;\n    if (mp->changes_map[0] == 0) {\n        *(uint16_t *)mp->frame->data[0] = get_bits(&gb, 15);\n        mp->changes_map[0] = 1;\n    }\n    mp_read_codes_table(mp, &gb);\n    sz = get_bits(&gb, 18);\n    if (avctx->extradata[0] != 5)\n        sz += get_bits(&gb, 18);\n    if (sz == 0)\n        goto end;\n    if (mp->max_codes_bits <= 0)\n        goto end;\n    if (init_vlc(&mp->vlc, mp->max_codes_bits, mp->codes_count, &mp->codes[0].size, sizeof(HuffCode), 1, &mp->codes[0].code, sizeof(HuffCode), 4, 0))\n        goto end;\n    mp_decode_frame_helper(mp, &gb);\n    ff_free_vlc(&mp->vlc);\nend:\n    if ((ret = av_frame_ref(data, mp->frame)) < 0)\n        return ret;\n    *got_frame       = 1;\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}']