~ubuntu-branches/debian/sid/ffmpeg/sid

« back to all changes in this revision

Viewing changes to libavcodec/aacenc.c

  • Committer: Package Import Robot
  • Author(s): Andreas Cadhalpun, Fabian Greffrath, Andreas Cadhalpun
  • Date: 2015-09-22 15:15:20 UTC
  • mfrom: (0.1.29)
  • Revision ID: package-import@ubuntu.com-20150922151520-hhmd3in9ykigjvs9
Tags: 7:2.8-1
[ Fabian Greffrath ]
* Pass the --dbg-package=ffmpeg-dbg parameter only to dh_strip.
* Add alternative Depends: libavcodec-ffmpeg-extra56 to libavcodec-dev and
  ffmpeg-dbg to allow for building and debugging with this library installed.

[ Andreas Cadhalpun ]
* Import new major upstream release 2.8.
* Remove the transitional lib*-ffmpeg-dev packages.
* Drop old Breaks on kodi-bin.
* Drop workaround for sparc, which is no Debian architecture anymore.
* Re-enable x265 on alpha, as it's available again.
* Disable unavailable frei0r, opencv and x264 on mips64el.
* Disable libopenjpeg (#787275) and libschroedinger (#787957) decoders.
  (Closes: #786670)
* Disable libdc1394 on sparc64, because it links against the broken due to
  #790560 libudev1.
* Enable libsnappy support.
* Add new symbols.
* Update debian/copyright.
* Update debian/tests/encdec_list.txt.
* Add hls-only-seek-if-there-is-an-offset.patch. (Closes: #798189)
* Add 'Breaks: libavutil-ffmpeg54 (>= 8:0)' to the libraries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
/***********************************
28
28
 *              TODOs:
29
29
 * add sane pulse detection
30
 
 * add temporal noise shaping
31
30
 ***********************************/
32
31
 
33
32
#include "libavutil/float_dsp.h"
42
41
#include "aac.h"
43
42
#include "aactab.h"
44
43
#include "aacenc.h"
 
44
#include "aacenctab.h"
 
45
#include "aacenc_utils.h"
45
46
 
46
47
#include "psymodel.h"
47
48
 
48
 
#define AAC_MAX_CHANNELS 6
49
 
 
50
 
#define ERROR_IF(cond, ...) \
51
 
    if (cond) { \
52
 
        av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
53
 
        return AVERROR(EINVAL); \
54
 
    }
55
 
 
56
 
#define WARN_IF(cond, ...) \
57
 
    if (cond) { \
58
 
        av_log(avctx, AV_LOG_WARNING, __VA_ARGS__); \
59
 
    }
60
 
 
61
 
float ff_aac_pow34sf_tab[428];
62
 
 
63
 
static const uint8_t swb_size_1024_96[] = {
64
 
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
65
 
    12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
66
 
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
67
 
};
68
 
 
69
 
static const uint8_t swb_size_1024_64[] = {
70
 
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
71
 
    12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
72
 
    40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
73
 
};
74
 
 
75
 
static const uint8_t swb_size_1024_48[] = {
76
 
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
77
 
    12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
78
 
    32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
79
 
    96
80
 
};
81
 
 
82
 
static const uint8_t swb_size_1024_32[] = {
83
 
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
84
 
    12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
85
 
    32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
86
 
};
87
 
 
88
 
static const uint8_t swb_size_1024_24[] = {
89
 
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
90
 
    12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
91
 
    32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
92
 
};
93
 
 
94
 
static const uint8_t swb_size_1024_16[] = {
95
 
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
96
 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
97
 
    32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
98
 
};
99
 
 
100
 
static const uint8_t swb_size_1024_8[] = {
101
 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
102
 
    16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
103
 
    32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
104
 
};
105
 
 
106
 
static const uint8_t *swb_size_1024[] = {
107
 
    swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
108
 
    swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
109
 
    swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
110
 
    swb_size_1024_16, swb_size_1024_16, swb_size_1024_8,
111
 
    swb_size_1024_8
112
 
};
113
 
 
114
 
static const uint8_t swb_size_128_96[] = {
115
 
    4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
116
 
};
117
 
 
118
 
static const uint8_t swb_size_128_48[] = {
119
 
    4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
120
 
};
121
 
 
122
 
static const uint8_t swb_size_128_24[] = {
123
 
    4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
124
 
};
125
 
 
126
 
static const uint8_t swb_size_128_16[] = {
127
 
    4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
128
 
};
129
 
 
130
 
static const uint8_t swb_size_128_8[] = {
131
 
    4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
132
 
};
133
 
 
134
 
static const uint8_t *swb_size_128[] = {
135
 
    /* the last entry on the following row is swb_size_128_64 but is a
136
 
       duplicate of swb_size_128_96 */
137
 
    swb_size_128_96, swb_size_128_96, swb_size_128_96,
138
 
    swb_size_128_48, swb_size_128_48, swb_size_128_48,
139
 
    swb_size_128_24, swb_size_128_24, swb_size_128_16,
140
 
    swb_size_128_16, swb_size_128_16, swb_size_128_8,
141
 
    swb_size_128_8
142
 
};
143
 
 
144
 
/** default channel configurations */
145
 
static const uint8_t aac_chan_configs[6][5] = {
146
 
 {1, TYPE_SCE},                               // 1 channel  - single channel element
147
 
 {1, TYPE_CPE},                               // 2 channels - channel pair
148
 
 {2, TYPE_SCE, TYPE_CPE},                     // 3 channels - center + stereo
149
 
 {3, TYPE_SCE, TYPE_CPE, TYPE_SCE},           // 4 channels - front center + stereo + back center
150
 
 {3, TYPE_SCE, TYPE_CPE, TYPE_CPE},           // 5 channels - front center + stereo + back stereo
151
 
 {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
152
 
};
153
 
 
154
 
/**
155
 
 * Table to remap channels from libavcodec's default order to AAC order.
156
 
 */
157
 
static const uint8_t aac_chan_maps[AAC_MAX_CHANNELS][AAC_MAX_CHANNELS] = {
158
 
    { 0 },
159
 
    { 0, 1 },
160
 
    { 2, 0, 1 },
161
 
    { 2, 0, 1, 3 },
162
 
    { 2, 0, 1, 3, 4 },
163
 
    { 2, 0, 1, 4, 5, 3 },
164
 
};
165
 
 
166
49
/**
167
50
 * Make AAC audio config object.
168
51
 * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
173
56
    AACEncContext *s = avctx->priv_data;
174
57
 
175
58
    init_put_bits(&pb, avctx->extradata, avctx->extradata_size);
176
 
    put_bits(&pb, 5, 2); //object type - AAC-LC
 
59
    put_bits(&pb, 5, s->profile+1); //profile
177
60
    put_bits(&pb, 4, s->samplerate_index); //sample rate index
178
61
    put_bits(&pb, 4, s->channels);
179
62
    //GASpecificConfig
265
148
        s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);
266
149
    else
267
150
        for (i = 0; i < 1024; i += 128)
268
 
            s->mdct128.mdct_calc(&s->mdct128, sce->coeffs + i, output + i*2);
 
151
            s->mdct128.mdct_calc(&s->mdct128, &sce->coeffs[i], output + i*2);
269
152
    memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);
270
153
    memcpy(sce->pcoeffs, sce->coeffs, sizeof(sce->pcoeffs));
271
154
}
283
166
    put_bits(&s->pb, 1, info->use_kb_window[0]);
284
167
    if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
285
168
        put_bits(&s->pb, 6, info->max_sfb);
286
 
        put_bits(&s->pb, 1, 0);            // no prediction
 
169
        put_bits(&s->pb, 1, !!info->predictor_present);
287
170
    } else {
288
171
        put_bits(&s->pb, 4, info->max_sfb);
289
172
        for (w = 1; w < 8; w++)
312
195
static void adjust_frame_information(ChannelElement *cpe, int chans)
313
196
{
314
197
    int i, w, w2, g, ch;
315
 
    int start, maxsfb, cmaxsfb;
 
198
    int maxsfb, cmaxsfb;
316
199
 
317
200
    for (ch = 0; ch < chans; ch++) {
318
201
        IndividualChannelStream *ics = &cpe->ch[ch].ics;
319
 
        start = 0;
320
202
        maxsfb = 0;
321
203
        cpe->ch[ch].pulse.num_pulse = 0;
322
204
        for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
323
 
            for (w2 = 0; w2 < ics->group_len[w]; w2++) {
324
 
                start = (w+w2) * 128;
325
 
                for (g = 0; g < ics->num_swb; g++) {
326
 
                    //apply M/S
327
 
                    if (cpe->common_window && !ch && cpe->ms_mask[w*16 + g]) {
328
 
                        for (i = 0; i < ics->swb_sizes[g]; i++) {
329
 
                            cpe->ch[0].coeffs[start+i] = (cpe->ch[0].pcoeffs[start+i] + cpe->ch[1].pcoeffs[start+i]) * 0.5f;
330
 
                            cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].pcoeffs[start+i];
331
 
                        }
332
 
                    }
333
 
                    start += ics->swb_sizes[g];
334
 
                }
 
205
            for (w2 =  0; w2 < ics->group_len[w]; w2++) {
335
206
                for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w*16+cmaxsfb-1]; cmaxsfb--)
336
207
                    ;
337
208
                maxsfb = FFMAX(maxsfb, cmaxsfb);
371
242
    }
372
243
}
373
244
 
 
245
static void apply_intensity_stereo(ChannelElement *cpe)
 
246
{
 
247
    int w, w2, g, i;
 
248
    IndividualChannelStream *ics = &cpe->ch[0].ics;
 
249
    if (!cpe->common_window)
 
250
        return;
 
251
    for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
 
252
        for (w2 =  0; w2 < ics->group_len[w]; w2++) {
 
253
            int start = (w+w2) * 128;
 
254
            for (g = 0; g < ics->num_swb; g++) {
 
255
                int p  = -1 + 2 * (cpe->ch[1].band_type[w*16+g] - 14);
 
256
                float scale = cpe->ch[0].is_ener[w*16+g];
 
257
                if (!cpe->is_mask[w*16 + g]) {
 
258
                    start += ics->swb_sizes[g];
 
259
                    continue;
 
260
                }
 
261
                for (i = 0; i < ics->swb_sizes[g]; i++) {
 
262
                    float sum = (cpe->ch[0].coeffs[start+i] + p*cpe->ch[1].coeffs[start+i])*scale;
 
263
                    cpe->ch[0].coeffs[start+i] = sum;
 
264
                    cpe->ch[1].coeffs[start+i] = 0.0f;
 
265
                }
 
266
                start += ics->swb_sizes[g];
 
267
            }
 
268
        }
 
269
    }
 
270
}
 
271
 
 
272
static void apply_mid_side_stereo(ChannelElement *cpe)
 
273
{
 
274
    int w, w2, g, i;
 
275
    IndividualChannelStream *ics = &cpe->ch[0].ics;
 
276
    if (!cpe->common_window)
 
277
        return;
 
278
    for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
 
279
        for (w2 =  0; w2 < ics->group_len[w]; w2++) {
 
280
            int start = (w+w2) * 128;
 
281
            for (g = 0; g < ics->num_swb; g++) {
 
282
                if (!cpe->ms_mask[w*16 + g]) {
 
283
                    start += ics->swb_sizes[g];
 
284
                    continue;
 
285
                }
 
286
                for (i = 0; i < ics->swb_sizes[g]; i++) {
 
287
                    float L = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) * 0.5f;
 
288
                    float R = L - cpe->ch[1].coeffs[start+i];
 
289
                    cpe->ch[0].coeffs[start+i] = L;
 
290
                    cpe->ch[1].coeffs[start+i] = R;
 
291
                }
 
292
                start += ics->swb_sizes[g];
 
293
            }
 
294
        }
 
295
    }
 
296
}
 
297
 
374
298
/**
375
299
 * Encode scalefactor band coding type.
376
300
 */
378
302
{
379
303
    int w;
380
304
 
 
305
    if (s->coder->set_special_band_scalefactors)
 
306
        s->coder->set_special_band_scalefactors(s, sce);
 
307
 
381
308
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
382
309
        s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
383
310
}
389
316
                                 SingleChannelElement *sce)
390
317
{
391
318
    int diff, off_sf = sce->sf_idx[0], off_pns = sce->sf_idx[0] - NOISE_OFFSET;
392
 
    int noise_flag = 1;
 
319
    int off_is = 0, noise_flag = 1;
393
320
    int i, w;
394
321
 
395
322
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
402
329
                        put_bits(&s->pb, NOISE_PRE_BITS, diff + NOISE_PRE);
403
330
                        continue;
404
331
                    }
 
332
                } else if (sce->band_type[w*16 + i] == INTENSITY_BT  ||
 
333
                           sce->band_type[w*16 + i] == INTENSITY_BT2) {
 
334
                    diff = sce->sf_idx[w*16 + i] - off_is;
 
335
                    off_is = sce->sf_idx[w*16 + i];
405
336
                } else {
406
337
                    diff = sce->sf_idx[w*16 + i] - off_sf;
407
338
                    off_sf = sce->sf_idx[w*16 + i];
447
378
                start += sce->ics.swb_sizes[i];
448
379
                continue;
449
380
            }
450
 
            for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
451
 
                s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
452
 
                                                   sce->ics.swb_sizes[i],
 
381
            for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) {
 
382
                s->coder->quantize_and_encode_band(s, &s->pb,
 
383
                                                   &sce->coeffs[start + w2*128],
 
384
                                                   NULL, sce->ics.swb_sizes[i],
453
385
                                                   sce->sf_idx[w*16 + i],
454
386
                                                   sce->band_type[w*16 + i],
455
 
                                                   s->lambda);
 
387
                                                   s->lambda,
 
388
                                                   sce->ics.window_clipping[w]);
 
389
            }
