From patchwork Fri Nov 20 07:33:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23917 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 58FBB44AE04 for ; Fri, 20 Nov 2020 09:56:16 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 748E268BE07; Fri, 20 Nov 2020 09:35:20 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f67.google.com (mail-ed1-f67.google.com [209.85.208.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 51C6E68BDC1 for ; Fri, 20 Nov 2020 09:35:09 +0200 (EET) Received: by mail-ed1-f67.google.com with SMTP id l5so8476713edq.11 for ; Thu, 19 Nov 2020 23:35:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=twLWwIfbquBffoL8p3adl3FdXlJZutOZvJ6CmtusWeU=; b=eaeM7Z0onIWTDQfWwleQmzfSvMIg0oIKyJSOU1zdkSeKDnQV4KVJHmqYXpPahGi471 dt69Bn17gnpdoMVYeXtf2TBFpdz/hgIolXq0k8a9xSN5Z7bdYlJyCKpg6GBZfChIldyu f2RYVJvWhLPxVtn0RD31Jtw+idXHHiBw9CjcFVNlNNwjhIMmmqojpPQ+4s9FW6Uf8xyJ AbOlhiMXkZXMxtXo41MQUn7GaNhr8MM2AwVrE5nvP1Ep5Ln3ES/yRTV/AxZ0IrVrk/am GCgHkVCc8YQpYoq3VT1jCauuAQ0WsnScu2YmU4nljVsltcIUe8Yl+HYNu4bY7hg1kcvE j6yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=twLWwIfbquBffoL8p3adl3FdXlJZutOZvJ6CmtusWeU=; b=OWjBwTxScnxBtL9Qh0McBpyk8GqVAx1VOXD2tXgfm0CVKQVYraRHRxtikkAKNrBA25 8YezRR74f/oMTPlxjzWcbyL5XibTkb8UDgT70/jHmsi0WAMeUKyf9O65DTMzU44Pv7cO 0FXV3zZEIKjsZZjshFgTU9LQHYT2P0YIWqTe3pbeg+bATsU6bfV34VwLeWD1U7wac3bC wmAE+5roVRNLp4/981Qzth0n0PNZvQpa5cfn+6k502VErv6wKqyEX8ftUgfioodE5Cxp 0eKROTAy+5h1H97qe2WHEUnTeBSDZkGS9GPCrBq5ytDWPPXOP3QAUJJ97dhCzFNzXqD2 o5gA== X-Gm-Message-State: AOAM533RMZ+UizeeLSr+iNHsaMr7Wt1WkKuIJ86GYI9vgU6dF0CzJuPA tFJN3S1ZZPEb2ukDVMK40CipXlJPsPVr9g== X-Google-Smtp-Source: ABdhPJyywBoL8w8IyfEeF37ego5ESNj3ms6fr84uSGWkqtlHw0zdc1nzly4HFMrTPOOkZ8Z9w2MbiQ== X-Received: by 2002:a50:d681:: with SMTP id r1mr422072edi.290.1605857708120; Thu, 19 Nov 2020 23:35:08 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id i13sm769110ejv.84.2020.11.19.23.35.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2020 23:35:07 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 20 Nov 2020 08:33:27 +0100 Message-Id: <20201120073327.820745-63-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201120072116.818090-1-andreas.rheinhardt@gmail.com> References: <20201120072116.818090-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 162/162] avcodec/mpegaudio_tablegen: Make exponential LUT shared X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Both the fixed as well as the floating point mpegaudio decoders use LUTs of type int8_t and uint32_t with 32K entries each; these tables are completely the same, yet they are not shared. This commit makes them shared. When both fixed as well as floating point decoders are enabled, this saves 160KiB from the bss segment for a normal build (translating into 160KiB less memory usage if both a shared as well as a floating point decoder have actually been used) and 160KiB from the binary for a build with hardcoded tables. It also means that the code to create said LUTs is no longer duplicated (for a normal build). Signed-off-by: Andreas Rheinhardt --- libavcodec/Makefile | 5 +- libavcodec/mpegaudio_tablegen.c | 2 - libavcodec/mpegaudio_tablegen.h | 22 +------ libavcodec/mpegaudiodata.h | 11 ++++ libavcodec/mpegaudiodec_common.c | 3 + libavcodec/mpegaudiodec_common_tablegen.c | 40 +++++++++++++ libavcodec/mpegaudiodec_common_tablegen.h | 72 +++++++++++++++++++++++ libavcodec/mpegaudiodec_template.c | 4 +- 8 files changed, 133 insertions(+), 26 deletions(-) create mode 100644 libavcodec/mpegaudiodec_common_tablegen.c create mode 100644 libavcodec/mpegaudiodec_common_tablegen.h diff --git a/libavcodec/Makefile b/libavcodec/Makefile index a851a0a3b2..4f6cc22034 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1238,6 +1238,7 @@ HOSTPROGS = aacps_tablegen \ dv_tablegen \ motionpixels_tablegen \ mpegaudio_tablegen \ + mpegaudiodec_common_tablegen \ pcm_tablegen \ qdm2_tablegen \ sinewin_tablegen \ @@ -1262,7 +1263,8 @@ endif GEN_HEADERS = cbrt_tables.h cbrt_fixed_tables.h aacps_tables.h aacps_fixed_tables.h \ dv_tables.h \ - sinewin_tables.h sinewin_fixed_tables.h mpegaudio_tables.h motionpixels_tables.h \ + sinewin_tables.h sinewin_fixed_tables.h mpegaudio_tables.h \ + mpegaudiodec_common_tables.h motionpixels_tables.h \ pcm_tables.h qdm2_tables.h GEN_HEADERS := $(addprefix $(SUBDIR), $(GEN_HEADERS)) @@ -1277,6 +1279,7 @@ $(SUBDIR)aacps_fixed.o: $(SUBDIR)aacps_fixed_tables.h $(SUBDIR)aactab_fixed.o: $(SUBDIR)aac_fixed_tables.h $(SUBDIR)dvenc.o: $(SUBDIR)dv_tables.h $(SUBDIR)motionpixels.o: $(SUBDIR)motionpixels_tables.h +$(SUBDIR)mpegaudiodec_common.o: $(SUBDIR)mpegaudiodec_common_tables.h $(SUBDIR)mpegaudiodec_fixed.o: $(SUBDIR)mpegaudio_tables.h $(SUBDIR)mpegaudiodec_float.o: $(SUBDIR)mpegaudio_tables.h $(SUBDIR)pcm.o: $(SUBDIR)pcm_tables.h diff --git a/libavcodec/mpegaudio_tablegen.c b/libavcodec/mpegaudio_tablegen.c index bff0f031a1..9721ae44e5 100644 --- a/libavcodec/mpegaudio_tablegen.c +++ b/libavcodec/mpegaudio_tablegen.c @@ -33,8 +33,6 @@ int main(void) write_fileheader(); - WRITE_ARRAY("static const", int8_t, table_4_3_exp); - WRITE_ARRAY("static const", uint32_t, table_4_3_value); WRITE_ARRAY("static const", uint32_t, exp_table_fixed); WRITE_ARRAY("static const", float, exp_table_float); WRITE_2D_ARRAY("static const", uint32_t, expval_table_fixed); diff --git a/libavcodec/mpegaudio_tablegen.h b/libavcodec/mpegaudio_tablegen.h index 1a92bb4de6..ed86257767 100644 --- a/libavcodec/mpegaudio_tablegen.h +++ b/libavcodec/mpegaudio_tablegen.h @@ -27,14 +27,10 @@ #include #include "libavutil/attributes.h" -#define TABLE_4_3_SIZE (8191 + 16)*4 #if CONFIG_HARDCODED_TABLES #define mpegaudio_tableinit() #include "libavcodec/mpegaudio_tables.h" #else -static int8_t table_4_3_exp[TABLE_4_3_SIZE]; -static uint32_t table_4_3_value[TABLE_4_3_SIZE]; - #if defined(BOTH_TABLES) || !USE_FLOATS #define FIXED_TABLE static uint32_t exp_table_fixed[512]; @@ -47,7 +43,6 @@ static float exp_table_float[512]; static float expval_table_float[512][16]; #endif -#define FRAC_BITS 23 #define IMDCT_SCALAR 1.759 static av_cold void mpegaudio_tableinit(void) @@ -62,25 +57,10 @@ static av_cold void mpegaudio_tableinit(void) double pow43_lut[16]; double exp2_base = 2.11758236813575084767080625169910490512847900390625e-22; // 2^(-72) double exp2_val; - double pow43_val = 0; + for (i = 0; i < 16; ++i) pow43_lut[i] = i * cbrt(i); - for (i = 1; i < TABLE_4_3_SIZE; i++) { - double f, fm; - int e, m; - double value = i / 4; - if ((i & 3) == 0) - pow43_val = value / IMDCT_SCALAR * cbrt(value); - f = pow43_val * exp2_lut[i & 3]; - fm = frexp(f, &e); - m = llrint(fm * (1LL << 31)); - e += FRAC_BITS - 31 + 5 - 100; - - /* normalized to FRAC_BITS */ - table_4_3_value[i] = m; - table_4_3_exp[i] = -e; - } for (exponent = 0; exponent < 512; exponent++) { if (exponent && (exponent & 3) == 0) exp2_base *= 2; diff --git a/libavcodec/mpegaudiodata.h b/libavcodec/mpegaudiodata.h index 01b1f88cd0..0a425ef6a8 100644 --- a/libavcodec/mpegaudiodata.h +++ b/libavcodec/mpegaudiodata.h @@ -29,6 +29,8 @@ #include +#include "config.h" + #include "internal.h" #include "vlc.h" @@ -42,6 +44,15 @@ extern const int ff_mpa_quant_steps[17]; extern const int ff_mpa_quant_bits[17]; extern const unsigned char * const ff_mpa_alloc_tables[5]; +#define TABLE_4_3_SIZE ((8191 + 16)*4) +#if CONFIG_HARDCODED_TABLES +extern const int8_t ff_table_4_3_exp [TABLE_4_3_SIZE]; +extern const uint32_t ff_table_4_3_value[TABLE_4_3_SIZE]; +#else +extern int8_t ff_table_4_3_exp [TABLE_4_3_SIZE]; +extern uint32_t ff_table_4_3_value[TABLE_4_3_SIZE]; +#endif + /* VLCs for decoding layer 3 huffman tables */ extern VLC ff_huff_vlc[16]; extern VLC ff_huff_quad_vlc[2]; diff --git a/libavcodec/mpegaudiodec_common.c b/libavcodec/mpegaudiodec_common.c index 4333746e9a..a963f6683a 100644 --- a/libavcodec/mpegaudiodec_common.c +++ b/libavcodec/mpegaudiodec_common.c @@ -32,6 +32,8 @@ #include "mpegaudiodata.h" +#include "mpegaudiodec_common_tablegen.h" + uint16_t ff_scale_factor_modshift[64]; static int16_t division_tab3[1 << 6 ]; @@ -469,6 +471,7 @@ static av_cold void mpegaudiodec_common_init_static(void) } } } + mpegaudiodec_common_tableinit(); } av_cold void ff_mpegaudiodec_common_init_static(void) diff --git a/libavcodec/mpegaudiodec_common_tablegen.c b/libavcodec/mpegaudiodec_common_tablegen.c new file mode 100644 index 0000000000..a319df65ab --- /dev/null +++ b/libavcodec/mpegaudiodec_common_tablegen.c @@ -0,0 +1,40 @@ +/* + * Generate a header file for hardcoded shared mpegaudiodec tables + * + * Copyright (c) 2009 Reimar Döffinger + * Copyright (c) 2020 Andreas Rheinhardt + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#define CONFIG_HARDCODED_TABLES 0 +#include "libavutil/tablegen.h" +#include "mpegaudiodec_common_tablegen.h" +#include "tableprint.h" + +int main(void) +{ + mpegaudiodec_common_tableinit(); + + write_fileheader(); + + WRITE_ARRAY("const", int8_t, ff_table_4_3_exp); + WRITE_ARRAY("const", uint32_t, ff_table_4_3_value); + + return 0; +} diff --git a/libavcodec/mpegaudiodec_common_tablegen.h b/libavcodec/mpegaudiodec_common_tablegen.h new file mode 100644 index 0000000000..bf402c9d84 --- /dev/null +++ b/libavcodec/mpegaudiodec_common_tablegen.h @@ -0,0 +1,72 @@ +/* + * Header file for hardcoded shared mpegaudiodec tables + * + * Copyright (c) 2009 Reimar Döffinger + * Copyright (c) 2020 Andreas Rheinhardt + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_MPEGAUDIODEC_COMMON_TABLEGEN_H +#define AVCODEC_MPEGAUDIODEC_COMMON_TABLEGEN_H + +#include + +#define TABLE_4_3_SIZE ((8191 + 16)*4) + +#if CONFIG_HARDCODED_TABLES +#define mpegaudiodec_common_tableinit() +#include "libavcodec/mpegaudiodec_common_tables.h" +#else +#include +#include "libavutil/attributes.h" + +int8_t ff_table_4_3_exp [TABLE_4_3_SIZE]; +uint32_t ff_table_4_3_value[TABLE_4_3_SIZE]; + +#define FRAC_BITS 23 +#define IMDCT_SCALAR 1.759 + +static av_cold void mpegaudiodec_common_tableinit(void) +{ + static const double exp2_lut[4] = { + 1.00000000000000000000, /* 2 ^ (0 * 0.25) */ + 1.18920711500272106672, /* 2 ^ (1 * 0.25) */ + M_SQRT2 , /* 2 ^ (2 * 0.25) */ + 1.68179283050742908606, /* 2 ^ (3 * 0.25) */ + }; + double pow43_val = 0; + + for (int i = 1; i < TABLE_4_3_SIZE; i++) { + double f, fm; + int e, m; + double value = i / 4; + if ((i & 3) == 0) + pow43_val = value / IMDCT_SCALAR * cbrt(value); + f = pow43_val * exp2_lut[i & 3]; + fm = frexp(f, &e); + m = llrint(fm * (1LL << 31)); + e += FRAC_BITS - 31 + 5 - 100; + + /* normalized to FRAC_BITS */ + ff_table_4_3_value[i] = m; + ff_table_4_3_exp [i] = -e; + } +} + +#endif /* CONFIG_HARDCODED_TABLES */ +#endif /* AVCODEC_MPEGAUDIODEC_COMMON_TABLEGEN_H */ diff --git a/libavcodec/mpegaudiodec_template.c b/libavcodec/mpegaudiodec_template.c index 1e8fd6064f..0ceeee2d46 100644 --- a/libavcodec/mpegaudiodec_template.c +++ b/libavcodec/mpegaudiodec_template.c @@ -219,8 +219,8 @@ static inline int l3_unscale(int value, int exponent) unsigned int m; int e; - e = table_4_3_exp [4 * value + (exponent & 3)]; - m = table_4_3_value[4 * value + (exponent & 3)]; + e = ff_table_4_3_exp [4 * value + (exponent & 3)]; + m = ff_table_4_3_value[4 * value + (exponent & 3)]; e -= exponent >> 2; #ifdef DEBUG if(e < 1)