~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/ac3enc_template.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include <stdint.h>
30
30
 
31
 
#include "ac3enc.h"
 
31
 
 
32
/* prototypes for static functions in ac3enc_fixed.c and ac3enc_float.c */
 
33
 
 
34
static void scale_coefficients(AC3EncodeContext *s);
 
35
 
 
36
static void apply_window(DSPContext *dsp, SampleType *output,
 
37
                         const SampleType *input, const SampleType *window,
 
38
                         unsigned int len);
 
39
 
 
40
static int normalize_samples(AC3EncodeContext *s);
 
41
 
 
42
static void clip_coefficients(DSPContext *dsp, CoefType *coef, unsigned int len);
 
43
 
 
44
static CoefType calc_cpl_coord(CoefSumType energy_ch, CoefSumType energy_cpl);
32
45
 
33
46
 
34
47
int AC3_NAME(allocate_sample_buffers)(AC3EncodeContext *s)
51
64
}
52
65
 
53
66
 
54
 
/**
 
67
/*
55
68
 * Deinterleave input samples.
56
69
 * Channels are reordered from Libav's default order to AC-3 order.
57
70
 */
58
 
void AC3_NAME(deinterleave_input_samples)(AC3EncodeContext *s,
59
 
                                          const SampleType *samples)
 
71
static void deinterleave_input_samples(AC3EncodeContext *s,
 
72
                                       const SampleType *samples)
60
73
{
61
74
    int ch, i;
62
75
 
66
79
        int sinc;
67
80
 
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]));
71
84
 
72
85
        /* deinterleave */
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;
77
90
            sptr += sinc;
78
91
        }
80
93
}
81
94
 
82
95
 
83
 
/**
 
96
/*
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.
87
100
 */
88
 
void AC3_NAME(apply_mdct)(AC3EncodeContext *s)
 
101
static void apply_mdct(AC3EncodeContext *s)
89
102
{
90
103
    int blk, ch;
91
104
 
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];
96
109
 
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);
99
112
 
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);
102
115
 
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);
105
118
        }
106
119
    }
107
120
}
108
121
 
109
122
 
110
 
/**
111
 
 * Calculate a single coupling coordinate.
112
 
 */
113
 
static inline float calc_cpl_coord(float energy_ch, float energy_cpl)
114
 
{
115
 
    float coord = 0.125;
116
 
    if (energy_cpl > 0)
117
 
        coord *= sqrtf(energy_ch / energy_cpl);
118
 
    return coord;
119
 
}
120
 
 
121
 
 
122
 
/**
 
123
/*
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.
129
125
 */
130
 
void AC3_NAME(apply_channel_coupling)(AC3EncodeContext *s)
 
126
static void apply_channel_coupling(AC3EncodeContext *s)
131
127
{
 
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]);
 
131
#else
 
132
    int32_t (*fixed_cpl_coords)[AC3_MAX_CHANNELS][16] = cpl_coords;
 
133
#endif
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;
138
137
 
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));
 
141
#endif
141
142
 
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 */
146
147
    cpl_start     = FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs;
147
148
 
148
149
    /* calculate coupling channel from fbw channels */
149
 
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
 
150
    for (blk = 0; blk < s->num_blocks; blk++) {
150
151
        AC3Block *block = &s->blocks[blk];
151
152
        CoefType *cpl_coef = &block->mdct_coef[CPL_CH][cpl_start];
152
153
        if (!block->cpl_in_use)
160
161
                cpl_coef[i] += ch_coef[i];
161
162
        }
162
163
 
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);
165
 
 
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);
169
166
    }
170
167
 
171
168
    /* calculate energy in each band in coupling channel and each fbw channel */
175
172
    while (i < s->cpl_end_freq) {
176
173
        int band_size = s->cpl_band_sizes[bnd];
177
174
        for (ch = CPL_CH; ch <= s->fbw_channels; ch++) {
178
 
            for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
 
175
            for (blk = 0; blk < s->num_blocks; blk++) {
179
176
                AC3Block *block = &s->blocks[blk];
180
177
                if (!block->cpl_in_use || (ch > CPL_CH && !block->channel_in_cpl[ch]))
181
178
                    continue;
189
186
        bnd++;
190
187
    }
191
188
 
 
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)
 
193
            continue;
 
194
        for (ch = 1; ch <= s->fbw_channels; ch++) {
 
195
            if (!block->channel_in_cpl[ch])
 
196
                continue;
 
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]);
 
