stb_vorbis.h 180 KB

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