avcodec/mpegvideo_enc: use av_packet_alloc() to allocate packets

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2021-01-28 23:02:50 -03:00
parent 2101b99777
commit ca966af497
1 changed files with 24 additions and 14 deletions

View File

@ -1365,23 +1365,20 @@ static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
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);
@ -1392,6 +1389,7 @@ static int encode_frame(AVCodecContext *c, AVFrame *frame)
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;
@ -1402,6 +1400,10 @@ static int estimate_best_b_count(MpegEncContext *s)
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];
@ -1453,8 +1455,10 @@ static int estimate_best_b_count(MpegEncContext *s)
break;
c = avcodec_alloc_context3(NULL);
if (!c)
return AVERROR(ENOMEM);
if (!c) {
ret = AVERROR(ENOMEM);
goto fail;
}
c->width = width;
c->height = height;
@ -1472,10 +1476,11 @@ static int estimate_best_b_count(MpegEncContext *s)
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;
@ -1490,7 +1495,7 @@ static int estimate_best_b_count(MpegEncContext *s)
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;
@ -1500,7 +1505,7 @@ static int estimate_best_b_count(MpegEncContext *s)
}
/* 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;
@ -1516,10 +1521,15 @@ static int estimate_best_b_count(MpegEncContext *s)
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;
}