diff mbox series

[FFmpeg-devel,3/4] doc/examples/transcoding.c: Fixed a compile warning

Message ID 46108efb9fa7a1ee4161e185f42f3baae536ad11.1588413324.git.gang.zhao.42@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/4] doc/examples/demuxing_decoding: Fixed a compile warning
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Zhao, Gang May 2, 2020, 10:10 a.m. UTC
Fixed deprecated declarations compile warning by replacing deprecated
api avcodec_encode_{audio,video}2() and
avcodec_decode_{audio4,video2}() with
avcodec_send_frame()/avcodec_receive_packet() and
avcodec_send_packet()/avcodec_receive_frame(). Also made some clean up
according to this change.

Signed-off-by: Zhao, Gang <gang.zhao.42@gmail.com>
---
Tested that the new code generated the same file as before by running
./doc/examples/transcoding_g my_samples/transcoding.mp4 my_samples/transcoding.avi

 doc/examples/transcoding.c | 106 +++++++++++++++++++------------------
 1 file changed, 55 insertions(+), 51 deletions(-)

Comments

Nicolas George May 2, 2020, 10:18 a.m. UTC | #1
Zhao, Gang (12020-05-02):
> Fixed deprecated declarations compile warning by replacing deprecated
> api avcodec_encode_{audio,video}2() and
> avcodec_decode_{audio4,video2}() with
> avcodec_send_frame()/avcodec_receive_packet() and
> avcodec_send_packet()/avcodec_receive_frame(). Also made some clean up
> according to this change.
> 
> Signed-off-by: Zhao, Gang <gang.zhao.42@gmail.com>
> ---
> Tested that the new code generated the same file as before by running
> ./doc/examples/transcoding_g my_samples/transcoding.mp4 my_samples/transcoding.avi
> 
>  doc/examples/transcoding.c | 106 +++++++++++++++++++------------------
>  1 file changed, 55 insertions(+), 51 deletions(-)

Again, >100 lines changed, that's not fixing a compilation warning.

Regards,
diff mbox series

Patch

diff --git doc/examples/transcoding.c doc/examples/transcoding.c
index e48837cbd2..8073e5a2e1 100644
--- doc/examples/transcoding.c
+++ doc/examples/transcoding.c
@@ -402,39 +402,50 @@  static int init_filters(void)
     return 0;
 }
 
-static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
+static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index) {
     int ret;
-    int got_frame_local;
     AVPacket enc_pkt;
-    int (*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
-        (ifmt_ctx->streams[stream_index]->codecpar->codec_type ==
-         AVMEDIA_TYPE_VIDEO) ? avcodec_encode_video2 : avcodec_encode_audio2;
-
-    if (!got_frame)
-        got_frame = &got_frame_local;
 
     av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
     /* encode filtered frame */
     enc_pkt.data = NULL;
     enc_pkt.size = 0;
     av_init_packet(&enc_pkt);
-    ret = enc_func(stream_ctx[stream_index].enc_ctx, &enc_pkt,
-            filt_frame, got_frame);
-    av_frame_free(&filt_frame);
-    if (ret < 0)
-        return ret;
-    if (!(*got_frame))
-        return 0;
 
-    /* prepare packet for muxing */
-    enc_pkt.stream_index = stream_index;
-    av_packet_rescale_ts(&enc_pkt,
-                         stream_ctx[stream_index].enc_ctx->time_base,
-                         ofmt_ctx->streams[stream_index]->time_base);
+    /* send the frame for encoding */
+    ret = avcodec_send_frame(stream_ctx[stream_index].enc_ctx, filt_frame);
+    if (ret < 0) {
+        fprintf(stderr, "(%s:%d) Error sending the frame to the encoder: %s\n",
+                __FUNCTION__, __LINE__, av_err2str(ret));
+        exit(1);
+    }
+
+    /* read all the available output packets (in general there may be any
+     * number of them */
+    while (ret >= 0) {
+        ret = avcodec_receive_packet(stream_ctx[stream_index].enc_ctx, &enc_pkt);
+        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
+            /* return OK. Should call avcodec_send_frame again to get more packets */
+            ret = 0;
+            break;
+        }
+        else if (ret < 0) {
+            fprintf(stderr, "Error encoding audio frame\n");
+            exit(1);
+        }
+
+        /* prepare packet for muxing */
+        enc_pkt.stream_index = stream_index;
+        av_packet_rescale_ts(&enc_pkt,
+                             stream_ctx[stream_index].enc_ctx->time_base,
+                             ofmt_ctx->streams[stream_index]->time_base);
+
+        av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
+        /* mux encoded frame */
+        ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
+    }
 
-    av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
-    /* mux encoded frame */
-    ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
+    av_frame_free(&filt_frame);
     return ret;
 }
 
