aacenc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. * @file libavcodec/aacenc.c
  23. * AAC encoder
  24. */
  25. /***********************************
  26. * TODOs:
  27. * psy model selection with some option
  28. * add sane pulse detection
  29. * add temporal noise shaping
  30. ***********************************/
  31. #include "avcodec.h"
  32. #include "bitstream.h"
  33. #include "dsputil.h"
  34. #include "mpeg4audio.h"
  35. #include "aacpsy.h"
  36. #include "aac.h"
  37. #include "aactab.h"
  38. static const uint8_t swb_size_1024_96[] = {
  39. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
  40. 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
  41. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  42. };
  43. static const uint8_t swb_size_1024_64[] = {
  44. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
  45. 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
  46. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
  47. };
  48. static const uint8_t swb_size_1024_48[] = {
  49. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
  50. 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
  51. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  52. 96
  53. };
  54. static const uint8_t swb_size_1024_32[] = {
  55. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
  56. 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
  57. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  58. };
  59. static const uint8_t swb_size_1024_24[] = {
  60. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  61. 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
  62. 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
  63. };
  64. static const uint8_t swb_size_1024_16[] = {
  65. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  66. 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
  67. 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
  68. };
  69. static const uint8_t swb_size_1024_8[] = {
  70. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  71. 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
  72. 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
  73. };
  74. static const uint8_t * const swb_size_1024[] = {
  75. swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
  76. swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
  77. swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
  78. swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
  79. };
  80. static const uint8_t swb_size_128_96[] = {
  81. 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
  82. };
  83. static const uint8_t swb_size_128_48[] = {
  84. 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
  85. };
  86. static const uint8_t swb_size_128_24[] = {
  87. 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
  88. };
  89. static const uint8_t swb_size_128_16[] = {
  90. 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
  91. };
  92. static const uint8_t swb_size_128_8[] = {
  93. 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
  94. };
  95. static const uint8_t * const swb_size_128[] = {
  96. /* the last entry on the following row is swb_size_128_64 but is a
  97. duplicate of swb_size_128_96 */
  98. swb_size_128_96, swb_size_128_96, swb_size_128_96,
  99. swb_size_128_48, swb_size_128_48, swb_size_128_48,
  100. swb_size_128_24, swb_size_128_24, swb_size_128_16,
  101. swb_size_128_16, swb_size_128_16, swb_size_128_8
  102. };
  103. /** bits needed to code codebook run value for long windows */
  104. static const uint8_t run_value_bits_long[64] = {
  105. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  106. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
  107. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  108. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
  109. };
  110. /** bits needed to code codebook run value for short windows */
  111. static const uint8_t run_value_bits_short[16] = {
  112. 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
  113. };
  114. static const uint8_t* const run_value_bits[2] = {
  115. run_value_bits_long, run_value_bits_short
  116. };
  117. /** default channel configurations */
  118. static const uint8_t aac_chan_configs[6][5] = {
  119. {1, TYPE_SCE}, // 1 channel - single channel element
  120. {1, TYPE_CPE}, // 2 channels - channel pair
  121. {2, TYPE_SCE, TYPE_CPE}, // 3 channels - center + stereo
  122. {3, TYPE_SCE, TYPE_CPE, TYPE_SCE}, // 4 channels - front center + stereo + back center
  123. {3, TYPE_SCE, TYPE_CPE, TYPE_CPE}, // 5 channels - front center + stereo + back stereo
  124. {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
  125. };
  126. /**
  127. * structure used in optimal codebook search
  128. */
  129. typedef struct BandCodingPath {
  130. int prev_idx; ///< pointer to the previous path point
  131. int codebook; ///< codebook for coding band run
  132. int bits; ///< number of bit needed to code given number of bands
  133. } BandCodingPath;
  134. /**
  135. * AAC encoder context
  136. */
  137. typedef struct {
  138. PutBitContext pb;
  139. MDCTContext mdct1024; ///< long (1024 samples) frame transform context
  140. MDCTContext mdct128; ///< short (128 samples) frame transform context
  141. DSPContext dsp;
  142. DECLARE_ALIGNED_16(FFTSample, output[2048]); ///< temporary buffer for MDCT input coefficients
  143. int16_t* samples; ///< saved preprocessed input
  144. int samplerate_index; ///< MPEG-4 samplerate index
  145. ChannelElement *cpe; ///< channel elements
  146. AACPsyContext psy; ///< psychoacoustic model context
  147. int last_frame;
  148. } AACEncContext;
  149. /**
  150. * Make AAC audio config object.
  151. * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
  152. */
  153. static void put_audio_specific_config(AVCodecContext *avctx)
  154. {
  155. PutBitContext pb;
  156. AACEncContext *s = avctx->priv_data;
  157. init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
  158. put_bits(&pb, 5, 2); //object type - AAC-LC
  159. put_bits(&pb, 4, s->samplerate_index); //sample rate index
  160. put_bits(&pb, 4, avctx->channels);
  161. //GASpecificConfig
  162. put_bits(&pb, 1, 0); //frame length - 1024 samples
  163. put_bits(&pb, 1, 0); //does not depend on core coder
  164. put_bits(&pb, 1, 0); //is not extension
  165. flush_put_bits(&pb);
  166. }
  167. static av_cold int aac_encode_init(AVCodecContext *avctx)
  168. {
  169. AACEncContext *s = avctx->priv_data;
  170. int i;
  171. avctx->frame_size = 1024;
  172. for(i = 0; i < 16; i++)
  173. if(avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
  174. break;
  175. if(i == 16){
  176. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
  177. return -1;
  178. }
  179. if(avctx->channels > 6){
  180. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
  181. return -1;
  182. }
  183. s->samplerate_index = i;
  184. dsputil_init(&s->dsp, avctx);
  185. ff_mdct_init(&s->mdct1024, 11, 0);
  186. ff_mdct_init(&s->mdct128, 8, 0);
  187. // window init
  188. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  189. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  190. ff_sine_window_init(ff_sine_1024, 1024);
  191. ff_sine_window_init(ff_sine_128, 128);
  192. s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
  193. s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
  194. if(ff_aac_psy_init(&s->psy, avctx, AAC_PSY_3GPP,
  195. aac_chan_configs[avctx->channels-1][0], 0,
  196. swb_size_1024[i], ff_aac_num_swb_1024[i], swb_size_128[i], ff_aac_num_swb_128[i]) < 0){
  197. av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n");
  198. return -1;
  199. }
  200. avctx->extradata = av_malloc(2);
  201. avctx->extradata_size = 2;
  202. put_audio_specific_config(avctx);
  203. return 0;
  204. }
  205. /**
  206. * Encode ics_info element.
  207. * @see Table 4.6 (syntax of ics_info)
  208. */
  209. static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
  210. {
  211. int i;
  212. put_bits(&s->pb, 1, 0); // ics_reserved bit
  213. put_bits(&s->pb, 2, info->window_sequence[0]);
  214. put_bits(&s->pb, 1, info->use_kb_window[0]);
  215. if(info->window_sequence[0] != EIGHT_SHORT_SEQUENCE){
  216. put_bits(&s->pb, 6, info->max_sfb);
  217. put_bits(&s->pb, 1, 0); // no prediction
  218. }else{
  219. put_bits(&s->pb, 4, info->max_sfb);
  220. for(i = 1; i < info->num_windows; i++)
  221. put_bits(&s->pb, 1, info->group_len[i]);
  222. }
  223. }
  224. /**
  225. * Calculate the number of bits needed to code all coefficient signs in current band.
  226. */
  227. static int calculate_band_sign_bits(AACEncContext *s, SingleChannelElement *sce,
  228. int group_len, int start, int size)
  229. {
  230. int bits = 0;
  231. int i, w;
  232. for(w = 0; w < group_len; w++){
  233. for(i = 0; i < size; i++){
  234. if(sce->icoefs[start + i])
  235. bits++;
  236. }
  237. start += 128;
  238. }
  239. return bits;
  240. }
  241. /**
  242. * Encode pulse data.
  243. */
  244. static void encode_pulses(AACEncContext *s, Pulse *pulse)
  245. {
  246. int i;
  247. put_bits(&s->pb, 1, !!pulse->num_pulse);
  248. if(!pulse->num_pulse) return;
  249. put_bits(&s->pb, 2, pulse->num_pulse - 1);
  250. put_bits(&s->pb, 6, pulse->start);
  251. for(i = 0; i < pulse->num_pulse; i++){
  252. put_bits(&s->pb, 5, pulse->pos[i]);
  253. put_bits(&s->pb, 4, pulse->amp[i]);
  254. }
  255. }
  256. /**
  257. * Encode spectral coefficients processed by psychoacoustic model.
  258. */
  259. static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
  260. {
  261. int start, i, w, w2, wg;
  262. w = 0;
  263. for(wg = 0; wg < sce->ics.num_window_groups; wg++){
  264. start = 0;
  265. for(i = 0; i < sce->ics.max_sfb; i++){
  266. if(sce->zeroes[w*16 + i]){
  267. start += sce->ics.swb_sizes[i];
  268. continue;
  269. }
  270. for(w2 = w; w2 < w + sce->ics.group_len[wg]; w2++){
  271. encode_band_coeffs(s, sce, start + w2*128,
  272. sce->ics.swb_sizes[i],
  273. sce->band_type[w*16 + i]);
  274. }
  275. start += sce->ics.swb_sizes[i];
  276. }
  277. w += sce->ics.group_len[wg];
  278. }
  279. }
  280. /**
  281. * Write some auxiliary information about the created AAC file.
  282. */
  283. static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name)
  284. {
  285. int i, namelen, padbits;
  286. namelen = strlen(name) + 2;
  287. put_bits(&s->pb, 3, TYPE_FIL);
  288. put_bits(&s->pb, 4, FFMIN(namelen, 15));
  289. if(namelen >= 15)
  290. put_bits(&s->pb, 8, namelen - 16);
  291. put_bits(&s->pb, 4, 0); //extension type - filler
  292. padbits = 8 - (put_bits_count(&s->pb) & 7);
  293. align_put_bits(&s->pb);
  294. for(i = 0; i < namelen - 2; i++)
  295. put_bits(&s->pb, 8, name[i]);
  296. put_bits(&s->pb, 12 - padbits, 0);
  297. }
  298. static av_cold int aac_encode_end(AVCodecContext *avctx)
  299. {
  300. AACEncContext *s = avctx->priv_data;
  301. ff_mdct_end(&s->mdct1024);
  302. ff_mdct_end(&s->mdct128);
  303. ff_aac_psy_end(&s->psy);
  304. av_freep(&s->samples);
  305. av_freep(&s->cpe);
  306. return 0;
  307. }
  308. AVCodec aac_encoder = {
  309. "aac",
  310. CODEC_TYPE_AUDIO,
  311. CODEC_ID_AAC,
  312. sizeof(AACEncContext),
  313. aac_encode_init,
  314. aac_encode_frame,
  315. aac_encode_end,
  316. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  317. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  318. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  319. };