From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <ffmpeg-devel-bounces@ffmpeg.org> Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id D107A4BAA6 for <ffmpegdev@gitmailbox.com>; Fri, 28 Mar 2025 11:42:04 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 18B3D687C2E; Fri, 28 Mar 2025 13:42:00 +0200 (EET) Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AA108687AB2 for <ffmpeg-devel@ffmpeg.org>; Fri, 28 Mar 2025 13:41:53 +0200 (EET) Received: by mail.gandi.net (Postfix) with ESMTPSA id C172D43188 for <ffmpeg-devel@ffmpeg.org>; Fri, 28 Mar 2025 11:41:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=niedermayer.cc; s=gm1; t=1743162112; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=w6ZFoJptlbisS9/s9wNFcaL7QHerYxNvmHrrDcAT1ws=; b=fj+NPJXJiyWpOx7lFAY0u/atMh8wvAnBKYzGJ4l5fxo9gcypgyElVihppb7Al/KuCygV7+ mo0IeMJWozfiVi0Yx1jXcjLfPiq8JyW3njPREqz2xzdRVgLeAGJSKNcHTNF4O7Edl6b+xW d2xL2ty4ykAFXhrJ3DhEmNPUG3Y7UYlUy7z7CJtSacwP699K6vQBpo248nuod8YbmFktSG Mfo7CTC8x6QlDr2cTkjKqs/LcgDtGbIuXS+Qnd9Y36lUIrL9ik99AusIlXfATlxjNJD5yf HcM7m9ToPOqsLZJqHLmL7KLD9XQ3H8HOcPP1D5Z1FDNKG82Kr8qxnNKaB/6qHQ== From: Michael Niedermayer <michael@niedermayer.cc> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org> Date: Fri, 28 Mar 2025 12:41:51 +0100 Message-ID: <20250328114151.918548-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.48.1 MIME-Version: 1.0 X-GND-State: clean X-GND-Score: -70 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddujeduvddtucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfghrlhcuvffnffculdeftddmnecujfgurhephffvufffkffoggfgsedtkeertdertddtnecuhfhrohhmpefoihgthhgrvghlucfpihgvuggvrhhmrgihvghruceomhhitghhrggvlhesnhhivgguvghrmhgrhigvrhdrtggtqeenucggtffrrghtthgvrhhnpefhledvvdehgfettdefueevtdeifffhkeeljeetjeeugfelgfffieduheevvefgvdenucfkphepgedurdeiiedrieejrdduudefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepgedurdeiiedrieejrdduudefpdhhvghloheplhhotggrlhhhohhsthdpmhgrihhlfhhrohhmpehmihgthhgrvghlsehnihgvuggvrhhmrgihvghrrdgttgdpnhgspghrtghpthhtohepuddprhgtphhtthhopehffhhmphgvghdquggvvhgvlhesfhhfmhhpvghgrdhorhhg X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH] avcodec/ffv1enc: Add -remap_optimizer option X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org> List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>, <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe> List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel> List-Post: <mailto:ffmpeg-devel@ffmpeg.org> List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help> List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>, <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe> Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org> Archived-At: <https://master.gitmailbox.com/ffmpegdev/20250328114151.918548-1-michael@niedermayer.cc/> List-Archive: <https://master.gitmailbox.com/ffmpegdev/> List-Post: <mailto:ffmpegdev@gitmailbox.com> This allows tuning how much effort (time) the encoder spends on optimizing the remap table Signed-off-by: Michael Niedermayer <michael@niedermayer.cc> --- doc/encoders.texi | 4 ++++ libavcodec/ffv1.h | 2 +- libavcodec/ffv1enc.c | 52 +++++++++++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/doc/encoders.texi b/doc/encoders.texi index f3fcc1aa60a..f987a234bc6 100644 --- a/doc/encoders.texi +++ b/doc/encoders.texi @@ -554,6 +554,10 @@ use 8bit default @item greater8bit use >8bit default @end table + +@item remap_optimizer +0 - 5, default 3, how much effort the encoder puts into optimizing the remap table. + @end table @anchor{flac} diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h index 09118e0b7dc..578b504c9f1 100644 --- a/libavcodec/ffv1.h +++ b/libavcodec/ffv1.h @@ -148,7 +148,7 @@ typedef struct FFV1Context { int colorspace; int flt; int remap_mode; - + int remap_optimizer; int use32bit; diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c index b156ff2dc10..a09b31218db 100644 --- a/libavcodec/ffv1enc.c +++ b/libavcodec/ffv1enc.c @@ -1369,33 +1369,46 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc, const uint8_t *src[4]) { int pixel_num = sc->slice_width * sc->slice_height; - int mul_count; + const int max_log2_mul_count = ((int[]){ 1, 1, 1, 9, 9, 10})[f->remap_optimizer]; + const int log2_mul_count_step = ((int[]){ 1, 1, 1, 9, 9, 1})[f->remap_optimizer]; + const int max_log2_mul = ((int[]){ 1, 8, 8, 9, 22, 22})[f->remap_optimizer]; + const int log2_mul_step = ((int[]){ 1, 8, 1, 1, 1, 1})[f->remap_optimizer]; + const int bruteforce_count = ((int[]){ 0, 0, 0, 1, 1, 1})[f->remap_optimizer]; + const int stair_mode = ((int[]){ 0, 0, 0, 1, 0, 0})[f->remap_optimizer]; av_assert0 (pixel_num <= 65536); for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) { - float score_sum[2] = {0}; - int mul_all[2][513]; + int best_log2_mul_count = 0; + float score_sum[11] = {0}; + int mul_all[11][1025]; - for (mul_count= 1; mul_count<=512; mul_count+=511) { - float score_tab_all[513][23] = {0}; + for (int log2_mul_count= 0; log2_mul_count <= max_log2_mul_count; log2_mul_count += log2_mul_count_step) { + float score_tab_all[1025][23] = {0}; int64_t last_val = -1; - int *mul_tab = mul_all[mul_count>>9]; + int *mul_tab = mul_all[log2_mul_count]; int last_mul_index = -1; - score_sum[mul_count>>9] += log2(mul_count); + int mul_count = 1 << log2_mul_count; + + score_sum[log2_mul_count] += log2_mul_count; for (int i= 0; i<pixel_num; i++) { int64_t val = sc->unit[p][i].val; int mul_index = (val + 1LL)*mul_count >> 32; if (val != last_val) { float *score_tab = score_tab_all[(last_val + 1LL)*mul_count >> 32]; av_assert2(last_val < val); - for(int si= 0; si < FF_ARRAY_ELEMS(*score_tab_all); si++) { + for(int si= 0; si <= max_log2_mul; si += log2_mul_step) { int64_t delta = val - last_val; int mul; int64_t cost; if (last_val < 0) { mul = 1; + } else if (stair_mode && mul_count == 512 && si == max_log2_mul ) { + if (mul_index >= 0x378/8 && mul_index <= 23 + 0x378/8) { + mul = (0x800080 >> (mul_index - 0x378/8)); + } else + mul = 1; } else { mul = (0x10001LL)<<si >> 16; } @@ -1414,20 +1427,29 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc, for(int i= 0; i<mul_count; i++) { int best_index = 0; float *score_tab = score_tab_all[i]; - for(int si= 1; si < FF_ARRAY_ELEMS(*score_tab_all); si++) { + for(int si= 0; si <= max_log2_mul; si += log2_mul_step) { if (score_tab[si] < score_tab[ best_index ]) best_index = si; } - mul_tab[i] = -((0x10001LL)<<best_index >> 16); - score_sum[mul_count>>9] += score_tab[ best_index ]; + if (stair_mode && mul_count == 512 && best_index == max_log2_mul ) { + if (i >= 0x378/8 && i <= 23 + 0x378/8) { + mul_tab[i] = -(0x800080 >> (i - 0x378/8)); + } else + mul_tab[i] = -1; + } else + mul_tab[i] = -((0x10001LL)<<best_index >> 16); + score_sum[log2_mul_count] += score_tab[ best_index ]; } mul_tab[mul_count] = 1; - score_sum[mul_count>>9] = encode_float32_remap_segment(sc, p, mul_count, mul_all[mul_count>>9], 0, 0); + if (bruteforce_count) + score_sum[log2_mul_count] = encode_float32_remap_segment(sc, p, mul_count, mul_all[log2_mul_count], 0, 0); + + if (score_sum[log2_mul_count] < score_sum[best_log2_mul_count]) + best_log2_mul_count = log2_mul_count; } - mul_count = score_sum[0] <= score_sum[1] ? 1 : 512; - encode_float32_remap_segment(sc, p, mul_count, mul_all[mul_count>>9], 1, 1); + encode_float32_remap_segment(sc, p, 1<<best_log2_mul_count, mul_all[best_log2_mul_count], 1, 1); } } @@ -1813,7 +1835,7 @@ static const AVOption options[] = { { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" }, { "flipdualrle", "Dual RLE", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" }, - + { "remap_optimizer", "Remap Optimizer", OFFSET(remap_optimizer), AV_OPT_TYPE_INT, { .i64 = 3 }, 0, 5, VE, .unit = "remap_optimizer" }, { NULL } }; -- 2.48.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".