@@ -474,7 +485,7 @@  static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
         }
 
         filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
-        ret = encode_write_frame(filt_frame, stream_index, NULL);
+        ret = encode_write_frame(filt_frame, stream_index);
         if (ret < 0)
             break;
     }
@@ -484,22 +495,12 @@  static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
 
 static int flush_encoder(unsigned int stream_index)
 {
-    int ret;
-    int got_frame;
-
     if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities &
                 AV_CODEC_CAP_DELAY))
         return 0;
 
-    while (1) {
-        av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
-        ret = encode_write_frame(NULL, stream_index, &got_frame);
-        if (ret < 0)
-            break;
-        if (!got_frame)
-            return 0;
-    }
-    return ret;
+    av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
+    return encode_write_frame(NULL, stream_index);
 }
 
 int main(int argc, char **argv)
@@ -507,17 +508,15 @@  int main(int argc, char **argv)
     int ret;
     AVPacket packet = { .data = NULL, .size = 0 };
     AVFrame *frame = NULL;
-    enum AVMediaType type;
     unsigned int stream_index;
     unsigned int i;
-    int got_frame;
-    int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
 
     if (argc != 3) {
         av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
         return 1;
     }
 
+    av_log_set_level(AV_LOG_DEBUG);
     if ((ret = open_input_file(argv[1])) < 0)
         goto end;
     if ((ret = open_output_file(argv[2])) < 0)
@@ -530,7 +529,6 @@  int main(int argc, char **argv)
         if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
             break;
         stream_index = packet.stream_index;
-        type = ifmt_ctx->streams[packet.stream_index]->codecpar->codec_type;
         av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
                 stream_index);
 
@@ -544,24 +542,30 @@  int main(int argc, char **argv)
             av_packet_rescale_ts(&packet,
                                  ifmt_ctx->streams[stream_index]->time_base,
                                  stream_ctx[stream_index].dec_ctx->time_base);
-            dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 :
-                avcodec_decode_audio4;
-            ret = dec_func(stream_ctx[stream_index].dec_ctx, frame,
-                    &got_frame, &packet);
+
+            /* send the packet for decoding */
+            ret = avcodec_send_packet(stream_ctx[stream_index].dec_ctx, &packet);
             if (ret < 0) {
-                av_frame_free(&frame);
-                av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
-                break;
+                fprintf(stderr, "(%s:%d) Error sending the packet to the encoder: %s\n",
+                        __FUNCTION__, __LINE__, av_err2str(ret));
+                exit(1);
             }
 
-            if (got_frame) {
+            /* read all the available output frames (in general there may be any
+             * number of them */
+            while (ret >= 0) {
+                ret = avcodec_receive_frame(stream_ctx[stream_index].dec_ctx, frame);
+                if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
+                    break;
+                else if (ret < 0) {
+                    fprintf(stderr, "Error encoding audio frame\n");
+                    exit(1);
+                }
+
                 frame->pts = frame->best_effort_timestamp;
                 ret = filter_encode_write_frame(frame, stream_index);
-                av_frame_free(&frame);
                 if (ret < 0)
                     goto end;
-            } else {
-                av_frame_free(&frame);
             }
         } else {
             /* remux this frame without reencoding */