456
390
            start += sce->ics.swb_sizes[i];
457
391
        }
458
392
    }
459
393
}
460
394
 
461
395
/**
 
396
 * Downscale spectral coefficients for near-clipping windows to avoid artifacts
 
397
 */
 
398
static void avoid_clipping(AACEncContext *s, SingleChannelElement *sce)
 
399
{
 
400
    int start, i, j, w;
 
401
 
 
402
    if (sce->ics.clip_avoidance_factor < 1.0f) {
 
403
        for (w = 0; w < sce->ics.num_windows; w++) {
 
404
            start = 0;
 
405
            for (i = 0; i < sce->ics.max_sfb; i++) {
 
406
                float *swb_coeffs = &sce->coeffs[start + w*128];
 
407
                for (j = 0; j < sce->ics.swb_sizes[i]; j++)
 
408
                    swb_coeffs[j] *= sce->ics.clip_avoidance_factor;
 
409
                start += sce->ics.swb_sizes[i];
 
410
            }
 
411
        }
 
412
    }
 
413
}
 
414
 
 
415
/**
462
416
 * Encode one channel of audio data.
463
417
 */
464
418
static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
466
420
                                     int common_window)
467
421
{
468
422
    put_bits(&s->pb, 8, sce->sf_idx[0]);
469
 
    if (!common_window)
 
423
    if (!common_window) {
470
424
        put_ics_info(s, &sce->ics);
 
425
        if (s->coder->encode_main_pred)
 
426
            s->coder->encode_main_pred(s, sce);
 
427
    }
471
428
    encode_band_info(s, sce);
472
429
    encode_scale_factors(avctx, s, sce);
473
430
    encode_pulses(s, &sce->pulse);
474
 
    put_bits(&s->pb, 1, 0); //tns
 
431
    put_bits(&s->pb, 1, !!sce->tns.present);
 
432
    if (s->coder->encode_tns_info)
 
433
        s->coder->encode_tns_info(s, sce);
475
434
    put_bits(&s->pb, 1, 0); //ssr
476
435
    encode_spectral_coeffs(s, sce);
477
436
    return 0;
529
488
    AACEncContext *s = avctx->priv_data;
530
489
    float **samples = s->planar_samples, *samples2, *la, *overlap;
531
490
    ChannelElement *cpe;
532
 
    int i, ch, w, g, chans, tag, start_ch, ret, ms_mode = 0;
 
491
    SingleChannelElement *sce;
 
492
    int i, ch, w, chans, tag, start_ch, ret;
 
493
    int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0;
533
494
    int chan_el_counter[4];
534
495
    FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
535
496
 
558
519
        for (ch = 0; ch < chans; ch++) {
559
520
            IndividualChannelStream *ics = &cpe->ch[ch].ics;
560
521
            int cur_channel = start_ch + ch;
 
522
            float clip_avoidance_factor;
561
523
            overlap  = &samples[cur_channel][0];
562
524
            samples2 = overlap + 1024;
563
525
            la       = samples2 + (448+64);
585
547
            ics->num_windows        = wi[ch].num_windows;
586
548
            ics->swb_sizes          = s->psy.bands    [ics->num_windows == 8];
587
549
            ics->num_swb            = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8];
 
550
            ics->swb_offset         = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
 
551
                                        ff_swb_offset_128 [s->samplerate_index]:
 
552
                                        ff_swb_offset_1024[s->samplerate_index];
 
553
            ics->tns_max_bands      = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
 
554
                                        ff_tns_max_bands_128 [s->samplerate_index]:
 
555
                                        ff_tns_max_bands_1024[s->samplerate_index];
 
556
            clip_avoidance_factor = 0.0f;
588
557
            for (w = 0; w < ics->num_windows; w++)
589
558
                ics->group_len[w] = wi[ch].grouping[w];
 
559
            for (w = 0; w < ics->num_windows; w++) {
 
560
                if (wi[ch].clipping[w] > CLIP_AVOIDANCE_FACTOR) {
 
561
                    ics->window_clipping[w] = 1;
 
562
                    clip_avoidance_factor = FFMAX(clip_avoidance_factor, wi[ch].clipping[w]);
 
563
                } else {
 
564
                    ics->window_clipping[w] = 0;
 
565
                }
 
566
            }
 
567
            if (clip_avoidance_factor > CLIP_AVOIDANCE_FACTOR) {
 
568
                ics->clip_avoidance_factor = CLIP_AVOIDANCE_FACTOR / clip_avoidance_factor;
 
569
            } else {
 
570
                ics->clip_avoidance_factor = 1.0f;
 
571
            }
590
572
 
591
573
            apply_window_and_mdct(s, &cpe->ch[ch], overlap);
592
574
            if (isnan(cpe->ch->coeffs[0])) {
593
575
                av_log(avctx, AV_LOG_ERROR, "Input contains NaN\n");
594
576
                return AVERROR(EINVAL);
595
577
            }
 
578
            avoid_clipping(s, &cpe->ch[ch]);
596
579
        }
