From patchwork Wed Sep 28 09:55:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 38419 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp93887pzh; Wed, 28 Sep 2022 02:58:58 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7NdbP6TaxVmuhABvM56M/0+BblnCPxcWkg2odXJOJ6i9DTJrGpaFDR4xbXrAf1zdQUt4B6 X-Received: by 2002:a17:907:2cf0:b0:77b:2ad7:121b with SMTP id hz16-20020a1709072cf000b0077b2ad7121bmr25688374ejc.577.1664359137814; Wed, 28 Sep 2022 02:58:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664359137; cv=none; d=google.com; s=arc-20160816; b=k5xUoQBK5+gmaG2UQuhIIK/R1LaGggjy9Kv2uEUDnwbkNLHHcOuaHh1DzlAAbxrey9 b8Q32tLYOAfPU2KvheBe8+QcfWm5A9Qwk49vI9SGjKPveiOcfQHS9olstBDmmAB6Isr1 RLVwnWtHSfOL2VdqckYBXxU6X+9WDe5cNthB5glKiUVsMFQ+lwPDS5+z9Z9N7KPubjAL tzgmEStiXpwgzkQ0acCyKpeWVuNCy7V6yYLupknCmyutXyKuGwncrvlVDrAPnKjBu9qJ GD1ZxRAbrNISWaKKQ+sJSZIEfct+bxkC3EMt2lnDQNit0kLElNXtmGTv8OU5bGM+y2mB 6Ymw== 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:references:in-reply-to:message-id :date:to:from:delivered-to; bh=BRxzisx6eU37D40nebLC8ET6ujTjY7P9r+G9yBJNFzo=; b=qUAIqjXujGRMkRWDvY/WufoZG4/kIa1bHTYDACyuw/wjNvlT8Z7QGSxfrvMm2PlOaf CTeTWnoheOD5fiz4ZrAYVp9HR0DSfs3QfQB/6UI1vmLUuNQ25VNjHR1CgoG6hYI8sMdI c4hDbXRQK1SA43saCLk2oFn8nFhDeP7WGYhsiI6XHYjXcRts0Pz6Serx9Bw3hFYNu1B+ 7aI3uwg+MXEyV3R5ImmvFw+ckjZCNsr2T98C7J5uJxzEqw71sLAhHxsfvpRN3BQrNeiE b7GB2G25i7O2YC+bdEvC3P8WV6ZNvM05CioJ7o5ehC3CYZhsHpLdavdTJEJulx9KmLz/ MNhw== 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 u17-20020a50c2d1000000b00457e6752422si1998510edf.189.2022.09.28.02.58.56; Wed, 28 Sep 2022 02:58:57 -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 0FE5E68BC14; Wed, 28 Sep 2022 12:58:15 +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 6DBCE68B8EF for ; Wed, 28 Sep 2022 12:58:07 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 607A52404E4 for ; Wed, 28 Sep 2022 11:58:03 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 29NEuRryN0CL for ; Wed, 28 Sep 2022 11:58:02 +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 02F772404F5 for ; Wed, 28 Sep 2022 11:58:01 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 37C6C3A0D7F for ; Wed, 28 Sep 2022 11:58:01 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 28 Sep 2022 11:55:50 +0200 Message-Id: <20220928095556.28209-3-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220928095556.28209-1-anton@khirnov.net> References: <20220928095556.28209-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/9] fftools/ffmpeg: move forced keyframe processing into its own function 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: 0tA+szPaxim8 --- fftools/ffmpeg.c | 108 +++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 50 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 2054271c82..833df0b167 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1133,6 +1133,63 @@ static void do_subtitle_out(OutputFile *of, } } +static enum AVPictureType forced_kf_apply(OutputStream *ost, + const AVFrame *in_picture, int dup_idx) +{ + AVCodecContext *enc = ost->enc_ctx; + double pts_time; + + if (ost->forced_kf_ref_pts == AV_NOPTS_VALUE) + ost->forced_kf_ref_pts = in_picture->pts; + + pts_time = (in_picture->pts - ost->forced_kf_ref_pts) * av_q2d(enc->time_base); + if (ost->forced_kf_index < ost->forced_kf_count && + in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) { + ost->forced_kf_index++; + goto force_keyframe; + } else if (ost->forced_keyframes_pexpr) { + double res; + ost->forced_keyframes_expr_const_values[FKF_T] = pts_time; + res = av_expr_eval(ost->forced_keyframes_pexpr, + ost->forced_keyframes_expr_const_values, NULL); + ff_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n", + ost->forced_keyframes_expr_const_values[FKF_N], + ost->forced_keyframes_expr_const_values[FKF_N_FORCED], + ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N], + ost->forced_keyframes_expr_const_values[FKF_T], + ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T], + res); + + ost->forced_keyframes_expr_const_values[FKF_N] += 1; + + if (res) { + ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = + ost->forced_keyframes_expr_const_values[FKF_N] - 1; + ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = + ost->forced_keyframes_expr_const_values[FKF_T]; + ost->forced_keyframes_expr_const_values[FKF_N_FORCED] += 1; + goto force_keyframe; + } + } else if ( ost->forced_keyframes + && !strncmp(ost->forced_keyframes, "source", 6) + && in_picture->key_frame==1 + && !dup_idx) { + goto force_keyframe; + } else if ( ost->forced_keyframes + && !strncmp(ost->forced_keyframes, "source_no_drop", 14) + && !dup_idx) { + ost->dropped_keyframe = 0; + if ((in_picture->key_frame == 1) || ost->dropped_keyframe) + goto force_keyframe; + } + + return AV_PICTURE_TYPE_NONE; + +force_keyframe: + av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time); + return AV_PICTURE_TYPE_I; +} + /* May modify/reset next_picture */ static void do_video_out(OutputFile *of, OutputStream *ost, @@ -1272,8 +1329,6 @@ static void do_video_out(OutputFile *of, /* duplicates frame if needed */ for (i = 0; i < nb_frames; i++) { AVFrame *in_picture; - int forced_keyframe = 0; - double pts_time; if (i < nb0_frames && ost->last_frame->buf[0]) { in_picture = ost->last_frame; @@ -1289,54 +1344,7 @@ static void do_video_out(OutputFile *of, return; in_picture->quality = enc->global_quality; - in_picture->pict_type = 0; - - if (ost->forced_kf_ref_pts == AV_NOPTS_VALUE) - ost->forced_kf_ref_pts = in_picture->pts; - - pts_time = (in_picture->pts - ost->forced_kf_ref_pts) * av_q2d(enc->time_base); - if (ost->forced_kf_index < ost->forced_kf_count && - in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) { - ost->forced_kf_index++; - forced_keyframe = 1; - } else if (ost->forced_keyframes_pexpr) { - double res; - ost->forced_keyframes_expr_const_values[FKF_T] = pts_time; - res = av_expr_eval(ost->forced_keyframes_pexpr, - ost->forced_keyframes_expr_const_values, NULL); - ff_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n", - ost->forced_keyframes_expr_const_values[FKF_N], - ost->forced_keyframes_expr_const_values[FKF_N_FORCED], - ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N], - ost->forced_keyframes_expr_const_values[FKF_T], - ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T], - res); - if (res) { - forced_keyframe = 1; - ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = - ost->forced_keyframes_expr_const_values[FKF_N]; - ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = - ost->forced_keyframes_expr_const_values[FKF_T]; - ost->forced_keyframes_expr_const_values[FKF_N_FORCED] += 1; - } - - ost->forced_keyframes_expr_const_values[FKF_N] += 1; - } else if ( ost->forced_keyframes - && !strncmp(ost->forced_keyframes, "source", 6) - && in_picture->key_frame==1 - && !i) { - forced_keyframe = 1; - } else if ( ost->forced_keyframes - && !strncmp(ost->forced_keyframes, "source_no_drop", 14) - && !i) { - forced_keyframe = (in_picture->key_frame == 1) || ost->dropped_keyframe; - ost->dropped_keyframe = 0; - } - - if (forced_keyframe) { - in_picture->pict_type = AV_PICTURE_TYPE_I; - av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time); - } + in_picture->pict_type = forced_kf_apply(ost, in_picture, i); ret = submit_encode_frame(of, ost, in_picture); if (ret < 0 && ret != AVERROR_EOF)