annotate aacenc.c @ 7605:681e7430fbeb libavcodec

Add okayed parts for AAC encoder
author kostya
date Mon, 18 Aug 2008 05:38:26 +0000
parents ab454e7f1c35
children 1c01b74dc78c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7571
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
1 /*
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
2 * AAC encoder
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
3 * Copyright (C) 2008 Konstantin Shishkov
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
4 *
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
5 * This file is part of FFmpeg.
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
6 *
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
11 *
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
16 *
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
20 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
21
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
22 /**
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
23 * @file aacenc.c
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
24 * AAC encoder
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
25 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
26
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
27 /***********************************
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
28 * TODOs:
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
29 * psy model selection with some option
7584
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
30 * add sane pulse detection
7571
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
31 ***********************************/
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
32
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
33 #include "avcodec.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
34 #include "bitstream.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
35 #include "dsputil.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
36 #include "mpeg4audio.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
37
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
38 #include "aacpsy.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
39 #include "aac.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
40 #include "aactab.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
41
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
42 static const uint8_t swb_size_1024_96[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
43 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
44 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
45 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
46 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
47
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
48 static const uint8_t swb_size_1024_64[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
49 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
50 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
51 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
52 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
53
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
54 static const uint8_t swb_size_1024_48[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
55 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
56 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
57 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
58 96
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
59 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
60
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
61 static const uint8_t swb_size_1024_32[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
62 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
63 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
64 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
65 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
66
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
67 static const uint8_t swb_size_1024_24[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
68 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
69 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
70 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
71 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
72
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
73 static const uint8_t swb_size_1024_16[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
74 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
75 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
76 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
77 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
78
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
79 static const uint8_t swb_size_1024_8[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
80 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
81 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
82 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
83 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
84
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
85 static const uint8_t *swb_size_1024[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
86 swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
87 swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
88 swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
89 swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
90 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
91
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
92 static const uint8_t swb_size_128_96[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
93 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
94 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
95
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
96 static const uint8_t swb_size_128_48[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
97 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
98 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
99
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
100 static const uint8_t swb_size_128_24[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
101 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
102 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
103
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
104 static const uint8_t swb_size_128_16[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
105 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
106 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
107
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
108 static const uint8_t swb_size_128_8[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
109 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
110 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
111
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
112 static const uint8_t *swb_size_128[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
113 /* the last entry on the following row is swb_size_128_64 but is a
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
114 duplicate of swb_size_128_96 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
115 swb_size_128_96, swb_size_128_96, swb_size_128_96,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
116 swb_size_128_48, swb_size_128_48, swb_size_128_48,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
117 swb_size_128_24, swb_size_128_24, swb_size_128_16,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
118 swb_size_128_16, swb_size_128_16, swb_size_128_8
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
119 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
120
7596
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
121 /** bits needed to code codebook run value for long windows */
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
122 static const uint8_t run_value_bits_long[64] = {
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
123 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
124 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
125 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
126 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
127 };
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
128
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
129 /** bits needed to code codebook run value for short windows */
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
130 static const uint8_t run_value_bits_short[16] = {
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
131 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
132 };
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
133
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
134 static const uint8_t* run_value_bits[2] = {
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
135 run_value_bits_long, run_value_bits_short
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
136 };
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
137
7571
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
138 /** default channel configurations */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
139 static const uint8_t aac_chan_configs[6][5] = {
7585
12976f458c7a Synchronize AAC encoder with renamings in aac.h
kostya
parents: 7584
diff changeset
140 {1, TYPE_SCE}, // 1 channel - single channel element
12976f458c7a Synchronize AAC encoder with renamings in aac.h
kostya
parents: 7584
diff changeset
141 {1, TYPE_CPE}, // 2 channels - channel pair
12976f458c7a Synchronize AAC encoder with renamings in aac.h
kostya
parents: 7584
diff changeset
142 {2, TYPE_SCE, TYPE_CPE}, // 3 channels - center + stereo
12976f458c7a Synchronize AAC encoder with renamings in aac.h
kostya
parents: 7584
diff changeset
143 {3, TYPE_SCE, TYPE_CPE, TYPE_SCE}, // 4 channels - front center + stereo + back center
12976f458c7a Synchronize AAC encoder with renamings in aac.h
kostya
parents: 7584
diff changeset
144 {3, TYPE_SCE, TYPE_CPE, TYPE_CPE}, // 5 channels - front center + stereo + back stereo
12976f458c7a Synchronize AAC encoder with renamings in aac.h
kostya
parents: 7584
diff changeset
145 {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
7571
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
146 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
147
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
148 /**
7596
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
149 * structure used in optimal codebook search
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
150 */
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
151 typedef struct BandCodingPath {
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
152 int prev_idx; ///< pointer to the previous path point
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
153 int codebook; ///< codebook for coding band run
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
154 int bits; ///< number of bit needed to code given number of bands
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
155 } BandCodingPath;
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
156
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
157 /**
7584
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
158 * AAC encoder context
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
159 */
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
160 typedef struct {
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
161 PutBitContext pb;
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
162 MDCTContext mdct1024; ///< long (1024 samples) frame transform context
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
163 MDCTContext mdct128; ///< short (128 samples) frame transform context
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
164 DSPContext dsp;
7605
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
165 DECLARE_ALIGNED_16(FFTSample, output[2048]); ///< temporary buffer for MDCT input coefficients
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
166 int16_t* samples; ///< saved preprocessed input
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
167
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
168 int samplerate_index; ///< MPEG-4 samplerate index
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
169
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
170 ChannelElement *cpe; ///< channel elements
7596
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
171 AACPsyContext psy; ///< psychoacoustic model context
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
172 int last_frame;
7584
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
173 } AACEncContext;
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
174
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
175 /**
7571
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
176 * Make AAC audio config object.
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
177 * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
178 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
179 static void put_audio_specific_config(AVCodecContext *avctx)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
180 {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
181 PutBitContext pb;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
182 AACEncContext *s = avctx->priv_data;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
183
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
184 init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
185 put_bits(&pb, 5, 2); //object type - AAC-LC
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
186 put_bits(&pb, 4, s->samplerate_index); //sample rate index
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
187 put_bits(&pb, 4, avctx->channels);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
188 //GASpecificConfig
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
189 put_bits(&pb, 1, 0); //frame length - 1024 samples
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
190 put_bits(&pb, 1, 0); //does not depend on core coder
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
191 put_bits(&pb, 1, 0); //is not extension
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
192 flush_put_bits(&pb);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
193 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
194
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
195 static av_cold int aac_encode_init(AVCodecContext *avctx)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
196 {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
197 AACEncContext *s = avctx->priv_data;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
198 int i;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
199
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
200 avctx->frame_size = 1024;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
201
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
202 for(i = 0; i < 16; i++)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
203 if(avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
204 break;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
205 if(i == 16){
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
206 av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
207 return -1;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
208 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
209 if(avctx->channels > 6){
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
210 av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
211 return -1;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
212 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
213 s->samplerate_index = i;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
214 s->swb_sizes1024 = swb_size_1024[i];
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
215 s->swb_num1024 = ff_aac_num_swb_1024[i];
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
216 s->swb_sizes128 = swb_size_128[i];
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
217 s->swb_num128 = ff_aac_num_swb_128[i];
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
218
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
219 dsputil_init(&s->dsp, avctx);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
220 ff_mdct_init(&s->mdct1024, 11, 0);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
221 ff_mdct_init(&s->mdct128, 8, 0);
7584
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
222 // window init
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
223 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
224 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
225 ff_sine_window_init(ff_sine_1024, 1024);
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
226 ff_sine_window_init(ff_sine_128, 128);
7571
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
227
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
228 s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
229 s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
7605
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
230 if(ff_aac_psy_init(&s->psy, avctx, AAC_PSY_3GPP,
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
231 aac_chan_configs[avctx->channels-1][0], 0,
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
232 s->swb_sizes1024, s->swb_num1024, s->swb_sizes128, s->swb_num128) < 0){
7571
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
233 av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n");
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
234 return -1;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
235 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
236 avctx->extradata = av_malloc(2);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
237 avctx->extradata_size = 2;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
238 put_audio_specific_config(avctx);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
239 return 0;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
240 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
241
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
242 /**
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
243 * Encode ics_info element.
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
244 * @see Table 4.6 (syntax of ics_info)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
245 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
246 static void put_ics_info(AVCodecContext *avctx, IndividualChannelStream *info)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
247 {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
248 AACEncContext *s = avctx->priv_data;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
249 int i;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
250
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
251 put_bits(&s->pb, 1, 0); // ics_reserved bit
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
252 put_bits(&s->pb, 2, info->window_sequence[0]);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
253 put_bits(&s->pb, 1, info->use_kb_window[0]);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
254 if(info->window_sequence[0] != EIGHT_SHORT_SEQUENCE){
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
255 put_bits(&s->pb, 6, info->max_sfb);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
256 put_bits(&s->pb, 1, 0); // no prediction
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
257 }else{
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
258 put_bits(&s->pb, 4, info->max_sfb);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
259 for(i = 1; i < info->num_windows; i++)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
260 put_bits(&s->pb, 1, info->group_len[i]);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
261 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
262 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
263
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
264 /**
7584
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
265 * Encode pulse data.
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
266 */
7605
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
267 static void encode_pulses(AACEncContext *s, Pulse *pulse, int channel)
7584
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
268 {
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
269 int i;
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
270
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
271 put_bits(&s->pb, 1, !!pulse->num_pulse);
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
272 if(!pulse->num_pulse) return;
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
273
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
274 put_bits(&s->pb, 2, pulse->num_pulse - 1);
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
275 put_bits(&s->pb, 6, pulse->start);
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
276 for(i = 0; i < pulse->num_pulse; i++){
7585
12976f458c7a Synchronize AAC encoder with renamings in aac.h
kostya
parents: 7584
diff changeset
277 put_bits(&s->pb, 5, pulse->pos[i]);
7584
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
278 put_bits(&s->pb, 4, pulse->amp[i]);
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
279 }
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
280 }
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
281
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
282 /**
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
283 * Encode spectral coefficients processed by psychoacoustic model.
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
284 */
7605
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
285 static void encode_spectral_coeffs(AACEncContext *s, ChannelElement *cpe, int channel)
7584
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
286 {
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
287 int start, i, w, w2, wg;
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
288
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
289 w = 0;
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
290 for(wg = 0; wg < cpe->ch[channel].ics.num_window_groups; wg++){
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
291 start = 0;
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
292 for(i = 0; i < cpe->ch[channel].ics.max_sfb; i++){
7596
ab454e7f1c35 More okayed chunks of AAC encoder
kostya
parents: 7585
diff changeset
293 if(cpe->ch[channel].zeroes[w*16 + i]){
7584
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
294 start += cpe->ch[channel].ics.swb_sizes[i];
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
295 continue;
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
296 }
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
297 for(w2 = w; w2 < w + cpe->ch[channel].ics.group_len[wg]; w2++){
7605
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
298 encode_band_coeffs(s, cpe, channel, start + w2*128,
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
299 cpe->ch[channel].ics.swb_sizes[i],
681e7430fbeb Add okayed parts for AAC encoder
kostya
parents: 7596
diff changeset
300 cpe->ch[channel].band_type[w*16 + i]);
7584
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
301 }
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
302 start += cpe->ch[channel].ics.swb_sizes[i];
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
303 }
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
304 w += cpe->ch[channel].ics.group_len[wg];
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
305 }
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
306 }
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
307
d8717018ac03 Add approved chunks to AAC encoder
kostya
parents: 7574
diff changeset
308 /**
7571
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
309 * Write some auxiliary information about the created AAC file.
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
310 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
311 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
312 {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
313 int i, namelen, padbits;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
314
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
315 namelen = strlen(name) + 2;
7585
12976f458c7a Synchronize AAC encoder with renamings in aac.h
kostya
parents: 7584
diff changeset
316 put_bits(&s->pb, 3, TYPE_FIL);
7571
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
317 put_bits(&s->pb, 4, FFMIN(namelen, 15));
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
318 if(namelen >= 15)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
319 put_bits(&s->pb, 8, namelen - 16);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
320 put_bits(&s->pb, 4, 0); //extension type - filler
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
321 padbits = 8 - (put_bits_count(&s->pb) & 7);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
322 align_put_bits(&s->pb);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
323 for(i = 0; i < namelen - 2; i++)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
324 put_bits(&s->pb, 8, name[i]);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
325 put_bits(&s->pb, 12 - padbits, 0);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
326 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
327
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
328 static av_cold int aac_encode_end(AVCodecContext *avctx)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
329 {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
330 AACEncContext *s = avctx->priv_data;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
331
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
332 ff_mdct_end(&s->mdct1024);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
333 ff_mdct_end(&s->mdct128);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
334 ff_aac_psy_end(&s->psy);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
335 av_freep(&s->samples);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
336 av_freep(&s->cpe);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
337 return 0;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
338 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
339
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
340 AVCodec aac_encoder = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
341 "aac",
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
342 CODEC_TYPE_AUDIO,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
343 CODEC_ID_AAC,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
344 sizeof(AACEncContext),
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
345 aac_encode_init,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
346 aac_encode_frame,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
347 aac_encode_end,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
348 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
349 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
350 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
351 };