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 flashsv_decode_frame(AVCodecContext *avctx, void *data,\n                                int *got_frame, AVPacket *avpkt)\n{\n    int buf_size = avpkt->size;\n    FlashSVContext *s = avctx->priv_data;\n    int h_blocks, v_blocks, h_part, v_part, i, j, ret;\n    GetBitContext gb;\n    if (buf_size == 0)\n        return 0;\n    if (buf_size < 4)\n        return -1;\n    init_get_bits(&gb, avpkt->data, buf_size * 8);\n    s->block_width  = 16 * (get_bits(&gb, 4) + 1);\n    s->image_width  = get_bits(&gb, 12);\n    s->block_height = 16 * (get_bits(&gb, 4) + 1);\n    s->image_height = get_bits(&gb, 12);\n    if (s->ver == 2) {\n        skip_bits(&gb, 6);\n        if (get_bits1(&gb)) {\n            avpriv_request_sample(avctx, "iframe");\n            return AVERROR_PATCHWELCOME;\n        }\n        if (get_bits1(&gb)) {\n            avpriv_request_sample(avctx, "Custom palette");\n            return AVERROR_PATCHWELCOME;\n        }\n    }\n    h_blocks = s->image_width  / s->block_width;\n    h_part   = s->image_width  % s->block_width;\n    v_blocks = s->image_height / s->block_height;\n    v_part   = s->image_height % s->block_height;\n    if (s->block_size < s->block_width * s->block_height) {\n        int tmpblock_size = 3 * s->block_width * s->block_height, err;\n        if ((err = av_reallocp(&s->tmpblock, tmpblock_size)) < 0) {\n            s->block_size = 0;\n            av_log(avctx, AV_LOG_ERROR,\n                   "Cannot allocate decompression buffer.\\n");\n            return err;\n        }\n        if (s->ver == 2) {\n            s->deflate_block_size = calc_deflate_block_size(tmpblock_size);\n            if (s->deflate_block_size <= 0) {\n                av_log(avctx, AV_LOG_ERROR,\n                       "Cannot determine deflate buffer size.\\n");\n                return -1;\n            }\n            if ((err = av_reallocp(&s->deflate_block, s->deflate_block_size)) < 0) {\n                s->block_size = 0;\n                av_log(avctx, AV_LOG_ERROR, "Cannot allocate deflate buffer.\\n");\n                return err;\n            }\n        }\n    }\n    s->block_size = s->block_width * s->block_height;\n    if (avctx->width == 0 && avctx->height == 0) {\n        avctx->width  = s->image_width;\n        avctx->height = s->image_height;\n    }\n    if (avctx->width != s->image_width || avctx->height != s->image_height) {\n        av_log(avctx, AV_LOG_ERROR,\n               "Frame width or height differs from first frame!\\n");\n        av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d  vs  ch = %d, cv = %d\\n",\n               avctx->height, avctx->width, s->image_height, s->image_width);\n        return AVERROR_INVALIDDATA;\n    }\n    s->is_keyframe = (avpkt->flags & AV_PKT_FLAG_KEY) && (s->ver == 2);\n    if (s->is_keyframe) {\n        int err;\n        if ((err = av_reallocp(&s->keyframedata, avpkt->size)) < 0)\n            return err;\n        memcpy(s->keyframedata, avpkt->data, avpkt->size);\n        if ((err = av_reallocp(&s->blocks, (v_blocks + !!v_part) *\n                               (h_blocks + !!h_part) * sizeof(s->blocks[0]))) < 0)\n            return err;\n    }\n    av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\\n",\n            s->image_width, s->image_height, s->block_width, s->block_height,\n            h_blocks, v_blocks, h_part, v_part);\n    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {\n        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\\n");\n        return ret;\n    }\n    for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {\n        int y_pos  = j * s->block_height;\n        int cur_blk_height = (j < v_blocks) ? s->block_height : v_part;\n        for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {\n            int x_pos = i * s->block_width;\n            int cur_blk_width = (i < h_blocks) ? s->block_width : h_part;\n            int has_diff = 0;\n            int size = get_bits(&gb, 16);\n            s->color_depth    = 0;\n            s->zlibprime_curr = 0;\n            s->zlibprime_prev = 0;\n            s->diff_start     = 0;\n            s->diff_height    = cur_blk_height;\n            if (8 * size > get_bits_left(&gb)) {\n                av_frame_unref(s->frame);\n                return AVERROR_INVALIDDATA;\n            }\n            if (s->ver == 2 && size) {\n                skip_bits(&gb, 3);\n                s->color_depth    = get_bits(&gb, 2);\n                has_diff          = get_bits1(&gb);\n                s->zlibprime_curr = get_bits1(&gb);\n                s->zlibprime_prev = get_bits1(&gb);\n                if (s->color_depth != 0 && s->color_depth != 2) {\n                    av_log(avctx, AV_LOG_ERROR,\n                           "%dx%d invalid color depth %d\\n",\n                           i, j, s->color_depth);\n                    return AVERROR_INVALIDDATA;\n                }\n                if (has_diff) {\n                    if (!s->keyframe) {\n                        av_log(avctx, AV_LOG_ERROR,\n                               "Inter frame without keyframe\\n");\n                        return AVERROR_INVALIDDATA;\n                    }\n                    s->diff_start  = get_bits(&gb, 8);\n                    s->diff_height = get_bits(&gb, 8);\n                    if (s->diff_start + s->diff_height > cur_blk_height) {\n                        av_log(avctx, AV_LOG_ERROR,\n                               "Block parameters invalid: %d + %d > %d\\n",\n                               s->diff_start, s->diff_height, cur_blk_height);\n                        return AVERROR_INVALIDDATA;\n                    }\n                    av_log(avctx, AV_LOG_DEBUG,\n                           "%dx%d diff start %d height %d\\n",\n                           i, j, s->diff_start, s->diff_height);\n                    size -= 2;\n                }\n                if (s->zlibprime_prev)\n                    av_log(avctx, AV_LOG_DEBUG, "%dx%d zlibprime_prev\\n", i, j);\n                if (s->zlibprime_curr) {\n                    int col = get_bits(&gb, 8);\n                    int row = get_bits(&gb, 8);\n                    av_log(avctx, AV_LOG_DEBUG, "%dx%d zlibprime_curr %dx%d\\n",\n                           i, j, col, row);\n                    size -= 2;\n                    avpriv_request_sample(avctx, "zlibprime_curr");\n                    return AVERROR_PATCHWELCOME;\n                }\n                if (!s->blocks && (s->zlibprime_curr || s->zlibprime_prev)) {\n                    av_log(avctx, AV_LOG_ERROR,\n                           "no data available for zlib priming\\n");\n                    return AVERROR_INVALIDDATA;\n                }\n                size--;\n            }\n            if (has_diff) {\n                int k;\n                int off = (s->image_height - y_pos - 1) * s->frame->linesize[0];\n                for (k = 0; k < cur_blk_height; k++) {\n                    int x = off - k * s->frame->linesize[0] + x_pos * 3;\n                    memcpy(s->frame->data[0] + x, s->keyframe + x,\n                           cur_blk_width * 3);\n                }\n            }\n            if (size) {\n                if (flashsv_decode_block(avctx, avpkt, &gb, size,\n                                         cur_blk_width, cur_blk_height,\n                                         x_pos, y_pos,\n                                         i + j * (h_blocks + !!h_part)))\n                    av_log(avctx, AV_LOG_ERROR,\n                           "error in decompression of block %dx%d\\n", i, j);\n            }\n        }\n    }\n    if (s->is_keyframe && s->ver == 2) {\n        if (!s->keyframe) {\n            s->keyframe = av_malloc(s->frame->linesize[0] * avctx->height);\n            if (!s->keyframe) {\n                av_log(avctx, AV_LOG_ERROR, "Cannot allocate image data\\n");\n                return AVERROR(ENOMEM);\n            }\n        }\n        memcpy(s->keyframe, s->frame->data[0],\n               s->frame->linesize[0] * avctx->height);\n    }\n    if ((ret = av_frame_ref(data, s->frame)) < 0)\n        return ret;\n    *got_frame = 1;\n    if ((get_bits_count(&gb) / 8) != buf_size)\n        av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\\n",\n               buf_size, (get_bits_count(&gb) / 8));\n    return buf_size;\n}', 'static int flashsv_decode_block(AVCodecContext *avctx, AVPacket *avpkt,\n                                GetBitContext *gb, int block_size,\n                                int width, int height, int x_pos, int y_pos,\n                                int blk_idx)\n{\n    struct FlashSVContext *s = avctx->priv_data;\n    uint8_t *line = s->tmpblock;\n    int k;\n    int ret = inflateReset(&s->zstream);\n    if (ret != Z_OK) {\n        av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\\n", ret);\n        return AVERROR_UNKNOWN;\n    }\n    if (s->zlibprime_curr || s->zlibprime_prev) {\n        ret = flashsv2_prime(s,\n                             s->blocks[blk_idx].pos,\n                             s->blocks[blk_idx].size);\n        if (ret < 0)\n            return ret;\n    }\n    s->zstream.next_in   = avpkt->data + get_bits_count(gb) / 8;\n    s->zstream.avail_in  = block_size;\n    s->zstream.next_out  = s->tmpblock;\n    s->zstream.avail_out = s->block_size * 3;\n    ret = inflate(&s->zstream, Z_FINISH);\n    if (ret == Z_DATA_ERROR) {\n        av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\\n");\n        inflateSync(&s->zstream);\n        ret = inflate(&s->zstream, Z_FINISH);\n    }\n    if (ret != Z_OK && ret != Z_STREAM_END) {\n    }\n    if (s->is_keyframe) {\n        s->blocks[blk_idx].pos  = s->keyframedata + (get_bits_count(gb) / 8);\n        s->blocks[blk_idx].size = block_size;\n    }\n    y_pos += s->diff_start;\n    if (!s->color_depth) {\n        for (k = 1; k <= s->diff_height; k++) {\n            memcpy(s->frame->data[0] + x_pos * 3 +\n                   (s->image_height - y_pos - k) * s->frame->linesize[0],\n                   line, width * 3);\n            line += width * 3;\n        }\n    } else {\n        decode_hybrid(s->tmpblock, s->frame->data[0],\n                      s->image_height - (y_pos + 1 + s->diff_height),\n                      x_pos, s->diff_height, width,\n                      s->frame->linesize[0], s->pal);\n    }\n    skip_bits_long(gb, 8 * block_size);\n    return 0;\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}']