597
580
        start_ch += chans;
598
581
    }
599
 
    if ((ret = ff_alloc_packet2(avctx, avpkt, 8192 * s->channels)) < 0)
 
582
    if ((ret = ff_alloc_packet2(avctx, avpkt, 8192 * s->channels, 0)) < 0)
600
583
        return ret;
601
584
    do {
602
585
        int frame_bits;
603
586
 
604
587
        init_put_bits(&s->pb, avpkt->data, avpkt->size);
605
588
 
606
 
        if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT))
 
589
        if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT))
607
590
            put_bitstream_info(s, LIBAVCODEC_IDENT);
608
591
        start_ch = 0;
609
592
        memset(chan_el_counter, 0, sizeof(chan_el_counter));
613
596
            tag      = s->chan_map[i+1];
614
597
            chans    = tag == TYPE_CPE ? 2 : 1;
615
598
            cpe      = &s->cpe[i];
 
599
            cpe->common_window = 0;
 
600
            memset(cpe->is_mask, 0, sizeof(cpe->is_mask));
 
601
            memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
616
602
            put_bits(&s->pb, 3, tag);
617
603
            put_bits(&s->pb, 4, chan_el_counter[tag]++);
618
 
            for (ch = 0; ch < chans; ch++)
619
 
                coeffs[ch] = cpe->ch[ch].coeffs;
 
