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 load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)\n{\n    Picture *pic = NULL;\n    int64_t pts;\n    int i, display_picture_number = 0, ret;\n    const int encoding_delay = s->max_b_frames ? s->max_b_frames :\n                                                 (s->low_delay ? 0 : 1);\n    int direct = 1;\n    if (pic_arg) {\n        pts = pic_arg->pts;\n        display_picture_number = s->input_picture_number++;\n        if (pts != AV_NOPTS_VALUE) {\n            if (s->user_specified_pts != AV_NOPTS_VALUE) {\n                int64_t time = pts;\n                int64_t last = s->user_specified_pts;\n                if (time <= last) {\n                    av_log(s->avctx, AV_LOG_ERROR,\n                           "Error, Invalid timestamp=%"PRId64", "\n                           "last=%"PRId64"\\n", pts, s->user_specified_pts);\n                    return -1;\n                }\n                if (!s->low_delay && display_picture_number == 1)\n                    s->dts_delta = time - last;\n            }\n            s->user_specified_pts = pts;\n        } else {\n            if (s->user_specified_pts != AV_NOPTS_VALUE) {\n                s->user_specified_pts =\n                pts = s->user_specified_pts + 1;\n                av_log(s->avctx, AV_LOG_INFO,\n                       "Warning: AVFrame.pts=? trying to guess (%"PRId64")\\n",\n                       pts);\n            } else {\n                pts = display_picture_number;\n            }\n        }\n    }\n    if (pic_arg) {\n        if (!pic_arg->buf[0]);\n            direct = 0;\n        if (pic_arg->linesize[0] != s->linesize)\n            direct = 0;\n        if (pic_arg->linesize[1] != s->uvlinesize)\n            direct = 0;\n        if (pic_arg->linesize[2] != s->uvlinesize)\n            direct = 0;\n        av_dlog(s->avctx, "%d %d %td %td\\n", pic_arg->linesize[0],\n                pic_arg->linesize[1], s->linesize, s->uvlinesize);\n        if (direct) {\n            i = ff_find_unused_picture(s, 1);\n            if (i < 0)\n                return i;\n            pic = &s->picture[i];\n            pic->reference = 3;\n            if ((ret = av_frame_ref(&pic->f, pic_arg)) < 0)\n                return ret;\n            if (ff_alloc_picture(s, pic, 1) < 0) {\n                return -1;\n            }\n        } else {\n            i = ff_find_unused_picture(s, 0);\n            if (i < 0)\n                return i;\n            pic = &s->picture[i];\n            pic->reference = 3;\n            if (ff_alloc_picture(s, pic, 0) < 0) {\n                return -1;\n            }\n            if (pic->f.data[0] + INPLACE_OFFSET == pic_arg->data[0] &&\n                pic->f.data[1] + INPLACE_OFFSET == pic_arg->data[1] &&\n                pic->f.data[2] + INPLACE_OFFSET == pic_arg->data[2]) {\n            } else {\n                int h_chroma_shift, v_chroma_shift;\n                av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,\n                                                 &h_chroma_shift,\n                                                 &v_chroma_shift);\n                for (i = 0; i < 3; i++) {\n                    int src_stride = pic_arg->linesize[i];\n                    int dst_stride = i ? s->uvlinesize : s->linesize;\n                    int h_shift = i ? h_chroma_shift : 0;\n                    int v_shift = i ? v_chroma_shift : 0;\n                    int w = s->width  >> h_shift;\n                    int h = s->height >> v_shift;\n                    uint8_t *src = pic_arg->data[i];\n                    uint8_t *dst = pic->f.data[i];\n                    if (!s->avctx->rc_buffer_size)\n                        dst += INPLACE_OFFSET;\n                    if (src_stride == dst_stride)\n                        memcpy(dst, src, src_stride * h);\n                    else {\n                        while (h--) {\n                            memcpy(dst, src, w);\n                            dst += dst_stride;\n                            src += src_stride;\n                        }\n                    }\n                }\n            }\n        }\n        ret = av_frame_copy_props(&pic->f, pic_arg);\n        if (ret < 0)\n            return ret;\n        pic->f.display_picture_number = display_picture_number;\n        pic->f.pts = pts;\n    }\n    for (i = 1; i < MAX_PICTURE_COUNT  ; i++)\n        s->input_picture[i - 1] = s->input_picture[i];\n    s->input_picture[encoding_delay] = (Picture*) pic;\n    return 0;\n}', 'int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)\n{\n    int i, ret;\n    if (shared) {\n        assert(pic->f.data[0]);\n        pic->shared = 1;\n    } else {\n        assert(!pic->f.buf[0]);\n        if (alloc_frame_buffer(s, pic) < 0)\n            return -1;\n        s->linesize   = pic->f.linesize[0];\n        s->uvlinesize = pic->f.linesize[1];\n    }\n    if (!pic->qscale_table_buf)\n        ret = alloc_picture_tables(s, pic);\n    else\n        ret = make_tables_writable(pic);\n    if (ret < 0)\n        goto fail;\n    if (s->encoding) {\n        pic->mb_var    = (uint16_t*)pic->mb_var_buf->data;\n        pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;\n        pic->mb_mean   = pic->mb_mean_buf->data;\n    }\n    pic->mbskip_table = pic->mbskip_table_buf->data;\n    pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;\n    pic->mb_type      = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;\n    if (pic->motion_val_buf[0]) {\n        for (i = 0; i < 2; i++) {\n            pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;\n            pic->ref_index[i]  = pic->ref_index_buf[i]->data;\n        }\n    }\n    return 0;\nfail:\n    av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\\n");\n    ff_mpeg_unref_picture(s, pic);\n    ff_free_picture_tables(pic);\n    return AVERROR(ENOMEM);\n}', 'static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)\n{\n    int edges_needed = av_codec_is_encoder(s->avctx->codec);\n    int r, ret;\n    pic->tf.f = &pic->f;\n    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&\n        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&\n        s->codec_id != AV_CODEC_ID_MSS2) {\n        if (edges_needed) {\n            pic->f.width  = s->avctx->width  + 2 * EDGE_WIDTH;\n            pic->f.height = s->avctx->height + 2 * EDGE_WIDTH;\n        }\n        r = ff_thread_get_buffer(s->avctx, &pic->tf,\n                                 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);\n    } else {\n        pic->f.width  = s->avctx->width;\n        pic->f.height = s->avctx->height;\n        pic->f.format = s->avctx->pix_fmt;\n        r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);\n    }\n    if (r < 0 || !pic->f.buf[0]) {\n        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\\n",\n               r, pic->f.data[0]);\n        return -1;\n    }\n    if (edges_needed) {\n        int i;\n        for (i = 0; pic->f.data[i]; i++) {\n            int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *\n                         pic->f.linesize[i] +\n                         (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));\n            pic->f.data[i] += offset;\n        }\n        pic->f.width  = s->avctx->width;\n        pic->f.height = s->avctx->height;\n    }\n    if (s->avctx->hwaccel) {\n        assert(!pic->hwaccel_picture_private);\n        if (s->avctx->hwaccel->priv_data_size) {\n            pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);\n            if (!pic->hwaccel_priv_buf) {\n                av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\\n");\n                return -1;\n            }\n            pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;\n        }\n    }\n    if (s->linesize && (s->linesize   != pic->f.linesize[0] ||\n                        s->uvlinesize != pic->f.linesize[1])) {\n        av_log(s->avctx, AV_LOG_ERROR,\n               "get_buffer() failed (stride changed)\\n");\n        ff_mpeg_unref_picture(s, pic);\n        return -1;\n    }\n    if (pic->f.linesize[1] != pic->f.linesize[2]) {\n        av_log(s->avctx, AV_LOG_ERROR,\n               "get_buffer() failed (uv stride mismatch)\\n");\n        ff_mpeg_unref_picture(s, pic);\n        return -1;\n    }\n    if (!s->edge_emu_buffer &&\n        (ret = frame_size_alloc(s, pic->f.linesize[0])) < 0) {\n        av_log(s->avctx, AV_LOG_ERROR,\n               "get_buffer() failed to allocate context scratch buffers.\\n");\n        ff_mpeg_unref_picture(s, pic);\n        return ret;\n    }\n    return 0;\n}', 'int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)\n{\n    int ret;\n    if ((ret = update_frame_pool(avctx, frame)) < 0)\n        return ret;\n#if FF_API_GET_BUFFER\nFF_DISABLE_DEPRECATION_WARNINGS\n    frame->type = FF_BUFFER_TYPE_INTERNAL;\nFF_ENABLE_DEPRECATION_WARNINGS\n#endif\n    switch (avctx->codec_type) {\n    case AVMEDIA_TYPE_VIDEO:\n        return video_get_buffer(avctx, frame);\n    case AVMEDIA_TYPE_AUDIO:\n        return audio_get_buffer(avctx, frame);\n    default:\n        return -1;\n    }\n}', 'static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)\n{\n    FramePool *pool = avctx->internal->pool;\n    int i, ret;\n    switch (avctx->codec_type) {\n    case AVMEDIA_TYPE_VIDEO: {\n        AVPicture picture;\n        int size[4] = { 0 };\n        int w = frame->width;\n        int h = frame->height;\n        int tmpsize, unaligned;\n        if (pool->format == frame->format &&\n            pool->width == frame->width && pool->height == frame->height)\n            return 0;\n        avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);\n        if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {\n            w += EDGE_WIDTH * 2;\n            h += EDGE_WIDTH * 2;\n        }\n        do {\n            av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);\n            w += w & ~(w - 1);\n            unaligned = 0;\n            for (i = 0; i < 4; i++)\n                unaligned |= picture.linesize[i] % pool->stride_align[i];\n        } while (unaligned);\n        tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,\n                                         NULL, picture.linesize);\n        if (tmpsize < 0)\n            return -1;\n        for (i = 0; i < 3 && picture.data[i + 1]; i++)\n            size[i] = picture.data[i + 1] - picture.data[i];\n        size[i] = tmpsize - (picture.data[i] - picture.data[0]);\n        for (i = 0; i < 4; i++) {\n            av_buffer_pool_uninit(&pool->pools[i]);\n            pool->linesize[i] = picture.linesize[i];\n            if (size[i]) {\n                pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);\n                if (!pool->pools[i]) {\n                    ret = AVERROR(ENOMEM);\n                    goto fail;\n                }\n            }\n        }\n        pool->format = frame->format;\n        pool->width  = frame->width;\n        pool->height = frame->height;\n        break;\n        }\n    case AVMEDIA_TYPE_AUDIO: {\n        int ch     = av_get_channel_layout_nb_channels(frame->channel_layout);\n        int planar = av_sample_fmt_is_planar(frame->format);\n        int planes = planar ? ch : 1;\n        if (pool->format == frame->format && pool->planes == planes &&\n            pool->channels == ch && frame->nb_samples == pool->samples)\n            return 0;\n        av_buffer_pool_uninit(&pool->pools[0]);\n        ret = av_samples_get_buffer_size(&pool->linesize[0], ch,\n                                         frame->nb_samples, frame->format, 0);\n        if (ret < 0)\n            goto fail;\n        pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);\n        if (!pool->pools[0]) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n        pool->format     = frame->format;\n        pool->planes     = planes;\n        pool->channels   = ch;\n        pool->samples = frame->nb_samples;\n        break;\n        }\n    default: av_assert0(0);\n    }\n    return 0;\nfail:\n    for (i = 0; i < 4; i++)\n        av_buffer_pool_uninit(&pool->pools[i]);\n    pool->format = -1;\n    pool->planes = pool->channels = pool->samples = 0;\n    pool->width  = pool->height = 0;\n    return ret;\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}']