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 select_input_picture(MpegEncContext *s)\n{\n    int i, ret;\n    for (i = 1; i < MAX_PICTURE_COUNT; i++)\n        s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];\n    s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;\n    if (!s->reordered_input_picture[0] && s->input_picture[0]) {\n        if (\n            !s->next_picture_ptr || s->intra_only) {\n            s->reordered_input_picture[0] = s->input_picture[0];\n            s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;\n            s->reordered_input_picture[0]->f->coded_picture_number =\n                s->coded_picture_number++;\n        } else {\n            int b_frames = 0;\n            if (s->frame_skip_threshold || s->frame_skip_factor) {\n                if (s->picture_in_gop_number < s->gop_size &&\n                    skip_check(s, s->input_picture[0], s->next_picture_ptr)) {\n                    av_frame_unref(s->input_picture[0]->f);\n                    emms_c();\n                    ff_vbv_update(s, 0);\n                    goto no_output_pic;\n                }\n            }\n            if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {\n                for (i = 0; i < s->max_b_frames + 1; i++) {\n                    int pict_num = s->input_picture[0]->f->display_picture_number + i;\n                    if (pict_num >= s->rc_context.num_entries)\n                        break;\n                    if (!s->input_picture[i]) {\n                        s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;\n                        break;\n                    }\n                    s->input_picture[i]->f->pict_type =\n                        s->rc_context.entry[pict_num].new_pict_type;\n                }\n            }\n            if (s->b_frame_strategy == 0) {\n                b_frames = s->max_b_frames;\n                while (b_frames && !s->input_picture[b_frames])\n                    b_frames--;\n            } else if (s->b_frame_strategy == 1) {\n                for (i = 1; i < s->max_b_frames + 1; i++) {\n                    if (s->input_picture[i] &&\n                        s->input_picture[i]->b_frame_score == 0) {\n                        s->input_picture[i]->b_frame_score =\n                            get_intra_count(s,\n                                            s->input_picture[i    ]->f->data[0],\n                                            s->input_picture[i - 1]->f->data[0],\n                                            s->linesize) + 1;\n                    }\n                }\n                for (i = 0; i < s->max_b_frames + 1; i++) {\n                    if (!s->input_picture[i] ||\n                        s->input_picture[i]->b_frame_score - 1 >\n                            s->mb_num / s->b_sensitivity)\n                        break;\n                }\n                b_frames = FFMAX(0, i - 1);\n                for (i = 0; i < b_frames + 1; i++) {\n                    s->input_picture[i]->b_frame_score = 0;\n                }\n            } else if (s->b_frame_strategy == 2) {\n                b_frames = estimate_best_b_count(s);\n            }\n            emms_c();\n            for (i = b_frames - 1; i >= 0; i--) {\n                int type = s->input_picture[i]->f->pict_type;\n                if (type && type != AV_PICTURE_TYPE_B)\n                    b_frames = i;\n            }\n            if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&\n                b_frames == s->max_b_frames) {\n                av_log(s->avctx, AV_LOG_ERROR,\n                       "warning, too many B-frames in a row\\n");\n            }\n            if (s->picture_in_gop_number + b_frames >= s->gop_size) {\n                if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&\n                    s->gop_size > s->picture_in_gop_number) {\n                    b_frames = s->gop_size - s->picture_in_gop_number - 1;\n                } else {\n                    if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)\n                        b_frames = 0;\n                    s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;\n                }\n            }\n            if ((s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&\n                s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)\n                b_frames--;\n            s->reordered_input_picture[0] = s->input_picture[b_frames];\n            if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)\n                s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;\n            s->reordered_input_picture[0]->f->coded_picture_number =\n                s->coded_picture_number++;\n            for (i = 0; i < b_frames; i++) {\n                s->reordered_input_picture[i + 1] = s->input_picture[i];\n                s->reordered_input_picture[i + 1]->f->pict_type =\n                    AV_PICTURE_TYPE_B;\n                s->reordered_input_picture[i + 1]->f->coded_picture_number =\n                    s->coded_picture_number++;\n            }\n        }\n    }\nno_output_pic:\n    ff_mpeg_unref_picture(s->avctx, &s->new_picture);\n    if (s->reordered_input_picture[0]) {\n        s->reordered_input_picture[0]->reference =\n           s->reordered_input_picture[0]->f->pict_type !=\n               AV_PICTURE_TYPE_B ? 3 : 0;\n        if ((ret = ff_mpeg_ref_picture(s->avctx, &s->new_picture, s->reordered_input_picture[0])))\n            return ret;\n        if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {\n            Picture *pic;\n            int i = ff_find_unused_picture(s->avctx, s->picture, 0);\n            if (i < 0)\n                return i;\n            pic = &s->picture[i];\n            pic->reference = s->reordered_input_picture[0]->reference;\n            if (alloc_picture(s, pic, 0) < 0) {\n                return -1;\n            }\n            ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);\n            if (ret < 0)\n                return ret;\n            av_frame_unref(s->reordered_input_picture[0]->f);\n            s->reordered_input_picture[0]->shared = 0;\n            s->current_picture_ptr = pic;\n        } else {\n            s->current_picture_ptr = s->reordered_input_picture[0];\n            for (i = 0; i < 4; i++) {\n                s->new_picture.f->data[i] += INPLACE_OFFSET;\n            }\n        }\n        ff_mpeg_unref_picture(s->avctx, &s->current_picture);\n        if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,\n                                       s->current_picture_ptr)) < 0)\n            return ret;\n        s->picture_number = s->new_picture.f->display_picture_number;\n    }\n    return 0;\n}', 'static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)\n{\n    return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 1,\n                            s->chroma_x_shift, s->chroma_y_shift, s->out_format,\n                            s->mb_stride, s->mb_height, s->b8_stride,\n                            &s->linesize, &s->uvlinesize);\n}', 'int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,\n                     ScratchpadContext *sc, int shared, int encoding,\n                     int chroma_x_shift, int chroma_y_shift, int out_format,\n                     int mb_stride, int mb_height, int b8_stride,\n                     ptrdiff_t *linesize, ptrdiff_t *uvlinesize)\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(avctx, pic, me, sc,\n                               chroma_x_shift, chroma_y_shift,\n                               *linesize, *uvlinesize) < 0)\n            return -1;\n        *linesize   = pic->f->linesize[0];\n        *uvlinesize = pic->f->linesize[1];\n    }\n    if (!pic->qscale_table_buf)\n        ret = alloc_picture_tables(pic, encoding, out_format,\n                                   mb_stride, mb_height, b8_stride);\n    else\n        ret = make_tables_writable(pic);\n    if (ret < 0)\n        goto fail;\n    if (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 * mb_stride + 1;\n    pic->mb_type      = (uint32_t*)pic->mb_type_buf->data + 2 * 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(avctx, AV_LOG_ERROR, "Error allocating a picture.\\n");\n    ff_mpeg_unref_picture(avctx, pic);\n    ff_free_picture_tables(pic);\n    return AVERROR(ENOMEM);\n}', 'static int alloc_frame_buffer(AVCodecContext *avctx,  Picture *pic,\n                              MotionEstContext *me, ScratchpadContext *sc,\n                              int chroma_x_shift, int chroma_y_shift,\n                              int linesize, int uvlinesize)\n{\n    int edges_needed = av_codec_is_encoder(avctx->codec);\n    int r, ret;\n    pic->tf.f = pic->f;\n    if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&\n        avctx->codec_id != AV_CODEC_ID_VC1IMAGE  &&\n        avctx->codec_id != AV_CODEC_ID_MSS2) {\n        if (edges_needed) {\n            pic->f->width  = avctx->width  + 2 * EDGE_WIDTH;\n            pic->f->height = avctx->height + 2 * EDGE_WIDTH;\n        }\n        r = ff_thread_get_buffer(avctx, &pic->tf,\n                                 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);\n    } else {\n        pic->f->width  = avctx->width;\n        pic->f->height = avctx->height;\n        pic->f->format = avctx->pix_fmt;\n        r = avcodec_default_get_buffer2(avctx, pic->f, 0);\n    }\n    if (r < 0 || !pic->f->buf[0]) {\n        av_log(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 ? chroma_y_shift : 0)) *\n                         pic->f->linesize[i] +\n                         (EDGE_WIDTH >> (i ? chroma_x_shift : 0));\n            pic->f->data[i] += offset;\n        }\n        pic->f->width  = avctx->width;\n        pic->f->height = avctx->height;\n    }\n    if (avctx->hwaccel) {\n        assert(!pic->hwaccel_picture_private);\n        if (avctx->hwaccel->frame_priv_data_size) {\n            pic->hwaccel_priv_buf = av_buffer_allocz(avctx->hwaccel->frame_priv_data_size);\n            if (!pic->hwaccel_priv_buf) {\n                av_log(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 (linesize && (linesize   != pic->f->linesize[0] ||\n                     uvlinesize != pic->f->linesize[1])) {\n        av_log(avctx, AV_LOG_ERROR,\n               "get_buffer() failed (stride changed)\\n");\n        ff_mpeg_unref_picture(avctx, pic);\n        return -1;\n    }\n    if (pic->f->linesize[1] != pic->f->linesize[2]) {\n        av_log(avctx, AV_LOG_ERROR,\n               "get_buffer() failed (uv stride mismatch)\\n");\n        ff_mpeg_unref_picture(avctx, pic);\n        return -1;\n    }\n    if (!sc->edge_emu_buffer &&\n        (ret = ff_mpeg_framesize_alloc(avctx, me, sc,\n                                       pic->f->linesize[0])) < 0) {\n        av_log(avctx, AV_LOG_ERROR,\n               "get_buffer() failed to allocate context scratch buffers.\\n");\n        ff_mpeg_unref_picture(avctx, 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 (avctx->hw_frames_ctx)\n        return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);\n    if ((ret = update_frame_pool(avctx, frame)) < 0)\n        return ret;\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        uint8_t *data[4];\n        int linesize[4];\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        do {\n            av_image_fill_linesizes(linesize, avctx->pix_fmt, w);\n            w += w & ~(w - 1);\n            unaligned = 0;\n            for (i = 0; i < 4; i++)\n                unaligned |= linesize[i] % pool->stride_align[i];\n        } while (unaligned);\n        tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,\n                                         NULL, linesize);\n        if (tmpsize < 0)\n            return -1;\n        for (i = 0; i < 3 && data[i + 1]; i++)\n            size[i] = data[i + 1] - data[i];\n        size[i] = tmpsize - (data[i] - data[0]);\n        for (i = 0; i < 4; i++) {\n            av_buffer_pool_uninit(&pool->pools[i]);\n            pool->linesize[i] = 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}']