00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00029 #include <stdint.h>
00030
00031
00032
00033
00034 static void scale_coefficients(AC3EncodeContext *s);
00035
00036 static void apply_window(void *dsp, SampleType *output,
00037 const SampleType *input, const SampleType *window,
00038 unsigned int len);
00039
00040 static int normalize_samples(AC3EncodeContext *s);
00041
00042 static void clip_coefficients(DSPContext *dsp, CoefType *coef, unsigned int len);
00043
00044 static CoefType calc_cpl_coord(CoefSumType energy_ch, CoefSumType energy_cpl);
00045
00046 static void sum_square_butterfly(AC3EncodeContext *s, CoefSumType sum[4],
00047 const CoefType *coef0, const CoefType *coef1,
00048 int len);
00049
00050 int AC3_NAME(allocate_sample_buffers)(AC3EncodeContext *s)
00051 {
00052 int ch;
00053
00054 FF_ALLOC_OR_GOTO(s->avctx, s->windowed_samples, AC3_WINDOW_SIZE *
00055 sizeof(*s->windowed_samples), alloc_fail);
00056 FF_ALLOC_OR_GOTO(s->avctx, s->planar_samples, s->channels * sizeof(*s->planar_samples),
00057 alloc_fail);
00058 for (ch = 0; ch < s->channels; ch++) {
00059 FF_ALLOCZ_OR_GOTO(s->avctx, s->planar_samples[ch],
00060 (AC3_FRAME_SIZE+AC3_BLOCK_SIZE) * sizeof(**s->planar_samples),
00061 alloc_fail);
00062 }
00063
00064 return 0;
00065 alloc_fail:
00066 return AVERROR(ENOMEM);
00067 }
00068
00069
00070
00071
00072
00073
00074 static void copy_input_samples(AC3EncodeContext *s, SampleType **samples)
00075 {
00076 int ch;
00077
00078
00079 for (ch = 0; ch < s->channels; ch++) {
00080
00081 memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_BLOCK_SIZE * s->num_blocks],
00082 AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
00083
00084
00085 memcpy(&s->planar_samples[ch][AC3_BLOCK_SIZE],
00086 samples[s->channel_map[ch]],
00087 AC3_BLOCK_SIZE * s->num_blocks * sizeof(s->planar_samples[0][0]));
00088 }
00089 }
00090
00091
00092
00093
00094
00095
00096
00097 static void apply_mdct(AC3EncodeContext *s)
00098 {
00099 int blk, ch;
00100
00101 for (ch = 0; ch < s->channels; ch++) {
00102 for (blk = 0; blk < s->num_blocks; blk++) {
00103 AC3Block *block = &s->blocks[blk];
00104 const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
00105
00106 #if CONFIG_AC3ENC_FLOAT
00107 apply_window(&s->fdsp, s->windowed_samples, input_samples,
00108 s->mdct_window, AC3_WINDOW_SIZE);
00109 #else
00110 apply_window(&s->dsp, s->windowed_samples, input_samples,
00111 s->mdct_window, AC3_WINDOW_SIZE);
00112 #endif
00113
00114 if (s->fixed_point)
00115 block->coeff_shift[ch+1] = normalize_samples(s);
00116
00117 s->mdct.mdct_calcw(&s->mdct, block->mdct_coef[ch+1],
00118 s->windowed_samples);
00119 }
00120 }
00121 }
00122
00123
00124
00125
00126
00127 static void apply_channel_coupling(AC3EncodeContext *s)
00128 {
00129 LOCAL_ALIGNED_16(CoefType, cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
00130 #if CONFIG_AC3ENC_FLOAT
00131 LOCAL_ALIGNED_16(int32_t, fixed_cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
00132 #else
00133 int32_t (*fixed_cpl_coords)[AC3_MAX_CHANNELS][16] = cpl_coords;
00134 #endif
00135 int blk, ch, bnd, i, j;
00136 CoefSumType energy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16] = {{{0}}};
00137 int cpl_start, num_cpl_coefs;
00138
00139 memset(cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
00140 #if CONFIG_AC3ENC_FLOAT
00141 memset(fixed_cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
00142 #endif
00143
00144
00145
00146 cpl_start = s->start_freq[CPL_CH] - 1;
00147 num_cpl_coefs = FFALIGN(s->num_cpl_subbands * 12 + 1, 32);
00148 cpl_start = FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs;
00149
00150
00151 for (blk = 0; blk < s->num_blocks; blk++) {
00152 AC3Block *block = &s->blocks[blk];
00153 CoefType *cpl_coef = &block->mdct_coef[CPL_CH][cpl_start];
00154 if (!block->cpl_in_use)
00155 continue;
00156 memset(cpl_coef, 0, num_cpl_coefs * sizeof(*cpl_coef));
00157 for (ch = 1; ch <= s->fbw_channels; ch++) {
00158 CoefType *ch_coef = &block->mdct_coef[ch][cpl_start];
00159 if (!block->channel_in_cpl[ch])
00160 continue;
00161 for (i = 0; i < num_cpl_coefs; i++)
00162 cpl_coef[i] += ch_coef[i];
00163 }
00164
00165
00166 clip_coefficients(&s->dsp, cpl_coef, num_cpl_coefs);
00167 }
00168
00169
00170
00171 bnd = 0;
00172 i = s->start_freq[CPL_CH];
00173 while (i < s->cpl_end_freq) {
00174 int band_size = s->cpl_band_sizes[bnd];
00175 for (ch = CPL_CH; ch <= s->fbw_channels; ch++) {
00176 for (blk = 0; blk < s->num_blocks; blk++) {
00177 AC3Block *block = &s->blocks[blk];
00178 if (!block->cpl_in_use || (ch > CPL_CH && !block->channel_in_cpl[ch]))
00179 continue;
00180 for (j = 0; j < band_size; j++) {
00181 CoefType v = block->mdct_coef[ch][i+j];
00182 MAC_COEF(energy[blk][ch][bnd], v, v);
00183 }
00184 }
00185 }
00186 i += band_size;
00187 bnd++;
00188 }
00189
00190
00191 for (blk = 0; blk < s->num_blocks; blk++) {
00192 AC3Block *block = &s->blocks[blk];
00193 if (!block->cpl_in_use)
00194 continue;
00195 for (ch = 1; ch <= s->fbw_channels; ch++) {
00196 if (!block->channel_in_cpl[ch])
00197 continue;
00198 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
00199 cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd],
00200 energy[blk][CPL_CH][bnd]);
00201 }
00202 }
00203 }
00204
00205
00206 for (blk = 0; blk < s->num_blocks; blk++) {
00207 AC3Block *block = &s->blocks[blk];
00208 AC3Block *block0 = blk ? &s->blocks[blk-1] : NULL;
00209
00210 memset(block->new_cpl_coords, 0, sizeof(block->new_cpl_coords));
00211
00212 if (block->cpl_in_use) {
00213
00214
00215
00216
00217
00218 if (blk == 0 || !block0->cpl_in_use) {
00219 for (ch = 1; ch <= s->fbw_channels; ch++)
00220 block->new_cpl_coords[ch] = 1;
00221 } else {
00222 for (ch = 1; ch <= s->fbw_channels; ch++) {
00223 if (!block->channel_in_cpl[ch])
00224 continue;
00225 if (!block0->channel_in_cpl[ch]) {
00226 block->new_cpl_coords[ch] = 1;
00227 } else {
00228 CoefSumType coord_diff = 0;
00229 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
00230 coord_diff += FFABS(cpl_coords[blk-1][ch][bnd] -
00231 cpl_coords[blk ][ch][bnd]);
00232 }
00233 coord_diff /= s->num_cpl_bands;
00234 if (coord_diff > NEW_CPL_COORD_THRESHOLD)
00235 block->new_cpl_coords[ch] = 1;
00236 }
00237 }
00238 }
00239 }
00240 }
00241
00242
00243
00244 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
00245 blk = 0;
00246 while (blk < s->num_blocks) {
00247 int av_uninit(blk1);
00248 AC3Block *block = &s->blocks[blk];
00249
00250 if (!block->cpl_in_use) {
00251 blk++;
00252 continue;
00253 }
00254
00255 for (ch = 1; ch <= s->fbw_channels; ch++) {
00256 CoefSumType energy_ch, energy_cpl;
00257 if (!block->channel_in_cpl[ch])
00258 continue;
00259 energy_cpl = energy[blk][CPL_CH][bnd];
00260 energy_ch = energy[blk][ch][bnd];
00261 blk1 = blk+1;
00262 while (!s->blocks[blk1].new_cpl_coords[ch] && blk1 < s->num_blocks) {
00263 if (s->blocks[blk1].cpl_in_use) {
00264 energy_cpl += energy[blk1][CPL_CH][bnd];
00265 energy_ch += energy[blk1][ch][bnd];
00266 }
00267 blk1++;
00268 }
00269 cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl);
00270 }
00271 blk = blk1;
00272 }
00273 }
00274
00275
00276 for (blk = 0; blk < s->num_blocks; blk++) {
00277 AC3Block *block = &s->blocks[blk];
00278 if (!block->cpl_in_use)
00279 continue;
00280
00281 #if CONFIG_AC3ENC_FLOAT
00282 s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1],
00283 cpl_coords[blk][1],
00284 s->fbw_channels * 16);
00285 #endif
00286 s->ac3dsp.extract_exponents(block->cpl_coord_exp[1],
00287 fixed_cpl_coords[blk][1],
00288 s->fbw_channels * 16);
00289
00290 for (ch = 1; ch <= s->fbw_channels; ch++) {
00291 int bnd, min_exp, max_exp, master_exp;
00292
00293 if (!block->new_cpl_coords[ch])
00294 continue;
00295
00296
00297 min_exp = max_exp = block->cpl_coord_exp[ch][0];
00298 for (bnd = 1; bnd < s->num_cpl_bands; bnd++) {
00299 int exp = block->cpl_coord_exp[ch][bnd];
00300 min_exp = FFMIN(exp, min_exp);
00301 max_exp = FFMAX(exp, max_exp);
00302 }
00303 master_exp = ((max_exp - 15) + 2) / 3;
00304 master_exp = FFMAX(master_exp, 0);
00305 while (min_exp < master_exp * 3)
00306 master_exp--;
00307 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
00308 block->cpl_coord_exp[ch][bnd] = av_clip(block->cpl_coord_exp[ch][bnd] -
00309 master_exp * 3, 0, 15);
00310 }
00311 block->cpl_master_exp[ch] = master_exp;
00312
00313
00314 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
00315 int cpl_exp = block->cpl_coord_exp[ch][bnd];
00316 int cpl_mant = (fixed_cpl_coords[blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
00317 if (cpl_exp == 15)
00318 cpl_mant >>= 1;
00319 else
00320 cpl_mant -= 16;
00321
00322 block->cpl_coord_mant[ch][bnd] = cpl_mant;
00323 }
00324 }
00325 }
00326
00327 if (CONFIG_EAC3_ENCODER && s->eac3)
00328 ff_eac3_set_cpl_states(s);
00329 }
00330
00331
00332
00333
00334
00335 static void compute_rematrixing_strategy(AC3EncodeContext *s)
00336 {
00337 int nb_coefs;
00338 int blk, bnd;
00339 AC3Block *block, *block0;
00340
00341 if (s->channel_mode != AC3_CHMODE_STEREO)
00342 return;
00343
00344 for (blk = 0; blk < s->num_blocks; blk++) {
00345 block = &s->blocks[blk];
00346 block->new_rematrixing_strategy = !blk;
00347
00348 block->num_rematrixing_bands = 4;
00349 if (block->cpl_in_use) {
00350 block->num_rematrixing_bands -= (s->start_freq[CPL_CH] <= 61);
00351 block->num_rematrixing_bands -= (s->start_freq[CPL_CH] == 37);
00352 if (blk && block->num_rematrixing_bands != block0->num_rematrixing_bands)
00353 block->new_rematrixing_strategy = 1;
00354 }
00355 nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
00356
00357 if (!s->rematrixing_enabled) {
00358 block0 = block;
00359 continue;
00360 }
00361
00362 for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
00363
00364 int start = ff_ac3_rematrix_band_tab[bnd];
00365 int end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
00366 CoefSumType sum[4];
00367 sum_square_butterfly(s, sum, block->mdct_coef[1] + start,
00368 block->mdct_coef[2] + start, end - start);
00369
00370
00371 if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
00372 block->rematrixing_flags[bnd] = 1;
00373 else
00374 block->rematrixing_flags[bnd] = 0;
00375
00376
00377 if (blk &&
00378 block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
00379 block->new_rematrixing_strategy = 1;
00380 }
00381 }
00382 block0 = block;
00383 }
00384 }
00385
00386
00387 int AC3_NAME(encode_frame)(AVCodecContext *avctx, AVPacket *avpkt,
00388 const AVFrame *frame, int *got_packet_ptr)
00389 {
00390 AC3EncodeContext *s = avctx->priv_data;
00391 int ret;
00392
00393 if (s->options.allow_per_frame_metadata) {
00394 ret = ff_ac3_validate_metadata(s);
00395 if (ret)
00396 return ret;
00397 }
00398
00399 if (s->bit_alloc.sr_code == 1 || s->eac3)
00400 ff_ac3_adjust_frame_size(s);
00401
00402 copy_input_samples(s, (SampleType **)frame->extended_data);
00403
00404 apply_mdct(s);
00405
00406 if (s->fixed_point)
00407 scale_coefficients(s);
00408
00409 clip_coefficients(&s->dsp, s->blocks[0].mdct_coef[1],
00410 AC3_MAX_COEFS * s->num_blocks * s->channels);
00411
00412 s->cpl_on = s->cpl_enabled;
00413 ff_ac3_compute_coupling_strategy(s);
00414
00415 if (s->cpl_on)
00416 apply_channel_coupling(s);
00417
00418 compute_rematrixing_strategy(s);
00419
00420 if (!s->fixed_point)
00421 scale_coefficients(s);
00422
00423 ff_ac3_apply_rematrixing(s);
00424
00425 ff_ac3_process_exponents(s);
00426
00427 ret = ff_ac3_compute_bit_allocation(s);
00428 if (ret) {
00429 av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
00430 return ret;
00431 }
00432
00433 ff_ac3_group_exponents(s);
00434
00435 ff_ac3_quantize_mantissas(s);
00436
00437 if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size)))
00438 return ret;
00439 ff_ac3_output_frame(s, avpkt->data);
00440
00441 if (frame->pts != AV_NOPTS_VALUE)
00442 avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
00443
00444 *got_packet_ptr = 1;
00445 return 0;
00446 }