604
            for (ch = 0; ch < chans; ch++) {
 
605
                sce = &cpe->ch[ch];
 
606
                coeffs[ch] = sce->coeffs;
 
607
                sce->ics.predictor_present = 0;
 
608
                memset(&sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used));
 
609
                memset(&sce->tns, 0, sizeof(TemporalNoiseShaping));
 
610
                for (w = 0; w < 128; w++)
 
611
                    if (sce->band_type[w] > RESERVED_BT)
 
612
                        sce->band_type[w] = 0;
 
613
            }
620
614
            s->psy.model->analyze(&s->psy, start_ch, coeffs, wi);
621
615
            for (ch = 0; ch < chans; ch++) {
622
616
                s->cur_channel = start_ch + ch;
623
617
                s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
624
618
            }
625
 
            cpe->common_window = 0;
626
619
            if (chans > 1
627
620
                && wi[0].window_type[0] == wi[1].window_type[0]
628
621
                && wi[0].window_shape   == wi[1].window_shape) {
635
628
                    }
636
629
                }
637
630
            }
 
631
            for (ch = 0; ch < chans; ch++) { /* TNS and PNS */
 
632
                sce = &cpe->ch[ch];
 
633
                s->cur_channel = start_ch + ch;
 
634
                if (s->options.pns && s->coder->search_for_pns)
 
635
                    s->coder->search_for_pns(s, avctx, sce);
 
636
                if (s->options.tns && s->coder->search_for_tns)
 
637
                    s->coder->search_for_tns(s, sce);
 
638
                if (s->options.tns && s->coder->apply_tns_filt)
 
639
                    s->coder->apply_tns_filt(s, sce);
 
640
                if (sce->tns.present)
 
641
                    tns_mode = 1;
 
642
            }
