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_entropy_coded_image(WebPContext *s, enum ImageRole role,\n                                      int w, int h)\n{\n    ImageContext *img;\n    HuffReader *hg;\n    int i, j, ret, x, y, width;\n    img       = &s->image[role];\n    img->role = role;\n    if (!img->frame) {\n        img->frame = av_frame_alloc();\n        if (!img->frame)\n            return AVERROR(ENOMEM);\n    }\n    img->frame->format = AV_PIX_FMT_ARGB;\n    img->frame->width  = w;\n    img->frame->height = h;\n    if (role == IMAGE_ROLE_ARGB && !img->is_alpha_primary) {\n        ThreadFrame pt = { .f = img->frame };\n        ret = ff_thread_get_buffer(s->avctx, &pt, 0);\n    } else\n        ret = av_frame_get_buffer(img->frame, 1);\n    if (ret < 0)\n        return ret;\n    if (get_bits1(&s->gb)) {\n        img->color_cache_bits = get_bits(&s->gb, 4);\n        if (img->color_cache_bits < 1 || img->color_cache_bits > 11) {\n            av_log(s->avctx, AV_LOG_ERROR, "invalid color cache bits: %d\\n",\n                   img->color_cache_bits);\n            return AVERROR_INVALIDDATA;\n        }\n        img->color_cache = av_mallocz_array(1 << img->color_cache_bits,\n                                            sizeof(*img->color_cache));\n        if (!img->color_cache)\n            return AVERROR(ENOMEM);\n    } else {\n        img->color_cache_bits = 0;\n    }\n    img->nb_huffman_groups = 1;\n    if (role == IMAGE_ROLE_ARGB && get_bits1(&s->gb)) {\n        ret = decode_entropy_image(s);\n        if (ret < 0)\n            return ret;\n        img->nb_huffman_groups = s->nb_huffman_groups;\n    }\n    img->huffman_groups = av_mallocz_array(img->nb_huffman_groups *\n                                           HUFFMAN_CODES_PER_META_CODE,\n                                           sizeof(*img->huffman_groups));\n    if (!img->huffman_groups)\n        return AVERROR(ENOMEM);\n    for (i = 0; i < img->nb_huffman_groups; i++) {\n        hg = &img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE];\n        for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) {\n            int alphabet_size = alphabet_sizes[j];\n            if (!j && img->color_cache_bits > 0)\n                alphabet_size += 1 << img->color_cache_bits;\n            if (get_bits1(&s->gb)) {\n                read_huffman_code_simple(s, &hg[j]);\n            } else {\n                ret = read_huffman_code_normal(s, &hg[j], alphabet_size);\n                if (ret < 0)\n                    return ret;\n            }\n        }\n    }\n    width = img->frame->width;\n    if (role == IMAGE_ROLE_ARGB && s->reduced_width > 0)\n        width = s->reduced_width;\n    x = 0; y = 0;\n    while (y < img->frame->height) {\n        int v;\n        hg = get_huffman_group(s, img, x, y);\n        v = huff_reader_get_symbol(&hg[HUFF_IDX_GREEN], &s->gb);\n        if (v < NUM_LITERAL_CODES) {\n            uint8_t *p = GET_PIXEL(img->frame, x, y);\n            p[2] = v;\n            p[1] = huff_reader_get_symbol(&hg[HUFF_IDX_RED],   &s->gb);\n            p[3] = huff_reader_get_symbol(&hg[HUFF_IDX_BLUE],  &s->gb);\n            p[0] = huff_reader_get_symbol(&hg[HUFF_IDX_ALPHA], &s->gb);\n            if (img->color_cache_bits)\n                color_cache_put(img, AV_RB32(p));\n            x++;\n            if (x == width) {\n                x = 0;\n                y++;\n            }\n        } else if (v < NUM_LITERAL_CODES + NUM_LENGTH_CODES) {\n            int prefix_code, length, distance, ref_x, ref_y;\n            prefix_code = v - NUM_LITERAL_CODES;\n            if (prefix_code < 4) {\n                length = prefix_code + 1;\n            } else {\n                int extra_bits = (prefix_code - 2) >> 1;\n                int offset     = 2 + (prefix_code & 1) << extra_bits;\n                length = offset + get_bits(&s->gb, extra_bits) + 1;\n            }\n            prefix_code = huff_reader_get_symbol(&hg[HUFF_IDX_DIST], &s->gb);\n            if (prefix_code < 4) {\n                distance = prefix_code + 1;\n            } else {\n                int extra_bits = prefix_code - 2 >> 1;\n                int offset     = 2 + (prefix_code & 1) << extra_bits;\n                distance = offset + get_bits(&s->gb, extra_bits) + 1;\n            }\n            if (distance <= NUM_SHORT_DISTANCES) {\n                int xi = lz77_distance_offsets[distance - 1][0];\n                int yi = lz77_distance_offsets[distance - 1][1];\n                distance = FFMAX(1, xi + yi * width);\n            } else {\n                distance -= NUM_SHORT_DISTANCES;\n            }\n            ref_x = x;\n            ref_y = y;\n            if (distance <= x) {\n                ref_x -= distance;\n                distance = 0;\n            } else {\n                ref_x = 0;\n                distance -= x;\n            }\n            while (distance >= width) {\n                ref_y--;\n                distance -= width;\n            }\n            if (distance > 0) {\n                ref_x = width - distance;\n                ref_y--;\n            }\n            ref_x = FFMAX(0, ref_x);\n            ref_y = FFMAX(0, ref_y);\n            for (i = 0; i < length; i++) {\n                uint8_t *p_ref = GET_PIXEL(img->frame, ref_x, ref_y);\n                uint8_t *p     = GET_PIXEL(img->frame,     x,     y);\n                AV_COPY32(p, p_ref);\n                if (img->color_cache_bits)\n                    color_cache_put(img, AV_RB32(p));\n                x++;\n                ref_x++;\n                if (x == width) {\n                    x = 0;\n                    y++;\n                }\n                if (ref_x == width) {\n                    ref_x = 0;\n                    ref_y++;\n                }\n                if (y == img->frame->height || ref_y == img->frame->height)\n                    break;\n            }\n        } else {\n            uint8_t *p = GET_PIXEL(img->frame, x, y);\n            int cache_idx = v - (NUM_LITERAL_CODES + NUM_LENGTH_CODES);\n            if (!img->color_cache_bits) {\n                av_log(s->avctx, AV_LOG_ERROR, "color cache not found\\n");\n                return AVERROR_INVALIDDATA;\n            }\n            if (cache_idx >= 1 << img->color_cache_bits) {\n                av_log(s->avctx, AV_LOG_ERROR,\n                       "color cache index out-of-bounds\\n");\n                return AVERROR_INVALIDDATA;\n            }\n            AV_WB32(p, img->color_cache[cache_idx]);\n            x++;\n            if (x == width) {\n                x = 0;\n                y++;\n            }\n        }\n    }\n    return 0;\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}']