annotate aacpsy.c @ 12530:63edd10ad4bc libavcodec tip

Try to fix crashes introduced by r25218 r25218 made assumptions about the existence of past reference frames that weren't necessarily true.
author darkshikari
date Tue, 28 Sep 2010 09:06:22 +0000
parents 94b578d0af10
children
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 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 9944
diff changeset
23 * @file
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 "aactab.h"
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
29 #include "psymodel.h"
7606
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 * thresholds linearization after their modifications for attaining given bitrate
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
34 * try other bitrate controlling mechanism (maybe use ratecontrol.c?)
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
35 * control quality for quality-based output
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
36 **********************************/
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 /**
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
39 * constants for 3GPP AAC psychoacoustic model
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
40 * @{
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
41 */
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
42 #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
43 #define PSY_3GPP_SPREAD_HI 3.0f // spreading factor for descending threshold spreading (30 dB/Bark)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
44
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
45 #define PSY_3GPP_RPEMIN 0.01f
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
46 #define PSY_3GPP_RPELEV 2.0f
12408
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
47
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
48 /* LAME psy model constants */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
49 #define PSY_LAME_FIR_LEN 21 ///< LAME psy model FIR order
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
50 #define AAC_BLOCK_SIZE_LONG 1024 ///< long block size
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
51 #define AAC_BLOCK_SIZE_SHORT 128 ///< short block size
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
52 #define AAC_NUM_BLOCKS_SHORT 8 ///< number of blocks in a short sequence
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
53 #define PSY_LAME_NUM_SUBBLOCKS 3 ///< Number of sub-blocks in each short block
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
54
7606
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 * @}
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
57 */
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
58
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
59 /**
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
60 * 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
61 */
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
62 typedef struct AacPsyBand{
7606
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
63 float energy; ///< band energy
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
64 float ffac; ///< form factor
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
65 float thr; ///< energy threshold
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
66 float min_snr; ///< minimal SNR
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
67 float thr_quiet; ///< threshold in quiet
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
68 }AacPsyBand;
7606
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 /**
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
71 * single/pair channel context for psychoacoustic model
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
72 */
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
73 typedef struct AacPsyChannel{
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
74 AacPsyBand band[128]; ///< bands information
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
75 AacPsyBand prev_band[128]; ///< bands information from the previous frame
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
76
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
77 float win_energy; ///< sliding average of channel energy
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
78 float iir_state[2]; ///< hi-pass IIR filter state
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
79 uint8_t next_grouping; ///< stored grouping scheme for the next frame (in case of 8 short window sequence)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
80 enum WindowSequence next_window_seq; ///< window sequence to be used in the next frame
12408
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
81 /* LAME psy model specific members */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
82 float attack_threshold; ///< attack threshold for this channel
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
83 float prev_energy_subshort[AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
84 int prev_attack; ///< attack value for the last short block in the previous sequence
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
85 }AacPsyChannel;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
86
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
87 /**
7606
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
88 * psychoacoustic model frame type-dependent coefficients
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
89 */
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
90 typedef struct AacPsyCoeffs{
7606
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
91 float ath [64]; ///< absolute threshold of hearing per bands
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
92 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
93 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
94 float spread_hi [64]; ///< spreading factor for high-to-low threshold spreading in long frame
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
95 }AacPsyCoeffs;
7606
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
96
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
97 /**
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
98 * 3GPP TS26.403-inspired psychoacoustic model specific data
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
99 */
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
100 typedef struct AacPsyContext{
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
101 AacPsyCoeffs psy_coef[2];
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
102 AacPsyChannel *ch;
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
103 }AacPsyContext;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
104
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
105 /**
12408
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
106 * LAME psy model preset struct
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
107 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
108 typedef struct {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
109 int quality; ///< Quality to map the rest of the vaules to.
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
110 /* This is overloaded to be both kbps per channel in ABR mode, and
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
111 * requested quality in constant quality mode.
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
112 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
113 float st_lrm; ///< short threshold for L, R, and M channels
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
114 } PsyLamePreset;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
115
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
116 /**
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
117 * LAME psy model preset table for ABR
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
118 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
119 static const PsyLamePreset psy_abr_map[] = {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
120 /* TODO: Tuning. These were taken from LAME. */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
121 /* kbps/ch st_lrm */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
122 { 8, 6.60},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
123 { 16, 6.60},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
124 { 24, 6.60},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
125 { 32, 6.60},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
126 { 40, 6.60},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
127 { 48, 6.60},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
128 { 56, 6.60},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
129 { 64, 6.40},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
130 { 80, 6.00},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
131 { 96, 5.60},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
132 {112, 5.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
133 {128, 5.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
134 {160, 5.20}
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
135 };
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
136
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
137 /**
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
138 * LAME psy model preset table for constant quality
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
139 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
140 static const PsyLamePreset psy_vbr_map[] = {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
141 /* vbr_q st_lrm */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
142 { 0, 4.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
143 { 1, 4.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
144 { 2, 4.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
145 { 3, 4.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
146 { 4, 4.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
147 { 5, 4.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
148 { 6, 4.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
149 { 7, 4.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
150 { 8, 4.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
151 { 9, 4.20},
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
152 {10, 4.20}
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
153 };
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
154
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
155 /**
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
156 * LAME psy model FIR coefficient table
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
157 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
158 static const float psy_fir_coeffs[] = {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
159 -8.65163e-18 * 2, -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
160 -3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2, 0.0931738 * 2,
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
161 -5.52212e-17 * 2, -0.313819 * 2
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
162 };
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
163
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
164 /**
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
165 * calculates the attack threshold for ABR from the above table for the LAME psy model
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
166 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
167 static float lame_calc_attack_threshold(int bitrate)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
168 {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
169 /* Assume max bitrate to start with */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
170 int lower_range = 12, upper_range = 12;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
171 int lower_range_kbps = psy_abr_map[12].quality;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
172 int upper_range_kbps = psy_abr_map[12].quality;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
173 int i;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
174
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
175 /* Determine which bitrates the value specified falls between.
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
176 * If the loop ends without breaking our above assumption of 320kbps was correct.
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
177 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
178 for (i = 1; i < 13; i++) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
179 if (FFMAX(bitrate, psy_abr_map[i].quality) != bitrate) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
180 upper_range = i;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
181 upper_range_kbps = psy_abr_map[i ].quality;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
182 lower_range = i - 1;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
183 lower_range_kbps = psy_abr_map[i - 1].quality;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
184 break; /* Upper range found */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
185 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
186 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
187
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
188 /* Determine which range the value specified is closer to */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
189 if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps))
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
190 return psy_abr_map[lower_range].st_lrm;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
191 return psy_abr_map[upper_range].st_lrm;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
192 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
193
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
194 /**
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
195 * LAME psy model specific initialization
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
196 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
197 static void lame_window_init(AacPsyContext *ctx, AVCodecContext *avctx) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
198 int i;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
199
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
200 for (i = 0; i < avctx->channels; i++) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
201 AacPsyChannel *pch = &ctx->ch[i];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
202
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
203 if (avctx->flags & CODEC_FLAG_QSCALE)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
204 pch->attack_threshold = psy_vbr_map[avctx->global_quality / FF_QP2LAMBDA].st_lrm;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
205 else
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
206 pch->attack_threshold = lame_calc_attack_threshold(avctx->bit_rate / avctx->channels / 1000);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
207
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
208 for (i = 0; i < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; i++)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
209 pch->prev_energy_subshort[i] = 10.0f;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
210 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
211 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
212
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
213 /**
7606
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
214 * Calculate Bark value for given line.
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
215 */
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
216 static av_cold float calc_bark(float f)
7606
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
217 {
e24539743ed8 Add okayed chunks of AAC encoder psychoacoustic model
kostya
parents:
diff changeset
218 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
219 }
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
220
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
221 #define ATH_ADD 4
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
222 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
223 * Calculate ATH value for given frequency.
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
224 * Borrowed from Lame.
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
225 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
226 static av_cold float ath(float f, float add)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
227 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
228 f /= 1000.0f;
9937
3e39dbd2d9eb cosmetics: prettyprinting, K&R style, break overly long lines
diego
parents: 9936
diff changeset
229 return 3.64 * pow(f, -0.8)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
230 - 6.8 * exp(-0.6 * (f - 3.4) * (f - 3.4))
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
231 + 6.0 * exp(-0.15 * (f - 8.7) * (f - 8.7))
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
232 + (0.6 + 0.04 * add) * 0.001 * f * f * f * f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
233 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
234
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
235 static av_cold int psy_3gpp_init(FFPsyContext *ctx) {
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
236 AacPsyContext *pctx;
12159
25ac974ce96d aacenc: psy_3gpp_init(): Calculate barks on demand.
alexc
parents: 11998
diff changeset
237 float bark;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
238 int i, j, g, start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
239 float prev, minscale, minath;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
240
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
241 ctx->model_priv_data = av_mallocz(sizeof(AacPsyContext));
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
242 pctx = (AacPsyContext*) ctx->model_priv_data;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
243
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
244 minath = ath(3410, ATH_ADD);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
245 for (j = 0; j < 2; j++) {
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
246 AacPsyCoeffs *coeffs = &pctx->psy_coef[j];
12162
f948f8cf97db aacenc: psy_3gpp_init(): Fix line_to_frequency for short windows.
alexc
parents: 12161
diff changeset
247 float line_to_frequency = ctx->avctx->sample_rate / (j ? 256.f : 2048.0f);
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
248 i = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
249 prev = 0.0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
250 for (g = 0; g < ctx->num_bands[j]; g++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
251 i += ctx->bands[j][g];
12161
d34f0f3ac678 aacenc: aac_psy_init(): Factorize line_to_frequency.
alexc
parents: 12160
diff changeset
252 bark = calc_bark((i-1) * line_to_frequency);
12159
25ac974ce96d aacenc: psy_3gpp_init(): Calculate barks on demand.
alexc
parents: 11998
diff changeset
253 coeffs->barks[g] = (bark + prev) / 2.0;
25ac974ce96d aacenc: psy_3gpp_init(): Calculate barks on demand.
alexc
parents: 11998
diff changeset
254 prev = bark;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
255 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
256 for (g = 0; g < ctx->num_bands[j] - 1; g++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
257 coeffs->spread_low[g] = pow(10.0, -(coeffs->barks[g+1] - coeffs->barks[g]) * PSY_3GPP_SPREAD_LOW);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
258 coeffs->spread_hi [g] = pow(10.0, -(coeffs->barks[g+1] - coeffs->barks[g]) * PSY_3GPP_SPREAD_HI);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
259 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
260 start = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
261 for (g = 0; g < ctx->num_bands[j]; g++) {
12161
d34f0f3ac678 aacenc: aac_psy_init(): Factorize line_to_frequency.
alexc
parents: 12160
diff changeset
262 minscale = ath(start * line_to_frequency, ATH_ADD);
9938
6c1ac45b3097 cosmetics: Remove unnecessary {} around if/for blocks;
diego
parents: 9937
diff changeset
263 for (i = 1; i < ctx->bands[j][g]; i++)
12161
d34f0f3ac678 aacenc: aac_psy_init(): Factorize line_to_frequency.
alexc
parents: 12160
diff changeset
264 minscale = FFMIN(minscale, ath((start + i) * line_to_frequency, ATH_ADD));
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
265 coeffs->ath[g] = minscale - minath;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
266 start += ctx->bands[j][g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
267 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
268 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
269
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
270 pctx->ch = av_mallocz(sizeof(AacPsyChannel) * ctx->avctx->channels);
12408
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
271
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
272 lame_window_init(pctx, ctx->avctx);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
273
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
274 return 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
275 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
276
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
277 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
278 * IIR filter used in block switching decision
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
279 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
280 static float iir_filter(int in, float state[2])
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
281 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
282 float ret;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
283
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
284 ret = 0.7548f * (in - state[0]) + 0.5095f * state[1];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
285 state[0] = in;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
286 state[1] = ret;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
287 return ret;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
288 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
289
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
290 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
291 * window grouping information stored as bits (0 - new group, 1 - group continues)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
292 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
293 static const uint8_t window_grouping[9] = {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
294 0xB6, 0x6C, 0xD8, 0xB2, 0x66, 0xC6, 0x96, 0x36, 0x36
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
295 };
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
296
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
297 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
298 * Tell encoder which window types to use.
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
299 * @see 3GPP TS26.403 5.4.1 "Blockswitching"
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
300 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
301 static FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
302 const int16_t *audio, const int16_t *la,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
303 int channel, int prev_type)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
304 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
305 int i, j;
9937
3e39dbd2d9eb cosmetics: prettyprinting, K&R style, break overly long lines
diego
parents: 9936
diff changeset
306 int br = ctx->avctx->bit_rate / ctx->avctx->channels;
3e39dbd2d9eb cosmetics: prettyprinting, K&R style, break overly long lines
diego
parents: 9936
diff changeset
307 int attack_ratio = br <= 16000 ? 18 : 10;
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
308 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
309 AacPsyChannel *pch = &pctx->ch[channel];
9937
3e39dbd2d9eb cosmetics: prettyprinting, K&R style, break overly long lines
diego
parents: 9936
diff changeset
310 uint8_t grouping = 0;
11998
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
311 int next_type = pch->next_window_seq;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
312 FFPsyWindowInfo wi;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
313
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
314 memset(&wi, 0, sizeof(wi));
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
315 if (la) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
316 float s[8], v;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
317 int switch_to_eight = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
318 float sum = 0.0, sum2 = 0.0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
319 int attack_n = 0;
11998
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
320 int stay_short = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
321 for (i = 0; i < 8; i++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
322 for (j = 0; j < 128; j++) {
11728
907ac02ef561 aacenc: Fix psy logic.
alexc
parents: 11644
diff changeset
323 v = iir_filter(la[(i*128+j)*ctx->avctx->channels], pch->iir_state);
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
324 sum += v*v;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
325 }
9937
3e39dbd2d9eb cosmetics: prettyprinting, K&R style, break overly long lines
diego
parents: 9936
diff changeset
326 s[i] = sum;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
327 sum2 += sum;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
328 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
329 for (i = 0; i < 8; i++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
330 if (s[i] > pch->win_energy * attack_ratio) {
9937
3e39dbd2d9eb cosmetics: prettyprinting, K&R style, break overly long lines
diego
parents: 9936
diff changeset
331 attack_n = i + 1;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
332 switch_to_eight = 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
333 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
334 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
335 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
336 pch->win_energy = pch->win_energy*7/8 + sum2/64;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
337
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
338 wi.window_type[1] = prev_type;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
339 switch (prev_type) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
340 case ONLY_LONG_SEQUENCE:
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
341 wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
11998
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
342 next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
343 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
344 case LONG_START_SEQUENCE:
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
345 wi.window_type[0] = EIGHT_SHORT_SEQUENCE;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
346 grouping = pch->next_grouping;
11998
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
347 next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
348 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
349 case LONG_STOP_SEQUENCE:
11998
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
350 wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
351 next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
352 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
353 case EIGHT_SHORT_SEQUENCE:
11998
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
354 stay_short = next_type == EIGHT_SHORT_SEQUENCE || switch_to_eight;
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
355 wi.window_type[0] = stay_short ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
356 grouping = next_type == EIGHT_SHORT_SEQUENCE ? pch->next_grouping : 0;
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
357 next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
358 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
359 }
11998
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
360
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
361 pch->next_grouping = window_grouping[attack_n];
11998
2e7db647fef8 aacenc: Fix window decision logic.
alexc
parents: 11728
diff changeset
362 pch->next_window_seq = next_type;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
363 } else {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
364 for (i = 0; i < 3; i++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
365 wi.window_type[i] = prev_type;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
366 grouping = (prev_type == EIGHT_SHORT_SEQUENCE) ? window_grouping[0] : 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
367 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
368
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
369 wi.window_shape = 1;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
370 if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
371 wi.num_windows = 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
372 wi.grouping[0] = 1;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
373 } else {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
374 int lastgrp = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
375 wi.num_windows = 8;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
376 for (i = 0; i < 8; i++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
377 if (!((grouping >> i) & 1))
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
378 lastgrp = i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
379 wi.grouping[lastgrp]++;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
380 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
381 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
382
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
383 return wi;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
384 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
385
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
386 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
387 * Calculate band thresholds as suggested in 3GPP TS26.403
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
388 */
9937
3e39dbd2d9eb cosmetics: prettyprinting, K&R style, break overly long lines
diego
parents: 9936
diff changeset
389 static void psy_3gpp_analyze(FFPsyContext *ctx, int channel,
12442
94b578d0af10 psymodel: Const correct FFPsyWindowInfo.
alexc
parents: 12408
diff changeset
390 const float *coefs, const FFPsyWindowInfo *wi)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
391 {
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
392 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
393 AacPsyChannel *pch = &pctx->ch[channel];
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
394 int start = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
395 int i, w, g;
9937
3e39dbd2d9eb cosmetics: prettyprinting, K&R style, break overly long lines
diego
parents: 9936
diff changeset
396 const int num_bands = ctx->num_bands[wi->num_windows == 8];
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
397 const uint8_t* band_sizes = ctx->bands[wi->num_windows == 8];
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
398 AacPsyCoeffs *coeffs = &pctx->psy_coef[wi->num_windows == 8];
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
399
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
400 //calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation"
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
401 for (w = 0; w < wi->num_windows*16; w += 16) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
402 for (g = 0; g < num_bands; g++) {
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
403 AacPsyBand *band = &pch->band[w+g];
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
404 band->energy = 0.0f;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
405 for (i = 0; i < band_sizes[g]; i++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
406 band->energy += coefs[start+i] * coefs[start+i];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
407 band->energy *= 1.0f / (512*512);
9937
3e39dbd2d9eb cosmetics: prettyprinting, K&R style, break overly long lines
diego
parents: 9936
diff changeset
408 band->thr = band->energy * 0.001258925f;
3e39dbd2d9eb cosmetics: prettyprinting, K&R style, break overly long lines
diego
parents: 9936
diff changeset
409 start += band_sizes[g];
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
410
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
411 ctx->psy_bands[channel*PSY_MAX_BANDS+w+g].energy = band->energy;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
412 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
413 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
414 //modify thresholds - spread, threshold in quiet - 5.4.3 "Spreaded Energy Calculation"
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
415 for (w = 0; w < wi->num_windows*16; w += 16) {
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
416 AacPsyBand *band = &pch->band[w];
9938
6c1ac45b3097 cosmetics: Remove unnecessary {} around if/for blocks;
diego
parents: 9937
diff changeset
417 for (g = 1; g < num_bands; g++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
418 band[g].thr = FFMAX(band[g].thr, band[g-1].thr * coeffs->spread_low[g-1]);
9938
6c1ac45b3097 cosmetics: Remove unnecessary {} around if/for blocks;
diego
parents: 9937
diff changeset
419 for (g = num_bands - 2; g >= 0; g--)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
420 band[g].thr = FFMAX(band[g].thr, band[g+1].thr * coeffs->spread_hi [g]);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
421 for (g = 0; g < num_bands; g++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
422 band[g].thr_quiet = FFMAX(band[g].thr, coeffs->ath[g]);
9938
6c1ac45b3097 cosmetics: Remove unnecessary {} around if/for blocks;
diego
parents: 9937
diff changeset
423 if (wi->num_windows != 8 && wi->window_type[1] != EIGHT_SHORT_SEQUENCE)
9944
c5ca5e520fe1 Change fminf/fmaxf to FFMIN/FFMAX to fix the build on broken operating systems.
alexc
parents: 9938
diff changeset
424 band[g].thr_quiet = FFMAX(PSY_3GPP_RPEMIN*band[g].thr_quiet,
c5ca5e520fe1 Change fminf/fmaxf to FFMIN/FFMAX to fix the build on broken operating systems.
alexc
parents: 9938
diff changeset
425 FFMIN(band[g].thr_quiet,
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
426 PSY_3GPP_RPELEV*pch->prev_band[w+g].thr_quiet));
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
427 band[g].thr = FFMAX(band[g].thr, band[g].thr_quiet * 0.25);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
428
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
429 ctx->psy_bands[channel*PSY_MAX_BANDS+w+g].threshold = band[g].thr;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
430 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
431 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
432 memcpy(pch->prev_band, pch->band, sizeof(pch->band));
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
433 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
434
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
435 static av_cold void psy_3gpp_end(FFPsyContext *apc)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
436 {
12406
b25537518e40 aacenc: Rename Psy3gpp* structs to AacPsy*
alexc
parents: 12162
diff changeset
437 AacPsyContext *pctx = (AacPsyContext*) apc->model_priv_data;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
438 av_freep(&pctx->ch);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
439 av_freep(&apc->model_priv_data);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
440 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
441
12408
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
442 static void lame_apply_block_type(AacPsyChannel *ctx, FFPsyWindowInfo *wi, int uselongblock)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
443 {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
444 int blocktype = ONLY_LONG_SEQUENCE;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
445 if (uselongblock) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
446 if (ctx->next_window_seq == EIGHT_SHORT_SEQUENCE)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
447 blocktype = LONG_STOP_SEQUENCE;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
448 } else {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
449 blocktype = EIGHT_SHORT_SEQUENCE;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
450 if (ctx->next_window_seq == ONLY_LONG_SEQUENCE)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
451 ctx->next_window_seq = LONG_START_SEQUENCE;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
452 if (ctx->next_window_seq == LONG_STOP_SEQUENCE)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
453 ctx->next_window_seq = EIGHT_SHORT_SEQUENCE;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
454 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
455
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
456 wi->window_type[0] = ctx->next_window_seq;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
457 ctx->next_window_seq = blocktype;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
458 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
459
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
460 static FFPsyWindowInfo psy_lame_window(FFPsyContext *ctx,
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
461 const int16_t *audio, const int16_t *la,
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
462 int channel, int prev_type)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
463 {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
464 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
465 AacPsyChannel *pch = &pctx->ch[channel];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
466 int grouping = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
467 int uselongblock = 1;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
468 int attacks[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
469 int i;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
470 FFPsyWindowInfo wi;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
471
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
472 memset(&wi, 0, sizeof(wi));
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
473 if (la) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
474 float hpfsmpl[AAC_BLOCK_SIZE_LONG];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
475 float const *pf = hpfsmpl;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
476 float attack_intensity[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
477 float energy_subshort[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
478 float energy_short[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
479 int chans = ctx->avctx->channels;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
480 const int16_t *firbuf = la + (AAC_BLOCK_SIZE_SHORT/4 - PSY_LAME_FIR_LEN) * chans;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
481 int j, att_sum = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
482
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
483 /* LAME comment: apply high pass filter of fs/4 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
484 for (i = 0; i < AAC_BLOCK_SIZE_LONG; i++) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
485 float sum1, sum2;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
486 sum1 = firbuf[(i + ((PSY_LAME_FIR_LEN - 1) / 2)) * chans];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
487 sum2 = 0.0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
488 for (j = 0; j < ((PSY_LAME_FIR_LEN - 1) / 2) - 1; j += 2) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
489 sum1 += psy_fir_coeffs[j] * (firbuf[(i + j) * chans] + firbuf[(i + PSY_LAME_FIR_LEN - j) * chans]);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
490 sum2 += psy_fir_coeffs[j + 1] * (firbuf[(i + j + 1) * chans] + firbuf[(i + PSY_LAME_FIR_LEN - j - 1) * chans]);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
491 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
492 hpfsmpl[i] = sum1 + sum2;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
493 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
494
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
495 /* Calculate the energies of each sub-shortblock */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
496 for (i = 0; i < PSY_LAME_NUM_SUBBLOCKS; i++) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
497 energy_subshort[i] = pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 1) * PSY_LAME_NUM_SUBBLOCKS)];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
498 assert(pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 2) * PSY_LAME_NUM_SUBBLOCKS + 1)] > 0);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
499 attack_intensity[i] = energy_subshort[i] / pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 2) * PSY_LAME_NUM_SUBBLOCKS + 1)];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
500 energy_short[0] += energy_subshort[i];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
501 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
502
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
503 for (i = 0; i < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; i++) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
504 float const *const pfe = pf + AAC_BLOCK_SIZE_LONG / (AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
505 float p = 1.0f;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
506 for (; pf < pfe; pf++)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
507 if (p < fabsf(*pf))
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
508 p = fabsf(*pf);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
509 pch->prev_energy_subshort[i] = energy_subshort[i + PSY_LAME_NUM_SUBBLOCKS] = p;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
510 energy_short[1 + i / PSY_LAME_NUM_SUBBLOCKS] += p;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
511 /* FIXME: The indexes below are [i + 3 - 2] in the LAME source.
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
512 * Obviously the 3 and 2 have some significance, or this would be just [i + 1]
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
513 * (which is what we use here). What the 3 stands for is ambigious, as it is both
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
514 * number of short blocks, and the number of sub-short blocks.
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
515 * It seems that LAME is comparing each sub-block to sub-block + 1 in the
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
516 * previous block.
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
517 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
518 if (p > energy_subshort[i + 1])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
519 p = p / energy_subshort[i + 1];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
520 else if (energy_subshort[i + 1] > p * 10.0f)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
521 p = energy_subshort[i + 1] / (p * 10.0f);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
522 else
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
523 p = 0.0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
524 attack_intensity[i + PSY_LAME_NUM_SUBBLOCKS] = p;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
525 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
526
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
527 /* compare energy between sub-short blocks */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
528 for (i = 0; i < (AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS; i++)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
529 if (!attacks[i / PSY_LAME_NUM_SUBBLOCKS])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
530 if (attack_intensity[i] > pch->attack_threshold)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
531 attacks[i / PSY_LAME_NUM_SUBBLOCKS] = (i % PSY_LAME_NUM_SUBBLOCKS) + 1;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
532
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
533 /* should have energy change between short blocks, in order to avoid periodic signals */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
534 /* Good samples to show the effect are Trumpet test songs */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
535 /* GB: tuned (1) to avoid too many short blocks for test sample TRUMPET */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
536 /* RH: tuned (2) to let enough short blocks through for test sample FSOL and SNAPS */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
537 for (i = 1; i < AAC_NUM_BLOCKS_SHORT + 1; i++) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
538 float const u = energy_short[i - 1];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
539 float const v = energy_short[i];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
540 float const m = FFMAX(u, v);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
541 if (m < 40000) { /* (2) */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
542 if (u < 1.7f * v && v < 1.7f * u) { /* (1) */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
543 if (i == 1 && attacks[0] < attacks[i])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
544 attacks[0] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
545 attacks[i] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
546 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
547 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
548 att_sum += attacks[i];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
549 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
550
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
551 if (attacks[0] <= pch->prev_attack)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
552 attacks[0] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
553
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
554 att_sum += attacks[0];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
555 /* 3 below indicates the previous attack happened in the last sub-block of the previous sequence */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
556 if (pch->prev_attack == 3 || att_sum) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
557 uselongblock = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
558
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
559 if (attacks[1] && attacks[0])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
560 attacks[1] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
561 if (attacks[2] && attacks[1])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
562 attacks[2] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
563 if (attacks[3] && attacks[2])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
564 attacks[3] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
565 if (attacks[4] && attacks[3])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
566 attacks[4] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
567 if (attacks[5] && attacks[4])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
568 attacks[5] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
569 if (attacks[6] && attacks[5])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
570 attacks[6] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
571 if (attacks[7] && attacks[6])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
572 attacks[7] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
573 if (attacks[8] && attacks[7])
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
574 attacks[8] = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
575 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
576 } else {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
577 /* We have no lookahead info, so just use same type as the previous sequence. */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
578 uselongblock = !(prev_type == EIGHT_SHORT_SEQUENCE);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
579 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
580
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
581 lame_apply_block_type(pch, &wi, uselongblock);
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
582
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
583 wi.window_type[1] = prev_type;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
584 if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
585 wi.num_windows = 1;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
586 wi.grouping[0] = 1;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
587 if (wi.window_type[0] == LONG_START_SEQUENCE)
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
588 wi.window_shape = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
589 else
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
590 wi.window_shape = 1;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
591 } else {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
592 int lastgrp = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
593
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
594 wi.num_windows = 8;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
595 wi.window_shape = 0;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
596 for (i = 0; i < 8; i++) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
597 if (!((pch->next_grouping >> i) & 1))
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
598 lastgrp = i;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
599 wi.grouping[lastgrp]++;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
600 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
601 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
602
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
603 /* Determine grouping, based on the location of the first attack, and save for
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
604 * the next frame.
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
605 * FIXME: Move this to analysis.
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
606 * TODO: Tune groupings depending on attack location
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
607 * TODO: Handle more than one attack in a group
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
608 */
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
609 for (i = 0; i < 9; i++) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
610 if (attacks[i]) {
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
611 grouping = i;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
612 break;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
613 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
614 }
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
615 pch->next_grouping = window_grouping[grouping];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
616
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
617 pch->prev_attack = attacks[8];
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
618
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
619 return wi;
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
620 }
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
621
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
622 const FFPsyModel ff_aac_psy_model =
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
623 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
624 .name = "3GPP TS 26.403-inspired model",
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
625 .init = psy_3gpp_init,
12408
ae72506d4c2a acenc: LAME-inspired window decision
alexc
parents: 12406
diff changeset
626 .window = psy_lame_window,
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
627 .analyze = psy_3gpp_analyze,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
628 .end = psy_3gpp_end,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents: 8718
diff changeset
629 };