638
643
            s->cur_channel = start_ch;
639
 
            if (s->options.stereo_mode && cpe->common_window) {
640
 
                if (s->options.stereo_mode > 0) {
641
 
                    IndividualChannelStream *ics = &cpe->ch[0].ics;
642
 
                    for (w = 0; w < ics->num_windows; w += ics->group_len[w])
643
 
                        for (g = 0;  g < ics->num_swb; g++)
644
 
                            cpe->ms_mask[w*16+g] = 1;
645
 
                } else if (s->coder->search_for_ms) {
646
 
                    s->coder->search_for_ms(s, cpe, s->lambda);
647
 
                }
 
644
            if (s->options.intensity_stereo) { /* Intensity Stereo */
 
645
                if (s->coder->search_for_is)
 
646
                    s->coder->search_for_is(s, avctx, cpe);
 
647
                if (cpe->is_mode) is_mode = 1;
 
648
                apply_intensity_stereo(cpe);
 
649
            }
 
650
            if (s->options.pred) { /* Prediction */
 
651
                for (ch = 0; ch < chans; ch++) {
 
652
                    sce = &cpe->ch[ch];
 
653
                    s->cur_channel = start_ch + ch;
 
654
                    if (s->options.pred && s->coder->search_for_pred)
 
655
                        s->coder->search_for_pred(s, sce);
 
656
                    if (cpe->ch[ch].ics.predictor_present) pred_mode = 1;
 
657
                }
 
658
                if (s->coder->adjust_common_prediction)
 
659
                    s->coder->adjust_common_prediction(s, cpe);
 
660
                for (ch = 0; ch < chans; ch++) {
 
661
                    sce = &cpe->ch[ch];
 
662
                    s->cur_channel = start_ch + ch;
 
663
                    if (s->options.pred && s->coder->apply_main_pred)
 
664
                        s->coder->apply_main_pred(s, sce);
 
665
                }
 
666
                s->cur_channel = start_ch;
 
667
            }
 
668
            if (s->options.stereo_mode) { /* Mid/Side stereo */
 
669
                if (s->options.stereo_mode == -1 && s->coder->search_for_ms)
 
670
                    s->coder->search_for_ms(s, cpe);
 
671
                else if (cpe->common_window)
 
672
                    memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask));
 
