Statistics
| Branch: | Revision:

ffmpeg / libavcodec / aacenc.c @ a45fbda9

History | View | Annotate | Download (22.7 KB)

1
/*
2
 * AAC encoder
3
 * Copyright (C) 2008 Konstantin Shishkov
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file
24
 * AAC encoder
25
 */
26

    
27
/***********************************
28
 *              TODOs:
29
 * add sane pulse detection
30
 * add temporal noise shaping
31
 ***********************************/
32

    
33
#include "avcodec.h"
34
#include "put_bits.h"
35
#include "dsputil.h"
36
#include "mpeg4audio.h"
37
#include "kbdwin.h"
38

    
39
#include "aac.h"
40
#include "aactab.h"
41
#include "aacenc.h"
42

    
43
#include "psymodel.h"
44

    
45
#define AAC_MAX_CHANNELS 6
46

    
47
static const uint8_t swb_size_1024_96[] = {
48
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
49
    12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
50
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
51
};
52

    
53
static const uint8_t swb_size_1024_64[] = {
54
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
55
    12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
56
    40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
57
};
58

    
59
static const uint8_t swb_size_1024_48[] = {
60
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
61
    12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
62
    32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
63
    96
64
};
65

    
66
static const uint8_t swb_size_1024_32[] = {
67
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
68
    12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
69
    32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
70
};
71

    
72
static const uint8_t swb_size_1024_24[] = {
73
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
74
    12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
75
    32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
76
};
77

    
78
static const uint8_t swb_size_1024_16[] = {
79
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
80
    12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
81
    32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
82
};
83

    
84
static const uint8_t swb_size_1024_8[] = {
85
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
86
    16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
87
    32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
88
};
89

    
90
static const uint8_t *swb_size_1024[] = {
91
    swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
92
    swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
93
    swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
94
    swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
95
};
96

    
97
static const uint8_t swb_size_128_96[] = {
98
    4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
99
};
100

    
101
static const uint8_t swb_size_128_48[] = {
102
    4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
103
};
104

    
105
static const uint8_t swb_size_128_24[] = {
106
    4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
107
};
108

    
109
static const uint8_t swb_size_128_16[] = {
110
    4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
111
};
112

    
113
static const uint8_t swb_size_128_8[] = {
114
    4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
115
};
116

    
117
static const uint8_t *swb_size_128[] = {
118
    /* the last entry on the following row is swb_size_128_64 but is a
119
       duplicate of swb_size_128_96 */
120
    swb_size_128_96, swb_size_128_96, swb_size_128_96,
121
    swb_size_128_48, swb_size_128_48, swb_size_128_48,
122
    swb_size_128_24, swb_size_128_24, swb_size_128_16,
123
    swb_size_128_16, swb_size_128_16, swb_size_128_8
124
};
125

    
126
/** default channel configurations */
127
static const uint8_t aac_chan_configs[6][5] = {
128
 {1, TYPE_SCE},                               // 1 channel  - single channel element
129
 {1, TYPE_CPE},                               // 2 channels - channel pair
130
 {2, TYPE_SCE, TYPE_CPE},                     // 3 channels - center + stereo
131
 {3, TYPE_SCE, TYPE_CPE, TYPE_SCE},           // 4 channels - front center + stereo + back center
132
 {3, TYPE_SCE, TYPE_CPE, TYPE_CPE},           // 5 channels - front center + stereo + back stereo
133
 {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
134
};
135

    
136
/**
137
 * Make AAC audio config object.
138
 * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
139
 */
140
static void put_audio_specific_config(AVCodecContext *avctx)
141
{
142
    PutBitContext pb;
143
    AACEncContext *s = avctx->priv_data;
144

    
145
    init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
146
    put_bits(&pb, 5, 2); //object type - AAC-LC
147
    put_bits(&pb, 4, s->samplerate_index); //sample rate index
148
    put_bits(&pb, 4, avctx->channels);
149
    //GASpecificConfig
150
    put_bits(&pb, 1, 0); //frame length - 1024 samples
151
    put_bits(&pb, 1, 0); //does not depend on core coder
152
    put_bits(&pb, 1, 0); //is not extension
153

    
154
    //Explicitly Mark SBR absent
155
    put_bits(&pb, 11, 0x2b7); //sync extension
156
    put_bits(&pb, 5,  AOT_SBR);
157
    put_bits(&pb, 1,  0);
158
    flush_put_bits(&pb);
159
}
160

    
161
static av_cold int aac_encode_init(AVCodecContext *avctx)
162
{
163
    AACEncContext *s = avctx->priv_data;
164
    int i;
165
    const uint8_t *sizes[2];
166
    int lengths[2];
167

    
168
    avctx->frame_size = 1024;
169

    
170
    for (i = 0; i < 16; i++)
171
        if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
172
            break;
173
    if (i == 16) {
174
        av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
175
        return -1;
176
    }
177
    if (avctx->channels > AAC_MAX_CHANNELS) {
178
        av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
179
        return -1;
180
    }
181
    if (avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW) {
182
        av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", avctx->profile);
183
        return -1;
184
    }
185
    if (1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * avctx->channels) {
186
        av_log(avctx, AV_LOG_ERROR, "Too many bits per frame requested\n");
187
        return -1;
188
    }
189
    s->samplerate_index = i;
190

    
191
    dsputil_init(&s->dsp, avctx);
192
    ff_mdct_init(&s->mdct1024, 11, 0, 1.0);
193
    ff_mdct_init(&s->mdct128,   8, 0, 1.0);
194
    // window init
195
    ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
196
    ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
197
    ff_init_ff_sine_windows(10);
198
    ff_init_ff_sine_windows(7);
199

    
200
    s->samples            = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
201
    s->cpe                = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
202
    avctx->extradata      = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
203
    avctx->extradata_size = 5;
204
    put_audio_specific_config(avctx);
205

    
206
    sizes[0]   = swb_size_1024[i];
207
    sizes[1]   = swb_size_128[i];
208
    lengths[0] = ff_aac_num_swb_1024[i];
209
    lengths[1] = ff_aac_num_swb_128[i];
210
    ff_psy_init(&s->psy, avctx, 2, sizes, lengths);
211
    s->psypp = ff_psy_preprocess_init(avctx);
212
    s->coder = &ff_aac_coders[2];
213

    
214
    s->lambda = avctx->global_quality ? avctx->global_quality : 120;
215

    
216
    ff_aac_tableinit();
217

    
218
    return 0;
219
}
220

    
221
static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
222
                                  SingleChannelElement *sce, short *audio)
