dr_mp3.h 173 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697
  1. /*
  2. MP3 audio decoder. Choice of public domain or MIT-0. See license statements at the end of this file.
  3. dr_mp3 - v0.6.16 - 2020-08-02
  4. David Reid - [email protected]
  5. GitHub: https://github.com/mackron/dr_libs
  6. Based on minimp3 (https://github.com/lieff/minimp3) which is where the real work was done. See the bottom of this file for differences between minimp3 and dr_mp3.
  7. */
  8. /*
  9. RELEASE NOTES - VERSION 0.6
  10. ===========================
  11. Version 0.6 includes breaking changes with the configuration of decoders. The ability to customize the number of output channels and the sample rate has been
  12. removed. You must now use the channel count and sample rate reported by the MP3 stream itself, and all channel and sample rate conversion must be done
  13. yourself.
  14. Changes to Initialization
  15. -------------------------
  16. Previously, `drmp3_init()`, etc. took a pointer to a `drmp3_config` object that allowed you to customize the output channels and sample rate. This has been
  17. removed. If you need the old behaviour you will need to convert the data yourself or just not upgrade. The following APIs have changed.
  18. `drmp3_init()`
  19. `drmp3_init_memory()`
  20. `drmp3_init_file()`
  21. Miscellaneous Changes
  22. ---------------------
  23. Support for loading a file from a `wchar_t` string has been added via the `drmp3_init_file_w()` API.
  24. */
  25. /*
  26. Introducation
  27. =============
  28. dr_mp3 is a single file library. To use it, do something like the following in one .c file.
  29. ```c
  30. #define DR_MP3_IMPLEMENTATION
  31. #include "dr_mp3.h"
  32. ```
  33. You can then #include this file in other parts of the program as you would with any other header file. To decode audio data, do something like the following:
  34. ```c
  35. drmp3 mp3;
  36. if (!drmp3_init_file(&mp3, "MySong.mp3", NULL)) {
  37. // Failed to open file
  38. }
  39. ...
  40. drmp3_uint64 framesRead = drmp3_read_pcm_frames_f32(pMP3, framesToRead, pFrames);
  41. ```
  42. The drmp3 object is transparent so you can get access to the channel count and sample rate like so:
  43. ```
  44. drmp3_uint32 channels = mp3.channels;
  45. drmp3_uint32 sampleRate = mp3.sampleRate;
  46. ```
  47. The example above initializes a decoder from a file, but you can also initialize it from a block of memory and read and seek callbacks with
  48. `drmp3_init_memory()` and `drmp3_init()` respectively.
  49. You do not need to do any annoying memory management when reading PCM frames - this is all managed internally. You can request any number of PCM frames in each
  50. call to `drmp3_read_pcm_frames_f32()` and it will return as many PCM frames as it can, up to the requested amount.
  51. You can also decode an entire file in one go with `drmp3_open_and_read_pcm_frames_f32()`, `drmp3_open_memory_and_read_pcm_frames_f32()` and
  52. `drmp3_open_file_and_read_pcm_frames_f32()`.
  53. Build Options
  54. =============
  55. #define these options before including this file.
  56. #define DR_MP3_NO_STDIO
  57. Disable drmp3_init_file(), etc.
  58. #define DR_MP3_NO_SIMD
  59. Disable SIMD optimizations.
  60. */
  61. #ifndef dr_mp3_h
  62. #define dr_mp3_h
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. #define DRMP3_STRINGIFY(x) #x
  67. #define DRMP3_XSTRINGIFY(x) DRMP3_STRINGIFY(x)
  68. #define DRMP3_VERSION_MAJOR 0
  69. #define DRMP3_VERSION_MINOR 6
  70. #define DRMP3_VERSION_REVISION 16
  71. #define DRMP3_VERSION_STRING DRMP3_XSTRINGIFY(DRMP3_VERSION_MAJOR) "." DRMP3_XSTRINGIFY(DRMP3_VERSION_MINOR) "." DRMP3_XSTRINGIFY(DRMP3_VERSION_REVISION)
  72. #include <stddef.h> /* For size_t. */
  73. /* Sized types. */
  74. typedef signed char drmp3_int8;
  75. typedef unsigned char drmp3_uint8;
  76. typedef signed short drmp3_int16;
  77. typedef unsigned short drmp3_uint16;
  78. typedef signed int drmp3_int32;
  79. typedef unsigned int drmp3_uint32;
  80. #if defined(_MSC_VER)
  81. typedef signed __int64 drmp3_int64;
  82. typedef unsigned __int64 drmp3_uint64;
  83. #else
  84. #if defined(__GNUC__)
  85. #pragma GCC diagnostic push
  86. #pragma GCC diagnostic ignored "-Wlong-long"
  87. #if defined(__clang__)
  88. #pragma GCC diagnostic ignored "-Wc++11-long-long"
  89. #endif
  90. #endif
  91. typedef signed long long drmp3_int64;
  92. typedef unsigned long long drmp3_uint64;
  93. #if defined(__GNUC__)
  94. #pragma GCC diagnostic pop
  95. #endif
  96. #endif
  97. #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
  98. typedef drmp3_uint64 drmp3_uintptr;
  99. #else
  100. typedef drmp3_uint32 drmp3_uintptr;
  101. #endif
  102. typedef drmp3_uint8 drmp3_bool8;
  103. typedef drmp3_uint32 drmp3_bool32;
  104. #define DRMP3_TRUE 1
  105. #define DRMP3_FALSE 0
  106. #if !defined(DRMP3_API)
  107. #if defined(DRMP3_DLL)
  108. #if defined(_WIN32)
  109. #define DRMP3_DLL_IMPORT __declspec(dllimport)
  110. #define DRMP3_DLL_EXPORT __declspec(dllexport)
  111. #define DRMP3_DLL_PRIVATE static
  112. #else
  113. #if defined(__GNUC__) && __GNUC__ >= 4
  114. #define DRMP3_DLL_IMPORT __attribute__((visibility("default")))
  115. #define DRMP3_DLL_EXPORT __attribute__((visibility("default")))
  116. #define DRMP3_DLL_PRIVATE __attribute__((visibility("hidden")))
  117. #else
  118. #define DRMP3_DLL_IMPORT
  119. #define DRMP3_DLL_EXPORT
  120. #define DRMP3_DLL_PRIVATE static
  121. #endif
  122. #endif
  123. #if defined(DR_MP3_IMPLEMENTATION) || defined(DRMP3_IMPLEMENTATION)
  124. #define DRMP3_API DRMP3_DLL_EXPORT
  125. #else
  126. #define DRMP3_API DRMP3_DLL_IMPORT
  127. #endif
  128. #define DRMP3_PRIVATE DRMP3_DLL_PRIVATE
  129. #else
  130. #define DRMP3_API extern
  131. #define DRMP3_PRIVATE static
  132. #endif
  133. #endif
  134. typedef drmp3_int32 drmp3_result;
  135. #define DRMP3_SUCCESS 0
  136. #define DRMP3_ERROR -1 /* A generic error. */
  137. #define DRMP3_INVALID_ARGS -2
  138. #define DRMP3_INVALID_OPERATION -3
  139. #define DRMP3_OUT_OF_MEMORY -4
  140. #define DRMP3_OUT_OF_RANGE -5
  141. #define DRMP3_ACCESS_DENIED -6
  142. #define DRMP3_DOES_NOT_EXIST -7
  143. #define DRMP3_ALREADY_EXISTS -8
  144. #define DRMP3_TOO_MANY_OPEN_FILES -9
  145. #define DRMP3_INVALID_FILE -10
  146. #define DRMP3_TOO_BIG -11
  147. #define DRMP3_PATH_TOO_LONG -12
  148. #define DRMP3_NAME_TOO_LONG -13
  149. #define DRMP3_NOT_DIRECTORY -14
  150. #define DRMP3_IS_DIRECTORY -15
  151. #define DRMP3_DIRECTORY_NOT_EMPTY -16
  152. #define DRMP3_END_OF_FILE -17
  153. #define DRMP3_NO_SPACE -18
  154. #define DRMP3_BUSY -19
  155. #define DRMP3_IO_ERROR -20
  156. #define DRMP3_INTERRUPT -21
  157. #define DRMP3_UNAVAILABLE -22
  158. #define DRMP3_ALREADY_IN_USE -23
  159. #define DRMP3_BAD_ADDRESS -24
  160. #define DRMP3_BAD_SEEK -25
  161. #define DRMP3_BAD_PIPE -26
  162. #define DRMP3_DEADLOCK -27
  163. #define DRMP3_TOO_MANY_LINKS -28
  164. #define DRMP3_NOT_IMPLEMENTED -29
  165. #define DRMP3_NO_MESSAGE -30
  166. #define DRMP3_BAD_MESSAGE -31
  167. #define DRMP3_NO_DATA_AVAILABLE -32
  168. #define DRMP3_INVALID_DATA -33
  169. #define DRMP3_TIMEOUT -34
  170. #define DRMP3_NO_NETWORK -35
  171. #define DRMP3_NOT_UNIQUE -36
  172. #define DRMP3_NOT_SOCKET -37
  173. #define DRMP3_NO_ADDRESS -38
  174. #define DRMP3_BAD_PROTOCOL -39
  175. #define DRMP3_PROTOCOL_UNAVAILABLE -40
  176. #define DRMP3_PROTOCOL_NOT_SUPPORTED -41
  177. #define DRMP3_PROTOCOL_FAMILY_NOT_SUPPORTED -42
  178. #define DRMP3_ADDRESS_FAMILY_NOT_SUPPORTED -43
  179. #define DRMP3_SOCKET_NOT_SUPPORTED -44
  180. #define DRMP3_CONNECTION_RESET -45
  181. #define DRMP3_ALREADY_CONNECTED -46
  182. #define DRMP3_NOT_CONNECTED -47
  183. #define DRMP3_CONNECTION_REFUSED -48
  184. #define DRMP3_NO_HOST -49
  185. #define DRMP3_IN_PROGRESS -50
  186. #define DRMP3_CANCELLED -51
  187. #define DRMP3_MEMORY_ALREADY_MAPPED -52
  188. #define DRMP3_AT_END -53
  189. #define DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME 1152
  190. #define DRMP3_MAX_SAMPLES_PER_FRAME (DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME*2)
  191. #ifdef _MSC_VER
  192. #define DRMP3_INLINE __forceinline
  193. #elif defined(__GNUC__)
  194. /*
  195. I've had a bug report where GCC is emitting warnings about functions possibly not being inlineable. This warning happens when
  196. the __attribute__((always_inline)) attribute is defined without an "inline" statement. I think therefore there must be some
  197. case where "__inline__" is not always defined, thus the compiler emitting these warnings. When using -std=c89 or -ansi on the
  198. command line, we cannot use the "inline" keyword and instead need to use "__inline__". In an attempt to work around this issue
  199. I am using "__inline__" only when we're compiling in strict ANSI mode.
  200. */
  201. #if defined(__STRICT_ANSI__)
  202. #define DRMP3_INLINE __inline__ __attribute__((always_inline))
  203. #else
  204. #define DRMP3_INLINE inline __attribute__((always_inline))
  205. #endif
  206. #else
  207. #define DRMP3_INLINE
  208. #endif
  209. DRMP3_API void drmp3_version(drmp3_uint32* pMajor, drmp3_uint32* pMinor, drmp3_uint32* pRevision);
  210. DRMP3_API const char* drmp3_version_string(void);
  211. /*
  212. Low Level Push API
  213. ==================
  214. */
  215. typedef struct
  216. {
  217. int frame_bytes, channels, hz, layer, bitrate_kbps;
  218. } drmp3dec_frame_info;
  219. typedef struct
  220. {
  221. float mdct_overlap[2][9*32], qmf_state[15*2*32];
  222. int reserv, free_format_bytes;
  223. drmp3_uint8 header[4], reserv_buf[511];
  224. } drmp3dec;
  225. /* Initializes a low level decoder. */
  226. DRMP3_API void drmp3dec_init(drmp3dec *dec);
  227. /* Reads a frame from a low level decoder. */
  228. DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info);
  229. /* Helper for converting between f32 and s16. */
  230. DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num_samples);
  231. /*
  232. Main API (Pull API)
  233. ===================
  234. */
  235. #ifndef DRMP3_DEFAULT_CHANNELS
  236. #define DRMP3_DEFAULT_CHANNELS 2
  237. #endif
  238. #ifndef DRMP3_DEFAULT_SAMPLE_RATE
  239. #define DRMP3_DEFAULT_SAMPLE_RATE 44100
  240. #endif
  241. typedef enum
  242. {
  243. drmp3_seek_origin_start,
  244. drmp3_seek_origin_current
  245. } drmp3_seek_origin;
  246. typedef struct
  247. {
  248. drmp3_uint64 seekPosInBytes; /* Points to the first byte of an MP3 frame. */
  249. drmp3_uint64 pcmFrameIndex; /* The index of the PCM frame this seek point targets. */
  250. drmp3_uint16 mp3FramesToDiscard; /* The number of whole MP3 frames to be discarded before pcmFramesToDiscard. */
  251. drmp3_uint16 pcmFramesToDiscard; /* The number of leading samples to read and discard. These are discarded after mp3FramesToDiscard. */
  252. } drmp3_seek_point;
  253. /*
  254. Callback for when data is read. Return value is the number of bytes actually read.
  255. pUserData [in] The user data that was passed to drmp3_init(), drmp3_open() and family.
  256. pBufferOut [out] The output buffer.
  257. bytesToRead [in] The number of bytes to read.
  258. Returns the number of bytes actually read.
  259. A return value of less than bytesToRead indicates the end of the stream. Do _not_ return from this callback until
  260. either the entire bytesToRead is filled or you have reached the end of the stream.
  261. */
  262. typedef size_t (* drmp3_read_proc)(void* pUserData, void* pBufferOut, size_t bytesToRead);
  263. /*
  264. Callback for when data needs to be seeked.
  265. pUserData [in] The user data that was passed to drmp3_init(), drmp3_open() and family.
  266. offset [in] The number of bytes to move, relative to the origin. Will never be negative.
  267. origin [in] The origin of the seek - the current position or the start of the stream.
  268. Returns whether or not the seek was successful.
  269. Whether or not it is relative to the beginning or current position is determined by the "origin" parameter which
  270. will be either drmp3_seek_origin_start or drmp3_seek_origin_current.
  271. */
  272. typedef drmp3_bool32 (* drmp3_seek_proc)(void* pUserData, int offset, drmp3_seek_origin origin);
  273. typedef struct
  274. {
  275. void* pUserData;
  276. void* (* onMalloc)(size_t sz, void* pUserData);
  277. void* (* onRealloc)(void* p, size_t sz, void* pUserData);
  278. void (* onFree)(void* p, void* pUserData);
  279. } drmp3_allocation_callbacks;
  280. typedef struct
  281. {
  282. drmp3_uint32 channels;
  283. drmp3_uint32 sampleRate;
  284. } drmp3_config;
  285. typedef struct
  286. {
  287. drmp3dec decoder;
  288. drmp3dec_frame_info frameInfo;
  289. drmp3_uint32 channels;
  290. drmp3_uint32 sampleRate;
  291. drmp3_read_proc onRead;
  292. drmp3_seek_proc onSeek;
  293. void* pUserData;
  294. drmp3_allocation_callbacks allocationCallbacks;
  295. drmp3_uint32 mp3FrameChannels; /* The number of channels in the currently loaded MP3 frame. Internal use only. */
  296. drmp3_uint32 mp3FrameSampleRate; /* The sample rate of the currently loaded MP3 frame. Internal use only. */
  297. drmp3_uint32 pcmFramesConsumedInMP3Frame;
  298. drmp3_uint32 pcmFramesRemainingInMP3Frame;
  299. drmp3_uint8 pcmFrames[sizeof(float)*DRMP3_MAX_SAMPLES_PER_FRAME]; /* <-- Multipled by sizeof(float) to ensure there's enough room for DR_MP3_FLOAT_OUTPUT. */
  300. drmp3_uint64 currentPCMFrame; /* The current PCM frame, globally, based on the output sample rate. Mainly used for seeking. */
  301. drmp3_uint64 streamCursor; /* The current byte the decoder is sitting on in the raw stream. */
  302. drmp3_seek_point* pSeekPoints; /* NULL by default. Set with drmp3_bind_seek_table(). Memory is owned by the client. dr_mp3 will never attempt to free this pointer. */
  303. drmp3_uint32 seekPointCount; /* The number of items in pSeekPoints. When set to 0 assumes to no seek table. Defaults to zero. */
  304. size_t dataSize;
  305. size_t dataCapacity;
  306. size_t dataConsumed;
  307. drmp3_uint8* pData;
  308. drmp3_bool32 atEnd : 1;
  309. struct
  310. {
  311. const drmp3_uint8* pData;
  312. size_t dataSize;
  313. size_t currentReadPos;
  314. } memory; /* Only used for decoders that were opened against a block of memory. */
  315. } drmp3;
  316. /*
  317. Initializes an MP3 decoder.
  318. onRead [in] The function to call when data needs to be read from the client.
  319. onSeek [in] The function to call when the read position of the client data needs to move.
  320. pUserData [in, optional] A pointer to application defined data that will be passed to onRead and onSeek.
  321. Returns true if successful; false otherwise.
  322. Close the loader with drmp3_uninit().
  323. See also: drmp3_init_file(), drmp3_init_memory(), drmp3_uninit()
  324. */
  325. DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks);
  326. /*
  327. Initializes an MP3 decoder from a block of memory.
  328. This does not create a copy of the data. It is up to the application to ensure the buffer remains valid for
  329. the lifetime of the drmp3 object.
  330. The buffer should contain the contents of the entire MP3 file.
  331. */
  332. DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_allocation_callbacks* pAllocationCallbacks);
  333. #ifndef DR_MP3_NO_STDIO
  334. /*
  335. Initializes an MP3 decoder from a file.
  336. This holds the internal FILE object until drmp3_uninit() is called. Keep this in mind if you're caching drmp3
  337. objects because the operating system may restrict the number of file handles an application can have open at
  338. any given time.
  339. */
  340. DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks);
  341. DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks);
  342. #endif
  343. /*
  344. Uninitializes an MP3 decoder.
  345. */
  346. DRMP3_API void drmp3_uninit(drmp3* pMP3);
  347. /*
  348. Reads PCM frames as interleaved 32-bit IEEE floating point PCM.
  349. Note that framesToRead specifies the number of PCM frames to read, _not_ the number of MP3 frames.
  350. */
  351. DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut);
  352. /*
  353. Reads PCM frames as interleaved signed 16-bit integer PCM.
  354. Note that framesToRead specifies the number of PCM frames to read, _not_ the number of MP3 frames.
  355. */
  356. DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 framesToRead, drmp3_int16* pBufferOut);
  357. /*
  358. Seeks to a specific frame.
  359. Note that this is _not_ an MP3 frame, but rather a PCM frame.
  360. */
  361. DRMP3_API drmp3_bool32 drmp3_seek_to_pcm_frame(drmp3* pMP3, drmp3_uint64 frameIndex);
  362. /*
  363. Calculates the total number of PCM frames in the MP3 stream. Cannot be used for infinite streams such as internet
  364. radio. Runs in linear time. Returns 0 on error.
  365. */
  366. DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3);
  367. /*
  368. Calculates the total number of MP3 frames in the MP3 stream. Cannot be used for infinite streams such as internet
  369. radio. Runs in linear time. Returns 0 on error.
  370. */
  371. DRMP3_API drmp3_uint64 drmp3_get_mp3_frame_count(drmp3* pMP3);
  372. /*
  373. Calculates the total number of MP3 and PCM frames in the MP3 stream. Cannot be used for infinite streams such as internet
  374. radio. Runs in linear time. Returns 0 on error.
  375. This is equivalent to calling drmp3_get_mp3_frame_count() and drmp3_get_pcm_frame_count() except that it's more efficient.
  376. */
  377. DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint64* pMP3FrameCount, drmp3_uint64* pPCMFrameCount);
  378. /*
  379. Calculates the seekpoints based on PCM frames. This is slow.
  380. pSeekpoint count is a pointer to a uint32 containing the seekpoint count. On input it contains the desired count.
  381. On output it contains the actual count. The reason for this design is that the client may request too many
  382. seekpoints, in which case dr_mp3 will return a corrected count.
  383. Note that seektable seeking is not quite sample exact when the MP3 stream contains inconsistent sample rates.
  384. */
  385. DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints);
  386. /*
  387. Binds a seek table to the decoder.
  388. This does _not_ make a copy of pSeekPoints - it only references it. It is up to the application to ensure this
  389. remains valid while it is bound to the decoder.
  390. Use drmp3_calculate_seek_points() to calculate the seek points.
  391. */
  392. DRMP3_API drmp3_bool32 drmp3_bind_seek_table(drmp3* pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point* pSeekPoints);
  393. /*
  394. Opens an decodes an entire MP3 stream as a single operation.
  395. On output pConfig will receive the channel count and sample rate of the stream.
  396. Free the returned pointer with drmp3_free().
  397. */
  398. DRMP3_API float* drmp3_open_and_read_pcm_frames_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
  399. DRMP3_API drmp3_int16* drmp3_open_and_read_pcm_frames_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
  400. DRMP3_API float* drmp3_open_memory_and_read_pcm_frames_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
  401. DRMP3_API drmp3_int16* drmp3_open_memory_and_read_pcm_frames_s16(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
  402. #ifndef DR_MP3_NO_STDIO
  403. DRMP3_API float* drmp3_open_file_and_read_pcm_frames_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
  404. DRMP3_API drmp3_int16* drmp3_open_file_and_read_pcm_frames_s16(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
  405. #endif
  406. /*
  407. Allocates a block of memory on the heap.
  408. */
  409. DRMP3_API void* drmp3_malloc(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks);
  410. /*
  411. Frees any memory that was allocated by a public drmp3 API.
  412. */
  413. DRMP3_API void drmp3_free(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks);
  414. #ifdef __cplusplus
  415. }
  416. #endif
  417. #endif /* dr_mp3_h */
  418. /************************************************************************************************************************************************************
  419. ************************************************************************************************************************************************************
  420. IMPLEMENTATION
  421. ************************************************************************************************************************************************************
  422. ************************************************************************************************************************************************************/
  423. #if defined(DR_MP3_IMPLEMENTATION) || defined(DRMP3_IMPLEMENTATION)
  424. #ifndef dr_mp3_c
  425. #define dr_mp3_c
  426. #include <stdlib.h>
  427. #include <string.h>
  428. #include <limits.h> /* For INT_MAX */
  429. DRMP3_API void drmp3_version(drmp3_uint32* pMajor, drmp3_uint32* pMinor, drmp3_uint32* pRevision)
  430. {
  431. if (pMajor) {
  432. *pMajor = DRMP3_VERSION_MAJOR;
  433. }
  434. if (pMinor) {
  435. *pMinor = DRMP3_VERSION_MINOR;
  436. }
  437. if (pRevision) {
  438. *pRevision = DRMP3_VERSION_REVISION;
  439. }
  440. }
  441. DRMP3_API const char* drmp3_version_string(void)
  442. {
  443. return DRMP3_VERSION_STRING;
  444. }
  445. /* Disable SIMD when compiling with TCC for now. */
  446. #if defined(__TINYC__)
  447. #define DR_MP3_NO_SIMD
  448. #endif
  449. #define DRMP3_OFFSET_PTR(p, offset) ((void*)((drmp3_uint8*)(p) + (offset)))
  450. #define DRMP3_MAX_FREE_FORMAT_FRAME_SIZE 2304 /* more than ISO spec's */
  451. #ifndef DRMP3_MAX_FRAME_SYNC_MATCHES
  452. #define DRMP3_MAX_FRAME_SYNC_MATCHES 10
  453. #endif
  454. #define DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES DRMP3_MAX_FREE_FORMAT_FRAME_SIZE /* MUST be >= 320000/8/32000*1152 = 1440 */
  455. #define DRMP3_MAX_BITRESERVOIR_BYTES 511
  456. #define DRMP3_SHORT_BLOCK_TYPE 2
  457. #define DRMP3_STOP_BLOCK_TYPE 3
  458. #define DRMP3_MODE_MONO 3
  459. #define DRMP3_MODE_JOINT_STEREO 1
  460. #define DRMP3_HDR_SIZE 4
  461. #define DRMP3_HDR_IS_MONO(h) (((h[3]) & 0xC0) == 0xC0)
  462. #define DRMP3_HDR_IS_MS_STEREO(h) (((h[3]) & 0xE0) == 0x60)
  463. #define DRMP3_HDR_IS_FREE_FORMAT(h) (((h[2]) & 0xF0) == 0)
  464. #define DRMP3_HDR_IS_CRC(h) (!((h[1]) & 1))
  465. #define DRMP3_HDR_TEST_PADDING(h) ((h[2]) & 0x2)
  466. #define DRMP3_HDR_TEST_MPEG1(h) ((h[1]) & 0x8)
  467. #define DRMP3_HDR_TEST_NOT_MPEG25(h) ((h[1]) & 0x10)
  468. #define DRMP3_HDR_TEST_I_STEREO(h) ((h[3]) & 0x10)
  469. #define DRMP3_HDR_TEST_MS_STEREO(h) ((h[3]) & 0x20)
  470. #define DRMP3_HDR_GET_STEREO_MODE(h) (((h[3]) >> 6) & 3)
  471. #define DRMP3_HDR_GET_STEREO_MODE_EXT(h) (((h[3]) >> 4) & 3)
  472. #define DRMP3_HDR_GET_LAYER(h) (((h[1]) >> 1) & 3)
  473. #define DRMP3_HDR_GET_BITRATE(h) ((h[2]) >> 4)
  474. #define DRMP3_HDR_GET_SAMPLE_RATE(h) (((h[2]) >> 2) & 3)
  475. #define DRMP3_HDR_GET_MY_SAMPLE_RATE(h) (DRMP3_HDR_GET_SAMPLE_RATE(h) + (((h[1] >> 3) & 1) + ((h[1] >> 4) & 1))*3)
  476. #define DRMP3_HDR_IS_FRAME_576(h) ((h[1] & 14) == 2)
  477. #define DRMP3_HDR_IS_LAYER_1(h) ((h[1] & 6) == 6)
  478. #define DRMP3_BITS_DEQUANTIZER_OUT -1
  479. #define DRMP3_MAX_SCF (255 + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210)
  480. #define DRMP3_MAX_SCFI ((DRMP3_MAX_SCF + 3) & ~3)
  481. #define DRMP3_MIN(a, b) ((a) > (b) ? (b) : (a))
  482. #define DRMP3_MAX(a, b) ((a) < (b) ? (b) : (a))
  483. #if !defined(DR_MP3_NO_SIMD)
  484. #if !defined(DR_MP3_ONLY_SIMD) && (defined(_M_X64) || defined(_M_ARM64) || defined(__x86_64__) || defined(__aarch64__))
  485. /* x64 always have SSE2, arm64 always have neon, no need for generic code */
  486. #define DR_MP3_ONLY_SIMD
  487. #endif
  488. #if ((defined(_MSC_VER) && _MSC_VER >= 1400) && (defined(_M_IX86) || defined(_M_X64))) || ((defined(__i386__) || defined(__x86_64__)) && defined(__SSE2__))
  489. #if defined(_MSC_VER)
  490. #include <intrin.h>
  491. #endif
  492. #include <emmintrin.h>
  493. #define DRMP3_HAVE_SSE 1
  494. #define DRMP3_HAVE_SIMD 1
  495. #define DRMP3_VSTORE _mm_storeu_ps
  496. #define DRMP3_VLD _mm_loadu_ps
  497. #define DRMP3_VSET _mm_set1_ps
  498. #define DRMP3_VADD _mm_add_ps
  499. #define DRMP3_VSUB _mm_sub_ps
  500. #define DRMP3_VMUL _mm_mul_ps
  501. #define DRMP3_VMAC(a, x, y) _mm_add_ps(a, _mm_mul_ps(x, y))
  502. #define DRMP3_VMSB(a, x, y) _mm_sub_ps(a, _mm_mul_ps(x, y))
  503. #define DRMP3_VMUL_S(x, s) _mm_mul_ps(x, _mm_set1_ps(s))
  504. #define DRMP3_VREV(x) _mm_shuffle_ps(x, x, _MM_SHUFFLE(0, 1, 2, 3))
  505. typedef __m128 drmp3_f4;
  506. #if defined(_MSC_VER) || defined(DR_MP3_ONLY_SIMD)
  507. #define drmp3_cpuid __cpuid
  508. #else
  509. static __inline__ __attribute__((always_inline)) void drmp3_cpuid(int CPUInfo[], const int InfoType)
  510. {
  511. #if defined(__PIC__)
  512. __asm__ __volatile__(
  513. #if defined(__x86_64__)
  514. "push %%rbx\n"
  515. "cpuid\n"
  516. "xchgl %%ebx, %1\n"
  517. "pop %%rbx\n"
  518. #else
  519. "xchgl %%ebx, %1\n"
  520. "cpuid\n"
  521. "xchgl %%ebx, %1\n"
  522. #endif
  523. : "=a" (CPUInfo[0]), "=r" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
  524. : "a" (InfoType));
  525. #else
  526. __asm__ __volatile__(
  527. "cpuid"
  528. : "=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
  529. : "a" (InfoType));
  530. #endif
  531. }
  532. #endif
  533. static int drmp3_have_simd(void)
  534. {
  535. #ifdef DR_MP3_ONLY_SIMD
  536. return 1;
  537. #else
  538. static int g_have_simd;
  539. int CPUInfo[4];
  540. #ifdef MINIMP3_TEST
  541. static int g_counter;
  542. if (g_counter++ > 100)
  543. return 0;
  544. #endif
  545. if (g_have_simd)
  546. goto end;
  547. drmp3_cpuid(CPUInfo, 0);
  548. if (CPUInfo[0] > 0)
  549. {
  550. drmp3_cpuid(CPUInfo, 1);
  551. g_have_simd = (CPUInfo[3] & (1 << 26)) + 1; /* SSE2 */
  552. return g_have_simd - 1;
  553. }
  554. end:
  555. return g_have_simd - 1;
  556. #endif
  557. }
  558. #elif defined(__ARM_NEON) || defined(__aarch64__)
  559. #include <arm_neon.h>
  560. #define DRMP3_HAVE_SSE 0
  561. #define DRMP3_HAVE_SIMD 1
  562. #define DRMP3_VSTORE vst1q_f32
  563. #define DRMP3_VLD vld1q_f32
  564. #define DRMP3_VSET vmovq_n_f32
  565. #define DRMP3_VADD vaddq_f32
  566. #define DRMP3_VSUB vsubq_f32
  567. #define DRMP3_VMUL vmulq_f32
  568. #define DRMP3_VMAC(a, x, y) vmlaq_f32(a, x, y)
  569. #define DRMP3_VMSB(a, x, y) vmlsq_f32(a, x, y)
  570. #define DRMP3_VMUL_S(x, s) vmulq_f32(x, vmovq_n_f32(s))
  571. #define DRMP3_VREV(x) vcombine_f32(vget_high_f32(vrev64q_f32(x)), vget_low_f32(vrev64q_f32(x)))
  572. typedef float32x4_t drmp3_f4;
  573. static int drmp3_have_simd(void)
  574. { /* TODO: detect neon for !DR_MP3_ONLY_SIMD */
  575. return 1;
  576. }
  577. #else
  578. #define DRMP3_HAVE_SSE 0
  579. #define DRMP3_HAVE_SIMD 0
  580. #ifdef DR_MP3_ONLY_SIMD
  581. #error DR_MP3_ONLY_SIMD used, but SSE/NEON not enabled
  582. #endif
  583. #endif
  584. #else
  585. #define DRMP3_HAVE_SIMD 0
  586. #endif
  587. #if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__)
  588. #define DRMP3_HAVE_ARMV6 1
  589. static __inline__ __attribute__((always_inline)) drmp3_int32 drmp3_clip_int16_arm(int32_t a)
  590. {
  591. drmp3_int32 x = 0;
  592. __asm__ ("ssat %0, #16, %1" : "=r"(x) : "r"(a));
  593. return x;
  594. }
  595. #endif
  596. typedef struct
  597. {
  598. const drmp3_uint8 *buf;
  599. int pos, limit;
  600. } drmp3_bs;
  601. typedef struct
  602. {
  603. float scf[3*64];
  604. drmp3_uint8 total_bands, stereo_bands, bitalloc[64], scfcod[64];
  605. } drmp3_L12_scale_info;
  606. typedef struct
  607. {
  608. drmp3_uint8 tab_offset, code_tab_width, band_count;
  609. } drmp3_L12_subband_alloc;
  610. typedef struct
  611. {
  612. const drmp3_uint8 *sfbtab;
  613. drmp3_uint16 part_23_length, big_values, scalefac_compress;
  614. drmp3_uint8 global_gain, block_type, mixed_block_flag, n_long_sfb, n_short_sfb;
  615. drmp3_uint8 table_select[3], region_count[3], subblock_gain[3];
  616. drmp3_uint8 preflag, scalefac_scale, count1_table, scfsi;
  617. } drmp3_L3_gr_info;
  618. typedef struct
  619. {
  620. drmp3_bs bs;
  621. drmp3_uint8 maindata[DRMP3_MAX_BITRESERVOIR_BYTES + DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES];
  622. drmp3_L3_gr_info gr_info[4];
  623. float grbuf[2][576], scf[40], syn[18 + 15][2*32];
  624. drmp3_uint8 ist_pos[2][39];
  625. } drmp3dec_scratch;
  626. static void drmp3_bs_init(drmp3_bs *bs, const drmp3_uint8 *data, int bytes)
  627. {
  628. bs->buf = data;
  629. bs->pos = 0;
  630. bs->limit = bytes*8;
  631. }
  632. static drmp3_uint32 drmp3_bs_get_bits(drmp3_bs *bs, int n)
  633. {
  634. drmp3_uint32 next, cache = 0, s = bs->pos & 7;
  635. int shl = n + s;
  636. const drmp3_uint8 *p = bs->buf + (bs->pos >> 3);
  637. if ((bs->pos += n) > bs->limit)
  638. return 0;
  639. next = *p++ & (255 >> s);
  640. while ((shl -= 8) > 0)
  641. {
  642. cache |= next << shl;
  643. next = *p++;
  644. }
  645. return cache | (next >> -shl);
  646. }
  647. static int drmp3_hdr_valid(const drmp3_uint8 *h)
  648. {
  649. return h[0] == 0xff &&
  650. ((h[1] & 0xF0) == 0xf0 || (h[1] & 0xFE) == 0xe2) &&
  651. (DRMP3_HDR_GET_LAYER(h) != 0) &&
  652. (DRMP3_HDR_GET_BITRATE(h) != 15) &&
  653. (DRMP3_HDR_GET_SAMPLE_RATE(h) != 3);
  654. }
  655. static int drmp3_hdr_compare(const drmp3_uint8 *h1, const drmp3_uint8 *h2)
  656. {
  657. return drmp3_hdr_valid(h2) &&
  658. ((h1[1] ^ h2[1]) & 0xFE) == 0 &&
  659. ((h1[2] ^ h2[2]) & 0x0C) == 0 &&
  660. !(DRMP3_HDR_IS_FREE_FORMAT(h1) ^ DRMP3_HDR_IS_FREE_FORMAT(h2));
  661. }
  662. static unsigned drmp3_hdr_bitrate_kbps(const drmp3_uint8 *h)
  663. {
  664. static const drmp3_uint8 halfrate[2][3][15] = {
  665. { { 0,4,8,12,16,20,24,28,32,40,48,56,64,72,80 }, { 0,4,8,12,16,20,24,28,32,40,48,56,64,72,80 }, { 0,16,24,28,32,40,48,56,64,72,80,88,96,112,128 } },
  666. { { 0,16,20,24,28,32,40,48,56,64,80,96,112,128,160 }, { 0,16,24,28,32,40,48,56,64,80,96,112,128,160,192 }, { 0,16,32,48,64,80,96,112,128,144,160,176,192,208,224 } },
  667. };
  668. return 2*halfrate[!!DRMP3_HDR_TEST_MPEG1(h)][DRMP3_HDR_GET_LAYER(h) - 1][DRMP3_HDR_GET_BITRATE(h)];
  669. }
  670. static unsigned drmp3_hdr_sample_rate_hz(const drmp3_uint8 *h)
  671. {
  672. static const unsigned g_hz[3] = { 44100, 48000, 32000 };
  673. return g_hz[DRMP3_HDR_GET_SAMPLE_RATE(h)] >> (int)!DRMP3_HDR_TEST_MPEG1(h) >> (int)!DRMP3_HDR_TEST_NOT_MPEG25(h);
  674. }
  675. static unsigned drmp3_hdr_frame_samples(const drmp3_uint8 *h)
  676. {
  677. return DRMP3_HDR_IS_LAYER_1(h) ? 384 : (1152 >> (int)DRMP3_HDR_IS_FRAME_576(h));
  678. }
  679. static int drmp3_hdr_frame_bytes(const drmp3_uint8 *h, int free_format_size)
  680. {
  681. int frame_bytes = drmp3_hdr_frame_samples(h)*drmp3_hdr_bitrate_kbps(h)*125/drmp3_hdr_sample_rate_hz(h);
  682. if (DRMP3_HDR_IS_LAYER_1(h))
  683. {
  684. frame_bytes &= ~3; /* slot align */
  685. }
  686. return frame_bytes ? frame_bytes : free_format_size;
  687. }
  688. static int drmp3_hdr_padding(const drmp3_uint8 *h)
  689. {
  690. return DRMP3_HDR_TEST_PADDING(h) ? (DRMP3_HDR_IS_LAYER_1(h) ? 4 : 1) : 0;
  691. }
  692. #ifndef DR_MP3_ONLY_MP3
  693. static const drmp3_L12_subband_alloc *drmp3_L12_subband_alloc_table(const drmp3_uint8 *hdr, drmp3_L12_scale_info *sci)
  694. {
  695. const drmp3_L12_subband_alloc *alloc;
  696. int mode = DRMP3_HDR_GET_STEREO_MODE(hdr);
  697. int nbands, stereo_bands = (mode == DRMP3_MODE_MONO) ? 0 : (mode == DRMP3_MODE_JOINT_STEREO) ? (DRMP3_HDR_GET_STEREO_MODE_EXT(hdr) << 2) + 4 : 32;
  698. if (DRMP3_HDR_IS_LAYER_1(hdr))
  699. {
  700. static const drmp3_L12_subband_alloc g_alloc_L1[] = { { 76, 4, 32 } };
  701. alloc = g_alloc_L1;
  702. nbands = 32;
  703. } else if (!DRMP3_HDR_TEST_MPEG1(hdr))
  704. {
  705. static const drmp3_L12_subband_alloc g_alloc_L2M2[] = { { 60, 4, 4 }, { 44, 3, 7 }, { 44, 2, 19 } };
  706. alloc = g_alloc_L2M2;
  707. nbands = 30;
  708. } else
  709. {
  710. static const drmp3_L12_subband_alloc g_alloc_L2M1[] = { { 0, 4, 3 }, { 16, 4, 8 }, { 32, 3, 12 }, { 40, 2, 7 } };
  711. int sample_rate_idx = DRMP3_HDR_GET_SAMPLE_RATE(hdr);
  712. unsigned kbps = drmp3_hdr_bitrate_kbps(hdr) >> (int)(mode != DRMP3_MODE_MONO);
  713. if (!kbps) /* free-format */
  714. {
  715. kbps = 192;
  716. }
  717. alloc = g_alloc_L2M1;
  718. nbands = 27;
  719. if (kbps < 56)
  720. {
  721. static const drmp3_L12_subband_alloc g_alloc_L2M1_lowrate[] = { { 44, 4, 2 }, { 44, 3, 10 } };
  722. alloc = g_alloc_L2M1_lowrate;
  723. nbands = sample_rate_idx == 2 ? 12 : 8;
  724. } else if (kbps >= 96 && sample_rate_idx != 1)
  725. {
  726. nbands = 30;
  727. }
  728. }
  729. sci->total_bands = (drmp3_uint8)nbands;
  730. sci->stereo_bands = (drmp3_uint8)DRMP3_MIN(stereo_bands, nbands);
  731. return alloc;
  732. }
  733. static void drmp3_L12_read_scalefactors(drmp3_bs *bs, drmp3_uint8 *pba, drmp3_uint8 *scfcod, int bands, float *scf)
  734. {
  735. static const float g_deq_L12[18*3] = {
  736. #define DRMP3_DQ(x) 9.53674316e-07f/x, 7.56931807e-07f/x, 6.00777173e-07f/x
  737. DRMP3_DQ(3),DRMP3_DQ(7),DRMP3_DQ(15),DRMP3_DQ(31),DRMP3_DQ(63),DRMP3_DQ(127),DRMP3_DQ(255),DRMP3_DQ(511),DRMP3_DQ(1023),DRMP3_DQ(2047),DRMP3_DQ(4095),DRMP3_DQ(8191),DRMP3_DQ(16383),DRMP3_DQ(32767),DRMP3_DQ(65535),DRMP3_DQ(3),DRMP3_DQ(5),DRMP3_DQ(9)
  738. };
  739. int i, m;
  740. for (i = 0; i < bands; i++)
  741. {
  742. float s = 0;
  743. int ba = *pba++;
  744. int mask = ba ? 4 + ((19 >> scfcod[i]) & 3) : 0;
  745. for (m = 4; m; m >>= 1)
  746. {
  747. if (mask & m)
  748. {
  749. int b = drmp3_bs_get_bits(bs, 6);
  750. s = g_deq_L12[ba*3 - 6 + b % 3]*(int)(1 << 21 >> b/3);
  751. }
  752. *scf++ = s;
  753. }
  754. }
  755. }
  756. static void drmp3_L12_read_scale_info(const drmp3_uint8 *hdr, drmp3_bs *bs, drmp3_L12_scale_info *sci)
  757. {
  758. static const drmp3_uint8 g_bitalloc_code_tab[] = {
  759. 0,17, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16,
  760. 0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,16,
  761. 0,17,18, 3,19,4,5,16,
  762. 0,17,18,16,
  763. 0,17,18,19, 4,5,6, 7,8, 9,10,11,12,13,14,15,
  764. 0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,14,
  765. 0, 2, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16
  766. };
  767. const drmp3_L12_subband_alloc *subband_alloc = drmp3_L12_subband_alloc_table(hdr, sci);
  768. int i, k = 0, ba_bits = 0;
  769. const drmp3_uint8 *ba_code_tab = g_bitalloc_code_tab;
  770. for (i = 0; i < sci->total_bands; i++)
  771. {
  772. drmp3_uint8 ba;
  773. if (i == k)
  774. {
  775. k += subband_alloc->band_count;
  776. ba_bits = subband_alloc->code_tab_width;
  777. ba_code_tab = g_bitalloc_code_tab + subband_alloc->tab_offset;
  778. subband_alloc++;
  779. }
  780. ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)];
  781. sci->bitalloc[2*i] = ba;
  782. if (i < sci->stereo_bands)
  783. {
  784. ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)];
  785. }
  786. sci->bitalloc[2*i + 1] = sci->stereo_bands ? ba : 0;
  787. }
  788. for (i = 0; i < 2*sci->total_bands; i++)
  789. {
  790. sci->scfcod[i] = (drmp3_uint8)(sci->bitalloc[i] ? DRMP3_HDR_IS_LAYER_1(hdr) ? 2 : drmp3_bs_get_bits(bs, 2) : 6);
  791. }
  792. drmp3_L12_read_scalefactors(bs, sci->bitalloc, sci->scfcod, sci->total_bands*2, sci->scf);
  793. for (i = sci->stereo_bands; i < sci->total_bands; i++)
  794. {
  795. sci->bitalloc[2*i + 1] = 0;
  796. }
  797. }
  798. static int drmp3_L12_dequantize_granule(float *grbuf, drmp3_bs *bs, drmp3_L12_scale_info *sci, int group_size)
  799. {
  800. int i, j, k, choff = 576;
  801. for (j = 0; j < 4; j++)
  802. {
  803. float *dst = grbuf + group_size*j;
  804. for (i = 0; i < 2*sci->total_bands; i++)
  805. {
  806. int ba = sci->bitalloc[i];
  807. if (ba != 0)
  808. {
  809. if (ba < 17)
  810. {
  811. int half = (1 << (ba - 1)) - 1;
  812. for (k = 0; k < group_size; k++)
  813. {
  814. dst[k] = (float)((int)drmp3_bs_get_bits(bs, ba) - half);
  815. }
  816. } else
  817. {
  818. unsigned mod = (2 << (ba - 17)) + 1; /* 3, 5, 9 */
  819. unsigned code = drmp3_bs_get_bits(bs, mod + 2 - (mod >> 3)); /* 5, 7, 10 */
  820. for (k = 0; k < group_size; k++, code /= mod)
  821. {
  822. dst[k] = (float)((int)(code % mod - mod/2));
  823. }
  824. }
  825. }
  826. dst += choff;
  827. choff = 18 - choff;
  828. }
  829. }
  830. return group_size*4;
  831. }
  832. static void drmp3_L12_apply_scf_384(drmp3_L12_scale_info *sci, const float *scf, float *dst)
  833. {
  834. int i, k;
  835. memcpy(dst + 576 + sci->stereo_bands*18, dst + sci->stereo_bands*18, (sci->total_bands - sci->stereo_bands)*18*sizeof(float));
  836. for (i = 0; i < sci->total_bands; i++, dst += 18, scf += 6)
  837. {
  838. for (k = 0; k < 12; k++)
  839. {
  840. dst[k + 0] *= scf[0];
  841. dst[k + 576] *= scf[3];
  842. }
  843. }
  844. }
  845. #endif
  846. static int drmp3_L3_read_side_info(drmp3_bs *bs, drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr)
  847. {
  848. static const drmp3_uint8 g_scf_long[8][23] = {
  849. { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
  850. { 12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2,0 },
  851. { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
  852. { 6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36,0 },
  853. { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
  854. { 4,4,4,4,4,4,6,6,8,8,10,12,16,20,24,28,34,42,50,54,76,158,0 },
  855. { 4,4,4,4,4,4,6,6,6,8,10,12,16,18,22,28,34,40,46,54,54,192,0 },
  856. { 4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102,26,0 }
  857. };
  858. static const drmp3_uint8 g_scf_short[8][40] = {
  859. { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
  860. { 8,8,8,8,8,8,8,8,8,12,12,12,16,16,16,20,20,20,24,24,24,28,28,28,36,36,36,2,2,2,2,2,2,2,2,2,26,26,26,0 },
  861. { 4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,8,8,8,10,10,10,14,14,14,18,18,18,26,26,26,32,32,32,42,42,42,18,18,18,0 },
  862. { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,32,32,32,44,44,44,12,12,12,0 },
  863. { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
  864. { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,22,22,22,30,30,30,56,56,56,0 },
  865. { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,10,10,10,12,12,12,14,14,14,16,16,16,20,20,20,26,26,26,66,66,66,0 },
  866. { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,12,12,12,16,16,16,20,20,20,26,26,26,34,34,34,42,42,42,12,12,12,0 }
  867. };
  868. static const drmp3_uint8 g_scf_mixed[8][40] = {
  869. { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
  870. { 12,12,12,4,4,4,8,8,8,12,12,12,16,16,16,20,20,20,24,24,24,28,28,28,36,36,36,2,2,2,2,2,2,2,2,2,26,26,26,0 },
  871. { 6,6,6,6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,14,14,14,18,18,18,26,26,26,32,32,32,42,42,42,18,18,18,0 },
  872. { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,32,32,32,44,44,44,12,12,12,0 },
  873. { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
  874. { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,22,22,22,30,30,30,56,56,56,0 },
  875. { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,6,6,6,10,10,10,12,12,12,14,14,14,16,16,16,20,20,20,26,26,26,66,66,66,0 },
  876. { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,8,8,8,12,12,12,16,16,16,20,20,20,26,26,26,34,34,34,42,42,42,12,12,12,0 }
  877. };
  878. unsigned tables, scfsi = 0;
  879. int main_data_begin, part_23_sum = 0;
  880. int gr_count = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2;
  881. int sr_idx = DRMP3_HDR_GET_MY_SAMPLE_RATE(hdr); sr_idx -= (sr_idx != 0);
  882. if (DRMP3_HDR_TEST_MPEG1(hdr))
  883. {
  884. gr_count *= 2;
  885. main_data_begin = drmp3_bs_get_bits(bs, 9);
  886. scfsi = drmp3_bs_get_bits(bs, 7 + gr_count);
  887. } else
  888. {
  889. main_data_begin = drmp3_bs_get_bits(bs, 8 + gr_count) >> gr_count;
  890. }
  891. do
  892. {
  893. if (DRMP3_HDR_IS_MONO(hdr))
  894. {
  895. scfsi <<= 4;
  896. }
  897. gr->part_23_length = (drmp3_uint16)drmp3_bs_get_bits(bs, 12);
  898. part_23_sum += gr->part_23_length;
  899. gr->big_values = (drmp3_uint16)drmp3_bs_get_bits(bs, 9);
  900. if (gr->big_values > 288)
  901. {
  902. return -1;
  903. }
  904. gr->global_gain = (drmp3_uint8)drmp3_bs_get_bits(bs, 8);
  905. gr->scalefac_compress = (drmp3_uint16)drmp3_bs_get_bits(bs, DRMP3_HDR_TEST_MPEG1(hdr) ? 4 : 9);
  906. gr->sfbtab = g_scf_long[sr_idx];
  907. gr->n_long_sfb = 22;
  908. gr->n_short_sfb = 0;
  909. if (drmp3_bs_get_bits(bs, 1))
  910. {
  911. gr->block_type = (drmp3_uint8)drmp3_bs_get_bits(bs, 2);
  912. if (!gr->block_type)
  913. {
  914. return -1;
  915. }
  916. gr->mixed_block_flag = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
  917. gr->region_count[0] = 7;
  918. gr->region_count[1] = 255;
  919. if (gr->block_type == DRMP3_SHORT_BLOCK_TYPE)
  920. {
  921. scfsi &= 0x0F0F;
  922. if (!gr->mixed_block_flag)
  923. {
  924. gr->region_count[0] = 8;
  925. gr->sfbtab = g_scf_short[sr_idx];
  926. gr->n_long_sfb = 0;
  927. gr->n_short_sfb = 39;
  928. } else
  929. {
  930. gr->sfbtab = g_scf_mixed[sr_idx];
  931. gr->n_long_sfb = DRMP3_HDR_TEST_MPEG1(hdr) ? 8 : 6;
  932. gr->n_short_sfb = 30;
  933. }
  934. }
  935. tables = drmp3_bs_get_bits(bs, 10);
  936. tables <<= 5;
  937. gr->subblock_gain[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
  938. gr->subblock_gain[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
  939. gr->subblock_gain[2] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
  940. } else
  941. {
  942. gr->block_type = 0;
  943. gr->mixed_block_flag = 0;
  944. tables = drmp3_bs_get_bits(bs, 15);
  945. gr->region_count[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 4);
  946. gr->region_count[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
  947. gr->region_count[2] = 255;
  948. }
  949. gr->table_select[0] = (drmp3_uint8)(tables >> 10);
  950. gr->table_select[1] = (drmp3_uint8)((tables >> 5) & 31);
  951. gr->table_select[2] = (drmp3_uint8)((tables) & 31);
  952. gr->preflag = (drmp3_uint8)(DRMP3_HDR_TEST_MPEG1(hdr) ? drmp3_bs_get_bits(bs, 1) : (gr->scalefac_compress >= 500));
  953. gr->scalefac_scale = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
  954. gr->count1_table = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
  955. gr->scfsi = (drmp3_uint8)((scfsi >> 12) & 15);
  956. scfsi <<= 4;
  957. gr++;
  958. } while(--gr_count);
  959. if (part_23_sum + bs->pos > bs->limit + main_data_begin*8)
  960. {
  961. return -1;
  962. }
  963. return main_data_begin;
  964. }
  965. static void drmp3_L3_read_scalefactors(drmp3_uint8 *scf, drmp3_uint8 *ist_pos, const drmp3_uint8 *scf_size, const drmp3_uint8 *scf_count, drmp3_bs *bitbuf, int scfsi)
  966. {
  967. int i, k;
  968. for (i = 0; i < 4 && scf_count[i]; i++, scfsi *= 2)
  969. {
  970. int cnt = scf_count[i];
  971. if (scfsi & 8)
  972. {
  973. memcpy(scf, ist_pos, cnt);
  974. } else
  975. {
  976. int bits = scf_size[i];
  977. if (!bits)
  978. {
  979. memset(scf, 0, cnt);
  980. memset(ist_pos, 0, cnt);
  981. } else
  982. {
  983. int max_scf = (scfsi < 0) ? (1 << bits) - 1 : -1;
  984. for (k = 0; k < cnt; k++)
  985. {
  986. int s = drmp3_bs_get_bits(bitbuf, bits);
  987. ist_pos[k] = (drmp3_uint8)(s == max_scf ? -1 : s);
  988. scf[k] = (drmp3_uint8)s;
  989. }
  990. }
  991. }
  992. ist_pos += cnt;
  993. scf += cnt;
  994. }
  995. scf[0] = scf[1] = scf[2] = 0;
  996. }
  997. static float drmp3_L3_ldexp_q2(float y, int exp_q2)
  998. {
  999. static const float g_expfrac[4] = { 9.31322575e-10f,7.83145814e-10f,6.58544508e-10f,5.53767716e-10f };
  1000. int e;
  1001. do
  1002. {
  1003. e = DRMP3_MIN(30*4, exp_q2);
  1004. y *= g_expfrac[e & 3]*(1 << 30 >> (e >> 2));
  1005. } while ((exp_q2 -= e) > 0);
  1006. return y;
  1007. }
  1008. static void drmp3_L3_decode_scalefactors(const drmp3_uint8 *hdr, drmp3_uint8 *ist_pos, drmp3_bs *bs, const drmp3_L3_gr_info *gr, float *scf, int ch)
  1009. {
  1010. static const drmp3_uint8 g_scf_partitions[3][28] = {
  1011. { 6,5,5, 5,6,5,5,5,6,5, 7,3,11,10,0,0, 7, 7, 7,0, 6, 6,6,3, 8, 8,5,0 },
  1012. { 8,9,6,12,6,9,9,9,6,9,12,6,15,18,0,0, 6,15,12,0, 6,12,9,6, 6,18,9,0 },
  1013. { 9,9,6,12,9,9,9,9,9,9,12,6,18,18,0,0,12,12,12,0,12, 9,9,6,15,12,9,0 }
  1014. };
  1015. const drmp3_uint8 *scf_partition = g_scf_partitions[!!gr->n_short_sfb + !gr->n_long_sfb];
  1016. drmp3_uint8 scf_size[4], iscf[40];
  1017. int i, scf_shift = gr->scalefac_scale + 1, gain_exp, scfsi = gr->scfsi;
  1018. float gain;
  1019. if (DRMP3_HDR_TEST_MPEG1(hdr))
  1020. {
  1021. static const drmp3_uint8 g_scfc_decode[16] = { 0,1,2,3, 12,5,6,7, 9,10,11,13, 14,15,18,19 };
  1022. int part = g_scfc_decode[gr->scalefac_compress];
  1023. scf_size[1] = scf_size[0] = (drmp3_uint8)(part >> 2);
  1024. scf_size[3] = scf_size[2] = (drmp3_uint8)(part & 3);
  1025. } else
  1026. {
  1027. static const drmp3_uint8 g_mod[6*4] = { 5,5,4,4,5,5,4,1,4,3,1,1,5,6,6,1,4,4,4,1,4,3,1,1 };
  1028. int k, modprod, sfc, ist = DRMP3_HDR_TEST_I_STEREO(hdr) && ch;
  1029. sfc = gr->scalefac_compress >> ist;
  1030. for (k = ist*3*4; sfc >= 0; sfc -= modprod, k += 4)
  1031. {
  1032. for (modprod = 1, i = 3; i >= 0; i--)
  1033. {
  1034. scf_size[i] = (drmp3_uint8)(sfc / modprod % g_mod[k + i]);
  1035. modprod *= g_mod[k + i];
  1036. }
  1037. }
  1038. scf_partition += k;
  1039. scfsi = -16;
  1040. }
  1041. drmp3_L3_read_scalefactors(iscf, ist_pos, scf_size, scf_partition, bs, scfsi);
  1042. if (gr->n_short_sfb)
  1043. {
  1044. int sh = 3 - scf_shift;
  1045. for (i = 0; i < gr->n_short_sfb; i += 3)
  1046. {
  1047. iscf[gr->n_long_sfb + i + 0] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 0] + (gr->subblock_gain[0] << sh));
  1048. iscf[gr->n_long_sfb + i + 1] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 1] + (gr->subblock_gain[1] << sh));
  1049. iscf[gr->n_long_sfb + i + 2] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 2] + (gr->subblock_gain[2] << sh));
  1050. }
  1051. } else if (gr->preflag)
  1052. {
  1053. static const drmp3_uint8 g_preamp[10] = { 1,1,1,1,2,2,3,3,3,2 };
  1054. for (i = 0; i < 10; i++)
  1055. {
  1056. iscf[11 + i] = (drmp3_uint8)(iscf[11 + i] + g_preamp[i]);
  1057. }
  1058. }
  1059. gain_exp = gr->global_gain + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210 - (DRMP3_HDR_IS_MS_STEREO(hdr) ? 2 : 0);
  1060. gain = drmp3_L3_ldexp_q2(1 << (DRMP3_MAX_SCFI/4), DRMP3_MAX_SCFI - gain_exp);
  1061. for (i = 0; i < (int)(gr->n_long_sfb + gr->n_short_sfb); i++)
  1062. {
  1063. scf[i] = drmp3_L3_ldexp_q2(gain, iscf[i] << scf_shift);
  1064. }
  1065. }
  1066. static const float g_drmp3_pow43[129 + 16] = {
  1067. 0,-1,-2.519842f,-4.326749f,-6.349604f,-8.549880f,-10.902724f,-13.390518f,-16.000000f,-18.720754f,-21.544347f,-24.463781f,-27.473142f,-30.567351f,-33.741992f,-36.993181f,
  1068. 0,1,2.519842f,4.326749f,6.349604f,8.549880f,10.902724f,13.390518f,16.000000f,18.720754f,21.544347f,24.463781f,27.473142f,30.567351f,33.741992f,36.993181f,40.317474f,43.711787f,47.173345f,50.699631f,54.288352f,57.937408f,61.644865f,65.408941f,69.227979f,73.100443f,77.024898f,81.000000f,85.024491f,89.097188f,93.216975f,97.382800f,101.593667f,105.848633f,110.146801f,114.487321f,118.869381f,123.292209f,127.755065f,132.257246f,136.798076f,141.376907f,145.993119f,150.646117f,155.335327f,160.060199f,164.820202f,169.614826f,174.443577f,179.305980f,184.201575f,189.129918f,194.090580f,199.083145f,204.107210f,209.162385f,214.248292f,219.364564f,224.510845f,229.686789f,234.892058f,240.126328f,245.389280f,250.680604f,256.000000f,261.347174f,266.721841f,272.123723f,277.552547f,283.008049f,288.489971f,293.998060f,299.532071f,305.091761f,310.676898f,316.287249f,321.922592f,327.582707f,333.267377f,338.976394f,344.709550f,350.466646f,356.247482f,362.051866f,367.879608f,373.730522f,379.604427f,385.501143f,391.420496f,397.362314f,403.326427f,409.312672f,415.320884f,421.350905f,427.402579f,433.475750f,439.570269f,445.685987f,451.822757f,457.980436f,464.158883f,470.357960f,476.577530f,482.817459f,489.077615f,495.357868f,501.658090f,507.978156f,514.317941f,520.677324f,527.056184f,533.454404f,539.871867f,546.308458f,552.764065f,559.238575f,565.731879f,572.243870f,578.774440f,585.323483f,591.890898f,598.476581f,605.080431f,611.702349f,618.342238f,625.000000f,631.675540f,638.368763f,645.079578f
  1069. };
  1070. static float drmp3_L3_pow_43(int x)
  1071. {
  1072. float frac;
  1073. int sign, mult = 256;
  1074. if (x < 129)
  1075. {
  1076. return g_drmp3_pow43[16 + x];
  1077. }
  1078. if (x < 1024)
  1079. {
  1080. mult = 16;
  1081. x <<= 3;
  1082. }
  1083. sign = 2*x & 64;
  1084. frac = (float)((x & 63) - sign) / ((x & ~63) + sign);
  1085. return g_drmp3_pow43[16 + ((x + sign) >> 6)]*(1.f + frac*((4.f/3) + frac*(2.f/9)))*mult;
  1086. }
  1087. static void drmp3_L3_huffman(float *dst, drmp3_bs *bs, const drmp3_L3_gr_info *gr_info, const float *scf, int layer3gr_limit)
  1088. {
  1089. static const drmp3_int16 tabs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1090. 785,785,785,785,784,784,784,784,513,513,513,513,513,513,513,513,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,
  1091. -255,1313,1298,1282,785,785,785,785,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,290,288,
  1092. -255,1313,1298,1282,769,769,769,769,529,529,529,529,529,529,529,529,528,528,528,528,528,528,528,528,512,512,512,512,512,512,512,512,290,288,
  1093. -253,-318,-351,-367,785,785,785,785,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,819,818,547,547,275,275,275,275,561,560,515,546,289,274,288,258,
  1094. -254,-287,1329,1299,1314,1312,1057,1057,1042,1042,1026,1026,784,784,784,784,529,529,529,529,529,529,529,529,769,769,769,769,768,768,768,768,563,560,306,306,291,259,
  1095. -252,-413,-477,-542,1298,-575,1041,1041,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-383,-399,1107,1092,1106,1061,849,849,789,789,1104,1091,773,773,1076,1075,341,340,325,309,834,804,577,577,532,532,516,516,832,818,803,816,561,561,531,531,515,546,289,289,288,258,
  1096. -252,-429,-493,-559,1057,1057,1042,1042,529,529,529,529,529,529,529,529,784,784,784,784,769,769,769,769,512,512,512,512,512,512,512,512,-382,1077,-415,1106,1061,1104,849,849,789,789,1091,1076,1029,1075,834,834,597,581,340,340,339,324,804,833,532,532,832,772,818,803,817,787,816,771,290,290,290,290,288,258,
  1097. -253,-349,-414,-447,-463,1329,1299,-479,1314,1312,1057,1057,1042,1042,1026,1026,785,785,785,785,784,784,784,784,769,769,769,769,768,768,768,768,-319,851,821,-335,836,850,805,849,341,340,325,336,533,533,579,579,564,564,773,832,578,548,563,516,321,276,306,291,304,259,
  1098. -251,-572,-733,-830,-863,-879,1041,1041,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-511,-527,-543,1396,1351,1381,1366,1395,1335,1380,-559,1334,1138,1138,1063,1063,1350,1392,1031,1031,1062,1062,1364,1363,1120,1120,1333,1348,881,881,881,881,375,374,359,373,343,358,341,325,791,791,1123,1122,-703,1105,1045,-719,865,865,790,790,774,774,1104,1029,338,293,323,308,-799,-815,833,788,772,818,803,816,322,292,307,320,561,531,515,546,289,274,288,258,
  1099. -251,-525,-605,-685,-765,-831,-846,1298,1057,1057,1312,1282,785,785,785,785,784,784,784,784,769,769,769,769,512,512,512,512,512,512,512,512,1399,1398,1383,1367,1382,1396,1351,-511,1381,1366,1139,1139,1079,1079,1124,1124,1364,1349,1363,1333,882,882,882,882,807,807,807,807,1094,1094,1136,1136,373,341,535,535,881,775,867,822,774,-591,324,338,-671,849,550,550,866,864,609,609,293,336,534,534,789,835,773,-751,834,804,308,307,833,788,832,772,562,562,547,547,305,275,560,515,290,290,
  1100. -252,-397,-477,-557,-622,-653,-719,-735,-750,1329,1299,1314,1057,1057,1042,1042,1312,1282,1024,1024,785,785,785,785,784,784,784,784,769,769,769,769,-383,1127,1141,1111,1126,1140,1095,1110,869,869,883,883,1079,1109,882,882,375,374,807,868,838,881,791,-463,867,822,368,263,852,837,836,-543,610,610,550,550,352,336,534,534,865,774,851,821,850,805,593,533,579,564,773,832,578,578,548,548,577,577,307,276,306,291,516,560,259,259,
  1101. -250,-2107,-2507,-2764,-2909,-2974,-3007,-3023,1041,1041,1040,1040,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-767,-1052,-1213,-1277,-1358,-1405,-1469,-1535,-1550,-1582,-1614,-1647,-1662,-1694,-1726,-1759,-1774,-1807,-1822,-1854,-1886,1565,-1919,-1935,-1951,-1967,1731,1730,1580,1717,-1983,1729,1564,-1999,1548,-2015,-2031,1715,1595,-2047,1714,-2063,1610,-2079,1609,-2095,1323,1323,1457,1457,1307,1307,1712,1547,1641,1700,1699,1594,1685,1625,1442,1442,1322,1322,-780,-973,-910,1279,1278,1277,1262,1276,1261,1275,1215,1260,1229,-959,974,974,989,989,-943,735,478,478,495,463,506,414,-1039,1003,958,1017,927,942,987,957,431,476,1272,1167,1228,-1183,1256,-1199,895,895,941,941,1242,1227,1212,1135,1014,1014,490,489,503,487,910,1013,985,925,863,894,970,955,1012,847,-1343,831,755,755,984,909,428,366,754,559,-1391,752,486,457,924,997,698,698,983,893,740,740,908,877,739,739,667,667,953,938,497,287,271,271,683,606,590,712,726,574,302,302,738,736,481,286,526,725,605,711,636,724,696,651,589,681,666,710,364,467,573,695,466,466,301,465,379,379,709,604,665,679,316,316,634,633,436,436,464,269,424,394,452,332,438,363,347,408,393,448,331,422,362,407,392,421,346,406,391,376,375,359,1441,1306,-2367,1290,-2383,1337,-2399,-2415,1426,1321,-2431,1411,1336,-2447,-2463,-2479,1169,1169,1049,1049,1424,1289,1412,1352,1319,-2495,1154,1154,1064,1064,1153,1153,416,390,360,404,403,389,344,374,373,343,358,372,327,357,342,311,356,326,1395,1394,1137,1137,1047,1047,1365,1392,1287,1379,1334,1364,1349,1378,1318,1363,792,792,792,792,1152,1152,1032,1032,1121,1121,1046,1046,1120,1120,1030,1030,-2895,1106,1061,1104,849,849,789,789,1091,1076,1029,1090,1060,1075,833,833,309,324,532,532,832,772,818,803,561,561,531,560,515,546,289,274,288,258,
  1102. -250,-1179,-1579,-1836,-1996,-2124,-2253,-2333,-2413,-2477,-2542,-2574,-2607,-2622,-2655,1314,1313,1298,1312,1282,785,785,785,785,1040,1040,1025,1025,768,768,768,768,-766,-798,-830,-862,-895,-911,-927,-943,-959,-975,-991,-1007,-1023,-1039,-1055,-1070,1724,1647,-1103,-1119,1631,1767,1662,1738,1708,1723,-1135,1780,1615,1779,1599,1677,1646,1778,1583,-1151,1777,1567,1737,1692,1765,1722,1707,1630,1751,1661,1764,1614,1736,1676,1763,1750,1645,1598,1721,1691,1762,1706,1582,1761,1566,-1167,1749,1629,767,766,751,765,494,494,735,764,719,749,734,763,447,447,748,718,477,506,431,491,446,476,461,505,415,430,475,445,504,399,460,489,414,503,383,474,429,459,502,502,746,752,488,398,501,473,413,472,486,271,480,270,-1439,-1455,1357,-1471,-1487,-1503,1341,1325,-1519,1489,1463,1403,1309,-1535,1372,1448,1418,1476,1356,1462,1387,-1551,1475,1340,1447,1402,1386,-1567,1068,1068,1474,1461,455,380,468,440,395,425,410,454,364,467,466,464,453,269,409,448,268,432,1371,1473,1432,1417,1308,1460,1355,1446,1459,1431,1083,1083,1401,1416,1458,1445,1067,1067,1370,1457,1051,1051,1291,1430,1385,1444,1354,1415,1400,1443,1082,1082,1173,1113,1186,1066,1185,1050,-1967,1158,1128,1172,1097,1171,1081,-1983,1157,1112,416,266,375,400,1170,1142,1127,1065,793,793,1169,1033,1156,1096,1141,1111,1155,1080,1126,1140,898,898,808,808,897,897,792,792,1095,1152,1032,1125,1110,1139,1079,1124,882,807,838,881,853,791,-2319,867,368,263,822,852,837,866,806,865,-2399,851,352,262,534,534,821,836,594,594,549,549,593,593,533,533,848,773,579,579,564,578,548,563,276,276,577,576,306,291,516,560,305,305,275,259,
  1103. -251,-892,-2058,-2620,-2828,-2957,-3023,-3039,1041,1041,1040,1040,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-511,-527,-543,-559,1530,-575,-591,1528,1527,1407,1526,1391,1023,1023,1023,1023,1525,1375,1268,1268,1103,1103,1087,1087,1039,1039,1523,-604,815,815,815,815,510,495,509,479,508,463,507,447,431,505,415,399,-734,-782,1262,-815,1259,1244,-831,1258,1228,-847,-863,1196,-879,1253,987,987,748,-767,493,493,462,477,414,414,686,669,478,446,461,445,474,429,487,458,412,471,1266,1264,1009,1009,799,799,-1019,-1276,-1452,-1581,-1677,-1757,-1821,-1886,-1933,-1997,1257,1257,1483,1468,1512,1422,1497,1406,1467,1496,1421,1510,1134,1134,1225,1225,1466,1451,1374,1405,1252,1252,1358,1480,1164,1164,1251,1251,1238,1238,1389,1465,-1407,1054,1101,-1423,1207,-1439,830,830,1248,1038,1237,1117,1223,1148,1236,1208,411,426,395,410,379,269,1193,1222,1132,1235,1221,1116,976,976,1192,1162,1177,1220,1131,1191,963,963,-1647,961,780,-1663,558,558,994,993,437,408,393,407,829,978,813,797,947,-1743,721,721,377,392,844,950,828,890,706,706,812,859,796,960,948,843,934,874,571,571,-1919,690,555,689,421,346,539,539,944,779,918,873,932,842,903,888,570,570,931,917,674,674,-2575,1562,-2591,1609,-2607,1654,1322,1322,1441,1441,1696,1546,1683,1593,1669,1624,1426,1426,1321,1321,1639,1680,1425,1425,1305,1305,1545,1668,1608,1623,1667,1592,1638,1666,1320,1320,1652,1607,1409,1409,1304,1304,1288,1288,1664,1637,1395,1395,1335,1335,1622,1636,1394,1394,1319,1319,1606,1621,1392,1392,1137,1137,1137,1137,345,390,360,375,404,373,1047,-2751,-2767,-2783,1062,1121,1046,-2799,1077,-2815,1106,1061,789,789,1105,1104,263,355,310,340,325,354,352,262,339,324,1091,1076,1029,1090,1060,1075,833,833,788,788,1088,1028,818,818,803,803,561,561,531,531,816,771,546,546,289,274,288,258,
  1104. -253,-317,-381,-446,-478,-509,1279,1279,-811,-1179,-1451,-1756,-1900,-2028,-2189,-2253,-2333,-2414,-2445,-2511,-2526,1313,1298,-2559,1041,1041,1040,1040,1025,1025,1024,1024,1022,1007,1021,991,1020,975,1019,959,687,687,1018,1017,671,671,655,655,1016,1015,639,639,758,758,623,623,757,607,756,591,755,575,754,559,543,543,1009,783,-575,-621,-685,-749,496,-590,750,749,734,748,974,989,1003,958,988,973,1002,942,987,957,972,1001,926,986,941,971,956,1000,910,985,925,999,894,970,-1071,-1087,-1102,1390,-1135,1436,1509,1451,1374,-1151,1405,1358,1480,1420,-1167,1507,1494,1389,1342,1465,1435,1450,1326,1505,1310,1493,1373,1479,1404,1492,1464,1419,428,443,472,397,736,526,464,464,486,457,442,471,484,482,1357,1449,1434,1478,1388,1491,1341,1490,1325,1489,1463,1403,1309,1477,1372,1448,1418,1433,1476,1356,1462,1387,-1439,1475,1340,1447,1402,1474,1324,1461,1371,1473,269,448,1432,1417,1308,1460,-1711,1459,-1727,1441,1099,1099,1446,1386,1431,1401,-1743,1289,1083,1083,1160,1160,1458,1445,1067,1067,1370,1457,1307,1430,1129,1129,1098,1098,268,432,267,416,266,400,-1887,1144,1187,1082,1173,1113,1186,1066,1050,1158,1128,1143,1172,1097,1171,1081,420,391,1157,1112,1170,1142,1127,1065,1169,1049,1156,1096,1141,1111,1155,1080,1126,1154,1064,1153,1140,1095,1048,-2159,1125,1110,1137,-2175,823,823,1139,1138,807,807,384,264,368,263,868,838,853,791,867,822,852,837,866,806,865,790,-2319,851,821,836,352,262,850,805,849,-2399,533,533,835,820,336,261,578,548,563,577,532,532,832,772,562,562,547,547,305,275,560,515,290,290,288,258 };
  1105. static const drmp3_uint8 tab32[] = { 130,162,193,209,44,28,76,140,9,9,9,9,9,9,9,9,190,254,222,238,126,94,157,157,109,61,173,205};
  1106. static const drmp3_uint8 tab33[] = { 252,236,220,204,188,172,156,140,124,108,92,76,60,44,28,12 };
  1107. static const drmp3_int16 tabindex[2*16] = { 0,32,64,98,0,132,180,218,292,364,426,538,648,746,0,1126,1460,1460,1460,1460,1460,1460,1460,1460,1842,1842,1842,1842,1842,1842,1842,1842 };
  1108. static const drmp3_uint8 g_linbits[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,6,8,10,13,4,5,6,7,8,9,11,13 };
  1109. #define DRMP3_PEEK_BITS(n) (bs_cache >> (32 - n))
  1110. #define DRMP3_FLUSH_BITS(n) { bs_cache <<= (n); bs_sh += (n); }
  1111. #define DRMP3_CHECK_BITS while (bs_sh >= 0) { bs_cache |= (drmp3_uint32)*bs_next_ptr++ << bs_sh; bs_sh -= 8; }
  1112. #define DRMP3_BSPOS ((bs_next_ptr - bs->buf)*8 - 24 + bs_sh)
  1113. float one = 0.0f;
  1114. int ireg = 0, big_val_cnt = gr_info->big_values;
  1115. const drmp3_uint8 *sfb = gr_info->sfbtab;
  1116. const drmp3_uint8 *bs_next_ptr = bs->buf + bs->pos/8;
  1117. drmp3_uint32 bs_cache = (((bs_next_ptr[0]*256u + bs_next_ptr[1])*256u + bs_next_ptr[2])*256u + bs_next_ptr[3]) << (bs->pos & 7);
  1118. int pairs_to_decode, np, bs_sh = (bs->pos & 7) - 8;
  1119. bs_next_ptr += 4;
  1120. while (big_val_cnt > 0)
  1121. {
  1122. int tab_num = gr_info->table_select[ireg];
  1123. int sfb_cnt = gr_info->region_count[ireg++];
  1124. const drmp3_int16 *codebook = tabs + tabindex[tab_num];
  1125. int linbits = g_linbits[tab_num];
  1126. if (linbits)
  1127. {
  1128. do
  1129. {
  1130. np = *sfb++ / 2;
  1131. pairs_to_decode = DRMP3_MIN(big_val_cnt, np);
  1132. one = *scf++;
  1133. do
  1134. {
  1135. int j, w = 5;
  1136. int leaf = codebook[DRMP3_PEEK_BITS(w)];
  1137. while (leaf < 0)
  1138. {
  1139. DRMP3_FLUSH_BITS(w);
  1140. w = leaf & 7;
  1141. leaf = codebook[DRMP3_PEEK_BITS(w) - (leaf >> 3)];
  1142. }
  1143. DRMP3_FLUSH_BITS(leaf >> 8);
  1144. for (j = 0; j < 2; j++, dst++, leaf >>= 4)
  1145. {
  1146. int lsb = leaf & 0x0F;
  1147. if (lsb == 15)
  1148. {
  1149. lsb += DRMP3_PEEK_BITS(linbits);
  1150. DRMP3_FLUSH_BITS(linbits);
  1151. DRMP3_CHECK_BITS;
  1152. *dst = one*drmp3_L3_pow_43(lsb)*((drmp3_int32)bs_cache < 0 ? -1: 1);
  1153. } else
  1154. {
  1155. *dst = g_drmp3_pow43[16 + lsb - 16*(bs_cache >> 31)]*one;
  1156. }
  1157. DRMP3_FLUSH_BITS(lsb ? 1 : 0);
  1158. }
  1159. DRMP3_CHECK_BITS;
  1160. } while (--pairs_to_decode);
  1161. } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0);
  1162. } else
  1163. {
  1164. do
  1165. {
  1166. np = *sfb++ / 2;
  1167. pairs_to_decode = DRMP3_MIN(big_val_cnt, np);
  1168. one = *scf++;
  1169. do
  1170. {
  1171. int j, w = 5;
  1172. int leaf = codebook[DRMP3_PEEK_BITS(w)];
  1173. while (leaf < 0)
  1174. {
  1175. DRMP3_FLUSH_BITS(w);
  1176. w = leaf & 7;
  1177. leaf = codebook[DRMP3_PEEK_BITS(w) - (leaf >> 3)];
  1178. }
  1179. DRMP3_FLUSH_BITS(leaf >> 8);
  1180. for (j = 0; j < 2; j++, dst++, leaf >>= 4)
  1181. {
  1182. int lsb = leaf & 0x0F;
  1183. *dst = g_drmp3_pow43[16 + lsb - 16*(bs_cache >> 31)]*one;
  1184. DRMP3_FLUSH_BITS(lsb ? 1 : 0);
  1185. }
  1186. DRMP3_CHECK_BITS;
  1187. } while (--pairs_to_decode);
  1188. } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0);
  1189. }
  1190. }
  1191. for (np = 1 - big_val_cnt;; dst += 4)
  1192. {
  1193. const drmp3_uint8 *codebook_count1 = (gr_info->count1_table) ? tab33 : tab32;
  1194. int leaf = codebook_count1[DRMP3_PEEK_BITS(4)];
  1195. if (!(leaf & 8))
  1196. {
  1197. leaf = codebook_count1[(leaf >> 3) + (bs_cache << 4 >> (32 - (leaf & 3)))];
  1198. }
  1199. DRMP3_FLUSH_BITS(leaf & 7);
  1200. if (DRMP3_BSPOS > layer3gr_limit)
  1201. {
  1202. break;
  1203. }
  1204. #define DRMP3_RELOAD_SCALEFACTOR if (!--np) { np = *sfb++/2; if (!np) break; one = *scf++; }
  1205. #define DRMP3_DEQ_COUNT1(s) if (leaf & (128 >> s)) { dst[s] = ((drmp3_int32)bs_cache < 0) ? -one : one; DRMP3_FLUSH_BITS(1) }
  1206. DRMP3_RELOAD_SCALEFACTOR;
  1207. DRMP3_DEQ_COUNT1(0);
  1208. DRMP3_DEQ_COUNT1(1);
  1209. DRMP3_RELOAD_SCALEFACTOR;
  1210. DRMP3_DEQ_COUNT1(2);
  1211. DRMP3_DEQ_COUNT1(3);
  1212. DRMP3_CHECK_BITS;
  1213. }
  1214. bs->pos = layer3gr_limit;
  1215. }
  1216. static void drmp3_L3_midside_stereo(float *left, int n)
  1217. {
  1218. int i = 0;
  1219. float *right = left + 576;
  1220. #if DRMP3_HAVE_SIMD
  1221. if (drmp3_have_simd()) for (; i < n - 3; i += 4)
  1222. {
  1223. drmp3_f4 vl = DRMP3_VLD(left + i);
  1224. drmp3_f4 vr = DRMP3_VLD(right + i);
  1225. DRMP3_VSTORE(left + i, DRMP3_VADD(vl, vr));
  1226. DRMP3_VSTORE(right + i, DRMP3_VSUB(vl, vr));
  1227. }
  1228. #endif
  1229. for (; i < n; i++)
  1230. {
  1231. float a = left[i];
  1232. float b = right[i];
  1233. left[i] = a + b;
  1234. right[i] = a - b;
  1235. }
  1236. }
  1237. static void drmp3_L3_intensity_stereo_band(float *left, int n, float kl, float kr)
  1238. {
  1239. int i;
  1240. for (i = 0; i < n; i++)
  1241. {
  1242. left[i + 576] = left[i]*kr;
  1243. left[i] = left[i]*kl;
  1244. }
  1245. }
  1246. static void drmp3_L3_stereo_top_band(const float *right, const drmp3_uint8 *sfb, int nbands, int max_band[3])
  1247. {
  1248. int i, k;
  1249. max_band[0] = max_band[1] = max_band[2] = -1;
  1250. for (i = 0; i < nbands; i++)
  1251. {
  1252. for (k = 0; k < sfb[i]; k += 2)
  1253. {
  1254. if (right[k] != 0 || right[k + 1] != 0)
  1255. {
  1256. max_band[i % 3] = i;
  1257. break;
  1258. }
  1259. }
  1260. right += sfb[i];
  1261. }
  1262. }
  1263. static void drmp3_L3_stereo_process(float *left, const drmp3_uint8 *ist_pos, const drmp3_uint8 *sfb, const drmp3_uint8 *hdr, int max_band[3], int mpeg2_sh)
  1264. {
  1265. static const float g_pan[7*2] = { 0,1,0.21132487f,0.78867513f,0.36602540f,0.63397460f,0.5f,0.5f,0.63397460f,0.36602540f,0.78867513f,0.21132487f,1,0 };
  1266. unsigned i, max_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 7 : 64;
  1267. for (i = 0; sfb[i]; i++)
  1268. {
  1269. unsigned ipos = ist_pos[i];
  1270. if ((int)i > max_band[i % 3] && ipos < max_pos)
  1271. {
  1272. float kl, kr, s = DRMP3_HDR_TEST_MS_STEREO(hdr) ? 1.41421356f : 1;
  1273. if (DRMP3_HDR_TEST_MPEG1(hdr))
  1274. {
  1275. kl = g_pan[2*ipos];
  1276. kr = g_pan[2*ipos + 1];
  1277. } else
  1278. {
  1279. kl = 1;
  1280. kr = drmp3_L3_ldexp_q2(1, (ipos + 1) >> 1 << mpeg2_sh);
  1281. if (ipos & 1)
  1282. {
  1283. kl = kr;
  1284. kr = 1;
  1285. }
  1286. }
  1287. drmp3_L3_intensity_stereo_band(left, sfb[i], kl*s, kr*s);
  1288. } else if (DRMP3_HDR_TEST_MS_STEREO(hdr))
  1289. {
  1290. drmp3_L3_midside_stereo(left, sfb[i]);
  1291. }
  1292. left += sfb[i];
  1293. }
  1294. }
  1295. static void drmp3_L3_intensity_stereo(float *left, drmp3_uint8 *ist_pos, const drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr)
  1296. {
  1297. int max_band[3], n_sfb = gr->n_long_sfb + gr->n_short_sfb;
  1298. int i, max_blocks = gr->n_short_sfb ? 3 : 1;
  1299. drmp3_L3_stereo_top_band(left + 576, gr->sfbtab, n_sfb, max_band);
  1300. if (gr->n_long_sfb)
  1301. {
  1302. max_band[0] = max_band[1] = max_band[2] = DRMP3_MAX(DRMP3_MAX(max_band[0], max_band[1]), max_band[2]);
  1303. }
  1304. for (i = 0; i < max_blocks; i++)
  1305. {
  1306. int default_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 3 : 0;
  1307. int itop = n_sfb - max_blocks + i;
  1308. int prev = itop - max_blocks;
  1309. ist_pos[itop] = (drmp3_uint8)(max_band[i] >= prev ? default_pos : ist_pos[prev]);
  1310. }
  1311. drmp3_L3_stereo_process(left, ist_pos, gr->sfbtab, hdr, max_band, gr[1].scalefac_compress & 1);
  1312. }
  1313. static void drmp3_L3_reorder(float *grbuf, float *scratch, const drmp3_uint8 *sfb)
  1314. {
  1315. int i, len;
  1316. float *src = grbuf, *dst = scratch;
  1317. for (;0 != (len = *sfb); sfb += 3, src += 2*len)
  1318. {
  1319. for (i = 0; i < len; i++, src++)
  1320. {
  1321. *dst++ = src[0*len];
  1322. *dst++ = src[1*len];
  1323. *dst++ = src[2*len];
  1324. }
  1325. }
  1326. memcpy(grbuf, scratch, (dst - scratch)*sizeof(float));
  1327. }
  1328. static void drmp3_L3_antialias(float *grbuf, int nbands)
  1329. {
  1330. static const float g_aa[2][8] = {
  1331. {0.85749293f,0.88174200f,0.94962865f,0.98331459f,0.99551782f,0.99916056f,0.99989920f,0.99999316f},
  1332. {0.51449576f,0.47173197f,0.31337745f,0.18191320f,0.09457419f,0.04096558f,0.01419856f,0.00369997f}
  1333. };
  1334. for (; nbands > 0; nbands--, grbuf += 18)
  1335. {
  1336. int i = 0;
  1337. #if DRMP3_HAVE_SIMD
  1338. if (drmp3_have_simd()) for (; i < 8; i += 4)
  1339. {
  1340. drmp3_f4 vu = DRMP3_VLD(grbuf + 18 + i);
  1341. drmp3_f4 vd = DRMP3_VLD(grbuf + 14 - i);
  1342. drmp3_f4 vc0 = DRMP3_VLD(g_aa[0] + i);
  1343. drmp3_f4 vc1 = DRMP3_VLD(g_aa[1] + i);
  1344. vd = DRMP3_VREV(vd);
  1345. DRMP3_VSTORE(grbuf + 18 + i, DRMP3_VSUB(DRMP3_VMUL(vu, vc0), DRMP3_VMUL(vd, vc1)));
  1346. vd = DRMP3_VADD(DRMP3_VMUL(vu, vc1), DRMP3_VMUL(vd, vc0));
  1347. DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vd));
  1348. }
  1349. #endif
  1350. #ifndef DR_MP3_ONLY_SIMD
  1351. for(; i < 8; i++)
  1352. {
  1353. float u = grbuf[18 + i];
  1354. float d = grbuf[17 - i];
  1355. grbuf[18 + i] = u*g_aa[0][i] - d*g_aa[1][i];
  1356. grbuf[17 - i] = u*g_aa[1][i] + d*g_aa[0][i];
  1357. }
  1358. #endif
  1359. }
  1360. }
  1361. static void drmp3_L3_dct3_9(float *y)
  1362. {
  1363. float s0, s1, s2, s3, s4, s5, s6, s7, s8, t0, t2, t4;
  1364. s0 = y[0]; s2 = y[2]; s4 = y[4]; s6 = y[6]; s8 = y[8];
  1365. t0 = s0 + s6*0.5f;
  1366. s0 -= s6;
  1367. t4 = (s4 + s2)*0.93969262f;
  1368. t2 = (s8 + s2)*0.76604444f;
  1369. s6 = (s4 - s8)*0.17364818f;
  1370. s4 += s8 - s2;
  1371. s2 = s0 - s4*0.5f;
  1372. y[4] = s4 + s0;
  1373. s8 = t0 - t2 + s6;
  1374. s0 = t0 - t4 + t2;
  1375. s4 = t0 + t4 - s6;
  1376. s1 = y[1]; s3 = y[3]; s5 = y[5]; s7 = y[7];
  1377. s3 *= 0.86602540f;
  1378. t0 = (s5 + s1)*0.98480775f;
  1379. t4 = (s5 - s7)*0.34202014f;
  1380. t2 = (s1 + s7)*0.64278761f;
  1381. s1 = (s1 - s5 - s7)*0.86602540f;
  1382. s5 = t0 - s3 - t2;
  1383. s7 = t4 - s3 - t0;
  1384. s3 = t4 + s3 - t2;
  1385. y[0] = s4 - s7;
  1386. y[1] = s2 + s1;
  1387. y[2] = s0 - s3;
  1388. y[3] = s8 + s5;
  1389. y[5] = s8 - s5;
  1390. y[6] = s0 + s3;
  1391. y[7] = s2 - s1;
  1392. y[8] = s4 + s7;
  1393. }
  1394. static void drmp3_L3_imdct36(float *grbuf, float *overlap, const float *window, int nbands)
  1395. {
  1396. int i, j;
  1397. static const float g_twid9[18] = {
  1398. 0.73727734f,0.79335334f,0.84339145f,0.88701083f,0.92387953f,0.95371695f,0.97629601f,0.99144486f,0.99904822f,0.67559021f,0.60876143f,0.53729961f,0.46174861f,0.38268343f,0.30070580f,0.21643961f,0.13052619f,0.04361938f
  1399. };
  1400. for (j = 0; j < nbands; j++, grbuf += 18, overlap += 9)
  1401. {
  1402. float co[9], si[9];
  1403. co[0] = -grbuf[0];
  1404. si[0] = grbuf[17];
  1405. for (i = 0; i < 4; i++)
  1406. {
  1407. si[8 - 2*i] = grbuf[4*i + 1] - grbuf[4*i + 2];
  1408. co[1 + 2*i] = grbuf[4*i + 1] + grbuf[4*i + 2];
  1409. si[7 - 2*i] = grbuf[4*i + 4] - grbuf[4*i + 3];
  1410. co[2 + 2*i] = -(grbuf[4*i + 3] + grbuf[4*i + 4]);
  1411. }
  1412. drmp3_L3_dct3_9(co);
  1413. drmp3_L3_dct3_9(si);
  1414. si[1] = -si[1];
  1415. si[3] = -si[3];
  1416. si[5] = -si[5];
  1417. si[7] = -si[7];
  1418. i = 0;
  1419. #if DRMP3_HAVE_SIMD
  1420. if (drmp3_have_simd()) for (; i < 8; i += 4)
  1421. {
  1422. drmp3_f4 vovl = DRMP3_VLD(overlap + i);
  1423. drmp3_f4 vc = DRMP3_VLD(co + i);
  1424. drmp3_f4 vs = DRMP3_VLD(si + i);
  1425. drmp3_f4 vr0 = DRMP3_VLD(g_twid9 + i);
  1426. drmp3_f4 vr1 = DRMP3_VLD(g_twid9 + 9 + i);
  1427. drmp3_f4 vw0 = DRMP3_VLD(window + i);
  1428. drmp3_f4 vw1 = DRMP3_VLD(window + 9 + i);
  1429. drmp3_f4 vsum = DRMP3_VADD(DRMP3_VMUL(vc, vr1), DRMP3_VMUL(vs, vr0));
  1430. DRMP3_VSTORE(overlap + i, DRMP3_VSUB(DRMP3_VMUL(vc, vr0), DRMP3_VMUL(vs, vr1)));
  1431. DRMP3_VSTORE(grbuf + i, DRMP3_VSUB(DRMP3_VMUL(vovl, vw0), DRMP3_VMUL(vsum, vw1)));
  1432. vsum = DRMP3_VADD(DRMP3_VMUL(vovl, vw1), DRMP3_VMUL(vsum, vw0));
  1433. DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vsum));
  1434. }
  1435. #endif
  1436. for (; i < 9; i++)
  1437. {
  1438. float ovl = overlap[i];
  1439. float sum = co[i]*g_twid9[9 + i] + si[i]*g_twid9[0 + i];
  1440. overlap[i] = co[i]*g_twid9[0 + i] - si[i]*g_twid9[9 + i];
  1441. grbuf[i] = ovl*window[0 + i] - sum*window[9 + i];
  1442. grbuf[17 - i] = ovl*window[9 + i] + sum*window[0 + i];
  1443. }
  1444. }
  1445. }
  1446. static void drmp3_L3_idct3(float x0, float x1, float x2, float *dst)
  1447. {
  1448. float m1 = x1*0.86602540f;
  1449. float a1 = x0 - x2*0.5f;
  1450. dst[1] = x0 + x2;
  1451. dst[0] = a1 + m1;
  1452. dst[2] = a1 - m1;
  1453. }
  1454. static void drmp3_L3_imdct12(float *x, float *dst, float *overlap)
  1455. {
  1456. static const float g_twid3[6] = { 0.79335334f,0.92387953f,0.99144486f, 0.60876143f,0.38268343f,0.13052619f };
  1457. float co[3], si[3];
  1458. int i;
  1459. drmp3_L3_idct3(-x[0], x[6] + x[3], x[12] + x[9], co);
  1460. drmp3_L3_idct3(x[15], x[12] - x[9], x[6] - x[3], si);
  1461. si[1] = -si[1];
  1462. for (i = 0; i < 3; i++)
  1463. {
  1464. float ovl = overlap[i];
  1465. float sum = co[i]*g_twid3[3 + i] + si[i]*g_twid3[0 + i];
  1466. overlap[i] = co[i]*g_twid3[0 + i] - si[i]*g_twid3[3 + i];
  1467. dst[i] = ovl*g_twid3[2 - i] - sum*g_twid3[5 - i];
  1468. dst[5 - i] = ovl*g_twid3[5 - i] + sum*g_twid3[2 - i];
  1469. }
  1470. }
  1471. static void drmp3_L3_imdct_short(float *grbuf, float *overlap, int nbands)
  1472. {
  1473. for (;nbands > 0; nbands--, overlap += 9, grbuf += 18)
  1474. {
  1475. float tmp[18];
  1476. memcpy(tmp, grbuf, sizeof(tmp));
  1477. memcpy(grbuf, overlap, 6*sizeof(float));
  1478. drmp3_L3_imdct12(tmp, grbuf + 6, overlap + 6);
  1479. drmp3_L3_imdct12(tmp + 1, grbuf + 12, overlap + 6);
  1480. drmp3_L3_imdct12(tmp + 2, overlap, overlap + 6);
  1481. }
  1482. }
  1483. static void drmp3_L3_change_sign(float *grbuf)
  1484. {
  1485. int b, i;
  1486. for (b = 0, grbuf += 18; b < 32; b += 2, grbuf += 36)
  1487. for (i = 1; i < 18; i += 2)
  1488. grbuf[i] = -grbuf[i];
  1489. }
  1490. static void drmp3_L3_imdct_gr(float *grbuf, float *overlap, unsigned block_type, unsigned n_long_bands)
  1491. {
  1492. static const float g_mdct_window[2][18] = {
  1493. { 0.99904822f,0.99144486f,0.97629601f,0.95371695f,0.92387953f,0.88701083f,0.84339145f,0.79335334f,0.73727734f,0.04361938f,0.13052619f,0.21643961f,0.30070580f,0.38268343f,0.46174861f,0.53729961f,0.60876143f,0.67559021f },
  1494. { 1,1,1,1,1,1,0.99144486f,0.92387953f,0.79335334f,0,0,0,0,0,0,0.13052619f,0.38268343f,0.60876143f }
  1495. };
  1496. if (n_long_bands)
  1497. {
  1498. drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[0], n_long_bands);
  1499. grbuf += 18*n_long_bands;
  1500. overlap += 9*n_long_bands;
  1501. }
  1502. if (block_type == DRMP3_SHORT_BLOCK_TYPE)
  1503. drmp3_L3_imdct_short(grbuf, overlap, 32 - n_long_bands);
  1504. else
  1505. drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[block_type == DRMP3_STOP_BLOCK_TYPE], 32 - n_long_bands);
  1506. }
  1507. static void drmp3_L3_save_reservoir(drmp3dec *h, drmp3dec_scratch *s)
  1508. {
  1509. int pos = (s->bs.pos + 7)/8u;
  1510. int remains = s->bs.limit/8u - pos;
  1511. if (remains > DRMP3_MAX_BITRESERVOIR_BYTES)
  1512. {
  1513. pos += remains - DRMP3_MAX_BITRESERVOIR_BYTES;
  1514. remains = DRMP3_MAX_BITRESERVOIR_BYTES;
  1515. }
  1516. if (remains > 0)
  1517. {
  1518. memmove(h->reserv_buf, s->maindata + pos, remains);
  1519. }
  1520. h->reserv = remains;
  1521. }
  1522. static int drmp3_L3_restore_reservoir(drmp3dec *h, drmp3_bs *bs, drmp3dec_scratch *s, int main_data_begin)
  1523. {
  1524. int frame_bytes = (bs->limit - bs->pos)/8;
  1525. int bytes_have = DRMP3_MIN(h->reserv, main_data_begin);
  1526. memcpy(s->maindata, h->reserv_buf + DRMP3_MAX(0, h->reserv - main_data_begin), DRMP3_MIN(h->reserv, main_data_begin));
  1527. memcpy(s->maindata + bytes_have, bs->buf + bs->pos/8, frame_bytes);
  1528. drmp3_bs_init(&s->bs, s->maindata, bytes_have + frame_bytes);
  1529. return h->reserv >= main_data_begin;
  1530. }
  1531. static void drmp3_L3_decode(drmp3dec *h, drmp3dec_scratch *s, drmp3_L3_gr_info *gr_info, int nch)
  1532. {
  1533. int ch;
  1534. for (ch = 0; ch < nch; ch++)
  1535. {
  1536. int layer3gr_limit = s->bs.pos + gr_info[ch].part_23_length;
  1537. drmp3_L3_decode_scalefactors(h->header, s->ist_pos[ch], &s->bs, gr_info + ch, s->scf, ch);
  1538. drmp3_L3_huffman(s->grbuf[ch], &s->bs, gr_info + ch, s->scf, layer3gr_limit);
  1539. }
  1540. if (DRMP3_HDR_TEST_I_STEREO(h->header))
  1541. {
  1542. drmp3_L3_intensity_stereo(s->grbuf[0], s->ist_pos[1], gr_info, h->header);
  1543. } else if (DRMP3_HDR_IS_MS_STEREO(h->header))
  1544. {
  1545. drmp3_L3_midside_stereo(s->grbuf[0], 576);
  1546. }
  1547. for (ch = 0; ch < nch; ch++, gr_info++)
  1548. {
  1549. int aa_bands = 31;
  1550. int n_long_bands = (gr_info->mixed_block_flag ? 2 : 0) << (int)(DRMP3_HDR_GET_MY_SAMPLE_RATE(h->header) == 2);
  1551. if (gr_info->n_short_sfb)
  1552. {
  1553. aa_bands = n_long_bands - 1;
  1554. drmp3_L3_reorder(s->grbuf[ch] + n_long_bands*18, s->syn[0], gr_info->sfbtab + gr_info->n_long_sfb);
  1555. }
  1556. drmp3_L3_antialias(s->grbuf[ch], aa_bands);
  1557. drmp3_L3_imdct_gr(s->grbuf[ch], h->mdct_overlap[ch], gr_info->block_type, n_long_bands);
  1558. drmp3_L3_change_sign(s->grbuf[ch]);
  1559. }
  1560. }
  1561. static void drmp3d_DCT_II(float *grbuf, int n)
  1562. {
  1563. static const float g_sec[24] = {
  1564. 10.19000816f,0.50060302f,0.50241929f,3.40760851f,0.50547093f,0.52249861f,2.05778098f,0.51544732f,0.56694406f,1.48416460f,0.53104258f,0.64682180f,1.16943991f,0.55310392f,0.78815460f,0.97256821f,0.58293498f,1.06067765f,0.83934963f,0.62250412f,1.72244716f,0.74453628f,0.67480832f,5.10114861f
  1565. };
  1566. int i, k = 0;
  1567. #if DRMP3_HAVE_SIMD
  1568. if (drmp3_have_simd()) for (; k < n; k += 4)
  1569. {
  1570. drmp3_f4 t[4][8], *x;
  1571. float *y = grbuf + k;
  1572. for (x = t[0], i = 0; i < 8; i++, x++)
  1573. {
  1574. drmp3_f4 x0 = DRMP3_VLD(&y[i*18]);
  1575. drmp3_f4 x1 = DRMP3_VLD(&y[(15 - i)*18]);
  1576. drmp3_f4 x2 = DRMP3_VLD(&y[(16 + i)*18]);
  1577. drmp3_f4 x3 = DRMP3_VLD(&y[(31 - i)*18]);
  1578. drmp3_f4 t0 = DRMP3_VADD(x0, x3);
  1579. drmp3_f4 t1 = DRMP3_VADD(x1, x2);
  1580. drmp3_f4 t2 = DRMP3_VMUL_S(DRMP3_VSUB(x1, x2), g_sec[3*i + 0]);
  1581. drmp3_f4 t3 = DRMP3_VMUL_S(DRMP3_VSUB(x0, x3), g_sec[3*i + 1]);
  1582. x[0] = DRMP3_VADD(t0, t1);
  1583. x[8] = DRMP3_VMUL_S(DRMP3_VSUB(t0, t1), g_sec[3*i + 2]);
  1584. x[16] = DRMP3_VADD(t3, t2);
  1585. x[24] = DRMP3_VMUL_S(DRMP3_VSUB(t3, t2), g_sec[3*i + 2]);
  1586. }
  1587. for (x = t[0], i = 0; i < 4; i++, x += 8)
  1588. {
  1589. drmp3_f4 x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4], x5 = x[5], x6 = x[6], x7 = x[7], xt;
  1590. xt = DRMP3_VSUB(x0, x7); x0 = DRMP3_VADD(x0, x7);
  1591. x7 = DRMP3_VSUB(x1, x6); x1 = DRMP3_VADD(x1, x6);
  1592. x6 = DRMP3_VSUB(x2, x5); x2 = DRMP3_VADD(x2, x5);
  1593. x5 = DRMP3_VSUB(x3, x4); x3 = DRMP3_VADD(x3, x4);
  1594. x4 = DRMP3_VSUB(x0, x3); x0 = DRMP3_VADD(x0, x3);
  1595. x3 = DRMP3_VSUB(x1, x2); x1 = DRMP3_VADD(x1, x2);
  1596. x[0] = DRMP3_VADD(x0, x1);
  1597. x[4] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x1), 0.70710677f);
  1598. x5 = DRMP3_VADD(x5, x6);
  1599. x6 = DRMP3_VMUL_S(DRMP3_VADD(x6, x7), 0.70710677f);
  1600. x7 = DRMP3_VADD(x7, xt);
  1601. x3 = DRMP3_VMUL_S(DRMP3_VADD(x3, x4), 0.70710677f);
  1602. x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f)); /* rotate by PI/8 */
  1603. x7 = DRMP3_VADD(x7, DRMP3_VMUL_S(x5, 0.382683432f));
  1604. x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f));
  1605. x0 = DRMP3_VSUB(xt, x6); xt = DRMP3_VADD(xt, x6);
  1606. x[1] = DRMP3_VMUL_S(DRMP3_VADD(xt, x7), 0.50979561f);
  1607. x[2] = DRMP3_VMUL_S(DRMP3_VADD(x4, x3), 0.54119611f);
  1608. x[3] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x5), 0.60134488f);
  1609. x[5] = DRMP3_VMUL_S(DRMP3_VADD(x0, x5), 0.89997619f);
  1610. x[6] = DRMP3_VMUL_S(DRMP3_VSUB(x4, x3), 1.30656302f);
  1611. x[7] = DRMP3_VMUL_S(DRMP3_VSUB(xt, x7), 2.56291556f);
  1612. }
  1613. if (k > n - 3)
  1614. {
  1615. #if DRMP3_HAVE_SSE
  1616. #define DRMP3_VSAVE2(i, v) _mm_storel_pi((__m64 *)(void*)&y[i*18], v)
  1617. #else
  1618. #define DRMP3_VSAVE2(i, v) vst1_f32((float32_t *)&y[i*18], vget_low_f32(v))
  1619. #endif
  1620. for (i = 0; i < 7; i++, y += 4*18)
  1621. {
  1622. drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]);
  1623. DRMP3_VSAVE2(0, t[0][i]);
  1624. DRMP3_VSAVE2(1, DRMP3_VADD(t[2][i], s));
  1625. DRMP3_VSAVE2(2, DRMP3_VADD(t[1][i], t[1][i + 1]));
  1626. DRMP3_VSAVE2(3, DRMP3_VADD(t[2][1 + i], s));
  1627. }
  1628. DRMP3_VSAVE2(0, t[0][7]);
  1629. DRMP3_VSAVE2(1, DRMP3_VADD(t[2][7], t[3][7]));
  1630. DRMP3_VSAVE2(2, t[1][7]);
  1631. DRMP3_VSAVE2(3, t[3][7]);
  1632. } else
  1633. {
  1634. #define DRMP3_VSAVE4(i, v) DRMP3_VSTORE(&y[i*18], v)
  1635. for (i = 0; i < 7; i++, y += 4*18)
  1636. {
  1637. drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]);
  1638. DRMP3_VSAVE4(0, t[0][i]);
  1639. DRMP3_VSAVE4(1, DRMP3_VADD(t[2][i], s));
  1640. DRMP3_VSAVE4(2, DRMP3_VADD(t[1][i], t[1][i + 1]));
  1641. DRMP3_VSAVE4(3, DRMP3_VADD(t[2][1 + i], s));
  1642. }
  1643. DRMP3_VSAVE4(0, t[0][7]);
  1644. DRMP3_VSAVE4(1, DRMP3_VADD(t[2][7], t[3][7]));
  1645. DRMP3_VSAVE4(2, t[1][7]);
  1646. DRMP3_VSAVE4(3, t[3][7]);
  1647. }
  1648. } else
  1649. #endif
  1650. #ifdef DR_MP3_ONLY_SIMD
  1651. {}
  1652. #else
  1653. for (; k < n; k++)
  1654. {
  1655. float t[4][8], *x, *y = grbuf + k;
  1656. for (x = t[0], i = 0; i < 8; i++, x++)
  1657. {
  1658. float x0 = y[i*18];
  1659. float x1 = y[(15 - i)*18];
  1660. float x2 = y[(16 + i)*18];
  1661. float x3 = y[(31 - i)*18];
  1662. float t0 = x0 + x3;
  1663. float t1 = x1 + x2;
  1664. float t2 = (x1 - x2)*g_sec[3*i + 0];
  1665. float t3 = (x0 - x3)*g_sec[3*i + 1];
  1666. x[0] = t0 + t1;
  1667. x[8] = (t0 - t1)*g_sec[3*i + 2];
  1668. x[16] = t3 + t2;
  1669. x[24] = (t3 - t2)*g_sec[3*i + 2];
  1670. }
  1671. for (x = t[0], i = 0; i < 4; i++, x += 8)
  1672. {
  1673. float x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4], x5 = x[5], x6 = x[6], x7 = x[7], xt;
  1674. xt = x0 - x7; x0 += x7;
  1675. x7 = x1 - x6; x1 += x6;
  1676. x6 = x2 - x5; x2 += x5;
  1677. x5 = x3 - x4; x3 += x4;
  1678. x4 = x0 - x3; x0 += x3;
  1679. x3 = x1 - x2; x1 += x2;
  1680. x[0] = x0 + x1;
  1681. x[4] = (x0 - x1)*0.70710677f;
  1682. x5 = x5 + x6;
  1683. x6 = (x6 + x7)*0.70710677f;
  1684. x7 = x7 + xt;
  1685. x3 = (x3 + x4)*0.70710677f;
  1686. x5 -= x7*0.198912367f; /* rotate by PI/8 */
  1687. x7 += x5*0.382683432f;
  1688. x5 -= x7*0.198912367f;
  1689. x0 = xt - x6; xt += x6;
  1690. x[1] = (xt + x7)*0.50979561f;
  1691. x[2] = (x4 + x3)*0.54119611f;
  1692. x[3] = (x0 - x5)*0.60134488f;
  1693. x[5] = (x0 + x5)*0.89997619f;
  1694. x[6] = (x4 - x3)*1.30656302f;
  1695. x[7] = (xt - x7)*2.56291556f;
  1696. }
  1697. for (i = 0; i < 7; i++, y += 4*18)
  1698. {
  1699. y[0*18] = t[0][i];
  1700. y[1*18] = t[2][i] + t[3][i] + t[3][i + 1];
  1701. y[2*18] = t[1][i] + t[1][i + 1];
  1702. y[3*18] = t[2][i + 1] + t[3][i] + t[3][i + 1];
  1703. }
  1704. y[0*18] = t[0][7];
  1705. y[1*18] = t[2][7] + t[3][7];
  1706. y[2*18] = t[1][7];
  1707. y[3*18] = t[3][7];
  1708. }
  1709. #endif
  1710. }
  1711. #ifndef DR_MP3_FLOAT_OUTPUT
  1712. typedef drmp3_int16 drmp3d_sample_t;
  1713. static drmp3_int16 drmp3d_scale_pcm(float sample)
  1714. {
  1715. drmp3_int16 s;
  1716. #if DRMP3_HAVE_ARMV6
  1717. drmp3_int32 s32 = (drmp3_int32)(sample + .5f);
  1718. s32 -= (s32 < 0);
  1719. s = (drmp3_int16)drmp3_clip_int16_arm(s32);
  1720. #else
  1721. if (sample >= 32766.5) return (drmp3_int16) 32767;
  1722. if (sample <= -32767.5) return (drmp3_int16)-32768;
  1723. s = (drmp3_int16)(sample + .5f);
  1724. s -= (s < 0); /* away from zero, to be compliant */
  1725. #endif
  1726. return s;
  1727. }
  1728. #else
  1729. typedef float drmp3d_sample_t;
  1730. static float drmp3d_scale_pcm(float sample)
  1731. {
  1732. return sample*(1.f/32768.f);
  1733. }
  1734. #endif
  1735. static void drmp3d_synth_pair(drmp3d_sample_t *pcm, int nch, const float *z)
  1736. {
  1737. float a;
  1738. a = (z[14*64] - z[ 0]) * 29;
  1739. a += (z[ 1*64] + z[13*64]) * 213;
  1740. a += (z[12*64] - z[ 2*64]) * 459;
  1741. a += (z[ 3*64] + z[11*64]) * 2037;
  1742. a += (z[10*64] - z[ 4*64]) * 5153;
  1743. a += (z[ 5*64] + z[ 9*64]) * 6574;
  1744. a += (z[ 8*64] - z[ 6*64]) * 37489;
  1745. a += z[ 7*64] * 75038;
  1746. pcm[0] = drmp3d_scale_pcm(a);
  1747. z += 2;
  1748. a = z[14*64] * 104;
  1749. a += z[12*64] * 1567;
  1750. a += z[10*64] * 9727;
  1751. a += z[ 8*64] * 64019;
  1752. a += z[ 6*64] * -9975;
  1753. a += z[ 4*64] * -45;
  1754. a += z[ 2*64] * 146;
  1755. a += z[ 0*64] * -5;
  1756. pcm[16*nch] = drmp3d_scale_pcm(a);
  1757. }
  1758. static void drmp3d_synth(float *xl, drmp3d_sample_t *dstl, int nch, float *lins)
  1759. {
  1760. int i;
  1761. float *xr = xl + 576*(nch - 1);
  1762. drmp3d_sample_t *dstr = dstl + (nch - 1);
  1763. static const float g_win[] = {
  1764. -1,26,-31,208,218,401,-519,2063,2000,4788,-5517,7134,5959,35640,-39336,74992,
  1765. -1,24,-35,202,222,347,-581,2080,1952,4425,-5879,7640,5288,33791,-41176,74856,
  1766. -1,21,-38,196,225,294,-645,2087,1893,4063,-6237,8092,4561,31947,-43006,74630,
  1767. -1,19,-41,190,227,244,-711,2085,1822,3705,-6589,8492,3776,30112,-44821,74313,
  1768. -1,17,-45,183,228,197,-779,2075,1739,3351,-6935,8840,2935,28289,-46617,73908,
  1769. -1,16,-49,176,228,153,-848,2057,1644,3004,-7271,9139,2037,26482,-48390,73415,
  1770. -2,14,-53,169,227,111,-919,2032,1535,2663,-7597,9389,1082,24694,-50137,72835,
  1771. -2,13,-58,161,224,72,-991,2001,1414,2330,-7910,9592,70,22929,-51853,72169,
  1772. -2,11,-63,154,221,36,-1064,1962,1280,2006,-8209,9750,-998,21189,-53534,71420,
  1773. -2,10,-68,147,215,2,-1137,1919,1131,1692,-8491,9863,-2122,19478,-55178,70590,
  1774. -3,9,-73,139,208,-29,-1210,1870,970,1388,-8755,9935,-3300,17799,-56778,69679,
  1775. -3,8,-79,132,200,-57,-1283,1817,794,1095,-8998,9966,-4533,16155,-58333,68692,
  1776. -4,7,-85,125,189,-83,-1356,1759,605,814,-9219,9959,-5818,14548,-59838,67629,
  1777. -4,7,-91,117,177,-106,-1428,1698,402,545,-9416,9916,-7154,12980,-61289,66494,
  1778. -5,6,-97,111,163,-127,-1498,1634,185,288,-9585,9838,-8540,11455,-62684,65290
  1779. };
  1780. float *zlin = lins + 15*64;
  1781. const float *w = g_win;
  1782. zlin[4*15] = xl[18*16];
  1783. zlin[4*15 + 1] = xr[18*16];
  1784. zlin[4*15 + 2] = xl[0];
  1785. zlin[4*15 + 3] = xr[0];
  1786. zlin[4*31] = xl[1 + 18*16];
  1787. zlin[4*31 + 1] = xr[1 + 18*16];
  1788. zlin[4*31 + 2] = xl[1];
  1789. zlin[4*31 + 3] = xr[1];
  1790. drmp3d_synth_pair(dstr, nch, lins + 4*15 + 1);
  1791. drmp3d_synth_pair(dstr + 32*nch, nch, lins + 4*15 + 64 + 1);
  1792. drmp3d_synth_pair(dstl, nch, lins + 4*15);
  1793. drmp3d_synth_pair(dstl + 32*nch, nch, lins + 4*15 + 64);
  1794. #if DRMP3_HAVE_SIMD
  1795. if (drmp3_have_simd()) for (i = 14; i >= 0; i--)
  1796. {
  1797. #define DRMP3_VLOAD(k) drmp3_f4 w0 = DRMP3_VSET(*w++); drmp3_f4 w1 = DRMP3_VSET(*w++); drmp3_f4 vz = DRMP3_VLD(&zlin[4*i - 64*k]); drmp3_f4 vy = DRMP3_VLD(&zlin[4*i - 64*(15 - k)]);
  1798. #define DRMP3_V0(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0)) ; a = DRMP3_VSUB(DRMP3_VMUL(vz, w0), DRMP3_VMUL(vy, w1)); }
  1799. #define DRMP3_V1(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(b, DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0))); a = DRMP3_VADD(a, DRMP3_VSUB(DRMP3_VMUL(vz, w0), DRMP3_VMUL(vy, w1))); }
  1800. #define DRMP3_V2(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(b, DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0))); a = DRMP3_VADD(a, DRMP3_VSUB(DRMP3_VMUL(vy, w1), DRMP3_VMUL(vz, w0))); }
  1801. drmp3_f4 a, b;
  1802. zlin[4*i] = xl[18*(31 - i)];
  1803. zlin[4*i + 1] = xr[18*(31 - i)];
  1804. zlin[4*i + 2] = xl[1 + 18*(31 - i)];
  1805. zlin[4*i + 3] = xr[1 + 18*(31 - i)];
  1806. zlin[4*i + 64] = xl[1 + 18*(1 + i)];
  1807. zlin[4*i + 64 + 1] = xr[1 + 18*(1 + i)];
  1808. zlin[4*i - 64 + 2] = xl[18*(1 + i)];
  1809. zlin[4*i - 64 + 3] = xr[18*(1 + i)];
  1810. DRMP3_V0(0) DRMP3_V2(1) DRMP3_V1(2) DRMP3_V2(3) DRMP3_V1(4) DRMP3_V2(5) DRMP3_V1(6) DRMP3_V2(7)
  1811. {
  1812. #ifndef DR_MP3_FLOAT_OUTPUT
  1813. #if DRMP3_HAVE_SSE
  1814. static const drmp3_f4 g_max = { 32767.0f, 32767.0f, 32767.0f, 32767.0f };
  1815. static const drmp3_f4 g_min = { -32768.0f, -32768.0f, -32768.0f, -32768.0f };
  1816. __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, g_max), g_min)),
  1817. _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, g_max), g_min)));
  1818. dstr[(15 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 1);
  1819. dstr[(17 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 5);
  1820. dstl[(15 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 0);
  1821. dstl[(17 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 4);
  1822. dstr[(47 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 3);
  1823. dstr[(49 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 7);
  1824. dstl[(47 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 2);
  1825. dstl[(49 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 6);
  1826. #else
  1827. int16x4_t pcma, pcmb;
  1828. a = DRMP3_VADD(a, DRMP3_VSET(0.5f));
  1829. b = DRMP3_VADD(b, DRMP3_VSET(0.5f));
  1830. pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0)))));
  1831. pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0)))));
  1832. vst1_lane_s16(dstr + (15 - i)*nch, pcma, 1);
  1833. vst1_lane_s16(dstr + (17 + i)*nch, pcmb, 1);
  1834. vst1_lane_s16(dstl + (15 - i)*nch, pcma, 0);
  1835. vst1_lane_s16(dstl + (17 + i)*nch, pcmb, 0);
  1836. vst1_lane_s16(dstr + (47 - i)*nch, pcma, 3);
  1837. vst1_lane_s16(dstr + (49 + i)*nch, pcmb, 3);
  1838. vst1_lane_s16(dstl + (47 - i)*nch, pcma, 2);
  1839. vst1_lane_s16(dstl + (49 + i)*nch, pcmb, 2);
  1840. #endif
  1841. #else
  1842. static const drmp3_f4 g_scale = { 1.0f/32768.0f, 1.0f/32768.0f, 1.0f/32768.0f, 1.0f/32768.0f };
  1843. a = DRMP3_VMUL(a, g_scale);
  1844. b = DRMP3_VMUL(b, g_scale);
  1845. #if DRMP3_HAVE_SSE
  1846. _mm_store_ss(dstr + (15 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(1, 1, 1, 1)));
  1847. _mm_store_ss(dstr + (17 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(1, 1, 1, 1)));
  1848. _mm_store_ss(dstl + (15 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(0, 0, 0, 0)));
  1849. _mm_store_ss(dstl + (17 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(0, 0, 0, 0)));
  1850. _mm_store_ss(dstr + (47 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(3, 3, 3, 3)));
  1851. _mm_store_ss(dstr + (49 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(3, 3, 3, 3)));
  1852. _mm_store_ss(dstl + (47 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(2, 2, 2, 2)));
  1853. _mm_store_ss(dstl + (49 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(2, 2, 2, 2)));
  1854. #else
  1855. vst1q_lane_f32(dstr + (15 - i)*nch, a, 1);
  1856. vst1q_lane_f32(dstr + (17 + i)*nch, b, 1);
  1857. vst1q_lane_f32(dstl + (15 - i)*nch, a, 0);
  1858. vst1q_lane_f32(dstl + (17 + i)*nch, b, 0);
  1859. vst1q_lane_f32(dstr + (47 - i)*nch, a, 3);
  1860. vst1q_lane_f32(dstr + (49 + i)*nch, b, 3);
  1861. vst1q_lane_f32(dstl + (47 - i)*nch, a, 2);
  1862. vst1q_lane_f32(dstl + (49 + i)*nch, b, 2);
  1863. #endif
  1864. #endif /* DR_MP3_FLOAT_OUTPUT */
  1865. }
  1866. } else
  1867. #endif
  1868. #ifdef DR_MP3_ONLY_SIMD
  1869. {}
  1870. #else
  1871. for (i = 14; i >= 0; i--)
  1872. {
  1873. #define DRMP3_LOAD(k) float w0 = *w++; float w1 = *w++; float *vz = &zlin[4*i - k*64]; float *vy = &zlin[4*i - (15 - k)*64];
  1874. #define DRMP3_S0(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] = vz[j]*w1 + vy[j]*w0, a[j] = vz[j]*w0 - vy[j]*w1; }
  1875. #define DRMP3_S1(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] += vz[j]*w1 + vy[j]*w0, a[j] += vz[j]*w0 - vy[j]*w1; }
  1876. #define DRMP3_S2(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] += vz[j]*w1 + vy[j]*w0, a[j] += vy[j]*w1 - vz[j]*w0; }
  1877. float a[4], b[4];
  1878. zlin[4*i] = xl[18*(31 - i)];
  1879. zlin[4*i + 1] = xr[18*(31 - i)];
  1880. zlin[4*i + 2] = xl[1 + 18*(31 - i)];
  1881. zlin[4*i + 3] = xr[1 + 18*(31 - i)];
  1882. zlin[4*(i + 16)] = xl[1 + 18*(1 + i)];
  1883. zlin[4*(i + 16) + 1] = xr[1 + 18*(1 + i)];
  1884. zlin[4*(i - 16) + 2] = xl[18*(1 + i)];
  1885. zlin[4*(i - 16) + 3] = xr[18*(1 + i)];
  1886. DRMP3_S0(0) DRMP3_S2(1) DRMP3_S1(2) DRMP3_S2(3) DRMP3_S1(4) DRMP3_S2(5) DRMP3_S1(6) DRMP3_S2(7)
  1887. dstr[(15 - i)*nch] = drmp3d_scale_pcm(a[1]);
  1888. dstr[(17 + i)*nch] = drmp3d_scale_pcm(b[1]);
  1889. dstl[(15 - i)*nch] = drmp3d_scale_pcm(a[0]);
  1890. dstl[(17 + i)*nch] = drmp3d_scale_pcm(b[0]);
  1891. dstr[(47 - i)*nch] = drmp3d_scale_pcm(a[3]);
  1892. dstr[(49 + i)*nch] = drmp3d_scale_pcm(b[3]);
  1893. dstl[(47 - i)*nch] = drmp3d_scale_pcm(a[2]);
  1894. dstl[(49 + i)*nch] = drmp3d_scale_pcm(b[2]);
  1895. }
  1896. #endif
  1897. }
  1898. static void drmp3d_synth_granule(float *qmf_state, float *grbuf, int nbands, int nch, drmp3d_sample_t *pcm, float *lins)
  1899. {
  1900. int i;
  1901. for (i = 0; i < nch; i++)
  1902. {
  1903. drmp3d_DCT_II(grbuf + 576*i, nbands);
  1904. }
  1905. memcpy(lins, qmf_state, sizeof(float)*15*64);
  1906. for (i = 0; i < nbands; i += 2)
  1907. {
  1908. drmp3d_synth(grbuf + i, pcm + 32*nch*i, nch, lins + i*64);
  1909. }
  1910. #ifndef DR_MP3_NONSTANDARD_BUT_LOGICAL
  1911. if (nch == 1)
  1912. {
  1913. for (i = 0; i < 15*64; i += 2)
  1914. {
  1915. qmf_state[i] = lins[nbands*64 + i];
  1916. }
  1917. } else
  1918. #endif
  1919. {
  1920. memcpy(qmf_state, lins + nbands*64, sizeof(float)*15*64);
  1921. }
  1922. }
  1923. static int drmp3d_match_frame(const drmp3_uint8 *hdr, int mp3_bytes, int frame_bytes)
  1924. {
  1925. int i, nmatch;
  1926. for (i = 0, nmatch = 0; nmatch < DRMP3_MAX_FRAME_SYNC_MATCHES; nmatch++)
  1927. {
  1928. i += drmp3_hdr_frame_bytes(hdr + i, frame_bytes) + drmp3_hdr_padding(hdr + i);
  1929. if (i + DRMP3_HDR_SIZE > mp3_bytes)
  1930. return nmatch > 0;
  1931. if (!drmp3_hdr_compare(hdr, hdr + i))
  1932. return 0;
  1933. }
  1934. return 1;
  1935. }
  1936. static int drmp3d_find_frame(const drmp3_uint8 *mp3, int mp3_bytes, int *free_format_bytes, int *ptr_frame_bytes)
  1937. {
  1938. int i, k;
  1939. for (i = 0; i < mp3_bytes - DRMP3_HDR_SIZE; i++, mp3++)
  1940. {
  1941. if (drmp3_hdr_valid(mp3))
  1942. {
  1943. int frame_bytes = drmp3_hdr_frame_bytes(mp3, *free_format_bytes);
  1944. int frame_and_padding = frame_bytes + drmp3_hdr_padding(mp3);
  1945. for (k = DRMP3_HDR_SIZE; !frame_bytes && k < DRMP3_MAX_FREE_FORMAT_FRAME_SIZE && i + 2*k < mp3_bytes - DRMP3_HDR_SIZE; k++)
  1946. {
  1947. if (drmp3_hdr_compare(mp3, mp3 + k))
  1948. {
  1949. int fb = k - drmp3_hdr_padding(mp3);
  1950. int nextfb = fb + drmp3_hdr_padding(mp3 + k);
  1951. if (i + k + nextfb + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + k + nextfb))
  1952. continue;
  1953. frame_and_padding = k;
  1954. frame_bytes = fb;
  1955. *free_format_bytes = fb;
  1956. }
  1957. }
  1958. if ((frame_bytes && i + frame_and_padding <= mp3_bytes &&
  1959. drmp3d_match_frame(mp3, mp3_bytes - i, frame_bytes)) ||
  1960. (!i && frame_and_padding == mp3_bytes))
  1961. {
  1962. *ptr_frame_bytes = frame_and_padding;
  1963. return i;
  1964. }
  1965. *free_format_bytes = 0;
  1966. }
  1967. }
  1968. *ptr_frame_bytes = 0;
  1969. return mp3_bytes;
  1970. }
  1971. DRMP3_API void drmp3dec_init(drmp3dec *dec)
  1972. {
  1973. dec->header[0] = 0;
  1974. }
  1975. DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info)
  1976. {
  1977. int i = 0, igr, frame_size = 0, success = 1;
  1978. const drmp3_uint8 *hdr;
  1979. drmp3_bs bs_frame[1];
  1980. drmp3dec_scratch scratch;
  1981. if (mp3_bytes > 4 && dec->header[0] == 0xff && drmp3_hdr_compare(dec->header, mp3))
  1982. {
  1983. frame_size = drmp3_hdr_frame_bytes(mp3, dec->free_format_bytes) + drmp3_hdr_padding(mp3);
  1984. if (frame_size != mp3_bytes && (frame_size + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + frame_size)))
  1985. {
  1986. frame_size = 0;
  1987. }
  1988. }
  1989. if (!frame_size)
  1990. {
  1991. memset(dec, 0, sizeof(drmp3dec));
  1992. i = drmp3d_find_frame(mp3, mp3_bytes, &dec->free_format_bytes, &frame_size);
  1993. if (!frame_size || i + frame_size > mp3_bytes)
  1994. {
  1995. info->frame_bytes = i;
  1996. return 0;
  1997. }
  1998. }
  1999. hdr = mp3 + i;
  2000. memcpy(dec->header, hdr, DRMP3_HDR_SIZE);
  2001. info->frame_bytes = i + frame_size;
  2002. info->channels = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2;
  2003. info->hz = drmp3_hdr_sample_rate_hz(hdr);
  2004. info->layer = 4 - DRMP3_HDR_GET_LAYER(hdr);
  2005. info->bitrate_kbps = drmp3_hdr_bitrate_kbps(hdr);
  2006. drmp3_bs_init(bs_frame, hdr + DRMP3_HDR_SIZE, frame_size - DRMP3_HDR_SIZE);
  2007. if (DRMP3_HDR_IS_CRC(hdr))
  2008. {
  2009. drmp3_bs_get_bits(bs_frame, 16);
  2010. }
  2011. if (info->layer == 3)
  2012. {
  2013. int main_data_begin = drmp3_L3_read_side_info(bs_frame, scratch.gr_info, hdr);
  2014. if (main_data_begin < 0 || bs_frame->pos > bs_frame->limit)
  2015. {
  2016. drmp3dec_init(dec);
  2017. return 0;
  2018. }
  2019. success = drmp3_L3_restore_reservoir(dec, bs_frame, &scratch, main_data_begin);
  2020. if (success && pcm != NULL)
  2021. {
  2022. for (igr = 0; igr < (DRMP3_HDR_TEST_MPEG1(hdr) ? 2 : 1); igr++, pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*576*info->channels))
  2023. {
  2024. memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
  2025. drmp3_L3_decode(dec, &scratch, scratch.gr_info + igr*info->channels, info->channels);
  2026. drmp3d_synth_granule(dec->qmf_state, scratch.grbuf[0], 18, info->channels, (drmp3d_sample_t*)pcm, scratch.syn[0]);
  2027. }
  2028. }
  2029. drmp3_L3_save_reservoir(dec, &scratch);
  2030. } else
  2031. {
  2032. #ifdef DR_MP3_ONLY_MP3
  2033. return 0;
  2034. #else
  2035. drmp3_L12_scale_info sci[1];
  2036. if (pcm == NULL) {
  2037. return drmp3_hdr_frame_samples(hdr);
  2038. }
  2039. drmp3_L12_read_scale_info(hdr, bs_frame, sci);
  2040. memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
  2041. for (i = 0, igr = 0; igr < 3; igr++)
  2042. {
  2043. if (12 == (i += drmp3_L12_dequantize_granule(scratch.grbuf[0] + i, bs_frame, sci, info->layer | 1)))
  2044. {
  2045. i = 0;
  2046. drmp3_L12_apply_scf_384(sci, sci->scf + igr, scratch.grbuf[0]);
  2047. drmp3d_synth_granule(dec->qmf_state, scratch.grbuf[0], 12, info->channels, (drmp3d_sample_t*)pcm, scratch.syn[0]);
  2048. memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
  2049. pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*384*info->channels);
  2050. }
  2051. if (bs_frame->pos > bs_frame->limit)
  2052. {
  2053. drmp3dec_init(dec);
  2054. return 0;
  2055. }
  2056. }
  2057. #endif
  2058. }
  2059. return success*drmp3_hdr_frame_samples(dec->header);
  2060. }
  2061. DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num_samples)
  2062. {
  2063. size_t i = 0;
  2064. #if DRMP3_HAVE_SIMD
  2065. size_t aligned_count = num_samples & ~7;
  2066. for(; i < aligned_count; i+=8)
  2067. {
  2068. drmp3_f4 scale = DRMP3_VSET(32768.0f);
  2069. drmp3_f4 a = DRMP3_VMUL(DRMP3_VLD(&in[i ]), scale);
  2070. drmp3_f4 b = DRMP3_VMUL(DRMP3_VLD(&in[i+4]), scale);
  2071. #if DRMP3_HAVE_SSE
  2072. drmp3_f4 s16max = DRMP3_VSET(32767.0f);
  2073. drmp3_f4 s16min = DRMP3_VSET(-32768.0f);
  2074. __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, s16max), s16min)),
  2075. _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, s16max), s16min)));
  2076. out[i ] = (drmp3_int16)_mm_extract_epi16(pcm8, 0);
  2077. out[i+1] = (drmp3_int16)_mm_extract_epi16(pcm8, 1);
  2078. out[i+2] = (drmp3_int16)_mm_extract_epi16(pcm8, 2);
  2079. out[i+3] = (drmp3_int16)_mm_extract_epi16(pcm8, 3);
  2080. out[i+4] = (drmp3_int16)_mm_extract_epi16(pcm8, 4);
  2081. out[i+5] = (drmp3_int16)_mm_extract_epi16(pcm8, 5);
  2082. out[i+6] = (drmp3_int16)_mm_extract_epi16(pcm8, 6);
  2083. out[i+7] = (drmp3_int16)_mm_extract_epi16(pcm8, 7);
  2084. #else
  2085. int16x4_t pcma, pcmb;
  2086. a = DRMP3_VADD(a, DRMP3_VSET(0.5f));
  2087. b = DRMP3_VADD(b, DRMP3_VSET(0.5f));
  2088. pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0)))));
  2089. pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0)))));
  2090. vst1_lane_s16(out+i , pcma, 0);
  2091. vst1_lane_s16(out+i+1, pcma, 1);
  2092. vst1_lane_s16(out+i+2, pcma, 2);
  2093. vst1_lane_s16(out+i+3, pcma, 3);
  2094. vst1_lane_s16(out+i+4, pcmb, 0);
  2095. vst1_lane_s16(out+i+5, pcmb, 1);
  2096. vst1_lane_s16(out+i+6, pcmb, 2);
  2097. vst1_lane_s16(out+i+7, pcmb, 3);
  2098. #endif
  2099. }
  2100. #endif
  2101. for(; i < num_samples; i++)
  2102. {
  2103. float sample = in[i] * 32768.0f;
  2104. if (sample >= 32766.5)
  2105. out[i] = (drmp3_int16) 32767;
  2106. else if (sample <= -32767.5)
  2107. out[i] = (drmp3_int16)-32768;
  2108. else
  2109. {
  2110. short s = (drmp3_int16)(sample + .5f);
  2111. s -= (s < 0); /* away from zero, to be compliant */
  2112. out[i] = s;
  2113. }
  2114. }
  2115. }
  2116. /************************************************************************************************************************************************************
  2117. Main Public API
  2118. ************************************************************************************************************************************************************/
  2119. #include <math.h> /* For sin() and exp(). */
  2120. #if defined(SIZE_MAX)
  2121. #define DRMP3_SIZE_MAX SIZE_MAX
  2122. #else
  2123. #if defined(_WIN64) || defined(_LP64) || defined(__LP64__)
  2124. #define DRMP3_SIZE_MAX ((drmp3_uint64)0xFFFFFFFFFFFFFFFF)
  2125. #else
  2126. #define DRMP3_SIZE_MAX 0xFFFFFFFF
  2127. #endif
  2128. #endif
  2129. /* Options. */
  2130. #ifndef DRMP3_SEEK_LEADING_MP3_FRAMES
  2131. #define DRMP3_SEEK_LEADING_MP3_FRAMES 2
  2132. #endif
  2133. #define DRMP3_MIN_DATA_CHUNK_SIZE 16384
  2134. /* The size in bytes of each chunk of data to read from the MP3 stream. minimp3 recommends at least 16K, but in an attempt to reduce data movement I'm making this slightly larger. */
  2135. #ifndef DRMP3_DATA_CHUNK_SIZE
  2136. #define DRMP3_DATA_CHUNK_SIZE DRMP3_MIN_DATA_CHUNK_SIZE*4
  2137. #endif
  2138. /* Standard library stuff. */
  2139. #ifndef DRMP3_ASSERT
  2140. #include <assert.h>
  2141. #define DRMP3_ASSERT(expression) assert(expression)
  2142. #endif
  2143. #ifndef DRMP3_COPY_MEMORY
  2144. #define DRMP3_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
  2145. #endif
  2146. #ifndef DRMP3_ZERO_MEMORY
  2147. #define DRMP3_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
  2148. #endif
  2149. #define DRMP3_ZERO_OBJECT(p) DRMP3_ZERO_MEMORY((p), sizeof(*(p)))
  2150. #ifndef DRMP3_MALLOC
  2151. #define DRMP3_MALLOC(sz) malloc((sz))
  2152. #endif
  2153. #ifndef DRMP3_REALLOC
  2154. #define DRMP3_REALLOC(p, sz) realloc((p), (sz))
  2155. #endif
  2156. #ifndef DRMP3_FREE
  2157. #define DRMP3_FREE(p) free((p))
  2158. #endif
  2159. #define DRMP3_COUNTOF(x) (sizeof(x) / sizeof(x[0]))
  2160. #define DRMP3_CLAMP(x, lo, hi) (DRMP3_MAX(lo, DRMP3_MIN(x, hi)))
  2161. #ifndef DRMP3_PI_D
  2162. #define DRMP3_PI_D 3.14159265358979323846264
  2163. #endif
  2164. #define DRMP3_DEFAULT_RESAMPLER_LPF_ORDER 2
  2165. static DRMP3_INLINE float drmp3_mix_f32(float x, float y, float a)
  2166. {
  2167. return x*(1-a) + y*a;
  2168. }
  2169. static DRMP3_INLINE float drmp3_mix_f32_fast(float x, float y, float a)
  2170. {
  2171. float r0 = (y - x);
  2172. float r1 = r0*a;
  2173. return x + r1;
  2174. /*return x + (y - x)*a;*/
  2175. }
  2176. /*
  2177. Greatest common factor using Euclid's algorithm iteratively.
  2178. */
  2179. static DRMP3_INLINE drmp3_uint32 drmp3_gcf_u32(drmp3_uint32 a, drmp3_uint32 b)
  2180. {
  2181. for (;;) {
  2182. if (b == 0) {
  2183. break;
  2184. } else {
  2185. drmp3_uint32 t = a;
  2186. a = b;
  2187. b = t % a;
  2188. }
  2189. }
  2190. return a;
  2191. }
  2192. static DRMP3_INLINE double drmp3_sin(double x)
  2193. {
  2194. /* TODO: Implement custom sin(x). */
  2195. return sin(x);
  2196. }
  2197. static DRMP3_INLINE double drmp3_exp(double x)
  2198. {
  2199. /* TODO: Implement custom exp(x). */
  2200. return exp(x);
  2201. }
  2202. static DRMP3_INLINE double drmp3_cos(double x)
  2203. {
  2204. return drmp3_sin((DRMP3_PI_D*0.5) - x);
  2205. }
  2206. static void* drmp3__malloc_default(size_t sz, void* pUserData)
  2207. {
  2208. (void)pUserData;
  2209. return DRMP3_MALLOC(sz);
  2210. }
  2211. static void* drmp3__realloc_default(void* p, size_t sz, void* pUserData)
  2212. {
  2213. (void)pUserData;
  2214. return DRMP3_REALLOC(p, sz);
  2215. }
  2216. static void drmp3__free_default(void* p, void* pUserData)
  2217. {
  2218. (void)pUserData;
  2219. DRMP3_FREE(p);
  2220. }
  2221. static void* drmp3__malloc_from_callbacks(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks)
  2222. {
  2223. if (pAllocationCallbacks == NULL) {
  2224. return NULL;
  2225. }
  2226. if (pAllocationCallbacks->onMalloc != NULL) {
  2227. return pAllocationCallbacks->onMalloc(sz, pAllocationCallbacks->pUserData);
  2228. }
  2229. /* Try using realloc(). */
  2230. if (pAllocationCallbacks->onRealloc != NULL) {
  2231. return pAllocationCallbacks->onRealloc(NULL, sz, pAllocationCallbacks->pUserData);
  2232. }
  2233. return NULL;
  2234. }
  2235. static void* drmp3__realloc_from_callbacks(void* p, size_t szNew, size_t szOld, const drmp3_allocation_callbacks* pAllocationCallbacks)
  2236. {
  2237. if (pAllocationCallbacks == NULL) {
  2238. return NULL;
  2239. }
  2240. if (pAllocationCallbacks->onRealloc != NULL) {
  2241. return pAllocationCallbacks->onRealloc(p, szNew, pAllocationCallbacks->pUserData);
  2242. }
  2243. /* Try emulating realloc() in terms of malloc()/free(). */
  2244. if (pAllocationCallbacks->onMalloc != NULL && pAllocationCallbacks->onFree != NULL) {
  2245. void* p2;
  2246. p2 = pAllocationCallbacks->onMalloc(szNew, pAllocationCallbacks->pUserData);
  2247. if (p2 == NULL) {
  2248. return NULL;
  2249. }
  2250. if (p != NULL) {
  2251. DRMP3_COPY_MEMORY(p2, p, szOld);
  2252. pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData);
  2253. }
  2254. return p2;
  2255. }
  2256. return NULL;
  2257. }
  2258. static void drmp3__free_from_callbacks(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks)
  2259. {
  2260. if (p == NULL || pAllocationCallbacks == NULL) {
  2261. return;
  2262. }
  2263. if (pAllocationCallbacks->onFree != NULL) {
  2264. pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData);
  2265. }
  2266. }
  2267. static drmp3_allocation_callbacks drmp3_copy_allocation_callbacks_or_defaults(const drmp3_allocation_callbacks* pAllocationCallbacks)
  2268. {
  2269. if (pAllocationCallbacks != NULL) {
  2270. /* Copy. */
  2271. return *pAllocationCallbacks;
  2272. } else {
  2273. /* Defaults. */
  2274. drmp3_allocation_callbacks allocationCallbacks;
  2275. allocationCallbacks.pUserData = NULL;
  2276. allocationCallbacks.onMalloc = drmp3__malloc_default;
  2277. allocationCallbacks.onRealloc = drmp3__realloc_default;
  2278. allocationCallbacks.onFree = drmp3__free_default;
  2279. return allocationCallbacks;
  2280. }
  2281. }
  2282. static size_t drmp3__on_read(drmp3* pMP3, void* pBufferOut, size_t bytesToRead)
  2283. {
  2284. size_t bytesRead = pMP3->onRead(pMP3->pUserData, pBufferOut, bytesToRead);
  2285. pMP3->streamCursor += bytesRead;
  2286. return bytesRead;
  2287. }
  2288. static drmp3_bool32 drmp3__on_seek(drmp3* pMP3, int offset, drmp3_seek_origin origin)
  2289. {
  2290. DRMP3_ASSERT(offset >= 0);
  2291. if (!pMP3->onSeek(pMP3->pUserData, offset, origin)) {
  2292. return DRMP3_FALSE;
  2293. }
  2294. if (origin == drmp3_seek_origin_start) {
  2295. pMP3->streamCursor = (drmp3_uint64)offset;
  2296. } else {
  2297. pMP3->streamCursor += offset;
  2298. }
  2299. return DRMP3_TRUE;
  2300. }
  2301. static drmp3_bool32 drmp3__on_seek_64(drmp3* pMP3, drmp3_uint64 offset, drmp3_seek_origin origin)
  2302. {
  2303. if (offset <= 0x7FFFFFFF) {
  2304. return drmp3__on_seek(pMP3, (int)offset, origin);
  2305. }
  2306. /* Getting here "offset" is too large for a 32-bit integer. We just keep seeking forward until we hit the offset. */
  2307. if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, drmp3_seek_origin_start)) {
  2308. return DRMP3_FALSE;
  2309. }
  2310. offset -= 0x7FFFFFFF;
  2311. while (offset > 0) {
  2312. if (offset <= 0x7FFFFFFF) {
  2313. if (!drmp3__on_seek(pMP3, (int)offset, drmp3_seek_origin_current)) {
  2314. return DRMP3_FALSE;
  2315. }
  2316. offset = 0;
  2317. } else {
  2318. if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, drmp3_seek_origin_current)) {
  2319. return DRMP3_FALSE;
  2320. }
  2321. offset -= 0x7FFFFFFF;
  2322. }
  2323. }
  2324. return DRMP3_TRUE;
  2325. }
  2326. static drmp3_uint32 drmp3_decode_next_frame_ex__callbacks(drmp3* pMP3, drmp3d_sample_t* pPCMFrames)
  2327. {
  2328. drmp3_uint32 pcmFramesRead = 0;
  2329. DRMP3_ASSERT(pMP3 != NULL);
  2330. DRMP3_ASSERT(pMP3->onRead != NULL);
  2331. if (pMP3->atEnd) {
  2332. return 0;
  2333. }
  2334. for (;;) {
  2335. drmp3dec_frame_info info;
  2336. /* minimp3 recommends doing data submission in chunks of at least 16K. If we don't have at least 16K bytes available, get more. */
  2337. if (pMP3->dataSize < DRMP3_MIN_DATA_CHUNK_SIZE) {
  2338. size_t bytesRead;
  2339. /* First we need to move the data down. */
  2340. if (pMP3->pData != NULL) {
  2341. memmove(pMP3->pData, pMP3->pData + pMP3->dataConsumed, pMP3->dataSize);
  2342. }
  2343. pMP3->dataConsumed = 0;
  2344. if (pMP3->dataCapacity < DRMP3_DATA_CHUNK_SIZE) {
  2345. drmp3_uint8* pNewData;
  2346. size_t newDataCap;
  2347. newDataCap = DRMP3_DATA_CHUNK_SIZE;
  2348. pNewData = (drmp3_uint8*)drmp3__realloc_from_callbacks(pMP3->pData, newDataCap, pMP3->dataCapacity, &pMP3->allocationCallbacks);
  2349. if (pNewData == NULL) {
  2350. return 0; /* Out of memory. */
  2351. }
  2352. pMP3->pData = pNewData;
  2353. pMP3->dataCapacity = newDataCap;
  2354. }
  2355. bytesRead = drmp3__on_read(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
  2356. if (bytesRead == 0) {
  2357. if (pMP3->dataSize == 0) {
  2358. pMP3->atEnd = DRMP3_TRUE;
  2359. return 0; /* No data. */
  2360. }
  2361. }
  2362. pMP3->dataSize += bytesRead;
  2363. }
  2364. if (pMP3->dataSize > INT_MAX) {
  2365. pMP3->atEnd = DRMP3_TRUE;
  2366. return 0; /* File too big. */
  2367. }
  2368. pcmFramesRead = drmp3dec_decode_frame(&pMP3->decoder, pMP3->pData + pMP3->dataConsumed, (int)pMP3->dataSize, pPCMFrames, &info); /* <-- Safe size_t -> int conversion thanks to the check above. */
  2369. /* Consume the data. */
  2370. if (info.frame_bytes > 0) {
  2371. pMP3->dataConsumed += (size_t)info.frame_bytes;
  2372. pMP3->dataSize -= (size_t)info.frame_bytes;
  2373. }
  2374. /* pcmFramesRead will be equal to 0 if decoding failed. If it is zero and info.frame_bytes > 0 then we have successfully decoded the frame. */
  2375. if (pcmFramesRead > 0) {
  2376. pcmFramesRead = drmp3_hdr_frame_samples(pMP3->decoder.header);
  2377. pMP3->pcmFramesConsumedInMP3Frame = 0;
  2378. pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead;
  2379. pMP3->mp3FrameChannels = info.channels;
  2380. pMP3->mp3FrameSampleRate = info.hz;
  2381. break;
  2382. } else if (info.frame_bytes == 0) {
  2383. /* Need more data. minimp3 recommends doing data submission in 16K chunks. */
  2384. size_t bytesRead;
  2385. /* First we need to move the data down. */
  2386. memmove(pMP3->pData, pMP3->pData + pMP3->dataConsumed, pMP3->dataSize);
  2387. pMP3->dataConsumed = 0;
  2388. if (pMP3->dataCapacity == pMP3->dataSize) {
  2389. /* No room. Expand. */
  2390. drmp3_uint8* pNewData;
  2391. size_t newDataCap;
  2392. newDataCap = pMP3->dataCapacity + DRMP3_DATA_CHUNK_SIZE;
  2393. pNewData = (drmp3_uint8*)drmp3__realloc_from_callbacks(pMP3->pData, newDataCap, pMP3->dataCapacity, &pMP3->allocationCallbacks);
  2394. if (pNewData == NULL) {
  2395. return 0; /* Out of memory. */
  2396. }
  2397. pMP3->pData = pNewData;
  2398. pMP3->dataCapacity = newDataCap;
  2399. }
  2400. /* Fill in a chunk. */
  2401. bytesRead = drmp3__on_read(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
  2402. if (bytesRead == 0) {
  2403. pMP3->atEnd = DRMP3_TRUE;
  2404. return 0; /* Error reading more data. */
  2405. }
  2406. pMP3->dataSize += bytesRead;
  2407. }
  2408. };
  2409. return pcmFramesRead;
  2410. }
  2411. static drmp3_uint32 drmp3_decode_next_frame_ex__memory(drmp3* pMP3, drmp3d_sample_t* pPCMFrames)
  2412. {
  2413. drmp3_uint32 pcmFramesRead = 0;
  2414. drmp3dec_frame_info info;
  2415. DRMP3_ASSERT(pMP3 != NULL);
  2416. DRMP3_ASSERT(pMP3->memory.pData != NULL);
  2417. if (pMP3->atEnd) {
  2418. return 0;
  2419. }
  2420. pcmFramesRead = drmp3dec_decode_frame(&pMP3->decoder, pMP3->memory.pData + pMP3->memory.currentReadPos, (int)(pMP3->memory.dataSize - pMP3->memory.currentReadPos), pPCMFrames, &info);
  2421. if (pcmFramesRead > 0) {
  2422. pMP3->pcmFramesConsumedInMP3Frame = 0;
  2423. pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead;
  2424. pMP3->mp3FrameChannels = info.channels;
  2425. pMP3->mp3FrameSampleRate = info.hz;
  2426. }
  2427. /* Consume the data. */
  2428. pMP3->memory.currentReadPos += (size_t)info.frame_bytes;
  2429. return pcmFramesRead;
  2430. }
  2431. static drmp3_uint32 drmp3_decode_next_frame_ex(drmp3* pMP3, drmp3d_sample_t* pPCMFrames)
  2432. {
  2433. if (pMP3->memory.pData != NULL && pMP3->memory.dataSize > 0) {
  2434. return drmp3_decode_next_frame_ex__memory(pMP3, pPCMFrames);
  2435. } else {
  2436. return drmp3_decode_next_frame_ex__callbacks(pMP3, pPCMFrames);
  2437. }
  2438. }
  2439. static drmp3_uint32 drmp3_decode_next_frame(drmp3* pMP3)
  2440. {
  2441. DRMP3_ASSERT(pMP3 != NULL);
  2442. return drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames);
  2443. }
  2444. #if 0
  2445. static drmp3_uint32 drmp3_seek_next_frame(drmp3* pMP3)
  2446. {
  2447. drmp3_uint32 pcmFrameCount;
  2448. DRMP3_ASSERT(pMP3 != NULL);
  2449. pcmFrameCount = drmp3_decode_next_frame_ex(pMP3, NULL);
  2450. if (pcmFrameCount == 0) {
  2451. return 0;
  2452. }
  2453. /* We have essentially just skipped past the frame, so just set the remaining samples to 0. */
  2454. pMP3->currentPCMFrame += pcmFrameCount;
  2455. pMP3->pcmFramesConsumedInMP3Frame = pcmFrameCount;
  2456. pMP3->pcmFramesRemainingInMP3Frame = 0;
  2457. return pcmFrameCount;
  2458. }
  2459. #endif
  2460. static drmp3_bool32 drmp3_init_internal(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks)
  2461. {
  2462. DRMP3_ASSERT(pMP3 != NULL);
  2463. DRMP3_ASSERT(onRead != NULL);
  2464. /* This function assumes the output object has already been reset to 0. Do not do that here, otherwise things will break. */
  2465. drmp3dec_init(&pMP3->decoder);
  2466. pMP3->onRead = onRead;
  2467. pMP3->onSeek = onSeek;
  2468. pMP3->pUserData = pUserData;
  2469. pMP3->allocationCallbacks = drmp3_copy_allocation_callbacks_or_defaults(pAllocationCallbacks);
  2470. if (pMP3->allocationCallbacks.onFree == NULL || (pMP3->allocationCallbacks.onMalloc == NULL && pMP3->allocationCallbacks.onRealloc == NULL)) {
  2471. return DRMP3_FALSE; /* Invalid allocation callbacks. */
  2472. }
  2473. /* Decode the first frame to confirm that it is indeed a valid MP3 stream. */
  2474. if (!drmp3_decode_next_frame(pMP3)) {
  2475. drmp3_uninit(pMP3);
  2476. return DRMP3_FALSE; /* Not a valid MP3 stream. */
  2477. }
  2478. pMP3->channels = pMP3->mp3FrameChannels;
  2479. pMP3->sampleRate = pMP3->mp3FrameSampleRate;
  2480. return DRMP3_TRUE;
  2481. }
  2482. DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks)
  2483. {
  2484. if (pMP3 == NULL || onRead == NULL) {
  2485. return DRMP3_FALSE;
  2486. }
  2487. DRMP3_ZERO_OBJECT(pMP3);
  2488. return drmp3_init_internal(pMP3, onRead, onSeek, pUserData, pAllocationCallbacks);
  2489. }
  2490. static size_t drmp3__on_read_memory(void* pUserData, void* pBufferOut, size_t bytesToRead)
  2491. {
  2492. drmp3* pMP3 = (drmp3*)pUserData;
  2493. size_t bytesRemaining;
  2494. DRMP3_ASSERT(pMP3 != NULL);
  2495. DRMP3_ASSERT(pMP3->memory.dataSize >= pMP3->memory.currentReadPos);
  2496. bytesRemaining = pMP3->memory.dataSize - pMP3->memory.currentReadPos;
  2497. if (bytesToRead > bytesRemaining) {
  2498. bytesToRead = bytesRemaining;
  2499. }
  2500. if (bytesToRead > 0) {
  2501. DRMP3_COPY_MEMORY(pBufferOut, pMP3->memory.pData + pMP3->memory.currentReadPos, bytesToRead);
  2502. pMP3->memory.currentReadPos += bytesToRead;
  2503. }
  2504. return bytesToRead;
  2505. }
  2506. static drmp3_bool32 drmp3__on_seek_memory(void* pUserData, int byteOffset, drmp3_seek_origin origin)
  2507. {
  2508. drmp3* pMP3 = (drmp3*)pUserData;
  2509. DRMP3_ASSERT(pMP3 != NULL);
  2510. if (origin == drmp3_seek_origin_current) {
  2511. if (byteOffset > 0) {
  2512. if (pMP3->memory.currentReadPos + byteOffset > pMP3->memory.dataSize) {
  2513. byteOffset = (int)(pMP3->memory.dataSize - pMP3->memory.currentReadPos); /* Trying to seek too far forward. */
  2514. }
  2515. } else {
  2516. if (pMP3->memory.currentReadPos < (size_t)-byteOffset) {
  2517. byteOffset = -(int)pMP3->memory.currentReadPos; /* Trying to seek too far backwards. */
  2518. }
  2519. }
  2520. /* This will never underflow thanks to the clamps above. */
  2521. pMP3->memory.currentReadPos += byteOffset;
  2522. } else {
  2523. if ((drmp3_uint32)byteOffset <= pMP3->memory.dataSize) {
  2524. pMP3->memory.currentReadPos = byteOffset;
  2525. } else {
  2526. pMP3->memory.currentReadPos = pMP3->memory.dataSize; /* Trying to seek too far forward. */
  2527. }
  2528. }
  2529. return DRMP3_TRUE;
  2530. }
  2531. DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_allocation_callbacks* pAllocationCallbacks)
  2532. {
  2533. if (pMP3 == NULL) {
  2534. return DRMP3_FALSE;
  2535. }
  2536. DRMP3_ZERO_OBJECT(pMP3);
  2537. if (pData == NULL || dataSize == 0) {
  2538. return DRMP3_FALSE;
  2539. }
  2540. pMP3->memory.pData = (const drmp3_uint8*)pData;
  2541. pMP3->memory.dataSize = dataSize;
  2542. pMP3->memory.currentReadPos = 0;
  2543. return drmp3_init_internal(pMP3, drmp3__on_read_memory, drmp3__on_seek_memory, pMP3, pAllocationCallbacks);
  2544. }
  2545. #ifndef DR_MP3_NO_STDIO
  2546. #include <stdio.h>
  2547. #include <wchar.h> /* For wcslen(), wcsrtombs() */
  2548. /* drmp3_result_from_errno() is only used inside DR_MP3_NO_STDIO for now. Move this out if it's ever used elsewhere. */
  2549. #include <errno.h>
  2550. static drmp3_result drmp3_result_from_errno(int e)
  2551. {
  2552. switch (e)
  2553. {
  2554. case 0: return DRMP3_SUCCESS;
  2555. #ifdef EPERM
  2556. case EPERM: return DRMP3_INVALID_OPERATION;
  2557. #endif
  2558. #ifdef ENOENT
  2559. case ENOENT: return DRMP3_DOES_NOT_EXIST;
  2560. #endif
  2561. #ifdef ESRCH
  2562. case ESRCH: return DRMP3_DOES_NOT_EXIST;
  2563. #endif
  2564. #ifdef EINTR
  2565. case EINTR: return DRMP3_INTERRUPT;
  2566. #endif
  2567. #ifdef EIO
  2568. case EIO: return DRMP3_IO_ERROR;
  2569. #endif
  2570. #ifdef ENXIO
  2571. case ENXIO: return DRMP3_DOES_NOT_EXIST;
  2572. #endif
  2573. #ifdef E2BIG
  2574. case E2BIG: return DRMP3_INVALID_ARGS;
  2575. #endif
  2576. #ifdef ENOEXEC
  2577. case ENOEXEC: return DRMP3_INVALID_FILE;
  2578. #endif
  2579. #ifdef EBADF
  2580. case EBADF: return DRMP3_INVALID_FILE;
  2581. #endif
  2582. #ifdef ECHILD
  2583. case ECHILD: return DRMP3_ERROR;
  2584. #endif
  2585. #ifdef EAGAIN
  2586. case EAGAIN: return DRMP3_UNAVAILABLE;
  2587. #endif
  2588. #ifdef ENOMEM
  2589. case ENOMEM: return DRMP3_OUT_OF_MEMORY;
  2590. #endif
  2591. #ifdef EACCES
  2592. case EACCES: return DRMP3_ACCESS_DENIED;
  2593. #endif
  2594. #ifdef EFAULT
  2595. case EFAULT: return DRMP3_BAD_ADDRESS;
  2596. #endif
  2597. #ifdef ENOTBLK
  2598. case ENOTBLK: return DRMP3_ERROR;
  2599. #endif
  2600. #ifdef EBUSY
  2601. case EBUSY: return DRMP3_BUSY;
  2602. #endif
  2603. #ifdef EEXIST
  2604. case EEXIST: return DRMP3_ALREADY_EXISTS;
  2605. #endif
  2606. #ifdef EXDEV
  2607. case EXDEV: return DRMP3_ERROR;
  2608. #endif
  2609. #ifdef ENODEV
  2610. case ENODEV: return DRMP3_DOES_NOT_EXIST;
  2611. #endif
  2612. #ifdef ENOTDIR
  2613. case ENOTDIR: return DRMP3_NOT_DIRECTORY;
  2614. #endif
  2615. #ifdef EISDIR
  2616. case EISDIR: return DRMP3_IS_DIRECTORY;
  2617. #endif
  2618. #ifdef EINVAL
  2619. case EINVAL: return DRMP3_INVALID_ARGS;
  2620. #endif
  2621. #ifdef ENFILE
  2622. case ENFILE: return DRMP3_TOO_MANY_OPEN_FILES;
  2623. #endif
  2624. #ifdef EMFILE
  2625. case EMFILE: return DRMP3_TOO_MANY_OPEN_FILES;
  2626. #endif
  2627. #ifdef ENOTTY
  2628. case ENOTTY: return DRMP3_INVALID_OPERATION;
  2629. #endif
  2630. #ifdef ETXTBSY
  2631. case ETXTBSY: return DRMP3_BUSY;
  2632. #endif
  2633. #ifdef EFBIG
  2634. case EFBIG: return DRMP3_TOO_BIG;
  2635. #endif
  2636. #ifdef ENOSPC
  2637. case ENOSPC: return DRMP3_NO_SPACE;
  2638. #endif
  2639. #ifdef ESPIPE
  2640. case ESPIPE: return DRMP3_BAD_SEEK;
  2641. #endif
  2642. #ifdef EROFS
  2643. case EROFS: return DRMP3_ACCESS_DENIED;
  2644. #endif
  2645. #ifdef EMLINK
  2646. case EMLINK: return DRMP3_TOO_MANY_LINKS;
  2647. #endif
  2648. #ifdef EPIPE
  2649. case EPIPE: return DRMP3_BAD_PIPE;
  2650. #endif
  2651. #ifdef EDOM
  2652. case EDOM: return DRMP3_OUT_OF_RANGE;
  2653. #endif
  2654. #ifdef ERANGE
  2655. case ERANGE: return DRMP3_OUT_OF_RANGE;
  2656. #endif
  2657. #ifdef EDEADLK
  2658. case EDEADLK: return DRMP3_DEADLOCK;
  2659. #endif
  2660. #ifdef ENAMETOOLONG
  2661. case ENAMETOOLONG: return DRMP3_PATH_TOO_LONG;
  2662. #endif
  2663. #ifdef ENOLCK
  2664. case ENOLCK: return DRMP3_ERROR;
  2665. #endif
  2666. #ifdef ENOSYS
  2667. case ENOSYS: return DRMP3_NOT_IMPLEMENTED;
  2668. #endif
  2669. #ifdef ENOTEMPTY
  2670. case ENOTEMPTY: return DRMP3_DIRECTORY_NOT_EMPTY;
  2671. #endif
  2672. #ifdef ELOOP
  2673. case ELOOP: return DRMP3_TOO_MANY_LINKS;
  2674. #endif
  2675. #ifdef ENOMSG
  2676. case ENOMSG: return DRMP3_NO_MESSAGE;
  2677. #endif
  2678. #ifdef EIDRM
  2679. case EIDRM: return DRMP3_ERROR;
  2680. #endif
  2681. #ifdef ECHRNG
  2682. case ECHRNG: return DRMP3_ERROR;
  2683. #endif
  2684. #ifdef EL2NSYNC
  2685. case EL2NSYNC: return DRMP3_ERROR;
  2686. #endif
  2687. #ifdef EL3HLT
  2688. case EL3HLT: return DRMP3_ERROR;
  2689. #endif
  2690. #ifdef EL3RST
  2691. case EL3RST: return DRMP3_ERROR;
  2692. #endif
  2693. #ifdef ELNRNG
  2694. case ELNRNG: return DRMP3_OUT_OF_RANGE;
  2695. #endif
  2696. #ifdef EUNATCH
  2697. case EUNATCH: return DRMP3_ERROR;
  2698. #endif
  2699. #ifdef ENOCSI
  2700. case ENOCSI: return DRMP3_ERROR;
  2701. #endif
  2702. #ifdef EL2HLT
  2703. case EL2HLT: return DRMP3_ERROR;
  2704. #endif
  2705. #ifdef EBADE
  2706. case EBADE: return DRMP3_ERROR;
  2707. #endif
  2708. #ifdef EBADR
  2709. case EBADR: return DRMP3_ERROR;
  2710. #endif
  2711. #ifdef EXFULL
  2712. case EXFULL: return DRMP3_ERROR;
  2713. #endif
  2714. #ifdef ENOANO
  2715. case ENOANO: return DRMP3_ERROR;
  2716. #endif
  2717. #ifdef EBADRQC
  2718. case EBADRQC: return DRMP3_ERROR;
  2719. #endif
  2720. #ifdef EBADSLT
  2721. case EBADSLT: return DRMP3_ERROR;
  2722. #endif
  2723. #ifdef EBFONT
  2724. case EBFONT: return DRMP3_INVALID_FILE;
  2725. #endif
  2726. #ifdef ENOSTR
  2727. case ENOSTR: return DRMP3_ERROR;
  2728. #endif
  2729. #ifdef ENODATA
  2730. case ENODATA: return DRMP3_NO_DATA_AVAILABLE;
  2731. #endif
  2732. #ifdef ETIME
  2733. case ETIME: return DRMP3_TIMEOUT;
  2734. #endif
  2735. #ifdef ENOSR
  2736. case ENOSR: return DRMP3_NO_DATA_AVAILABLE;
  2737. #endif
  2738. #ifdef ENONET
  2739. case ENONET: return DRMP3_NO_NETWORK;
  2740. #endif
  2741. #ifdef ENOPKG
  2742. case ENOPKG: return DRMP3_ERROR;
  2743. #endif
  2744. #ifdef EREMOTE
  2745. case EREMOTE: return DRMP3_ERROR;
  2746. #endif
  2747. #ifdef ENOLINK
  2748. case ENOLINK: return DRMP3_ERROR;
  2749. #endif
  2750. #ifdef EADV
  2751. case EADV: return DRMP3_ERROR;
  2752. #endif
  2753. #ifdef ESRMNT
  2754. case ESRMNT: return DRMP3_ERROR;
  2755. #endif
  2756. #ifdef ECOMM
  2757. case ECOMM: return DRMP3_ERROR;
  2758. #endif
  2759. #ifdef EPROTO
  2760. case EPROTO: return DRMP3_ERROR;
  2761. #endif
  2762. #ifdef EMULTIHOP
  2763. case EMULTIHOP: return DRMP3_ERROR;
  2764. #endif
  2765. #ifdef EDOTDOT
  2766. case EDOTDOT: return DRMP3_ERROR;
  2767. #endif
  2768. #ifdef EBADMSG
  2769. case EBADMSG: return DRMP3_BAD_MESSAGE;
  2770. #endif
  2771. #ifdef EOVERFLOW
  2772. case EOVERFLOW: return DRMP3_TOO_BIG;
  2773. #endif
  2774. #ifdef ENOTUNIQ
  2775. case ENOTUNIQ: return DRMP3_NOT_UNIQUE;
  2776. #endif
  2777. #ifdef EBADFD
  2778. case EBADFD: return DRMP3_ERROR;
  2779. #endif
  2780. #ifdef EREMCHG
  2781. case EREMCHG: return DRMP3_ERROR;
  2782. #endif
  2783. #ifdef ELIBACC
  2784. case ELIBACC: return DRMP3_ACCESS_DENIED;
  2785. #endif
  2786. #ifdef ELIBBAD
  2787. case ELIBBAD: return DRMP3_INVALID_FILE;
  2788. #endif
  2789. #ifdef ELIBSCN
  2790. case ELIBSCN: return DRMP3_INVALID_FILE;
  2791. #endif
  2792. #ifdef ELIBMAX
  2793. case ELIBMAX: return DRMP3_ERROR;
  2794. #endif
  2795. #ifdef ELIBEXEC
  2796. case ELIBEXEC: return DRMP3_ERROR;
  2797. #endif
  2798. #ifdef EILSEQ
  2799. case EILSEQ: return DRMP3_INVALID_DATA;
  2800. #endif
  2801. #ifdef ERESTART
  2802. case ERESTART: return DRMP3_ERROR;
  2803. #endif
  2804. #ifdef ESTRPIPE
  2805. case ESTRPIPE: return DRMP3_ERROR;
  2806. #endif
  2807. #ifdef EUSERS
  2808. case EUSERS: return DRMP3_ERROR;
  2809. #endif
  2810. #ifdef ENOTSOCK
  2811. case ENOTSOCK: return DRMP3_NOT_SOCKET;
  2812. #endif
  2813. #ifdef EDESTADDRREQ
  2814. case EDESTADDRREQ: return DRMP3_NO_ADDRESS;
  2815. #endif
  2816. #ifdef EMSGSIZE
  2817. case EMSGSIZE: return DRMP3_TOO_BIG;
  2818. #endif
  2819. #ifdef EPROTOTYPE
  2820. case EPROTOTYPE: return DRMP3_BAD_PROTOCOL;
  2821. #endif
  2822. #ifdef ENOPROTOOPT
  2823. case ENOPROTOOPT: return DRMP3_PROTOCOL_UNAVAILABLE;
  2824. #endif
  2825. #ifdef EPROTONOSUPPORT
  2826. case EPROTONOSUPPORT: return DRMP3_PROTOCOL_NOT_SUPPORTED;
  2827. #endif
  2828. #ifdef ESOCKTNOSUPPORT
  2829. case ESOCKTNOSUPPORT: return DRMP3_SOCKET_NOT_SUPPORTED;
  2830. #endif
  2831. #ifdef EOPNOTSUPP
  2832. case EOPNOTSUPP: return DRMP3_INVALID_OPERATION;
  2833. #endif
  2834. #ifdef EPFNOSUPPORT
  2835. case EPFNOSUPPORT: return DRMP3_PROTOCOL_FAMILY_NOT_SUPPORTED;
  2836. #endif
  2837. #ifdef EAFNOSUPPORT
  2838. case EAFNOSUPPORT: return DRMP3_ADDRESS_FAMILY_NOT_SUPPORTED;
  2839. #endif
  2840. #ifdef EADDRINUSE
  2841. case EADDRINUSE: return DRMP3_ALREADY_IN_USE;
  2842. #endif
  2843. #ifdef EADDRNOTAVAIL
  2844. case EADDRNOTAVAIL: return DRMP3_ERROR;
  2845. #endif
  2846. #ifdef ENETDOWN
  2847. case ENETDOWN: return DRMP3_NO_NETWORK;
  2848. #endif
  2849. #ifdef ENETUNREACH
  2850. case ENETUNREACH: return DRMP3_NO_NETWORK;
  2851. #endif
  2852. #ifdef ENETRESET
  2853. case ENETRESET: return DRMP3_NO_NETWORK;
  2854. #endif
  2855. #ifdef ECONNABORTED
  2856. case ECONNABORTED: return DRMP3_NO_NETWORK;
  2857. #endif
  2858. #ifdef ECONNRESET
  2859. case ECONNRESET: return DRMP3_CONNECTION_RESET;
  2860. #endif
  2861. #ifdef ENOBUFS
  2862. case ENOBUFS: return DRMP3_NO_SPACE;
  2863. #endif
  2864. #ifdef EISCONN
  2865. case EISCONN: return DRMP3_ALREADY_CONNECTED;
  2866. #endif
  2867. #ifdef ENOTCONN
  2868. case ENOTCONN: return DRMP3_NOT_CONNECTED;
  2869. #endif
  2870. #ifdef ESHUTDOWN
  2871. case ESHUTDOWN: return DRMP3_ERROR;
  2872. #endif
  2873. #ifdef ETOOMANYREFS
  2874. case ETOOMANYREFS: return DRMP3_ERROR;
  2875. #endif
  2876. #ifdef ETIMEDOUT
  2877. case ETIMEDOUT: return DRMP3_TIMEOUT;
  2878. #endif
  2879. #ifdef ECONNREFUSED
  2880. case ECONNREFUSED: return DRMP3_CONNECTION_REFUSED;
  2881. #endif
  2882. #ifdef EHOSTDOWN
  2883. case EHOSTDOWN: return DRMP3_NO_HOST;
  2884. #endif
  2885. #ifdef EHOSTUNREACH
  2886. case EHOSTUNREACH: return DRMP3_NO_HOST;
  2887. #endif
  2888. #ifdef EALREADY
  2889. case EALREADY: return DRMP3_IN_PROGRESS;
  2890. #endif
  2891. #ifdef EINPROGRESS
  2892. case EINPROGRESS: return DRMP3_IN_PROGRESS;
  2893. #endif
  2894. #ifdef ESTALE
  2895. case ESTALE: return DRMP3_INVALID_FILE;
  2896. #endif
  2897. #ifdef EUCLEAN
  2898. case EUCLEAN: return DRMP3_ERROR;
  2899. #endif
  2900. #ifdef ENOTNAM
  2901. case ENOTNAM: return DRMP3_ERROR;
  2902. #endif
  2903. #ifdef ENAVAIL
  2904. case ENAVAIL: return DRMP3_ERROR;
  2905. #endif
  2906. #ifdef EISNAM
  2907. case EISNAM: return DRMP3_ERROR;
  2908. #endif
  2909. #ifdef EREMOTEIO
  2910. case EREMOTEIO: return DRMP3_IO_ERROR;
  2911. #endif
  2912. #ifdef EDQUOT
  2913. case EDQUOT: return DRMP3_NO_SPACE;
  2914. #endif
  2915. #ifdef ENOMEDIUM
  2916. case ENOMEDIUM: return DRMP3_DOES_NOT_EXIST;
  2917. #endif
  2918. #ifdef EMEDIUMTYPE
  2919. case EMEDIUMTYPE: return DRMP3_ERROR;
  2920. #endif
  2921. #ifdef ECANCELED
  2922. case ECANCELED: return DRMP3_CANCELLED;
  2923. #endif
  2924. #ifdef ENOKEY
  2925. case ENOKEY: return DRMP3_ERROR;
  2926. #endif
  2927. #ifdef EKEYEXPIRED
  2928. case EKEYEXPIRED: return DRMP3_ERROR;
  2929. #endif
  2930. #ifdef EKEYREVOKED
  2931. case EKEYREVOKED: return DRMP3_ERROR;
  2932. #endif
  2933. #ifdef EKEYREJECTED
  2934. case EKEYREJECTED: return DRMP3_ERROR;
  2935. #endif
  2936. #ifdef EOWNERDEAD
  2937. case EOWNERDEAD: return DRMP3_ERROR;
  2938. #endif
  2939. #ifdef ENOTRECOVERABLE
  2940. case ENOTRECOVERABLE: return DRMP3_ERROR;
  2941. #endif
  2942. #ifdef ERFKILL
  2943. case ERFKILL: return DRMP3_ERROR;
  2944. #endif
  2945. #ifdef EHWPOISON
  2946. case EHWPOISON: return DRMP3_ERROR;
  2947. #endif
  2948. default: return DRMP3_ERROR;
  2949. }
  2950. }
  2951. static drmp3_result drmp3_fopen(FILE** ppFile, const char* pFilePath, const char* pOpenMode)
  2952. {
  2953. #if _MSC_VER && _MSC_VER >= 1400
  2954. errno_t err;
  2955. #endif
  2956. if (ppFile != NULL) {
  2957. *ppFile = NULL; /* Safety. */
  2958. }
  2959. if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) {
  2960. return DRMP3_INVALID_ARGS;
  2961. }
  2962. #if _MSC_VER && _MSC_VER >= 1400
  2963. err = fopen_s(ppFile, pFilePath, pOpenMode);
  2964. if (err != 0) {
  2965. return drmp3_result_from_errno(err);
  2966. }
  2967. #else
  2968. #if defined(_WIN32) || defined(__APPLE__)
  2969. *ppFile = fopen(pFilePath, pOpenMode);
  2970. #else
  2971. #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE)
  2972. *ppFile = fopen64(pFilePath, pOpenMode);
  2973. #else
  2974. *ppFile = fopen(pFilePath, pOpenMode);
  2975. #endif
  2976. #endif
  2977. if (*ppFile == NULL) {
  2978. drmp3_result result = drmp3_result_from_errno(errno);
  2979. if (result == DRMP3_SUCCESS) {
  2980. result = DRMP3_ERROR; /* Just a safety check to make sure we never ever return success when pFile == NULL. */
  2981. }
  2982. return result;
  2983. }
  2984. #endif
  2985. return DRMP3_SUCCESS;
  2986. }
  2987. /*
  2988. _wfopen() isn't always available in all compilation environments.
  2989. * Windows only.
  2990. * MSVC seems to support it universally as far back as VC6 from what I can tell (haven't checked further back).
  2991. * MinGW-64 (both 32- and 64-bit) seems to support it.
  2992. * MinGW wraps it in !defined(__STRICT_ANSI__).
  2993. This can be reviewed as compatibility issues arise. The preference is to use _wfopen_s() and _wfopen() as opposed to the wcsrtombs()
  2994. fallback, so if you notice your compiler not detecting this properly I'm happy to look at adding support.
  2995. */
  2996. #if defined(_WIN32)
  2997. #if defined(_MSC_VER) || defined(__MINGW64__) || !defined(__STRICT_ANSI__)
  2998. #define DRMP3_HAS_WFOPEN
  2999. #endif
  3000. #endif
  3001. static drmp3_result drmp3_wfopen(FILE** ppFile, const wchar_t* pFilePath, const wchar_t* pOpenMode, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3002. {
  3003. if (ppFile != NULL) {
  3004. *ppFile = NULL; /* Safety. */
  3005. }
  3006. if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) {
  3007. return DRMP3_INVALID_ARGS;
  3008. }
  3009. #if defined(DRMP3_HAS_WFOPEN)
  3010. {
  3011. /* Use _wfopen() on Windows. */
  3012. #if defined(_MSC_VER) && _MSC_VER >= 1400
  3013. errno_t err = _wfopen_s(ppFile, pFilePath, pOpenMode);
  3014. if (err != 0) {
  3015. return drmp3_result_from_errno(err);
  3016. }
  3017. #else
  3018. *ppFile = _wfopen(pFilePath, pOpenMode);
  3019. if (*ppFile == NULL) {
  3020. return drmp3_result_from_errno(errno);
  3021. }
  3022. #endif
  3023. (void)pAllocationCallbacks;
  3024. }
  3025. #else
  3026. /*
  3027. Use fopen() on anything other than Windows. Requires a conversion. This is annoying because fopen() is locale specific. The only real way I can
  3028. think of to do this is with wcsrtombs(). Note that wcstombs() is apparently not thread-safe because it uses a static global mbstate_t object for
  3029. maintaining state. I've checked this with -std=c89 and it works, but if somebody get's a compiler error I'll look into improving compatibility.
  3030. */
  3031. {
  3032. mbstate_t mbs;
  3033. size_t lenMB;
  3034. const wchar_t* pFilePathTemp = pFilePath;
  3035. char* pFilePathMB = NULL;
  3036. char pOpenModeMB[32] = {0};
  3037. /* Get the length first. */
  3038. DRMP3_ZERO_OBJECT(&mbs);
  3039. lenMB = wcsrtombs(NULL, &pFilePathTemp, 0, &mbs);
  3040. if (lenMB == (size_t)-1) {
  3041. return drmp3_result_from_errno(errno);
  3042. }
  3043. pFilePathMB = (char*)drmp3__malloc_from_callbacks(lenMB + 1, pAllocationCallbacks);
  3044. if (pFilePathMB == NULL) {
  3045. return DRMP3_OUT_OF_MEMORY;
  3046. }
  3047. pFilePathTemp = pFilePath;
  3048. DRMP3_ZERO_OBJECT(&mbs);
  3049. wcsrtombs(pFilePathMB, &pFilePathTemp, lenMB + 1, &mbs);
  3050. /* The open mode should always consist of ASCII characters so we should be able to do a trivial conversion. */
  3051. {
  3052. size_t i = 0;
  3053. for (;;) {
  3054. if (pOpenMode[i] == 0) {
  3055. pOpenModeMB[i] = '\0';
  3056. break;
  3057. }
  3058. pOpenModeMB[i] = (char)pOpenMode[i];
  3059. i += 1;
  3060. }
  3061. }
  3062. *ppFile = fopen(pFilePathMB, pOpenModeMB);
  3063. drmp3__free_from_callbacks(pFilePathMB, pAllocationCallbacks);
  3064. }
  3065. if (*ppFile == NULL) {
  3066. return DRMP3_ERROR;
  3067. }
  3068. #endif
  3069. return DRMP3_SUCCESS;
  3070. }
  3071. static size_t drmp3__on_read_stdio(void* pUserData, void* pBufferOut, size_t bytesToRead)
  3072. {
  3073. return fread(pBufferOut, 1, bytesToRead, (FILE*)pUserData);
  3074. }
  3075. static drmp3_bool32 drmp3__on_seek_stdio(void* pUserData, int offset, drmp3_seek_origin origin)
  3076. {
  3077. return fseek((FILE*)pUserData, offset, (origin == drmp3_seek_origin_current) ? SEEK_CUR : SEEK_SET) == 0;
  3078. }
  3079. DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3080. {
  3081. FILE* pFile;
  3082. if (drmp3_fopen(&pFile, pFilePath, "rb") != DRMP3_SUCCESS) {
  3083. return DRMP3_FALSE;
  3084. }
  3085. return drmp3_init(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, (void*)pFile, pAllocationCallbacks);
  3086. }
  3087. DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3088. {
  3089. FILE* pFile;
  3090. if (drmp3_wfopen(&pFile, pFilePath, L"rb", pAllocationCallbacks) != DRMP3_SUCCESS) {
  3091. return DRMP3_FALSE;
  3092. }
  3093. return drmp3_init(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, (void*)pFile, pAllocationCallbacks);
  3094. }
  3095. #endif
  3096. DRMP3_API void drmp3_uninit(drmp3* pMP3)
  3097. {
  3098. if (pMP3 == NULL) {
  3099. return;
  3100. }
  3101. #ifndef DR_MP3_NO_STDIO
  3102. if (pMP3->onRead == drmp3__on_read_stdio) {
  3103. fclose((FILE*)pMP3->pUserData);
  3104. }
  3105. #endif
  3106. drmp3__free_from_callbacks(pMP3->pData, &pMP3->allocationCallbacks);
  3107. }
  3108. #if defined(DR_MP3_FLOAT_OUTPUT)
  3109. static void drmp3_f32_to_s16(drmp3_int16* dst, const float* src, drmp3_uint64 sampleCount)
  3110. {
  3111. drmp3_uint64 i;
  3112. drmp3_uint64 i4;
  3113. drmp3_uint64 sampleCount4;
  3114. /* Unrolled. */
  3115. i = 0;
  3116. sampleCount4 = sampleCount >> 2;
  3117. for (i4 = 0; i4 < sampleCount4; i4 += 1) {
  3118. float x0 = src[i+0];
  3119. float x1 = src[i+1];
  3120. float x2 = src[i+2];
  3121. float x3 = src[i+3];
  3122. x0 = ((x0 < -1) ? -1 : ((x0 > 1) ? 1 : x0));
  3123. x1 = ((x1 < -1) ? -1 : ((x1 > 1) ? 1 : x1));
  3124. x2 = ((x2 < -1) ? -1 : ((x2 > 1) ? 1 : x2));
  3125. x3 = ((x3 < -1) ? -1 : ((x3 > 1) ? 1 : x3));
  3126. x0 = x0 * 32767.0f;
  3127. x1 = x1 * 32767.0f;
  3128. x2 = x2 * 32767.0f;
  3129. x3 = x3 * 32767.0f;
  3130. dst[i+0] = (drmp3_int16)x0;
  3131. dst[i+1] = (drmp3_int16)x1;
  3132. dst[i+2] = (drmp3_int16)x2;
  3133. dst[i+3] = (drmp3_int16)x3;
  3134. i += 4;
  3135. }
  3136. /* Leftover. */
  3137. for (; i < sampleCount; i += 1) {
  3138. float x = src[i];
  3139. x = ((x < -1) ? -1 : ((x > 1) ? 1 : x)); /* clip */
  3140. x = x * 32767.0f; /* -1..1 to -32767..32767 */
  3141. dst[i] = (drmp3_int16)x;
  3142. }
  3143. }
  3144. #endif
  3145. #if !defined(DR_MP3_FLOAT_OUTPUT)
  3146. static void drmp3_s16_to_f32(float* dst, const drmp3_int16* src, drmp3_uint64 sampleCount)
  3147. {
  3148. drmp3_uint64 i;
  3149. for (i = 0; i < sampleCount; i += 1) {
  3150. float x = (float)src[i];
  3151. x = x * 0.000030517578125f; /* -32768..32767 to -1..0.999969482421875 */
  3152. dst[i] = x;
  3153. }
  3154. }
  3155. #endif
  3156. static drmp3_uint64 drmp3_read_pcm_frames_raw(drmp3* pMP3, drmp3_uint64 framesToRead, void* pBufferOut)
  3157. {
  3158. drmp3_uint64 totalFramesRead = 0;
  3159. DRMP3_ASSERT(pMP3 != NULL);
  3160. DRMP3_ASSERT(pMP3->onRead != NULL);
  3161. while (framesToRead > 0) {
  3162. drmp3_uint32 framesToConsume = (drmp3_uint32)DRMP3_MIN(pMP3->pcmFramesRemainingInMP3Frame, framesToRead);
  3163. if (pBufferOut != NULL) {
  3164. #if defined(DR_MP3_FLOAT_OUTPUT)
  3165. /* f32 */
  3166. float* pFramesOutF32 = (float*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(float) * totalFramesRead * pMP3->channels);
  3167. float* pFramesInF32 = (float*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(float) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels);
  3168. DRMP3_COPY_MEMORY(pFramesOutF32, pFramesInF32, sizeof(float) * framesToConsume * pMP3->channels);
  3169. #else
  3170. /* s16 */
  3171. drmp3_int16* pFramesOutS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(drmp3_int16) * totalFramesRead * pMP3->channels);
  3172. drmp3_int16* pFramesInS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(drmp3_int16) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels);
  3173. DRMP3_COPY_MEMORY(pFramesOutS16, pFramesInS16, sizeof(drmp3_int16) * framesToConsume * pMP3->channels);
  3174. #endif
  3175. }
  3176. pMP3->currentPCMFrame += framesToConsume;
  3177. pMP3->pcmFramesConsumedInMP3Frame += framesToConsume;
  3178. pMP3->pcmFramesRemainingInMP3Frame -= framesToConsume;
  3179. totalFramesRead += framesToConsume;
  3180. framesToRead -= framesToConsume;
  3181. if (framesToRead == 0) {
  3182. break;
  3183. }
  3184. DRMP3_ASSERT(pMP3->pcmFramesRemainingInMP3Frame == 0);
  3185. /*
  3186. At this point we have exhausted our in-memory buffer so we need to re-fill. Note that the sample rate may have changed
  3187. at this point which means we'll also need to update our sample rate conversion pipeline.
  3188. */
  3189. if (drmp3_decode_next_frame(pMP3) == 0) {
  3190. break;
  3191. }
  3192. }
  3193. return totalFramesRead;
  3194. }
  3195. DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut)
  3196. {
  3197. if (pMP3 == NULL || pMP3->onRead == NULL) {
  3198. return 0;
  3199. }
  3200. #if defined(DR_MP3_FLOAT_OUTPUT)
  3201. /* Fast path. No conversion required. */
  3202. return drmp3_read_pcm_frames_raw(pMP3, framesToRead, pBufferOut);
  3203. #else
  3204. /* Slow path. Convert from s16 to f32. */
  3205. {
  3206. drmp3_int16 pTempS16[8192];
  3207. drmp3_uint64 totalPCMFramesRead = 0;
  3208. while (totalPCMFramesRead < framesToRead) {
  3209. drmp3_uint64 framesJustRead;
  3210. drmp3_uint64 framesRemaining = framesToRead - totalPCMFramesRead;
  3211. drmp3_uint64 framesToReadNow = DRMP3_COUNTOF(pTempS16) / pMP3->channels;
  3212. if (framesToReadNow > framesRemaining) {
  3213. framesToReadNow = framesRemaining;
  3214. }
  3215. framesJustRead = drmp3_read_pcm_frames_raw(pMP3, framesToReadNow, pTempS16);
  3216. if (framesJustRead == 0) {
  3217. break;
  3218. }
  3219. drmp3_s16_to_f32((float*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(float) * totalPCMFramesRead * pMP3->channels), pTempS16, framesJustRead * pMP3->channels);
  3220. totalPCMFramesRead += framesJustRead;
  3221. }
  3222. return totalPCMFramesRead;
  3223. }
  3224. #endif
  3225. }
  3226. DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 framesToRead, drmp3_int16* pBufferOut)
  3227. {
  3228. if (pMP3 == NULL || pMP3->onRead == NULL) {
  3229. return 0;
  3230. }
  3231. #if !defined(DR_MP3_FLOAT_OUTPUT)
  3232. /* Fast path. No conversion required. */
  3233. return drmp3_read_pcm_frames_raw(pMP3, framesToRead, pBufferOut);
  3234. #else
  3235. /* Slow path. Convert from f32 to s16. */
  3236. {
  3237. float pTempF32[4096];
  3238. drmp3_uint64 totalPCMFramesRead = 0;
  3239. while (totalPCMFramesRead < framesToRead) {
  3240. drmp3_uint64 framesJustRead;
  3241. drmp3_uint64 framesRemaining = framesToRead - totalPCMFramesRead;
  3242. drmp3_uint64 framesToReadNow = DRMP3_COUNTOF(pTempF32) / pMP3->channels;
  3243. if (framesToReadNow > framesRemaining) {
  3244. framesToReadNow = framesRemaining;
  3245. }
  3246. framesJustRead = drmp3_read_pcm_frames_raw(pMP3, framesToReadNow, pTempF32);
  3247. if (framesJustRead == 0) {
  3248. break;
  3249. }
  3250. drmp3_f32_to_s16((drmp3_int16*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(drmp3_int16) * totalPCMFramesRead * pMP3->channels), pTempF32, framesJustRead * pMP3->channels);
  3251. totalPCMFramesRead += framesJustRead;
  3252. }
  3253. return totalPCMFramesRead;
  3254. }
  3255. #endif
  3256. }
  3257. static void drmp3_reset(drmp3* pMP3)
  3258. {
  3259. DRMP3_ASSERT(pMP3 != NULL);
  3260. pMP3->pcmFramesConsumedInMP3Frame = 0;
  3261. pMP3->pcmFramesRemainingInMP3Frame = 0;
  3262. pMP3->currentPCMFrame = 0;
  3263. pMP3->dataSize = 0;
  3264. pMP3->atEnd = DRMP3_FALSE;
  3265. drmp3dec_init(&pMP3->decoder);
  3266. }
  3267. static drmp3_bool32 drmp3_seek_to_start_of_stream(drmp3* pMP3)
  3268. {
  3269. DRMP3_ASSERT(pMP3 != NULL);
  3270. DRMP3_ASSERT(pMP3->onSeek != NULL);
  3271. /* Seek to the start of the stream to begin with. */
  3272. if (!drmp3__on_seek(pMP3, 0, drmp3_seek_origin_start)) {
  3273. return DRMP3_FALSE;
  3274. }
  3275. /* Clear any cached data. */
  3276. drmp3_reset(pMP3);
  3277. return DRMP3_TRUE;
  3278. }
  3279. static drmp3_bool32 drmp3_seek_forward_by_pcm_frames__brute_force(drmp3* pMP3, drmp3_uint64 frameOffset)
  3280. {
  3281. drmp3_uint64 framesRead;
  3282. /*
  3283. Just using a dumb read-and-discard for now. What would be nice is to parse only the header of the MP3 frame, and then skip over leading
  3284. frames without spending the time doing a full decode. I cannot see an easy way to do this in minimp3, however, so it may involve some
  3285. kind of manual processing.
  3286. */
  3287. #if defined(DR_MP3_FLOAT_OUTPUT)
  3288. framesRead = drmp3_read_pcm_frames_f32(pMP3, frameOffset, NULL);
  3289. #else
  3290. framesRead = drmp3_read_pcm_frames_s16(pMP3, frameOffset, NULL);
  3291. #endif
  3292. if (framesRead != frameOffset) {
  3293. return DRMP3_FALSE;
  3294. }
  3295. return DRMP3_TRUE;
  3296. }
  3297. static drmp3_bool32 drmp3_seek_to_pcm_frame__brute_force(drmp3* pMP3, drmp3_uint64 frameIndex)
  3298. {
  3299. DRMP3_ASSERT(pMP3 != NULL);
  3300. if (frameIndex == pMP3->currentPCMFrame) {
  3301. return DRMP3_TRUE;
  3302. }
  3303. /*
  3304. If we're moving foward we just read from where we're at. Otherwise we need to move back to the start of
  3305. the stream and read from the beginning.
  3306. */
  3307. if (frameIndex < pMP3->currentPCMFrame) {
  3308. /* Moving backward. Move to the start of the stream and then move forward. */
  3309. if (!drmp3_seek_to_start_of_stream(pMP3)) {
  3310. return DRMP3_FALSE;
  3311. }
  3312. }
  3313. DRMP3_ASSERT(frameIndex >= pMP3->currentPCMFrame);
  3314. return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, (frameIndex - pMP3->currentPCMFrame));
  3315. }
  3316. static drmp3_bool32 drmp3_find_closest_seek_point(drmp3* pMP3, drmp3_uint64 frameIndex, drmp3_uint32* pSeekPointIndex)
  3317. {
  3318. drmp3_uint32 iSeekPoint;
  3319. DRMP3_ASSERT(pSeekPointIndex != NULL);
  3320. *pSeekPointIndex = 0;
  3321. if (frameIndex < pMP3->pSeekPoints[0].pcmFrameIndex) {
  3322. return DRMP3_FALSE;
  3323. }
  3324. /* Linear search for simplicity to begin with while I'm getting this thing working. Once it's all working change this to a binary search. */
  3325. for (iSeekPoint = 0; iSeekPoint < pMP3->seekPointCount; ++iSeekPoint) {
  3326. if (pMP3->pSeekPoints[iSeekPoint].pcmFrameIndex > frameIndex) {
  3327. break; /* Found it. */
  3328. }
  3329. *pSeekPointIndex = iSeekPoint;
  3330. }
  3331. return DRMP3_TRUE;
  3332. }
  3333. static drmp3_bool32 drmp3_seek_to_pcm_frame__seek_table(drmp3* pMP3, drmp3_uint64 frameIndex)
  3334. {
  3335. drmp3_seek_point seekPoint;
  3336. drmp3_uint32 priorSeekPointIndex;
  3337. drmp3_uint16 iMP3Frame;
  3338. drmp3_uint64 leftoverFrames;
  3339. DRMP3_ASSERT(pMP3 != NULL);
  3340. DRMP3_ASSERT(pMP3->pSeekPoints != NULL);
  3341. DRMP3_ASSERT(pMP3->seekPointCount > 0);
  3342. /* If there is no prior seekpoint it means the target PCM frame comes before the first seek point. Just assume a seekpoint at the start of the file in this case. */
  3343. if (drmp3_find_closest_seek_point(pMP3, frameIndex, &priorSeekPointIndex)) {
  3344. seekPoint = pMP3->pSeekPoints[priorSeekPointIndex];
  3345. } else {
  3346. seekPoint.seekPosInBytes = 0;
  3347. seekPoint.pcmFrameIndex = 0;
  3348. seekPoint.mp3FramesToDiscard = 0;
  3349. seekPoint.pcmFramesToDiscard = 0;
  3350. }
  3351. /* First thing to do is seek to the first byte of the relevant MP3 frame. */
  3352. if (!drmp3__on_seek_64(pMP3, seekPoint.seekPosInBytes, drmp3_seek_origin_start)) {
  3353. return DRMP3_FALSE; /* Failed to seek. */
  3354. }
  3355. /* Clear any cached data. */
  3356. drmp3_reset(pMP3);
  3357. /* Whole MP3 frames need to be discarded first. */
  3358. for (iMP3Frame = 0; iMP3Frame < seekPoint.mp3FramesToDiscard; ++iMP3Frame) {
  3359. drmp3_uint32 pcmFramesRead;
  3360. drmp3d_sample_t* pPCMFrames;
  3361. /* Pass in non-null for the last frame because we want to ensure the sample rate converter is preloaded correctly. */
  3362. pPCMFrames = NULL;
  3363. if (iMP3Frame == seekPoint.mp3FramesToDiscard-1) {
  3364. pPCMFrames = (drmp3d_sample_t*)pMP3->pcmFrames;
  3365. }
  3366. /* We first need to decode the next frame. */
  3367. pcmFramesRead = drmp3_decode_next_frame_ex(pMP3, pPCMFrames);
  3368. if (pcmFramesRead == 0) {
  3369. return DRMP3_FALSE;
  3370. }
  3371. }
  3372. /* We seeked to an MP3 frame in the raw stream so we need to make sure the current PCM frame is set correctly. */
  3373. pMP3->currentPCMFrame = seekPoint.pcmFrameIndex - seekPoint.pcmFramesToDiscard;
  3374. /*
  3375. Now at this point we can follow the same process as the brute force technique where we just skip over unnecessary MP3 frames and then
  3376. read-and-discard at least 2 whole MP3 frames.
  3377. */
  3378. leftoverFrames = frameIndex - pMP3->currentPCMFrame;
  3379. return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, leftoverFrames);
  3380. }
  3381. DRMP3_API drmp3_bool32 drmp3_seek_to_pcm_frame(drmp3* pMP3, drmp3_uint64 frameIndex)
  3382. {
  3383. if (pMP3 == NULL || pMP3->onSeek == NULL) {
  3384. return DRMP3_FALSE;
  3385. }
  3386. if (frameIndex == 0) {
  3387. return drmp3_seek_to_start_of_stream(pMP3);
  3388. }
  3389. /* Use the seek table if we have one. */
  3390. if (pMP3->pSeekPoints != NULL && pMP3->seekPointCount > 0) {
  3391. return drmp3_seek_to_pcm_frame__seek_table(pMP3, frameIndex);
  3392. } else {
  3393. return drmp3_seek_to_pcm_frame__brute_force(pMP3, frameIndex);
  3394. }
  3395. }
  3396. DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint64* pMP3FrameCount, drmp3_uint64* pPCMFrameCount)
  3397. {
  3398. drmp3_uint64 currentPCMFrame;
  3399. drmp3_uint64 totalPCMFrameCount;
  3400. drmp3_uint64 totalMP3FrameCount;
  3401. if (pMP3 == NULL) {
  3402. return DRMP3_FALSE;
  3403. }
  3404. /*
  3405. The way this works is we move back to the start of the stream, iterate over each MP3 frame and calculate the frame count based
  3406. on our output sample rate, the seek back to the PCM frame we were sitting on before calling this function.
  3407. */
  3408. /* The stream must support seeking for this to work. */
  3409. if (pMP3->onSeek == NULL) {
  3410. return DRMP3_FALSE;
  3411. }
  3412. /* We'll need to seek back to where we were, so grab the PCM frame we're currently sitting on so we can restore later. */
  3413. currentPCMFrame = pMP3->currentPCMFrame;
  3414. if (!drmp3_seek_to_start_of_stream(pMP3)) {
  3415. return DRMP3_FALSE;
  3416. }
  3417. totalPCMFrameCount = 0;
  3418. totalMP3FrameCount = 0;
  3419. for (;;) {
  3420. drmp3_uint32 pcmFramesInCurrentMP3Frame;
  3421. pcmFramesInCurrentMP3Frame = drmp3_decode_next_frame_ex(pMP3, NULL);
  3422. if (pcmFramesInCurrentMP3Frame == 0) {
  3423. break;
  3424. }
  3425. totalPCMFrameCount += pcmFramesInCurrentMP3Frame;
  3426. totalMP3FrameCount += 1;
  3427. }
  3428. /* Finally, we need to seek back to where we were. */
  3429. if (!drmp3_seek_to_start_of_stream(pMP3)) {
  3430. return DRMP3_FALSE;
  3431. }
  3432. if (!drmp3_seek_to_pcm_frame(pMP3, currentPCMFrame)) {
  3433. return DRMP3_FALSE;
  3434. }
  3435. if (pMP3FrameCount != NULL) {
  3436. *pMP3FrameCount = totalMP3FrameCount;
  3437. }
  3438. if (pPCMFrameCount != NULL) {
  3439. *pPCMFrameCount = totalPCMFrameCount;
  3440. }
  3441. return DRMP3_TRUE;
  3442. }
  3443. DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3)
  3444. {
  3445. drmp3_uint64 totalPCMFrameCount;
  3446. if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, NULL, &totalPCMFrameCount)) {
  3447. return 0;
  3448. }
  3449. return totalPCMFrameCount;
  3450. }
  3451. DRMP3_API drmp3_uint64 drmp3_get_mp3_frame_count(drmp3* pMP3)
  3452. {
  3453. drmp3_uint64 totalMP3FrameCount;
  3454. if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, NULL)) {
  3455. return 0;
  3456. }
  3457. return totalMP3FrameCount;
  3458. }
  3459. static void drmp3__accumulate_running_pcm_frame_count(drmp3* pMP3, drmp3_uint32 pcmFrameCountIn, drmp3_uint64* pRunningPCMFrameCount, float* pRunningPCMFrameCountFractionalPart)
  3460. {
  3461. float srcRatio;
  3462. float pcmFrameCountOutF;
  3463. drmp3_uint32 pcmFrameCountOut;
  3464. srcRatio = (float)pMP3->mp3FrameSampleRate / (float)pMP3->sampleRate;
  3465. DRMP3_ASSERT(srcRatio > 0);
  3466. pcmFrameCountOutF = *pRunningPCMFrameCountFractionalPart + (pcmFrameCountIn / srcRatio);
  3467. pcmFrameCountOut = (drmp3_uint32)pcmFrameCountOutF;
  3468. *pRunningPCMFrameCountFractionalPart = pcmFrameCountOutF - pcmFrameCountOut;
  3469. *pRunningPCMFrameCount += pcmFrameCountOut;
  3470. }
  3471. typedef struct
  3472. {
  3473. drmp3_uint64 bytePos;
  3474. drmp3_uint64 pcmFrameIndex; /* <-- After sample rate conversion. */
  3475. } drmp3__seeking_mp3_frame_info;
  3476. DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints)
  3477. {
  3478. drmp3_uint32 seekPointCount;
  3479. drmp3_uint64 currentPCMFrame;
  3480. drmp3_uint64 totalMP3FrameCount;
  3481. drmp3_uint64 totalPCMFrameCount;
  3482. if (pMP3 == NULL || pSeekPointCount == NULL || pSeekPoints == NULL) {
  3483. return DRMP3_FALSE; /* Invalid args. */
  3484. }
  3485. seekPointCount = *pSeekPointCount;
  3486. if (seekPointCount == 0) {
  3487. return DRMP3_FALSE; /* The client has requested no seek points. Consider this to be invalid arguments since the client has probably not intended this. */
  3488. }
  3489. /* We'll need to seek back to the current sample after calculating the seekpoints so we need to go ahead and grab the current location at the top. */
  3490. currentPCMFrame = pMP3->currentPCMFrame;
  3491. /* We never do more than the total number of MP3 frames and we limit it to 32-bits. */
  3492. if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, &totalPCMFrameCount)) {
  3493. return DRMP3_FALSE;
  3494. }
  3495. /* If there's less than DRMP3_SEEK_LEADING_MP3_FRAMES+1 frames we just report 1 seek point which will be the very start of the stream. */
  3496. if (totalMP3FrameCount < DRMP3_SEEK_LEADING_MP3_FRAMES+1) {
  3497. seekPointCount = 1;
  3498. pSeekPoints[0].seekPosInBytes = 0;
  3499. pSeekPoints[0].pcmFrameIndex = 0;
  3500. pSeekPoints[0].mp3FramesToDiscard = 0;
  3501. pSeekPoints[0].pcmFramesToDiscard = 0;
  3502. } else {
  3503. drmp3_uint64 pcmFramesBetweenSeekPoints;
  3504. drmp3__seeking_mp3_frame_info mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES+1];
  3505. drmp3_uint64 runningPCMFrameCount = 0;
  3506. float runningPCMFrameCountFractionalPart = 0;
  3507. drmp3_uint64 nextTargetPCMFrame;
  3508. drmp3_uint32 iMP3Frame;
  3509. drmp3_uint32 iSeekPoint;
  3510. if (seekPointCount > totalMP3FrameCount-1) {
  3511. seekPointCount = (drmp3_uint32)totalMP3FrameCount-1;
  3512. }
  3513. pcmFramesBetweenSeekPoints = totalPCMFrameCount / (seekPointCount+1);
  3514. /*
  3515. Here is where we actually calculate the seek points. We need to start by moving the start of the stream. We then enumerate over each
  3516. MP3 frame.
  3517. */
  3518. if (!drmp3_seek_to_start_of_stream(pMP3)) {
  3519. return DRMP3_FALSE;
  3520. }
  3521. /*
  3522. We need to cache the byte positions of the previous MP3 frames. As a new MP3 frame is iterated, we cycle the byte positions in this
  3523. array. The value in the first item in this array is the byte position that will be reported in the next seek point.
  3524. */
  3525. /* We need to initialize the array of MP3 byte positions for the leading MP3 frames. */
  3526. for (iMP3Frame = 0; iMP3Frame < DRMP3_SEEK_LEADING_MP3_FRAMES+1; ++iMP3Frame) {
  3527. drmp3_uint32 pcmFramesInCurrentMP3FrameIn;
  3528. /* The byte position of the next frame will be the stream's cursor position, minus whatever is sitting in the buffer. */
  3529. DRMP3_ASSERT(pMP3->streamCursor >= pMP3->dataSize);
  3530. mp3FrameInfo[iMP3Frame].bytePos = pMP3->streamCursor - pMP3->dataSize;
  3531. mp3FrameInfo[iMP3Frame].pcmFrameIndex = runningPCMFrameCount;
  3532. /* We need to get information about this frame so we can know how many samples it contained. */
  3533. pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL);
  3534. if (pcmFramesInCurrentMP3FrameIn == 0) {
  3535. return DRMP3_FALSE; /* This should never happen. */
  3536. }
  3537. drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart);
  3538. }
  3539. /*
  3540. At this point we will have extracted the byte positions of the leading MP3 frames. We can now start iterating over each seek point and
  3541. calculate them.
  3542. */
  3543. nextTargetPCMFrame = 0;
  3544. for (iSeekPoint = 0; iSeekPoint < seekPointCount; ++iSeekPoint) {
  3545. nextTargetPCMFrame += pcmFramesBetweenSeekPoints;
  3546. for (;;) {
  3547. if (nextTargetPCMFrame < runningPCMFrameCount) {
  3548. /* The next seek point is in the current MP3 frame. */
  3549. pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos;
  3550. pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame;
  3551. pSeekPoints[iSeekPoint].mp3FramesToDiscard = DRMP3_SEEK_LEADING_MP3_FRAMES;
  3552. pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES-1].pcmFrameIndex);
  3553. break;
  3554. } else {
  3555. size_t i;
  3556. drmp3_uint32 pcmFramesInCurrentMP3FrameIn;
  3557. /*
  3558. The next seek point is not in the current MP3 frame, so continue on to the next one. The first thing to do is cycle the cached
  3559. MP3 frame info.
  3560. */
  3561. for (i = 0; i < DRMP3_COUNTOF(mp3FrameInfo)-1; ++i) {
  3562. mp3FrameInfo[i] = mp3FrameInfo[i+1];
  3563. }
  3564. /* Cache previous MP3 frame info. */
  3565. mp3FrameInfo[DRMP3_COUNTOF(mp3FrameInfo)-1].bytePos = pMP3->streamCursor - pMP3->dataSize;
  3566. mp3FrameInfo[DRMP3_COUNTOF(mp3FrameInfo)-1].pcmFrameIndex = runningPCMFrameCount;
  3567. /*
  3568. Go to the next MP3 frame. This shouldn't ever fail, but just in case it does we just set the seek point and break. If it happens, it
  3569. should only ever do it for the last seek point.
  3570. */
  3571. pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL);
  3572. if (pcmFramesInCurrentMP3FrameIn == 0) {
  3573. pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos;
  3574. pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame;
  3575. pSeekPoints[iSeekPoint].mp3FramesToDiscard = DRMP3_SEEK_LEADING_MP3_FRAMES;
  3576. pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES-1].pcmFrameIndex);
  3577. break;
  3578. }
  3579. drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart);
  3580. }
  3581. }
  3582. }
  3583. /* Finally, we need to seek back to where we were. */
  3584. if (!drmp3_seek_to_start_of_stream(pMP3)) {
  3585. return DRMP3_FALSE;
  3586. }
  3587. if (!drmp3_seek_to_pcm_frame(pMP3, currentPCMFrame)) {
  3588. return DRMP3_FALSE;
  3589. }
  3590. }
  3591. *pSeekPointCount = seekPointCount;
  3592. return DRMP3_TRUE;
  3593. }
  3594. DRMP3_API drmp3_bool32 drmp3_bind_seek_table(drmp3* pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point* pSeekPoints)
  3595. {
  3596. if (pMP3 == NULL) {
  3597. return DRMP3_FALSE;
  3598. }
  3599. if (seekPointCount == 0 || pSeekPoints == NULL) {
  3600. /* Unbinding. */
  3601. pMP3->seekPointCount = 0;
  3602. pMP3->pSeekPoints = NULL;
  3603. } else {
  3604. /* Binding. */
  3605. pMP3->seekPointCount = seekPointCount;
  3606. pMP3->pSeekPoints = pSeekPoints;
  3607. }
  3608. return DRMP3_TRUE;
  3609. }
  3610. static float* drmp3__full_read_and_close_f32(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
  3611. {
  3612. drmp3_uint64 totalFramesRead = 0;
  3613. drmp3_uint64 framesCapacity = 0;
  3614. float* pFrames = NULL;
  3615. float temp[4096];
  3616. DRMP3_ASSERT(pMP3 != NULL);
  3617. for (;;) {
  3618. drmp3_uint64 framesToReadRightNow = DRMP3_COUNTOF(temp) / pMP3->channels;
  3619. drmp3_uint64 framesJustRead = drmp3_read_pcm_frames_f32(pMP3, framesToReadRightNow, temp);
  3620. if (framesJustRead == 0) {
  3621. break;
  3622. }
  3623. /* Reallocate the output buffer if there's not enough room. */
  3624. if (framesCapacity < totalFramesRead + framesJustRead) {
  3625. drmp3_uint64 oldFramesBufferSize;
  3626. drmp3_uint64 newFramesBufferSize;
  3627. drmp3_uint64 newFramesCap;
  3628. float* pNewFrames;
  3629. newFramesCap = framesCapacity * 2;
  3630. if (newFramesCap < totalFramesRead + framesJustRead) {
  3631. newFramesCap = totalFramesRead + framesJustRead;
  3632. }
  3633. oldFramesBufferSize = framesCapacity * pMP3->channels * sizeof(float);
  3634. newFramesBufferSize = newFramesCap * pMP3->channels * sizeof(float);
  3635. if (newFramesBufferSize > DRMP3_SIZE_MAX) {
  3636. break;
  3637. }
  3638. pNewFrames = (float*)drmp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks);
  3639. if (pNewFrames == NULL) {
  3640. drmp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks);
  3641. break;
  3642. }
  3643. pFrames = pNewFrames;
  3644. framesCapacity = newFramesCap;
  3645. }
  3646. DRMP3_COPY_MEMORY(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(float)));
  3647. totalFramesRead += framesJustRead;
  3648. /* If the number of frames we asked for is less that what we actually read it means we've reached the end. */
  3649. if (framesJustRead != framesToReadRightNow) {
  3650. break;
  3651. }
  3652. }
  3653. if (pConfig != NULL) {
  3654. pConfig->channels = pMP3->channels;
  3655. pConfig->sampleRate = pMP3->sampleRate;
  3656. }
  3657. drmp3_uninit(pMP3);
  3658. if (pTotalFrameCount) {
  3659. *pTotalFrameCount = totalFramesRead;
  3660. }
  3661. return pFrames;
  3662. }
  3663. static drmp3_int16* drmp3__full_read_and_close_s16(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
  3664. {
  3665. drmp3_uint64 totalFramesRead = 0;
  3666. drmp3_uint64 framesCapacity = 0;
  3667. drmp3_int16* pFrames = NULL;
  3668. drmp3_int16 temp[4096];
  3669. DRMP3_ASSERT(pMP3 != NULL);
  3670. for (;;) {
  3671. drmp3_uint64 framesToReadRightNow = DRMP3_COUNTOF(temp) / pMP3->channels;
  3672. drmp3_uint64 framesJustRead = drmp3_read_pcm_frames_s16(pMP3, framesToReadRightNow, temp);
  3673. if (framesJustRead == 0) {
  3674. break;
  3675. }
  3676. /* Reallocate the output buffer if there's not enough room. */
  3677. if (framesCapacity < totalFramesRead + framesJustRead) {
  3678. drmp3_uint64 newFramesBufferSize;
  3679. drmp3_uint64 oldFramesBufferSize;
  3680. drmp3_uint64 newFramesCap;
  3681. drmp3_int16* pNewFrames;
  3682. newFramesCap = framesCapacity * 2;
  3683. if (newFramesCap < totalFramesRead + framesJustRead) {
  3684. newFramesCap = totalFramesRead + framesJustRead;
  3685. }
  3686. oldFramesBufferSize = framesCapacity * pMP3->channels * sizeof(drmp3_int16);
  3687. newFramesBufferSize = newFramesCap * pMP3->channels * sizeof(drmp3_int16);
  3688. if (newFramesBufferSize > DRMP3_SIZE_MAX) {
  3689. break;
  3690. }
  3691. pNewFrames = (drmp3_int16*)drmp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks);
  3692. if (pNewFrames == NULL) {
  3693. drmp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks);
  3694. break;
  3695. }
  3696. pFrames = pNewFrames;
  3697. framesCapacity = newFramesCap;
  3698. }
  3699. DRMP3_COPY_MEMORY(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(drmp3_int16)));
  3700. totalFramesRead += framesJustRead;
  3701. /* If the number of frames we asked for is less that what we actually read it means we've reached the end. */
  3702. if (framesJustRead != framesToReadRightNow) {
  3703. break;
  3704. }
  3705. }
  3706. if (pConfig != NULL) {
  3707. pConfig->channels = pMP3->channels;
  3708. pConfig->sampleRate = pMP3->sampleRate;
  3709. }
  3710. drmp3_uninit(pMP3);
  3711. if (pTotalFrameCount) {
  3712. *pTotalFrameCount = totalFramesRead;
  3713. }
  3714. return pFrames;
  3715. }
  3716. DRMP3_API float* drmp3_open_and_read_pcm_frames_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3717. {
  3718. drmp3 mp3;
  3719. if (!drmp3_init(&mp3, onRead, onSeek, pUserData, pAllocationCallbacks)) {
  3720. return NULL;
  3721. }
  3722. return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount);
  3723. }
  3724. DRMP3_API drmp3_int16* drmp3_open_and_read_pcm_frames_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3725. {
  3726. drmp3 mp3;
  3727. if (!drmp3_init(&mp3, onRead, onSeek, pUserData, pAllocationCallbacks)) {
  3728. return NULL;
  3729. }
  3730. return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount);
  3731. }
  3732. DRMP3_API float* drmp3_open_memory_and_read_pcm_frames_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3733. {
  3734. drmp3 mp3;
  3735. if (!drmp3_init_memory(&mp3, pData, dataSize, pAllocationCallbacks)) {
  3736. return NULL;
  3737. }
  3738. return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount);
  3739. }
  3740. DRMP3_API drmp3_int16* drmp3_open_memory_and_read_pcm_frames_s16(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3741. {
  3742. drmp3 mp3;
  3743. if (!drmp3_init_memory(&mp3, pData, dataSize, pAllocationCallbacks)) {
  3744. return NULL;
  3745. }
  3746. return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount);
  3747. }
  3748. #ifndef DR_MP3_NO_STDIO
  3749. DRMP3_API float* drmp3_open_file_and_read_pcm_frames_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3750. {
  3751. drmp3 mp3;
  3752. if (!drmp3_init_file(&mp3, filePath, pAllocationCallbacks)) {
  3753. return NULL;
  3754. }
  3755. return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount);
  3756. }
  3757. DRMP3_API drmp3_int16* drmp3_open_file_and_read_pcm_frames_s16(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3758. {
  3759. drmp3 mp3;
  3760. if (!drmp3_init_file(&mp3, filePath, pAllocationCallbacks)) {
  3761. return NULL;
  3762. }
  3763. return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount);
  3764. }
  3765. #endif
  3766. DRMP3_API void* drmp3_malloc(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3767. {
  3768. if (pAllocationCallbacks != NULL) {
  3769. return drmp3__malloc_from_callbacks(sz, pAllocationCallbacks);
  3770. } else {
  3771. return drmp3__malloc_default(sz, NULL);
  3772. }
  3773. }
  3774. DRMP3_API void drmp3_free(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks)
  3775. {
  3776. if (pAllocationCallbacks != NULL) {
  3777. drmp3__free_from_callbacks(p, pAllocationCallbacks);
  3778. } else {
  3779. drmp3__free_default(p, NULL);
  3780. }
  3781. }
  3782. #endif /* dr_mp3_c */
  3783. #endif /*DR_MP3_IMPLEMENTATION*/
  3784. /*
  3785. DIFFERENCES BETWEEN minimp3 AND dr_mp3
  3786. ======================================
  3787. - First, keep in mind that minimp3 (https://github.com/lieff/minimp3) is where all the real work was done. All of the
  3788. code relating to the actual decoding remains mostly unmodified, apart from some namespacing changes.
  3789. - dr_mp3 adds a pulling style API which allows you to deliver raw data via callbacks. So, rather than pushing data
  3790. to the decoder, the decoder _pulls_ data from your callbacks.
  3791. - In addition to callbacks, a decoder can be initialized from a block of memory and a file.
  3792. - The dr_mp3 pull API reads PCM frames rather than whole MP3 frames.
  3793. - dr_mp3 adds convenience APIs for opening and decoding entire files in one go.
  3794. - dr_mp3 is fully namespaced, including the implementation section, which is more suitable when compiling projects
  3795. as a single translation unit (aka unity builds). At the time of writing this, a unity build is not possible when
  3796. using minimp3 in conjunction with stb_vorbis. dr_mp3 addresses this.
  3797. */
  3798. /*
  3799. RELEASE NOTES - v0.5.0
  3800. =======================
  3801. Version 0.5.0 has breaking API changes.
  3802. Improved Client-Defined Memory Allocation
  3803. -----------------------------------------
  3804. The main change with this release is the addition of a more flexible way of implementing custom memory allocation routines. The
  3805. existing system of DRMP3_MALLOC, DRMP3_REALLOC and DRMP3_FREE are still in place and will be used by default when no custom
  3806. allocation callbacks are specified.
  3807. To use the new system, you pass in a pointer to a drmp3_allocation_callbacks object to drmp3_init() and family, like this:
  3808. void* my_malloc(size_t sz, void* pUserData)
  3809. {
  3810. return malloc(sz);
  3811. }
  3812. void* my_realloc(void* p, size_t sz, void* pUserData)
  3813. {
  3814. return realloc(p, sz);
  3815. }
  3816. void my_free(void* p, void* pUserData)
  3817. {
  3818. free(p);
  3819. }
  3820. ...
  3821. drmp3_allocation_callbacks allocationCallbacks;
  3822. allocationCallbacks.pUserData = &myData;
  3823. allocationCallbacks.onMalloc = my_malloc;
  3824. allocationCallbacks.onRealloc = my_realloc;
  3825. allocationCallbacks.onFree = my_free;
  3826. drmp3_init_file(&mp3, "my_file.mp3", NULL, &allocationCallbacks);
  3827. The advantage of this new system is that it allows you to specify user data which will be passed in to the allocation routines.
  3828. Passing in null for the allocation callbacks object will cause dr_mp3 to use defaults which is the same as DRMP3_MALLOC,
  3829. DRMP3_REALLOC and DRMP3_FREE and the equivalent of how it worked in previous versions.
  3830. Every API that opens a drmp3 object now takes this extra parameter. These include the following:
  3831. drmp3_init()
  3832. drmp3_init_file()
  3833. drmp3_init_memory()
  3834. drmp3_open_and_read_pcm_frames_f32()
  3835. drmp3_open_and_read_pcm_frames_s16()
  3836. drmp3_open_memory_and_read_pcm_frames_f32()
  3837. drmp3_open_memory_and_read_pcm_frames_s16()
  3838. drmp3_open_file_and_read_pcm_frames_f32()
  3839. drmp3_open_file_and_read_pcm_frames_s16()
  3840. Renamed APIs
  3841. ------------
  3842. The following APIs have been renamed for consistency with other dr_* libraries and to make it clear that they return PCM frame
  3843. counts rather than sample counts.
  3844. drmp3_open_and_read_f32() -> drmp3_open_and_read_pcm_frames_f32()
  3845. drmp3_open_and_read_s16() -> drmp3_open_and_read_pcm_frames_s16()
  3846. drmp3_open_memory_and_read_f32() -> drmp3_open_memory_and_read_pcm_frames_f32()
  3847. drmp3_open_memory_and_read_s16() -> drmp3_open_memory_and_read_pcm_frames_s16()
  3848. drmp3_open_file_and_read_f32() -> drmp3_open_file_and_read_pcm_frames_f32()
  3849. drmp3_open_file_and_read_s16() -> drmp3_open_file_and_read_pcm_frames_s16()
  3850. */
  3851. /*
  3852. REVISION HISTORY
  3853. ================
  3854. v0.6.16 - 2020-08-02
  3855. - Simplify sized types.
  3856. v0.6.15 - 2020-07-25
  3857. - Fix a compilation warning.
  3858. v0.6.14 - 2020-07-23
  3859. - Fix undefined behaviour with memmove().
  3860. v0.6.13 - 2020-07-06
  3861. - Fix a bug when converting from s16 to f32 in drmp3_read_pcm_frames_f32().
  3862. v0.6.12 - 2020-06-23
  3863. - Add include guard for the implementation section.
  3864. v0.6.11 - 2020-05-26
  3865. - Fix use of uninitialized variable error.
  3866. v0.6.10 - 2020-05-16
  3867. - Add compile-time and run-time version querying.
  3868. - DRMP3_VERSION_MINOR
  3869. - DRMP3_VERSION_MAJOR
  3870. - DRMP3_VERSION_REVISION
  3871. - DRMP3_VERSION_STRING
  3872. - drmp3_version()
  3873. - drmp3_version_string()
  3874. v0.6.9 - 2020-04-30
  3875. - Change the `pcm` parameter of drmp3dec_decode_frame() to a `const drmp3_uint8*` for consistency with internal APIs.
  3876. v0.6.8 - 2020-04-26
  3877. - Optimizations to decoding when initializing from memory.
  3878. v0.6.7 - 2020-04-25
  3879. - Fix a compilation error with DR_MP3_NO_STDIO
  3880. - Optimization to decoding by reducing some data movement.
  3881. v0.6.6 - 2020-04-23
  3882. - Fix a minor bug with the running PCM frame counter.
  3883. v0.6.5 - 2020-04-19
  3884. - Fix compilation error on ARM builds.
  3885. v0.6.4 - 2020-04-19
  3886. - Bring up to date with changes to minimp3.
  3887. v0.6.3 - 2020-04-13
  3888. - Fix some pedantic warnings.
  3889. v0.6.2 - 2020-04-10
  3890. - Fix a crash in drmp3_open_*_and_read_pcm_frames_*() if the output config object is NULL.
  3891. v0.6.1 - 2020-04-05
  3892. - Fix warnings.
  3893. v0.6.0 - 2020-04-04
  3894. - API CHANGE: Remove the pConfig parameter from the following APIs:
  3895. - drmp3_init()
  3896. - drmp3_init_memory()
  3897. - drmp3_init_file()
  3898. - Add drmp3_init_file_w() for opening a file from a wchar_t encoded path.
  3899. v0.5.6 - 2020-02-12
  3900. - Bring up to date with minimp3.
  3901. v0.5.5 - 2020-01-29
  3902. - Fix a memory allocation bug in high level s16 decoding APIs.
  3903. v0.5.4 - 2019-12-02
  3904. - Fix a possible null pointer dereference when using custom memory allocators for realloc().
  3905. v0.5.3 - 2019-11-14
  3906. - Fix typos in documentation.
  3907. v0.5.2 - 2019-11-02
  3908. - Bring up to date with minimp3.
  3909. v0.5.1 - 2019-10-08
  3910. - Fix a warning with GCC.
  3911. v0.5.0 - 2019-10-07
  3912. - API CHANGE: Add support for user defined memory allocation routines. This system allows the program to specify their own memory allocation
  3913. routines with a user data pointer for client-specific contextual data. This adds an extra parameter to the end of the following APIs:
  3914. - drmp3_init()
  3915. - drmp3_init_file()
  3916. - drmp3_init_memory()
  3917. - drmp3_open_and_read_pcm_frames_f32()
  3918. - drmp3_open_and_read_pcm_frames_s16()
  3919. - drmp3_open_memory_and_read_pcm_frames_f32()
  3920. - drmp3_open_memory_and_read_pcm_frames_s16()
  3921. - drmp3_open_file_and_read_pcm_frames_f32()
  3922. - drmp3_open_file_and_read_pcm_frames_s16()
  3923. - API CHANGE: Renamed the following APIs:
  3924. - drmp3_open_and_read_f32() -> drmp3_open_and_read_pcm_frames_f32()
  3925. - drmp3_open_and_read_s16() -> drmp3_open_and_read_pcm_frames_s16()
  3926. - drmp3_open_memory_and_read_f32() -> drmp3_open_memory_and_read_pcm_frames_f32()
  3927. - drmp3_open_memory_and_read_s16() -> drmp3_open_memory_and_read_pcm_frames_s16()
  3928. - drmp3_open_file_and_read_f32() -> drmp3_open_file_and_read_pcm_frames_f32()
  3929. - drmp3_open_file_and_read_s16() -> drmp3_open_file_and_read_pcm_frames_s16()
  3930. v0.4.7 - 2019-07-28
  3931. - Fix a compiler error.
  3932. v0.4.6 - 2019-06-14
  3933. - Fix a compiler error.
  3934. v0.4.5 - 2019-06-06
  3935. - Bring up to date with minimp3.
  3936. v0.4.4 - 2019-05-06
  3937. - Fixes to the VC6 build.
  3938. v0.4.3 - 2019-05-05
  3939. - Use the channel count and/or sample rate of the first MP3 frame instead of DRMP3_DEFAULT_CHANNELS and
  3940. DRMP3_DEFAULT_SAMPLE_RATE when they are set to 0. To use the old behaviour, just set the relevant property to
  3941. DRMP3_DEFAULT_CHANNELS or DRMP3_DEFAULT_SAMPLE_RATE.
  3942. - Add s16 reading APIs
  3943. - drmp3_read_pcm_frames_s16
  3944. - drmp3_open_memory_and_read_pcm_frames_s16
  3945. - drmp3_open_and_read_pcm_frames_s16
  3946. - drmp3_open_file_and_read_pcm_frames_s16
  3947. - Add drmp3_get_mp3_and_pcm_frame_count() to the public header section.
  3948. - Add support for C89.
  3949. - Change license to choice of public domain or MIT-0.
  3950. v0.4.2 - 2019-02-21
  3951. - Fix a warning.
  3952. v0.4.1 - 2018-12-30
  3953. - Fix a warning.
  3954. v0.4.0 - 2018-12-16
  3955. - API CHANGE: Rename some APIs:
  3956. - drmp3_read_f32 -> to drmp3_read_pcm_frames_f32
  3957. - drmp3_seek_to_frame -> drmp3_seek_to_pcm_frame
  3958. - drmp3_open_and_decode_f32 -> drmp3_open_and_read_pcm_frames_f32
  3959. - drmp3_open_and_decode_memory_f32 -> drmp3_open_memory_and_read_pcm_frames_f32
  3960. - drmp3_open_and_decode_file_f32 -> drmp3_open_file_and_read_pcm_frames_f32
  3961. - Add drmp3_get_pcm_frame_count().
  3962. - Add drmp3_get_mp3_frame_count().
  3963. - Improve seeking performance.
  3964. v0.3.2 - 2018-09-11
  3965. - Fix a couple of memory leaks.
  3966. - Bring up to date with minimp3.
  3967. v0.3.1 - 2018-08-25
  3968. - Fix C++ build.
  3969. v0.3.0 - 2018-08-25
  3970. - Bring up to date with minimp3. This has a minor API change: the "pcm" parameter of drmp3dec_decode_frame() has
  3971. been changed from short* to void* because it can now output both s16 and f32 samples, depending on whether or
  3972. not the DR_MP3_FLOAT_OUTPUT option is set.
  3973. v0.2.11 - 2018-08-08
  3974. - Fix a bug where the last part of a file is not read.
  3975. v0.2.10 - 2018-08-07
  3976. - Improve 64-bit detection.
  3977. v0.2.9 - 2018-08-05
  3978. - Fix C++ build on older versions of GCC.
  3979. - Bring up to date with minimp3.
  3980. v0.2.8 - 2018-08-02
  3981. - Fix compilation errors with older versions of GCC.
  3982. v0.2.7 - 2018-07-13
  3983. - Bring up to date with minimp3.
  3984. v0.2.6 - 2018-07-12
  3985. - Bring up to date with minimp3.
  3986. v0.2.5 - 2018-06-22
  3987. - Bring up to date with minimp3.
  3988. v0.2.4 - 2018-05-12
  3989. - Bring up to date with minimp3.
  3990. v0.2.3 - 2018-04-29
  3991. - Fix TCC build.
  3992. v0.2.2 - 2018-04-28
  3993. - Fix bug when opening a decoder from memory.
  3994. v0.2.1 - 2018-04-27
  3995. - Efficiency improvements when the decoder reaches the end of the stream.
  3996. v0.2 - 2018-04-21
  3997. - Bring up to date with minimp3.
  3998. - Start using major.minor.revision versioning.
  3999. v0.1d - 2018-03-30
  4000. - Bring up to date with minimp3.
  4001. v0.1c - 2018-03-11
  4002. - Fix C++ build error.
  4003. v0.1b - 2018-03-07
  4004. - Bring up to date with minimp3.
  4005. v0.1a - 2018-02-28
  4006. - Fix compilation error on GCC/Clang.
  4007. - Fix some warnings.
  4008. v0.1 - 2018-02-xx
  4009. - Initial versioned release.
  4010. */
  4011. /*
  4012. This software is available as a choice of the following licenses. Choose
  4013. whichever you prefer.
  4014. ===============================================================================
  4015. ALTERNATIVE 1 - Public Domain (www.unlicense.org)
  4016. ===============================================================================
  4017. This is free and unencumbered software released into the public domain.
  4018. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  4019. software, either in source code form or as a compiled binary, for any purpose,
  4020. commercial or non-commercial, and by any means.
  4021. In jurisdictions that recognize copyright laws, the author or authors of this
  4022. software dedicate any and all copyright interest in the software to the public
  4023. domain. We make this dedication for the benefit of the public at large and to
  4024. the detriment of our heirs and successors. We intend this dedication to be an
  4025. overt act of relinquishment in perpetuity of all present and future rights to
  4026. this software under copyright law.
  4027. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  4028. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  4029. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  4030. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  4031. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  4032. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  4033. For more information, please refer to <http://unlicense.org/>
  4034. ===============================================================================
  4035. ALTERNATIVE 2 - MIT No Attribution
  4036. ===============================================================================
  4037. Copyright 2020 David Reid
  4038. Permission is hereby granted, free of charge, to any person obtaining a copy of
  4039. this software and associated documentation files (the "Software"), to deal in
  4040. the Software without restriction, including without limitation the rights to
  4041. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  4042. of the Software, and to permit persons to whom the Software is furnished to do
  4043. so.
  4044. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  4045. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  4046. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  4047. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  4048. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  4049. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  4050. SOFTWARE.
  4051. */
  4052. /*
  4053. https://github.com/lieff/minimp3
  4054. To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide.
  4055. This software is distributed without any warranty.
  4056. See <http://creativecommons.org/publicdomain/zero/1.0/>.
  4057. */