From 2dce8373cb7adfd3228759bfa5ff80ebcd9116db Mon Sep 17 00:00:00 2001
From: Lennart Blanco <cokebuttle@gmail.com>
Date: Wed, 30 Jan 2019 12:26:54 +0100
Subject: [PATCH] doc/examples: port muxing example to new send/receive API
Replaces calls to the depricated avcodec_encode_audio2() and
avcodec_encode_video2() function with calls to
avcodec_send_frame()/avcodec_receive_packet() pair.
Fixes the depricated warning/error while compiling muxing example.
---
doc/examples/muxing.c | 76 +++++++++++++++++++++++++++++++++++----------------
1 file changed, 52 insertions(+), 24 deletions(-)
@@ -303,7 +303,10 @@ static AVFrame *get_audio_frame(OutputStream *ost)
}
/*
- * encode one audio frame and send it to the muxer
+ * send one audio frame to the encoder,
+ * get one encoded packet, if available,
+ * and send it to the muxer
+ *
* return 1 when encoding is finished, 0 otherwise
*/
static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
@@ -349,22 +352,34 @@ static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
ost->samples_count += dst_nb_samples;
}
- ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
- if (ret < 0) {
- fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
+ ret = avcodec_send_frame(c, frame);
+ if (ret < 0 && ret != AVERROR_EOF) {
+ fprintf(stderr, "Error sending a frame for encoding %s\n", av_err2str(ret));
exit(1);
}
- if (got_packet) {
- ret = write_frame(oc, &c->time_base, ost->st, &pkt);
- if (ret < 0) {
- fprintf(stderr, "Error while writing audio frame: %s\n",
- av_err2str(ret));
- exit(1);
- }
+ /* try to get encoded packet */
+ ret = avcodec_receive_packet(c, &pkt);
+ if (ret == AVERROR(EAGAIN))
+ return 0;
+ else if (ret == AVERROR_EOF)
+ /* done encoding */
+ return 1;
+ else if (ret < 0) {
+ fprintf(stderr, "Error during encoding\n");
+ exit(1);
+ }
+
+ /* write packet package to the file */
+ ret = write_frame(oc, &c->time_base, ost->st, &pkt);
+ if (ret < 0) {
+ fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
+ exit(1);
}
- return (frame || got_packet) ? 0 : 1;
+ av_packet_unref(&pkt);
+
+ return 0;
}
/**************************************************************/
@@ -501,7 +516,10 @@ static AVFrame *get_video_frame(OutputStream *ost)
}
/*
- * encode one video frame and send it to the muxer
+ * send one video frame to the encoder,
+ * get one encoded packet, if available,
+ * and send it to the muxer
+ *
* return 1 when encoding is finished, 0 otherwise
*/
static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
@@ -514,29 +532,39 @@ static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
c = ost->enc;
+ /* send new frame to the encoder */
frame = get_video_frame(ost);
+ ret = avcodec_send_frame(c, frame);
- av_init_packet(&pkt);
-
- /* encode the image */
- ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
- if (ret < 0) {
- fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
+ if (ret < 0 && ret != AVERROR_EOF) {
+ fprintf(stderr, "Error sending a frame for encoding %s\n", av_err2str(ret));
exit(1);
}
- if (got_packet) {
- ret = write_frame(oc, &c->time_base, ost->st, &pkt);
- } else {
- ret = 0;
+ av_init_packet(&pkt);
+
+ /* try to get encoded packet */
+ ret = avcodec_receive_packet(c, &pkt);
+ if (ret == AVERROR(EAGAIN))
+ return 0;
+ else if (ret == AVERROR_EOF)
+ /* done encoding */
+ return 1;
+ else if (ret < 0) {
+ fprintf(stderr, "Error during encoding\n");
+ exit(1);
}
+ /* write packet package to the file */
+ ret = write_frame(oc, &c->time_base, ost->st, &pkt);
if (ret < 0) {
fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
exit(1);
}
- return (frame || got_packet) ? 0 : 1;
+ av_packet_unref(&pkt);
+
+ return 0;
}
static void close_stream(AVFormatContext *oc, OutputStream *ost)
--
2.7.4