223
{
224
    int i, k;
225
    const int chans = avctx->channels;
226
    const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
227
    const float * swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
228
    const float * pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
229
    float *output = sce->ret;
230

    
231
    if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
232
        memcpy(output, sce->saved, sizeof(float)*1024);
233
        if (sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE) {
234
            memset(output, 0, sizeof(output[0]) * 448);
235
            for (i = 448; i < 576; i++)
236
                output[i] = sce->saved[i] * pwindow[i - 448];
237
            for (i = 576; i < 704; i++)
238
                output[i] = sce->saved[i];
239
        }
240
        if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
241
            for (i = 0; i < 1024; i++) {
242
                output[i+1024]         = audio[i * chans] * lwindow[1024 - i - 1];
243
                sce->saved[i] = audio[i * chans] * lwindow[i];
244
            }
245
        } else {
246
            for (i = 0; i < 448; i++)
247
                output[i+1024]         = audio[i * chans];
248
            for (; i < 576; i++)
249
                output[i+1024]         = audio[i * chans] * swindow[576 - i - 1];
250
            memset(output+1024+576, 0, sizeof(output[0]) * 448);
251
            for (i = 0; i < 1024; i++)
252
                sce->saved[i] = audio[i * chans];
253
        }
254
        s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);
255
    } else {
256
        for (k = 0; k < 1024; k += 128) {
257
            for (i = 448 + k; i < 448 + k + 256; i++)
258
                output[i - 448 - k] = (i < 1024)
259
                                         ? sce->saved[i]
260
                                         : audio[(i-1024)*chans];
261
            s->dsp.vector_fmul        (output,     output, k ?  swindow : pwindow, 128);
262
            s->dsp.vector_fmul_reverse(output+128, output+128, swindow, 128);
263
            s->mdct128.mdct_calc(&s->mdct128, sce->coeffs + k, output);
264
        }
265
        for (i = 0; i < 1024; i++)
266
            sce->saved[i] = audio[i * chans];
267
    }
