annotate aacenc.c @ 7571:58f6bb760994 libavcodec

Okayed parts of AAC encoder
author kostya
date Thu, 14 Aug 2008 05:52:29 +0000
parents
children 27ee0ceab150
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
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
30 * change greedy codebook search into something more optimal, like Viterbi algorithm
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
31 * determine run lengths along with codebook
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
32 ***********************************/
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
33
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
34 #include "avcodec.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
35 #include "bitstream.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
36 #include "dsputil.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
37 #include "mpeg4audio.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
38
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
39 #include "aacpsy.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
40 #include "aac.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
41 #include "aactab.h"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
42
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
43 static const uint8_t swb_size_1024_96[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
44 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
45 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
46 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
47 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
48
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
49 static const uint8_t swb_size_1024_64[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
50 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
51 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
52 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
53 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
54
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
55 static const uint8_t swb_size_1024_48[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
56 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
57 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
58 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
59 96
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
60 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
61
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
62 static const uint8_t swb_size_1024_32[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
63 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
64 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
65 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
66 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
67
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
68 static const uint8_t swb_size_1024_24[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
69 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
70 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
71 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
72 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
73
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
74 static const uint8_t swb_size_1024_16[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
75 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
76 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
77 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
78 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
79
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
80 static const uint8_t swb_size_1024_8[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
81 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
82 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
83 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
84 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
85
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
86 static const uint8_t *swb_size_1024[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
87 swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
88 swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
89 swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
90 swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
91 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
92
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
93 static const uint8_t swb_size_128_96[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
94 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
95 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
96
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
97 static const uint8_t swb_size_128_48[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
98 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
99 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
100
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
101 static const uint8_t swb_size_128_24[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
102 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
103 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
104
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
105 static const uint8_t swb_size_128_16[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
106 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
107 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
108
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
109 static const uint8_t swb_size_128_8[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
110 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
111 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
112
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
113 static const uint8_t *swb_size_128[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
114 /* 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
115 duplicate of swb_size_128_96 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
116 swb_size_128_96, swb_size_128_96, swb_size_128_96,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
117 swb_size_128_48, swb_size_128_48, swb_size_128_48,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
118 swb_size_128_24, swb_size_128_24, swb_size_128_16,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
119 swb_size_128_16, swb_size_128_16, swb_size_128_8
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
120 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
121
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
122 #define CB_UNSIGNED 0x01 ///< coefficients are coded as absolute values
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
123 #define CB_PAIRS 0x02 ///< coefficients are grouped into pairs before coding (quads by default)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
124 #define CB_ESCAPE 0x04 ///< codebook allows escapes
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
125
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
126 /** spectral coefficients codebook information */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
127 static const struct {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
128 int16_t maxval; ///< maximum possible value
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
129 int8_t cb_num; ///< codebook number
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
130 uint8_t flags; ///< codebook features
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
131 } aac_cb_info[] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
132 { 0, -1, CB_UNSIGNED }, // zero codebook
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
133 { 1, 0, 0 },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
134 { 1, 1, 0 },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
135 { 2, 2, CB_UNSIGNED },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
136 { 2, 3, CB_UNSIGNED },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
137 { 4, 4, CB_PAIRS },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
138 { 4, 5, CB_PAIRS },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
139 { 7, 6, CB_PAIRS | CB_UNSIGNED },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
140 { 7, 7, CB_PAIRS | CB_UNSIGNED },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
141 { 12, 8, CB_PAIRS | CB_UNSIGNED },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
142 { 12, 9, CB_PAIRS | CB_UNSIGNED },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
143 { 8191, 10, CB_PAIRS | CB_UNSIGNED | CB_ESCAPE },
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
144 { -1, -1, 0 }, // reserved
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
145 { -1, -1, 0 }, // perceptual noise substitution
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
146 { -1, -1, 0 }, // intensity out-of-phase
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
147 { -1, -1, 0 }, // intensity in-phase
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
148 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
149
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
150 /** default channel configurations */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
151 static const uint8_t aac_chan_configs[6][5] = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
152 {1, ID_SCE}, // 1 channel - single channel element
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
153 {1, ID_CPE}, // 2 channels - channel pair
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
154 {2, ID_SCE, ID_CPE}, // 3 channels - center + stereo
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
155 {3, ID_SCE, ID_CPE, ID_SCE}, // 4 channels - front center + stereo + back center
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
156 {3, ID_SCE, ID_CPE, ID_CPE}, // 5 channels - front center + stereo + back stereo
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
157 {4, ID_SCE, ID_CPE, ID_CPE, ID_LFE}, // 6 channels - front center + stereo + back stereo + LFE
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
158 };
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
159
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
160 /**
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
161 * AAC encoder context
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
162 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
163 typedef struct {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
164 PutBitContext pb;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
165 MDCTContext mdct1024; ///< long (1024 samples) frame transform context
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
166 MDCTContext mdct128; ///< short (128 samples) frame transform context
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
167 DSPContext dsp;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
168 DECLARE_ALIGNED_16(FFTSample, output[2048]); ///< temporary buffer for MDCT input coefficients
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
169 DECLARE_ALIGNED_16(FFTSample, tmp[1024]); ///< temporary buffer used by MDCT
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
170 int16_t* samples; ///< saved preprocessed input
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
171
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
172 int samplerate_index; ///< MPEG-4 samplerate index
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
173 const uint8_t *swb_sizes1024; ///< scalefactor band sizes for long frame
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
174 int swb_num1024; ///< number of scalefactor bands for long frame
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
175 const uint8_t *swb_sizes128; ///< scalefactor band sizes for short frame
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
176 int swb_num128; ///< number of scalefactor bands for short frame
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
177
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
178 ChannelElement *cpe; ///< channel elements
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
179 AACPsyContext psy; ///< psychoacoustic model context
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
180 int last_frame;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
181 } AACEncContext;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
182
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
183 /**
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
184 * Make AAC audio config object.
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
185 * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
186 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
187 static void put_audio_specific_config(AVCodecContext *avctx)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
188 {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
189 PutBitContext pb;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
190 AACEncContext *s = avctx->priv_data;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
191
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
192 init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
193 put_bits(&pb, 5, 2); //object type - AAC-LC
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
194 put_bits(&pb, 4, s->samplerate_index); //sample rate index
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
195 put_bits(&pb, 4, avctx->channels);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
196 //GASpecificConfig
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
197 put_bits(&pb, 1, 0); //frame length - 1024 samples
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
198 put_bits(&pb, 1, 0); //does not depend on core coder
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
199 put_bits(&pb, 1, 0); //is not extension
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
200 flush_put_bits(&pb);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
201 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
202
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
203 static av_cold int aac_encode_init(AVCodecContext *avctx)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
204 {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
205 AACEncContext *s = avctx->priv_data;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
206 int i;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
207
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
208 avctx->frame_size = 1024;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
209
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
210 for(i = 0; i < 16; i++)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
211 if(avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
212 break;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
213 if(i == 16){
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
214 av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
215 return -1;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
216 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
217 if(avctx->channels > 6){
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
218 av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
219 return -1;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
220 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
221 s->samplerate_index = i;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
222 s->swb_sizes1024 = swb_size_1024[i];
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
223 s->swb_num1024 = ff_aac_num_swb_1024[i];
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
224 s->swb_sizes128 = swb_size_128[i];
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
225 s->swb_num128 = ff_aac_num_swb_128[i];
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
226
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
227 dsputil_init(&s->dsp, avctx);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
228 ff_mdct_init(&s->mdct1024, 11, 0);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
229 ff_mdct_init(&s->mdct128, 8, 0);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
230 // window init
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
231 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
232 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
233 ff_sine_window_init(ff_aac_sine_long_1024, 1024);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
234 ff_sine_window_init(ff_aac_sine_short_128, 128);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
235
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
236 s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
237 s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
238 if(ff_aac_psy_init(&s->psy, avctx, AAC_PSY_3GPP, aac_chan_configs[avctx->channels-1][0], 0, s->swb_sizes1024, s->swb_num1024, s->swb_sizes128, s->swb_num128) < 0){
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
239 av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n");
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
240 return -1;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
241 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
242 avctx->extradata = av_malloc(2);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
243 avctx->extradata_size = 2;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
244 put_audio_specific_config(avctx);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
245 return 0;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
246 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
247
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
248 /**
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
249 * Encode ics_info element.
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
250 * @see Table 4.6 (syntax of ics_info)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
251 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
252 static void put_ics_info(AVCodecContext *avctx, IndividualChannelStream *info)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
253 {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
254 AACEncContext *s = avctx->priv_data;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
255 int i;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
256
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
257 put_bits(&s->pb, 1, 0); // ics_reserved bit
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
258 put_bits(&s->pb, 2, info->window_sequence[0]);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
259 put_bits(&s->pb, 1, info->use_kb_window[0]);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
260 if(info->window_sequence[0] != EIGHT_SHORT_SEQUENCE){
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
261 put_bits(&s->pb, 6, info->max_sfb);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
262 put_bits(&s->pb, 1, 0); // no prediction
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
263 }else{
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
264 put_bits(&s->pb, 4, info->max_sfb);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
265 for(i = 1; i < info->num_windows; i++)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
266 put_bits(&s->pb, 1, info->group_len[i]);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
267 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
268 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
269
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
270 /**
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
271 * Write some auxiliary information about the created AAC file.
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
272 */
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
273 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
274 {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
275 int i, namelen, padbits;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
276
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
277 namelen = strlen(name) + 2;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
278 put_bits(&s->pb, 3, ID_FIL);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
279 put_bits(&s->pb, 4, FFMIN(namelen, 15));
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
280 if(namelen >= 15)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
281 put_bits(&s->pb, 8, namelen - 16);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
282 put_bits(&s->pb, 4, 0); //extension type - filler
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
283 padbits = 8 - (put_bits_count(&s->pb) & 7);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
284 align_put_bits(&s->pb);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
285 for(i = 0; i < namelen - 2; i++)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
286 put_bits(&s->pb, 8, name[i]);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
287 put_bits(&s->pb, 12 - padbits, 0);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
288 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
289
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
290 static av_cold int aac_encode_end(AVCodecContext *avctx)
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
291 {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
292 AACEncContext *s = avctx->priv_data;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
293
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
294 ff_mdct_end(&s->mdct1024);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
295 ff_mdct_end(&s->mdct128);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
296 ff_aac_psy_end(&s->psy);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
297 av_freep(&s->samples);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
298 av_freep(&s->cpe);
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
299 return 0;
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
300 }
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
301
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
302 AVCodec aac_encoder = {
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
303 "aac",
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
304 CODEC_TYPE_AUDIO,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
305 CODEC_ID_AAC,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
306 sizeof(AACEncContext),
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
307 aac_encode_init,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
308 aac_encode_frame,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
309 aac_encode_end,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
310 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
311 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
312 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
58f6bb760994 Okayed parts of AAC encoder
kostya
parents:
diff changeset
313 };