From patchwork Wed May 11 08:16:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35724 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:a885:b0:7f:4be2:bd17 with SMTP id ca5csp3781049pzb; Wed, 11 May 2022 01:17:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgXrA2pYfA7LEltMljTAImyEJKRfrl9E9bb+0jhAJwIR6HbQ2+Dsc2RzgRe0nGXY57lKxn X-Received: by 2002:a17:906:6a1b:b0:6f4:a9b8:e027 with SMTP id qw27-20020a1709066a1b00b006f4a9b8e027mr23295542ejc.222.1652257070895; Wed, 11 May 2022 01:17:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652257070; cv=none; d=google.com; s=arc-20160816; b=vUm1TYV+MHALzcGCU41eAMrLc+VfX2X9Dvd+4Un1QJ5QW/5kkcnI5EdP9wkZysGhgI bPEM203pshC51iLRXjTrSEL9ZvRAhhSka9Oc2gZ8/J6SW0URn5oSyVHcoKKQ8qlr9Nsc kZ+v6MXnAOz/RS0XTj7sN4hs8acEwjYK3/VA/MTiV0jO1QgscghWwcHT2IaYu+wF2IBN mLVOaSqnCeLDjn3uBhVN9tcXG/Am7SsUBV1K/cDjSlPAjuDu3484wx5J4vKiJKzvSGI9 sp7aAIv6YcI4q0SqufTB6+NvG9DevAfZi3Z5Q8UlKBUcxEB4UDgkLDlY3sKG9Z08XFOf tZBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=O2wSuQUBjHolZGjwDup3U7leKYK2BKkzPgCbq5RA7fg=; b=zksi383JpxKE6fOvZvvapg/zfcDXiYjmzT9pxYojDH3iwZZDeN52uEPoraTUKSFiEx mfqxAkeMlgRe1nBN7D25pS2u5ZP4OEW3/nhbTDwDAnrc5gitqXzo5UxxjWRmgiX8l0rC sx4q4BJzDvDL1zl+AxExGifQQOW+7LNJNP8lGyZ9R2C4mInbocfPqldnq9jjZEcC8MIy a1jJLBw9mTlbm+sQ9DnCm9CFvr98fYakmcU1nholgkKKndZ5mTErqN9m835ZTuof5Jju dLGJXrKNHqZQMePr4XwlV3oUDCX/C9f+wigYMeIgBKcriDJrUoZtNXbCsnHQsxIZ+X6P 04ig== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u18-20020aa7d992000000b004281818148bsi1453744eds.240.2022.05.11.01.17.49; Wed, 11 May 2022 01:17:50 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BAC0768B379; Wed, 11 May 2022 11:17:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: yjy00KK08rj9 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)