From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id C71C340EA2 for ; Wed, 11 May 2022 08:17:46 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8512768B3F5; Wed, 11 May 2022 11:17:19 +0300 (EEST) Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E60DF68B379 for ; Wed, 11 May 2022 11:17:10 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 54870240179 for ; Wed, 11 May 2022 10:17:10 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id won-SIXXHe8I for ; Wed, 11 May 2022 10:17:09 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (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 "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 92E2224017E for ; Wed, 11 May 2022 10:17:07 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id A51F83A0406; Wed, 11 May 2022 10:17:02 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 11 May 2022 10:16:46 +0200 Message-Id: <20220511081654.15127-1-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Call do_video_stats() for every video packets produced by the encoder, rather than for every frame sent to the encoder. --- fftools/ffmpeg.c | 145 ++++++++++++++++++++--------------------------- 1 file changed, 60 insertions(+), 85 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index a85ed18b08..0e62c39522 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -831,63 +831,96 @@ static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame, return ret; } -static void do_audio_out(OutputFile *of, OutputStream *ost, - AVFrame *frame) +static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) { - AVCodecContext *enc = ost->enc_ctx; - AVPacket *pkt = ost->pkt; + AVCodecContext *enc = ost->enc_ctx; + AVPacket *pkt = ost->pkt; + const char *type_desc = av_get_media_type_string(enc->codec_type); int ret; - adjust_frame_pts_to_encoder_tb(of, ost, frame); - - if (!check_recording_time(ost)) - return; - - if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0) - frame->pts = ost->sync_opts; - ost->sync_opts = frame->pts + frame->nb_samples; - ost->samples_encoded += frame->nb_samples; ost->frames_encoded++; update_benchmark(NULL); + if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder <- type:audio " + av_log(NULL, AV_LOG_INFO, "encoder <- type:%s " "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n", + type_desc, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base), enc->time_base.num, enc->time_base.den); } ret = avcodec_send_frame(enc, frame); - if (ret < 0) - goto error; + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n", + type_desc); + return ret; + } while (1) { ret = avcodec_receive_packet(enc, pkt); + update_benchmark("encode_%s %d.%d", type_desc, + ost->file_index, ost->index); if (ret == AVERROR(EAGAIN)) - break; - if (ret < 0) - goto error; + return 0; + else if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "%s encoding failed\n", type_desc); + return ret; + } - update_benchmark("encode_audio %d.%d", ost->file_index, ost->index); + if (debug_ts) { + av_log(NULL, AV_LOG_INFO, "encoder -> type:%s " + "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " + "duration:%s duration_time:%s\n", + type_desc, + av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base), + av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base), + av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); + } av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase); if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder -> type:audio " + av_log(NULL, AV_LOG_INFO, "encoder -> type:%s " "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " "duration:%s duration_time:%s\n", + type_desc, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base), av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base), av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); } + if (enc->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) + do_video_stats(ost, pkt->size); + output_packet(of, pkt, ost, 0); + + /* if two pass, output log */ + if (ost->logfile && enc->stats_out) + fprintf(ost->logfile, "%s", enc->stats_out); } - return; -error: - av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n"); - exit_program(1); + av_assert0(0); +} + +static void do_audio_out(OutputFile *of, OutputStream *ost, + AVFrame *frame) +{ + int ret; + + adjust_frame_pts_to_encoder_tb(of, ost, frame); + + if (!check_recording_time(ost)) + return; + + if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0) + frame->pts = ost->sync_opts; + ost->sync_opts = frame->pts + frame->nb_samples; + ost->samples_encoded += frame->nb_samples; + + ret = encode_frame(of, ost, frame); + if (ret < 0) + exit_program(1); } static void do_subtitle_out(OutputFile *of, @@ -979,14 +1012,12 @@ static void do_video_out(OutputFile *of, AVFrame *next_picture) { int ret; - AVPacket *pkt = ost->pkt; AVCodecContext *enc = ost->enc_ctx; AVRational frame_rate; int nb_frames, nb0_frames, i; double delta, delta0; double duration = 0; double sync_ipts = AV_NOPTS_VALUE; - int frame_size = 0; InputStream *ist = NULL; AVFilterContext *filter = ost->filter->filter; @@ -1179,73 +1210,17 @@ static void do_video_out(OutputFile *of, av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time); } - update_benchmark(NULL); - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder <- type:video " - "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n", - av_ts2str(in_picture->pts), av_ts2timestr(in_picture->pts, &enc->time_base), - enc->time_base.num, enc->time_base.den); - } - - ost->frames_encoded++; - - ret = avcodec_send_frame(enc, in_picture); + ret = encode_frame(of, ost, in_picture); if (ret < 0) - goto error; - // Make sure Closed Captions will not be duplicated - av_frame_remove_side_data(in_picture, AV_FRAME_DATA_A53_CC); - - while (1) { - ret = avcodec_receive_packet(enc, pkt); - update_benchmark("encode_video %d.%d", ost->file_index, ost->index); - if (ret == AVERROR(EAGAIN)) - break; - if (ret < 0) - goto error; - - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder -> type:video " - "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " - "duration:%s duration_time:%s\n", - av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base), - av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base), - av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); - } - - av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase); - - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder -> type:video " - "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " - "duration:%s duration_time:%s\n", - av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->mux_timebase), - av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->mux_timebase), - av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->mux_timebase)); - } - - frame_size = pkt->size; - output_packet(of, pkt, ost, 0); + exit_program(1); - /* if two pass, output log */ - if (ost->logfile && enc->stats_out) { - fprintf(ost->logfile, "%s", enc->stats_out); - } - } ost->sync_opts++; ost->frame_number++; - - if (vstats_filename && frame_size) - do_video_stats(ost, frame_size); } av_frame_unref(ost->last_frame); if (next_picture) av_frame_move_ref(ost->last_frame, next_picture); - - return; -error: - av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n"); - exit_program(1); } static double psnr(double d) -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".