From patchwork Sat Apr 11 15:06:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18864 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 9861F44BB3B for ; Sat, 11 Apr 2020 18:06:34 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 801CD68B7EA; Sat, 11 Apr 2020 18:06:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0911168B6FE for ; Sat, 11 Apr 2020 18:06:27 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id C4510288552 for ; Sat, 11 Apr 2020 17:06:26 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id g7WajU9JqO_8 for ; Sat, 11 Apr 2020 17:06:26 +0200 (CEST) Received: from quelana.khirnov.net (unknown [IPv6:2002:b061:f0a:201:5e:e696:5100:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "quelana.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id 2CB16288550 for ; Sat, 11 Apr 2020 17:06:26 +0200 (CEST) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 44F272182F for ; Sat, 11 Apr 2020 17:06:25 +0200 (CEST) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id j8-bmA9qyyot for ; Sat, 11 Apr 2020 17:06:23 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id 914A7213CC for ; Sat, 11 Apr 2020 17:06:23 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id E97F720E0012; Sat, 11 Apr 2020 17:06:18 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 11 Apr 2020 17:06:14 +0200 Message-Id: <20200411150614.24578-3-anton@khirnov.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200411150614.24578-1-anton@khirnov.net> References: <20200411150614.24578-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] doc/examples/muxing: convert to new encoding API 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- doc/examples/muxing.c | 95 ++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 56 deletions(-) diff --git a/doc/examples/muxing.c b/doc/examples/muxing.c index 9af9aae483..c1d42303af 100644 --- a/doc/examples/muxing.c +++ b/doc/examples/muxing.c @@ -78,15 +78,45 @@ static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt) pkt->stream_index); } -static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt) +static int write_frame(AVFormatContext *fmt_ctx, AVCodecContext *c, + AVStream *st, AVFrame *frame) { - /* rescale output packet timestamp values from codec to stream timebase */ - av_packet_rescale_ts(pkt, *time_base, st->time_base); - pkt->stream_index = st->index; + int ret; + + // send the frame to the encoder + ret = avcodec_send_frame(c, frame); + if (ret < 0) { + fprintf(stderr, "Error sending a frame to the encoder: %s\n", + av_err2str(ret)); + exit(1); + } + + while (ret >= 0) { + AVPacket pkt = { 0 }; + + ret = avcodec_receive_packet(c, &pkt); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + break; + else if (ret < 0) { + fprintf(stderr, "Error encoding a frame: %s\n", av_err2str(ret)); + exit(1); + } + + /* rescale output packet timestamp values from codec to stream timebase */ + av_packet_rescale_ts(&pkt, c->time_base, st->time_base); + pkt.stream_index = st->index; - /* Write the compressed frame to the media file. */ - log_packet(fmt_ctx, pkt); - return av_interleaved_write_frame(fmt_ctx, pkt); + /* Write the compressed frame to the media file. */ + log_packet(fmt_ctx, &pkt); + ret = av_interleaved_write_frame(fmt_ctx, &pkt); + av_packet_unref(&pkt); + if (ret < 0) { + fprintf(stderr, "Error while writing output packet: %s\n", av_err2str(ret)); + exit(1); + } + } + + return ret == AVERROR_EOF ? 1 : 0; } /* Add an output stream. */ @@ -309,13 +339,10 @@ static AVFrame *get_audio_frame(OutputStream *ost) 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); @@ -349,22 +376,7 @@ 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)); - 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); - } - } - - return (frame || got_packet) ? 0 : 1; + return write_frame(oc, c, ost->st, frame); } /**************************************************************/ @@ -506,37 +518,8 @@ static AVFrame *get_video_frame(OutputStream *ost) */ 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 write_frame(oc, ost->enc, ost->st, get_video_frame(ost)); - return (frame || got_packet) ? 0 : 1; } static void close_stream(AVFormatContext *oc, OutputStream *ost)