ad296e15903642b5dcec6ab21f50dfb42280fa61
[libav.git] / libavcodec / ac3enc_template.c
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 Libav.
8 *
9 * Libav 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 * Libav 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 Libav; 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
31 #include "libavutil/attributes.h"
32 #include "libavutil/internal.h"
33 #include "internal.h"
34 #include "ac3enc.h"
35 #include "eac3enc.h"
36
37 /* prototypes for static functions in ac3enc_fixed.c and ac3enc_float.c */
38
39 static void scale_coefficients(AC3EncodeContext *s);
40
41 static int normalize_samples(AC3EncodeContext *s);
42
43 static void clip_coefficients(DSPContext *dsp, CoefType *coef, unsigned int len);
44
45 static CoefType calc_cpl_coord(CoefSumType energy_ch, CoefSumType energy_cpl);
46
47
48 int AC3_NAME(allocate_sample_buffers)(AC3EncodeContext *s)
49 {
50 int ch;
51
52 FF_ALLOC_OR_GOTO(s->avctx, s->windowed_samples, AC3_WINDOW_SIZE *
53 sizeof(*s->windowed_samples), alloc_fail);
54 FF_ALLOC_OR_GOTO(s->avctx, s->planar_samples, s->channels * sizeof(*s->planar_samples),
55 alloc_fail);
56 for (ch = 0; ch < s->channels; ch++) {
57 FF_ALLOCZ_OR_GOTO(s->avctx, s->planar_samples[ch],
58 (AC3_FRAME_SIZE+AC3_BLOCK_SIZE) * sizeof(**s->planar_samples),
59 alloc_fail);
60 }
61
62 return 0;
63 alloc_fail:
64 return AVERROR(ENOMEM);
65 }
66
67
68 /*
69 * Copy input samples.
70 * Channels are reordered from Libav's default order to AC-3 order.
71 */
72 static void copy_input_samples(AC3EncodeContext *s, SampleType **samples)
73 {
74 int ch;
75
76 /* copy and remap input samples */
77 for (ch = 0; ch < s->channels; ch++) {
78 /* copy last 256 samples of previous frame to the start of the current frame */
79 memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_BLOCK_SIZE * s->num_blocks],
80 AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
81
82 /* copy new samples for current frame */
83 memcpy(&s->planar_samples[ch][AC3_BLOCK_SIZE],
84 samples[s->channel_map[ch]],
85 AC3_BLOCK_SIZE * s->num_blocks * sizeof(s->planar_samples[0][0]));
86 }
87 }
88
89
90 /*
91 * Apply the MDCT to input samples to generate frequency coefficients.
92 * This applies the KBD window and normalizes the input to reduce precision
93 * loss due to fixed-point calculations.
94 */
95 static void apply_mdct(AC3EncodeContext *s)
96 {
97 int blk, ch;
98
99 for (ch = 0; ch < s->channels; ch++) {
100 for (blk = 0; blk < s->num_blocks; blk++) {
101 AC3Block *block = &s->blocks[blk];
102 const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
103
104 #if CONFIG_AC3ENC_FLOAT
105 s->fdsp.vector_fmul(s->windowed_samples, input_samples,
106 s->mdct_window, AC3_WINDOW_SIZE);
107 #else
108 s->ac3dsp.apply_window_int16(s->windowed_samples, input_samples,
109 s->mdct_window, AC3_WINDOW_SIZE);
110 #endif
111
112 if (s->fixed_point)
113 block->coeff_shift[ch+1] = normalize_samples(s);
114
115 s->mdct.mdct_calcw(&s->mdct, block->mdct_coef[ch+1],
116 s->windowed_samples);
117 }
118 }
119 }
120
121
122 /*
123 * Calculate coupling channel and coupling coordinates.
124 */
125 static void apply_channel_coupling(AC3EncodeContext *s)
126 {
127 LOCAL_ALIGNED_16(CoefType, cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
128 #if CONFIG_AC3ENC_FLOAT
129 LOCAL_ALIGNED_16(int32_t, fixed_cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
130 #else
131 int32_t (*fixed_cpl_coords)[AC3_MAX_CHANNELS][16] = cpl_coords;
132 #endif
133 int blk, ch, bnd, i, j;
134 CoefSumType energy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16] = {{{0}}};
135 int cpl_start, num_cpl_coefs;
136
137 memset(cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
138 #if CONFIG_AC3ENC_FLOAT
139 memset(fixed_cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
140 #endif
141
142 /* align start to 16-byte boundary. align length to multiple of 32.
143 note: coupling start bin % 4 will always be 1 */
144 cpl_start = s->start_freq[CPL_CH] - 1;
145 num_cpl_coefs = FFALIGN(s->num_cpl_subbands * 12 + 1, 32);
146 cpl_start = FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs;
147
148 /* calculate coupling channel from fbw channels */
149 for (blk = 0; blk < s->num_blocks; blk++) {
150 AC3Block *block = &s->blocks[blk];
151 CoefType *cpl_coef = &block->mdct_coef[CPL_CH][cpl_start];
152 if (!block->cpl_in_use)
153 continue;
154 memset(cpl_coef, 0, num_cpl_coefs * sizeof(*cpl_coef));
155 for (ch = 1; ch <= s->fbw_channels; ch++) {
156 CoefType *ch_coef = &block->mdct_coef[ch][cpl_start];
157 if (!block->channel_in_cpl[ch])
158 continue;
159 for (i = 0; i < num_cpl_coefs; i++)
160 cpl_coef[i] += ch_coef[i];
161 }
162
163 /* coefficients must be clipped in order to be encoded */
164 clip_coefficients(&s->dsp, cpl_coef, num_cpl_coefs);
165 }
166
167 /* calculate energy in each band in coupling channel and each fbw channel */
168 /* TODO: possibly use SIMD to speed up energy calculation */
169 bnd = 0;
170 i = s->start_freq[CPL_CH];
171 while (i < s->cpl_end_freq) {
172 int band_size = s->cpl_band_sizes[bnd];
173 for (ch = CPL_CH; ch <= s->fbw_channels; ch++) {
174 for (blk = 0; blk < s->num_blocks; blk++) {
175 AC3Block *block = &s->blocks[blk];
176 if (!block->cpl_in_use || (ch > CPL_CH && !block->channel_in_cpl[ch]))
177 continue;
178 for (j = 0; j < band_size; j++) {
179 CoefType v = block->mdct_coef[ch][i+j];
180 MAC_COEF(energy[blk][ch][bnd], v, v);
181 }
182 }
183 }
184 i += band_size;
185 bnd++;
186 }
187
188 /* calculate coupling coordinates for all blocks for all channels */
189 for (blk = 0; blk < s->num_blocks; blk++) {
190 AC3Block *block = &s->blocks[blk];
191 if (!block->cpl_in_use)
192 continue;
193 for (ch = 1; ch <= s->fbw_channels; ch++) {
194 if (!block->channel_in_cpl[ch])
195 continue;
196 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
197 cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd],
198 energy[blk][CPL_CH][bnd]);
199 }
200 }
201 }
202
203 /* determine which blocks to send new coupling coordinates for */
204 for (blk = 0; blk < s->num_blocks; blk++) {
205 AC3Block *block = &s->blocks[blk];
206 AC3Block *block0 = blk ? &s->blocks[blk-1] : NULL;
207
208 memset(block->new_cpl_coords, 0, sizeof(block->new_cpl_coords));
209
210 if (block->cpl_in_use) {
211 /* send new coordinates if this is the first block, if previous
212 * block did not use coupling but this block does, the channels
213 * using coupling has changed from the previous block, or the
214 * coordinate difference from the last block for any channel is
215 * greater than a threshold value. */
216 if (blk == 0 || !block0->cpl_in_use) {
217 for (ch = 1; ch <= s->fbw_channels; ch++)
218 block->new_cpl_coords[ch] = 1;
219 } else {
220 for (ch = 1; ch <= s->fbw_channels; ch++) {
221 if (!block->channel_in_cpl[ch])
222 continue;
223 if (!block0->channel_in_cpl[ch]) {
224 block->new_cpl_coords[ch] = 1;
225 } else {
226 CoefSumType coord_diff = 0;
227 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
228 coord_diff += FFABS(cpl_coords[blk-1][ch][bnd] -
229 cpl_coords[blk ][ch][bnd]);
230 }
231 coord_diff /= s->num_cpl_bands;
232 if (coord_diff > NEW_CPL_COORD_THRESHOLD)
233 block->new_cpl_coords[ch] = 1;
234 }
235 }
236 }
237 }
238 }
239
240 /* calculate final coupling coordinates, taking into account reusing of
241 coordinates in successive blocks */
242 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
243 blk = 0;
244 while (blk < s->num_blocks) {
245 int av_uninit(blk1);
246 AC3Block *block = &s->blocks[blk];
247
248 if (!block->cpl_in_use) {
249 blk++;
250 continue;
251 }
252
253 for (ch = 1; ch <= s->fbw_channels; ch++) {
254 CoefSumType energy_ch, energy_cpl;
255 if (!block->channel_in_cpl[ch])
256 continue;
257 energy_cpl = energy[blk][CPL_CH][bnd];
258 energy_ch = energy[blk][ch][bnd];
259 blk1 = blk+1;
260 while (!s->blocks[blk1].new_cpl_coords[ch] && blk1 < s->num_blocks) {
261 if (s->blocks[blk1].cpl_in_use) {
262 energy_cpl += energy[blk1][CPL_CH][bnd];
263 energy_ch += energy[blk1][ch][bnd];
264 }
265 blk1++;
266 }
267 cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl);
268 }
269 blk = blk1;
270 }
271 }
272
273 /* calculate exponents/mantissas for coupling coordinates */
274 for (blk = 0; blk < s->num_blocks; blk++) {
275 AC3Block *block = &s->blocks[blk];
276 if (!block->cpl_in_use)
277 continue;
278
279 #if CONFIG_AC3ENC_FLOAT
280 s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1],
281 cpl_coords[blk][1],
282 s->fbw_channels * 16);
283 #endif
284 s->ac3dsp.extract_exponents(block->cpl_coord_exp[1],
285 fixed_cpl_coords[blk][1],
286 s->fbw_channels * 16);
287
288 for (ch = 1; ch <= s->fbw_channels; ch++) {
289 int bnd, min_exp, max_exp, master_exp;
290
291 if (!block->new_cpl_coords[ch])
292 continue;
293
294 /* determine master exponent */
295 min_exp = max_exp = block->cpl_coord_exp[ch][0];
296 for (bnd = 1; bnd < s->num_cpl_bands; bnd++) {
297 int exp = block->cpl_coord_exp[ch][bnd];
298 min_exp = FFMIN(exp, min_exp);
299 max_exp = FFMAX(exp, max_exp);
300 }
301 master_exp = ((max_exp - 15) + 2) / 3;
302 master_exp = FFMAX(master_exp, 0);
303 while (min_exp < master_exp * 3)
304 master_exp--;
305 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
306 block->cpl_coord_exp[ch][bnd] = av_clip(block->cpl_coord_exp[ch][bnd] -
307 master_exp * 3, 0, 15);
308 }
309 block->cpl_master_exp[ch] = master_exp;
310
311 /* quantize mantissas */
312 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
313 int cpl_exp = block->cpl_coord_exp[ch][bnd];
314 int cpl_mant = (fixed_cpl_coords[blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
315 if (cpl_exp == 15)
316 cpl_mant >>= 1;
317 else
318 cpl_mant -= 16;
319
320 block->cpl_coord_mant[ch][bnd] = cpl_mant;
321 }
322 }
323 }
324
325 if (CONFIG_EAC3_ENCODER && s->eac3)
326 ff_eac3_set_cpl_states(s);
327 }
328
329
330 /*
331 * Determine rematrixing flags for each block and band.
332 */
333 static void compute_rematrixing_strategy(AC3EncodeContext *s)
334 {
335 int nb_coefs;
336 int blk, bnd, i;
337 AC3Block *block, *block0;
338
339 if (s->channel_mode != AC3_CHMODE_STEREO)
340 return;
341
342 for (blk = 0; blk < s->num_blocks; blk++) {
343 block = &s->blocks[blk];
344 block->new_rematrixing_strategy = !blk;
345
346 block->num_rematrixing_bands = 4;
347 if (block->cpl_in_use) {
348 block->num_rematrixing_bands -= (s->start_freq[CPL_CH] <= 61);
349 block->num_rematrixing_bands -= (s->start_freq[CPL_CH] == 37);
350 if (blk && block->num_rematrixing_bands != block0->num_rematrixing_bands)
351 block->new_rematrixing_strategy = 1;
352 }
353 nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
354
355 if (!s->rematrixing_enabled) {
356 block0 = block;
357 continue;
358 }
359
360 for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
361 /* calculate calculate sum of squared coeffs for one band in one block */
362 int start = ff_ac3_rematrix_band_tab[bnd];
363 int end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
364 CoefSumType sum[4] = {0,};
365 for (i = start; i < end; i++) {
366 CoefType lt = block->mdct_coef[1][i];
367 CoefType rt = block->mdct_coef[2][i];
368 CoefType md = lt + rt;
369 CoefType sd = lt - rt;
370 MAC_COEF(sum[0], lt, lt);
371 MAC_COEF(sum[1], rt, rt);
372 MAC_COEF(sum[2], md, md);
373 MAC_COEF(sum[3], sd, sd);
374 }
375
376 /* compare sums to determine if rematrixing will be used for this band */
377 if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
378 block->rematrixing_flags[bnd] = 1;
379 else
380 block->rematrixing_flags[bnd] = 0;
381
382 /* determine if new rematrixing flags will be sent */
383 if (blk &&
384 block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
385 block->new_rematrixing_strategy = 1;
386 }
387 }
388 block0 = block;
389 }
390 }
391
392
393 int AC3_NAME(encode_frame)(AVCodecContext *avctx, AVPacket *avpkt,
394 const AVFrame *frame, int *got_packet_ptr)
395 {
396 AC3EncodeContext *s = avctx->priv_data;
397 int ret;
398
399 if (s->options.allow_per_frame_metadata) {
400 ret = ff_ac3_validate_metadata(s);
401 if (ret)
402 return ret;
403 }
404
405 if (s->bit_alloc.sr_code == 1 || s->eac3)
406 ff_ac3_adjust_frame_size(s);
407
408 copy_input_samples(s, (SampleType **)frame->extended_data);
409
410 apply_mdct(s);
411
412 if (s->fixed_point)
413 scale_coefficients(s);
414
415 clip_coefficients(&s->dsp, s->blocks[0].mdct_coef[1],
416 AC3_MAX_COEFS * s->num_blocks * s->channels);
417
418 s->cpl_on = s->cpl_enabled;
419 ff_ac3_compute_coupling_strategy(s);
420
421 if (s->cpl_on)
422 apply_channel_coupling(s);
423
424 compute_rematrixing_strategy(s);
425
426 if (!s->fixed_point)
427 scale_coefficients(s);
428
429 ff_ac3_apply_rematrixing(s);
430
431 ff_ac3_process_exponents(s);
432
433 ret = ff_ac3_compute_bit_allocation(s);
434 if (ret) {
435 av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
436 return ret;
437 }
438
439 ff_ac3_group_exponents(s);
440
441 ff_ac3_quantize_mantissas(s);
442
443 if ((ret = ff_alloc_packet(avpkt, s->frame_size))) {
444 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
445 return ret;
446 }
447 ff_ac3_output_frame(s, avpkt->data);
448
449 if (frame->pts != AV_NOPTS_VALUE)
450 avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
451
452 *got_packet_ptr = 1;
453 return 0;
454 }