aacdec.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  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. *
  6. * AAC LATM decoder
  7. * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
  8. * Copyright (c) 2010 Janne Grunau <janne-ffmpeg@jannau.net>
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. /**
  27. * @file
  28. * AAC decoder
  29. * @author Oded Shimon ( ods15 ods15 dyndns org )
  30. * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
  31. */
  32. /*
  33. * supported tools
  34. *
  35. * Support? Name
  36. * N (code in SoC repo) gain control
  37. * Y block switching
  38. * Y window shapes - standard
  39. * N window shapes - Low Delay
  40. * Y filterbank - standard
  41. * N (code in SoC repo) filterbank - Scalable Sample Rate
  42. * Y Temporal Noise Shaping
  43. * Y Long Term Prediction
  44. * Y intensity stereo
  45. * Y channel coupling
  46. * Y frequency domain prediction
  47. * Y Perceptual Noise Substitution
  48. * Y Mid/Side stereo
  49. * N Scalable Inverse AAC Quantization
  50. * N Frequency Selective Switch
  51. * N upsampling filter
  52. * Y quantization & coding - AAC
  53. * N quantization & coding - TwinVQ
  54. * N quantization & coding - BSAC
  55. * N AAC Error Resilience tools
  56. * N Error Resilience payload syntax
  57. * N Error Protection tool
  58. * N CELP
  59. * N Silence Compression
  60. * N HVXC
  61. * N HVXC 4kbits/s VR
  62. * N Structured Audio tools
  63. * N Structured Audio Sample Bank Format
  64. * N MIDI
  65. * N Harmonic and Individual Lines plus Noise
  66. * N Text-To-Speech Interface
  67. * Y Spectral Band Replication
  68. * Y (not in this code) Layer-1
  69. * Y (not in this code) Layer-2
  70. * Y (not in this code) Layer-3
  71. * N SinuSoidal Coding (Transient, Sinusoid, Noise)
  72. * Y Parametric Stereo
  73. * N Direct Stream Transfer
  74. *
  75. * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
  76. * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
  77. Parametric Stereo.
  78. */
  79. #include "avcodec.h"
  80. #include "internal.h"
  81. #include "get_bits.h"
  82. #include "dsputil.h"
  83. #include "fft.h"
  84. #include "fmtconvert.h"
  85. #include "lpc.h"
  86. #include "kbdwin.h"
  87. #include "sinewin.h"
  88. #include "aac.h"
  89. #include "aactab.h"
  90. #include "aacdectab.h"
  91. #include "cbrt_tablegen.h"
  92. #include "sbr.h"
  93. #include "aacsbr.h"
  94. #include "mpeg4audio.h"
  95. #include "aacadtsdec.h"
  96. #include <assert.h>
  97. #include <errno.h>
  98. #include <math.h>
  99. #include <string.h>
  100. #if ARCH_ARM
  101. # include "arm/aac.h"
  102. #endif
  103. union float754 {
  104. float f;
  105. uint32_t i;
  106. };
  107. static VLC vlc_scalefactors;
  108. static VLC vlc_spectral[11];
  109. static const char overread_err[] = "Input buffer exhausted before END element found\n";
  110. static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
  111. {
  112. // For PCE based channel configurations map the channels solely based on tags.
  113. if (!ac->m4ac.chan_config) {
  114. return ac->tag_che_map[type][elem_id];
  115. }
  116. // For indexed channel configurations map the channels solely based on position.
  117. switch (ac->m4ac.chan_config) {
  118. case 7:
  119. if (ac->tags_mapped == 3 && type == TYPE_CPE) {
  120. ac->tags_mapped++;
  121. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
  122. }
  123. case 6:
  124. /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
  125. instead of SCE[0] CPE[0] CPE[1] LFE[0]. If we seem to have
  126. encountered such a stream, transfer the LFE[0] element to the SCE[1]'s mapping */
  127. if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
  128. ac->tags_mapped++;
  129. return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
  130. }
  131. case 5:
  132. if (ac->tags_mapped == 2 && type == TYPE_CPE) {
  133. ac->tags_mapped++;
  134. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
  135. }
  136. case 4:
  137. if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
  138. ac->tags_mapped++;
  139. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
  140. }
  141. case 3:
  142. case 2:
  143. if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
  144. ac->tags_mapped++;
  145. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
  146. } else if (ac->m4ac.chan_config == 2) {
  147. return NULL;
  148. }
  149. case 1:
  150. if (!ac->tags_mapped && type == TYPE_SCE) {
  151. ac->tags_mapped++;
  152. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
  153. }
  154. default:
  155. return NULL;
  156. }
  157. }
  158. /**
  159. * Check for the channel element in the current channel position configuration.
  160. * If it exists, make sure the appropriate element is allocated and map the
  161. * channel order to match the internal FFmpeg channel layout.
  162. *
  163. * @param che_pos current channel position configuration
  164. * @param type channel element type
  165. * @param id channel element id
  166. * @param channels count of the number of channels in the configuration
  167. *
  168. * @return Returns error status. 0 - OK, !0 - error
  169. */
  170. static av_cold int che_configure(AACContext *ac,
  171. enum ChannelPosition che_pos[4][MAX_ELEM_ID],
  172. int type, int id, int *channels)
  173. {
  174. if (che_pos[type][id]) {
  175. if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
  176. return AVERROR(ENOMEM);
  177. ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr);
  178. if (type != TYPE_CCE) {
  179. ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
  180. if (type == TYPE_CPE ||
  181. (type == TYPE_SCE && ac->m4ac.ps == 1)) {
  182. ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
  183. }
  184. }
  185. } else {
  186. if (ac->che[type][id])
  187. ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
  188. av_freep(&ac->che[type][id]);
  189. }
  190. return 0;
  191. }
  192. /**
  193. * Configure output channel order based on the current program configuration element.
  194. *
  195. * @param che_pos current channel position configuration
  196. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  197. *
  198. * @return Returns error status. 0 - OK, !0 - error
  199. */
  200. static av_cold int output_configure(AACContext *ac,
  201. enum ChannelPosition che_pos[4][MAX_ELEM_ID],
  202. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  203. int channel_config, enum OCStatus oc_type)
  204. {
  205. AVCodecContext *avctx = ac->avctx;
  206. int i, type, channels = 0, ret;
  207. if (new_che_pos != che_pos)
  208. memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  209. if (channel_config) {
  210. for (i = 0; i < tags_per_config[channel_config]; i++) {
  211. if ((ret = che_configure(ac, che_pos,
  212. aac_channel_layout_map[channel_config - 1][i][0],
  213. aac_channel_layout_map[channel_config - 1][i][1],
  214. &channels)))
  215. return ret;
  216. }
  217. memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
  218. avctx->channel_layout = aac_channel_layout[channel_config - 1];
  219. } else {
  220. /* Allocate or free elements depending on if they are in the
  221. * current program configuration.
  222. *
  223. * Set up default 1:1 output mapping.
  224. *
  225. * For a 5.1 stream the output order will be:
  226. * [ Center ] [ Front Left ] [ Front Right ] [ LFE ] [ Surround Left ] [ Surround Right ]
  227. */
  228. for (i = 0; i < MAX_ELEM_ID; i++) {
  229. for (type = 0; type < 4; type++) {
  230. if ((ret = che_configure(ac, che_pos, type, i, &channels)))
  231. return ret;
  232. }
  233. }
  234. memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
  235. }
  236. avctx->channels = channels;
  237. ac->output_configured = oc_type;
  238. return 0;
  239. }
  240. /**
  241. * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
  242. *
  243. * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present.
  244. * @param sce_map mono (Single Channel Element) map
  245. * @param type speaker type/position for these channels
  246. */
  247. static void decode_channel_map(enum ChannelPosition *cpe_map,
  248. enum ChannelPosition *sce_map,
  249. enum ChannelPosition type,
  250. GetBitContext *gb, int n)
  251. {
  252. while (n--) {
  253. enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
  254. map[get_bits(gb, 4)] = type;
  255. }
  256. }
  257. /**
  258. * Decode program configuration element; reference: table 4.2.
  259. *
  260. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  261. *
  262. * @return Returns error status. 0 - OK, !0 - error
  263. */
  264. static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
  265. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  266. GetBitContext *gb)
  267. {
  268. int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
  269. int comment_len;
  270. skip_bits(gb, 2); // object_type
  271. sampling_index = get_bits(gb, 4);
  272. if (m4ac->sampling_index != sampling_index)
  273. av_log(avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
  274. num_front = get_bits(gb, 4);
  275. num_side = get_bits(gb, 4);
  276. num_back = get_bits(gb, 4);
  277. num_lfe = get_bits(gb, 2);
  278. num_assoc_data = get_bits(gb, 3);
  279. num_cc = get_bits(gb, 4);
  280. if (get_bits1(gb))
  281. skip_bits(gb, 4); // mono_mixdown_tag
  282. if (get_bits1(gb))
  283. skip_bits(gb, 4); // stereo_mixdown_tag
  284. if (get_bits1(gb))
  285. skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
  286. if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
  287. av_log(avctx, AV_LOG_ERROR, overread_err);
  288. return -1;
  289. }
  290. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
  291. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
  292. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
  293. decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
  294. skip_bits_long(gb, 4 * num_assoc_data);
  295. decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
  296. align_get_bits(gb);
  297. /* comment field, first byte is length */
  298. comment_len = get_bits(gb, 8) * 8;
  299. if (get_bits_left(gb) < comment_len) {
  300. av_log(avctx, AV_LOG_ERROR, overread_err);
  301. return -1;
  302. }
  303. skip_bits_long(gb, comment_len);
  304. return 0;
  305. }
  306. /**
  307. * Set up channel positions based on a default channel configuration
  308. * as specified in table 1.17.
  309. *
  310. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  311. *
  312. * @return Returns error status. 0 - OK, !0 - error
  313. */
  314. static av_cold int set_default_channel_config(AVCodecContext *avctx,
  315. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  316. int channel_config)
  317. {
  318. if (channel_config < 1 || channel_config > 7) {
  319. av_log(avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
  320. channel_config);
  321. return -1;
  322. }
  323. /* default channel configurations:
  324. *
  325. * 1ch : front center (mono)
  326. * 2ch : L + R (stereo)
  327. * 3ch : front center + L + R
  328. * 4ch : front center + L + R + back center
  329. * 5ch : front center + L + R + back stereo
  330. * 6ch : front center + L + R + back stereo + LFE
  331. * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
  332. */
  333. if (channel_config != 2)
  334. new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
  335. if (channel_config > 1)
  336. new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
  337. if (channel_config == 4)
  338. new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK; // back center
  339. if (channel_config > 4)
  340. new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
  341. = AAC_CHANNEL_BACK; // back stereo
  342. if (channel_config > 5)
  343. new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE; // LFE
  344. if (channel_config == 7)
  345. new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
  346. return 0;
  347. }
  348. /**
  349. * Decode GA "General Audio" specific configuration; reference: table 4.1.
  350. *
  351. * @param ac pointer to AACContext, may be null
  352. * @param avctx pointer to AVCCodecContext, used for logging
  353. *
  354. * @return Returns error status. 0 - OK, !0 - error
  355. */
  356. static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
  357. GetBitContext *gb,
  358. MPEG4AudioConfig *m4ac,
  359. int channel_config)
  360. {
  361. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  362. int extension_flag, ret;
  363. if (get_bits1(gb)) { // frameLengthFlag
  364. av_log_missing_feature(avctx, "960/120 MDCT window is", 1);
  365. return -1;
  366. }
  367. if (get_bits1(gb)) // dependsOnCoreCoder
  368. skip_bits(gb, 14); // coreCoderDelay
  369. extension_flag = get_bits1(gb);
  370. if (m4ac->object_type == AOT_AAC_SCALABLE ||
  371. m4ac->object_type == AOT_ER_AAC_SCALABLE)
  372. skip_bits(gb, 3); // layerNr
  373. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  374. if (channel_config == 0) {
  375. skip_bits(gb, 4); // element_instance_tag
  376. if ((ret = decode_pce(avctx, m4ac, new_che_pos, gb)))
  377. return ret;
  378. } else {
  379. if ((ret = set_default_channel_config(avctx, new_che_pos, channel_config)))
  380. return ret;
  381. }
  382. if (ac && (ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
  383. return ret;
  384. if (extension_flag) {
  385. switch (m4ac->object_type) {
  386. case AOT_ER_BSAC:
  387. skip_bits(gb, 5); // numOfSubFrame
  388. skip_bits(gb, 11); // layer_length
  389. break;
  390. case AOT_ER_AAC_LC:
  391. case AOT_ER_AAC_LTP:
  392. case AOT_ER_AAC_SCALABLE:
  393. case AOT_ER_AAC_LD:
  394. skip_bits(gb, 3); /* aacSectionDataResilienceFlag
  395. * aacScalefactorDataResilienceFlag
  396. * aacSpectralDataResilienceFlag
  397. */
  398. break;
  399. }
  400. skip_bits1(gb); // extensionFlag3 (TBD in version 3)
  401. }
  402. return 0;
  403. }
  404. /**
  405. * Decode audio specific configuration; reference: table 1.13.
  406. *
  407. * @param ac pointer to AACContext, may be null
  408. * @param avctx pointer to AVCCodecContext, used for logging
  409. * @param m4ac pointer to MPEG4AudioConfig, used for parsing
  410. * @param data pointer to AVCodecContext extradata
  411. * @param data_size size of AVCCodecContext extradata
  412. *
  413. * @return Returns error status or number of consumed bits. <0 - error
  414. */
  415. static int decode_audio_specific_config(AACContext *ac,
  416. AVCodecContext *avctx,
  417. MPEG4AudioConfig *m4ac,
  418. const uint8_t *data, int data_size)
  419. {
  420. GetBitContext gb;
  421. int i;
  422. av_dlog(avctx, "extradata size %d\n", avctx->extradata_size);
  423. for (i = 0; i < avctx->extradata_size; i++)
  424. av_dlog(avctx, "%02x ", avctx->extradata[i]);
  425. av_dlog(avctx, "\n");
  426. init_get_bits(&gb, data, data_size * 8);
  427. if ((i = ff_mpeg4audio_get_config(m4ac, data, data_size)) < 0)
  428. return -1;
  429. if (m4ac->sampling_index > 12) {
  430. av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
  431. return -1;
  432. }
  433. if (m4ac->sbr == 1 && m4ac->ps == -1)
  434. m4ac->ps = 1;
  435. skip_bits_long(&gb, i);
  436. switch (m4ac->object_type) {
  437. case AOT_AAC_MAIN:
  438. case AOT_AAC_LC:
  439. case AOT_AAC_LTP:
  440. if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
  441. return -1;
  442. break;
  443. default:
  444. av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
  445. m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
  446. return -1;
  447. }
  448. av_dlog(avctx, "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
  449. m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
  450. m4ac->sample_rate, m4ac->sbr, m4ac->ps);
  451. return get_bits_count(&gb);
  452. }
  453. /**
  454. * linear congruential pseudorandom number generator
  455. *
  456. * @param previous_val pointer to the current state of the generator
  457. *
  458. * @return Returns a 32-bit pseudorandom integer
  459. */
  460. static av_always_inline int lcg_random(int previous_val)
  461. {
  462. return previous_val * 1664525 + 1013904223;
  463. }
  464. static av_always_inline void reset_predict_state(PredictorState *ps)
  465. {
  466. ps->r0 = 0.0f;
  467. ps->r1 = 0.0f;
  468. ps->cor0 = 0.0f;
  469. ps->cor1 = 0.0f;
  470. ps->var0 = 1.0f;
  471. ps->var1 = 1.0f;
  472. }
  473. static void reset_all_predictors(PredictorState *ps)
  474. {
  475. int i;
  476. for (i = 0; i < MAX_PREDICTORS; i++)
  477. reset_predict_state(&ps[i]);
  478. }
  479. static void reset_predictor_group(PredictorState *ps, int group_num)
  480. {
  481. int i;
  482. for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
  483. reset_predict_state(&ps[i]);
  484. }
  485. #define AAC_INIT_VLC_STATIC(num, size) \
  486. INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
  487. ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
  488. ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
  489. size);
  490. static av_cold int aac_decode_init(AVCodecContext *avctx)
  491. {
  492. AACContext *ac = avctx->priv_data;
  493. float output_scale_factor;
  494. ac->avctx = avctx;
  495. ac->m4ac.sample_rate = avctx->sample_rate;
  496. if (avctx->extradata_size > 0) {
  497. if (decode_audio_specific_config(ac, ac->avctx, &ac->m4ac,
  498. avctx->extradata,
  499. avctx->extradata_size) < 0)
  500. return -1;
  501. }
  502. if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
  503. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  504. output_scale_factor = 1.0 / 32768.0;
  505. } else {
  506. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  507. output_scale_factor = 1.0;
  508. }
  509. AAC_INIT_VLC_STATIC( 0, 304);
  510. AAC_INIT_VLC_STATIC( 1, 270);
  511. AAC_INIT_VLC_STATIC( 2, 550);
  512. AAC_INIT_VLC_STATIC( 3, 300);
  513. AAC_INIT_VLC_STATIC( 4, 328);
  514. AAC_INIT_VLC_STATIC( 5, 294);
  515. AAC_INIT_VLC_STATIC( 6, 306);
  516. AAC_INIT_VLC_STATIC( 7, 268);
  517. AAC_INIT_VLC_STATIC( 8, 510);
  518. AAC_INIT_VLC_STATIC( 9, 366);
  519. AAC_INIT_VLC_STATIC(10, 462);
  520. ff_aac_sbr_init();
  521. dsputil_init(&ac->dsp, avctx);
  522. ff_fmt_convert_init(&ac->fmt_conv, avctx);
  523. ac->random_state = 0x1f2e3d4c;
  524. ff_aac_tableinit();
  525. INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
  526. ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
  527. ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
  528. 352);
  529. ff_mdct_init(&ac->mdct, 11, 1, output_scale_factor/1024.0);
  530. ff_mdct_init(&ac->mdct_small, 8, 1, output_scale_factor/128.0);
  531. ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0/output_scale_factor);
  532. // window initialization
  533. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  534. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  535. ff_init_ff_sine_windows(10);
  536. ff_init_ff_sine_windows( 7);
  537. cbrt_tableinit();
  538. return 0;
  539. }
  540. /**
  541. * Skip data_stream_element; reference: table 4.10.
  542. */
  543. static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
  544. {
  545. int byte_align = get_bits1(gb);
  546. int count = get_bits(gb, 8);
  547. if (count == 255)
  548. count += get_bits(gb, 8);
  549. if (byte_align)
  550. align_get_bits(gb);
  551. if (get_bits_left(gb) < 8 * count) {
  552. av_log(ac->avctx, AV_LOG_ERROR, overread_err);
  553. return -1;
  554. }
  555. skip_bits_long(gb, 8 * count);
  556. return 0;
  557. }
  558. static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
  559. GetBitContext *gb)
  560. {
  561. int sfb;
  562. if (get_bits1(gb)) {
  563. ics->predictor_reset_group = get_bits(gb, 5);
  564. if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
  565. av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
  566. return -1;
  567. }
  568. }
  569. for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
  570. ics->prediction_used[sfb] = get_bits1(gb);
  571. }
  572. return 0;
  573. }
  574. /**
  575. * Decode Long Term Prediction data; reference: table 4.xx.
  576. */
  577. static void decode_ltp(AACContext *ac, LongTermPrediction *ltp,
  578. GetBitContext *gb, uint8_t max_sfb)
  579. {
  580. int sfb;
  581. ltp->lag = get_bits(gb, 11);
  582. ltp->coef = ltp_coef[get_bits(gb, 3)];
  583. for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
  584. ltp->used[sfb] = get_bits1(gb);
  585. }
  586. /**
  587. * Decode Individual Channel Stream info; reference: table 4.6.
  588. *
  589. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  590. */
  591. static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
  592. GetBitContext *gb, int common_window)
  593. {
  594. if (get_bits1(gb)) {
  595. av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
  596. memset(ics, 0, sizeof(IndividualChannelStream));
  597. return -1;
  598. }
  599. ics->window_sequence[1] = ics->window_sequence[0];
  600. ics->window_sequence[0] = get_bits(gb, 2);
  601. ics->use_kb_window[1] = ics->use_kb_window[0];
  602. ics->use_kb_window[0] = get_bits1(gb);
  603. ics->num_window_groups = 1;
  604. ics->group_len[0] = 1;
  605. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  606. int i;
  607. ics->max_sfb = get_bits(gb, 4);
  608. for (i = 0; i < 7; i++) {
  609. if (get_bits1(gb)) {
  610. ics->group_len[ics->num_window_groups - 1]++;
  611. } else {
  612. ics->num_window_groups++;
  613. ics->group_len[ics->num_window_groups - 1] = 1;
  614. }
  615. }
  616. ics->num_windows = 8;
  617. ics->swb_offset = ff_swb_offset_128[ac->m4ac.sampling_index];
  618. ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index];
  619. ics->tns_max_bands = ff_tns_max_bands_128[ac->m4ac.sampling_index];
  620. ics->predictor_present = 0;
  621. } else {
  622. ics->max_sfb = get_bits(gb, 6);
  623. ics->num_windows = 1;
  624. ics->swb_offset = ff_swb_offset_1024[ac->m4ac.sampling_index];
  625. ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
  626. ics->tns_max_bands = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
  627. ics->predictor_present = get_bits1(gb);
  628. ics->predictor_reset_group = 0;
  629. if (ics->predictor_present) {
  630. if (ac->m4ac.object_type == AOT_AAC_MAIN) {
  631. if (decode_prediction(ac, ics, gb)) {
  632. memset(ics, 0, sizeof(IndividualChannelStream));
  633. return -1;
  634. }
  635. } else if (ac->m4ac.object_type == AOT_AAC_LC) {
  636. av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
  637. memset(ics, 0, sizeof(IndividualChannelStream));
  638. return -1;
  639. } else {
  640. if ((ics->ltp.present = get_bits(gb, 1)))
  641. decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
  642. }
  643. }
  644. }
  645. if (ics->max_sfb > ics->num_swb) {
  646. av_log(ac->avctx, AV_LOG_ERROR,
  647. "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
  648. ics->max_sfb, ics->num_swb);
  649. memset(ics, 0, sizeof(IndividualChannelStream));
  650. return -1;
  651. }
  652. return 0;
  653. }
  654. /**
  655. * Decode band types (section_data payload); reference: table 4.46.
  656. *
  657. * @param band_type array of the used band type
  658. * @param band_type_run_end array of the last scalefactor band of a band type run
  659. *
  660. * @return Returns error status. 0 - OK, !0 - error
  661. */
  662. static int decode_band_types(AACContext *ac, enum BandType band_type[120],
  663. int band_type_run_end[120], GetBitContext *gb,
  664. IndividualChannelStream *ics)
  665. {
  666. int g, idx = 0;
  667. const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
  668. for (g = 0; g < ics->num_window_groups; g++) {
  669. int k = 0;
  670. while (k < ics->max_sfb) {
  671. uint8_t sect_end = k;
  672. int sect_len_incr;
  673. int sect_band_type = get_bits(gb, 4);
  674. if (sect_band_type == 12) {
  675. av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
  676. return -1;
  677. }
  678. while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1)
  679. sect_end += sect_len_incr;
  680. sect_end += sect_len_incr;
  681. if (get_bits_left(gb) < 0) {
  682. av_log(ac->avctx, AV_LOG_ERROR, overread_err);
  683. return -1;
  684. }
  685. if (sect_end > ics->max_sfb) {
  686. av_log(ac->avctx, AV_LOG_ERROR,
  687. "Number of bands (%d) exceeds limit (%d).\n",
  688. sect_end, ics->max_sfb);
  689. return -1;
  690. }
  691. for (; k < sect_end; k++) {
  692. band_type [idx] = sect_band_type;
  693. band_type_run_end[idx++] = sect_end;
  694. }
  695. }
  696. }
  697. return 0;
  698. }
  699. /**
  700. * Decode scalefactors; reference: table 4.47.
  701. *
  702. * @param global_gain first scalefactor value as scalefactors are differentially coded
  703. * @param band_type array of the used band type
  704. * @param band_type_run_end array of the last scalefactor band of a band type run
  705. * @param sf array of scalefactors or intensity stereo positions
  706. *
  707. * @return Returns error status. 0 - OK, !0 - error
  708. */
  709. static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
  710. unsigned int global_gain,
  711. IndividualChannelStream *ics,
  712. enum BandType band_type[120],
  713. int band_type_run_end[120])
  714. {
  715. int g, i, idx = 0;
  716. int offset[3] = { global_gain, global_gain - 90, 0 };
  717. int clipped_offset;
  718. int noise_flag = 1;
  719. static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
  720. for (g = 0; g < ics->num_window_groups; g++) {
  721. for (i = 0; i < ics->max_sfb;) {
  722. int run_end = band_type_run_end[idx];
  723. if (band_type[idx] == ZERO_BT) {
  724. for (; i < run_end; i++, idx++)
  725. sf[idx] = 0.;
  726. } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
  727. for (; i < run_end; i++, idx++) {
  728. offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  729. clipped_offset = av_clip(offset[2], -155, 100);
  730. if (offset[2] != clipped_offset) {
  731. av_log_ask_for_sample(ac->avctx, "Intensity stereo "
  732. "position clipped (%d -> %d).\nIf you heard an "
  733. "audible artifact, there may be a bug in the "
  734. "decoder. ", offset[2], clipped_offset);
  735. }
  736. sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
  737. }
  738. } else if (band_type[idx] == NOISE_BT) {
  739. for (; i < run_end; i++, idx++) {
  740. if (noise_flag-- > 0)
  741. offset[1] += get_bits(gb, 9) - 256;
  742. else
  743. offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  744. clipped_offset = av_clip(offset[1], -100, 155);
  745. if (offset[1] != clipped_offset) {
  746. av_log_ask_for_sample(ac->avctx, "Noise gain clipped "
  747. "(%d -> %d).\nIf you heard an audible "
  748. "artifact, there may be a bug in the decoder. ",
  749. offset[1], clipped_offset);
  750. }
  751. sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
  752. }
  753. } else {
  754. for (; i < run_end; i++, idx++) {
  755. offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  756. if (offset[0] > 255U) {
  757. av_log(ac->avctx, AV_LOG_ERROR,
  758. "%s (%d) out of range.\n", sf_str[0], offset[0]);
  759. return -1;
  760. }
  761. sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
  762. }
  763. }
  764. }
  765. }
  766. return 0;
  767. }
  768. /**
  769. * Decode pulse data; reference: table 4.7.
  770. */
  771. static int decode_pulses(Pulse *pulse, GetBitContext *gb,
  772. const uint16_t *swb_offset, int num_swb)
  773. {
  774. int i, pulse_swb;
  775. pulse->num_pulse = get_bits(gb, 2) + 1;
  776. pulse_swb = get_bits(gb, 6);
  777. if (pulse_swb >= num_swb)
  778. return -1;
  779. pulse->pos[0] = swb_offset[pulse_swb];
  780. pulse->pos[0] += get_bits(gb, 5);
  781. if (pulse->pos[0] > 1023)
  782. return -1;
  783. pulse->amp[0] = get_bits(gb, 4);
  784. for (i = 1; i < pulse->num_pulse; i++) {
  785. pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
  786. if (pulse->pos[i] > 1023)
  787. return -1;
  788. pulse->amp[i] = get_bits(gb, 4);
  789. }
  790. return 0;
  791. }
  792. /**
  793. * Decode Temporal Noise Shaping data; reference: table 4.48.
  794. *
  795. * @return Returns error status. 0 - OK, !0 - error
  796. */
  797. static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
  798. GetBitContext *gb, const IndividualChannelStream *ics)
  799. {
  800. int w, filt, i, coef_len, coef_res, coef_compress;
  801. const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  802. const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
  803. for (w = 0; w < ics->num_windows; w++) {
  804. if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
  805. coef_res = get_bits1(gb);
  806. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  807. int tmp2_idx;
  808. tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
  809. if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
  810. av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
  811. tns->order[w][filt], tns_max_order);
  812. tns->order[w][filt] = 0;
  813. return -1;
  814. }
  815. if (tns->order[w][filt]) {
  816. tns->direction[w][filt] = get_bits1(gb);
  817. coef_compress = get_bits1(gb);
  818. coef_len = coef_res + 3 - coef_compress;
  819. tmp2_idx = 2 * coef_compress + coef_res;
  820. for (i = 0; i < tns->order[w][filt]; i++)
  821. tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
  822. }
  823. }
  824. }
  825. }
  826. return 0;
  827. }
  828. /**
  829. * Decode Mid/Side data; reference: table 4.54.
  830. *
  831. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  832. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  833. * [3] reserved for scalable AAC
  834. */
  835. static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
  836. int ms_present)
  837. {
  838. int idx;
  839. if (ms_present == 1) {
  840. for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
  841. cpe->ms_mask[idx] = get_bits1(gb);
  842. } else if (ms_present == 2) {
  843. memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
  844. }
  845. }
  846. #ifndef VMUL2
  847. static inline float *VMUL2(float *dst, const float *v, unsigned idx,
  848. const float *scale)
  849. {
  850. float s = *scale;
  851. *dst++ = v[idx & 15] * s;
  852. *dst++ = v[idx>>4 & 15] * s;
  853. return dst;
  854. }
  855. #endif
  856. #ifndef VMUL4
  857. static inline float *VMUL4(float *dst, const float *v, unsigned idx,
  858. const float *scale)
  859. {
  860. float s = *scale;
  861. *dst++ = v[idx & 3] * s;
  862. *dst++ = v[idx>>2 & 3] * s;
  863. *dst++ = v[idx>>4 & 3] * s;
  864. *dst++ = v[idx>>6 & 3] * s;
  865. return dst;
  866. }
  867. #endif
  868. #ifndef VMUL2S
  869. static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
  870. unsigned sign, const float *scale)
  871. {
  872. union float754 s0, s1;
  873. s0.f = s1.f = *scale;
  874. s0.i ^= sign >> 1 << 31;
  875. s1.i ^= sign << 31;
  876. *dst++ = v[idx & 15] * s0.f;
  877. *dst++ = v[idx>>4 & 15] * s1.f;
  878. return dst;
  879. }
  880. #endif
  881. #ifndef VMUL4S
  882. static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
  883. unsigned sign, const float *scale)
  884. {
  885. unsigned nz = idx >> 12;
  886. union float754 s = { .f = *scale };
  887. union float754 t;
  888. t.i = s.i ^ (sign & 1U<<31);
  889. *dst++ = v[idx & 3] * t.f;
  890. sign <<= nz & 1; nz >>= 1;
  891. t.i = s.i ^ (sign & 1U<<31);
  892. *dst++ = v[idx>>2 & 3] * t.f;
  893. sign <<= nz & 1; nz >>= 1;
  894. t.i = s.i ^ (sign & 1U<<31);
  895. *dst++ = v[idx>>4 & 3] * t.f;
  896. sign <<= nz & 1; nz >>= 1;
  897. t.i = s.i ^ (sign & 1U<<31);
  898. *dst++ = v[idx>>6 & 3] * t.f;
  899. return dst;
  900. }
  901. #endif
  902. /**
  903. * Decode spectral data; reference: table 4.50.
  904. * Dequantize and scale spectral data; reference: 4.6.3.3.
  905. *
  906. * @param coef array of dequantized, scaled spectral data
  907. * @param sf array of scalefactors or intensity stereo positions
  908. * @param pulse_present set if pulses are present
  909. * @param pulse pointer to pulse data struct
  910. * @param band_type array of the used band type
  911. *
  912. * @return Returns error status. 0 - OK, !0 - error
  913. */
  914. static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
  915. GetBitContext *gb, const float sf[120],
  916. int pulse_present, const Pulse *pulse,
  917. const IndividualChannelStream *ics,
  918. enum BandType band_type[120])
  919. {
  920. int i, k, g, idx = 0;
  921. const int c = 1024 / ics->num_windows;
  922. const uint16_t *offsets = ics->swb_offset;
  923. float *coef_base = coef;
  924. for (g = 0; g < ics->num_windows; g++)
  925. memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
  926. for (g = 0; g < ics->num_window_groups; g++) {
  927. unsigned g_len = ics->group_len[g];
  928. for (i = 0; i < ics->max_sfb; i++, idx++) {
  929. const unsigned cbt_m1 = band_type[idx] - 1;
  930. float *cfo = coef + offsets[i];
  931. int off_len = offsets[i + 1] - offsets[i];
  932. int group;
  933. if (cbt_m1 >= INTENSITY_BT2 - 1) {
  934. for (group = 0; group < g_len; group++, cfo+=128) {
  935. memset(cfo, 0, off_len * sizeof(float));
  936. }
  937. } else if (cbt_m1 == NOISE_BT - 1) {
  938. for (group = 0; group < g_len; group++, cfo+=128) {
  939. float scale;
  940. float band_energy;
  941. for (k = 0; k < off_len; k++) {
  942. ac->random_state = lcg_random(ac->random_state);
  943. cfo[k] = ac->random_state;
  944. }
  945. band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
  946. scale = sf[idx] / sqrtf(band_energy);
  947. ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
  948. }
  949. } else {
  950. const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
  951. const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
  952. VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
  953. OPEN_READER(re, gb);
  954. switch (cbt_m1 >> 1) {
  955. case 0:
  956. for (group = 0; group < g_len; group++, cfo+=128) {
  957. float *cf = cfo;
  958. int len = off_len;
  959. do {
  960. int code;
  961. unsigned cb_idx;
  962. UPDATE_CACHE(re, gb);
  963. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  964. cb_idx = cb_vector_idx[code];
  965. cf = VMUL4(cf, vq, cb_idx, sf + idx);
  966. } while (len -= 4);
  967. }
  968. break;
  969. case 1:
  970. for (group = 0; group < g_len; group++, cfo+=128) {
  971. float *cf = cfo;
  972. int len = off_len;
  973. do {
  974. int code;
  975. unsigned nnz;
  976. unsigned cb_idx;
  977. uint32_t bits;
  978. UPDATE_CACHE(re, gb);
  979. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  980. cb_idx = cb_vector_idx[code];
  981. nnz = cb_idx >> 8 & 15;
  982. bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
  983. LAST_SKIP_BITS(re, gb, nnz);
  984. cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
  985. } while (len -= 4);
  986. }
  987. break;
  988. case 2:
  989. for (group = 0; group < g_len; group++, cfo+=128) {
  990. float *cf = cfo;
  991. int len = off_len;
  992. do {
  993. int code;
  994. unsigned cb_idx;
  995. UPDATE_CACHE(re, gb);
  996. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  997. cb_idx = cb_vector_idx[code];
  998. cf = VMUL2(cf, vq, cb_idx, sf + idx);
  999. } while (len -= 2);
  1000. }
  1001. break;
  1002. case 3:
  1003. case 4:
  1004. for (group = 0; group < g_len; group++, cfo+=128) {
  1005. float *cf = cfo;
  1006. int len = off_len;
  1007. do {
  1008. int code;
  1009. unsigned nnz;
  1010. unsigned cb_idx;
  1011. unsigned sign;
  1012. UPDATE_CACHE(re, gb);
  1013. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1014. cb_idx = cb_vector_idx[code];
  1015. nnz = cb_idx >> 8 & 15;
  1016. sign = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
  1017. LAST_SKIP_BITS(re, gb, nnz);
  1018. cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
  1019. } while (len -= 2);
  1020. }
  1021. break;
  1022. default:
  1023. for (group = 0; group < g_len; group++, cfo+=128) {
  1024. float *cf = cfo;
  1025. uint32_t *icf = (uint32_t *) cf;
  1026. int len = off_len;
  1027. do {
  1028. int code;
  1029. unsigned nzt, nnz;
  1030. unsigned cb_idx;
  1031. uint32_t bits;
  1032. int j;
  1033. UPDATE_CACHE(re, gb);
  1034. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1035. if (!code) {
  1036. *icf++ = 0;
  1037. *icf++ = 0;
  1038. continue;
  1039. }
  1040. cb_idx = cb_vector_idx[code];
  1041. nnz = cb_idx >> 12;
  1042. nzt = cb_idx >> 8;
  1043. bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
  1044. LAST_SKIP_BITS(re, gb, nnz);
  1045. for (j = 0; j < 2; j++) {
  1046. if (nzt & 1<<j) {
  1047. uint32_t b;
  1048. int n;
  1049. /* The total length of escape_sequence must be < 22 bits according
  1050. to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
  1051. UPDATE_CACHE(re, gb);
  1052. b = GET_CACHE(re, gb);
  1053. b = 31 - av_log2(~b);
  1054. if (b > 8) {
  1055. av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
  1056. return -1;
  1057. }
  1058. SKIP_BITS(re, gb, b + 1);
  1059. b += 4;
  1060. n = (1 << b) + SHOW_UBITS(re, gb, b);
  1061. LAST_SKIP_BITS(re, gb, b);
  1062. *icf++ = cbrt_tab[n] | (bits & 1U<<31);
  1063. bits <<= 1;
  1064. } else {
  1065. unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
  1066. *icf++ = (bits & 1U<<31) | v;
  1067. bits <<= !!v;
  1068. }
  1069. cb_idx >>= 4;
  1070. }
  1071. } while (len -= 2);
  1072. ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
  1073. }
  1074. }
  1075. CLOSE_READER(re, gb);
  1076. }
  1077. }
  1078. coef += g_len << 7;
  1079. }
  1080. if (pulse_present) {
  1081. idx = 0;
  1082. for (i = 0; i < pulse->num_pulse; i++) {
  1083. float co = coef_base[ pulse->pos[i] ];
  1084. while (offsets[idx + 1] <= pulse->pos[i])
  1085. idx++;
  1086. if (band_type[idx] != NOISE_BT && sf[idx]) {
  1087. float ico = -pulse->amp[i];
  1088. if (co) {
  1089. co /= sf[idx];
  1090. ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
  1091. }
  1092. coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
  1093. }
  1094. }
  1095. }
  1096. return 0;
  1097. }
  1098. static av_always_inline float flt16_round(float pf)
  1099. {
  1100. union float754 tmp;
  1101. tmp.f = pf;
  1102. tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
  1103. return tmp.f;
  1104. }
  1105. static av_always_inline float flt16_even(float pf)
  1106. {
  1107. union float754 tmp;
  1108. tmp.f = pf;
  1109. tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
  1110. return tmp.f;
  1111. }
  1112. static av_always_inline float flt16_trunc(float pf)
  1113. {
  1114. union float754 pun;
  1115. pun.f = pf;
  1116. pun.i &= 0xFFFF0000U;
  1117. return pun.f;
  1118. }
  1119. static av_always_inline void predict(PredictorState *ps, float *coef,
  1120. int output_enable)
  1121. {
  1122. const float a = 0.953125; // 61.0 / 64
  1123. const float alpha = 0.90625; // 29.0 / 32
  1124. float e0, e1;
  1125. float pv;
  1126. float k1, k2;
  1127. float r0 = ps->r0, r1 = ps->r1;
  1128. float cor0 = ps->cor0, cor1 = ps->cor1;
  1129. float var0 = ps->var0, var1 = ps->var1;
  1130. k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
  1131. k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
  1132. pv = flt16_round(k1 * r0 + k2 * r1);
  1133. if (output_enable)
  1134. *coef += pv;
  1135. e0 = *coef;
  1136. e1 = e0 - k1 * r0;
  1137. ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
  1138. ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
  1139. ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
  1140. ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
  1141. ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
  1142. ps->r0 = flt16_trunc(a * e0);
  1143. }
  1144. /**
  1145. * Apply AAC-Main style frequency domain prediction.
  1146. */
  1147. static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
  1148. {
  1149. int sfb, k;
  1150. if (!sce->ics.predictor_initialized) {
  1151. reset_all_predictors(sce->predictor_state);
  1152. sce->ics.predictor_initialized = 1;
  1153. }
  1154. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  1155. for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
  1156. for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
  1157. predict(&sce->predictor_state[k], &sce->coeffs[k],
  1158. sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
  1159. }
  1160. }
  1161. if (sce->ics.predictor_reset_group)
  1162. reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
  1163. } else
  1164. reset_all_predictors(sce->predictor_state);
  1165. }
  1166. /**
  1167. * Decode an individual_channel_stream payload; reference: table 4.44.
  1168. *
  1169. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  1170. * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
  1171. *
  1172. * @return Returns error status. 0 - OK, !0 - error
  1173. */
  1174. static int decode_ics(AACContext *ac, SingleChannelElement *sce,
  1175. GetBitContext *gb, int common_window, int scale_flag)
  1176. {
  1177. Pulse pulse;
  1178. TemporalNoiseShaping *tns = &sce->tns;
  1179. IndividualChannelStream *ics = &sce->ics;
  1180. float *out = sce->coeffs;
  1181. int global_gain, pulse_present = 0;
  1182. /* This assignment is to silence a GCC warning about the variable being used
  1183. * uninitialized when in fact it always is.
  1184. */
  1185. pulse.num_pulse = 0;
  1186. global_gain = get_bits(gb, 8);
  1187. if (!common_window && !scale_flag) {
  1188. if (decode_ics_info(ac, ics, gb, 0) < 0)
  1189. return -1;
  1190. }
  1191. if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
  1192. return -1;
  1193. if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
  1194. return -1;
  1195. pulse_present = 0;
  1196. if (!scale_flag) {
  1197. if ((pulse_present = get_bits1(gb))) {
  1198. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1199. av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
  1200. return -1;
  1201. }
  1202. if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
  1203. av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
  1204. return -1;
  1205. }
  1206. }
  1207. if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
  1208. return -1;
  1209. if (get_bits1(gb)) {
  1210. av_log_missing_feature(ac->avctx, "SSR", 1);
  1211. return -1;
  1212. }
  1213. }
  1214. if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
  1215. return -1;
  1216. if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
  1217. apply_prediction(ac, sce);
  1218. return 0;
  1219. }
  1220. /**
  1221. * Mid/Side stereo decoding; reference: 4.6.8.1.3.
  1222. */
  1223. static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
  1224. {
  1225. const IndividualChannelStream *ics = &cpe->ch[0].ics;
  1226. float *ch0 = cpe->ch[0].coeffs;
  1227. float *ch1 = cpe->ch[1].coeffs;
  1228. int g, i, group, idx = 0;
  1229. const uint16_t *offsets = ics->swb_offset;
  1230. for (g = 0; g < ics->num_window_groups; g++) {
  1231. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1232. if (cpe->ms_mask[idx] &&
  1233. cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
  1234. for (group = 0; group < ics->group_len[g]; group++) {
  1235. ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
  1236. ch1 + group * 128 + offsets[i],
  1237. offsets[i+1] - offsets[i]);
  1238. }
  1239. }
  1240. }
  1241. ch0 += ics->group_len[g] * 128;
  1242. ch1 += ics->group_len[g] * 128;
  1243. }
  1244. }
  1245. /**
  1246. * intensity stereo decoding; reference: 4.6.8.2.3
  1247. *
  1248. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  1249. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  1250. * [3] reserved for scalable AAC
  1251. */
  1252. static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
  1253. {
  1254. const IndividualChannelStream *ics = &cpe->ch[1].ics;
  1255. SingleChannelElement *sce1 = &cpe->ch[1];
  1256. float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
  1257. const uint16_t *offsets = ics->swb_offset;
  1258. int g, group, i, idx = 0;
  1259. int c;
  1260. float scale;
  1261. for (g = 0; g < ics->num_window_groups; g++) {
  1262. for (i = 0; i < ics->max_sfb;) {
  1263. if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
  1264. const int bt_run_end = sce1->band_type_run_end[idx];
  1265. for (; i < bt_run_end; i++, idx++) {
  1266. c = -1 + 2 * (sce1->band_type[idx] - 14);
  1267. if (ms_present)
  1268. c *= 1 - 2 * cpe->ms_mask[idx];
  1269. scale = c * sce1->sf[idx];
  1270. for (group = 0; group < ics->group_len[g]; group++)
  1271. ac->dsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
  1272. coef0 + group * 128 + offsets[i],
  1273. scale,
  1274. offsets[i + 1] - offsets[i]);
  1275. }
  1276. } else {
  1277. int bt_run_end = sce1->band_type_run_end[idx];
  1278. idx += bt_run_end - i;
  1279. i = bt_run_end;
  1280. }
  1281. }
  1282. coef0 += ics->group_len[g] * 128;
  1283. coef1 += ics->group_len[g] * 128;
  1284. }
  1285. }
  1286. /**
  1287. * Decode a channel_pair_element; reference: table 4.4.
  1288. *
  1289. * @return Returns error status. 0 - OK, !0 - error
  1290. */
  1291. static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
  1292. {
  1293. int i, ret, common_window, ms_present = 0;
  1294. common_window = get_bits1(gb);
  1295. if (common_window) {
  1296. if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
  1297. return -1;
  1298. i = cpe->ch[1].ics.use_kb_window[0];
  1299. cpe->ch[1].ics = cpe->ch[0].ics;
  1300. cpe->ch[1].ics.use_kb_window[1] = i;
  1301. if (cpe->ch[1].ics.predictor_present && (ac->m4ac.object_type != AOT_AAC_MAIN))
  1302. if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
  1303. decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
  1304. ms_present = get_bits(gb, 2);
  1305. if (ms_present == 3) {
  1306. av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
  1307. return -1;
  1308. } else if (ms_present)
  1309. decode_mid_side_stereo(cpe, gb, ms_present);
  1310. }
  1311. if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
  1312. return ret;
  1313. if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
  1314. return ret;
  1315. if (common_window) {
  1316. if (ms_present)
  1317. apply_mid_side_stereo(ac, cpe);
  1318. if (ac->m4ac.object_type == AOT_AAC_MAIN) {
  1319. apply_prediction(ac, &cpe->ch[0]);
  1320. apply_prediction(ac, &cpe->ch[1]);
  1321. }
  1322. }
  1323. apply_intensity_stereo(ac, cpe, ms_present);
  1324. return 0;
  1325. }
  1326. static const float cce_scale[] = {
  1327. 1.09050773266525765921, //2^(1/8)
  1328. 1.18920711500272106672, //2^(1/4)
  1329. M_SQRT2,
  1330. 2,
  1331. };
  1332. /**
  1333. * Decode coupling_channel_element; reference: table 4.8.
  1334. *
  1335. * @return Returns error status. 0 - OK, !0 - error
  1336. */
  1337. static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
  1338. {
  1339. int num_gain = 0;
  1340. int c, g, sfb, ret;
  1341. int sign;
  1342. float scale;
  1343. SingleChannelElement *sce = &che->ch[0];
  1344. ChannelCoupling *coup = &che->coup;
  1345. coup->coupling_point = 2 * get_bits1(gb);
  1346. coup->num_coupled = get_bits(gb, 3);
  1347. for (c = 0; c <= coup->num_coupled; c++) {
  1348. num_gain++;
  1349. coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
  1350. coup->id_select[c] = get_bits(gb, 4);
  1351. if (coup->type[c] == TYPE_CPE) {
  1352. coup->ch_select[c] = get_bits(gb, 2);
  1353. if (coup->ch_select[c] == 3)
  1354. num_gain++;
  1355. } else
  1356. coup->ch_select[c] = 2;
  1357. }
  1358. coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
  1359. sign = get_bits(gb, 1);
  1360. scale = cce_scale[get_bits(gb, 2)];
  1361. if ((ret = decode_ics(ac, sce, gb, 0, 0)))
  1362. return ret;
  1363. for (c = 0; c < num_gain; c++) {
  1364. int idx = 0;
  1365. int cge = 1;
  1366. int gain = 0;
  1367. float gain_cache = 1.;
  1368. if (c) {
  1369. cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
  1370. gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
  1371. gain_cache = powf(scale, -gain);
  1372. }
  1373. if (coup->coupling_point == AFTER_IMDCT) {
  1374. coup->gain[c][0] = gain_cache;
  1375. } else {
  1376. for (g = 0; g < sce->ics.num_window_groups; g++) {
  1377. for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
  1378. if (sce->band_type[idx] != ZERO_BT) {
  1379. if (!cge) {
  1380. int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1381. if (t) {
  1382. int s = 1;
  1383. t = gain += t;
  1384. if (sign) {
  1385. s -= 2 * (t & 0x1);
  1386. t >>= 1;
  1387. }
  1388. gain_cache = powf(scale, -t) * s;
  1389. }
  1390. }
  1391. coup->gain[c][idx] = gain_cache;
  1392. }
  1393. }
  1394. }
  1395. }
  1396. }
  1397. return 0;
  1398. }
  1399. /**
  1400. * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
  1401. *
  1402. * @return Returns number of bytes consumed.
  1403. */
  1404. static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
  1405. GetBitContext *gb)
  1406. {
  1407. int i;
  1408. int num_excl_chan = 0;
  1409. do {
  1410. for (i = 0; i < 7; i++)
  1411. che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
  1412. } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
  1413. return num_excl_chan / 7;
  1414. }
  1415. /**
  1416. * Decode dynamic range information; reference: table 4.52.
  1417. *
  1418. * @param cnt length of TYPE_FIL syntactic element in bytes
  1419. *
  1420. * @return Returns number of bytes consumed.
  1421. */
  1422. static int decode_dynamic_range(DynamicRangeControl *che_drc,
  1423. GetBitContext *gb, int cnt)
  1424. {
  1425. int n = 1;
  1426. int drc_num_bands = 1;
  1427. int i;
  1428. /* pce_tag_present? */
  1429. if (get_bits1(gb)) {
  1430. che_drc->pce_instance_tag = get_bits(gb, 4);
  1431. skip_bits(gb, 4); // tag_reserved_bits
  1432. n++;
  1433. }
  1434. /* excluded_chns_present? */
  1435. if (get_bits1(gb)) {
  1436. n += decode_drc_channel_exclusions(che_drc, gb);
  1437. }
  1438. /* drc_bands_present? */
  1439. if (get_bits1(gb)) {
  1440. che_drc->band_incr = get_bits(gb, 4);
  1441. che_drc->interpolation_scheme = get_bits(gb, 4);
  1442. n++;
  1443. drc_num_bands += che_drc->band_incr;
  1444. for (i = 0; i < drc_num_bands; i++) {
  1445. che_drc->band_top[i] = get_bits(gb, 8);
  1446. n++;
  1447. }
  1448. }
  1449. /* prog_ref_level_present? */
  1450. if (get_bits1(gb)) {
  1451. che_drc->prog_ref_level = get_bits(gb, 7);
  1452. skip_bits1(gb); // prog_ref_level_reserved_bits
  1453. n++;
  1454. }
  1455. for (i = 0; i < drc_num_bands; i++) {
  1456. che_drc->dyn_rng_sgn[i] = get_bits1(gb);
  1457. che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
  1458. n++;
  1459. }
  1460. return n;
  1461. }
  1462. /**
  1463. * Decode extension data (incomplete); reference: table 4.51.
  1464. *
  1465. * @param cnt length of TYPE_FIL syntactic element in bytes
  1466. *
  1467. * @return Returns number of bytes consumed
  1468. */
  1469. static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
  1470. ChannelElement *che, enum RawDataBlockType elem_type)
  1471. {
  1472. int crc_flag = 0;
  1473. int res = cnt;
  1474. switch (get_bits(gb, 4)) { // extension type
  1475. case EXT_SBR_DATA_CRC:
  1476. crc_flag++;
  1477. case EXT_SBR_DATA:
  1478. if (!che) {
  1479. av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
  1480. return res;
  1481. } else if (!ac->m4ac.sbr) {
  1482. av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
  1483. skip_bits_long(gb, 8 * cnt - 4);
  1484. return res;
  1485. } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
  1486. av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
  1487. skip_bits_long(gb, 8 * cnt - 4);
  1488. return res;
  1489. } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) {
  1490. ac->m4ac.sbr = 1;
  1491. ac->m4ac.ps = 1;
  1492. output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured);
  1493. } else {
  1494. ac->m4ac.sbr = 1;
  1495. }
  1496. res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
  1497. break;
  1498. case EXT_DYNAMIC_RANGE:
  1499. res = decode_dynamic_range(&ac->che_drc, gb, cnt);
  1500. break;
  1501. case EXT_FILL:
  1502. case EXT_FILL_DATA:
  1503. case EXT_DATA_ELEMENT:
  1504. default:
  1505. skip_bits_long(gb, 8 * cnt - 4);
  1506. break;
  1507. };
  1508. return res;
  1509. }
  1510. /**
  1511. * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
  1512. *
  1513. * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
  1514. * @param coef spectral coefficients
  1515. */
  1516. static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
  1517. IndividualChannelStream *ics, int decode)
  1518. {
  1519. const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
  1520. int w, filt, m, i;
  1521. int bottom, top, order, start, end, size, inc;
  1522. float lpc[TNS_MAX_ORDER];
  1523. float tmp[TNS_MAX_ORDER];
  1524. for (w = 0; w < ics->num_windows; w++) {
  1525. bottom = ics->num_swb;
  1526. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  1527. top = bottom;
  1528. bottom = FFMAX(0, top - tns->length[w][filt]);
  1529. order = tns->order[w][filt];
  1530. if (order == 0)
  1531. continue;
  1532. // tns_decode_coef
  1533. compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
  1534. start = ics->swb_offset[FFMIN(bottom, mmm)];
  1535. end = ics->swb_offset[FFMIN( top, mmm)];
  1536. if ((size = end - start) <= 0)
  1537. continue;
  1538. if (tns->direction[w][filt]) {
  1539. inc = -1;
  1540. start = end - 1;
  1541. } else {
  1542. inc = 1;
  1543. }
  1544. start += w * 128;
  1545. if (decode) {
  1546. // ar filter
  1547. for (m = 0; m < size; m++, start += inc)
  1548. for (i = 1; i <= FFMIN(m, order); i++)
  1549. coef[start] -= coef[start - i * inc] * lpc[i - 1];
  1550. } else {
  1551. // ma filter
  1552. for (m = 0; m < size; m++, start += inc) {
  1553. tmp[0] = coef[start];
  1554. for (i = 1; i <= FFMIN(m, order); i++)
  1555. coef[start] += tmp[i] * lpc[i - 1];
  1556. for (i = order; i > 0; i--)
  1557. tmp[i] = tmp[i - 1];
  1558. }
  1559. }
  1560. }
  1561. }
  1562. }
  1563. /**
  1564. * Apply windowing and MDCT to obtain the spectral
  1565. * coefficient from the predicted sample by LTP.
  1566. */
  1567. static void windowing_and_mdct_ltp(AACContext *ac, float *out,
  1568. float *in, IndividualChannelStream *ics)
  1569. {
  1570. const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  1571. const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  1572. const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  1573. const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  1574. if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
  1575. ac->dsp.vector_fmul(in, in, lwindow_prev, 1024);
  1576. } else {
  1577. memset(in, 0, 448 * sizeof(float));
  1578. ac->dsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
  1579. memcpy(in + 576, in + 576, 448 * sizeof(float));
  1580. }
  1581. if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
  1582. ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
  1583. } else {
  1584. memcpy(in + 1024, in + 1024, 448 * sizeof(float));
  1585. ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
  1586. memset(in + 1024 + 576, 0, 448 * sizeof(float));
  1587. }
  1588. ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
  1589. }
  1590. /**
  1591. * Apply the long term prediction
  1592. */
  1593. static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
  1594. {
  1595. const LongTermPrediction *ltp = &sce->ics.ltp;
  1596. const uint16_t *offsets = sce->ics.swb_offset;
  1597. int i, sfb;
  1598. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  1599. float *predTime = sce->ret;
  1600. float *predFreq = ac->buf_mdct;
  1601. int16_t num_samples = 2048;
  1602. if (ltp->lag < 1024)
  1603. num_samples = ltp->lag + 1024;
  1604. for (i = 0; i < num_samples; i++)
  1605. predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
  1606. memset(&predTime[i], 0, (2048 - i) * sizeof(float));
  1607. windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
  1608. if (sce->tns.present)
  1609. apply_tns(predFreq, &sce->tns, &sce->ics, 0);
  1610. for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
  1611. if (ltp->used[sfb])
  1612. for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
  1613. sce->coeffs[i] += predFreq[i];
  1614. }
  1615. }
  1616. /**
  1617. * Update the LTP buffer for next frame
  1618. */
  1619. static void update_ltp(AACContext *ac, SingleChannelElement *sce)
  1620. {
  1621. IndividualChannelStream *ics = &sce->ics;
  1622. float *saved = sce->saved;
  1623. float *saved_ltp = sce->coeffs;
  1624. const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  1625. const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  1626. int i;
  1627. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1628. memcpy(saved_ltp, saved, 512 * sizeof(float));
  1629. memset(saved_ltp + 576, 0, 448 * sizeof(float));
  1630. ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
  1631. for (i = 0; i < 64; i++)
  1632. saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
  1633. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  1634. memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
  1635. memset(saved_ltp + 576, 0, 448 * sizeof(float));
  1636. ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
  1637. for (i = 0; i < 64; i++)
  1638. saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
  1639. } else { // LONG_STOP or ONLY_LONG
  1640. ac->dsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
  1641. for (i = 0; i < 512; i++)
  1642. saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
  1643. }
  1644. memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
  1645. memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
  1646. memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
  1647. }
  1648. /**
  1649. * Conduct IMDCT and windowing.
  1650. */
  1651. static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
  1652. {
  1653. IndividualChannelStream *ics = &sce->ics;
  1654. float *in = sce->coeffs;
  1655. float *out = sce->ret;
  1656. float *saved = sce->saved;
  1657. const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  1658. const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  1659. const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  1660. float *buf = ac->buf_mdct;
  1661. float *temp = ac->temp;
  1662. int i;
  1663. // imdct
  1664. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1665. for (i = 0; i < 1024; i += 128)
  1666. ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
  1667. } else
  1668. ac->mdct.imdct_half(&ac->mdct, buf, in);
  1669. /* window overlapping
  1670. * NOTE: To simplify the overlapping code, all 'meaningless' short to long
  1671. * and long to short transitions are considered to be short to short
  1672. * transitions. This leaves just two cases (long to long and short to short)
  1673. * with a little special sauce for EIGHT_SHORT_SEQUENCE.
  1674. */
  1675. if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
  1676. (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
  1677. ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
  1678. } else {
  1679. memcpy( out, saved, 448 * sizeof(float));
  1680. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1681. ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
  1682. ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
  1683. ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
  1684. ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
  1685. ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
  1686. memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
  1687. } else {
  1688. ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
  1689. memcpy( out + 576, buf + 64, 448 * sizeof(float));
  1690. }
  1691. }
  1692. // buffer update
  1693. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1694. memcpy( saved, temp + 64, 64 * sizeof(float));
  1695. ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
  1696. ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
  1697. ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
  1698. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  1699. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  1700. memcpy( saved, buf + 512, 448 * sizeof(float));
  1701. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  1702. } else { // LONG_STOP or ONLY_LONG
  1703. memcpy( saved, buf + 512, 512 * sizeof(float));
  1704. }
  1705. }
  1706. /**
  1707. * Apply dependent channel coupling (applied before IMDCT).
  1708. *
  1709. * @param index index into coupling gain array
  1710. */
  1711. static void apply_dependent_coupling(AACContext *ac,
  1712. SingleChannelElement *target,
  1713. ChannelElement *cce, int index)
  1714. {
  1715. IndividualChannelStream *ics = &cce->ch[0].ics;
  1716. const uint16_t *offsets = ics->swb_offset;
  1717. float *dest = target->coeffs;
  1718. const float *src = cce->ch[0].coeffs;
  1719. int g, i, group, k, idx = 0;
  1720. if (ac->m4ac.object_type == AOT_AAC_LTP) {
  1721. av_log(ac->avctx, AV_LOG_ERROR,
  1722. "Dependent coupling is not supported together with LTP\n");
  1723. return;
  1724. }
  1725. for (g = 0; g < ics->num_window_groups; g++) {
  1726. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1727. if (cce->ch[0].band_type[idx] != ZERO_BT) {
  1728. const float gain = cce->coup.gain[index][idx];
  1729. for (group = 0; group < ics->group_len[g]; group++) {
  1730. for (k = offsets[i]; k < offsets[i + 1]; k++) {
  1731. // XXX dsputil-ize
  1732. dest[group * 128 + k] += gain * src[group * 128 + k];
  1733. }
  1734. }
  1735. }
  1736. }
  1737. dest += ics->group_len[g] * 128;
  1738. src += ics->group_len[g] * 128;
  1739. }
  1740. }
  1741. /**
  1742. * Apply independent channel coupling (applied after IMDCT).
  1743. *
  1744. * @param index index into coupling gain array
  1745. */
  1746. static void apply_independent_coupling(AACContext *ac,
  1747. SingleChannelElement *target,
  1748. ChannelElement *cce, int index)
  1749. {
  1750. int i;
  1751. const float gain = cce->coup.gain[index][0];
  1752. const float *src = cce->ch[0].ret;
  1753. float *dest = target->ret;
  1754. const int len = 1024 << (ac->m4ac.sbr == 1);
  1755. for (i = 0; i < len; i++)
  1756. dest[i] += gain * src[i];
  1757. }
  1758. /**
  1759. * channel coupling transformation interface
  1760. *
  1761. * @param apply_coupling_method pointer to (in)dependent coupling function
  1762. */
  1763. static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
  1764. enum RawDataBlockType type, int elem_id,
  1765. enum CouplingPoint coupling_point,
  1766. void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
  1767. {
  1768. int i, c;
  1769. for (i = 0; i < MAX_ELEM_ID; i++) {
  1770. ChannelElement *cce = ac->che[TYPE_CCE][i];
  1771. int index = 0;
  1772. if (cce && cce->coup.coupling_point == coupling_point) {
  1773. ChannelCoupling *coup = &cce->coup;
  1774. for (c = 0; c <= coup->num_coupled; c++) {
  1775. if (coup->type[c] == type && coup->id_select[c] == elem_id) {
  1776. if (coup->ch_select[c] != 1) {
  1777. apply_coupling_method(ac, &cc->ch[0], cce, index);
  1778. if (coup->ch_select[c] != 0)
  1779. index++;
  1780. }
  1781. if (coup->ch_select[c] != 2)
  1782. apply_coupling_method(ac, &cc->ch[1], cce, index++);
  1783. } else
  1784. index += 1 + (coup->ch_select[c] == 3);
  1785. }
  1786. }
  1787. }
  1788. }
  1789. /**
  1790. * Convert spectral data to float samples, applying all supported tools as appropriate.
  1791. */
  1792. static void spectral_to_sample(AACContext *ac)
  1793. {
  1794. int i, type;
  1795. for (type = 3; type >= 0; type--) {
  1796. for (i = 0; i < MAX_ELEM_ID; i++) {
  1797. ChannelElement *che = ac->che[type][i];
  1798. if (che) {
  1799. if (type <= TYPE_CPE)
  1800. apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
  1801. if (ac->m4ac.object_type == AOT_AAC_LTP) {
  1802. if (che->ch[0].ics.predictor_present) {
  1803. if (che->ch[0].ics.ltp.present)
  1804. apply_ltp(ac, &che->ch[0]);
  1805. if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
  1806. apply_ltp(ac, &che->ch[1]);
  1807. }
  1808. }
  1809. if (che->ch[0].tns.present)
  1810. apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
  1811. if (che->ch[1].tns.present)
  1812. apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
  1813. if (type <= TYPE_CPE)
  1814. apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
  1815. if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
  1816. imdct_and_windowing(ac, &che->ch[0]);
  1817. if (ac->m4ac.object_type == AOT_AAC_LTP)
  1818. update_ltp(ac, &che->ch[0]);
  1819. if (type == TYPE_CPE) {
  1820. imdct_and_windowing(ac, &che->ch[1]);
  1821. if (ac->m4ac.object_type == AOT_AAC_LTP)
  1822. update_ltp(ac, &che->ch[1]);
  1823. }
  1824. if (ac->m4ac.sbr > 0) {
  1825. ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
  1826. }
  1827. }
  1828. if (type <= TYPE_CCE)
  1829. apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
  1830. }
  1831. }
  1832. }
  1833. }
  1834. static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
  1835. {
  1836. int size;
  1837. AACADTSHeaderInfo hdr_info;
  1838. size = ff_aac_parse_header(gb, &hdr_info);
  1839. if (size > 0) {
  1840. if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
  1841. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  1842. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  1843. ac->m4ac.chan_config = hdr_info.chan_config;
  1844. if (set_default_channel_config(ac->avctx, new_che_pos, hdr_info.chan_config))
  1845. return -7;
  1846. if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
  1847. return -7;
  1848. } else if (ac->output_configured != OC_LOCKED) {
  1849. ac->output_configured = OC_NONE;
  1850. }
  1851. if (ac->output_configured != OC_LOCKED) {
  1852. ac->m4ac.sbr = -1;
  1853. ac->m4ac.ps = -1;
  1854. }
  1855. ac->m4ac.sample_rate = hdr_info.sample_rate;
  1856. ac->m4ac.sampling_index = hdr_info.sampling_index;
  1857. ac->m4ac.object_type = hdr_info.object_type;
  1858. if (!ac->avctx->sample_rate)
  1859. ac->avctx->sample_rate = hdr_info.sample_rate;
  1860. if (hdr_info.num_aac_frames == 1) {
  1861. if (!hdr_info.crc_absent)
  1862. skip_bits(gb, 16);
  1863. } else {
  1864. av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
  1865. return -1;
  1866. }
  1867. }
  1868. return size;
  1869. }
  1870. static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
  1871. int *data_size, GetBitContext *gb)
  1872. {
  1873. AACContext *ac = avctx->priv_data;
  1874. ChannelElement *che = NULL, *che_prev = NULL;
  1875. enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
  1876. int err, elem_id, data_size_tmp;
  1877. int samples = 0, multiplier;
  1878. if (show_bits(gb, 12) == 0xfff) {
  1879. if (parse_adts_frame_header(ac, gb) < 0) {
  1880. av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
  1881. return -1;
  1882. }
  1883. if (ac->m4ac.sampling_index > 12) {
  1884. av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
  1885. return -1;
  1886. }
  1887. }
  1888. ac->tags_mapped = 0;
  1889. // parse
  1890. while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
  1891. elem_id = get_bits(gb, 4);
  1892. if (elem_type < TYPE_DSE) {
  1893. if (!(che=get_che(ac, elem_type, elem_id))) {
  1894. av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
  1895. elem_type, elem_id);
  1896. return -1;
  1897. }
  1898. samples = 1024;
  1899. }
  1900. switch (elem_type) {
  1901. case TYPE_SCE:
  1902. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  1903. break;
  1904. case TYPE_CPE:
  1905. err = decode_cpe(ac, gb, che);
  1906. break;
  1907. case TYPE_CCE:
  1908. err = decode_cce(ac, gb, che);
  1909. break;
  1910. case TYPE_LFE:
  1911. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  1912. break;
  1913. case TYPE_DSE:
  1914. err = skip_data_stream_element(ac, gb);
  1915. break;
  1916. case TYPE_PCE: {
  1917. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  1918. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  1919. if ((err = decode_pce(avctx, &ac->m4ac, new_che_pos, gb)))
  1920. break;
  1921. if (ac->output_configured > OC_TRIAL_PCE)
  1922. av_log(avctx, AV_LOG_ERROR,
  1923. "Not evaluating a further program_config_element as this construct is dubious at best.\n");
  1924. else
  1925. err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
  1926. break;
  1927. }
  1928. case TYPE_FIL:
  1929. if (elem_id == 15)
  1930. elem_id += get_bits(gb, 8) - 1;
  1931. if (get_bits_left(gb) < 8 * elem_id) {
  1932. av_log(avctx, AV_LOG_ERROR, overread_err);
  1933. return -1;
  1934. }
  1935. while (elem_id > 0)
  1936. elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
  1937. err = 0; /* FIXME */
  1938. break;
  1939. default:
  1940. err = -1; /* should not happen, but keeps compiler happy */
  1941. break;
  1942. }
  1943. che_prev = che;
  1944. elem_type_prev = elem_type;
  1945. if (err)
  1946. return err;
  1947. if (get_bits_left(gb) < 3) {
  1948. av_log(avctx, AV_LOG_ERROR, overread_err);
  1949. return -1;
  1950. }
  1951. }
  1952. spectral_to_sample(ac);
  1953. multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
  1954. samples <<= multiplier;
  1955. if (ac->output_configured < OC_LOCKED) {
  1956. avctx->sample_rate = ac->m4ac.sample_rate << multiplier;
  1957. avctx->frame_size = samples;
  1958. }
  1959. data_size_tmp = samples * avctx->channels *
  1960. av_get_bytes_per_sample(avctx->sample_fmt);
  1961. if (*data_size < data_size_tmp) {
  1962. av_log(avctx, AV_LOG_ERROR,
  1963. "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
  1964. *data_size, data_size_tmp);
  1965. return -1;
  1966. }
  1967. *data_size = data_size_tmp;
  1968. if (samples) {
  1969. if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
  1970. ac->fmt_conv.float_interleave(data, (const float **)ac->output_data,
  1971. samples, avctx->channels);
  1972. else
  1973. ac->fmt_conv.float_to_int16_interleave(data, (const float **)ac->output_data,
  1974. samples, avctx->channels);
  1975. }
  1976. if (ac->output_configured)
  1977. ac->output_configured = OC_LOCKED;
  1978. return 0;
  1979. }
  1980. static int aac_decode_frame(AVCodecContext *avctx, void *data,
  1981. int *data_size, AVPacket *avpkt)
  1982. {
  1983. const uint8_t *buf = avpkt->data;
  1984. int buf_size = avpkt->size;
  1985. GetBitContext gb;
  1986. int buf_consumed;
  1987. int buf_offset;
  1988. int err;
  1989. init_get_bits(&gb, buf, buf_size * 8);
  1990. if ((err = aac_decode_frame_int(avctx, data, data_size, &gb)) < 0)
  1991. return err;
  1992. buf_consumed = (get_bits_count(&gb) + 7) >> 3;
  1993. for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
  1994. if (buf[buf_offset])
  1995. break;
  1996. return buf_size > buf_offset ? buf_consumed : buf_size;
  1997. }
  1998. static av_cold int aac_decode_close(AVCodecContext *avctx)
  1999. {
  2000. AACContext *ac = avctx->priv_data;
  2001. int i, type;
  2002. for (i = 0; i < MAX_ELEM_ID; i++) {
  2003. for (type = 0; type < 4; type++) {
  2004. if (ac->che[type][i])
  2005. ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
  2006. av_freep(&ac->che[type][i]);
  2007. }
  2008. }
  2009. ff_mdct_end(&ac->mdct);
  2010. ff_mdct_end(&ac->mdct_small);
  2011. ff_mdct_end(&ac->mdct_ltp);
  2012. return 0;
  2013. }
  2014. #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
  2015. struct LATMContext {
  2016. AACContext aac_ctx; ///< containing AACContext
  2017. int initialized; ///< initilized after a valid extradata was seen
  2018. // parser data
  2019. int audio_mux_version_A; ///< LATM syntax version
  2020. int frame_length_type; ///< 0/1 variable/fixed frame length
  2021. int frame_length; ///< frame length for fixed frame length
  2022. };
  2023. static inline uint32_t latm_get_value(GetBitContext *b)
  2024. {
  2025. int length = get_bits(b, 2);
  2026. return get_bits_long(b, (length+1)*8);
  2027. }
  2028. static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
  2029. GetBitContext *gb)
  2030. {
  2031. AVCodecContext *avctx = latmctx->aac_ctx.avctx;
  2032. MPEG4AudioConfig m4ac;
  2033. int config_start_bit = get_bits_count(gb);
  2034. int bits_consumed, esize;
  2035. if (config_start_bit % 8) {
  2036. av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
  2037. "config not byte aligned.\n", 1);
  2038. return AVERROR_INVALIDDATA;
  2039. } else {
  2040. bits_consumed =
  2041. decode_audio_specific_config(NULL, avctx, &m4ac,
  2042. gb->buffer + (config_start_bit / 8),
  2043. get_bits_left(gb) / 8);
  2044. if (bits_consumed < 0)
  2045. return AVERROR_INVALIDDATA;
  2046. esize = (bits_consumed+7) / 8;
  2047. if (avctx->extradata_size <= esize) {
  2048. av_free(avctx->extradata);
  2049. avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
  2050. if (!avctx->extradata)
  2051. return AVERROR(ENOMEM);
  2052. }
  2053. avctx->extradata_size = esize;
  2054. memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
  2055. memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  2056. skip_bits_long(gb, bits_consumed);
  2057. }
  2058. return bits_consumed;
  2059. }
  2060. static int read_stream_mux_config(struct LATMContext *latmctx,
  2061. GetBitContext *gb)
  2062. {
  2063. int ret, audio_mux_version = get_bits(gb, 1);
  2064. latmctx->audio_mux_version_A = 0;
  2065. if (audio_mux_version)
  2066. latmctx->audio_mux_version_A = get_bits(gb, 1);
  2067. if (!latmctx->audio_mux_version_A) {
  2068. if (audio_mux_version)
  2069. latm_get_value(gb); // taraFullness
  2070. skip_bits(gb, 1); // allStreamSameTimeFraming
  2071. skip_bits(gb, 6); // numSubFrames
  2072. // numPrograms
  2073. if (get_bits(gb, 4)) { // numPrograms
  2074. av_log_missing_feature(latmctx->aac_ctx.avctx,
  2075. "multiple programs are not supported\n", 1);
  2076. return AVERROR_PATCHWELCOME;
  2077. }
  2078. // for each program (which there is only on in DVB)
  2079. // for each layer (which there is only on in DVB)
  2080. if (get_bits(gb, 3)) { // numLayer
  2081. av_log_missing_feature(latmctx->aac_ctx.avctx,
  2082. "multiple layers are not supported\n", 1);
  2083. return AVERROR_PATCHWELCOME;
  2084. }
  2085. // for all but first stream: use_same_config = get_bits(gb, 1);
  2086. if (!audio_mux_version) {
  2087. if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
  2088. return ret;
  2089. } else {
  2090. int ascLen = latm_get_value(gb);
  2091. if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
  2092. return ret;
  2093. ascLen -= ret;
  2094. skip_bits_long(gb, ascLen);
  2095. }
  2096. latmctx->frame_length_type = get_bits(gb, 3);
  2097. switch (latmctx->frame_length_type) {
  2098. case 0:
  2099. skip_bits(gb, 8); // latmBufferFullness
  2100. break;
  2101. case 1:
  2102. latmctx->frame_length = get_bits(gb, 9);
  2103. break;
  2104. case 3:
  2105. case 4:
  2106. case 5:
  2107. skip_bits(gb, 6); // CELP frame length table index
  2108. break;
  2109. case 6:
  2110. case 7:
  2111. skip_bits(gb, 1); // HVXC frame length table index
  2112. break;
  2113. }
  2114. if (get_bits(gb, 1)) { // other data
  2115. if (audio_mux_version) {
  2116. latm_get_value(gb); // other_data_bits
  2117. } else {
  2118. int esc;
  2119. do {
  2120. esc = get_bits(gb, 1);
  2121. skip_bits(gb, 8);
  2122. } while (esc);
  2123. }
  2124. }
  2125. if (get_bits(gb, 1)) // crc present
  2126. skip_bits(gb, 8); // config_crc
  2127. }
  2128. return 0;
  2129. }
  2130. static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
  2131. {
  2132. uint8_t tmp;
  2133. if (ctx->frame_length_type == 0) {
  2134. int mux_slot_length = 0;
  2135. do {
  2136. tmp = get_bits(gb, 8);
  2137. mux_slot_length += tmp;
  2138. } while (tmp == 255);
  2139. return mux_slot_length;
  2140. } else if (ctx->frame_length_type == 1) {
  2141. return ctx->frame_length;
  2142. } else if (ctx->frame_length_type == 3 ||
  2143. ctx->frame_length_type == 5 ||
  2144. ctx->frame_length_type == 7) {
  2145. skip_bits(gb, 2); // mux_slot_length_coded
  2146. }
  2147. return 0;
  2148. }
  2149. static int read_audio_mux_element(struct LATMContext *latmctx,
  2150. GetBitContext *gb)
  2151. {
  2152. int err;
  2153. uint8_t use_same_mux = get_bits(gb, 1);
  2154. if (!use_same_mux) {
  2155. if ((err = read_stream_mux_config(latmctx, gb)) < 0)
  2156. return err;
  2157. } else if (!latmctx->aac_ctx.avctx->extradata) {
  2158. av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
  2159. "no decoder config found\n");
  2160. return AVERROR(EAGAIN);
  2161. }
  2162. if (latmctx->audio_mux_version_A == 0) {
  2163. int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
  2164. if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
  2165. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
  2166. return AVERROR_INVALIDDATA;
  2167. } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
  2168. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  2169. "frame length mismatch %d << %d\n",
  2170. mux_slot_length_bytes * 8, get_bits_left(gb));
  2171. return AVERROR_INVALIDDATA;
  2172. }
  2173. }
  2174. return 0;
  2175. }
  2176. static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size,
  2177. AVPacket *avpkt)
  2178. {
  2179. struct LATMContext *latmctx = avctx->priv_data;
  2180. int muxlength, err;
  2181. GetBitContext gb;
  2182. if (avpkt->size == 0)
  2183. return 0;
  2184. init_get_bits(&gb, avpkt->data, avpkt->size * 8);
  2185. // check for LOAS sync word
  2186. if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
  2187. return AVERROR_INVALIDDATA;
  2188. muxlength = get_bits(&gb, 13) + 3;
  2189. // not enough data, the parser should have sorted this
  2190. if (muxlength > avpkt->size)
  2191. return AVERROR_INVALIDDATA;
  2192. if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
  2193. return err;
  2194. if (!latmctx->initialized) {
  2195. if (!avctx->extradata) {
  2196. *out_size = 0;
  2197. return avpkt->size;
  2198. } else {
  2199. aac_decode_close(avctx);
  2200. if ((err = aac_decode_init(avctx)) < 0)
  2201. return err;
  2202. latmctx->initialized = 1;
  2203. }
  2204. }
  2205. if (show_bits(&gb, 12) == 0xfff) {
  2206. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  2207. "ADTS header detected, probably as result of configuration "
  2208. "misparsing\n");
  2209. return AVERROR_INVALIDDATA;
  2210. }
  2211. if ((err = aac_decode_frame_int(avctx, out, out_size, &gb)) < 0)
  2212. return err;
  2213. return muxlength;
  2214. }
  2215. av_cold static int latm_decode_init(AVCodecContext *avctx)
  2216. {
  2217. struct LATMContext *latmctx = avctx->priv_data;
  2218. int ret;
  2219. ret = aac_decode_init(avctx);
  2220. if (avctx->extradata_size > 0) {
  2221. latmctx->initialized = !ret;
  2222. } else {
  2223. latmctx->initialized = 0;
  2224. }
  2225. return ret;
  2226. }
  2227. AVCodec ff_aac_decoder = {
  2228. "aac",
  2229. AVMEDIA_TYPE_AUDIO,
  2230. CODEC_ID_AAC,
  2231. sizeof(AACContext),
  2232. aac_decode_init,
  2233. NULL,
  2234. aac_decode_close,
  2235. aac_decode_frame,
  2236. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  2237. .sample_fmts = (const enum AVSampleFormat[]) {
  2238. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
  2239. },
  2240. .channel_layouts = aac_channel_layout,
  2241. };
  2242. /*
  2243. Note: This decoder filter is intended to decode LATM streams transferred
  2244. in MPEG transport streams which only contain one program.
  2245. To do a more complex LATM demuxing a separate LATM demuxer should be used.
  2246. */
  2247. AVCodec ff_aac_latm_decoder = {
  2248. .name = "aac_latm",
  2249. .type = AVMEDIA_TYPE_AUDIO,
  2250. .id = CODEC_ID_AAC_LATM,
  2251. .priv_data_size = sizeof(struct LATMContext),
  2252. .init = latm_decode_init,
  2253. .close = aac_decode_close,
  2254. .decode = latm_decode_frame,
  2255. .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM syntax)"),
  2256. .sample_fmts = (const enum AVSampleFormat[]) {
  2257. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
  2258. },
  2259. .channel_layouts = aac_channel_layout,
  2260. };