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