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>
7 * This file is part of Libav.
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.
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.
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
26 * AC-3 encoder float/fixed template
31 #include "libavutil/internal.h"
33 /* prototypes for static functions in ac3enc_fixed.c and ac3enc_float.c */
35 static void scale_coefficients(AC3EncodeContext
*s
);
37 static void apply_window(void *dsp
, SampleType
*output
,
38 const SampleType
*input
, const SampleType
*window
,
41 static int normalize_samples(AC3EncodeContext
*s
);
43 static void clip_coefficients(DSPContext
*dsp
, CoefType
*coef
, unsigned int len
);
45 static CoefType
calc_cpl_coord(CoefSumType energy_ch
, CoefSumType energy_cpl
);
48 int AC3_NAME(allocate_sample_buffers
)(AC3EncodeContext
*s
)
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
),
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
),
64 return AVERROR(ENOMEM
);
70 * Channels are reordered from Libav's default order to AC-3 order.
72 static void copy_input_samples(AC3EncodeContext
*s
, SampleType
**samples
)
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]));
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]));
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.
95 static void apply_mdct(AC3EncodeContext
*s
)
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
];
104 #if CONFIG_AC3ENC_FLOAT
105 apply_window(&s
->fdsp
, s
->windowed_samples
, input_samples
,
106 s
->mdct_window
, AC3_WINDOW_SIZE
);
108 apply_window(&s
->dsp
, s
->windowed_samples
, input_samples
,
109 s
->mdct_window
, AC3_WINDOW_SIZE
);
113 block
->coeff_shift
[ch
+1] = normalize_samples(s
);
115 s
->mdct
.mdct_calcw(&s
->mdct
, block
->mdct_coef
[ch
+1],
116 s
->windowed_samples
);
123 * Calculate coupling channel and coupling coordinates.
125 static void apply_channel_coupling(AC3EncodeContext
*s
)
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]);
131 int32_t (*fixed_cpl_coords
)[AC3_MAX_CHANNELS
][16] = cpl_coords
;
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
;
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
));
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
;
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
)
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
])
159 for (i
= 0; i
< num_cpl_coefs
; i
++)
160 cpl_coef
[i
] += ch_coef
[i
];
163 /* coefficients must be clipped in order to be encoded */
164 clip_coefficients(&s
->dsp
, cpl_coef
, num_cpl_coefs
);
167 /* calculate energy in each band in coupling channel and each fbw channel */
168 /* TODO: possibly use SIMD to speed up energy calculation */
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
]))
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
);
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
)
193 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
194 if (!block
->channel_in_cpl
[ch
])
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
]);
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
;
208 memset(block
->new_cpl_coords
, 0, sizeof(block
->new_cpl_coords
));
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;
220 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
221 if (!block
->channel_in_cpl
[ch
])
223 if (!block0
->channel_in_cpl
[ch
]) {
224 block
->new_cpl_coords
[ch
] = 1;
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
]);
231 coord_diff
/= s
->num_cpl_bands
;
232 if (coord_diff
> NEW_CPL_COORD_THRESHOLD
)
233 block
->new_cpl_coords
[ch
] = 1;
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
++) {
244 while (blk
< s
->num_blocks
) {
246 AC3Block
*block
= &s
->blocks
[blk
];
248 if (!block
->cpl_in_use
) {
253 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
254 CoefSumType energy_ch
, energy_cpl
;
255 if (!block
->channel_in_cpl
[ch
])
257 energy_cpl
= energy
[blk
][CPL_CH
][bnd
];
258 energy_ch
= energy
[blk
][ch
][bnd
];
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
];
267 cpl_coords
[blk
][ch
][bnd
] = calc_cpl_coord(energy_ch
, energy_cpl
);
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
)
279 #if CONFIG_AC3ENC_FLOAT
280 s
->ac3dsp
.float_to_fixed24(fixed_cpl_coords
[blk
][1],
282 s
->fbw_channels
* 16);
284 s
->ac3dsp
.extract_exponents(block
->cpl_coord_exp
[1],
285 fixed_cpl_coords
[blk
][1],
286 s
->fbw_channels
* 16);
288 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
289 int bnd
, min_exp
, max_exp
, master_exp
;
291 if (!block
->new_cpl_coords
[ch
])
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
);
301 master_exp
= ((max_exp
- 15) + 2) / 3;
302 master_exp
= FFMAX(master_exp
, 0);
303 while (min_exp
< master_exp
* 3)
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);
309 block
->cpl_master_exp
[ch
] = master_exp
;
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;
320 block
->cpl_coord_mant
[ch
][bnd
] = cpl_mant
;
325 if (CONFIG_EAC3_ENCODER
&& s
->eac3
)
326 ff_eac3_set_cpl_states(s
);
331 * Determine rematrixing flags for each block and band.
333 static void compute_rematrixing_strategy(AC3EncodeContext
*s
)
337 AC3Block
*block
, *block0
;
339 if (s
->channel_mode
!= AC3_CHMODE_STEREO
)
342 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
343 block
= &s
->blocks
[blk
];
344 block
->new_rematrixing_strategy
= !blk
;
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;
353 nb_coefs
= FFMIN(block
->end_freq
[1], block
->end_freq
[2]);
355 if (!s
->rematrixing_enabled
) {
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
);
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;
380 block
->rematrixing_flags
[bnd
] = 0;
382 /* determine if new rematrixing flags will be sent */
384 block
->rematrixing_flags
[bnd
] != block0
->rematrixing_flags
[bnd
]) {
385 block
->new_rematrixing_strategy
= 1;
393 int AC3_NAME(encode_frame
)(AVCodecContext
*avctx
, AVPacket
*avpkt
,
394 const AVFrame
*frame
, int *got_packet_ptr
)
396 AC3EncodeContext
*s
= avctx
->priv_data
;
399 if (s
->options
.allow_per_frame_metadata
) {
400 ret
= ff_ac3_validate_metadata(s
);
405 if (s
->bit_alloc
.sr_code
== 1 || s
->eac3
)
406 ff_ac3_adjust_frame_size(s
);
408 copy_input_samples(s
, (SampleType
**)frame
->extended_data
);
413 scale_coefficients(s
);
415 clip_coefficients(&s
->dsp
, s
->blocks
[0].mdct_coef
[1],
416 AC3_MAX_COEFS
* s
->num_blocks
* s
->channels
);
418 s
->cpl_on
= s
->cpl_enabled
;
419 ff_ac3_compute_coupling_strategy(s
);
422 apply_channel_coupling(s
);
424 compute_rematrixing_strategy(s
);
427 scale_coefficients(s
);
429 ff_ac3_apply_rematrixing(s
);
431 ff_ac3_process_exponents(s
);
433 ret
= ff_ac3_compute_bit_allocation(s
);
435 av_log(avctx
, AV_LOG_ERROR
, "Bit allocation failed. Try increasing the bitrate.\n");
439 ff_ac3_group_exponents(s
);
441 ff_ac3_quantize_mantissas(s
);
443 if ((ret
= ff_alloc_packet(avpkt
, s
->frame_size
))) {
444 av_log(avctx
, AV_LOG_ERROR
, "Error getting output packet\n");
447 ff_ac3_output_frame(s
, avpkt
->data
);
449 if (frame
->pts
!= AV_NOPTS_VALUE
)
450 avpkt
->pts
= frame
->pts
- ff_samples_to_time_base(avctx
, avctx
->delay
);