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 mss4_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,\n                             AVPacket *avpkt)\n{\n    const uint8_t *buf = avpkt->data;\n    int buf_size = avpkt->size;\n    MSS4Context *c = avctx->priv_data;\n    GetBitContext gb;\n    GetByteContext bc;\n    uint8_t *dst[3];\n    int width, height, quality, frame_type;\n    int x, y, i, mb_width, mb_height, blk_type;\n    int ret;\n    if (buf_size < HEADER_SIZE) {\n        av_log(avctx, AV_LOG_ERROR,\n               "Frame should have at least %d bytes, got %d instead\\n",\n               HEADER_SIZE, buf_size);\n        return AVERROR_INVALIDDATA;\n    }\n    bytestream2_init(&bc, buf, buf_size);\n    width      = bytestream2_get_be16(&bc);\n    height     = bytestream2_get_be16(&bc);\n    bytestream2_skip(&bc, 2);\n    quality    = bytestream2_get_byte(&bc);\n    frame_type = bytestream2_get_byte(&bc);\n    if (width > avctx->width ||\n        height != avctx->height) {\n        av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d\\n",\n               width, height);\n        return AVERROR_INVALIDDATA;\n    }\n    if (quality < 1 || quality > 100) {\n        av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\\n", quality);\n        return AVERROR_INVALIDDATA;\n    }\n    if ((frame_type & ~3) || frame_type == 3) {\n        av_log(avctx, AV_LOG_ERROR, "Invalid frame type %d\\n", frame_type);\n        return AVERROR_INVALIDDATA;\n    }\n    if (frame_type != SKIP_FRAME && !bytestream2_get_bytes_left(&bc)) {\n        av_log(avctx, AV_LOG_ERROR,\n               "Empty frame found but it is not a skip frame.\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0) {\n        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\\n");\n        return ret;\n    }\n    c->pic->key_frame = (frame_type == INTRA_FRAME);\n    c->pic->pict_type = (frame_type == INTRA_FRAME) ? AV_PICTURE_TYPE_I\n                                                   : AV_PICTURE_TYPE_P;\n    if (frame_type == SKIP_FRAME) {\n        *got_frame      = 1;\n        if ((ret = av_frame_ref(data, c->pic)) < 0)\n            return ret;\n        return buf_size;\n    }\n    if (c->quality != quality) {\n        c->quality = quality;\n        for (i = 0; i < 2; i++)\n            ff_mss34_gen_quant_mat(c->quant_mat[i], quality, !i);\n    }\n    init_get_bits(&gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);\n    mb_width  = FFALIGN(width,  16) >> 4;\n    mb_height = FFALIGN(height, 16) >> 4;\n    dst[0] = c->pic->data[0];\n    dst[1] = c->pic->data[1];\n    dst[2] = c->pic->data[2];\n    memset(c->prev_vec, 0, sizeof(c->prev_vec));\n    for (y = 0; y < mb_height; y++) {\n        memset(c->dc_cache, 0, sizeof(c->dc_cache));\n        for (x = 0; x < mb_width; x++) {\n            blk_type = decode012(&gb);\n            switch (blk_type) {\n            case DCT_BLOCK:\n                if (mss4_decode_dct_block(c, &gb, dst, x, y) < 0) {\n                    av_log(avctx, AV_LOG_ERROR,\n                           "Error decoding DCT block %d,%d\\n",\n                           x, y);\n                    return AVERROR_INVALIDDATA;\n                }\n                break;\n            case IMAGE_BLOCK:\n                if (mss4_decode_image_block(c, &gb, dst, x, y) < 0) {\n                    av_log(avctx, AV_LOG_ERROR,\n                           "Error decoding VQ block %d,%d\\n",\n                           x, y);\n                    return AVERROR_INVALIDDATA;\n                }\n                break;\n            case SKIP_BLOCK:\n                if (frame_type == INTRA_FRAME) {\n                    av_log(avctx, AV_LOG_ERROR, "Skip block in intra frame\\n");\n                    return AVERROR_INVALIDDATA;\n                }\n                break;\n            }\n            if (blk_type != DCT_BLOCK)\n                mss4_update_dc_cache(c, x);\n        }\n        dst[0] += c->pic->linesize[0] * 16;\n        dst[1] += c->pic->linesize[1] * 16;\n        dst[2] += c->pic->linesize[2] * 16;\n    }\n    if ((ret = av_frame_ref(data, c->pic)) < 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}']