FFmpeg
Loading...
Searching...
No Matches
ac3enc_template.c
Go to the documentation of this file.
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
34#include "libavutil/avassert.h"
36
37#include "audiodsp.h"
38#include "ac3enc.h"
39#include "eac3enc.h"
40
41#if AC3ENC_FLOAT
42#define RENAME(element) element ## _float
43#else
44#define RENAME(element) element ## _fixed
45#endif
46
47/* power ratios for the -42 dB band floor and the 12 dB difference margin. */
48#define PHASE_BAND_ENERGY_DENOMINATOR (1 << 14)
49#define PHASE_DIFF_ENERGY_FACTOR (1 << 4)
50
51/*
52 * Apply the MDCT to input samples to generate frequency coefficients.
53 * This applies the KBD window and normalizes the input to reduce precision
54 * loss due to fixed-point calculations.
55 */
56static void apply_mdct(AC3EncodeContext *s, uint8_t * const *samples)
57{
58 av_assert1(s->num_blocks > 0);
59
60 for (int ch = 0; ch < s->channels; ch++) {
61 const SampleType *input_samples0 = (const SampleType*)s->planar_samples[ch];
62 /* Reorder channels from native order to AC-3 order. */
63 const SampleType *input_samples1 = (const SampleType*)samples[s->channel_map[ch]];
64 int blk = 0;
65
66 do {
67 AC3Block *block = &s->blocks[blk];
68 SampleType *windowed_samples = s->RENAME(windowed_samples);
69
70 s->fdsp->vector_fmul(windowed_samples, input_samples0,
71 s->RENAME(mdct_window), AC3_BLOCK_SIZE);
72 s->fdsp->vector_fmul_reverse(windowed_samples + AC3_BLOCK_SIZE,
73 input_samples1,
74 s->RENAME(mdct_window), AC3_BLOCK_SIZE);
75
76 s->tx_fn(s->tx, block->mdct_coef[ch+1],
77 windowed_samples, sizeof(*windowed_samples));
78 input_samples0 = input_samples1;
79 input_samples1 += AC3_BLOCK_SIZE;
80 } while (++blk < s->num_blocks);
81
82 /* Store last 256 samples of current frame */
83 memcpy(s->planar_samples[ch], input_samples0,
84 AC3_BLOCK_SIZE * sizeof(*input_samples0));
85 }
86}
87
88
89/*
90 * Calculate coupling channel and coupling coordinates.
91 */
93{
95#if AC3ENC_FLOAT
96 LOCAL_ALIGNED_32(int32_t, fixed_cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
97#else
98 int32_t (*fixed_cpl_coords)[AC3_MAX_CHANNELS][16] = cpl_coords;
99#endif
100 int av_uninit(blk), ch, bnd, i, j;
101 CoefSumType energy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16] = {{{0}}};
102 int cpl_start, num_cpl_coefs;
103
104 s->phase_flags_in_use = 0;
105 memset(cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
106#if AC3ENC_FLOAT
107 memset(fixed_cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
108#endif
109
110 /* align start to 16-byte boundary. align length to multiple of 32.
111 note: coupling start bin % 4 will always be 1 */
112 cpl_start = s->start_freq[CPL_CH] - 1;
113 num_cpl_coefs = FFALIGN(s->num_cpl_subbands * 12 + 1, 32);
114 cpl_start = FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs;
115
116 if (s->channel_mode == AC3_CHMODE_STEREO) {
117 uint8_t phase_flags[AC3_MAX_CPL_BANDS];
118 int cpl_blocks = 0;
119
120 /* use a single phase strategy for the frame. a difference carrier is
121 * selected only when it has at least 12 dB more energy and the band
122 * is within 42 dB of the coded channel energy in every coupling
123 * block. */
124 memset(phase_flags, 1, s->num_cpl_bands);
125 for (blk = 0; blk < s->num_blocks; blk++) {
126 AC3Block *block = &s->blocks[blk];
128 /* the DSP also returns sum and difference energy in slots 2/3. */
129 CoefSumType block_energy[4];
130 CoefSumType max_energy;
131
132 if (!block->cpl_in_use)
133 continue;
134 cpl_blocks++;
135 sum_square_butterfly(s, block_energy,
136 block->mdct_coef[1], block->mdct_coef[2],
137 s->start_freq[CPL_CH]);
138 i = s->start_freq[CPL_CH];
139 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
140 sum_square_butterfly(s, sum[bnd],
141 block->mdct_coef[1] + i,
142 block->mdct_coef[2] + i,
143 s->cpl_band_sizes[bnd]);
144 /* reused by the coupling coordinate calculation below. */
145 energy[blk][1][bnd] = sum[bnd][0];
146 energy[blk][2][bnd] = sum[bnd][1];
147 block_energy[0] += sum[bnd][0];
148 block_energy[1] += sum[bnd][1];
149 i += s->cpl_band_sizes[bnd];
150 }
151 max_energy = FFMAX(block_energy[0], block_energy[1]);
152 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
153 CoefSumType band_energy = FFMAX(sum[bnd][0], sum[bnd][1]);
154 int significant = band_energy >
156
157 phase_flags[bnd] &= significant &&
158 sum[bnd][3] / PHASE_DIFF_ENERGY_FACTOR > sum[bnd][2];
159 }
160 }
161 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
162 int phase = phase_flags[bnd] && cpl_blocks;
163
164 s->phase_flags[bnd] = phase;
165 s->phase_flags_in_use |= phase;
166 }
167 }
168
169 /* calculate coupling channel from fbw channels */
170 for (blk = 0; blk < s->num_blocks; blk++) {
171 AC3Block *block = &s->blocks[blk];
172 CoefType *cpl_coef = &block->mdct_coef[CPL_CH][cpl_start];
173 if (!block->cpl_in_use)
174 continue;
175 memset(cpl_coef, 0, num_cpl_coefs * sizeof(*cpl_coef));
176 for (ch = 1; ch <= s->fbw_channels; ch++) {
177 CoefType *ch_coef = &block->mdct_coef[ch][cpl_start];
178 if (!block->channel_in_cpl[ch])
179 continue;
180 for (i = 0; i < num_cpl_coefs; i++)
181 cpl_coef[i] += ch_coef[i];
182 }
183
184 if (s->channel_mode == AC3_CHMODE_STEREO) {
185 CoefType *left = block->mdct_coef[1];
186 CoefType *right = block->mdct_coef[2];
187
188 i = s->start_freq[CPL_CH];
189 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
190 if (s->phase_flags[bnd]) {
191 for (j = 0; j < s->cpl_band_sizes[bnd]; j++)
192 block->mdct_coef[CPL_CH][i + j] = left[i + j] - right[i + j];
193 }
194 i += s->cpl_band_sizes[bnd];
195 }
196 }
197
198 /* coefficients must be clipped in order to be encoded */
199 clip_coefficients(&s->adsp, cpl_coef, num_cpl_coefs);
200 }
201
202 /* calculate energy in each band in coupling channel and each fbw channel */
203 /* TODO: possibly use SIMD to speed up energy calculation */
204 bnd = 0;
205 i = s->start_freq[CPL_CH];
206 while (i < s->cpl_end_freq) {
207 int band_size = s->cpl_band_sizes[bnd];
208 /* stereo channel energies were filled during phase analysis above. */
209 int last_ch = s->channel_mode == AC3_CHMODE_STEREO ?
210 CPL_CH : s->fbw_channels;
211 for (ch = CPL_CH; ch <= last_ch; ch++) {
212 for (blk = 0; blk < s->num_blocks; blk++) {
213 AC3Block *block = &s->blocks[blk];
214 if (!block->cpl_in_use || (ch > CPL_CH && !block->channel_in_cpl[ch]))
215 continue;
216 for (j = 0; j < band_size; j++) {
217 CoefType v = block->mdct_coef[ch][i+j];
218 MAC_COEF(energy[blk][ch][bnd], v, v);
219 }
220 }
221 }
222 i += band_size;
223 bnd++;
224 }
225
226 /* calculate coupling coordinates for all blocks for all channels */
227 for (blk = 0; blk < s->num_blocks; blk++) {
228 AC3Block *block = &s->blocks[blk];
229 if (!block->cpl_in_use)
230 continue;
231 for (ch = 1; ch <= s->fbw_channels; ch++) {
232 if (!block->channel_in_cpl[ch])
233 continue;
234 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
235 cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd],
236 energy[blk][CPL_CH][bnd]);
237 }
238 }
239 }
240
241 /* determine which blocks to send new coupling coordinates for */
242 for (blk = 0; blk < s->num_blocks; blk++) {
243 AC3Block *block = &s->blocks[blk];
244 AC3Block *block0 = blk ? &s->blocks[blk-1] : NULL;
245
246 memset(block->new_cpl_coords, 0, sizeof(block->new_cpl_coords));
247
248 if (block->cpl_in_use) {
249 /* send new coordinates if this is the first block, if previous
250 * block did not use coupling but this block does, the channels
251 * using coupling has changed from the previous block, or the
252 * coordinate difference from the last block for any channel is
253 * greater than a threshold value. */
254 if (blk == 0 || !block0->cpl_in_use) {
255 for (ch = 1; ch <= s->fbw_channels; ch++)
256 block->new_cpl_coords[ch] = 1;
257 } else {
258 for (ch = 1; ch <= s->fbw_channels; ch++) {
259 if (!block->channel_in_cpl[ch])
260 continue;
261 if (!block0->channel_in_cpl[ch]) {
262 block->new_cpl_coords[ch] = 1;
263 } else {
264 CoefSumType coord_diff = 0;
265 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
266 coord_diff += FFABS(cpl_coords[blk-1][ch][bnd] -
267 cpl_coords[blk ][ch][bnd]);
268 }
269 coord_diff /= s->num_cpl_bands;
270 if (coord_diff > NEW_CPL_COORD_THRESHOLD)
271 block->new_cpl_coords[ch] = 1;
272 }
273 }
274 }
275 }
276 }
277
278 av_assert1(s->fbw_channels > 0);
279
280 /* calculate final coupling coordinates, taking into account reusing of
281 coordinates in successive blocks */
282 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
283 blk = 0;
284 while (blk < s->num_blocks) {
285 int av_uninit(blk1);
286 AC3Block *block = &s->blocks[blk];
287
288 if (!block->cpl_in_use) {
289 blk++;
290 continue;
291 }
292
293 for (ch = 1; ch <= s->fbw_channels; ch++) {
294 CoefSumType energy_ch, energy_cpl;
295 if (!block->channel_in_cpl[ch])
296 continue;
297 energy_cpl = energy[blk][CPL_CH][bnd];
298 energy_ch = energy[blk][ch][bnd];
299 blk1 = blk+1;
300 while (blk1 < s->num_blocks && !s->blocks[blk1].new_cpl_coords[ch]) {
301 if (s->blocks[blk1].cpl_in_use) {
302 energy_cpl += energy[blk1][CPL_CH][bnd];
303 energy_ch += energy[blk1][ch][bnd];
304 }
305 blk1++;
306 }
307 cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl);
308 }
309 blk = blk1;
310 }
311 }
312
313 /* calculate exponents/mantissas for coupling coordinates */
314 for (blk = 0; blk < s->num_blocks; blk++) {
315 AC3Block *block = &s->blocks[blk];
316 if (!block->cpl_in_use)
317 continue;
318
319#if AC3ENC_FLOAT
320 s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1],
321 cpl_coords[blk][1],
322 s->fbw_channels * 16);
323#endif
324 s->ac3dsp.extract_exponents(block->cpl_coord_exp[1],
325 fixed_cpl_coords[blk][1],
326 s->fbw_channels * 16);
327
328 for (ch = 1; ch <= s->fbw_channels; ch++) {
329 int bnd, min_exp, max_exp, master_exp;
330
331 if (!block->new_cpl_coords[ch])
332 continue;
333
334 /* determine master exponent */
335 min_exp = max_exp = block->cpl_coord_exp[ch][0];
336 for (bnd = 1; bnd < s->num_cpl_bands; bnd++) {
337 int exp = block->cpl_coord_exp[ch][bnd];
338 min_exp = FFMIN(exp, min_exp);
339 max_exp = FFMAX(exp, max_exp);
340 }
341 master_exp = ((max_exp - 15) + 2) / 3;
342 master_exp = FFMAX(master_exp, 0);
343 while (min_exp < master_exp * 3)
344 master_exp--;
345 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
346 block->cpl_coord_exp[ch][bnd] = av_clip(block->cpl_coord_exp[ch][bnd] -
347 master_exp * 3, 0, 15);
348 }
349 block->cpl_master_exp[ch] = master_exp;
350
351 /* quantize mantissas */
352 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
353 int cpl_exp = block->cpl_coord_exp[ch][bnd];
354 int cpl_mant = (fixed_cpl_coords[blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
355 if (cpl_exp == 15)
356 cpl_mant >>= 1;
357 else
358 cpl_mant -= 16;
359
360 block->cpl_coord_mant[ch][bnd] = cpl_mant;
361 }
362 }
363 }
364
365 if (AC3ENC_FLOAT && CONFIG_EAC3_ENCODER && s->eac3)
367}
368
369
370/*
371 * Determine rematrixing flags for each block and band.
372 */
374{
375 int nb_coefs;
376 int blk, bnd;
377 AC3Block *block, *block0 = NULL;
378
379 if (s->channel_mode != AC3_CHMODE_STEREO)
380 return;
381
382 for (blk = 0; blk < s->num_blocks; blk++) {
383 block = &s->blocks[blk];
384 block->new_rematrixing_strategy = !blk;
385
386 block->num_rematrixing_bands = 4;
387 if (block->cpl_in_use) {
388 block->num_rematrixing_bands -= (s->start_freq[CPL_CH] <= 61);
389 block->num_rematrixing_bands -= (s->start_freq[CPL_CH] == 37);
390 if (blk && block->num_rematrixing_bands != block0->num_rematrixing_bands)
391 block->new_rematrixing_strategy = 1;
392 }
393 nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
394
395 if (!s->rematrixing_enabled) {
396 block0 = block;
397 continue;
398 }
399
400 for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
401 /* calculate sum of squared coeffs for one band in one block */
402 int start = ff_ac3_rematrix_band_tab[bnd];
403 int end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
404 CoefSumType sum[4];
405 sum_square_butterfly(s, sum, block->mdct_coef[1] + start,
406 block->mdct_coef[2] + start, end - start);
407
408 /* compare sums to determine if rematrixing will be used for this band */
409 if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
410 block->rematrixing_flags[bnd] = 1;
411 else
412 block->rematrixing_flags[bnd] = 0;
413
414 /* determine if new rematrixing flags will be sent */
415 if (blk &&
416 block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
417 block->new_rematrixing_strategy = 1;
418 }
419 }
420 block0 = block;
421 }
422}
423
425{
426 int end = frame ? frame->nb_samples : 0;
427
428 /* copy new samples and zero any remaining samples */
429 if (frame) {
430 av_samples_copy(s->input_samples, frame->extended_data, 0, 0,
431 frame->nb_samples, s->channels,
432 s->avctx->sample_fmt);
433 }
434 av_samples_set_silence(s->input_samples, end,
435 s->avctx->frame_size - end,
436 s->channels, s->avctx->sample_fmt);
437}
438
440{
441 uint8_t **samples;
442
443 if (!frame || frame->nb_samples < s->avctx->frame_size) {
445 samples = s->input_samples;
446 } else
447 samples = frame->extended_data;
448
449 apply_mdct(s, samples);
450
451 s->cpl_on = s->cpl_enabled;
453
454 if (s->cpl_on)
456
458
459#if AC3ENC_FLOAT
461#endif
462}
#define CPL_CH
coupling channel index
Definition ac3defs.h:27
#define AC3_MAX_CHANNELS
maximum number of channels, including coupling channel
Definition ac3defs.h:26
#define AC3_BLOCK_SIZE
Definition ac3defs.h:30
@ AC3_CHMODE_STEREO
Definition ac3defs.h:70
#define AC3_MAX_BLOCKS
Definition ac3defs.h:31
#define AC3_MAX_CPL_BANDS
Definition ac3defs.h:35
void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s)
Set the initial coupling strategy parameters prior to coupling analysis.
Definition ac3enc.c:507
AC-3 encoder & E-AC-3 encoder common header.
#define NEW_CPL_COORD_THRESHOLD
Definition ac3enc.h:65
#define MAC_COEF(d, a, b)
Definition ac3enc.h:62
int32_t SampleType
Definition ac3enc.h:66
#define AC3ENC_FLOAT
Definition ac3enc.h:48
int64_t CoefSumType
Definition ac3enc.h:68
int32_t CoefType
Definition ac3enc.h:67
static void clip_coefficients(AudioDSPContext *adsp, int32_t *coef, unsigned int len)
static void sum_square_butterfly(AC3EncodeContext *s, int64_t sum[4], const int32_t *coef0, const int32_t *coef1, int len)
static CoefType calc_cpl_coord(CoefSumType energy_ch, CoefSumType energy_cpl)
static void scale_coefficients(AC3EncodeContext *s)
static void encode_frame(AC3EncodeContext *s, const AVFrame *frame)
static void apply_channel_coupling(AC3EncodeContext *s)
#define PHASE_DIFF_ENERGY_FACTOR
static void apply_mdct(AC3EncodeContext *s, uint8_t *const *samples)
#define PHASE_BAND_ENERGY_DENOMINATOR
static void copy_input_samples(AC3EncodeContext *s, const AVFrame *frame)
static void compute_rematrixing_strategy(AC3EncodeContext *s)
const uint8_t ff_ac3_rematrix_band_tab[5]
Table of bin locations for rematrixing bands reference: Section 7.5.2 Rematrixing : Frequency Band De...
Definition ac3tab.c:108
static int nb_coefs(int length, int level, uint64_t sn)
Definition af_afwtdn.c:515
static float mdct_window[MDCT_SIZE]
Definition atrac3.c:125
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define av_clip
Definition common.h:100
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
static int16_t block[64]
Definition dct.c:125
static AVFrame * frame
void ff_eac3_set_cpl_states(AC3EncodeContext *s)
Set coupling states.
Definition eac3enc.c:98
E-AC-3 encoder.
int8_t exp
Definition eval.c:76
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition samplefmt.c:246
int av_samples_copy(uint8_t *const *dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
Definition samplefmt.c:222
Macro definitions for various function/variable attributes.
#define av_uninit(x)
Definition attributes.h:187
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
#define LOCAL_ALIGNED_32(t, v,...)
#define blk(i)
Definition sha.c:55
Data for a single audio block.
Definition ac3enc.h:129
uint8_t rematrixing_flags[4]
rematrixing flags
Definition ac3enc.h:142
int num_rematrixing_bands
number of rematrixing bands
Definition ac3enc.h:141
uint8_t channel_in_cpl[AC3_MAX_CHANNELS]
channel in coupling (chincpl)
Definition ac3enc.h:145
int cpl_in_use
coupling in use for this block (cplinu)
Definition ac3enc.h:144
AC-3 encoder private context.
Definition ac3enc.h:159
This structure describes decoded (raw) audio or video data.
Definition frame.h:472