268
}
269

    
270
/**
271
 * Encode ics_info element.
272
 * @see Table 4.6 (syntax of ics_info)
273
 */
274
static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
275
{
276
    int w;
277

    
278
    put_bits(&s->pb, 1, 0);                // ics_reserved bit
279
    put_bits(&s->pb, 2, info->window_sequence[0]);
280
    put_bits(&s->pb, 1, info->use_kb_window[0]);
281
    if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
282
        put_bits(&s->pb, 6, info->max_sfb);
283
        put_bits(&s->pb, 1, 0);            // no prediction
284
    } else {
285
        put_bits(&s->pb, 4, info->max_sfb);
286
        for (w = 1; w < 8; w++)
287
            put_bits(&s->pb, 1, !info->group_len[w]);
288
    }
289
}
290

    
291
/**
292
 * Encode MS data.
293
 * @see 4.6.8.1 "Joint Coding - M/S Stereo"
294
 */
295
static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
296
{
297
    int i, w;
298

    
299
    put_bits(pb, 2, cpe->ms_mode);
300
    if (cpe->ms_mode == 1)
301
        for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
302
            for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
303
                put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
304
}
305

    
306
/**
307
 * Produce integer coefficients from scalefactors provided by the model.
308
 */
309
static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, int chans)
310
{
311
    int i, w, w2, g, ch;
312
    int start, maxsfb, cmaxsfb;
313

    
314
    for (ch = 0; ch < chans; ch++) {
315
        IndividualChannelStream *ics = &cpe->ch[ch].ics;
316
        start = 0;
317
        maxsfb = 0;
318
        cpe->ch[ch].pulse.num_pulse = 0;
319
        for (w = 0; w < ics->num_windows*16; w += 16) {
320
            for (g = 0; g < ics->num_swb; g++) {
321
                //apply M/S
322
                if (cpe->common_window && !ch && cpe->ms_mask[w + g]) {
323
                    for (i = 0; i < ics->swb_sizes[g]; i++) {
324
                        cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0;
325
                        cpe->ch[1].coeffs[start+i] =  cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
326
                    }
327
                }
328
                start += ics->swb_sizes[g];
329
            }
330
            for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--)
331
                ;
332
            maxsfb = FFMAX(maxsfb, cmaxsfb);
333
        }
334
        ics->max_sfb = maxsfb;
335

    
336
        //adjust zero bands for window groups
337
        for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
338
            for (g = 0; g < ics->max_sfb; g++) {
339
                i = 1;
340
                for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
341
                    if (!cpe->ch[ch].zeroes[w2*16 + g]) {
342
                        i = 0;
343
                        break;
344
                    }
345
                }
346
                cpe->ch[ch].zeroes[w*16 + g] = i;
347
            }
348
        }
349
    }
