layer3.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696
  1. /*
  2. * libmad - MPEG audio decoder library
  3. * Copyright (C) 2000-2004 Underbit Technologies, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * $Id: layer3.c,v 1.43 2004/01/23 09:41:32 rob Exp $
  20. */
  21. # include "libmad_config.h"
  22. # include "libmad_global.h"
  23. # include <stdlib.h>
  24. # include <string.h>
  25. # ifdef HAVE_ASSERT_H
  26. # include <assert.h>
  27. # endif
  28. # ifdef HAVE_LIMITS_H
  29. # include <limits.h>
  30. # else
  31. # define CHAR_BIT 8
  32. # endif
  33. # include "fixed.h"
  34. # include "bit.h"
  35. # include "stream.h"
  36. # include "frame.h"
  37. # include "huffman.h"
  38. # include "layer3.h"
  39. /* --- Layer III ----------------------------------------------------------- */
  40. enum {
  41. count1table_select = 0x01,
  42. scalefac_scale = 0x02,
  43. preflag = 0x04,
  44. mixed_block_flag = 0x08
  45. };
  46. enum {
  47. I_STEREO = 0x1,
  48. MS_STEREO = 0x2
  49. };
  50. struct sideinfo {
  51. unsigned int main_data_begin;
  52. unsigned int private_bits;
  53. unsigned char scfsi[2];
  54. struct granule {
  55. struct channel {
  56. /* from side info */
  57. unsigned short part2_3_length;
  58. unsigned short big_values;
  59. unsigned short global_gain;
  60. unsigned short scalefac_compress;
  61. unsigned char flags;
  62. unsigned char block_type;
  63. unsigned char table_select[3];
  64. unsigned char subblock_gain[3];
  65. unsigned char region0_count;
  66. unsigned char region1_count;
  67. /* from main_data */
  68. unsigned char scalefac[39]; /* scalefac_l and/or scalefac_s */
  69. } ch[2];
  70. } gr[2];
  71. };
  72. /*
  73. * scalefactor bit lengths
  74. * derived from section 2.4.2.7 of ISO/IEC 11172-3
  75. */
  76. static
  77. struct {
  78. unsigned char slen1;
  79. unsigned char slen2;
  80. } const sflen_table[16] = {
  81. { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
  82. { 3, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
  83. { 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 1 },
  84. { 3, 2 }, { 3, 3 }, { 4, 2 }, { 4, 3 }
  85. };
  86. /*
  87. * number of LSF scalefactor band values
  88. * derived from section 2.4.3.2 of ISO/IEC 13818-3
  89. */
  90. static
  91. unsigned char const nsfb_table[6][3][4] = {
  92. { { 6, 5, 5, 5 },
  93. { 9, 9, 9, 9 },
  94. { 6, 9, 9, 9 } },
  95. { { 6, 5, 7, 3 },
  96. { 9, 9, 12, 6 },
  97. { 6, 9, 12, 6 } },
  98. { { 11, 10, 0, 0 },
  99. { 18, 18, 0, 0 },
  100. { 15, 18, 0, 0 } },
  101. { { 7, 7, 7, 0 },
  102. { 12, 12, 12, 0 },
  103. { 6, 15, 12, 0 } },
  104. { { 6, 6, 6, 3 },
  105. { 12, 9, 9, 6 },
  106. { 6, 12, 9, 6 } },
  107. { { 8, 8, 5, 0 },
  108. { 15, 12, 9, 0 },
  109. { 6, 18, 9, 0 } }
  110. };
  111. /*
  112. * MPEG-1 scalefactor band widths
  113. * derived from Table B.8 of ISO/IEC 11172-3
  114. */
  115. static
  116. unsigned char const sfb_48000_long[] = {
  117. 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10,
  118. 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192
  119. };
  120. static
  121. unsigned char const sfb_44100_long[] = {
  122. 4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10,
  123. 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158
  124. };
  125. static
  126. unsigned char const sfb_32000_long[] = {
  127. 4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12,
  128. 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26
  129. };
  130. static
  131. unsigned char const sfb_48000_short[] = {
  132. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
  133. 6, 6, 6, 6, 6, 10, 10, 10, 12, 12, 12, 14, 14,
  134. 14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
  135. };
  136. static
  137. unsigned char const sfb_44100_short[] = {
  138. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
  139. 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14,
  140. 14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
  141. };
  142. static
  143. unsigned char const sfb_32000_short[] = {
  144. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
  145. 6, 6, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 20,
  146. 20, 26, 26, 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
  147. };
  148. static
  149. unsigned char const sfb_48000_mixed[] = {
  150. /* long */ 4, 4, 4, 4, 4, 4, 6, 6,
  151. /* short */ 4, 4, 4, 6, 6, 6, 6, 6, 6, 10,
  152. 10, 10, 12, 12, 12, 14, 14, 14, 16, 16,
  153. 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
  154. };
  155. static
  156. unsigned char const sfb_44100_mixed[] = {
  157. /* long */ 4, 4, 4, 4, 4, 4, 6, 6,
  158. /* short */ 4, 4, 4, 6, 6, 6, 8, 8, 8, 10,
  159. 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
  160. 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
  161. };
  162. static
  163. unsigned char const sfb_32000_mixed[] = {
  164. /* long */ 4, 4, 4, 4, 4, 4, 6, 6,
  165. /* short */ 4, 4, 4, 6, 6, 6, 8, 8, 8, 12,
  166. 12, 12, 16, 16, 16, 20, 20, 20, 26, 26,
  167. 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
  168. };
  169. /*
  170. * MPEG-2 scalefactor band widths
  171. * derived from Table B.2 of ISO/IEC 13818-3
  172. */
  173. static
  174. unsigned char const sfb_24000_long[] = {
  175. 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
  176. 18, 22, 26, 32, 38, 46, 54, 62, 70, 76, 36
  177. };
  178. static
  179. unsigned char const sfb_22050_long[] = {
  180. 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
  181. 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54
  182. };
  183. # define sfb_16000_long sfb_22050_long
  184. static
  185. unsigned char const sfb_24000_short[] = {
  186. 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8,
  187. 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
  188. 18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
  189. };
  190. static
  191. unsigned char const sfb_22050_short[] = {
  192. 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6,
  193. 6, 6, 8, 8, 8, 10, 10, 10, 14, 14, 14, 18, 18,
  194. 18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
  195. };
  196. static
  197. unsigned char const sfb_16000_short[] = {
  198. 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8,
  199. 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
  200. 18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
  201. };
  202. static
  203. unsigned char const sfb_24000_mixed[] = {
  204. /* long */ 6, 6, 6, 6, 6, 6,
  205. /* short */ 6, 6, 6, 8, 8, 8, 10, 10, 10, 12,
  206. 12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
  207. 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
  208. };
  209. static
  210. unsigned char const sfb_22050_mixed[] = {
  211. /* long */ 6, 6, 6, 6, 6, 6,
  212. /* short */ 6, 6, 6, 6, 6, 6, 8, 8, 8, 10,
  213. 10, 10, 14, 14, 14, 18, 18, 18, 26, 26,
  214. 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
  215. };
  216. static
  217. unsigned char const sfb_16000_mixed[] = {
  218. /* long */ 6, 6, 6, 6, 6, 6,
  219. /* short */ 6, 6, 6, 8, 8, 8, 10, 10, 10, 12,
  220. 12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
  221. 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
  222. };
  223. /*
  224. * MPEG 2.5 scalefactor band widths
  225. * derived from public sources
  226. */
  227. # define sfb_12000_long sfb_16000_long
  228. # define sfb_11025_long sfb_12000_long
  229. static
  230. unsigned char const sfb_8000_long[] = {
  231. 12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32,
  232. 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2
  233. };
  234. # define sfb_12000_short sfb_16000_short
  235. # define sfb_11025_short sfb_12000_short
  236. static
  237. unsigned char const sfb_8000_short[] = {
  238. 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12, 16,
  239. 16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36,
  240. 36, 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 26, 26
  241. };
  242. # define sfb_12000_mixed sfb_16000_mixed
  243. # define sfb_11025_mixed sfb_12000_mixed
  244. /* the 8000 Hz short block scalefactor bands do not break after
  245. the first 36 frequency lines, so this is probably wrong */
  246. static
  247. unsigned char const sfb_8000_mixed[] = {
  248. /* long */ 12, 12, 12,
  249. /* short */ 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16,
  250. 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36,
  251. 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 26, 26
  252. };
  253. static
  254. struct {
  255. unsigned char const *l;
  256. unsigned char const *s;
  257. unsigned char const *m;
  258. } const sfbwidth_table[9] = {
  259. { sfb_48000_long, sfb_48000_short, sfb_48000_mixed },
  260. { sfb_44100_long, sfb_44100_short, sfb_44100_mixed },
  261. { sfb_32000_long, sfb_32000_short, sfb_32000_mixed },
  262. { sfb_24000_long, sfb_24000_short, sfb_24000_mixed },
  263. { sfb_22050_long, sfb_22050_short, sfb_22050_mixed },
  264. { sfb_16000_long, sfb_16000_short, sfb_16000_mixed },
  265. { sfb_12000_long, sfb_12000_short, sfb_12000_mixed },
  266. { sfb_11025_long, sfb_11025_short, sfb_11025_mixed },
  267. { sfb_8000_long, sfb_8000_short, sfb_8000_mixed }
  268. };
  269. /*
  270. * scalefactor band preemphasis (used only when preflag is set)
  271. * derived from Table B.6 of ISO/IEC 11172-3
  272. */
  273. static
  274. unsigned char const pretab[22] = {
  275. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
  276. };
  277. /*
  278. * table for requantization
  279. *
  280. * rq_table[x].mantissa * 2^(rq_table[x].exponent) = x^(4/3)
  281. */
  282. static
  283. struct fixedfloat {
  284. unsigned long mantissa : 27;
  285. unsigned short exponent : 5;
  286. } const rq_table[8207] = {
  287. # include "rq_table.dat"
  288. };
  289. /*
  290. * fractional powers of two
  291. * used for requantization and joint stereo decoding
  292. *
  293. * root_table[3 + x] = 2^(x/4)
  294. */
  295. static
  296. mad_fixed_t const root_table[7] = {
  297. MAD_F(0x09837f05) /* 2^(-3/4) == 0.59460355750136 */,
  298. MAD_F(0x0b504f33) /* 2^(-2/4) == 0.70710678118655 */,
  299. MAD_F(0x0d744fcd) /* 2^(-1/4) == 0.84089641525371 */,
  300. MAD_F(0x10000000) /* 2^( 0/4) == 1.00000000000000 */,
  301. MAD_F(0x1306fe0a) /* 2^(+1/4) == 1.18920711500272 */,
  302. MAD_F(0x16a09e66) /* 2^(+2/4) == 1.41421356237310 */,
  303. MAD_F(0x1ae89f99) /* 2^(+3/4) == 1.68179283050743 */
  304. };
  305. /*
  306. * coefficients for aliasing reduction
  307. * derived from Table B.9 of ISO/IEC 11172-3
  308. *
  309. * c[] = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 }
  310. * cs[i] = 1 / sqrt(1 + c[i]^2)
  311. * ca[i] = c[i] / sqrt(1 + c[i]^2)
  312. */
  313. static
  314. mad_fixed_t const cs[8] = {
  315. +MAD_F(0x0db84a81) /* +0.857492926 */, +MAD_F(0x0e1b9d7f) /* +0.881741997 */,
  316. +MAD_F(0x0f31adcf) /* +0.949628649 */, +MAD_F(0x0fbba815) /* +0.983314592 */,
  317. +MAD_F(0x0feda417) /* +0.995517816 */, +MAD_F(0x0ffc8fc8) /* +0.999160558 */,
  318. +MAD_F(0x0fff964c) /* +0.999899195 */, +MAD_F(0x0ffff8d3) /* +0.999993155 */
  319. };
  320. static
  321. mad_fixed_t const ca[8] = {
  322. -MAD_F(0x083b5fe7) /* -0.514495755 */, -MAD_F(0x078c36d2) /* -0.471731969 */,
  323. -MAD_F(0x05039814) /* -0.313377454 */, -MAD_F(0x02e91dd1) /* -0.181913200 */,
  324. -MAD_F(0x0183603a) /* -0.094574193 */, -MAD_F(0x00a7cb87) /* -0.040965583 */,
  325. -MAD_F(0x003a2847) /* -0.014198569 */, -MAD_F(0x000f27b4) /* -0.003699975 */
  326. };
  327. /*
  328. * IMDCT coefficients for short blocks
  329. * derived from section 2.4.3.4.10.2 of ISO/IEC 11172-3
  330. *
  331. * imdct_s[i/even][k] = cos((PI / 24) * (2 * (i / 2) + 7) * (2 * k + 1))
  332. * imdct_s[i /odd][k] = cos((PI / 24) * (2 * (6 + (i-1)/2) + 7) * (2 * k + 1))
  333. */
  334. static
  335. mad_fixed_t const imdct_s[6][6] = {
  336. # include "imdct_s.dat"
  337. };
  338. # if !defined(ASO_IMDCT)
  339. /*
  340. * windowing coefficients for long blocks
  341. * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
  342. *
  343. * window_l[i] = sin((PI / 36) * (i + 1/2))
  344. */
  345. static
  346. mad_fixed_t const window_l[36] = {
  347. MAD_F(0x00b2aa3e) /* 0.043619387 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
  348. MAD_F(0x03768962) /* 0.216439614 */, MAD_F(0x04cfb0e2) /* 0.300705800 */,
  349. MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x07635284) /* 0.461748613 */,
  350. MAD_F(0x0898c779) /* 0.537299608 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
  351. MAD_F(0x0acf37ad) /* 0.675590208 */, MAD_F(0x0bcbe352) /* 0.737277337 */,
  352. MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x0d7e8807) /* 0.843391446 */,
  353. MAD_F(0x0e313245) /* 0.887010833 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
  354. MAD_F(0x0f426cb5) /* 0.953716951 */, MAD_F(0x0f9ee890) /* 0.976296007 */,
  355. MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ffc19fd) /* 0.999048222 */,
  356. MAD_F(0x0ffc19fd) /* 0.999048222 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
  357. MAD_F(0x0f9ee890) /* 0.976296007 */, MAD_F(0x0f426cb5) /* 0.953716951 */,
  358. MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0e313245) /* 0.887010833 */,
  359. MAD_F(0x0d7e8807) /* 0.843391446 */, MAD_F(0x0cb19346) /* 0.793353340 */,
  360. MAD_F(0x0bcbe352) /* 0.737277337 */, MAD_F(0x0acf37ad) /* 0.675590208 */,
  361. MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0898c779) /* 0.537299608 */,
  362. MAD_F(0x07635284) /* 0.461748613 */, MAD_F(0x061f78aa) /* 0.382683432 */,
  363. MAD_F(0x04cfb0e2) /* 0.300705800 */, MAD_F(0x03768962) /* 0.216439614 */,
  364. MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x00b2aa3e) /* 0.043619387 */,
  365. };
  366. # endif /* ASO_IMDCT */
  367. /*
  368. * windowing coefficients for short blocks
  369. * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
  370. *
  371. * window_s[i] = sin((PI / 12) * (i + 1/2))
  372. */
  373. static
  374. mad_fixed_t const window_s[12] = {
  375. MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x061f78aa) /* 0.382683432 */,
  376. MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0cb19346) /* 0.793353340 */,
  377. MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
  378. MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
  379. MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
  380. MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
  381. };
  382. /*
  383. * coefficients for intensity stereo processing
  384. * derived from section 2.4.3.4.9.3 of ISO/IEC 11172-3
  385. *
  386. * is_ratio[i] = tan(i * (PI / 12))
  387. * is_table[i] = is_ratio[i] / (1 + is_ratio[i])
  388. */
  389. static
  390. mad_fixed_t const is_table[7] = {
  391. MAD_F(0x00000000) /* 0.000000000 */,
  392. MAD_F(0x0361962f) /* 0.211324865 */,
  393. MAD_F(0x05db3d74) /* 0.366025404 */,
  394. MAD_F(0x08000000) /* 0.500000000 */,
  395. MAD_F(0x0a24c28c) /* 0.633974596 */,
  396. MAD_F(0x0c9e69d1) /* 0.788675135 */,
  397. MAD_F(0x10000000) /* 1.000000000 */
  398. };
  399. /*
  400. * coefficients for LSF intensity stereo processing
  401. * derived from section 2.4.3.2 of ISO/IEC 13818-3
  402. *
  403. * is_lsf_table[0][i] = (1 / sqrt(sqrt(2)))^(i + 1)
  404. * is_lsf_table[1][i] = (1 / sqrt(2)) ^(i + 1)
  405. */
  406. static
  407. mad_fixed_t const is_lsf_table[2][15] = {
  408. {
  409. MAD_F(0x0d744fcd) /* 0.840896415 */,
  410. MAD_F(0x0b504f33) /* 0.707106781 */,
  411. MAD_F(0x09837f05) /* 0.594603558 */,
  412. MAD_F(0x08000000) /* 0.500000000 */,
  413. MAD_F(0x06ba27e6) /* 0.420448208 */,
  414. MAD_F(0x05a8279a) /* 0.353553391 */,
  415. MAD_F(0x04c1bf83) /* 0.297301779 */,
  416. MAD_F(0x04000000) /* 0.250000000 */,
  417. MAD_F(0x035d13f3) /* 0.210224104 */,
  418. MAD_F(0x02d413cd) /* 0.176776695 */,
  419. MAD_F(0x0260dfc1) /* 0.148650889 */,
  420. MAD_F(0x02000000) /* 0.125000000 */,
  421. MAD_F(0x01ae89fa) /* 0.105112052 */,
  422. MAD_F(0x016a09e6) /* 0.088388348 */,
  423. MAD_F(0x01306fe1) /* 0.074325445 */
  424. }, {
  425. MAD_F(0x0b504f33) /* 0.707106781 */,
  426. MAD_F(0x08000000) /* 0.500000000 */,
  427. MAD_F(0x05a8279a) /* 0.353553391 */,
  428. MAD_F(0x04000000) /* 0.250000000 */,
  429. MAD_F(0x02d413cd) /* 0.176776695 */,
  430. MAD_F(0x02000000) /* 0.125000000 */,
  431. MAD_F(0x016a09e6) /* 0.088388348 */,
  432. MAD_F(0x01000000) /* 0.062500000 */,
  433. MAD_F(0x00b504f3) /* 0.044194174 */,
  434. MAD_F(0x00800000) /* 0.031250000 */,
  435. MAD_F(0x005a827a) /* 0.022097087 */,
  436. MAD_F(0x00400000) /* 0.015625000 */,
  437. MAD_F(0x002d413d) /* 0.011048543 */,
  438. MAD_F(0x00200000) /* 0.007812500 */,
  439. MAD_F(0x0016a09e) /* 0.005524272 */
  440. }
  441. };
  442. /*
  443. * NAME: III_sideinfo()
  444. * DESCRIPTION: decode frame side information from a bitstream
  445. */
  446. static
  447. enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
  448. int lsf, struct sideinfo *si,
  449. unsigned int *data_bitlen,
  450. unsigned int *priv_bitlen)
  451. {
  452. unsigned int ngr, gr, ch, i;
  453. enum mad_error result = MAD_ERROR_NONE;
  454. *data_bitlen = 0;
  455. *priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3);
  456. si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9);
  457. si->private_bits = mad_bit_read(ptr, *priv_bitlen);
  458. ngr = 1;
  459. if (!lsf) {
  460. ngr = 2;
  461. for (ch = 0; ch < nch; ++ch)
  462. si->scfsi[ch] = mad_bit_read(ptr, 4);
  463. }
  464. for (gr = 0; gr < ngr; ++gr) {
  465. struct granule *granule = &si->gr[gr];
  466. for (ch = 0; ch < nch; ++ch) {
  467. struct channel *channel = &granule->ch[ch];
  468. channel->part2_3_length = mad_bit_read(ptr, 12);
  469. channel->big_values = mad_bit_read(ptr, 9);
  470. channel->global_gain = mad_bit_read(ptr, 8);
  471. channel->scalefac_compress = mad_bit_read(ptr, lsf ? 9 : 4);
  472. *data_bitlen += channel->part2_3_length;
  473. if (channel->big_values > 288 && result == 0)
  474. result = MAD_ERROR_BADBIGVALUES;
  475. channel->flags = 0;
  476. /* window_switching_flag */
  477. if (mad_bit_read(ptr, 1)) {
  478. channel->block_type = mad_bit_read(ptr, 2);
  479. if (channel->block_type == 0 && result == 0)
  480. result = MAD_ERROR_BADBLOCKTYPE;
  481. if (!lsf && channel->block_type == 2 && si->scfsi[ch] && result == 0)
  482. result = MAD_ERROR_BADSCFSI;
  483. channel->region0_count = 7;
  484. channel->region1_count = 36;
  485. if (mad_bit_read(ptr, 1))
  486. channel->flags |= mixed_block_flag;
  487. else if (channel->block_type == 2)
  488. channel->region0_count = 8;
  489. for (i = 0; i < 2; ++i)
  490. channel->table_select[i] = mad_bit_read(ptr, 5);
  491. # if defined(DEBUG)
  492. channel->table_select[2] = 4; /* not used */
  493. # endif
  494. for (i = 0; i < 3; ++i)
  495. channel->subblock_gain[i] = mad_bit_read(ptr, 3);
  496. }
  497. else {
  498. channel->block_type = 0;
  499. for (i = 0; i < 3; ++i)
  500. channel->table_select[i] = mad_bit_read(ptr, 5);
  501. channel->region0_count = mad_bit_read(ptr, 4);
  502. channel->region1_count = mad_bit_read(ptr, 3);
  503. }
  504. /* [preflag,] scalefac_scale, count1table_select */
  505. channel->flags |= mad_bit_read(ptr, lsf ? 2 : 3);
  506. }
  507. }
  508. return result;
  509. }
  510. /*
  511. * NAME: III_scalefactors_lsf()
  512. * DESCRIPTION: decode channel scalefactors for LSF from a bitstream
  513. */
  514. static
  515. unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
  516. struct channel *channel,
  517. struct channel *gr1ch, int mode_extension)
  518. {
  519. struct mad_bitptr start;
  520. unsigned int scalefac_compress, index, slen[4], part, n, i;
  521. unsigned char const *nsfb;
  522. start = *ptr;
  523. scalefac_compress = channel->scalefac_compress;
  524. index = (channel->block_type == 2) ?
  525. ((channel->flags & mixed_block_flag) ? 2 : 1) : 0;
  526. if (!((mode_extension & I_STEREO) && gr1ch)) {
  527. if (scalefac_compress < 400) {
  528. slen[0] = (scalefac_compress >> 4) / 5;
  529. slen[1] = (scalefac_compress >> 4) % 5;
  530. slen[2] = (scalefac_compress % 16) >> 2;
  531. slen[3] = scalefac_compress % 4;
  532. nsfb = nsfb_table[0][index];
  533. }
  534. else if (scalefac_compress < 500) {
  535. scalefac_compress -= 400;
  536. slen[0] = (scalefac_compress >> 2) / 5;
  537. slen[1] = (scalefac_compress >> 2) % 5;
  538. slen[2] = scalefac_compress % 4;
  539. slen[3] = 0;
  540. nsfb = nsfb_table[1][index];
  541. }
  542. else {
  543. scalefac_compress -= 500;
  544. slen[0] = scalefac_compress / 3;
  545. slen[1] = scalefac_compress % 3;
  546. slen[2] = 0;
  547. slen[3] = 0;
  548. channel->flags |= preflag;
  549. nsfb = nsfb_table[2][index];
  550. }
  551. n = 0;
  552. for (part = 0; part < 4; ++part) {
  553. for (i = 0; i < nsfb[part]; ++i)
  554. channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
  555. }
  556. while (n < 39)
  557. channel->scalefac[n++] = 0;
  558. }
  559. else { /* (mode_extension & I_STEREO) && gr1ch (i.e. ch == 1) */
  560. scalefac_compress >>= 1;
  561. if (scalefac_compress < 180) {
  562. slen[0] = scalefac_compress / 36;
  563. slen[1] = (scalefac_compress % 36) / 6;
  564. slen[2] = (scalefac_compress % 36) % 6;
  565. slen[3] = 0;
  566. nsfb = nsfb_table[3][index];
  567. }
  568. else if (scalefac_compress < 244) {
  569. scalefac_compress -= 180;
  570. slen[0] = (scalefac_compress % 64) >> 4;
  571. slen[1] = (scalefac_compress % 16) >> 2;
  572. slen[2] = scalefac_compress % 4;
  573. slen[3] = 0;
  574. nsfb = nsfb_table[4][index];
  575. }
  576. else {
  577. scalefac_compress -= 244;
  578. slen[0] = scalefac_compress / 3;
  579. slen[1] = scalefac_compress % 3;
  580. slen[2] = 0;
  581. slen[3] = 0;
  582. nsfb = nsfb_table[5][index];
  583. }
  584. n = 0;
  585. for (part = 0; part < 4; ++part) {
  586. unsigned int max, is_pos;
  587. max = (1 << slen[part]) - 1;
  588. for (i = 0; i < nsfb[part]; ++i) {
  589. is_pos = mad_bit_read(ptr, slen[part]);
  590. channel->scalefac[n] = is_pos;
  591. gr1ch->scalefac[n++] = (is_pos == max);
  592. }
  593. }
  594. while (n < 39) {
  595. channel->scalefac[n] = 0;
  596. gr1ch->scalefac[n++] = 0; /* apparently not illegal */
  597. }
  598. }
  599. return mad_bit_length(&start, ptr);
  600. }
  601. /*
  602. * NAME: III_scalefactors()
  603. * DESCRIPTION: decode channel scalefactors of one granule from a bitstream
  604. */
  605. static
  606. unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
  607. struct channel const *gr0ch, unsigned int scfsi)
  608. {
  609. struct mad_bitptr start;
  610. unsigned int slen1, slen2, sfbi;
  611. start = *ptr;
  612. slen1 = sflen_table[channel->scalefac_compress].slen1;
  613. slen2 = sflen_table[channel->scalefac_compress].slen2;
  614. if (channel->block_type == 2) {
  615. unsigned int nsfb;
  616. sfbi = 0;
  617. nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
  618. while (nsfb--)
  619. channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
  620. nsfb = 6 * 3;
  621. while (nsfb--)
  622. channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
  623. nsfb = 1 * 3;
  624. while (nsfb--)
  625. channel->scalefac[sfbi++] = 0;
  626. }
  627. else { /* channel->block_type != 2 */
  628. if (scfsi & 0x8) {
  629. for (sfbi = 0; sfbi < 6; ++sfbi)
  630. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  631. }
  632. else {
  633. for (sfbi = 0; sfbi < 6; ++sfbi)
  634. channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
  635. }
  636. if (scfsi & 0x4) {
  637. for (sfbi = 6; sfbi < 11; ++sfbi)
  638. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  639. }
  640. else {
  641. for (sfbi = 6; sfbi < 11; ++sfbi)
  642. channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
  643. }
  644. if (scfsi & 0x2) {
  645. for (sfbi = 11; sfbi < 16; ++sfbi)
  646. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  647. }
  648. else {
  649. for (sfbi = 11; sfbi < 16; ++sfbi)
  650. channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
  651. }
  652. if (scfsi & 0x1) {
  653. for (sfbi = 16; sfbi < 21; ++sfbi)
  654. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  655. }
  656. else {
  657. for (sfbi = 16; sfbi < 21; ++sfbi)
  658. channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
  659. }
  660. channel->scalefac[21] = 0;
  661. }
  662. return mad_bit_length(&start, ptr);
  663. }
  664. /*
  665. * The Layer III formula for requantization and scaling is defined by
  666. * section 2.4.3.4.7.1 of ISO/IEC 11172-3, as follows:
  667. *
  668. * long blocks:
  669. * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
  670. * 2^((1/4) * (global_gain - 210)) *
  671. * 2^-(scalefac_multiplier *
  672. * (scalefac_l[sfb] + preflag * pretab[sfb]))
  673. *
  674. * short blocks:
  675. * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
  676. * 2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
  677. * 2^-(scalefac_multiplier * scalefac_s[sfb][w])
  678. *
  679. * where:
  680. * scalefac_multiplier = (scalefac_scale + 1) / 2
  681. *
  682. * The routines III_exponents() and III_requantize() facilitate this
  683. * calculation.
  684. */
  685. /*
  686. * NAME: III_exponents()
  687. * DESCRIPTION: calculate scalefactor exponents
  688. */
  689. static
  690. void III_exponents(struct channel const *channel,
  691. unsigned char const *sfbwidth, signed int exponents[39])
  692. {
  693. signed int gain;
  694. unsigned int scalefac_multiplier, sfbi;
  695. gain = (signed int) channel->global_gain - 210;
  696. scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1;
  697. if (channel->block_type == 2) {
  698. unsigned int l;
  699. signed int gain0, gain1, gain2;
  700. sfbi = l = 0;
  701. if (channel->flags & mixed_block_flag) {
  702. unsigned int premask;
  703. premask = (channel->flags & preflag) ? ~0 : 0;
  704. /* long block subbands 0-1 */
  705. while (l < 36) {
  706. exponents[sfbi] = gain -
  707. (signed int) ((channel->scalefac[sfbi] + (pretab[sfbi] & premask)) <<
  708. scalefac_multiplier);
  709. l += sfbwidth[sfbi++];
  710. }
  711. }
  712. /* this is probably wrong for 8000 Hz short/mixed blocks */
  713. gain0 = gain - 8 * (signed int) channel->subblock_gain[0];
  714. gain1 = gain - 8 * (signed int) channel->subblock_gain[1];
  715. gain2 = gain - 8 * (signed int) channel->subblock_gain[2];
  716. while (l < 576) {
  717. exponents[sfbi + 0] = gain0 -
  718. (signed int) (channel->scalefac[sfbi + 0] << scalefac_multiplier);
  719. exponents[sfbi + 1] = gain1 -
  720. (signed int) (channel->scalefac[sfbi + 1] << scalefac_multiplier);
  721. exponents[sfbi + 2] = gain2 -
  722. (signed int) (channel->scalefac[sfbi + 2] << scalefac_multiplier);
  723. l += 3 * sfbwidth[sfbi];
  724. sfbi += 3;
  725. }
  726. }
  727. else { /* channel->block_type != 2 */
  728. if (channel->flags & preflag) {
  729. for (sfbi = 0; sfbi < 22; ++sfbi) {
  730. exponents[sfbi] = gain -
  731. (signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) <<
  732. scalefac_multiplier);
  733. }
  734. }
  735. else {
  736. for (sfbi = 0; sfbi < 22; ++sfbi) {
  737. exponents[sfbi] = gain -
  738. (signed int) (channel->scalefac[sfbi] << scalefac_multiplier);
  739. }
  740. }
  741. }
  742. }
  743. /*
  744. * NAME: III_requantize()
  745. * DESCRIPTION: requantize one (positive) value
  746. */
  747. static
  748. mad_fixed_t III_requantize(unsigned int value, signed int exp)
  749. {
  750. mad_fixed_t requantized;
  751. signed int frac;
  752. struct fixedfloat const *power;
  753. frac = exp % 4; /* assumes sign(frac) == sign(exp) */
  754. exp /= 4;
  755. power = &rq_table[value];
  756. requantized = power->mantissa;
  757. exp += power->exponent;
  758. if (exp < 0) {
  759. if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) {
  760. /* underflow */
  761. requantized = 0;
  762. }
  763. else {
  764. requantized += 1L << (-exp - 1);
  765. requantized >>= -exp;
  766. }
  767. }
  768. else {
  769. if (exp >= 5) {
  770. /* overflow */
  771. # if defined(DEBUG)
  772. fprintf(stderr, "requantize overflow (%f * 2^%d)\n",
  773. mad_f_todouble(requantized), exp);
  774. # endif
  775. requantized = MAD_F_MAX;
  776. }
  777. else
  778. requantized <<= exp;
  779. }
  780. return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized;
  781. }
  782. /* we must take care that sz >= bits and sz < sizeof(long) lest bits == 0 */
  783. # define MASK(cache, sz, bits) \
  784. (((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1))
  785. # define MASK1BIT(cache, sz) \
  786. ((cache) & (1 << ((sz) - 1)))
  787. /*
  788. * NAME: III_huffdecode()
  789. * DESCRIPTION: decode Huffman code words of one channel of one granule
  790. */
  791. static
  792. enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
  793. struct channel *channel,
  794. unsigned char const *sfbwidth,
  795. unsigned int part2_length)
  796. {
  797. signed int exponents[39], exp;
  798. signed int const *expptr;
  799. struct mad_bitptr peek;
  800. signed int bits_left, cachesz;
  801. register mad_fixed_t *xrptr;
  802. mad_fixed_t const *sfbound;
  803. register unsigned long bitcache;
  804. bits_left = (signed) channel->part2_3_length - (signed) part2_length;
  805. if (bits_left < 0)
  806. return MAD_ERROR_BADPART3LEN;
  807. III_exponents(channel, sfbwidth, exponents);
  808. peek = *ptr;
  809. mad_bit_skip(ptr, bits_left);
  810. /* align bit reads to byte boundaries */
  811. cachesz = mad_bit_bitsleft(&peek);
  812. cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;
  813. bitcache = mad_bit_read(&peek, cachesz);
  814. bits_left -= cachesz;
  815. xrptr = &xr[0];
  816. /* big_values */
  817. {
  818. unsigned int region, rcount;
  819. struct hufftable const *entry;
  820. union huffpair const *table;
  821. unsigned int linbits, startbits, big_values, reqhits;
  822. mad_fixed_t reqcache[16];
  823. sfbound = xrptr + *sfbwidth++;
  824. rcount = channel->region0_count + 1;
  825. entry = &mad_huff_pair_table[channel->table_select[region = 0]];
  826. table = entry->table;
  827. linbits = entry->linbits;
  828. startbits = entry->startbits;
  829. if (table == 0)
  830. return MAD_ERROR_BADHUFFTABLE;
  831. expptr = &exponents[0];
  832. exp = *expptr++;
  833. reqhits = 0;
  834. big_values = channel->big_values;
  835. while (big_values-- && cachesz + bits_left > 0) {
  836. union huffpair const *pair;
  837. unsigned int clumpsz, value;
  838. register mad_fixed_t requantized;
  839. if (xrptr == sfbound) {
  840. sfbound += *sfbwidth++;
  841. /* change table if region boundary */
  842. if (--rcount == 0) {
  843. if (region == 0)
  844. rcount = channel->region1_count + 1;
  845. else
  846. rcount = 0; /* all remaining */
  847. entry = &mad_huff_pair_table[channel->table_select[++region]];
  848. table = entry->table;
  849. linbits = entry->linbits;
  850. startbits = entry->startbits;
  851. if (table == 0)
  852. return MAD_ERROR_BADHUFFTABLE;
  853. }
  854. if (exp != *expptr) {
  855. exp = *expptr;
  856. reqhits = 0;
  857. }
  858. ++expptr;
  859. }
  860. if (cachesz < 21) {
  861. unsigned int bits;
  862. bits = ((32 - 1 - 21) + (21 - cachesz)) & ~7;
  863. bitcache = (bitcache << bits) | mad_bit_read(&peek, bits);
  864. cachesz += bits;
  865. bits_left -= bits;
  866. }
  867. /* hcod (0..19) */
  868. clumpsz = startbits;
  869. pair = &table[MASK(bitcache, cachesz, clumpsz)];
  870. while (!pair->final) {
  871. cachesz -= clumpsz;
  872. clumpsz = pair->ptr.bits;
  873. pair = &table[pair->ptr.offset + MASK(bitcache, cachesz, clumpsz)];
  874. }
  875. cachesz -= pair->value.hlen;
  876. if (linbits) {
  877. /* x (0..14) */
  878. value = pair->value.x;
  879. switch (value) {
  880. case 0:
  881. xrptr[0] = 0;
  882. break;
  883. case 15:
  884. if (cachesz < linbits + 2) {
  885. bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
  886. cachesz += 16;
  887. bits_left -= 16;
  888. }
  889. value += MASK(bitcache, cachesz, linbits);
  890. cachesz -= linbits;
  891. requantized = III_requantize(value, exp);
  892. goto x_final;
  893. default:
  894. if (reqhits & (1 << value))
  895. requantized = reqcache[value];
  896. else {
  897. reqhits |= (1 << value);
  898. requantized = reqcache[value] = III_requantize(value, exp);
  899. }
  900. x_final:
  901. xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
  902. -requantized : requantized;
  903. }
  904. /* y (0..14) */
  905. value = pair->value.y;
  906. switch (value) {
  907. case 0:
  908. xrptr[1] = 0;
  909. break;
  910. case 15:
  911. if (cachesz < linbits + 1) {
  912. bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
  913. cachesz += 16;
  914. bits_left -= 16;
  915. }
  916. value += MASK(bitcache, cachesz, linbits);
  917. cachesz -= linbits;
  918. requantized = III_requantize(value, exp);
  919. goto y_final;
  920. default:
  921. if (reqhits & (1 << value))
  922. requantized = reqcache[value];
  923. else {
  924. reqhits |= (1 << value);
  925. requantized = reqcache[value] = III_requantize(value, exp);
  926. }
  927. y_final:
  928. xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
  929. -requantized : requantized;
  930. }
  931. }
  932. else {
  933. /* x (0..1) */
  934. value = pair->value.x;
  935. if (value == 0)
  936. xrptr[0] = 0;
  937. else {
  938. if (reqhits & (1 << value))
  939. requantized = reqcache[value];
  940. else {
  941. reqhits |= (1 << value);
  942. requantized = reqcache[value] = III_requantize(value, exp);
  943. }
  944. xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
  945. -requantized : requantized;
  946. }
  947. /* y (0..1) */
  948. value = pair->value.y;
  949. if (value == 0)
  950. xrptr[1] = 0;
  951. else {
  952. if (reqhits & (1 << value))
  953. requantized = reqcache[value];
  954. else {
  955. reqhits |= (1 << value);
  956. requantized = reqcache[value] = III_requantize(value, exp);
  957. }
  958. xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
  959. -requantized : requantized;
  960. }
  961. }
  962. xrptr += 2;
  963. }
  964. }
  965. if (cachesz + bits_left < 0)
  966. return MAD_ERROR_BADHUFFDATA; /* big_values overrun */
  967. /* count1 */
  968. {
  969. union huffquad const *table;
  970. register mad_fixed_t requantized;
  971. table = mad_huff_quad_table[channel->flags & count1table_select];
  972. requantized = III_requantize(1, exp);
  973. while (cachesz + bits_left > 0 && xrptr <= &xr[572]) {
  974. union huffquad const *quad;
  975. /* hcod (1..6) */
  976. if (cachesz < 10) {
  977. bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
  978. cachesz += 16;
  979. bits_left -= 16;
  980. }
  981. quad = &table[MASK(bitcache, cachesz, 4)];
  982. /* quad tables guaranteed to have at most one extra lookup */
  983. if (!quad->final) {
  984. cachesz -= 4;
  985. quad = &table[quad->ptr.offset +
  986. MASK(bitcache, cachesz, quad->ptr.bits)];
  987. }
  988. cachesz -= quad->value.hlen;
  989. if (xrptr == sfbound) {
  990. sfbound += *sfbwidth++;
  991. if (exp != *expptr) {
  992. exp = *expptr;
  993. requantized = III_requantize(1, exp);
  994. }
  995. ++expptr;
  996. }
  997. /* v (0..1) */
  998. xrptr[0] = quad->value.v ?
  999. (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
  1000. /* w (0..1) */
  1001. xrptr[1] = quad->value.w ?
  1002. (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
  1003. xrptr += 2;
  1004. if (xrptr == sfbound) {
  1005. sfbound += *sfbwidth++;
  1006. if (exp != *expptr) {
  1007. exp = *expptr;
  1008. requantized = III_requantize(1, exp);
  1009. }
  1010. ++expptr;
  1011. }
  1012. /* x (0..1) */
  1013. xrptr[0] = quad->value.x ?
  1014. (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
  1015. /* y (0..1) */
  1016. xrptr[1] = quad->value.y ?
  1017. (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
  1018. xrptr += 2;
  1019. }
  1020. if (cachesz + bits_left < 0) {
  1021. # if 0 && defined(DEBUG)
  1022. fprintf(stderr, "huffman count1 overrun (%d bits)\n",
  1023. -(cachesz + bits_left));
  1024. # endif
  1025. /* technically the bitstream is misformatted, but apparently
  1026. some encoders are just a bit sloppy with stuffing bits */
  1027. xrptr -= 4;
  1028. }
  1029. }
  1030. assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT);
  1031. # if 0 && defined(DEBUG)
  1032. if (bits_left < 0)
  1033. fprintf(stderr, "read %d bits too many\n", -bits_left);
  1034. else if (cachesz + bits_left > 0)
  1035. fprintf(stderr, "%d stuffing bits\n", cachesz + bits_left);
  1036. # endif
  1037. /* rzero */
  1038. while (xrptr < &xr[576]) {
  1039. xrptr[0] = 0;
  1040. xrptr[1] = 0;
  1041. xrptr += 2;
  1042. }
  1043. return MAD_ERROR_NONE;
  1044. }
  1045. # undef MASK
  1046. # undef MASK1BIT
  1047. /*
  1048. * NAME: III_reorder()
  1049. * DESCRIPTION: reorder frequency lines of a short block into subband order
  1050. */
  1051. static
  1052. void III_reorder(mad_fixed_t xr[576], struct channel const *channel,
  1053. unsigned char const sfbwidth[39])
  1054. {
  1055. mad_fixed_t tmp[32][3][6];
  1056. unsigned int sb, l, f, w, sbw[3], sw[3];
  1057. /* this is probably wrong for 8000 Hz mixed blocks */
  1058. sb = 0;
  1059. if (channel->flags & mixed_block_flag) {
  1060. sb = 2;
  1061. l = 0;
  1062. while (l < 36)
  1063. l += *sfbwidth++;
  1064. }
  1065. for (w = 0; w < 3; ++w) {
  1066. sbw[w] = sb;
  1067. sw[w] = 0;
  1068. }
  1069. f = *sfbwidth++;
  1070. w = 0;
  1071. for (l = 18 * sb; l < 576; ++l) {
  1072. if (f-- == 0) {
  1073. f = *sfbwidth++ - 1;
  1074. w = (w + 1) % 3;
  1075. }
  1076. tmp[sbw[w]][w][sw[w]++] = xr[l];
  1077. if (sw[w] == 6) {
  1078. sw[w] = 0;
  1079. ++sbw[w];
  1080. }
  1081. }
  1082. memcpy(&xr[18 * sb], &tmp[sb], (576 - 18 * sb) * sizeof(mad_fixed_t));
  1083. }
  1084. /*
  1085. * NAME: III_stereo()
  1086. * DESCRIPTION: perform joint stereo processing on a granule
  1087. */
  1088. static
  1089. enum mad_error III_stereo(mad_fixed_t xr[2][576],
  1090. struct granule const *granule,
  1091. struct mad_header *header,
  1092. unsigned char const *sfbwidth)
  1093. {
  1094. short modes[39];
  1095. unsigned int sfbi, l, n, i;
  1096. if (granule->ch[0].block_type !=
  1097. granule->ch[1].block_type ||
  1098. (granule->ch[0].flags & mixed_block_flag) !=
  1099. (granule->ch[1].flags & mixed_block_flag))
  1100. return MAD_ERROR_BADSTEREO;
  1101. for (i = 0; i < 39; ++i)
  1102. modes[i] = header->mode_extension;
  1103. /* intensity stereo */
  1104. if (header->mode_extension & I_STEREO) {
  1105. struct channel const *right_ch = &granule->ch[1];
  1106. mad_fixed_t const *right_xr = xr[1];
  1107. unsigned int is_pos;
  1108. header->flags |= MAD_FLAG_I_STEREO;
  1109. /* first determine which scalefactor bands are to be processed */
  1110. if (right_ch->block_type == 2) {
  1111. unsigned int lower, start, max, bound[3], w;
  1112. lower = start = max = bound[0] = bound[1] = bound[2] = 0;
  1113. sfbi = l = 0;
  1114. if (right_ch->flags & mixed_block_flag) {
  1115. while (l < 36) {
  1116. n = sfbwidth[sfbi++];
  1117. for (i = 0; i < n; ++i) {
  1118. if (right_xr[i]) {
  1119. lower = sfbi;
  1120. break;
  1121. }
  1122. }
  1123. right_xr += n;
  1124. l += n;
  1125. }
  1126. start = sfbi;
  1127. }
  1128. w = 0;
  1129. while (l < 576) {
  1130. n = sfbwidth[sfbi++];
  1131. for (i = 0; i < n; ++i) {
  1132. if (right_xr[i]) {
  1133. max = bound[w] = sfbi;
  1134. break;
  1135. }
  1136. }
  1137. right_xr += n;
  1138. l += n;
  1139. w = (w + 1) % 3;
  1140. }
  1141. if (max)
  1142. lower = start;
  1143. /* long blocks */
  1144. for (i = 0; i < lower; ++i)
  1145. modes[i] = header->mode_extension & ~I_STEREO;
  1146. /* short blocks */
  1147. w = 0;
  1148. for (i = start; i < max; ++i) {
  1149. if (i < bound[w])
  1150. modes[i] = header->mode_extension & ~I_STEREO;
  1151. w = (w + 1) % 3;
  1152. }
  1153. }
  1154. else { /* right_ch->block_type != 2 */
  1155. unsigned int bound;
  1156. bound = 0;
  1157. for (sfbi = l = 0; l < 576; l += n) {
  1158. n = sfbwidth[sfbi++];
  1159. for (i = 0; i < n; ++i) {
  1160. if (right_xr[i]) {
  1161. bound = sfbi;
  1162. break;
  1163. }
  1164. }
  1165. right_xr += n;
  1166. }
  1167. for (i = 0; i < bound; ++i)
  1168. modes[i] = header->mode_extension & ~I_STEREO;
  1169. }
  1170. /* now do the actual processing */
  1171. if (header->flags & MAD_FLAG_LSF_EXT) {
  1172. unsigned char const *illegal_pos = granule[1].ch[1].scalefac;
  1173. mad_fixed_t const *lsf_scale;
  1174. /* intensity_scale */
  1175. lsf_scale = is_lsf_table[right_ch->scalefac_compress & 0x1];
  1176. for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
  1177. n = sfbwidth[sfbi];
  1178. if (!(modes[sfbi] & I_STEREO))
  1179. continue;
  1180. if (illegal_pos[sfbi]) {
  1181. modes[sfbi] &= ~I_STEREO;
  1182. continue;
  1183. }
  1184. is_pos = right_ch->scalefac[sfbi];
  1185. for (i = 0; i < n; ++i) {
  1186. register mad_fixed_t left;
  1187. left = xr[0][l + i];
  1188. if (is_pos == 0)
  1189. xr[1][l + i] = left;
  1190. else {
  1191. register mad_fixed_t opposite;
  1192. opposite = mad_f_mul(left, lsf_scale[(is_pos - 1) / 2]);
  1193. if (is_pos & 1) {
  1194. xr[0][l + i] = opposite;
  1195. xr[1][l + i] = left;
  1196. }
  1197. else
  1198. xr[1][l + i] = opposite;
  1199. }
  1200. }
  1201. }
  1202. }
  1203. else { /* !(header->flags & MAD_FLAG_LSF_EXT) */
  1204. for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
  1205. n = sfbwidth[sfbi];
  1206. if (!(modes[sfbi] & I_STEREO))
  1207. continue;
  1208. is_pos = right_ch->scalefac[sfbi];
  1209. if (is_pos >= 7) { /* illegal intensity position */
  1210. modes[sfbi] &= ~I_STEREO;
  1211. continue;
  1212. }
  1213. for (i = 0; i < n; ++i) {
  1214. register mad_fixed_t left;
  1215. left = xr[0][l + i];
  1216. xr[0][l + i] = mad_f_mul(left, is_table[ is_pos]);
  1217. xr[1][l + i] = mad_f_mul(left, is_table[6 - is_pos]);
  1218. }
  1219. }
  1220. }
  1221. }
  1222. /* middle/side stereo */
  1223. if (header->mode_extension & MS_STEREO) {
  1224. register mad_fixed_t invsqrt2;
  1225. header->flags |= MAD_FLAG_MS_STEREO;
  1226. invsqrt2 = root_table[3 + -2];
  1227. for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
  1228. n = sfbwidth[sfbi];
  1229. if (modes[sfbi] != MS_STEREO)
  1230. continue;
  1231. for (i = 0; i < n; ++i) {
  1232. register mad_fixed_t m, s;
  1233. m = xr[0][l + i];
  1234. s = xr[1][l + i];
  1235. xr[0][l + i] = mad_f_mul(m + s, invsqrt2); /* l = (m + s) / sqrt(2) */
  1236. xr[1][l + i] = mad_f_mul(m - s, invsqrt2); /* r = (m - s) / sqrt(2) */
  1237. }
  1238. }
  1239. }
  1240. return MAD_ERROR_NONE;
  1241. }
  1242. /*
  1243. * NAME: III_aliasreduce()
  1244. * DESCRIPTION: perform frequency line alias reduction
  1245. */
  1246. static
  1247. void III_aliasreduce(mad_fixed_t xr[576], int lines)
  1248. {
  1249. mad_fixed_t const *bound;
  1250. int i;
  1251. bound = &xr[lines];
  1252. for (xr += 18; xr < bound; xr += 18) {
  1253. for (i = 0; i < 8; ++i) {
  1254. register mad_fixed_t a, b;
  1255. register mad_fixed64hi_t hi;
  1256. register mad_fixed64lo_t lo;
  1257. a = xr[-1 - i];
  1258. b = xr[ i];
  1259. # if defined(ASO_ZEROCHECK)
  1260. if (a | b) {
  1261. # endif
  1262. MAD_F_ML0(hi, lo, a, cs[i]);
  1263. MAD_F_MLA(hi, lo, -b, ca[i]);
  1264. xr[-1 - i] = MAD_F_MLZ(hi, lo);
  1265. MAD_F_ML0(hi, lo, b, cs[i]);
  1266. MAD_F_MLA(hi, lo, a, ca[i]);
  1267. xr[ i] = MAD_F_MLZ(hi, lo);
  1268. # if defined(ASO_ZEROCHECK)
  1269. }
  1270. # endif
  1271. }
  1272. }
  1273. }
  1274. # if defined(ASO_IMDCT)
  1275. void III_imdct_l(mad_fixed_t const [18], mad_fixed_t [36], unsigned int);
  1276. # else
  1277. # if 1
  1278. static
  1279. void fastsdct(mad_fixed_t const x[9], mad_fixed_t y[18])
  1280. {
  1281. mad_fixed_t a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12;
  1282. mad_fixed_t a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25;
  1283. mad_fixed_t m0, m1, m2, m3, m4, m5, m6, m7;
  1284. enum {
  1285. c0 = MAD_F(0x1f838b8d), /* 2 * cos( 1 * PI / 18) */
  1286. c1 = MAD_F(0x1bb67ae8), /* 2 * cos( 3 * PI / 18) */
  1287. c2 = MAD_F(0x18836fa3), /* 2 * cos( 4 * PI / 18) */
  1288. c3 = MAD_F(0x1491b752), /* 2 * cos( 5 * PI / 18) */
  1289. c4 = MAD_F(0x0af1d43a), /* 2 * cos( 7 * PI / 18) */
  1290. c5 = MAD_F(0x058e86a0), /* 2 * cos( 8 * PI / 18) */
  1291. c6 = -MAD_F(0x1e11f642) /* 2 * cos(16 * PI / 18) */
  1292. };
  1293. a0 = x[3] + x[5];
  1294. a1 = x[3] - x[5];
  1295. a2 = x[6] + x[2];
  1296. a3 = x[6] - x[2];
  1297. a4 = x[1] + x[7];
  1298. a5 = x[1] - x[7];
  1299. a6 = x[8] + x[0];
  1300. a7 = x[8] - x[0];
  1301. a8 = a0 + a2;
  1302. a9 = a0 - a2;
  1303. a10 = a0 - a6;
  1304. a11 = a2 - a6;
  1305. a12 = a8 + a6;
  1306. a13 = a1 - a3;
  1307. a14 = a13 + a7;
  1308. a15 = a3 + a7;
  1309. a16 = a1 - a7;
  1310. a17 = a1 + a3;
  1311. m0 = mad_f_mul(a17, -c3);
  1312. m1 = mad_f_mul(a16, -c0);
  1313. m2 = mad_f_mul(a15, -c4);
  1314. m3 = mad_f_mul(a14, -c1);
  1315. m4 = mad_f_mul(a5, -c1);
  1316. m5 = mad_f_mul(a11, -c6);
  1317. m6 = mad_f_mul(a10, -c5);
  1318. m7 = mad_f_mul(a9, -c2);
  1319. a18 = x[4] + a4;
  1320. a19 = 2 * x[4] - a4;
  1321. a20 = a19 + m5;
  1322. a21 = a19 - m5;
  1323. a22 = a19 + m6;
  1324. a23 = m4 + m2;
  1325. a24 = m4 - m2;
  1326. a25 = m4 + m1;
  1327. /* output to every other slot for convenience */
  1328. y[ 0] = a18 + a12;
  1329. y[ 2] = m0 - a25;
  1330. y[ 4] = m7 - a20;
  1331. y[ 6] = m3;
  1332. y[ 8] = a21 - m6;
  1333. y[10] = a24 - m1;
  1334. y[12] = a12 - 2 * a18;
  1335. y[14] = a23 + m0;
  1336. y[16] = a22 + m7;
  1337. }
  1338. static inline
  1339. void sdctII(mad_fixed_t const x[18], mad_fixed_t X[18])
  1340. {
  1341. mad_fixed_t tmp[9];
  1342. int i;
  1343. /* scale[i] = 2 * cos(PI * (2 * i + 1) / (2 * 18)) */
  1344. static mad_fixed_t const scale[9] = {
  1345. MAD_F(0x1fe0d3b4), MAD_F(0x1ee8dd47), MAD_F(0x1d007930),
  1346. MAD_F(0x1a367e59), MAD_F(0x16a09e66), MAD_F(0x125abcf8),
  1347. MAD_F(0x0d8616bc), MAD_F(0x08483ee1), MAD_F(0x02c9fad7)
  1348. };
  1349. /* divide the 18-point SDCT-II into two 9-point SDCT-IIs */
  1350. /* even input butterfly */
  1351. for (i = 0; i < 9; i += 3) {
  1352. tmp[i + 0] = x[i + 0] + x[18 - (i + 0) - 1];
  1353. tmp[i + 1] = x[i + 1] + x[18 - (i + 1) - 1];
  1354. tmp[i + 2] = x[i + 2] + x[18 - (i + 2) - 1];
  1355. }
  1356. fastsdct(tmp, &X[0]);
  1357. /* odd input butterfly and scaling */
  1358. for (i = 0; i < 9; i += 3) {
  1359. tmp[i + 0] = mad_f_mul(x[i + 0] - x[18 - (i + 0) - 1], scale[i + 0]);
  1360. tmp[i + 1] = mad_f_mul(x[i + 1] - x[18 - (i + 1) - 1], scale[i + 1]);
  1361. tmp[i + 2] = mad_f_mul(x[i + 2] - x[18 - (i + 2) - 1], scale[i + 2]);
  1362. }
  1363. fastsdct(tmp, &X[1]);
  1364. /* output accumulation */
  1365. for (i = 3; i < 18; i += 8) {
  1366. X[i + 0] -= X[(i + 0) - 2];
  1367. X[i + 2] -= X[(i + 2) - 2];
  1368. X[i + 4] -= X[(i + 4) - 2];
  1369. X[i + 6] -= X[(i + 6) - 2];
  1370. }
  1371. }
  1372. static inline
  1373. void dctIV(mad_fixed_t const y[18], mad_fixed_t X[18])
  1374. {
  1375. mad_fixed_t tmp[18];
  1376. int i;
  1377. /* scale[i] = 2 * cos(PI * (2 * i + 1) / (4 * 18)) */
  1378. static mad_fixed_t const scale[18] = {
  1379. MAD_F(0x1ff833fa), MAD_F(0x1fb9ea93), MAD_F(0x1f3dd120),
  1380. MAD_F(0x1e84d969), MAD_F(0x1d906bcf), MAD_F(0x1c62648b),
  1381. MAD_F(0x1afd100f), MAD_F(0x1963268b), MAD_F(0x1797c6a4),
  1382. MAD_F(0x159e6f5b), MAD_F(0x137af940), MAD_F(0x11318ef3),
  1383. MAD_F(0x0ec6a507), MAD_F(0x0c3ef153), MAD_F(0x099f61c5),
  1384. MAD_F(0x06ed12c5), MAD_F(0x042d4544), MAD_F(0x0165547c)
  1385. };
  1386. /* scaling */
  1387. for (i = 0; i < 18; i += 3) {
  1388. tmp[i + 0] = mad_f_mul(y[i + 0], scale[i + 0]);
  1389. tmp[i + 1] = mad_f_mul(y[i + 1], scale[i + 1]);
  1390. tmp[i + 2] = mad_f_mul(y[i + 2], scale[i + 2]);
  1391. }
  1392. /* SDCT-II */
  1393. sdctII(tmp, X);
  1394. /* scale reduction and output accumulation */
  1395. X[0] /= 2;
  1396. for (i = 1; i < 17; i += 4) {
  1397. X[i + 0] = X[i + 0] / 2 - X[(i + 0) - 1];
  1398. X[i + 1] = X[i + 1] / 2 - X[(i + 1) - 1];
  1399. X[i + 2] = X[i + 2] / 2 - X[(i + 2) - 1];
  1400. X[i + 3] = X[i + 3] / 2 - X[(i + 3) - 1];
  1401. }
  1402. X[17] = X[17] / 2 - X[16];
  1403. }
  1404. /*
  1405. * NAME: imdct36
  1406. * DESCRIPTION: perform X[18]->x[36] IMDCT using Szu-Wei Lee's fast algorithm
  1407. */
  1408. static inline
  1409. void imdct36(mad_fixed_t const x[18], mad_fixed_t y[36])
  1410. {
  1411. mad_fixed_t tmp[18];
  1412. int i;
  1413. /* DCT-IV */
  1414. dctIV(x, tmp);
  1415. /* convert 18-point DCT-IV to 36-point IMDCT */
  1416. for (i = 0; i < 9; i += 3) {
  1417. y[i + 0] = tmp[9 + (i + 0)];
  1418. y[i + 1] = tmp[9 + (i + 1)];
  1419. y[i + 2] = tmp[9 + (i + 2)];
  1420. }
  1421. for (i = 9; i < 27; i += 3) {
  1422. y[i + 0] = -tmp[36 - (9 + (i + 0)) - 1];
  1423. y[i + 1] = -tmp[36 - (9 + (i + 1)) - 1];
  1424. y[i + 2] = -tmp[36 - (9 + (i + 2)) - 1];
  1425. }
  1426. for (i = 27; i < 36; i += 3) {
  1427. y[i + 0] = -tmp[(i + 0) - 27];
  1428. y[i + 1] = -tmp[(i + 1) - 27];
  1429. y[i + 2] = -tmp[(i + 2) - 27];
  1430. }
  1431. }
  1432. # else
  1433. /*
  1434. * NAME: imdct36
  1435. * DESCRIPTION: perform X[18]->x[36] IMDCT
  1436. */
  1437. static inline
  1438. void imdct36(mad_fixed_t const X[18], mad_fixed_t x[36])
  1439. {
  1440. mad_fixed_t t0, t1, t2, t3, t4, t5, t6, t7;
  1441. mad_fixed_t t8, t9, t10, t11, t12, t13, t14, t15;
  1442. register mad_fixed64hi_t hi;
  1443. register mad_fixed64lo_t lo;
  1444. MAD_F_ML0(hi, lo, X[4], MAD_F(0x0ec835e8));
  1445. MAD_F_MLA(hi, lo, X[13], MAD_F(0x061f78aa));
  1446. t6 = MAD_F_MLZ(hi, lo);
  1447. MAD_F_MLA(hi, lo, (t14 = X[1] - X[10]), -MAD_F(0x061f78aa));
  1448. MAD_F_MLA(hi, lo, (t15 = X[7] + X[16]), -MAD_F(0x0ec835e8));
  1449. t0 = MAD_F_MLZ(hi, lo);
  1450. MAD_F_MLA(hi, lo, (t8 = X[0] - X[11] - X[12]), MAD_F(0x0216a2a2));
  1451. MAD_F_MLA(hi, lo, (t9 = X[2] - X[9] - X[14]), MAD_F(0x09bd7ca0));
  1452. MAD_F_MLA(hi, lo, (t10 = X[3] - X[8] - X[15]), -MAD_F(0x0cb19346));
  1453. MAD_F_MLA(hi, lo, (t11 = X[5] - X[6] - X[17]), -MAD_F(0x0fdcf549));
  1454. x[7] = MAD_F_MLZ(hi, lo);
  1455. x[10] = -x[7];
  1456. MAD_F_ML0(hi, lo, t8, -MAD_F(0x0cb19346));
  1457. MAD_F_MLA(hi, lo, t9, MAD_F(0x0fdcf549));
  1458. MAD_F_MLA(hi, lo, t10, MAD_F(0x0216a2a2));
  1459. MAD_F_MLA(hi, lo, t11, -MAD_F(0x09bd7ca0));
  1460. x[19] = x[34] = MAD_F_MLZ(hi, lo) - t0;
  1461. t12 = X[0] - X[3] + X[8] - X[11] - X[12] + X[15];
  1462. t13 = X[2] + X[5] - X[6] - X[9] - X[14] - X[17];
  1463. MAD_F_ML0(hi, lo, t12, -MAD_F(0x0ec835e8));
  1464. MAD_F_MLA(hi, lo, t13, MAD_F(0x061f78aa));
  1465. x[22] = x[31] = MAD_F_MLZ(hi, lo) + t0;
  1466. MAD_F_ML0(hi, lo, X[1], -MAD_F(0x09bd7ca0));
  1467. MAD_F_MLA(hi, lo, X[7], MAD_F(0x0216a2a2));
  1468. MAD_F_MLA(hi, lo, X[10], -MAD_F(0x0fdcf549));
  1469. MAD_F_MLA(hi, lo, X[16], MAD_F(0x0cb19346));
  1470. t1 = MAD_F_MLZ(hi, lo) + t6;
  1471. MAD_F_ML0(hi, lo, X[0], MAD_F(0x03768962));
  1472. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0e313245));
  1473. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x0ffc19fd));
  1474. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0acf37ad));
  1475. MAD_F_MLA(hi, lo, X[6], MAD_F(0x04cfb0e2));
  1476. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0898c779));
  1477. MAD_F_MLA(hi, lo, X[9], MAD_F(0x0d7e8807));
  1478. MAD_F_MLA(hi, lo, X[11], MAD_F(0x0f426cb5));
  1479. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0bcbe352));
  1480. MAD_F_MLA(hi, lo, X[14], MAD_F(0x00b2aa3e));
  1481. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x07635284));
  1482. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0f9ee890));
  1483. x[6] = MAD_F_MLZ(hi, lo) + t1;
  1484. x[11] = -x[6];
  1485. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0f426cb5));
  1486. MAD_F_MLA(hi, lo, X[2], -MAD_F(0x00b2aa3e));
  1487. MAD_F_MLA(hi, lo, X[3], MAD_F(0x0898c779));
  1488. MAD_F_MLA(hi, lo, X[5], MAD_F(0x0f9ee890));
  1489. MAD_F_MLA(hi, lo, X[6], MAD_F(0x0acf37ad));
  1490. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x07635284));
  1491. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0e313245));
  1492. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0bcbe352));
  1493. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x03768962));
  1494. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0d7e8807));
  1495. MAD_F_MLA(hi, lo, X[15], MAD_F(0x0ffc19fd));
  1496. MAD_F_MLA(hi, lo, X[17], MAD_F(0x04cfb0e2));
  1497. x[23] = x[30] = MAD_F_MLZ(hi, lo) + t1;
  1498. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0bcbe352));
  1499. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0d7e8807));
  1500. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x07635284));
  1501. MAD_F_MLA(hi, lo, X[5], MAD_F(0x04cfb0e2));
  1502. MAD_F_MLA(hi, lo, X[6], MAD_F(0x0f9ee890));
  1503. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0ffc19fd));
  1504. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x00b2aa3e));
  1505. MAD_F_MLA(hi, lo, X[11], MAD_F(0x03768962));
  1506. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0f426cb5));
  1507. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0e313245));
  1508. MAD_F_MLA(hi, lo, X[15], MAD_F(0x0898c779));
  1509. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0acf37ad));
  1510. x[18] = x[35] = MAD_F_MLZ(hi, lo) - t1;
  1511. MAD_F_ML0(hi, lo, X[4], MAD_F(0x061f78aa));
  1512. MAD_F_MLA(hi, lo, X[13], -MAD_F(0x0ec835e8));
  1513. t7 = MAD_F_MLZ(hi, lo);
  1514. MAD_F_MLA(hi, lo, X[1], -MAD_F(0x0cb19346));
  1515. MAD_F_MLA(hi, lo, X[7], MAD_F(0x0fdcf549));
  1516. MAD_F_MLA(hi, lo, X[10], MAD_F(0x0216a2a2));
  1517. MAD_F_MLA(hi, lo, X[16], -MAD_F(0x09bd7ca0));
  1518. t2 = MAD_F_MLZ(hi, lo);
  1519. MAD_F_MLA(hi, lo, X[0], MAD_F(0x04cfb0e2));
  1520. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0ffc19fd));
  1521. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x0d7e8807));
  1522. MAD_F_MLA(hi, lo, X[5], MAD_F(0x03768962));
  1523. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x0bcbe352));
  1524. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0e313245));
  1525. MAD_F_MLA(hi, lo, X[9], MAD_F(0x07635284));
  1526. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0acf37ad));
  1527. MAD_F_MLA(hi, lo, X[12], MAD_F(0x0f9ee890));
  1528. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0898c779));
  1529. MAD_F_MLA(hi, lo, X[15], MAD_F(0x00b2aa3e));
  1530. MAD_F_MLA(hi, lo, X[17], MAD_F(0x0f426cb5));
  1531. x[5] = MAD_F_MLZ(hi, lo);
  1532. x[12] = -x[5];
  1533. MAD_F_ML0(hi, lo, X[0], MAD_F(0x0acf37ad));
  1534. MAD_F_MLA(hi, lo, X[2], -MAD_F(0x0898c779));
  1535. MAD_F_MLA(hi, lo, X[3], MAD_F(0x0e313245));
  1536. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0f426cb5));
  1537. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x03768962));
  1538. MAD_F_MLA(hi, lo, X[8], MAD_F(0x00b2aa3e));
  1539. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0ffc19fd));
  1540. MAD_F_MLA(hi, lo, X[11], MAD_F(0x0f9ee890));
  1541. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x04cfb0e2));
  1542. MAD_F_MLA(hi, lo, X[14], MAD_F(0x07635284));
  1543. MAD_F_MLA(hi, lo, X[15], MAD_F(0x0d7e8807));
  1544. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0bcbe352));
  1545. x[0] = MAD_F_MLZ(hi, lo) + t2;
  1546. x[17] = -x[0];
  1547. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0f9ee890));
  1548. MAD_F_MLA(hi, lo, X[2], -MAD_F(0x07635284));
  1549. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x00b2aa3e));
  1550. MAD_F_MLA(hi, lo, X[5], MAD_F(0x0bcbe352));
  1551. MAD_F_MLA(hi, lo, X[6], MAD_F(0x0f426cb5));
  1552. MAD_F_MLA(hi, lo, X[8], MAD_F(0x0d7e8807));
  1553. MAD_F_MLA(hi, lo, X[9], MAD_F(0x0898c779));
  1554. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x04cfb0e2));
  1555. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0acf37ad));
  1556. MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0ffc19fd));
  1557. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0e313245));
  1558. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x03768962));
  1559. x[24] = x[29] = MAD_F_MLZ(hi, lo) + t2;
  1560. MAD_F_ML0(hi, lo, X[1], -MAD_F(0x0216a2a2));
  1561. MAD_F_MLA(hi, lo, X[7], -MAD_F(0x09bd7ca0));
  1562. MAD_F_MLA(hi, lo, X[10], MAD_F(0x0cb19346));
  1563. MAD_F_MLA(hi, lo, X[16], MAD_F(0x0fdcf549));
  1564. t3 = MAD_F_MLZ(hi, lo) + t7;
  1565. MAD_F_ML0(hi, lo, X[0], MAD_F(0x00b2aa3e));
  1566. MAD_F_MLA(hi, lo, X[2], MAD_F(0x03768962));
  1567. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x04cfb0e2));
  1568. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x07635284));
  1569. MAD_F_MLA(hi, lo, X[6], MAD_F(0x0898c779));
  1570. MAD_F_MLA(hi, lo, X[8], MAD_F(0x0acf37ad));
  1571. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0bcbe352));
  1572. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0d7e8807));
  1573. MAD_F_MLA(hi, lo, X[12], MAD_F(0x0e313245));
  1574. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0f426cb5));
  1575. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0f9ee890));
  1576. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0ffc19fd));
  1577. x[8] = MAD_F_MLZ(hi, lo) + t3;
  1578. x[9] = -x[8];
  1579. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0e313245));
  1580. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0bcbe352));
  1581. MAD_F_MLA(hi, lo, X[3], MAD_F(0x0f9ee890));
  1582. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0898c779));
  1583. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x0ffc19fd));
  1584. MAD_F_MLA(hi, lo, X[8], MAD_F(0x04cfb0e2));
  1585. MAD_F_MLA(hi, lo, X[9], MAD_F(0x0f426cb5));
  1586. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x00b2aa3e));
  1587. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0d7e8807));
  1588. MAD_F_MLA(hi, lo, X[14], -MAD_F(0x03768962));
  1589. MAD_F_MLA(hi, lo, X[15], MAD_F(0x0acf37ad));
  1590. MAD_F_MLA(hi, lo, X[17], MAD_F(0x07635284));
  1591. x[21] = x[32] = MAD_F_MLZ(hi, lo) + t3;
  1592. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0d7e8807));
  1593. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0f426cb5));
  1594. MAD_F_MLA(hi, lo, X[3], MAD_F(0x0acf37ad));
  1595. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0ffc19fd));
  1596. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x07635284));
  1597. MAD_F_MLA(hi, lo, X[8], MAD_F(0x0f9ee890));
  1598. MAD_F_MLA(hi, lo, X[9], MAD_F(0x03768962));
  1599. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0e313245));
  1600. MAD_F_MLA(hi, lo, X[12], MAD_F(0x00b2aa3e));
  1601. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0bcbe352));
  1602. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x04cfb0e2));
  1603. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0898c779));
  1604. x[20] = x[33] = MAD_F_MLZ(hi, lo) - t3;
  1605. MAD_F_ML0(hi, lo, t14, -MAD_F(0x0ec835e8));
  1606. MAD_F_MLA(hi, lo, t15, MAD_F(0x061f78aa));
  1607. t4 = MAD_F_MLZ(hi, lo) - t7;
  1608. MAD_F_ML0(hi, lo, t12, MAD_F(0x061f78aa));
  1609. MAD_F_MLA(hi, lo, t13, MAD_F(0x0ec835e8));
  1610. x[4] = MAD_F_MLZ(hi, lo) + t4;
  1611. x[13] = -x[4];
  1612. MAD_F_ML0(hi, lo, t8, MAD_F(0x09bd7ca0));
  1613. MAD_F_MLA(hi, lo, t9, -MAD_F(0x0216a2a2));
  1614. MAD_F_MLA(hi, lo, t10, MAD_F(0x0fdcf549));
  1615. MAD_F_MLA(hi, lo, t11, -MAD_F(0x0cb19346));
  1616. x[1] = MAD_F_MLZ(hi, lo) + t4;
  1617. x[16] = -x[1];
  1618. MAD_F_ML0(hi, lo, t8, -MAD_F(0x0fdcf549));
  1619. MAD_F_MLA(hi, lo, t9, -MAD_F(0x0cb19346));
  1620. MAD_F_MLA(hi, lo, t10, -MAD_F(0x09bd7ca0));
  1621. MAD_F_MLA(hi, lo, t11, -MAD_F(0x0216a2a2));
  1622. x[25] = x[28] = MAD_F_MLZ(hi, lo) + t4;
  1623. MAD_F_ML0(hi, lo, X[1], -MAD_F(0x0fdcf549));
  1624. MAD_F_MLA(hi, lo, X[7], -MAD_F(0x0cb19346));
  1625. MAD_F_MLA(hi, lo, X[10], -MAD_F(0x09bd7ca0));
  1626. MAD_F_MLA(hi, lo, X[16], -MAD_F(0x0216a2a2));
  1627. t5 = MAD_F_MLZ(hi, lo) - t6;
  1628. MAD_F_ML0(hi, lo, X[0], MAD_F(0x0898c779));
  1629. MAD_F_MLA(hi, lo, X[2], MAD_F(0x04cfb0e2));
  1630. MAD_F_MLA(hi, lo, X[3], MAD_F(0x0bcbe352));
  1631. MAD_F_MLA(hi, lo, X[5], MAD_F(0x00b2aa3e));
  1632. MAD_F_MLA(hi, lo, X[6], MAD_F(0x0e313245));
  1633. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x03768962));
  1634. MAD_F_MLA(hi, lo, X[9], MAD_F(0x0f9ee890));
  1635. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x07635284));
  1636. MAD_F_MLA(hi, lo, X[12], MAD_F(0x0ffc19fd));
  1637. MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0acf37ad));
  1638. MAD_F_MLA(hi, lo, X[15], MAD_F(0x0f426cb5));
  1639. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0d7e8807));
  1640. x[2] = MAD_F_MLZ(hi, lo) + t5;
  1641. x[15] = -x[2];
  1642. MAD_F_ML0(hi, lo, X[0], MAD_F(0x07635284));
  1643. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0acf37ad));
  1644. MAD_F_MLA(hi, lo, X[3], MAD_F(0x03768962));
  1645. MAD_F_MLA(hi, lo, X[5], MAD_F(0x0d7e8807));
  1646. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x00b2aa3e));
  1647. MAD_F_MLA(hi, lo, X[8], MAD_F(0x0f426cb5));
  1648. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x04cfb0e2));
  1649. MAD_F_MLA(hi, lo, X[11], MAD_F(0x0ffc19fd));
  1650. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0898c779));
  1651. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0f9ee890));
  1652. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0bcbe352));
  1653. MAD_F_MLA(hi, lo, X[17], MAD_F(0x0e313245));
  1654. x[3] = MAD_F_MLZ(hi, lo) + t5;
  1655. x[14] = -x[3];
  1656. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0ffc19fd));
  1657. MAD_F_MLA(hi, lo, X[2], -MAD_F(0x0f9ee890));
  1658. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x0f426cb5));
  1659. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0e313245));
  1660. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x0d7e8807));
  1661. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0bcbe352));
  1662. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0acf37ad));
  1663. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0898c779));
  1664. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x07635284));
  1665. MAD_F_MLA(hi, lo, X[14], -MAD_F(0x04cfb0e2));
  1666. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x03768962));
  1667. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x00b2aa3e));
  1668. x[26] = x[27] = MAD_F_MLZ(hi, lo) + t5;
  1669. }
  1670. # endif
  1671. /*
  1672. * NAME: III_imdct_l()
  1673. * DESCRIPTION: perform IMDCT and windowing for long blocks
  1674. */
  1675. static
  1676. void III_imdct_l(mad_fixed_t const X[18], mad_fixed_t z[36],
  1677. unsigned int block_type)
  1678. {
  1679. unsigned int i;
  1680. /* IMDCT */
  1681. imdct36(X, z);
  1682. /* windowing */
  1683. switch (block_type) {
  1684. case 0: /* normal window */
  1685. # if defined(ASO_INTERLEAVE1)
  1686. {
  1687. register mad_fixed_t tmp1, tmp2;
  1688. tmp1 = window_l[0];
  1689. tmp2 = window_l[1];
  1690. for (i = 0; i < 34; i += 2) {
  1691. z[i + 0] = mad_f_mul(z[i + 0], tmp1);
  1692. tmp1 = window_l[i + 2];
  1693. z[i + 1] = mad_f_mul(z[i + 1], tmp2);
  1694. tmp2 = window_l[i + 3];
  1695. }
  1696. z[34] = mad_f_mul(z[34], tmp1);
  1697. z[35] = mad_f_mul(z[35], tmp2);
  1698. }
  1699. # elif defined(ASO_INTERLEAVE2)
  1700. {
  1701. register mad_fixed_t tmp1, tmp2;
  1702. tmp1 = z[0];
  1703. tmp2 = window_l[0];
  1704. for (i = 0; i < 35; ++i) {
  1705. z[i] = mad_f_mul(tmp1, tmp2);
  1706. tmp1 = z[i + 1];
  1707. tmp2 = window_l[i + 1];
  1708. }
  1709. z[35] = mad_f_mul(tmp1, tmp2);
  1710. }
  1711. # elif 1
  1712. for (i = 0; i < 36; i += 4) {
  1713. z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
  1714. z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
  1715. z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
  1716. z[i + 3] = mad_f_mul(z[i + 3], window_l[i + 3]);
  1717. }
  1718. # else
  1719. for (i = 0; i < 36; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
  1720. # endif
  1721. break;
  1722. case 1: /* start block */
  1723. for (i = 0; i < 18; i += 3) {
  1724. z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
  1725. z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
  1726. z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
  1727. }
  1728. /* (i = 18; i < 24; ++i) z[i] unchanged */
  1729. for (i = 24; i < 30; ++i) z[i] = mad_f_mul(z[i], window_s[i - 18]);
  1730. for (i = 30; i < 36; ++i) z[i] = 0;
  1731. break;
  1732. case 3: /* stop block */
  1733. for (i = 0; i < 6; ++i) z[i] = 0;
  1734. for (i = 6; i < 12; ++i) z[i] = mad_f_mul(z[i], window_s[i - 6]);
  1735. /* (i = 12; i < 18; ++i) z[i] unchanged */
  1736. for (i = 18; i < 36; i += 3) {
  1737. z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
  1738. z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
  1739. z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
  1740. }
  1741. break;
  1742. }
  1743. }
  1744. # endif /* ASO_IMDCT */
  1745. /*
  1746. * NAME: III_imdct_s()
  1747. * DESCRIPTION: perform IMDCT and windowing for short blocks
  1748. */
  1749. static
  1750. void III_imdct_s(mad_fixed_t const X[18], mad_fixed_t z[36])
  1751. {
  1752. mad_fixed_t y[36], *yptr;
  1753. mad_fixed_t const *wptr;
  1754. int w, i;
  1755. register mad_fixed64hi_t hi;
  1756. register mad_fixed64lo_t lo;
  1757. /* IMDCT */
  1758. yptr = &y[0];
  1759. for (w = 0; w < 3; ++w) {
  1760. register mad_fixed_t const (*s)[6];
  1761. s = imdct_s;
  1762. for (i = 0; i < 3; ++i) {
  1763. MAD_F_ML0(hi, lo, X[0], (*s)[0]);
  1764. MAD_F_MLA(hi, lo, X[1], (*s)[1]);
  1765. MAD_F_MLA(hi, lo, X[2], (*s)[2]);
  1766. MAD_F_MLA(hi, lo, X[3], (*s)[3]);
  1767. MAD_F_MLA(hi, lo, X[4], (*s)[4]);
  1768. MAD_F_MLA(hi, lo, X[5], (*s)[5]);
  1769. yptr[i + 0] = MAD_F_MLZ(hi, lo);
  1770. yptr[5 - i] = -yptr[i + 0];
  1771. ++s;
  1772. MAD_F_ML0(hi, lo, X[0], (*s)[0]);
  1773. MAD_F_MLA(hi, lo, X[1], (*s)[1]);
  1774. MAD_F_MLA(hi, lo, X[2], (*s)[2]);
  1775. MAD_F_MLA(hi, lo, X[3], (*s)[3]);
  1776. MAD_F_MLA(hi, lo, X[4], (*s)[4]);
  1777. MAD_F_MLA(hi, lo, X[5], (*s)[5]);
  1778. yptr[ i + 6] = MAD_F_MLZ(hi, lo);
  1779. yptr[11 - i] = yptr[i + 6];
  1780. ++s;
  1781. }
  1782. yptr += 12;
  1783. X += 6;
  1784. }
  1785. /* windowing, overlapping and concatenation */
  1786. yptr = &y[0];
  1787. wptr = &window_s[0];
  1788. for (i = 0; i < 6; ++i) {
  1789. z[i + 0] = 0;
  1790. z[i + 6] = mad_f_mul(yptr[ 0 + 0], wptr[0]);
  1791. MAD_F_ML0(hi, lo, yptr[ 0 + 6], wptr[6]);
  1792. MAD_F_MLA(hi, lo, yptr[12 + 0], wptr[0]);
  1793. z[i + 12] = MAD_F_MLZ(hi, lo);
  1794. MAD_F_ML0(hi, lo, yptr[12 + 6], wptr[6]);
  1795. MAD_F_MLA(hi, lo, yptr[24 + 0], wptr[0]);
  1796. z[i + 18] = MAD_F_MLZ(hi, lo);
  1797. z[i + 24] = mad_f_mul(yptr[24 + 6], wptr[6]);
  1798. z[i + 30] = 0;
  1799. ++yptr;
  1800. ++wptr;
  1801. }
  1802. }
  1803. /*
  1804. * NAME: III_overlap()
  1805. * DESCRIPTION: perform overlap-add of windowed IMDCT outputs
  1806. */
  1807. static
  1808. void III_overlap(mad_fixed_t const output[36], mad_fixed_t overlap[18],
  1809. mad_fixed_t sample[18][32], unsigned int sb)
  1810. {
  1811. unsigned int i;
  1812. # if defined(ASO_INTERLEAVE2)
  1813. {
  1814. register mad_fixed_t tmp1, tmp2;
  1815. tmp1 = overlap[0];
  1816. tmp2 = overlap[1];
  1817. for (i = 0; i < 16; i += 2) {
  1818. sample[i + 0][sb] = output[i + 0 + 0] + tmp1;
  1819. overlap[i + 0] = output[i + 0 + 18];
  1820. tmp1 = overlap[i + 2];
  1821. sample[i + 1][sb] = output[i + 1 + 0] + tmp2;
  1822. overlap[i + 1] = output[i + 1 + 18];
  1823. tmp2 = overlap[i + 3];
  1824. }
  1825. sample[16][sb] = output[16 + 0] + tmp1;
  1826. overlap[16] = output[16 + 18];
  1827. sample[17][sb] = output[17 + 0] + tmp2;
  1828. overlap[17] = output[17 + 18];
  1829. }
  1830. # elif 0
  1831. for (i = 0; i < 18; i += 2) {
  1832. sample[i + 0][sb] = output[i + 0 + 0] + overlap[i + 0];
  1833. overlap[i + 0] = output[i + 0 + 18];
  1834. sample[i + 1][sb] = output[i + 1 + 0] + overlap[i + 1];
  1835. overlap[i + 1] = output[i + 1 + 18];
  1836. }
  1837. # else
  1838. for (i = 0; i < 18; ++i) {
  1839. sample[i][sb] = output[i + 0] + overlap[i];
  1840. overlap[i] = output[i + 18];
  1841. }
  1842. # endif
  1843. }
  1844. /*
  1845. * NAME: III_overlap_z()
  1846. * DESCRIPTION: perform "overlap-add" of zero IMDCT outputs
  1847. */
  1848. static inline
  1849. void III_overlap_z(mad_fixed_t overlap[18],
  1850. mad_fixed_t sample[18][32], unsigned int sb)
  1851. {
  1852. unsigned int i;
  1853. # if defined(ASO_INTERLEAVE2)
  1854. {
  1855. register mad_fixed_t tmp1, tmp2;
  1856. tmp1 = overlap[0];
  1857. tmp2 = overlap[1];
  1858. for (i = 0; i < 16; i += 2) {
  1859. sample[i + 0][sb] = tmp1;
  1860. overlap[i + 0] = 0;
  1861. tmp1 = overlap[i + 2];
  1862. sample[i + 1][sb] = tmp2;
  1863. overlap[i + 1] = 0;
  1864. tmp2 = overlap[i + 3];
  1865. }
  1866. sample[16][sb] = tmp1;
  1867. overlap[16] = 0;
  1868. sample[17][sb] = tmp2;
  1869. overlap[17] = 0;
  1870. }
  1871. # else
  1872. for (i = 0; i < 18; ++i) {
  1873. sample[i][sb] = overlap[i];
  1874. overlap[i] = 0;
  1875. }
  1876. # endif
  1877. }
  1878. /*
  1879. * NAME: III_freqinver()
  1880. * DESCRIPTION: perform subband frequency inversion for odd sample lines
  1881. */
  1882. static
  1883. void III_freqinver(mad_fixed_t sample[18][32], unsigned int sb)
  1884. {
  1885. unsigned int i;
  1886. # if 1 || defined(ASO_INTERLEAVE1) || defined(ASO_INTERLEAVE2)
  1887. {
  1888. register mad_fixed_t tmp1, tmp2;
  1889. tmp1 = sample[1][sb];
  1890. tmp2 = sample[3][sb];
  1891. for (i = 1; i < 13; i += 4) {
  1892. sample[i + 0][sb] = -tmp1;
  1893. tmp1 = sample[i + 4][sb];
  1894. sample[i + 2][sb] = -tmp2;
  1895. tmp2 = sample[i + 6][sb];
  1896. }
  1897. sample[13][sb] = -tmp1;
  1898. tmp1 = sample[17][sb];
  1899. sample[15][sb] = -tmp2;
  1900. sample[17][sb] = -tmp1;
  1901. }
  1902. # else
  1903. for (i = 1; i < 18; i += 2)
  1904. sample[i][sb] = -sample[i][sb];
  1905. # endif
  1906. }
  1907. /*
  1908. * NAME: III_decode()
  1909. * DESCRIPTION: decode frame main_data
  1910. */
  1911. static
  1912. enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
  1913. struct sideinfo *si, unsigned int nch)
  1914. {
  1915. struct mad_header *header = &frame->header;
  1916. unsigned int sfreqi, ngr, gr;
  1917. {
  1918. unsigned int sfreq;
  1919. sfreq = header->samplerate;
  1920. if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
  1921. sfreq *= 2;
  1922. /* 48000 => 0, 44100 => 1, 32000 => 2,
  1923. 24000 => 3, 22050 => 4, 16000 => 5 */
  1924. sfreqi = ((sfreq >> 7) & 0x000f) +
  1925. ((sfreq >> 15) & 0x0001) - 8;
  1926. if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
  1927. sfreqi += 3;
  1928. }
  1929. /* scalefactors, Huffman decoding, requantization */
  1930. ngr = (header->flags & MAD_FLAG_LSF_EXT) ? 1 : 2;
  1931. for (gr = 0; gr < ngr; ++gr) {
  1932. struct granule *granule = &si->gr[gr];
  1933. unsigned char const *sfbwidth[2];
  1934. mad_fixed_t xr[2][576];
  1935. unsigned int ch;
  1936. enum mad_error error;
  1937. for (ch = 0; ch < nch; ++ch) {
  1938. struct channel *channel = &granule->ch[ch];
  1939. unsigned int part2_length;
  1940. sfbwidth[ch] = sfbwidth_table[sfreqi].l;
  1941. if (channel->block_type == 2) {
  1942. sfbwidth[ch] = (channel->flags & mixed_block_flag) ?
  1943. sfbwidth_table[sfreqi].m : sfbwidth_table[sfreqi].s;
  1944. }
  1945. if (header->flags & MAD_FLAG_LSF_EXT) {
  1946. part2_length = III_scalefactors_lsf(ptr, channel,
  1947. ch == 0 ? 0 : &si->gr[1].ch[1],
  1948. header->mode_extension);
  1949. }
  1950. else {
  1951. part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
  1952. gr == 0 ? 0 : si->scfsi[ch]);
  1953. }
  1954. error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part2_length);
  1955. if (error)
  1956. return error;
  1957. }
  1958. /* joint stereo processing */
  1959. if (header->mode == MAD_MODE_JOINT_STEREO && header->mode_extension) {
  1960. error = III_stereo(xr, granule, header, sfbwidth[0]);
  1961. if (error)
  1962. return error;
  1963. }
  1964. /* reordering, alias reduction, IMDCT, overlap-add, frequency inversion */
  1965. for (ch = 0; ch < nch; ++ch) {
  1966. struct channel const *channel = &granule->ch[ch];
  1967. mad_fixed_t (*sample)[32] = &frame->sbsample[ch][18 * gr];
  1968. unsigned int sb, l, i, sblimit;
  1969. mad_fixed_t output[36];
  1970. if (channel->block_type == 2) {
  1971. III_reorder(xr[ch], channel, sfbwidth[ch]);
  1972. # if !defined(OPT_STRICT)
  1973. /*
  1974. * According to ISO/IEC 11172-3, "Alias reduction is not applied for
  1975. * granules with block_type == 2 (short block)." However, other
  1976. * sources suggest alias reduction should indeed be performed on the
  1977. * lower two subbands of mixed blocks. Most other implementations do
  1978. * this, so by default we will too.
  1979. */
  1980. if (channel->flags & mixed_block_flag)
  1981. III_aliasreduce(xr[ch], 36);
  1982. # endif
  1983. }
  1984. else
  1985. III_aliasreduce(xr[ch], 576);
  1986. l = 0;
  1987. /* subbands 0-1 */
  1988. if (channel->block_type != 2 || (channel->flags & mixed_block_flag)) {
  1989. unsigned int block_type;
  1990. block_type = channel->block_type;
  1991. if (channel->flags & mixed_block_flag)
  1992. block_type = 0;
  1993. /* long blocks */
  1994. for (sb = 0; sb < 2; ++sb, l += 18) {
  1995. III_imdct_l(&xr[ch][l], output, block_type);
  1996. III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
  1997. }
  1998. }
  1999. else {
  2000. /* short blocks */
  2001. for (sb = 0; sb < 2; ++sb, l += 18) {
  2002. III_imdct_s(&xr[ch][l], output);
  2003. III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
  2004. }
  2005. }
  2006. III_freqinver(sample, 1);
  2007. /* (nonzero) subbands 2-31 */
  2008. i = 576;
  2009. while (i > 36 && xr[ch][i - 1] == 0)
  2010. --i;
  2011. sblimit = 32 - (576 - i) / 18;
  2012. if (channel->block_type != 2) {
  2013. /* long blocks */
  2014. for (sb = 2; sb < sblimit; ++sb, l += 18) {
  2015. III_imdct_l(&xr[ch][l], output, channel->block_type);
  2016. III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
  2017. if (sb & 1)
  2018. III_freqinver(sample, sb);
  2019. }
  2020. }
  2021. else {
  2022. /* short blocks */
  2023. for (sb = 2; sb < sblimit; ++sb, l += 18) {
  2024. III_imdct_s(&xr[ch][l], output);
  2025. III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
  2026. if (sb & 1)
  2027. III_freqinver(sample, sb);
  2028. }
  2029. }
  2030. /* remaining (zero) subbands */
  2031. for (sb = sblimit; sb < 32; ++sb) {
  2032. III_overlap_z((*frame->overlap)[ch][sb], sample, sb);
  2033. if (sb & 1)
  2034. III_freqinver(sample, sb);
  2035. }
  2036. }
  2037. }
  2038. return MAD_ERROR_NONE;
  2039. }
  2040. /*
  2041. * NAME: layer->III()
  2042. * DESCRIPTION: decode a single Layer III frame
  2043. */
  2044. int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
  2045. {
  2046. struct mad_header *header = &frame->header;
  2047. unsigned int nch, priv_bitlen, next_md_begin = 0;
  2048. unsigned int si_len, data_bitlen, md_len;
  2049. unsigned int frame_space, frame_used, frame_free;
  2050. struct mad_bitptr ptr;
  2051. struct sideinfo si;
  2052. enum mad_error error;
  2053. int result = 0;
  2054. /* allocate Layer III dynamic structures */
  2055. if (stream->main_data == 0) {
  2056. stream->main_data = malloc(MAD_BUFFER_MDLEN);
  2057. if (stream->main_data == 0) {
  2058. stream->error = MAD_ERROR_NOMEM;
  2059. return -1;
  2060. }
  2061. }
  2062. if (frame->overlap == 0) {
  2063. frame->overlap = calloc(2 * 32 * 18, sizeof(mad_fixed_t));
  2064. if (frame->overlap == 0) {
  2065. stream->error = MAD_ERROR_NOMEM;
  2066. return -1;
  2067. }
  2068. }
  2069. nch = MAD_NCHANNELS(header);
  2070. si_len = (header->flags & MAD_FLAG_LSF_EXT) ?
  2071. (nch == 1 ? 9 : 17) : (nch == 1 ? 17 : 32);
  2072. /* check frame sanity */
  2073. if (stream->next_frame - mad_bit_nextbyte(&stream->ptr) <
  2074. (signed int) si_len) {
  2075. stream->error = MAD_ERROR_BADFRAMELEN;
  2076. stream->md_len = 0;
  2077. return -1;
  2078. }
  2079. /* check CRC word */
  2080. if (header->flags & MAD_FLAG_PROTECTION) {
  2081. header->crc_check =
  2082. mad_bit_crc(stream->ptr, si_len * CHAR_BIT, header->crc_check);
  2083. if (header->crc_check != header->crc_target &&
  2084. !(frame->options & MAD_OPTION_IGNORECRC)) {
  2085. stream->error = MAD_ERROR_BADCRC;
  2086. result = -1;
  2087. }
  2088. }
  2089. /* decode frame side information */
  2090. error = III_sideinfo(&stream->ptr, nch, header->flags & MAD_FLAG_LSF_EXT,
  2091. &si, &data_bitlen, &priv_bitlen);
  2092. if (error && result == 0) {
  2093. stream->error = error;
  2094. result = -1;
  2095. }
  2096. header->flags |= priv_bitlen;
  2097. header->private_bits |= si.private_bits;
  2098. /* find main_data of next frame */
  2099. {
  2100. struct mad_bitptr peek;
  2101. unsigned long header;
  2102. mad_bit_init(&peek, stream->next_frame);
  2103. header = mad_bit_read(&peek, 32);
  2104. if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
  2105. if (!(header & 0x00010000L)) /* protection_bit */
  2106. mad_bit_skip(&peek, 16); /* crc_check */
  2107. next_md_begin =
  2108. mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
  2109. }
  2110. mad_bit_finish(&peek);
  2111. }
  2112. /* find main_data of this frame */
  2113. frame_space = stream->next_frame - mad_bit_nextbyte(&stream->ptr);
  2114. if (next_md_begin > si.main_data_begin + frame_space)
  2115. next_md_begin = 0;
  2116. md_len = si.main_data_begin + frame_space - next_md_begin;
  2117. frame_used = 0;
  2118. if (si.main_data_begin == 0) {
  2119. ptr = stream->ptr;
  2120. stream->md_len = 0;
  2121. frame_used = md_len;
  2122. }
  2123. else {
  2124. if (si.main_data_begin > stream->md_len) {
  2125. if (result == 0) {
  2126. stream->error = MAD_ERROR_BADDATAPTR;
  2127. result = -1;
  2128. }
  2129. }
  2130. else {
  2131. mad_bit_init(&ptr,
  2132. *stream->main_data + stream->md_len - si.main_data_begin);
  2133. if (md_len > si.main_data_begin) {
  2134. assert(stream->md_len + md_len -
  2135. si.main_data_begin <= MAD_BUFFER_MDLEN);
  2136. memcpy(*stream->main_data + stream->md_len,
  2137. mad_bit_nextbyte(&stream->ptr),
  2138. frame_used = md_len - si.main_data_begin);
  2139. stream->md_len += frame_used;
  2140. }
  2141. }
  2142. }
  2143. frame_free = frame_space - frame_used;
  2144. /* decode main_data */
  2145. if (result == 0) {
  2146. error = III_decode(&ptr, frame, &si, nch);
  2147. if (error) {
  2148. stream->error = error;
  2149. result = -1;
  2150. }
  2151. /* designate ancillary bits */
  2152. stream->anc_ptr = ptr;
  2153. stream->anc_bitlen = md_len * CHAR_BIT - data_bitlen;
  2154. }
  2155. # if 0 && defined(DEBUG)
  2156. fprintf(stderr,
  2157. "main_data_begin:%u, md_len:%u, frame_free:%u, "
  2158. "data_bitlen:%u, anc_bitlen: %u\n",
  2159. si.main_data_begin, md_len, frame_free,
  2160. data_bitlen, stream->anc_bitlen);
  2161. # endif
  2162. /* preload main_data buffer with up to 511 bytes for next frame(s) */
  2163. if (frame_free >= next_md_begin) {
  2164. memcpy(*stream->main_data,
  2165. stream->next_frame - next_md_begin, next_md_begin);
  2166. stream->md_len = next_md_begin;
  2167. }
  2168. else {
  2169. if (md_len < si.main_data_begin) {
  2170. unsigned int extra;
  2171. extra = si.main_data_begin - md_len;
  2172. if (extra + frame_free > next_md_begin)
  2173. extra = next_md_begin - frame_free;
  2174. if (extra < stream->md_len) {
  2175. memmove(*stream->main_data,
  2176. *stream->main_data + stream->md_len - extra, extra);
  2177. stream->md_len = extra;
  2178. }
  2179. }
  2180. else
  2181. stream->md_len = 0;
  2182. memcpy(*stream->main_data + stream->md_len,
  2183. stream->next_frame - frame_free, frame_free);
  2184. stream->md_len += frame_free;
  2185. }
  2186. return result;
  2187. }