From patchwork Sat May 2 10:10:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhao, Gang" X-Patchwork-Id: 19427 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 8C7DA449CF4 for ; Sat, 2 May 2020 13:10:41 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 630C268C506; Sat, 2 May 2020 13:10:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f66.google.com (mail-pj1-f66.google.com [209.85.216.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 81B0868C506 for ; Sat, 2 May 2020 13:10:34 +0300 (EEST) Received: by mail-pj1-f66.google.com with SMTP id e6so1126640pjt.4 for ; Sat, 02 May 2020 03:10:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:in-reply-to :references; bh=3TxpNw35Z4gkmeiAykN8Gh5gnNSaHhMcs6iDgY3O3LI=; b=bWXrdskumc9f9F5x6FIwqX6E63oLg1WbOTGNChqeM0Uhg+nyfuP7wbaKjvXgqRrUnt zmcW8dh1//ONYg8JeUvb2Oy3eyTp+u8A5KN/olfQkaDz1lSTt3do5kNoo1iVfmJIT4nJ wiUV30eD/9OSLJXmP4qw1/hArNXBVD7Pbvvc4WoE9hyyXqfgvYyzvdnDEGxbnH0Co/tx LMZlnOlwTZjvf2p/AFCUBco9Zban+vcmYoxqmUcMuHP4PCzjdafmn4U4W/wbBz/404uU HUoyh5i9M0ODBpE1f3J6SNotvLCRdPNmShY3MMXH0WdHCPHT/+aYpmGwhVp/nkC7QdeP 1F+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=3TxpNw35Z4gkmeiAykN8Gh5gnNSaHhMcs6iDgY3O3LI=; b=bmVwP2Jfu8AEDR/P0Wp8yZWgNqE75NSAMHMkFh6IpkDUDCD6zqj6DNO/Hy0TxI5nKc raze9Eq8TGSKXT7W5Zgl5my1o95HzWmXjE0K5afTe9ReppMuClPCebr3uDdmQV9OMPpX ozvLAmQhWRoHqrlkz5LsDcCMSbw1Tjo6h5ApUZhKnx+02j5+qLMiMWaXMZw6+Kn6bQx7 DnaX0IPuV8DgPsgIpoOODkHdxYJ9P5pFJc8yqqtuA2tzt16thyeeTbUuIP4xc5THHDJk B+z4e7Kbb8is6rGZeqMx5e5mbP+z38Mfwxq9SF+xdX54DBmiArV40gJEj8QfLgjMB0ma Woww== X-Gm-Message-State: AGi0PuYu3omRAWYTHUNJR4U8W7mGAYZBii2EoPk36EI90jWSRLARX8NB +AtHkG3+TlvfU0o0bIUl71ruZvybQVc= X-Google-Smtp-Source: APiQypIQ3WrPcK6zS35OHVMfG3kczFy4YQT/zsyVRyNa31TdMrg+wUTjn1VXhThWlM2taLRhP2l3Ag== X-Received: by 2002:a17:902:b682:: with SMTP id c2mr8838484pls.144.1588414231990; Sat, 02 May 2020 03:10:31 -0700 (PDT) Received: from localhost.localdomain ([2600:3c01::f03c:91ff:feee:c20d]) by smtp.gmail.com with ESMTPSA id m1sm1775387pjg.26.2020.05.02.03.10.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 May 2020 03:10:31 -0700 (PDT) From: "Zhao, Gang" To: Date: Sat, 2 May 2020 18:10:11 +0800 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: <4a8148cffdcf122e3b7e0b9ef5308a6db3cc1334.1588413324.git.gang.zhao.42@gmail.com> References: <4a8148cffdcf122e3b7e0b9ef5308a6db3cc1334.1588413324.git.gang.zhao.42@gmail.com> In-Reply-To: <4a8148cffdcf122e3b7e0b9ef5308a6db3cc1334.1588413324.git.gang.zhao.42@gmail.com> References: <4a8148cffdcf122e3b7e0b9ef5308a6db3cc1334.1588413324.git.gang.zhao.42@gmail.com> Subject: [FFmpeg-devel] [PATCH 2/4] doc/examples/muxing.c: Fixed a compile warning X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Fixed deprecation declarations compile warning by replacing deprecated api avcodec_encode_{audio,video}2() with avcodec_send_frame()/avcodec_receive_packet(). Also made some clean up according to this change. Signed-off-by: Zhao, Gang --- Tested that the new code generated the same file as before by running ./doc/examples/muxing_g my_samples/muxing.mp4 doc/examples/muxing.c | 104 +++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 67 deletions(-) diff --git doc/examples/muxing.c doc/examples/muxing.c index 9af9aae483..7e781c0783 100644 --- doc/examples/muxing.c +++ doc/examples/muxing.c @@ -280,8 +280,10 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, A static AVFrame *get_audio_frame(OutputStream *ost) { AVFrame *frame = ost->tmp_frame; - int j, i, v; + int j, i, v, ret; int16_t *q = (int16_t*)frame->data[0]; + int dst_nb_samples; + AVCodecContext *c = ost->enc; /* check if we want to generate more frames */ if (av_compare_ts(ost->next_pts, ost->enc->time_base, @@ -299,27 +301,6 @@ static AVFrame *get_audio_frame(OutputStream *ost) frame->pts = ost->next_pts; ost->next_pts += frame->nb_samples; - return frame; -} - -/* - * encode one audio frame and send it to the muxer - * return 1 when encoding is finished, 0 otherwise - */ -static int write_audio_frame(AVFormatContext *oc, OutputStream *ost) -{ - AVCodecContext *c; - AVPacket pkt = { 0 }; // data and size must be 0; - AVFrame *frame; - int ret; - int got_packet; - int dst_nb_samples; - - av_init_packet(&pkt); - c = ost->enc; - - frame = get_audio_frame(ost); - if (frame) { /* convert samples from native format to destination codec format, using the resampler */ /* compute destination number of samples */ @@ -349,13 +330,40 @@ static int write_audio_frame(AVFormatContext *oc, OutputStream *ost) ost->samples_count += dst_nb_samples; } - ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet); + return frame; +} + +/* + * encode one frame and send it to the muxer + * return 1 when encoding is finished, 0 otherwise + */ +static int muxing_write_frame(AVFormatContext *oc, OutputStream *ost, AVFrame *frame) +{ + AVCodecContext *c; + AVPacket pkt = { 0 }; // data and size must be 0; + int ret; + + av_init_packet(&pkt); + c = ost->enc; + + /* send the frame for encoding */ + ret = avcodec_send_frame(c, frame); if (ret < 0) { - fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret)); + fprintf(stderr, "Error sending the frame to the encoder: %s\n", av_err2str(ret)); exit(1); } - if (got_packet) { + /* read all the available output packets (in general there may be any + * number of them */ + while (ret >= 0) { + ret = avcodec_receive_packet(c, &pkt); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + goto exit; + else if (ret < 0) { + fprintf(stderr, "Error encoding audio frame\n"); + exit(1); + } + ret = write_frame(oc, &c->time_base, ost->st, &pkt); if (ret < 0) { fprintf(stderr, "Error while writing audio frame: %s\n", @@ -364,7 +372,8 @@ static int write_audio_frame(AVFormatContext *oc, OutputStream *ost) } } - return (frame || got_packet) ? 0 : 1; +exit: + return frame ? 0 : 1; } /**************************************************************/ @@ -500,45 +509,6 @@ static AVFrame *get_video_frame(OutputStream *ost) return ost->frame; } -/* - * encode one video frame and send it to the muxer - * return 1 when encoding is finished, 0 otherwise - */ -static int write_video_frame(AVFormatContext *oc, OutputStream *ost) -{ - int ret; - AVCodecContext *c; - AVFrame *frame; - int got_packet = 0; - AVPacket pkt = { 0 }; - - c = ost->enc; - - frame = get_video_frame(ost); - - 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)); - exit(1); - } - - if (got_packet) { - ret = write_frame(oc, &c->time_base, ost->st, &pkt); - } else { - ret = 0; - } - - if (ret < 0) { - fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret)); - exit(1); - } - - return (frame || got_packet) ? 0 : 1; -} - static void close_stream(AVFormatContext *oc, OutputStream *ost) { avcodec_free_context(&ost->enc); @@ -638,9 +608,9 @@ int main(int argc, char **argv) if (encode_video && (!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base, audio_st.next_pts, audio_st.enc->time_base) <= 0)) { - encode_video = !write_video_frame(oc, &video_st); + encode_video = !muxing_write_frame(oc, &video_st, get_video_frame(&video_st)); } else { - encode_audio = !write_audio_frame(oc, &audio_st); + encode_audio = !muxing_write_frame(oc, &audio_st, get_audio_frame(&audio_st)); } }