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 <stdint.h>
30
32
33 /* prototypes for static functions in ac3enc_fixed.c and ac3enc_float.c */
34
36
40
42
44
46
50
52 {
53 int ch;
54
56 sizeof(*
s->windowed_samples), alloc_fail);
58 alloc_fail);
59 for (ch = 0; ch <
s->channels; ch++) {
62 alloc_fail);
63 }
64
65 return 0;
66 alloc_fail:
68 }
69
70
71 /*
72 * Copy input samples.
73 * Channels are reordered from FFmpeg's default order to AC-3 order.
74 */
76 {
77 int ch;
78
79 /* copy and remap input samples */
80 for (ch = 0; ch < s->
channels; ch++) {
81 /* copy last 256 samples of previous frame to the start of the current frame */
84
85 /* copy new samples for current frame */
89 }
90 }
91
92
93 /*
94 * Apply the MDCT to input samples to generate frequency coefficients.
95 * This applies the KBD window and normalizes the input to reduce precision
96 * loss due to fixed-point calculations.
97 */
99 {
101
102 for (ch = 0; ch < s->
channels; ch++) {
106
107 #if CONFIG_AC3ENC_FLOAT
110 #else
113 #endif
114
117
120 }
121 }
122 }
123
124
125 /*
126 * Calculate coupling channel and coupling coordinates.
127 */
129 {
131 #if CONFIG_AC3ENC_FLOAT
133 #else
135 #endif
138 int cpl_start, num_cpl_coefs;
139
141 #if CONFIG_AC3ENC_FLOAT
142 memset(fixed_cpl_coords, 0,
AC3_MAX_BLOCKS *
sizeof(*cpl_coords));
143 #endif
144
145 /* align start to 16-byte boundary. align length to multiple of 32.
146 note: coupling start bin % 4 will always be 1 */
149 cpl_start =
FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs;
150
151 /* calculate coupling channel from fbw channels */
156 continue;
157 memset(cpl_coef, 0, num_cpl_coefs * sizeof(*cpl_coef));
161 continue;
162 for (i = 0; i < num_cpl_coefs; i++)
163 cpl_coef[i] += ch_coef[i];
164 }
165
166 /* coefficients must be clipped in order to be encoded */
168 }
169
170 /* calculate energy in each band in coupling channel and each fbw channel */
171 /* TODO: possibly use SIMD to speed up energy calculation */
172 bnd = 0;
180 continue;
181 for (j = 0; j < band_size; j++) {
184 }
185 }
186 }
187 i += band_size;
188 bnd++;
189 }
190
191 /* calculate coupling coordinates for all blocks for all channels */
195 continue;
198 continue;
202 }
203 }
204 }
205
206 /* determine which blocks to send new coupling coordinates for */
210
212
214 /* send new coordinates if this is the first block, if previous
215 * block did not use coupling but this block does, the channels
216 * using coupling has changed from the previous block, or the
217 * coordinate difference from the last block for any channel is
218 * greater than a threshold value. */
222 } else {
225 continue;
228 } else {
231 coord_diff +=
FFABS(cpl_coords[
blk-1][ch][bnd] -
232 cpl_coords[
blk ][ch][bnd]);
233 }
237 }
238 }
239 }
240 }
241 }
242
243 /* calculate final coupling coordinates, taking into account reusing of
244 coordinates in successive blocks */
250
253 continue;
254 }
255
259 continue;
261 energy_ch = energy[
blk][ch][bnd];
265 energy_cpl += energy[blk1][
CPL_CH][bnd];
266 energy_ch += energy[blk1][ch][bnd];
267 }
268 blk1++;
269 }
271 }
273 }
274 }
275
276 /* calculate exponents/mantissas for coupling coordinates */
280 continue;
281
282 #if CONFIG_AC3ENC_FLOAT
284 cpl_coords[blk][1],
286 #endif
288 fixed_cpl_coords[blk][1],
290
292 int bnd, min_exp, max_exp, master_exp;
293
295 continue;
296
297 /* determine master exponent */
301 min_exp =
FFMIN(exp, min_exp);
302 max_exp =
FFMAX(exp, max_exp);
303 }
304 master_exp = ((max_exp - 15) + 2) / 3;
305 master_exp =
FFMAX(master_exp, 0);
306 while (min_exp < master_exp * 3)
307 master_exp--;
310 master_exp * 3, 0, 15);
311 }
313
314 /* quantize mantissas */
317 int cpl_mant = (fixed_cpl_coords[
blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
318 if (cpl_exp == 15)
319 cpl_mant >>= 1;
320 else
321 cpl_mant -= 16;
322
324 }
325 }
326 }
327
328 if (CONFIG_EAC3_ENCODER && s->
eac3)
330 }
331
332
333 /*
334 * Determine rematrixing flags for each block and band.
335 */
337 {
338 int nb_coefs;
341
343 return;
344
348
355 }
357
360 continue;
361 }
362
364 /* calculate sum of squared coeffs for one band in one block */
369 block->
mdct_coef[2] + start, end - start);
370
371 /* compare sums to determine if rematrixing will be used for this band */
372 if (
FFMIN(sum[2], sum[3]) <
FFMIN(sum[0], sum[1]))
374 else
376
377 /* determine if new rematrixing flags will be sent */
378 if (blk &&
381 }
382 }
384 }
385 }
386
387
390 {
393
396 if (ret)
398 }
399
402
404
406
409
412
415
418
420
423
425
427
429 if (ret) {
432 }
433
435
437
441
444
445 *got_packet_ptr = 1;
446 return 0;
447 }