200
            }
 
201
        }
 
202
    }
 
203
 
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;
196
 
        int new_coords = 0;
197
 
        CoefSumType coord_diff[AC3_MAX_CHANNELS] = {0,};
 
208
 
 
209
        memset(block->new_cpl_coords, 0, sizeof(block->new_cpl_coords));
198
210
 
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])
204
 
                    continue;
205
 
 
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]);
213
 
                    }
214
 
                }
215
 
                coord_diff[ch] /= s->num_cpl_bands;
216
 
            }
217
 
 
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. */
223
 
            if (blk == 0) {
224
 
                new_coords = 1;
225
 
            } else if (!block0->cpl_in_use) {
226
 
                new_coords = 1;
 
217
            if (blk == 0 || !block0->cpl_in_use) {
 
218
                for (ch = 1; ch <= s->fbw_channels; ch++)
 
219
                    block->new_cpl_coords[ch] = 1;
227
220
            } else {
228
221
                for (ch = 1; ch <= s->fbw_channels; ch++) {
229
 
                    if (block->channel_in_cpl[ch] && !block0->channel_in_cpl[ch]) {
230
 
                        new_coords = 1;
231
 
                        break;
232
 
                    }
233
 
                }
234
 
                if (!new_coords) {
235
 
                    for (ch = 1; ch <= s->fbw_channels; ch++) {
236
 
                        if (block->channel_in_cpl[ch] && coord_diff[ch] > 0.04) {
237
 
                            new_coords = 1;
238
 
                            break;
 
222
                    if (!block->channel_in_cpl[ch])
 
223
                        continue;
 
224
                    if (!block0->channel_in_cpl[ch]) {
 
225
                        block->new_cpl_coords[ch] = 1;
 
226
                    } else {
 
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]);
239
231
                        }
 
232
                        coord_diff /= s->num_cpl_bands;
 
233
                        if (coord_diff > NEW_CPL_COORD_THRESHOLD)
 
234
                            block->new_cpl_coords[ch] = 1;
240
235
                    }
241
236
                }
242
237
            }
243
238
        }
244
 
        block->new_cpl_coords = new_coords;
245
239
    }
246
240
 
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++) {
250
244
        blk = 0;
251
 
        while (blk < AC3_MAX_BLOCKS) {
252
 
            int blk1;
253
 
            CoefSumType energy_cpl;
 
245
        while (blk < s->num_blocks) {
 
246
            int av_uninit(blk1);
254
247
            AC3Block *block  = &s->blocks[blk];
255
248
 
256
249
            if (!block->cpl_in_use) {
258
251
                continue;
259
252
            }
260
253
 
261
 
            energy_cpl = energy[blk][CPL_CH][bnd];
262
 
            blk1 = blk+1;
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];
266
 
                blk1++;
267
 
            }
268
 
 
269
254
            for (ch = 1; ch <= s->fbw_channels; ch++) {
270
 
                CoefType energy_ch;
 
255
                CoefSumType energy_ch, energy_cpl;
271
256
                if (!block->channel_in_cpl[ch])
272
257
                    continue;
 
258
                energy_cpl = energy[blk][CPL_CH][bnd];
273
259
                energy_ch = energy[blk][ch][bnd];
274
260
                blk1 = blk+1;
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];
 
265
                    }
278
266
                    blk1++;
279
267
                }
280
268
                cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl);
284
272
    }
285
273
 
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)
290
278
            continue;
291
279
 
 
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);
 
284
#endif
295
285
        s->ac3dsp.extract_exponents(block->cpl_coord_exp[1],
296
286
                                    fixed_cpl_coords[blk][1],
297
287
                                    s->fbw_channels * 16);