673
                for (w = 0; w < 128; w++)
 
674
                    cpe->ms_mask[w] = cpe->is_mask[w] ? 0 : cpe->ms_mask[w];
 
675
                apply_mid_side_stereo(cpe);
648
676
            }
649
677
            adjust_frame_information(cpe, chans);
650
678
            if (chans == 2) {
651
679
                put_bits(&s->pb, 1, cpe->common_window);
652
680
                if (cpe->common_window) {
653
681
                    put_ics_info(s, &cpe->ch[0].ics);
 
682
                    if (s->coder->encode_main_pred)
 
683
                        s->coder->encode_main_pred(s, &cpe->ch[0]);
654
684
                    encode_ms_info(&s->pb, cpe);
655
685
                    if (cpe->ms_mode) ms_mode = 1;
656
686
                }
667
697
            s->psy.bitres.bits = frame_bits / s->channels;
668
698
            break;
669
699
        }
670
 
        if (ms_mode) {
 
700
        if (is_mode || ms_mode || tns_mode || pred_mode) {
671
701
            for (i = 0; i < s->chan_map[0]; i++) {
672
702
                // Must restore coeffs
673
703
                chans = tag == TYPE_CPE ? 2 : 1;
686
716
    avctx->frame_bits = put_bits_count(&s->pb);
687
717
 
688
718
    // rate control stuff
689
 
    if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
 
719
    if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
690
720
        float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits;
691
721
        s->lambda *= ratio;
692
722
        s->lambda = FFMIN(s->lambda, 65536.f);
710
740
    ff_mdct_end(&s->mdct1024);
711
741
    ff_mdct_end(&s->mdct128);
712
742
    ff_psy_end(&s->psy);
 
743
    ff_lpc_end(&s->lpc);
713
744
    if (s->psypp)
714
745
        ff_psy_preprocess_end(s->psypp);
715
746
    av_freep(&s->buffer.samples);
723
754
{
724
755
    int ret = 0;
725
756
 
726
 
    s->fdsp = avpriv_float_dsp_alloc(avctx->flags & CODEC_FLAG_BITEXACT);
 
757
    s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
727
758
    if (!s->fdsp)
728
759
        return AVERROR(ENOMEM);
729
760
 
746
777
    int ch;
747
778
    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->buffer.samples, s->channels, 3 * 1024 * sizeof(s->buffer.samples[0]), alloc_fail);
748
779
    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->cpe, s->chan_map[0], sizeof(ChannelElement), alloc_fail);
749
 
    FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + FF_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
 
780
    FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + AV_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
750
781
 
751
782
    for(ch = 0; ch < s->channels; ch++)
752
783
        s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
772
803
 
773
804
    s->channels = avctx->channels;
774
805
 
775
 
    ERROR_IF(i == 16
776
 
                || i >= (sizeof(swb_size_1024) / sizeof(*swb_size_1024))
777
 
                || i >= (sizeof(swb_size_128) / sizeof(*swb_size_128)),
 
806
    ERROR_IF(i == 16 || i >= ff_aac_swb_size_1024_len || i >= ff_aac_swb_size_128_len,
778
807
             "Unsupported sample rate %d\n", avctx->sample_rate);
779
808
    ERROR_IF(s->channels > AAC_MAX_CHANNELS,
780
809
             "Unsupported number of channels: %d\n", s->channels);
781
 
    ERROR_IF(avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW,
782
 
             "Unsupported profile %d\n", avctx->profile);
783
810
    WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
784
811
             "Too many bits per frame requested, clamping to max\n");
 
812
    if (avctx->profile == FF_PROFILE_AAC_MAIN) {
 
813
        s->options.pred = 1;
 
814
    } else if ((avctx->profile == FF_PROFILE_AAC_LOW ||
 
815
                avctx->profile == FF_PROFILE_UNKNOWN) && s->options.pred) {
 
816
        s->profile = 0; /* Main */
 
817
        WARN_IF(1, "Prediction requested, changing profile to AAC-Main\n");
 
818
    } else if (avctx->profile == FF_PROFILE_AAC_LOW ||
 
819
               avctx->profile == FF_PROFILE_UNKNOWN) {
 
820
        s->profile = 1; /* Low */
 
821
    } else {
 
822
        ERROR_IF(1, "Unsupported profile %d\n", avctx->profile);
 
823
    }
 
824
 
 
825
    if (s->options.aac_coder != AAC_CODER_TWOLOOP) {
 
826
        s->options.intensity_stereo = 0;
 
827
        s->options.pns = 0;
 
828
    }
785
829
 
786
830
    avctx->bit_rate = (int)FFMIN(
787
831
        6144 * s->channels / 1024.0 * avctx->sample_rate,
800
844
    avctx->extradata_size = 5;
801
845
    put_audio_specific_config(avctx);
802
846
 
803
 
    sizes[0]   = swb_size_1024[i];
804
 
    sizes[1]   = swb_size_128[i];
 
847
    sizes[0]   = ff_aac_swb_size_1024[i];
 
848
    sizes[1]   = ff_aac_swb_size_128[i];
805
849
    lengths[0] = ff_aac_num_swb_1024[i];
806
850
    lengths[1] = ff_aac_num_swb_128[i];
807
851
    for (i = 0; i < s->chan_map[0]; i++)
811
855
        goto fail;
812
856
    s->psypp = ff_psy_preprocess_init(avctx);
813
857
    s->coder = &ff_aac_coders[s->options.aac_coder];
 
858
    ff_lpc_init(&s->lpc, 2*avctx->frame_size, TNS_MAX_ORDER, FF_LPC_TYPE_LEVINSON);
814
859
 
815
860
    if (HAVE_MIPSDSPR1)
816
861
        ff_aac_coder_init_mips(s);
819
864
 
820
865
    ff_aac_tableinit();
821
866
 
822
 
    for (i = 0; i < 428; i++)
823
 
        ff_aac_pow34sf_tab[i] = sqrt(ff_aac_pow2sf_tab[i] * sqrt(ff_aac_pow2sf_tab[i]));
824
 
 
825
867
    avctx->initial_padding = 1024;
826
868
    ff_af_queue_init(avctx, &s->afq);
827
869
 
837
879
        {"auto",     "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.i64 = -1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
838
880
        {"ms_off",   "Disable Mid/Side coding", 0, AV_OPT_TYPE_CONST, {.i64 =  0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
839
881
        {"ms_force", "Force Mid/Side for the whole frame if possible", 0, AV_OPT_TYPE_CONST, {.i64 =  1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
840
 
    {"aac_coder", "", offsetof(AACEncContext, options.aac_coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, 0, AAC_CODER_NB-1, AACENC_FLAGS, "aac_coder"},
 
882
    {"aac_coder", "Coding algorithm", offsetof(AACEncContext, options.aac_coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, 0, AAC_CODER_NB-1, AACENC_FLAGS, "aac_coder"},
841
883
        {"faac",     "FAAC-inspired method",      0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAAC},    INT_MIN, INT_MAX, AACENC_FLAGS, "aac_coder"},
842
884
        {"anmr",     "ANMR method",               0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_ANMR},    INT_MIN, INT_MAX, AACENC_FLAGS, "aac_coder"},
843
885
        {"twoloop",  "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_coder"},
844
886
        {"fast",     "Constant quantizer",        0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST},    INT_MIN, INT_MAX, AACENC_FLAGS, "aac_coder"},
845
 
    {"aac_pns", "Perceptual Noise Substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "aac_pns"},
846
 
        {"disable",  "Disable PNS", 0, AV_OPT_TYPE_CONST, {.i64 =  0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
847
 
        {"enable",   "Enable PNS (Proof of concept)",  0, AV_OPT_TYPE_CONST, {.i64 =  1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
 
887
    {"aac_pns", "Perceptual Noise Substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AACENC_FLAGS, "aac_pns"},
 
888
        {"disable",  "Disable perceptual noise substitution", 0, AV_OPT_TYPE_CONST, {.i64 =  0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
 
889
        {"enable",   "Enable perceptual noise substitution",  0, AV_OPT_TYPE_CONST, {.i64 =  1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
 
890
    {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AACENC_FLAGS, "intensity_stereo"},
 
891
        {"disable",  "Disable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"},
 
892
        {"enable",   "Enable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"},
 
893
    {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "aac_tns"},
 
894
        {"disable",  "Disable temporal noise shaping", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_tns"},
 
895
        {"enable",   "Enable temporal noise shaping", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_tns"},
 
896
    {"aac_pred", "AAC-Main prediction", offsetof(AACEncContext, options.pred), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "aac_pred"},
 
897
        {"disable",  "Disable AAC-Main prediction", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pred"},
 
898
        {"enable",   "Enable AAC-Main prediction", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pred"},
848
899
    {NULL}
849
900
};
850
901
 
855
906
    LIBAVUTIL_VERSION_INT,
856
907
};
857
908
 
858
 
/* duplicated from avpriv_mpeg4audio_sample_rates to avoid shared build
859
 
 * failures */
860
 
static const int mpeg4audio_sample_rates[16] = {
861
 
    96000, 88200, 64000, 48000, 44100, 32000,
862
 
    24000, 22050, 16000, 12000, 11025, 8000, 7350
863
 
};
864
 
 
865
909
AVCodec ff_aac_encoder = {
866
910
    .name           = "aac",
867
911
    .long_name      = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
872
916
    .encode2        = aac_encode_frame,
873
917
    .close          = aac_encode_end,
874
918
    .supported_samplerates = mpeg4audio_sample_rates,
875
 
    .capabilities   = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY |
876
 
                      CODEC_CAP_EXPERIMENTAL,
 
919
    .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY |
 
920
                      AV_CODEC_CAP_EXPERIMENTAL,
877
921
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
878
922
                                                     AV_SAMPLE_FMT_NONE },
879
923
    .priv_class     = &aacenc_class,