1 /*
2 * AC-3 encoder float/fixed template
3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2006-2011 Justin Ruggles <justin.ruggles@gmail.com>
5 * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * AC-3 encoder float/fixed template
27 */
28
29 #include "config_components.h"
30
31 #include <stdint.h>
32
36
40
41 #if AC3ENC_FLOAT
42 #define RENAME(element) element ## _float
43 #else
44 #define RENAME(element) element ## _fixed
45 #endif
46
47 /*
48 * Apply the MDCT to input samples to generate frequency coefficients.
49 * This applies the KBD window and normalizes the input to reduce precision
50 * loss due to fixed-point calculations.
51 */
53 {
55
56 for (
int ch = 0; ch <
s->channels; ch++) {
58 /* Reorder channels from native order to AC-3 order. */
61
62 do {
64 SampleType *windowed_samples =
s->RENAME(windowed_samples);
65
66 s->fdsp->vector_fmul(windowed_samples, input_samples0,
69 input_samples1,
71
72 s->tx_fn(
s->tx,
block->mdct_coef[ch+1],
73 windowed_samples, sizeof(*windowed_samples));
74 input_samples0 = input_samples1;
76 } while (++blk < s->num_blocks);
77
78 /* Store last 256 samples of current frame */
79 memcpy(
s->planar_samples[ch], input_samples0,
81 }
82 }
83
84
85 /*
86 * Calculate coupling channel and coupling coordinates.
87 */
89 {
91 #if AC3ENC_FLOAT
93 #else
95 #endif
98 int cpl_start, num_cpl_coefs;
99
101 #if AC3ENC_FLOAT
102 memset(fixed_cpl_coords, 0,
AC3_MAX_BLOCKS *
sizeof(*cpl_coords));
103 #endif
104
105 /* align start to 16-byte boundary. align length to multiple of 32.
106 note: coupling start bin % 4 will always be 1 */
107 cpl_start =
s->start_freq[
CPL_CH] - 1;
108 num_cpl_coefs =
FFALIGN(
s->num_cpl_subbands * 12 + 1, 32);
109 cpl_start =
FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs;
110
111 /* calculate coupling channel from fbw channels */
115 if (!
block->cpl_in_use)
116 continue;
117 memset(cpl_coef, 0, num_cpl_coefs * sizeof(*cpl_coef));
118 for (ch = 1; ch <=
s->fbw_channels; ch++) {
120 if (!
block->channel_in_cpl[ch])
121 continue;
122 for (
i = 0;
i < num_cpl_coefs;
i++)
123 cpl_coef[
i] += ch_coef[
i];
124 }
125
126 /* coefficients must be clipped in order to be encoded */
128 }
129
130 /* calculate energy in each band in coupling channel and each fbw channel */
131 /* TODO: possibly use SIMD to speed up energy calculation */
132 bnd = 0;
134 while (i < s->cpl_end_freq) {
135 int band_size =
s->cpl_band_sizes[bnd];
136 for (ch =
CPL_CH; ch <=
s->fbw_channels; ch++) {
140 continue;
141 for (j = 0; j < band_size; j++) {
144 }
145 }
146 }
148 bnd++;
149 }
150
151 /* calculate coupling coordinates for all blocks for all channels */
154 if (!
block->cpl_in_use)
155 continue;
156 for (ch = 1; ch <=
s->fbw_channels; ch++) {
157 if (!
block->channel_in_cpl[ch])
158 continue;
159 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
162 }
163 }
164 }
165
166 /* determine which blocks to send new coupling coordinates for */
170
171 memset(
block->new_cpl_coords, 0,
sizeof(
block->new_cpl_coords));
172
173 if (
block->cpl_in_use) {
174 /* send new coordinates if this is the first block, if previous
175 * block did not use coupling but this block does, the channels
176 * using coupling has changed from the previous block, or the
177 * coordinate difference from the last block for any channel is
178 * greater than a threshold value. */
180 for (ch = 1; ch <=
s->fbw_channels; ch++)
181 block->new_cpl_coords[ch] = 1;
182 } else {
183 for (ch = 1; ch <=
s->fbw_channels; ch++) {
184 if (!
block->channel_in_cpl[ch])
185 continue;
187 block->new_cpl_coords[ch] = 1;
188 } else {
190 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
191 coord_diff +=
FFABS(cpl_coords[
blk-1][ch][bnd] -
192 cpl_coords[
blk ][ch][bnd]);
193 }
194 coord_diff /=
s->num_cpl_bands;
196 block->new_cpl_coords[ch] = 1;
197 }
198 }
199 }
200 }
201 }
202
204
205 /* calculate final coupling coordinates, taking into account reusing of
206 coordinates in successive blocks */
207 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
209 while (blk < s->num_blocks) {
212
213 if (!
block->cpl_in_use) {
215 continue;
216 }
217
218 for (ch = 1; ch <=
s->fbw_channels; ch++) {
220 if (!
block->channel_in_cpl[ch])
221 continue;
223 energy_ch = energy[
blk][ch][bnd];
225 while (blk1 < s->num_blocks && !
s->blocks[blk1].new_cpl_coords[ch]) {
226 if (
s->blocks[blk1].cpl_in_use) {
227 energy_cpl += energy[blk1][
CPL_CH][bnd];
228 energy_ch += energy[blk1][ch][bnd];
229 }
230 blk1++;
231 }
233 }
235 }
236 }
237
238 /* calculate exponents/mantissas for coupling coordinates */
241 if (!
block->cpl_in_use)
242 continue;
243
244 #if AC3ENC_FLOAT
245 s->ac3dsp.float_to_fixed24(fixed_cpl_coords[
blk][1],
247 s->fbw_channels * 16);
248 #endif
249 s->ac3dsp.extract_exponents(
block->cpl_coord_exp[1],
250 fixed_cpl_coords[
blk][1],
251 s->fbw_channels * 16);
252
253 for (ch = 1; ch <=
s->fbw_channels; ch++) {
254 int bnd, min_exp, max_exp, master_exp;
255
256 if (!
block->new_cpl_coords[ch])
257 continue;
258
259 /* determine master exponent */
260 min_exp = max_exp =
block->cpl_coord_exp[ch][0];
261 for (bnd = 1; bnd <
s->num_cpl_bands; bnd++) {
262 int exp =
block->cpl_coord_exp[ch][bnd];
265 }
266 master_exp = ((max_exp - 15) + 2) / 3;
267 master_exp =
FFMAX(master_exp, 0);
268 while (min_exp < master_exp * 3)
269 master_exp--;
270 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
272 master_exp * 3, 0, 15);
273 }
274 block->cpl_master_exp[ch] = master_exp;
275
276 /* quantize mantissas */
277 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
278 int cpl_exp =
block->cpl_coord_exp[ch][bnd];
279 int cpl_mant = (fixed_cpl_coords[
blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
280 if (cpl_exp == 15)
281 cpl_mant >>= 1;
282 else
283 cpl_mant -= 16;
284
285 block->cpl_coord_mant[ch][bnd] = cpl_mant;
286 }
287 }
288 }
289
292 }
293
294
295 /*
296 * Determine rematrixing flags for each block and band.
297 */
299 {
303
305 return;
306
309 block->new_rematrixing_strategy = !
blk;
310
311 block->num_rematrixing_bands = 4;
312 if (
block->cpl_in_use) {
313 block->num_rematrixing_bands -= (
s->start_freq[
CPL_CH] <= 61);
314 block->num_rematrixing_bands -= (
s->start_freq[
CPL_CH] == 37);
316 block->new_rematrixing_strategy = 1;
317 }
319
320 if (!
s->rematrixing_enabled) {
322 continue;
323 }
324
325 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
326 /* calculate sum of squared coeffs for one band in one block */
331 block->mdct_coef[2] + start, end - start);
332
333 /* compare sums to determine if rematrixing will be used for this band */
334 if (
FFMIN(sum[2], sum[3]) <
FFMIN(sum[0], sum[1]))
335 block->rematrixing_flags[bnd] = 1;
336 else
337 block->rematrixing_flags[bnd] = 0;
338
339 /* determine if new rematrixing flags will be sent */
342 block->new_rematrixing_strategy = 1;
343 }
344 }
346 }
347 }
348
349
351 {
353
354 s->cpl_on =
s->cpl_enabled;
356
359
361
362 #if AC3ENC_FLOAT
364 #endif
365 }