return 0;
}
-static int encode_frame(AVCodecContext *c, AVFrame *frame)
+static int encode_frame(AVCodecContext *c, AVFrame *frame, AVPacket *pkt)
{
- AVPacket pkt = { 0 };
int ret;
int size = 0;
- av_init_packet(&pkt);
-
ret = avcodec_send_frame(c, frame);
if (ret < 0)
return ret;
do {
- ret = avcodec_receive_packet(c, &pkt);
+ ret = avcodec_receive_packet(c, pkt);
if (ret >= 0) {
- size += pkt.size;
- av_packet_unref(&pkt);
+ size += pkt->size;
+ av_packet_unref(pkt);
} else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
return ret;
} while (ret >= 0);
static int estimate_best_b_count(MpegEncContext *s)
{
const AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
+ AVPacket *pkt;
const int scale = s->brd_scale;
int width = s->width >> scale;
int height = s->height >> scale;
av_assert0(scale >= 0 && scale <= 3);
+ pkt = av_packet_alloc();
+ if (!pkt)
+ return AVERROR(ENOMEM);
+
//emms_c();
//s->next_picture_ptr->quality;
p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
break;
c = avcodec_alloc_context3(NULL);
- if (!c)
- return AVERROR(ENOMEM);
+ if (!c) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
c->width = width;
c->height = height;
if (ret < 0)
goto fail;
+
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]);
+ out_size = encode_frame(c, s->tmp_frames[0], pkt);
if (out_size < 0) {
ret = out_size;
goto fail;
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]);
+ out_size = encode_frame(c, s->tmp_frames[i + 1], pkt);
if (out_size < 0) {
ret = out_size;
goto fail;
}
/* get the delayed frames */
- out_size = encode_frame(c, NULL);
+ out_size = encode_frame(c, NULL, pkt);
if (out_size < 0) {
ret = out_size;
goto fail;
fail:
avcodec_free_context(&c);
- if (ret < 0)
- return ret;
+ av_packet_unref(pkt);
+ if (ret < 0) {
+ best_b_count = ret;
+ break;
+ }
}
+ av_packet_free(&pkt);
+
return best_b_count;
}