aacdec_template.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453
  1. /*
  2. * AAC decoder
  3. * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
  4. * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
  5. * Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
  6. *
  7. * AAC LATM decoder
  8. * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
  9. * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
  10. *
  11. * AAC decoder fixed-point implementation
  12. * Copyright (c) 2013
  13. * MIPS Technologies, Inc., California.
  14. *
  15. * This file is part of FFmpeg.
  16. *
  17. * FFmpeg is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU Lesser General Public
  19. * License as published by the Free Software Foundation; either
  20. * version 2.1 of the License, or (at your option) any later version.
  21. *
  22. * FFmpeg is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * Lesser General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Lesser General Public
  28. * License along with FFmpeg; if not, write to the Free Software
  29. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  30. */
  31. /**
  32. * @file
  33. * AAC decoder
  34. * @author Oded Shimon ( ods15 ods15 dyndns org )
  35. * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
  36. *
  37. * AAC decoder fixed-point implementation
  38. * @author Stanislav Ocovaj ( stanislav.ocovaj imgtec com )
  39. * @author Nedeljko Babic ( nedeljko.babic imgtec com )
  40. */
  41. /*
  42. * supported tools
  43. *
  44. * Support? Name
  45. * N (code in SoC repo) gain control
  46. * Y block switching
  47. * Y window shapes - standard
  48. * N window shapes - Low Delay
  49. * Y filterbank - standard
  50. * N (code in SoC repo) filterbank - Scalable Sample Rate
  51. * Y Temporal Noise Shaping
  52. * Y Long Term Prediction
  53. * Y intensity stereo
  54. * Y channel coupling
  55. * Y frequency domain prediction
  56. * Y Perceptual Noise Substitution
  57. * Y Mid/Side stereo
  58. * N Scalable Inverse AAC Quantization
  59. * N Frequency Selective Switch
  60. * N upsampling filter
  61. * Y quantization & coding - AAC
  62. * N quantization & coding - TwinVQ
  63. * N quantization & coding - BSAC
  64. * N AAC Error Resilience tools
  65. * N Error Resilience payload syntax
  66. * N Error Protection tool
  67. * N CELP
  68. * N Silence Compression
  69. * N HVXC
  70. * N HVXC 4kbits/s VR
  71. * N Structured Audio tools
  72. * N Structured Audio Sample Bank Format
  73. * N MIDI
  74. * N Harmonic and Individual Lines plus Noise
  75. * N Text-To-Speech Interface
  76. * Y Spectral Band Replication
  77. * Y (not in this code) Layer-1
  78. * Y (not in this code) Layer-2
  79. * Y (not in this code) Layer-3
  80. * N SinuSoidal Coding (Transient, Sinusoid, Noise)
  81. * Y Parametric Stereo
  82. * N Direct Stream Transfer
  83. * Y (not in fixed point code) Enhanced AAC Low Delay (ER AAC ELD)
  84. *
  85. * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
  86. * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
  87. Parametric Stereo.
  88. */
  89. #include "libavutil/thread.h"
  90. static VLC vlc_scalefactors;
  91. static VLC vlc_spectral[11];
  92. static int output_configure(AACContext *ac,
  93. uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
  94. enum OCStatus oc_type, int get_new_frame);
  95. #define overread_err "Input buffer exhausted before END element found\n"
  96. static int count_channels(uint8_t (*layout)[3], int tags)
  97. {
  98. int i, sum = 0;
  99. for (i = 0; i < tags; i++) {
  100. int syn_ele = layout[i][0];
  101. int pos = layout[i][2];
  102. sum += (1 + (syn_ele == TYPE_CPE)) *
  103. (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
  104. }
  105. return sum;
  106. }
  107. /**
  108. * Check for the channel element in the current channel position configuration.
  109. * If it exists, make sure the appropriate element is allocated and map the
  110. * channel order to match the internal FFmpeg channel layout.
  111. *
  112. * @param che_pos current channel position configuration
  113. * @param type channel element type
  114. * @param id channel element id
  115. * @param channels count of the number of channels in the configuration
  116. *
  117. * @return Returns error status. 0 - OK, !0 - error
  118. */
  119. static av_cold int che_configure(AACContext *ac,
  120. enum ChannelPosition che_pos,
  121. int type, int id, int *channels)
  122. {
  123. if (*channels >= MAX_CHANNELS)
  124. return AVERROR_INVALIDDATA;
  125. if (che_pos) {
  126. if (!ac->che[type][id]) {
  127. if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
  128. return AVERROR(ENOMEM);
  129. AAC_RENAME(ff_aac_sbr_ctx_init)(ac, &ac->che[type][id]->sbr, type);
  130. }
  131. if (type != TYPE_CCE) {
  132. if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
  133. av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
  137. if (type == TYPE_CPE ||
  138. (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
  139. ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
  140. }
  141. }
  142. } else {
  143. if (ac->che[type][id])
  144. AAC_RENAME(ff_aac_sbr_ctx_close)(&ac->che[type][id]->sbr);
  145. av_freep(&ac->che[type][id]);
  146. }
  147. return 0;
  148. }
  149. static int frame_configure_elements(AVCodecContext *avctx)
  150. {
  151. AACContext *ac = avctx->priv_data;
  152. int type, id, ch, ret;
  153. /* set channel pointers to internal buffers by default */
  154. for (type = 0; type < 4; type++) {
  155. for (id = 0; id < MAX_ELEM_ID; id++) {
  156. ChannelElement *che = ac->che[type][id];
  157. if (che) {
  158. che->ch[0].ret = che->ch[0].ret_buf;
  159. che->ch[1].ret = che->ch[1].ret_buf;
  160. }
  161. }
  162. }
  163. /* get output buffer */
  164. av_frame_unref(ac->frame);
  165. if (!avctx->channels)
  166. return 1;
  167. ac->frame->nb_samples = 2048;
  168. if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
  169. return ret;
  170. /* map output channel pointers to AVFrame data */
  171. for (ch = 0; ch < avctx->channels; ch++) {
  172. if (ac->output_element[ch])
  173. ac->output_element[ch]->ret = (INTFLOAT *)ac->frame->extended_data[ch];
  174. }
  175. return 0;
  176. }
  177. struct elem_to_channel {
  178. uint64_t av_position;
  179. uint8_t syn_ele;
  180. uint8_t elem_id;
  181. uint8_t aac_position;
  182. };
  183. static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
  184. uint8_t (*layout_map)[3], int offset, uint64_t left,
  185. uint64_t right, int pos)
  186. {
  187. if (layout_map[offset][0] == TYPE_CPE) {
  188. e2c_vec[offset] = (struct elem_to_channel) {
  189. .av_position = left | right,
  190. .syn_ele = TYPE_CPE,
  191. .elem_id = layout_map[offset][1],
  192. .aac_position = pos
  193. };
  194. return 1;
  195. } else {
  196. e2c_vec[offset] = (struct elem_to_channel) {
  197. .av_position = left,
  198. .syn_ele = TYPE_SCE,
  199. .elem_id = layout_map[offset][1],
  200. .aac_position = pos
  201. };
  202. e2c_vec[offset + 1] = (struct elem_to_channel) {
  203. .av_position = right,
  204. .syn_ele = TYPE_SCE,
  205. .elem_id = layout_map[offset + 1][1],
  206. .aac_position = pos
  207. };
  208. return 2;
  209. }
  210. }
  211. static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
  212. int *current)
  213. {
  214. int num_pos_channels = 0;
  215. int first_cpe = 0;
  216. int sce_parity = 0;
  217. int i;
  218. for (i = *current; i < tags; i++) {
  219. if (layout_map[i][2] != pos)
  220. break;
  221. if (layout_map[i][0] == TYPE_CPE) {
  222. if (sce_parity) {
  223. if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
  224. sce_parity = 0;
  225. } else {
  226. return -1;
  227. }
  228. }
  229. num_pos_channels += 2;
  230. first_cpe = 1;
  231. } else {
  232. num_pos_channels++;
  233. sce_parity ^= 1;
  234. }
  235. }
  236. if (sce_parity &&
  237. ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
  238. return -1;
  239. *current = i;
  240. return num_pos_channels;
  241. }
  242. static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
  243. {
  244. int i, n, total_non_cc_elements;
  245. struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
  246. int num_front_channels, num_side_channels, num_back_channels;
  247. uint64_t layout;
  248. if (FF_ARRAY_ELEMS(e2c_vec) < tags)
  249. return 0;
  250. i = 0;
  251. num_front_channels =
  252. count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
  253. if (num_front_channels < 0)
  254. return 0;
  255. num_side_channels =
  256. count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
  257. if (num_side_channels < 0)
  258. return 0;
  259. num_back_channels =
  260. count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
  261. if (num_back_channels < 0)
  262. return 0;
  263. if (num_side_channels == 0 && num_back_channels >= 4) {
  264. num_side_channels = 2;
  265. num_back_channels -= 2;
  266. }
  267. i = 0;
  268. if (num_front_channels & 1) {
  269. e2c_vec[i] = (struct elem_to_channel) {
  270. .av_position = AV_CH_FRONT_CENTER,
  271. .syn_ele = TYPE_SCE,
  272. .elem_id = layout_map[i][1],
  273. .aac_position = AAC_CHANNEL_FRONT
  274. };
  275. i++;
  276. num_front_channels--;
  277. }
  278. if (num_front_channels >= 4) {
  279. i += assign_pair(e2c_vec, layout_map, i,
  280. AV_CH_FRONT_LEFT_OF_CENTER,
  281. AV_CH_FRONT_RIGHT_OF_CENTER,
  282. AAC_CHANNEL_FRONT);
  283. num_front_channels -= 2;
  284. }
  285. if (num_front_channels >= 2) {
  286. i += assign_pair(e2c_vec, layout_map, i,
  287. AV_CH_FRONT_LEFT,
  288. AV_CH_FRONT_RIGHT,
  289. AAC_CHANNEL_FRONT);
  290. num_front_channels -= 2;
  291. }
  292. while (num_front_channels >= 2) {
  293. i += assign_pair(e2c_vec, layout_map, i,
  294. UINT64_MAX,
  295. UINT64_MAX,
  296. AAC_CHANNEL_FRONT);
  297. num_front_channels -= 2;
  298. }
  299. if (num_side_channels >= 2) {
  300. i += assign_pair(e2c_vec, layout_map, i,
  301. AV_CH_SIDE_LEFT,
  302. AV_CH_SIDE_RIGHT,
  303. AAC_CHANNEL_FRONT);
  304. num_side_channels -= 2;
  305. }
  306. while (num_side_channels >= 2) {
  307. i += assign_pair(e2c_vec, layout_map, i,
  308. UINT64_MAX,
  309. UINT64_MAX,
  310. AAC_CHANNEL_SIDE);
  311. num_side_channels -= 2;
  312. }
  313. while (num_back_channels >= 4) {
  314. i += assign_pair(e2c_vec, layout_map, i,
  315. UINT64_MAX,
  316. UINT64_MAX,
  317. AAC_CHANNEL_BACK);
  318. num_back_channels -= 2;
  319. }
  320. if (num_back_channels >= 2) {
  321. i += assign_pair(e2c_vec, layout_map, i,
  322. AV_CH_BACK_LEFT,
  323. AV_CH_BACK_RIGHT,
  324. AAC_CHANNEL_BACK);
  325. num_back_channels -= 2;
  326. }
  327. if (num_back_channels) {
  328. e2c_vec[i] = (struct elem_to_channel) {
  329. .av_position = AV_CH_BACK_CENTER,
  330. .syn_ele = TYPE_SCE,
  331. .elem_id = layout_map[i][1],
  332. .aac_position = AAC_CHANNEL_BACK
  333. };
  334. i++;
  335. num_back_channels--;
  336. }
  337. if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
  338. e2c_vec[i] = (struct elem_to_channel) {
  339. .av_position = AV_CH_LOW_FREQUENCY,
  340. .syn_ele = TYPE_LFE,
  341. .elem_id = layout_map[i][1],
  342. .aac_position = AAC_CHANNEL_LFE
  343. };
  344. i++;
  345. }
  346. while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
  347. e2c_vec[i] = (struct elem_to_channel) {
  348. .av_position = UINT64_MAX,
  349. .syn_ele = TYPE_LFE,
  350. .elem_id = layout_map[i][1],
  351. .aac_position = AAC_CHANNEL_LFE
  352. };
  353. i++;
  354. }
  355. // Must choose a stable sort
  356. total_non_cc_elements = n = i;
  357. do {
  358. int next_n = 0;
  359. for (i = 1; i < n; i++)
  360. if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
  361. FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
  362. next_n = i;
  363. }
  364. n = next_n;
  365. } while (n > 0);
  366. layout = 0;
  367. for (i = 0; i < total_non_cc_elements; i++) {
  368. layout_map[i][0] = e2c_vec[i].syn_ele;
  369. layout_map[i][1] = e2c_vec[i].elem_id;
  370. layout_map[i][2] = e2c_vec[i].aac_position;
  371. if (e2c_vec[i].av_position != UINT64_MAX) {
  372. layout |= e2c_vec[i].av_position;
  373. }
  374. }
  375. return layout;
  376. }
  377. /**
  378. * Save current output configuration if and only if it has been locked.
  379. */
  380. static int push_output_configuration(AACContext *ac) {
  381. int pushed = 0;
  382. if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
  383. ac->oc[0] = ac->oc[1];
  384. pushed = 1;
  385. }
  386. ac->oc[1].status = OC_NONE;
  387. return pushed;
  388. }
  389. /**
  390. * Restore the previous output configuration if and only if the current
  391. * configuration is unlocked.
  392. */
  393. static void pop_output_configuration(AACContext *ac) {
  394. if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
  395. ac->oc[1] = ac->oc[0];
  396. ac->avctx->channels = ac->oc[1].channels;
  397. ac->avctx->channel_layout = ac->oc[1].channel_layout;
  398. output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
  399. ac->oc[1].status, 0);
  400. }
  401. }
  402. /**
  403. * Configure output channel order based on the current program
  404. * configuration element.
  405. *
  406. * @return Returns error status. 0 - OK, !0 - error
  407. */
  408. static int output_configure(AACContext *ac,
  409. uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
  410. enum OCStatus oc_type, int get_new_frame)
  411. {
  412. AVCodecContext *avctx = ac->avctx;
  413. int i, channels = 0, ret;
  414. uint64_t layout = 0;
  415. uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
  416. uint8_t type_counts[TYPE_END] = { 0 };
  417. if (ac->oc[1].layout_map != layout_map) {
  418. memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
  419. ac->oc[1].layout_map_tags = tags;
  420. }
  421. for (i = 0; i < tags; i++) {
  422. int type = layout_map[i][0];
  423. int id = layout_map[i][1];
  424. id_map[type][id] = type_counts[type]++;
  425. if (id_map[type][id] >= MAX_ELEM_ID) {
  426. avpriv_request_sample(ac->avctx, "Too large remapped id");
  427. return AVERROR_PATCHWELCOME;
  428. }
  429. }
  430. // Try to sniff a reasonable channel order, otherwise output the
  431. // channels in the order the PCE declared them.
  432. if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
  433. layout = sniff_channel_order(layout_map, tags);
  434. for (i = 0; i < tags; i++) {
  435. int type = layout_map[i][0];
  436. int id = layout_map[i][1];
  437. int iid = id_map[type][id];
  438. int position = layout_map[i][2];
  439. // Allocate or free elements depending on if they are in the
  440. // current program configuration.
  441. ret = che_configure(ac, position, type, iid, &channels);
  442. if (ret < 0)
  443. return ret;
  444. ac->tag_che_map[type][id] = ac->che[type][iid];
  445. }
  446. if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
  447. if (layout == AV_CH_FRONT_CENTER) {
  448. layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
  449. } else {
  450. layout = 0;
  451. }
  452. }
  453. if (layout) avctx->channel_layout = layout;
  454. ac->oc[1].channel_layout = layout;
  455. avctx->channels = ac->oc[1].channels = channels;
  456. ac->oc[1].status = oc_type;
  457. if (get_new_frame) {
  458. if ((ret = frame_configure_elements(ac->avctx)) < 0)
  459. return ret;
  460. }
  461. return 0;
  462. }
  463. static void flush(AVCodecContext *avctx)
  464. {
  465. AACContext *ac= avctx->priv_data;
  466. int type, i, j;
  467. for (type = 3; type >= 0; type--) {
  468. for (i = 0; i < MAX_ELEM_ID; i++) {
  469. ChannelElement *che = ac->che[type][i];
  470. if (che) {
  471. for (j = 0; j <= 1; j++) {
  472. memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
  473. }
  474. }
  475. }
  476. }
  477. }
  478. /**
  479. * Set up channel positions based on a default channel configuration
  480. * as specified in table 1.17.
  481. *
  482. * @return Returns error status. 0 - OK, !0 - error
  483. */
  484. static int set_default_channel_config(AVCodecContext *avctx,
  485. uint8_t (*layout_map)[3],
  486. int *tags,
  487. int channel_config)
  488. {
  489. if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
  490. channel_config > 12) {
  491. av_log(avctx, AV_LOG_ERROR,
  492. "invalid default channel configuration (%d)\n",
  493. channel_config);
  494. return AVERROR_INVALIDDATA;
  495. }
  496. *tags = tags_per_config[channel_config];
  497. memcpy(layout_map, aac_channel_layout_map[channel_config - 1],
  498. *tags * sizeof(*layout_map));
  499. /*
  500. * AAC specification has 7.1(wide) as a default layout for 8-channel streams.
  501. * However, at least Nero AAC encoder encodes 7.1 streams using the default
  502. * channel config 7, mapping the side channels of the original audio stream
  503. * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
  504. * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
  505. * the incorrect streams as if they were correct (and as the encoder intended).
  506. *
  507. * As actual intended 7.1(wide) streams are very rare, default to assuming a
  508. * 7.1 layout was intended.
  509. */
  510. if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
  511. av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
  512. " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
  513. " according to the specification instead.\n", FF_COMPLIANCE_STRICT);
  514. layout_map[2][2] = AAC_CHANNEL_SIDE;
  515. }
  516. return 0;
  517. }
  518. static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
  519. {
  520. /* For PCE based channel configurations map the channels solely based
  521. * on tags. */
  522. if (!ac->oc[1].m4ac.chan_config) {
  523. return ac->tag_che_map[type][elem_id];
  524. }
  525. // Allow single CPE stereo files to be signalled with mono configuration.
  526. if (!ac->tags_mapped && type == TYPE_CPE &&
  527. ac->oc[1].m4ac.chan_config == 1) {
  528. uint8_t layout_map[MAX_ELEM_ID*4][3];
  529. int layout_map_tags;
  530. push_output_configuration(ac);
  531. av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
  532. if (set_default_channel_config(ac->avctx, layout_map,
  533. &layout_map_tags, 2) < 0)
  534. return NULL;
  535. if (output_configure(ac, layout_map, layout_map_tags,
  536. OC_TRIAL_FRAME, 1) < 0)
  537. return NULL;
  538. ac->oc[1].m4ac.chan_config = 2;
  539. ac->oc[1].m4ac.ps = 0;
  540. }
  541. // And vice-versa
  542. if (!ac->tags_mapped && type == TYPE_SCE &&
  543. ac->oc[1].m4ac.chan_config == 2) {
  544. uint8_t layout_map[MAX_ELEM_ID * 4][3];
  545. int layout_map_tags;
  546. push_output_configuration(ac);
  547. av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
  548. if (set_default_channel_config(ac->avctx, layout_map,
  549. &layout_map_tags, 1) < 0)
  550. return NULL;
  551. if (output_configure(ac, layout_map, layout_map_tags,
  552. OC_TRIAL_FRAME, 1) < 0)
  553. return NULL;
  554. ac->oc[1].m4ac.chan_config = 1;
  555. if (ac->oc[1].m4ac.sbr)
  556. ac->oc[1].m4ac.ps = -1;
  557. }
  558. /* For indexed channel configurations map the channels solely based
  559. * on position. */
  560. switch (ac->oc[1].m4ac.chan_config) {
  561. case 12:
  562. case 7:
  563. if (ac->tags_mapped == 3 && type == TYPE_CPE) {
  564. ac->tags_mapped++;
  565. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
  566. }
  567. case 11:
  568. if (ac->tags_mapped == 2 &&
  569. ac->oc[1].m4ac.chan_config == 11 &&
  570. type == TYPE_SCE) {
  571. ac->tags_mapped++;
  572. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
  573. }
  574. case 6:
  575. /* Some streams incorrectly code 5.1 audio as
  576. * SCE[0] CPE[0] CPE[1] SCE[1]
  577. * instead of
  578. * SCE[0] CPE[0] CPE[1] LFE[0].
  579. * If we seem to have encountered such a stream, transfer
  580. * the LFE[0] element to the SCE[1]'s mapping */
  581. if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
  582. if (!ac->warned_remapping_once && (type != TYPE_LFE || elem_id != 0)) {
  583. av_log(ac->avctx, AV_LOG_WARNING,
  584. "This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
  585. type == TYPE_SCE ? "SCE" : "LFE", elem_id);
  586. ac->warned_remapping_once++;
  587. }
  588. ac->tags_mapped++;
  589. return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
  590. }
  591. case 5:
  592. if (ac->tags_mapped == 2 && type == TYPE_CPE) {
  593. ac->tags_mapped++;
  594. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
  595. }
  596. case 4:
  597. /* Some streams incorrectly code 4.0 audio as
  598. * SCE[0] CPE[0] LFE[0]
  599. * instead of
  600. * SCE[0] CPE[0] SCE[1].
  601. * If we seem to have encountered such a stream, transfer
  602. * the SCE[1] element to the LFE[0]'s mapping */
  603. if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
  604. if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
  605. av_log(ac->avctx, AV_LOG_WARNING,
  606. "This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
  607. type == TYPE_SCE ? "SCE" : "LFE", elem_id);
  608. ac->warned_remapping_once++;
  609. }
  610. ac->tags_mapped++;
  611. return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
  612. }
  613. if (ac->tags_mapped == 2 &&
  614. ac->oc[1].m4ac.chan_config == 4 &&
  615. type == TYPE_SCE) {
  616. ac->tags_mapped++;
  617. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
  618. }
  619. case 3:
  620. case 2:
  621. if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
  622. type == TYPE_CPE) {
  623. ac->tags_mapped++;
  624. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
  625. } else if (ac->oc[1].m4ac.chan_config == 2) {
  626. return NULL;
  627. }
  628. case 1:
  629. if (!ac->tags_mapped && type == TYPE_SCE) {
  630. ac->tags_mapped++;
  631. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
  632. }
  633. default:
  634. return NULL;
  635. }
  636. }
  637. /**
  638. * Decode an array of 4 bit element IDs, optionally interleaved with a
  639. * stereo/mono switching bit.
  640. *
  641. * @param type speaker type/position for these channels
  642. */
  643. static void decode_channel_map(uint8_t layout_map[][3],
  644. enum ChannelPosition type,
  645. GetBitContext *gb, int n)
  646. {
  647. while (n--) {
  648. enum RawDataBlockType syn_ele;
  649. switch (type) {
  650. case AAC_CHANNEL_FRONT:
  651. case AAC_CHANNEL_BACK:
  652. case AAC_CHANNEL_SIDE:
  653. syn_ele = get_bits1(gb);
  654. break;
  655. case AAC_CHANNEL_CC:
  656. skip_bits1(gb);
  657. syn_ele = TYPE_CCE;
  658. break;
  659. case AAC_CHANNEL_LFE:
  660. syn_ele = TYPE_LFE;
  661. break;
  662. default:
  663. // AAC_CHANNEL_OFF has no channel map
  664. av_assert0(0);
  665. }
  666. layout_map[0][0] = syn_ele;
  667. layout_map[0][1] = get_bits(gb, 4);
  668. layout_map[0][2] = type;
  669. layout_map++;
  670. }
  671. }
  672. static inline void relative_align_get_bits(GetBitContext *gb,
  673. int reference_position) {
  674. int n = (reference_position - get_bits_count(gb) & 7);
  675. if (n)
  676. skip_bits(gb, n);
  677. }
  678. /**
  679. * Decode program configuration element; reference: table 4.2.
  680. *
  681. * @return Returns error status. 0 - OK, !0 - error
  682. */
  683. static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
  684. uint8_t (*layout_map)[3],
  685. GetBitContext *gb, int byte_align_ref)
  686. {
  687. int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
  688. int sampling_index;
  689. int comment_len;
  690. int tags;
  691. skip_bits(gb, 2); // object_type
  692. sampling_index = get_bits(gb, 4);
  693. if (m4ac->sampling_index != sampling_index)
  694. av_log(avctx, AV_LOG_WARNING,
  695. "Sample rate index in program config element does not "
  696. "match the sample rate index configured by the container.\n");
  697. num_front = get_bits(gb, 4);
  698. num_side = get_bits(gb, 4);
  699. num_back = get_bits(gb, 4);
  700. num_lfe = get_bits(gb, 2);
  701. num_assoc_data = get_bits(gb, 3);
  702. num_cc = get_bits(gb, 4);
  703. if (get_bits1(gb))
  704. skip_bits(gb, 4); // mono_mixdown_tag
  705. if (get_bits1(gb))
  706. skip_bits(gb, 4); // stereo_mixdown_tag
  707. if (get_bits1(gb))
  708. skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
  709. if (get_bits_left(gb) < 5 * (num_front + num_side + num_back + num_cc) + 4 *(num_lfe + num_assoc_data + num_cc)) {
  710. av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
  711. return -1;
  712. }
  713. decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
  714. tags = num_front;
  715. decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
  716. tags += num_side;
  717. decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
  718. tags += num_back;
  719. decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
  720. tags += num_lfe;
  721. skip_bits_long(gb, 4 * num_assoc_data);
  722. decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
  723. tags += num_cc;
  724. relative_align_get_bits(gb, byte_align_ref);
  725. /* comment field, first byte is length */
  726. comment_len = get_bits(gb, 8) * 8;
  727. if (get_bits_left(gb) < comment_len) {
  728. av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
  729. return AVERROR_INVALIDDATA;
  730. }
  731. skip_bits_long(gb, comment_len);
  732. return tags;
  733. }
  734. /**
  735. * Decode GA "General Audio" specific configuration; reference: table 4.1.
  736. *
  737. * @param ac pointer to AACContext, may be null
  738. * @param avctx pointer to AVCCodecContext, used for logging
  739. *
  740. * @return Returns error status. 0 - OK, !0 - error
  741. */
  742. static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
  743. GetBitContext *gb,
  744. int get_bit_alignment,
  745. MPEG4AudioConfig *m4ac,
  746. int channel_config)
  747. {
  748. int extension_flag, ret, ep_config, res_flags;
  749. uint8_t layout_map[MAX_ELEM_ID*4][3];
  750. int tags = 0;
  751. #if USE_FIXED
  752. if (get_bits1(gb)) { // frameLengthFlag
  753. avpriv_report_missing_feature(avctx, "Fixed point 960/120 MDCT window");
  754. return AVERROR_PATCHWELCOME;
  755. }
  756. m4ac->frame_length_short = 0;
  757. #else
  758. m4ac->frame_length_short = get_bits1(gb);
  759. if (m4ac->frame_length_short && m4ac->sbr == 1) {
  760. avpriv_report_missing_feature(avctx, "SBR with 960 frame length");
  761. if (ac) ac->warned_960_sbr = 1;
  762. m4ac->sbr = 0;
  763. m4ac->ps = 0;
  764. }
  765. #endif
  766. if (get_bits1(gb)) // dependsOnCoreCoder
  767. skip_bits(gb, 14); // coreCoderDelay
  768. extension_flag = get_bits1(gb);
  769. if (m4ac->object_type == AOT_AAC_SCALABLE ||
  770. m4ac->object_type == AOT_ER_AAC_SCALABLE)
  771. skip_bits(gb, 3); // layerNr
  772. if (channel_config == 0) {
  773. skip_bits(gb, 4); // element_instance_tag
  774. tags = decode_pce(avctx, m4ac, layout_map, gb, get_bit_alignment);
  775. if (tags < 0)
  776. return tags;
  777. } else {
  778. if ((ret = set_default_channel_config(avctx, layout_map,
  779. &tags, channel_config)))
  780. return ret;
  781. }
  782. if (count_channels(layout_map, tags) > 1) {
  783. m4ac->ps = 0;
  784. } else if (m4ac->sbr == 1 && m4ac->ps == -1)
  785. m4ac->ps = 1;
  786. if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
  787. return ret;
  788. if (extension_flag) {
  789. switch (m4ac->object_type) {
  790. case AOT_ER_BSAC:
  791. skip_bits(gb, 5); // numOfSubFrame
  792. skip_bits(gb, 11); // layer_length
  793. break;
  794. case AOT_ER_AAC_LC:
  795. case AOT_ER_AAC_LTP:
  796. case AOT_ER_AAC_SCALABLE:
  797. case AOT_ER_AAC_LD:
  798. res_flags = get_bits(gb, 3);
  799. if (res_flags) {
  800. avpriv_report_missing_feature(avctx,
  801. "AAC data resilience (flags %x)",
  802. res_flags);
  803. return AVERROR_PATCHWELCOME;
  804. }
  805. break;
  806. }
  807. skip_bits1(gb); // extensionFlag3 (TBD in version 3)
  808. }
  809. switch (m4ac->object_type) {
  810. case AOT_ER_AAC_LC:
  811. case AOT_ER_AAC_LTP:
  812. case AOT_ER_AAC_SCALABLE:
  813. case AOT_ER_AAC_LD:
  814. ep_config = get_bits(gb, 2);
  815. if (ep_config) {
  816. avpriv_report_missing_feature(avctx,
  817. "epConfig %d", ep_config);
  818. return AVERROR_PATCHWELCOME;
  819. }
  820. }
  821. return 0;
  822. }
  823. static int decode_eld_specific_config(AACContext *ac, AVCodecContext *avctx,
  824. GetBitContext *gb,
  825. MPEG4AudioConfig *m4ac,
  826. int channel_config)
  827. {
  828. int ret, ep_config, res_flags;
  829. uint8_t layout_map[MAX_ELEM_ID*4][3];
  830. int tags = 0;
  831. const int ELDEXT_TERM = 0;
  832. m4ac->ps = 0;
  833. m4ac->sbr = 0;
  834. #if USE_FIXED
  835. if (get_bits1(gb)) { // frameLengthFlag
  836. avpriv_request_sample(avctx, "960/120 MDCT window");
  837. return AVERROR_PATCHWELCOME;
  838. }
  839. #else
  840. m4ac->frame_length_short = get_bits1(gb);
  841. #endif
  842. res_flags = get_bits(gb, 3);
  843. if (res_flags) {
  844. avpriv_report_missing_feature(avctx,
  845. "AAC data resilience (flags %x)",
  846. res_flags);
  847. return AVERROR_PATCHWELCOME;
  848. }
  849. if (get_bits1(gb)) { // ldSbrPresentFlag
  850. avpriv_report_missing_feature(avctx,
  851. "Low Delay SBR");
  852. return AVERROR_PATCHWELCOME;
  853. }
  854. while (get_bits(gb, 4) != ELDEXT_TERM) {
  855. int len = get_bits(gb, 4);
  856. if (len == 15)
  857. len += get_bits(gb, 8);
  858. if (len == 15 + 255)
  859. len += get_bits(gb, 16);
  860. if (get_bits_left(gb) < len * 8 + 4) {
  861. av_log(avctx, AV_LOG_ERROR, overread_err);
  862. return AVERROR_INVALIDDATA;
  863. }
  864. skip_bits_long(gb, 8 * len);
  865. }
  866. if ((ret = set_default_channel_config(avctx, layout_map,
  867. &tags, channel_config)))
  868. return ret;
  869. if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
  870. return ret;
  871. ep_config = get_bits(gb, 2);
  872. if (ep_config) {
  873. avpriv_report_missing_feature(avctx,
  874. "epConfig %d", ep_config);
  875. return AVERROR_PATCHWELCOME;
  876. }
  877. return 0;
  878. }
  879. /**
  880. * Decode audio specific configuration; reference: table 1.13.
  881. *
  882. * @param ac pointer to AACContext, may be null
  883. * @param avctx pointer to AVCCodecContext, used for logging
  884. * @param m4ac pointer to MPEG4AudioConfig, used for parsing
  885. * @param gb buffer holding an audio specific config
  886. * @param get_bit_alignment relative alignment for byte align operations
  887. * @param sync_extension look for an appended sync extension
  888. *
  889. * @return Returns error status or number of consumed bits. <0 - error
  890. */
  891. static int decode_audio_specific_config_gb(AACContext *ac,
  892. AVCodecContext *avctx,
  893. MPEG4AudioConfig *m4ac,
  894. GetBitContext *gb,
  895. int get_bit_alignment,
  896. int sync_extension)
  897. {
  898. int i, ret;
  899. GetBitContext gbc = *gb;
  900. if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension)) < 0)
  901. return AVERROR_INVALIDDATA;
  902. if (m4ac->sampling_index > 12) {
  903. av_log(avctx, AV_LOG_ERROR,
  904. "invalid sampling rate index %d\n",
  905. m4ac->sampling_index);
  906. return AVERROR_INVALIDDATA;
  907. }
  908. if (m4ac->object_type == AOT_ER_AAC_LD &&
  909. (m4ac->sampling_index < 3 || m4ac->sampling_index > 7)) {
  910. av_log(avctx, AV_LOG_ERROR,
  911. "invalid low delay sampling rate index %d\n",
  912. m4ac->sampling_index);
  913. return AVERROR_INVALIDDATA;
  914. }
  915. skip_bits_long(gb, i);
  916. switch (m4ac->object_type) {
  917. case AOT_AAC_MAIN:
  918. case AOT_AAC_LC:
  919. case AOT_AAC_SSR:
  920. case AOT_AAC_LTP:
  921. case AOT_ER_AAC_LC:
  922. case AOT_ER_AAC_LD:
  923. if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,
  924. m4ac, m4ac->chan_config)) < 0)
  925. return ret;
  926. break;
  927. case AOT_ER_AAC_ELD:
  928. if ((ret = decode_eld_specific_config(ac, avctx, gb,
  929. m4ac, m4ac->chan_config)) < 0)
  930. return ret;
  931. break;
  932. default:
  933. avpriv_report_missing_feature(avctx,
  934. "Audio object type %s%d",
  935. m4ac->sbr == 1 ? "SBR+" : "",
  936. m4ac->object_type);
  937. return AVERROR(ENOSYS);
  938. }
  939. ff_dlog(avctx,
  940. "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
  941. m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
  942. m4ac->sample_rate, m4ac->sbr,
  943. m4ac->ps);
  944. return get_bits_count(gb);
  945. }
  946. static int decode_audio_specific_config(AACContext *ac,
  947. AVCodecContext *avctx,
  948. MPEG4AudioConfig *m4ac,
  949. const uint8_t *data, int64_t bit_size,
  950. int sync_extension)
  951. {
  952. int i, ret;
  953. GetBitContext gb;
  954. if (bit_size < 0 || bit_size > INT_MAX) {
  955. av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
  956. return AVERROR_INVALIDDATA;
  957. }
  958. ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
  959. for (i = 0; i < bit_size >> 3; i++)
  960. ff_dlog(avctx, "%02x ", data[i]);
  961. ff_dlog(avctx, "\n");
  962. if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
  963. return ret;
  964. return decode_audio_specific_config_gb(ac, avctx, m4ac, &gb, 0,
  965. sync_extension);
  966. }
  967. /**
  968. * linear congruential pseudorandom number generator
  969. *
  970. * @param previous_val pointer to the current state of the generator
  971. *
  972. * @return Returns a 32-bit pseudorandom integer
  973. */
  974. static av_always_inline int lcg_random(unsigned previous_val)
  975. {
  976. union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
  977. return v.s;
  978. }
  979. static void reset_all_predictors(PredictorState *ps)
  980. {
  981. int i;
  982. for (i = 0; i < MAX_PREDICTORS; i++)
  983. reset_predict_state(&ps[i]);
  984. }
  985. static int sample_rate_idx (int rate)
  986. {
  987. if (92017 <= rate) return 0;
  988. else if (75132 <= rate) return 1;
  989. else if (55426 <= rate) return 2;
  990. else if (46009 <= rate) return 3;
  991. else if (37566 <= rate) return 4;
  992. else if (27713 <= rate) return 5;
  993. else if (23004 <= rate) return 6;
  994. else if (18783 <= rate) return 7;
  995. else if (13856 <= rate) return 8;
  996. else if (11502 <= rate) return 9;
  997. else if (9391 <= rate) return 10;
  998. else return 11;
  999. }
  1000. static void reset_predictor_group(PredictorState *ps, int group_num)
  1001. {
  1002. int i;
  1003. for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
  1004. reset_predict_state(&ps[i]);
  1005. }
  1006. #define AAC_INIT_VLC_STATIC(num, size) \
  1007. INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
  1008. ff_aac_spectral_bits[num], sizeof(ff_aac_spectral_bits[num][0]), \
  1009. sizeof(ff_aac_spectral_bits[num][0]), \
  1010. ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), \
  1011. sizeof(ff_aac_spectral_codes[num][0]), \
  1012. size);
  1013. static void aacdec_init(AACContext *ac);
  1014. static av_cold void aac_static_table_init(void)
  1015. {
  1016. AAC_INIT_VLC_STATIC( 0, 304);
  1017. AAC_INIT_VLC_STATIC( 1, 270);
  1018. AAC_INIT_VLC_STATIC( 2, 550);
  1019. AAC_INIT_VLC_STATIC( 3, 300);
  1020. AAC_INIT_VLC_STATIC( 4, 328);
  1021. AAC_INIT_VLC_STATIC( 5, 294);
  1022. AAC_INIT_VLC_STATIC( 6, 306);
  1023. AAC_INIT_VLC_STATIC( 7, 268);
  1024. AAC_INIT_VLC_STATIC( 8, 510);
  1025. AAC_INIT_VLC_STATIC( 9, 366);
  1026. AAC_INIT_VLC_STATIC(10, 462);
  1027. AAC_RENAME(ff_aac_sbr_init)();
  1028. ff_aac_tableinit();
  1029. INIT_VLC_STATIC(&vlc_scalefactors, 7,
  1030. FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
  1031. ff_aac_scalefactor_bits,
  1032. sizeof(ff_aac_scalefactor_bits[0]),
  1033. sizeof(ff_aac_scalefactor_bits[0]),
  1034. ff_aac_scalefactor_code,
  1035. sizeof(ff_aac_scalefactor_code[0]),
  1036. sizeof(ff_aac_scalefactor_code[0]),
  1037. 352);
  1038. // window initialization
  1039. AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_long_1024), 4.0, 1024);
  1040. AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_short_128), 6.0, 128);
  1041. #if !USE_FIXED
  1042. AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_long_960), 4.0, 960);
  1043. AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_short_120), 6.0, 120);
  1044. AAC_RENAME(ff_sine_window_init)(AAC_RENAME(ff_sine_960), 960);
  1045. AAC_RENAME(ff_sine_window_init)(AAC_RENAME(ff_sine_120), 120);
  1046. #endif
  1047. AAC_RENAME(ff_init_ff_sine_windows)(10);
  1048. AAC_RENAME(ff_init_ff_sine_windows)( 9);
  1049. AAC_RENAME(ff_init_ff_sine_windows)( 7);
  1050. AAC_RENAME(ff_cbrt_tableinit)();
  1051. }
  1052. static AVOnce aac_table_init = AV_ONCE_INIT;
  1053. static av_cold int aac_decode_init(AVCodecContext *avctx)
  1054. {
  1055. AACContext *ac = avctx->priv_data;
  1056. int ret;
  1057. if (avctx->sample_rate > 96000)
  1058. return AVERROR_INVALIDDATA;
  1059. ret = ff_thread_once(&aac_table_init, &aac_static_table_init);
  1060. if (ret != 0)
  1061. return AVERROR_UNKNOWN;
  1062. ac->avctx = avctx;
  1063. ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
  1064. aacdec_init(ac);
  1065. #if USE_FIXED
  1066. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  1067. #else
  1068. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  1069. #endif /* USE_FIXED */
  1070. if (avctx->extradata_size > 0) {
  1071. if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
  1072. avctx->extradata,
  1073. avctx->extradata_size * 8LL,
  1074. 1)) < 0)
  1075. return ret;
  1076. } else {
  1077. int sr, i;
  1078. uint8_t layout_map[MAX_ELEM_ID*4][3];
  1079. int layout_map_tags;
  1080. sr = sample_rate_idx(avctx->sample_rate);
  1081. ac->oc[1].m4ac.sampling_index = sr;
  1082. ac->oc[1].m4ac.channels = avctx->channels;
  1083. ac->oc[1].m4ac.sbr = -1;
  1084. ac->oc[1].m4ac.ps = -1;
  1085. for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
  1086. if (ff_mpeg4audio_channels[i] == avctx->channels)
  1087. break;
  1088. if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
  1089. i = 0;
  1090. }
  1091. ac->oc[1].m4ac.chan_config = i;
  1092. if (ac->oc[1].m4ac.chan_config) {
  1093. int ret = set_default_channel_config(avctx, layout_map,
  1094. &layout_map_tags, ac->oc[1].m4ac.chan_config);
  1095. if (!ret)
  1096. output_configure(ac, layout_map, layout_map_tags,
  1097. OC_GLOBAL_HDR, 0);
  1098. else if (avctx->err_recognition & AV_EF_EXPLODE)
  1099. return AVERROR_INVALIDDATA;
  1100. }
  1101. }
  1102. if (avctx->channels > MAX_CHANNELS) {
  1103. av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
  1104. return AVERROR_INVALIDDATA;
  1105. }
  1106. #if USE_FIXED
  1107. ac->fdsp = avpriv_alloc_fixed_dsp(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  1108. #else
  1109. ac->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  1110. #endif /* USE_FIXED */
  1111. if (!ac->fdsp) {
  1112. return AVERROR(ENOMEM);
  1113. }
  1114. ac->random_state = 0x1f2e3d4c;
  1115. AAC_RENAME_32(ff_mdct_init)(&ac->mdct, 11, 1, 1.0 / RANGE15(1024.0));
  1116. AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ld, 10, 1, 1.0 / RANGE15(512.0));
  1117. AAC_RENAME_32(ff_mdct_init)(&ac->mdct_small, 8, 1, 1.0 / RANGE15(128.0));
  1118. AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ltp, 11, 0, RANGE15(-2.0));
  1119. #if !USE_FIXED
  1120. ret = ff_mdct15_init(&ac->mdct120, 1, 3, 1.0f/(16*1024*120*2));
  1121. if (ret < 0)
  1122. return ret;
  1123. ret = ff_mdct15_init(&ac->mdct480, 1, 5, 1.0f/(16*1024*960));
  1124. if (ret < 0)
  1125. return ret;
  1126. ret = ff_mdct15_init(&ac->mdct960, 1, 6, 1.0f/(16*1024*960*2));
  1127. if (ret < 0)
  1128. return ret;
  1129. #endif
  1130. return 0;
  1131. }
  1132. /**
  1133. * Skip data_stream_element; reference: table 4.10.
  1134. */
  1135. static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
  1136. {
  1137. int byte_align = get_bits1(gb);
  1138. int count = get_bits(gb, 8);
  1139. if (count == 255)
  1140. count += get_bits(gb, 8);
  1141. if (byte_align)
  1142. align_get_bits(gb);
  1143. if (get_bits_left(gb) < 8 * count) {
  1144. av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
  1145. return AVERROR_INVALIDDATA;
  1146. }
  1147. skip_bits_long(gb, 8 * count);
  1148. return 0;
  1149. }
  1150. static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
  1151. GetBitContext *gb)
  1152. {
  1153. int sfb;
  1154. if (get_bits1(gb)) {
  1155. ics->predictor_reset_group = get_bits(gb, 5);
  1156. if (ics->predictor_reset_group == 0 ||
  1157. ics->predictor_reset_group > 30) {
  1158. av_log(ac->avctx, AV_LOG_ERROR,
  1159. "Invalid Predictor Reset Group.\n");
  1160. return AVERROR_INVALIDDATA;
  1161. }
  1162. }
  1163. for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
  1164. ics->prediction_used[sfb] = get_bits1(gb);
  1165. }
  1166. return 0;
  1167. }
  1168. /**
  1169. * Decode Long Term Prediction data; reference: table 4.xx.
  1170. */
  1171. static void decode_ltp(LongTermPrediction *ltp,
  1172. GetBitContext *gb, uint8_t max_sfb)
  1173. {
  1174. int sfb;
  1175. ltp->lag = get_bits(gb, 11);
  1176. ltp->coef = ltp_coef[get_bits(gb, 3)];
  1177. for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
  1178. ltp->used[sfb] = get_bits1(gb);
  1179. }
  1180. /**
  1181. * Decode Individual Channel Stream info; reference: table 4.6.
  1182. */
  1183. static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
  1184. GetBitContext *gb)
  1185. {
  1186. const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
  1187. const int aot = m4ac->object_type;
  1188. const int sampling_index = m4ac->sampling_index;
  1189. int ret_fail = AVERROR_INVALIDDATA;
  1190. if (aot != AOT_ER_AAC_ELD) {
  1191. if (get_bits1(gb)) {
  1192. av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
  1193. if (ac->avctx->err_recognition & AV_EF_BITSTREAM)
  1194. return AVERROR_INVALIDDATA;
  1195. }
  1196. ics->window_sequence[1] = ics->window_sequence[0];
  1197. ics->window_sequence[0] = get_bits(gb, 2);
  1198. if (aot == AOT_ER_AAC_LD &&
  1199. ics->window_sequence[0] != ONLY_LONG_SEQUENCE) {
  1200. av_log(ac->avctx, AV_LOG_ERROR,
  1201. "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
  1202. "window sequence %d found.\n", ics->window_sequence[0]);
  1203. ics->window_sequence[0] = ONLY_LONG_SEQUENCE;
  1204. return AVERROR_INVALIDDATA;
  1205. }
  1206. ics->use_kb_window[1] = ics->use_kb_window[0];
  1207. ics->use_kb_window[0] = get_bits1(gb);
  1208. }
  1209. ics->num_window_groups = 1;
  1210. ics->group_len[0] = 1;
  1211. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1212. int i;
  1213. ics->max_sfb = get_bits(gb, 4);
  1214. for (i = 0; i < 7; i++) {
  1215. if (get_bits1(gb)) {
  1216. ics->group_len[ics->num_window_groups - 1]++;
  1217. } else {
  1218. ics->num_window_groups++;
  1219. ics->group_len[ics->num_window_groups - 1] = 1;
  1220. }
  1221. }
  1222. ics->num_windows = 8;
  1223. if (m4ac->frame_length_short) {
  1224. ics->swb_offset = ff_swb_offset_120[sampling_index];
  1225. ics->num_swb = ff_aac_num_swb_120[sampling_index];
  1226. } else {
  1227. ics->swb_offset = ff_swb_offset_128[sampling_index];
  1228. ics->num_swb = ff_aac_num_swb_128[sampling_index];
  1229. }
  1230. ics->tns_max_bands = ff_tns_max_bands_128[sampling_index];
  1231. ics->predictor_present = 0;
  1232. } else {
  1233. ics->max_sfb = get_bits(gb, 6);
  1234. ics->num_windows = 1;
  1235. if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
  1236. if (m4ac->frame_length_short) {
  1237. ics->swb_offset = ff_swb_offset_480[sampling_index];
  1238. ics->num_swb = ff_aac_num_swb_480[sampling_index];
  1239. ics->tns_max_bands = ff_tns_max_bands_480[sampling_index];
  1240. } else {
  1241. ics->swb_offset = ff_swb_offset_512[sampling_index];
  1242. ics->num_swb = ff_aac_num_swb_512[sampling_index];
  1243. ics->tns_max_bands = ff_tns_max_bands_512[sampling_index];
  1244. }
  1245. if (!ics->num_swb || !ics->swb_offset) {
  1246. ret_fail = AVERROR_BUG;
  1247. goto fail;
  1248. }
  1249. } else {
  1250. if (m4ac->frame_length_short) {
  1251. ics->num_swb = ff_aac_num_swb_960[sampling_index];
  1252. ics->swb_offset = ff_swb_offset_960[sampling_index];
  1253. } else {
  1254. ics->num_swb = ff_aac_num_swb_1024[sampling_index];
  1255. ics->swb_offset = ff_swb_offset_1024[sampling_index];
  1256. }
  1257. ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
  1258. }
  1259. if (aot != AOT_ER_AAC_ELD) {
  1260. ics->predictor_present = get_bits1(gb);
  1261. ics->predictor_reset_group = 0;
  1262. }
  1263. if (ics->predictor_present) {
  1264. if (aot == AOT_AAC_MAIN) {
  1265. if (decode_prediction(ac, ics, gb)) {
  1266. goto fail;
  1267. }
  1268. } else if (aot == AOT_AAC_LC ||
  1269. aot == AOT_ER_AAC_LC) {
  1270. av_log(ac->avctx, AV_LOG_ERROR,
  1271. "Prediction is not allowed in AAC-LC.\n");
  1272. goto fail;
  1273. } else {
  1274. if (aot == AOT_ER_AAC_LD) {
  1275. av_log(ac->avctx, AV_LOG_ERROR,
  1276. "LTP in ER AAC LD not yet implemented.\n");
  1277. ret_fail = AVERROR_PATCHWELCOME;
  1278. goto fail;
  1279. }
  1280. if ((ics->ltp.present = get_bits(gb, 1)))
  1281. decode_ltp(&ics->ltp, gb, ics->max_sfb);
  1282. }
  1283. }
  1284. }
  1285. if (ics->max_sfb > ics->num_swb) {
  1286. av_log(ac->avctx, AV_LOG_ERROR,
  1287. "Number of scalefactor bands in group (%d) "
  1288. "exceeds limit (%d).\n",
  1289. ics->max_sfb, ics->num_swb);
  1290. goto fail;
  1291. }
  1292. return 0;
  1293. fail:
  1294. ics->max_sfb = 0;
  1295. return ret_fail;
  1296. }
  1297. /**
  1298. * Decode band types (section_data payload); reference: table 4.46.
  1299. *
  1300. * @param band_type array of the used band type
  1301. * @param band_type_run_end array of the last scalefactor band of a band type run
  1302. *
  1303. * @return Returns error status. 0 - OK, !0 - error
  1304. */
  1305. static int decode_band_types(AACContext *ac, enum BandType band_type[120],
  1306. int band_type_run_end[120], GetBitContext *gb,
  1307. IndividualChannelStream *ics)
  1308. {
  1309. int g, idx = 0;
  1310. const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
  1311. for (g = 0; g < ics->num_window_groups; g++) {
  1312. int k = 0;
  1313. while (k < ics->max_sfb) {
  1314. uint8_t sect_end = k;
  1315. int sect_len_incr;
  1316. int sect_band_type = get_bits(gb, 4);
  1317. if (sect_band_type == 12) {
  1318. av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
  1319. return AVERROR_INVALIDDATA;
  1320. }
  1321. do {
  1322. sect_len_incr = get_bits(gb, bits);
  1323. sect_end += sect_len_incr;
  1324. if (get_bits_left(gb) < 0) {
  1325. av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
  1326. return AVERROR_INVALIDDATA;
  1327. }
  1328. if (sect_end > ics->max_sfb) {
  1329. av_log(ac->avctx, AV_LOG_ERROR,
  1330. "Number of bands (%d) exceeds limit (%d).\n",
  1331. sect_end, ics->max_sfb);
  1332. return AVERROR_INVALIDDATA;
  1333. }
  1334. } while (sect_len_incr == (1 << bits) - 1);
  1335. for (; k < sect_end; k++) {
  1336. band_type [idx] = sect_band_type;
  1337. band_type_run_end[idx++] = sect_end;
  1338. }
  1339. }
  1340. }
  1341. return 0;
  1342. }
  1343. /**
  1344. * Decode scalefactors; reference: table 4.47.
  1345. *
  1346. * @param global_gain first scalefactor value as scalefactors are differentially coded
  1347. * @param band_type array of the used band type
  1348. * @param band_type_run_end array of the last scalefactor band of a band type run
  1349. * @param sf array of scalefactors or intensity stereo positions
  1350. *
  1351. * @return Returns error status. 0 - OK, !0 - error
  1352. */
  1353. static int decode_scalefactors(AACContext *ac, INTFLOAT sf[120], GetBitContext *gb,
  1354. unsigned int global_gain,
  1355. IndividualChannelStream *ics,
  1356. enum BandType band_type[120],
  1357. int band_type_run_end[120])
  1358. {
  1359. int g, i, idx = 0;
  1360. int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
  1361. int clipped_offset;
  1362. int noise_flag = 1;
  1363. for (g = 0; g < ics->num_window_groups; g++) {
  1364. for (i = 0; i < ics->max_sfb;) {
  1365. int run_end = band_type_run_end[idx];
  1366. if (band_type[idx] == ZERO_BT) {
  1367. for (; i < run_end; i++, idx++)
  1368. sf[idx] = FIXR(0.);
  1369. } else if ((band_type[idx] == INTENSITY_BT) ||
  1370. (band_type[idx] == INTENSITY_BT2)) {
  1371. for (; i < run_end; i++, idx++) {
  1372. offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
  1373. clipped_offset = av_clip(offset[2], -155, 100);
  1374. if (offset[2] != clipped_offset) {
  1375. avpriv_request_sample(ac->avctx,
  1376. "If you heard an audible artifact, there may be a bug in the decoder. "
  1377. "Clipped intensity stereo position (%d -> %d)",
  1378. offset[2], clipped_offset);
  1379. }
  1380. #if USE_FIXED
  1381. sf[idx] = 100 - clipped_offset;
  1382. #else
  1383. sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
  1384. #endif /* USE_FIXED */
  1385. }
  1386. } else if (band_type[idx] == NOISE_BT) {
  1387. for (; i < run_end; i++, idx++) {
  1388. if (noise_flag-- > 0)
  1389. offset[1] += get_bits(gb, NOISE_PRE_BITS) - NOISE_PRE;
  1390. else
  1391. offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
  1392. clipped_offset = av_clip(offset[1], -100, 155);
  1393. if (offset[1] != clipped_offset) {
  1394. avpriv_request_sample(ac->avctx,
  1395. "If you heard an audible artifact, there may be a bug in the decoder. "
  1396. "Clipped noise gain (%d -> %d)",
  1397. offset[1], clipped_offset);
  1398. }
  1399. #if USE_FIXED
  1400. sf[idx] = -(100 + clipped_offset);
  1401. #else
  1402. sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
  1403. #endif /* USE_FIXED */
  1404. }
  1405. } else {
  1406. for (; i < run_end; i++, idx++) {
  1407. offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
  1408. if (offset[0] > 255U) {
  1409. av_log(ac->avctx, AV_LOG_ERROR,
  1410. "Scalefactor (%d) out of range.\n", offset[0]);
  1411. return AVERROR_INVALIDDATA;
  1412. }
  1413. #if USE_FIXED
  1414. sf[idx] = -offset[0];
  1415. #else
  1416. sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
  1417. #endif /* USE_FIXED */
  1418. }
  1419. }
  1420. }
  1421. }
  1422. return 0;
  1423. }
  1424. /**
  1425. * Decode pulse data; reference: table 4.7.
  1426. */
  1427. static int decode_pulses(Pulse *pulse, GetBitContext *gb,
  1428. const uint16_t *swb_offset, int num_swb)
  1429. {
  1430. int i, pulse_swb;
  1431. pulse->num_pulse = get_bits(gb, 2) + 1;
  1432. pulse_swb = get_bits(gb, 6);
  1433. if (pulse_swb >= num_swb)
  1434. return -1;
  1435. pulse->pos[0] = swb_offset[pulse_swb];
  1436. pulse->pos[0] += get_bits(gb, 5);
  1437. if (pulse->pos[0] >= swb_offset[num_swb])
  1438. return -1;
  1439. pulse->amp[0] = get_bits(gb, 4);
  1440. for (i = 1; i < pulse->num_pulse; i++) {
  1441. pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
  1442. if (pulse->pos[i] >= swb_offset[num_swb])
  1443. return -1;
  1444. pulse->amp[i] = get_bits(gb, 4);
  1445. }
  1446. return 0;
  1447. }
  1448. /**
  1449. * Decode Temporal Noise Shaping data; reference: table 4.48.
  1450. *
  1451. * @return Returns error status. 0 - OK, !0 - error
  1452. */
  1453. static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
  1454. GetBitContext *gb, const IndividualChannelStream *ics)
  1455. {
  1456. int w, filt, i, coef_len, coef_res, coef_compress;
  1457. const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  1458. const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
  1459. for (w = 0; w < ics->num_windows; w++) {
  1460. if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
  1461. coef_res = get_bits1(gb);
  1462. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  1463. int tmp2_idx;
  1464. tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
  1465. if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
  1466. av_log(ac->avctx, AV_LOG_ERROR,
  1467. "TNS filter order %d is greater than maximum %d.\n",
  1468. tns->order[w][filt], tns_max_order);
  1469. tns->order[w][filt] = 0;
  1470. return AVERROR_INVALIDDATA;
  1471. }
  1472. if (tns->order[w][filt]) {
  1473. tns->direction[w][filt] = get_bits1(gb);
  1474. coef_compress = get_bits1(gb);
  1475. coef_len = coef_res + 3 - coef_compress;
  1476. tmp2_idx = 2 * coef_compress + coef_res;
  1477. for (i = 0; i < tns->order[w][filt]; i++)
  1478. tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
  1479. }
  1480. }
  1481. }
  1482. }
  1483. return 0;
  1484. }
  1485. /**
  1486. * Decode Mid/Side data; reference: table 4.54.
  1487. *
  1488. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  1489. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  1490. * [3] reserved for scalable AAC
  1491. */
  1492. static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
  1493. int ms_present)
  1494. {
  1495. int idx;
  1496. int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
  1497. if (ms_present == 1) {
  1498. for (idx = 0; idx < max_idx; idx++)
  1499. cpe->ms_mask[idx] = get_bits1(gb);
  1500. } else if (ms_present == 2) {
  1501. memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
  1502. }
  1503. }
  1504. /**
  1505. * Decode spectral data; reference: table 4.50.
  1506. * Dequantize and scale spectral data; reference: 4.6.3.3.
  1507. *
  1508. * @param coef array of dequantized, scaled spectral data
  1509. * @param sf array of scalefactors or intensity stereo positions
  1510. * @param pulse_present set if pulses are present
  1511. * @param pulse pointer to pulse data struct
  1512. * @param band_type array of the used band type
  1513. *
  1514. * @return Returns error status. 0 - OK, !0 - error
  1515. */
  1516. static int decode_spectrum_and_dequant(AACContext *ac, INTFLOAT coef[1024],
  1517. GetBitContext *gb, const INTFLOAT sf[120],
  1518. int pulse_present, const Pulse *pulse,
  1519. const IndividualChannelStream *ics,
  1520. enum BandType band_type[120])
  1521. {
  1522. int i, k, g, idx = 0;
  1523. const int c = 1024 / ics->num_windows;
  1524. const uint16_t *offsets = ics->swb_offset;
  1525. INTFLOAT *coef_base = coef;
  1526. for (g = 0; g < ics->num_windows; g++)
  1527. memset(coef + g * 128 + offsets[ics->max_sfb], 0,
  1528. sizeof(INTFLOAT) * (c - offsets[ics->max_sfb]));
  1529. for (g = 0; g < ics->num_window_groups; g++) {
  1530. unsigned g_len = ics->group_len[g];
  1531. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1532. const unsigned cbt_m1 = band_type[idx] - 1;
  1533. INTFLOAT *cfo = coef + offsets[i];
  1534. int off_len = offsets[i + 1] - offsets[i];
  1535. int group;
  1536. if (cbt_m1 >= INTENSITY_BT2 - 1) {
  1537. for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
  1538. memset(cfo, 0, off_len * sizeof(*cfo));
  1539. }
  1540. } else if (cbt_m1 == NOISE_BT - 1) {
  1541. for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
  1542. INTFLOAT band_energy;
  1543. #if USE_FIXED
  1544. for (k = 0; k < off_len; k++) {
  1545. ac->random_state = lcg_random(ac->random_state);
  1546. cfo[k] = ac->random_state >> 3;
  1547. }
  1548. band_energy = ac->fdsp->scalarproduct_fixed(cfo, cfo, off_len);
  1549. band_energy = fixed_sqrt(band_energy, 31);
  1550. noise_scale(cfo, sf[idx], band_energy, off_len);
  1551. #else
  1552. float scale;
  1553. for (k = 0; k < off_len; k++) {
  1554. ac->random_state = lcg_random(ac->random_state);
  1555. cfo[k] = ac->random_state;
  1556. }
  1557. band_energy = ac->fdsp->scalarproduct_float(cfo, cfo, off_len);
  1558. scale = sf[idx] / sqrtf(band_energy);
  1559. ac->fdsp->vector_fmul_scalar(cfo, cfo, scale, off_len);
  1560. #endif /* USE_FIXED */
  1561. }
  1562. } else {
  1563. #if !USE_FIXED
  1564. const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
  1565. #endif /* !USE_FIXED */
  1566. const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
  1567. VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
  1568. OPEN_READER(re, gb);
  1569. switch (cbt_m1 >> 1) {
  1570. case 0:
  1571. for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
  1572. INTFLOAT *cf = cfo;
  1573. int len = off_len;
  1574. do {
  1575. int code;
  1576. unsigned cb_idx;
  1577. UPDATE_CACHE(re, gb);
  1578. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1579. cb_idx = cb_vector_idx[code];
  1580. #if USE_FIXED
  1581. cf = DEC_SQUAD(cf, cb_idx);
  1582. #else
  1583. cf = VMUL4(cf, vq, cb_idx, sf + idx);
  1584. #endif /* USE_FIXED */
  1585. } while (len -= 4);
  1586. }
  1587. break;
  1588. case 1:
  1589. for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
  1590. INTFLOAT *cf = cfo;
  1591. int len = off_len;
  1592. do {
  1593. int code;
  1594. unsigned nnz;
  1595. unsigned cb_idx;
  1596. uint32_t bits;
  1597. UPDATE_CACHE(re, gb);
  1598. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1599. cb_idx = cb_vector_idx[code];
  1600. nnz = cb_idx >> 8 & 15;
  1601. bits = nnz ? GET_CACHE(re, gb) : 0;
  1602. LAST_SKIP_BITS(re, gb, nnz);
  1603. #if USE_FIXED
  1604. cf = DEC_UQUAD(cf, cb_idx, bits);
  1605. #else
  1606. cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
  1607. #endif /* USE_FIXED */
  1608. } while (len -= 4);
  1609. }
  1610. break;
  1611. case 2:
  1612. for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
  1613. INTFLOAT *cf = cfo;
  1614. int len = off_len;
  1615. do {
  1616. int code;
  1617. unsigned cb_idx;
  1618. UPDATE_CACHE(re, gb);
  1619. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1620. cb_idx = cb_vector_idx[code];
  1621. #if USE_FIXED
  1622. cf = DEC_SPAIR(cf, cb_idx);
  1623. #else
  1624. cf = VMUL2(cf, vq, cb_idx, sf + idx);
  1625. #endif /* USE_FIXED */
  1626. } while (len -= 2);
  1627. }
  1628. break;
  1629. case 3:
  1630. case 4:
  1631. for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
  1632. INTFLOAT *cf = cfo;
  1633. int len = off_len;
  1634. do {
  1635. int code;
  1636. unsigned nnz;
  1637. unsigned cb_idx;
  1638. unsigned sign;
  1639. UPDATE_CACHE(re, gb);
  1640. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1641. cb_idx = cb_vector_idx[code];
  1642. nnz = cb_idx >> 8 & 15;
  1643. sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
  1644. LAST_SKIP_BITS(re, gb, nnz);
  1645. #if USE_FIXED
  1646. cf = DEC_UPAIR(cf, cb_idx, sign);
  1647. #else
  1648. cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
  1649. #endif /* USE_FIXED */
  1650. } while (len -= 2);
  1651. }
  1652. break;
  1653. default:
  1654. for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
  1655. #if USE_FIXED
  1656. int *icf = cfo;
  1657. int v;
  1658. #else
  1659. float *cf = cfo;
  1660. uint32_t *icf = (uint32_t *) cf;
  1661. #endif /* USE_FIXED */
  1662. int len = off_len;
  1663. do {
  1664. int code;
  1665. unsigned nzt, nnz;
  1666. unsigned cb_idx;
  1667. uint32_t bits;
  1668. int j;
  1669. UPDATE_CACHE(re, gb);
  1670. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1671. if (!code) {
  1672. *icf++ = 0;
  1673. *icf++ = 0;
  1674. continue;
  1675. }
  1676. cb_idx = cb_vector_idx[code];
  1677. nnz = cb_idx >> 12;
  1678. nzt = cb_idx >> 8;
  1679. bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
  1680. LAST_SKIP_BITS(re, gb, nnz);
  1681. for (j = 0; j < 2; j++) {
  1682. if (nzt & 1<<j) {
  1683. uint32_t b;
  1684. int n;
  1685. /* The total length of escape_sequence must be < 22 bits according
  1686. to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
  1687. UPDATE_CACHE(re, gb);
  1688. b = GET_CACHE(re, gb);
  1689. b = 31 - av_log2(~b);
  1690. if (b > 8) {
  1691. av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
  1692. return AVERROR_INVALIDDATA;
  1693. }
  1694. SKIP_BITS(re, gb, b + 1);
  1695. b += 4;
  1696. n = (1 << b) + SHOW_UBITS(re, gb, b);
  1697. LAST_SKIP_BITS(re, gb, b);
  1698. #if USE_FIXED
  1699. v = n;
  1700. if (bits & 1U<<31)
  1701. v = -v;
  1702. *icf++ = v;
  1703. #else
  1704. *icf++ = ff_cbrt_tab[n] | (bits & 1U<<31);
  1705. #endif /* USE_FIXED */
  1706. bits <<= 1;
  1707. } else {
  1708. #if USE_FIXED
  1709. v = cb_idx & 15;
  1710. if (bits & 1U<<31)
  1711. v = -v;
  1712. *icf++ = v;
  1713. #else
  1714. unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
  1715. *icf++ = (bits & 1U<<31) | v;
  1716. #endif /* USE_FIXED */
  1717. bits <<= !!v;
  1718. }
  1719. cb_idx >>= 4;
  1720. }
  1721. } while (len -= 2);
  1722. #if !USE_FIXED
  1723. ac->fdsp->vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
  1724. #endif /* !USE_FIXED */
  1725. }
  1726. }
  1727. CLOSE_READER(re, gb);
  1728. }
  1729. }
  1730. coef += g_len << 7;
  1731. }
  1732. if (pulse_present) {
  1733. idx = 0;
  1734. for (i = 0; i < pulse->num_pulse; i++) {
  1735. INTFLOAT co = coef_base[ pulse->pos[i] ];
  1736. while (offsets[idx + 1] <= pulse->pos[i])
  1737. idx++;
  1738. if (band_type[idx] != NOISE_BT && sf[idx]) {
  1739. INTFLOAT ico = -pulse->amp[i];
  1740. #if USE_FIXED
  1741. if (co) {
  1742. ico = co + (co > 0 ? -ico : ico);
  1743. }
  1744. coef_base[ pulse->pos[i] ] = ico;
  1745. #else
  1746. if (co) {
  1747. co /= sf[idx];
  1748. ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
  1749. }
  1750. coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
  1751. #endif /* USE_FIXED */
  1752. }
  1753. }
  1754. }
  1755. #if USE_FIXED
  1756. coef = coef_base;
  1757. idx = 0;
  1758. for (g = 0; g < ics->num_window_groups; g++) {
  1759. unsigned g_len = ics->group_len[g];
  1760. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1761. const unsigned cbt_m1 = band_type[idx] - 1;
  1762. int *cfo = coef + offsets[i];
  1763. int off_len = offsets[i + 1] - offsets[i];
  1764. int group;
  1765. if (cbt_m1 < NOISE_BT - 1) {
  1766. for (group = 0; group < (int)g_len; group++, cfo+=128) {
  1767. ac->vector_pow43(cfo, off_len);
  1768. ac->subband_scale(cfo, cfo, sf[idx], 34, off_len);
  1769. }
  1770. }
  1771. }
  1772. coef += g_len << 7;
  1773. }
  1774. #endif /* USE_FIXED */
  1775. return 0;
  1776. }
  1777. /**
  1778. * Apply AAC-Main style frequency domain prediction.
  1779. */
  1780. static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
  1781. {
  1782. int sfb, k;
  1783. if (!sce->ics.predictor_initialized) {
  1784. reset_all_predictors(sce->predictor_state);
  1785. sce->ics.predictor_initialized = 1;
  1786. }
  1787. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  1788. for (sfb = 0;
  1789. sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
  1790. sfb++) {
  1791. for (k = sce->ics.swb_offset[sfb];
  1792. k < sce->ics.swb_offset[sfb + 1];
  1793. k++) {
  1794. predict(&sce->predictor_state[k], &sce->coeffs[k],
  1795. sce->ics.predictor_present &&
  1796. sce->ics.prediction_used[sfb]);
  1797. }
  1798. }
  1799. if (sce->ics.predictor_reset_group)
  1800. reset_predictor_group(sce->predictor_state,
  1801. sce->ics.predictor_reset_group);
  1802. } else
  1803. reset_all_predictors(sce->predictor_state);
  1804. }
  1805. static void decode_gain_control(SingleChannelElement * sce, GetBitContext * gb)
  1806. {
  1807. // wd_num, wd_test, aloc_size
  1808. static const uint8_t gain_mode[4][3] = {
  1809. {1, 0, 5}, // ONLY_LONG_SEQUENCE = 0,
  1810. {2, 1, 2}, // LONG_START_SEQUENCE,
  1811. {8, 0, 2}, // EIGHT_SHORT_SEQUENCE,
  1812. {2, 1, 5}, // LONG_STOP_SEQUENCE
  1813. };
  1814. const int mode = sce->ics.window_sequence[0];
  1815. uint8_t bd, wd, ad;
  1816. // FIXME: Store the gain control data on |sce| and do something with it.
  1817. uint8_t max_band = get_bits(gb, 2);
  1818. for (bd = 0; bd < max_band; bd++) {
  1819. for (wd = 0; wd < gain_mode[mode][0]; wd++) {
  1820. uint8_t adjust_num = get_bits(gb, 3);
  1821. for (ad = 0; ad < adjust_num; ad++) {
  1822. skip_bits(gb, 4 + ((wd == 0 && gain_mode[mode][1])
  1823. ? 4
  1824. : gain_mode[mode][2]));
  1825. }
  1826. }
  1827. }
  1828. }
  1829. /**
  1830. * Decode an individual_channel_stream payload; reference: table 4.44.
  1831. *
  1832. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  1833. * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
  1834. *
  1835. * @return Returns error status. 0 - OK, !0 - error
  1836. */
  1837. static int decode_ics(AACContext *ac, SingleChannelElement *sce,
  1838. GetBitContext *gb, int common_window, int scale_flag)
  1839. {
  1840. Pulse pulse;
  1841. TemporalNoiseShaping *tns = &sce->tns;
  1842. IndividualChannelStream *ics = &sce->ics;
  1843. INTFLOAT *out = sce->coeffs;
  1844. int global_gain, eld_syntax, er_syntax, pulse_present = 0;
  1845. int ret;
  1846. eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
  1847. er_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
  1848. ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
  1849. ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
  1850. ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
  1851. /* This assignment is to silence a GCC warning about the variable being used
  1852. * uninitialized when in fact it always is.
  1853. */
  1854. pulse.num_pulse = 0;
  1855. global_gain = get_bits(gb, 8);
  1856. if (!common_window && !scale_flag) {
  1857. ret = decode_ics_info(ac, ics, gb);
  1858. if (ret < 0)
  1859. goto fail;
  1860. }
  1861. if ((ret = decode_band_types(ac, sce->band_type,
  1862. sce->band_type_run_end, gb, ics)) < 0)
  1863. goto fail;
  1864. if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
  1865. sce->band_type, sce->band_type_run_end)) < 0)
  1866. goto fail;
  1867. pulse_present = 0;
  1868. if (!scale_flag) {
  1869. if (!eld_syntax && (pulse_present = get_bits1(gb))) {
  1870. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1871. av_log(ac->avctx, AV_LOG_ERROR,
  1872. "Pulse tool not allowed in eight short sequence.\n");
  1873. ret = AVERROR_INVALIDDATA;
  1874. goto fail;
  1875. }
  1876. if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
  1877. av_log(ac->avctx, AV_LOG_ERROR,
  1878. "Pulse data corrupt or invalid.\n");
  1879. ret = AVERROR_INVALIDDATA;
  1880. goto fail;
  1881. }
  1882. }
  1883. tns->present = get_bits1(gb);
  1884. if (tns->present && !er_syntax) {
  1885. ret = decode_tns(ac, tns, gb, ics);
  1886. if (ret < 0)
  1887. goto fail;
  1888. }
  1889. if (!eld_syntax && get_bits1(gb)) {
  1890. decode_gain_control(sce, gb);
  1891. if (!ac->warned_gain_control) {
  1892. avpriv_report_missing_feature(ac->avctx, "Gain control");
  1893. ac->warned_gain_control = 1;
  1894. }
  1895. }
  1896. // I see no textual basis in the spec for this occurring after SSR gain
  1897. // control, but this is what both reference and real implmentations do
  1898. if (tns->present && er_syntax) {
  1899. ret = decode_tns(ac, tns, gb, ics);
  1900. if (ret < 0)
  1901. goto fail;
  1902. }
  1903. }
  1904. ret = decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
  1905. &pulse, ics, sce->band_type);
  1906. if (ret < 0)
  1907. goto fail;
  1908. if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
  1909. apply_prediction(ac, sce);
  1910. return 0;
  1911. fail:
  1912. tns->present = 0;
  1913. return ret;
  1914. }
  1915. /**
  1916. * Mid/Side stereo decoding; reference: 4.6.8.1.3.
  1917. */
  1918. static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
  1919. {
  1920. const IndividualChannelStream *ics = &cpe->ch[0].ics;
  1921. INTFLOAT *ch0 = cpe->ch[0].coeffs;
  1922. INTFLOAT *ch1 = cpe->ch[1].coeffs;
  1923. int g, i, group, idx = 0;
  1924. const uint16_t *offsets = ics->swb_offset;
  1925. for (g = 0; g < ics->num_window_groups; g++) {
  1926. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1927. if (cpe->ms_mask[idx] &&
  1928. cpe->ch[0].band_type[idx] < NOISE_BT &&
  1929. cpe->ch[1].band_type[idx] < NOISE_BT) {
  1930. #if USE_FIXED
  1931. for (group = 0; group < ics->group_len[g]; group++) {
  1932. ac->fdsp->butterflies_fixed(ch0 + group * 128 + offsets[i],
  1933. ch1 + group * 128 + offsets[i],
  1934. offsets[i+1] - offsets[i]);
  1935. #else
  1936. for (group = 0; group < ics->group_len[g]; group++) {
  1937. ac->fdsp->butterflies_float(ch0 + group * 128 + offsets[i],
  1938. ch1 + group * 128 + offsets[i],
  1939. offsets[i+1] - offsets[i]);
  1940. #endif /* USE_FIXED */
  1941. }
  1942. }
  1943. }
  1944. ch0 += ics->group_len[g] * 128;
  1945. ch1 += ics->group_len[g] * 128;
  1946. }
  1947. }
  1948. /**
  1949. * intensity stereo decoding; reference: 4.6.8.2.3
  1950. *
  1951. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  1952. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  1953. * [3] reserved for scalable AAC
  1954. */
  1955. static void apply_intensity_stereo(AACContext *ac,
  1956. ChannelElement *cpe, int ms_present)
  1957. {
  1958. const IndividualChannelStream *ics = &cpe->ch[1].ics;
  1959. SingleChannelElement *sce1 = &cpe->ch[1];
  1960. INTFLOAT *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
  1961. const uint16_t *offsets = ics->swb_offset;
  1962. int g, group, i, idx = 0;
  1963. int c;
  1964. INTFLOAT scale;
  1965. for (g = 0; g < ics->num_window_groups; g++) {
  1966. for (i = 0; i < ics->max_sfb;) {
  1967. if (sce1->band_type[idx] == INTENSITY_BT ||
  1968. sce1->band_type[idx] == INTENSITY_BT2) {
  1969. const int bt_run_end = sce1->band_type_run_end[idx];
  1970. for (; i < bt_run_end; i++, idx++) {
  1971. c = -1 + 2 * (sce1->band_type[idx] - 14);
  1972. if (ms_present)
  1973. c *= 1 - 2 * cpe->ms_mask[idx];
  1974. scale = c * sce1->sf[idx];
  1975. for (group = 0; group < ics->group_len[g]; group++)
  1976. #if USE_FIXED
  1977. ac->subband_scale(coef1 + group * 128 + offsets[i],
  1978. coef0 + group * 128 + offsets[i],
  1979. scale,
  1980. 23,
  1981. offsets[i + 1] - offsets[i]);
  1982. #else
  1983. ac->fdsp->vector_fmul_scalar(coef1 + group * 128 + offsets[i],
  1984. coef0 + group * 128 + offsets[i],
  1985. scale,
  1986. offsets[i + 1] - offsets[i]);
  1987. #endif /* USE_FIXED */
  1988. }
  1989. } else {
  1990. int bt_run_end = sce1->band_type_run_end[idx];
  1991. idx += bt_run_end - i;
  1992. i = bt_run_end;
  1993. }
  1994. }
  1995. coef0 += ics->group_len[g] * 128;
  1996. coef1 += ics->group_len[g] * 128;
  1997. }
  1998. }
  1999. /**
  2000. * Decode a channel_pair_element; reference: table 4.4.
  2001. *
  2002. * @return Returns error status. 0 - OK, !0 - error
  2003. */
  2004. static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
  2005. {
  2006. int i, ret, common_window, ms_present = 0;
  2007. int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
  2008. common_window = eld_syntax || get_bits1(gb);
  2009. if (common_window) {
  2010. if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
  2011. return AVERROR_INVALIDDATA;
  2012. i = cpe->ch[1].ics.use_kb_window[0];
  2013. cpe->ch[1].ics = cpe->ch[0].ics;
  2014. cpe->ch[1].ics.use_kb_window[1] = i;
  2015. if (cpe->ch[1].ics.predictor_present &&
  2016. (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
  2017. if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
  2018. decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
  2019. ms_present = get_bits(gb, 2);
  2020. if (ms_present == 3) {
  2021. av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
  2022. return AVERROR_INVALIDDATA;
  2023. } else if (ms_present)
  2024. decode_mid_side_stereo(cpe, gb, ms_present);
  2025. }
  2026. if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
  2027. return ret;
  2028. if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
  2029. return ret;
  2030. if (common_window) {
  2031. if (ms_present)
  2032. apply_mid_side_stereo(ac, cpe);
  2033. if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
  2034. apply_prediction(ac, &cpe->ch[0]);
  2035. apply_prediction(ac, &cpe->ch[1]);
  2036. }
  2037. }
  2038. apply_intensity_stereo(ac, cpe, ms_present);
  2039. return 0;
  2040. }
  2041. static const float cce_scale[] = {
  2042. 1.09050773266525765921, //2^(1/8)
  2043. 1.18920711500272106672, //2^(1/4)
  2044. M_SQRT2,
  2045. 2,
  2046. };
  2047. /**
  2048. * Decode coupling_channel_element; reference: table 4.8.
  2049. *
  2050. * @return Returns error status. 0 - OK, !0 - error
  2051. */
  2052. static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
  2053. {
  2054. int num_gain = 0;
  2055. int c, g, sfb, ret;
  2056. int sign;
  2057. INTFLOAT scale;
  2058. SingleChannelElement *sce = &che->ch[0];
  2059. ChannelCoupling *coup = &che->coup;
  2060. coup->coupling_point = 2 * get_bits1(gb);
  2061. coup->num_coupled = get_bits(gb, 3);
  2062. for (c = 0; c <= coup->num_coupled; c++) {
  2063. num_gain++;
  2064. coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
  2065. coup->id_select[c] = get_bits(gb, 4);
  2066. if (coup->type[c] == TYPE_CPE) {
  2067. coup->ch_select[c] = get_bits(gb, 2);
  2068. if (coup->ch_select[c] == 3)
  2069. num_gain++;
  2070. } else
  2071. coup->ch_select[c] = 2;
  2072. }
  2073. coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
  2074. sign = get_bits(gb, 1);
  2075. #if USE_FIXED
  2076. scale = get_bits(gb, 2);
  2077. #else
  2078. scale = cce_scale[get_bits(gb, 2)];
  2079. #endif
  2080. if ((ret = decode_ics(ac, sce, gb, 0, 0)))
  2081. return ret;
  2082. for (c = 0; c < num_gain; c++) {
  2083. int idx = 0;
  2084. int cge = 1;
  2085. int gain = 0;
  2086. INTFLOAT gain_cache = FIXR10(1.);
  2087. if (c) {
  2088. cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
  2089. gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
  2090. gain_cache = GET_GAIN(scale, gain);
  2091. #if USE_FIXED
  2092. if ((abs(gain_cache)-1024) >> 3 > 30)
  2093. return AVERROR(ERANGE);
  2094. #endif
  2095. }
  2096. if (coup->coupling_point == AFTER_IMDCT) {
  2097. coup->gain[c][0] = gain_cache;
  2098. } else {
  2099. for (g = 0; g < sce->ics.num_window_groups; g++) {
  2100. for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
  2101. if (sce->band_type[idx] != ZERO_BT) {
  2102. if (!cge) {
  2103. int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  2104. if (t) {
  2105. int s = 1;
  2106. t = gain += t;
  2107. if (sign) {
  2108. s -= 2 * (t & 0x1);
  2109. t >>= 1;
  2110. }
  2111. gain_cache = GET_GAIN(scale, t) * s;
  2112. #if USE_FIXED
  2113. if ((abs(gain_cache)-1024) >> 3 > 30)
  2114. return AVERROR(ERANGE);
  2115. #endif
  2116. }
  2117. }
  2118. coup->gain[c][idx] = gain_cache;
  2119. }
  2120. }
  2121. }
  2122. }
  2123. }
  2124. return 0;
  2125. }
  2126. /**
  2127. * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
  2128. *
  2129. * @return Returns number of bytes consumed.
  2130. */
  2131. static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
  2132. GetBitContext *gb)
  2133. {
  2134. int i;
  2135. int num_excl_chan = 0;
  2136. do {
  2137. for (i = 0; i < 7; i++)
  2138. che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
  2139. } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
  2140. return num_excl_chan / 7;
  2141. }
  2142. /**
  2143. * Decode dynamic range information; reference: table 4.52.
  2144. *
  2145. * @return Returns number of bytes consumed.
  2146. */
  2147. static int decode_dynamic_range(DynamicRangeControl *che_drc,
  2148. GetBitContext *gb)
  2149. {
  2150. int n = 1;
  2151. int drc_num_bands = 1;
  2152. int i;
  2153. /* pce_tag_present? */
  2154. if (get_bits1(gb)) {
  2155. che_drc->pce_instance_tag = get_bits(gb, 4);
  2156. skip_bits(gb, 4); // tag_reserved_bits
  2157. n++;
  2158. }
  2159. /* excluded_chns_present? */
  2160. if (get_bits1(gb)) {
  2161. n += decode_drc_channel_exclusions(che_drc, gb);
  2162. }
  2163. /* drc_bands_present? */
  2164. if (get_bits1(gb)) {
  2165. che_drc->band_incr = get_bits(gb, 4);
  2166. che_drc->interpolation_scheme = get_bits(gb, 4);
  2167. n++;
  2168. drc_num_bands += che_drc->band_incr;
  2169. for (i = 0; i < drc_num_bands; i++) {
  2170. che_drc->band_top[i] = get_bits(gb, 8);
  2171. n++;
  2172. }
  2173. }
  2174. /* prog_ref_level_present? */
  2175. if (get_bits1(gb)) {
  2176. che_drc->prog_ref_level = get_bits(gb, 7);
  2177. skip_bits1(gb); // prog_ref_level_reserved_bits
  2178. n++;
  2179. }
  2180. for (i = 0; i < drc_num_bands; i++) {
  2181. che_drc->dyn_rng_sgn[i] = get_bits1(gb);
  2182. che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
  2183. n++;
  2184. }
  2185. return n;
  2186. }
  2187. static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
  2188. uint8_t buf[256];
  2189. int i, major, minor;
  2190. if (len < 13+7*8)
  2191. goto unknown;
  2192. get_bits(gb, 13); len -= 13;
  2193. for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
  2194. buf[i] = get_bits(gb, 8);
  2195. buf[i] = 0;
  2196. if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
  2197. av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
  2198. if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
  2199. ac->avctx->internal->skip_samples = 1024;
  2200. }
  2201. unknown:
  2202. skip_bits_long(gb, len);
  2203. return 0;
  2204. }
  2205. /**
  2206. * Decode extension data (incomplete); reference: table 4.51.
  2207. *
  2208. * @param cnt length of TYPE_FIL syntactic element in bytes
  2209. *
  2210. * @return Returns number of bytes consumed
  2211. */
  2212. static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
  2213. ChannelElement *che, enum RawDataBlockType elem_type)
  2214. {
  2215. int crc_flag = 0;
  2216. int res = cnt;
  2217. int type = get_bits(gb, 4);
  2218. if (ac->avctx->debug & FF_DEBUG_STARTCODE)
  2219. av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
  2220. switch (type) { // extension type
  2221. case EXT_SBR_DATA_CRC:
  2222. crc_flag++;
  2223. case EXT_SBR_DATA:
  2224. if (!che) {
  2225. av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
  2226. return res;
  2227. } else if (ac->oc[1].m4ac.frame_length_short) {
  2228. if (!ac->warned_960_sbr)
  2229. avpriv_report_missing_feature(ac->avctx,
  2230. "SBR with 960 frame length");
  2231. ac->warned_960_sbr = 1;
  2232. skip_bits_long(gb, 8 * cnt - 4);
  2233. return res;
  2234. } else if (!ac->oc[1].m4ac.sbr) {
  2235. av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
  2236. skip_bits_long(gb, 8 * cnt - 4);
  2237. return res;
  2238. } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
  2239. av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
  2240. skip_bits_long(gb, 8 * cnt - 4);
  2241. return res;
  2242. } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
  2243. ac->oc[1].m4ac.sbr = 1;
  2244. ac->oc[1].m4ac.ps = 1;
  2245. ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
  2246. output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
  2247. ac->oc[1].status, 1);
  2248. } else {
  2249. ac->oc[1].m4ac.sbr = 1;
  2250. ac->avctx->profile = FF_PROFILE_AAC_HE;
  2251. }
  2252. res = AAC_RENAME(ff_decode_sbr_extension)(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
  2253. break;
  2254. case EXT_DYNAMIC_RANGE:
  2255. res = decode_dynamic_range(&ac->che_drc, gb);
  2256. break;
  2257. case EXT_FILL:
  2258. decode_fill(ac, gb, 8 * cnt - 4);
  2259. break;
  2260. case EXT_FILL_DATA:
  2261. case EXT_DATA_ELEMENT:
  2262. default:
  2263. skip_bits_long(gb, 8 * cnt - 4);
  2264. break;
  2265. };
  2266. return res;
  2267. }
  2268. /**
  2269. * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
  2270. *
  2271. * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
  2272. * @param coef spectral coefficients
  2273. */
  2274. static void apply_tns(INTFLOAT coef_param[1024], TemporalNoiseShaping *tns,
  2275. IndividualChannelStream *ics, int decode)
  2276. {
  2277. const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
  2278. int w, filt, m, i;
  2279. int bottom, top, order, start, end, size, inc;
  2280. INTFLOAT lpc[TNS_MAX_ORDER];
  2281. INTFLOAT tmp[TNS_MAX_ORDER+1];
  2282. UINTFLOAT *coef = coef_param;
  2283. if(!mmm)
  2284. return;
  2285. for (w = 0; w < ics->num_windows; w++) {
  2286. bottom = ics->num_swb;
  2287. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  2288. top = bottom;
  2289. bottom = FFMAX(0, top - tns->length[w][filt]);
  2290. order = tns->order[w][filt];
  2291. if (order == 0)
  2292. continue;
  2293. // tns_decode_coef
  2294. AAC_RENAME(compute_lpc_coefs)(tns->coef[w][filt], order, lpc, 0, 0, 0);
  2295. start = ics->swb_offset[FFMIN(bottom, mmm)];
  2296. end = ics->swb_offset[FFMIN( top, mmm)];
  2297. if ((size = end - start) <= 0)
  2298. continue;
  2299. if (tns->direction[w][filt]) {
  2300. inc = -1;
  2301. start = end - 1;
  2302. } else {
  2303. inc = 1;
  2304. }
  2305. start += w * 128;
  2306. if (decode) {
  2307. // ar filter
  2308. for (m = 0; m < size; m++, start += inc)
  2309. for (i = 1; i <= FFMIN(m, order); i++)
  2310. coef[start] -= AAC_MUL26((INTFLOAT)coef[start - i * inc], lpc[i - 1]);
  2311. } else {
  2312. // ma filter
  2313. for (m = 0; m < size; m++, start += inc) {
  2314. tmp[0] = coef[start];
  2315. for (i = 1; i <= FFMIN(m, order); i++)
  2316. coef[start] += AAC_MUL26(tmp[i], lpc[i - 1]);
  2317. for (i = order; i > 0; i--)
  2318. tmp[i] = tmp[i - 1];
  2319. }
  2320. }
  2321. }
  2322. }
  2323. }
  2324. /**
  2325. * Apply windowing and MDCT to obtain the spectral
  2326. * coefficient from the predicted sample by LTP.
  2327. */
  2328. static void windowing_and_mdct_ltp(AACContext *ac, INTFLOAT *out,
  2329. INTFLOAT *in, IndividualChannelStream *ics)
  2330. {
  2331. const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
  2332. const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
  2333. const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
  2334. const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
  2335. if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
  2336. ac->fdsp->vector_fmul(in, in, lwindow_prev, 1024);
  2337. } else {
  2338. memset(in, 0, 448 * sizeof(*in));
  2339. ac->fdsp->vector_fmul(in + 448, in + 448, swindow_prev, 128);
  2340. }
  2341. if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
  2342. ac->fdsp->vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
  2343. } else {
  2344. ac->fdsp->vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
  2345. memset(in + 1024 + 576, 0, 448 * sizeof(*in));
  2346. }
  2347. ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
  2348. }
  2349. /**
  2350. * Apply the long term prediction
  2351. */
  2352. static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
  2353. {
  2354. const LongTermPrediction *ltp = &sce->ics.ltp;
  2355. const uint16_t *offsets = sce->ics.swb_offset;
  2356. int i, sfb;
  2357. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  2358. INTFLOAT *predTime = sce->ret;
  2359. INTFLOAT *predFreq = ac->buf_mdct;
  2360. int16_t num_samples = 2048;
  2361. if (ltp->lag < 1024)
  2362. num_samples = ltp->lag + 1024;
  2363. for (i = 0; i < num_samples; i++)
  2364. predTime[i] = AAC_MUL30(sce->ltp_state[i + 2048 - ltp->lag], ltp->coef);
  2365. memset(&predTime[i], 0, (2048 - i) * sizeof(*predTime));
  2366. ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
  2367. if (sce->tns.present)
  2368. ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
  2369. for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
  2370. if (ltp->used[sfb])
  2371. for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
  2372. sce->coeffs[i] += (UINTFLOAT)predFreq[i];
  2373. }
  2374. }
  2375. /**
  2376. * Update the LTP buffer for next frame
  2377. */
  2378. static void update_ltp(AACContext *ac, SingleChannelElement *sce)
  2379. {
  2380. IndividualChannelStream *ics = &sce->ics;
  2381. INTFLOAT *saved = sce->saved;
  2382. INTFLOAT *saved_ltp = sce->coeffs;
  2383. const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
  2384. const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
  2385. int i;
  2386. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2387. memcpy(saved_ltp, saved, 512 * sizeof(*saved_ltp));
  2388. memset(saved_ltp + 576, 0, 448 * sizeof(*saved_ltp));
  2389. ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
  2390. for (i = 0; i < 64; i++)
  2391. saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
  2392. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  2393. memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(*saved_ltp));
  2394. memset(saved_ltp + 576, 0, 448 * sizeof(*saved_ltp));
  2395. ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
  2396. for (i = 0; i < 64; i++)
  2397. saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
  2398. } else { // LONG_STOP or ONLY_LONG
  2399. ac->fdsp->vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
  2400. for (i = 0; i < 512; i++)
  2401. saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], lwindow[511 - i]);
  2402. }
  2403. memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
  2404. memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
  2405. memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
  2406. }
  2407. /**
  2408. * Conduct IMDCT and windowing.
  2409. */
  2410. static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
  2411. {
  2412. IndividualChannelStream *ics = &sce->ics;
  2413. INTFLOAT *in = sce->coeffs;
  2414. INTFLOAT *out = sce->ret;
  2415. INTFLOAT *saved = sce->saved;
  2416. const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
  2417. const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
  2418. const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
  2419. INTFLOAT *buf = ac->buf_mdct;
  2420. INTFLOAT *temp = ac->temp;
  2421. int i;
  2422. // imdct
  2423. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2424. for (i = 0; i < 1024; i += 128)
  2425. ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
  2426. } else {
  2427. ac->mdct.imdct_half(&ac->mdct, buf, in);
  2428. #if USE_FIXED
  2429. for (i=0; i<1024; i++)
  2430. buf[i] = (buf[i] + 4LL) >> 3;
  2431. #endif /* USE_FIXED */
  2432. }
  2433. /* window overlapping
  2434. * NOTE: To simplify the overlapping code, all 'meaningless' short to long
  2435. * and long to short transitions are considered to be short to short
  2436. * transitions. This leaves just two cases (long to long and short to short)
  2437. * with a little special sauce for EIGHT_SHORT_SEQUENCE.
  2438. */
  2439. if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
  2440. (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
  2441. ac->fdsp->vector_fmul_window( out, saved, buf, lwindow_prev, 512);
  2442. } else {
  2443. memcpy( out, saved, 448 * sizeof(*out));
  2444. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2445. ac->fdsp->vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
  2446. ac->fdsp->vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
  2447. ac->fdsp->vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
  2448. ac->fdsp->vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
  2449. ac->fdsp->vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
  2450. memcpy( out + 448 + 4*128, temp, 64 * sizeof(*out));
  2451. } else {
  2452. ac->fdsp->vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
  2453. memcpy( out + 576, buf + 64, 448 * sizeof(*out));
  2454. }
  2455. }
  2456. // buffer update
  2457. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2458. memcpy( saved, temp + 64, 64 * sizeof(*saved));
  2459. ac->fdsp->vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
  2460. ac->fdsp->vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
  2461. ac->fdsp->vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
  2462. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(*saved));
  2463. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  2464. memcpy( saved, buf + 512, 448 * sizeof(*saved));
  2465. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(*saved));
  2466. } else { // LONG_STOP or ONLY_LONG
  2467. memcpy( saved, buf + 512, 512 * sizeof(*saved));
  2468. }
  2469. }
  2470. /**
  2471. * Conduct IMDCT and windowing.
  2472. */
  2473. static void imdct_and_windowing_960(AACContext *ac, SingleChannelElement *sce)
  2474. {
  2475. #if !USE_FIXED
  2476. IndividualChannelStream *ics = &sce->ics;
  2477. INTFLOAT *in = sce->coeffs;
  2478. INTFLOAT *out = sce->ret;
  2479. INTFLOAT *saved = sce->saved;
  2480. const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_120) : AAC_RENAME(ff_sine_120);
  2481. const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_960) : AAC_RENAME(ff_sine_960);
  2482. const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_120) : AAC_RENAME(ff_sine_120);
  2483. INTFLOAT *buf = ac->buf_mdct;
  2484. INTFLOAT *temp = ac->temp;
  2485. int i;
  2486. // imdct
  2487. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2488. for (i = 0; i < 8; i++)
  2489. ac->mdct120->imdct_half(ac->mdct120, buf + i * 120, in + i * 128, 1);
  2490. } else {
  2491. ac->mdct960->imdct_half(ac->mdct960, buf, in, 1);
  2492. }
  2493. /* window overlapping
  2494. * NOTE: To simplify the overlapping code, all 'meaningless' short to long
  2495. * and long to short transitions are considered to be short to short
  2496. * transitions. This leaves just two cases (long to long and short to short)
  2497. * with a little special sauce for EIGHT_SHORT_SEQUENCE.
  2498. */
  2499. if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
  2500. (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
  2501. ac->fdsp->vector_fmul_window( out, saved, buf, lwindow_prev, 480);
  2502. } else {
  2503. memcpy( out, saved, 420 * sizeof(*out));
  2504. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2505. ac->fdsp->vector_fmul_window(out + 420 + 0*120, saved + 420, buf + 0*120, swindow_prev, 60);
  2506. ac->fdsp->vector_fmul_window(out + 420 + 1*120, buf + 0*120 + 60, buf + 1*120, swindow, 60);
  2507. ac->fdsp->vector_fmul_window(out + 420 + 2*120, buf + 1*120 + 60, buf + 2*120, swindow, 60);
  2508. ac->fdsp->vector_fmul_window(out + 420 + 3*120, buf + 2*120 + 60, buf + 3*120, swindow, 60);
  2509. ac->fdsp->vector_fmul_window(temp, buf + 3*120 + 60, buf + 4*120, swindow, 60);
  2510. memcpy( out + 420 + 4*120, temp, 60 * sizeof(*out));
  2511. } else {
  2512. ac->fdsp->vector_fmul_window(out + 420, saved + 420, buf, swindow_prev, 60);
  2513. memcpy( out + 540, buf + 60, 420 * sizeof(*out));
  2514. }
  2515. }
  2516. // buffer update
  2517. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2518. memcpy( saved, temp + 60, 60 * sizeof(*saved));
  2519. ac->fdsp->vector_fmul_window(saved + 60, buf + 4*120 + 60, buf + 5*120, swindow, 60);
  2520. ac->fdsp->vector_fmul_window(saved + 180, buf + 5*120 + 60, buf + 6*120, swindow, 60);
  2521. ac->fdsp->vector_fmul_window(saved + 300, buf + 6*120 + 60, buf + 7*120, swindow, 60);
  2522. memcpy( saved + 420, buf + 7*120 + 60, 60 * sizeof(*saved));
  2523. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  2524. memcpy( saved, buf + 480, 420 * sizeof(*saved));
  2525. memcpy( saved + 420, buf + 7*120 + 60, 60 * sizeof(*saved));
  2526. } else { // LONG_STOP or ONLY_LONG
  2527. memcpy( saved, buf + 480, 480 * sizeof(*saved));
  2528. }
  2529. #endif
  2530. }
  2531. static void imdct_and_windowing_ld(AACContext *ac, SingleChannelElement *sce)
  2532. {
  2533. IndividualChannelStream *ics = &sce->ics;
  2534. INTFLOAT *in = sce->coeffs;
  2535. INTFLOAT *out = sce->ret;
  2536. INTFLOAT *saved = sce->saved;
  2537. INTFLOAT *buf = ac->buf_mdct;
  2538. #if USE_FIXED
  2539. int i;
  2540. #endif /* USE_FIXED */
  2541. // imdct
  2542. ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
  2543. #if USE_FIXED
  2544. for (i = 0; i < 1024; i++)
  2545. buf[i] = (buf[i] + 2) >> 2;
  2546. #endif /* USE_FIXED */
  2547. // window overlapping
  2548. if (ics->use_kb_window[1]) {
  2549. // AAC LD uses a low overlap sine window instead of a KBD window
  2550. memcpy(out, saved, 192 * sizeof(*out));
  2551. ac->fdsp->vector_fmul_window(out + 192, saved + 192, buf, AAC_RENAME(ff_sine_128), 64);
  2552. memcpy( out + 320, buf + 64, 192 * sizeof(*out));
  2553. } else {
  2554. ac->fdsp->vector_fmul_window(out, saved, buf, AAC_RENAME(ff_sine_512), 256);
  2555. }
  2556. // buffer update
  2557. memcpy(saved, buf + 256, 256 * sizeof(*saved));
  2558. }
  2559. static void imdct_and_windowing_eld(AACContext *ac, SingleChannelElement *sce)
  2560. {
  2561. INTFLOAT *in = sce->coeffs;
  2562. INTFLOAT *out = sce->ret;
  2563. INTFLOAT *saved = sce->saved;
  2564. INTFLOAT *buf = ac->buf_mdct;
  2565. int i;
  2566. const int n = ac->oc[1].m4ac.frame_length_short ? 480 : 512;
  2567. const int n2 = n >> 1;
  2568. const int n4 = n >> 2;
  2569. const INTFLOAT *const window = n == 480 ? AAC_RENAME(ff_aac_eld_window_480) :
  2570. AAC_RENAME(ff_aac_eld_window_512);
  2571. // Inverse transform, mapped to the conventional IMDCT by
  2572. // Chivukula, R.K.; Reznik, Y.A.; Devarajan, V.,
  2573. // "Efficient algorithms for MPEG-4 AAC-ELD, AAC-LD and AAC-LC filterbanks,"
  2574. // International Conference on Audio, Language and Image Processing, ICALIP 2008.
  2575. // URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4590245&isnumber=4589950
  2576. for (i = 0; i < n2; i+=2) {
  2577. INTFLOAT temp;
  2578. temp = in[i ]; in[i ] = -in[n - 1 - i]; in[n - 1 - i] = temp;
  2579. temp = -in[i + 1]; in[i + 1] = in[n - 2 - i]; in[n - 2 - i] = temp;
  2580. }
  2581. #if !USE_FIXED
  2582. if (n == 480)
  2583. ac->mdct480->imdct_half(ac->mdct480, buf, in, 1);
  2584. else
  2585. #endif
  2586. ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
  2587. #if USE_FIXED
  2588. for (i = 0; i < 1024; i++)
  2589. buf[i] = (buf[i] + 1) >> 1;
  2590. #endif /* USE_FIXED */
  2591. for (i = 0; i < n; i+=2) {
  2592. buf[i] = -buf[i];
  2593. }
  2594. // Like with the regular IMDCT at this point we still have the middle half
  2595. // of a transform but with even symmetry on the left and odd symmetry on
  2596. // the right
  2597. // window overlapping
  2598. // The spec says to use samples [0..511] but the reference decoder uses
  2599. // samples [128..639].
  2600. for (i = n4; i < n2; i ++) {
  2601. out[i - n4] = AAC_MUL31( buf[ n2 - 1 - i] , window[i - n4]) +
  2602. AAC_MUL31( saved[ i + n2] , window[i + n - n4]) +
  2603. AAC_MUL31(-saved[n + n2 - 1 - i] , window[i + 2*n - n4]) +
  2604. AAC_MUL31(-saved[ 2*n + n2 + i] , window[i + 3*n - n4]);
  2605. }
  2606. for (i = 0; i < n2; i ++) {
  2607. out[n4 + i] = AAC_MUL31( buf[ i] , window[i + n2 - n4]) +
  2608. AAC_MUL31(-saved[ n - 1 - i] , window[i + n2 + n - n4]) +
  2609. AAC_MUL31(-saved[ n + i] , window[i + n2 + 2*n - n4]) +
  2610. AAC_MUL31( saved[2*n + n - 1 - i] , window[i + n2 + 3*n - n4]);
  2611. }
  2612. for (i = 0; i < n4; i ++) {
  2613. out[n2 + n4 + i] = AAC_MUL31( buf[ i + n2] , window[i + n - n4]) +
  2614. AAC_MUL31(-saved[n2 - 1 - i] , window[i + 2*n - n4]) +
  2615. AAC_MUL31(-saved[n + n2 + i] , window[i + 3*n - n4]);
  2616. }
  2617. // buffer update
  2618. memmove(saved + n, saved, 2 * n * sizeof(*saved));
  2619. memcpy( saved, buf, n * sizeof(*saved));
  2620. }
  2621. /**
  2622. * channel coupling transformation interface
  2623. *
  2624. * @param apply_coupling_method pointer to (in)dependent coupling function
  2625. */
  2626. static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
  2627. enum RawDataBlockType type, int elem_id,
  2628. enum CouplingPoint coupling_point,
  2629. void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
  2630. {
  2631. int i, c;
  2632. for (i = 0; i < MAX_ELEM_ID; i++) {
  2633. ChannelElement *cce = ac->che[TYPE_CCE][i];
  2634. int index = 0;
  2635. if (cce && cce->coup.coupling_point == coupling_point) {
  2636. ChannelCoupling *coup = &cce->coup;
  2637. for (c = 0; c <= coup->num_coupled; c++) {
  2638. if (coup->type[c] == type && coup->id_select[c] == elem_id) {
  2639. if (coup->ch_select[c] != 1) {
  2640. apply_coupling_method(ac, &cc->ch[0], cce, index);
  2641. if (coup->ch_select[c] != 0)
  2642. index++;
  2643. }
  2644. if (coup->ch_select[c] != 2)
  2645. apply_coupling_method(ac, &cc->ch[1], cce, index++);
  2646. } else
  2647. index += 1 + (coup->ch_select[c] == 3);
  2648. }
  2649. }
  2650. }
  2651. }
  2652. /**
  2653. * Convert spectral data to samples, applying all supported tools as appropriate.
  2654. */
  2655. static void spectral_to_sample(AACContext *ac, int samples)
  2656. {
  2657. int i, type;
  2658. void (*imdct_and_window)(AACContext *ac, SingleChannelElement *sce);
  2659. switch (ac->oc[1].m4ac.object_type) {
  2660. case AOT_ER_AAC_LD:
  2661. imdct_and_window = imdct_and_windowing_ld;
  2662. break;
  2663. case AOT_ER_AAC_ELD:
  2664. imdct_and_window = imdct_and_windowing_eld;
  2665. break;
  2666. default:
  2667. if (ac->oc[1].m4ac.frame_length_short)
  2668. imdct_and_window = imdct_and_windowing_960;
  2669. else
  2670. imdct_and_window = ac->imdct_and_windowing;
  2671. }
  2672. for (type = 3; type >= 0; type--) {
  2673. for (i = 0; i < MAX_ELEM_ID; i++) {
  2674. ChannelElement *che = ac->che[type][i];
  2675. if (che && che->present) {
  2676. if (type <= TYPE_CPE)
  2677. apply_channel_coupling(ac, che, type, i, BEFORE_TNS, AAC_RENAME(apply_dependent_coupling));
  2678. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
  2679. if (che->ch[0].ics.predictor_present) {
  2680. if (che->ch[0].ics.ltp.present)
  2681. ac->apply_ltp(ac, &che->ch[0]);
  2682. if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
  2683. ac->apply_ltp(ac, &che->ch[1]);
  2684. }
  2685. }
  2686. if (che->ch[0].tns.present)
  2687. ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
  2688. if (che->ch[1].tns.present)
  2689. ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
  2690. if (type <= TYPE_CPE)
  2691. apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, AAC_RENAME(apply_dependent_coupling));
  2692. if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
  2693. imdct_and_window(ac, &che->ch[0]);
  2694. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
  2695. ac->update_ltp(ac, &che->ch[0]);
  2696. if (type == TYPE_CPE) {
  2697. imdct_and_window(ac, &che->ch[1]);
  2698. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
  2699. ac->update_ltp(ac, &che->ch[1]);
  2700. }
  2701. if (ac->oc[1].m4ac.sbr > 0) {
  2702. AAC_RENAME(ff_sbr_apply)(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
  2703. }
  2704. }
  2705. if (type <= TYPE_CCE)
  2706. apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, AAC_RENAME(apply_independent_coupling));
  2707. #if USE_FIXED
  2708. {
  2709. int j;
  2710. /* preparation for resampler */
  2711. for(j = 0; j<samples; j++){
  2712. che->ch[0].ret[j] = (int32_t)av_clip64((int64_t)che->ch[0].ret[j]*128, INT32_MIN, INT32_MAX-0x8000)+0x8000;
  2713. if(type == TYPE_CPE)
  2714. che->ch[1].ret[j] = (int32_t)av_clip64((int64_t)che->ch[1].ret[j]*128, INT32_MIN, INT32_MAX-0x8000)+0x8000;
  2715. }
  2716. }
  2717. #endif /* USE_FIXED */
  2718. che->present = 0;
  2719. } else if (che) {
  2720. av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
  2721. }
  2722. }
  2723. }
  2724. }
  2725. static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
  2726. {
  2727. int size;
  2728. AACADTSHeaderInfo hdr_info;
  2729. uint8_t layout_map[MAX_ELEM_ID*4][3];
  2730. int layout_map_tags, ret;
  2731. size = ff_adts_header_parse(gb, &hdr_info);
  2732. if (size > 0) {
  2733. if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
  2734. // This is 2 for "VLB " audio in NSV files.
  2735. // See samples/nsv/vlb_audio.
  2736. avpriv_report_missing_feature(ac->avctx,
  2737. "More than one AAC RDB per ADTS frame");
  2738. ac->warned_num_aac_frames = 1;
  2739. }
  2740. push_output_configuration(ac);
  2741. if (hdr_info.chan_config) {
  2742. ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
  2743. if ((ret = set_default_channel_config(ac->avctx,
  2744. layout_map,
  2745. &layout_map_tags,
  2746. hdr_info.chan_config)) < 0)
  2747. return ret;
  2748. if ((ret = output_configure(ac, layout_map, layout_map_tags,
  2749. FFMAX(ac->oc[1].status,
  2750. OC_TRIAL_FRAME), 0)) < 0)
  2751. return ret;
  2752. } else {
  2753. ac->oc[1].m4ac.chan_config = 0;
  2754. /**
  2755. * dual mono frames in Japanese DTV can have chan_config 0
  2756. * WITHOUT specifying PCE.
  2757. * thus, set dual mono as default.
  2758. */
  2759. if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
  2760. layout_map_tags = 2;
  2761. layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
  2762. layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
  2763. layout_map[0][1] = 0;
  2764. layout_map[1][1] = 1;
  2765. if (output_configure(ac, layout_map, layout_map_tags,
  2766. OC_TRIAL_FRAME, 0))
  2767. return -7;
  2768. }
  2769. }
  2770. ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
  2771. ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
  2772. ac->oc[1].m4ac.object_type = hdr_info.object_type;
  2773. ac->oc[1].m4ac.frame_length_short = 0;
  2774. if (ac->oc[0].status != OC_LOCKED ||
  2775. ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
  2776. ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
  2777. ac->oc[1].m4ac.sbr = -1;
  2778. ac->oc[1].m4ac.ps = -1;
  2779. }
  2780. if (!hdr_info.crc_absent)
  2781. skip_bits(gb, 16);
  2782. }
  2783. return size;
  2784. }
  2785. static int aac_decode_er_frame(AVCodecContext *avctx, void *data,
  2786. int *got_frame_ptr, GetBitContext *gb)
  2787. {
  2788. AACContext *ac = avctx->priv_data;
  2789. const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
  2790. ChannelElement *che;
  2791. int err, i;
  2792. int samples = m4ac->frame_length_short ? 960 : 1024;
  2793. int chan_config = m4ac->chan_config;
  2794. int aot = m4ac->object_type;
  2795. if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
  2796. samples >>= 1;
  2797. ac->frame = data;
  2798. if ((err = frame_configure_elements(avctx)) < 0)
  2799. return err;
  2800. // The FF_PROFILE_AAC_* defines are all object_type - 1
  2801. // This may lead to an undefined profile being signaled
  2802. ac->avctx->profile = aot - 1;
  2803. ac->tags_mapped = 0;
  2804. if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
  2805. avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
  2806. chan_config);
  2807. return AVERROR_INVALIDDATA;
  2808. }
  2809. for (i = 0; i < tags_per_config[chan_config]; i++) {
  2810. const int elem_type = aac_channel_layout_map[chan_config-1][i][0];
  2811. const int elem_id = aac_channel_layout_map[chan_config-1][i][1];
  2812. if (!(che=get_che(ac, elem_type, elem_id))) {
  2813. av_log(ac->avctx, AV_LOG_ERROR,
  2814. "channel element %d.%d is not allocated\n",
  2815. elem_type, elem_id);
  2816. return AVERROR_INVALIDDATA;
  2817. }
  2818. che->present = 1;
  2819. if (aot != AOT_ER_AAC_ELD)
  2820. skip_bits(gb, 4);
  2821. switch (elem_type) {
  2822. case TYPE_SCE:
  2823. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  2824. break;
  2825. case TYPE_CPE:
  2826. err = decode_cpe(ac, gb, che);
  2827. break;
  2828. case TYPE_LFE:
  2829. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  2830. break;
  2831. }
  2832. if (err < 0)
  2833. return err;
  2834. }
  2835. spectral_to_sample(ac, samples);
  2836. if (!ac->frame->data[0] && samples) {
  2837. av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
  2838. return AVERROR_INVALIDDATA;
  2839. }
  2840. ac->frame->nb_samples = samples;
  2841. ac->frame->sample_rate = avctx->sample_rate;
  2842. *got_frame_ptr = 1;
  2843. skip_bits_long(gb, get_bits_left(gb));
  2844. return 0;
  2845. }
  2846. static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
  2847. int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
  2848. {
  2849. AACContext *ac = avctx->priv_data;
  2850. ChannelElement *che = NULL, *che_prev = NULL;
  2851. enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
  2852. int err, elem_id;
  2853. int samples = 0, multiplier, audio_found = 0, pce_found = 0;
  2854. int is_dmono, sce_count = 0;
  2855. int payload_alignment;
  2856. uint8_t che_presence[4][MAX_ELEM_ID] = {{0}};
  2857. ac->frame = data;
  2858. if (show_bits(gb, 12) == 0xfff) {
  2859. if ((err = parse_adts_frame_header(ac, gb)) < 0) {
  2860. av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
  2861. goto fail;
  2862. }
  2863. if (ac->oc[1].m4ac.sampling_index > 12) {
  2864. av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
  2865. err = AVERROR_INVALIDDATA;
  2866. goto fail;
  2867. }
  2868. }
  2869. if ((err = frame_configure_elements(avctx)) < 0)
  2870. goto fail;
  2871. // The FF_PROFILE_AAC_* defines are all object_type - 1
  2872. // This may lead to an undefined profile being signaled
  2873. ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
  2874. payload_alignment = get_bits_count(gb);
  2875. ac->tags_mapped = 0;
  2876. // parse
  2877. while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
  2878. elem_id = get_bits(gb, 4);
  2879. if (avctx->debug & FF_DEBUG_STARTCODE)
  2880. av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
  2881. if (!avctx->channels && elem_type != TYPE_PCE) {
  2882. err = AVERROR_INVALIDDATA;
  2883. goto fail;
  2884. }
  2885. if (elem_type < TYPE_DSE) {
  2886. if (che_presence[elem_type][elem_id]) {
  2887. int error = che_presence[elem_type][elem_id] > 1;
  2888. av_log(ac->avctx, error ? AV_LOG_ERROR : AV_LOG_DEBUG, "channel element %d.%d duplicate\n",
  2889. elem_type, elem_id);
  2890. if (error) {
  2891. err = AVERROR_INVALIDDATA;
  2892. goto fail;
  2893. }
  2894. }
  2895. che_presence[elem_type][elem_id]++;
  2896. if (!(che=get_che(ac, elem_type, elem_id))) {
  2897. av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
  2898. elem_type, elem_id);
  2899. err = AVERROR_INVALIDDATA;
  2900. goto fail;
  2901. }
  2902. samples = ac->oc[1].m4ac.frame_length_short ? 960 : 1024;
  2903. che->present = 1;
  2904. }
  2905. switch (elem_type) {
  2906. case TYPE_SCE:
  2907. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  2908. audio_found = 1;
  2909. sce_count++;
  2910. break;
  2911. case TYPE_CPE:
  2912. err = decode_cpe(ac, gb, che);
  2913. audio_found = 1;
  2914. break;
  2915. case TYPE_CCE:
  2916. err = decode_cce(ac, gb, che);
  2917. break;
  2918. case TYPE_LFE:
  2919. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  2920. audio_found = 1;
  2921. break;
  2922. case TYPE_DSE:
  2923. err = skip_data_stream_element(ac, gb);
  2924. break;
  2925. case TYPE_PCE: {
  2926. uint8_t layout_map[MAX_ELEM_ID*4][3];
  2927. int tags;
  2928. int pushed = push_output_configuration(ac);
  2929. if (pce_found && !pushed) {
  2930. err = AVERROR_INVALIDDATA;
  2931. goto fail;
  2932. }
  2933. tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb,
  2934. payload_alignment);
  2935. if (tags < 0) {
  2936. err = tags;
  2937. break;
  2938. }
  2939. if (pce_found) {
  2940. av_log(avctx, AV_LOG_ERROR,
  2941. "Not evaluating a further program_config_element as this construct is dubious at best.\n");
  2942. pop_output_configuration(ac);
  2943. } else {
  2944. err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
  2945. if (!err)
  2946. ac->oc[1].m4ac.chan_config = 0;
  2947. pce_found = 1;
  2948. }
  2949. break;
  2950. }
  2951. case TYPE_FIL:
  2952. if (elem_id == 15)
  2953. elem_id += get_bits(gb, 8) - 1;
  2954. if (get_bits_left(gb) < 8 * elem_id) {
  2955. av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
  2956. err = AVERROR_INVALIDDATA;
  2957. goto fail;
  2958. }
  2959. while (elem_id > 0)
  2960. elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, che_prev_type);
  2961. err = 0; /* FIXME */
  2962. break;
  2963. default:
  2964. err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
  2965. break;
  2966. }
  2967. if (elem_type < TYPE_DSE) {
  2968. che_prev = che;
  2969. che_prev_type = elem_type;
  2970. }
  2971. if (err)
  2972. goto fail;
  2973. if (get_bits_left(gb) < 3) {
  2974. av_log(avctx, AV_LOG_ERROR, overread_err);
  2975. err = AVERROR_INVALIDDATA;
  2976. goto fail;
  2977. }
  2978. }
  2979. if (!avctx->channels) {
  2980. *got_frame_ptr = 0;
  2981. return 0;
  2982. }
  2983. multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
  2984. samples <<= multiplier;
  2985. spectral_to_sample(ac, samples);
  2986. if (ac->oc[1].status && audio_found) {
  2987. avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
  2988. avctx->frame_size = samples;
  2989. ac->oc[1].status = OC_LOCKED;
  2990. }
  2991. if (multiplier)
  2992. avctx->internal->skip_samples_multiplier = 2;
  2993. if (!ac->frame->data[0] && samples) {
  2994. av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
  2995. err = AVERROR_INVALIDDATA;
  2996. goto fail;
  2997. }
  2998. if (samples) {
  2999. ac->frame->nb_samples = samples;
  3000. ac->frame->sample_rate = avctx->sample_rate;
  3001. } else
  3002. av_frame_unref(ac->frame);
  3003. *got_frame_ptr = !!samples;
  3004. /* for dual-mono audio (SCE + SCE) */
  3005. is_dmono = ac->dmono_mode && sce_count == 2 &&
  3006. ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
  3007. if (is_dmono) {
  3008. if (ac->dmono_mode == 1)
  3009. ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
  3010. else if (ac->dmono_mode == 2)
  3011. ((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
  3012. }
  3013. return 0;
  3014. fail:
  3015. pop_output_configuration(ac);
  3016. return err;
  3017. }
  3018. static int aac_decode_frame(AVCodecContext *avctx, void *data,
  3019. int *got_frame_ptr, AVPacket *avpkt)
  3020. {
  3021. AACContext *ac = avctx->priv_data;
  3022. const uint8_t *buf = avpkt->data;
  3023. int buf_size = avpkt->size;
  3024. GetBitContext gb;
  3025. int buf_consumed;
  3026. int buf_offset;
  3027. int err;
  3028. int new_extradata_size;
  3029. const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
  3030. AV_PKT_DATA_NEW_EXTRADATA,
  3031. &new_extradata_size);
  3032. int jp_dualmono_size;
  3033. const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
  3034. AV_PKT_DATA_JP_DUALMONO,
  3035. &jp_dualmono_size);
  3036. if (new_extradata) {
  3037. /* discard previous configuration */
  3038. ac->oc[1].status = OC_NONE;
  3039. err = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
  3040. new_extradata,
  3041. new_extradata_size * 8LL, 1);
  3042. if (err < 0) {
  3043. return err;
  3044. }
  3045. }
  3046. ac->dmono_mode = 0;
  3047. if (jp_dualmono && jp_dualmono_size > 0)
  3048. ac->dmono_mode = 1 + *jp_dualmono;
  3049. if (ac->force_dmono_mode >= 0)
  3050. ac->dmono_mode = ac->force_dmono_mode;
  3051. if (INT_MAX / 8 <= buf_size)
  3052. return AVERROR_INVALIDDATA;
  3053. if ((err = init_get_bits8(&gb, buf, buf_size)) < 0)
  3054. return err;
  3055. switch (ac->oc[1].m4ac.object_type) {
  3056. case AOT_ER_AAC_LC:
  3057. case AOT_ER_AAC_LTP:
  3058. case AOT_ER_AAC_LD:
  3059. case AOT_ER_AAC_ELD:
  3060. err = aac_decode_er_frame(avctx, data, got_frame_ptr, &gb);
  3061. break;
  3062. default:
  3063. err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt);
  3064. }
  3065. if (err < 0)
  3066. return err;
  3067. buf_consumed = (get_bits_count(&gb) + 7) >> 3;
  3068. for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
  3069. if (buf[buf_offset])
  3070. break;
  3071. return buf_size > buf_offset ? buf_consumed : buf_size;
  3072. }
  3073. static av_cold int aac_decode_close(AVCodecContext *avctx)
  3074. {
  3075. AACContext *ac = avctx->priv_data;
  3076. int i, type;
  3077. for (i = 0; i < MAX_ELEM_ID; i++) {
  3078. for (type = 0; type < 4; type++) {
  3079. if (ac->che[type][i])
  3080. AAC_RENAME(ff_aac_sbr_ctx_close)(&ac->che[type][i]->sbr);
  3081. av_freep(&ac->che[type][i]);
  3082. }
  3083. }
  3084. ff_mdct_end(&ac->mdct);
  3085. ff_mdct_end(&ac->mdct_small);
  3086. ff_mdct_end(&ac->mdct_ld);
  3087. ff_mdct_end(&ac->mdct_ltp);
  3088. #if !USE_FIXED
  3089. ff_mdct15_uninit(&ac->mdct120);
  3090. ff_mdct15_uninit(&ac->mdct480);
  3091. ff_mdct15_uninit(&ac->mdct960);
  3092. #endif
  3093. av_freep(&ac->fdsp);
  3094. return 0;
  3095. }
  3096. static void aacdec_init(AACContext *c)
  3097. {
  3098. c->imdct_and_windowing = imdct_and_windowing;
  3099. c->apply_ltp = apply_ltp;
  3100. c->apply_tns = apply_tns;
  3101. c->windowing_and_mdct_ltp = windowing_and_mdct_ltp;
  3102. c->update_ltp = update_ltp;
  3103. #if USE_FIXED
  3104. c->vector_pow43 = vector_pow43;
  3105. c->subband_scale = subband_scale;
  3106. #endif
  3107. #if !USE_FIXED
  3108. if(ARCH_MIPS)
  3109. ff_aacdec_init_mips(c);
  3110. #endif /* !USE_FIXED */
  3111. }
  3112. /**
  3113. * AVOptions for Japanese DTV specific extensions (ADTS only)
  3114. */
  3115. #define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  3116. static const AVOption options[] = {
  3117. {"dual_mono_mode", "Select the channel to decode for dual mono",
  3118. offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
  3119. AACDEC_FLAGS, "dual_mono_mode"},
  3120. {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
  3121. {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
  3122. {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
  3123. {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
  3124. {NULL},
  3125. };
  3126. static const AVClass aac_decoder_class = {
  3127. .class_name = "AAC decoder",
  3128. .item_name = av_default_item_name,
  3129. .option = options,
  3130. .version = LIBAVUTIL_VERSION_INT,
  3131. };