static int estimate_best_b_count(MpegEncContext *s)
{
    AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);
    AVCodecContext *c = avcodec_alloc_context3(NULL);
    const int scale = s->avctx->brd_scale;
    int i, j, out_size, p_lambda, b_lambda, lambda2;
    int64_t best_rd  = INT64_MAX;
    int best_b_count = -1;
    assert(scale >= 0 && scale <= 3);
    p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
    b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
    if (!b_lambda)
        b_lambda = p_lambda;
    lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
               FF_LAMBDA_SHIFT;
    c->width        = s->width  >> scale;
    c->height       = s->height >> scale;
    c->flags        = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR;
    c->flags       |= s->avctx->flags & CODEC_FLAG_QPEL;
    c->mb_decision  = s->avctx->mb_decision;
    c->me_cmp       = s->avctx->me_cmp;
    c->mb_cmp       = s->avctx->mb_cmp;
    c->me_sub_cmp   = s->avctx->me_sub_cmp;
    c->pix_fmt      = AV_PIX_FMT_YUV420P;
    c->time_base    = s->avctx->time_base;
    c->max_b_frames = s->max_b_frames;
    if (avcodec_open2(c, codec, NULL) < 0)
        return -1;
    for (i = 0; i < s->max_b_frames + 2; i++) {
        Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
                                                s->next_picture_ptr;
        if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
            pre_input = *pre_input_ptr;
            if (!pre_input.shared && i) {
                pre_input.f->data[0] += INPLACE_OFFSET;
                pre_input.f->data[1] += INPLACE_OFFSET;
                pre_input.f->data[2] += INPLACE_OFFSET;
            }
            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
                                       s->tmp_frames[i]->linesize[0],
                                       pre_input.f->data[0],
                                       pre_input.f->linesize[0],
                                       c->width, c->height);
            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
                                       s->tmp_frames[i]->linesize[1],
                                       pre_input.f->data[1],
                                       pre_input.f->linesize[1],
                                       c->width >> 1, c->height >> 1);
            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
                                       s->tmp_frames[i]->linesize[2],
                                       pre_input.f->data[2],
                                       pre_input.f->linesize[2],
                                       c->width >> 1, c->height >> 1);
        }
    }
    for (j = 0; j < s->max_b_frames + 1; j++) {
        int64_t rd = 0;
        if (!s->input_picture[j])
            break;
        c->error[0] = c->error[1] = c->error[2] = 0;
        s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
        s->tmp_frames[0]->quality   = 1 * FF_QP2LAMBDA;
        out_size = encode_frame(c, s->tmp_frames[0]);
        for (i = 0; i < s->max_b_frames + 1; i++) {
            int is_p = i % (j + 1) == j || i == s->max_b_frames;
            s->tmp_frames[i + 1]->pict_type = is_p ?
                                     AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
            s->tmp_frames[i + 1]->quality   = is_p ? p_lambda : b_lambda;
            out_size = encode_frame(c, s->tmp_frames[i + 1]);
            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
        }
        while (out_size) {
            out_size = encode_frame(c, NULL);
            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
        }
        rd += c->error[0] + c->error[1] + c->error[2];
        if (rd < best_rd) {
            best_rd = rd;
            best_b_count = j;
        }
    }
    avcodec_close(c);
    av_freep(&c);
    return best_b_count;
}
['static int estimate_best_b_count(MpegEncContext *s)\n{\n    AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);\n    AVCodecContext *c = avcodec_alloc_context3(NULL);\n    const int scale = s->avctx->brd_scale;\n    int i, j, out_size, p_lambda, b_lambda, lambda2;\n    int64_t best_rd  = INT64_MAX;\n    int best_b_count = -1;\n    assert(scale >= 0 && scale <= 3);\n    p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];\n    b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];\n    if (!b_lambda)\n        b_lambda = p_lambda;\n    lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>\n               FF_LAMBDA_SHIFT;\n    c->width        = s->width  >> scale;\n    c->height       = s->height >> scale;\n    c->flags        = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR;\n    c->flags       |= s->avctx->flags & CODEC_FLAG_QPEL;\n    c->mb_decision  = s->avctx->mb_decision;\n    c->me_cmp       = s->avctx->me_cmp;\n    c->mb_cmp       = s->avctx->mb_cmp;\n    c->me_sub_cmp   = s->avctx->me_sub_cmp;\n    c->pix_fmt      = AV_PIX_FMT_YUV420P;\n    c->time_base    = s->avctx->time_base;\n    c->max_b_frames = s->max_b_frames;\n    if (avcodec_open2(c, codec, NULL) < 0)\n        return -1;\n    for (i = 0; i < s->max_b_frames + 2; i++) {\n        Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :\n                                                s->next_picture_ptr;\n        if (pre_input_ptr && (!i || s->input_picture[i - 1])) {\n            pre_input = *pre_input_ptr;\n            if (!pre_input.shared && i) {\n                pre_input.f->data[0] += INPLACE_OFFSET;\n                pre_input.f->data[1] += INPLACE_OFFSET;\n                pre_input.f->data[2] += INPLACE_OFFSET;\n            }\n            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],\n                                       s->tmp_frames[i]->linesize[0],\n                                       pre_input.f->data[0],\n                                       pre_input.f->linesize[0],\n                                       c->width, c->height);\n            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],\n                                       s->tmp_frames[i]->linesize[1],\n                                       pre_input.f->data[1],\n                                       pre_input.f->linesize[1],\n                                       c->width >> 1, c->height >> 1);\n            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],\n                                       s->tmp_frames[i]->linesize[2],\n                                       pre_input.f->data[2],\n                                       pre_input.f->linesize[2],\n                                       c->width >> 1, c->height >> 1);\n        }\n    }\n    for (j = 0; j < s->max_b_frames + 1; j++) {\n        int64_t rd = 0;\n        if (!s->input_picture[j])\n            break;\n        c->error[0] = c->error[1] = c->error[2] = 0;\n        s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;\n        s->tmp_frames[0]->quality   = 1 * FF_QP2LAMBDA;\n        out_size = encode_frame(c, s->tmp_frames[0]);\n        for (i = 0; i < s->max_b_frames + 1; i++) {\n            int is_p = i % (j + 1) == j || i == s->max_b_frames;\n            s->tmp_frames[i + 1]->pict_type = is_p ?\n                                     AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;\n            s->tmp_frames[i + 1]->quality   = is_p ? p_lambda : b_lambda;\n            out_size = encode_frame(c, s->tmp_frames[i + 1]);\n            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n        }\n        while (out_size) {\n            out_size = encode_frame(c, NULL);\n            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n        }\n        rd += c->error[0] + c->error[1] + c->error[2];\n        if (rd < best_rd) {\n            best_rd = rd;\n            best_b_count = j;\n        }\n    }\n    avcodec_close(c);\n    av_freep(&c);\n    return best_b_count;\n}', 'AVCodec *avcodec_find_encoder(enum AVCodecID id)\n{\n    return find_encdec(id, 1);\n}', 'AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)\n{\n    AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));\n    if (!avctx)\n        return NULL;\n    if(avcodec_get_context_defaults3(avctx, codec) < 0){\n        av_free(avctx);\n        return NULL;\n    }\n    return avctx;\n}', 'void *av_malloc(size_t size)\n{\n    void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n    long diff;\n#endif\n    if (size > (INT_MAX - 32) || !size)\n        return NULL;\n#if CONFIG_MEMALIGN_HACK\n    ptr = malloc(size + 32);\n    if (!ptr)\n        return ptr;\n    diff              = ((-(long)ptr - 1) & 31) + 1;\n    ptr               = (char *)ptr + diff;\n    ((char *)ptr)[-1] = diff;\n#elif HAVE_POSIX_MEMALIGN\n    if (posix_memalign(&ptr, 32, size))\n        ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n    ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n    ptr = memalign(32, size);\n#else\n    ptr = malloc(size);\n#endif\n    return ptr;\n}']