From patchwork Mon Aug 1 12:23:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 37071 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:1649:b0:8b:613a:194d with SMTP id no9csp2470231pzb; Mon, 1 Aug 2022 05:25:16 -0700 (PDT) X-Google-Smtp-Source: AA6agR4xsk46WPubeynm19DlXtFzf4t2J+BzlGsRze68aKJUdnvdo6TiB8p6naZ8MA+Gpkpz/xHw X-Received: by 2002:aa7:db85:0:b0:43d:609b:2592 with SMTP id u5-20020aa7db85000000b0043d609b2592mr9365371edt.147.1659356715831; Mon, 01 Aug 2022 05:25:15 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id lk10-20020a170906cb0a00b00722668dbcacsi8530324ejb.575.2022.08.01.05.25.15; Mon, 01 Aug 2022 05:25:15 -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; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b=gasI9j3P; arc=fail (body hash mismatch); 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 332D268BA07; Mon, 1 Aug 2022 15:24:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-oln040092073017.outbound.protection.outlook.com [40.92.73.17]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8FE3468B9E5 for ; Mon, 1 Aug 2022 15:24:18 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=A05PBvmdsGEgKrLaVQo4EgttodVTxtQBSMBt2OoNaL4p5xK1/hGD9KV47f7lbQShrCcM3JZ2LTYZK0yIrsZU+6q5BYYYhRh9kJLuy/AyklOf2YeW4zidWo7FtzPlOBJldsYXQ3h1I3FjCu6b+GND1IPQEzIUUsypuUtZ4QUEdeQXPz9FjHYcnsWheYONV3z8CAUyr2R+ZTvQby8NwNuYxCBhhw7Bhb72NPTPYDS9Tze5cnRAni9ki2LWAtGtCEtx1UfYX4+VsSF31kRxNCPup7NWMCOgHSoNuQn1wL7N6GtIhk+qBEE9VeR8HPOT/HI4kTB5mqcb5VNdKTVGHuJFUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=aWiLIhWtO1pJ3jE+2T/wJYn56RvOaPRhx1B9E/CL0f0=; b=FYZBA9+/KeLQ6ASaXj6fFD/BiSuaOumJDeAYhGwY5K5Z1t5aJx2lHkLTyx6VzujbCWvmtmAlKf6sWCQfqqJR1jfvfPxh2arrjfRBeitD7NfaiRcQbq4X5wuy0yvdkrQQjWoLGp8eClle2WoNiyoLw4pO80ElILbatgUD3D016aYBngFF5UPmuT0XJeiJboyQ7J6/oXPZZsUFghUV/urxKTFpt+LgyY9quCnlNdxi7Sha+rJfBdbb54zVArgnb9ZHfsQWWtRnUIq1FQG0VGGj1vHa6PJdtvHgbD1hdzqQQO6leHebG0dcvpoG39yXsqtbnRL50WMOi7+VrNKPOAv1kw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aWiLIhWtO1pJ3jE+2T/wJYn56RvOaPRhx1B9E/CL0f0=; b=gasI9j3P7cNfvZYAzq4rG52wMoR5xGkU33Q5vW3eaCQ8Wi4QZTM36BRNVJlK4/dGqnGJlRGslKvKnNzk/XEnlN68FuE9xOpgAZSZJOeZz/sYqOlf86nOex6NHVSq72T0Bt9O6Kt07/z7j6AV27kBHMEx4aYlbcekypW43KMLuvk09PQLscmkAk/ArWOYTfjcyVOJ0GCCjsaGJjKgsX6QMuwkcphtW0JDMCL5wxD8vqadipaRPrcLex6776DcPPXbzxx3bAyvWBHt/4+/4bexj65naO1QcYEbGlb3W2N0wzZpqxZcd/VWlCVO1YJ284wQr0EfJ4bxEy6ycjyo8+dsdg== Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) by GV1PR01MB8529.eurprd01.prod.exchangelabs.com (2603:10a6:150:24::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5482.10; Mon, 1 Aug 2022 12:24:16 +0000 Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com ([fe80::210e:b627:bcc9:8c46]) by DB6PR0101MB2214.eurprd01.prod.exchangelabs.com ([fe80::210e:b627:bcc9:8c46%11]) with mapi id 15.20.5482.016; Mon, 1 Aug 2022 12:24:15 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 1 Aug 2022 14:23:47 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [9Eo70WDQn+G0Soaf3syCjC1gZsF81IsF] X-ClientProxiedBy: FR0P281CA0137.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:96::13) To DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) X-Microsoft-Original-Message-ID: <20220801122349.3996066-8-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 974a9d2b-6fb2-4d16-722a-08da73b8bfc3 X-MS-Exchange-SLBlob-MailProps: gjx25WM8ZNVakJMqjhoreY9eRj3hMzSf858kNP9/Egp3BLffYqKDkYfLdAtPrD9/vk5Z6D3VXRfEDOqWxvemguKqGkhTjxRs4LnBEfA97oETD9ilnc0OrwaC7wKP8IPQbDbihfLhLytfCUeLocwX4cJVWqqAtjl9DLjm93yCWYo0x9EfALyl6vUJK1SthUbA8SmGQqyoTSl1amQuAg6l+VL6gDrQ039gebpCqVUibhP+6AbuOR3zLG5fvjd9G5mv06btfW4PFDjstgpzNXIDIptwJgIlOoRKs4U0LHwKE2zTKvl/4DoTGIWzWNuLjicy2kdpLXWgKHo0PjoWS/+KdwJHJMv760aoOkmAWV/kjhuB3IJt0pnJsT0EEd9mYlAgmGIbJAY1IOvpX2klqRXc7jehhHt7nC9z3zuBm1b9gs9tY4vIThUHaGFgjHRGshjawpPr5c0LBLBfonQn4J7NHNgIeUhQ0nMmihtOpyPn4j5vyiMJB574WlfDz8KH+aXfS8DT4H/PceO76V9R62/aYzGDzFvSy7sNGlQrdVeBELzqP1vSTDY0IaaVgQKQIf7E1S8x2nJnR0jhIyQ2Xn4XeT3BwapWybXa341VpuCxr0AyXE0uZS5u1cE5P1XNSSM9GLJLuYvK4SwXrMHrCrTmO4jOfAiIa0VxB+/s4JJBpm9LN8D/DGPUHOQi5Ngrp4oyLfteVLXRV6Q623h++3mBMvNI6XEJlikRlp6gU2SjMes= X-MS-TrafficTypeDiagnostic: GV1PR01MB8529:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: c9c4pqlv8zSNbtxdO89qEGxGcBqgTSFxRFksKQRDdfnVUDrP6TQyBISWNodV3PnZVG9j9hdQ/9K0HgJc6JJlnc1RTfyXN3enNhzOz+GQUW05iJ1cd0O89S7MOurmSgUcH9Vl6ah3iLe/Dep0eE2ZR+ZYyKk6W68QfxaqlxmtdzxnkxR8/YmsXV6U35Mwcklic7jU/TP5gOb9MGAtb7mL/VAXSdEO1mm153wFyhv9DqH359udP5CKlq12YB6F9cysJm1QeTGDHLXTA7hkChiDywAJ9wJiIPO2uvhjOm3dOgBa8Tb9HTMrodYk9UknOhHfUWgef9KxZvL/ieDKB/63U6iGFONU4zLws8mhiHcHGYBPCFMonNIVVUJCbGCil5Dv5JdV+YmTyyfWtTZrIyPhsIHrZ4XgXGPKA7nSW1VxZqBWp1SBkN6YFaxEJ27LfdKXrEu2zpt1E2wkXXQV9Lwl1bgrRSqshB2B20blGttlBSZayZ8ut9f60DT0pnl0MbPpVL0JNYCJWVDWsahgxBaSvuq/LnmSs0LBM7gYXTB8NeG8ELyfQdcQ+Rz45YuSkAJUi6luJJZe8GV1zQDRaVpzmLSrP/mferWSx7LQwjfTXTeoOxdsqj/fZHCPu7enjobUTTHPRLnPsMUw58XA2RmvFA== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: M8my9PDMKyNvWf/hnSSzinquaVm+hAHv4keApzh8TH/aWINoAqIaAWKVlLrFPbGn3aLsosUoYS3/R2LIM8rWKotF/+SCsMjWYWAW2yEVdPse29wdAW3teXP2r2BdYUbqTXbydU2dDlPN3PpBbcWtwXsiir3ijxPzSa+uqPrUTHfmkabZ4EC5TgLVRSv+OHvGKClfoZmLhxdHSUfjcSKtObm7Y+/cpTO+fEhm9fRC42UfI33iBGwZRS87lGjQJyuUq31Gxlv4RwzeSEbVakQiS9/O8VeJ+4Vtguxgg6wIAIw3GWo4nS5gmsIC0m1UFsZQvjhmLF5kkjfJ5nJDVtU4TpYONRRb203g6kJ5elbRDJExAsmcDFkivY8OKc8+dYeoFVBGGGGR+jD9BCjO7wtR1ax3ndOdELiSm3475W4CYoa2RrdIzhX13vkS1P9rL/r8KJ4SZdZ5qgSjhsBZ4rTKUMQ1x+/x9qbMzmNm55XUHVg5LJh0+Gd3VTvVK4x3F1voe4M8r2YZbKcTNPPt26RfjKFG8N4zEvlXGVNVOgspn1QRPxFbIqtd4KTGPNSs4rA+hEOmfcaNATUeD1pf7UBen75IDXDmNhMI6OfbsHxku6Ulu4OTONTfB7EmA0K5IoukzjZJyTlyGixerG4o3pY2S5g28tt6/nW9/Vvb4lZVYe5T0pi5h/rbL2Ao0SAXvkVx2QVx+Yri1g2/vbHfbjeFRIlX/p66MPcAtXjHH8WSPj71FZO/wKmBtv4Uyv9yFTRPi8P7SXREuUfU7U0z7ipkerBkdbYW1uynZbX/PByC1EnOE3ra8vofd0Odj7i6TqQ6SzCWHxkFNg8XTIsmLqmEEyCXgOsFR0s/wRHKUuEQzoJ7cE+4YrKSueCPir7MHU3WxciJzL7S79PzhjtADZmnF/0OaCpm3SLSQH6+uIzxKBDe8DdRwzxSlO7vDz+hJ0SLDGRJxZGzRiD2TOLwTU4sD2qVh1Unx+klYe4XLcjqSyrZtLGzWEMOkDxe3/FXhX/ytZ9whJblIJB91Pw1DQEfYCdciCzyY221PkMKa9Rs/EroNRFHN4UggwLdF+zG7VYl6Q1wiNy7HNt+WK4jelKTVQ/+5rhFsGBgDa8ujnI/SxP+iElDCpntuFaElX/T9dptG7N7I9pymivn+gRq0VkwXIMVHswjgyHz98Fi1WqEl5bqHaSvy8z8utVu0rRxgIavzOiXCmbKhHWBy2penqIJ7ZogYNGYiz2BT3Q0IHy1JOXjxye7Mkm2/04kHI0Sa5flcRxxjqg7OoZrWs2WBo8GyCe2etvq31qzxyqmrNPNWR4= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 974a9d2b-6fb2-4d16-722a-08da73b8bfc3 X-MS-Exchange-CrossTenant-AuthSource: DB6PR0101MB2214.eurprd01.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Aug 2022 12:24:15.7748 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV1PR01MB8529 Subject: [FFmpeg-devel] [PATCH 09/11] avcodec/aacenc_quantization: Deduplicate quantization functions 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 8962QL/KO+Zd Up until now, there were four copies of quantize_and_encode_band_cost_(ZERO|[SU]QUAD|[SU]PAIR|ESC|NONE|NOISE|STEREO) (namely in aaccoder.o, aacenc_is.o, aacenc_ltp.o, aacenc_pred.o). As 43b378a0d321e3d01f196cec95e13acfac80d464 says, this is meant to enable more optimizations. Yet neither GCC nor Clang performed such optimizations: The functions in the aforementioned files are not optimized according to the specifics of the calls in the respective file. Therefore duplicating them is a waste of space; this commit therefore stops doing so. The remaining copy is placed into aaccoder.c (which is the only place where the "round to zero" variant of quantize_and_encode_band() is used, so that this can be completely internal to aaccoder.c). Signed-off-by: Andreas Rheinhardt --- libavcodec/aaccoder.c | 223 ++++++++++++++++++++++++++++ libavcodec/aacenc_pred.c | 18 +-- libavcodec/aacenc_quantization.h | 239 ++----------------------------- 3 files changed, 244 insertions(+), 236 deletions(-) diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c index 2988247a15..e3b6b2f02c 100644 --- a/libavcodec/aaccoder.c +++ b/libavcodec/aaccoder.c @@ -62,6 +62,229 @@ #include "libavcodec/aaccoder_trellis.h" +typedef float (*quantize_and_encode_band_func)(struct AACEncContext *s, PutBitContext *pb, + const float *in, float *quant, const float *scaled, + int size, int scale_idx, int cb, + const float lambda, const float uplim, + int *bits, float *energy); + +/** + * Calculate rate distortion cost for quantizing with given codebook + * + * @return quantization distortion + */ +static av_always_inline float quantize_and_encode_band_cost_template( + struct AACEncContext *s, + PutBitContext *pb, const float *in, float *out, + const float *scaled, int size, int scale_idx, + int cb, const float lambda, const float uplim, + int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED, + int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO, + const float ROUNDING) +{ + const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512; + const float Q = ff_aac_pow2sf_tab [q_idx]; + const float Q34 = ff_aac_pow34sf_tab[q_idx]; + const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; + const float CLIPPED_ESCAPE = 165140.0f*IQ; + float cost = 0; + float qenergy = 0; + const int dim = BT_PAIR ? 2 : 4; + int resbits = 0; + int off; + + if (BT_ZERO || BT_NOISE || BT_STEREO) { + for (int i = 0; i < size; i++) + cost += in[i]*in[i]; + if (bits) + *bits = 0; + if (energy) + *energy = qenergy; + if (out) { + for (int i = 0; i < size; i += dim) + for (int j = 0; j < dim; j++) + out[i+j] = 0.0f; + } + return cost * lambda; + } + if (!scaled) { + s->abs_pow34(s->scoefs, in, size); + scaled = s->scoefs; + } + s->quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING); + if (BT_UNSIGNED) { + off = 0; + } else { + off = aac_cb_maxval[cb]; + } + for (int i = 0; i < size; i += dim) { + const float *vec; + int *quants = s->qcoefs + i; + int curidx = 0; + int curbits; + float quantized, rd = 0.0f; + for (int j = 0; j < dim; j++) { + curidx *= aac_cb_range[cb]; + curidx += quants[j] + off; + } + curbits = ff_aac_spectral_bits[cb-1][curidx]; + vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; + if (BT_UNSIGNED) { + for (int j = 0; j < dim; j++) { + float t = fabsf(in[i+j]); + float di; + if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow + if (t >= CLIPPED_ESCAPE) { + quantized = CLIPPED_ESCAPE; + curbits += 21; + } else { + int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13); + quantized = c*cbrtf(c)*IQ; + curbits += av_log2(c)*2 - 4 + 1; + } + } else { + quantized = vec[j]*IQ; + } + di = t - quantized; + if (out) + out[i+j] = in[i+j] >= 0 ? quantized : -quantized; + if (vec[j] != 0.0f) + curbits++; + qenergy += quantized*quantized; + rd += di*di; + } + } else { + for (int j = 0; j < dim; j++) { + quantized = vec[j]*IQ; + qenergy += quantized*quantized; + if (out) + out[i+j] = quantized; + rd += (in[i+j] - quantized)*(in[i+j] - quantized); + } + } + cost += rd * lambda + curbits; + resbits += curbits; + if (cost >= uplim) + return uplim; + if (pb) { + put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]); + if (BT_UNSIGNED) + for (int j = 0; j < dim; j++) + if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f) + put_bits(pb, 1, in[i+j] < 0.0f); + if (BT_ESC) { + for (int j = 0; j < 2; j++) { + if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) { + int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13); + int len = av_log2(coef); + + put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); + put_sbits(pb, len, coef); + } + } + } + } + } + + if (bits) + *bits = resbits; + if (energy) + *energy = qenergy; + return cost; +} + +static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb, + const float *in, float *quant, const float *scaled, + int size, int scale_idx, int cb, + const float lambda, const float uplim, + int *bits, float *energy) { + av_assert0(0); + return 0.0f; +} + +#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \ +static float quantize_and_encode_band_cost_ ## NAME( \ + struct AACEncContext *s, \ + PutBitContext *pb, const float *in, float *quant, \ + const float *scaled, int size, int scale_idx, \ + int cb, const float lambda, const float uplim, \ + int *bits, float *energy) { \ + return quantize_and_encode_band_cost_template( \ + s, pb, in, quant, scaled, size, scale_idx, \ + BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \ + BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \ + ROUNDING); \ +} + +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD) +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD) +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD) +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD) +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD) +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD) +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO) +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD) +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD) + +static quantize_and_encode_band_func quantize_and_encode_band_cost_arr[] = +{ + quantize_and_encode_band_cost_ZERO, + quantize_and_encode_band_cost_SQUAD, + quantize_and_encode_band_cost_SQUAD, + quantize_and_encode_band_cost_UQUAD, + quantize_and_encode_band_cost_UQUAD, + quantize_and_encode_band_cost_SPAIR, + quantize_and_encode_band_cost_SPAIR, + quantize_and_encode_band_cost_UPAIR, + quantize_and_encode_band_cost_UPAIR, + quantize_and_encode_band_cost_UPAIR, + quantize_and_encode_band_cost_UPAIR, + quantize_and_encode_band_cost_ESC, + quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ + quantize_and_encode_band_cost_NOISE, + quantize_and_encode_band_cost_STEREO, + quantize_and_encode_band_cost_STEREO, +}; + +static quantize_and_encode_band_func quantize_and_encode_band_cost_rtz_arr[] = +{ + quantize_and_encode_band_cost_ZERO, + quantize_and_encode_band_cost_SQUAD, + quantize_and_encode_band_cost_SQUAD, + quantize_and_encode_band_cost_UQUAD, + quantize_and_encode_band_cost_UQUAD, + quantize_and_encode_band_cost_SPAIR, + quantize_and_encode_band_cost_SPAIR, + quantize_and_encode_band_cost_UPAIR, + quantize_and_encode_band_cost_UPAIR, + quantize_and_encode_band_cost_UPAIR, + quantize_and_encode_band_cost_UPAIR, + quantize_and_encode_band_cost_ESC_RTZ, + quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ + quantize_and_encode_band_cost_NOISE, + quantize_and_encode_band_cost_STEREO, + quantize_and_encode_band_cost_STEREO, +}; + +float ff_quantize_and_encode_band_cost(struct AACEncContext *s, PutBitContext *pb, + const float *in, float *quant, const float *scaled, + int size, int scale_idx, int cb, + const float lambda, const float uplim, + int *bits, float *energy) +{ + return quantize_and_encode_band_cost_arr[cb](s, pb, in, quant, scaled, size, + scale_idx, cb, lambda, uplim, + bits, energy); +} + +static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, + const float *in, float *out, int size, int scale_idx, + int cb, const float lambda, int rtz) +{ + (rtz ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb](s, pb, in, out, NULL, size, scale_idx, cb, + lambda, INFINITY, NULL, NULL); +} + /** * structure used in optimal codebook search */ diff --git a/libavcodec/aacenc_pred.c b/libavcodec/aacenc_pred.c index d111192f06..447444cb82 100644 --- a/libavcodec/aacenc_pred.c +++ b/libavcodec/aacenc_pred.c @@ -271,9 +271,9 @@ void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce) /* Normal coefficients */ s->abs_pow34(O34, &sce->coeffs[start_coef], num_coeffs); - dist1 = quantize_and_encode_band_cost(s, NULL, &sce->coeffs[start_coef], NULL, - O34, num_coeffs, sce->sf_idx[sfb], - cb_n, s->lambda / band->threshold, INFINITY, &cost1, NULL, 0); + dist1 = ff_quantize_and_encode_band_cost(s, NULL, &sce->coeffs[start_coef], NULL, + O34, num_coeffs, sce->sf_idx[sfb], + cb_n, s->lambda / band->threshold, INFINITY, &cost1, NULL); cost_coeffs += cost1; /* Encoded coefficients - needed for #bits, band type and quant. error */ @@ -284,9 +284,9 @@ void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce) cb_p = av_clip(find_min_book(find_max_val(1, num_coeffs, S34), sce->sf_idx[sfb]), cb_min, cb_max); else cb_p = cb_n; - quantize_and_encode_band_cost(s, NULL, SENT, QERR, S34, num_coeffs, - sce->sf_idx[sfb], cb_p, s->lambda / band->threshold, INFINITY, - &cost2, NULL, 0); + ff_quantize_and_encode_band_cost(s, NULL, SENT, QERR, S34, num_coeffs, + sce->sf_idx[sfb], cb_p, s->lambda / band->threshold, INFINITY, + &cost2, NULL); /* Reconstructed coefficients - needed for distortion measurements */ for (i = 0; i < num_coeffs; i++) @@ -296,9 +296,9 @@ void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce) cb_p = av_clip(find_min_book(find_max_val(1, num_coeffs, P34), sce->sf_idx[sfb]), cb_min, cb_max); else cb_p = cb_n; - dist2 = quantize_and_encode_band_cost(s, NULL, &sce->prcoeffs[start_coef], NULL, - P34, num_coeffs, sce->sf_idx[sfb], - cb_p, s->lambda / band->threshold, INFINITY, NULL, NULL, 0); + dist2 = ff_quantize_and_encode_band_cost(s, NULL, &sce->prcoeffs[start_coef], NULL, + P34, num_coeffs, sce->sf_idx[sfb], + cb_p, s->lambda / band->threshold, INFINITY, NULL, NULL); for (i = 0; i < num_coeffs; i++) dist_spec_err += (O34[i] - P34[i])*(O34[i] - P34[i]); dist_spec_err *= s->lambda / band->threshold; diff --git a/libavcodec/aacenc_quantization.h b/libavcodec/aacenc_quantization.h index f3c3553886..185430b0dd 100644 --- a/libavcodec/aacenc_quantization.h +++ b/libavcodec/aacenc_quantization.h @@ -28,232 +28,25 @@ #ifndef AVCODEC_AACENC_QUANTIZATION_H #define AVCODEC_AACENC_QUANTIZATION_H -#include "aactab.h" -#include "aacenc.h" -#include "aacenctab.h" -#include "aacenc_utils.h" - -/** - * Calculate rate distortion cost for quantizing with given codebook - * - * @return quantization distortion - */ -static av_always_inline float quantize_and_encode_band_cost_template( - struct AACEncContext *s, - PutBitContext *pb, const float *in, float *out, - const float *scaled, int size, int scale_idx, - int cb, const float lambda, const float uplim, - int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED, - int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO, - const float ROUNDING) -{ - const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512; - const float Q = ff_aac_pow2sf_tab [q_idx]; - const float Q34 = ff_aac_pow34sf_tab[q_idx]; - const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; - const float CLIPPED_ESCAPE = 165140.0f*IQ; - int i, j; - float cost = 0; - float qenergy = 0; - const int dim = BT_PAIR ? 2 : 4; - int resbits = 0; - int off; +#include - if (BT_ZERO || BT_NOISE || BT_STEREO) { - for (i = 0; i < size; i++) - cost += in[i]*in[i]; - if (bits) - *bits = 0; - if (energy) - *energy = qenergy; - if (out) { - for (i = 0; i < size; i += dim) - for (j = 0; j < dim; j++) - out[i+j] = 0.0f; - } - return cost * lambda; - } - if (!scaled) { - s->abs_pow34(s->scoefs, in, size); - scaled = s->scoefs; - } - s->quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING); - if (BT_UNSIGNED) { - off = 0; - } else { - off = aac_cb_maxval[cb]; - } - for (i = 0; i < size; i += dim) { - const float *vec; - int *quants = s->qcoefs + i; - int curidx = 0; - int curbits; - float quantized, rd = 0.0f; - for (j = 0; j < dim; j++) { - curidx *= aac_cb_range[cb]; - curidx += quants[j] + off; - } - curbits = ff_aac_spectral_bits[cb-1][curidx]; - vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; - if (BT_UNSIGNED) { - for (j = 0; j < dim; j++) { - float t = fabsf(in[i+j]); - float di; - if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow - if (t >= CLIPPED_ESCAPE) { - quantized = CLIPPED_ESCAPE; - curbits += 21; - } else { - int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13); - quantized = c*cbrtf(c)*IQ; - curbits += av_log2(c)*2 - 4 + 1; - } - } else { - quantized = vec[j]*IQ; - } - di = t - quantized; - if (out) - out[i+j] = in[i+j] >= 0 ? quantized : -quantized; - if (vec[j] != 0.0f) - curbits++; - qenergy += quantized*quantized; - rd += di*di; - } - } else { - for (j = 0; j < dim; j++) { - quantized = vec[j]*IQ; - qenergy += quantized*quantized; - if (out) - out[i+j] = quantized; - rd += (in[i+j] - quantized)*(in[i+j] - quantized); - } - } - cost += rd * lambda + curbits; - resbits += curbits; - if (cost >= uplim) - return uplim; - if (pb) { - put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]); - if (BT_UNSIGNED) - for (j = 0; j < dim; j++) - if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f) - put_bits(pb, 1, in[i+j] < 0.0f); - if (BT_ESC) { - for (j = 0; j < 2; j++) { - if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) { - int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13); - int len = av_log2(coef); - - put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); - put_sbits(pb, len, coef); - } - } - } - } - } - - if (bits) - *bits = resbits; - if (energy) - *energy = qenergy; - return cost; -} - -static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb, - const float *in, float *quant, const float *scaled, - int size, int scale_idx, int cb, - const float lambda, const float uplim, - int *bits, float *energy) { - av_assert0(0); - return 0.0f; -} - -#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \ -static float quantize_and_encode_band_cost_ ## NAME( \ - struct AACEncContext *s, \ - PutBitContext *pb, const float *in, float *quant, \ - const float *scaled, int size, int scale_idx, \ - int cb, const float lambda, const float uplim, \ - int *bits, float *energy) { \ - return quantize_and_encode_band_cost_template( \ - s, pb, in, quant, scaled, size, scale_idx, \ - BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \ - BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \ - ROUNDING); \ -} - -QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD) -QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD) -QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD) -QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD) -QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD) -QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD) -QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO) -QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD) -QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD) - -static float (*const quantize_and_encode_band_cost_arr[])( - struct AACEncContext *s, - PutBitContext *pb, const float *in, float *quant, - const float *scaled, int size, int scale_idx, - int cb, const float lambda, const float uplim, - int *bits, float *energy) = { - quantize_and_encode_band_cost_ZERO, - quantize_and_encode_band_cost_SQUAD, - quantize_and_encode_band_cost_SQUAD, - quantize_and_encode_band_cost_UQUAD, - quantize_and_encode_band_cost_UQUAD, - quantize_and_encode_band_cost_SPAIR, - quantize_and_encode_band_cost_SPAIR, - quantize_and_encode_band_cost_UPAIR, - quantize_and_encode_band_cost_UPAIR, - quantize_and_encode_band_cost_UPAIR, - quantize_and_encode_band_cost_UPAIR, - quantize_and_encode_band_cost_ESC, - quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ - quantize_and_encode_band_cost_NOISE, - quantize_and_encode_band_cost_STEREO, - quantize_and_encode_band_cost_STEREO, -}; +#include "aacenc.h" +#include "put_bits.h" -static float (*const quantize_and_encode_band_cost_rtz_arr[])( - struct AACEncContext *s, - PutBitContext *pb, const float *in, float *quant, - const float *scaled, int size, int scale_idx, - int cb, const float lambda, const float uplim, - int *bits, float *energy) = { - quantize_and_encode_band_cost_ZERO, - quantize_and_encode_band_cost_SQUAD, - quantize_and_encode_band_cost_SQUAD, - quantize_and_encode_band_cost_UQUAD, - quantize_and_encode_band_cost_UQUAD, - quantize_and_encode_band_cost_SPAIR, - quantize_and_encode_band_cost_SPAIR, - quantize_and_encode_band_cost_UPAIR, - quantize_and_encode_band_cost_UPAIR, - quantize_and_encode_band_cost_UPAIR, - quantize_and_encode_band_cost_UPAIR, - quantize_and_encode_band_cost_ESC_RTZ, - quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ - quantize_and_encode_band_cost_NOISE, - quantize_and_encode_band_cost_STEREO, - quantize_and_encode_band_cost_STEREO, -}; -#define quantize_and_encode_band_cost( \ - s, pb, in, quant, scaled, size, scale_idx, cb, \ - lambda, uplim, bits, energy, rtz) \ - ((rtz) ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb]( \ - s, pb, in, quant, scaled, size, scale_idx, cb, \ - lambda, uplim, bits, energy) +float ff_quantize_and_encode_band_cost(AACEncContext *s, PutBitContext *pb, + const float *in, float *quant, const float *scaled, + int size, int scale_idx, int cb, + const float lambda, const float uplim, + int *bits, float *energy); static inline float quantize_band_cost(struct AACEncContext *s, const float *in, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy) { - return quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx, - cb, lambda, uplim, bits, energy, 0); + return ff_quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx, + cb, lambda, uplim, bits, energy); } static inline int quantize_band_cost_bits(struct AACEncContext *s, const float *in, @@ -262,22 +55,14 @@ static inline int quantize_band_cost_bits(struct AACEncContext *s, const float * int *bits, float *energy) { int auxbits; - quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx, - cb, 0.0f, uplim, &auxbits, energy, 0); + ff_quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx, + cb, 0.0f, uplim, &auxbits, energy); if (bits) { *bits = auxbits; } return auxbits; } -static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, - const float *in, float *out, int size, int scale_idx, - int cb, const float lambda, int rtz) -{ - quantize_and_encode_band_cost(s, pb, in, out, NULL, size, scale_idx, cb, lambda, - INFINITY, NULL, NULL, rtz); -} - #include "aacenc_quantization_misc.h" #endif /* AVCODEC_AACENC_QUANTIZATION_H */