350

    
351
    if (chans > 1 && cpe->common_window) {
352
        IndividualChannelStream *ics0 = &cpe->ch[0].ics;
353
        IndividualChannelStream *ics1 = &cpe->ch[1].ics;
354
        int msc = 0;
355
        ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
356
        ics1->max_sfb = ics0->max_sfb;
357
        for (w = 0; w < ics0->num_windows*16; w += 16)
358
            for (i = 0; i < ics0->max_sfb; i++)
359
                if (cpe->ms_mask[w+i])
360
                    msc++;
361
        if (msc == 0 || ics0->max_sfb == 0)
362
            cpe->ms_mode = 0;
363
        else
364
            cpe->ms_mode = msc < ics0->max_sfb ? 1 : 2;
365
    }
366
}
367

    
368
/**
369
 * Encode scalefactor band coding type.
370
 */
371
static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
372
{
373
    int w;
374

    
375
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
376
        s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
377
}
378

    
379
/**
380
 * Encode scalefactors.
381
 */
382
static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
383
                                 SingleChannelElement *sce)
384
{
385
    int off = sce->sf_idx[0], diff;
386
    int i, w;
387

    
388
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
389
        for (i = 0; i < sce->ics.max_sfb; i++) {
390
            if (!sce->zeroes[w*16 + i]) {
391
                diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO;
392
                if (diff < 0 || diff > 120)
393
                    av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
394
                off = sce->sf_idx[w*16 + i];
395
                put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
396
            }
397
        }
398
    }
399
}
400

    
401
/**
402
 * Encode pulse data.
403
 */
404
static void encode_pulses(AACEncContext *s, Pulse *pulse)
405
{
406
    int i;
407

    
408
    put_bits(&s->pb, 1, !!pulse->num_pulse);
409
    if (!pulse->num_pulse)
410
        return;
411

    
412
    put_bits(&s->pb, 2, pulse->num_pulse - 1);
413
    put_bits(&s->pb, 6, pulse->start);
414
    for (i = 0; i < pulse->num_pulse; i++) {
415
        put_bits(&s->pb, 5, pulse->pos[i]);
416
        put_bits(&s->pb, 4, pulse->amp[i]);
417
    }
418
}
419

    
420
/**
421
 * Encode spectral coefficients processed by psychoacoustic model.
422
 */
423
static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
424
{
425
    int start, i, w, w2;
426

    
427
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
428
        start = 0;
429
        for (i = 0; i < sce->ics.max_sfb; i++) {
430
            if (sce->zeroes[w*16 + i]) {
431
                start += sce->ics.swb_sizes[i];
432
                continue;
433
            }
434
            for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
435
                s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
436
                                                   sce->ics.swb_sizes[i],
437
                                                   sce->sf_idx[w*16 + i],
438
                                                   sce->band_type[w*16 + i],
439
                                                   s->lambda);
440
            start += sce->ics.swb_sizes[i];
441
        }
442
    }
443
}
444

    
445
/**
446
 * Encode one channel of audio data.
447
 */
448
static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
449
                                     SingleChannelElement *sce,
450
                                     int common_window)
451
{
452
    put_bits(&s->pb, 8, sce->sf_idx[0]);
453
    if (!common_window)
454
        put_ics_info(s, &sce->ics);
455
    encode_band_info(s, sce);
456
    encode_scale_factors(avctx, s, sce);
457
    encode_pulses(s, &sce->pulse);
458
    put_bits(&s->pb, 1, 0); //tns
459
    put_bits(&s->pb, 1, 0); //ssr
460
    encode_spectral_coeffs(s, sce);
461
    return 0;
462
}
463

    
464
/**
465
 * Write some auxiliary information about the created AAC file.
466
 */
467
static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
468
                               const char *name)
