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
33
38
39 /* prototypes for static functions in ac3enc_fixed.c and ac3enc_float.c */
40
42
44
47
49
53
55 {
56 int ch;
57
59 sizeof(*
s->windowed_samples), alloc_fail);
61 alloc_fail);
62 for (ch = 0; ch <
s->channels; ch++) {
65 alloc_fail);
66 }
67
68 return 0;
69 alloc_fail:
71 }
72
73
74 /*
75 * Copy input samples.
76 * Channels are reordered from FFmpeg's default order to AC-3 order.
77 */
79 {
80 int ch;
81
82 /* copy and remap input samples */
83 for (ch = 0; ch < s->
channels; ch++) {
84 /* copy last 256 samples of previous frame to the start of the current frame */
87
88 /* copy new samples for current frame */
92 }
93 }
94
95
96 /*
97 * Apply the MDCT to input samples to generate frequency coefficients.
98 * This applies the KBD window and normalizes the input to reduce precision
99 * loss due to fixed-point calculations.
100 */
102 {
104
105 for (ch = 0; ch < s->
channels; ch++) {
109
110 #if CONFIG_AC3ENC_FLOAT
113 #else
116 #endif
117
120
123 }
124 }
125 }
126
127
128 /*
129 * Calculate coupling channel and coupling coordinates.
130 */
132 {
134 #if CONFIG_AC3ENC_FLOAT
136 #else
138 #endif
141 int cpl_start, num_cpl_coefs;
142
144 #if CONFIG_AC3ENC_FLOAT
145 memset(fixed_cpl_coords, 0,
AC3_MAX_BLOCKS *
sizeof(*cpl_coords));
146 #endif
147
148 /* align start to 16-byte boundary. align length to multiple of 32.
149 note: coupling start bin % 4 will always be 1 */
152 cpl_start =
FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs;
153
154 /* calculate coupling channel from fbw channels */
159 continue;
160 memset(cpl_coef, 0, num_cpl_coefs * sizeof(*cpl_coef));
164 continue;
165 for (i = 0; i < num_cpl_coefs; i++)
166 cpl_coef[i] += ch_coef[i];
167 }
168
169 /* coefficients must be clipped in order to be encoded */
171 }
172
173 /* calculate energy in each band in coupling channel and each fbw channel */
174 /* TODO: possibly use SIMD to speed up energy calculation */
175 bnd = 0;
183 continue;
184 for (j = 0; j < band_size; j++) {
187 }
188 }
189 }
190 i += band_size;
191 bnd++;
192 }
193
194 /* calculate coupling coordinates for all blocks for all channels */
198 continue;
201 continue;
205 }
206 }
207 }
208
209 /* determine which blocks to send new coupling coordinates for */
213
215
217 /* send new coordinates if this is the first block, if previous
218 * block did not use coupling but this block does, the channels
219 * using coupling has changed from the previous block, or the
220 * coordinate difference from the last block for any channel is
221 * greater than a threshold value. */
225 } else {
228 continue;
231 } else {
234 coord_diff +=
FFABS(cpl_coords[
blk-1][ch][bnd] -
235 cpl_coords[
blk ][ch][bnd]);
236 }
240 }
241 }
242 }
243 }
244 }
245
246 /* calculate final coupling coordinates, taking into account reusing of
247 coordinates in successive blocks */
253
256 continue;
257 }
258
262 continue;
264 energy_ch = energy[
blk][ch][bnd];
268 energy_cpl += energy[blk1][
CPL_CH][bnd];
269 energy_ch += energy[blk1][ch][bnd];
270 }
271 blk1++;
272 }
274 }
276 }
277 }
278
279 /* calculate exponents/mantissas for coupling coordinates */
283 continue;
284
285 #if CONFIG_AC3ENC_FLOAT
287 cpl_coords[blk][1],
289 #endif
291 fixed_cpl_coords[blk][1],
293
295 int bnd, min_exp, max_exp, master_exp;
296
298 continue;
299
300 /* determine master exponent */
304 min_exp =
FFMIN(exp, min_exp);
305 max_exp =
FFMAX(exp, max_exp);
306 }
307 master_exp = ((max_exp - 15) + 2) / 3;
308 master_exp =
FFMAX(master_exp, 0);
309 while (min_exp < master_exp * 3)
310 master_exp--;
313 master_exp * 3, 0, 15);
314 }
316
317 /* quantize mantissas */
320 int cpl_mant = (fixed_cpl_coords[
blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
321 if (cpl_exp == 15)
322 cpl_mant >>= 1;
323 else
324 cpl_mant -= 16;
325
327 }
328 }
329 }
330
331 if (CONFIG_EAC3_ENCODER && s->
eac3)
333 }
334
335
336 /*
337 * Determine rematrixing flags for each block and band.
338 */
340 {
341 int nb_coefs;
344
346 return;
347
351
358 }
360
363 continue;
364 }
365
367 /* calculate sum of squared coeffs for one band in one block */
372 block->
mdct_coef[2] + start, end - start);
373
374 /* compare sums to determine if rematrixing will be used for this band */
375 if (
FFMIN(sum[2], sum[3]) <
FFMIN(sum[0], sum[1]))
377 else
379
380 /* determine if new rematrixing flags will be sent */
381 if (blk &&
384 }
385 }
387 }
388 }
389
390
393 {
396
399 if (ret)
401 }
402
405
407
409
412
415
418
421
423
426
428
430
432 if (ret) {
435 }
436
438
440
444
447
448 *got_packet_ptr = 1;
449 return 0;
450 }