comparison aacenc.c @ 7584:d8717018ac03 libavcodec

Add approved chunks to AAC encoder
author kostya
date Sat, 16 Aug 2008 05:47:18 +0000
parents 27ee0ceab150
children 12976f458c7a
comparison
equal deleted inserted replaced
7583:2a3f40605dec 7584:d8717018ac03
25 */ 25 */
26 26
27 /*********************************** 27 /***********************************
28 * TODOs: 28 * TODOs:
29 * psy model selection with some option 29 * psy model selection with some option
30 * change greedy codebook search into something more optimal, like Viterbi algorithm 30 * add sane pulse detection
31 * determine run lengths along with codebook
32 ***********************************/ 31 ***********************************/
33 32
34 #include "avcodec.h" 33 #include "avcodec.h"
35 #include "bitstream.h" 34 #include "bitstream.h"
36 #include "dsputil.h" 35 #include "dsputil.h"
126 {2, ID_SCE, ID_CPE}, // 3 channels - center + stereo 125 {2, ID_SCE, ID_CPE}, // 3 channels - center + stereo
127 {3, ID_SCE, ID_CPE, ID_SCE}, // 4 channels - front center + stereo + back center 126 {3, ID_SCE, ID_CPE, ID_SCE}, // 4 channels - front center + stereo + back center
128 {3, ID_SCE, ID_CPE, ID_CPE}, // 5 channels - front center + stereo + back stereo 127 {3, ID_SCE, ID_CPE, ID_CPE}, // 5 channels - front center + stereo + back stereo
129 {4, ID_SCE, ID_CPE, ID_CPE, ID_LFE}, // 6 channels - front center + stereo + back stereo + LFE 128 {4, ID_SCE, ID_CPE, ID_CPE, ID_LFE}, // 6 channels - front center + stereo + back stereo + LFE
130 }; 129 };
130
131 /**
132 * AAC encoder context
133 */
134 typedef struct {
135 PutBitContext pb;
136 MDCTContext mdct1024; ///< long (1024 samples) frame transform context
137 MDCTContext mdct128; ///< short (128 samples) frame transform context
138 DSPContext dsp;
139 } AACEncContext;
131 140
132 /** 141 /**
133 * Make AAC audio config object. 142 * Make AAC audio config object.
134 * @see 1.6.2.1 "Syntax - AudioSpecificConfig" 143 * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
135 */ 144 */
174 s->swb_num128 = ff_aac_num_swb_128[i]; 183 s->swb_num128 = ff_aac_num_swb_128[i];
175 184
176 dsputil_init(&s->dsp, avctx); 185 dsputil_init(&s->dsp, avctx);
177 ff_mdct_init(&s->mdct1024, 11, 0); 186 ff_mdct_init(&s->mdct1024, 11, 0);
178 ff_mdct_init(&s->mdct128, 8, 0); 187 ff_mdct_init(&s->mdct128, 8, 0);
188 // window init
189 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
190 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
191 ff_sine_window_init(ff_sine_1024, 1024);
192 ff_sine_window_init(ff_sine_128, 128);
179 193
180 s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0])); 194 s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
181 s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]); 195 s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
182 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){ 196 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){
183 av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n"); 197 av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n");
206 put_bits(&s->pb, 1, 0); // no prediction 220 put_bits(&s->pb, 1, 0); // no prediction
207 }else{ 221 }else{
208 put_bits(&s->pb, 4, info->max_sfb); 222 put_bits(&s->pb, 4, info->max_sfb);
209 for(i = 1; i < info->num_windows; i++) 223 for(i = 1; i < info->num_windows; i++)
210 put_bits(&s->pb, 1, info->group_len[i]); 224 put_bits(&s->pb, 1, info->group_len[i]);
225 }
226 }
227
228 /**
229 * Encode pulse data.
230 */
231 static void encode_pulses(AVCodecContext *avctx, AACEncContext *s, Pulse *pulse, int channel)
232 {
233 int i;
234
235 put_bits(&s->pb, 1, !!pulse->num_pulse);
236 if(!pulse->num_pulse) return;
237
238 put_bits(&s->pb, 2, pulse->num_pulse - 1);
239 put_bits(&s->pb, 6, pulse->start);
240 for(i = 0; i < pulse->num_pulse; i++){
241 put_bits(&s->pb, 5, pulse->offset[i]);
242 put_bits(&s->pb, 4, pulse->amp[i]);
243 }
244 }
245
246 /**
247 * Encode spectral coefficients processed by psychoacoustic model.
248 */
249 static void encode_spectral_coeffs(AVCodecContext *avctx, AACEncContext *s, ChannelElement *cpe, int channel)
250 {
251 int start, i, w, w2, wg;
252
253 w = 0;
254 for(wg = 0; wg < cpe->ch[channel].ics.num_window_groups; wg++){
255 start = 0;
256 for(i = 0; i < cpe->ch[channel].ics.max_sfb; i++){
257 if(cpe->ch[channel].zeroes[w][i]){
258 start += cpe->ch[channel].ics.swb_sizes[i];
259 continue;
260 }
261 for(w2 = w; w2 < w + cpe->ch[channel].ics.group_len[wg]; w2++){
262 encode_band_coeffs(s, cpe, channel, start + w2*128, cpe->ch[channel].ics.swb_sizes[i], cpe->ch[channel].band_type[w][i]);
263 }
264 start += cpe->ch[channel].ics.swb_sizes[i];
265 }
266 w += cpe->ch[channel].ics.group_len[wg];
211 } 267 }
212 } 268 }
213 269
214 /** 270 /**
215 * Write some auxiliary information about the created AAC file. 271 * Write some auxiliary information about the created AAC file.