stb_vorbis.h 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267
  1. // Ogg Vorbis audio decoder - v1.09 - public domain
  2. // http://nothings.org/stb_vorbis/
  3. //
  4. // Original version written by Sean Barrett in 2007.
  5. //
  6. // Originally sponsored by RAD Game Tools. Seeking sponsored
  7. // by Phillip Bennefall, Marc Andersen, Aaron Baker, Elias Software,
  8. // Aras Pranckevicius, and Sean Barrett.
  9. //
  10. // LICENSE
  11. //
  12. // This software is dual-licensed to the public domain and under the following
  13. // license: you are granted a perpetual, irrevocable license to copy, modify,
  14. // publish, and distribute this file as you see fit.
  15. //
  16. // No warranty for any purpose is expressed or implied by the author (nor
  17. // by RAD Game Tools). Report bugs and send enhancements to the author.
  18. //
  19. // Limitations:
  20. //
  21. // - floor 0 not supported (used in old ogg vorbis files pre-2004)
  22. // - lossless sample-truncation at beginning ignored
  23. // - cannot concatenate multiple vorbis streams
  24. // - sample positions are 32-bit, limiting seekable 192Khz
  25. // files to around 6 hours (Ogg supports 64-bit)
  26. //
  27. // Feature contributors:
  28. // Dougall Johnson (sample-exact seeking)
  29. //
  30. // Bugfix/warning contributors:
  31. // Terje Mathisen Niklas Frykholm Andy Hill
  32. // Casey Muratori John Bolton Gargaj
  33. // Laurent Gomila Marc LeBlanc Ronny Chevalier
  34. // Bernhard Wodo Evan Balster alxprd@github
  35. // Tom Beaumont Ingo Leitgeb Nicolas Guillemot
  36. // Phillip Bennefall Rohit Thiago Goulart
  37. // manxorist@github saga musix
  38. //
  39. // Partial history:
  40. // 1.09 - 2016/04/04 - back out 'truncation of last frame' fix from previous version
  41. // 1.08 - 2016/04/02 - warnings; setup memory leaks; truncation of last frame
  42. // 1.07 - 2015/01/16 - fixes for crashes on invalid files; warning fixes; const
  43. // 1.06 - 2015/08/31 - full, correct support for seeking API (Dougall Johnson)
  44. // some crash fixes when out of memory or with corrupt files
  45. // fix some inappropriately signed shifts
  46. // 1.05 - 2015/04/19 - don't define __forceinline if it's redundant
  47. // 1.04 - 2014/08/27 - fix missing const-correct case in API
  48. // 1.03 - 2014/08/07 - warning fixes
  49. // 1.02 - 2014/07/09 - declare qsort comparison as explicitly _cdecl in Windows
  50. // 1.01 - 2014/06/18 - fix stb_vorbis_get_samples_float (interleaved was correct)
  51. // 1.0 - 2014/05/26 - fix memory leaks; fix warnings; fix bugs in >2-channel;
  52. // (API change) report sample rate for decode-full-file funcs
  53. //
  54. // See end of file for full version history.
  55. //////////////////////////////////////////////////////////////////////////////
  56. //
  57. // HEADER BEGINS HERE
  58. //
  59. #ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H
  60. #define STB_VORBIS_INCLUDE_STB_VORBIS_H
  61. #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
  62. #define STB_VORBIS_NO_STDIO 1
  63. #endif
  64. #ifndef STB_VORBIS_NO_STDIO
  65. #include <stdio.h>
  66. #include "polycode/core/PolyCoreServices.h"
  67. #include "polycode/core/PolyCore.h"
  68. #endif
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. /////////// THREAD SAFETY
  73. // Individual stb_vorbis* handles are not thread-safe; you cannot decode from
  74. // them from multiple threads at the same time. However, you can have multiple
  75. // stb_vorbis* handles and decode from them independently in multiple thrads.
  76. /////////// MEMORY ALLOCATION
  77. // normally stb_vorbis uses malloc() to allocate memory at startup,
  78. // and alloca() to allocate temporary memory during a frame on the
  79. // stack. (Memory consumption will depend on the amount of setup
  80. // data in the file and how you set the compile flags for speed
  81. // vs. size. In my test files the maximal-size usage is ~150KB.)
  82. //
  83. // You can modify the wrapper functions in the source (setup_malloc,
  84. // setup_temp_malloc, temp_malloc) to change this behavior, or you
  85. // can use a simpler allocation model: you pass in a buffer from
  86. // which stb_vorbis will allocate _all_ its memory (including the
  87. // temp memory). "open" may fail with a VORBIS_outofmem if you
  88. // do not pass in enough data; there is no way to determine how
  89. // much you do need except to succeed (at which point you can
  90. // query get_info to find the exact amount required. yes I know
  91. // this is lame).
  92. //
  93. // If you pass in a non-NULL buffer of the type below, allocation
  94. // will occur from it as described above. Otherwise just pass NULL
  95. // to use malloc()/alloca()
  96. typedef struct {
  97. char *alloc_buffer;
  98. int alloc_buffer_length_in_bytes;
  99. } stb_vorbis_alloc;
  100. /////////// FUNCTIONS USEABLE WITH ALL INPUT MODES
  101. typedef struct stb_vorbis stb_vorbis;
  102. typedef struct {
  103. unsigned int sample_rate;
  104. int channels;
  105. unsigned int setup_memory_required;
  106. unsigned int setup_temp_memory_required;
  107. unsigned int temp_memory_required;
  108. int max_frame_size;
  109. } stb_vorbis_info;
  110. // get general information about the file
  111. extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f);
  112. // get the last error detected (clears it, too)
  113. extern int stb_vorbis_get_error(stb_vorbis *f);
  114. // close an ogg vorbis file and free all memory in use
  115. extern void stb_vorbis_close(stb_vorbis *f);
  116. // this function returns the offset (in samples) from the beginning of the
  117. // file that will be returned by the next decode, if it is known, or -1
  118. // otherwise. after a flush_pushdata() call, this may take a while before
  119. // it becomes valid again.
  120. // NOT WORKING YET after a seek with PULLDATA API
  121. extern int stb_vorbis_get_sample_offset(stb_vorbis *f);
  122. // returns the current seek point within the file, or offset from the beginning
  123. // of the memory buffer. In pushdata mode it returns 0.
  124. extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);
  125. /////////// PUSHDATA API
  126. #ifndef STB_VORBIS_NO_PUSHDATA_API
  127. // this API allows you to get blocks of data from any source and hand
  128. // them to stb_vorbis. you have to buffer them; stb_vorbis will tell
  129. // you how much it used, and you have to give it the rest next time;
  130. // and stb_vorbis may not have enough data to work with and you will
  131. // need to give it the same data again PLUS more. Note that the Vorbis
  132. // specification does not bound the size of an individual frame.
  133. extern stb_vorbis *stb_vorbis_open_pushdata(
  134. const unsigned char * datablock, int datablock_length_in_bytes,
  135. int *datablock_memory_consumed_in_bytes,
  136. int *error,
  137. const stb_vorbis_alloc *alloc_buffer);
  138. // create a vorbis decoder by passing in the initial data block containing
  139. // the ogg&vorbis headers (you don't need to do parse them, just provide
  140. // the first N bytes of the file--you're told if it's not enough, see below)
  141. // on success, returns an stb_vorbis *, does not set error, returns the amount of
  142. // data parsed/consumed on this call in *datablock_memory_consumed_in_bytes;
  143. // on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed
  144. // if returns NULL and *error is VORBIS_need_more_data, then the input block was
  145. // incomplete and you need to pass in a larger block from the start of the file
  146. extern int stb_vorbis_decode_frame_pushdata(
  147. stb_vorbis *f,
  148. const unsigned char *datablock, int datablock_length_in_bytes,
  149. int *channels, // place to write number of float * buffers
  150. float ***output, // place to write float ** array of float * buffers
  151. int *samples // place to write number of output samples
  152. );
  153. // decode a frame of audio sample data if possible from the passed-in data block
  154. //
  155. // return value: number of bytes we used from datablock
  156. //
  157. // possible cases:
  158. // 0 bytes used, 0 samples output (need more data)
  159. // N bytes used, 0 samples output (resynching the stream, keep going)
  160. // N bytes used, M samples output (one frame of data)
  161. // note that after opening a file, you will ALWAYS get one N-bytes,0-sample
  162. // frame, because Vorbis always "discards" the first frame.
  163. //
  164. // Note that on resynch, stb_vorbis will rarely consume all of the buffer,
  165. // instead only datablock_length_in_bytes-3 or less. This is because it wants
  166. // to avoid missing parts of a page header if they cross a datablock boundary,
  167. // without writing state-machiney code to record a partial detection.
  168. //
  169. // The number of channels returned are stored in *channels (which can be
  170. // NULL--it is always the same as the number of channels reported by
  171. // get_info). *output will contain an array of float* buffers, one per
  172. // channel. In other words, (*output)[0][0] contains the first sample from
  173. // the first channel, and (*output)[1][0] contains the first sample from
  174. // the second channel.
  175. extern void stb_vorbis_flush_pushdata(stb_vorbis *f);
  176. // inform stb_vorbis that your next datablock will not be contiguous with
  177. // previous ones (e.g. you've seeked in the data); future attempts to decode
  178. // frames will cause stb_vorbis to resynchronize (as noted above), and
  179. // once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it
  180. // will begin decoding the _next_ frame.
  181. //
  182. // if you want to seek using pushdata, you need to seek in your file, then
  183. // call stb_vorbis_flush_pushdata(), then start calling decoding, then once
  184. // decoding is returning you data, call stb_vorbis_get_sample_offset, and
  185. // if you don't like the result, seek your file again and repeat.
  186. #endif
  187. ////////// PULLING INPUT API
  188. #ifndef STB_VORBIS_NO_PULLDATA_API
  189. // This API assumes stb_vorbis is allowed to pull data from a source--
  190. // either a block of memory containing the _entire_ vorbis stream, or a
  191. // FILE * that you or it create, or possibly some other reading mechanism
  192. // if you go modify the source to replace the FILE * case with some kind
  193. // of callback to your code. (But if you don't support seeking, you may
  194. // just want to go ahead and use pushdata.)
  195. #if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
  196. extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output);
  197. #endif
  198. #if !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
  199. extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output);
  200. #endif
  201. // decode an entire file and output the data interleaved into a malloc()ed
  202. // buffer stored in *output. The return value is the number of samples
  203. // decoded, or -1 if the file could not be opened or was not an ogg vorbis file.
  204. // When you're done with it, just free() the pointer returned in *output.
  205. extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len,
  206. int *error, const stb_vorbis_alloc *alloc_buffer);
  207. // create an ogg vorbis decoder from an ogg vorbis stream in memory (note
  208. // this must be the entire stream!). on failure, returns NULL and sets *error
  209. #ifndef STB_VORBIS_NO_STDIO
  210. extern stb_vorbis * stb_vorbis_open_filename(const char *filename,
  211. int *error, const stb_vorbis_alloc *alloc_buffer);
  212. // create an ogg vorbis decoder from a filename via fopen(). on failure,
  213. // returns NULL and sets *error (possibly to VORBIS_file_open_failure).
  214. extern stb_vorbis * stb_vorbis_open_file(Polycode::CoreFile *f, int close_handle_on_close,
  215. int *error, const stb_vorbis_alloc *alloc_buffer);
  216. // create an ogg vorbis decoder from an open FILE *, looking for a stream at
  217. // the _current_ seek point (ftell). on failure, returns NULL and sets *error.
  218. // note that stb_vorbis must "own" this stream; if you seek it in between
  219. // calls to stb_vorbis, it will become confused. Morever, if you attempt to
  220. // perform stb_vorbis_seek_*() operations on this file, it will assume it
  221. // owns the _entire_ rest of the file after the start point. Use the next
  222. // function, stb_vorbis_open_file_section(), to limit it.
  223. extern stb_vorbis * stb_vorbis_open_file_section(Polycode::CoreFile *f, int close_handle_on_close,
  224. int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len);
  225. // create an ogg vorbis decoder from an open FILE *, looking for a stream at
  226. // the _current_ seek point (ftell); the stream will be of length 'len' bytes.
  227. // on failure, returns NULL and sets *error. note that stb_vorbis must "own"
  228. // this stream; if you seek it in between calls to stb_vorbis, it will become
  229. // confused.
  230. #endif
  231. extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number);
  232. extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number);
  233. // these functions seek in the Vorbis file to (approximately) 'sample_number'.
  234. // after calling seek_frame(), the next call to get_frame_*() will include
  235. // the specified sample. after calling stb_vorbis_seek(), the next call to
  236. // stb_vorbis_get_samples_* will start with the specified sample. If you
  237. // do not need to seek to EXACTLY the target sample when using get_samples_*,
  238. // you can also use seek_frame().
  239. extern void stb_vorbis_seek_start(stb_vorbis *f);
  240. // this function is equivalent to stb_vorbis_seek(f,0)
  241. extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f);
  242. extern float stb_vorbis_stream_length_in_seconds(stb_vorbis *f);
  243. // these functions return the total length of the vorbis stream
  244. extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output);
  245. // decode the next frame and return the number of samples. the number of
  246. // channels returned are stored in *channels (which can be NULL--it is always
  247. // the same as the number of channels reported by get_info). *output will
  248. // contain an array of float* buffers, one per channel. These outputs will
  249. // be overwritten on the next call to stb_vorbis_get_frame_*.
  250. //
  251. // You generally should not intermix calls to stb_vorbis_get_frame_*()
  252. // and stb_vorbis_get_samples_*(), since the latter calls the former.
  253. #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
  254. extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts);
  255. extern int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples);
  256. #endif
  257. // decode the next frame and return the number of *samples* per channel.
  258. // Note that for interleaved data, you pass in the number of shorts (the
  259. // size of your array), but the return value is the number of samples per
  260. // channel, not the total number of samples.
  261. //
  262. // The data is coerced to the number of channels you request according to the
  263. // channel coercion rules (see below). You must pass in the size of your
  264. // buffer(s) so that stb_vorbis will not overwrite the end of the buffer.
  265. // The maximum buffer size needed can be gotten from get_info(); however,
  266. // the Vorbis I specification implies an absolute maximum of 4096 samples
  267. // per channel.
  268. // Channel coercion rules:
  269. // Let M be the number of channels requested, and N the number of channels present,
  270. // and Cn be the nth channel; let stereo L be the sum of all L and center channels,
  271. // and stereo R be the sum of all R and center channels (channel assignment from the
  272. // vorbis spec).
  273. // M N output
  274. // 1 k sum(Ck) for all k
  275. // 2 * stereo L, stereo R
  276. // k l k > l, the first l channels, then 0s
  277. // k l k <= l, the first k channels
  278. // Note that this is not _good_ surround etc. mixing at all! It's just so
  279. // you get something useful.
  280. extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats);
  281. extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples);
  282. // gets num_samples samples, not necessarily on a frame boundary--this requires
  283. // buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES.
  284. // Returns the number of samples stored per channel; it may be less than requested
  285. // at the end of the file. If there are no more samples in the file, returns 0.
  286. #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
  287. extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts);
  288. extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples);
  289. #endif
  290. // gets num_samples samples, not necessarily on a frame boundary--this requires
  291. // buffering so you have to supply the buffers. Applies the coercion rules above
  292. // to produce 'channels' channels. Returns the number of samples stored per channel;
  293. // it may be less than requested at the end of the file. If there are no more
  294. // samples in the file, returns 0.
  295. #endif
  296. //////// ERROR CODES
  297. enum STBVorbisError {
  298. VORBIS__no_error,
  299. VORBIS_need_more_data = 1, // not a real error
  300. VORBIS_invalid_api_mixing, // can't mix API modes
  301. VORBIS_outofmem, // not enough memory
  302. VORBIS_feature_not_supported, // uses floor 0
  303. VORBIS_too_many_channels, // STB_VORBIS_MAX_CHANNELS is too small
  304. VORBIS_file_open_failure, // fopen() failed
  305. VORBIS_seek_without_length, // can't seek in unknown-length file
  306. VORBIS_unexpected_eof = 10, // file is truncated?
  307. VORBIS_seek_invalid, // seek past EOF
  308. // decoding errors (corrupt/invalid stream) -- you probably
  309. // don't care about the exact details of these
  310. // vorbis errors:
  311. VORBIS_invalid_setup = 20,
  312. VORBIS_invalid_stream,
  313. // ogg errors:
  314. VORBIS_missing_capture_pattern = 30,
  315. VORBIS_invalid_stream_structure_version,
  316. VORBIS_continued_packet_flag_invalid,
  317. VORBIS_incorrect_stream_serial_number,
  318. VORBIS_invalid_first_page,
  319. VORBIS_bad_packet_type,
  320. VORBIS_cant_find_last_page,
  321. VORBIS_seek_failed
  322. };
  323. #ifdef __cplusplus
  324. }
  325. #endif
  326. #endif // STB_VORBIS_INCLUDE_STB_VORBIS_H
  327. //
  328. // HEADER ENDS HERE
  329. //
  330. //////////////////////////////////////////////////////////////////////////////
  331. #ifndef STB_VORBIS_HEADER_ONLY
  332. // global configuration settings (e.g. set these in the project/makefile),
  333. // or just set them in this file at the top (although ideally the first few
  334. // should be visible when the header file is compiled too, although it's not
  335. // crucial)
  336. // STB_VORBIS_NO_PUSHDATA_API
  337. // does not compile the code for the various stb_vorbis_*_pushdata()
  338. // functions
  339. // #define STB_VORBIS_NO_PUSHDATA_API
  340. // STB_VORBIS_NO_PULLDATA_API
  341. // does not compile the code for the non-pushdata APIs
  342. // #define STB_VORBIS_NO_PULLDATA_API
  343. // STB_VORBIS_NO_STDIO
  344. // does not compile the code for the APIs that use FILE *s internally
  345. // or externally (implied by STB_VORBIS_NO_PULLDATA_API)
  346. // #define STB_VORBIS_NO_STDIO
  347. // STB_VORBIS_NO_INTEGER_CONVERSION
  348. // does not compile the code for converting audio sample data from
  349. // float to integer (implied by STB_VORBIS_NO_PULLDATA_API)
  350. // #define STB_VORBIS_NO_INTEGER_CONVERSION
  351. // STB_VORBIS_NO_FAST_SCALED_FLOAT
  352. // does not use a fast float-to-int trick to accelerate float-to-int on
  353. // most platforms which requires endianness be defined correctly.
  354. //#define STB_VORBIS_NO_FAST_SCALED_FLOAT
  355. // STB_VORBIS_MAX_CHANNELS [number]
  356. // globally define this to the maximum number of channels you need.
  357. // The spec does not put a restriction on channels except that
  358. // the count is stored in a byte, so 255 is the hard limit.
  359. // Reducing this saves about 16 bytes per value, so using 16 saves
  360. // (255-16)*16 or around 4KB. Plus anything other memory usage
  361. // I forgot to account for. Can probably go as low as 8 (7.1 audio),
  362. // 6 (5.1 audio), or 2 (stereo only).
  363. #ifndef STB_VORBIS_MAX_CHANNELS
  364. #define STB_VORBIS_MAX_CHANNELS 16 // enough for anyone?
  365. #endif
  366. // STB_VORBIS_PUSHDATA_CRC_COUNT [number]
  367. // after a flush_pushdata(), stb_vorbis begins scanning for the
  368. // next valid page, without backtracking. when it finds something
  369. // that looks like a page, it streams through it and verifies its
  370. // CRC32. Should that validation fail, it keeps scanning. But it's
  371. // possible that _while_ streaming through to check the CRC32 of
  372. // one candidate page, it sees another candidate page. This #define
  373. // determines how many "overlapping" candidate pages it can search
  374. // at once. Note that "real" pages are typically ~4KB to ~8KB, whereas
  375. // garbage pages could be as big as 64KB, but probably average ~16KB.
  376. // So don't hose ourselves by scanning an apparent 64KB page and
  377. // missing a ton of real ones in the interim; so minimum of 2
  378. #ifndef STB_VORBIS_PUSHDATA_CRC_COUNT
  379. #define STB_VORBIS_PUSHDATA_CRC_COUNT 4
  380. #endif
  381. // STB_VORBIS_FAST_HUFFMAN_LENGTH [number]
  382. // sets the log size of the huffman-acceleration table. Maximum
  383. // supported value is 24. with larger numbers, more decodings are O(1),
  384. // but the table size is larger so worse cache missing, so you'll have
  385. // to probe (and try multiple ogg vorbis files) to find the sweet spot.
  386. #ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH
  387. #define STB_VORBIS_FAST_HUFFMAN_LENGTH 10
  388. #endif
  389. // STB_VORBIS_FAST_BINARY_LENGTH [number]
  390. // sets the log size of the binary-search acceleration table. this
  391. // is used in similar fashion to the fast-huffman size to set initial
  392. // parameters for the binary search
  393. // STB_VORBIS_FAST_HUFFMAN_INT
  394. // The fast huffman tables are much more efficient if they can be
  395. // stored as 16-bit results instead of 32-bit results. This restricts
  396. // the codebooks to having only 65535 possible outcomes, though.
  397. // (At least, accelerated by the huffman table.)
  398. #ifndef STB_VORBIS_FAST_HUFFMAN_INT
  399. #define STB_VORBIS_FAST_HUFFMAN_SHORT
  400. #endif
  401. // STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
  402. // If the 'fast huffman' search doesn't succeed, then stb_vorbis falls
  403. // back on binary searching for the correct one. This requires storing
  404. // extra tables with the huffman codes in sorted order. Defining this
  405. // symbol trades off space for speed by forcing a linear search in the
  406. // non-fast case, except for "sparse" codebooks.
  407. // #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
  408. // STB_VORBIS_DIVIDES_IN_RESIDUE
  409. // stb_vorbis precomputes the result of the scalar residue decoding
  410. // that would otherwise require a divide per chunk. you can trade off
  411. // space for time by defining this symbol.
  412. // #define STB_VORBIS_DIVIDES_IN_RESIDUE
  413. // STB_VORBIS_DIVIDES_IN_CODEBOOK
  414. // vorbis VQ codebooks can be encoded two ways: with every case explicitly
  415. // stored, or with all elements being chosen from a small range of values,
  416. // and all values possible in all elements. By default, stb_vorbis expands
  417. // this latter kind out to look like the former kind for ease of decoding,
  418. // because otherwise an integer divide-per-vector-element is required to
  419. // unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can
  420. // trade off storage for speed.
  421. //#define STB_VORBIS_DIVIDES_IN_CODEBOOK
  422. #ifdef STB_VORBIS_CODEBOOK_SHORTS
  423. #error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats"
  424. #endif
  425. // STB_VORBIS_DIVIDE_TABLE
  426. // this replaces small integer divides in the floor decode loop with
  427. // table lookups. made less than 1% difference, so disabled by default.
  428. // STB_VORBIS_NO_INLINE_DECODE
  429. // disables the inlining of the scalar codebook fast-huffman decode.
  430. // might save a little codespace; useful for debugging
  431. // #define STB_VORBIS_NO_INLINE_DECODE
  432. // STB_VORBIS_NO_DEFER_FLOOR
  433. // Normally we only decode the floor without synthesizing the actual
  434. // full curve. We can instead synthesize the curve immediately. This
  435. // requires more memory and is very likely slower, so I don't think
  436. // you'd ever want to do it except for debugging.
  437. // #define STB_VORBIS_NO_DEFER_FLOOR
  438. //////////////////////////////////////////////////////////////////////////////
  439. #ifdef STB_VORBIS_NO_PULLDATA_API
  440. #define STB_VORBIS_NO_INTEGER_CONVERSION
  441. #define STB_VORBIS_NO_STDIO
  442. #endif
  443. #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
  444. #define STB_VORBIS_NO_STDIO 1
  445. #endif
  446. #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
  447. #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
  448. // only need endianness for fast-float-to-int, which we don't
  449. // use for pushdata
  450. #ifndef STB_VORBIS_BIG_ENDIAN
  451. #define STB_VORBIS_ENDIAN 0
  452. #else
  453. #define STB_VORBIS_ENDIAN 1
  454. #endif
  455. #endif
  456. #endif
  457. #ifndef STB_VORBIS_NO_STDIO
  458. #include <stdio.h>
  459. #endif
  460. #ifndef STB_VORBIS_NO_CRT
  461. #include <stdlib.h>
  462. #include <string.h>
  463. #include <assert.h>
  464. #include <math.h>
  465. #if !(defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh))
  466. #include <malloc.h>
  467. #if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__)
  468. #include <alloca.h>
  469. #endif
  470. #endif
  471. #else // STB_VORBIS_NO_CRT
  472. #define NULL 0
  473. #define malloc(s) 0
  474. #define free(s) ((void) 0)
  475. #define realloc(s) 0
  476. #endif // STB_VORBIS_NO_CRT
  477. #include <limits.h>
  478. #ifdef __MINGW32__
  479. // eff you mingw:
  480. // "fixed":
  481. // http://sourceforge.net/p/mingw-w64/mailman/message/32882927/
  482. // "no that broke the build, reverted, who cares about C":
  483. // http://sourceforge.net/p/mingw-w64/mailman/message/32890381/
  484. #ifdef __forceinline
  485. #undef __forceinline
  486. #endif
  487. #define __forceinline
  488. #elif !defined(_MSC_VER)
  489. #if __GNUC__
  490. #define __forceinline inline
  491. #else
  492. #define __forceinline
  493. #endif
  494. #endif
  495. #if STB_VORBIS_MAX_CHANNELS > 256
  496. #error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range"
  497. #endif
  498. #if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24
  499. #error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range"
  500. #endif
  501. #if 0
  502. #include <crtdbg.h>
  503. #define CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1])
  504. #else
  505. #define CHECK(f) ((void) 0)
  506. #endif
  507. #define MAX_BLOCKSIZE_LOG 13 // from specification
  508. #define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG)
  509. typedef unsigned char uint8;
  510. typedef signed char int8;
  511. typedef unsigned short uint16;
  512. typedef signed short int16;
  513. typedef unsigned int uint32;
  514. typedef signed int int32;
  515. #ifndef TRUE
  516. #define TRUE 1
  517. #define FALSE 0
  518. #endif
  519. typedef float codetype;
  520. // @NOTE
  521. //
  522. // Some arrays below are tagged "//varies", which means it's actually
  523. // a variable-sized piece of data, but rather than malloc I assume it's
  524. // small enough it's better to just allocate it all together with the
  525. // main thing
  526. //
  527. // Most of the variables are specified with the smallest size I could pack
  528. // them into. It might give better performance to make them all full-sized
  529. // integers. It should be safe to freely rearrange the structures or change
  530. // the sizes larger--nothing relies on silently truncating etc., nor the
  531. // order of variables.
  532. #define FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH)
  533. #define FAST_HUFFMAN_TABLE_MASK (FAST_HUFFMAN_TABLE_SIZE - 1)
  534. typedef struct {
  535. int dimensions, entries;
  536. uint8 *codeword_lengths;
  537. float minimum_value;
  538. float delta_value;
  539. uint8 value_bits;
  540. uint8 lookup_type;
  541. uint8 sequence_p;
  542. uint8 sparse;
  543. uint32 lookup_values;
  544. codetype *multiplicands;
  545. uint32 *codewords;
  546. #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
  547. int16 fast_huffman[FAST_HUFFMAN_TABLE_SIZE];
  548. #else
  549. int32 fast_huffman[FAST_HUFFMAN_TABLE_SIZE];
  550. #endif
  551. uint32 *sorted_codewords;
  552. int *sorted_values;
  553. int sorted_entries;
  554. } Codebook;
  555. typedef struct {
  556. uint8 order;
  557. uint16 rate;
  558. uint16 bark_map_size;
  559. uint8 amplitude_bits;
  560. uint8 amplitude_offset;
  561. uint8 number_of_books;
  562. uint8 book_list[16]; // varies
  563. } Floor0;
  564. typedef struct {
  565. uint8 partitions;
  566. uint8 partition_class_list[32]; // varies
  567. uint8 class_dimensions[16]; // varies
  568. uint8 class_subclasses[16]; // varies
  569. uint8 class_masterbooks[16]; // varies
  570. int16 subclass_books[16][8]; // varies
  571. uint16 Xlist[31 * 8 + 2]; // varies
  572. uint8 sorted_order[31 * 8 + 2];
  573. uint8 neighbors[31 * 8 + 2][2];
  574. uint8 floor1_multiplier;
  575. uint8 rangebits;
  576. int values;
  577. } Floor1;
  578. typedef union {
  579. Floor0 floor0;
  580. Floor1 floor1;
  581. } Floor;
  582. typedef struct {
  583. uint32 begin, end;
  584. uint32 part_size;
  585. uint8 classifications;
  586. uint8 classbook;
  587. uint8 **classdata;
  588. int16(*residue_books)[8];
  589. } Residue;
  590. typedef struct {
  591. uint8 magnitude;
  592. uint8 angle;
  593. uint8 mux;
  594. } MappingChannel;
  595. typedef struct {
  596. uint16 coupling_steps;
  597. MappingChannel *chan;
  598. uint8 submaps;
  599. uint8 submap_floor[15]; // varies
  600. uint8 submap_residue[15]; // varies
  601. } Mapping;
  602. typedef struct {
  603. uint8 blockflag;
  604. uint8 mapping;
  605. uint16 windowtype;
  606. uint16 transformtype;
  607. } Mode;
  608. typedef struct {
  609. uint32 goal_crc; // expected crc if match
  610. int bytes_left; // bytes left in packet
  611. uint32 crc_so_far; // running crc
  612. int bytes_done; // bytes processed in _current_ chunk
  613. uint32 sample_loc; // granule pos encoded in page
  614. } CRCscan;
  615. typedef struct {
  616. uint32 page_start, page_end;
  617. uint32 last_decoded_sample;
  618. } ProbedPage;
  619. struct stb_vorbis {
  620. // user-accessible info
  621. unsigned int sample_rate;
  622. int channels;
  623. unsigned int setup_memory_required;
  624. unsigned int temp_memory_required;
  625. unsigned int setup_temp_memory_required;
  626. // input config
  627. #ifndef STB_VORBIS_NO_STDIO
  628. FILE *f;
  629. uint32 f_start;
  630. int close_on_free;
  631. #endif
  632. uint8 *stream;
  633. uint8 *stream_start;
  634. uint8 *stream_end;
  635. uint32 stream_len;
  636. uint8 push_mode;
  637. uint32 first_audio_page_offset;
  638. ProbedPage p_first, p_last;
  639. // memory management
  640. stb_vorbis_alloc alloc;
  641. int setup_offset;
  642. int temp_offset;
  643. // run-time results
  644. int eof;
  645. enum STBVorbisError error;
  646. // user-useful data
  647. // header info
  648. int blocksize[2];
  649. int blocksize_0, blocksize_1;
  650. int codebook_count;
  651. Codebook *codebooks;
  652. int floor_count;
  653. uint16 floor_types[64]; // varies
  654. Floor *floor_config;
  655. int residue_count;
  656. uint16 residue_types[64]; // varies
  657. Residue *residue_config;
  658. int mapping_count;
  659. Mapping *mapping;
  660. int mode_count;
  661. Mode mode_config[64]; // varies
  662. uint32 total_samples;
  663. // decode buffer
  664. float *channel_buffers[STB_VORBIS_MAX_CHANNELS];
  665. float *outputs[STB_VORBIS_MAX_CHANNELS];
  666. float *previous_window[STB_VORBIS_MAX_CHANNELS];
  667. int previous_length;
  668. #ifndef STB_VORBIS_NO_DEFER_FLOOR
  669. int16 *finalY[STB_VORBIS_MAX_CHANNELS];
  670. #else
  671. float *floor_buffers[STB_VORBIS_MAX_CHANNELS];
  672. #endif
  673. uint32 current_loc; // sample location of next frame to decode
  674. int current_loc_valid;
  675. // per-blocksize precomputed data
  676. // twiddle factors
  677. float *A[2], *B[2], *C[2];
  678. float *window[2];
  679. uint16 *bit_reverse[2];
  680. // current page/packet/segment streaming info
  681. uint32 serial; // stream serial number for verification
  682. int last_page;
  683. int segment_count;
  684. uint8 segments[255];
  685. uint8 page_flag;
  686. uint8 bytes_in_seg;
  687. uint8 first_decode;
  688. int next_seg;
  689. int last_seg; // flag that we're on the last segment
  690. int last_seg_which; // what was the segment number of the last seg?
  691. uint32 acc;
  692. int valid_bits;
  693. int packet_bytes;
  694. int end_seg_with_known_loc;
  695. uint32 known_loc_for_packet;
  696. int discard_samples_deferred;
  697. uint32 samples_output;
  698. // push mode scanning
  699. int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching
  700. #ifndef STB_VORBIS_NO_PUSHDATA_API
  701. CRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT];
  702. #endif
  703. // sample-access
  704. int channel_buffer_start;
  705. int channel_buffer_end;
  706. };
  707. #if defined(STB_VORBIS_NO_PUSHDATA_API)
  708. #define IS_PUSH_MODE(f) FALSE
  709. #elif defined(STB_VORBIS_NO_PULLDATA_API)
  710. #define IS_PUSH_MODE(f) TRUE
  711. #else
  712. #define IS_PUSH_MODE(f) ((f)->push_mode)
  713. #endif
  714. typedef struct stb_vorbis vorb;
  715. static int error(vorb *f, enum STBVorbisError e) {
  716. f->error = e;
  717. if (!f->eof && e != VORBIS_need_more_data) {
  718. f->error = e; // breakpoint for debugging
  719. }
  720. return 0;
  721. }
  722. // these functions are used for allocating temporary memory
  723. // while decoding. if you can afford the stack space, use
  724. // alloca(); otherwise, provide a temp buffer and it will
  725. // allocate out of those.
  726. #define array_size_required(count,size) (count*(sizeof(void *)+(size)))
  727. #define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size))
  728. #ifdef dealloca
  729. #define temp_free(f,p) (f->alloc.alloc_buffer ? 0 : dealloca(size))
  730. #else
  731. #define temp_free(f,p) 0
  732. #endif
  733. #define temp_alloc_save(f) ((f)->temp_offset)
  734. #define temp_alloc_restore(f,p) ((f)->temp_offset = (p))
  735. #define temp_block_array(f,count,size) make_block_array(temp_alloc(f,array_size_required(count,size)), count, size)
  736. // given a sufficiently large block of memory, make an array of pointers to subblocks of it
  737. static void *make_block_array(void *mem, int count, int size) {
  738. int i;
  739. void ** p = (void **) mem;
  740. char *q = (char *) (p + count);
  741. for (i = 0; i < count; ++i) {
  742. p[i] = q;
  743. q += size;
  744. }
  745. return p;
  746. }
  747. static void *setup_malloc(vorb *f, int sz) {
  748. sz = (sz + 3) & ~3;
  749. f->setup_memory_required += sz;
  750. if (f->alloc.alloc_buffer) {
  751. void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;
  752. if (f->setup_offset + sz > f->temp_offset) return NULL;
  753. f->setup_offset += sz;
  754. return p;
  755. }
  756. return sz ? malloc(sz) : NULL;
  757. }
  758. static void setup_free(vorb *f, void *p) {
  759. if (f->alloc.alloc_buffer) return; // do nothing; setup mem is a stack
  760. free(p);
  761. }
  762. static void *setup_temp_malloc(vorb *f, int sz) {
  763. sz = (sz + 3) & ~3;
  764. if (f->alloc.alloc_buffer) {
  765. if (f->temp_offset - sz < f->setup_offset) return NULL;
  766. f->temp_offset -= sz;
  767. return (char *) f->alloc.alloc_buffer + f->temp_offset;
  768. }
  769. return malloc(sz);
  770. }
  771. static void setup_temp_free(vorb *f, void *p, int sz) {
  772. if (f->alloc.alloc_buffer) {
  773. f->temp_offset += (sz + 3)&~3;
  774. return;
  775. }
  776. free(p);
  777. }
  778. #define CRC32_POLY 0x04c11db7 // from spec
  779. static uint32 crc_table[256];
  780. static void crc32_init(void) {
  781. int i, j;
  782. uint32 s;
  783. for (i = 0; i < 256; i++) {
  784. for (s = (uint32) i << 24, j = 0; j < 8; ++j)
  785. s = (s << 1) ^ (s >= (1U << 31) ? CRC32_POLY : 0);
  786. crc_table[i] = s;
  787. }
  788. }
  789. static __forceinline uint32 crc32_update(uint32 crc, uint8 byte) {
  790. return (crc << 8) ^ crc_table[byte ^ (crc >> 24)];
  791. }
  792. // used in setup, and for huffman that doesn't go fast path
  793. static unsigned int bit_reverse(unsigned int n) {
  794. n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
  795. n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
  796. n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);
  797. n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);
  798. return (n >> 16) | (n << 16);
  799. }
  800. static float square(float x) {
  801. return x*x;
  802. }
  803. // this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3
  804. // as required by the specification. fast(?) implementation from stb.h
  805. // @OPTIMIZE: called multiple times per-packet with "constants"; move to setup
  806. static int ilog(int32 n) {
  807. static signed char log2_4[16] = {0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4};
  808. // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29)
  809. if (n < (1 << 14))
  810. if (n < (1 << 4)) return 0 + log2_4[n];
  811. else if (n < (1 << 9)) return 5 + log2_4[n >> 5];
  812. else return 10 + log2_4[n >> 10];
  813. else if (n < (1 << 24))
  814. if (n < (1 << 19)) return 15 + log2_4[n >> 15];
  815. else return 20 + log2_4[n >> 20];
  816. else if (n < (1 << 29)) return 25 + log2_4[n >> 25];
  817. else if (n < (1 << 31)) return 30 + log2_4[n >> 30];
  818. else return 0; // signed n returns 0
  819. }
  820. #ifndef M_PI
  821. #define M_PI 3.14159265358979323846264f // from CRC
  822. #endif
  823. // code length assigned to a value with no huffman encoding
  824. #define NO_CODE 255
  825. /////////////////////// LEAF SETUP FUNCTIONS //////////////////////////
  826. //
  827. // these functions are only called at setup, and only a few times
  828. // per file
  829. static float float32_unpack(uint32 x) {
  830. // from the specification
  831. uint32 mantissa = x & 0x1fffff;
  832. uint32 sign = x & 0x80000000;
  833. uint32 exp = (x & 0x7fe00000) >> 21;
  834. double res = sign ? -(double) mantissa : (double) mantissa;
  835. return (float) ldexp((float) res, exp - 788);
  836. }
  837. // zlib & jpeg huffman tables assume that the output symbols
  838. // can either be arbitrarily arranged, or have monotonically
  839. // increasing frequencies--they rely on the lengths being sorted;
  840. // this makes for a very simple generation algorithm.
  841. // vorbis allows a huffman table with non-sorted lengths. This
  842. // requires a more sophisticated construction, since symbols in
  843. // order do not map to huffman codes "in order".
  844. static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values) {
  845. if (!c->sparse) {
  846. c->codewords[symbol] = huff_code;
  847. } else {
  848. c->codewords[count] = huff_code;
  849. c->codeword_lengths[count] = len;
  850. values[count] = symbol;
  851. }
  852. }
  853. static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values) {
  854. int i, k, m = 0;
  855. uint32 available[32];
  856. memset(available, 0, sizeof(available));
  857. // find the first entry
  858. for (k = 0; k < n; ++k) if (len[k] < NO_CODE) break;
  859. if (k == n) { assert(c->sorted_entries == 0); return TRUE; }
  860. // add to the list
  861. add_entry(c, 0, k, m++, len[k], values);
  862. // add all available leaves
  863. for (i = 1; i <= len[k]; ++i)
  864. available[i] = 1U << (32 - i);
  865. // note that the above code treats the first case specially,
  866. // but it's really the same as the following code, so they
  867. // could probably be combined (except the initial code is 0,
  868. // and I use 0 in available[] to mean 'empty')
  869. for (i = k + 1; i < n; ++i) {
  870. uint32 res;
  871. int z = len[i], y;
  872. if (z == NO_CODE) continue;
  873. // find lowest available leaf (should always be earliest,
  874. // which is what the specification calls for)
  875. // note that this property, and the fact we can never have
  876. // more than one free leaf at a given level, isn't totally
  877. // trivial to prove, but it seems true and the assert never
  878. // fires, so!
  879. while (z > 0 && !available[z]) --z;
  880. if (z == 0) { return FALSE; }
  881. res = available[z];
  882. assert(z >= 0 && z < 32);
  883. available[z] = 0;
  884. add_entry(c, bit_reverse(res), i, m++, len[i], values);
  885. // propogate availability up the tree
  886. if (z != len[i]) {
  887. assert(len[i] >= 0 && len[i] < 32);
  888. for (y = len[i]; y > z; --y) {
  889. assert(available[y] == 0);
  890. available[y] = res + (1 << (32 - y));
  891. }
  892. }
  893. }
  894. return TRUE;
  895. }
  896. // accelerated huffman table allows fast O(1) match of all symbols
  897. // of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH
  898. static void compute_accelerated_huffman(Codebook *c) {
  899. int i, len;
  900. for (i = 0; i < FAST_HUFFMAN_TABLE_SIZE; ++i)
  901. c->fast_huffman[i] = -1;
  902. len = c->sparse ? c->sorted_entries : c->entries;
  903. #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
  904. if (len > 32767) len = 32767; // largest possible value we can encode!
  905. #endif
  906. for (i = 0; i < len; ++i) {
  907. if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) {
  908. uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i];
  909. // set table entries for all bit combinations in the higher bits
  910. while (z < FAST_HUFFMAN_TABLE_SIZE) {
  911. c->fast_huffman[z] = i;
  912. z += 1 << c->codeword_lengths[i];
  913. }
  914. }
  915. }
  916. }
  917. #ifdef _MSC_VER
  918. #define STBV_CDECL __cdecl
  919. #else
  920. #define STBV_CDECL
  921. #endif
  922. static int STBV_CDECL uint32_compare(const void *p, const void *q) {
  923. uint32 x = *(uint32 *) p;
  924. uint32 y = *(uint32 *) q;
  925. return x < y ? -1 : x > y;
  926. }
  927. static int include_in_sort(Codebook *c, uint8 len) {
  928. if (c->sparse) { assert(len != NO_CODE); return TRUE; }
  929. if (len == NO_CODE) return FALSE;
  930. if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE;
  931. return FALSE;
  932. }
  933. // if the fast table above doesn't work, we want to binary
  934. // search them... need to reverse the bits
  935. static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values) {
  936. int i, len;
  937. // build a list of all the entries
  938. // OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN.
  939. // this is kind of a frivolous optimization--I don't see any performance improvement,
  940. // but it's like 4 extra lines of code, so.
  941. if (!c->sparse) {
  942. int k = 0;
  943. for (i = 0; i < c->entries; ++i)
  944. if (include_in_sort(c, lengths[i]))
  945. c->sorted_codewords[k++] = bit_reverse(c->codewords[i]);
  946. assert(k == c->sorted_entries);
  947. } else {
  948. for (i = 0; i < c->sorted_entries; ++i)
  949. c->sorted_codewords[i] = bit_reverse(c->codewords[i]);
  950. }
  951. qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare);
  952. c->sorted_codewords[c->sorted_entries] = 0xffffffff;
  953. len = c->sparse ? c->sorted_entries : c->entries;
  954. // now we need to indicate how they correspond; we could either
  955. // #1: sort a different data structure that says who they correspond to
  956. // #2: for each sorted entry, search the original list to find who corresponds
  957. // #3: for each original entry, find the sorted entry
  958. // #1 requires extra storage, #2 is slow, #3 can use binary search!
  959. for (i = 0; i < len; ++i) {
  960. int huff_len = c->sparse ? lengths[values[i]] : lengths[i];
  961. if (include_in_sort(c, huff_len)) {
  962. uint32 code = bit_reverse(c->codewords[i]);
  963. int x = 0, n = c->sorted_entries;
  964. while (n > 1) {
  965. // invariant: sc[x] <= code < sc[x+n]
  966. int m = x + (n >> 1);
  967. if (c->sorted_codewords[m] <= code) {
  968. x = m;
  969. n -= (n >> 1);
  970. } else {
  971. n >>= 1;
  972. }
  973. }
  974. assert(c->sorted_codewords[x] == code);
  975. if (c->sparse) {
  976. c->sorted_values[x] = values[i];
  977. c->codeword_lengths[x] = huff_len;
  978. } else {
  979. c->sorted_values[x] = i;
  980. }
  981. }
  982. }
  983. }
  984. // only run while parsing the header (3 times)
  985. static int vorbis_validate(uint8 *data) {
  986. static uint8 vorbis[6] = {'v', 'o', 'r', 'b', 'i', 's'};
  987. return memcmp(data, vorbis, 6) == 0;
  988. }
  989. // called from setup only, once per code book
  990. // (formula implied by specification)
  991. static int lookup1_values(int entries, int dim) {
  992. int r = (int) floor(exp((float) log((float) entries) / dim));
  993. if ((int) floor(pow((float) r + 1, dim)) <= entries) // (int) cast for MinGW warning;
  994. ++r; // floor() to avoid _ftol() when non-CRT
  995. assert(pow((float) r + 1, dim) > entries);
  996. assert((int) floor(pow((float) r, dim)) <= entries); // (int),floor() as above
  997. return r;
  998. }
  999. // called twice per file
  1000. static void compute_twiddle_factors(int n, float *A, float *B, float *C) {
  1001. int n4 = n >> 2, n8 = n >> 3;
  1002. int k, k2;
  1003. for (k = k2 = 0; k < n4; ++k, k2 += 2) {
  1004. A[k2] = (float) cos(4 * k*M_PI / n);
  1005. A[k2 + 1] = (float) -sin(4 * k*M_PI / n);
  1006. B[k2] = (float) cos((k2 + 1)*M_PI / n / 2) * 0.5f;
  1007. B[k2 + 1] = (float) sin((k2 + 1)*M_PI / n / 2) * 0.5f;
  1008. }
  1009. for (k = k2 = 0; k < n8; ++k, k2 += 2) {
  1010. C[k2] = (float) cos(2 * (k2 + 1)*M_PI / n);
  1011. C[k2 + 1] = (float) -sin(2 * (k2 + 1)*M_PI / n);
  1012. }
  1013. }
  1014. static void compute_window(int n, float *window) {
  1015. int n2 = n >> 1, i;
  1016. for (i = 0; i < n2; ++i)
  1017. window[i] = (float) sin(0.5 * M_PI * square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI)));
  1018. }
  1019. static void compute_bitreverse(int n, uint16 *rev) {
  1020. int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
  1021. int i, n8 = n >> 3;
  1022. for (i = 0; i < n8; ++i)
  1023. rev[i] = (bit_reverse(i) >> (32 - ld + 3)) << 2;
  1024. }
  1025. static int init_blocksize(vorb *f, int b, int n) {
  1026. int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3;
  1027. f->A[b] = (float *) setup_malloc(f, sizeof(float) * n2);
  1028. f->B[b] = (float *) setup_malloc(f, sizeof(float) * n2);
  1029. f->C[b] = (float *) setup_malloc(f, sizeof(float) * n4);
  1030. if (!f->A[b] || !f->B[b] || !f->C[b]) return error(f, VORBIS_outofmem);
  1031. compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]);
  1032. f->window[b] = (float *) setup_malloc(f, sizeof(float) * n2);
  1033. if (!f->window[b]) return error(f, VORBIS_outofmem);
  1034. compute_window(n, f->window[b]);
  1035. f->bit_reverse[b] = (uint16 *) setup_malloc(f, sizeof(uint16) * n8);
  1036. if (!f->bit_reverse[b]) return error(f, VORBIS_outofmem);
  1037. compute_bitreverse(n, f->bit_reverse[b]);
  1038. return TRUE;
  1039. }
  1040. static void neighbors(uint16 *x, int n, int *plow, int *phigh) {
  1041. int low = -1;
  1042. int high = 65536;
  1043. int i;
  1044. for (i = 0; i < n; ++i) {
  1045. if (x[i] > low && x[i] < x[n]) { *plow = i; low = x[i]; }
  1046. if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; }
  1047. }
  1048. }
  1049. // this has been repurposed so y is now the original index instead of y
  1050. typedef struct {
  1051. uint16 x, y;
  1052. } Point;
  1053. static int STBV_CDECL point_compare(const void *p, const void *q) {
  1054. Point *a = (Point *) p;
  1055. Point *b = (Point *) q;
  1056. return a->x < b->x ? -1 : a->x > b->x;
  1057. }
  1058. //
  1059. /////////////////////// END LEAF SETUP FUNCTIONS //////////////////////////
  1060. #if defined(STB_VORBIS_NO_STDIO)
  1061. #define USE_MEMORY(z) TRUE
  1062. #else
  1063. #define USE_MEMORY(z) ((z)->stream)
  1064. #endif
  1065. static uint8 get8(vorb *z) {
  1066. if (USE_MEMORY(z)) {
  1067. if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
  1068. return *z->stream++;
  1069. }
  1070. #ifndef STB_VORBIS_NO_STDIO
  1071. {
  1072. int c = fgetc(z->f);
  1073. if (c == EOF) { z->eof = TRUE; return 0; }
  1074. return c;
  1075. }
  1076. #endif
  1077. }
  1078. static uint32 get32(vorb *f) {
  1079. uint32 x;
  1080. x = get8(f);
  1081. x += get8(f) << 8;
  1082. x += get8(f) << 16;
  1083. x += (uint32) get8(f) << 24;
  1084. return x;
  1085. }
  1086. static int getn(vorb *z, uint8 *data, int n) {
  1087. if (USE_MEMORY(z)) {
  1088. if (z->stream + n > z->stream_end) { z->eof = 1; return 0; }
  1089. memcpy(data, z->stream, n);
  1090. z->stream += n;
  1091. return 1;
  1092. }
  1093. #ifndef STB_VORBIS_NO_STDIO
  1094. if (fread(data, n, 1, z->f) == 1)
  1095. return 1;
  1096. else {
  1097. z->eof = 1;
  1098. return 0;
  1099. }
  1100. #endif
  1101. }
  1102. static void skip(vorb *z, int n) {
  1103. if (USE_MEMORY(z)) {
  1104. z->stream += n;
  1105. if (z->stream >= z->stream_end) z->eof = 1;
  1106. return;
  1107. }
  1108. #ifndef STB_VORBIS_NO_STDIO
  1109. {
  1110. long x = ftell(z->f);
  1111. fseek(z->f, x + n, SEEK_SET);
  1112. }
  1113. #endif
  1114. }
  1115. static int set_file_offset(stb_vorbis *f, unsigned int loc) {
  1116. #ifndef STB_VORBIS_NO_PUSHDATA_API
  1117. if (f->push_mode) return 0;
  1118. #endif
  1119. f->eof = 0;
  1120. if (USE_MEMORY(f)) {
  1121. if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
  1122. f->stream = f->stream_end;
  1123. f->eof = 1;
  1124. return 0;
  1125. } else {
  1126. f->stream = f->stream_start + loc;
  1127. return 1;
  1128. }
  1129. }
  1130. #ifndef STB_VORBIS_NO_STDIO
  1131. if (loc + f->f_start < loc || loc >= 0x80000000) {
  1132. loc = 0x7fffffff;
  1133. f->eof = 1;
  1134. } else {
  1135. loc += f->f_start;
  1136. }
  1137. if (!fseek(f->f, loc, SEEK_SET))
  1138. return 1;
  1139. f->eof = 1;
  1140. fseek(f->f, f->f_start, SEEK_END);
  1141. return 0;
  1142. #endif
  1143. }
  1144. static uint8 ogg_page_header[4] = {0x4f, 0x67, 0x67, 0x53};
  1145. static int capture_pattern(vorb *f) {
  1146. if (0x4f != get8(f)) return FALSE;
  1147. if (0x67 != get8(f)) return FALSE;
  1148. if (0x67 != get8(f)) return FALSE;
  1149. if (0x53 != get8(f)) return FALSE;
  1150. return TRUE;
  1151. }
  1152. #define PAGEFLAG_continued_packet 1
  1153. #define PAGEFLAG_first_page 2
  1154. #define PAGEFLAG_last_page 4
  1155. static int start_page_no_capturepattern(vorb *f) {
  1156. uint32 loc0, loc1, n;
  1157. // stream structure version
  1158. if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version);
  1159. // header flag
  1160. f->page_flag = get8(f);
  1161. // absolute granule position
  1162. loc0 = get32(f);
  1163. loc1 = get32(f);
  1164. // @TODO: validate loc0,loc1 as valid positions?
  1165. // stream serial number -- vorbis doesn't interleave, so discard
  1166. get32(f);
  1167. //if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number);
  1168. // page sequence number
  1169. n = get32(f);
  1170. f->last_page = n;
  1171. // CRC32
  1172. get32(f);
  1173. // page_segments
  1174. f->segment_count = get8(f);
  1175. if (!getn(f, f->segments, f->segment_count))
  1176. return error(f, VORBIS_unexpected_eof);
  1177. // assume we _don't_ know any the sample position of any segments
  1178. f->end_seg_with_known_loc = -2;
  1179. if (loc0 != ~0U || loc1 != ~0U) {
  1180. int i;
  1181. // determine which packet is the last one that will complete
  1182. for (i = f->segment_count - 1; i >= 0; --i)
  1183. if (f->segments[i] < 255)
  1184. break;
  1185. // 'i' is now the index of the _last_ segment of a packet that ends
  1186. if (i >= 0) {
  1187. f->end_seg_with_known_loc = i;
  1188. f->known_loc_for_packet = loc0;
  1189. }
  1190. }
  1191. if (f->first_decode) {
  1192. int i, len;
  1193. ProbedPage p;
  1194. len = 0;
  1195. for (i = 0; i < f->segment_count; ++i)
  1196. len += f->segments[i];
  1197. len += 27 + f->segment_count;
  1198. p.page_start = f->first_audio_page_offset;
  1199. p.page_end = p.page_start + len;
  1200. p.last_decoded_sample = loc0;
  1201. f->p_first = p;
  1202. }
  1203. f->next_seg = 0;
  1204. return TRUE;
  1205. }
  1206. static int start_page(vorb *f) {
  1207. if (!capture_pattern(f)) return error(f, VORBIS_missing_capture_pattern);
  1208. return start_page_no_capturepattern(f);
  1209. }
  1210. static int start_packet(vorb *f) {
  1211. while (f->next_seg == -1) {
  1212. if (!start_page(f)) return FALSE;
  1213. if (f->page_flag & PAGEFLAG_continued_packet)
  1214. return error(f, VORBIS_continued_packet_flag_invalid);
  1215. }
  1216. f->last_seg = FALSE;
  1217. f->valid_bits = 0;
  1218. f->packet_bytes = 0;
  1219. f->bytes_in_seg = 0;
  1220. // f->next_seg is now valid
  1221. return TRUE;
  1222. }
  1223. static int maybe_start_packet(vorb *f) {
  1224. if (f->next_seg == -1) {
  1225. int x = get8(f);
  1226. if (f->eof) return FALSE; // EOF at page boundary is not an error!
  1227. if (0x4f != x) return error(f, VORBIS_missing_capture_pattern);
  1228. if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
  1229. if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
  1230. if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
  1231. if (!start_page_no_capturepattern(f)) return FALSE;
  1232. if (f->page_flag & PAGEFLAG_continued_packet) {
  1233. // set up enough state that we can read this packet if we want,
  1234. // e.g. during recovery
  1235. f->last_seg = FALSE;
  1236. f->bytes_in_seg = 0;
  1237. return error(f, VORBIS_continued_packet_flag_invalid);
  1238. }
  1239. }
  1240. return start_packet(f);
  1241. }
  1242. static int next_segment(vorb *f) {
  1243. int len;
  1244. if (f->last_seg) return 0;
  1245. if (f->next_seg == -1) {
  1246. f->last_seg_which = f->segment_count - 1; // in case start_page fails
  1247. if (!start_page(f)) { f->last_seg = 1; return 0; }
  1248. if (!(f->page_flag & PAGEFLAG_continued_packet)) return error(f, VORBIS_continued_packet_flag_invalid);
  1249. }
  1250. len = f->segments[f->next_seg++];
  1251. if (len < 255) {
  1252. f->last_seg = TRUE;
  1253. f->last_seg_which = f->next_seg - 1;
  1254. }
  1255. if (f->next_seg >= f->segment_count)
  1256. f->next_seg = -1;
  1257. assert(f->bytes_in_seg == 0);
  1258. f->bytes_in_seg = len;
  1259. return len;
  1260. }
  1261. #define EOP (-1)
  1262. #define INVALID_BITS (-1)
  1263. static int get8_packet_raw(vorb *f) {
  1264. if (!f->bytes_in_seg) { // CLANG!
  1265. if (f->last_seg) return EOP;
  1266. else if (!next_segment(f)) return EOP;
  1267. }
  1268. assert(f->bytes_in_seg > 0);
  1269. --f->bytes_in_seg;
  1270. ++f->packet_bytes;
  1271. return get8(f);
  1272. }
  1273. static int get8_packet(vorb *f) {
  1274. int x = get8_packet_raw(f);
  1275. f->valid_bits = 0;
  1276. return x;
  1277. }
  1278. static void flush_packet(vorb *f) {
  1279. while (get8_packet_raw(f) != EOP);
  1280. }
  1281. // @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important
  1282. // as the huffman decoder?
  1283. static uint32 get_bits(vorb *f, int n) {
  1284. uint32 z;
  1285. if (f->valid_bits < 0) return 0;
  1286. if (f->valid_bits < n) {
  1287. if (n > 24) {
  1288. // the accumulator technique below would not work correctly in this case
  1289. z = get_bits(f, 24);
  1290. z += get_bits(f, n - 24) << 24;
  1291. return z;
  1292. }
  1293. if (f->valid_bits == 0) f->acc = 0;
  1294. while (f->valid_bits < n) {
  1295. int z = get8_packet_raw(f);
  1296. if (z == EOP) {
  1297. f->valid_bits = INVALID_BITS;
  1298. return 0;
  1299. }
  1300. f->acc += z << f->valid_bits;
  1301. f->valid_bits += 8;
  1302. }
  1303. }
  1304. if (f->valid_bits < 0) return 0;
  1305. z = f->acc & ((1 << n) - 1);
  1306. f->acc >>= n;
  1307. f->valid_bits -= n;
  1308. return z;
  1309. }
  1310. // @OPTIMIZE: primary accumulator for huffman
  1311. // expand the buffer to as many bits as possible without reading off end of packet
  1312. // it might be nice to allow f->valid_bits and f->acc to be stored in registers,
  1313. // e.g. cache them locally and decode locally
  1314. static __forceinline void prep_huffman(vorb *f) {
  1315. if (f->valid_bits <= 24) {
  1316. if (f->valid_bits == 0) f->acc = 0;
  1317. do {
  1318. int z;
  1319. if (f->last_seg && !f->bytes_in_seg) return;
  1320. z = get8_packet_raw(f);
  1321. if (z == EOP) return;
  1322. f->acc += (unsigned) z << f->valid_bits;
  1323. f->valid_bits += 8;
  1324. } while (f->valid_bits <= 24);
  1325. }
  1326. }
  1327. enum {
  1328. VORBIS_packet_id = 1,
  1329. VORBIS_packet_comment = 3,
  1330. VORBIS_packet_setup = 5
  1331. };
  1332. static int codebook_decode_scalar_raw(vorb *f, Codebook *c) {
  1333. int i;
  1334. prep_huffman(f);
  1335. if (c->codewords == NULL && c->sorted_codewords == NULL)
  1336. return -1;
  1337. // cases to use binary search: sorted_codewords && !c->codewords
  1338. // sorted_codewords && c->entries > 8
  1339. if (c->entries > 8 ? c->sorted_codewords != NULL : !c->codewords) {
  1340. // binary search
  1341. uint32 code = bit_reverse(f->acc);
  1342. int x = 0, n = c->sorted_entries, len;
  1343. while (n > 1) {
  1344. // invariant: sc[x] <= code < sc[x+n]
  1345. int m = x + (n >> 1);
  1346. if (c->sorted_codewords[m] <= code) {
  1347. x = m;
  1348. n -= (n >> 1);
  1349. } else {
  1350. n >>= 1;
  1351. }
  1352. }
  1353. // x is now the sorted index
  1354. if (!c->sparse) x = c->sorted_values[x];
  1355. // x is now sorted index if sparse, or symbol otherwise
  1356. len = c->codeword_lengths[x];
  1357. if (f->valid_bits >= len) {
  1358. f->acc >>= len;
  1359. f->valid_bits -= len;
  1360. return x;
  1361. }
  1362. f->valid_bits = 0;
  1363. return -1;
  1364. }
  1365. // if small, linear search
  1366. assert(!c->sparse);
  1367. for (i = 0; i < c->entries; ++i) {
  1368. if (c->codeword_lengths[i] == NO_CODE) continue;
  1369. if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i]) - 1))) {
  1370. if (f->valid_bits >= c->codeword_lengths[i]) {
  1371. f->acc >>= c->codeword_lengths[i];
  1372. f->valid_bits -= c->codeword_lengths[i];
  1373. return i;
  1374. }
  1375. f->valid_bits = 0;
  1376. return -1;
  1377. }
  1378. }
  1379. error(f, VORBIS_invalid_stream);
  1380. f->valid_bits = 0;
  1381. return -1;
  1382. }
  1383. #ifndef STB_VORBIS_NO_INLINE_DECODE
  1384. #define DECODE_RAW(var, f,c) \
  1385. if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \
  1386. prep_huffman(f); \
  1387. var = f->acc & FAST_HUFFMAN_TABLE_MASK; \
  1388. var = c->fast_huffman[var]; \
  1389. if (var >= 0) { \
  1390. int n = c->codeword_lengths[var]; \
  1391. f->acc >>= n; \
  1392. f->valid_bits -= n; \
  1393. if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \
  1394. } else { \
  1395. var = codebook_decode_scalar_raw(f,c); \
  1396. }
  1397. #else
  1398. static int codebook_decode_scalar(vorb *f, Codebook *c) {
  1399. int i;
  1400. if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)
  1401. prep_huffman(f);
  1402. // fast huffman table lookup
  1403. i = f->acc & FAST_HUFFMAN_TABLE_MASK;
  1404. i = c->fast_huffman[i];
  1405. if (i >= 0) {
  1406. f->acc >>= c->codeword_lengths[i];
  1407. f->valid_bits -= c->codeword_lengths[i];
  1408. if (f->valid_bits < 0) { f->valid_bits = 0; return -1; }
  1409. return i;
  1410. }
  1411. return codebook_decode_scalar_raw(f, c);
  1412. }
  1413. #define DECODE_RAW(var,f,c) var = codebook_decode_scalar(f,c);
  1414. #endif
  1415. #define DECODE(var,f,c) \
  1416. DECODE_RAW(var,f,c) \
  1417. if (c->sparse) var = c->sorted_values[var];
  1418. #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1419. #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c)
  1420. #else
  1421. #define DECODE_VQ(var,f,c) DECODE(var,f,c)
  1422. #endif
  1423. // CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case
  1424. // where we avoid one addition
  1425. #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off])
  1426. #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off])
  1427. #define CODEBOOK_ELEMENT_BASE(c) (0)
  1428. static int codebook_decode_start(vorb *f, Codebook *c) {
  1429. int z = -1;
  1430. // type 0 is only legal in a scalar context
  1431. if (c->lookup_type == 0)
  1432. error(f, VORBIS_invalid_stream);
  1433. else {
  1434. DECODE_VQ(z, f, c);
  1435. if (c->sparse) assert(z < c->sorted_entries);
  1436. if (z < 0) { // check for EOP
  1437. if (!f->bytes_in_seg)
  1438. if (f->last_seg)
  1439. return z;
  1440. error(f, VORBIS_invalid_stream);
  1441. }
  1442. }
  1443. return z;
  1444. }
  1445. static int codebook_decode(vorb *f, Codebook *c, float *output, int len) {
  1446. int i, z = codebook_decode_start(f, c);
  1447. if (z < 0) return FALSE;
  1448. if (len > c->dimensions) len = c->dimensions;
  1449. #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1450. if (c->lookup_type == 1) {
  1451. float last = CODEBOOK_ELEMENT_BASE(c);
  1452. int div = 1;
  1453. for (i = 0; i < len; ++i) {
  1454. int off = (z / div) % c->lookup_values;
  1455. float val = CODEBOOK_ELEMENT_FAST(c, off) + last;
  1456. output[i] += val;
  1457. if (c->sequence_p) last = val + c->minimum_value;
  1458. div *= c->lookup_values;
  1459. }
  1460. return TRUE;
  1461. }
  1462. #endif
  1463. z *= c->dimensions;
  1464. if (c->sequence_p) {
  1465. float last = CODEBOOK_ELEMENT_BASE(c);
  1466. for (i = 0; i < len; ++i) {
  1467. float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
  1468. output[i] += val;
  1469. last = val + c->minimum_value;
  1470. }
  1471. } else {
  1472. float last = CODEBOOK_ELEMENT_BASE(c);
  1473. for (i = 0; i < len; ++i) {
  1474. output[i] += CODEBOOK_ELEMENT_FAST(c, z + i) + last;
  1475. }
  1476. }
  1477. return TRUE;
  1478. }
  1479. static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step) {
  1480. int i, z = codebook_decode_start(f, c);
  1481. float last = CODEBOOK_ELEMENT_BASE(c);
  1482. if (z < 0) return FALSE;
  1483. if (len > c->dimensions) len = c->dimensions;
  1484. #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1485. if (c->lookup_type == 1) {
  1486. int div = 1;
  1487. for (i = 0; i < len; ++i) {
  1488. int off = (z / div) % c->lookup_values;
  1489. float val = CODEBOOK_ELEMENT_FAST(c, off) + last;
  1490. output[i*step] += val;
  1491. if (c->sequence_p) last = val;
  1492. div *= c->lookup_values;
  1493. }
  1494. return TRUE;
  1495. }
  1496. #endif
  1497. z *= c->dimensions;
  1498. for (i = 0; i < len; ++i) {
  1499. float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
  1500. output[i*step] += val;
  1501. if (c->sequence_p) last = val;
  1502. }
  1503. return TRUE;
  1504. }
  1505. static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **outputs, int ch, int *c_inter_p, int *p_inter_p, int len, int total_decode) {
  1506. int c_inter = *c_inter_p;
  1507. int p_inter = *p_inter_p;
  1508. int i, z, effective = c->dimensions;
  1509. // type 0 is only legal in a scalar context
  1510. if (c->lookup_type == 0) return error(f, VORBIS_invalid_stream);
  1511. while (total_decode > 0) {
  1512. float last = CODEBOOK_ELEMENT_BASE(c);
  1513. DECODE_VQ(z, f, c);
  1514. #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1515. assert(!c->sparse || z < c->sorted_entries);
  1516. #endif
  1517. if (z < 0) {
  1518. if (!f->bytes_in_seg)
  1519. if (f->last_seg) return FALSE;
  1520. return error(f, VORBIS_invalid_stream);
  1521. }
  1522. // if this will take us off the end of the buffers, stop short!
  1523. // we check by computing the length of the virtual interleaved
  1524. // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),
  1525. // and the length we'll be using (effective)
  1526. if (c_inter + p_inter*ch + effective > len * ch) {
  1527. effective = len*ch - (p_inter*ch - c_inter);
  1528. }
  1529. #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1530. if (c->lookup_type == 1) {
  1531. int div = 1;
  1532. for (i = 0; i < effective; ++i) {
  1533. int off = (z / div) % c->lookup_values;
  1534. float val = CODEBOOK_ELEMENT_FAST(c, off) + last;
  1535. if (outputs[c_inter])
  1536. outputs[c_inter][p_inter] += val;
  1537. if (++c_inter == ch) { c_inter = 0; ++p_inter; }
  1538. if (c->sequence_p) last = val;
  1539. div *= c->lookup_values;
  1540. }
  1541. } else
  1542. #endif
  1543. {
  1544. z *= c->dimensions;
  1545. if (c->sequence_p) {
  1546. for (i = 0; i < effective; ++i) {
  1547. float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
  1548. if (outputs[c_inter])
  1549. outputs[c_inter][p_inter] += val;
  1550. if (++c_inter == ch) { c_inter = 0; ++p_inter; }
  1551. last = val;
  1552. }
  1553. } else {
  1554. for (i = 0; i < effective; ++i) {
  1555. float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
  1556. if (outputs[c_inter])
  1557. outputs[c_inter][p_inter] += val;
  1558. if (++c_inter == ch) { c_inter = 0; ++p_inter; }
  1559. }
  1560. }
  1561. }
  1562. total_decode -= effective;
  1563. }
  1564. *c_inter_p = c_inter;
  1565. *p_inter_p = p_inter;
  1566. return TRUE;
  1567. }
  1568. static int predict_point(int x, int x0, int x1, int y0, int y1) {
  1569. int dy = y1 - y0;
  1570. int adx = x1 - x0;
  1571. // @OPTIMIZE: force int division to round in the right direction... is this necessary on x86?
  1572. int err = abs(dy) * (x - x0);
  1573. int off = err / adx;
  1574. return dy < 0 ? y0 - off : y0 + off;
  1575. }
  1576. // the following table is block-copied from the specification
  1577. static float inverse_db_table[256] =
  1578. {
  1579. 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,
  1580. 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,
  1581. 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f,
  1582. 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f,
  1583. 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f,
  1584. 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f,
  1585. 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f,
  1586. 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f,
  1587. 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f,
  1588. 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f,
  1589. 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f,
  1590. 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f,
  1591. 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f,
  1592. 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f,
  1593. 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f,
  1594. 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f,
  1595. 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f,
  1596. 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f,
  1597. 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f,
  1598. 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f,
  1599. 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f,
  1600. 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f,
  1601. 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f,
  1602. 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f,
  1603. 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f,
  1604. 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f,
  1605. 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f,
  1606. 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f,
  1607. 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f,
  1608. 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f,
  1609. 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f,
  1610. 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f,
  1611. 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f,
  1612. 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f,
  1613. 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f,
  1614. 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f,
  1615. 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f,
  1616. 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f,
  1617. 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f,
  1618. 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f,
  1619. 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f,
  1620. 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f,
  1621. 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f,
  1622. 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f,
  1623. 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f,
  1624. 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f,
  1625. 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f,
  1626. 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f,
  1627. 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f,
  1628. 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f,
  1629. 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f,
  1630. 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f,
  1631. 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f,
  1632. 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f,
  1633. 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f,
  1634. 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f,
  1635. 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f,
  1636. 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f,
  1637. 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f,
  1638. 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f,
  1639. 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f,
  1640. 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f,
  1641. 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f,
  1642. 0.82788260f, 0.88168307f, 0.9389798f, 1.0f
  1643. };
  1644. // @OPTIMIZE: if you want to replace this bresenham line-drawing routine,
  1645. // note that you must produce bit-identical output to decode correctly;
  1646. // this specific sequence of operations is specified in the spec (it's
  1647. // drawing integer-quantized frequency-space lines that the encoder
  1648. // expects to be exactly the same)
  1649. // ... also, isn't the whole point of Bresenham's algorithm to NOT
  1650. // have to divide in the setup? sigh.
  1651. #ifndef STB_VORBIS_NO_DEFER_FLOOR
  1652. #define LINE_OP(a,b) a *= b
  1653. #else
  1654. #define LINE_OP(a,b) a = b
  1655. #endif
  1656. #ifdef STB_VORBIS_DIVIDE_TABLE
  1657. #define DIVTAB_NUMER 32
  1658. #define DIVTAB_DENOM 64
  1659. int8 integer_divide_table[DIVTAB_NUMER][DIVTAB_DENOM]; // 2KB
  1660. #endif
  1661. static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y1, int n) {
  1662. int dy = y1 - y0;
  1663. int adx = x1 - x0;
  1664. int ady = abs(dy);
  1665. int base;
  1666. int x = x0, y = y0;
  1667. int err = 0;
  1668. int sy;
  1669. #ifdef STB_VORBIS_DIVIDE_TABLE
  1670. if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) {
  1671. if (dy < 0) {
  1672. base = -integer_divide_table[ady][adx];
  1673. sy = base - 1;
  1674. } else {
  1675. base = integer_divide_table[ady][adx];
  1676. sy = base + 1;
  1677. }
  1678. } else {
  1679. base = dy / adx;
  1680. if (dy < 0)
  1681. sy = base - 1;
  1682. else
  1683. sy = base + 1;
  1684. }
  1685. #else
  1686. base = dy / adx;
  1687. if (dy < 0)
  1688. sy = base - 1;
  1689. else
  1690. sy = base + 1;
  1691. #endif
  1692. ady -= abs(base) * adx;
  1693. if (x1 > n) x1 = n;
  1694. if (x < x1) {
  1695. LINE_OP(output[x], inverse_db_table[y]);
  1696. for (++x; x < x1; ++x) {
  1697. err += ady;
  1698. if (err >= adx) {
  1699. err -= adx;
  1700. y += sy;
  1701. } else
  1702. y += base;
  1703. LINE_OP(output[x], inverse_db_table[y]);
  1704. }
  1705. }
  1706. }
  1707. static int residue_decode(vorb *f, Codebook *book, float *target, int offset, int n, int rtype) {
  1708. int k;
  1709. if (rtype == 0) {
  1710. int step = n / book->dimensions;
  1711. for (k = 0; k < step; ++k)
  1712. if (!codebook_decode_step(f, book, target + offset + k, n - offset - k, step))
  1713. return FALSE;
  1714. } else {
  1715. for (k = 0; k < n; ) {
  1716. if (!codebook_decode(f, book, target + offset, n - k))
  1717. return FALSE;
  1718. k += book->dimensions;
  1719. offset += book->dimensions;
  1720. }
  1721. }
  1722. return TRUE;
  1723. }
  1724. static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int rn, uint8 *do_not_decode) {
  1725. int i, j, pass;
  1726. Residue *r = f->residue_config + rn;
  1727. int rtype = f->residue_types[rn];
  1728. int c = r->classbook;
  1729. int classwords = f->codebooks[c].dimensions;
  1730. int n_read = r->end - r->begin;
  1731. int part_read = n_read / r->part_size;
  1732. int temp_alloc_point = temp_alloc_save(f);
  1733. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1734. uint8 ***part_classdata = (uint8 ***) temp_block_array(f, f->channels, part_read * sizeof(**part_classdata));
  1735. #else
  1736. int **classifications = (int **) temp_block_array(f, f->channels, part_read * sizeof(**classifications));
  1737. #endif
  1738. CHECK(f);
  1739. for (i = 0; i < ch; ++i)
  1740. if (!do_not_decode[i])
  1741. memset(residue_buffers[i], 0, sizeof(float) * n);
  1742. if (rtype == 2 && ch != 1) {
  1743. for (j = 0; j < ch; ++j)
  1744. if (!do_not_decode[j])
  1745. break;
  1746. if (j == ch)
  1747. goto done;
  1748. for (pass = 0; pass < 8; ++pass) {
  1749. int pcount = 0, class_set = 0;
  1750. if (ch == 2) {
  1751. while (pcount < part_read) {
  1752. int z = r->begin + pcount*r->part_size;
  1753. int c_inter = (z & 1), p_inter = z >> 1;
  1754. if (pass == 0) {
  1755. Codebook *c = f->codebooks + r->classbook;
  1756. int q;
  1757. DECODE(q, f, c);
  1758. if (q == EOP) goto done;
  1759. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1760. part_classdata[0][class_set] = r->classdata[q];
  1761. #else
  1762. for (i = classwords - 1; i >= 0; --i) {
  1763. classifications[0][i + pcount] = q % r->classifications;
  1764. q /= r->classifications;
  1765. }
  1766. #endif
  1767. }
  1768. for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
  1769. int z = r->begin + pcount*r->part_size;
  1770. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1771. int c = part_classdata[0][class_set][i];
  1772. #else
  1773. int c = classifications[0][pcount];
  1774. #endif
  1775. int b = r->residue_books[c][pass];
  1776. if (b >= 0) {
  1777. Codebook *book = f->codebooks + b;
  1778. #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1779. if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
  1780. goto done;
  1781. #else
  1782. // saves 1%
  1783. if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
  1784. goto done;
  1785. #endif
  1786. } else {
  1787. z += r->part_size;
  1788. c_inter = z & 1;
  1789. p_inter = z >> 1;
  1790. }
  1791. }
  1792. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1793. ++class_set;
  1794. #endif
  1795. }
  1796. } else if (ch == 1) {
  1797. while (pcount < part_read) {
  1798. int z = r->begin + pcount*r->part_size;
  1799. int c_inter = 0, p_inter = z;
  1800. if (pass == 0) {
  1801. Codebook *c = f->codebooks + r->classbook;
  1802. int q;
  1803. DECODE(q, f, c);
  1804. if (q == EOP) goto done;
  1805. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1806. part_classdata[0][class_set] = r->classdata[q];
  1807. #else
  1808. for (i = classwords - 1; i >= 0; --i) {
  1809. classifications[0][i + pcount] = q % r->classifications;
  1810. q /= r->classifications;
  1811. }
  1812. #endif
  1813. }
  1814. for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
  1815. int z = r->begin + pcount*r->part_size;
  1816. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1817. int c = part_classdata[0][class_set][i];
  1818. #else
  1819. int c = classifications[0][pcount];
  1820. #endif
  1821. int b = r->residue_books[c][pass];
  1822. if (b >= 0) {
  1823. Codebook *book = f->codebooks + b;
  1824. if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
  1825. goto done;
  1826. } else {
  1827. z += r->part_size;
  1828. c_inter = 0;
  1829. p_inter = z;
  1830. }
  1831. }
  1832. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1833. ++class_set;
  1834. #endif
  1835. }
  1836. } else {
  1837. while (pcount < part_read) {
  1838. int z = r->begin + pcount*r->part_size;
  1839. int c_inter = z % ch, p_inter = z / ch;
  1840. if (pass == 0) {
  1841. Codebook *c = f->codebooks + r->classbook;
  1842. int q;
  1843. DECODE(q, f, c);
  1844. if (q == EOP) goto done;
  1845. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1846. part_classdata[0][class_set] = r->classdata[q];
  1847. #else
  1848. for (i = classwords - 1; i >= 0; --i) {
  1849. classifications[0][i + pcount] = q % r->classifications;
  1850. q /= r->classifications;
  1851. }
  1852. #endif
  1853. }
  1854. for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
  1855. int z = r->begin + pcount*r->part_size;
  1856. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1857. int c = part_classdata[0][class_set][i];
  1858. #else
  1859. int c = classifications[0][pcount];
  1860. #endif
  1861. int b = r->residue_books[c][pass];
  1862. if (b >= 0) {
  1863. Codebook *book = f->codebooks + b;
  1864. if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
  1865. goto done;
  1866. } else {
  1867. z += r->part_size;
  1868. c_inter = z % ch;
  1869. p_inter = z / ch;
  1870. }
  1871. }
  1872. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1873. ++class_set;
  1874. #endif
  1875. }
  1876. }
  1877. }
  1878. goto done;
  1879. }
  1880. CHECK(f);
  1881. for (pass = 0; pass < 8; ++pass) {
  1882. int pcount = 0, class_set = 0;
  1883. while (pcount < part_read) {
  1884. if (pass == 0) {
  1885. for (j = 0; j < ch; ++j) {
  1886. if (!do_not_decode[j]) {
  1887. Codebook *c = f->codebooks + r->classbook;
  1888. int temp;
  1889. DECODE(temp, f, c);
  1890. if (temp == EOP) goto done;
  1891. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1892. part_classdata[j][class_set] = r->classdata[temp];
  1893. #else
  1894. for (i = classwords - 1; i >= 0; --i) {
  1895. classifications[j][i + pcount] = temp % r->classifications;
  1896. temp /= r->classifications;
  1897. }
  1898. #endif
  1899. }
  1900. }
  1901. }
  1902. for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
  1903. for (j = 0; j < ch; ++j) {
  1904. if (!do_not_decode[j]) {
  1905. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1906. int c = part_classdata[j][class_set][i];
  1907. #else
  1908. int c = classifications[j][pcount];
  1909. #endif
  1910. int b = r->residue_books[c][pass];
  1911. if (b >= 0) {
  1912. float *target = residue_buffers[j];
  1913. int offset = r->begin + pcount * r->part_size;
  1914. int n = r->part_size;
  1915. Codebook *book = f->codebooks + b;
  1916. if (!residue_decode(f, book, target, offset, n, rtype))
  1917. goto done;
  1918. }
  1919. }
  1920. }
  1921. }
  1922. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1923. ++class_set;
  1924. #endif
  1925. }
  1926. }
  1927. done:
  1928. CHECK(f);
  1929. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  1930. temp_free(f, part_classdata);
  1931. #else
  1932. temp_free(f, classifications);
  1933. #endif
  1934. temp_alloc_restore(f, temp_alloc_point);
  1935. }
  1936. #if 0
  1937. // slow way for debugging
  1938. void inverse_mdct_slow(float *buffer, int n) {
  1939. int i, j;
  1940. int n2 = n >> 1;
  1941. float *x = (float *) malloc(sizeof(*x) * n2);
  1942. memcpy(x, buffer, sizeof(*x) * n2);
  1943. for (i = 0; i < n; ++i) {
  1944. float acc = 0;
  1945. for (j = 0; j < n2; ++j)
  1946. // formula from paper:
  1947. //acc += n/4.0f * x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1));
  1948. // formula from wikipedia
  1949. //acc += 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5));
  1950. // these are equivalent, except the formula from the paper inverts the multiplier!
  1951. // however, what actually works is NO MULTIPLIER!?!
  1952. //acc += 64 * 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5));
  1953. acc += x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n / 2.0)*(2 * j + 1));
  1954. buffer[i] = acc;
  1955. }
  1956. free(x);
  1957. }
  1958. #elif 0
  1959. // same as above, but just barely able to run in real time on modern machines
  1960. void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) {
  1961. float mcos[16384];
  1962. int i, j;
  1963. int n2 = n >> 1, nmask = (n << 2) - 1;
  1964. float *x = (float *) malloc(sizeof(*x) * n2);
  1965. memcpy(x, buffer, sizeof(*x) * n2);
  1966. for (i = 0; i < 4 * n; ++i)
  1967. mcos[i] = (float) cos(M_PI / 2 * i / n);
  1968. for (i = 0; i < n; ++i) {
  1969. float acc = 0;
  1970. for (j = 0; j < n2; ++j)
  1971. acc += x[j] * mcos[(2 * i + 1 + n2)*(2 * j + 1) & nmask];
  1972. buffer[i] = acc;
  1973. }
  1974. free(x);
  1975. }
  1976. #elif 0
  1977. // transform to use a slow dct-iv; this is STILL basically trivial,
  1978. // but only requires half as many ops
  1979. void dct_iv_slow(float *buffer, int n) {
  1980. float mcos[16384];
  1981. float x[2048];
  1982. int i, j;
  1983. int n2 = n >> 1, nmask = (n << 3) - 1;
  1984. memcpy(x, buffer, sizeof(*x) * n);
  1985. for (i = 0; i < 8 * n; ++i)
  1986. mcos[i] = (float) cos(M_PI / 4 * i / n);
  1987. for (i = 0; i < n; ++i) {
  1988. float acc = 0;
  1989. for (j = 0; j < n; ++j)
  1990. acc += x[j] * mcos[((2 * i + 1)*(2 * j + 1)) & nmask];
  1991. buffer[i] = acc;
  1992. }
  1993. }
  1994. void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) {
  1995. int i, n4 = n >> 2, n2 = n >> 1, n3_4 = n - n4;
  1996. float temp[4096];
  1997. memcpy(temp, buffer, n2 * sizeof(float));
  1998. dct_iv_slow(temp, n2); // returns -c'-d, a-b'
  1999. for (i = 0; i < n4; ++i) buffer[i] = temp[i + n4]; // a-b'
  2000. for (; i < n3_4; ++i) buffer[i] = -temp[n3_4 - i - 1]; // b-a', c+d'
  2001. for (; i < n; ++i) buffer[i] = -temp[i - n3_4]; // c'+d
  2002. }
  2003. #endif
  2004. #ifndef LIBVORBIS_MDCT
  2005. #define LIBVORBIS_MDCT 0
  2006. #endif
  2007. #if LIBVORBIS_MDCT
  2008. // directly call the vorbis MDCT using an interface documented
  2009. // by Jeff Roberts... useful for performance comparison
  2010. typedef struct {
  2011. int n;
  2012. int log2n;
  2013. float *trig;
  2014. int *bitrev;
  2015. float scale;
  2016. } mdct_lookup;
  2017. extern void mdct_init(mdct_lookup *lookup, int n);
  2018. extern void mdct_clear(mdct_lookup *l);
  2019. extern void mdct_backward(mdct_lookup *init, float *in, float *out);
  2020. mdct_lookup M1, M2;
  2021. void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) {
  2022. mdct_lookup *M;
  2023. if (M1.n == n) M = &M1;
  2024. else if (M2.n == n) M = &M2;
  2025. else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; } else {
  2026. if (M2.n) __asm int 3;
  2027. mdct_init(&M2, n);
  2028. M = &M2;
  2029. }
  2030. mdct_backward(M, buffer, buffer);
  2031. }
  2032. #endif
  2033. // the following were split out into separate functions while optimizing;
  2034. // they could be pushed back up but eh. __forceinline showed no change;
  2035. // they're probably already being inlined.
  2036. static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A) {
  2037. float *ee0 = e + i_off;
  2038. float *ee2 = ee0 + k_off;
  2039. int i;
  2040. assert((n & 3) == 0);
  2041. for (i = (n >> 2); i > 0; --i) {
  2042. float k00_20, k01_21;
  2043. k00_20 = ee0[0] - ee2[0];
  2044. k01_21 = ee0[-1] - ee2[-1];
  2045. ee0[0] += ee2[0];//ee0[ 0] = ee0[ 0] + ee2[ 0];
  2046. ee0[-1] += ee2[-1];//ee0[-1] = ee0[-1] + ee2[-1];
  2047. ee2[0] = k00_20 * A[0] - k01_21 * A[1];
  2048. ee2[-1] = k01_21 * A[0] + k00_20 * A[1];
  2049. A += 8;
  2050. k00_20 = ee0[-2] - ee2[-2];
  2051. k01_21 = ee0[-3] - ee2[-3];
  2052. ee0[-2] += ee2[-2];//ee0[-2] = ee0[-2] + ee2[-2];
  2053. ee0[-3] += ee2[-3];//ee0[-3] = ee0[-3] + ee2[-3];
  2054. ee2[-2] = k00_20 * A[0] - k01_21 * A[1];
  2055. ee2[-3] = k01_21 * A[0] + k00_20 * A[1];
  2056. A += 8;
  2057. k00_20 = ee0[-4] - ee2[-4];
  2058. k01_21 = ee0[-5] - ee2[-5];
  2059. ee0[-4] += ee2[-4];//ee0[-4] = ee0[-4] + ee2[-4];
  2060. ee0[-5] += ee2[-5];//ee0[-5] = ee0[-5] + ee2[-5];
  2061. ee2[-4] = k00_20 * A[0] - k01_21 * A[1];
  2062. ee2[-5] = k01_21 * A[0] + k00_20 * A[1];
  2063. A += 8;
  2064. k00_20 = ee0[-6] - ee2[-6];
  2065. k01_21 = ee0[-7] - ee2[-7];
  2066. ee0[-6] += ee2[-6];//ee0[-6] = ee0[-6] + ee2[-6];
  2067. ee0[-7] += ee2[-7];//ee0[-7] = ee0[-7] + ee2[-7];
  2068. ee2[-6] = k00_20 * A[0] - k01_21 * A[1];
  2069. ee2[-7] = k01_21 * A[0] + k00_20 * A[1];
  2070. A += 8;
  2071. ee0 -= 8;
  2072. ee2 -= 8;
  2073. }
  2074. }
  2075. static void imdct_step3_inner_r_loop(int lim, float *e, int d0, int k_off, float *A, int k1) {
  2076. int i;
  2077. float k00_20, k01_21;
  2078. float *e0 = e + d0;
  2079. float *e2 = e0 + k_off;
  2080. for (i = lim >> 2; i > 0; --i) {
  2081. k00_20 = e0[-0] - e2[-0];
  2082. k01_21 = e0[-1] - e2[-1];
  2083. e0[-0] += e2[-0];//e0[-0] = e0[-0] + e2[-0];
  2084. e0[-1] += e2[-1];//e0[-1] = e0[-1] + e2[-1];
  2085. e2[-0] = (k00_20) *A[0] - (k01_21) * A[1];
  2086. e2[-1] = (k01_21) *A[0] + (k00_20) * A[1];
  2087. A += k1;
  2088. k00_20 = e0[-2] - e2[-2];
  2089. k01_21 = e0[-3] - e2[-3];
  2090. e0[-2] += e2[-2];//e0[-2] = e0[-2] + e2[-2];
  2091. e0[-3] += e2[-3];//e0[-3] = e0[-3] + e2[-3];
  2092. e2[-2] = (k00_20) *A[0] - (k01_21) * A[1];
  2093. e2[-3] = (k01_21) *A[0] + (k00_20) * A[1];
  2094. A += k1;
  2095. k00_20 = e0[-4] - e2[-4];
  2096. k01_21 = e0[-5] - e2[-5];
  2097. e0[-4] += e2[-4];//e0[-4] = e0[-4] + e2[-4];
  2098. e0[-5] += e2[-5];//e0[-5] = e0[-5] + e2[-5];
  2099. e2[-4] = (k00_20) *A[0] - (k01_21) * A[1];
  2100. e2[-5] = (k01_21) *A[0] + (k00_20) * A[1];
  2101. A += k1;
  2102. k00_20 = e0[-6] - e2[-6];
  2103. k01_21 = e0[-7] - e2[-7];
  2104. e0[-6] += e2[-6];//e0[-6] = e0[-6] + e2[-6];
  2105. e0[-7] += e2[-7];//e0[-7] = e0[-7] + e2[-7];
  2106. e2[-6] = (k00_20) *A[0] - (k01_21) * A[1];
  2107. e2[-7] = (k01_21) *A[0] + (k00_20) * A[1];
  2108. e0 -= 8;
  2109. e2 -= 8;
  2110. A += k1;
  2111. }
  2112. }
  2113. static void imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, float *A, int a_off, int k0) {
  2114. int i;
  2115. float A0 = A[0];
  2116. float A1 = A[0 + 1];
  2117. float A2 = A[0 + a_off];
  2118. float A3 = A[0 + a_off + 1];
  2119. float A4 = A[0 + a_off * 2 + 0];
  2120. float A5 = A[0 + a_off * 2 + 1];
  2121. float A6 = A[0 + a_off * 3 + 0];
  2122. float A7 = A[0 + a_off * 3 + 1];
  2123. float k00, k11;
  2124. float *ee0 = e + i_off;
  2125. float *ee2 = ee0 + k_off;
  2126. for (i = n; i > 0; --i) {
  2127. k00 = ee0[0] - ee2[0];
  2128. k11 = ee0[-1] - ee2[-1];
  2129. ee0[0] = ee0[0] + ee2[0];
  2130. ee0[-1] = ee0[-1] + ee2[-1];
  2131. ee2[0] = (k00) * A0 - (k11) * A1;
  2132. ee2[-1] = (k11) * A0 + (k00) * A1;
  2133. k00 = ee0[-2] - ee2[-2];
  2134. k11 = ee0[-3] - ee2[-3];
  2135. ee0[-2] = ee0[-2] + ee2[-2];
  2136. ee0[-3] = ee0[-3] + ee2[-3];
  2137. ee2[-2] = (k00) * A2 - (k11) * A3;
  2138. ee2[-3] = (k11) * A2 + (k00) * A3;
  2139. k00 = ee0[-4] - ee2[-4];
  2140. k11 = ee0[-5] - ee2[-5];
  2141. ee0[-4] = ee0[-4] + ee2[-4];
  2142. ee0[-5] = ee0[-5] + ee2[-5];
  2143. ee2[-4] = (k00) * A4 - (k11) * A5;
  2144. ee2[-5] = (k11) * A4 + (k00) * A5;
  2145. k00 = ee0[-6] - ee2[-6];
  2146. k11 = ee0[-7] - ee2[-7];
  2147. ee0[-6] = ee0[-6] + ee2[-6];
  2148. ee0[-7] = ee0[-7] + ee2[-7];
  2149. ee2[-6] = (k00) * A6 - (k11) * A7;
  2150. ee2[-7] = (k11) * A6 + (k00) * A7;
  2151. ee0 -= k0;
  2152. ee2 -= k0;
  2153. }
  2154. }
  2155. static __forceinline void iter_54(float *z) {
  2156. float k00, k11, k22, k33;
  2157. float y0, y1, y2, y3;
  2158. k00 = z[0] - z[-4];
  2159. y0 = z[0] + z[-4];
  2160. y2 = z[-2] + z[-6];
  2161. k22 = z[-2] - z[-6];
  2162. z[-0] = y0 + y2; // z0 + z4 + z2 + z6
  2163. z[-2] = y0 - y2; // z0 + z4 - z2 - z6
  2164. // done with y0,y2
  2165. k33 = z[-3] - z[-7];
  2166. z[-4] = k00 + k33; // z0 - z4 + z3 - z7
  2167. z[-6] = k00 - k33; // z0 - z4 - z3 + z7
  2168. // done with k33
  2169. k11 = z[-1] - z[-5];
  2170. y1 = z[-1] + z[-5];
  2171. y3 = z[-3] + z[-7];
  2172. z[-1] = y1 + y3; // z1 + z5 + z3 + z7
  2173. z[-3] = y1 - y3; // z1 + z5 - z3 - z7
  2174. z[-5] = k11 - k22; // z1 - z5 + z2 - z6
  2175. z[-7] = k11 + k22; // z1 - z5 - z2 + z6
  2176. }
  2177. static void imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A, int base_n) {
  2178. int a_off = base_n >> 3;
  2179. float A2 = A[0 + a_off];
  2180. float *z = e + i_off;
  2181. float *base = z - 16 * n;
  2182. while (z > base) {
  2183. float k00, k11;
  2184. k00 = z[-0] - z[-8];
  2185. k11 = z[-1] - z[-9];
  2186. z[-0] = z[-0] + z[-8];
  2187. z[-1] = z[-1] + z[-9];
  2188. z[-8] = k00;
  2189. z[-9] = k11;
  2190. k00 = z[-2] - z[-10];
  2191. k11 = z[-3] - z[-11];
  2192. z[-2] = z[-2] + z[-10];
  2193. z[-3] = z[-3] + z[-11];
  2194. z[-10] = (k00 + k11) * A2;
  2195. z[-11] = (k11 - k00) * A2;
  2196. k00 = z[-12] - z[-4]; // reverse to avoid a unary negation
  2197. k11 = z[-5] - z[-13];
  2198. z[-4] = z[-4] + z[-12];
  2199. z[-5] = z[-5] + z[-13];
  2200. z[-12] = k11;
  2201. z[-13] = k00;
  2202. k00 = z[-14] - z[-6]; // reverse to avoid a unary negation
  2203. k11 = z[-7] - z[-15];
  2204. z[-6] = z[-6] + z[-14];
  2205. z[-7] = z[-7] + z[-15];
  2206. z[-14] = (k00 + k11) * A2;
  2207. z[-15] = (k00 - k11) * A2;
  2208. iter_54(z);
  2209. iter_54(z - 8);
  2210. z -= 16;
  2211. }
  2212. }
  2213. static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) {
  2214. int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
  2215. int ld;
  2216. // @OPTIMIZE: reduce register pressure by using fewer variables?
  2217. int save_point = temp_alloc_save(f);
  2218. float *buf2 = (float *) temp_alloc(f, n2 * sizeof(*buf2));
  2219. float *u = NULL, *v = NULL;
  2220. // twiddle factors
  2221. float *A = f->A[blocktype];
  2222. // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
  2223. // See notes about bugs in that paper in less-optimal implementation 'inverse_mdct_old' after this function.
  2224. // kernel from paper
  2225. // merged:
  2226. // copy and reflect spectral data
  2227. // step 0
  2228. // note that it turns out that the items added together during
  2229. // this step are, in fact, being added to themselves (as reflected
  2230. // by step 0). inexplicable inefficiency! this became obvious
  2231. // once I combined the passes.
  2232. // so there's a missing 'times 2' here (for adding X to itself).
  2233. // this propogates through linearly to the end, where the numbers
  2234. // are 1/2 too small, and need to be compensated for.
  2235. {
  2236. float *d, *e, *AA, *e_stop;
  2237. d = &buf2[n2 - 2];
  2238. AA = A;
  2239. e = &buffer[0];
  2240. e_stop = &buffer[n2];
  2241. while (e != e_stop) {
  2242. d[1] = (e[0] * AA[0] - e[2] * AA[1]);
  2243. d[0] = (e[0] * AA[1] + e[2] * AA[0]);
  2244. d -= 2;
  2245. AA += 2;
  2246. e += 4;
  2247. }
  2248. e = &buffer[n2 - 3];
  2249. while (d >= buf2) {
  2250. d[1] = (-e[2] * AA[0] - -e[0] * AA[1]);
  2251. d[0] = (-e[2] * AA[1] + -e[0] * AA[0]);
  2252. d -= 2;
  2253. AA += 2;
  2254. e -= 4;
  2255. }
  2256. }
  2257. // now we use symbolic names for these, so that we can
  2258. // possibly swap their meaning as we change which operations
  2259. // are in place
  2260. u = buffer;
  2261. v = buf2;
  2262. // step 2 (paper output is w, now u)
  2263. // this could be in place, but the data ends up in the wrong
  2264. // place... _somebody_'s got to swap it, so this is nominated
  2265. {
  2266. float *AA = &A[n2 - 8];
  2267. float *d0, *d1, *e0, *e1;
  2268. e0 = &v[n4];
  2269. e1 = &v[0];
  2270. d0 = &u[n4];
  2271. d1 = &u[0];
  2272. while (AA >= A) {
  2273. float v40_20, v41_21;
  2274. v41_21 = e0[1] - e1[1];
  2275. v40_20 = e0[0] - e1[0];
  2276. d0[1] = e0[1] + e1[1];
  2277. d0[0] = e0[0] + e1[0];
  2278. d1[1] = v41_21*AA[4] - v40_20*AA[5];
  2279. d1[0] = v40_20*AA[4] + v41_21*AA[5];
  2280. v41_21 = e0[3] - e1[3];
  2281. v40_20 = e0[2] - e1[2];
  2282. d0[3] = e0[3] + e1[3];
  2283. d0[2] = e0[2] + e1[2];
  2284. d1[3] = v41_21*AA[0] - v40_20*AA[1];
  2285. d1[2] = v40_20*AA[0] + v41_21*AA[1];
  2286. AA -= 8;
  2287. d0 += 4;
  2288. d1 += 4;
  2289. e0 += 4;
  2290. e1 += 4;
  2291. }
  2292. }
  2293. // step 3
  2294. ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
  2295. // optimized step 3:
  2296. // the original step3 loop can be nested r inside s or s inside r;
  2297. // it's written originally as s inside r, but this is dumb when r
  2298. // iterates many times, and s few. So I have two copies of it and
  2299. // switch between them halfway.
  2300. // this is iteration 0 of step 3
  2301. imdct_step3_iter0_loop(n >> 4, u, n2 - 1 - n4 * 0, -(n >> 3), A);
  2302. imdct_step3_iter0_loop(n >> 4, u, n2 - 1 - n4 * 1, -(n >> 3), A);
  2303. // this is iteration 1 of step 3
  2304. imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 0, -(n >> 4), A, 16);
  2305. imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 1, -(n >> 4), A, 16);
  2306. imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 2, -(n >> 4), A, 16);
  2307. imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 3, -(n >> 4), A, 16);
  2308. l = 2;
  2309. for (; l < (ld - 3) >> 1; ++l) {
  2310. int k0 = n >> (l + 2), k0_2 = k0 >> 1;
  2311. int lim = 1 << (l + 1);
  2312. int i;
  2313. for (i = 0; i < lim; ++i)
  2314. imdct_step3_inner_r_loop(n >> (l + 4), u, n2 - 1 - k0*i, -k0_2, A, 1 << (l + 3));
  2315. }
  2316. for (; l < ld - 6; ++l) {
  2317. int k0 = n >> (l + 2), k1 = 1 << (l + 3), k0_2 = k0 >> 1;
  2318. int rlim = n >> (l + 6), r;
  2319. int lim = 1 << (l + 1);
  2320. int i_off;
  2321. float *A0 = A;
  2322. i_off = n2 - 1;
  2323. for (r = rlim; r > 0; --r) {
  2324. imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, k1, k0);
  2325. A0 += k1 * 4;
  2326. i_off -= 8;
  2327. }
  2328. }
  2329. // iterations with count:
  2330. // ld-6,-5,-4 all interleaved together
  2331. // the big win comes from getting rid of needless flops
  2332. // due to the constants on pass 5 & 4 being all 1 and 0;
  2333. // combining them to be simultaneous to improve cache made little difference
  2334. imdct_step3_inner_s_loop_ld654(n >> 5, u, n2 - 1, A, n);
  2335. // output is u
  2336. // step 4, 5, and 6
  2337. // cannot be in-place because of step 5
  2338. {
  2339. uint16 *bitrev = f->bit_reverse[blocktype];
  2340. // weirdly, I'd have thought reading sequentially and writing
  2341. // erratically would have been better than vice-versa, but in
  2342. // fact that's not what my testing showed. (That is, with
  2343. // j = bitreverse(i), do you read i and write j, or read j and write i.)
  2344. float *d0 = &v[n4 - 4];
  2345. float *d1 = &v[n2 - 4];
  2346. while (d0 >= v) {
  2347. int k4;
  2348. k4 = bitrev[0];
  2349. d1[3] = u[k4 + 0];
  2350. d1[2] = u[k4 + 1];
  2351. d0[3] = u[k4 + 2];
  2352. d0[2] = u[k4 + 3];
  2353. k4 = bitrev[1];
  2354. d1[1] = u[k4 + 0];
  2355. d1[0] = u[k4 + 1];
  2356. d0[1] = u[k4 + 2];
  2357. d0[0] = u[k4 + 3];
  2358. d0 -= 4;
  2359. d1 -= 4;
  2360. bitrev += 2;
  2361. }
  2362. }
  2363. // (paper output is u, now v)
  2364. // data must be in buf2
  2365. assert(v == buf2);
  2366. // step 7 (paper output is v, now v)
  2367. // this is now in place
  2368. {
  2369. float *C = f->C[blocktype];
  2370. float *d, *e;
  2371. d = v;
  2372. e = v + n2 - 4;
  2373. while (d < e) {
  2374. float a02, a11, b0, b1, b2, b3;
  2375. a02 = d[0] - e[2];
  2376. a11 = d[1] + e[3];
  2377. b0 = C[1] * a02 + C[0] * a11;
  2378. b1 = C[1] * a11 - C[0] * a02;
  2379. b2 = d[0] + e[2];
  2380. b3 = d[1] - e[3];
  2381. d[0] = b2 + b0;
  2382. d[1] = b3 + b1;
  2383. e[2] = b2 - b0;
  2384. e[3] = b1 - b3;
  2385. a02 = d[2] - e[0];
  2386. a11 = d[3] + e[1];
  2387. b0 = C[3] * a02 + C[2] * a11;
  2388. b1 = C[3] * a11 - C[2] * a02;
  2389. b2 = d[2] + e[0];
  2390. b3 = d[3] - e[1];
  2391. d[2] = b2 + b0;
  2392. d[3] = b3 + b1;
  2393. e[0] = b2 - b0;
  2394. e[1] = b1 - b3;
  2395. C += 4;
  2396. d += 4;
  2397. e -= 4;
  2398. }
  2399. }
  2400. // data must be in buf2
  2401. // step 8+decode (paper output is X, now buffer)
  2402. // this generates pairs of data a la 8 and pushes them directly through
  2403. // the decode kernel (pushing rather than pulling) to avoid having
  2404. // to make another pass later
  2405. // this cannot POSSIBLY be in place, so we refer to the buffers directly
  2406. {
  2407. float *d0, *d1, *d2, *d3;
  2408. float *B = f->B[blocktype] + n2 - 8;
  2409. float *e = buf2 + n2 - 8;
  2410. d0 = &buffer[0];
  2411. d1 = &buffer[n2 - 4];
  2412. d2 = &buffer[n2];
  2413. d3 = &buffer[n - 4];
  2414. while (e >= v) {
  2415. float p0, p1, p2, p3;
  2416. p3 = e[6] * B[7] - e[7] * B[6];
  2417. p2 = -e[6] * B[6] - e[7] * B[7];
  2418. d0[0] = p3;
  2419. d1[3] = -p3;
  2420. d2[0] = p2;
  2421. d3[3] = p2;
  2422. p1 = e[4] * B[5] - e[5] * B[4];
  2423. p0 = -e[4] * B[4] - e[5] * B[5];
  2424. d0[1] = p1;
  2425. d1[2] = -p1;
  2426. d2[1] = p0;
  2427. d3[2] = p0;
  2428. p3 = e[2] * B[3] - e[3] * B[2];
  2429. p2 = -e[2] * B[2] - e[3] * B[3];
  2430. d0[2] = p3;
  2431. d1[1] = -p3;
  2432. d2[2] = p2;
  2433. d3[1] = p2;
  2434. p1 = e[0] * B[1] - e[1] * B[0];
  2435. p0 = -e[0] * B[0] - e[1] * B[1];
  2436. d0[3] = p1;
  2437. d1[0] = -p1;
  2438. d2[3] = p0;
  2439. d3[0] = p0;
  2440. B -= 8;
  2441. e -= 8;
  2442. d0 += 4;
  2443. d2 += 4;
  2444. d1 -= 4;
  2445. d3 -= 4;
  2446. }
  2447. }
  2448. temp_free(f, buf2);
  2449. temp_alloc_restore(f, save_point);
  2450. }
  2451. #if 0
  2452. // this is the original version of the above code, if you want to optimize it from scratch
  2453. void inverse_mdct_naive(float *buffer, int n) {
  2454. float s;
  2455. float A[1 << 12], B[1 << 12], C[1 << 11];
  2456. int i, k, k2, k4, n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
  2457. int n3_4 = n - n4, ld;
  2458. // how can they claim this only uses N words?!
  2459. // oh, because they're only used sparsely, whoops
  2460. float u[1 << 13], X[1 << 13], v[1 << 13], w[1 << 13];
  2461. // set up twiddle factors
  2462. for (k = k2 = 0; k < n4; ++k, k2 += 2) {
  2463. A[k2] = (float) cos(4 * k*M_PI / n);
  2464. A[k2 + 1] = (float) -sin(4 * k*M_PI / n);
  2465. B[k2] = (float) cos((k2 + 1)*M_PI / n / 2);
  2466. B[k2 + 1] = (float) sin((k2 + 1)*M_PI / n / 2);
  2467. }
  2468. for (k = k2 = 0; k < n8; ++k, k2 += 2) {
  2469. C[k2] = (float) cos(2 * (k2 + 1)*M_PI / n);
  2470. C[k2 + 1] = (float) -sin(2 * (k2 + 1)*M_PI / n);
  2471. }
  2472. // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
  2473. // Note there are bugs in that pseudocode, presumably due to them attempting
  2474. // to rename the arrays nicely rather than representing the way their actual
  2475. // implementation bounces buffers back and forth. As a result, even in the
  2476. // "some formulars corrected" version, a direct implementation fails. These
  2477. // are noted below as "paper bug".
  2478. // copy and reflect spectral data
  2479. for (k = 0; k < n2; ++k) u[k] = buffer[k];
  2480. for (; k < n; ++k) u[k] = -buffer[n - k - 1];
  2481. // kernel from paper
  2482. // step 1
  2483. for (k = k2 = k4 = 0; k < n4; k += 1, k2 += 2, k4 += 4) {
  2484. v[n - k4 - 1] = (u[k4] - u[n - k4 - 1]) * A[k2] - (u[k4 + 2] - u[n - k4 - 3])*A[k2 + 1];
  2485. v[n - k4 - 3] = (u[k4] - u[n - k4 - 1]) * A[k2 + 1] + (u[k4 + 2] - u[n - k4 - 3])*A[k2];
  2486. }
  2487. // step 2
  2488. for (k = k4 = 0; k < n8; k += 1, k4 += 4) {
  2489. w[n2 + 3 + k4] = v[n2 + 3 + k4] + v[k4 + 3];
  2490. w[n2 + 1 + k4] = v[n2 + 1 + k4] + v[k4 + 1];
  2491. w[k4 + 3] = (v[n2 + 3 + k4] - v[k4 + 3])*A[n2 - 4 - k4] - (v[n2 + 1 + k4] - v[k4 + 1])*A[n2 - 3 - k4];
  2492. w[k4 + 1] = (v[n2 + 1 + k4] - v[k4 + 1])*A[n2 - 4 - k4] + (v[n2 + 3 + k4] - v[k4 + 3])*A[n2 - 3 - k4];
  2493. }
  2494. // step 3
  2495. ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
  2496. for (l = 0; l < ld - 3; ++l) {
  2497. int k0 = n >> (l + 2), k1 = 1 << (l + 3);
  2498. int rlim = n >> (l + 4), r4, r;
  2499. int s2lim = 1 << (l + 2), s2;
  2500. for (r = r4 = 0; r < rlim; r4 += 4, ++r) {
  2501. for (s2 = 0; s2 < s2lim; s2 += 2) {
  2502. u[n - 1 - k0*s2 - r4] = w[n - 1 - k0*s2 - r4] + w[n - 1 - k0*(s2 + 1) - r4];
  2503. u[n - 3 - k0*s2 - r4] = w[n - 3 - k0*s2 - r4] + w[n - 3 - k0*(s2 + 1) - r4];
  2504. u[n - 1 - k0*(s2 + 1) - r4] = (w[n - 1 - k0*s2 - r4] - w[n - 1 - k0*(s2 + 1) - r4]) * A[r*k1]
  2505. - (w[n - 3 - k0*s2 - r4] - w[n - 3 - k0*(s2 + 1) - r4]) * A[r*k1 + 1];
  2506. u[n - 3 - k0*(s2 + 1) - r4] = (w[n - 3 - k0*s2 - r4] - w[n - 3 - k0*(s2 + 1) - r4]) * A[r*k1]
  2507. + (w[n - 1 - k0*s2 - r4] - w[n - 1 - k0*(s2 + 1) - r4]) * A[r*k1 + 1];
  2508. }
  2509. }
  2510. if (l + 1 < ld - 3) {
  2511. // paper bug: ping-ponging of u&w here is omitted
  2512. memcpy(w, u, sizeof(u));
  2513. }
  2514. }
  2515. // step 4
  2516. for (i = 0; i < n8; ++i) {
  2517. int j = bit_reverse(i) >> (32 - ld + 3);
  2518. assert(j < n8);
  2519. if (i == j) {
  2520. // paper bug: original code probably swapped in place; if copying,
  2521. // need to directly copy in this case
  2522. int i8 = i << 3;
  2523. v[i8 + 1] = u[i8 + 1];
  2524. v[i8 + 3] = u[i8 + 3];
  2525. v[i8 + 5] = u[i8 + 5];
  2526. v[i8 + 7] = u[i8 + 7];
  2527. } else if (i < j) {
  2528. int i8 = i << 3, j8 = j << 3;
  2529. v[j8 + 1] = u[i8 + 1], v[i8 + 1] = u[j8 + 1];
  2530. v[j8 + 3] = u[i8 + 3], v[i8 + 3] = u[j8 + 3];
  2531. v[j8 + 5] = u[i8 + 5], v[i8 + 5] = u[j8 + 5];
  2532. v[j8 + 7] = u[i8 + 7], v[i8 + 7] = u[j8 + 7];
  2533. }
  2534. }
  2535. // step 5
  2536. for (k = 0; k < n2; ++k) {
  2537. w[k] = v[k * 2 + 1];
  2538. }
  2539. // step 6
  2540. for (k = k2 = k4 = 0; k < n8; ++k, k2 += 2, k4 += 4) {
  2541. u[n - 1 - k2] = w[k4];
  2542. u[n - 2 - k2] = w[k4 + 1];
  2543. u[n3_4 - 1 - k2] = w[k4 + 2];
  2544. u[n3_4 - 2 - k2] = w[k4 + 3];
  2545. }
  2546. // step 7
  2547. for (k = k2 = 0; k < n8; ++k, k2 += 2) {
  2548. v[n2 + k2] = (u[n2 + k2] + u[n - 2 - k2] + C[k2 + 1] * (u[n2 + k2] - u[n - 2 - k2]) + C[k2] * (u[n2 + k2 + 1] + u[n - 2 - k2 + 1])) / 2;
  2549. v[n - 2 - k2] = (u[n2 + k2] + u[n - 2 - k2] - C[k2 + 1] * (u[n2 + k2] - u[n - 2 - k2]) - C[k2] * (u[n2 + k2 + 1] + u[n - 2 - k2 + 1])) / 2;
  2550. v[n2 + 1 + k2] = (u[n2 + 1 + k2] - u[n - 1 - k2] + C[k2 + 1] * (u[n2 + 1 + k2] + u[n - 1 - k2]) - C[k2] * (u[n2 + k2] - u[n - 2 - k2])) / 2;
  2551. v[n - 1 - k2] = (-u[n2 + 1 + k2] + u[n - 1 - k2] + C[k2 + 1] * (u[n2 + 1 + k2] + u[n - 1 - k2]) - C[k2] * (u[n2 + k2] - u[n - 2 - k2])) / 2;
  2552. }
  2553. // step 8
  2554. for (k = k2 = 0; k < n4; ++k, k2 += 2) {
  2555. X[k] = v[k2 + n2] * B[k2] + v[k2 + 1 + n2] * B[k2 + 1];
  2556. X[n2 - 1 - k] = v[k2 + n2] * B[k2 + 1] - v[k2 + 1 + n2] * B[k2];
  2557. }
  2558. // decode kernel to output
  2559. // determined the following value experimentally
  2560. // (by first figuring out what made inverse_mdct_slow work); then matching that here
  2561. // (probably vorbis encoder premultiplies by n or n/2, to save it on the decoder?)
  2562. s = 0.5; // theoretically would be n4
  2563. // [[[ note! the s value of 0.5 is compensated for by the B[] in the current code,
  2564. // so it needs to use the "old" B values to behave correctly, or else
  2565. // set s to 1.0 ]]]
  2566. for (i = 0; i < n4; ++i) buffer[i] = s * X[i + n4];
  2567. for (; i < n3_4; ++i) buffer[i] = -s * X[n3_4 - i - 1];
  2568. for (; i < n; ++i) buffer[i] = -s * X[i - n3_4];
  2569. }
  2570. #endif
  2571. static float *get_window(vorb *f, int len) {
  2572. len <<= 1;
  2573. if (len == f->blocksize_0) return f->window[0];
  2574. if (len == f->blocksize_1) return f->window[1];
  2575. assert(0);
  2576. return NULL;
  2577. }
  2578. #ifndef STB_VORBIS_NO_DEFER_FLOOR
  2579. typedef int16 YTYPE;
  2580. #else
  2581. typedef int YTYPE;
  2582. #endif
  2583. static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *finalY, uint8 *step2_flag) {
  2584. int n2 = n >> 1;
  2585. int s = map->chan[i].mux, floor;
  2586. floor = map->submap_floor[s];
  2587. if (f->floor_types[floor] == 0) {
  2588. return error(f, VORBIS_invalid_stream);
  2589. } else {
  2590. Floor1 *g = &f->floor_config[floor].floor1;
  2591. int j, q;
  2592. int lx = 0, ly = finalY[0] * g->floor1_multiplier;
  2593. for (q = 1; q < g->values; ++q) {
  2594. j = g->sorted_order[q];
  2595. #ifndef STB_VORBIS_NO_DEFER_FLOOR
  2596. if (finalY[j] >= 0)
  2597. #else
  2598. if (step2_flag[j])
  2599. #endif
  2600. {
  2601. int hy = finalY[j] * g->floor1_multiplier;
  2602. int hx = g->Xlist[j];
  2603. if (lx != hx)
  2604. draw_line(target, lx, ly, hx, hy, n2);
  2605. CHECK(f);
  2606. lx = hx, ly = hy;
  2607. }
  2608. }
  2609. if (lx < n2) {
  2610. // optimization of: draw_line(target, lx,ly, n,ly, n2);
  2611. for (j = lx; j < n2; ++j)
  2612. LINE_OP(target[j], inverse_db_table[ly]);
  2613. CHECK(f);
  2614. }
  2615. }
  2616. return TRUE;
  2617. }
  2618. // The meaning of "left" and "right"
  2619. //
  2620. // For a given frame:
  2621. // we compute samples from 0..n
  2622. // window_center is n/2
  2623. // we'll window and mix the samples from left_start to left_end with data from the previous frame
  2624. // all of the samples from left_end to right_start can be output without mixing; however,
  2625. // this interval is 0-length except when transitioning between short and long frames
  2626. // all of the samples from right_start to right_end need to be mixed with the next frame,
  2627. // which we don't have, so those get saved in a buffer
  2628. // frame N's right_end-right_start, the number of samples to mix with the next frame,
  2629. // has to be the same as frame N+1's left_end-left_start (which they are by
  2630. // construction)
  2631. static int vorbis_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) {
  2632. Mode *m;
  2633. int i, n, prev, next, window_center;
  2634. f->channel_buffer_start = f->channel_buffer_end = 0;
  2635. retry:
  2636. if (f->eof) return FALSE;
  2637. if (!maybe_start_packet(f))
  2638. return FALSE;
  2639. // check packet type
  2640. if (get_bits(f, 1) != 0) {
  2641. if (IS_PUSH_MODE(f))
  2642. return error(f, VORBIS_bad_packet_type);
  2643. while (EOP != get8_packet(f));
  2644. goto retry;
  2645. }
  2646. if (f->alloc.alloc_buffer)
  2647. assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
  2648. i = get_bits(f, ilog(f->mode_count - 1));
  2649. if (i == EOP) return FALSE;
  2650. if (i >= f->mode_count) return FALSE;
  2651. *mode = i;
  2652. m = f->mode_config + i;
  2653. if (m->blockflag) {
  2654. n = f->blocksize_1;
  2655. prev = get_bits(f, 1);
  2656. next = get_bits(f, 1);
  2657. } else {
  2658. prev = next = 0;
  2659. n = f->blocksize_0;
  2660. }
  2661. // WINDOWING
  2662. window_center = n >> 1;
  2663. if (m->blockflag && !prev) {
  2664. *p_left_start = (n - f->blocksize_0) >> 2;
  2665. *p_left_end = (n + f->blocksize_0) >> 2;
  2666. } else {
  2667. *p_left_start = 0;
  2668. *p_left_end = window_center;
  2669. }
  2670. if (m->blockflag && !next) {
  2671. *p_right_start = (n * 3 - f->blocksize_0) >> 2;
  2672. *p_right_end = (n * 3 + f->blocksize_0) >> 2;
  2673. } else {
  2674. *p_right_start = window_center;
  2675. *p_right_end = n;
  2676. }
  2677. return TRUE;
  2678. }
  2679. static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, int left_end, int right_start, int right_end, int *p_left) {
  2680. Mapping *map;
  2681. int i, j, k, n, n2;
  2682. int zero_channel[256];
  2683. int really_zero_channel[256];
  2684. // WINDOWING
  2685. n = f->blocksize[m->blockflag];
  2686. map = &f->mapping[m->mapping];
  2687. // FLOORS
  2688. n2 = n >> 1;
  2689. CHECK(f);
  2690. for (i = 0; i < f->channels; ++i) {
  2691. int s = map->chan[i].mux, floor;
  2692. zero_channel[i] = FALSE;
  2693. floor = map->submap_floor[s];
  2694. if (f->floor_types[floor] == 0) {
  2695. return error(f, VORBIS_invalid_stream);
  2696. } else {
  2697. Floor1 *g = &f->floor_config[floor].floor1;
  2698. if (get_bits(f, 1)) {
  2699. short *finalY;
  2700. uint8 step2_flag[256];
  2701. static int range_list[4] = {256, 128, 86, 64};
  2702. int range = range_list[g->floor1_multiplier - 1];
  2703. int offset = 2;
  2704. finalY = f->finalY[i];
  2705. finalY[0] = get_bits(f, ilog(range) - 1);
  2706. finalY[1] = get_bits(f, ilog(range) - 1);
  2707. for (j = 0; j < g->partitions; ++j) {
  2708. int pclass = g->partition_class_list[j];
  2709. int cdim = g->class_dimensions[pclass];
  2710. int cbits = g->class_subclasses[pclass];
  2711. int csub = (1 << cbits) - 1;
  2712. int cval = 0;
  2713. if (cbits) {
  2714. Codebook *c = f->codebooks + g->class_masterbooks[pclass];
  2715. DECODE(cval, f, c);
  2716. }
  2717. for (k = 0; k < cdim; ++k) {
  2718. int book = g->subclass_books[pclass][cval & csub];
  2719. cval = cval >> cbits;
  2720. if (book >= 0) {
  2721. int temp;
  2722. Codebook *c = f->codebooks + book;
  2723. DECODE(temp, f, c);
  2724. finalY[offset++] = temp;
  2725. } else
  2726. finalY[offset++] = 0;
  2727. }
  2728. }
  2729. if (f->valid_bits == INVALID_BITS) goto error; // behavior according to spec
  2730. step2_flag[0] = step2_flag[1] = 1;
  2731. for (j = 2; j < g->values; ++j) {
  2732. int low, high, pred, highroom, lowroom, room, val;
  2733. low = g->neighbors[j][0];
  2734. high = g->neighbors[j][1];
  2735. //neighbors(g->Xlist, j, &low, &high);
  2736. pred = predict_point(g->Xlist[j], g->Xlist[low], g->Xlist[high], finalY[low], finalY[high]);
  2737. val = finalY[j];
  2738. highroom = range - pred;
  2739. lowroom = pred;
  2740. if (highroom < lowroom)
  2741. room = highroom * 2;
  2742. else
  2743. room = lowroom * 2;
  2744. if (val) {
  2745. step2_flag[low] = step2_flag[high] = 1;
  2746. step2_flag[j] = 1;
  2747. if (val >= room)
  2748. if (highroom > lowroom)
  2749. finalY[j] = val - lowroom + pred;
  2750. else
  2751. finalY[j] = pred - val + highroom - 1;
  2752. else
  2753. if (val & 1)
  2754. finalY[j] = pred - ((val + 1) >> 1);
  2755. else
  2756. finalY[j] = pred + (val >> 1);
  2757. } else {
  2758. step2_flag[j] = 0;
  2759. finalY[j] = pred;
  2760. }
  2761. }
  2762. #ifdef STB_VORBIS_NO_DEFER_FLOOR
  2763. do_floor(f, map, i, n, f->floor_buffers[i], finalY, step2_flag);
  2764. #else
  2765. // defer final floor computation until _after_ residue
  2766. for (j = 0; j < g->values; ++j) {
  2767. if (!step2_flag[j])
  2768. finalY[j] = -1;
  2769. }
  2770. #endif
  2771. } else {
  2772. error:
  2773. zero_channel[i] = TRUE;
  2774. }
  2775. // So we just defer everything else to later
  2776. // at this point we've decoded the floor into buffer
  2777. }
  2778. }
  2779. CHECK(f);
  2780. // at this point we've decoded all floors
  2781. if (f->alloc.alloc_buffer)
  2782. assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
  2783. // re-enable coupled channels if necessary
  2784. memcpy(really_zero_channel, zero_channel, sizeof(really_zero_channel[0]) * f->channels);
  2785. for (i = 0; i < map->coupling_steps; ++i)
  2786. if (!zero_channel[map->chan[i].magnitude] || !zero_channel[map->chan[i].angle]) {
  2787. zero_channel[map->chan[i].magnitude] = zero_channel[map->chan[i].angle] = FALSE;
  2788. }
  2789. CHECK(f);
  2790. // RESIDUE DECODE
  2791. for (i = 0; i < map->submaps; ++i) {
  2792. float *residue_buffers[STB_VORBIS_MAX_CHANNELS];
  2793. int r;
  2794. uint8 do_not_decode[256];
  2795. int ch = 0;
  2796. for (j = 0; j < f->channels; ++j) {
  2797. if (map->chan[j].mux == i) {
  2798. if (zero_channel[j]) {
  2799. do_not_decode[ch] = TRUE;
  2800. residue_buffers[ch] = NULL;
  2801. } else {
  2802. do_not_decode[ch] = FALSE;
  2803. residue_buffers[ch] = f->channel_buffers[j];
  2804. }
  2805. ++ch;
  2806. }
  2807. }
  2808. r = map->submap_residue[i];
  2809. decode_residue(f, residue_buffers, ch, n2, r, do_not_decode);
  2810. }
  2811. if (f->alloc.alloc_buffer)
  2812. assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
  2813. CHECK(f);
  2814. // INVERSE COUPLING
  2815. for (i = map->coupling_steps - 1; i >= 0; --i) {
  2816. int n2 = n >> 1;
  2817. float *m = f->channel_buffers[map->chan[i].magnitude];
  2818. float *a = f->channel_buffers[map->chan[i].angle];
  2819. for (j = 0; j < n2; ++j) {
  2820. float a2, m2;
  2821. if (m[j] > 0)
  2822. if (a[j] > 0)
  2823. m2 = m[j], a2 = m[j] - a[j];
  2824. else
  2825. a2 = m[j], m2 = m[j] + a[j];
  2826. else
  2827. if (a[j] > 0)
  2828. m2 = m[j], a2 = m[j] + a[j];
  2829. else
  2830. a2 = m[j], m2 = m[j] - a[j];
  2831. m[j] = m2;
  2832. a[j] = a2;
  2833. }
  2834. }
  2835. CHECK(f);
  2836. // finish decoding the floors
  2837. #ifndef STB_VORBIS_NO_DEFER_FLOOR
  2838. for (i = 0; i < f->channels; ++i) {
  2839. if (really_zero_channel[i]) {
  2840. memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
  2841. } else {
  2842. do_floor(f, map, i, n, f->channel_buffers[i], f->finalY[i], NULL);
  2843. }
  2844. }
  2845. #else
  2846. for (i = 0; i < f->channels; ++i) {
  2847. if (really_zero_channel[i]) {
  2848. memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
  2849. } else {
  2850. for (j = 0; j < n2; ++j)
  2851. f->channel_buffers[i][j] *= f->floor_buffers[i][j];
  2852. }
  2853. }
  2854. #endif
  2855. // INVERSE MDCT
  2856. CHECK(f);
  2857. for (i = 0; i < f->channels; ++i)
  2858. inverse_mdct(f->channel_buffers[i], n, f, m->blockflag);
  2859. CHECK(f);
  2860. // this shouldn't be necessary, unless we exited on an error
  2861. // and want to flush to get to the next packet
  2862. flush_packet(f);
  2863. if (f->first_decode) {
  2864. // assume we start so first non-discarded sample is sample 0
  2865. // this isn't to spec, but spec would require us to read ahead
  2866. // and decode the size of all current frames--could be done,
  2867. // but presumably it's not a commonly used feature
  2868. f->current_loc = -n2; // start of first frame is positioned for discard
  2869. // we might have to discard samples "from" the next frame too,
  2870. // if we're lapping a large block then a small at the start?
  2871. f->discard_samples_deferred = n - right_end;
  2872. f->current_loc_valid = TRUE;
  2873. f->first_decode = FALSE;
  2874. } else if (f->discard_samples_deferred) {
  2875. if (f->discard_samples_deferred >= right_start - left_start) {
  2876. f->discard_samples_deferred -= (right_start - left_start);
  2877. left_start = right_start;
  2878. *p_left = left_start;
  2879. } else {
  2880. left_start += f->discard_samples_deferred;
  2881. *p_left = left_start;
  2882. f->discard_samples_deferred = 0;
  2883. }
  2884. } else if (f->previous_length == 0 && f->current_loc_valid) {
  2885. // we're recovering from a seek... that means we're going to discard
  2886. // the samples from this packet even though we know our position from
  2887. // the last page header, so we need to update the position based on
  2888. // the discarded samples here
  2889. // but wait, the code below is going to add this in itself even
  2890. // on a discard, so we don't need to do it here...
  2891. }
  2892. // check if we have ogg information about the sample # for this packet
  2893. if (f->last_seg_which == f->end_seg_with_known_loc) {
  2894. // if we have a valid current loc, and this is final:
  2895. if (f->current_loc_valid && (f->page_flag & PAGEFLAG_last_page)) {
  2896. uint32 current_end = f->known_loc_for_packet - (n - right_end);
  2897. // then let's infer the size of the (probably) short final frame
  2898. if (current_end < f->current_loc + (right_end - left_start)) {
  2899. if (current_end < f->current_loc) {
  2900. // negative truncation, that's impossible!
  2901. *len = 0;
  2902. } else {
  2903. *len = current_end - f->current_loc;
  2904. }
  2905. *len += left_start;
  2906. if (*len > right_end) *len = right_end; // this should never happen
  2907. f->current_loc += *len;
  2908. return TRUE;
  2909. }
  2910. }
  2911. // otherwise, just set our sample loc
  2912. // guess that the ogg granule pos refers to the _middle_ of the
  2913. // last frame?
  2914. // set f->current_loc to the position of left_start
  2915. f->current_loc = f->known_loc_for_packet - (n2 - left_start);
  2916. f->current_loc_valid = TRUE;
  2917. }
  2918. if (f->current_loc_valid)
  2919. f->current_loc += (right_start - left_start);
  2920. if (f->alloc.alloc_buffer)
  2921. assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
  2922. *len = right_end; // ignore samples after the window goes to 0
  2923. CHECK(f);
  2924. return TRUE;
  2925. }
  2926. static int vorbis_decode_packet(vorb *f, int *len, int *p_left, int *p_right) {
  2927. int mode, left_end, right_end;
  2928. if (!vorbis_decode_initial(f, p_left, &left_end, p_right, &right_end, &mode)) return 0;
  2929. return vorbis_decode_packet_rest(f, len, f->mode_config + mode, *p_left, left_end, *p_right, right_end, p_left);
  2930. }
  2931. static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right) {
  2932. int prev, i, j;
  2933. // we use right&left (the start of the right- and left-window sin()-regions)
  2934. // to determine how much to return, rather than inferring from the rules
  2935. // (same result, clearer code); 'left' indicates where our sin() window
  2936. // starts, therefore where the previous window's right edge starts, and
  2937. // therefore where to start mixing from the previous buffer. 'right'
  2938. // indicates where our sin() ending-window starts, therefore that's where
  2939. // we start saving, and where our returned-data ends.
  2940. // mixin from previous window
  2941. if (f->previous_length) {
  2942. int i, j, n = f->previous_length;
  2943. float *w = get_window(f, n);
  2944. for (i = 0; i < f->channels; ++i) {
  2945. for (j = 0; j < n; ++j)
  2946. f->channel_buffers[i][left + j] =
  2947. f->channel_buffers[i][left + j] * w[j] +
  2948. f->previous_window[i][j] * w[n - 1 - j];
  2949. }
  2950. }
  2951. prev = f->previous_length;
  2952. // last half of this data becomes previous window
  2953. f->previous_length = len - right;
  2954. // @OPTIMIZE: could avoid this copy by double-buffering the
  2955. // output (flipping previous_window with channel_buffers), but
  2956. // then previous_window would have to be 2x as large, and
  2957. // channel_buffers couldn't be temp mem (although they're NOT
  2958. // currently temp mem, they could be (unless we want to level
  2959. // performance by spreading out the computation))
  2960. for (i = 0; i < f->channels; ++i)
  2961. for (j = 0; right + j < len; ++j)
  2962. f->previous_window[i][j] = f->channel_buffers[i][right + j];
  2963. if (!prev)
  2964. // there was no previous packet, so this data isn't valid...
  2965. // this isn't entirely true, only the would-have-overlapped data
  2966. // isn't valid, but this seems to be what the spec requires
  2967. return 0;
  2968. // truncate a short frame
  2969. if (len < right) right = len;
  2970. f->samples_output += right - left;
  2971. return right - left;
  2972. }
  2973. static void vorbis_pump_first_frame(stb_vorbis *f) {
  2974. int len, right, left;
  2975. if (vorbis_decode_packet(f, &len, &left, &right))
  2976. vorbis_finish_frame(f, len, left, right);
  2977. }
  2978. #ifndef STB_VORBIS_NO_PUSHDATA_API
  2979. static int is_whole_packet_present(stb_vorbis *f, int end_page) {
  2980. // make sure that we have the packet available before continuing...
  2981. // this requires a full ogg parse, but we know we can fetch from f->stream
  2982. // instead of coding this out explicitly, we could save the current read state,
  2983. // read the next packet with get8() until end-of-packet, check f->eof, then
  2984. // reset the state? but that would be slower, esp. since we'd have over 256 bytes
  2985. // of state to restore (primarily the page segment table)
  2986. int s = f->next_seg, first = TRUE;
  2987. uint8 *p = f->stream;
  2988. if (s != -1) { // if we're not starting the packet with a 'continue on next page' flag
  2989. for (; s < f->segment_count; ++s) {
  2990. p += f->segments[s];
  2991. if (f->segments[s] < 255) // stop at first short segment
  2992. break;
  2993. }
  2994. // either this continues, or it ends it...
  2995. if (end_page)
  2996. if (s < f->segment_count - 1) return error(f, VORBIS_invalid_stream);
  2997. if (s == f->segment_count)
  2998. s = -1; // set 'crosses page' flag
  2999. if (p > f->stream_end) return error(f, VORBIS_need_more_data);
  3000. first = FALSE;
  3001. }
  3002. for (; s == -1;) {
  3003. uint8 *q;
  3004. int n;
  3005. // check that we have the page header ready
  3006. if (p + 26 >= f->stream_end) return error(f, VORBIS_need_more_data);
  3007. // validate the page
  3008. if (memcmp(p, ogg_page_header, 4)) return error(f, VORBIS_invalid_stream);
  3009. if (p[4] != 0) return error(f, VORBIS_invalid_stream);
  3010. if (first) { // the first segment must NOT have 'continued_packet', later ones MUST
  3011. if (f->previous_length)
  3012. if ((p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream);
  3013. // if no previous length, we're resynching, so we can come in on a continued-packet,
  3014. // which we'll just drop
  3015. } else {
  3016. if (!(p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream);
  3017. }
  3018. n = p[26]; // segment counts
  3019. q = p + 27; // q points to segment table
  3020. p = q + n; // advance past header
  3021. // make sure we've read the segment table
  3022. if (p > f->stream_end) return error(f, VORBIS_need_more_data);
  3023. for (s = 0; s < n; ++s) {
  3024. p += q[s];
  3025. if (q[s] < 255)
  3026. break;
  3027. }
  3028. if (end_page)
  3029. if (s < n - 1) return error(f, VORBIS_invalid_stream);
  3030. if (s == n)
  3031. s = -1; // set 'crosses page' flag
  3032. if (p > f->stream_end) return error(f, VORBIS_need_more_data);
  3033. first = FALSE;
  3034. }
  3035. return TRUE;
  3036. }
  3037. #endif // !STB_VORBIS_NO_PUSHDATA_API
  3038. static int start_decoder(vorb *f) {
  3039. uint8 header[6], x, y;
  3040. int len, i, j, k, max_submaps = 0;
  3041. int longest_floorlist = 0;
  3042. // first page, first packet
  3043. if (!start_page(f)) return FALSE;
  3044. // validate page flag
  3045. if (!(f->page_flag & PAGEFLAG_first_page)) return error(f, VORBIS_invalid_first_page);
  3046. if (f->page_flag & PAGEFLAG_last_page) return error(f, VORBIS_invalid_first_page);
  3047. if (f->page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_invalid_first_page);
  3048. // check for expected packet length
  3049. if (f->segment_count != 1) return error(f, VORBIS_invalid_first_page);
  3050. if (f->segments[0] != 30) return error(f, VORBIS_invalid_first_page);
  3051. // read packet
  3052. // check packet header
  3053. if (get8(f) != VORBIS_packet_id) return error(f, VORBIS_invalid_first_page);
  3054. if (!getn(f, header, 6)) return error(f, VORBIS_unexpected_eof);
  3055. if (!vorbis_validate(header)) return error(f, VORBIS_invalid_first_page);
  3056. // vorbis_version
  3057. if (get32(f) != 0) return error(f, VORBIS_invalid_first_page);
  3058. f->channels = get8(f); if (!f->channels) return error(f, VORBIS_invalid_first_page);
  3059. if (f->channels > STB_VORBIS_MAX_CHANNELS) return error(f, VORBIS_too_many_channels);
  3060. f->sample_rate = get32(f); if (!f->sample_rate) return error(f, VORBIS_invalid_first_page);
  3061. get32(f); // bitrate_maximum
  3062. get32(f); // bitrate_nominal
  3063. get32(f); // bitrate_minimum
  3064. x = get8(f);
  3065. {
  3066. int log0, log1;
  3067. log0 = x & 15;
  3068. log1 = x >> 4;
  3069. f->blocksize_0 = 1 << log0;
  3070. f->blocksize_1 = 1 << log1;
  3071. if (log0 < 6 || log0 > 13) return error(f, VORBIS_invalid_setup);
  3072. if (log1 < 6 || log1 > 13) return error(f, VORBIS_invalid_setup);
  3073. if (log0 > log1) return error(f, VORBIS_invalid_setup);
  3074. }
  3075. // framing_flag
  3076. x = get8(f);
  3077. if (!(x & 1)) return error(f, VORBIS_invalid_first_page);
  3078. // second packet!
  3079. if (!start_page(f)) return FALSE;
  3080. if (!start_packet(f)) return FALSE;
  3081. do {
  3082. len = next_segment(f);
  3083. skip(f, len);
  3084. f->bytes_in_seg = 0;
  3085. } while (len);
  3086. // third packet!
  3087. if (!start_packet(f)) return FALSE;
  3088. #ifndef STB_VORBIS_NO_PUSHDATA_API
  3089. if (IS_PUSH_MODE(f)) {
  3090. if (!is_whole_packet_present(f, TRUE)) {
  3091. // convert error in ogg header to write type
  3092. if (f->error == VORBIS_invalid_stream)
  3093. f->error = VORBIS_invalid_setup;
  3094. return FALSE;
  3095. }
  3096. }
  3097. #endif
  3098. crc32_init(); // always init it, to avoid multithread race conditions
  3099. if (get8_packet(f) != VORBIS_packet_setup) return error(f, VORBIS_invalid_setup);
  3100. for (i = 0; i < 6; ++i) header[i] = get8_packet(f);
  3101. if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup);
  3102. // codebooks
  3103. f->codebook_count = get_bits(f, 8) + 1;
  3104. f->codebooks = (Codebook *) setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count);
  3105. if (f->codebooks == NULL) return error(f, VORBIS_outofmem);
  3106. memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count);
  3107. for (i = 0; i < f->codebook_count; ++i) {
  3108. uint32 *values;
  3109. int ordered, sorted_count;
  3110. int total = 0;
  3111. uint8 *lengths;
  3112. Codebook *c = f->codebooks + i;
  3113. CHECK(f);
  3114. x = get_bits(f, 8); if (x != 0x42) return error(f, VORBIS_invalid_setup);
  3115. x = get_bits(f, 8); if (x != 0x43) return error(f, VORBIS_invalid_setup);
  3116. x = get_bits(f, 8); if (x != 0x56) return error(f, VORBIS_invalid_setup);
  3117. x = get_bits(f, 8);
  3118. c->dimensions = (get_bits(f, 8) << 8) + x;
  3119. x = get_bits(f, 8);
  3120. y = get_bits(f, 8);
  3121. c->entries = (get_bits(f, 8) << 16) + (y << 8) + x;
  3122. ordered = get_bits(f, 1);
  3123. c->sparse = ordered ? 0 : get_bits(f, 1);
  3124. if (c->dimensions == 0 && c->entries != 0) return error(f, VORBIS_invalid_setup);
  3125. if (c->sparse)
  3126. lengths = (uint8 *) setup_temp_malloc(f, c->entries);
  3127. else
  3128. lengths = c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
  3129. if (!lengths) return error(f, VORBIS_outofmem);
  3130. if (ordered) {
  3131. int current_entry = 0;
  3132. int current_length = get_bits(f, 5) + 1;
  3133. while (current_entry < c->entries) {
  3134. int limit = c->entries - current_entry;
  3135. int n = get_bits(f, ilog(limit));
  3136. if (current_entry + n >(int) c->entries) { return error(f, VORBIS_invalid_setup); }
  3137. memset(lengths + current_entry, current_length, n);
  3138. current_entry += n;
  3139. ++current_length;
  3140. }
  3141. } else {
  3142. for (j = 0; j < c->entries; ++j) {
  3143. int present = c->sparse ? get_bits(f, 1) : 1;
  3144. if (present) {
  3145. lengths[j] = get_bits(f, 5) + 1;
  3146. ++total;
  3147. if (lengths[j] == 32)
  3148. return error(f, VORBIS_invalid_setup);
  3149. } else {
  3150. lengths[j] = NO_CODE;
  3151. }
  3152. }
  3153. }
  3154. if (c->sparse && total >= c->entries >> 2) {
  3155. // convert sparse items to non-sparse!
  3156. if (c->entries > (int) f->setup_temp_memory_required)
  3157. f->setup_temp_memory_required = c->entries;
  3158. c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
  3159. if (c->codeword_lengths == NULL) return error(f, VORBIS_outofmem);
  3160. memcpy(c->codeword_lengths, lengths, c->entries);
  3161. setup_temp_free(f, lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs!
  3162. lengths = c->codeword_lengths;
  3163. c->sparse = 0;
  3164. }
  3165. // compute the size of the sorted tables
  3166. if (c->sparse) {
  3167. sorted_count = total;
  3168. } else {
  3169. sorted_count = 0;
  3170. #ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
  3171. for (j = 0; j < c->entries; ++j)
  3172. if (lengths[j] > STB_VORBIS_FAST_HUFFMAN_LENGTH && lengths[j] != NO_CODE)
  3173. ++sorted_count;
  3174. #endif
  3175. }
  3176. c->sorted_entries = sorted_count;
  3177. values = NULL;
  3178. CHECK(f);
  3179. if (!c->sparse) {
  3180. c->codewords = (uint32 *) setup_malloc(f, sizeof(c->codewords[0]) * c->entries);
  3181. if (!c->codewords) return error(f, VORBIS_outofmem);
  3182. } else {
  3183. unsigned int size;
  3184. if (c->sorted_entries) {
  3185. c->codeword_lengths = (uint8 *) setup_malloc(f, c->sorted_entries);
  3186. if (!c->codeword_lengths) return error(f, VORBIS_outofmem);
  3187. c->codewords = (uint32 *) setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries);
  3188. if (!c->codewords) return error(f, VORBIS_outofmem);
  3189. values = (uint32 *) setup_temp_malloc(f, sizeof(*values) * c->sorted_entries);
  3190. if (!values) return error(f, VORBIS_outofmem);
  3191. }
  3192. size = c->entries + (sizeof(*c->codewords) + sizeof(*values)) * c->sorted_entries;
  3193. if (size > f->setup_temp_memory_required)
  3194. f->setup_temp_memory_required = size;
  3195. }
  3196. if (!compute_codewords(c, lengths, c->entries, values)) {
  3197. if (c->sparse) setup_temp_free(f, values, 0);
  3198. return error(f, VORBIS_invalid_setup);
  3199. }
  3200. if (c->sorted_entries) {
  3201. // allocate an extra slot for sentinels
  3202. c->sorted_codewords = (uint32 *) setup_malloc(f, sizeof(*c->sorted_codewords) * (c->sorted_entries + 1));
  3203. if (c->sorted_codewords == NULL) return error(f, VORBIS_outofmem);
  3204. // allocate an extra slot at the front so that c->sorted_values[-1] is defined
  3205. // so that we can catch that case without an extra if
  3206. c->sorted_values = (int *) setup_malloc(f, sizeof(*c->sorted_values) * (c->sorted_entries + 1));
  3207. if (c->sorted_values == NULL) return error(f, VORBIS_outofmem);
  3208. ++c->sorted_values;
  3209. c->sorted_values[-1] = -1;
  3210. compute_sorted_huffman(c, lengths, values);
  3211. }
  3212. if (c->sparse) {
  3213. setup_temp_free(f, values, sizeof(*values)*c->sorted_entries);
  3214. setup_temp_free(f, c->codewords, sizeof(*c->codewords)*c->sorted_entries);
  3215. setup_temp_free(f, lengths, c->entries);
  3216. c->codewords = NULL;
  3217. }
  3218. compute_accelerated_huffman(c);
  3219. CHECK(f);
  3220. c->lookup_type = get_bits(f, 4);
  3221. if (c->lookup_type > 2) return error(f, VORBIS_invalid_setup);
  3222. if (c->lookup_type > 0) {
  3223. uint16 *mults;
  3224. c->minimum_value = float32_unpack(get_bits(f, 32));
  3225. c->delta_value = float32_unpack(get_bits(f, 32));
  3226. c->value_bits = get_bits(f, 4) + 1;
  3227. c->sequence_p = get_bits(f, 1);
  3228. if (c->lookup_type == 1) {
  3229. c->lookup_values = lookup1_values(c->entries, c->dimensions);
  3230. } else {
  3231. c->lookup_values = c->entries * c->dimensions;
  3232. }
  3233. if (c->lookup_values == 0) return error(f, VORBIS_invalid_setup);
  3234. mults = (uint16 *) setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values);
  3235. if (mults == NULL) return error(f, VORBIS_outofmem);
  3236. for (j = 0; j < (int) c->lookup_values; ++j) {
  3237. int q = get_bits(f, c->value_bits);
  3238. if (q == EOP) { setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_invalid_setup); }
  3239. mults[j] = q;
  3240. }
  3241. #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  3242. if (c->lookup_type == 1) {
  3243. int len, sparse = c->sparse;
  3244. float last = 0;
  3245. // pre-expand the lookup1-style multiplicands, to avoid a divide in the inner loop
  3246. if (sparse) {
  3247. if (c->sorted_entries == 0) goto skip;
  3248. c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions);
  3249. } else
  3250. c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries * c->dimensions);
  3251. if (c->multiplicands == NULL) { setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); }
  3252. len = sparse ? c->sorted_entries : c->entries;
  3253. for (j = 0; j < len; ++j) {
  3254. unsigned int z = sparse ? c->sorted_values[j] : j;
  3255. unsigned int div = 1;
  3256. for (k = 0; k < c->dimensions; ++k) {
  3257. int off = (z / div) % c->lookup_values;
  3258. float val = mults[off];
  3259. val = mults[off] * c->delta_value + c->minimum_value + last;
  3260. c->multiplicands[j*c->dimensions + k] = val;
  3261. if (c->sequence_p)
  3262. last = val;
  3263. if (k + 1 < c->dimensions) {
  3264. if (div > UINT_MAX / (unsigned int) c->lookup_values) {
  3265. setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values);
  3266. return error(f, VORBIS_invalid_setup);
  3267. }
  3268. div *= c->lookup_values;
  3269. }
  3270. }
  3271. }
  3272. c->lookup_type = 2;
  3273. } else
  3274. #endif
  3275. {
  3276. float last = 0;
  3277. CHECK(f);
  3278. c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values);
  3279. if (c->multiplicands == NULL) { setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); }
  3280. for (j = 0; j < (int) c->lookup_values; ++j) {
  3281. float val = mults[j] * c->delta_value + c->minimum_value + last;
  3282. c->multiplicands[j] = val;
  3283. if (c->sequence_p)
  3284. last = val;
  3285. }
  3286. }
  3287. #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  3288. skip : ;
  3289. #endif
  3290. setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values);
  3291. CHECK(f);
  3292. }
  3293. CHECK(f);
  3294. }
  3295. // time domain transfers (notused)
  3296. x = get_bits(f, 6) + 1;
  3297. for (i = 0; i < x; ++i) {
  3298. uint32 z = get_bits(f, 16);
  3299. if (z != 0) return error(f, VORBIS_invalid_setup);
  3300. }
  3301. // Floors
  3302. f->floor_count = get_bits(f, 6) + 1;
  3303. f->floor_config = (Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config));
  3304. if (f->floor_config == NULL) return error(f, VORBIS_outofmem);
  3305. for (i = 0; i < f->floor_count; ++i) {
  3306. f->floor_types[i] = get_bits(f, 16);
  3307. if (f->floor_types[i] > 1) return error(f, VORBIS_invalid_setup);
  3308. if (f->floor_types[i] == 0) {
  3309. Floor0 *g = &f->floor_config[i].floor0;
  3310. g->order = get_bits(f, 8);
  3311. g->rate = get_bits(f, 16);
  3312. g->bark_map_size = get_bits(f, 16);
  3313. g->amplitude_bits = get_bits(f, 6);
  3314. g->amplitude_offset = get_bits(f, 8);
  3315. g->number_of_books = get_bits(f, 4) + 1;
  3316. for (j = 0; j < g->number_of_books; ++j)
  3317. g->book_list[j] = get_bits(f, 8);
  3318. return error(f, VORBIS_feature_not_supported);
  3319. } else {
  3320. Point p[31 * 8 + 2];
  3321. Floor1 *g = &f->floor_config[i].floor1;
  3322. int max_class = -1;
  3323. g->partitions = get_bits(f, 5);
  3324. for (j = 0; j < g->partitions; ++j) {
  3325. g->partition_class_list[j] = get_bits(f, 4);
  3326. if (g->partition_class_list[j] > max_class)
  3327. max_class = g->partition_class_list[j];
  3328. }
  3329. for (j = 0; j <= max_class; ++j) {
  3330. g->class_dimensions[j] = get_bits(f, 3) + 1;
  3331. g->class_subclasses[j] = get_bits(f, 2);
  3332. if (g->class_subclasses[j]) {
  3333. g->class_masterbooks[j] = get_bits(f, 8);
  3334. if (g->class_masterbooks[j] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
  3335. }
  3336. for (k = 0; k < 1 << g->class_subclasses[j]; ++k) {
  3337. g->subclass_books[j][k] = get_bits(f, 8) - 1;
  3338. if (g->subclass_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
  3339. }
  3340. }
  3341. g->floor1_multiplier = get_bits(f, 2) + 1;
  3342. g->rangebits = get_bits(f, 4);
  3343. g->Xlist[0] = 0;
  3344. g->Xlist[1] = 1 << g->rangebits;
  3345. g->values = 2;
  3346. for (j = 0; j < g->partitions; ++j) {
  3347. int c = g->partition_class_list[j];
  3348. for (k = 0; k < g->class_dimensions[c]; ++k) {
  3349. g->Xlist[g->values] = get_bits(f, g->rangebits);
  3350. ++g->values;
  3351. }
  3352. }
  3353. // precompute the sorting
  3354. for (j = 0; j < g->values; ++j) {
  3355. p[j].x = g->Xlist[j];
  3356. p[j].y = j;
  3357. }
  3358. qsort(p, g->values, sizeof(p[0]), point_compare);
  3359. for (j = 0; j < g->values; ++j)
  3360. g->sorted_order[j] = (uint8) p[j].y;
  3361. // precompute the neighbors
  3362. for (j = 2; j < g->values; ++j) {
  3363. int low, hi;
  3364. neighbors(g->Xlist, j, &low, &hi);
  3365. g->neighbors[j][0] = low;
  3366. g->neighbors[j][1] = hi;
  3367. }
  3368. if (g->values > longest_floorlist)
  3369. longest_floorlist = g->values;
  3370. }
  3371. }
  3372. // Residue
  3373. f->residue_count = get_bits(f, 6) + 1;
  3374. f->residue_config = (Residue *) setup_malloc(f, f->residue_count * sizeof(f->residue_config[0]));
  3375. if (f->residue_config == NULL) return error(f, VORBIS_outofmem);
  3376. memset(f->residue_config, 0, f->residue_count * sizeof(f->residue_config[0]));
  3377. for (i = 0; i < f->residue_count; ++i) {
  3378. uint8 residue_cascade[64];
  3379. Residue *r = f->residue_config + i;
  3380. f->residue_types[i] = get_bits(f, 16);
  3381. if (f->residue_types[i] > 2) return error(f, VORBIS_invalid_setup);
  3382. r->begin = get_bits(f, 24);
  3383. r->end = get_bits(f, 24);
  3384. if (r->end < r->begin) return error(f, VORBIS_invalid_setup);
  3385. r->part_size = get_bits(f, 24) + 1;
  3386. r->classifications = get_bits(f, 6) + 1;
  3387. r->classbook = get_bits(f, 8);
  3388. if (r->classbook >= f->codebook_count) return error(f, VORBIS_invalid_setup);
  3389. for (j = 0; j < r->classifications; ++j) {
  3390. uint8 high_bits = 0;
  3391. uint8 low_bits = get_bits(f, 3);
  3392. if (get_bits(f, 1))
  3393. high_bits = get_bits(f, 5);
  3394. residue_cascade[j] = high_bits * 8 + low_bits;
  3395. }
  3396. r->residue_books = (short(*)[8]) setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications);
  3397. if (r->residue_books == NULL) return error(f, VORBIS_outofmem);
  3398. for (j = 0; j < r->classifications; ++j) {
  3399. for (k = 0; k < 8; ++k) {
  3400. if (residue_cascade[j] & (1 << k)) {
  3401. r->residue_books[j][k] = get_bits(f, 8);
  3402. if (r->residue_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
  3403. } else {
  3404. r->residue_books[j][k] = -1;
  3405. }
  3406. }
  3407. }
  3408. // precompute the classifications[] array to avoid inner-loop mod/divide
  3409. // call it 'classdata' since we already have r->classifications
  3410. r->classdata = (uint8 **) setup_malloc(f, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
  3411. if (!r->classdata) return error(f, VORBIS_outofmem);
  3412. memset(r->classdata, 0, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
  3413. for (j = 0; j < f->codebooks[r->classbook].entries; ++j) {
  3414. int classwords = f->codebooks[r->classbook].dimensions;
  3415. int temp = j;
  3416. r->classdata[j] = (uint8 *) setup_malloc(f, sizeof(r->classdata[j][0]) * classwords);
  3417. if (r->classdata[j] == NULL) return error(f, VORBIS_outofmem);
  3418. for (k = classwords - 1; k >= 0; --k) {
  3419. r->classdata[j][k] = temp % r->classifications;
  3420. temp /= r->classifications;
  3421. }
  3422. }
  3423. }
  3424. f->mapping_count = get_bits(f, 6) + 1;
  3425. f->mapping = (Mapping *) setup_malloc(f, f->mapping_count * sizeof(*f->mapping));
  3426. if (f->mapping == NULL) return error(f, VORBIS_outofmem);
  3427. memset(f->mapping, 0, f->mapping_count * sizeof(*f->mapping));
  3428. for (i = 0; i < f->mapping_count; ++i) {
  3429. Mapping *m = f->mapping + i;
  3430. int mapping_type = get_bits(f, 16);
  3431. if (mapping_type != 0) return error(f, VORBIS_invalid_setup);
  3432. m->chan = (MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan));
  3433. if (m->chan == NULL) return error(f, VORBIS_outofmem);
  3434. if (get_bits(f, 1))
  3435. m->submaps = get_bits(f, 4) + 1;
  3436. else
  3437. m->submaps = 1;
  3438. if (m->submaps > max_submaps)
  3439. max_submaps = m->submaps;
  3440. if (get_bits(f, 1)) {
  3441. m->coupling_steps = get_bits(f, 8) + 1;
  3442. for (k = 0; k < m->coupling_steps; ++k) {
  3443. m->chan[k].magnitude = get_bits(f, ilog(f->channels - 1));
  3444. m->chan[k].angle = get_bits(f, ilog(f->channels - 1));
  3445. if (m->chan[k].magnitude >= f->channels) return error(f, VORBIS_invalid_setup);
  3446. if (m->chan[k].angle >= f->channels) return error(f, VORBIS_invalid_setup);
  3447. if (m->chan[k].magnitude == m->chan[k].angle) return error(f, VORBIS_invalid_setup);
  3448. }
  3449. } else
  3450. m->coupling_steps = 0;
  3451. // reserved field
  3452. if (get_bits(f, 2)) return error(f, VORBIS_invalid_setup);
  3453. if (m->submaps > 1) {
  3454. for (j = 0; j < f->channels; ++j) {
  3455. m->chan[j].mux = get_bits(f, 4);
  3456. if (m->chan[j].mux >= m->submaps) return error(f, VORBIS_invalid_setup);
  3457. }
  3458. } else
  3459. // @SPECIFICATION: this case is missing from the spec
  3460. for (j = 0; j < f->channels; ++j)
  3461. m->chan[j].mux = 0;
  3462. for (j = 0; j < m->submaps; ++j) {
  3463. get_bits(f, 8); // discard
  3464. m->submap_floor[j] = get_bits(f, 8);
  3465. m->submap_residue[j] = get_bits(f, 8);
  3466. if (m->submap_floor[j] >= f->floor_count) return error(f, VORBIS_invalid_setup);
  3467. if (m->submap_residue[j] >= f->residue_count) return error(f, VORBIS_invalid_setup);
  3468. }
  3469. }
  3470. // Modes
  3471. f->mode_count = get_bits(f, 6) + 1;
  3472. for (i = 0; i < f->mode_count; ++i) {
  3473. Mode *m = f->mode_config + i;
  3474. m->blockflag = get_bits(f, 1);
  3475. m->windowtype = get_bits(f, 16);
  3476. m->transformtype = get_bits(f, 16);
  3477. m->mapping = get_bits(f, 8);
  3478. if (m->windowtype != 0) return error(f, VORBIS_invalid_setup);
  3479. if (m->transformtype != 0) return error(f, VORBIS_invalid_setup);
  3480. if (m->mapping >= f->mapping_count) return error(f, VORBIS_invalid_setup);
  3481. }
  3482. flush_packet(f);
  3483. f->previous_length = 0;
  3484. for (i = 0; i < f->channels; ++i) {
  3485. f->channel_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1);
  3486. f->previous_window[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1 / 2);
  3487. f->finalY[i] = (int16 *) setup_malloc(f, sizeof(int16) * longest_floorlist);
  3488. if (f->channel_buffers[i] == NULL || f->previous_window[i] == NULL || f->finalY[i] == NULL) return error(f, VORBIS_outofmem);
  3489. #ifdef STB_VORBIS_NO_DEFER_FLOOR
  3490. f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1 / 2);
  3491. if (f->floor_buffers[i] == NULL) return error(f, VORBIS_outofmem);
  3492. #endif
  3493. }
  3494. if (!init_blocksize(f, 0, f->blocksize_0)) return FALSE;
  3495. if (!init_blocksize(f, 1, f->blocksize_1)) return FALSE;
  3496. f->blocksize[0] = f->blocksize_0;
  3497. f->blocksize[1] = f->blocksize_1;
  3498. #ifdef STB_VORBIS_DIVIDE_TABLE
  3499. if (integer_divide_table[1][1] == 0)
  3500. for (i = 0; i < DIVTAB_NUMER; ++i)
  3501. for (j = 1; j < DIVTAB_DENOM; ++j)
  3502. integer_divide_table[i][j] = i / j;
  3503. #endif
  3504. // compute how much temporary memory is needed
  3505. // 1.
  3506. {
  3507. uint32 imdct_mem = (f->blocksize_1 * sizeof(float) >> 1);
  3508. uint32 classify_mem;
  3509. int i, max_part_read = 0;
  3510. for (i = 0; i < f->residue_count; ++i) {
  3511. Residue *r = f->residue_config + i;
  3512. int n_read = r->end - r->begin;
  3513. int part_read = n_read / r->part_size;
  3514. if (part_read > max_part_read)
  3515. max_part_read = part_read;
  3516. }
  3517. #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  3518. classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(uint8 *));
  3519. #else
  3520. classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(int *));
  3521. #endif
  3522. f->temp_memory_required = classify_mem;
  3523. if (imdct_mem > f->temp_memory_required)
  3524. f->temp_memory_required = imdct_mem;
  3525. }
  3526. f->first_decode = TRUE;
  3527. if (f->alloc.alloc_buffer) {
  3528. assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes);
  3529. // check if there's enough temp memory so we don't error later
  3530. if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned) f->temp_offset)
  3531. return error(f, VORBIS_outofmem);
  3532. }
  3533. f->first_audio_page_offset = stb_vorbis_get_file_offset(f);
  3534. return TRUE;
  3535. }
  3536. static void vorbis_deinit(stb_vorbis *p) {
  3537. int i, j;
  3538. if (p->residue_config) {
  3539. for (i = 0; i < p->residue_count; ++i) {
  3540. Residue *r = p->residue_config + i;
  3541. if (r->classdata) {
  3542. for (j = 0; j < p->codebooks[r->classbook].entries; ++j)
  3543. setup_free(p, r->classdata[j]);
  3544. setup_free(p, r->classdata);
  3545. }
  3546. setup_free(p, r->residue_books);
  3547. }
  3548. }
  3549. if (p->codebooks) {
  3550. CHECK(p);
  3551. for (i = 0; i < p->codebook_count; ++i) {
  3552. Codebook *c = p->codebooks + i;
  3553. setup_free(p, c->codeword_lengths);
  3554. setup_free(p, c->multiplicands);
  3555. setup_free(p, c->codewords);
  3556. setup_free(p, c->sorted_codewords);
  3557. // c->sorted_values[-1] is the first entry in the array
  3558. setup_free(p, c->sorted_values ? c->sorted_values - 1 : NULL);
  3559. }
  3560. setup_free(p, p->codebooks);
  3561. }
  3562. setup_free(p, p->floor_config);
  3563. setup_free(p, p->residue_config);
  3564. if (p->mapping) {
  3565. for (i = 0; i < p->mapping_count; ++i)
  3566. setup_free(p, p->mapping[i].chan);
  3567. setup_free(p, p->mapping);
  3568. }
  3569. CHECK(p);
  3570. for (i = 0; i < p->channels && i < STB_VORBIS_MAX_CHANNELS; ++i) {
  3571. setup_free(p, p->channel_buffers[i]);
  3572. setup_free(p, p->previous_window[i]);
  3573. #ifdef STB_VORBIS_NO_DEFER_FLOOR
  3574. setup_free(p, p->floor_buffers[i]);
  3575. #endif
  3576. setup_free(p, p->finalY[i]);
  3577. }
  3578. for (i = 0; i < 2; ++i) {
  3579. setup_free(p, p->A[i]);
  3580. setup_free(p, p->B[i]);
  3581. setup_free(p, p->C[i]);
  3582. setup_free(p, p->window[i]);
  3583. setup_free(p, p->bit_reverse[i]);
  3584. }
  3585. #ifndef STB_VORBIS_NO_STDIO
  3586. if (p->close_on_free) fclose(p->f);
  3587. #endif
  3588. }
  3589. void stb_vorbis_close(stb_vorbis *p) {
  3590. if (p == NULL) return;
  3591. vorbis_deinit(p);
  3592. setup_free(p, p);
  3593. }
  3594. static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z) {
  3595. memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start
  3596. if (z) {
  3597. p->alloc = *z;
  3598. p->alloc.alloc_buffer_length_in_bytes = (p->alloc.alloc_buffer_length_in_bytes + 3) & ~3;
  3599. p->temp_offset = p->alloc.alloc_buffer_length_in_bytes;
  3600. }
  3601. p->eof = 0;
  3602. p->error = VORBIS__no_error;
  3603. p->stream = NULL;
  3604. p->codebooks = NULL;
  3605. p->page_crc_tests = -1;
  3606. #ifndef STB_VORBIS_NO_STDIO
  3607. p->close_on_free = FALSE;
  3608. p->f = NULL;
  3609. #endif
  3610. }
  3611. int stb_vorbis_get_sample_offset(stb_vorbis *f) {
  3612. if (f->current_loc_valid)
  3613. return f->current_loc;
  3614. else
  3615. return -1;
  3616. }
  3617. stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f) {
  3618. stb_vorbis_info d;
  3619. d.channels = f->channels;
  3620. d.sample_rate = f->sample_rate;
  3621. d.setup_memory_required = f->setup_memory_required;
  3622. d.setup_temp_memory_required = f->setup_temp_memory_required;
  3623. d.temp_memory_required = f->temp_memory_required;
  3624. d.max_frame_size = f->blocksize_1 >> 1;
  3625. return d;
  3626. }
  3627. int stb_vorbis_get_error(stb_vorbis *f) {
  3628. int e = f->error;
  3629. f->error = VORBIS__no_error;
  3630. return e;
  3631. }
  3632. static stb_vorbis * vorbis_alloc(stb_vorbis *f) {
  3633. stb_vorbis *p = (stb_vorbis *) setup_malloc(f, sizeof(*p));
  3634. return p;
  3635. }
  3636. #ifndef STB_VORBIS_NO_PUSHDATA_API
  3637. void stb_vorbis_flush_pushdata(stb_vorbis *f) {
  3638. f->previous_length = 0;
  3639. f->page_crc_tests = 0;
  3640. f->discard_samples_deferred = 0;
  3641. f->current_loc_valid = FALSE;
  3642. f->first_decode = FALSE;
  3643. f->samples_output = 0;
  3644. f->channel_buffer_start = 0;
  3645. f->channel_buffer_end = 0;
  3646. }
  3647. static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len) {
  3648. int i, n;
  3649. for (i = 0; i < f->page_crc_tests; ++i)
  3650. f->scan[i].bytes_done = 0;
  3651. // if we have room for more scans, search for them first, because
  3652. // they may cause us to stop early if their header is incomplete
  3653. if (f->page_crc_tests < STB_VORBIS_PUSHDATA_CRC_COUNT) {
  3654. if (data_len < 4) return 0;
  3655. data_len -= 3; // need to look for 4-byte sequence, so don't miss
  3656. // one that straddles a boundary
  3657. for (i = 0; i < data_len; ++i) {
  3658. if (data[i] == 0x4f) {
  3659. if (0 == memcmp(data + i, ogg_page_header, 4)) {
  3660. int j, len;
  3661. uint32 crc;
  3662. // make sure we have the whole page header
  3663. if (i + 26 >= data_len || i + 27 + data[i + 26] >= data_len) {
  3664. // only read up to this page start, so hopefully we'll
  3665. // have the whole page header start next time
  3666. data_len = i;
  3667. break;
  3668. }
  3669. // ok, we have it all; compute the length of the page
  3670. len = 27 + data[i + 26];
  3671. for (j = 0; j < data[i + 26]; ++j)
  3672. len += data[i + 27 + j];
  3673. // scan everything up to the embedded crc (which we must 0)
  3674. crc = 0;
  3675. for (j = 0; j < 22; ++j)
  3676. crc = crc32_update(crc, data[i + j]);
  3677. // now process 4 0-bytes
  3678. for (; j < 26; ++j)
  3679. crc = crc32_update(crc, 0);
  3680. // len is the total number of bytes we need to scan
  3681. n = f->page_crc_tests++;
  3682. f->scan[n].bytes_left = len - j;
  3683. f->scan[n].crc_so_far = crc;
  3684. f->scan[n].goal_crc = data[i + 22] + (data[i + 23] << 8) + (data[i + 24] << 16) + (data[i + 25] << 24);
  3685. // if the last frame on a page is continued to the next, then
  3686. // we can't recover the sample_loc immediately
  3687. if (data[i + 27 + data[i + 26] - 1] == 255)
  3688. f->scan[n].sample_loc = ~0;
  3689. else
  3690. f->scan[n].sample_loc = data[i + 6] + (data[i + 7] << 8) + (data[i + 8] << 16) + (data[i + 9] << 24);
  3691. f->scan[n].bytes_done = i + j;
  3692. if (f->page_crc_tests == STB_VORBIS_PUSHDATA_CRC_COUNT)
  3693. break;
  3694. // keep going if we still have room for more
  3695. }
  3696. }
  3697. }
  3698. }
  3699. for (i = 0; i < f->page_crc_tests;) {
  3700. uint32 crc;
  3701. int j;
  3702. int n = f->scan[i].bytes_done;
  3703. int m = f->scan[i].bytes_left;
  3704. if (m > data_len - n) m = data_len - n;
  3705. // m is the bytes to scan in the current chunk
  3706. crc = f->scan[i].crc_so_far;
  3707. for (j = 0; j < m; ++j)
  3708. crc = crc32_update(crc, data[n + j]);
  3709. f->scan[i].bytes_left -= m;
  3710. f->scan[i].crc_so_far = crc;
  3711. if (f->scan[i].bytes_left == 0) {
  3712. // does it match?
  3713. if (f->scan[i].crc_so_far == f->scan[i].goal_crc) {
  3714. // Houston, we have page
  3715. data_len = n + m; // consumption amount is wherever that scan ended
  3716. f->page_crc_tests = -1; // drop out of page scan mode
  3717. f->previous_length = 0; // decode-but-don't-output one frame
  3718. f->next_seg = -1; // start a new page
  3719. f->current_loc = f->scan[i].sample_loc; // set the current sample location
  3720. // to the amount we'd have decoded had we decoded this page
  3721. f->current_loc_valid = f->current_loc != ~0U;
  3722. return data_len;
  3723. }
  3724. // delete entry
  3725. f->scan[i] = f->scan[--f->page_crc_tests];
  3726. } else {
  3727. ++i;
  3728. }
  3729. }
  3730. return data_len;
  3731. }
  3732. // return value: number of bytes we used
  3733. int stb_vorbis_decode_frame_pushdata(
  3734. stb_vorbis *f, // the file we're decoding
  3735. const uint8 *data, int data_len, // the memory available for decoding
  3736. int *channels, // place to write number of float * buffers
  3737. float ***output, // place to write float ** array of float * buffers
  3738. int *samples // place to write number of output samples
  3739. ) {
  3740. int i;
  3741. int len, right, left;
  3742. if (!IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
  3743. if (f->page_crc_tests >= 0) {
  3744. *samples = 0;
  3745. return vorbis_search_for_page_pushdata(f, (uint8 *) data, data_len);
  3746. }
  3747. f->stream = (uint8 *) data;
  3748. f->stream_end = (uint8 *) data + data_len;
  3749. f->error = VORBIS__no_error;
  3750. // check that we have the entire packet in memory
  3751. if (!is_whole_packet_present(f, FALSE)) {
  3752. *samples = 0;
  3753. return 0;
  3754. }
  3755. if (!vorbis_decode_packet(f, &len, &left, &right)) {
  3756. // save the actual error we encountered
  3757. enum STBVorbisError error = f->error;
  3758. if (error == VORBIS_bad_packet_type) {
  3759. // flush and resynch
  3760. f->error = VORBIS__no_error;
  3761. while (get8_packet(f) != EOP)
  3762. if (f->eof) break;
  3763. *samples = 0;
  3764. return (int) (f->stream - data);
  3765. }
  3766. if (error == VORBIS_continued_packet_flag_invalid) {
  3767. if (f->previous_length == 0) {
  3768. // we may be resynching, in which case it's ok to hit one
  3769. // of these; just discard the packet
  3770. f->error = VORBIS__no_error;
  3771. while (get8_packet(f) != EOP)
  3772. if (f->eof) break;
  3773. *samples = 0;
  3774. return (int) (f->stream - data);
  3775. }
  3776. }
  3777. // if we get an error while parsing, what to do?
  3778. // well, it DEFINITELY won't work to continue from where we are!
  3779. stb_vorbis_flush_pushdata(f);
  3780. // restore the error that actually made us bail
  3781. f->error = error;
  3782. *samples = 0;
  3783. return 1;
  3784. }
  3785. // success!
  3786. len = vorbis_finish_frame(f, len, left, right);
  3787. for (i = 0; i < f->channels; ++i)
  3788. f->outputs[i] = f->channel_buffers[i] + left;
  3789. if (channels) *channels = f->channels;
  3790. *samples = len;
  3791. *output = f->outputs;
  3792. return (int) (f->stream - data);
  3793. }
  3794. stb_vorbis *stb_vorbis_open_pushdata(
  3795. const unsigned char *data, int data_len, // the memory available for decoding
  3796. int *data_used, // only defined if result is not NULL
  3797. int *error, const stb_vorbis_alloc *alloc) {
  3798. stb_vorbis *f, p;
  3799. vorbis_init(&p, alloc);
  3800. p.stream = (uint8 *) data;
  3801. p.stream_end = (uint8 *) data + data_len;
  3802. p.push_mode = TRUE;
  3803. if (!start_decoder(&p)) {
  3804. if (p.eof)
  3805. *error = VORBIS_need_more_data;
  3806. else
  3807. *error = p.error;
  3808. return NULL;
  3809. }
  3810. f = vorbis_alloc(&p);
  3811. if (f) {
  3812. *f = p;
  3813. *data_used = (int) (f->stream - data);
  3814. *error = 0;
  3815. return f;
  3816. } else {
  3817. vorbis_deinit(&p);
  3818. return NULL;
  3819. }
  3820. }
  3821. #endif // STB_VORBIS_NO_PUSHDATA_API
  3822. unsigned int stb_vorbis_get_file_offset(stb_vorbis *f) {
  3823. #ifndef STB_VORBIS_NO_PUSHDATA_API
  3824. if (f->push_mode) return 0;
  3825. #endif
  3826. if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
  3827. #ifndef STB_VORBIS_NO_STDIO
  3828. return (unsigned int) (ftell(f->f) - f->f_start);
  3829. #endif
  3830. }
  3831. #ifndef STB_VORBIS_NO_PULLDATA_API
  3832. //
  3833. // DATA-PULLING API
  3834. //
  3835. static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last) {
  3836. for (;;) {
  3837. int n;
  3838. if (f->eof) return 0;
  3839. n = get8(f);
  3840. if (n == 0x4f) { // page header candidate
  3841. unsigned int retry_loc = stb_vorbis_get_file_offset(f);
  3842. int i;
  3843. // check if we're off the end of a file_section stream
  3844. if (retry_loc - 25 > f->stream_len)
  3845. return 0;
  3846. // check the rest of the header
  3847. for (i = 1; i < 4; ++i)
  3848. if (get8(f) != ogg_page_header[i])
  3849. break;
  3850. if (f->eof) return 0;
  3851. if (i == 4) {
  3852. uint8 header[27];
  3853. uint32 i, crc, goal, len;
  3854. for (i = 0; i < 4; ++i)
  3855. header[i] = ogg_page_header[i];
  3856. for (; i < 27; ++i)
  3857. header[i] = get8(f);
  3858. if (f->eof) return 0;
  3859. if (header[4] != 0) goto invalid;
  3860. goal = header[22] + (header[23] << 8) + (header[24] << 16) + (header[25] << 24);
  3861. for (i = 22; i < 26; ++i)
  3862. header[i] = 0;
  3863. crc = 0;
  3864. for (i = 0; i < 27; ++i)
  3865. crc = crc32_update(crc, header[i]);
  3866. len = 0;
  3867. for (i = 0; i < header[26]; ++i) {
  3868. int s = get8(f);
  3869. crc = crc32_update(crc, s);
  3870. len += s;
  3871. }
  3872. if (len && f->eof) return 0;
  3873. for (i = 0; i < len; ++i)
  3874. crc = crc32_update(crc, get8(f));
  3875. // finished parsing probable page
  3876. if (crc == goal) {
  3877. // we could now check that it's either got the last
  3878. // page flag set, OR it's followed by the capture
  3879. // pattern, but I guess TECHNICALLY you could have
  3880. // a file with garbage between each ogg page and recover
  3881. // from it automatically? So even though that paranoia
  3882. // might decrease the chance of an invalid decode by
  3883. // another 2^32, not worth it since it would hose those
  3884. // invalid-but-useful files?
  3885. if (end)
  3886. *end = stb_vorbis_get_file_offset(f);
  3887. if (last) {
  3888. if (header[5] & 0x04)
  3889. *last = 1;
  3890. else
  3891. *last = 0;
  3892. }
  3893. set_file_offset(f, retry_loc - 1);
  3894. return 1;
  3895. }
  3896. }
  3897. invalid:
  3898. // not a valid page, so rewind and look for next one
  3899. set_file_offset(f, retry_loc);
  3900. }
  3901. }
  3902. }
  3903. #define SAMPLE_unknown 0xffffffff
  3904. // seeking is implemented with a binary search, which narrows down the range to
  3905. // 64K, before using a linear search (because finding the synchronization
  3906. // pattern can be expensive, and the chance we'd find the end page again is
  3907. // relatively high for small ranges)
  3908. //
  3909. // two initial interpolation-style probes are used at the start of the search
  3910. // to try to bound either side of the binary search sensibly, while still
  3911. // working in O(log n) time if they fail.
  3912. static int get_seek_page_info(stb_vorbis *f, ProbedPage *z) {
  3913. uint8 header[27], lacing[255];
  3914. int i, len;
  3915. // record where the page starts
  3916. z->page_start = stb_vorbis_get_file_offset(f);
  3917. // parse the header
  3918. getn(f, header, 27);
  3919. if (header[0] != 'O' || header[1] != 'g' || header[2] != 'g' || header[3] != 'S')
  3920. return 0;
  3921. getn(f, lacing, header[26]);
  3922. // determine the length of the payload
  3923. len = 0;
  3924. for (i = 0; i < header[26]; ++i)
  3925. len += lacing[i];
  3926. // this implies where the page ends
  3927. z->page_end = z->page_start + 27 + header[26] + len;
  3928. // read the last-decoded sample out of the data
  3929. z->last_decoded_sample = header[6] + (header[7] << 8) + (header[8] << 16) + (header[9] << 24);
  3930. // restore file state to where we were
  3931. set_file_offset(f, z->page_start);
  3932. return 1;
  3933. }
  3934. // rarely used function to seek back to the preceeding page while finding the
  3935. // start of a packet
  3936. static int go_to_page_before(stb_vorbis *f, unsigned int limit_offset) {
  3937. unsigned int previous_safe, end;
  3938. // now we want to seek back 64K from the limit
  3939. if (limit_offset >= 65536 && limit_offset - 65536 >= f->first_audio_page_offset)
  3940. previous_safe = limit_offset - 65536;
  3941. else
  3942. previous_safe = f->first_audio_page_offset;
  3943. set_file_offset(f, previous_safe);
  3944. while (vorbis_find_page(f, &end, NULL)) {
  3945. if (end >= limit_offset && stb_vorbis_get_file_offset(f) < limit_offset)
  3946. return 1;
  3947. set_file_offset(f, end);
  3948. }
  3949. return 0;
  3950. }
  3951. // implements the search logic for finding a page and starting decoding. if
  3952. // the function succeeds, current_loc_valid will be true and current_loc will
  3953. // be less than or equal to the provided sample number (the closer the
  3954. // better).
  3955. static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) {
  3956. ProbedPage left, right, mid;
  3957. int i, start_seg_with_known_loc, end_pos, page_start;
  3958. uint32 delta, stream_length, padding;
  3959. double offset, bytes_per_sample;
  3960. int probe = 0;
  3961. // find the last page and validate the target sample
  3962. stream_length = stb_vorbis_stream_length_in_samples(f);
  3963. if (stream_length == 0) return error(f, VORBIS_seek_without_length);
  3964. if (sample_number > stream_length) return error(f, VORBIS_seek_invalid);
  3965. // this is the maximum difference between the window-center (which is the
  3966. // actual granule position value), and the right-start (which the spec
  3967. // indicates should be the granule position (give or take one)).
  3968. padding = ((f->blocksize_1 - f->blocksize_0) >> 2);
  3969. if (sample_number < padding)
  3970. sample_number = 0;
  3971. else
  3972. sample_number -= padding;
  3973. left = f->p_first;
  3974. while (left.last_decoded_sample == ~0U) {
  3975. // (untested) the first page does not have a 'last_decoded_sample'
  3976. set_file_offset(f, left.page_end);
  3977. if (!get_seek_page_info(f, &left)) goto error;
  3978. }
  3979. right = f->p_last;
  3980. assert(right.last_decoded_sample != ~0U);
  3981. // starting from the start is handled differently
  3982. if (sample_number <= left.last_decoded_sample) {
  3983. stb_vorbis_seek_start(f);
  3984. return 1;
  3985. }
  3986. while (left.page_end != right.page_start) {
  3987. assert(left.page_end < right.page_start);
  3988. // search range in bytes
  3989. delta = right.page_start - left.page_end;
  3990. if (delta <= 65536) {
  3991. // there's only 64K left to search - handle it linearly
  3992. set_file_offset(f, left.page_end);
  3993. } else {
  3994. if (probe < 2) {
  3995. if (probe == 0) {
  3996. // first probe (interpolate)
  3997. double data_bytes = right.page_end - left.page_start;
  3998. bytes_per_sample = data_bytes / right.last_decoded_sample;
  3999. offset = left.page_start + bytes_per_sample * (sample_number - left.last_decoded_sample);
  4000. } else {
  4001. // second probe (try to bound the other side)
  4002. double error = ((double) sample_number - mid.last_decoded_sample) * bytes_per_sample;
  4003. if (error >= 0 && error < 8000) error = 8000;
  4004. if (error < 0 && error > -8000) error = -8000;
  4005. offset += error * 2;
  4006. }
  4007. // ensure the offset is valid
  4008. if (offset < left.page_end)
  4009. offset = left.page_end;
  4010. if (offset > right.page_start - 65536)
  4011. offset = right.page_start - 65536;
  4012. set_file_offset(f, (unsigned int) offset);
  4013. } else {
  4014. // binary search for large ranges (offset by 32K to ensure
  4015. // we don't hit the right page)
  4016. set_file_offset(f, left.page_end + (delta / 2) - 32768);
  4017. }
  4018. if (!vorbis_find_page(f, NULL, NULL)) goto error;
  4019. }
  4020. for (;;) {
  4021. if (!get_seek_page_info(f, &mid)) goto error;
  4022. if (mid.last_decoded_sample != ~0U) break;
  4023. // (untested) no frames end on this page
  4024. set_file_offset(f, mid.page_end);
  4025. assert(mid.page_start < right.page_start);
  4026. }
  4027. // if we've just found the last page again then we're in a tricky file,
  4028. // and we're close enough.
  4029. if (mid.page_start == right.page_start)
  4030. break;
  4031. if (sample_number < mid.last_decoded_sample)
  4032. right = mid;
  4033. else
  4034. left = mid;
  4035. ++probe;
  4036. }
  4037. // seek back to start of the last packet
  4038. page_start = left.page_start;
  4039. set_file_offset(f, page_start);
  4040. if (!start_page(f)) return error(f, VORBIS_seek_failed);
  4041. end_pos = f->end_seg_with_known_loc;
  4042. assert(end_pos >= 0);
  4043. for (;;) {
  4044. for (i = end_pos; i > 0; --i)
  4045. if (f->segments[i - 1] != 255)
  4046. break;
  4047. start_seg_with_known_loc = i;
  4048. if (start_seg_with_known_loc > 0 || !(f->page_flag & PAGEFLAG_continued_packet))
  4049. break;
  4050. // (untested) the final packet begins on an earlier page
  4051. if (!go_to_page_before(f, page_start))
  4052. goto error;
  4053. page_start = stb_vorbis_get_file_offset(f);
  4054. if (!start_page(f)) goto error;
  4055. end_pos = f->segment_count - 1;
  4056. }
  4057. // prepare to start decoding
  4058. f->current_loc_valid = FALSE;
  4059. f->last_seg = FALSE;
  4060. f->valid_bits = 0;
  4061. f->packet_bytes = 0;
  4062. f->bytes_in_seg = 0;
  4063. f->previous_length = 0;
  4064. f->next_seg = start_seg_with_known_loc;
  4065. for (i = 0; i < start_seg_with_known_loc; i++)
  4066. skip(f, f->segments[i]);
  4067. // start decoding (optimizable - this frame is generally discarded)
  4068. vorbis_pump_first_frame(f);
  4069. return 1;
  4070. error:
  4071. // try to restore the file to a valid state
  4072. stb_vorbis_seek_start(f);
  4073. return error(f, VORBIS_seek_failed);
  4074. }
  4075. // the same as vorbis_decode_initial, but without advancing
  4076. static int peek_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) {
  4077. int bits_read, bytes_read;
  4078. if (!vorbis_decode_initial(f, p_left_start, p_left_end, p_right_start, p_right_end, mode))
  4079. return 0;
  4080. // either 1 or 2 bytes were read, figure out which so we can rewind
  4081. bits_read = 1 + ilog(f->mode_count - 1);
  4082. if (f->mode_config[*mode].blockflag)
  4083. bits_read += 2;
  4084. bytes_read = (bits_read + 7) / 8;
  4085. f->bytes_in_seg += bytes_read;
  4086. f->packet_bytes -= bytes_read;
  4087. skip(f, -bytes_read);
  4088. if (f->next_seg == -1)
  4089. f->next_seg = f->segment_count - 1;
  4090. else
  4091. f->next_seg--;
  4092. f->valid_bits = 0;
  4093. return 1;
  4094. }
  4095. int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number) {
  4096. uint32 max_frame_samples;
  4097. if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
  4098. // fast page-level search
  4099. if (!seek_to_sample_coarse(f, sample_number))
  4100. return 0;
  4101. assert(f->current_loc_valid);
  4102. assert(f->current_loc <= sample_number);
  4103. // linear search for the relevant packet
  4104. max_frame_samples = (f->blocksize_1 * 3 - f->blocksize_0) >> 2;
  4105. while (f->current_loc < sample_number) {
  4106. int left_start, left_end, right_start, right_end, mode, frame_samples;
  4107. if (!peek_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode))
  4108. return error(f, VORBIS_seek_failed);
  4109. // calculate the number of samples returned by the next frame
  4110. frame_samples = right_start - left_start;
  4111. if (f->current_loc + frame_samples > sample_number) {
  4112. return 1; // the next frame will contain the sample
  4113. } else if (f->current_loc + frame_samples + max_frame_samples > sample_number) {
  4114. // there's a chance the frame after this could contain the sample
  4115. vorbis_pump_first_frame(f);
  4116. } else {
  4117. // this frame is too early to be relevant
  4118. f->current_loc += frame_samples;
  4119. f->previous_length = 0;
  4120. maybe_start_packet(f);
  4121. flush_packet(f);
  4122. }
  4123. }
  4124. // the next frame will start with the sample
  4125. assert(f->current_loc == sample_number);
  4126. return 1;
  4127. }
  4128. int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number) {
  4129. if (!stb_vorbis_seek_frame(f, sample_number))
  4130. return 0;
  4131. if (sample_number != f->current_loc) {
  4132. int n;
  4133. uint32 frame_start = f->current_loc;
  4134. stb_vorbis_get_frame_float(f, &n, NULL);
  4135. assert(sample_number > frame_start);
  4136. assert(f->channel_buffer_start + (int) (sample_number - frame_start) <= f->channel_buffer_end);
  4137. f->channel_buffer_start += (sample_number - frame_start);
  4138. }
  4139. return 1;
  4140. }
  4141. void stb_vorbis_seek_start(stb_vorbis *f) {
  4142. if (IS_PUSH_MODE(f)) { error(f, VORBIS_invalid_api_mixing); return; }
  4143. set_file_offset(f, f->first_audio_page_offset);
  4144. f->previous_length = 0;
  4145. f->first_decode = TRUE;
  4146. f->next_seg = -1;
  4147. vorbis_pump_first_frame(f);
  4148. }
  4149. unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) {
  4150. unsigned int restore_offset, previous_safe;
  4151. unsigned int end, last_page_loc;
  4152. if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
  4153. if (!f->total_samples) {
  4154. unsigned int last;
  4155. uint32 lo, hi;
  4156. char header[6];
  4157. // first, store the current decode position so we can restore it
  4158. restore_offset = stb_vorbis_get_file_offset(f);
  4159. // now we want to seek back 64K from the end (the last page must
  4160. // be at most a little less than 64K, but let's allow a little slop)
  4161. if (f->stream_len >= 65536 && f->stream_len - 65536 >= f->first_audio_page_offset)
  4162. previous_safe = f->stream_len - 65536;
  4163. else
  4164. previous_safe = f->first_audio_page_offset;
  4165. set_file_offset(f, previous_safe);
  4166. // previous_safe is now our candidate 'earliest known place that seeking
  4167. // to will lead to the final page'
  4168. if (!vorbis_find_page(f, &end, &last)) {
  4169. // if we can't find a page, we're hosed!
  4170. f->error = VORBIS_cant_find_last_page;
  4171. f->total_samples = 0xffffffff;
  4172. goto done;
  4173. }
  4174. // check if there are more pages
  4175. last_page_loc = stb_vorbis_get_file_offset(f);
  4176. // stop when the last_page flag is set, not when we reach eof;
  4177. // this allows us to stop short of a 'file_section' end without
  4178. // explicitly checking the length of the section
  4179. while (!last) {
  4180. set_file_offset(f, end);
  4181. if (!vorbis_find_page(f, &end, &last)) {
  4182. // the last page we found didn't have the 'last page' flag
  4183. // set. whoops!
  4184. break;
  4185. }
  4186. previous_safe = last_page_loc + 1;
  4187. last_page_loc = stb_vorbis_get_file_offset(f);
  4188. }
  4189. set_file_offset(f, last_page_loc);
  4190. // parse the header
  4191. getn(f, (unsigned char *) header, 6);
  4192. // extract the absolute granule position
  4193. lo = get32(f);
  4194. hi = get32(f);
  4195. if (lo == 0xffffffff && hi == 0xffffffff) {
  4196. f->error = VORBIS_cant_find_last_page;
  4197. f->total_samples = SAMPLE_unknown;
  4198. goto done;
  4199. }
  4200. if (hi)
  4201. lo = 0xfffffffe; // saturate
  4202. f->total_samples = lo;
  4203. f->p_last.page_start = last_page_loc;
  4204. f->p_last.page_end = end;
  4205. f->p_last.last_decoded_sample = lo;
  4206. done:
  4207. set_file_offset(f, restore_offset);
  4208. }
  4209. return f->total_samples == SAMPLE_unknown ? 0 : f->total_samples;
  4210. }
  4211. float stb_vorbis_stream_length_in_seconds(stb_vorbis *f) {
  4212. return stb_vorbis_stream_length_in_samples(f) / (float) f->sample_rate;
  4213. }
  4214. int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output) {
  4215. int len, right, left, i;
  4216. if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
  4217. if (!vorbis_decode_packet(f, &len, &left, &right)) {
  4218. f->channel_buffer_start = f->channel_buffer_end = 0;
  4219. return 0;
  4220. }
  4221. len = vorbis_finish_frame(f, len, left, right);
  4222. for (i = 0; i < f->channels; ++i)
  4223. f->outputs[i] = f->channel_buffers[i] + left;
  4224. f->channel_buffer_start = left;
  4225. f->channel_buffer_end = left + len;
  4226. if (channels) *channels = f->channels;
  4227. if (output) *output = f->outputs;
  4228. return len;
  4229. }
  4230. #ifndef STB_VORBIS_NO_STDIO
  4231. stb_vorbis * stb_vorbis_open_file_section(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length) {
  4232. stb_vorbis *f, p;
  4233. vorbis_init(&p, alloc);
  4234. p.f = file;
  4235. p.f_start = (uint32) ftell(file);
  4236. p.stream_len = length;
  4237. p.close_on_free = close_on_free;
  4238. if (start_decoder(&p)) {
  4239. f = vorbis_alloc(&p);
  4240. if (f) {
  4241. *f = p;
  4242. vorbis_pump_first_frame(f);
  4243. return f;
  4244. }
  4245. }
  4246. if (error) *error = p.error;
  4247. vorbis_deinit(&p);
  4248. return NULL;
  4249. }
  4250. stb_vorbis * stb_vorbis_open_file(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc) {
  4251. unsigned int len, start;
  4252. start = (unsigned int) ftell(file);
  4253. fseek(file, 0, SEEK_END);
  4254. len = (unsigned int) (ftell(file) - start);
  4255. fseek(file, start, SEEK_SET);
  4256. return stb_vorbis_open_file_section(file, close_on_free, error, alloc, len);
  4257. }
  4258. stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc) {
  4259. FILE *f = fopen(filename, "rb");
  4260. if (f)
  4261. return stb_vorbis_open_file(f, TRUE, error, alloc);
  4262. if (error) *error = VORBIS_file_open_failure;
  4263. return NULL;
  4264. }
  4265. #endif // STB_VORBIS_NO_STDIO
  4266. stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc) {
  4267. stb_vorbis *f, p;
  4268. if (data == NULL) return NULL;
  4269. vorbis_init(&p, alloc);
  4270. p.stream = (uint8 *) data;
  4271. p.stream_end = (uint8 *) data + len;
  4272. p.stream_start = (uint8 *) p.stream;
  4273. p.stream_len = len;
  4274. p.push_mode = FALSE;
  4275. if (start_decoder(&p)) {
  4276. f = vorbis_alloc(&p);
  4277. if (f) {
  4278. *f = p;
  4279. vorbis_pump_first_frame(f);
  4280. return f;
  4281. }
  4282. }
  4283. if (error) *error = p.error;
  4284. vorbis_deinit(&p);
  4285. return NULL;
  4286. }
  4287. #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
  4288. #define PLAYBACK_MONO 1
  4289. #define PLAYBACK_LEFT 2
  4290. #define PLAYBACK_RIGHT 4
  4291. #define L (PLAYBACK_LEFT | PLAYBACK_MONO)
  4292. #define C (PLAYBACK_LEFT | PLAYBACK_RIGHT | PLAYBACK_MONO)
  4293. #define R (PLAYBACK_RIGHT | PLAYBACK_MONO)
  4294. static int8 channel_position[7][6] =
  4295. {
  4296. {0},
  4297. {C},
  4298. {L, R},
  4299. {L, C, R},
  4300. {L, R, L, R},
  4301. {L, C, R, L, R},
  4302. {L, C, R, L, R, C},
  4303. };
  4304. #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
  4305. typedef union {
  4306. float f;
  4307. int i;
  4308. } float_conv;
  4309. typedef char stb_vorbis_float_size_test[sizeof(float) == 4 && sizeof(int) == 4];
  4310. #define FASTDEF(x) float_conv x
  4311. // add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round
  4312. #define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT))
  4313. #define ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22))
  4314. #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + MAGIC(s), temp.i - ADDEND(s))
  4315. #define check_endianness()
  4316. #else
  4317. #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s))))
  4318. #define check_endianness()
  4319. #define FASTDEF(x)
  4320. #endif
  4321. static void copy_samples(short *dest, float *src, int len) {
  4322. int i;
  4323. check_endianness();
  4324. for (i = 0; i < len; ++i) {
  4325. FASTDEF(temp);
  4326. int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i], 15);
  4327. if ((unsigned int) (v + 32768) > 65535)
  4328. v = v < 0 ? -32768 : 32767;
  4329. dest[i] = v;
  4330. }
  4331. }
  4332. static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len) {
  4333. #define BUFFER_SIZE 32
  4334. float buffer[BUFFER_SIZE];
  4335. int i, j, o, n = BUFFER_SIZE;
  4336. check_endianness();
  4337. for (o = 0; o < len; o += BUFFER_SIZE) {
  4338. memset(buffer, 0, sizeof(buffer));
  4339. if (o + n > len) n = len - o;
  4340. for (j = 0; j < num_c; ++j) {
  4341. if (channel_position[num_c][j] & mask) {
  4342. for (i = 0; i < n; ++i)
  4343. buffer[i] += data[j][d_offset + o + i];
  4344. }
  4345. }
  4346. for (i = 0; i < n; ++i) {
  4347. FASTDEF(temp);
  4348. int v = FAST_SCALED_FLOAT_TO_INT(temp, buffer[i], 15);
  4349. if ((unsigned int) (v + 32768) > 65535)
  4350. v = v < 0 ? -32768 : 32767;
  4351. output[o + i] = v;
  4352. }
  4353. }
  4354. }
  4355. static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len) {
  4356. #define BUFFER_SIZE 32
  4357. float buffer[BUFFER_SIZE];
  4358. int i, j, o, n = BUFFER_SIZE >> 1;
  4359. // o is the offset in the source data
  4360. check_endianness();
  4361. for (o = 0; o < len; o += BUFFER_SIZE >> 1) {
  4362. // o2 is the offset in the output data
  4363. int o2 = o << 1;
  4364. memset(buffer, 0, sizeof(buffer));
  4365. if (o + n > len) n = len - o;
  4366. for (j = 0; j < num_c; ++j) {
  4367. int m = channel_position[num_c][j] & (PLAYBACK_LEFT | PLAYBACK_RIGHT);
  4368. if (m == (PLAYBACK_LEFT | PLAYBACK_RIGHT)) {
  4369. for (i = 0; i < n; ++i) {
  4370. buffer[i * 2 + 0] += data[j][d_offset + o + i];
  4371. buffer[i * 2 + 1] += data[j][d_offset + o + i];
  4372. }
  4373. } else if (m == PLAYBACK_LEFT) {
  4374. for (i = 0; i < n; ++i) {
  4375. buffer[i * 2 + 0] += data[j][d_offset + o + i];
  4376. }
  4377. } else if (m == PLAYBACK_RIGHT) {
  4378. for (i = 0; i < n; ++i) {
  4379. buffer[i * 2 + 1] += data[j][d_offset + o + i];
  4380. }
  4381. }
  4382. }
  4383. for (i = 0; i < (n << 1); ++i) {
  4384. FASTDEF(temp);
  4385. int v = FAST_SCALED_FLOAT_TO_INT(temp, buffer[i], 15);
  4386. if ((unsigned int) (v + 32768) > 65535)
  4387. v = v < 0 ? -32768 : 32767;
  4388. output[o2 + i] = v;
  4389. }
  4390. }
  4391. }
  4392. static void convert_samples_short(int buf_c, short **buffer, int b_offset, int data_c, float **data, int d_offset, int samples) {
  4393. int i;
  4394. if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
  4395. static int channel_selector[3][2] = {{0},{PLAYBACK_MONO},{PLAYBACK_LEFT, PLAYBACK_RIGHT}};
  4396. for (i = 0; i < buf_c; ++i)
  4397. compute_samples(channel_selector[buf_c][i], buffer[i] + b_offset, data_c, data, d_offset, samples);
  4398. } else {
  4399. int limit = buf_c < data_c ? buf_c : data_c;
  4400. for (i = 0; i < limit; ++i)
  4401. copy_samples(buffer[i] + b_offset, data[i] + d_offset, samples);
  4402. for (; i < buf_c; ++i)
  4403. memset(buffer[i] + b_offset, 0, sizeof(short) * samples);
  4404. }
  4405. }
  4406. int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples) {
  4407. float **output;
  4408. int len = stb_vorbis_get_frame_float(f, NULL, &output);
  4409. if (len > num_samples) len = num_samples;
  4410. if (len)
  4411. convert_samples_short(num_c, buffer, 0, f->channels, output, 0, len);
  4412. return len;
  4413. }
  4414. static void convert_channels_short_interleaved(int buf_c, short *buffer, int data_c, float **data, int d_offset, int len) {
  4415. int i;
  4416. check_endianness();
  4417. if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
  4418. assert(buf_c == 2);
  4419. for (i = 0; i < buf_c; ++i)
  4420. compute_stereo_samples(buffer, data_c, data, d_offset, len);
  4421. } else {
  4422. int limit = buf_c < data_c ? buf_c : data_c;
  4423. int j;
  4424. for (j = 0; j < len; ++j) {
  4425. for (i = 0; i < limit; ++i) {
  4426. FASTDEF(temp);
  4427. float f = data[i][d_offset + j];
  4428. int v = FAST_SCALED_FLOAT_TO_INT(temp, f, 15);//data[i][d_offset+j],15);
  4429. if ((unsigned int) (v + 32768) > 65535)
  4430. v = v < 0 ? -32768 : 32767;
  4431. *buffer++ = v;
  4432. }
  4433. for (; i < buf_c; ++i)
  4434. *buffer++ = 0;
  4435. }
  4436. }
  4437. }
  4438. int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts) {
  4439. float **output;
  4440. int len;
  4441. if (num_c == 1) return stb_vorbis_get_frame_short(f, num_c, &buffer, num_shorts);
  4442. len = stb_vorbis_get_frame_float(f, NULL, &output);
  4443. if (len) {
  4444. if (len*num_c > num_shorts) len = num_shorts / num_c;
  4445. convert_channels_short_interleaved(num_c, buffer, f->channels, output, 0, len);
  4446. }
  4447. return len;
  4448. }
  4449. int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts) {
  4450. float **outputs;
  4451. int len = num_shorts / channels;
  4452. int n = 0;
  4453. int z = f->channels;
  4454. if (z > channels) z = channels;
  4455. while (n < len) {
  4456. int k = f->channel_buffer_end - f->channel_buffer_start;
  4457. if (n + k >= len) k = len - n;
  4458. if (k)
  4459. convert_channels_short_interleaved(channels, buffer, f->channels, f->channel_buffers, f->channel_buffer_start, k);
  4460. buffer += k*channels;
  4461. n += k;
  4462. f->channel_buffer_start += k;
  4463. if (n == len) break;
  4464. if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
  4465. }
  4466. return n;
  4467. }
  4468. int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len) {
  4469. float **outputs;
  4470. int n = 0;
  4471. int z = f->channels;
  4472. if (z > channels) z = channels;
  4473. while (n < len) {
  4474. int k = f->channel_buffer_end - f->channel_buffer_start;
  4475. if (n + k >= len) k = len - n;
  4476. if (k)
  4477. convert_samples_short(channels, buffer, n, f->channels, f->channel_buffers, f->channel_buffer_start, k);
  4478. n += k;
  4479. f->channel_buffer_start += k;
  4480. if (n == len) break;
  4481. if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
  4482. }
  4483. return n;
  4484. }
  4485. #ifndef STB_VORBIS_NO_STDIO
  4486. int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output) {
  4487. int data_len, offset, total, limit, error;
  4488. short *data;
  4489. stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL);
  4490. if (v == NULL) return -1;
  4491. limit = v->channels * 4096;
  4492. *channels = v->channels;
  4493. if (sample_rate)
  4494. *sample_rate = v->sample_rate;
  4495. offset = data_len = 0;
  4496. total = limit;
  4497. data = (short *) malloc(total * sizeof(*data));
  4498. if (data == NULL) {
  4499. stb_vorbis_close(v);
  4500. return -2;
  4501. }
  4502. for (;;) {
  4503. int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data + offset, total - offset);
  4504. if (n == 0) break;
  4505. data_len += n;
  4506. offset += n * v->channels;
  4507. if (offset + limit > total) {
  4508. short *data2;
  4509. total *= 2;
  4510. data2 = (short *) realloc(data, total * sizeof(*data));
  4511. if (data2 == NULL) {
  4512. free(data);
  4513. stb_vorbis_close(v);
  4514. return -2;
  4515. }
  4516. data = data2;
  4517. }
  4518. }
  4519. *output = data;
  4520. stb_vorbis_close(v);
  4521. return data_len;
  4522. }
  4523. #endif // NO_STDIO
  4524. int stb_vorbis_decode_memory(const uint8 *mem, int len, int *channels, int *sample_rate, short **output) {
  4525. int data_len, offset, total, limit, error;
  4526. short *data;
  4527. stb_vorbis *v = stb_vorbis_open_memory(mem, len, &error, NULL);
  4528. if (v == NULL) return -1;
  4529. limit = v->channels * 4096;
  4530. *channels = v->channels;
  4531. if (sample_rate)
  4532. *sample_rate = v->sample_rate;
  4533. offset = data_len = 0;
  4534. total = limit;
  4535. data = (short *) malloc(total * sizeof(*data));
  4536. if (data == NULL) {
  4537. stb_vorbis_close(v);
  4538. return -2;
  4539. }
  4540. for (;;) {
  4541. int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data + offset, total - offset);
  4542. if (n == 0) break;
  4543. data_len += n;
  4544. offset += n * v->channels;
  4545. if (offset + limit > total) {
  4546. short *data2;
  4547. total *= 2;
  4548. data2 = (short *) realloc(data, total * sizeof(*data));
  4549. if (data2 == NULL) {
  4550. free(data);
  4551. stb_vorbis_close(v);
  4552. return -2;
  4553. }
  4554. data = data2;
  4555. }
  4556. }
  4557. *output = data;
  4558. stb_vorbis_close(v);
  4559. return data_len;
  4560. }
  4561. #endif // STB_VORBIS_NO_INTEGER_CONVERSION
  4562. int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats) {
  4563. float **outputs;
  4564. int len = num_floats / channels;
  4565. int n = 0;
  4566. int z = f->channels;
  4567. if (z > channels) z = channels;
  4568. while (n < len) {
  4569. int i, j;
  4570. int k = f->channel_buffer_end - f->channel_buffer_start;
  4571. if (n + k >= len) k = len - n;
  4572. for (j = 0; j < k; ++j) {
  4573. for (i = 0; i < z; ++i)
  4574. *buffer++ = f->channel_buffers[i][f->channel_buffer_start + j];
  4575. for (; i < channels; ++i)
  4576. *buffer++ = 0;
  4577. }
  4578. n += k;
  4579. f->channel_buffer_start += k;
  4580. if (n == len)
  4581. break;
  4582. if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
  4583. break;
  4584. }
  4585. return n;
  4586. }
  4587. int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples) {
  4588. float **outputs;
  4589. int n = 0;
  4590. int z = f->channels;
  4591. if (z > channels) z = channels;
  4592. while (n < num_samples) {
  4593. int i;
  4594. int k = f->channel_buffer_end - f->channel_buffer_start;
  4595. if (n + k >= num_samples) k = num_samples - n;
  4596. if (k) {
  4597. for (i = 0; i < z; ++i)
  4598. memcpy(buffer[i] + n, f->channel_buffers[i] + f->channel_buffer_start, sizeof(float)*k);
  4599. for (; i < channels; ++i)
  4600. memset(buffer[i] + n, 0, sizeof(float) * k);
  4601. }
  4602. n += k;
  4603. f->channel_buffer_start += k;
  4604. if (n == num_samples)
  4605. break;
  4606. if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
  4607. break;
  4608. }
  4609. return n;
  4610. }
  4611. #endif // STB_VORBIS_NO_PULLDATA_API
  4612. /* Version history
  4613. 1.09 - 2016/04/04 - back out 'avoid discarding last frame' fix from previous version
  4614. 1.08 - 2016/04/02 - fixed multiple warnings; fix setup memory leaks;
  4615. avoid discarding last frame of audio data
  4616. 1.07 - 2015/01/16 - fixed some warnings, fix mingw, const-correct API
  4617. some more crash fixes when out of memory or with corrupt files
  4618. 1.06 - 2015/08/31 - full, correct support for seeking API (Dougall Johnson)
  4619. some crash fixes when out of memory or with corrupt files
  4620. 1.05 - 2015/04/19 - don't define __forceinline if it's redundant
  4621. 1.04 - 2014/08/27 - fix missing const-correct case in API
  4622. 1.03 - 2014/08/07 - Warning fixes
  4623. 1.02 - 2014/07/09 - Declare qsort compare function _cdecl on windows
  4624. 1.01 - 2014/06/18 - fix stb_vorbis_get_samples_float
  4625. 1.0 - 2014/05/26 - fix memory leaks; fix warnings; fix bugs in multichannel
  4626. (API change) report sample rate for decode-full-file funcs
  4627. 0.99996 - bracket #include <malloc.h> for macintosh compilation by Laurent Gomila
  4628. 0.99995 - use union instead of pointer-cast for fast-float-to-int to avoid alias-optimization problem
  4629. 0.99994 - change fast-float-to-int to work in single-precision FPU mode, remove endian-dependence
  4630. 0.99993 - remove assert that fired on legal files with empty tables
  4631. 0.99992 - rewind-to-start
  4632. 0.99991 - bugfix to stb_vorbis_get_samples_short by Bernhard Wodo
  4633. 0.9999 - (should have been 0.99990) fix no-CRT support, compiling as C++
  4634. 0.9998 - add a full-decode function with a memory source
  4635. 0.9997 - fix a bug in the read-from-FILE case in 0.9996 addition
  4636. 0.9996 - query length of vorbis stream in samples/seconds
  4637. 0.9995 - bugfix to another optimization that only happened in certain files
  4638. 0.9994 - bugfix to one of the optimizations that caused significant (but inaudible?) errors
  4639. 0.9993 - performance improvements; runs in 99% to 104% of time of reference implementation
  4640. 0.9992 - performance improvement of IMDCT; now performs close to reference implementation
  4641. 0.9991 - performance improvement of IMDCT
  4642. 0.999 - (should have been 0.9990) performance improvement of IMDCT
  4643. 0.998 - no-CRT support from Casey Muratori
  4644. 0.997 - bugfixes for bugs found by Terje Mathisen
  4645. 0.996 - bugfix: fast-huffman decode initialized incorrectly for sparse codebooks; fixing gives 10% speedup - found by Terje Mathisen
  4646. 0.995 - bugfix: fix to 'effective' overrun detection - found by Terje Mathisen
  4647. 0.994 - bugfix: garbage decode on final VQ symbol of a non-multiple - found by Terje Mathisen
  4648. 0.993 - bugfix: pushdata API required 1 extra byte for empty page (failed to consume final page if empty) - found by Terje Mathisen
  4649. 0.992 - fixes for MinGW warning
  4650. 0.991 - turn fast-float-conversion on by default
  4651. 0.990 - fix push-mode seek recovery if you seek into the headers
  4652. 0.98b - fix to bad release of 0.98
  4653. 0.98 - fix push-mode seek recovery; robustify float-to-int and support non-fast mode
  4654. 0.97 - builds under c++ (typecasting, don't use 'class' keyword)
  4655. 0.96 - somehow MY 0.95 was right, but the web one was wrong, so here's my 0.95 rereleased as 0.96, fixes a typo in the clamping code
  4656. 0.95 - clamping code for 16-bit functions
  4657. 0.94 - not publically released
  4658. 0.93 - fixed all-zero-floor case (was decoding garbage)
  4659. 0.92 - fixed a memory leak
  4660. 0.91 - conditional compiles to omit parts of the API and the infrastructure to support them: STB_VORBIS_NO_PULLDATA_API, STB_VORBIS_NO_PUSHDATA_API, STB_VORBIS_NO_STDIO, STB_VORBIS_NO_INTEGER_CONVERSION
  4661. 0.90 - first public release
  4662. */
  4663. #endif // STB_VORBIS_HEADER_ONLY