From patchwork Sat Jul 23 14:09:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 36934 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a9d:f07:0:b0:61c:acf1:bf6b with SMTP id 7csp644533ott; Sat, 23 Jul 2022 07:13:48 -0700 (PDT) X-Google-Smtp-Source: AGRyM1teimSL/Ym8K8eW1G/jozbDVF/4xQz5QQ4V9nYDk0mXOQYzwVS6veA3N27ziLlFT9GlLwLt X-Received: by 2002:a05:6402:5489:b0:43b:b935:db37 with SMTP id fg9-20020a056402548900b0043bb935db37mr4642976edb.347.1658585628637; Sat, 23 Jul 2022 07:13:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658585628; cv=none; d=google.com; s=arc-20160816; b=U0WFKKt+nw06A02ePZohFrR1nCevFO0z3MD6qirWaP9t7z8gocSAuSOJOr7h3cjpjb 0YurlSw3uYpc135h6f04NNijCI/VeVHRTs9YwTMAX2o4/ahmqT6G82jwsjmsn1gOD3sK WUGLtppAdkfarO5HJYZ0lPp6rS3B9I9H73oYQWUnRz3jewUaWYXgy+UJFBfTXf3FYk3l Nx9KH0BFaYPQeShbcmfaWB+ZCwYYZ12CkJzM9zpyIozLXpEvMZlWqUHemyEzlUySi4Ub UQMmXWBj/1Kc4UItl9q/sXTfCT79HFUL5qBuP2Io4XGxeotNpflISRBoqYQL+BTbZOEG Q1Ow== 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=dzG1fKMAK5WAw0/ucV2rWWJUQ/p3p/FIMKY4V28A41g=; b=0dLaMITePkruekWDL8D1ks8cISdqNvzaI0Tknld7f2PHjYxB3UFmmKl9zfTBoTRTH5 3VKOkA3qVOPu5Wqy/pEAXZEIZpDiwFGbC7QBzbYZTsQb+LxQe/yJZvofS6WTXXZ/7jCI iH9030luSHe6pgHyV/ig1CtD+s4HFWCmTsJ23fUhoWKScX7rN0EV+wwXOqjLXfa1dgkY L/gY4RVoHG/8ZsiwxAe8+7JcH2jiqrXiiuB8SKi2lKXHVmhyeJ7H7M2LGL2yebMkNc2k 0uxZh4LiSqq/yc4axglpGARY3YMqmGQ3W/ylgdKaqll91LXmhQv8VnXnMW88yn/a3Umg yHag== 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 qb29-20020a1709077e9d00b00722ea4dd14csi9283772ejc.387.2022.07.23.07.13.48; Sat, 23 Jul 2022 07:13:48 -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 C63DE68B8EC; Sat, 23 Jul 2022 17:10:48 +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 006E368B803 for ; Sat, 23 Jul 2022 17:10:26 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 1CB7C2404FE for ; Sat, 23 Jul 2022 16:10:23 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id h_W_xXV5xa9n for ; Sat, 23 Jul 2022 16:10:22 +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 7C96424050B for ; Sat, 23 Jul 2022 16:10:19 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 7A6E83A046F; Sat, 23 Jul 2022 16:10:19 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 23 Jul 2022 16:09:35 +0200 Message-Id: <20220723140952.31814-10-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220723140952.31814-1-anton@khirnov.net> References: <20220723140952.31814-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/27] fftools/ffmpeg: stop allocating an encoder context when not encoding 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: SeufzhtB28dv Content-Length: 6869 --- fftools/ffmpeg.c | 12 +++++++++--- fftools/ffmpeg_opt.c | 36 ++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 0e30f8ce01..30618c6a6a 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1462,7 +1462,8 @@ static void print_final_stats(int64_t total_size) } extra_size += par->extradata_size; data_size += ost->data_size; - if ( (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) + if (ost->enc_ctx && + (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) != AV_CODEC_FLAG_PASS1) pass1_used = 0; } @@ -1630,7 +1631,8 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti av_bprintf(&buf, "%X", av_log2(qp_histogram[j] + 1)); } - if ((enc->flags & AV_CODEC_FLAG_PSNR) && (ost->pict_type != AV_PICTURE_TYPE_NONE || is_last_report)) { + if (enc && (enc->flags & AV_CODEC_FLAG_PSNR) && + (ost->pict_type != AV_PICTURE_TYPE_NONE || is_last_report)) { int j; double error, error_sum = 0; double scale, scale_sum = 0; @@ -3139,6 +3141,9 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) break; } + if (ost->bitexact) + enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT; + if (ost->sq_idx_encode >= 0) sq_set_tb(of->sq_encode, ost->sq_idx_encode, enc_ctx->time_base); @@ -3635,7 +3640,8 @@ static int check_keyboard_interaction(int64_t cur_time) } for(i=0;ienc_ctx->debug = debug; + if (ost->enc_ctx) + ost->enc_ctx->debug = debug; } if(debug) av_log_set_level(AV_LOG_DEBUG); fprintf(stderr,"debug=%d\n", debug); diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 716b53d81f..f6e354355b 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1568,12 +1568,14 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e exit_program(1); } - ost->enc_ctx = avcodec_alloc_context3(ost->enc); - if (!ost->enc_ctx) { - av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding context.\n"); - exit_program(1); + if (ost->enc) { + ost->enc_ctx = avcodec_alloc_context3(ost->enc); + if (!ost->enc_ctx) { + av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding context.\n"); + exit_program(1); + } + ost->enc_ctx->codec_type = type; } - ost->enc_ctx->codec_type = type; ost->filtered_frame = av_frame_alloc(); if (!ost->filtered_frame) @@ -1622,9 +1624,8 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e if (o->bitexact) { - ost->enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT; ost->bitexact = 1; - } else { + } else if (ost->enc_ctx) { ost->bitexact = check_opt_bitexact(ost->enc_ctx, ost->encoder_opts, "flags", AV_CODEC_FLAG_BITEXACT); } @@ -1678,12 +1679,13 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e uint32_t tag = strtol(codec_tag, &next, 0); if (*next) tag = AV_RL32(codec_tag); - ost->st->codecpar->codec_tag = - ost->enc_ctx->codec_tag = tag; + ost->st->codecpar->codec_tag = tag; + if (ost->enc_ctx) + ost->enc_ctx->codec_tag = tag; } MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st); - if (qscale >= 0) { + if (ost->enc_ctx && qscale >= 0) { ost->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE; ost->enc_ctx->global_quality = FF_QP2LAMBDA * qscale; } @@ -1700,7 +1702,7 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e MATCH_PER_STREAM_OPT(bits_per_raw_sample, i, ost->bits_per_raw_sample, oc, st); - if (oc->oformat->flags & AVFMT_GLOBALHEADER) + if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc_ctx) ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; av_dict_copy(&ost->sws_dict, o->g->sws_dict, 0); @@ -1802,12 +1804,10 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in { AVStream *st; OutputStream *ost; - AVCodecContext *video_enc; char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL; ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO, source_index); st = ost->st; - video_enc = ost->enc_ctx; MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st); if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) { @@ -1845,6 +1845,7 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in MATCH_PER_STREAM_OPT(filters, str, ost->filters, oc, st); if (!ost->stream_copy) { + AVCodecContext *video_enc = ost->enc_ctx; const char *p = NULL; char *frame_size = NULL; char *frame_pix_fmt = NULL; @@ -2045,18 +2046,16 @@ static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc, in int n; AVStream *st; OutputStream *ost; - AVCodecContext *audio_enc; ost = new_output_stream(o, oc, AVMEDIA_TYPE_AUDIO, source_index); st = ost->st; - audio_enc = ost->enc_ctx; - audio_enc->codec_type = AVMEDIA_TYPE_AUDIO; MATCH_PER_STREAM_OPT(filter_scripts, str, ost->filters_script, oc, st); MATCH_PER_STREAM_OPT(filters, str, ost->filters, oc, st); if (!ost->stream_copy) { + AVCodecContext *audio_enc = ost->enc_ctx; int channels = 0; char *layout = NULL; char *sample_fmt = NULL; @@ -2178,15 +2177,12 @@ static OutputStream *new_subtitle_stream(OptionsContext *o, AVFormatContext *oc, { AVStream *st; OutputStream *ost; - AVCodecContext *subtitle_enc; ost = new_output_stream(o, oc, AVMEDIA_TYPE_SUBTITLE, source_index); st = ost->st; - subtitle_enc = ost->enc_ctx; - - subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE; if (!ost->stream_copy) { + AVCodecContext *subtitle_enc = ost->enc_ctx; char *frame_size = NULL; MATCH_PER_STREAM_OPT(frame_sizes, str, frame_size, oc, st);