stb_vorbis.h 170 KB

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