29
29
#include <stdint.h>
32
/* prototypes for static functions in ac3enc_fixed.c and ac3enc_float.c */
34
static void scale_coefficients(AC3EncodeContext *s);
36
static void apply_window(DSPContext *dsp, SampleType *output,
37
const SampleType *input, const SampleType *window,
40
static int normalize_samples(AC3EncodeContext *s);
42
static void clip_coefficients(DSPContext *dsp, CoefType *coef, unsigned int len);
44
static CoefType calc_cpl_coord(CoefSumType energy_ch, CoefSumType energy_cpl);
34
47
int AC3_NAME(allocate_sample_buffers)(AC3EncodeContext *s)
68
81
/* copy last 256 samples of previous frame to the start of the current frame */
69
memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_FRAME_SIZE],
82
memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_BLOCK_SIZE * s->num_blocks],
70
83
AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
73
86
sinc = s->channels;
74
87
sptr = samples + s->channel_map[ch];
75
for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) {
88
for (i = AC3_BLOCK_SIZE; i < AC3_BLOCK_SIZE * (s->num_blocks + 1); i++) {
76
89
s->planar_samples[ch][i] = *sptr;
84
97
* Apply the MDCT to input samples to generate frequency coefficients.
85
98
* This applies the KBD window and normalizes the input to reduce precision
86
99
* loss due to fixed-point calculations.
88
void AC3_NAME(apply_mdct)(AC3EncodeContext *s)
101
static void apply_mdct(AC3EncodeContext *s)
92
105
for (ch = 0; ch < s->channels; ch++) {
93
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
106
for (blk = 0; blk < s->num_blocks; blk++) {
94
107
AC3Block *block = &s->blocks[blk];
95
108
const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
97
s->apply_window(&s->dsp, s->windowed_samples, input_samples,
98
s->mdct->window, AC3_WINDOW_SIZE);
110
apply_window(&s->dsp, s->windowed_samples, input_samples,
111
s->mdct_window, AC3_WINDOW_SIZE);
100
113
if (s->fixed_point)
101
block->coeff_shift[ch+1] = s->normalize_samples(s);
114
block->coeff_shift[ch+1] = normalize_samples(s);
103
s->mdct->fft.mdct_calcw(&s->mdct->fft, block->mdct_coef[ch+1],
104
s->windowed_samples);
116
s->mdct.mdct_calcw(&s->mdct, block->mdct_coef[ch+1],
117
s->windowed_samples);
111
* Calculate a single coupling coordinate.
113
static inline float calc_cpl_coord(float energy_ch, float energy_cpl)
117
coord *= sqrtf(energy_ch / energy_cpl);
123
124
* Calculate coupling channel and coupling coordinates.
124
* TODO: Currently this is only used for the floating-point encoder. I was
125
* able to make it work for the fixed-point encoder, but quality was
126
* generally lower in most cases than not using coupling. If a more
127
* adaptive coupling strategy were to be implemented it might be useful
128
* at that time to use coupling for the fixed-point encoder as well.
130
void AC3_NAME(apply_channel_coupling)(AC3EncodeContext *s)
126
static void apply_channel_coupling(AC3EncodeContext *s)
128
LOCAL_ALIGNED_16(CoefType, cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
132
129
#if CONFIG_AC3ENC_FLOAT
133
LOCAL_ALIGNED_16(float, cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
134
130
LOCAL_ALIGNED_16(int32_t, fixed_cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
132
int32_t (*fixed_cpl_coords)[AC3_MAX_CHANNELS][16] = cpl_coords;
135
134
int blk, ch, bnd, i, j;
136
135
CoefSumType energy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16] = {{{0}}};
137
136
int cpl_start, num_cpl_coefs;
139
138
memset(cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
140
memset(fixed_cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*fixed_cpl_coords));
139
#if CONFIG_AC3ENC_FLOAT
140
memset(fixed_cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
142
143
/* align start to 16-byte boundary. align length to multiple of 32.
143
144
note: coupling start bin % 4 will always be 1 */
160
161
cpl_coef[i] += ch_coef[i];
163
/* coefficients must be clipped to +/- 1.0 in order to be encoded */
164
s->dsp.vector_clipf(cpl_coef, cpl_coef, -1.0f, 1.0f, num_cpl_coefs);
166
/* scale coupling coefficients from float to 24-bit fixed-point */
167
s->ac3dsp.float_to_fixed24(&block->fixed_coef[CPL_CH][cpl_start],
168
cpl_coef, num_cpl_coefs);
164
/* coefficients must be clipped in order to be encoded */
165
clip_coefficients(&s->dsp, cpl_coef, num_cpl_coefs);
171
168
/* calculate energy in each band in coupling channel and each fbw channel */
189
/* calculate coupling coordinates for all blocks for all channels */
190
for (blk = 0; blk < s->num_blocks; blk++) {
191
AC3Block *block = &s->blocks[blk];
192
if (!block->cpl_in_use)
194
for (ch = 1; ch <= s->fbw_channels; ch++) {
195
if (!block->channel_in_cpl[ch])
197
for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
198
cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd],
199
energy[blk][CPL_CH][bnd]);
192
204
/* determine which blocks to send new coupling coordinates for */
193
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
205
for (blk = 0; blk < s->num_blocks; blk++) {
194
206
AC3Block *block = &s->blocks[blk];
195
207
AC3Block *block0 = blk ? &s->blocks[blk-1] : NULL;
197
CoefSumType coord_diff[AC3_MAX_CHANNELS] = {0,};
209
memset(block->new_cpl_coords, 0, sizeof(block->new_cpl_coords));
199
211
if (block->cpl_in_use) {
200
/* calculate coupling coordinates for all blocks and calculate the
201
average difference between coordinates in successive blocks */
202
for (ch = 1; ch <= s->fbw_channels; ch++) {
203
if (!block->channel_in_cpl[ch])
206
for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
207
cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd],
208
energy[blk][CPL_CH][bnd]);
209
if (blk > 0 && block0->cpl_in_use &&
210
block0->channel_in_cpl[ch]) {
211
coord_diff[ch] += fabs(cpl_coords[blk-1][ch][bnd] -
212
cpl_coords[blk ][ch][bnd]);
215
coord_diff[ch] /= s->num_cpl_bands;
218
212
/* send new coordinates if this is the first block, if previous
219
213
* block did not use coupling but this block does, the channels
220
214
* using coupling has changed from the previous block, or the
221
215
* coordinate difference from the last block for any channel is
222
216
* greater than a threshold value. */
225
} else if (!block0->cpl_in_use) {
217
if (blk == 0 || !block0->cpl_in_use) {
218
for (ch = 1; ch <= s->fbw_channels; ch++)
219
block->new_cpl_coords[ch] = 1;
228
221
for (ch = 1; ch <= s->fbw_channels; ch++) {
229
if (block->channel_in_cpl[ch] && !block0->channel_in_cpl[ch]) {
235
for (ch = 1; ch <= s->fbw_channels; ch++) {
236
if (block->channel_in_cpl[ch] && coord_diff[ch] > 0.04) {
222
if (!block->channel_in_cpl[ch])
224
if (!block0->channel_in_cpl[ch]) {
225
block->new_cpl_coords[ch] = 1;
227
CoefSumType coord_diff = 0;
228
for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
229
coord_diff += FFABS(cpl_coords[blk-1][ch][bnd] -
230
cpl_coords[blk ][ch][bnd]);
232
coord_diff /= s->num_cpl_bands;
233
if (coord_diff > NEW_CPL_COORD_THRESHOLD)
234
block->new_cpl_coords[ch] = 1;
244
block->new_cpl_coords = new_coords;
247
241
/* calculate final coupling coordinates, taking into account reusing of
248
242
coordinates in successive blocks */
249
243
for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
251
while (blk < AC3_MAX_BLOCKS) {
253
CoefSumType energy_cpl;
245
while (blk < s->num_blocks) {
254
247
AC3Block *block = &s->blocks[blk];
256
249
if (!block->cpl_in_use) {
261
energy_cpl = energy[blk][CPL_CH][bnd];
263
while (!s->blocks[blk1].new_cpl_coords && blk1 < AC3_MAX_BLOCKS) {
264
if (s->blocks[blk1].cpl_in_use)
265
energy_cpl += energy[blk1][CPL_CH][bnd];
269
254
for (ch = 1; ch <= s->fbw_channels; ch++) {
255
CoefSumType energy_ch, energy_cpl;
271
256
if (!block->channel_in_cpl[ch])
258
energy_cpl = energy[blk][CPL_CH][bnd];
273
259
energy_ch = energy[blk][ch][bnd];
275
while (!s->blocks[blk1].new_cpl_coords && blk1 < AC3_MAX_BLOCKS) {
276
if (s->blocks[blk1].cpl_in_use)
261
while (!s->blocks[blk1].new_cpl_coords[ch] && blk1 < s->num_blocks) {
262
if (s->blocks[blk1].cpl_in_use) {
263
energy_cpl += energy[blk1][CPL_CH][bnd];
277
264
energy_ch += energy[blk1][ch][bnd];
280
268
cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl);
286
274
/* calculate exponents/mantissas for coupling coordinates */
287
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
275
for (blk = 0; blk < s->num_blocks; blk++) {
288
276
AC3Block *block = &s->blocks[blk];
289
if (!block->cpl_in_use || !block->new_cpl_coords)
277
if (!block->cpl_in_use)
280
#if CONFIG_AC3ENC_FLOAT
292
281
s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1],
293
282
cpl_coords[blk][1],
294
283
s->fbw_channels * 16);
295
285
s->ac3dsp.extract_exponents(block->cpl_coord_exp[1],
296
286
fixed_cpl_coords[blk][1],
297
287
s->fbw_channels * 16);
394
int AC3_NAME(encode_frame)(AVCodecContext *avctx, unsigned char *frame,
395
int buf_size, void *data)
397
AC3EncodeContext *s = avctx->priv_data;
398
const SampleType *samples = data;
401
if (s->options.allow_per_frame_metadata) {
402
ret = ff_ac3_validate_metadata(s);
407
if (s->bit_alloc.sr_code == 1 || s->eac3)
408
ff_ac3_adjust_frame_size(s);
410
deinterleave_input_samples(s, samples);
415
scale_coefficients(s);
417
clip_coefficients(&s->dsp, s->blocks[0].mdct_coef[1],
418
AC3_MAX_COEFS * s->num_blocks * s->channels);
420
s->cpl_on = s->cpl_enabled;
421
ff_ac3_compute_coupling_strategy(s);
424
apply_channel_coupling(s);
426
compute_rematrixing_strategy(s);
429
scale_coefficients(s);
431
ff_ac3_apply_rematrixing(s);
433
ff_ac3_process_exponents(s);
435
ret = ff_ac3_compute_bit_allocation(s);
437
av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
441
ff_ac3_group_exponents(s);
443
ff_ac3_quantize_mantissas(s);
445
ff_ac3_output_frame(s, frame);
447
return s->frame_size;