aacenc.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. #ifndef AVCODEC_AACENC_H
  22. #define AVCODEC_AACENC_H
  23. #include "libavutil/float_dsp.h"
  24. #include "avcodec.h"
  25. #include "put_bits.h"
  26. #include "aac.h"
  27. #include "audio_frame_queue.h"
  28. #include "psymodel.h"
  29. #include "lpc.h"
  30. typedef enum AACCoder {
  31. AAC_CODER_ANMR = 0,
  32. AAC_CODER_TWOLOOP,
  33. AAC_CODER_FAST,
  34. AAC_CODER_NB,
  35. }AACCoder;
  36. typedef struct AACEncOptions {
  37. int coder;
  38. int pns;
  39. int tns;
  40. int ltp;
  41. int pce;
  42. int pred;
  43. int mid_side;
  44. int intensity_stereo;
  45. } AACEncOptions;
  46. struct AACEncContext;
  47. typedef struct AACCoefficientsEncoder {
  48. void (*search_for_quantizers)(AVCodecContext *avctx, struct AACEncContext *s,
  49. SingleChannelElement *sce, const float lambda);
  50. void (*encode_window_bands_info)(struct AACEncContext *s, SingleChannelElement *sce,
  51. int win, int group_len, const float lambda);
  52. void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size,
  53. int scale_idx, int cb, const float lambda, int rtz);
  54. void (*encode_tns_info)(struct AACEncContext *s, SingleChannelElement *sce);
  55. void (*encode_ltp_info)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
  56. void (*encode_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
  57. void (*adjust_common_pred)(struct AACEncContext *s, ChannelElement *cpe);
  58. void (*adjust_common_ltp)(struct AACEncContext *s, ChannelElement *cpe);
  59. void (*apply_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
  60. void (*apply_tns_filt)(struct AACEncContext *s, SingleChannelElement *sce);
  61. void (*update_ltp)(struct AACEncContext *s, SingleChannelElement *sce);
  62. void (*ltp_insert_new_frame)(struct AACEncContext *s);
  63. void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
  64. void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
  65. void (*mark_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
  66. void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);
  67. void (*search_for_ltp)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
  68. void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe);
  69. void (*search_for_is)(struct AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe);
  70. void (*search_for_pred)(struct AACEncContext *s, SingleChannelElement *sce);
  71. } AACCoefficientsEncoder;
  72. extern const AACCoefficientsEncoder ff_aac_coders[];
  73. typedef struct AACQuantizeBandCostCacheEntry {
  74. float rd;
  75. float energy;
  76. int bits;
  77. char cb;
  78. char rtz;
  79. uint16_t generation;
  80. } AACQuantizeBandCostCacheEntry;
  81. typedef struct AACPCEInfo {
  82. int64_t layout;
  83. int num_ele[4]; ///< front, side, back, lfe
  84. int pairing[3][8]; ///< front, side, back
  85. int index[4][8]; ///< front, side, back, lfe
  86. uint8_t config_map[16]; ///< configs the encoder's channel specific settings
  87. uint8_t reorder_map[16]; ///< maps channels from lavc to aac order
  88. } AACPCEInfo;
  89. /**
  90. * List of PCE (Program Configuration Element) for the channel layouts listed
  91. * in channel_layout.h
  92. *
  93. * For those wishing in the future to add other layouts:
  94. *
  95. * - num_ele: number of elements in each group of front, side, back, lfe channels
  96. * (an element is of type SCE (single channel), CPE (channel pair) for
  97. * the first 3 groups; and is LFE for LFE group).
  98. *
  99. * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group
  100. *
  101. * - index: there are three independent indices for SCE, CPE and LFE;
  102. * they are incremented irrespective of the group to which the element belongs;
  103. * they are not reset when going from one group to another
  104. *
  105. * Example: for 7.0 channel layout,
  106. * .pairing = { { 1, 0 }, { 1 }, { 1 }, }, (3 CPE and 1 SCE in front group)
  107. * .index = { { 0, 0 }, { 1 }, { 2 }, },
  108. * (index is 0 for the single SCE but goes from 0 to 2 for the CPEs)
  109. *
  110. * The index order impacts the channel ordering. But is otherwise arbitrary
  111. * (the sequence could have been 2, 0, 1 instead of 0, 1, 2).
  112. *
  113. * Spec allows for discontinuous indices, e.g. if one has a total of two SCE,
  114. * SCE.0 SCE.15 is OK per spec; BUT it won't be decoded by our AAC decoder
  115. * which at this time requires that indices fully cover some range starting
  116. * from 0 (SCE.1 SCE.0 is OK but not SCE.0 SCE.15).
  117. *
  118. * - config_map: total number of elements and their types. Beware, the way the
  119. * types are ordered impacts the final channel ordering.
  120. *
  121. * - reorder_map: reorders the channels.
  122. *
  123. */
  124. static const AACPCEInfo aac_pce_configs[] = {
  125. {
  126. .layout = AV_CH_LAYOUT_MONO,
  127. .num_ele = { 1, 0, 0, 0 },
  128. .pairing = { { 0 }, },
  129. .index = { { 0 }, },
  130. .config_map = { 1, TYPE_SCE, },
  131. .reorder_map = { 0 },
  132. },
  133. {
  134. .layout = AV_CH_LAYOUT_STEREO,
  135. .num_ele = { 1, 0, 0, 0 },
  136. .pairing = { { 1 }, },
  137. .index = { { 0 }, },
  138. .config_map = { 1, TYPE_CPE, },
  139. .reorder_map = { 0, 1 },
  140. },
  141. {
  142. .layout = AV_CH_LAYOUT_2POINT1,
  143. .num_ele = { 1, 0, 0, 1 },
  144. .pairing = { { 1 }, },
  145. .index = { { 0 },{ 0 },{ 0 },{ 0 } },
  146. .config_map = { 2, TYPE_CPE, TYPE_LFE },
  147. .reorder_map = { 0, 1, 2 },
  148. },
  149. {
  150. .layout = AV_CH_LAYOUT_2_1,
  151. .num_ele = { 1, 0, 1, 0 },
  152. .pairing = { { 1 },{ 0 },{ 0 } },
  153. .index = { { 0 },{ 0 },{ 0 }, },
  154. .config_map = { 2, TYPE_CPE, TYPE_SCE },
  155. .reorder_map = { 0, 1, 2 },
  156. },
  157. {
  158. .layout = AV_CH_LAYOUT_SURROUND,
  159. .num_ele = { 2, 0, 0, 0 },
  160. .pairing = { { 1, 0 }, },
  161. .index = { { 0, 0 }, },
  162. .config_map = { 2, TYPE_CPE, TYPE_SCE, },
  163. .reorder_map = { 0, 1, 2 },
  164. },
  165. {
  166. .layout = AV_CH_LAYOUT_3POINT1,
  167. .num_ele = { 2, 0, 0, 1 },
  168. .pairing = { { 1, 0 }, },
  169. .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, },
  170. .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_LFE },
  171. .reorder_map = { 0, 1, 2, 3 },
  172. },
  173. {
  174. .layout = AV_CH_LAYOUT_4POINT0,
  175. .num_ele = { 2, 0, 1, 0 },
  176. .pairing = { { 1, 0 }, { 0 }, { 0 }, },
  177. .index = { { 0, 0 }, { 0 }, { 1 } },
  178. .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_SCE },
  179. .reorder_map = { 0, 1, 2, 3 },
  180. },
  181. {
  182. .layout = AV_CH_LAYOUT_4POINT1,
  183. .num_ele = { 2, 1, 1, 0 },
  184. .pairing = { { 1, 0 }, { 0 }, { 0 }, },
  185. .index = { { 0, 0 }, { 1 }, { 2 }, { 0 } },
  186. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_SCE },
  187. .reorder_map = { 0, 1, 2, 3, 4 },
  188. },
  189. {
  190. .layout = AV_CH_LAYOUT_2_2,
  191. .num_ele = { 1, 1, 0, 0 },
  192. .pairing = { { 1 }, { 1 }, },
  193. .index = { { 0 }, { 1 }, },
  194. .config_map = { 2, TYPE_CPE, TYPE_CPE },
  195. .reorder_map = { 0, 1, 2, 3 },
  196. },
  197. {
  198. .layout = AV_CH_LAYOUT_QUAD,
  199. .num_ele = { 1, 0, 1, 0 },
  200. .pairing = { { 1 }, { 0 }, { 1 }, },
  201. .index = { { 0 }, { 0 }, { 1 } },
  202. .config_map = { 2, TYPE_CPE, TYPE_CPE },
  203. .reorder_map = { 0, 1, 2, 3 },
  204. },
  205. {
  206. .layout = AV_CH_LAYOUT_5POINT0,
  207. .num_ele = { 2, 1, 0, 0 },
  208. .pairing = { { 1, 0 }, { 1 }, },
  209. .index = { { 0, 0 }, { 1 } },
  210. .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE },
  211. .reorder_map = { 0, 1, 2, 3, 4 },
  212. },
  213. {
  214. .layout = AV_CH_LAYOUT_5POINT1,
  215. .num_ele = { 2, 1, 1, 0 },
  216. .pairing = { { 1, 0 }, { 0 }, { 1 }, },
  217. .index = { { 0, 0 }, { 1 }, { 1 } },
  218. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE },
  219. .reorder_map = { 0, 1, 2, 3, 4, 5 },
  220. },
  221. {
  222. .layout = AV_CH_LAYOUT_5POINT0_BACK,
  223. .num_ele = { 2, 0, 1, 0 },
  224. .pairing = { { 1, 0 }, { 0 }, { 1 } },
  225. .index = { { 0, 0 }, { 0 }, { 1 } },
  226. .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE },
  227. .reorder_map = { 0, 1, 2, 3, 4 },
  228. },
  229. {
  230. .layout = AV_CH_LAYOUT_5POINT1_BACK,
  231. .num_ele = { 2, 1, 1, 0 },
  232. .pairing = { { 1, 0 }, { 0 }, { 1 }, },
  233. .index = { { 0, 0 }, { 1 }, { 1 } },
  234. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE },
  235. .reorder_map = { 0, 1, 2, 3, 4, 5 },
  236. },
  237. {
  238. .layout = AV_CH_LAYOUT_6POINT0,
  239. .num_ele = { 2, 1, 1, 0 },
  240. .pairing = { { 1, 0 }, { 1 }, { 0 }, },
  241. .index = { { 0, 0 }, { 1 }, { 1 } },
  242. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  243. .reorder_map = { 0, 1, 2, 3, 4, 5 },
  244. },
  245. {
  246. .layout = AV_CH_LAYOUT_6POINT0_FRONT,
  247. .num_ele = { 2, 1, 0, 0 },
  248. .pairing = { { 1, 1 }, { 1 } },
  249. .index = { { 1, 0 }, { 2 }, },
  250. .config_map = { 3, TYPE_CPE, TYPE_CPE, TYPE_CPE, },
  251. .reorder_map = { 0, 1, 2, 3, 4, 5 },
  252. },
  253. {
  254. .layout = AV_CH_LAYOUT_HEXAGONAL,
  255. .num_ele = { 2, 0, 2, 0 },
  256. .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
  257. .index = { { 0, 0 },{ 0 },{ 1, 1 } },
  258. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, },
  259. .reorder_map = { 0, 1, 2, 3, 4, 5 },
  260. },
  261. {
  262. .layout = AV_CH_LAYOUT_6POINT1,
  263. .num_ele = { 2, 1, 2, 0 },
  264. .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
  265. .index = { { 0, 0 },{ 1 },{ 1, 2 } },
  266. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  267. .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
  268. },
  269. {
  270. .layout = AV_CH_LAYOUT_6POINT1_BACK,
  271. .num_ele = { 2, 1, 2, 0 },
  272. .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
  273. .index = { { 0, 0 }, { 1 }, { 1, 2 } },
  274. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  275. .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
  276. },
  277. {
  278. .layout = AV_CH_LAYOUT_6POINT1_FRONT,
  279. .num_ele = { 2, 1, 2, 0 },
  280. .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
  281. .index = { { 0, 0 }, { 1 }, { 1, 2 } },
  282. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  283. .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
  284. },
  285. {
  286. .layout = AV_CH_LAYOUT_7POINT0,
  287. .num_ele = { 2, 1, 1, 0 },
  288. .pairing = { { 1, 0 }, { 1 }, { 1 }, },
  289. .index = { { 0, 0 }, { 1 }, { 2 }, },
  290. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
  291. .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
  292. },
  293. {
  294. .layout = AV_CH_LAYOUT_7POINT0_FRONT,
  295. .num_ele = { 2, 1, 1, 0 },
  296. .pairing = { { 1, 0 }, { 1 }, { 1 }, },
  297. .index = { { 0, 0 }, { 1 }, { 2 }, },
  298. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
  299. .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
  300. },
  301. {
  302. .layout = AV_CH_LAYOUT_7POINT1,
  303. .num_ele = { 2, 1, 2, 0 },
  304. .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
  305. .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
  306. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
  307. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
  308. },
  309. {
  310. .layout = AV_CH_LAYOUT_7POINT1_WIDE,
  311. .num_ele = { 2, 1, 2, 0 },
  312. .pairing = { { 1, 0 }, { 0 },{ 1, 1 }, },
  313. .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
  314. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
  315. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
  316. },
  317. {
  318. .layout = AV_CH_LAYOUT_7POINT1_WIDE_BACK,
  319. .num_ele = { 2, 1, 2, 0 },
  320. .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
  321. .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
  322. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
  323. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
  324. },
  325. {
  326. .layout = AV_CH_LAYOUT_OCTAGONAL,
  327. .num_ele = { 2, 1, 2, 0 },
  328. .pairing = { { 1, 0 }, { 1 }, { 1, 0 }, },
  329. .index = { { 0, 0 }, { 1 }, { 2, 1 } },
  330. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE },
  331. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
  332. },
  333. { /* Meant for order 2/mixed ambisonics */
  334. .layout = AV_CH_LAYOUT_OCTAGONAL | AV_CH_TOP_CENTER,
  335. .num_ele = { 2, 2, 2, 0 },
  336. .pairing = { { 1, 0 }, { 1, 0 }, { 1, 0 }, },
  337. .index = { { 0, 0 }, { 1, 1 }, { 2, 2 } },
  338. .config_map = { 6, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  339. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8 },
  340. },
  341. { /* Meant for order 2/mixed ambisonics */
  342. .layout = AV_CH_LAYOUT_6POINT0_FRONT | AV_CH_BACK_CENTER |
  343. AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_TOP_CENTER,
  344. .num_ele = { 2, 2, 2, 0 },
  345. .pairing = { { 1, 1 }, { 1, 0 }, { 1, 0 }, },
  346. .index = { { 0, 1 }, { 2, 0 }, { 3, 1 } },
  347. .config_map = { 6, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  348. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
  349. },
  350. {
  351. .layout = AV_CH_LAYOUT_HEXADECAGONAL,
  352. .num_ele = { 4, 2, 4, 0 },
  353. .pairing = { { 1, 0, 1, 0 }, { 1, 1 }, { 1, 0, 1, 0 }, },
  354. .index = { { 0, 0, 1, 1 }, { 2, 3 }, { 4, 2, 5, 3 } },
  355. .config_map = { 10, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  356. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
  357. },
  358. };
  359. /**
  360. * AAC encoder context
  361. */
  362. typedef struct AACEncContext {
  363. AVClass *av_class;
  364. AACEncOptions options; ///< encoding options
  365. PutBitContext pb;
  366. FFTContext mdct1024; ///< long (1024 samples) frame transform context
  367. FFTContext mdct128; ///< short (128 samples) frame transform context
  368. AVFloatDSPContext *fdsp;
  369. AACPCEInfo pce; ///< PCE data, if needed
  370. float *planar_samples[16]; ///< saved preprocessed input
  371. int profile; ///< copied from avctx
  372. int needs_pce; ///< flag for non-standard layout
  373. LPCContext lpc; ///< used by TNS
  374. int samplerate_index; ///< MPEG-4 samplerate index
  375. int channels; ///< channel count
  376. const uint8_t *reorder_map; ///< lavc to aac reorder map
  377. const uint8_t *chan_map; ///< channel configuration map
  378. ChannelElement *cpe; ///< channel elements
  379. FFPsyContext psy;
  380. struct FFPsyPreprocessContext* psypp;
  381. const AACCoefficientsEncoder *coder;
  382. int cur_channel; ///< current channel for coder context
  383. int random_state;
  384. float lambda;
  385. int last_frame_pb_count; ///< number of bits for the previous frame
  386. float lambda_sum; ///< sum(lambda), for Qvg reporting
  387. int lambda_count; ///< count(lambda), for Qvg reporting
  388. enum RawDataBlockType cur_type; ///< channel group type cur_channel belongs to
  389. AudioFrameQueue afq;
  390. DECLARE_ALIGNED(16, int, qcoefs)[96]; ///< quantized coefficients
  391. DECLARE_ALIGNED(32, float, scoefs)[1024]; ///< scaled coefficients
  392. uint16_t quantize_band_cost_cache_generation;
  393. AACQuantizeBandCostCacheEntry quantize_band_cost_cache[256][128]; ///< memoization area for quantize_band_cost
  394. void (*abs_pow34)(float *out, const float *in, const int size);
  395. void (*quant_bands)(int *out, const float *in, const float *scaled,
  396. int size, int is_signed, int maxval, const float Q34,
  397. const float rounding);
  398. struct {
  399. float *samples;
  400. } buffer;
  401. } AACEncContext;
  402. void ff_aac_dsp_init_x86(AACEncContext *s);
  403. void ff_aac_coder_init_mips(AACEncContext *c);
  404. void ff_quantize_band_cost_cache_init(struct AACEncContext *s);
  405. #endif /* AVCODEC_AACENC_H */