469
{
470
    int i, namelen, padbits;
471

    
472
    namelen = strlen(name) + 2;
473
    put_bits(&s->pb, 3, TYPE_FIL);
474
    put_bits(&s->pb, 4, FFMIN(namelen, 15));
475
    if (namelen >= 15)
476
        put_bits(&s->pb, 8, namelen - 16);
477
    put_bits(&s->pb, 4, 0); //extension type - filler
478
    padbits = 8 - (put_bits_count(&s->pb) & 7);
479
    align_put_bits(&s->pb);
480
    for (i = 0; i < namelen - 2; i++)
481
        put_bits(&s->pb, 8, name[i]);
482
    put_bits(&s->pb, 12 - padbits, 0);
483
}
484

    
485
static int aac_encode_frame(AVCodecContext *avctx,
486
                            uint8_t *frame, int buf_size, void *data)
487
{
488
    AACEncContext *s = avctx->priv_data;
489
    int16_t *samples = s->samples, *samples2, *la;
490
    ChannelElement *cpe;
491
    int i, j, chans, tag, start_ch;
492
    const uint8_t *chan_map = aac_chan_configs[avctx->channels-1];
493
    int chan_el_counter[4];
494
    FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
495

    
496
    if (s->last_frame)
497
        return 0;
498
    if (data) {
499
        if (!s->psypp) {
500
            memcpy(s->samples + 1024 * avctx->channels, data,
501
                   1024 * avctx->channels * sizeof(s->samples[0]));
502
        } else {
503
            start_ch = 0;
504
            samples2 = s->samples + 1024 * avctx->channels;
505
            for (i = 0; i < chan_map[0]; i++) {
506
                tag = chan_map[i+1];
507
                chans = tag == TYPE_CPE ? 2 : 1;
508
                ff_psy_preprocess(s->psypp, (uint16_t*)data + start_ch,
509
                                  samples2 + start_ch, start_ch, chans);
510
                start_ch += chans;
511
            }
512
        }
513
    }
514
    if (!avctx->frame_number) {
515
        memcpy(s->samples, s->samples + 1024 * avctx->channels,
516
               1024 * avctx->channels * sizeof(s->samples[0]));
517
        return 0;
518
    }
519

    
520
    start_ch = 0;
521
    for (i = 0; i < chan_map[0]; i++) {
522
        FFPsyWindowInfo* wi = windows + start_ch;
523
        tag      = chan_map[i+1];
524
        chans    = tag == TYPE_CPE ? 2 : 1;
525
        cpe      = &s->cpe[i];
526
        for (j = 0; j < chans; j++) {
527
            IndividualChannelStream *ics = &cpe->ch[j].ics;
528
            int k;
529
            int cur_channel = start_ch + j;
530
            samples2 = samples + cur_channel;
531
            la       = samples2 + (448+64) * avctx->channels;
532
            if (!data)
533
                la = NULL;
534
            if (tag == TYPE_LFE) {
535
                wi[j].window_type[0] = ONLY_LONG_SEQUENCE;
536
                wi[j].window_shape   = 0;
537
                wi[j].num_windows    = 1;
538
                wi[j].grouping[0]    = 1;
539
            } else {
540
                wi[j] = ff_psy_suggest_window(&s->psy, samples2, la, cur_channel,
541
                                              ics->window_sequence[0]);
542
            }
543
            ics->window_sequence[1] = ics->window_sequence[0];
544
            ics->window_sequence[0] = wi[j].window_type[0];
545
            ics->use_kb_window[1]   = ics->use_kb_window[0];
546
            ics->use_kb_window[0]   = wi[j].window_shape;
547
            ics->num_windows        = wi[j].num_windows;
548
            ics->swb_sizes          = s->psy.bands    [ics->num_windows == 8];
549
            ics->num_swb            = tag == TYPE_LFE ? 12 : s->psy.num_bands[ics->num_windows == 8];
550
            for (k = 0; k < ics->num_windows; k++)
551
                ics->group_len[k] = wi[j].grouping[k];
552

    
553
            apply_window_and_mdct(avctx, s, &cpe->ch[j], samples2);
554
        }
555
        start_ch += chans;
556
    }
557
    do {
558
        int frame_bits;
559
        init_put_bits(&s->pb, frame, buf_size*8);
560
        if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT))
