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;
}
['int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)\n{\n    int i, ret;\n    Picture *pic;\n    s->mb_skipped = 0;\n    if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&\n        s->last_picture_ptr != s->next_picture_ptr &&\n        s->last_picture_ptr->f.buf[0]) {\n        ff_mpeg_unref_picture(s, s->last_picture_ptr);\n    }\n    for (i = 0; i < MAX_PICTURE_COUNT; i++) {\n        if (&s->picture[i] != s->last_picture_ptr &&\n            &s->picture[i] != s->next_picture_ptr &&\n            s->picture[i].reference && !s->picture[i].needs_realloc) {\n            if (!(avctx->active_thread_type & FF_THREAD_FRAME))\n                av_log(avctx, AV_LOG_ERROR,\n                       "releasing zombie picture\\n");\n            ff_mpeg_unref_picture(s, &s->picture[i]);\n        }\n    }\n    ff_mpeg_unref_picture(s, &s->current_picture);\n    release_unused_pictures(s);\n    if (s->current_picture_ptr &&\n        s->current_picture_ptr->f.buf[0] == NULL) {\n        pic = s->current_picture_ptr;\n    } else {\n        i   = ff_find_unused_picture(s, 0);\n        if (i < 0) {\n            av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\\n");\n            return i;\n        }\n        pic = &s->picture[i];\n    }\n    pic->reference = 0;\n    if (!s->droppable) {\n        if (s->pict_type != AV_PICTURE_TYPE_B)\n            pic->reference = 3;\n    }\n    pic->f.coded_picture_number = s->coded_picture_number++;\n    if (ff_alloc_picture(s, pic, 0) < 0)\n        return -1;\n    s->current_picture_ptr = pic;\n    s->current_picture_ptr->f.top_field_first = s->top_field_first;\n    if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||\n        s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {\n        if (s->picture_structure != PICT_FRAME)\n            s->current_picture_ptr->f.top_field_first =\n                (s->picture_structure == PICT_TOP_FIELD) == s->first_field;\n    }\n    s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&\n                                                 !s->progressive_sequence;\n    s->current_picture_ptr->field_picture      =  s->picture_structure != PICT_FRAME;\n    s->current_picture_ptr->f.pict_type = s->pict_type;\n    s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;\n    if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,\n                                   s->current_picture_ptr)) < 0)\n        return ret;\n    if (s->pict_type != AV_PICTURE_TYPE_B) {\n        s->last_picture_ptr = s->next_picture_ptr;\n        if (!s->droppable)\n            s->next_picture_ptr = s->current_picture_ptr;\n    }\n    av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\\n",\n            s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,\n            s->last_picture_ptr    ? s->last_picture_ptr->f.data[0]    : NULL,\n            s->next_picture_ptr    ? s->next_picture_ptr->f.data[0]    : NULL,\n            s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,\n            s->pict_type, s->droppable);\n    if ((s->last_picture_ptr == NULL ||\n         s->last_picture_ptr->f.buf[0] == NULL) &&\n        (s->pict_type != AV_PICTURE_TYPE_I ||\n         s->picture_structure != PICT_FRAME)) {\n        int h_chroma_shift, v_chroma_shift;\n        av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,\n                                         &h_chroma_shift, &v_chroma_shift);\n        if (s->pict_type != AV_PICTURE_TYPE_I)\n            av_log(avctx, AV_LOG_ERROR,\n                   "warning: first frame is no keyframe\\n");\n        else if (s->picture_structure != PICT_FRAME)\n            av_log(avctx, AV_LOG_INFO,\n                   "allocate dummy last picture for field based first keyframe\\n");\n        i = ff_find_unused_picture(s, 0);\n        if (i < 0) {\n            av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\\n");\n            return i;\n        }\n        s->last_picture_ptr = &s->picture[i];\n        s->last_picture_ptr->reference   = 3;\n        s->last_picture_ptr->f.pict_type = AV_PICTURE_TYPE_I;\n        if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {\n            s->last_picture_ptr = NULL;\n            return -1;\n        }\n        memset(s->last_picture_ptr->f.data[0], 0,\n               avctx->height * s->last_picture_ptr->f.linesize[0]);\n        memset(s->last_picture_ptr->f.data[1], 0x80,\n               (avctx->height >> v_chroma_shift) *\n               s->last_picture_ptr->f.linesize[1]);\n        memset(s->last_picture_ptr->f.data[2], 0x80,\n               (avctx->height >> v_chroma_shift) *\n               s->last_picture_ptr->f.linesize[2]);\n        ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);\n        ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);\n    }\n    if ((s->next_picture_ptr == NULL ||\n         s->next_picture_ptr->f.buf[0] == NULL) &&\n        s->pict_type == AV_PICTURE_TYPE_B) {\n        i = ff_find_unused_picture(s, 0);\n        if (i < 0) {\n            av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\\n");\n            return i;\n        }\n        s->next_picture_ptr = &s->picture[i];\n        s->next_picture_ptr->reference   = 3;\n        s->next_picture_ptr->f.pict_type = AV_PICTURE_TYPE_I;\n        if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {\n            s->next_picture_ptr = NULL;\n            return -1;\n        }\n        ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);\n        ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);\n    }\n    if (s->last_picture_ptr) {\n        ff_mpeg_unref_picture(s, &s->last_picture);\n        if (s->last_picture_ptr->f.buf[0] &&\n            (ret = ff_mpeg_ref_picture(s, &s->last_picture,\n                                       s->last_picture_ptr)) < 0)\n            return ret;\n    }\n    if (s->next_picture_ptr) {\n        ff_mpeg_unref_picture(s, &s->next_picture);\n        if (s->next_picture_ptr->f.buf[0] &&\n            (ret = ff_mpeg_ref_picture(s, &s->next_picture,\n                                       s->next_picture_ptr)) < 0)\n            return ret;\n    }\n    if (s->pict_type != AV_PICTURE_TYPE_I &&\n        !(s->last_picture_ptr && s->last_picture_ptr->f.buf[0])) {\n        av_log(s, AV_LOG_ERROR,\n               "Non-reference picture received and no reference available\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (s->picture_structure!= PICT_FRAME) {\n        int i;\n        for (i = 0; i < 4; i++) {\n            if (s->picture_structure == PICT_BOTTOM_FIELD) {\n                s->current_picture.f.data[i] +=\n                    s->current_picture.f.linesize[i];\n            }\n            s->current_picture.f.linesize[i] *= 2;\n            s->last_picture.f.linesize[i]    *= 2;\n            s->next_picture.f.linesize[i]    *= 2;\n        }\n    }\n    s->err_recognition = avctx->err_recognition;\n    if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {\n        s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;\n        s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;\n    } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {\n        s->dct_unquantize_intra = s->dct_unquantize_h263_intra;\n        s->dct_unquantize_inter = s->dct_unquantize_h263_inter;\n    } else {\n        s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;\n        s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;\n    }\n#if FF_API_XVMC\nFF_DISABLE_DEPRECATION_WARNINGS\n    if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)\n        return ff_xvmc_field_start(s, avctx);\nFF_ENABLE_DEPRECATION_WARNINGS\n#endif\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}']