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

Signed-off-by: James Almer <jamrial@gmail.com>
pull/371/head
James Almer 4 years ago
parent 6394c7d51a
commit d492b17024
  1. 42
      doc/examples/vaapi_transcode.c

@ -109,28 +109,25 @@ static int open_input_file(const char *filename)
return ret; return ret;
} }
static int encode_write(AVFrame *frame) static int encode_write(AVPacket *enc_pkt, AVFrame *frame)
{ {
int ret = 0; int ret = 0;
AVPacket enc_pkt;
av_init_packet(&enc_pkt); av_packet_unref(enc_pkt);
enc_pkt.data = NULL;
enc_pkt.size = 0;
if ((ret = avcodec_send_frame(encoder_ctx, frame)) < 0) { if ((ret = avcodec_send_frame(encoder_ctx, frame)) < 0) {
fprintf(stderr, "Error during encoding. Error code: %s\n", av_err2str(ret)); fprintf(stderr, "Error during encoding. Error code: %s\n", av_err2str(ret));
goto end; goto end;
} }
while (1) { while (1) {
ret = avcodec_receive_packet(encoder_ctx, &enc_pkt); ret = avcodec_receive_packet(encoder_ctx, enc_pkt);
if (ret) if (ret)
break; break;
enc_pkt.stream_index = 0; enc_pkt->stream_index = 0;
av_packet_rescale_ts(&enc_pkt, ifmt_ctx->streams[video_stream]->time_base, av_packet_rescale_ts(enc_pkt, ifmt_ctx->streams[video_stream]->time_base,
ofmt_ctx->streams[0]->time_base); ofmt_ctx->streams[0]->time_base);
ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt); ret = av_interleaved_write_frame(ofmt_ctx, enc_pkt);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Error during writing data to output file. " fprintf(stderr, "Error during writing data to output file. "
"Error code: %s\n", av_err2str(ret)); "Error code: %s\n", av_err2str(ret));
@ -216,7 +213,7 @@ static int dec_enc(AVPacket *pkt, AVCodec *enc_codec)
initialized = 1; initialized = 1;
} }
if ((ret = encode_write(frame)) < 0) if ((ret = encode_write(pkt, frame)) < 0)
fprintf(stderr, "Error during encoding and writing.\n"); fprintf(stderr, "Error during encoding and writing.\n");
fail: fail:
@ -230,7 +227,7 @@ fail:
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int ret = 0; int ret = 0;
AVPacket dec_pkt; AVPacket *dec_pkt;
AVCodec *enc_codec; AVCodec *enc_codec;
if (argc != 4) { if (argc != 4) {
@ -246,6 +243,12 @@ int main(int argc, char **argv)
return -1; return -1;
} }
dec_pkt = av_packet_alloc();
if (!dec_pkt) {
fprintf(stderr, "Failed to allocate decode packet\n");
goto end;
}
if ((ret = open_input_file(argv[1])) < 0) if ((ret = open_input_file(argv[1])) < 0)
goto end; goto end;
@ -275,23 +278,21 @@ int main(int argc, char **argv)
/* read all packets and only transcoding video */ /* read all packets and only transcoding video */
while (ret >= 0) { while (ret >= 0) {
if ((ret = av_read_frame(ifmt_ctx, &dec_pkt)) < 0) if ((ret = av_read_frame(ifmt_ctx, dec_pkt)) < 0)
break; break;
if (video_stream == dec_pkt.stream_index) if (video_stream == dec_pkt->stream_index)
ret = dec_enc(&dec_pkt, enc_codec); ret = dec_enc(dec_pkt, enc_codec);
av_packet_unref(&dec_pkt); av_packet_unref(dec_pkt);
} }
/* flush decoder */ /* flush decoder */
dec_pkt.data = NULL; av_packet_unref(dec_pkt);
dec_pkt.size = 0; ret = dec_enc(dec_pkt, enc_codec);
ret = dec_enc(&dec_pkt, enc_codec);
av_packet_unref(&dec_pkt);
/* flush encoder */ /* flush encoder */
ret = encode_write(NULL); ret = encode_write(dec_pkt, NULL);
/* write the trailer for output stream */ /* write the trailer for output stream */
av_write_trailer(ofmt_ctx); av_write_trailer(ofmt_ctx);
@ -302,5 +303,6 @@ end:
avcodec_free_context(&decoder_ctx); avcodec_free_context(&decoder_ctx);
avcodec_free_context(&encoder_ctx); avcodec_free_context(&encoder_ctx);
av_buffer_unref(&hw_device_ctx); av_buffer_unref(&hw_device_ctx);
av_packet_free(&dec_pkt);
return ret; return ret;
} }

Loading…
Cancel
Save