annotate aacpsy.c @ 9696:d3b8845e7f55 libavcodec

Fix lowres with rv20. Fixes issue1102.
author michael
date Sat, 23 May 2009 22:59:35 +0000
parents e9d9d946f213
children d09283aeeef8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7606
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
1 /*
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
2 * AAC encoder psychoacoustic model
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
3 * Copyright (C) 2008 Konstantin Shishkov
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
4 *
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
5 * This file is part of FFmpeg.
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
6 *
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
11 *
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
16 *
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
20 */
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
21
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
22 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 7606
diff changeset
23 * @file libavcodec/aacpsy.c
7606
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
24 * AAC encoder psychoacoustic model
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
25 */
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
26
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
27 #include "avcodec.h"
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
28 #include "aacpsy.h"
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
29 #include "aactab.h"
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
30
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
31 /***********************************
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
32 * TODOs:
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
33 * General:
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
34 * better audio preprocessing (add DC highpass filter?)
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
35 * more psy models
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
36 * maybe improve coefficient quantization function in some way
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
37 *
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
38 * 3GPP-based psy model:
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
39 * thresholds linearization after their modifications for attaining given bitrate
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
40 * try other bitrate controlling mechanism (maybe use ratecontrol.c?)
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
41 * control quality for quality-based output
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
42 **********************************/
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
43
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
44 /**
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
45 * Quantize one coefficient.
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
46 * @return absolute value of the quantized coefficient
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
47 * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
48 */
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
49 static av_always_inline int quant(float coef, const float Q)
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
50 {
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
51 return av_clip((int)(pow(fabsf(coef) * Q, 0.75) + 0.4054), 0, 8191);
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
52 }
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
53
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
54 static inline float get_approximate_quant_error(float *c, int size, int scale_idx)
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
55 {
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
56 int i;
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
57 int q;
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
58 float coef, unquant, sum = 0.0f;
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
59 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
60 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
61 for(i = 0; i < size; i++){
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
62 coef = fabs(c[i]);
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
63 q = quant(c[i], Q);
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
64 unquant = (q * cbrt(q)) * IQ;
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
65 sum += (coef - unquant) * (coef - unquant);
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
66 }
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
67 return sum;
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
68 }
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
69
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
70 /**
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
71 * constants for 3GPP AAC psychoacoustic model
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
72 * @{
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
73 */
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
74 #define PSY_3GPP_SPREAD_LOW 1.5f // spreading factor for ascending threshold spreading (15 dB/Bark)
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
75 #define PSY_3GPP_SPREAD_HI 3.0f // spreading factor for descending threshold spreading (30 dB/Bark)
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
76 /**
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
77 * @}
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
78 */
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
79
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
80 /**
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
81 * information for single band used by 3GPP TS26.403-inspired psychoacoustic model
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
82 */
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
83 typedef struct Psy3gppBand{
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
84 float energy; ///< band energy
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
85 float ffac; ///< form factor
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
86 }Psy3gppBand;
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
87
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
88 /**
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
89 * psychoacoustic model frame type-dependent coefficients
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
90 */
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
91 typedef struct Psy3gppCoeffs{
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
92 float ath [64]; ///< absolute threshold of hearing per bands
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
93 float barks [64]; ///< Bark value for each spectral band in long frame
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
94 float spread_low[64]; ///< spreading factor for low-to-high threshold spreading in long frame
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
95 float spread_hi [64]; ///< spreading factor for high-to-low threshold spreading in long frame
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
96 }Psy3gppCoeffs;
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
97
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
98 /**
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
99 * Calculate Bark value for given line.
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
100 */
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
101 static inline float calc_bark(float f)
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
102 {
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
103 return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
104 }