299
289
        for (ch = 1; ch <= s->fbw_channels; ch++) {
300
290
            int bnd, min_exp, max_exp, master_exp;
301
291
 
 
292
            if (!block->new_cpl_coords[ch])
 
293
                continue;
 
294
 
302
295
            /* determine master exponent */
303
296
            min_exp = max_exp = block->cpl_coord_exp[ch][0];
304
297
            for (bnd = 1; bnd < s->num_cpl_bands; bnd++) {
332
325
 
333
326
    if (CONFIG_EAC3_ENCODER && s->eac3)
334
327
        ff_eac3_set_cpl_states(s);
335
 
#endif /* CONFIG_AC3ENC_FLOAT */
336
328
}
337
329
 
338
330
 
339
 
/**
 
331
/*
340
332
 * Determine rematrixing flags for each block and band.
341
333
 */
342
 
void AC3_NAME(compute_rematrixing_strategy)(AC3EncodeContext *s)
 
334
static void compute_rematrixing_strategy(AC3EncodeContext *s)
343
335
{
344
336
    int nb_coefs;
345
337
    int blk, bnd, i;
348
340
    if (s->channel_mode != AC3_CHMODE_STEREO)
349
341
        return;
350
342
 
351
 
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
 
343
    for (blk = 0; blk < s->num_blocks; blk++) {
352
344
        block = &s->blocks[blk];
353
345
        block->new_rematrixing_strategy = !blk;
354
346
 
355
 
        if (!s->rematrixing_enabled) {
356
 
            block0 = block;
357
 
            continue;
358
 
        }
359
 
 
360
347
        block->num_rematrixing_bands = 4;
361
348
        if (block->cpl_in_use) {
362
349
            block->num_rematrixing_bands -= (s->start_freq[CPL_CH] <= 61);
366
353
        }
367
354
        nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
368
355
 
 
356
        if (!s->rematrixing_enabled) {
 
357
            block0 = block;
 
358
            continue;
 
359
        }
 
360
 
369
361
        for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
370
362
            /* calculate calculate sum of squared coeffs for one band in one block */
371
363
            int start = ff_ac3_rematrix_band_tab[bnd];
397
389
        block0 = block;
398
390
    }
399
391
}
 
392
 
 
393
 
 
394
int AC3_NAME(encode_frame)(AVCodecContext *avctx, unsigned char *frame,
 
395
                           int buf_size, void *data)
 
396
{
 
397
    AC3EncodeContext *s = avctx->priv_data;
 
398
    const SampleType *samples = data;
 
399
    int ret;
 
400
 
 
401
    if (s->options.allow_per_frame_metadata) {
 
402
        ret = ff_ac3_validate_metadata(s);
 
403
        if (ret)
 
404
            return ret;
 
405
    }
 
406
 
 
407
    if (s->bit_alloc.sr_code == 1 || s->eac3)
 
408
        ff_ac3_adjust_frame_size(s);
 
409
 
 
410
    deinterleave_input_samples(s, samples);
 
411
 
 
412
    apply_mdct(s);
 
413
 
 
414
    if (s->fixed_point)
 
415
        scale_coefficients(s);
 
416
 
 
417
    clip_coefficients(&s->dsp, s->blocks[0].mdct_coef[1],
 
418
                      AC3_MAX_COEFS * s->num_blocks * s->channels);
 
419
 
 
420
    s->cpl_on = s->cpl_enabled;
 
421
    ff_ac3_compute_coupling_strategy(s);
 
422
 
 
423
    if (s->cpl_on)
 
424
        apply_channel_coupling(s);
 
425
 
 
426
    compute_rematrixing_strategy(s);
 
427
 
 
428
    if (!s->fixed_point)
 
429
        scale_coefficients(s);
 
430
 
 
431
    ff_ac3_apply_rematrixing(s);
 
432
 
 
433
    ff_ac3_process_exponents(s);
 
434
 
 
435
    ret = ff_ac3_compute_bit_allocation(s);
 
436
    if (ret) {
 
437
        av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
 
438
        return ret;
 
439
    }
 
440
 
 
441
    ff_ac3_group_exponents(s);
 
442
 
 
443
    ff_ac3_quantize_mantissas(s);
 
444
 
 
445
    ff_ac3_output_frame(s, frame);
 
446
 
 
447
    return s->frame_size;
 
448
}