From patchwork Fri Jul 7 09:48:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 42488 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1e:b0:12b:9ae3:586d with SMTP id c30csp7162552pzh; Fri, 7 Jul 2023 02:51:19 -0700 (PDT) X-Google-Smtp-Source: APBJJlF+d6MWvf1ysMw9KI/sM6mWLofe7HREbkY07io1XUmp9SPn1TpRoWWhsilFFI2nbt6cnmA9 X-Received: by 2002:a05:6402:1a33:b0:51b:df14:594e with SMTP id be19-20020a0564021a3300b0051bdf14594emr3427932edb.39.1688723479592; Fri, 07 Jul 2023 02:51:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688723479; cv=none; d=google.com; s=arc-20160816; b=GOW2BnT+uAA7l+/OMHveugYQN5LTcKkui8fsce/vRuF4Ep4puBs/a071sQe6whDseO Gxj86dICrgtRZJSEHepWUxXsAFb6EB5/D0y3664qCJqo8w8r3z2XW5msCLU4SZLSdD4r itLgfYQPFwDUeiVJjQAdwoV2SXGaVnhBLv5tW5NOWmJOPbVI+34qRNToYo66F+IhtI25 BX/ahqu0QxOk/Mr2QP+fTwddbiLGb6otCEnaEeX3fGvCIQ8utG0bclAa5XBC0F4HibMa hdf2p/p4qXA7Itl0/SmiVlweO7vhhlaVtcFxlX2IeNvP5xDMlyN8ckc7pI5nD2ed5cWF drcA== 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=CrLLvTqUd39V0b8fpPJfz2DYn3cSpqhlacjunoSEXiE=; fh=8ZF2QtiKOjBtyn9wuO2cMeZaLsE4aq02Bq77gGK61E8=; b=SngAYu8+wuB7tyTMMMlIu503HsIbuDOhhnSFQNh9iFMcmZ7rt/P3csq9S0xgkMNORV dCi7WHEOD/X2zPm8VrYmpLobwme3RVaWlKIZK9HhO5T6ENoOqnAXmPv7PVue4CNbbnJJ fj8CLa0oFqstyw1O+0Nb4asuzTxgyF11vmb06XhL5bS141QfpMR7g4uZGv49WpBZVsrK P2SjY8ZWt39EnxDiCgt0X6jejE2YzVIOjnQ4tz4XyiXrIM5TpRLENaplnhImhAMg+JeR OJG/Gi+xBNDCk36GoHADcu1kCffiHigD4jOu425v19NX18i1G+ureSKibplC4HK9wn4j gwpQ== 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 b3-20020a05640202c300b0051de3779436si2072321edx.454.2023.07.07.02.51.18; Fri, 07 Jul 2023 02:51:19 -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 4B6E568C813; Fri, 7 Jul 2023 12:49:23 +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 A793C68C7CC for ; Fri, 7 Jul 2023 12:49:10 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 7106A2404EA for ; Fri, 7 Jul 2023 11:49:10 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 1DdU1-s0CJIJ for ; Fri, 7 Jul 2023 11:49: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 2006C240D0E for ; Fri, 7 Jul 2023 11:49:01 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 0D05B3A188A for ; Fri, 7 Jul 2023 11:48:55 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 7 Jul 2023 11:48:43 +0200 Message-Id: <20230707094847.25324-18-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230707094847.25324-1-anton@khirnov.net> References: <20230707094847.25324-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 18/22] fftools/ffmpeg_enc: initialize audio/video encoders from frame parameters 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: pdeFRHnVLwey This is possible now that enc_open() is always called with a non-NULL frame for audio/video. Previously the code would directly reach into the buffersink, which is a layering violation. --- fftools/ffmpeg_enc.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index dd056e42f5..1347493f9f 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -34,8 +34,6 @@ #include "libavutil/rational.h" #include "libavutil/timestamp.h" -#include "libavfilter/buffersink.h" - #include "libavcodec/avcodec.h" // FIXME private header, used for mid_pred() @@ -198,12 +196,21 @@ int enc_open(OutputStream *ost, AVFrame *frame) AVCodecContext *dec_ctx = NULL; const AVCodec *enc = enc_ctx->codec; OutputFile *of = output_files[ost->file_index]; - FrameData *fd = frame ? frame_data(frame) : NULL; + FrameData *fd; int ret; if (e->opened) return 0; + // frame is always non-NULL for audio and video + av_assert0(frame || (enc->type != AVMEDIA_TYPE_VIDEO && enc->type != AVMEDIA_TYPE_AUDIO)); + + if (frame) { + fd = frame_data(frame); + if (!fd) + return AVERROR(ENOMEM); + } + set_encoder_id(output_files[ost->file_index], ost); if (ist) { @@ -212,15 +219,15 @@ int enc_open(OutputStream *ost, AVFrame *frame) switch (enc_ctx->codec_type) { case AVMEDIA_TYPE_AUDIO: - enc_ctx->sample_fmt = av_buffersink_get_format(ost->filter->filter); - enc_ctx->sample_rate = av_buffersink_get_sample_rate(ost->filter->filter); - ret = av_buffersink_get_ch_layout(ost->filter->filter, &enc_ctx->ch_layout); + enc_ctx->sample_fmt = frame->format; + enc_ctx->sample_rate = frame->sample_rate; + ret = av_channel_layout_copy(&enc_ctx->ch_layout, &frame->ch_layout); if (ret < 0) return ret; if (ost->bits_per_raw_sample) enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample; - else if (fd) + else enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample, av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3); @@ -231,7 +238,7 @@ int enc_open(OutputStream *ost, AVFrame *frame) case AVMEDIA_TYPE_VIDEO: { AVRational fr = ost->frame_rate; - if (!fr.num && fd) + if (!fr.num) fr = fd->frame_rate_filter; if (!fr.num && !ost->max_frame_rate.num) { fr = (AVRational){25, 1}; @@ -261,7 +268,7 @@ int enc_open(OutputStream *ost, AVFrame *frame) av_inv_q(fr); if (!(enc_ctx->time_base.num && enc_ctx->time_base.den)) - enc_ctx->time_base = av_buffersink_get_time_base(ost->filter->filter); + enc_ctx->time_base = frame->time_base; if ( av_q2d(enc_ctx->time_base) < 0.001 && ost->vsync_method != VSYNC_PASSTHROUGH && (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR || (ost->vsync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){ @@ -270,18 +277,18 @@ int enc_open(OutputStream *ost, AVFrame *frame) "setting vsync/fps_mode to vfr\n"); } - enc_ctx->width = av_buffersink_get_w(ost->filter->filter); - enc_ctx->height = av_buffersink_get_h(ost->filter->filter); + enc_ctx->width = frame->width; + enc_ctx->height = frame->height; enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio = ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) : - av_buffersink_get_sample_aspect_ratio(ost->filter->filter); + frame->sample_aspect_ratio; - enc_ctx->pix_fmt = av_buffersink_get_format(ost->filter->filter); + enc_ctx->pix_fmt = frame->format; if (ost->bits_per_raw_sample) enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample; - else if (fd) + else enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample, av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);