aacenc.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  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
  23. * AAC encoder
  24. */
  25. /***********************************
  26. * TODOs:
  27. * add sane pulse detection
  28. ***********************************/
  29. #include <float.h>
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/libm.h"
  32. #include "libavutil/float_dsp.h"
  33. #include "libavutil/opt.h"
  34. #include "avcodec.h"
  35. #include "codec_internal.h"
  36. #include "encode.h"
  37. #include "put_bits.h"
  38. #include "mpeg4audio.h"
  39. #include "sinewin.h"
  40. #include "profiles.h"
  41. #include "version.h"
  42. #include "aac.h"
  43. #include "aactab.h"
  44. #include "aacenc.h"
  45. #include "aacenctab.h"
  46. #include "aacenc_utils.h"
  47. #include "psymodel.h"
  48. /**
  49. * List of PCE (Program Configuration Element) for the channel layouts listed
  50. * in channel_layout.h
  51. *
  52. * For those wishing in the future to add other layouts:
  53. *
  54. * - num_ele: number of elements in each group of front, side, back, lfe channels
  55. * (an element is of type SCE (single channel), CPE (channel pair) for
  56. * the first 3 groups; and is LFE for LFE group).
  57. *
  58. * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group
  59. *
  60. * - index: there are three independent indices for SCE, CPE and LFE;
  61. * they are incremented irrespective of the group to which the element belongs;
  62. * they are not reset when going from one group to another
  63. *
  64. * Example: for 7.0 channel layout,
  65. * .pairing = { { 1, 0 }, { 1 }, { 1 }, }, (3 CPE and 1 SCE in front group)
  66. * .index = { { 0, 0 }, { 1 }, { 2 }, },
  67. * (index is 0 for the single SCE but goes from 0 to 2 for the CPEs)
  68. *
  69. * The index order impacts the channel ordering. But is otherwise arbitrary
  70. * (the sequence could have been 2, 0, 1 instead of 0, 1, 2).
  71. *
  72. * Spec allows for discontinuous indices, e.g. if one has a total of two SCE,
  73. * SCE.0 SCE.15 is OK per spec; BUT it won't be decoded by our AAC decoder
  74. * which at this time requires that indices fully cover some range starting
  75. * from 0 (SCE.1 SCE.0 is OK but not SCE.0 SCE.15).
  76. *
  77. * - config_map: total number of elements and their types. Beware, the way the
  78. * types are ordered impacts the final channel ordering.
  79. *
  80. * - reorder_map: reorders the channels.
  81. *
  82. */
  83. static const AACPCEInfo aac_pce_configs[] = {
  84. {
  85. .layout = AV_CHANNEL_LAYOUT_MONO,
  86. .num_ele = { 1, 0, 0, 0 },
  87. .pairing = { { 0 }, },
  88. .index = { { 0 }, },
  89. .config_map = { 1, TYPE_SCE, },
  90. .reorder_map = { 0 },
  91. },
  92. {
  93. .layout = AV_CHANNEL_LAYOUT_STEREO,
  94. .num_ele = { 1, 0, 0, 0 },
  95. .pairing = { { 1 }, },
  96. .index = { { 0 }, },
  97. .config_map = { 1, TYPE_CPE, },
  98. .reorder_map = { 0, 1 },
  99. },
  100. {
  101. .layout = AV_CHANNEL_LAYOUT_2POINT1,
  102. .num_ele = { 1, 0, 0, 1 },
  103. .pairing = { { 1 }, },
  104. .index = { { 0 },{ 0 },{ 0 },{ 0 } },
  105. .config_map = { 2, TYPE_CPE, TYPE_LFE },
  106. .reorder_map = { 0, 1, 2 },
  107. },
  108. {
  109. .layout = AV_CHANNEL_LAYOUT_2_1,
  110. .num_ele = { 1, 0, 1, 0 },
  111. .pairing = { { 1 },{ 0 },{ 0 } },
  112. .index = { { 0 },{ 0 },{ 0 }, },
  113. .config_map = { 2, TYPE_CPE, TYPE_SCE },
  114. .reorder_map = { 0, 1, 2 },
  115. },
  116. {
  117. .layout = AV_CHANNEL_LAYOUT_SURROUND,
  118. .num_ele = { 2, 0, 0, 0 },
  119. .pairing = { { 1, 0 }, },
  120. .index = { { 0, 0 }, },
  121. .config_map = { 2, TYPE_CPE, TYPE_SCE, },
  122. .reorder_map = { 0, 1, 2 },
  123. },
  124. {
  125. .layout = AV_CHANNEL_LAYOUT_3POINT1,
  126. .num_ele = { 2, 0, 0, 1 },
  127. .pairing = { { 1, 0 }, },
  128. .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, },
  129. .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_LFE },
  130. .reorder_map = { 0, 1, 2, 3 },
  131. },
  132. {
  133. .layout = AV_CHANNEL_LAYOUT_4POINT0,
  134. .num_ele = { 2, 0, 1, 0 },
  135. .pairing = { { 1, 0 }, { 0 }, { 0 }, },
  136. .index = { { 0, 0 }, { 0 }, { 1 } },
  137. .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_SCE },
  138. .reorder_map = { 0, 1, 2, 3 },
  139. },
  140. {
  141. .layout = AV_CHANNEL_LAYOUT_4POINT1,
  142. .num_ele = { 2, 1, 1, 0 },
  143. .pairing = { { 1, 0 }, { 0 }, { 0 }, },
  144. .index = { { 0, 0 }, { 1 }, { 2 }, { 0 } },
  145. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_SCE },
  146. .reorder_map = { 0, 1, 2, 3, 4 },
  147. },
  148. {
  149. .layout = AV_CHANNEL_LAYOUT_2_2,
  150. .num_ele = { 1, 1, 0, 0 },
  151. .pairing = { { 1 }, { 1 }, },
  152. .index = { { 0 }, { 1 }, },
  153. .config_map = { 2, TYPE_CPE, TYPE_CPE },
  154. .reorder_map = { 0, 1, 2, 3 },
  155. },
  156. {
  157. .layout = AV_CHANNEL_LAYOUT_QUAD,
  158. .num_ele = { 1, 0, 1, 0 },
  159. .pairing = { { 1 }, { 0 }, { 1 }, },
  160. .index = { { 0 }, { 0 }, { 1 } },
  161. .config_map = { 2, TYPE_CPE, TYPE_CPE },
  162. .reorder_map = { 0, 1, 2, 3 },
  163. },
  164. {
  165. .layout = AV_CHANNEL_LAYOUT_5POINT0,
  166. .num_ele = { 2, 1, 0, 0 },
  167. .pairing = { { 1, 0 }, { 1 }, },
  168. .index = { { 0, 0 }, { 1 } },
  169. .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE },
  170. .reorder_map = { 0, 1, 2, 3, 4 },
  171. },
  172. {
  173. .layout = AV_CHANNEL_LAYOUT_5POINT1,
  174. .num_ele = { 2, 1, 1, 0 },
  175. .pairing = { { 1, 0 }, { 0 }, { 1 }, },
  176. .index = { { 0, 0 }, { 1 }, { 1 } },
  177. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE },
  178. .reorder_map = { 0, 1, 2, 3, 4, 5 },
  179. },
  180. {
  181. .layout = AV_CHANNEL_LAYOUT_5POINT0_BACK,
  182. .num_ele = { 2, 0, 1, 0 },
  183. .pairing = { { 1, 0 }, { 0 }, { 1 } },
  184. .index = { { 0, 0 }, { 0 }, { 1 } },
  185. .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE },
  186. .reorder_map = { 0, 1, 2, 3, 4 },
  187. },
  188. {
  189. .layout = AV_CHANNEL_LAYOUT_5POINT1_BACK,
  190. .num_ele = { 2, 1, 1, 0 },
  191. .pairing = { { 1, 0 }, { 0 }, { 1 }, },
  192. .index = { { 0, 0 }, { 1 }, { 1 } },
  193. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE },
  194. .reorder_map = { 0, 1, 2, 3, 4, 5 },
  195. },
  196. {
  197. .layout = AV_CHANNEL_LAYOUT_6POINT0,
  198. .num_ele = { 2, 1, 1, 0 },
  199. .pairing = { { 1, 0 }, { 1 }, { 0 }, },
  200. .index = { { 0, 0 }, { 1 }, { 1 } },
  201. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  202. .reorder_map = { 0, 1, 2, 3, 4, 5 },
  203. },
  204. {
  205. .layout = AV_CHANNEL_LAYOUT_6POINT0_FRONT,
  206. .num_ele = { 2, 1, 0, 0 },
  207. .pairing = { { 1, 1 }, { 1 } },
  208. .index = { { 1, 0 }, { 2 }, },
  209. .config_map = { 3, TYPE_CPE, TYPE_CPE, TYPE_CPE, },
  210. .reorder_map = { 0, 1, 2, 3, 4, 5 },
  211. },
  212. {
  213. .layout = AV_CHANNEL_LAYOUT_HEXAGONAL,
  214. .num_ele = { 2, 0, 2, 0 },
  215. .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
  216. .index = { { 0, 0 },{ 0 },{ 1, 1 } },
  217. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, },
  218. .reorder_map = { 0, 1, 2, 3, 4, 5 },
  219. },
  220. {
  221. .layout = AV_CHANNEL_LAYOUT_6POINT1,
  222. .num_ele = { 2, 1, 2, 0 },
  223. .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
  224. .index = { { 0, 0 },{ 1 },{ 1, 2 } },
  225. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  226. .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
  227. },
  228. {
  229. .layout = AV_CHANNEL_LAYOUT_6POINT1_BACK,
  230. .num_ele = { 2, 1, 2, 0 },
  231. .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
  232. .index = { { 0, 0 }, { 1 }, { 1, 2 } },
  233. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  234. .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
  235. },
  236. {
  237. .layout = AV_CHANNEL_LAYOUT_6POINT1_FRONT,
  238. .num_ele = { 2, 1, 2, 0 },
  239. .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
  240. .index = { { 0, 0 }, { 1 }, { 1, 2 } },
  241. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  242. .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
  243. },
  244. {
  245. .layout = AV_CHANNEL_LAYOUT_7POINT0,
  246. .num_ele = { 2, 1, 1, 0 },
  247. .pairing = { { 1, 0 }, { 1 }, { 1 }, },
  248. .index = { { 0, 0 }, { 1 }, { 2 }, },
  249. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
  250. .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
  251. },
  252. {
  253. .layout = AV_CHANNEL_LAYOUT_7POINT0_FRONT,
  254. .num_ele = { 2, 1, 1, 0 },
  255. .pairing = { { 1, 0 }, { 1 }, { 1 }, },
  256. .index = { { 0, 0 }, { 1 }, { 2 }, },
  257. .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
  258. .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
  259. },
  260. {
  261. .layout = AV_CHANNEL_LAYOUT_7POINT1,
  262. .num_ele = { 2, 1, 2, 0 },
  263. .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
  264. .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
  265. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
  266. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
  267. },
  268. {
  269. .layout = AV_CHANNEL_LAYOUT_7POINT1_WIDE,
  270. .num_ele = { 2, 1, 2, 0 },
  271. .pairing = { { 1, 0 }, { 0 },{ 1, 1 }, },
  272. .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
  273. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
  274. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
  275. },
  276. {
  277. .layout = AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK,
  278. .num_ele = { 2, 1, 2, 0 },
  279. .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
  280. .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
  281. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
  282. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
  283. },
  284. {
  285. .layout = AV_CHANNEL_LAYOUT_OCTAGONAL,
  286. .num_ele = { 2, 1, 2, 0 },
  287. .pairing = { { 1, 0 }, { 1 }, { 1, 0 }, },
  288. .index = { { 0, 0 }, { 1 }, { 2, 1 } },
  289. .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE },
  290. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
  291. },
  292. { /* Meant for order 2/mixed ambisonics */
  293. .layout = { .order = AV_CHANNEL_ORDER_NATIVE, .nb_channels = 9,
  294. .u.mask = AV_CH_LAYOUT_OCTAGONAL | AV_CH_TOP_CENTER },
  295. .num_ele = { 2, 2, 2, 0 },
  296. .pairing = { { 1, 0 }, { 1, 0 }, { 1, 0 }, },
  297. .index = { { 0, 0 }, { 1, 1 }, { 2, 2 } },
  298. .config_map = { 6, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  299. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8 },
  300. },
  301. { /* Meant for order 2/mixed ambisonics */
  302. .layout = { .order = AV_CHANNEL_ORDER_NATIVE, .nb_channels = 10,
  303. .u.mask = AV_CH_LAYOUT_6POINT0_FRONT | AV_CH_BACK_CENTER |
  304. AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_TOP_CENTER },
  305. .num_ele = { 2, 2, 2, 0 },
  306. .pairing = { { 1, 1 }, { 1, 0 }, { 1, 0 }, },
  307. .index = { { 0, 1 }, { 2, 0 }, { 3, 1 } },
  308. .config_map = { 6, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  309. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
  310. },
  311. {
  312. .layout = AV_CHANNEL_LAYOUT_HEXADECAGONAL,
  313. .num_ele = { 4, 2, 4, 0 },
  314. .pairing = { { 1, 0, 1, 0 }, { 1, 1 }, { 1, 0, 1, 0 }, },
  315. .index = { { 0, 0, 1, 1 }, { 2, 3 }, { 4, 2, 5, 3 } },
  316. .config_map = { 10, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
  317. .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
  318. },
  319. };
  320. static void put_pce(PutBitContext *pb, AVCodecContext *avctx)
  321. {
  322. int i, j;
  323. AACEncContext *s = avctx->priv_data;
  324. AACPCEInfo *pce = &s->pce;
  325. const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT;
  326. const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT;
  327. put_bits(pb, 4, 0);
  328. put_bits(pb, 2, avctx->profile);
  329. put_bits(pb, 4, s->samplerate_index);
  330. put_bits(pb, 4, pce->num_ele[0]); /* Front */
  331. put_bits(pb, 4, pce->num_ele[1]); /* Side */
  332. put_bits(pb, 4, pce->num_ele[2]); /* Back */
  333. put_bits(pb, 2, pce->num_ele[3]); /* LFE */
  334. put_bits(pb, 3, 0); /* Assoc data */
  335. put_bits(pb, 4, 0); /* CCs */
  336. put_bits(pb, 1, 0); /* Stereo mixdown */
  337. put_bits(pb, 1, 0); /* Mono mixdown */
  338. put_bits(pb, 1, 0); /* Something else */
  339. for (i = 0; i < 4; i++) {
  340. for (j = 0; j < pce->num_ele[i]; j++) {
  341. if (i < 3)
  342. put_bits(pb, 1, pce->pairing[i][j]);
  343. put_bits(pb, 4, pce->index[i][j]);
  344. }
  345. }
  346. align_put_bits(pb);
  347. put_bits(pb, 8, strlen(aux_data));
  348. ff_put_string(pb, aux_data, 0);
  349. }
  350. /**
  351. * Make AAC audio config object.
  352. * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
  353. */
  354. static int put_audio_specific_config(AVCodecContext *avctx)
  355. {
  356. PutBitContext pb;
  357. AACEncContext *s = avctx->priv_data;
  358. int channels = (!s->needs_pce)*(s->channels - (s->channels == 8 ? 1 : 0));
  359. const int max_size = 32;
  360. avctx->extradata = av_mallocz(max_size);
  361. if (!avctx->extradata)
  362. return AVERROR(ENOMEM);
  363. init_put_bits(&pb, avctx->extradata, max_size);
  364. put_bits(&pb, 5, s->profile+1); //profile
  365. put_bits(&pb, 4, s->samplerate_index); //sample rate index
  366. put_bits(&pb, 4, channels);
  367. //GASpecificConfig
  368. put_bits(&pb, 1, 0); //frame length - 1024 samples
  369. put_bits(&pb, 1, 0); //does not depend on core coder
  370. put_bits(&pb, 1, 0); //is not extension
  371. if (s->needs_pce)
  372. put_pce(&pb, avctx);
  373. //Explicitly Mark SBR absent
  374. put_bits(&pb, 11, 0x2b7); //sync extension
  375. put_bits(&pb, 5, AOT_SBR);
  376. put_bits(&pb, 1, 0);
  377. flush_put_bits(&pb);
  378. avctx->extradata_size = put_bytes_output(&pb);
  379. return 0;
  380. }
  381. void ff_quantize_band_cost_cache_init(struct AACEncContext *s)
  382. {
  383. ++s->quantize_band_cost_cache_generation;
  384. if (s->quantize_band_cost_cache_generation == 0) {
  385. memset(s->quantize_band_cost_cache, 0, sizeof(s->quantize_band_cost_cache));
  386. s->quantize_band_cost_cache_generation = 1;
  387. }
  388. }
  389. #define WINDOW_FUNC(type) \
  390. static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
  391. SingleChannelElement *sce, \
  392. const float *audio)
  393. WINDOW_FUNC(only_long)
  394. {
  395. const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  396. const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  397. float *out = sce->ret_buf;
  398. fdsp->vector_fmul (out, audio, lwindow, 1024);
  399. fdsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024);
  400. }
  401. WINDOW_FUNC(long_start)
  402. {
  403. const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  404. const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  405. float *out = sce->ret_buf;
  406. fdsp->vector_fmul(out, audio, lwindow, 1024);
  407. memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448);
  408. fdsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128);
  409. memset(out + 1024 + 576, 0, sizeof(out[0]) * 448);
  410. }
  411. WINDOW_FUNC(long_stop)
  412. {
  413. const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  414. const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  415. float *out = sce->ret_buf;
  416. memset(out, 0, sizeof(out[0]) * 448);
  417. fdsp->vector_fmul(out + 448, audio + 448, swindow, 128);
  418. memcpy(out + 576, audio + 576, sizeof(out[0]) * 448);
  419. fdsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024);
  420. }
  421. WINDOW_FUNC(eight_short)
  422. {
  423. const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  424. const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  425. const float *in = audio + 448;
  426. float *out = sce->ret_buf;
  427. int w;
  428. for (w = 0; w < 8; w++) {
  429. fdsp->vector_fmul (out, in, w ? pwindow : swindow, 128);
  430. out += 128;
  431. in += 128;
  432. fdsp->vector_fmul_reverse(out, in, swindow, 128);
  433. out += 128;
  434. }
  435. }
  436. static void (*const apply_window[4])(AVFloatDSPContext *fdsp,
  437. SingleChannelElement *sce,
  438. const float *audio) = {
  439. [ONLY_LONG_SEQUENCE] = apply_only_long_window,
  440. [LONG_START_SEQUENCE] = apply_long_start_window,
  441. [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window,
  442. [LONG_STOP_SEQUENCE] = apply_long_stop_window
  443. };
  444. static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce,
  445. float *audio)
  446. {
  447. int i;
  448. float *output = sce->ret_buf;
  449. apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, audio);
  450. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE)
  451. s->mdct1024_fn(s->mdct1024, sce->coeffs, output, sizeof(float));
  452. else
  453. for (i = 0; i < 1024; i += 128)
  454. s->mdct128_fn(s->mdct128, &sce->coeffs[i], output + i*2, sizeof(float));
  455. memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);
  456. memcpy(sce->pcoeffs, sce->coeffs, sizeof(sce->pcoeffs));
  457. }
  458. /**
  459. * Encode ics_info element.
  460. * @see Table 4.6 (syntax of ics_info)
  461. */
  462. static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
  463. {
  464. int w;
  465. put_bits(&s->pb, 1, 0); // ics_reserved bit
  466. put_bits(&s->pb, 2, info->window_sequence[0]);
  467. put_bits(&s->pb, 1, info->use_kb_window[0]);
  468. if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  469. put_bits(&s->pb, 6, info->max_sfb);
  470. put_bits(&s->pb, 1, !!info->predictor_present);
  471. } else {
  472. put_bits(&s->pb, 4, info->max_sfb);
  473. for (w = 1; w < 8; w++)
  474. put_bits(&s->pb, 1, !info->group_len[w]);
  475. }
  476. }
  477. /**
  478. * Encode MS data.
  479. * @see 4.6.8.1 "Joint Coding - M/S Stereo"
  480. */
  481. static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
  482. {
  483. int i, w;
  484. put_bits(pb, 2, cpe->ms_mode);
  485. if (cpe->ms_mode == 1)
  486. for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
  487. for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
  488. put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
  489. }
  490. /**
  491. * Produce integer coefficients from scalefactors provided by the model.
  492. */
  493. static void adjust_frame_information(ChannelElement *cpe, int chans)
  494. {
  495. int i, w, w2, g, ch;
  496. int maxsfb, cmaxsfb;
  497. for (ch = 0; ch < chans; ch++) {
  498. IndividualChannelStream *ics = &cpe->ch[ch].ics;
  499. maxsfb = 0;
  500. cpe->ch[ch].pulse.num_pulse = 0;
  501. for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
  502. for (w2 = 0; w2 < ics->group_len[w]; w2++) {
  503. for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w*16+cmaxsfb-1]; cmaxsfb--)
  504. ;
  505. maxsfb = FFMAX(maxsfb, cmaxsfb);
  506. }
  507. }
  508. ics->max_sfb = maxsfb;
  509. //adjust zero bands for window groups
  510. for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
  511. for (g = 0; g < ics->max_sfb; g++) {
  512. i = 1;
  513. for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
  514. if (!cpe->ch[ch].zeroes[w2*16 + g]) {
  515. i = 0;
  516. break;
  517. }
  518. }
  519. cpe->ch[ch].zeroes[w*16 + g] = i;
  520. }
  521. }
  522. }
  523. if (chans > 1 && cpe->common_window) {
  524. IndividualChannelStream *ics0 = &cpe->ch[0].ics;
  525. IndividualChannelStream *ics1 = &cpe->ch[1].ics;
  526. int msc = 0;
  527. ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
  528. ics1->max_sfb = ics0->max_sfb;
  529. for (w = 0; w < ics0->num_windows*16; w += 16)
  530. for (i = 0; i < ics0->max_sfb; i++)
  531. if (cpe->ms_mask[w+i])
  532. msc++;
  533. if (msc == 0 || ics0->max_sfb == 0)
  534. cpe->ms_mode = 0;
  535. else
  536. cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2;
  537. }
  538. }
  539. static void apply_intensity_stereo(ChannelElement *cpe)
  540. {
  541. int w, w2, g, i;
  542. IndividualChannelStream *ics = &cpe->ch[0].ics;
  543. if (!cpe->common_window)
  544. return;
  545. for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
  546. for (w2 = 0; w2 < ics->group_len[w]; w2++) {
  547. int start = (w+w2) * 128;
  548. for (g = 0; g < ics->num_swb; g++) {
  549. int p = -1 + 2 * (cpe->ch[1].band_type[w*16+g] - 14);
  550. float scale = cpe->ch[0].is_ener[w*16+g];
  551. if (!cpe->is_mask[w*16 + g]) {
  552. start += ics->swb_sizes[g];
  553. continue;
  554. }
  555. if (cpe->ms_mask[w*16 + g])
  556. p *= -1;
  557. for (i = 0; i < ics->swb_sizes[g]; i++) {
  558. float sum = (cpe->ch[0].coeffs[start+i] + p*cpe->ch[1].coeffs[start+i])*scale;
  559. cpe->ch[0].coeffs[start+i] = sum;
  560. cpe->ch[1].coeffs[start+i] = 0.0f;
  561. }
  562. start += ics->swb_sizes[g];
  563. }
  564. }
  565. }
  566. }
  567. static void apply_mid_side_stereo(ChannelElement *cpe)
  568. {
  569. int w, w2, g, i;
  570. IndividualChannelStream *ics = &cpe->ch[0].ics;
  571. if (!cpe->common_window)
  572. return;
  573. for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
  574. for (w2 = 0; w2 < ics->group_len[w]; w2++) {
  575. int start = (w+w2) * 128;
  576. for (g = 0; g < ics->num_swb; g++) {
  577. /* ms_mask can be used for other purposes in PNS and I/S,
  578. * so must not apply M/S if any band uses either, even if
  579. * ms_mask is set.
  580. */
  581. if (!cpe->ms_mask[w*16 + g] || cpe->is_mask[w*16 + g]
  582. || cpe->ch[0].band_type[w*16 + g] >= NOISE_BT
  583. || cpe->ch[1].band_type[w*16 + g] >= NOISE_BT) {
  584. start += ics->swb_sizes[g];
  585. continue;
  586. }
  587. for (i = 0; i < ics->swb_sizes[g]; i++) {
  588. float L = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) * 0.5f;
  589. float R = L - cpe->ch[1].coeffs[start+i];
  590. cpe->ch[0].coeffs[start+i] = L;
  591. cpe->ch[1].coeffs[start+i] = R;
  592. }
  593. start += ics->swb_sizes[g];
  594. }
  595. }
  596. }
  597. }
  598. /**
  599. * Encode scalefactor band coding type.
  600. */
  601. static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
  602. {
  603. int w;
  604. if (s->coder->set_special_band_scalefactors)
  605. s->coder->set_special_band_scalefactors(s, sce);
  606. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  607. s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
  608. }
  609. /**
  610. * Encode scalefactors.
  611. */
  612. static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
  613. SingleChannelElement *sce)
  614. {
  615. int diff, off_sf = sce->sf_idx[0], off_pns = sce->sf_idx[0] - NOISE_OFFSET;
  616. int off_is = 0, noise_flag = 1;
  617. int i, w;
  618. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  619. for (i = 0; i < sce->ics.max_sfb; i++) {
  620. if (!sce->zeroes[w*16 + i]) {
  621. if (sce->band_type[w*16 + i] == NOISE_BT) {
  622. diff = sce->sf_idx[w*16 + i] - off_pns;
  623. off_pns = sce->sf_idx[w*16 + i];
  624. if (noise_flag-- > 0) {
  625. put_bits(&s->pb, NOISE_PRE_BITS, diff + NOISE_PRE);
  626. continue;
  627. }
  628. } else if (sce->band_type[w*16 + i] == INTENSITY_BT ||
  629. sce->band_type[w*16 + i] == INTENSITY_BT2) {
  630. diff = sce->sf_idx[w*16 + i] - off_is;
  631. off_is = sce->sf_idx[w*16 + i];
  632. } else {
  633. diff = sce->sf_idx[w*16 + i] - off_sf;
  634. off_sf = sce->sf_idx[w*16 + i];
  635. }
  636. diff += SCALE_DIFF_ZERO;
  637. av_assert0(diff >= 0 && diff <= 120);
  638. put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
  639. }
  640. }
  641. }
  642. }
  643. /**
  644. * Encode pulse data.
  645. */
  646. static void encode_pulses(AACEncContext *s, Pulse *pulse)
  647. {
  648. int i;
  649. put_bits(&s->pb, 1, !!pulse->num_pulse);
  650. if (!pulse->num_pulse)
  651. return;
  652. put_bits(&s->pb, 2, pulse->num_pulse - 1);
  653. put_bits(&s->pb, 6, pulse->start);
  654. for (i = 0; i < pulse->num_pulse; i++) {
  655. put_bits(&s->pb, 5, pulse->pos[i]);
  656. put_bits(&s->pb, 4, pulse->amp[i]);
  657. }
  658. }
  659. /**
  660. * Encode spectral coefficients processed by psychoacoustic model.
  661. */
  662. static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
  663. {
  664. int start, i, w, w2;
  665. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  666. start = 0;
  667. for (i = 0; i < sce->ics.max_sfb; i++) {
  668. if (sce->zeroes[w*16 + i]) {
  669. start += sce->ics.swb_sizes[i];
  670. continue;
  671. }
  672. for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) {
  673. s->coder->quantize_and_encode_band(s, &s->pb,
  674. &sce->coeffs[start + w2*128],
  675. NULL, sce->ics.swb_sizes[i],
  676. sce->sf_idx[w*16 + i],
  677. sce->band_type[w*16 + i],
  678. s->lambda,
  679. sce->ics.window_clipping[w]);
  680. }
  681. start += sce->ics.swb_sizes[i];
  682. }
  683. }
  684. }
  685. /**
  686. * Downscale spectral coefficients for near-clipping windows to avoid artifacts
  687. */
  688. static void avoid_clipping(AACEncContext *s, SingleChannelElement *sce)
  689. {
  690. int start, i, j, w;
  691. if (sce->ics.clip_avoidance_factor < 1.0f) {
  692. for (w = 0; w < sce->ics.num_windows; w++) {
  693. start = 0;
  694. for (i = 0; i < sce->ics.max_sfb; i++) {
  695. float *swb_coeffs = &sce->coeffs[start + w*128];
  696. for (j = 0; j < sce->ics.swb_sizes[i]; j++)
  697. swb_coeffs[j] *= sce->ics.clip_avoidance_factor;
  698. start += sce->ics.swb_sizes[i];
  699. }
  700. }
  701. }
  702. }
  703. /**
  704. * Encode one channel of audio data.
  705. */
  706. static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
  707. SingleChannelElement *sce,
  708. int common_window)
  709. {
  710. put_bits(&s->pb, 8, sce->sf_idx[0]);
  711. if (!common_window) {
  712. put_ics_info(s, &sce->ics);
  713. if (s->coder->encode_main_pred)
  714. s->coder->encode_main_pred(s, sce);
  715. if (s->coder->encode_ltp_info)
  716. s->coder->encode_ltp_info(s, sce, 0);
  717. }
  718. encode_band_info(s, sce);
  719. encode_scale_factors(avctx, s, sce);
  720. encode_pulses(s, &sce->pulse);
  721. put_bits(&s->pb, 1, !!sce->tns.present);
  722. if (s->coder->encode_tns_info)
  723. s->coder->encode_tns_info(s, sce);
  724. put_bits(&s->pb, 1, 0); //ssr
  725. encode_spectral_coeffs(s, sce);
  726. return 0;
  727. }
  728. /**
  729. * Write some auxiliary information about the created AAC file.
  730. */
  731. static void put_bitstream_info(AACEncContext *s, const char *name)
  732. {
  733. int i, namelen, padbits;
  734. namelen = strlen(name) + 2;
  735. put_bits(&s->pb, 3, TYPE_FIL);
  736. put_bits(&s->pb, 4, FFMIN(namelen, 15));
  737. if (namelen >= 15)
  738. put_bits(&s->pb, 8, namelen - 14);
  739. put_bits(&s->pb, 4, 0); //extension type - filler
  740. padbits = -put_bits_count(&s->pb) & 7;
  741. align_put_bits(&s->pb);
  742. for (i = 0; i < namelen - 2; i++)
  743. put_bits(&s->pb, 8, name[i]);
  744. put_bits(&s->pb, 12 - padbits, 0);
  745. }
  746. /*
  747. * Copy input samples.
  748. * Channels are reordered from libavcodec's default order to AAC order.
  749. */
  750. static void copy_input_samples(AACEncContext *s, const AVFrame *frame)
  751. {
  752. int ch;
  753. int end = 2048 + (frame ? frame->nb_samples : 0);
  754. const uint8_t *channel_map = s->reorder_map;
  755. /* copy and remap input samples */
  756. for (ch = 0; ch < s->channels; ch++) {
  757. /* copy last 1024 samples of previous frame to the start of the current frame */
  758. memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0]));
  759. /* copy new samples and zero any remaining samples */
  760. if (frame) {
  761. memcpy(&s->planar_samples[ch][2048],
  762. frame->extended_data[channel_map[ch]],
  763. frame->nb_samples * sizeof(s->planar_samples[0][0]));
  764. }
  765. memset(&s->planar_samples[ch][end], 0,
  766. (3072 - end) * sizeof(s->planar_samples[0][0]));
  767. }
  768. }
  769. static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  770. const AVFrame *frame, int *got_packet_ptr)
  771. {
  772. AACEncContext *s = avctx->priv_data;
  773. float **samples = s->planar_samples, *samples2, *la, *overlap;
  774. ChannelElement *cpe;
  775. SingleChannelElement *sce;
  776. IndividualChannelStream *ics;
  777. int i, its, ch, w, chans, tag, start_ch, ret, frame_bits;
  778. int target_bits, rate_bits, too_many_bits, too_few_bits;
  779. int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0;
  780. int chan_el_counter[4];
  781. FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
  782. /* add current frame to queue */
  783. if (frame) {
  784. if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
  785. return ret;
  786. } else {
  787. if (!s->afq.remaining_samples || (!s->afq.frame_alloc && !s->afq.frame_count))
  788. return 0;
  789. }
  790. copy_input_samples(s, frame);
  791. if (s->psypp)
  792. ff_psy_preprocess(s->psypp, s->planar_samples, s->channels);
  793. if (!avctx->frame_num)
  794. return 0;
  795. start_ch = 0;
  796. for (i = 0; i < s->chan_map[0]; i++) {
  797. FFPsyWindowInfo* wi = windows + start_ch;
  798. tag = s->chan_map[i+1];
  799. chans = tag == TYPE_CPE ? 2 : 1;
  800. cpe = &s->cpe[i];
  801. for (ch = 0; ch < chans; ch++) {
  802. int k;
  803. float clip_avoidance_factor;
  804. sce = &cpe->ch[ch];
  805. ics = &sce->ics;
  806. s->cur_channel = start_ch + ch;
  807. overlap = &samples[s->cur_channel][0];
  808. samples2 = overlap + 1024;
  809. la = samples2 + (448+64);
  810. if (!frame)
  811. la = NULL;
  812. if (tag == TYPE_LFE) {
  813. wi[ch].window_type[0] = wi[ch].window_type[1] = ONLY_LONG_SEQUENCE;
  814. wi[ch].window_shape = 0;
  815. wi[ch].num_windows = 1;
  816. wi[ch].grouping[0] = 1;
  817. wi[ch].clipping[0] = 0;
  818. /* Only the lowest 12 coefficients are used in a LFE channel.
  819. * The expression below results in only the bottom 8 coefficients
  820. * being used for 11.025kHz to 16kHz sample rates.
  821. */
  822. ics->num_swb = s->samplerate_index >= 8 ? 1 : 3;
  823. } else {
  824. wi[ch] = s->psy.model->window(&s->psy, samples2, la, s->cur_channel,
  825. ics->window_sequence[0]);
  826. }
  827. ics->window_sequence[1] = ics->window_sequence[0];
  828. ics->window_sequence[0] = wi[ch].window_type[0];
  829. ics->use_kb_window[1] = ics->use_kb_window[0];
  830. ics->use_kb_window[0] = wi[ch].window_shape;
  831. ics->num_windows = wi[ch].num_windows;
  832. ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
  833. ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8];
  834. ics->max_sfb = FFMIN(ics->max_sfb, ics->num_swb);
  835. ics->swb_offset = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
  836. ff_swb_offset_128 [s->samplerate_index]:
  837. ff_swb_offset_1024[s->samplerate_index];
  838. ics->tns_max_bands = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
  839. ff_tns_max_bands_128 [s->samplerate_index]:
  840. ff_tns_max_bands_1024[s->samplerate_index];
  841. for (w = 0; w < ics->num_windows; w++)
  842. ics->group_len[w] = wi[ch].grouping[w];
  843. /* Calculate input sample maximums and evaluate clipping risk */
  844. clip_avoidance_factor = 0.0f;
  845. for (w = 0; w < ics->num_windows; w++) {
  846. const float *wbuf = overlap + w * 128;
  847. const int wlen = 2048 / ics->num_windows;
  848. float max = 0;
  849. int j;
  850. /* mdct input is 2 * output */
  851. for (j = 0; j < wlen; j++)
  852. max = FFMAX(max, fabsf(wbuf[j]));
  853. wi[ch].clipping[w] = max;
  854. }
  855. for (w = 0; w < ics->num_windows; w++) {
  856. if (wi[ch].clipping[w] > CLIP_AVOIDANCE_FACTOR) {
  857. ics->window_clipping[w] = 1;
  858. clip_avoidance_factor = FFMAX(clip_avoidance_factor, wi[ch].clipping[w]);
  859. } else {
  860. ics->window_clipping[w] = 0;
  861. }
  862. }
  863. if (clip_avoidance_factor > CLIP_AVOIDANCE_FACTOR) {
  864. ics->clip_avoidance_factor = CLIP_AVOIDANCE_FACTOR / clip_avoidance_factor;
  865. } else {
  866. ics->clip_avoidance_factor = 1.0f;
  867. }
  868. apply_window_and_mdct(s, sce, overlap);
  869. if (s->options.ltp && s->coder->update_ltp) {
  870. s->coder->update_ltp(s, sce);
  871. apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, &sce->ltp_state[0]);
  872. s->mdct1024_fn(s->mdct1024, sce->lcoeffs, sce->ret_buf, sizeof(float));
  873. }
  874. for (k = 0; k < 1024; k++) {
  875. if (!(fabs(cpe->ch[ch].coeffs[k]) < 1E16)) { // Ensure headroom for energy calculation
  876. av_log(avctx, AV_LOG_ERROR, "Input contains (near) NaN/+-Inf\n");
  877. return AVERROR(EINVAL);
  878. }
  879. }
  880. avoid_clipping(s, sce);
  881. }
  882. start_ch += chans;
  883. }
  884. if ((ret = ff_alloc_packet(avctx, avpkt, 8192 * s->channels)) < 0)
  885. return ret;
  886. frame_bits = its = 0;
  887. do {
  888. init_put_bits(&s->pb, avpkt->data, avpkt->size);
  889. if ((avctx->frame_num & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT))
  890. put_bitstream_info(s, LIBAVCODEC_IDENT);
  891. start_ch = 0;
  892. target_bits = 0;
  893. memset(chan_el_counter, 0, sizeof(chan_el_counter));
  894. for (i = 0; i < s->chan_map[0]; i++) {
  895. FFPsyWindowInfo* wi = windows + start_ch;
  896. const float *coeffs[2];
  897. tag = s->chan_map[i+1];
  898. chans = tag == TYPE_CPE ? 2 : 1;
  899. cpe = &s->cpe[i];
  900. cpe->common_window = 0;
  901. memset(cpe->is_mask, 0, sizeof(cpe->is_mask));
  902. memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
  903. put_bits(&s->pb, 3, tag);
  904. put_bits(&s->pb, 4, chan_el_counter[tag]++);
  905. for (ch = 0; ch < chans; ch++) {
  906. sce = &cpe->ch[ch];
  907. coeffs[ch] = sce->coeffs;
  908. sce->ics.predictor_present = 0;
  909. sce->ics.ltp.present = 0;
  910. memset(sce->ics.ltp.used, 0, sizeof(sce->ics.ltp.used));
  911. memset(sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used));
  912. memset(&sce->tns, 0, sizeof(TemporalNoiseShaping));
  913. for (w = 0; w < 128; w++)
  914. if (sce->band_type[w] > RESERVED_BT)
  915. sce->band_type[w] = 0;
  916. }
  917. s->psy.bitres.alloc = -1;
  918. s->psy.bitres.bits = s->last_frame_pb_count / s->channels;
  919. s->psy.model->analyze(&s->psy, start_ch, coeffs, wi);
  920. if (s->psy.bitres.alloc > 0) {
  921. /* Lambda unused here on purpose, we need to take psy's unscaled allocation */
  922. target_bits += s->psy.bitres.alloc
  923. * (s->lambda / (avctx->global_quality ? avctx->global_quality : 120));
  924. s->psy.bitres.alloc /= chans;
  925. }
  926. s->cur_type = tag;
  927. for (ch = 0; ch < chans; ch++) {
  928. s->cur_channel = start_ch + ch;
  929. if (s->options.pns && s->coder->mark_pns)
  930. s->coder->mark_pns(s, avctx, &cpe->ch[ch]);
  931. s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
  932. }
  933. if (chans > 1
  934. && wi[0].window_type[0] == wi[1].window_type[0]
  935. && wi[0].window_shape == wi[1].window_shape) {
  936. cpe->common_window = 1;
  937. for (w = 0; w < wi[0].num_windows; w++) {
  938. if (wi[0].grouping[w] != wi[1].grouping[w]) {
  939. cpe->common_window = 0;
  940. break;
  941. }
  942. }
  943. }
  944. for (ch = 0; ch < chans; ch++) { /* TNS and PNS */
  945. sce = &cpe->ch[ch];
  946. s->cur_channel = start_ch + ch;
  947. if (s->options.tns && s->coder->search_for_tns)
  948. s->coder->search_for_tns(s, sce);
  949. if (s->options.tns && s->coder->apply_tns_filt)
  950. s->coder->apply_tns_filt(s, sce);
  951. if (sce->tns.present)
  952. tns_mode = 1;
  953. if (s->options.pns && s->coder->search_for_pns)
  954. s->coder->search_for_pns(s, avctx, sce);
  955. }
  956. s->cur_channel = start_ch;
  957. if (s->options.intensity_stereo) { /* Intensity Stereo */
  958. if (s->coder->search_for_is)
  959. s->coder->search_for_is(s, avctx, cpe);
  960. if (cpe->is_mode) is_mode = 1;
  961. apply_intensity_stereo(cpe);
  962. }
  963. if (s->options.pred) { /* Prediction */
  964. for (ch = 0; ch < chans; ch++) {
  965. sce = &cpe->ch[ch];
  966. s->cur_channel = start_ch + ch;
  967. if (s->options.pred && s->coder->search_for_pred)
  968. s->coder->search_for_pred(s, sce);
  969. if (cpe->ch[ch].ics.predictor_present) pred_mode = 1;
  970. }
  971. if (s->coder->adjust_common_pred)
  972. s->coder->adjust_common_pred(s, cpe);
  973. for (ch = 0; ch < chans; ch++) {
  974. sce = &cpe->ch[ch];
  975. s->cur_channel = start_ch + ch;
  976. if (s->options.pred && s->coder->apply_main_pred)
  977. s->coder->apply_main_pred(s, sce);
  978. }
  979. s->cur_channel = start_ch;
  980. }
  981. if (s->options.mid_side) { /* Mid/Side stereo */
  982. if (s->options.mid_side == -1 && s->coder->search_for_ms)
  983. s->coder->search_for_ms(s, cpe);
  984. else if (cpe->common_window)
  985. memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask));
  986. apply_mid_side_stereo(cpe);
  987. }
  988. adjust_frame_information(cpe, chans);
  989. if (s->options.ltp) { /* LTP */
  990. for (ch = 0; ch < chans; ch++) {
  991. sce = &cpe->ch[ch];
  992. s->cur_channel = start_ch + ch;
  993. if (s->coder->search_for_ltp)
  994. s->coder->search_for_ltp(s, sce, cpe->common_window);
  995. if (sce->ics.ltp.present) pred_mode = 1;
  996. }
  997. s->cur_channel = start_ch;
  998. if (s->coder->adjust_common_ltp)
  999. s->coder->adjust_common_ltp(s, cpe);
  1000. }
  1001. if (chans == 2) {
  1002. put_bits(&s->pb, 1, cpe->common_window);
  1003. if (cpe->common_window) {
  1004. put_ics_info(s, &cpe->ch[0].ics);
  1005. if (s->coder->encode_main_pred)
  1006. s->coder->encode_main_pred(s, &cpe->ch[0]);
  1007. if (s->coder->encode_ltp_info)
  1008. s->coder->encode_ltp_info(s, &cpe->ch[0], 1);
  1009. encode_ms_info(&s->pb, cpe);
  1010. if (cpe->ms_mode) ms_mode = 1;
  1011. }
  1012. }
  1013. for (ch = 0; ch < chans; ch++) {
  1014. s->cur_channel = start_ch + ch;
  1015. encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window);
  1016. }
  1017. start_ch += chans;
  1018. }
  1019. if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
  1020. /* When using a constant Q-scale, don't mess with lambda */
  1021. break;
  1022. }
  1023. /* rate control stuff
  1024. * allow between the nominal bitrate, and what psy's bit reservoir says to target
  1025. * but drift towards the nominal bitrate always
  1026. */
  1027. frame_bits = put_bits_count(&s->pb);
  1028. rate_bits = avctx->bit_rate * 1024 / avctx->sample_rate;
  1029. rate_bits = FFMIN(rate_bits, 6144 * s->channels - 3);
  1030. too_many_bits = FFMAX(target_bits, rate_bits);
  1031. too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3);
  1032. too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
  1033. /* When strict bit-rate control is demanded */
  1034. if (avctx->bit_rate_tolerance == 0) {
  1035. if (rate_bits < frame_bits) {
  1036. float ratio = ((float)rate_bits) / frame_bits;
  1037. s->lambda *= FFMIN(0.9f, ratio);
  1038. continue;
  1039. }
  1040. /* reset lambda when solution is found */
  1041. s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
  1042. break;
  1043. }
  1044. /* When using ABR, be strict (but only for increasing) */
  1045. too_few_bits = too_few_bits - too_few_bits/8;
  1046. too_many_bits = too_many_bits + too_many_bits/2;
  1047. if ( its == 0 /* for steady-state Q-scale tracking */
  1048. || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
  1049. || frame_bits >= 6144 * s->channels - 3 )
  1050. {
  1051. float ratio = ((float)rate_bits) / frame_bits;
  1052. if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
  1053. /*
  1054. * This path is for steady-state Q-scale tracking
  1055. * When frame bits fall within the stable range, we still need to adjust
  1056. * lambda to maintain it like so in a stable fashion (large jumps in lambda
  1057. * create artifacts and should be avoided), but slowly
  1058. */
  1059. ratio = sqrtf(sqrtf(ratio));
  1060. ratio = av_clipf(ratio, 0.9f, 1.1f);
  1061. } else {
  1062. /* Not so fast though */
  1063. ratio = sqrtf(ratio);
  1064. }
  1065. s->lambda = av_clipf(s->lambda * ratio, FLT_EPSILON, 65536.f);
  1066. /* Keep iterating if we must reduce and lambda is in the sky */
  1067. if (ratio > 0.9f && ratio < 1.1f) {
  1068. break;
  1069. } else {
  1070. if (is_mode || ms_mode || tns_mode || pred_mode) {
  1071. for (i = 0; i < s->chan_map[0]; i++) {
  1072. // Must restore coeffs
  1073. chans = tag == TYPE_CPE ? 2 : 1;
  1074. cpe = &s->cpe[i];
  1075. for (ch = 0; ch < chans; ch++)
  1076. memcpy(cpe->ch[ch].coeffs, cpe->ch[ch].pcoeffs, sizeof(cpe->ch[ch].coeffs));
  1077. }
  1078. }
  1079. its++;
  1080. }
  1081. } else {
  1082. break;
  1083. }
  1084. } while (1);
  1085. if (s->options.ltp && s->coder->ltp_insert_new_frame)
  1086. s->coder->ltp_insert_new_frame(s);
  1087. put_bits(&s->pb, 3, TYPE_END);
  1088. flush_put_bits(&s->pb);
  1089. s->last_frame_pb_count = put_bits_count(&s->pb);
  1090. avpkt->size = put_bytes_output(&s->pb);
  1091. s->lambda_sum += s->lambda;
  1092. s->lambda_count++;
  1093. ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
  1094. &avpkt->duration);
  1095. *got_packet_ptr = 1;
  1096. return 0;
  1097. }
  1098. static av_cold int aac_encode_end(AVCodecContext *avctx)
  1099. {
  1100. AACEncContext *s = avctx->priv_data;
  1101. av_log(avctx, AV_LOG_INFO, "Qavg: %.3f\n", s->lambda_count ? s->lambda_sum / s->lambda_count : NAN);
  1102. av_tx_uninit(&s->mdct1024);
  1103. av_tx_uninit(&s->mdct128);
  1104. ff_psy_end(&s->psy);
  1105. ff_lpc_end(&s->lpc);
  1106. if (s->psypp)
  1107. ff_psy_preprocess_end(s->psypp);
  1108. av_freep(&s->buffer.samples);
  1109. av_freep(&s->cpe);
  1110. av_freep(&s->fdsp);
  1111. ff_af_queue_close(&s->afq);
  1112. return 0;
  1113. }
  1114. static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
  1115. {
  1116. int ret = 0;
  1117. float scale = 32768.0f;
  1118. s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  1119. if (!s->fdsp)
  1120. return AVERROR(ENOMEM);
  1121. if ((ret = av_tx_init(&s->mdct1024, &s->mdct1024_fn, AV_TX_FLOAT_MDCT, 0,
  1122. 1024, &scale, 0)) < 0)
  1123. return ret;
  1124. if ((ret = av_tx_init(&s->mdct128, &s->mdct128_fn, AV_TX_FLOAT_MDCT, 0,
  1125. 128, &scale, 0)) < 0)
  1126. return ret;
  1127. return 0;
  1128. }
  1129. static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
  1130. {
  1131. int ch;
  1132. if (!FF_ALLOCZ_TYPED_ARRAY(s->buffer.samples, s->channels * 3 * 1024) ||
  1133. !FF_ALLOCZ_TYPED_ARRAY(s->cpe, s->chan_map[0]))
  1134. return AVERROR(ENOMEM);
  1135. for(ch = 0; ch < s->channels; ch++)
  1136. s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
  1137. return 0;
  1138. }
  1139. static av_cold int aac_encode_init(AVCodecContext *avctx)
  1140. {
  1141. AACEncContext *s = avctx->priv_data;
  1142. int i, ret = 0;
  1143. const uint8_t *sizes[2];
  1144. uint8_t grouping[AAC_MAX_CHANNELS];
  1145. int lengths[2];
  1146. /* Constants */
  1147. s->last_frame_pb_count = 0;
  1148. avctx->frame_size = 1024;
  1149. avctx->initial_padding = 1024;
  1150. s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
  1151. /* Channel map and unspecified bitrate guessing */
  1152. s->channels = avctx->ch_layout.nb_channels;
  1153. s->needs_pce = 1;
  1154. for (i = 0; i < FF_ARRAY_ELEMS(aac_normal_chan_layouts); i++) {
  1155. if (!av_channel_layout_compare(&avctx->ch_layout, &aac_normal_chan_layouts[i])) {
  1156. s->needs_pce = s->options.pce;
  1157. break;
  1158. }
  1159. }
  1160. if (s->needs_pce) {
  1161. char buf[64];
  1162. for (i = 0; i < FF_ARRAY_ELEMS(aac_pce_configs); i++)
  1163. if (!av_channel_layout_compare(&avctx->ch_layout, &aac_pce_configs[i].layout))
  1164. break;
  1165. av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
  1166. if (i == FF_ARRAY_ELEMS(aac_pce_configs)) {
  1167. av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout \"%s\"\n", buf);
  1168. return AVERROR(EINVAL);
  1169. }
  1170. av_log(avctx, AV_LOG_INFO, "Using a PCE to encode channel layout \"%s\"\n", buf);
  1171. s->pce = aac_pce_configs[i];
  1172. s->reorder_map = s->pce.reorder_map;
  1173. s->chan_map = s->pce.config_map;
  1174. } else {
  1175. s->reorder_map = aac_chan_maps[s->channels - 1];
  1176. s->chan_map = aac_chan_configs[s->channels - 1];
  1177. }
  1178. if (!avctx->bit_rate) {
  1179. for (i = 1; i <= s->chan_map[0]; i++) {
  1180. avctx->bit_rate += s->chan_map[i] == TYPE_CPE ? 128000 : /* Pair */
  1181. s->chan_map[i] == TYPE_LFE ? 16000 : /* LFE */
  1182. 69000 ; /* SCE */
  1183. }
  1184. }
  1185. /* Samplerate */
  1186. for (i = 0; i < 16; i++)
  1187. if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
  1188. break;
  1189. s->samplerate_index = i;
  1190. ERROR_IF(s->samplerate_index == 16 ||
  1191. s->samplerate_index >= ff_aac_swb_size_1024_len ||
  1192. s->samplerate_index >= ff_aac_swb_size_128_len,
  1193. "Unsupported sample rate %d\n", avctx->sample_rate);
  1194. /* Bitrate limiting */
  1195. WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
  1196. "Too many bits %f > %d per frame requested, clamping to max\n",
  1197. 1024.0 * avctx->bit_rate / avctx->sample_rate,
  1198. 6144 * s->channels);
  1199. avctx->bit_rate = (int64_t)FFMIN(6144 * s->channels / 1024.0 * avctx->sample_rate,
  1200. avctx->bit_rate);
  1201. /* Profile and option setting */
  1202. avctx->profile = avctx->profile == AV_PROFILE_UNKNOWN ? AV_PROFILE_AAC_LOW :
  1203. avctx->profile;
  1204. for (i = 0; i < FF_ARRAY_ELEMS(aacenc_profiles); i++)
  1205. if (avctx->profile == aacenc_profiles[i])
  1206. break;
  1207. if (avctx->profile == AV_PROFILE_MPEG2_AAC_LOW) {
  1208. avctx->profile = AV_PROFILE_AAC_LOW;
  1209. ERROR_IF(s->options.pred,
  1210. "Main prediction unavailable in the \"mpeg2_aac_low\" profile\n");
  1211. ERROR_IF(s->options.ltp,
  1212. "LTP prediction unavailable in the \"mpeg2_aac_low\" profile\n");
  1213. WARN_IF(s->options.pns,
  1214. "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n");
  1215. s->options.pns = 0;
  1216. } else if (avctx->profile == AV_PROFILE_AAC_LTP) {
  1217. s->options.ltp = 1;
  1218. ERROR_IF(s->options.pred,
  1219. "Main prediction unavailable in the \"aac_ltp\" profile\n");
  1220. } else if (avctx->profile == AV_PROFILE_AAC_MAIN) {
  1221. s->options.pred = 1;
  1222. ERROR_IF(s->options.ltp,
  1223. "LTP prediction unavailable in the \"aac_main\" profile\n");
  1224. } else if (s->options.ltp) {
  1225. avctx->profile = AV_PROFILE_AAC_LTP;
  1226. WARN_IF(1,
  1227. "Chainging profile to \"aac_ltp\"\n");
  1228. ERROR_IF(s->options.pred,
  1229. "Main prediction unavailable in the \"aac_ltp\" profile\n");
  1230. } else if (s->options.pred) {
  1231. avctx->profile = AV_PROFILE_AAC_MAIN;
  1232. WARN_IF(1,
  1233. "Chainging profile to \"aac_main\"\n");
  1234. ERROR_IF(s->options.ltp,
  1235. "LTP prediction unavailable in the \"aac_main\" profile\n");
  1236. }
  1237. s->profile = avctx->profile;
  1238. /* Coder limitations */
  1239. s->coder = &ff_aac_coders[s->options.coder];
  1240. if (s->options.coder == AAC_CODER_ANMR) {
  1241. ERROR_IF(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL,
  1242. "The ANMR coder is considered experimental, add -strict -2 to enable!\n");
  1243. s->options.intensity_stereo = 0;
  1244. s->options.pns = 0;
  1245. }
  1246. ERROR_IF(s->options.ltp && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL,
  1247. "The LPT profile requires experimental compliance, add -strict -2 to enable!\n");
  1248. /* M/S introduces horrible artifacts with multichannel files, this is temporary */
  1249. if (s->channels > 3)
  1250. s->options.mid_side = 0;
  1251. // Initialize static tables
  1252. ff_aac_float_common_init();
  1253. if ((ret = dsp_init(avctx, s)) < 0)
  1254. return ret;
  1255. if ((ret = alloc_buffers(avctx, s)) < 0)
  1256. return ret;
  1257. if ((ret = put_audio_specific_config(avctx)))
  1258. return ret;
  1259. sizes[0] = ff_aac_swb_size_1024[s->samplerate_index];
  1260. sizes[1] = ff_aac_swb_size_128[s->samplerate_index];
  1261. lengths[0] = ff_aac_num_swb_1024[s->samplerate_index];
  1262. lengths[1] = ff_aac_num_swb_128[s->samplerate_index];
  1263. for (i = 0; i < s->chan_map[0]; i++)
  1264. grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
  1265. if ((ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths,
  1266. s->chan_map[0], grouping)) < 0)
  1267. return ret;
  1268. s->psypp = ff_psy_preprocess_init(avctx);
  1269. ff_lpc_init(&s->lpc, 2*avctx->frame_size, TNS_MAX_ORDER, FF_LPC_TYPE_LEVINSON);
  1270. s->random_state = 0x1f2e3d4c;
  1271. ff_aac_dsp_init(s);
  1272. ff_af_queue_init(avctx, &s->afq);
  1273. return 0;
  1274. }
  1275. #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  1276. static const AVOption aacenc_options[] = {
  1277. {"aac_coder", "Coding algorithm", offsetof(AACEncContext, options.coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, 0, AAC_CODER_NB-1, AACENC_FLAGS, "coder"},
  1278. {"anmr", "ANMR method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_ANMR}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  1279. {"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  1280. {"fast", "Default fast search", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  1281. {"aac_ms", "Force M/S stereo coding", offsetof(AACEncContext, options.mid_side), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AACENC_FLAGS},
  1282. {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  1283. {"aac_pns", "Perceptual noise substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  1284. {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  1285. {"aac_ltp", "Long term prediction", offsetof(AACEncContext, options.ltp), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
  1286. {"aac_pred", "AAC-Main prediction", offsetof(AACEncContext, options.pred), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
  1287. {"aac_pce", "Forces the use of PCEs", offsetof(AACEncContext, options.pce), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
  1288. FF_AAC_PROFILE_OPTS
  1289. {NULL}
  1290. };
  1291. static const AVClass aacenc_class = {
  1292. .class_name = "AAC encoder",
  1293. .item_name = av_default_item_name,
  1294. .option = aacenc_options,
  1295. .version = LIBAVUTIL_VERSION_INT,
  1296. };
  1297. static const FFCodecDefault aac_encode_defaults[] = {
  1298. { "b", "0" },
  1299. { NULL }
  1300. };
  1301. const FFCodec ff_aac_encoder = {
  1302. .p.name = "aac",
  1303. CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
  1304. .p.type = AVMEDIA_TYPE_AUDIO,
  1305. .p.id = AV_CODEC_ID_AAC,
  1306. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
  1307. AV_CODEC_CAP_SMALL_LAST_FRAME,
  1308. .priv_data_size = sizeof(AACEncContext),
  1309. .init = aac_encode_init,
  1310. FF_CODEC_ENCODE_CB(aac_encode_frame),
  1311. .close = aac_encode_end,
  1312. .defaults = aac_encode_defaults,
  1313. .p.supported_samplerates = ff_mpeg4audio_sample_rates,
  1314. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  1315. .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
  1316. AV_SAMPLE_FMT_NONE },
  1317. .p.priv_class = &aacenc_class,
  1318. };
  1319. void ff_aac_dsp_init(AACEncContext *s){
  1320. s->abs_pow34 = abs_pow34_v;
  1321. s->quant_bands = quantize_bands;
  1322. #if ARCH_RISCV
  1323. ff_aac_dsp_init_riscv(s);
  1324. #elif ARCH_X86
  1325. ff_aac_dsp_init_x86(s);
  1326. #endif
  1327. #if HAVE_MIPSDSP
  1328. ff_aac_coder_init_mips(s);
  1329. #endif
  1330. }