FFmpeg
aacenc.c
Go to the documentation of this file.
1 /*
2  * AAC encoder
3  * Copyright (C) 2008 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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  ***********************************/
31 
32 #include "libavutil/libm.h"
33 #include "libavutil/thread.h"
34 #include "libavutil/float_dsp.h"
35 #include "libavutil/opt.h"
36 #include "avcodec.h"
37 #include "put_bits.h"
38 #include "internal.h"
39 #include "mpeg4audio.h"
40 #include "kbdwin.h"
41 #include "sinewin.h"
42 
43 #include "aac.h"
44 #include "aactab.h"
45 #include "aacenc.h"
46 #include "aacenctab.h"
47 #include "aacenc_utils.h"
48 
49 #include "psymodel.h"
50 
52 
53 static void put_pce(PutBitContext *pb, AVCodecContext *avctx)
54 {
55  int i, j;
56  AACEncContext *s = avctx->priv_data;
57  AACPCEInfo *pce = &s->pce;
58  const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT;
59  const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT;
60 
61  put_bits(pb, 4, 0);
62 
63  put_bits(pb, 2, avctx->profile);
64  put_bits(pb, 4, s->samplerate_index);
65 
66  put_bits(pb, 4, pce->num_ele[0]); /* Front */
67  put_bits(pb, 4, pce->num_ele[1]); /* Side */
68  put_bits(pb, 4, pce->num_ele[2]); /* Back */
69  put_bits(pb, 2, pce->num_ele[3]); /* LFE */
70  put_bits(pb, 3, 0); /* Assoc data */
71  put_bits(pb, 4, 0); /* CCs */
72 
73  put_bits(pb, 1, 0); /* Stereo mixdown */
74  put_bits(pb, 1, 0); /* Mono mixdown */
75  put_bits(pb, 1, 0); /* Something else */
76 
77  for (i = 0; i < 4; i++) {
78  for (j = 0; j < pce->num_ele[i]; j++) {
79  if (i < 3)
80  put_bits(pb, 1, pce->pairing[i][j]);
81  put_bits(pb, 4, pce->index[i][j]);
82  }
83  }
84 
86  put_bits(pb, 8, strlen(aux_data));
87  avpriv_put_string(pb, aux_data, 0);
88 }
89 
90 /**
91  * Make AAC audio config object.
92  * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
93  */
95 {
96  PutBitContext pb;
97  AACEncContext *s = avctx->priv_data;
98  int channels = (!s->needs_pce)*(s->channels - (s->channels == 8 ? 1 : 0));
99  const int max_size = 32;
100 
101  avctx->extradata = av_mallocz(max_size);
102  if (!avctx->extradata)
103  return AVERROR(ENOMEM);
104 
105  init_put_bits(&pb, avctx->extradata, max_size);
106  put_bits(&pb, 5, s->profile+1); //profile
107  put_bits(&pb, 4, s->samplerate_index); //sample rate index
108  put_bits(&pb, 4, channels);
109  //GASpecificConfig
110  put_bits(&pb, 1, 0); //frame length - 1024 samples
111  put_bits(&pb, 1, 0); //does not depend on core coder
112  put_bits(&pb, 1, 0); //is not extension
113  if (s->needs_pce)
114  put_pce(&pb, avctx);
115 
116  //Explicitly Mark SBR absent
117  put_bits(&pb, 11, 0x2b7); //sync extension
118  put_bits(&pb, 5, AOT_SBR);
119  put_bits(&pb, 1, 0);
120  flush_put_bits(&pb);
121  avctx->extradata_size = put_bits_count(&pb) >> 3;
122 
123  return 0;
124 }
125 
127 {
130  memset(s->quantize_band_cost_cache, 0, sizeof(s->quantize_band_cost_cache));
132  }
133 }
134 
135 #define WINDOW_FUNC(type) \
136 static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
137  SingleChannelElement *sce, \
138  const float *audio)
139 
140 WINDOW_FUNC(only_long)
141 {
142  const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
143  const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
144  float *out = sce->ret_buf;
145 
146  fdsp->vector_fmul (out, audio, lwindow, 1024);
147  fdsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024);
148 }
149 
150 WINDOW_FUNC(long_start)
151 {
152  const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
153  const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
154  float *out = sce->ret_buf;
155 
156  fdsp->vector_fmul(out, audio, lwindow, 1024);
157  memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448);
158  fdsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128);
159  memset(out + 1024 + 576, 0, sizeof(out[0]) * 448);
160 }
161 
162 WINDOW_FUNC(long_stop)
163 {
164  const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
165  const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
166  float *out = sce->ret_buf;
167 
168  memset(out, 0, sizeof(out[0]) * 448);
169  fdsp->vector_fmul(out + 448, audio + 448, swindow, 128);
170  memcpy(out + 576, audio + 576, sizeof(out[0]) * 448);
171  fdsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024);
172 }
173 
174 WINDOW_FUNC(eight_short)
175 {
176  const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
177  const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
178  const float *in = audio + 448;
179  float *out = sce->ret_buf;
180  int w;
181 
182  for (w = 0; w < 8; w++) {
183  fdsp->vector_fmul (out, in, w ? pwindow : swindow, 128);
184  out += 128;
185  in += 128;
186  fdsp->vector_fmul_reverse(out, in, swindow, 128);
187  out += 128;
188  }
189 }
190 
191 static void (*const apply_window[4])(AVFloatDSPContext *fdsp,
193  const float *audio) = {
194  [ONLY_LONG_SEQUENCE] = apply_only_long_window,
195  [LONG_START_SEQUENCE] = apply_long_start_window,
196  [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window,
197  [LONG_STOP_SEQUENCE] = apply_long_stop_window
198 };
199 
201  float *audio)
202 {
203  int i;
204  const float *output = sce->ret_buf;
205 
206  apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, audio);
207 
209  s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);
210  else
211  for (i = 0; i < 1024; i += 128)
212  s->mdct128.mdct_calc(&s->mdct128, &sce->coeffs[i], output + i*2);
213  memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);
214  memcpy(sce->pcoeffs, sce->coeffs, sizeof(sce->pcoeffs));
215 }
216 
217 /**
218  * Encode ics_info element.
219  * @see Table 4.6 (syntax of ics_info)
220  */
222 {
223  int w;
224 
225  put_bits(&s->pb, 1, 0);