561
            put_bitstream_info(avctx, s, LIBAVCODEC_IDENT);
562
        start_ch = 0;
563
        memset(chan_el_counter, 0, sizeof(chan_el_counter));
564
        for (i = 0; i < chan_map[0]; i++) {
565
            FFPsyWindowInfo* wi = windows + start_ch;
566
            tag      = chan_map[i+1];
567
            chans    = tag == TYPE_CPE ? 2 : 1;
568
            cpe      = &s->cpe[i];
569
            put_bits(&s->pb, 3, tag);
570
            put_bits(&s->pb, 4, chan_el_counter[tag]++);
571
            for (j = 0; j < chans; j++) {
572
                s->cur_channel = start_ch + j;
573
                ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]);
574
                s->coder->search_for_quantizers(avctx, s, &cpe->ch[j], s->lambda);
575
            }
576
            cpe->common_window = 0;
577
            if (chans > 1
578
                && wi[0].window_type[0] == wi[1].window_type[0]
579
                && wi[0].window_shape   == wi[1].window_shape) {
580

    
581
                cpe->common_window = 1;
582
                for (j = 0; j < wi[0].num_windows; j++) {
583
                    if (wi[0].grouping[j] != wi[1].grouping[j]) {
584
                        cpe->common_window = 0;
585
                        break;
586
                    }
587
                }
588
            }
589
            s->cur_channel = start_ch;
590
            if (cpe->common_window && s->coder->search_for_ms)
591
                s->coder->search_for_ms(s, cpe, s->lambda);
592
            adjust_frame_information(s, cpe, chans);
593
            if (chans == 2) {
594
                put_bits(&s->pb, 1, cpe->common_window);
595
                if (cpe->common_window) {
596
                    put_ics_info(s, &cpe->ch[0].ics);
597
                    encode_ms_info(&s->pb, cpe);
598
                }
599
            }
600
            for (j = 0; j < chans; j++) {
601
                s->cur_channel = start_ch + j;
602
                encode_individual_channel(avctx, s, &cpe->ch[j], cpe->common_window);
603
            }
604
            start_ch += chans;
605
        }
606

    
607
        frame_bits = put_bits_count(&s->pb);
608
        if (frame_bits <= 6144 * avctx->channels - 3)
609
            break;
610

    
611
        s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
612

    
613
    } while (1);
614

    
615
    put_bits(&s->pb, 3, TYPE_END);
616
    flush_put_bits(&s->pb);
617
    avctx->frame_bits = put_bits_count(&s->pb);
618

    
619
    // rate control stuff
620
    if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
621
        float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits;
622
        s->lambda *= ratio;
623
        s->lambda = FFMIN(s->lambda, 65536.f);
624
    }
625

    
626
    if (!data)
627
        s->last_frame = 1;
628
    memcpy(s->samples, s->samples + 1024 * avctx->channels,
629
           1024 * avctx->channels * sizeof(s->samples[0]));
630
    return put_bits_count(&s->pb)>>3;
631
}
632

    
633
static av_cold int aac_encode_end(AVCodecContext *avctx)
634
{
635
    AACEncContext *s = avctx->priv_data;
636

    
637
    ff_mdct_end(&s->mdct1024);
638
    ff_mdct_end(&s->mdct128);
639
    ff_psy_end(&s->psy);
640
    ff_psy_preprocess_end(s->psypp);
641
    av_freep(&s->samples);
642
    av_freep(&s->cpe);
643
    return 0;
644
}
645

    
646
AVCodec ff_aac_encoder = {
647
    "aac",
648
    AVMEDIA_TYPE_AUDIO,
649
    CODEC_ID_AAC,
650
    sizeof(AACEncContext),
651
    aac_encode_init,
652
    aac_encode_frame,
653
    aac_encode_end,
654
    .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
655
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
656
    .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
657
};