avcodec/mpegvideo_enc: use av_packet_alloc() to allocate packets

Signed-off-by: James Almer <jamrial@gmail.com>
pull/371/head
James Almer 4 years ago
parent 2101b99777
commit ca966af497
  1. 38
      libavcodec/mpegvideo_enc.c

@ -1365,23 +1365,20 @@ static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
return 0; 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 ret;
int size = 0; int size = 0;
av_init_packet(&pkt);
ret = avcodec_send_frame(c, frame); ret = avcodec_send_frame(c, frame);
if (ret < 0) if (ret < 0)
return ret; return ret;
do { do {
ret = avcodec_receive_packet(c, &pkt); ret = avcodec_receive_packet(c, pkt);
if (ret >= 0) { if (ret >= 0) {
size += pkt.size; size += pkt->size;
av_packet_unref(&pkt); av_packet_unref(pkt);
} else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
return ret; return ret;
} while (ret >= 0); } while (ret >= 0);
@ -1392,6 +1389,7 @@ static int encode_frame(AVCodecContext *c, AVFrame *frame)
static int estimate_best_b_count(MpegEncContext *s) static int estimate_best_b_count(MpegEncContext *s)
{ {
const AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id); const AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
AVPacket *pkt;
const int scale = s->brd_scale; const int scale = s->brd_scale;
int width = s->width >> scale; int width = s->width >> scale;
int height = s->height >> scale; int height = s->height >> scale;
@ -1402,6 +1400,10 @@ static int estimate_best_b_count(MpegEncContext *s)
av_assert0(scale >= 0 && scale <= 3); av_assert0(scale >= 0 && scale <= 3);
pkt = av_packet_alloc();
if (!pkt)
return AVERROR(ENOMEM);
//emms_c(); //emms_c();
//s->next_picture_ptr->quality; //s->next_picture_ptr->quality;
p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P]; p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
@ -1453,8 +1455,10 @@ static int estimate_best_b_count(MpegEncContext *s)
break; break;
c = avcodec_alloc_context3(NULL); c = avcodec_alloc_context3(NULL);
if (!c) if (!c) {
return AVERROR(ENOMEM); ret = AVERROR(ENOMEM);
goto fail;
}
c->width = width; c->width = width;
c->height = height; c->height = height;
@ -1472,10 +1476,11 @@ static int estimate_best_b_count(MpegEncContext *s)
if (ret < 0) if (ret < 0)
goto fail; goto fail;
s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I; s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA; 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) { if (out_size < 0) {
ret = out_size; ret = out_size;
goto fail; goto fail;
@ -1490,7 +1495,7 @@ static int estimate_best_b_count(MpegEncContext *s)
AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B; AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda; 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) { if (out_size < 0) {
ret = out_size; ret = out_size;
goto fail; goto fail;
@ -1500,7 +1505,7 @@ static int estimate_best_b_count(MpegEncContext *s)
} }
/* get the delayed frames */ /* get the delayed frames */
out_size = encode_frame(c, NULL); out_size = encode_frame(c, NULL, pkt);
if (out_size < 0) { if (out_size < 0) {
ret = out_size; ret = out_size;
goto fail; goto fail;
@ -1516,10 +1521,15 @@ static int estimate_best_b_count(MpegEncContext *s)
fail: fail:
avcodec_free_context(&c); avcodec_free_context(&c);
if (ret < 0) av_packet_unref(pkt);
return ret; if (ret < 0) {
best_b_count = ret;
break;
}
} }
av_packet_free(&pkt);
return best_b_count; return best_b_count;
} }

Loading…
Cancel
Save