doc/examples/vaapi_encode: use av_packet_alloc() to allocate packets

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2021-01-31 14:42:47 -03:00
parent b896832b78
commit 6394c7d51a
1 changed files with 8 additions and 8 deletions

View File

@ -74,27 +74,27 @@ static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout) static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
{ {
int ret = 0; int ret = 0;
AVPacket enc_pkt; AVPacket *enc_pkt;
av_init_packet(&enc_pkt); if (!(enc_pkt = av_packet_alloc()))
enc_pkt.data = NULL; return AVERROR(ENOMEM);
enc_pkt.size = 0;
if ((ret = avcodec_send_frame(avctx, frame)) < 0) { if ((ret = avcodec_send_frame(avctx, frame)) < 0) {
fprintf(stderr, "Error code: %s\n", av_err2str(ret)); fprintf(stderr, "Error code: %s\n", av_err2str(ret));
goto end; goto end;
} }
while (1) { while (1) {
ret = avcodec_receive_packet(avctx, &enc_pkt); ret = avcodec_receive_packet(avctx, enc_pkt);
if (ret) if (ret)
break; break;
enc_pkt.stream_index = 0; enc_pkt->stream_index = 0;
ret = fwrite(enc_pkt.data, enc_pkt.size, 1, fout); ret = fwrite(enc_pkt->data, enc_pkt->size, 1, fout);
av_packet_unref(&enc_pkt); av_packet_unref(enc_pkt);
} }
end: end:
av_packet_free(&enc_pkt);
ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1); ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
return ret; return ret;
} }