lame.all.js 518 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519
  1. function lamejs() {
  2. function new_byte(count) {
  3. return new Int8Array(count);
  4. }
  5. function new_short(count) {
  6. return new Int16Array(count);
  7. }
  8. function new_int(count) {
  9. return new Int32Array(count);
  10. }
  11. function new_float(count) {
  12. return new Float32Array(count);
  13. }
  14. function new_double(count) {
  15. return new Float64Array(count);
  16. }
  17. function new_float_n(args) {
  18. if (args.length == 1) {
  19. return new_float(args[0]);
  20. }
  21. var sz = args[0];
  22. args = args.slice(1);
  23. var A = [];
  24. for (var i = 0; i < sz; i++) {
  25. A.push(new_float_n(args));
  26. }
  27. return A;
  28. }
  29. function new_int_n(args) {
  30. if (args.length == 1) {
  31. return new_int(args[0]);
  32. }
  33. var sz = args[0];
  34. args = args.slice(1);
  35. var A = [];
  36. for (var i = 0; i < sz; i++) {
  37. A.push(new_int_n(args));
  38. }
  39. return A;
  40. }
  41. function new_short_n(args) {
  42. if (args.length == 1) {
  43. return new_short(args[0]);
  44. }
  45. var sz = args[0];
  46. args = args.slice(1);
  47. var A = [];
  48. for (var i = 0; i < sz; i++) {
  49. A.push(new_short_n(args));
  50. }
  51. return A;
  52. }
  53. function new_array_n(args) {
  54. if (args.length == 1) {
  55. return new Array(args[0]);
  56. }
  57. var sz = args[0];
  58. args = args.slice(1);
  59. var A = [];
  60. for (var i = 0; i < sz; i++) {
  61. A.push(new_array_n(args));
  62. }
  63. return A;
  64. }
  65. var Arrays = {};
  66. Arrays.fill = function (a, fromIndex, toIndex, val) {
  67. if (arguments.length == 2) {
  68. for (var i = 0; i < a.length; i++) {
  69. a[i] = arguments[1];
  70. }
  71. } else {
  72. for (var i = fromIndex; i < toIndex; i++) {
  73. a[i] = val;
  74. }
  75. }
  76. };
  77. var System = {};
  78. System.arraycopy = function (src, srcPos, dest, destPos, length) {
  79. var srcEnd = srcPos + length;
  80. while (srcPos < srcEnd)
  81. dest[destPos++] = src[srcPos++];
  82. };
  83. var Util = {};
  84. Util.SQRT2 = 1.41421356237309504880;
  85. Util.FAST_LOG10 = function (x) {
  86. return Math.log10(x);
  87. };
  88. Util.FAST_LOG10_X = function (x, y) {
  89. return Math.log10(x) * y;
  90. };
  91. function ShortBlock(ordinal) {
  92. this.ordinal = ordinal;
  93. }
  94. /**
  95. * LAME may use them, even different block types for L/R.
  96. */
  97. ShortBlock.short_block_allowed = new ShortBlock(0);
  98. /**
  99. * LAME may use them, but always same block types in L/R.
  100. */
  101. ShortBlock.short_block_coupled = new ShortBlock(1);
  102. /**
  103. * LAME will not use short blocks, long blocks only.
  104. */
  105. ShortBlock.short_block_dispensed = new ShortBlock(2);
  106. /**
  107. * LAME will not use long blocks, short blocks only.
  108. */
  109. ShortBlock.short_block_forced = new ShortBlock(3);
  110. var Float = {};
  111. Float.MAX_VALUE = 3.4028235e+38;
  112. function VbrMode(ordinal) {
  113. this.ordinal = ordinal;
  114. }
  115. VbrMode.vbr_off = new VbrMode(0);
  116. VbrMode.vbr_mt = new VbrMode(1);
  117. VbrMode.vbr_rh = new VbrMode(2);
  118. VbrMode.vbr_abr = new VbrMode(3);
  119. VbrMode.vbr_mtrh = new VbrMode(4);
  120. VbrMode.vbr_default = VbrMode.vbr_mtrh;
  121. var assert = function (x) {
  122. //console.assert(x);
  123. };
  124. var module_exports = {
  125. "System": System,
  126. "VbrMode": VbrMode,
  127. "Float": Float,
  128. "ShortBlock": ShortBlock,
  129. "Util": Util,
  130. "Arrays": Arrays,
  131. "new_array_n": new_array_n,
  132. "new_byte": new_byte,
  133. "new_double": new_double,
  134. "new_float": new_float,
  135. "new_float_n": new_float_n,
  136. "new_int": new_int,
  137. "new_int_n": new_int_n,
  138. "new_short": new_short,
  139. "new_short_n": new_short_n,
  140. "assert": assert
  141. };
  142. //package mp3;
  143. /* MPEG modes */
  144. function MPEGMode(ordinal) {
  145. var _ordinal = ordinal;
  146. this.ordinal = function () {
  147. return _ordinal;
  148. }
  149. }
  150. MPEGMode.STEREO = new MPEGMode(0);
  151. MPEGMode.JOINT_STEREO = new MPEGMode(1);
  152. MPEGMode.DUAL_CHANNEL = new MPEGMode(2);
  153. MPEGMode.MONO = new MPEGMode(3);
  154. MPEGMode.NOT_SET = new MPEGMode(4);
  155. function Version() {
  156. /**
  157. * URL for the LAME website.
  158. */
  159. var LAME_URL = "http://www.mp3dev.org/";
  160. /**
  161. * Major version number.
  162. */
  163. var LAME_MAJOR_VERSION = 3;
  164. /**
  165. * Minor version number.
  166. */
  167. var LAME_MINOR_VERSION = 98;
  168. /**
  169. * Patch level.
  170. */
  171. var LAME_PATCH_VERSION = 4;
  172. /**
  173. * Major version number.
  174. */
  175. var PSY_MAJOR_VERSION = 0;
  176. /**
  177. * Minor version number.
  178. */
  179. var PSY_MINOR_VERSION = 93;
  180. /**
  181. * A string which describes the version of LAME.
  182. *
  183. * @return string which describes the version of LAME
  184. */
  185. this.getLameVersion = function () {
  186. // primary to write screen reports
  187. return (LAME_MAJOR_VERSION + "." + LAME_MINOR_VERSION + "." + LAME_PATCH_VERSION);
  188. }
  189. /**
  190. * The short version of the LAME version string.
  191. *
  192. * @return short version of the LAME version string
  193. */
  194. this.getLameShortVersion = function () {
  195. // Adding date and time to version string makes it harder for output
  196. // validation
  197. return (LAME_MAJOR_VERSION + "." + LAME_MINOR_VERSION + "." + LAME_PATCH_VERSION);
  198. }
  199. /**
  200. * The shortest version of the LAME version string.
  201. *
  202. * @return shortest version of the LAME version string
  203. */
  204. this.getLameVeryShortVersion = function () {
  205. // Adding date and time to version string makes it harder for output
  206. return ("LAME" + LAME_MAJOR_VERSION + "." + LAME_MINOR_VERSION + "r");
  207. }
  208. /**
  209. * String which describes the version of GPSYCHO
  210. *
  211. * @return string which describes the version of GPSYCHO
  212. */
  213. this.getPsyVersion = function () {
  214. return (PSY_MAJOR_VERSION + "." + PSY_MINOR_VERSION);
  215. }
  216. /**
  217. * String which is a URL for the LAME website.
  218. *
  219. * @return string which is a URL for the LAME website
  220. */
  221. this.getLameUrl = function () {
  222. return LAME_URL;
  223. }
  224. /**
  225. * Quite useless for a java version, however we are compatible ;-)
  226. *
  227. * @return "32bits"
  228. */
  229. this.getLameOsBitness = function () {
  230. return "32bits";
  231. }
  232. }
  233. /*
  234. * ReplayGainAnalysis - analyzes input samples and give the recommended dB change
  235. * Copyright (C) 2001 David Robinson and Glen Sawyer
  236. * Improvements and optimizations added by Frank Klemm, and by Marcel Muller
  237. *
  238. * This library is free software; you can redistribute it and/or
  239. * modify it under the terms of the GNU Lesser General Public
  240. * License as published by the Free Software Foundation; either
  241. * version 2.1 of the License, or (at your option) any later version.
  242. *
  243. * This library is distributed in the hope that it will be useful,
  244. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  245. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  246. * Lesser General Public License for more details.
  247. *
  248. * You should have received a copy of the GNU Lesser General Public
  249. * License along with this library; if not, write to the Free Software
  250. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  251. *
  252. * concept and filter values by David Robinson (David@Robinson.org)
  253. * -- blame him if you think the idea is flawed
  254. * original coding by Glen Sawyer (mp3gain@hotmail.com)
  255. * -- blame him if you think this runs too slowly, or the coding is otherwise flawed
  256. *
  257. * lots of code improvements by Frank Klemm ( http://www.uni-jena.de/~pfk/mpp/ )
  258. * -- credit him for all the _good_ programming ;)
  259. *
  260. *
  261. * For an explanation of the concepts and the basic algorithms involved, go to:
  262. * http://www.replaygain.org/
  263. */
  264. /*
  265. * Here's the deal. Call
  266. *
  267. * InitGainAnalysis ( long samplefreq );
  268. *
  269. * to initialize everything. Call
  270. *
  271. * AnalyzeSamples ( var Float_t* left_samples,
  272. * var Float_t* right_samples,
  273. * size_t num_samples,
  274. * int num_channels );
  275. *
  276. * as many times as you want, with as many or as few samples as you want.
  277. * If mono, pass the sample buffer in through left_samples, leave
  278. * right_samples NULL, and make sure num_channels = 1.
  279. *
  280. * GetTitleGain()
  281. *
  282. * will return the recommended dB level change for all samples analyzed
  283. * SINCE THE LAST TIME you called GetTitleGain() OR InitGainAnalysis().
  284. *
  285. * GetAlbumGain()
  286. *
  287. * will return the recommended dB level change for all samples analyzed
  288. * since InitGainAnalysis() was called and finalized with GetTitleGain().
  289. *
  290. * Pseudo-code to process an album:
  291. *
  292. * Float_t l_samples [4096];
  293. * Float_t r_samples [4096];
  294. * size_t num_samples;
  295. * unsigned int num_songs;
  296. * unsigned int i;
  297. *
  298. * InitGainAnalysis ( 44100 );
  299. * for ( i = 1; i <= num_songs; i++ ) {
  300. * while ( ( num_samples = getSongSamples ( song[i], left_samples, right_samples ) ) > 0 )
  301. * AnalyzeSamples ( left_samples, right_samples, num_samples, 2 );
  302. * fprintf ("Recommended dB change for song %2d: %+6.2 dB\n", i, GetTitleGain() );
  303. * }
  304. * fprintf ("Recommended dB change for whole album: %+6.2 dB\n", GetAlbumGain() );
  305. */
  306. /*
  307. * So here's the main source of potential code confusion:
  308. *
  309. * The filters applied to the incoming samples are IIR filters,
  310. * meaning they rely on up to <filter order> number of previous samples
  311. * AND up to <filter order> number of previous filtered samples.
  312. *
  313. * I set up the AnalyzeSamples routine to minimize memory usage and interface
  314. * complexity. The speed isn't compromised too much (I don't think), but the
  315. * internal complexity is higher than it should be for such a relatively
  316. * simple routine.
  317. *
  318. * Optimization/clarity suggestions are welcome.
  319. */
  320. /**
  321. * Table entries per dB
  322. */
  323. GainAnalysis.STEPS_per_dB = 100.;
  324. /**
  325. * Table entries for 0...MAX_dB (normal max. values are 70...80 dB)
  326. */
  327. GainAnalysis.MAX_dB = 120.;
  328. GainAnalysis.GAIN_NOT_ENOUGH_SAMPLES = -24601;
  329. GainAnalysis.GAIN_ANALYSIS_ERROR = 0;
  330. GainAnalysis.GAIN_ANALYSIS_OK = 1;
  331. GainAnalysis.INIT_GAIN_ANALYSIS_ERROR = 0;
  332. GainAnalysis.INIT_GAIN_ANALYSIS_OK = 1;
  333. GainAnalysis.YULE_ORDER = 10;
  334. GainAnalysis.MAX_ORDER = GainAnalysis.YULE_ORDER;
  335. GainAnalysis.MAX_SAMP_FREQ = 48000;
  336. GainAnalysis.RMS_WINDOW_TIME_NUMERATOR = 1;
  337. GainAnalysis.RMS_WINDOW_TIME_DENOMINATOR = 20;
  338. GainAnalysis.MAX_SAMPLES_PER_WINDOW = ((GainAnalysis.MAX_SAMP_FREQ * GainAnalysis.RMS_WINDOW_TIME_NUMERATOR) / GainAnalysis.RMS_WINDOW_TIME_DENOMINATOR + 1);
  339. function GainAnalysis() {
  340. /**
  341. * calibration value for 89dB
  342. */
  343. var PINK_REF = 64.82;
  344. var YULE_ORDER = GainAnalysis.YULE_ORDER;
  345. /**
  346. * percentile which is louder than the proposed level
  347. */
  348. var RMS_PERCENTILE = 0.95;
  349. /**
  350. * maximum allowed sample frequency [Hz]
  351. */
  352. var MAX_SAMP_FREQ = GainAnalysis.MAX_SAMP_FREQ;
  353. var RMS_WINDOW_TIME_NUMERATOR = GainAnalysis.RMS_WINDOW_TIME_NUMERATOR;
  354. /**
  355. * numerator / denominator = time slice size [s]
  356. */
  357. var RMS_WINDOW_TIME_DENOMINATOR = GainAnalysis.RMS_WINDOW_TIME_DENOMINATOR;
  358. /**
  359. * max. Samples per Time slice
  360. */
  361. var MAX_SAMPLES_PER_WINDOW = GainAnalysis.MAX_SAMPLES_PER_WINDOW;
  362. var ABYule = [
  363. [0.03857599435200, -3.84664617118067, -0.02160367184185,
  364. 7.81501653005538, -0.00123395316851, -11.34170355132042,
  365. -0.00009291677959, 13.05504219327545, -0.01655260341619,
  366. -12.28759895145294, 0.02161526843274, 9.48293806319790,
  367. -0.02074045215285, -5.87257861775999, 0.00594298065125,
  368. 2.75465861874613, 0.00306428023191, -0.86984376593551,
  369. 0.00012025322027, 0.13919314567432, 0.00288463683916],
  370. [0.05418656406430, -3.47845948550071, -0.02911007808948,
  371. 6.36317777566148, -0.00848709379851, -8.54751527471874,
  372. -0.00851165645469, 9.47693607801280, -0.00834990904936,
  373. -8.81498681370155, 0.02245293253339, 6.85401540936998,
  374. -0.02596338512915, -4.39470996079559, 0.01624864962975,
  375. 2.19611684890774, -0.00240879051584, -0.75104302451432,
  376. 0.00674613682247, 0.13149317958808, -0.00187763777362],
  377. [0.15457299681924, -2.37898834973084, -0.09331049056315,
  378. 2.84868151156327, -0.06247880153653, -2.64577170229825,
  379. 0.02163541888798, 2.23697657451713, -0.05588393329856,
  380. -1.67148153367602, 0.04781476674921, 1.00595954808547,
  381. 0.00222312597743, -0.45953458054983, 0.03174092540049,
  382. 0.16378164858596, -0.01390589421898, -0.05032077717131,
  383. 0.00651420667831, 0.02347897407020, -0.00881362733839],
  384. [0.30296907319327, -1.61273165137247, -0.22613988682123,
  385. 1.07977492259970, -0.08587323730772, -0.25656257754070,
  386. 0.03282930172664, -0.16276719120440, -0.00915702933434,
  387. -0.22638893773906, -0.02364141202522, 0.39120800788284,
  388. -0.00584456039913, -0.22138138954925, 0.06276101321749,
  389. 0.04500235387352, -0.00000828086748, 0.02005851806501,
  390. 0.00205861885564, 0.00302439095741, -0.02950134983287],
  391. [0.33642304856132, -1.49858979367799, -0.25572241425570,
  392. 0.87350271418188, -0.11828570177555, 0.12205022308084,
  393. 0.11921148675203, -0.80774944671438, -0.07834489609479,
  394. 0.47854794562326, -0.00469977914380, -0.12453458140019,
  395. -0.00589500224440, -0.04067510197014, 0.05724228140351,
  396. 0.08333755284107, 0.00832043980773, -0.04237348025746,
  397. -0.01635381384540, 0.02977207319925, -0.01760176568150],
  398. [0.44915256608450, -0.62820619233671, -0.14351757464547,
  399. 0.29661783706366, -0.22784394429749, -0.37256372942400,
  400. -0.01419140100551, 0.00213767857124, 0.04078262797139,
  401. -0.42029820170918, -0.12398163381748, 0.22199650564824,
  402. 0.04097565135648, 0.00613424350682, 0.10478503600251,
  403. 0.06747620744683, -0.01863887810927, 0.05784820375801,
  404. -0.03193428438915, 0.03222754072173, 0.00541907748707],
  405. [0.56619470757641, -1.04800335126349, -0.75464456939302,
  406. 0.29156311971249, 0.16242137742230, -0.26806001042947,
  407. 0.16744243493672, 0.00819999645858, -0.18901604199609,
  408. 0.45054734505008, 0.30931782841830, -0.33032403314006,
  409. -0.27562961986224, 0.06739368333110, 0.00647310677246,
  410. -0.04784254229033, 0.08647503780351, 0.01639907836189,
  411. -0.03788984554840, 0.01807364323573, -0.00588215443421],
  412. [0.58100494960553, -0.51035327095184, -0.53174909058578,
  413. -0.31863563325245, -0.14289799034253, -0.20256413484477,
  414. 0.17520704835522, 0.14728154134330, 0.02377945217615,
  415. 0.38952639978999, 0.15558449135573, -0.23313271880868,
  416. -0.25344790059353, -0.05246019024463, 0.01628462406333,
  417. -0.02505961724053, 0.06920467763959, 0.02442357316099,
  418. -0.03721611395801, 0.01818801111503, -0.00749618797172],
  419. [0.53648789255105, -0.25049871956020, -0.42163034350696,
  420. -0.43193942311114, -0.00275953611929, -0.03424681017675,
  421. 0.04267842219415, -0.04678328784242, -0.10214864179676,
  422. 0.26408300200955, 0.14590772289388, 0.15113130533216,
  423. -0.02459864859345, -0.17556493366449, -0.11202315195388,
  424. -0.18823009262115, -0.04060034127000, 0.05477720428674,
  425. 0.04788665548180, 0.04704409688120, -0.02217936801134]];
  426. var ABButter = [
  427. [0.98621192462708, -1.97223372919527, -1.97242384925416,
  428. 0.97261396931306, 0.98621192462708],
  429. [0.98500175787242, -1.96977855582618, -1.97000351574484,
  430. 0.97022847566350, 0.98500175787242],
  431. [0.97938932735214, -1.95835380975398, -1.95877865470428,
  432. 0.95920349965459, 0.97938932735214],
  433. [0.97531843204928, -1.95002759149878, -1.95063686409857,
  434. 0.95124613669835, 0.97531843204928],
  435. [0.97316523498161, -1.94561023566527, -1.94633046996323,
  436. 0.94705070426118, 0.97316523498161],
  437. [0.96454515552826, -1.92783286977036, -1.92909031105652,
  438. 0.93034775234268, 0.96454515552826],
  439. [0.96009142950541, -1.91858953033784, -1.92018285901082,
  440. 0.92177618768381, 0.96009142950541],
  441. [0.95856916599601, -1.91542108074780, -1.91713833199203,
  442. 0.91885558323625, 0.95856916599601],
  443. [0.94597685600279, -1.88903307939452, -1.89195371200558,
  444. 0.89487434461664, 0.94597685600279]];
  445. /**
  446. * When calling this procedure, make sure that ip[-order] and op[-order]
  447. * point to real data
  448. */
  449. //private void filterYule(final float[] input, int inputPos, float[] output,
  450. //int outputPos, int nSamples, final float[] kernel) {
  451. function filterYule(input, inputPos, output, outputPos, nSamples, kernel) {
  452. while ((nSamples--) != 0) {
  453. /* 1e-10 is a hack to avoid slowdown because of denormals */
  454. output[outputPos] = 1e-10 + input[inputPos + 0] * kernel[0]
  455. - output[outputPos - 1] * kernel[1] + input[inputPos - 1]
  456. * kernel[2] - output[outputPos - 2] * kernel[3]
  457. + input[inputPos - 2] * kernel[4] - output[outputPos - 3]
  458. * kernel[5] + input[inputPos - 3] * kernel[6]
  459. - output[outputPos - 4] * kernel[7] + input[inputPos - 4]
  460. * kernel[8] - output[outputPos - 5] * kernel[9]
  461. + input[inputPos - 5] * kernel[10] - output[outputPos - 6]
  462. * kernel[11] + input[inputPos - 6] * kernel[12]
  463. - output[outputPos - 7] * kernel[13] + input[inputPos - 7]
  464. * kernel[14] - output[outputPos - 8] * kernel[15]
  465. + input[inputPos - 8] * kernel[16] - output[outputPos - 9]
  466. * kernel[17] + input[inputPos - 9] * kernel[18]
  467. - output[outputPos - 10] * kernel[19]
  468. + input[inputPos - 10] * kernel[20];
  469. ++outputPos;
  470. ++inputPos;
  471. }
  472. }
  473. //private void filterButter(final float[] input, int inputPos,
  474. // float[] output, int outputPos, int nSamples, final float[] kernel) {
  475. function filterButter(input, inputPos, output, outputPos, nSamples, kernel) {
  476. while ((nSamples--) != 0) {
  477. output[outputPos] = input[inputPos + 0] * kernel[0]
  478. - output[outputPos - 1] * kernel[1] + input[inputPos - 1]
  479. * kernel[2] - output[outputPos - 2] * kernel[3]
  480. + input[inputPos - 2] * kernel[4];
  481. ++outputPos;
  482. ++inputPos;
  483. }
  484. }
  485. /**
  486. * @return INIT_GAIN_ANALYSIS_OK if successful, INIT_GAIN_ANALYSIS_ERROR if
  487. * not
  488. */
  489. function ResetSampleFrequency(rgData, samplefreq) {
  490. /* zero out initial values */
  491. for (var i = 0; i < MAX_ORDER; i++)
  492. rgData.linprebuf[i] = rgData.lstepbuf[i] = rgData.loutbuf[i] = rgData.rinprebuf[i] = rgData.rstepbuf[i] = rgData.routbuf[i] = 0.;
  493. switch (0 | (samplefreq)) {
  494. case 48000:
  495. rgData.reqindex = 0;
  496. break;
  497. case 44100:
  498. rgData.reqindex = 1;
  499. break;
  500. case 32000:
  501. rgData.reqindex = 2;
  502. break;
  503. case 24000:
  504. rgData.reqindex = 3;
  505. break;
  506. case 22050:
  507. rgData.reqindex = 4;
  508. break;
  509. case 16000:
  510. rgData.reqindex = 5;
  511. break;
  512. case 12000:
  513. rgData.reqindex = 6;
  514. break;
  515. case 11025:
  516. rgData.reqindex = 7;
  517. break;
  518. case 8000:
  519. rgData.reqindex = 8;
  520. break;
  521. default:
  522. return INIT_GAIN_ANALYSIS_ERROR;
  523. }
  524. rgData.sampleWindow = 0 | ((samplefreq * RMS_WINDOW_TIME_NUMERATOR
  525. + RMS_WINDOW_TIME_DENOMINATOR - 1) / RMS_WINDOW_TIME_DENOMINATOR);
  526. rgData.lsum = 0.;
  527. rgData.rsum = 0.;
  528. rgData.totsamp = 0;
  529. Arrays.ill(rgData.A, 0);
  530. return INIT_GAIN_ANALYSIS_OK;
  531. }
  532. this.InitGainAnalysis = function (rgData, samplefreq) {
  533. if (ResetSampleFrequency(rgData, samplefreq) != INIT_GAIN_ANALYSIS_OK) {
  534. return INIT_GAIN_ANALYSIS_ERROR;
  535. }
  536. rgData.linpre = MAX_ORDER;
  537. rgData.rinpre = MAX_ORDER;
  538. rgData.lstep = MAX_ORDER;
  539. rgData.rstep = MAX_ORDER;
  540. rgData.lout = MAX_ORDER;
  541. rgData.rout = MAX_ORDER;
  542. Arrays.fill(rgData.B, 0);
  543. return INIT_GAIN_ANALYSIS_OK;
  544. };
  545. /**
  546. * square
  547. */
  548. function fsqr(d) {
  549. return d * d;
  550. }
  551. this.AnalyzeSamples = function (rgData, left_samples, left_samplesPos, right_samples, right_samplesPos, num_samples,
  552. num_channels) {
  553. var curleft;
  554. var curleftBase;
  555. var curright;
  556. var currightBase;
  557. var batchsamples;
  558. var cursamples;
  559. var cursamplepos;
  560. if (num_samples == 0)
  561. return GAIN_ANALYSIS_OK;
  562. cursamplepos = 0;
  563. batchsamples = num_samples;
  564. switch (num_channels) {
  565. case 1:
  566. right_samples = left_samples;
  567. right_samplesPos = left_samplesPos;
  568. break;
  569. case 2:
  570. break;
  571. default:
  572. return GAIN_ANALYSIS_ERROR;
  573. }
  574. if (num_samples < MAX_ORDER) {
  575. System.arraycopy(left_samples, left_samplesPos, rgData.linprebuf,
  576. MAX_ORDER, num_samples);
  577. System.arraycopy(right_samples, right_samplesPos, rgData.rinprebuf,
  578. MAX_ORDER, num_samples);
  579. } else {
  580. System.arraycopy(left_samples, left_samplesPos, rgData.linprebuf,
  581. MAX_ORDER, MAX_ORDER);
  582. System.arraycopy(right_samples, right_samplesPos, rgData.rinprebuf,
  583. MAX_ORDER, MAX_ORDER);
  584. }
  585. while (batchsamples > 0) {
  586. cursamples = batchsamples > rgData.sampleWindow - rgData.totsamp ? rgData.sampleWindow
  587. - rgData.totsamp
  588. : batchsamples;
  589. if (cursamplepos < MAX_ORDER) {
  590. curleft = rgData.linpre + cursamplepos;
  591. curleftBase = rgData.linprebuf;
  592. curright = rgData.rinpre + cursamplepos;
  593. currightBase = rgData.rinprebuf;
  594. if (cursamples > MAX_ORDER - cursamplepos)
  595. cursamples = MAX_ORDER - cursamplepos;
  596. } else {
  597. curleft = left_samplesPos + cursamplepos;
  598. curleftBase = left_samples;
  599. curright = right_samplesPos + cursamplepos;
  600. currightBase = right_samples;
  601. }
  602. filterYule(curleftBase, curleft, rgData.lstepbuf, rgData.lstep
  603. + rgData.totsamp, cursamples, ABYule[rgData.reqindex]);
  604. filterYule(currightBase, curright, rgData.rstepbuf, rgData.rstep
  605. + rgData.totsamp, cursamples, ABYule[rgData.reqindex]);
  606. filterButter(rgData.lstepbuf, rgData.lstep + rgData.totsamp,
  607. rgData.loutbuf, rgData.lout + rgData.totsamp, cursamples,
  608. ABButter[rgData.reqindex]);
  609. filterButter(rgData.rstepbuf, rgData.rstep + rgData.totsamp,
  610. rgData.routbuf, rgData.rout + rgData.totsamp, cursamples,
  611. ABButter[rgData.reqindex]);
  612. curleft = rgData.lout + rgData.totsamp;
  613. /* Get the squared values */
  614. curleftBase = rgData.loutbuf;
  615. curright = rgData.rout + rgData.totsamp;
  616. currightBase = rgData.routbuf;
  617. var i = cursamples % 8;
  618. while ((i--) != 0) {
  619. rgData.lsum += fsqr(curleftBase[curleft++]);
  620. rgData.rsum += fsqr(currightBase[curright++]);
  621. }
  622. i = cursamples / 8;
  623. while ((i--) != 0) {
  624. rgData.lsum += fsqr(curleftBase[curleft + 0])
  625. + fsqr(curleftBase[curleft + 1])
  626. + fsqr(curleftBase[curleft + 2])
  627. + fsqr(curleftBase[curleft + 3])
  628. + fsqr(curleftBase[curleft + 4])
  629. + fsqr(curleftBase[curleft + 5])
  630. + fsqr(curleftBase[curleft + 6])
  631. + fsqr(curleftBase[curleft + 7]);
  632. curleft += 8;
  633. rgData.rsum += fsqr(currightBase[curright + 0])
  634. + fsqr(currightBase[curright + 1])
  635. + fsqr(currightBase[curright + 2])
  636. + fsqr(currightBase[curright + 3])
  637. + fsqr(currightBase[curright + 4])
  638. + fsqr(currightBase[curright + 5])
  639. + fsqr(currightBase[curright + 6])
  640. + fsqr(currightBase[curright + 7]);
  641. curright += 8;
  642. }
  643. batchsamples -= cursamples;
  644. cursamplepos += cursamples;
  645. rgData.totsamp += cursamples;
  646. if (rgData.totsamp == rgData.sampleWindow) {
  647. /* Get the Root Mean Square (RMS) for this set of samples */
  648. var val = GainAnalysis.STEPS_per_dB
  649. * 10.
  650. * Math.log10((rgData.lsum + rgData.rsum)
  651. / rgData.totsamp * 0.5 + 1.e-37);
  652. var ival = (val <= 0) ? 0 : 0 | val;
  653. if (ival >= rgData.A.length)
  654. ival = rgData.A.length - 1;
  655. rgData.A[ival]++;
  656. rgData.lsum = rgData.rsum = 0.;
  657. System.arraycopy(rgData.loutbuf, rgData.totsamp,
  658. rgData.loutbuf, 0, MAX_ORDER);
  659. System.arraycopy(rgData.routbuf, rgData.totsamp,
  660. rgData.routbuf, 0, MAX_ORDER);
  661. System.arraycopy(rgData.lstepbuf, rgData.totsamp,
  662. rgData.lstepbuf, 0, MAX_ORDER);
  663. System.arraycopy(rgData.rstepbuf, rgData.totsamp,
  664. rgData.rstepbuf, 0, MAX_ORDER);
  665. rgData.totsamp = 0;
  666. }
  667. if (rgData.totsamp > rgData.sampleWindow) {
  668. /*
  669. * somehow I really screwed up: Error in programming! Contact
  670. * author about totsamp > sampleWindow
  671. */
  672. return GAIN_ANALYSIS_ERROR;
  673. }
  674. }
  675. if (num_samples < MAX_ORDER) {
  676. System.arraycopy(rgData.linprebuf, num_samples, rgData.linprebuf,
  677. 0, MAX_ORDER - num_samples);
  678. System.arraycopy(rgData.rinprebuf, num_samples, rgData.rinprebuf,
  679. 0, MAX_ORDER - num_samples);
  680. System.arraycopy(left_samples, left_samplesPos, rgData.linprebuf,
  681. MAX_ORDER - num_samples, num_samples);
  682. System.arraycopy(right_samples, right_samplesPos, rgData.rinprebuf,
  683. MAX_ORDER - num_samples, num_samples);
  684. } else {
  685. System.arraycopy(left_samples, left_samplesPos + num_samples
  686. - MAX_ORDER, rgData.linprebuf, 0, MAX_ORDER);
  687. System.arraycopy(right_samples, right_samplesPos + num_samples
  688. - MAX_ORDER, rgData.rinprebuf, 0, MAX_ORDER);
  689. }
  690. return GAIN_ANALYSIS_OK;
  691. };
  692. function analyzeResult(Array, len) {
  693. var i;
  694. var elems = 0;
  695. for (i = 0; i < len; i++)
  696. elems += Array[i];
  697. if (elems == 0)
  698. return GAIN_NOT_ENOUGH_SAMPLES;
  699. var upper = 0 | Math.ceil(elems * (1. - RMS_PERCENTILE));
  700. for (i = len; i-- > 0;) {
  701. if ((upper -= Array[i]) <= 0)
  702. break;
  703. }
  704. //return (float) ((float) PINK_REF - (float) i / (float) STEPS_per_dB);
  705. return (PINK_REF - i / GainAnalysis.STEPS_per_dB);
  706. }
  707. this.GetTitleGain = function (rgData) {
  708. var retval = analyzeResult(rgData.A, rgData.A.length);
  709. for (var i = 0; i < rgData.A.length; i++) {
  710. rgData.B[i] += rgData.A[i];
  711. rgData.A[i] = 0;
  712. }
  713. for (var i = 0; i < MAX_ORDER; i++)
  714. rgData.linprebuf[i] = rgData.lstepbuf[i] = rgData.loutbuf[i] = rgData.rinprebuf[i] = rgData.rstepbuf[i] = rgData.routbuf[i] = 0.;
  715. rgData.totsamp = 0;
  716. rgData.lsum = rgData.rsum = 0.;
  717. return retval;
  718. }
  719. }
  720. function Presets() {
  721. function VBRPresets(qual, comp, compS,
  722. y, shThreshold, shThresholdS,
  723. adj, adjShort, lower,
  724. curve, sens, inter,
  725. joint, mod, fix) {
  726. this.vbr_q = qual;
  727. this.quant_comp = comp;
  728. this.quant_comp_s = compS;
  729. this.expY = y;
  730. this.st_lrm = shThreshold;
  731. this.st_s = shThresholdS;
  732. this.masking_adj = adj;
  733. this.masking_adj_short = adjShort;
  734. this.ath_lower = lower;
  735. this.ath_curve = curve;
  736. this.ath_sensitivity = sens;
  737. this.interch = inter;
  738. this.safejoint = joint;
  739. this.sfb21mod = mod;
  740. this.msfix = fix;
  741. }
  742. function ABRPresets(kbps, comp, compS,
  743. joint, fix, shThreshold,
  744. shThresholdS, bass, sc,
  745. mask, lower, curve,
  746. interCh, sfScale) {
  747. this.quant_comp = comp;
  748. this.quant_comp_s = compS;
  749. this.safejoint = joint;
  750. this.nsmsfix = fix;
  751. this.st_lrm = shThreshold;
  752. this.st_s = shThresholdS;
  753. this.nsbass = bass;
  754. this.scale = sc;
  755. this.masking_adj = mask;
  756. this.ath_lower = lower;
  757. this.ath_curve = curve;
  758. this.interch = interCh;
  759. this.sfscale = sfScale;
  760. }
  761. var lame;
  762. this.setModules = function (_lame) {
  763. lame = _lame;
  764. };
  765. /**
  766. * <PRE>
  767. * Switch mappings for VBR mode VBR_RH
  768. * vbr_q qcomp_l qcomp_s expY st_lrm st_s mask adj_l adj_s ath_lower ath_curve ath_sens interChR safejoint sfb21mod msfix
  769. * </PRE>
  770. */
  771. var vbr_old_switch_map = [
  772. new VBRPresets(0, 9, 9, 0, 5.20, 125.0, -4.2, -6.3, 4.8, 1, 0, 0, 2, 21, 0.97),
  773. new VBRPresets(1, 9, 9, 0, 5.30, 125.0, -3.6, -5.6, 4.5, 1.5, 0, 0, 2, 21, 1.35),
  774. new VBRPresets(2, 9, 9, 0, 5.60, 125.0, -2.2, -3.5, 2.8, 2, 0, 0, 2, 21, 1.49),
  775. new VBRPresets(3, 9, 9, 1, 5.80, 130.0, -1.8, -2.8, 2.6, 3, -4, 0, 2, 20, 1.64),
  776. new VBRPresets(4, 9, 9, 1, 6.00, 135.0, -0.7, -1.1, 1.1, 3.5, -8, 0, 2, 0, 1.79),
  777. new VBRPresets(5, 9, 9, 1, 6.40, 140.0, 0.5, 0.4, -7.5, 4, -12, 0.0002, 0, 0, 1.95),
  778. new VBRPresets(6, 9, 9, 1, 6.60, 145.0, 0.67, 0.65, -14.7, 6.5, -19, 0.0004, 0, 0, 2.30),
  779. new VBRPresets(7, 9, 9, 1, 6.60, 145.0, 0.8, 0.75, -19.7, 8, -22, 0.0006, 0, 0, 2.70),
  780. new VBRPresets(8, 9, 9, 1, 6.60, 145.0, 1.2, 1.15, -27.5, 10, -23, 0.0007, 0, 0, 0),
  781. new VBRPresets(9, 9, 9, 1, 6.60, 145.0, 1.6, 1.6, -36, 11, -25, 0.0008, 0, 0, 0),
  782. new VBRPresets(10, 9, 9, 1, 6.60, 145.0, 2.0, 2.0, -36, 12, -25, 0.0008, 0, 0, 0)
  783. ];
  784. /**
  785. * <PRE>
  786. * vbr_q qcomp_l qcomp_s expY st_lrm st_s mask adj_l adj_s ath_lower ath_curve ath_sens interChR safejoint sfb21mod msfix
  787. * </PRE>
  788. */
  789. var vbr_psy_switch_map = [
  790. new VBRPresets(0, 9, 9, 0, 4.20, 25.0, -7.0, -4.0, 7.5, 1, 0, 0, 2, 26, 0.97),
  791. new VBRPresets(1, 9, 9, 0, 4.20, 25.0, -5.6, -3.6, 4.5, 1.5, 0, 0, 2, 21, 1.35),
  792. new VBRPresets(2, 9, 9, 0, 4.20, 25.0, -4.4, -1.8, 2, 2, 0, 0, 2, 18, 1.49),
  793. new VBRPresets(3, 9, 9, 1, 4.20, 25.0, -3.4, -1.25, 1.1, 3, -4, 0, 2, 15, 1.64),
  794. new VBRPresets(4, 9, 9, 1, 4.20, 25.0, -2.2, 0.1, 0, 3.5, -8, 0, 2, 0, 1.79),
  795. new VBRPresets(5, 9, 9, 1, 4.20, 25.0, -1.0, 1.65, -7.7, 4, -12, 0.0002, 0, 0, 1.95),
  796. new VBRPresets(6, 9, 9, 1, 4.20, 25.0, -0.0, 2.47, -7.7, 6.5, -19, 0.0004, 0, 0, 2),
  797. new VBRPresets(7, 9, 9, 1, 4.20, 25.0, 0.5, 2.0, -14.5, 8, -22, 0.0006, 0, 0, 2),
  798. new VBRPresets(8, 9, 9, 1, 4.20, 25.0, 1.0, 2.4, -22.0, 10, -23, 0.0007, 0, 0, 2),
  799. new VBRPresets(9, 9, 9, 1, 4.20, 25.0, 1.5, 2.95, -30.0, 11, -25, 0.0008, 0, 0, 2),
  800. new VBRPresets(10, 9, 9, 1, 4.20, 25.0, 2.0, 2.95, -36.0, 12, -30, 0.0008, 0, 0, 2)
  801. ];
  802. function apply_vbr_preset(gfp, a, enforce) {
  803. var vbr_preset = gfp.VBR == VbrMode.vbr_rh ? vbr_old_switch_map
  804. : vbr_psy_switch_map;
  805. var x = gfp.VBR_q_frac;
  806. var p = vbr_preset[a];
  807. var q = vbr_preset[a + 1];
  808. var set = p;
  809. // NOOP(vbr_q);
  810. // NOOP(quant_comp);
  811. // NOOP(quant_comp_s);
  812. // NOOP(expY);
  813. p.st_lrm = p.st_lrm + x * (q.st_lrm - p.st_lrm);
  814. // LERP(st_lrm);
  815. p.st_s = p.st_s + x * (q.st_s - p.st_s);
  816. // LERP(st_s);
  817. p.masking_adj = p.masking_adj + x * (q.masking_adj - p.masking_adj);
  818. // LERP(masking_adj);
  819. p.masking_adj_short = p.masking_adj_short + x
  820. * (q.masking_adj_short - p.masking_adj_short);
  821. // LERP(masking_adj_short);
  822. p.ath_lower = p.ath_lower + x * (q.ath_lower - p.ath_lower);
  823. // LERP(ath_lower);
  824. p.ath_curve = p.ath_curve + x * (q.ath_curve - p.ath_curve);
  825. // LERP(ath_curve);
  826. p.ath_sensitivity = p.ath_sensitivity + x
  827. * (q.ath_sensitivity - p.ath_sensitivity);
  828. // LERP(ath_sensitivity);
  829. p.interch = p.interch + x * (q.interch - p.interch);
  830. // LERP(interch);
  831. // NOOP(safejoint);
  832. // NOOP(sfb21mod);
  833. p.msfix = p.msfix + x * (q.msfix - p.msfix);
  834. // LERP(msfix);
  835. lame_set_VBR_q(gfp, set.vbr_q);
  836. if (enforce != 0)
  837. gfp.quant_comp = set.quant_comp;
  838. else if (!(Math.abs(gfp.quant_comp - -1) > 0))
  839. gfp.quant_comp = set.quant_comp;
  840. // SET_OPTION(quant_comp, set.quant_comp, -1);
  841. if (enforce != 0)
  842. gfp.quant_comp_short = set.quant_comp_s;
  843. else if (!(Math.abs(gfp.quant_comp_short - -1) > 0))
  844. gfp.quant_comp_short = set.quant_comp_s;
  845. // SET_OPTION(quant_comp_short, set.quant_comp_s, -1);
  846. if (set.expY != 0) {
  847. gfp.experimentalY = set.expY != 0;
  848. }
  849. if (enforce != 0)
  850. gfp.internal_flags.nsPsy.attackthre = set.st_lrm;
  851. else if (!(Math.abs(gfp.internal_flags.nsPsy.attackthre - -1) > 0))
  852. gfp.internal_flags.nsPsy.attackthre = set.st_lrm;
  853. // SET_OPTION(short_threshold_lrm, set.st_lrm, -1);
  854. if (enforce != 0)
  855. gfp.internal_flags.nsPsy.attackthre_s = set.st_s;
  856. else if (!(Math.abs(gfp.internal_flags.nsPsy.attackthre_s - -1) > 0))
  857. gfp.internal_flags.nsPsy.attackthre_s = set.st_s;
  858. // SET_OPTION(short_threshold_s, set.st_s, -1);
  859. if (enforce != 0)
  860. gfp.maskingadjust = set.masking_adj;
  861. else if (!(Math.abs(gfp.maskingadjust - 0) > 0))
  862. gfp.maskingadjust = set.masking_adj;
  863. // SET_OPTION(maskingadjust, set.masking_adj, 0);
  864. if (enforce != 0)
  865. gfp.maskingadjust_short = set.masking_adj_short;
  866. else if (!(Math.abs(gfp.maskingadjust_short - 0) > 0))
  867. gfp.maskingadjust_short = set.masking_adj_short;
  868. // SET_OPTION(maskingadjust_short, set.masking_adj_short, 0);
  869. if (enforce != 0)
  870. gfp.ATHlower = -set.ath_lower / 10.0;
  871. else if (!(Math.abs((-gfp.ATHlower * 10.0) - 0) > 0))
  872. gfp.ATHlower = -set.ath_lower / 10.0;
  873. // SET_OPTION(ATHlower, set.ath_lower, 0);
  874. if (enforce != 0)
  875. gfp.ATHcurve = set.ath_curve;
  876. else if (!(Math.abs(gfp.ATHcurve - -1) > 0))
  877. gfp.ATHcurve = set.ath_curve;
  878. // SET_OPTION(ATHcurve, set.ath_curve, -1);
  879. if (enforce != 0)
  880. gfp.athaa_sensitivity = set.ath_sensitivity;
  881. else if (!(Math.abs(gfp.athaa_sensitivity - -1) > 0))
  882. gfp.athaa_sensitivity = set.ath_sensitivity;
  883. // SET_OPTION(athaa_sensitivity, set.ath_sensitivity, 0);
  884. if (set.interch > 0) {
  885. if (enforce != 0)
  886. gfp.interChRatio = set.interch;
  887. else if (!(Math.abs(gfp.interChRatio - -1) > 0))
  888. gfp.interChRatio = set.interch;
  889. // SET_OPTION(interChRatio, set.interch, -1);
  890. }
  891. /* parameters for which there is no proper set/get interface */
  892. if (set.safejoint > 0) {
  893. gfp.exp_nspsytune = gfp.exp_nspsytune | set.safejoint;
  894. }
  895. if (set.sfb21mod > 0) {
  896. gfp.exp_nspsytune = gfp.exp_nspsytune | (set.sfb21mod << 20);
  897. }
  898. if (enforce != 0)
  899. gfp.msfix = set.msfix;
  900. else if (!(Math.abs(gfp.msfix - -1) > 0))
  901. gfp.msfix = set.msfix;
  902. // SET_OPTION(msfix, set.msfix, -1);
  903. if (enforce == 0) {
  904. gfp.VBR_q = a;
  905. gfp.VBR_q_frac = x;
  906. }
  907. }
  908. /**
  909. * <PRE>
  910. * Switch mappings for ABR mode
  911. *
  912. * kbps quant q_s safejoint nsmsfix st_lrm st_s ns-bass scale msk ath_lwr ath_curve interch , sfscale
  913. * </PRE>
  914. */
  915. var abr_switch_map = [
  916. new ABRPresets(8, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -30.0, 11, 0.0012, 1), /* 8, impossible to use in stereo */
  917. new ABRPresets(16, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -25.0, 11, 0.0010, 1), /* 16 */
  918. new ABRPresets(24, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -20.0, 11, 0.0010, 1), /* 24 */
  919. new ABRPresets(32, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -15.0, 11, 0.0010, 1), /* 32 */
  920. new ABRPresets(40, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -10.0, 11, 0.0009, 1), /* 40 */
  921. new ABRPresets(48, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -10.0, 11, 0.0009, 1), /* 48 */
  922. new ABRPresets(56, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -6.0, 11, 0.0008, 1), /* 56 */
  923. new ABRPresets(64, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -2.0, 11, 0.0008, 1), /* 64 */
  924. new ABRPresets(80, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, .0, 8, 0.0007, 1), /* 80 */
  925. new ABRPresets(96, 9, 9, 0, 2.50, 6.60, 145, 0, 0.95, 0, 1.0, 5.5, 0.0006, 1), /* 96 */
  926. new ABRPresets(112, 9, 9, 0, 2.25, 6.60, 145, 0, 0.95, 0, 2.0, 4.5, 0.0005, 1), /* 112 */
  927. new ABRPresets(128, 9, 9, 0, 1.95, 6.40, 140, 0, 0.95, 0, 3.0, 4, 0.0002, 1), /* 128 */
  928. new ABRPresets(160, 9, 9, 1, 1.79, 6.00, 135, 0, 0.95, -2, 5.0, 3.5, 0, 1), /* 160 */
  929. new ABRPresets(192, 9, 9, 1, 1.49, 5.60, 125, 0, 0.97, -4, 7.0, 3, 0, 0), /* 192 */
  930. new ABRPresets(224, 9, 9, 1, 1.25, 5.20, 125, 0, 0.98, -6, 9.0, 2, 0, 0), /* 224 */
  931. new ABRPresets(256, 9, 9, 1, 0.97, 5.20, 125, 0, 1.00, -8, 10.0, 1, 0, 0), /* 256 */
  932. new ABRPresets(320, 9, 9, 1, 0.90, 5.20, 125, 0, 1.00, -10, 12.0, 0, 0, 0) /* 320 */
  933. ];
  934. function apply_abr_preset(gfp, preset, enforce) {
  935. /* Variables for the ABR stuff */
  936. var actual_bitrate = preset;
  937. var r = lame.nearestBitrateFullIndex(preset);
  938. gfp.VBR = VbrMode.vbr_abr;
  939. gfp.VBR_mean_bitrate_kbps = actual_bitrate;
  940. gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps, 320);
  941. gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps, 8);
  942. gfp.brate = gfp.VBR_mean_bitrate_kbps;
  943. if (gfp.VBR_mean_bitrate_kbps > 320) {
  944. gfp.disable_reservoir = true;
  945. }
  946. /* parameters for which there is no proper set/get interface */
  947. if (abr_switch_map[r].safejoint > 0)
  948. gfp.exp_nspsytune = gfp.exp_nspsytune | 2;
  949. /* safejoint */
  950. if (abr_switch_map[r].sfscale > 0) {
  951. gfp.internal_flags.noise_shaping = 2;
  952. }
  953. /* ns-bass tweaks */
  954. if (Math.abs(abr_switch_map[r].nsbass) > 0) {
  955. var k = (int)(abr_switch_map[r].nsbass * 4);
  956. if (k < 0)
  957. k += 64;
  958. gfp.exp_nspsytune = gfp.exp_nspsytune | (k << 2);
  959. }
  960. if (enforce != 0)
  961. gfp.quant_comp = abr_switch_map[r].quant_comp;
  962. else if (!(Math.abs(gfp.quant_comp - -1) > 0))
  963. gfp.quant_comp = abr_switch_map[r].quant_comp;
  964. // SET_OPTION(quant_comp, abr_switch_map[r].quant_comp, -1);
  965. if (enforce != 0)
  966. gfp.quant_comp_short = abr_switch_map[r].quant_comp_s;
  967. else if (!(Math.abs(gfp.quant_comp_short - -1) > 0))
  968. gfp.quant_comp_short = abr_switch_map[r].quant_comp_s;
  969. // SET_OPTION(quant_comp_short, abr_switch_map[r].quant_comp_s, -1);
  970. if (enforce != 0)
  971. gfp.msfix = abr_switch_map[r].nsmsfix;
  972. else if (!(Math.abs(gfp.msfix - -1) > 0))
  973. gfp.msfix = abr_switch_map[r].nsmsfix;
  974. // SET_OPTION(msfix, abr_switch_map[r].nsmsfix, -1);
  975. if (enforce != 0)
  976. gfp.internal_flags.nsPsy.attackthre = abr_switch_map[r].st_lrm;
  977. else if (!(Math.abs(gfp.internal_flags.nsPsy.attackthre - -1) > 0))
  978. gfp.internal_flags.nsPsy.attackthre = abr_switch_map[r].st_lrm;
  979. // SET_OPTION(short_threshold_lrm, abr_switch_map[r].st_lrm, -1);
  980. if (enforce != 0)
  981. gfp.internal_flags.nsPsy.attackthre_s = abr_switch_map[r].st_s;
  982. else if (!(Math.abs(gfp.internal_flags.nsPsy.attackthre_s - -1) > 0))
  983. gfp.internal_flags.nsPsy.attackthre_s = abr_switch_map[r].st_s;
  984. // SET_OPTION(short_threshold_s, abr_switch_map[r].st_s, -1);
  985. /*
  986. * ABR seems to have big problems with clipping, especially at low
  987. * bitrates
  988. */
  989. /*
  990. * so we compensate for that here by using a scale value depending on
  991. * bitrate
  992. */
  993. if (enforce != 0)
  994. gfp.scale = abr_switch_map[r].scale;
  995. else if (!(Math.abs(gfp.scale - -1) > 0))
  996. gfp.scale = abr_switch_map[r].scale;
  997. // SET_OPTION(scale, abr_switch_map[r].scale, -1);
  998. if (enforce != 0)
  999. gfp.maskingadjust = abr_switch_map[r].masking_adj;
  1000. else if (!(Math.abs(gfp.maskingadjust - 0) > 0))
  1001. gfp.maskingadjust = abr_switch_map[r].masking_adj;
  1002. // SET_OPTION(maskingadjust, abr_switch_map[r].masking_adj, 0);
  1003. if (abr_switch_map[r].masking_adj > 0) {
  1004. if (enforce != 0)
  1005. gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * .9);
  1006. else if (!(Math.abs(gfp.maskingadjust_short - 0) > 0))
  1007. gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * .9);
  1008. // SET_OPTION(maskingadjust_short, abr_switch_map[r].masking_adj *
  1009. // .9, 0);
  1010. } else {
  1011. if (enforce != 0)
  1012. gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * 1.1);
  1013. else if (!(Math.abs(gfp.maskingadjust_short - 0) > 0))
  1014. gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * 1.1);
  1015. // SET_OPTION(maskingadjust_short, abr_switch_map[r].masking_adj *
  1016. // 1.1, 0);
  1017. }
  1018. if (enforce != 0)
  1019. gfp.ATHlower = -abr_switch_map[r].ath_lower / 10.;
  1020. else if (!(Math.abs((-gfp.ATHlower * 10.) - 0) > 0))
  1021. gfp.ATHlower = -abr_switch_map[r].ath_lower / 10.;
  1022. // SET_OPTION(ATHlower, abr_switch_map[r].ath_lower, 0);
  1023. if (enforce != 0)
  1024. gfp.ATHcurve = abr_switch_map[r].ath_curve;
  1025. else if (!(Math.abs(gfp.ATHcurve - -1) > 0))
  1026. gfp.ATHcurve = abr_switch_map[r].ath_curve;
  1027. // SET_OPTION(ATHcurve, abr_switch_map[r].ath_curve, -1);
  1028. if (enforce != 0)
  1029. gfp.interChRatio = abr_switch_map[r].interch;
  1030. else if (!(Math.abs(gfp.interChRatio - -1) > 0))
  1031. gfp.interChRatio = abr_switch_map[r].interch;
  1032. // SET_OPTION(interChRatio, abr_switch_map[r].interch, -1);
  1033. return preset;
  1034. }
  1035. this.apply_preset = function(gfp, preset, enforce) {
  1036. /* translate legacy presets */
  1037. switch (preset) {
  1038. case Lame.R3MIX:
  1039. {
  1040. preset = Lame.V3;
  1041. gfp.VBR = VbrMode.vbr_mtrh;
  1042. break;
  1043. }
  1044. case Lame.MEDIUM:
  1045. {
  1046. preset = Lame.V4;
  1047. gfp.VBR = VbrMode.vbr_rh;
  1048. break;
  1049. }
  1050. case Lame.MEDIUM_FAST:
  1051. {
  1052. preset = Lame.V4;
  1053. gfp.VBR = VbrMode.vbr_mtrh;
  1054. break;
  1055. }
  1056. case Lame.STANDARD:
  1057. {
  1058. preset = Lame.V2;
  1059. gfp.VBR = VbrMode.vbr_rh;
  1060. break;
  1061. }
  1062. case Lame.STANDARD_FAST:
  1063. {
  1064. preset = Lame.V2;
  1065. gfp.VBR = VbrMode.vbr_mtrh;
  1066. break;
  1067. }
  1068. case Lame.EXTREME:
  1069. {
  1070. preset = Lame.V0;
  1071. gfp.VBR = VbrMode.vbr_rh;
  1072. break;
  1073. }
  1074. case Lame.EXTREME_FAST:
  1075. {
  1076. preset = Lame.V0;
  1077. gfp.VBR = VbrMode.vbr_mtrh;
  1078. break;
  1079. }
  1080. case Lame.INSANE:
  1081. {
  1082. preset = 320;
  1083. gfp.preset = preset;
  1084. apply_abr_preset(gfp, preset, enforce);
  1085. gfp.VBR = VbrMode.vbr_off;
  1086. return preset;
  1087. }
  1088. }
  1089. gfp.preset = preset;
  1090. {
  1091. switch (preset) {
  1092. case Lame.V9:
  1093. apply_vbr_preset(gfp, 9, enforce);
  1094. return preset;
  1095. case Lame.V8:
  1096. apply_vbr_preset(gfp, 8, enforce);
  1097. return preset;
  1098. case Lame.V7:
  1099. apply_vbr_preset(gfp, 7, enforce);
  1100. return preset;
  1101. case Lame.V6:
  1102. apply_vbr_preset(gfp, 6, enforce);
  1103. return preset;
  1104. case Lame.V5:
  1105. apply_vbr_preset(gfp, 5, enforce);
  1106. return preset;
  1107. case Lame.V4:
  1108. apply_vbr_preset(gfp, 4, enforce);
  1109. return preset;
  1110. case Lame.V3:
  1111. apply_vbr_preset(gfp, 3, enforce);
  1112. return preset;
  1113. case Lame.V2:
  1114. apply_vbr_preset(gfp, 2, enforce);
  1115. return preset;
  1116. case Lame.V1:
  1117. apply_vbr_preset(gfp, 1, enforce);
  1118. return preset;
  1119. case Lame.V0:
  1120. apply_vbr_preset(gfp, 0, enforce);
  1121. return preset;
  1122. default:
  1123. break;
  1124. }
  1125. }
  1126. if (8 <= preset && preset <= 320) {
  1127. return apply_abr_preset(gfp, preset, enforce);
  1128. }
  1129. /* no corresponding preset found */
  1130. gfp.preset = 0;
  1131. return preset;
  1132. }
  1133. // Rest from getset.c:
  1134. /**
  1135. * VBR quality level.<BR>
  1136. * 0 = highest<BR>
  1137. * 9 = lowest
  1138. */
  1139. function lame_set_VBR_q(gfp, VBR_q) {
  1140. var ret = 0;
  1141. if (0 > VBR_q) {
  1142. /* Unknown VBR quality level! */
  1143. ret = -1;
  1144. VBR_q = 0;
  1145. }
  1146. if (9 < VBR_q) {
  1147. ret = -1;
  1148. VBR_q = 9;
  1149. }
  1150. gfp.VBR_q = VBR_q;
  1151. gfp.VBR_q_frac = 0;
  1152. return ret;
  1153. }
  1154. }
  1155. /*
  1156. * MP3 huffman table selecting and bit counting
  1157. *
  1158. * Copyright (c) 1999-2005 Takehiro TOMINAGA
  1159. * Copyright (c) 2002-2005 Gabriel Bouvigne
  1160. *
  1161. * This library is free software; you can redistribute it and/or
  1162. * modify it under the terms of the GNU Lesser General Public
  1163. * License as published by the Free Software Foundation; either
  1164. * version 2 of the License, or (at your option) any later version.
  1165. *
  1166. * This library is distributed in the hope that it will be useful,
  1167. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1168. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1169. * Library General Public License for more details.
  1170. *
  1171. * You should have received a copy of the GNU Lesser General Public
  1172. * License along with this library; if not, write to the
  1173. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  1174. * Boston, MA 02111-1307, USA.
  1175. */
  1176. /* $Id: Takehiro.java,v 1.26 2011/05/24 20:48:06 kenchis Exp $ */
  1177. //package mp3;
  1178. //import java.util.Arrays;
  1179. function Takehiro() {
  1180. var qupvt = null;
  1181. this.qupvt = null;
  1182. this.setModules = function (_qupvt) {
  1183. this.qupvt = _qupvt;
  1184. qupvt = _qupvt;
  1185. }
  1186. function Bits(b) {
  1187. this.bits = 0 | b;
  1188. }
  1189. var subdv_table = [[0, 0], /* 0 bands */
  1190. [0, 0], /* 1 bands */
  1191. [0, 0], /* 2 bands */
  1192. [0, 0], /* 3 bands */
  1193. [0, 0], /* 4 bands */
  1194. [0, 1], /* 5 bands */
  1195. [1, 1], /* 6 bands */
  1196. [1, 1], /* 7 bands */
  1197. [1, 2], /* 8 bands */
  1198. [2, 2], /* 9 bands */
  1199. [2, 3], /* 10 bands */
  1200. [2, 3], /* 11 bands */
  1201. [3, 4], /* 12 bands */
  1202. [3, 4], /* 13 bands */
  1203. [3, 4], /* 14 bands */
  1204. [4, 5], /* 15 bands */
  1205. [4, 5], /* 16 bands */
  1206. [4, 6], /* 17 bands */
  1207. [5, 6], /* 18 bands */
  1208. [5, 6], /* 19 bands */
  1209. [5, 7], /* 20 bands */
  1210. [6, 7], /* 21 bands */
  1211. [6, 7], /* 22 bands */
  1212. ];
  1213. /**
  1214. * nonlinear quantization of xr More accurate formula than the ISO formula.
  1215. * Takes into account the fact that we are quantizing xr . ix, but we want
  1216. * ix^4/3 to be as close as possible to x^4/3. (taking the nearest int would
  1217. * mean ix is as close as possible to xr, which is different.)
  1218. *
  1219. * From Segher Boessenkool <segher@eastsite.nl> 11/1999
  1220. *
  1221. * 09/2000: ASM code removed in favor of IEEE754 hack by Takehiro Tominaga.
  1222. * If you need the ASM code, check CVS circa Aug 2000.
  1223. *
  1224. * 01/2004: Optimizations by Gabriel Bouvigne
  1225. */
  1226. function quantize_lines_xrpow_01(l, istep, xr, xrPos, ix, ixPos) {
  1227. var compareval0 = (1.0 - 0.4054) / istep;
  1228. l = l >> 1;
  1229. while ((l--) != 0) {
  1230. ix[ixPos++] = (compareval0 > xr[xrPos++]) ? 0 : 1;
  1231. ix[ixPos++] = (compareval0 > xr[xrPos++]) ? 0 : 1;
  1232. }
  1233. }
  1234. /**
  1235. * XRPOW_FTOI is a macro to convert floats to ints.<BR>
  1236. * if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]<BR>
  1237. * ROUNDFAC= -0.0946<BR>
  1238. *
  1239. * if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]<BR>
  1240. * ROUNDFAC=0.4054<BR>
  1241. *
  1242. * Note: using floor() or 0| is extremely slow. On machines where the
  1243. * TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile to write
  1244. * some ASM for XRPOW_FTOI().
  1245. */
  1246. function quantize_lines_xrpow(l, istep, xr, xrPos, ix, ixPos) {
  1247. l = l >> 1;
  1248. var remaining = l % 2;
  1249. l = l >> 1;
  1250. while (l-- != 0) {
  1251. var x0, x1, x2, x3;
  1252. var rx0, rx1, rx2, rx3;
  1253. x0 = xr[xrPos++] * istep;
  1254. x1 = xr[xrPos++] * istep;
  1255. rx0 = 0 | x0;
  1256. x2 = xr[xrPos++] * istep;
  1257. rx1 = 0 | x1;
  1258. x3 = xr[xrPos++] * istep;
  1259. rx2 = 0 | x2;
  1260. x0 += qupvt.adj43[rx0];
  1261. rx3 = 0 | x3;
  1262. x1 += qupvt.adj43[rx1];
  1263. ix[ixPos++] = 0 | x0;
  1264. x2 += qupvt.adj43[rx2];
  1265. ix[ixPos++] = 0 | x1;
  1266. x3 += qupvt.adj43[rx3];
  1267. ix[ixPos++] = 0 | x2;
  1268. ix[ixPos++] = 0 | x3;
  1269. }
  1270. if (remaining != 0) {
  1271. var x0, x1;
  1272. var rx0, rx1;
  1273. x0 = xr[xrPos++] * istep;
  1274. x1 = xr[xrPos++] * istep;
  1275. rx0 = 0 | x0;
  1276. rx1 = 0 | x1;
  1277. x0 += qupvt.adj43[rx0];
  1278. x1 += qupvt.adj43[rx1];
  1279. ix[ixPos++] = 0 | x0;
  1280. ix[ixPos++] = 0 | x1;
  1281. }
  1282. }
  1283. /**
  1284. * Quantization function This function will select which lines to quantize
  1285. * and call the proper quantization function
  1286. */
  1287. function quantize_xrpow(xp, pi, istep, codInfo, prevNoise) {
  1288. /* quantize on xr^(3/4) instead of xr */
  1289. var sfb;
  1290. var sfbmax;
  1291. var j = 0;
  1292. var prev_data_use;
  1293. var accumulate = 0;
  1294. var accumulate01 = 0;
  1295. var xpPos = 0;
  1296. var iData = pi;
  1297. var iDataPos = 0;
  1298. var acc_iData = iData;
  1299. var acc_iDataPos = 0;
  1300. var acc_xp = xp;
  1301. var acc_xpPos = 0;
  1302. /*
  1303. * Reusing previously computed data does not seems to work if global
  1304. * gain is changed. Finding why it behaves this way would allow to use a
  1305. * cache of previously computed values (let's 10 cached values per sfb)
  1306. * that would probably provide a noticeable speedup
  1307. */
  1308. prev_data_use = (prevNoise != null && (codInfo.global_gain == prevNoise.global_gain));
  1309. if (codInfo.block_type == Encoder.SHORT_TYPE)
  1310. sfbmax = 38;
  1311. else
  1312. sfbmax = 21;
  1313. for (sfb = 0; sfb <= sfbmax; sfb++) {
  1314. var step = -1;
  1315. if (prev_data_use || codInfo.block_type == Encoder.NORM_TYPE) {
  1316. step = codInfo.global_gain
  1317. - ((codInfo.scalefac[sfb] + (codInfo.preflag != 0 ? qupvt.pretab[sfb]
  1318. : 0)) << (codInfo.scalefac_scale + 1))
  1319. - codInfo.subblock_gain[codInfo.window[sfb]] * 8;
  1320. }
  1321. if (prev_data_use && (prevNoise.step[sfb] == step)) {
  1322. /*
  1323. * do not recompute this part, but compute accumulated lines
  1324. */
  1325. if (accumulate != 0) {
  1326. quantize_lines_xrpow(accumulate, istep, acc_xp, acc_xpPos,
  1327. acc_iData, acc_iDataPos);
  1328. accumulate = 0;
  1329. }
  1330. if (accumulate01 != 0) {
  1331. quantize_lines_xrpow_01(accumulate01, istep, acc_xp,
  1332. acc_xpPos, acc_iData, acc_iDataPos);
  1333. accumulate01 = 0;
  1334. }
  1335. } else { /* should compute this part */
  1336. var l = codInfo.width[sfb];
  1337. if ((j + codInfo.width[sfb]) > codInfo.max_nonzero_coeff) {
  1338. /* do not compute upper zero part */
  1339. var usefullsize;
  1340. usefullsize = codInfo.max_nonzero_coeff - j + 1;
  1341. Arrays.fill(pi, codInfo.max_nonzero_coeff, 576, 0);
  1342. l = usefullsize;
  1343. if (l < 0) {
  1344. l = 0;
  1345. }
  1346. /* no need to compute higher sfb values */
  1347. sfb = sfbmax + 1;
  1348. }
  1349. /* accumulate lines to quantize */
  1350. if (0 == accumulate && 0 == accumulate01) {
  1351. acc_iData = iData;
  1352. acc_iDataPos = iDataPos;
  1353. acc_xp = xp;
  1354. acc_xpPos = xpPos;
  1355. }
  1356. if (prevNoise != null && prevNoise.sfb_count1 > 0
  1357. && sfb >= prevNoise.sfb_count1
  1358. && prevNoise.step[sfb] > 0
  1359. && step >= prevNoise.step[sfb]) {
  1360. if (accumulate != 0) {
  1361. quantize_lines_xrpow(accumulate, istep, acc_xp,
  1362. acc_xpPos, acc_iData, acc_iDataPos);
  1363. accumulate = 0;
  1364. acc_iData = iData;
  1365. acc_iDataPos = iDataPos;
  1366. acc_xp = xp;
  1367. acc_xpPos = xpPos;
  1368. }
  1369. accumulate01 += l;
  1370. } else {
  1371. if (accumulate01 != 0) {
  1372. quantize_lines_xrpow_01(accumulate01, istep, acc_xp,
  1373. acc_xpPos, acc_iData, acc_iDataPos);
  1374. accumulate01 = 0;
  1375. acc_iData = iData;
  1376. acc_iDataPos = iDataPos;
  1377. acc_xp = xp;
  1378. acc_xpPos = xpPos;
  1379. }
  1380. accumulate += l;
  1381. }
  1382. if (l <= 0) {
  1383. /*
  1384. * rh: 20040215 may happen due to "prev_data_use"
  1385. * optimization
  1386. */
  1387. if (accumulate01 != 0) {
  1388. quantize_lines_xrpow_01(accumulate01, istep, acc_xp,
  1389. acc_xpPos, acc_iData, acc_iDataPos);
  1390. accumulate01 = 0;
  1391. }
  1392. if (accumulate != 0) {
  1393. quantize_lines_xrpow(accumulate, istep, acc_xp,
  1394. acc_xpPos, acc_iData, acc_iDataPos);
  1395. accumulate = 0;
  1396. }
  1397. break;
  1398. /* ends for-loop */
  1399. }
  1400. }
  1401. if (sfb <= sfbmax) {
  1402. iDataPos += codInfo.width[sfb];
  1403. xpPos += codInfo.width[sfb];
  1404. j += codInfo.width[sfb];
  1405. }
  1406. }
  1407. if (accumulate != 0) { /* last data part */
  1408. quantize_lines_xrpow(accumulate, istep, acc_xp, acc_xpPos,
  1409. acc_iData, acc_iDataPos);
  1410. accumulate = 0;
  1411. }
  1412. if (accumulate01 != 0) { /* last data part */
  1413. quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_xpPos,
  1414. acc_iData, acc_iDataPos);
  1415. accumulate01 = 0;
  1416. }
  1417. }
  1418. /**
  1419. * ix_max
  1420. */
  1421. function ix_max(ix, ixPos, endPos) {
  1422. var max1 = 0, max2 = 0;
  1423. do {
  1424. var x1 = ix[ixPos++];
  1425. var x2 = ix[ixPos++];
  1426. if (max1 < x1)
  1427. max1 = x1;
  1428. if (max2 < x2)
  1429. max2 = x2;
  1430. } while (ixPos < endPos);
  1431. if (max1 < max2)
  1432. max1 = max2;
  1433. return max1;
  1434. }
  1435. function count_bit_ESC(ix, ixPos, end, t1, t2, s) {
  1436. /* ESC-table is used */
  1437. var linbits = Tables.ht[t1].xlen * 65536 + Tables.ht[t2].xlen;
  1438. var sum = 0, sum2;
  1439. do {
  1440. var x = ix[ixPos++];
  1441. var y = ix[ixPos++];
  1442. if (x != 0) {
  1443. if (x > 14) {
  1444. x = 15;
  1445. sum += linbits;
  1446. }
  1447. x *= 16;
  1448. }
  1449. if (y != 0) {
  1450. if (y > 14) {
  1451. y = 15;
  1452. sum += linbits;
  1453. }
  1454. x += y;
  1455. }
  1456. sum += Tables.largetbl[x];
  1457. } while (ixPos < end);
  1458. sum2 = sum & 0xffff;
  1459. sum >>= 16;
  1460. if (sum > sum2) {
  1461. sum = sum2;
  1462. t1 = t2;
  1463. }
  1464. s.bits += sum;
  1465. return t1;
  1466. }
  1467. function count_bit_noESC(ix, ixPos, end, s) {
  1468. /* No ESC-words */
  1469. var sum1 = 0;
  1470. var hlen1 = Tables.ht[1].hlen;
  1471. do {
  1472. var x = ix[ixPos + 0] * 2 + ix[ixPos + 1];
  1473. ixPos += 2;
  1474. sum1 += hlen1[x];
  1475. } while (ixPos < end);
  1476. s.bits += sum1;
  1477. return 1;
  1478. }
  1479. function count_bit_noESC_from2(ix, ixPos, end, t1, s) {
  1480. /* No ESC-words */
  1481. var sum = 0, sum2;
  1482. var xlen = Tables.ht[t1].xlen;
  1483. var hlen;
  1484. if (t1 == 2)
  1485. hlen = Tables.table23;
  1486. else
  1487. hlen = Tables.table56;
  1488. do {
  1489. var x = ix[ixPos + 0] * xlen + ix[ixPos + 1];
  1490. ixPos += 2;
  1491. sum += hlen[x];
  1492. } while (ixPos < end);
  1493. sum2 = sum & 0xffff;
  1494. sum >>= 16;
  1495. if (sum > sum2) {
  1496. sum = sum2;
  1497. t1++;
  1498. }
  1499. s.bits += sum;
  1500. return t1;
  1501. }
  1502. function count_bit_noESC_from3(ix, ixPos, end, t1, s) {
  1503. /* No ESC-words */
  1504. var sum1 = 0;
  1505. var sum2 = 0;
  1506. var sum3 = 0;
  1507. var xlen = Tables.ht[t1].xlen;
  1508. var hlen1 = Tables.ht[t1].hlen;
  1509. var hlen2 = Tables.ht[t1 + 1].hlen;
  1510. var hlen3 = Tables.ht[t1 + 2].hlen;
  1511. do {
  1512. var x = ix[ixPos + 0] * xlen + ix[ixPos + 1];
  1513. ixPos += 2;
  1514. sum1 += hlen1[x];
  1515. sum2 += hlen2[x];
  1516. sum3 += hlen3[x];
  1517. } while (ixPos < end);
  1518. var t = t1;
  1519. if (sum1 > sum2) {
  1520. sum1 = sum2;
  1521. t++;
  1522. }
  1523. if (sum1 > sum3) {
  1524. sum1 = sum3;
  1525. t = t1 + 2;
  1526. }
  1527. s.bits += sum1;
  1528. return t;
  1529. }
  1530. /*************************************************************************/
  1531. /* choose table */
  1532. /*************************************************************************/
  1533. var huf_tbl_noESC = [1, 2, 5, 7, 7, 10, 10, 13, 13,
  1534. 13, 13, 13, 13, 13, 13];
  1535. /**
  1536. * Choose the Huffman table that will encode ix[begin..end] with the fewest
  1537. * bits.
  1538. *
  1539. * Note: This code contains knowledge about the sizes and characteristics of
  1540. * the Huffman tables as defined in the IS (Table B.7), and will not work
  1541. * with any arbitrary tables.
  1542. */
  1543. function choose_table(ix, ixPos, endPos, s) {
  1544. var max = ix_max(ix, ixPos, endPos);
  1545. switch (max) {
  1546. case 0:
  1547. return max;
  1548. case 1:
  1549. return count_bit_noESC(ix, ixPos, endPos, s);
  1550. case 2:
  1551. case 3:
  1552. return count_bit_noESC_from2(ix, ixPos, endPos,
  1553. huf_tbl_noESC[max - 1], s);
  1554. case 4:
  1555. case 5:
  1556. case 6:
  1557. case 7:
  1558. case 8:
  1559. case 9:
  1560. case 10:
  1561. case 11:
  1562. case 12:
  1563. case 13:
  1564. case 14:
  1565. case 15:
  1566. return count_bit_noESC_from3(ix, ixPos, endPos,
  1567. huf_tbl_noESC[max - 1], s);
  1568. default:
  1569. /* try tables with linbits */
  1570. if (max > QuantizePVT.IXMAX_VAL) {
  1571. s.bits = QuantizePVT.LARGE_BITS;
  1572. return -1;
  1573. }
  1574. max -= 15;
  1575. var choice2;
  1576. for (choice2 = 24; choice2 < 32; choice2++) {
  1577. if (Tables.ht[choice2].linmax >= max) {
  1578. break;
  1579. }
  1580. }
  1581. var choice;
  1582. for (choice = choice2 - 8; choice < 24; choice++) {
  1583. if (Tables.ht[choice].linmax >= max) {
  1584. break;
  1585. }
  1586. }
  1587. return count_bit_ESC(ix, ixPos, endPos, choice, choice2, s);
  1588. }
  1589. }
  1590. /**
  1591. * count_bit
  1592. */
  1593. this.noquant_count_bits = function (gfc, gi, prev_noise) {
  1594. var ix = gi.l3_enc;
  1595. var i = Math.min(576, ((gi.max_nonzero_coeff + 2) >> 1) << 1);
  1596. if (prev_noise != null)
  1597. prev_noise.sfb_count1 = 0;
  1598. /* Determine count1 region */
  1599. for (; i > 1; i -= 2)
  1600. if ((ix[i - 1] | ix[i - 2]) != 0)
  1601. break;
  1602. gi.count1 = i;
  1603. /* Determines the number of bits to encode the quadruples. */
  1604. var a1 = 0;
  1605. var a2 = 0;
  1606. for (; i > 3; i -= 4) {
  1607. var p;
  1608. /* hack to check if all values <= 1 */
  1609. //throw "TODO: HACK if ((((long) ix[i - 1] | (long) ix[i - 2] | (long) ix[i - 3] | (long) ix[i - 4]) & 0xffffffffL) > 1L "
  1610. //if (true) {
  1611. if (((ix[i - 1] | ix[i - 2] | ix[i - 3] | ix[i - 4]) & 0x7fffffff) > 1) {
  1612. break;
  1613. }
  1614. p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
  1615. a1 += Tables.t32l[p];
  1616. a2 += Tables.t33l[p];
  1617. }
  1618. var bits = a1;
  1619. gi.count1table_select = 0;
  1620. if (a1 > a2) {
  1621. bits = a2;
  1622. gi.count1table_select = 1;
  1623. }
  1624. gi.count1bits = bits;
  1625. gi.big_values = i;
  1626. if (i == 0)
  1627. return bits;
  1628. if (gi.block_type == Encoder.SHORT_TYPE) {
  1629. a1 = 3 * gfc.scalefac_band.s[3];
  1630. if (a1 > gi.big_values)
  1631. a1 = gi.big_values;
  1632. a2 = gi.big_values;
  1633. } else if (gi.block_type == Encoder.NORM_TYPE) {
  1634. /* bv_scf has 576 entries (0..575) */
  1635. a1 = gi.region0_count = gfc.bv_scf[i - 2];
  1636. a2 = gi.region1_count = gfc.bv_scf[i - 1];
  1637. a2 = gfc.scalefac_band.l[a1 + a2 + 2];
  1638. a1 = gfc.scalefac_band.l[a1 + 1];
  1639. if (a2 < i) {
  1640. var bi = new Bits(bits);
  1641. gi.table_select[2] = choose_table(ix, a2, i, bi);
  1642. bits = bi.bits;
  1643. }
  1644. } else {
  1645. gi.region0_count = 7;
  1646. /* gi.region1_count = SBPSY_l - 7 - 1; */
  1647. gi.region1_count = Encoder.SBMAX_l - 1 - 7 - 1;
  1648. a1 = gfc.scalefac_band.l[7 + 1];
  1649. a2 = i;
  1650. if (a1 > a2) {
  1651. a1 = a2;
  1652. }
  1653. }
  1654. /* have to allow for the case when bigvalues < region0 < region1 */
  1655. /* (and region0, region1 are ignored) */
  1656. a1 = Math.min(a1, i);
  1657. a2 = Math.min(a2, i);
  1658. /* Count the number of bits necessary to code the bigvalues region. */
  1659. if (0 < a1) {
  1660. var bi = new Bits(bits);
  1661. gi.table_select[0] = choose_table(ix, 0, a1, bi);
  1662. bits = bi.bits;
  1663. }
  1664. if (a1 < a2) {
  1665. var bi = new Bits(bits);
  1666. gi.table_select[1] = choose_table(ix, a1, a2, bi);
  1667. bits = bi.bits;
  1668. }
  1669. if (gfc.use_best_huffman == 2) {
  1670. gi.part2_3_length = bits;
  1671. best_huffman_divide(gfc, gi);
  1672. bits = gi.part2_3_length;
  1673. }
  1674. if (prev_noise != null) {
  1675. if (gi.block_type == Encoder.NORM_TYPE) {
  1676. var sfb = 0;
  1677. while (gfc.scalefac_band.l[sfb] < gi.big_values) {
  1678. sfb++;
  1679. }
  1680. prev_noise.sfb_count1 = sfb;
  1681. }
  1682. }
  1683. return bits;
  1684. }
  1685. this.count_bits = function (gfc, xr, gi, prev_noise) {
  1686. var ix = gi.l3_enc;
  1687. /* since quantize_xrpow uses table lookup, we need to check this first: */
  1688. var w = (QuantizePVT.IXMAX_VAL) / qupvt.IPOW20(gi.global_gain);
  1689. if (gi.xrpow_max > w)
  1690. return QuantizePVT.LARGE_BITS;
  1691. quantize_xrpow(xr, ix, qupvt.IPOW20(gi.global_gain), gi, prev_noise);
  1692. if ((gfc.substep_shaping & 2) != 0) {
  1693. var j = 0;
  1694. /* 0.634521682242439 = 0.5946*2**(.5*0.1875) */
  1695. var gain = gi.global_gain + gi.scalefac_scale;
  1696. var roundfac = 0.634521682242439 / qupvt.IPOW20(gain);
  1697. for (var sfb = 0; sfb < gi.sfbmax; sfb++) {
  1698. var width = gi.width[sfb];
  1699. if (0 == gfc.pseudohalf[sfb]) {
  1700. j += width;
  1701. } else {
  1702. var k;
  1703. for (k = j, j += width; k < j; ++k) {
  1704. ix[k] = (xr[k] >= roundfac) ? ix[k] : 0;
  1705. }
  1706. }
  1707. }
  1708. }
  1709. return this.noquant_count_bits(gfc, gi, prev_noise);
  1710. }
  1711. /**
  1712. * re-calculate the best scalefac_compress using scfsi the saved bits are
  1713. * kept in the bit reservoir.
  1714. */
  1715. function recalc_divide_init(gfc, cod_info, ix, r01_bits, r01_div, r0_tbl, r1_tbl) {
  1716. var bigv = cod_info.big_values;
  1717. for (var r0 = 0; r0 <= 7 + 15; r0++) {
  1718. r01_bits[r0] = QuantizePVT.LARGE_BITS;
  1719. }
  1720. for (var r0 = 0; r0 < 16; r0++) {
  1721. var a1 = gfc.scalefac_band.l[r0 + 1];
  1722. if (a1 >= bigv)
  1723. break;
  1724. var r0bits = 0;
  1725. var bi = new Bits(r0bits);
  1726. var r0t = choose_table(ix, 0, a1, bi);
  1727. r0bits = bi.bits;
  1728. for (var r1 = 0; r1 < 8; r1++) {
  1729. var a2 = gfc.scalefac_band.l[r0 + r1 + 2];
  1730. if (a2 >= bigv)
  1731. break;
  1732. var bits = r0bits;
  1733. bi = new Bits(bits);
  1734. var r1t = choose_table(ix, a1, a2, bi);
  1735. bits = bi.bits;
  1736. if (r01_bits[r0 + r1] > bits) {
  1737. r01_bits[r0 + r1] = bits;
  1738. r01_div[r0 + r1] = r0;
  1739. r0_tbl[r0 + r1] = r0t;
  1740. r1_tbl[r0 + r1] = r1t;
  1741. }
  1742. }
  1743. }
  1744. }
  1745. function recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl) {
  1746. var bigv = cod_info2.big_values;
  1747. for (var r2 = 2; r2 < Encoder.SBMAX_l + 1; r2++) {
  1748. var a2 = gfc.scalefac_band.l[r2];
  1749. if (a2 >= bigv)
  1750. break;
  1751. var bits = r01_bits[r2 - 2] + cod_info2.count1bits;
  1752. if (gi.part2_3_length <= bits)
  1753. break;
  1754. var bi = new Bits(bits);
  1755. var r2t = choose_table(ix, a2, bigv, bi);
  1756. bits = bi.bits;
  1757. if (gi.part2_3_length <= bits)
  1758. continue;
  1759. gi.assign(cod_info2);
  1760. gi.part2_3_length = bits;
  1761. gi.region0_count = r01_div[r2 - 2];
  1762. gi.region1_count = r2 - 2 - r01_div[r2 - 2];
  1763. gi.table_select[0] = r0_tbl[r2 - 2];
  1764. gi.table_select[1] = r1_tbl[r2 - 2];
  1765. gi.table_select[2] = r2t;
  1766. }
  1767. }
  1768. this.best_huffman_divide = function (gfc, gi) {
  1769. var cod_info2 = new GrInfo();
  1770. var ix = gi.l3_enc;
  1771. var r01_bits = new_int(7 + 15 + 1);
  1772. var r01_div = new_int(7 + 15 + 1);
  1773. var r0_tbl = new_int(7 + 15 + 1);
  1774. var r1_tbl = new_int(7 + 15 + 1);
  1775. /* SHORT BLOCK stuff fails for MPEG2 */
  1776. if (gi.block_type == Encoder.SHORT_TYPE && gfc.mode_gr == 1)
  1777. return;
  1778. cod_info2.assign(gi);
  1779. if (gi.block_type == Encoder.NORM_TYPE) {
  1780. recalc_divide_init(gfc, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
  1781. recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div,
  1782. r0_tbl, r1_tbl);
  1783. }
  1784. var i = cod_info2.big_values;
  1785. if (i == 0 || (ix[i - 2] | ix[i - 1]) > 1)
  1786. return;
  1787. i = gi.count1 + 2;
  1788. if (i > 576)
  1789. return;
  1790. /* Determines the number of bits to encode the quadruples. */
  1791. cod_info2.assign(gi);
  1792. cod_info2.count1 = i;
  1793. var a1 = 0;
  1794. var a2 = 0;
  1795. for (; i > cod_info2.big_values; i -= 4) {
  1796. var p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2
  1797. + ix[i - 1];
  1798. a1 += Tables.t32l[p];
  1799. a2 += Tables.t33l[p];
  1800. }
  1801. cod_info2.big_values = i;
  1802. cod_info2.count1table_select = 0;
  1803. if (a1 > a2) {
  1804. a1 = a2;
  1805. cod_info2.count1table_select = 1;
  1806. }
  1807. cod_info2.count1bits = a1;
  1808. if (cod_info2.block_type == Encoder.NORM_TYPE)
  1809. recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div,
  1810. r0_tbl, r1_tbl);
  1811. else {
  1812. /* Count the number of bits necessary to code the bigvalues region. */
  1813. cod_info2.part2_3_length = a1;
  1814. a1 = gfc.scalefac_band.l[7 + 1];
  1815. if (a1 > i) {
  1816. a1 = i;
  1817. }
  1818. if (a1 > 0) {
  1819. var bi = new Bits(cod_info2.part2_3_length);
  1820. cod_info2.table_select[0] = choose_table(ix, 0, a1, bi);
  1821. cod_info2.part2_3_length = bi.bits;
  1822. }
  1823. if (i > a1) {
  1824. var bi = new Bits(cod_info2.part2_3_length);
  1825. cod_info2.table_select[1] = choose_table(ix, a1, i, bi);
  1826. cod_info2.part2_3_length = bi.bits;
  1827. }
  1828. if (gi.part2_3_length > cod_info2.part2_3_length)
  1829. gi.assign(cod_info2);
  1830. }
  1831. }
  1832. var slen1_n = [1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16];
  1833. var slen2_n = [1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8];
  1834. var slen1_tab = [0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4];
  1835. var slen2_tab = [0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3];
  1836. Takehiro.slen1_tab = slen1_tab;
  1837. Takehiro.slen2_tab = slen2_tab;
  1838. function scfsi_calc(ch, l3_side) {
  1839. var sfb;
  1840. var gi = l3_side.tt[1][ch];
  1841. var g0 = l3_side.tt[0][ch];
  1842. for (var i = 0; i < Tables.scfsi_band.length - 1; i++) {
  1843. for (sfb = Tables.scfsi_band[i]; sfb < Tables.scfsi_band[i + 1]; sfb++) {
  1844. if (g0.scalefac[sfb] != gi.scalefac[sfb]
  1845. && gi.scalefac[sfb] >= 0)
  1846. break;
  1847. }
  1848. if (sfb == Tables.scfsi_band[i + 1]) {
  1849. for (sfb = Tables.scfsi_band[i]; sfb < Tables.scfsi_band[i + 1]; sfb++) {
  1850. gi.scalefac[sfb] = -1;
  1851. }
  1852. l3_side.scfsi[ch][i] = 1;
  1853. }
  1854. }
  1855. var s1 = 0;
  1856. var c1 = 0;
  1857. for (sfb = 0; sfb < 11; sfb++) {
  1858. if (gi.scalefac[sfb] == -1)
  1859. continue;
  1860. c1++;
  1861. if (s1 < gi.scalefac[sfb])
  1862. s1 = gi.scalefac[sfb];
  1863. }
  1864. var s2 = 0;
  1865. var c2 = 0;
  1866. for (; sfb < Encoder.SBPSY_l; sfb++) {
  1867. if (gi.scalefac[sfb] == -1)
  1868. continue;
  1869. c2++;
  1870. if (s2 < gi.scalefac[sfb])
  1871. s2 = gi.scalefac[sfb];
  1872. }
  1873. for (var i = 0; i < 16; i++) {
  1874. if (s1 < slen1_n[i] && s2 < slen2_n[i]) {
  1875. var c = slen1_tab[i] * c1 + slen2_tab[i] * c2;
  1876. if (gi.part2_length > c) {
  1877. gi.part2_length = c;
  1878. gi.scalefac_compress = i;
  1879. }
  1880. }
  1881. }
  1882. }
  1883. /**
  1884. * Find the optimal way to store the scalefactors. Only call this routine
  1885. * after final scalefactors have been chosen and the channel/granule will
  1886. * not be re-encoded.
  1887. */
  1888. this.best_scalefac_store = function (gfc, gr, ch, l3_side) {
  1889. /* use scalefac_scale if we can */
  1890. var gi = l3_side.tt[gr][ch];
  1891. var sfb, i, j, l;
  1892. var recalc = 0;
  1893. /*
  1894. * remove scalefacs from bands with ix=0. This idea comes from the AAC
  1895. * ISO docs. added mt 3/00
  1896. */
  1897. /* check if l3_enc=0 */
  1898. j = 0;
  1899. for (sfb = 0; sfb < gi.sfbmax; sfb++) {
  1900. var width = gi.width[sfb];
  1901. j += width;
  1902. for (l = -width; l < 0; l++) {
  1903. if (gi.l3_enc[l + j] != 0)
  1904. break;
  1905. }
  1906. if (l == 0)
  1907. gi.scalefac[sfb] = recalc = -2;
  1908. /* anything goes. */
  1909. /*
  1910. * only best_scalefac_store and calc_scfsi know--and only they
  1911. * should know--about the magic number -2.
  1912. */
  1913. }
  1914. if (0 == gi.scalefac_scale && 0 == gi.preflag) {
  1915. var s = 0;
  1916. for (sfb = 0; sfb < gi.sfbmax; sfb++)
  1917. if (gi.scalefac[sfb] > 0)
  1918. s |= gi.scalefac[sfb];
  1919. if (0 == (s & 1) && s != 0) {
  1920. for (sfb = 0; sfb < gi.sfbmax; sfb++)
  1921. if (gi.scalefac[sfb] > 0)
  1922. gi.scalefac[sfb] >>= 1;
  1923. gi.scalefac_scale = recalc = 1;
  1924. }
  1925. }
  1926. if (0 == gi.preflag && gi.block_type != Encoder.SHORT_TYPE
  1927. && gfc.mode_gr == 2) {
  1928. for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
  1929. if (gi.scalefac[sfb] < qupvt.pretab[sfb]
  1930. && gi.scalefac[sfb] != -2)
  1931. break;
  1932. if (sfb == Encoder.SBPSY_l) {
  1933. for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
  1934. if (gi.scalefac[sfb] > 0)
  1935. gi.scalefac[sfb] -= qupvt.pretab[sfb];
  1936. gi.preflag = recalc = 1;
  1937. }
  1938. }
  1939. for (i = 0; i < 4; i++)
  1940. l3_side.scfsi[ch][i] = 0;
  1941. if (gfc.mode_gr == 2 && gr == 1
  1942. && l3_side.tt[0][ch].block_type != Encoder.SHORT_TYPE
  1943. && l3_side.tt[1][ch].block_type != Encoder.SHORT_TYPE) {
  1944. scfsi_calc(ch, l3_side);
  1945. recalc = 0;
  1946. }
  1947. for (sfb = 0; sfb < gi.sfbmax; sfb++) {
  1948. if (gi.scalefac[sfb] == -2) {
  1949. gi.scalefac[sfb] = 0;
  1950. /* if anything goes, then 0 is a good choice */
  1951. }
  1952. }
  1953. if (recalc != 0) {
  1954. if (gfc.mode_gr == 2) {
  1955. this.scale_bitcount(gi);
  1956. } else {
  1957. this.scale_bitcount_lsf(gfc, gi);
  1958. }
  1959. }
  1960. }
  1961. function all_scalefactors_not_negative(scalefac, n) {
  1962. for (var i = 0; i < n; ++i) {
  1963. if (scalefac[i] < 0)
  1964. return false;
  1965. }
  1966. return true;
  1967. }
  1968. /**
  1969. * number of bits used to encode scalefacs.
  1970. *
  1971. * 18*slen1_tab[i] + 18*slen2_tab[i]
  1972. */
  1973. var scale_short = [0, 18, 36, 54, 54, 36, 54, 72,
  1974. 54, 72, 90, 72, 90, 108, 108, 126];
  1975. /**
  1976. * number of bits used to encode scalefacs.
  1977. *
  1978. * 17*slen1_tab[i] + 18*slen2_tab[i]
  1979. */
  1980. var scale_mixed = [0, 18, 36, 54, 51, 35, 53, 71,
  1981. 52, 70, 88, 69, 87, 105, 104, 122];
  1982. /**
  1983. * number of bits used to encode scalefacs.
  1984. *
  1985. * 11*slen1_tab[i] + 10*slen2_tab[i]
  1986. */
  1987. var scale_long = [0, 10, 20, 30, 33, 21, 31, 41, 32, 42,
  1988. 52, 43, 53, 63, 64, 74];
  1989. /**
  1990. * Also calculates the number of bits necessary to code the scalefactors.
  1991. */
  1992. this.scale_bitcount = function (cod_info) {
  1993. var k, sfb, max_slen1 = 0, max_slen2 = 0;
  1994. /* maximum values */
  1995. var tab;
  1996. var scalefac = cod_info.scalefac;
  1997. if (cod_info.block_type == Encoder.SHORT_TYPE) {
  1998. tab = scale_short;
  1999. if (cod_info.mixed_block_flag != 0)
  2000. tab = scale_mixed;
  2001. } else { /* block_type == 1,2,or 3 */
  2002. tab = scale_long;
  2003. if (0 == cod_info.preflag) {
  2004. for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
  2005. if (scalefac[sfb] < qupvt.pretab[sfb])
  2006. break;
  2007. if (sfb == Encoder.SBPSY_l) {
  2008. cod_info.preflag = 1;
  2009. for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
  2010. scalefac[sfb] -= qupvt.pretab[sfb];
  2011. }
  2012. }
  2013. }
  2014. for (sfb = 0; sfb < cod_info.sfbdivide; sfb++)
  2015. if (max_slen1 < scalefac[sfb])
  2016. max_slen1 = scalefac[sfb];
  2017. for (; sfb < cod_info.sfbmax; sfb++)
  2018. if (max_slen2 < scalefac[sfb])
  2019. max_slen2 = scalefac[sfb];
  2020. /*
  2021. * from Takehiro TOMINAGA <tominaga@isoternet.org> 10/99 loop over *all*
  2022. * posible values of scalefac_compress to find the one which uses the
  2023. * smallest number of bits. ISO would stop at first valid index
  2024. */
  2025. cod_info.part2_length = QuantizePVT.LARGE_BITS;
  2026. for (k = 0; k < 16; k++) {
  2027. if (max_slen1 < slen1_n[k] && max_slen2 < slen2_n[k]
  2028. && cod_info.part2_length > tab[k]) {
  2029. cod_info.part2_length = tab[k];
  2030. cod_info.scalefac_compress = k;
  2031. }
  2032. }
  2033. return cod_info.part2_length == QuantizePVT.LARGE_BITS;
  2034. }
  2035. /**
  2036. * table of largest scalefactor values for MPEG2
  2037. */
  2038. var max_range_sfac_tab = [[15, 15, 7, 7],
  2039. [15, 15, 7, 0], [7, 3, 0, 0], [15, 31, 31, 0],
  2040. [7, 7, 7, 0], [3, 3, 0, 0]];
  2041. /**
  2042. * Also counts the number of bits to encode the scalefacs but for MPEG 2
  2043. * Lower sampling frequencies (24, 22.05 and 16 kHz.)
  2044. *
  2045. * This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS,
  2046. * "Audio Decoding Layer III"
  2047. */
  2048. this.scale_bitcount_lsf = function (gfc, cod_info) {
  2049. var table_number, row_in_table, partition, nr_sfb, window;
  2050. var over;
  2051. var i, sfb;
  2052. var max_sfac = new_int(4);
  2053. //var partition_table;
  2054. var scalefac = cod_info.scalefac;
  2055. /*
  2056. * Set partition table. Note that should try to use table one, but do
  2057. * not yet...
  2058. */
  2059. if (cod_info.preflag != 0)
  2060. table_number = 2;
  2061. else
  2062. table_number = 0;
  2063. for (i = 0; i < 4; i++)
  2064. max_sfac[i] = 0;
  2065. if (cod_info.block_type == Encoder.SHORT_TYPE) {
  2066. row_in_table = 1;
  2067. var partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
  2068. for (sfb = 0, partition = 0; partition < 4; partition++) {
  2069. nr_sfb = partition_table[partition] / 3;
  2070. for (i = 0; i < nr_sfb; i++, sfb++)
  2071. for (window = 0; window < 3; window++)
  2072. if (scalefac[sfb * 3 + window] > max_sfac[partition])
  2073. max_sfac[partition] = scalefac[sfb * 3 + window];
  2074. }
  2075. } else {
  2076. row_in_table = 0;
  2077. var partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
  2078. for (sfb = 0, partition = 0; partition < 4; partition++) {
  2079. nr_sfb = partition_table[partition];
  2080. for (i = 0; i < nr_sfb; i++, sfb++)
  2081. if (scalefac[sfb] > max_sfac[partition])
  2082. max_sfac[partition] = scalefac[sfb];
  2083. }
  2084. }
  2085. for (over = false, partition = 0; partition < 4; partition++) {
  2086. if (max_sfac[partition] > max_range_sfac_tab[table_number][partition])
  2087. over = true;
  2088. }
  2089. if (!over) {
  2090. var slen1, slen2, slen3, slen4;
  2091. cod_info.sfb_partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
  2092. for (partition = 0; partition < 4; partition++)
  2093. cod_info.slen[partition] = log2tab[max_sfac[partition]];
  2094. /* set scalefac_compress */
  2095. slen1 = cod_info.slen[0];
  2096. slen2 = cod_info.slen[1];
  2097. slen3 = cod_info.slen[2];
  2098. slen4 = cod_info.slen[3];
  2099. switch (table_number) {
  2100. case 0:
  2101. cod_info.scalefac_compress = (((slen1 * 5) + slen2) << 4)
  2102. + (slen3 << 2) + slen4;
  2103. break;
  2104. case 1:
  2105. cod_info.scalefac_compress = 400 + (((slen1 * 5) + slen2) << 2)
  2106. + slen3;
  2107. break;
  2108. case 2:
  2109. cod_info.scalefac_compress = 500 + (slen1 * 3) + slen2;
  2110. break;
  2111. default:
  2112. System.err.printf("intensity stereo not implemented yet\n");
  2113. break;
  2114. }
  2115. }
  2116. if (!over) {
  2117. cod_info.part2_length = 0;
  2118. for (partition = 0; partition < 4; partition++)
  2119. cod_info.part2_length += cod_info.slen[partition]
  2120. * cod_info.sfb_partition_table[partition];
  2121. }
  2122. return over;
  2123. }
  2124. /*
  2125. * Since no bands have been over-amplified, we can set scalefac_compress and
  2126. * slen[] for the formatter
  2127. */
  2128. var log2tab = [0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
  2129. 4, 4, 4, 4];
  2130. this.huffman_init = function (gfc) {
  2131. for (var i = 2; i <= 576; i += 2) {
  2132. var scfb_anz = 0, bv_index;
  2133. while (gfc.scalefac_band.l[++scfb_anz] < i)
  2134. ;
  2135. bv_index = subdv_table[scfb_anz][0]; // .region0_count
  2136. while (gfc.scalefac_band.l[bv_index + 1] > i)
  2137. bv_index--;
  2138. if (bv_index < 0) {
  2139. /*
  2140. * this is an indication that everything is going to be encoded
  2141. * as region0: bigvalues < region0 < region1 so lets set
  2142. * region0, region1 to some value larger than bigvalues
  2143. */
  2144. bv_index = subdv_table[scfb_anz][0]; // .region0_count
  2145. }
  2146. gfc.bv_scf[i - 2] = bv_index;
  2147. bv_index = subdv_table[scfb_anz][1]; // .region1_count
  2148. while (gfc.scalefac_band.l[bv_index + gfc.bv_scf[i - 2] + 2] > i)
  2149. bv_index--;
  2150. if (bv_index < 0) {
  2151. bv_index = subdv_table[scfb_anz][1]; // .region1_count
  2152. }
  2153. gfc.bv_scf[i - 1] = bv_index;
  2154. }
  2155. }
  2156. }
  2157. /*
  2158. * bit reservoir source file
  2159. *
  2160. * Copyright (c) 1999-2000 Mark Taylor
  2161. *
  2162. * This library is free software; you can redistribute it and/or
  2163. * modify it under the terms of the GNU Lesser General Public
  2164. * License as published by the Free Software Foundation; either
  2165. * version 2 of the License, or (at your option) any later version.
  2166. *
  2167. * This library is distributed in the hope that it will be useful,
  2168. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2169. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  2170. * Library General Public License for more details.
  2171. *
  2172. * You should have received a copy of the GNU Lesser General Public
  2173. * License along with this library; if not, write to the
  2174. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  2175. * Boston, MA 02111-1307, USA.
  2176. */
  2177. /* $Id: Reservoir.java,v 1.9 2011/05/24 20:48:06 kenchis Exp $ */
  2178. //package mp3;
  2179. /**
  2180. * ResvFrameBegin:<BR>
  2181. * Called (repeatedly) at the beginning of a frame. Updates the maximum size of
  2182. * the reservoir, and checks to make sure main_data_begin was set properly by
  2183. * the formatter<BR>
  2184. * Background information:
  2185. *
  2186. * This is the original text from the ISO standard. Because of sooo many bugs
  2187. * and irritations correcting comments are added in brackets []. A '^W' means
  2188. * you should remove the last word.
  2189. *
  2190. * <PRE>
  2191. * 1. The following rule can be used to calculate the maximum
  2192. * number of bits used for one granule [^W frame]:<BR>
  2193. * At the highest possible bitrate of Layer III (320 kbps
  2194. * per stereo signal [^W^W^W], 48 kHz) the frames must be of
  2195. * [^W^W^W are designed to have] constant length, i.e.
  2196. * one buffer [^W^W the frame] length is:<BR>
  2197. *
  2198. * 320 kbps * 1152/48 kHz = 7680 bit = 960 byte
  2199. *
  2200. * This value is used as the maximum buffer per channel [^W^W] at
  2201. * lower bitrates [than 320 kbps]. At 64 kbps mono or 128 kbps
  2202. * stereo the main granule length is 64 kbps * 576/48 kHz = 768 bit
  2203. * [per granule and channel] at 48 kHz sampling frequency.
  2204. * This means that there is a maximum deviation (short time buffer
  2205. * [= reservoir]) of 7680 - 2*2*768 = 4608 bits is allowed at 64 kbps.
  2206. * The actual deviation is equal to the number of bytes [with the
  2207. * meaning of octets] denoted by the main_data_end offset pointer.
  2208. * The actual maximum deviation is (2^9-1)*8 bit = 4088 bits
  2209. * [for MPEG-1 and (2^8-1)*8 bit for MPEG-2, both are hard limits].
  2210. * ... The xchange of buffer bits between the left and right channel
  2211. * is allowed without restrictions [exception: dual channel].
  2212. * Because of the [constructed] constraint on the buffer size
  2213. * main_data_end is always set to 0 in the case of bit_rate_index==14,
  2214. * i.e. data rate 320 kbps per stereo signal [^W^W^W]. In this case
  2215. * all data are allocated between adjacent header [^W sync] words
  2216. * [, i.e. there is no buffering at all].
  2217. * </PRE>
  2218. */
  2219. function Reservoir() {
  2220. var bs;
  2221. this.setModules = function(_bs) {
  2222. bs = _bs;
  2223. }
  2224. this.ResvFrameBegin = function(gfp, mean_bits) {
  2225. var gfc = gfp.internal_flags;
  2226. var maxmp3buf;
  2227. var l3_side = gfc.l3_side;
  2228. var frameLength = bs.getframebits(gfp);
  2229. mean_bits.bits = (frameLength - gfc.sideinfo_len * 8) / gfc.mode_gr;
  2230. /**
  2231. * <PRE>
  2232. * Meaning of the variables:
  2233. * resvLimit: (0, 8, ..., 8*255 (MPEG-2), 8*511 (MPEG-1))
  2234. * Number of bits can be stored in previous frame(s) due to
  2235. * counter size constaints
  2236. * maxmp3buf: ( ??? ... 8*1951 (MPEG-1 and 2), 8*2047 (MPEG-2.5))
  2237. * Number of bits allowed to encode one frame (you can take 8*511 bit
  2238. * from the bit reservoir and at most 8*1440 bit from the current
  2239. * frame (320 kbps, 32 kHz), so 8*1951 bit is the largest possible
  2240. * value for MPEG-1 and -2)
  2241. *
  2242. * maximum allowed granule/channel size times 4 = 8*2047 bits.,
  2243. * so this is the absolute maximum supported by the format.
  2244. *
  2245. *
  2246. * fullFrameBits: maximum number of bits available for encoding
  2247. * the current frame.
  2248. *
  2249. * mean_bits: target number of bits per granule.
  2250. *
  2251. * frameLength:
  2252. *
  2253. * gfc.ResvMax: maximum allowed reservoir
  2254. *
  2255. * gfc.ResvSize: current reservoir size
  2256. *
  2257. * l3_side.resvDrain_pre:
  2258. * ancillary data to be added to previous frame:
  2259. * (only usefull in VBR modes if it is possible to have
  2260. * maxmp3buf < fullFrameBits)). Currently disabled,
  2261. * see #define NEW_DRAIN
  2262. * 2010-02-13: RH now enabled, it seems to be needed for CBR too,
  2263. * as there exists one example, where the FhG decoder
  2264. * can't decode a -b320 CBR file anymore.
  2265. *
  2266. * l3_side.resvDrain_post:
  2267. * ancillary data to be added to this frame:
  2268. *
  2269. * </PRE>
  2270. */
  2271. /* main_data_begin has 9 bits in MPEG-1, 8 bits MPEG-2 */
  2272. var resvLimit = (8 * 256) * gfc.mode_gr - 8;
  2273. /*
  2274. * maximum allowed frame size. dont use more than this number of bits,
  2275. * even if the frame has the space for them:
  2276. */
  2277. if (gfp.brate > 320) {
  2278. /* in freeformat the buffer is constant */
  2279. maxmp3buf = 8 * ((int) ((gfp.brate * 1000)
  2280. / (gfp.out_samplerate / 1152) / 8 + .5));
  2281. } else {
  2282. /*
  2283. * all mp3 decoders should have enough buffer to handle this value:
  2284. * size of a 320kbps 32kHz frame
  2285. */
  2286. maxmp3buf = 8 * 1440;
  2287. /*
  2288. * Bouvigne suggests this more lax interpretation of the ISO doc
  2289. * instead of using 8*960.
  2290. */
  2291. if (gfp.strict_ISO) {
  2292. maxmp3buf = 8 * ((int) (320000 / (gfp.out_samplerate / 1152) / 8 + .5));
  2293. }
  2294. }
  2295. gfc.ResvMax = maxmp3buf - frameLength;
  2296. if (gfc.ResvMax > resvLimit)
  2297. gfc.ResvMax = resvLimit;
  2298. if (gfc.ResvMax < 0 || gfp.disable_reservoir)
  2299. gfc.ResvMax = 0;
  2300. var fullFrameBits = mean_bits.bits * gfc.mode_gr
  2301. + Math.min(gfc.ResvSize, gfc.ResvMax);
  2302. if (fullFrameBits > maxmp3buf)
  2303. fullFrameBits = maxmp3buf;
  2304. l3_side.resvDrain_pre = 0;
  2305. // frame analyzer code
  2306. if (gfc.pinfo != null) {
  2307. /*
  2308. * expected bits per channel per granule [is this also right for
  2309. * mono/stereo, MPEG-1/2 ?]
  2310. */
  2311. gfc.pinfo.mean_bits = mean_bits.bits / 2;
  2312. gfc.pinfo.resvsize = gfc.ResvSize;
  2313. }
  2314. return fullFrameBits;
  2315. }
  2316. /**
  2317. * returns targ_bits: target number of bits to use for 1 granule<BR>
  2318. * extra_bits: amount extra available from reservoir<BR>
  2319. * Mark Taylor 4/99
  2320. */
  2321. this.ResvMaxBits = function(gfp, mean_bits, targ_bits, cbr) {
  2322. var gfc = gfp.internal_flags;
  2323. var add_bits;
  2324. var ResvSize = gfc.ResvSize, ResvMax = gfc.ResvMax;
  2325. /* compensate the saved bits used in the 1st granule */
  2326. if (cbr != 0)
  2327. ResvSize += mean_bits;
  2328. if ((gfc.substep_shaping & 1) != 0)
  2329. ResvMax *= 0.9;
  2330. targ_bits.bits = mean_bits;
  2331. /* extra bits if the reservoir is almost full */
  2332. if (ResvSize * 10 > ResvMax * 9) {
  2333. add_bits = ResvSize - (ResvMax * 9) / 10;
  2334. targ_bits.bits += add_bits;
  2335. gfc.substep_shaping |= 0x80;
  2336. } else {
  2337. add_bits = 0;
  2338. gfc.substep_shaping &= 0x7f;
  2339. /*
  2340. * build up reservoir. this builds the reservoir a little slower
  2341. * than FhG. It could simple be mean_bits/15, but this was rigged to
  2342. * always produce 100 (the old value) at 128kbs
  2343. */
  2344. if (!gfp.disable_reservoir && 0 == (gfc.substep_shaping & 1))
  2345. targ_bits.bits -= .1 * mean_bits;
  2346. }
  2347. /* amount from the reservoir we are allowed to use. ISO says 6/10 */
  2348. var extra_bits = (ResvSize < (gfc.ResvMax * 6) / 10 ? ResvSize
  2349. : (gfc.ResvMax * 6) / 10);
  2350. extra_bits -= add_bits;
  2351. if (extra_bits < 0)
  2352. extra_bits = 0;
  2353. return extra_bits;
  2354. }
  2355. /**
  2356. * Called after a granule's bit allocation. Readjusts the size of the
  2357. * reservoir to reflect the granule's usage.
  2358. */
  2359. this.ResvAdjust = function(gfc, gi) {
  2360. gfc.ResvSize -= gi.part2_3_length + gi.part2_length;
  2361. }
  2362. /**
  2363. * Called after all granules in a frame have been allocated. Makes sure that
  2364. * the reservoir size is within limits, possibly by adding stuffing bits.
  2365. */
  2366. this.ResvFrameEnd = function(gfc, mean_bits) {
  2367. var over_bits;
  2368. var l3_side = gfc.l3_side;
  2369. gfc.ResvSize += mean_bits * gfc.mode_gr;
  2370. var stuffingBits = 0;
  2371. l3_side.resvDrain_post = 0;
  2372. l3_side.resvDrain_pre = 0;
  2373. /* we must be byte aligned */
  2374. if ((over_bits = gfc.ResvSize % 8) != 0)
  2375. stuffingBits += over_bits;
  2376. over_bits = (gfc.ResvSize - stuffingBits) - gfc.ResvMax;
  2377. if (over_bits > 0) {
  2378. stuffingBits += over_bits;
  2379. }
  2380. /*
  2381. * NOTE: enabling the NEW_DRAIN code fixes some problems with FhG
  2382. * decoder shipped with MS Windows operating systems. Using this, it is
  2383. * even possible to use Gabriel's lax buffer consideration again, which
  2384. * assumes, any decoder should have a buffer large enough for a 320 kbps
  2385. * frame at 32 kHz sample rate.
  2386. *
  2387. * old drain code: lame -b320 BlackBird.wav --. does not play with
  2388. * GraphEdit.exe using FhG decoder V1.5 Build 50
  2389. *
  2390. * new drain code: lame -b320 BlackBird.wav --. plays fine with
  2391. * GraphEdit.exe using FhG decoder V1.5 Build 50
  2392. *
  2393. * Robert Hegemann, 2010-02-13.
  2394. */
  2395. /*
  2396. * drain as many bits as possible into previous frame ancillary data In
  2397. * particular, in VBR mode ResvMax may have changed, and we have to make
  2398. * sure main_data_begin does not create a reservoir bigger than ResvMax
  2399. * mt 4/00
  2400. */
  2401. {
  2402. var mdb_bytes = Math.min(l3_side.main_data_begin * 8, stuffingBits) / 8;
  2403. l3_side.resvDrain_pre += 8 * mdb_bytes;
  2404. stuffingBits -= 8 * mdb_bytes;
  2405. gfc.ResvSize -= 8 * mdb_bytes;
  2406. l3_side.main_data_begin -= mdb_bytes;
  2407. }
  2408. /* drain the rest into this frames ancillary data */
  2409. l3_side.resvDrain_post += stuffingBits;
  2410. gfc.ResvSize -= stuffingBits;
  2411. }
  2412. }
  2413. BitStream.EQ = function (a, b) {
  2414. return (Math.abs(a) > Math.abs(b)) ? (Math.abs((a) - (b)) <= (Math
  2415. .abs(a) * 1e-6))
  2416. : (Math.abs((a) - (b)) <= (Math.abs(b) * 1e-6));
  2417. };
  2418. BitStream.NEQ = function (a, b) {
  2419. return !BitStream.EQ(a, b);
  2420. };
  2421. function BitStream() {
  2422. var self = this;
  2423. var CRC16_POLYNOMIAL = 0x8005;
  2424. /*
  2425. * we work with ints, so when doing bit manipulation, we limit ourselves to
  2426. * MAX_LENGTH-2 just to be on the safe side
  2427. */
  2428. var MAX_LENGTH = 32;
  2429. //GainAnalysis ga;
  2430. //MPGLib mpg;
  2431. //Version ver;
  2432. //VBRTag vbr;
  2433. var ga = null;
  2434. var mpg = null;
  2435. var ver = null;
  2436. var vbr = null;
  2437. //public final void setModules(GainAnalysis ga, MPGLib mpg, Version ver,
  2438. // VBRTag vbr) {
  2439. this.setModules = function (_ga, _mpg, _ver, _vbr) {
  2440. ga = _ga;
  2441. mpg = _mpg;
  2442. ver = _ver;
  2443. vbr = _vbr;
  2444. };
  2445. /**
  2446. * Bit stream buffer.
  2447. */
  2448. //private byte[] buf;
  2449. var buf = null;
  2450. /**
  2451. * Bit counter of bit stream.
  2452. */
  2453. var totbit = 0;
  2454. /**
  2455. * Pointer to top byte in buffer.
  2456. */
  2457. var bufByteIdx = 0;
  2458. /**
  2459. * Pointer to top bit of top byte in buffer.
  2460. */
  2461. var bufBitIdx = 0;
  2462. /**
  2463. * compute bitsperframe and mean_bits for a layer III frame
  2464. */
  2465. this.getframebits = function (gfp) {
  2466. var gfc = gfp.internal_flags;
  2467. var bit_rate;
  2468. /* get bitrate in kbps [?] */
  2469. if (gfc.bitrate_index != 0)
  2470. bit_rate = Tables.bitrate_table[gfp.version][gfc.bitrate_index];
  2471. else
  2472. bit_rate = gfp.brate;
  2473. /* main encoding routine toggles padding on and off */
  2474. /* one Layer3 Slot consists of 8 bits */
  2475. var bytes = 0 | (gfp.version + 1) * 72000 * bit_rate / gfp.out_samplerate + gfc.padding;
  2476. return 8 * bytes;
  2477. };
  2478. function putheader_bits(gfc) {
  2479. System.arraycopy(gfc.header[gfc.w_ptr].buf, 0, buf, bufByteIdx, gfc.sideinfo_len);
  2480. bufByteIdx += gfc.sideinfo_len;
  2481. totbit += gfc.sideinfo_len * 8;
  2482. gfc.w_ptr = (gfc.w_ptr + 1) & (LameInternalFlags.MAX_HEADER_BUF - 1);
  2483. }
  2484. /**
  2485. * write j bits into the bit stream
  2486. */
  2487. function putbits2(gfc, val, j) {
  2488. while (j > 0) {
  2489. var k;
  2490. if (bufBitIdx == 0) {
  2491. bufBitIdx = 8;
  2492. bufByteIdx++;
  2493. if (gfc.header[gfc.w_ptr].write_timing == totbit) {
  2494. putheader_bits(gfc);
  2495. }
  2496. buf[bufByteIdx] = 0;
  2497. }
  2498. k = Math.min(j, bufBitIdx);
  2499. j -= k;
  2500. bufBitIdx -= k;
  2501. /* 32 too large on 32 bit machines */
  2502. buf[bufByteIdx] |= ((val >> j) << bufBitIdx);
  2503. totbit += k;
  2504. }
  2505. }
  2506. /**
  2507. * write j bits into the bit stream, ignoring frame headers
  2508. */
  2509. function putbits_noheaders(gfc, val, j) {
  2510. while (j > 0) {
  2511. var k;
  2512. if (bufBitIdx == 0) {
  2513. bufBitIdx = 8;
  2514. bufByteIdx++;
  2515. buf[bufByteIdx] = 0;
  2516. }
  2517. k = Math.min(j, bufBitIdx);
  2518. j -= k;
  2519. bufBitIdx -= k;
  2520. /* 32 too large on 32 bit machines */
  2521. buf[bufByteIdx] |= ((val >> j) << bufBitIdx);
  2522. totbit += k;
  2523. }
  2524. }
  2525. /**
  2526. * Some combinations of bitrate, Fs, and stereo make it impossible to stuff
  2527. * out a frame using just main_data, due to the limited number of bits to
  2528. * indicate main_data_length. In these situations, we put stuffing bits into
  2529. * the ancillary data...
  2530. */
  2531. function drain_into_ancillary(gfp, remainingBits) {
  2532. var gfc = gfp.internal_flags;
  2533. var i;
  2534. if (remainingBits >= 8) {
  2535. putbits2(gfc, 0x4c, 8);
  2536. remainingBits -= 8;
  2537. }
  2538. if (remainingBits >= 8) {
  2539. putbits2(gfc, 0x41, 8);
  2540. remainingBits -= 8;
  2541. }
  2542. if (remainingBits >= 8) {
  2543. putbits2(gfc, 0x4d, 8);
  2544. remainingBits -= 8;
  2545. }
  2546. if (remainingBits >= 8) {
  2547. putbits2(gfc, 0x45, 8);
  2548. remainingBits -= 8;
  2549. }
  2550. if (remainingBits >= 32) {
  2551. var version = ver.getLameShortVersion();
  2552. if (remainingBits >= 32)
  2553. for (i = 0; i < version.length && remainingBits >= 8; ++i) {
  2554. remainingBits -= 8;
  2555. putbits2(gfc, version.charAt(i), 8);
  2556. }
  2557. }
  2558. for (; remainingBits >= 1; remainingBits -= 1) {
  2559. putbits2(gfc, gfc.ancillary_flag, 1);
  2560. gfc.ancillary_flag ^= (!gfp.disable_reservoir ? 1 : 0);
  2561. }
  2562. }
  2563. /**
  2564. * write N bits into the header
  2565. */
  2566. function writeheader(gfc, val, j) {
  2567. var ptr = gfc.header[gfc.h_ptr].ptr;
  2568. while (j > 0) {
  2569. var k = Math.min(j, 8 - (ptr & 7));
  2570. j -= k;
  2571. /* >> 32 too large for 32 bit machines */
  2572. gfc.header[gfc.h_ptr].buf[ptr >> 3] |= ((val >> j)) << (8 - (ptr & 7) - k);
  2573. ptr += k;
  2574. }
  2575. gfc.header[gfc.h_ptr].ptr = ptr;
  2576. }
  2577. function CRC_update(value, crc) {
  2578. value <<= 8;
  2579. for (var i = 0; i < 8; i++) {
  2580. value <<= 1;
  2581. crc <<= 1;
  2582. if ((((crc ^ value) & 0x10000) != 0))
  2583. crc ^= CRC16_POLYNOMIAL;
  2584. }
  2585. return crc;
  2586. }
  2587. this.CRC_writeheader = function (gfc, header) {
  2588. var crc = 0xffff;
  2589. /* (jo) init crc16 for error_protection */
  2590. crc = CRC_update(header[2] & 0xff, crc);
  2591. crc = CRC_update(header[3] & 0xff, crc);
  2592. for (var i = 6; i < gfc.sideinfo_len; i++) {
  2593. crc = CRC_update(header[i] & 0xff, crc);
  2594. }
  2595. header[4] = (byte)(crc >> 8);
  2596. header[5] = (byte)(crc & 255);
  2597. };
  2598. function encodeSideInfo2(gfp, bitsPerFrame) {
  2599. var gfc = gfp.internal_flags;
  2600. var l3_side;
  2601. var gr, ch;
  2602. l3_side = gfc.l3_side;
  2603. gfc.header[gfc.h_ptr].ptr = 0;
  2604. Arrays.fill(gfc.header[gfc.h_ptr].buf, 0, gfc.sideinfo_len, 0);
  2605. if (gfp.out_samplerate < 16000)
  2606. writeheader(gfc, 0xffe, 12);
  2607. else
  2608. writeheader(gfc, 0xfff, 12);
  2609. writeheader(gfc, (gfp.version), 1);
  2610. writeheader(gfc, 4 - 3, 2);
  2611. writeheader(gfc, (!gfp.error_protection ? 1 : 0), 1);
  2612. writeheader(gfc, (gfc.bitrate_index), 4);
  2613. writeheader(gfc, (gfc.samplerate_index), 2);
  2614. writeheader(gfc, (gfc.padding), 1);
  2615. writeheader(gfc, (gfp.extension), 1);
  2616. writeheader(gfc, (gfp.mode.ordinal()), 2);
  2617. writeheader(gfc, (gfc.mode_ext), 2);
  2618. writeheader(gfc, (gfp.copyright), 1);
  2619. writeheader(gfc, (gfp.original), 1);
  2620. writeheader(gfc, (gfp.emphasis), 2);
  2621. if (gfp.error_protection) {
  2622. writeheader(gfc, 0, 16);
  2623. /* dummy */
  2624. }
  2625. if (gfp.version == 1) {
  2626. /* MPEG1 */
  2627. writeheader(gfc, (l3_side.main_data_begin), 9);
  2628. if (gfc.channels_out == 2)
  2629. writeheader(gfc, l3_side.private_bits, 3);
  2630. else
  2631. writeheader(gfc, l3_side.private_bits, 5);
  2632. for (ch = 0; ch < gfc.channels_out; ch++) {
  2633. var band;
  2634. for (band = 0; band < 4; band++) {
  2635. writeheader(gfc, l3_side.scfsi[ch][band], 1);
  2636. }
  2637. }
  2638. for (gr = 0; gr < 2; gr++) {
  2639. for (ch = 0; ch < gfc.channels_out; ch++) {
  2640. var gi = l3_side.tt[gr][ch];
  2641. writeheader(gfc, gi.part2_3_length + gi.part2_length, 12);
  2642. writeheader(gfc, gi.big_values / 2, 9);
  2643. writeheader(gfc, gi.global_gain, 8);
  2644. writeheader(gfc, gi.scalefac_compress, 4);
  2645. if (gi.block_type != Encoder.NORM_TYPE) {
  2646. writeheader(gfc, 1, 1);
  2647. /* window_switching_flag */
  2648. writeheader(gfc, gi.block_type, 2);
  2649. writeheader(gfc, gi.mixed_block_flag, 1);
  2650. if (gi.table_select[0] == 14)
  2651. gi.table_select[0] = 16;
  2652. writeheader(gfc, gi.table_select[0], 5);
  2653. if (gi.table_select[1] == 14)
  2654. gi.table_select[1] = 16;
  2655. writeheader(gfc, gi.table_select[1], 5);
  2656. writeheader(gfc, gi.subblock_gain[0], 3);
  2657. writeheader(gfc, gi.subblock_gain[1], 3);
  2658. writeheader(gfc, gi.subblock_gain[2], 3);
  2659. } else {
  2660. writeheader(gfc, 0, 1);
  2661. /* window_switching_flag */
  2662. if (gi.table_select[0] == 14)
  2663. gi.table_select[0] = 16;
  2664. writeheader(gfc, gi.table_select[0], 5);
  2665. if (gi.table_select[1] == 14)
  2666. gi.table_select[1] = 16;
  2667. writeheader(gfc, gi.table_select[1], 5);
  2668. if (gi.table_select[2] == 14)
  2669. gi.table_select[2] = 16;
  2670. writeheader(gfc, gi.table_select[2], 5);
  2671. writeheader(gfc, gi.region0_count, 4);
  2672. writeheader(gfc, gi.region1_count, 3);
  2673. }
  2674. writeheader(gfc, gi.preflag, 1);
  2675. writeheader(gfc, gi.scalefac_scale, 1);
  2676. writeheader(gfc, gi.count1table_select, 1);
  2677. }
  2678. }
  2679. } else {
  2680. /* MPEG2 */
  2681. writeheader(gfc, (l3_side.main_data_begin), 8);
  2682. writeheader(gfc, l3_side.private_bits, gfc.channels_out);
  2683. gr = 0;
  2684. for (ch = 0; ch < gfc.channels_out; ch++) {
  2685. var gi = l3_side.tt[gr][ch];
  2686. writeheader(gfc, gi.part2_3_length + gi.part2_length, 12);
  2687. writeheader(gfc, gi.big_values / 2, 9);
  2688. writeheader(gfc, gi.global_gain, 8);
  2689. writeheader(gfc, gi.scalefac_compress, 9);
  2690. if (gi.block_type != Encoder.NORM_TYPE) {
  2691. writeheader(gfc, 1, 1);
  2692. /* window_switching_flag */
  2693. writeheader(gfc, gi.block_type, 2);
  2694. writeheader(gfc, gi.mixed_block_flag, 1);
  2695. if (gi.table_select[0] == 14)
  2696. gi.table_select[0] = 16;
  2697. writeheader(gfc, gi.table_select[0], 5);
  2698. if (gi.table_select[1] == 14)
  2699. gi.table_select[1] = 16;
  2700. writeheader(gfc, gi.table_select[1], 5);
  2701. writeheader(gfc, gi.subblock_gain[0], 3);
  2702. writeheader(gfc, gi.subblock_gain[1], 3);
  2703. writeheader(gfc, gi.subblock_gain[2], 3);
  2704. } else {
  2705. writeheader(gfc, 0, 1);
  2706. /* window_switching_flag */
  2707. if (gi.table_select[0] == 14)
  2708. gi.table_select[0] = 16;
  2709. writeheader(gfc, gi.table_select[0], 5);
  2710. if (gi.table_select[1] == 14)
  2711. gi.table_select[1] = 16;
  2712. writeheader(gfc, gi.table_select[1], 5);
  2713. if (gi.table_select[2] == 14)
  2714. gi.table_select[2] = 16;
  2715. writeheader(gfc, gi.table_select[2], 5);
  2716. writeheader(gfc, gi.region0_count, 4);
  2717. writeheader(gfc, gi.region1_count, 3);
  2718. }
  2719. writeheader(gfc, gi.scalefac_scale, 1);
  2720. writeheader(gfc, gi.count1table_select, 1);
  2721. }
  2722. }
  2723. if (gfp.error_protection) {
  2724. /* (jo) error_protection: add crc16 information to header */
  2725. CRC_writeheader(gfc, gfc.header[gfc.h_ptr].buf);
  2726. }
  2727. {
  2728. var old = gfc.h_ptr;
  2729. gfc.h_ptr = (old + 1) & (LameInternalFlags.MAX_HEADER_BUF - 1);
  2730. gfc.header[gfc.h_ptr].write_timing = gfc.header[old].write_timing
  2731. + bitsPerFrame;
  2732. if (gfc.h_ptr == gfc.w_ptr) {
  2733. /* yikes! we are out of header buffer space */
  2734. System.err
  2735. .println("Error: MAX_HEADER_BUF too small in bitstream.c \n");
  2736. }
  2737. }
  2738. }
  2739. function huffman_coder_count1(gfc, gi) {
  2740. /* Write count1 area */
  2741. var h = Tables.ht[gi.count1table_select + 32];
  2742. var i, bits = 0;
  2743. var ix = gi.big_values;
  2744. var xr = gi.big_values;
  2745. for (i = (gi.count1 - gi.big_values) / 4; i > 0; --i) {
  2746. var huffbits = 0;
  2747. var p = 0, v;
  2748. v = gi.l3_enc[ix + 0];
  2749. if (v != 0) {
  2750. p += 8;
  2751. if (gi.xr[xr + 0] < 0)
  2752. huffbits++;
  2753. }
  2754. v = gi.l3_enc[ix + 1];
  2755. if (v != 0) {
  2756. p += 4;
  2757. huffbits *= 2;
  2758. if (gi.xr[xr + 1] < 0)
  2759. huffbits++;
  2760. }
  2761. v = gi.l3_enc[ix + 2];
  2762. if (v != 0) {
  2763. p += 2;
  2764. huffbits *= 2;
  2765. if (gi.xr[xr + 2] < 0)
  2766. huffbits++;
  2767. }
  2768. v = gi.l3_enc[ix + 3];
  2769. if (v != 0) {
  2770. p++;
  2771. huffbits *= 2;
  2772. if (gi.xr[xr + 3] < 0)
  2773. huffbits++;
  2774. }
  2775. ix += 4;
  2776. xr += 4;
  2777. putbits2(gfc, huffbits + h.table[p], h.hlen[p]);
  2778. bits += h.hlen[p];
  2779. }
  2780. return bits;
  2781. }
  2782. /**
  2783. * Implements the pseudocode of page 98 of the IS
  2784. */
  2785. function Huffmancode(gfc, tableindex, start, end, gi) {
  2786. var h = Tables.ht[tableindex];
  2787. var bits = 0;
  2788. if (0 == tableindex)
  2789. return bits;
  2790. for (var i = start; i < end; i += 2) {
  2791. var cbits = 0;
  2792. var xbits = 0;
  2793. var linbits = h.xlen;
  2794. var xlen = h.xlen;
  2795. var ext = 0;
  2796. var x1 = gi.l3_enc[i];
  2797. var x2 = gi.l3_enc[i + 1];
  2798. if (x1 != 0) {
  2799. if (gi.xr[i] < 0)
  2800. ext++;
  2801. cbits--;
  2802. }
  2803. if (tableindex > 15) {
  2804. /* use ESC-words */
  2805. if (x1 > 14) {
  2806. var linbits_x1 = x1 - 15;
  2807. ext |= linbits_x1 << 1;
  2808. xbits = linbits;
  2809. x1 = 15;
  2810. }
  2811. if (x2 > 14) {
  2812. var linbits_x2 = x2 - 15;
  2813. ext <<= linbits;
  2814. ext |= linbits_x2;
  2815. xbits += linbits;
  2816. x2 = 15;
  2817. }
  2818. xlen = 16;
  2819. }
  2820. if (x2 != 0) {
  2821. ext <<= 1;
  2822. if (gi.xr[i + 1] < 0)
  2823. ext++;
  2824. cbits--;
  2825. }
  2826. x1 = x1 * xlen + x2;
  2827. xbits -= cbits;
  2828. cbits += h.hlen[x1];
  2829. putbits2(gfc, h.table[x1], cbits);
  2830. putbits2(gfc, ext, xbits);
  2831. bits += cbits + xbits;
  2832. }
  2833. return bits;
  2834. }
  2835. /**
  2836. * Note the discussion of huffmancodebits() on pages 28 and 29 of the IS, as
  2837. * well as the definitions of the side information on pages 26 and 27.
  2838. */
  2839. function ShortHuffmancodebits(gfc, gi) {
  2840. var region1Start = 3 * gfc.scalefac_band.s[3];
  2841. if (region1Start > gi.big_values)
  2842. region1Start = gi.big_values;
  2843. /* short blocks do not have a region2 */
  2844. var bits = Huffmancode(gfc, gi.table_select[0], 0, region1Start, gi);
  2845. bits += Huffmancode(gfc, gi.table_select[1], region1Start,
  2846. gi.big_values, gi);
  2847. return bits;
  2848. }
  2849. function LongHuffmancodebits(gfc, gi) {
  2850. var bigvalues, bits;
  2851. var region1Start, region2Start;
  2852. bigvalues = gi.big_values;
  2853. var i = gi.region0_count + 1;
  2854. region1Start = gfc.scalefac_band.l[i];
  2855. i += gi.region1_count + 1;
  2856. region2Start = gfc.scalefac_band.l[i];
  2857. if (region1Start > bigvalues)
  2858. region1Start = bigvalues;
  2859. if (region2Start > bigvalues)
  2860. region2Start = bigvalues;
  2861. bits = Huffmancode(gfc, gi.table_select[0], 0, region1Start, gi);
  2862. bits += Huffmancode(gfc, gi.table_select[1], region1Start,
  2863. region2Start, gi);
  2864. bits += Huffmancode(gfc, gi.table_select[2], region2Start, bigvalues,
  2865. gi);
  2866. return bits;
  2867. }
  2868. function writeMainData(gfp) {
  2869. var gr, ch, sfb, data_bits, tot_bits = 0;
  2870. var gfc = gfp.internal_flags;
  2871. var l3_side = gfc.l3_side;
  2872. if (gfp.version == 1) {
  2873. /* MPEG 1 */
  2874. for (gr = 0; gr < 2; gr++) {
  2875. for (ch = 0; ch < gfc.channels_out; ch++) {
  2876. var gi = l3_side.tt[gr][ch];
  2877. var slen1 = Takehiro.slen1_tab[gi.scalefac_compress];
  2878. var slen2 = Takehiro.slen2_tab[gi.scalefac_compress];
  2879. data_bits = 0;
  2880. for (sfb = 0; sfb < gi.sfbdivide; sfb++) {
  2881. if (gi.scalefac[sfb] == -1)
  2882. continue;
  2883. /* scfsi is used */
  2884. putbits2(gfc, gi.scalefac[sfb], slen1);
  2885. data_bits += slen1;
  2886. }
  2887. for (; sfb < gi.sfbmax; sfb++) {
  2888. if (gi.scalefac[sfb] == -1)
  2889. continue;
  2890. /* scfsi is used */
  2891. putbits2(gfc, gi.scalefac[sfb], slen2);
  2892. data_bits += slen2;
  2893. }
  2894. if (gi.block_type == Encoder.SHORT_TYPE) {
  2895. data_bits += ShortHuffmancodebits(gfc, gi);
  2896. } else {
  2897. data_bits += LongHuffmancodebits(gfc, gi);
  2898. }
  2899. data_bits += huffman_coder_count1(gfc, gi);
  2900. /* does bitcount in quantize.c agree with actual bit count? */
  2901. tot_bits += data_bits;
  2902. }
  2903. /* for ch */
  2904. }
  2905. /* for gr */
  2906. } else {
  2907. /* MPEG 2 */
  2908. gr = 0;
  2909. for (ch = 0; ch < gfc.channels_out; ch++) {
  2910. var gi = l3_side.tt[gr][ch];
  2911. var i, sfb_partition, scale_bits = 0;
  2912. data_bits = 0;
  2913. sfb = 0;
  2914. sfb_partition = 0;
  2915. if (gi.block_type == Encoder.SHORT_TYPE) {
  2916. for (; sfb_partition < 4; sfb_partition++) {
  2917. var sfbs = gi.sfb_partition_table[sfb_partition] / 3;
  2918. var slen = gi.slen[sfb_partition];
  2919. for (i = 0; i < sfbs; i++, sfb++) {
  2920. putbits2(gfc,
  2921. Math.max(gi.scalefac[sfb * 3 + 0], 0), slen);
  2922. putbits2(gfc,
  2923. Math.max(gi.scalefac[sfb * 3 + 1], 0), slen);
  2924. putbits2(gfc,
  2925. Math.max(gi.scalefac[sfb * 3 + 2], 0), slen);
  2926. scale_bits += 3 * slen;
  2927. }
  2928. }
  2929. data_bits += ShortHuffmancodebits(gfc, gi);
  2930. } else {
  2931. for (; sfb_partition < 4; sfb_partition++) {
  2932. var sfbs = gi.sfb_partition_table[sfb_partition];
  2933. var slen = gi.slen[sfb_partition];
  2934. for (i = 0; i < sfbs; i++, sfb++) {
  2935. putbits2(gfc, Math.max(gi.scalefac[sfb], 0), slen);
  2936. scale_bits += slen;
  2937. }
  2938. }
  2939. data_bits += LongHuffmancodebits(gfc, gi);
  2940. }
  2941. data_bits += huffman_coder_count1(gfc, gi);
  2942. /* does bitcount in quantize.c agree with actual bit count? */
  2943. tot_bits += scale_bits + data_bits;
  2944. }
  2945. /* for ch */
  2946. }
  2947. /* for gf */
  2948. return tot_bits;
  2949. }
  2950. /* main_data */
  2951. function TotalBytes() {
  2952. this.total = 0;
  2953. }
  2954. /*
  2955. * compute the number of bits required to flush all mp3 frames currently in
  2956. * the buffer. This should be the same as the reservoir size. Only call this
  2957. * routine between frames - i.e. only after all headers and data have been
  2958. * added to the buffer by format_bitstream().
  2959. *
  2960. * Also compute total_bits_output = size of mp3 buffer (including frame
  2961. * headers which may not have yet been send to the mp3 buffer) + number of
  2962. * bits needed to flush all mp3 frames.
  2963. *
  2964. * total_bytes_output is the size of the mp3 output buffer if
  2965. * lame_encode_flush_nogap() was called right now.
  2966. */
  2967. function compute_flushbits(gfp, total_bytes_output) {
  2968. var gfc = gfp.internal_flags;
  2969. var flushbits, remaining_headers;
  2970. var bitsPerFrame;
  2971. var last_ptr, first_ptr;
  2972. first_ptr = gfc.w_ptr;
  2973. /* first header to add to bitstream */
  2974. last_ptr = gfc.h_ptr - 1;
  2975. /* last header to add to bitstream */
  2976. if (last_ptr == -1)
  2977. last_ptr = LameInternalFlags.MAX_HEADER_BUF - 1;
  2978. /* add this many bits to bitstream so we can flush all headers */
  2979. flushbits = gfc.header[last_ptr].write_timing - totbit;
  2980. total_bytes_output.total = flushbits;
  2981. if (flushbits >= 0) {
  2982. /* if flushbits >= 0, some headers have not yet been written */
  2983. /* reduce flushbits by the size of the headers */
  2984. remaining_headers = 1 + last_ptr - first_ptr;
  2985. if (last_ptr < first_ptr)
  2986. remaining_headers = 1 + last_ptr - first_ptr
  2987. + LameInternalFlags.MAX_HEADER_BUF;
  2988. flushbits -= remaining_headers * 8 * gfc.sideinfo_len;
  2989. }
  2990. /*
  2991. * finally, add some bits so that the last frame is complete these bits
  2992. * are not necessary to decode the last frame, but some decoders will
  2993. * ignore last frame if these bits are missing
  2994. */
  2995. bitsPerFrame = self.getframebits(gfp);
  2996. flushbits += bitsPerFrame;
  2997. total_bytes_output.total += bitsPerFrame;
  2998. /* round up: */
  2999. if ((total_bytes_output.total % 8) != 0)
  3000. total_bytes_output.total = 1 + (total_bytes_output.total / 8);
  3001. else
  3002. total_bytes_output.total = (total_bytes_output.total / 8);
  3003. total_bytes_output.total += bufByteIdx + 1;
  3004. if (flushbits < 0) {
  3005. System.err.println("strange error flushing buffer ... \n");
  3006. }
  3007. return flushbits;
  3008. }
  3009. this.flush_bitstream = function (gfp) {
  3010. var gfc = gfp.internal_flags;
  3011. var l3_side;
  3012. var flushbits;
  3013. var last_ptr = gfc.h_ptr - 1;
  3014. /* last header to add to bitstream */
  3015. if (last_ptr == -1)
  3016. last_ptr = LameInternalFlags.MAX_HEADER_BUF - 1;
  3017. l3_side = gfc.l3_side;
  3018. if ((flushbits = compute_flushbits(gfp, new TotalBytes())) < 0)
  3019. return;
  3020. drain_into_ancillary(gfp, flushbits);
  3021. /* check that the 100% of the last frame has been written to bitstream */
  3022. /*
  3023. * we have padded out all frames with ancillary data, which is the same
  3024. * as filling the bitreservoir with ancillary data, so :
  3025. */
  3026. gfc.ResvSize = 0;
  3027. l3_side.main_data_begin = 0;
  3028. /* save the ReplayGain value */
  3029. if (gfc.findReplayGain) {
  3030. var RadioGain = ga.GetTitleGain(gfc.rgdata);
  3031. gfc.RadioGain = Math.floor(RadioGain * 10.0 + 0.5) | 0;
  3032. /* round to nearest */
  3033. }
  3034. /* find the gain and scale change required for no clipping */
  3035. if (gfc.findPeakSample) {
  3036. gfc.noclipGainChange = Math.ceil(Math
  3037. .log10(gfc.PeakSample / 32767.0) * 20.0 * 10.0) | 0;
  3038. /* round up */
  3039. if (gfc.noclipGainChange > 0) {
  3040. /* clipping occurs */
  3041. if (EQ(gfp.scale, 1.0) || EQ(gfp.scale, 0.0))
  3042. gfc.noclipScale = (Math
  3043. .floor((32767.0 / gfc.PeakSample) * 100.0) / 100.0);
  3044. /* round down */
  3045. else {
  3046. /*
  3047. * the user specified his own scaling factor. We could
  3048. * suggest the scaling factor of
  3049. * (32767.0/gfp.PeakSample)*(gfp.scale) but it's usually
  3050. * very inaccurate. So we'd rather not advice him on the
  3051. * scaling factor.
  3052. */
  3053. gfc.noclipScale = -1;
  3054. }
  3055. } else
  3056. /* no clipping */
  3057. gfc.noclipScale = -1;
  3058. }
  3059. };
  3060. this.add_dummy_byte = function (gfp, val, n) {
  3061. var gfc = gfp.internal_flags;
  3062. var i;
  3063. while (n-- > 0) {
  3064. putbits_noheaders(gfc, val, 8);
  3065. for (i = 0; i < LameInternalFlags.MAX_HEADER_BUF; ++i)
  3066. gfc.header[i].write_timing += 8;
  3067. }
  3068. };
  3069. /**
  3070. * This is called after a frame of audio has been quantized and coded. It
  3071. * will write the encoded audio to the bitstream. Note that from a layer3
  3072. * encoder's perspective the bit stream is primarily a series of main_data()
  3073. * blocks, with header and side information inserted at the proper locations
  3074. * to maintain framing. (See Figure A.7 in the IS).
  3075. */
  3076. this.format_bitstream = function (gfp) {
  3077. var gfc = gfp.internal_flags;
  3078. var l3_side;
  3079. l3_side = gfc.l3_side;
  3080. var bitsPerFrame = this.getframebits(gfp);
  3081. drain_into_ancillary(gfp, l3_side.resvDrain_pre);
  3082. encodeSideInfo2(gfp, bitsPerFrame);
  3083. var bits = 8 * gfc.sideinfo_len;
  3084. bits += writeMainData(gfp);
  3085. drain_into_ancillary(gfp, l3_side.resvDrain_post);
  3086. bits += l3_side.resvDrain_post;
  3087. l3_side.main_data_begin += (bitsPerFrame - bits) / 8;
  3088. /*
  3089. * compare number of bits needed to clear all buffered mp3 frames with
  3090. * what we think the resvsize is:
  3091. */
  3092. if (compute_flushbits(gfp, new TotalBytes()) != gfc.ResvSize) {
  3093. System.err.println("Internal buffer inconsistency. flushbits <> ResvSize");
  3094. }
  3095. /*
  3096. * compare main_data_begin for the next frame with what we think the
  3097. * resvsize is:
  3098. */
  3099. if ((l3_side.main_data_begin * 8) != gfc.ResvSize) {
  3100. System.err.printf("bit reservoir error: \n"
  3101. + "l3_side.main_data_begin: %d \n"
  3102. + "Resvoir size: %d \n"
  3103. + "resv drain (post) %d \n"
  3104. + "resv drain (pre) %d \n"
  3105. + "header and sideinfo: %d \n"
  3106. + "data bits: %d \n"
  3107. + "total bits: %d (remainder: %d) \n"
  3108. + "bitsperframe: %d \n",
  3109. 8 * l3_side.main_data_begin, gfc.ResvSize,
  3110. l3_side.resvDrain_post, l3_side.resvDrain_pre,
  3111. 8 * gfc.sideinfo_len, bits - l3_side.resvDrain_post - 8
  3112. * gfc.sideinfo_len, bits, bits % 8, bitsPerFrame);
  3113. System.err.println("This is a fatal error. It has several possible causes:");
  3114. System.err.println("90%% LAME compiled with buggy version of gcc using advanced optimizations");
  3115. System.err.println(" 9%% Your system is overclocked");
  3116. System.err.println(" 1%% bug in LAME encoding library");
  3117. gfc.ResvSize = l3_side.main_data_begin * 8;
  3118. }
  3119. //;
  3120. if (totbit > 1000000000) {
  3121. /*
  3122. * to avoid totbit overflow, (at 8h encoding at 128kbs) lets reset
  3123. * bit counter
  3124. */
  3125. var i;
  3126. for (i = 0; i < LameInternalFlags.MAX_HEADER_BUF; ++i)
  3127. gfc.header[i].write_timing -= totbit;
  3128. totbit = 0;
  3129. }
  3130. return 0;
  3131. };
  3132. /**
  3133. * <PRE>
  3134. * copy data out of the internal MP3 bit buffer into a user supplied
  3135. * unsigned char buffer.
  3136. *
  3137. * mp3data=0 indicates data in buffer is an id3tags and VBR tags
  3138. * mp3data=1 data is real mp3 frame data.
  3139. * </PRE>
  3140. */
  3141. this.copy_buffer = function (gfc, buffer, bufferPos, size, mp3data) {
  3142. var minimum = bufByteIdx + 1;
  3143. if (minimum <= 0)
  3144. return 0;
  3145. if (size != 0 && minimum > size) {
  3146. /* buffer is too small */
  3147. return -1;
  3148. }
  3149. System.arraycopy(buf, 0, buffer, bufferPos, minimum);
  3150. bufByteIdx = -1;
  3151. bufBitIdx = 0;
  3152. if (mp3data != 0) {
  3153. var crc = new_int(1);
  3154. crc[0] = gfc.nMusicCRC;
  3155. vbr.updateMusicCRC(crc, buffer, bufferPos, minimum);
  3156. gfc.nMusicCRC = crc[0];
  3157. /**
  3158. * sum number of bytes belonging to the mp3 stream this info will be
  3159. * written into the Xing/LAME header for seeking
  3160. */
  3161. if (minimum > 0) {
  3162. gfc.VBR_seek_table.nBytesWritten += minimum;
  3163. }
  3164. if (gfc.decode_on_the_fly) { /* decode the frame */
  3165. var pcm_buf = new_float_n([2, 1152]);
  3166. var mp3_in = minimum;
  3167. var samples_out = -1;
  3168. var i;
  3169. /* re-synthesis to pcm. Repeat until we get a samples_out=0 */
  3170. while (samples_out != 0) {
  3171. samples_out = mpg.hip_decode1_unclipped(gfc.hip, buffer,
  3172. bufferPos, mp3_in, pcm_buf[0], pcm_buf[1]);
  3173. /*
  3174. * samples_out = 0: need more data to decode samples_out =
  3175. * -1: error. Lets assume 0 pcm output samples_out = number
  3176. * of samples output
  3177. */
  3178. /*
  3179. * set the lenght of the mp3 input buffer to zero, so that
  3180. * in the next iteration of the loop we will be querying
  3181. * mpglib about buffered data
  3182. */
  3183. mp3_in = 0;
  3184. if (samples_out == -1) {
  3185. /*
  3186. * error decoding. Not fatal, but might screw up the
  3187. * ReplayGain tag. What should we do? Ignore for now
  3188. */
  3189. samples_out = 0;
  3190. }
  3191. if (samples_out > 0) {
  3192. /* process the PCM data */
  3193. /*
  3194. * this should not be possible, and indicates we have
  3195. * overflown the pcm_buf buffer
  3196. */
  3197. if (gfc.findPeakSample) {
  3198. for (i = 0; i < samples_out; i++) {
  3199. if (pcm_buf[0][i] > gfc.PeakSample)
  3200. gfc.PeakSample = pcm_buf[0][i];
  3201. else if (-pcm_buf[0][i] > gfc.PeakSample)
  3202. gfc.PeakSample = -pcm_buf[0][i];
  3203. }
  3204. if (gfc.channels_out > 1)
  3205. for (i = 0; i < samples_out; i++) {
  3206. if (pcm_buf[1][i] > gfc.PeakSample)
  3207. gfc.PeakSample = pcm_buf[1][i];
  3208. else if (-pcm_buf[1][i] > gfc.PeakSample)
  3209. gfc.PeakSample = -pcm_buf[1][i];
  3210. }
  3211. }
  3212. if (gfc.findReplayGain)
  3213. if (ga.AnalyzeSamples(gfc.rgdata, pcm_buf[0], 0,
  3214. pcm_buf[1], 0, samples_out,
  3215. gfc.channels_out) == GainAnalysis.GAIN_ANALYSIS_ERROR)
  3216. return -6;
  3217. }
  3218. /* if (samples_out>0) */
  3219. }
  3220. /* while (samples_out!=0) */
  3221. }
  3222. /* if (gfc.decode_on_the_fly) */
  3223. }
  3224. /* if (mp3data) */
  3225. return minimum;
  3226. };
  3227. this.init_bit_stream_w = function (gfc) {
  3228. buf = new_byte(Lame.LAME_MAXMP3BUFFER);
  3229. gfc.h_ptr = gfc.w_ptr = 0;
  3230. gfc.header[gfc.h_ptr].write_timing = 0;
  3231. bufByteIdx = -1;
  3232. bufBitIdx = 0;
  3233. totbit = 0;
  3234. };
  3235. // From machine.h
  3236. }
  3237. /**
  3238. * A Vbr header may be present in the ancillary data field of the first frame of
  3239. * an mp3 bitstream<BR>
  3240. * The Vbr header (optionally) contains
  3241. * <UL>
  3242. * <LI>frames total number of audio frames in the bitstream
  3243. * <LI>bytes total number of bytes in the bitstream
  3244. * <LI>toc table of contents
  3245. * </UL>
  3246. *
  3247. * toc (table of contents) gives seek points for random access.<BR>
  3248. * The ith entry determines the seek point for i-percent duration.<BR>
  3249. * seek point in bytes = (toc[i]/256.0) * total_bitstream_bytes<BR>
  3250. * e.g. half duration seek point = (toc[50]/256.0) * total_bitstream_bytes
  3251. */
  3252. VBRTag.NUMTOCENTRIES = 100;
  3253. VBRTag.MAXFRAMESIZE = 2880;
  3254. function VBRTag() {
  3255. var lame;
  3256. var bs;
  3257. var v;
  3258. this.setModules = function (_lame, _bs, _v) {
  3259. lame = _lame;
  3260. bs = _bs;
  3261. v = _v;
  3262. };
  3263. var FRAMES_FLAG = 0x0001;
  3264. var BYTES_FLAG = 0x0002;
  3265. var TOC_FLAG = 0x0004;
  3266. var VBR_SCALE_FLAG = 0x0008;
  3267. var NUMTOCENTRIES = VBRTag.NUMTOCENTRIES;
  3268. /**
  3269. * (0xB40) the max freeformat 640 32kHz framesize.
  3270. */
  3271. var MAXFRAMESIZE = VBRTag.MAXFRAMESIZE;
  3272. /**
  3273. * <PRE>
  3274. * 4 bytes for Header Tag
  3275. * 4 bytes for Header Flags
  3276. * 100 bytes for entry (toc)
  3277. * 4 bytes for frame size
  3278. * 4 bytes for stream size
  3279. * 4 bytes for VBR scale. a VBR quality indicator: 0=best 100=worst
  3280. * 20 bytes for LAME tag. for example, "LAME3.12 (beta 6)"
  3281. * ___________
  3282. * 140 bytes
  3283. * </PRE>
  3284. */
  3285. var VBRHEADERSIZE = (NUMTOCENTRIES + 4 + 4 + 4 + 4 + 4);
  3286. var LAMEHEADERSIZE = (VBRHEADERSIZE + 9 + 1 + 1 + 8
  3287. + 1 + 1 + 3 + 1 + 1 + 2 + 4 + 2 + 2);
  3288. /**
  3289. * The size of the Xing header MPEG-1, bit rate in kbps.
  3290. */
  3291. var XING_BITRATE1 = 128;
  3292. /**
  3293. * The size of the Xing header MPEG-2, bit rate in kbps.
  3294. */
  3295. var XING_BITRATE2 = 64;
  3296. /**
  3297. * The size of the Xing header MPEG-2.5, bit rate in kbps.
  3298. */
  3299. var XING_BITRATE25 = 32;
  3300. /**
  3301. * ISO-8859-1 charset for byte to string operations.
  3302. */
  3303. var ISO_8859_1 = null; //Charset.forName("ISO-8859-1");
  3304. /**
  3305. * VBR header magic string.
  3306. */
  3307. var VBRTag0 = "Xing";
  3308. /**
  3309. * VBR header magic string (VBR == VBRMode.vbr_off).
  3310. */
  3311. var VBRTag1 = "Info";
  3312. /**
  3313. * Lookup table for fast CRC-16 computation. Uses the polynomial
  3314. * x^16+x^15+x^2+1
  3315. */
  3316. var crc16Lookup = [0x0000, 0xC0C1, 0xC181, 0x0140,
  3317. 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741,
  3318. 0x0500, 0xC5C1, 0xC481, 0x0440, 0xCC01, 0x0CC0, 0x0D80, 0xCD41,
  3319. 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, 0x0B40,
  3320. 0xC901, 0x09C0, 0x0880, 0xC841, 0xD801, 0x18C0, 0x1980, 0xD941,
  3321. 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40,
  3322. 0xDD01, 0x1DC0, 0x1C80, 0xDC41, 0x1400, 0xD4C1, 0xD581, 0x1540,
  3323. 0xD701, 0x17C0, 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341,
  3324. 0x1100, 0xD1C1, 0xD081, 0x1040, 0xF001, 0x30C0, 0x3180, 0xF141,
  3325. 0x3300, 0xF3C1, 0xF281, 0x3240, 0x3600, 0xF6C1, 0xF781, 0x3740,
  3326. 0xF501, 0x35C0, 0x3480, 0xF441, 0x3C00, 0xFCC1, 0xFD81, 0x3D40,
  3327. 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, 0x3B80, 0xFB41,
  3328. 0x3900, 0xF9C1, 0xF881, 0x3840, 0x2800, 0xE8C1, 0xE981, 0x2940,
  3329. 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41,
  3330. 0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE401, 0x24C0, 0x2580, 0xE541,
  3331. 0x2700, 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340,
  3332. 0xE101, 0x21C0, 0x2080, 0xE041, 0xA001, 0x60C0, 0x6180, 0xA141,
  3333. 0x6300, 0xA3C1, 0xA281, 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740,
  3334. 0xA501, 0x65C0, 0x6480, 0xA441, 0x6C00, 0xACC1, 0xAD81, 0x6D40,
  3335. 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, 0x6AC0, 0x6B80, 0xAB41,
  3336. 0x6900, 0xA9C1, 0xA881, 0x6840, 0x7800, 0xB8C1, 0xB981, 0x7940,
  3337. 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, 0xBF41,
  3338. 0x7D00, 0xBDC1, 0xBC81, 0x7C40, 0xB401, 0x74C0, 0x7580, 0xB541,
  3339. 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340,
  3340. 0xB101, 0x71C0, 0x7080, 0xB041, 0x5000, 0x90C1, 0x9181, 0x5140,
  3341. 0x9301, 0x53C0, 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741,
  3342. 0x5500, 0x95C1, 0x9481, 0x5440, 0x9C01, 0x5CC0, 0x5D80, 0x9D41,
  3343. 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 0x5A00, 0x9AC1, 0x9B81, 0x5B40,
  3344. 0x9901, 0x59C0, 0x5880, 0x9841, 0x8801, 0x48C0, 0x4980, 0x8941,
  3345. 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, 0x8F81, 0x4F40,
  3346. 0x8D01, 0x4DC0, 0x4C80, 0x8C41, 0x4400, 0x84C1, 0x8581, 0x4540,
  3347. 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341,
  3348. 0x4100, 0x81C1, 0x8081, 0x4040];
  3349. /***********************************************************************
  3350. * Robert Hegemann 2001-01-17
  3351. ***********************************************************************/
  3352. function addVbr(v, bitrate) {
  3353. v.nVbrNumFrames++;
  3354. v.sum += bitrate;
  3355. v.seen++;
  3356. if (v.seen < v.want) {
  3357. return;
  3358. }
  3359. if (v.pos < v.size) {
  3360. v.bag[v.pos] = v.sum;
  3361. v.pos++;
  3362. v.seen = 0;
  3363. }
  3364. if (v.pos == v.size) {
  3365. for (var i = 1; i < v.size; i += 2) {
  3366. v.bag[i / 2] = v.bag[i];
  3367. }
  3368. v.want *= 2;
  3369. v.pos /= 2;
  3370. }
  3371. }
  3372. function xingSeekTable(v, t) {
  3373. if (v.pos <= 0)
  3374. return;
  3375. for (var i = 1; i < NUMTOCENTRIES; ++i) {
  3376. var j = i / NUMTOCENTRIES, act, sum;
  3377. var indx = 0 | (Math.floor(j * v.pos));
  3378. if (indx > v.pos - 1)
  3379. indx = v.pos - 1;
  3380. act = v.bag[indx];
  3381. sum = v.sum;
  3382. var seek_point = 0 | (256. * act / sum);
  3383. if (seek_point > 255)
  3384. seek_point = 255;
  3385. t[i] = 0xff & seek_point;
  3386. }
  3387. }
  3388. /**
  3389. * Add VBR entry, used to fill the VBR TOC entries.
  3390. *
  3391. * @param gfp
  3392. * global flags
  3393. */
  3394. this.addVbrFrame = function (gfp) {
  3395. var gfc = gfp.internal_flags;
  3396. var kbps = Tables.bitrate_table[gfp.version][gfc.bitrate_index];
  3397. addVbr(gfc.VBR_seek_table, kbps);
  3398. }
  3399. /**
  3400. * Read big endian integer (4-bytes) from header.
  3401. *
  3402. * @param buf
  3403. * header containing the integer
  3404. * @param bufPos
  3405. * offset into the header
  3406. * @return extracted integer
  3407. */
  3408. function extractInteger(buf, bufPos) {
  3409. var x = buf[bufPos + 0] & 0xff;
  3410. x <<= 8;
  3411. x |= buf[bufPos + 1] & 0xff;
  3412. x <<= 8;
  3413. x |= buf[bufPos + 2] & 0xff;
  3414. x <<= 8;
  3415. x |= buf[bufPos + 3] & 0xff;
  3416. return x;
  3417. }
  3418. /**
  3419. * Write big endian integer (4-bytes) in the header.
  3420. *
  3421. * @param buf
  3422. * header to write the integer into
  3423. * @param bufPos
  3424. * offset into the header
  3425. * @param value
  3426. * integer value to write
  3427. */
  3428. function createInteger(buf, bufPos, value) {
  3429. buf[bufPos + 0] = 0xff & ((value >> 24) & 0xff);
  3430. buf[bufPos + 1] = 0xff & ((value >> 16) & 0xff);
  3431. buf[bufPos + 2] = 0xff & ((value >> 8) & 0xff);
  3432. buf[bufPos + 3] = 0xff & (value & 0xff);
  3433. }
  3434. /**
  3435. * Write big endian short (2-bytes) in the header.
  3436. *
  3437. * @param buf
  3438. * header to write the integer into
  3439. * @param bufPos
  3440. * offset into the header
  3441. * @param value
  3442. * integer value to write
  3443. */
  3444. function createShort(buf, bufPos, value) {
  3445. buf[bufPos + 0] = 0xff & ((value >> 8) & 0xff);
  3446. buf[bufPos + 1] = 0xff & (value & 0xff);
  3447. }
  3448. /**
  3449. * Check for magic strings (Xing/Info).
  3450. *
  3451. * @param buf
  3452. * header to check
  3453. * @param bufPos
  3454. * header offset to check
  3455. * @return magic string found
  3456. */
  3457. function isVbrTag(buf, bufPos) {
  3458. return new String(buf, bufPos, VBRTag0.length(), ISO_8859_1)
  3459. .equals(VBRTag0)
  3460. || new String(buf, bufPos, VBRTag1.length(), ISO_8859_1)
  3461. .equals(VBRTag1);
  3462. }
  3463. function shiftInBitsValue(x, n, v) {
  3464. return 0xff & ((x << n) | (v & ~(-1 << n)));
  3465. }
  3466. /**
  3467. * Construct the MP3 header using the settings of the global flags.
  3468. *
  3469. * <img src="1000px-Mp3filestructure.svg.png">
  3470. *
  3471. * @param gfp
  3472. * global flags
  3473. * @param buffer
  3474. * header
  3475. */
  3476. function setLameTagFrameHeader(gfp, buffer) {
  3477. var gfc = gfp.internal_flags;
  3478. // MP3 Sync Word
  3479. buffer[0] = shiftInBitsValue(buffer[0], 8, 0xff);
  3480. buffer[1] = shiftInBitsValue(buffer[1], 3, 7);
  3481. buffer[1] = shiftInBitsValue(buffer[1], 1,
  3482. (gfp.out_samplerate < 16000) ? 0 : 1);
  3483. // Version
  3484. buffer[1] = shiftInBitsValue(buffer[1], 1, gfp.version);
  3485. // 01 == Layer 3
  3486. buffer[1] = shiftInBitsValue(buffer[1], 2, 4 - 3);
  3487. // Error protection
  3488. buffer[1] = shiftInBitsValue(buffer[1], 1, (!gfp.error_protection) ? 1
  3489. : 0);
  3490. // Bit rate
  3491. buffer[2] = shiftInBitsValue(buffer[2], 4, gfc.bitrate_index);
  3492. // Frequency
  3493. buffer[2] = shiftInBitsValue(buffer[2], 2, gfc.samplerate_index);
  3494. // Pad. Bit
  3495. buffer[2] = shiftInBitsValue(buffer[2], 1, 0);
  3496. // Priv. Bit
  3497. buffer[2] = shiftInBitsValue(buffer[2], 1, gfp.extension);
  3498. // Mode
  3499. buffer[3] = shiftInBitsValue(buffer[3], 2, gfp.mode.ordinal());
  3500. // Mode extension (Used with Joint Stereo)
  3501. buffer[3] = shiftInBitsValue(buffer[3], 2, gfc.mode_ext);
  3502. // Copy
  3503. buffer[3] = shiftInBitsValue(buffer[3], 1, gfp.copyright);
  3504. // Original
  3505. buffer[3] = shiftInBitsValue(buffer[3], 1, gfp.original);
  3506. // Emphasis
  3507. buffer[3] = shiftInBitsValue(buffer[3], 2, gfp.emphasis);
  3508. /* the default VBR header. 48 kbps layer III, no padding, no crc */
  3509. /* but sampling freq, mode and copyright/copy protection taken */
  3510. /* from first valid frame */
  3511. buffer[0] = 0xff;
  3512. var abyte = 0xff & (buffer[1] & 0xf1);
  3513. var bitrate;
  3514. if (1 == gfp.version) {
  3515. bitrate = XING_BITRATE1;
  3516. } else {
  3517. if (gfp.out_samplerate < 16000)
  3518. bitrate = XING_BITRATE25;
  3519. else
  3520. bitrate = XING_BITRATE2;
  3521. }
  3522. if (gfp.VBR == VbrMode.vbr_off)
  3523. bitrate = gfp.brate;
  3524. var bbyte;
  3525. if (gfp.free_format)
  3526. bbyte = 0x00;
  3527. else
  3528. bbyte = 0xff & (16 * lame.BitrateIndex(bitrate, gfp.version,
  3529. gfp.out_samplerate));
  3530. /*
  3531. * Use as much of the info from the real frames in the Xing header:
  3532. * samplerate, channels, crc, etc...
  3533. */
  3534. if (gfp.version == 1) {
  3535. /* MPEG1 */
  3536. buffer[1] = 0xff & (abyte | 0x0a);
  3537. /* was 0x0b; */
  3538. abyte = 0xff & (buffer[2] & 0x0d);
  3539. /* AF keep also private bit */
  3540. buffer[2] = 0xff & (bbyte | abyte);
  3541. /* 64kbs MPEG1 frame */
  3542. } else {
  3543. /* MPEG2 */
  3544. buffer[1] = 0xff & (abyte | 0x02);
  3545. /* was 0x03; */
  3546. abyte = 0xff & (buffer[2] & 0x0d);
  3547. /* AF keep also private bit */
  3548. buffer[2] = 0xff & (bbyte | abyte);
  3549. /* 64kbs MPEG2 frame */
  3550. }
  3551. }
  3552. /**
  3553. * Get VBR tag information
  3554. *
  3555. * @param buf
  3556. * header to analyze
  3557. * @param bufPos
  3558. * offset into the header
  3559. * @return VBR tag data
  3560. */
  3561. this.getVbrTag = function (buf) {
  3562. var pTagData = new VBRTagData();
  3563. var bufPos = 0;
  3564. /* get Vbr header data */
  3565. pTagData.flags = 0;
  3566. /* get selected MPEG header data */
  3567. var hId = (buf[bufPos + 1] >> 3) & 1;
  3568. var hSrIndex = (buf[bufPos + 2] >> 2) & 3;
  3569. var hMode = (buf[bufPos + 3] >> 6) & 3;
  3570. var hBitrate = ((buf[bufPos + 2] >> 4) & 0xf);
  3571. hBitrate = Tables.bitrate_table[hId][hBitrate];
  3572. /* check for FFE syncword */
  3573. if ((buf[bufPos + 1] >> 4) == 0xE)
  3574. pTagData.samprate = Tables.samplerate_table[2][hSrIndex];
  3575. else
  3576. pTagData.samprate = Tables.samplerate_table[hId][hSrIndex];
  3577. /* determine offset of header */
  3578. if (hId != 0) {
  3579. /* mpeg1 */
  3580. if (hMode != 3)
  3581. bufPos += (32 + 4);
  3582. else
  3583. bufPos += (17 + 4);
  3584. } else {
  3585. /* mpeg2 */
  3586. if (hMode != 3)
  3587. bufPos += (17 + 4);
  3588. else
  3589. bufPos += (9 + 4);
  3590. }
  3591. if (!isVbrTag(buf, bufPos))
  3592. return null;
  3593. bufPos += 4;
  3594. pTagData.hId = hId;
  3595. /* get flags */
  3596. var head_flags = pTagData.flags = extractInteger(buf, bufPos);
  3597. bufPos += 4;
  3598. if ((head_flags & FRAMES_FLAG) != 0) {
  3599. pTagData.frames = extractInteger(buf, bufPos);
  3600. bufPos += 4;
  3601. }
  3602. if ((head_flags & BYTES_FLAG) != 0) {
  3603. pTagData.bytes = extractInteger(buf, bufPos);
  3604. bufPos += 4;
  3605. }
  3606. if ((head_flags & TOC_FLAG) != 0) {
  3607. if (pTagData.toc != null) {
  3608. for (var i = 0; i < NUMTOCENTRIES; i++)
  3609. pTagData.toc[i] = buf[bufPos + i];
  3610. }
  3611. bufPos += NUMTOCENTRIES;
  3612. }
  3613. pTagData.vbrScale = -1;
  3614. if ((head_flags & VBR_SCALE_FLAG) != 0) {
  3615. pTagData.vbrScale = extractInteger(buf, bufPos);
  3616. bufPos += 4;
  3617. }
  3618. pTagData.headersize = ((hId + 1) * 72000 * hBitrate)
  3619. / pTagData.samprate;
  3620. bufPos += 21;
  3621. var encDelay = buf[bufPos + 0] << 4;
  3622. encDelay += buf[bufPos + 1] >> 4;
  3623. var encPadding = (buf[bufPos + 1] & 0x0F) << 8;
  3624. encPadding += buf[bufPos + 2] & 0xff;
  3625. /* check for reasonable values (this may be an old Xing header, */
  3626. /* not a INFO tag) */
  3627. if (encDelay < 0 || encDelay > 3000)
  3628. encDelay = -1;
  3629. if (encPadding < 0 || encPadding > 3000)
  3630. encPadding = -1;
  3631. pTagData.encDelay = encDelay;
  3632. pTagData.encPadding = encPadding;
  3633. /* success */
  3634. return pTagData;
  3635. }
  3636. /**
  3637. * Initializes the header
  3638. *
  3639. * @param gfp
  3640. * global flags
  3641. */
  3642. this.InitVbrTag = function (gfp) {
  3643. var gfc = gfp.internal_flags;
  3644. /**
  3645. * <PRE>
  3646. * Xing VBR pretends to be a 48kbs layer III frame. (at 44.1kHz).
  3647. * (at 48kHz they use 56kbs since 48kbs frame not big enough for
  3648. * table of contents)
  3649. * let's always embed Xing header inside a 64kbs layer III frame.
  3650. * this gives us enough room for a LAME version string too.
  3651. * size determined by sampling frequency (MPEG1)
  3652. * 32kHz: 216 bytes@48kbs 288bytes@ 64kbs
  3653. * 44.1kHz: 156 bytes 208bytes@64kbs (+1 if padding = 1)
  3654. * 48kHz: 144 bytes 192
  3655. *
  3656. * MPEG 2 values are the same since the framesize and samplerate
  3657. * are each reduced by a factor of 2.
  3658. * </PRE>
  3659. */
  3660. var kbps_header;
  3661. if (1 == gfp.version) {
  3662. kbps_header = XING_BITRATE1;
  3663. } else {
  3664. if (gfp.out_samplerate < 16000)
  3665. kbps_header = XING_BITRATE25;
  3666. else
  3667. kbps_header = XING_BITRATE2;
  3668. }
  3669. if (gfp.VBR == VbrMode.vbr_off)
  3670. kbps_header = gfp.brate;
  3671. // make sure LAME Header fits into Frame
  3672. var totalFrameSize = ((gfp.version + 1) * 72000 * kbps_header)
  3673. / gfp.out_samplerate;
  3674. var headerSize = (gfc.sideinfo_len + LAMEHEADERSIZE);
  3675. gfc.VBR_seek_table.TotalFrameSize = totalFrameSize;
  3676. if (totalFrameSize < headerSize || totalFrameSize > MAXFRAMESIZE) {
  3677. /* disable tag, it wont fit */
  3678. gfp.bWriteVbrTag = false;
  3679. return;
  3680. }
  3681. gfc.VBR_seek_table.nVbrNumFrames = 0;
  3682. gfc.VBR_seek_table.nBytesWritten = 0;
  3683. gfc.VBR_seek_table.sum = 0;
  3684. gfc.VBR_seek_table.seen = 0;
  3685. gfc.VBR_seek_table.want = 1;
  3686. gfc.VBR_seek_table.pos = 0;
  3687. if (gfc.VBR_seek_table.bag == null) {
  3688. gfc.VBR_seek_table.bag = new int[400];
  3689. gfc.VBR_seek_table.size = 400;
  3690. }
  3691. // write dummy VBR tag of all 0's into bitstream
  3692. var buffer = new_byte(MAXFRAMESIZE);
  3693. setLameTagFrameHeader(gfp, buffer);
  3694. var n = gfc.VBR_seek_table.TotalFrameSize;
  3695. for (var i = 0; i < n; ++i) {
  3696. bs.add_dummy_byte(gfp, buffer[i] & 0xff, 1);
  3697. }
  3698. }
  3699. /**
  3700. * Fast CRC-16 computation (uses table crc16Lookup).
  3701. *
  3702. * @param value
  3703. * @param crc
  3704. * @return
  3705. */
  3706. function crcUpdateLookup(value, crc) {
  3707. var tmp = crc ^ value;
  3708. crc = (crc >> 8) ^ crc16Lookup[tmp & 0xff];
  3709. return crc;
  3710. }
  3711. this.updateMusicCRC = function (crc, buffer, bufferPos, size) {
  3712. for (var i = 0; i < size; ++i)
  3713. crc[0] = crcUpdateLookup(buffer[bufferPos + i], crc[0]);
  3714. }
  3715. /**
  3716. * Write LAME info: mini version + info on various switches used (Jonathan
  3717. * Dee 2001/08/31).
  3718. *
  3719. * @param gfp
  3720. * global flags
  3721. * @param musicLength
  3722. * music length
  3723. * @param streamBuffer
  3724. * pointer to output buffer
  3725. * @param streamBufferPos
  3726. * offset into the output buffer
  3727. * @param crc
  3728. * computation of CRC-16 of Lame Tag so far (starting at frame
  3729. * sync)
  3730. * @return number of bytes written to the stream
  3731. */
  3732. function putLameVBR(gfp, musicLength, streamBuffer, streamBufferPos, crc) {
  3733. var gfc = gfp.internal_flags;
  3734. var bytesWritten = 0;
  3735. /* encoder delay */
  3736. var encDelay = gfp.encoder_delay;
  3737. /* encoder padding */
  3738. var encPadding = gfp.encoder_padding;
  3739. /* recall: gfp.VBR_q is for example set by the switch -V */
  3740. /* gfp.quality by -q, -h, -f, etc */
  3741. var quality = (100 - 10 * gfp.VBR_q - gfp.quality);
  3742. var version = v.getLameVeryShortVersion();
  3743. var vbr;
  3744. var revision = 0x00;
  3745. var revMethod;
  3746. // numbering different in vbr_mode vs. Lame tag
  3747. var vbrTypeTranslator = [1, 5, 3, 2, 4, 0, 3];
  3748. var lowpass = 0 | (((gfp.lowpassfreq / 100.0) + .5) > 255 ? 255
  3749. : (gfp.lowpassfreq / 100.0) + .5);
  3750. var peakSignalAmplitude = 0;
  3751. var radioReplayGain = 0;
  3752. var audiophileReplayGain = 0;
  3753. var noiseShaping = gfp.internal_flags.noise_shaping;
  3754. var stereoMode = 0;
  3755. var nonOptimal = 0;
  3756. var sourceFreq = 0;
  3757. var misc = 0;
  3758. var musicCRC = 0;
  3759. // psy model type: Gpsycho or NsPsytune
  3760. var expNPsyTune = (gfp.exp_nspsytune & 1) != 0;
  3761. var safeJoint = (gfp.exp_nspsytune & 2) != 0;
  3762. var noGapMore = false;
  3763. var noGapPrevious = false;
  3764. var noGapCount = gfp.internal_flags.nogap_total;
  3765. var noGapCurr = gfp.internal_flags.nogap_current;
  3766. // 4 bits
  3767. var athType = gfp.ATHtype;
  3768. var flags = 0;
  3769. // vbr modes
  3770. var abrBitrate;
  3771. switch (gfp.VBR) {
  3772. case vbr_abr:
  3773. abrBitrate = gfp.VBR_mean_bitrate_kbps;
  3774. break;
  3775. case vbr_off:
  3776. abrBitrate = gfp.brate;
  3777. break;
  3778. default:
  3779. abrBitrate = gfp.VBR_min_bitrate_kbps;
  3780. }
  3781. // revision and vbr method
  3782. if (gfp.VBR.ordinal() < vbrTypeTranslator.length)
  3783. vbr = vbrTypeTranslator[gfp.VBR.ordinal()];
  3784. else
  3785. vbr = 0x00; // unknown
  3786. revMethod = 0x10 * revision + vbr;
  3787. // ReplayGain
  3788. if (gfc.findReplayGain) {
  3789. if (gfc.RadioGain > 0x1FE)
  3790. gfc.RadioGain = 0x1FE;
  3791. if (gfc.RadioGain < -0x1FE)
  3792. gfc.RadioGain = -0x1FE;
  3793. // set name code
  3794. radioReplayGain = 0x2000;
  3795. // set originator code to `determined automatically'
  3796. radioReplayGain |= 0xC00;
  3797. if (gfc.RadioGain >= 0) {
  3798. // set gain adjustment
  3799. radioReplayGain |= gfc.RadioGain;
  3800. } else {
  3801. // set the sign bit
  3802. radioReplayGain |= 0x200;
  3803. // set gain adjustment
  3804. radioReplayGain |= -gfc.RadioGain;
  3805. }
  3806. }
  3807. // peak sample
  3808. if (gfc.findPeakSample)
  3809. peakSignalAmplitude = Math
  3810. .abs(0 | ((( gfc.PeakSample) / 32767.0) * Math.pow(2, 23) + .5));
  3811. // nogap
  3812. if (noGapCount != -1) {
  3813. if (noGapCurr > 0)
  3814. noGapPrevious = true;
  3815. if (noGapCurr < noGapCount - 1)
  3816. noGapMore = true;
  3817. }
  3818. // flags
  3819. flags = athType + ((expNPsyTune ? 1 : 0) << 4)
  3820. + ((safeJoint ? 1 : 0) << 5) + ((noGapMore ? 1 : 0) << 6)
  3821. + ((noGapPrevious ? 1 : 0) << 7);
  3822. if (quality < 0)
  3823. quality = 0;
  3824. // stereo mode field (Intensity stereo is not implemented)
  3825. switch (gfp.mode) {
  3826. case MONO:
  3827. stereoMode = 0;
  3828. break;
  3829. case STEREO:
  3830. stereoMode = 1;
  3831. break;
  3832. case DUAL_CHANNEL:
  3833. stereoMode = 2;
  3834. break;
  3835. case JOINT_STEREO:
  3836. if (gfp.force_ms)
  3837. stereoMode = 4;
  3838. else
  3839. stereoMode = 3;
  3840. break;
  3841. case NOT_SET:
  3842. //$FALL-THROUGH$
  3843. default:
  3844. stereoMode = 7;
  3845. break;
  3846. }
  3847. if (gfp.in_samplerate <= 32000)
  3848. sourceFreq = 0x00;
  3849. else if (gfp.in_samplerate == 48000)
  3850. sourceFreq = 0x02;
  3851. else if (gfp.in_samplerate > 48000)
  3852. sourceFreq = 0x03;
  3853. else {
  3854. // default is 44100Hz
  3855. sourceFreq = 0x01;
  3856. }
  3857. // Check if the user overrided the default LAME behavior with some
  3858. // nasty options
  3859. if (gfp.short_blocks == ShortBlock.short_block_forced
  3860. || gfp.short_blocks == ShortBlock.short_block_dispensed
  3861. || ((gfp.lowpassfreq == -1) && (gfp.highpassfreq == -1)) || /* "-k" */
  3862. (gfp.scale_left < gfp.scale_right)
  3863. || (gfp.scale_left > gfp.scale_right)
  3864. || (gfp.disable_reservoir && gfp.brate < 320) || gfp.noATH
  3865. || gfp.ATHonly || (athType == 0) || gfp.in_samplerate <= 32000)
  3866. nonOptimal = 1;
  3867. misc = noiseShaping + (stereoMode << 2) + (nonOptimal << 5)
  3868. + (sourceFreq << 6);
  3869. musicCRC = gfc.nMusicCRC;
  3870. // Write all this information into the stream
  3871. createInteger(streamBuffer, streamBufferPos + bytesWritten, quality);
  3872. bytesWritten += 4;
  3873. for (var j = 0; j < 9; j++) {
  3874. streamBuffer[streamBufferPos + bytesWritten + j] = 0xff & version .charAt(j);
  3875. }
  3876. bytesWritten += 9;
  3877. streamBuffer[streamBufferPos + bytesWritten] = 0xff & revMethod;
  3878. bytesWritten++;
  3879. streamBuffer[streamBufferPos + bytesWritten] = 0xff & lowpass;
  3880. bytesWritten++;
  3881. createInteger(streamBuffer, streamBufferPos + bytesWritten,
  3882. peakSignalAmplitude);
  3883. bytesWritten += 4;
  3884. createShort(streamBuffer, streamBufferPos + bytesWritten,
  3885. radioReplayGain);
  3886. bytesWritten += 2;
  3887. createShort(streamBuffer, streamBufferPos + bytesWritten,
  3888. audiophileReplayGain);
  3889. bytesWritten += 2;
  3890. streamBuffer[streamBufferPos + bytesWritten] = 0xff & flags;
  3891. bytesWritten++;
  3892. if (abrBitrate >= 255)
  3893. streamBuffer[streamBufferPos + bytesWritten] = 0xFF;
  3894. else
  3895. streamBuffer[streamBufferPos + bytesWritten] = 0xff & abrBitrate;
  3896. bytesWritten++;
  3897. streamBuffer[streamBufferPos + bytesWritten] = 0xff & (encDelay >> 4);
  3898. streamBuffer[streamBufferPos + bytesWritten + 1] = 0xff & ((encDelay << 4) + (encPadding >> 8));
  3899. streamBuffer[streamBufferPos + bytesWritten + 2] = 0xff & encPadding;
  3900. bytesWritten += 3;
  3901. streamBuffer[streamBufferPos + bytesWritten] = 0xff & misc;
  3902. bytesWritten++;
  3903. // unused in rev0
  3904. streamBuffer[streamBufferPos + bytesWritten++] = 0;
  3905. createShort(streamBuffer, streamBufferPos + bytesWritten, gfp.preset);
  3906. bytesWritten += 2;
  3907. createInteger(streamBuffer, streamBufferPos + bytesWritten, musicLength);
  3908. bytesWritten += 4;
  3909. createShort(streamBuffer, streamBufferPos + bytesWritten, musicCRC);
  3910. bytesWritten += 2;
  3911. // Calculate tag CRC.... must be done here, since it includes previous
  3912. // information
  3913. for (var i = 0; i < bytesWritten; i++)
  3914. crc = crcUpdateLookup(streamBuffer[streamBufferPos + i], crc);
  3915. createShort(streamBuffer, streamBufferPos + bytesWritten, crc);
  3916. bytesWritten += 2;
  3917. return bytesWritten;
  3918. }
  3919. function skipId3v2(fpStream) {
  3920. // seek to the beginning of the stream
  3921. fpStream.seek(0);
  3922. // read 10 bytes in case there's an ID3 version 2 header here
  3923. var id3v2Header = new_byte(10);
  3924. fpStream.readFully(id3v2Header);
  3925. /* does the stream begin with the ID3 version 2 file identifier? */
  3926. var id3v2TagSize;
  3927. if (!new String(id3v2Header, "ISO-8859-1").startsWith("ID3")) {
  3928. /*
  3929. * the tag size (minus the 10-byte header) is encoded into four
  3930. * bytes where the most significant bit is clear in each byte
  3931. */
  3932. id3v2TagSize = (((id3v2Header[6] & 0x7f) << 21)
  3933. | ((id3v2Header[7] & 0x7f) << 14)
  3934. | ((id3v2Header[8] & 0x7f) << 7) | (id3v2Header[9] & 0x7f))
  3935. + id3v2Header.length;
  3936. } else {
  3937. /* no ID3 version 2 tag in this stream */
  3938. id3v2TagSize = 0;
  3939. }
  3940. return id3v2TagSize;
  3941. }
  3942. this.getLameTagFrame = function (gfp, buffer) {
  3943. var gfc = gfp.internal_flags;
  3944. if (!gfp.bWriteVbrTag) {
  3945. return 0;
  3946. }
  3947. if (gfc.Class_ID != Lame.LAME_ID) {
  3948. return 0;
  3949. }
  3950. if (gfc.VBR_seek_table.pos <= 0) {
  3951. return 0;
  3952. }
  3953. if (buffer.length < gfc.VBR_seek_table.TotalFrameSize) {
  3954. return gfc.VBR_seek_table.TotalFrameSize;
  3955. }
  3956. Arrays.fill(buffer, 0, gfc.VBR_seek_table.TotalFrameSize, 0);
  3957. // 4 bytes frame header
  3958. setLameTagFrameHeader(gfp, buffer);
  3959. // Create TOC entries
  3960. var toc = new_byte(NUMTOCENTRIES);
  3961. if (gfp.free_format) {
  3962. for (var i = 1; i < NUMTOCENTRIES; ++i)
  3963. toc[i] = 0xff & (255 * i / 100);
  3964. } else {
  3965. xingSeekTable(gfc.VBR_seek_table, toc);
  3966. }
  3967. // Start writing the tag after the zero frame
  3968. var streamIndex = gfc.sideinfo_len;
  3969. /**
  3970. * Note: Xing header specifies that Xing data goes in the ancillary data
  3971. * with NO ERROR PROTECTION. If error protecton in enabled, the Xing
  3972. * data still starts at the same offset, and now it is in sideinfo data
  3973. * block, and thus will not decode correctly by non-Xing tag aware
  3974. * players
  3975. */
  3976. if (gfp.error_protection)
  3977. streamIndex -= 2;
  3978. // Put Vbr tag
  3979. if (gfp.VBR == VbrMode.vbr_off) {
  3980. buffer[streamIndex++] = 0xff & VBRTag1.charAt(0);
  3981. buffer[streamIndex++] = 0xff & VBRTag1.charAt(1);
  3982. buffer[streamIndex++] = 0xff & VBRTag1.charAt(2);
  3983. buffer[streamIndex++] = 0xff & VBRTag1.charAt(3);
  3984. } else {
  3985. buffer[streamIndex++] = 0xff & VBRTag0.charAt(0);
  3986. buffer[streamIndex++] = 0xff & VBRTag0.charAt(1);
  3987. buffer[streamIndex++] = 0xff & VBRTag0.charAt(2);
  3988. buffer[streamIndex++] = 0xff & VBRTag0.charAt(3);
  3989. }
  3990. // Put header flags
  3991. createInteger(buffer, streamIndex, FRAMES_FLAG + BYTES_FLAG + TOC_FLAG
  3992. + VBR_SCALE_FLAG);
  3993. streamIndex += 4;
  3994. // Put Total Number of frames
  3995. createInteger(buffer, streamIndex, gfc.VBR_seek_table.nVbrNumFrames);
  3996. streamIndex += 4;
  3997. // Put total audio stream size, including Xing/LAME Header
  3998. var streamSize = (gfc.VBR_seek_table.nBytesWritten + gfc.VBR_seek_table.TotalFrameSize);
  3999. createInteger(buffer, streamIndex, 0 | streamSize);
  4000. streamIndex += 4;
  4001. /* Put TOC */
  4002. System.arraycopy(toc, 0, buffer, streamIndex, toc.length);
  4003. streamIndex += toc.length;
  4004. if (gfp.error_protection) {
  4005. // (jo) error_protection: add crc16 information to header
  4006. bs.CRC_writeheader(gfc, buffer);
  4007. }
  4008. // work out CRC so far: initially crc = 0
  4009. var crc = 0x00;
  4010. for (var i = 0; i < streamIndex; i++)
  4011. crc = crcUpdateLookup(buffer[i], crc);
  4012. // Put LAME VBR info
  4013. streamIndex += putLameVBR(gfp, streamSize, buffer, streamIndex, crc);
  4014. return gfc.VBR_seek_table.TotalFrameSize;
  4015. }
  4016. /**
  4017. * Write final VBR tag to the file.
  4018. *
  4019. * @param gfp
  4020. * global flags
  4021. * @param stream
  4022. * stream to add the VBR tag to
  4023. * @return 0 (OK), -1 else
  4024. * @throws IOException
  4025. * I/O error
  4026. */
  4027. this.putVbrTag = function (gfp, stream) {
  4028. var gfc = gfp.internal_flags;
  4029. if (gfc.VBR_seek_table.pos <= 0)
  4030. return -1;
  4031. // Seek to end of file
  4032. stream.seek(stream.length());
  4033. // Get file size, abort if file has zero length.
  4034. if (stream.length() == 0)
  4035. return -1;
  4036. // The VBR tag may NOT be located at the beginning of the stream. If an
  4037. // ID3 version 2 tag was added, then it must be skipped to write the VBR
  4038. // tag data.
  4039. var id3v2TagSize = skipId3v2(stream);
  4040. // Seek to the beginning of the stream
  4041. stream.seek(id3v2TagSize);
  4042. var buffer = new_byte(MAXFRAMESIZE);
  4043. var bytes = getLameTagFrame(gfp, buffer);
  4044. if (bytes > buffer.length) {
  4045. return -1;
  4046. }
  4047. if (bytes < 1) {
  4048. return 0;
  4049. }
  4050. // Put it all to disk again
  4051. stream.write(buffer, 0, bytes);
  4052. // success
  4053. return 0;
  4054. }
  4055. }
  4056. function HuffCodeTab(len, max, tab, hl) {
  4057. this.xlen = len;
  4058. this.linmax = max;
  4059. this.table = tab;
  4060. this.hlen = hl;
  4061. }
  4062. var Tables = {};
  4063. Tables.t1HB = [
  4064. 1, 1,
  4065. 1, 0
  4066. ];
  4067. Tables.t2HB = [
  4068. 1, 2, 1,
  4069. 3, 1, 1,
  4070. 3, 2, 0
  4071. ];
  4072. Tables.t3HB = [
  4073. 3, 2, 1,
  4074. 1, 1, 1,
  4075. 3, 2, 0
  4076. ];
  4077. Tables.t5HB = [
  4078. 1, 2, 6, 5,
  4079. 3, 1, 4, 4,
  4080. 7, 5, 7, 1,
  4081. 6, 1, 1, 0
  4082. ];
  4083. Tables.t6HB = [
  4084. 7, 3, 5, 1,
  4085. 6, 2, 3, 2,
  4086. 5, 4, 4, 1,
  4087. 3, 3, 2, 0
  4088. ];
  4089. Tables.t7HB = [
  4090. 1, 2, 10, 19, 16, 10,
  4091. 3, 3, 7, 10, 5, 3,
  4092. 11, 4, 13, 17, 8, 4,
  4093. 12, 11, 18, 15, 11, 2,
  4094. 7, 6, 9, 14, 3, 1,
  4095. 6, 4, 5, 3, 2, 0
  4096. ];
  4097. Tables.t8HB = [
  4098. 3, 4, 6, 18, 12, 5,
  4099. 5, 1, 2, 16, 9, 3,
  4100. 7, 3, 5, 14, 7, 3,
  4101. 19, 17, 15, 13, 10, 4,
  4102. 13, 5, 8, 11, 5, 1,
  4103. 12, 4, 4, 1, 1, 0
  4104. ];
  4105. Tables.t9HB = [
  4106. 7, 5, 9, 14, 15, 7,
  4107. 6, 4, 5, 5, 6, 7,
  4108. 7, 6, 8, 8, 8, 5,
  4109. 15, 6, 9, 10, 5, 1,
  4110. 11, 7, 9, 6, 4, 1,
  4111. 14, 4, 6, 2, 6, 0
  4112. ];
  4113. Tables.t10HB = [
  4114. 1, 2, 10, 23, 35, 30, 12, 17,
  4115. 3, 3, 8, 12, 18, 21, 12, 7,
  4116. 11, 9, 15, 21, 32, 40, 19, 6,
  4117. 14, 13, 22, 34, 46, 23, 18, 7,
  4118. 20, 19, 33, 47, 27, 22, 9, 3,
  4119. 31, 22, 41, 26, 21, 20, 5, 3,
  4120. 14, 13, 10, 11, 16, 6, 5, 1,
  4121. 9, 8, 7, 8, 4, 4, 2, 0
  4122. ];
  4123. Tables.t11HB = [
  4124. 3, 4, 10, 24, 34, 33, 21, 15,
  4125. 5, 3, 4, 10, 32, 17, 11, 10,
  4126. 11, 7, 13, 18, 30, 31, 20, 5,
  4127. 25, 11, 19, 59, 27, 18, 12, 5,
  4128. 35, 33, 31, 58, 30, 16, 7, 5,
  4129. 28, 26, 32, 19, 17, 15, 8, 14,
  4130. 14, 12, 9, 13, 14, 9, 4, 1,
  4131. 11, 4, 6, 6, 6, 3, 2, 0
  4132. ];
  4133. Tables.t12HB = [
  4134. 9, 6, 16, 33, 41, 39, 38, 26,
  4135. 7, 5, 6, 9, 23, 16, 26, 11,
  4136. 17, 7, 11, 14, 21, 30, 10, 7,
  4137. 17, 10, 15, 12, 18, 28, 14, 5,
  4138. 32, 13, 22, 19, 18, 16, 9, 5,
  4139. 40, 17, 31, 29, 17, 13, 4, 2,
  4140. 27, 12, 11, 15, 10, 7, 4, 1,
  4141. 27, 12, 8, 12, 6, 3, 1, 0
  4142. ];
  4143. Tables.t13HB = [
  4144. 1, 5, 14, 21, 34, 51, 46, 71, 42, 52, 68, 52, 67, 44, 43, 19,
  4145. 3, 4, 12, 19, 31, 26, 44, 33, 31, 24, 32, 24, 31, 35, 22, 14,
  4146. 15, 13, 23, 36, 59, 49, 77, 65, 29, 40, 30, 40, 27, 33, 42, 16,
  4147. 22, 20, 37, 61, 56, 79, 73, 64, 43, 76, 56, 37, 26, 31, 25, 14,
  4148. 35, 16, 60, 57, 97, 75, 114, 91, 54, 73, 55, 41, 48, 53, 23, 24,
  4149. 58, 27, 50, 96, 76, 70, 93, 84, 77, 58, 79, 29, 74, 49, 41, 17,
  4150. 47, 45, 78, 74, 115, 94, 90, 79, 69, 83, 71, 50, 59, 38, 36, 15,
  4151. 72, 34, 56, 95, 92, 85, 91, 90, 86, 73, 77, 65, 51, 44, 43, 42,
  4152. 43, 20, 30, 44, 55, 78, 72, 87, 78, 61, 46, 54, 37, 30, 20, 16,
  4153. 53, 25, 41, 37, 44, 59, 54, 81, 66, 76, 57, 54, 37, 18, 39, 11,
  4154. 35, 33, 31, 57, 42, 82, 72, 80, 47, 58, 55, 21, 22, 26, 38, 22,
  4155. 53, 25, 23, 38, 70, 60, 51, 36, 55, 26, 34, 23, 27, 14, 9, 7,
  4156. 34, 32, 28, 39, 49, 75, 30, 52, 48, 40, 52, 28, 18, 17, 9, 5,
  4157. 45, 21, 34, 64, 56, 50, 49, 45, 31, 19, 12, 15, 10, 7, 6, 3,
  4158. 48, 23, 20, 39, 36, 35, 53, 21, 16, 23, 13, 10, 6, 1, 4, 2,
  4159. 16, 15, 17, 27, 25, 20, 29, 11, 17, 12, 16, 8, 1, 1, 0, 1
  4160. ];
  4161. Tables.t15HB = [
  4162. 7, 12, 18, 53, 47, 76, 124, 108, 89, 123, 108, 119, 107, 81, 122, 63,
  4163. 13, 5, 16, 27, 46, 36, 61, 51, 42, 70, 52, 83, 65, 41, 59, 36,
  4164. 19, 17, 15, 24, 41, 34, 59, 48, 40, 64, 50, 78, 62, 80, 56, 33,
  4165. 29, 28, 25, 43, 39, 63, 55, 93, 76, 59, 93, 72, 54, 75, 50, 29,
  4166. 52, 22, 42, 40, 67, 57, 95, 79, 72, 57, 89, 69, 49, 66, 46, 27,
  4167. 77, 37, 35, 66, 58, 52, 91, 74, 62, 48, 79, 63, 90, 62, 40, 38,
  4168. 125, 32, 60, 56, 50, 92, 78, 65, 55, 87, 71, 51, 73, 51, 70, 30,
  4169. 109, 53, 49, 94, 88, 75, 66, 122, 91, 73, 56, 42, 64, 44, 21, 25,
  4170. 90, 43, 41, 77, 73, 63, 56, 92, 77, 66, 47, 67, 48, 53, 36, 20,
  4171. 71, 34, 67, 60, 58, 49, 88, 76, 67, 106, 71, 54, 38, 39, 23, 15,
  4172. 109, 53, 51, 47, 90, 82, 58, 57, 48, 72, 57, 41, 23, 27, 62, 9,
  4173. 86, 42, 40, 37, 70, 64, 52, 43, 70, 55, 42, 25, 29, 18, 11, 11,
  4174. 118, 68, 30, 55, 50, 46, 74, 65, 49, 39, 24, 16, 22, 13, 14, 7,
  4175. 91, 44, 39, 38, 34, 63, 52, 45, 31, 52, 28, 19, 14, 8, 9, 3,
  4176. 123, 60, 58, 53, 47, 43, 32, 22, 37, 24, 17, 12, 15, 10, 2, 1,
  4177. 71, 37, 34, 30, 28, 20, 17, 26, 21, 16, 10, 6, 8, 6, 2, 0
  4178. ];
  4179. Tables.t16HB = [
  4180. 1, 5, 14, 44, 74, 63, 110, 93, 172, 149, 138, 242, 225, 195, 376, 17,
  4181. 3, 4, 12, 20, 35, 62, 53, 47, 83, 75, 68, 119, 201, 107, 207, 9,
  4182. 15, 13, 23, 38, 67, 58, 103, 90, 161, 72, 127, 117, 110, 209, 206, 16,
  4183. 45, 21, 39, 69, 64, 114, 99, 87, 158, 140, 252, 212, 199, 387, 365, 26,
  4184. 75, 36, 68, 65, 115, 101, 179, 164, 155, 264, 246, 226, 395, 382, 362, 9,
  4185. 66, 30, 59, 56, 102, 185, 173, 265, 142, 253, 232, 400, 388, 378, 445, 16,
  4186. 111, 54, 52, 100, 184, 178, 160, 133, 257, 244, 228, 217, 385, 366, 715, 10,
  4187. 98, 48, 91, 88, 165, 157, 148, 261, 248, 407, 397, 372, 380, 889, 884, 8,
  4188. 85, 84, 81, 159, 156, 143, 260, 249, 427, 401, 392, 383, 727, 713, 708, 7,
  4189. 154, 76, 73, 141, 131, 256, 245, 426, 406, 394, 384, 735, 359, 710, 352, 11,
  4190. 139, 129, 67, 125, 247, 233, 229, 219, 393, 743, 737, 720, 885, 882, 439, 4,
  4191. 243, 120, 118, 115, 227, 223, 396, 746, 742, 736, 721, 712, 706, 223, 436, 6,
  4192. 202, 224, 222, 218, 216, 389, 386, 381, 364, 888, 443, 707, 440, 437, 1728, 4,
  4193. 747, 211, 210, 208, 370, 379, 734, 723, 714, 1735, 883, 877, 876, 3459, 865, 2,
  4194. 377, 369, 102, 187, 726, 722, 358, 711, 709, 866, 1734, 871, 3458, 870, 434, 0,
  4195. 12, 10, 7, 11, 10, 17, 11, 9, 13, 12, 10, 7, 5, 3, 1, 3
  4196. ];
  4197. Tables.t24HB = [
  4198. 15, 13, 46, 80, 146, 262, 248, 434, 426, 669, 653, 649, 621, 517, 1032, 88,
  4199. 14, 12, 21, 38, 71, 130, 122, 216, 209, 198, 327, 345, 319, 297, 279, 42,
  4200. 47, 22, 41, 74, 68, 128, 120, 221, 207, 194, 182, 340, 315, 295, 541, 18,
  4201. 81, 39, 75, 70, 134, 125, 116, 220, 204, 190, 178, 325, 311, 293, 271, 16,
  4202. 147, 72, 69, 135, 127, 118, 112, 210, 200, 188, 352, 323, 306, 285, 540, 14,
  4203. 263, 66, 129, 126, 119, 114, 214, 202, 192, 180, 341, 317, 301, 281, 262, 12,
  4204. 249, 123, 121, 117, 113, 215, 206, 195, 185, 347, 330, 308, 291, 272, 520, 10,
  4205. 435, 115, 111, 109, 211, 203, 196, 187, 353, 332, 313, 298, 283, 531, 381, 17,
  4206. 427, 212, 208, 205, 201, 193, 186, 177, 169, 320, 303, 286, 268, 514, 377, 16,
  4207. 335, 199, 197, 191, 189, 181, 174, 333, 321, 305, 289, 275, 521, 379, 371, 11,
  4208. 668, 184, 183, 179, 175, 344, 331, 314, 304, 290, 277, 530, 383, 373, 366, 10,
  4209. 652, 346, 171, 168, 164, 318, 309, 299, 287, 276, 263, 513, 375, 368, 362, 6,
  4210. 648, 322, 316, 312, 307, 302, 292, 284, 269, 261, 512, 376, 370, 364, 359, 4,
  4211. 620, 300, 296, 294, 288, 282, 273, 266, 515, 380, 374, 369, 365, 361, 357, 2,
  4212. 1033, 280, 278, 274, 267, 264, 259, 382, 378, 372, 367, 363, 360, 358, 356, 0,
  4213. 43, 20, 19, 17, 15, 13, 11, 9, 7, 6, 4, 7, 5, 3, 1, 3
  4214. ];
  4215. Tables.t32HB = [
  4216. 1 << 0, 5 << 1, 4 << 1, 5 << 2, 6 << 1, 5 << 2, 4 << 2, 4 << 3,
  4217. 7 << 1, 3 << 2, 6 << 2, 0 << 3, 7 << 2, 2 << 3, 3 << 3, 1 << 4
  4218. ];
  4219. Tables.t33HB = [
  4220. 15 << 0, 14 << 1, 13 << 1, 12 << 2, 11 << 1, 10 << 2, 9 << 2, 8 << 3,
  4221. 7 << 1, 6 << 2, 5 << 2, 4 << 3, 3 << 2, 2 << 3, 1 << 3, 0 << 4
  4222. ];
  4223. Tables.t1l = [
  4224. 1, 4,
  4225. 3, 5
  4226. ];
  4227. Tables.t2l = [
  4228. 1, 4, 7,
  4229. 4, 5, 7,
  4230. 6, 7, 8
  4231. ];
  4232. Tables.t3l = [
  4233. 2, 3, 7,
  4234. 4, 4, 7,
  4235. 6, 7, 8
  4236. ];
  4237. Tables.t5l = [
  4238. 1, 4, 7, 8,
  4239. 4, 5, 8, 9,
  4240. 7, 8, 9, 10,
  4241. 8, 8, 9, 10
  4242. ];
  4243. Tables.t6l = [
  4244. 3, 4, 6, 8,
  4245. 4, 4, 6, 7,
  4246. 5, 6, 7, 8,
  4247. 7, 7, 8, 9
  4248. ];
  4249. Tables.t7l = [
  4250. 1, 4, 7, 9, 9, 10,
  4251. 4, 6, 8, 9, 9, 10,
  4252. 7, 7, 9, 10, 10, 11,
  4253. 8, 9, 10, 11, 11, 11,
  4254. 8, 9, 10, 11, 11, 12,
  4255. 9, 10, 11, 12, 12, 12
  4256. ];
  4257. Tables.t8l = [
  4258. 2, 4, 7, 9, 9, 10,
  4259. 4, 4, 6, 10, 10, 10,
  4260. 7, 6, 8, 10, 10, 11,
  4261. 9, 10, 10, 11, 11, 12,
  4262. 9, 9, 10, 11, 12, 12,
  4263. 10, 10, 11, 11, 13, 13
  4264. ];
  4265. Tables.t9l = [
  4266. 3, 4, 6, 7, 9, 10,
  4267. 4, 5, 6, 7, 8, 10,
  4268. 5, 6, 7, 8, 9, 10,
  4269. 7, 7, 8, 9, 9, 10,
  4270. 8, 8, 9, 9, 10, 11,
  4271. 9, 9, 10, 10, 11, 11
  4272. ];
  4273. Tables.t10l = [
  4274. 1, 4, 7, 9, 10, 10, 10, 11,
  4275. 4, 6, 8, 9, 10, 11, 10, 10,
  4276. 7, 8, 9, 10, 11, 12, 11, 11,
  4277. 8, 9, 10, 11, 12, 12, 11, 12,
  4278. 9, 10, 11, 12, 12, 12, 12, 12,
  4279. 10, 11, 12, 12, 13, 13, 12, 13,
  4280. 9, 10, 11, 12, 12, 12, 13, 13,
  4281. 10, 10, 11, 12, 12, 13, 13, 13
  4282. ];
  4283. Tables.t11l = [
  4284. 2, 4, 6, 8, 9, 10, 9, 10,
  4285. 4, 5, 6, 8, 10, 10, 9, 10,
  4286. 6, 7, 8, 9, 10, 11, 10, 10,
  4287. 8, 8, 9, 11, 10, 12, 10, 11,
  4288. 9, 10, 10, 11, 11, 12, 11, 12,
  4289. 9, 10, 11, 12, 12, 13, 12, 13,
  4290. 9, 9, 9, 10, 11, 12, 12, 12,
  4291. 9, 9, 10, 11, 12, 12, 12, 12
  4292. ];
  4293. Tables.t12l = [
  4294. 4, 4, 6, 8, 9, 10, 10, 10,
  4295. 4, 5, 6, 7, 9, 9, 10, 10,
  4296. 6, 6, 7, 8, 9, 10, 9, 10,
  4297. 7, 7, 8, 8, 9, 10, 10, 10,
  4298. 8, 8, 9, 9, 10, 10, 10, 11,
  4299. 9, 9, 10, 10, 10, 11, 10, 11,
  4300. 9, 9, 9, 10, 10, 11, 11, 12,
  4301. 10, 10, 10, 11, 11, 11, 11, 12
  4302. ];
  4303. Tables.t13l = [
  4304. 1, 5, 7, 8, 9, 10, 10, 11, 10, 11, 12, 12, 13, 13, 14, 14,
  4305. 4, 6, 8, 9, 10, 10, 11, 11, 11, 11, 12, 12, 13, 14, 14, 14,
  4306. 7, 8, 9, 10, 11, 11, 12, 12, 11, 12, 12, 13, 13, 14, 15, 15,
  4307. 8, 9, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 15, 15,
  4308. 9, 9, 11, 11, 12, 12, 13, 13, 12, 13, 13, 14, 14, 15, 15, 16,
  4309. 10, 10, 11, 12, 12, 12, 13, 13, 13, 13, 14, 13, 15, 15, 16, 16,
  4310. 10, 11, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16,
  4311. 11, 11, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16, 18, 18,
  4312. 10, 10, 11, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 16, 17, 17,
  4313. 11, 11, 12, 12, 13, 13, 13, 15, 14, 15, 15, 16, 16, 16, 18, 17,
  4314. 11, 12, 12, 13, 13, 14, 14, 15, 14, 15, 16, 15, 16, 17, 18, 19,
  4315. 12, 12, 12, 13, 14, 14, 14, 14, 15, 15, 15, 16, 17, 17, 17, 18,
  4316. 12, 13, 13, 14, 14, 15, 14, 15, 16, 16, 17, 17, 17, 18, 18, 18,
  4317. 13, 13, 14, 15, 15, 15, 16, 16, 16, 16, 16, 17, 18, 17, 18, 18,
  4318. 14, 14, 14, 15, 15, 15, 17, 16, 16, 19, 17, 17, 17, 19, 18, 18,
  4319. 13, 14, 15, 16, 16, 16, 17, 16, 17, 17, 18, 18, 21, 20, 21, 18
  4320. ];
  4321. Tables.t15l = [
  4322. 3, 5, 6, 8, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 14,
  4323. 5, 5, 7, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 13,
  4324. 6, 7, 7, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 13,
  4325. 7, 8, 8, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13,
  4326. 8, 8, 9, 9, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 13,
  4327. 9, 9, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 13, 14,
  4328. 10, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 14, 14,
  4329. 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 14,
  4330. 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 14, 14, 14,
  4331. 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14,
  4332. 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 15, 14,
  4333. 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15,
  4334. 12, 12, 11, 12, 12, 12, 13, 13, 13, 13, 13, 13, 14, 14, 15, 15,
  4335. 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15,
  4336. 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 14, 15,
  4337. 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15
  4338. ];
  4339. Tables.t16_5l = [
  4340. 1, 5, 7, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 11,
  4341. 4, 6, 8, 9, 10, 11, 11, 11, 12, 12, 12, 13, 14, 13, 14, 11,
  4342. 7, 8, 9, 10, 11, 11, 12, 12, 13, 12, 13, 13, 13, 14, 14, 12,
  4343. 9, 9, 10, 11, 11, 12, 12, 12, 13, 13, 14, 14, 14, 15, 15, 13,
  4344. 10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 12,
  4345. 10, 10, 11, 11, 12, 13, 13, 14, 13, 14, 14, 15, 15, 15, 16, 13,
  4346. 11, 11, 11, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 16, 13,
  4347. 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 17, 17, 13,
  4348. 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 13,
  4349. 12, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 15, 16, 15, 14,
  4350. 12, 13, 12, 13, 14, 14, 14, 14, 15, 16, 16, 16, 17, 17, 16, 13,
  4351. 13, 13, 13, 13, 14, 14, 15, 16, 16, 16, 16, 16, 16, 15, 16, 14,
  4352. 13, 14, 14, 14, 14, 15, 15, 15, 15, 17, 16, 16, 16, 16, 18, 14,
  4353. 15, 14, 14, 14, 15, 15, 16, 16, 16, 18, 17, 17, 17, 19, 17, 14,
  4354. 14, 15, 13, 14, 16, 16, 15, 16, 16, 17, 18, 17, 19, 17, 16, 14,
  4355. 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 12
  4356. ];
  4357. Tables.t16l = [
  4358. 1, 5, 7, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 10,
  4359. 4, 6, 8, 9, 10, 11, 11, 11, 12, 12, 12, 13, 14, 13, 14, 10,
  4360. 7, 8, 9, 10, 11, 11, 12, 12, 13, 12, 13, 13, 13, 14, 14, 11,
  4361. 9, 9, 10, 11, 11, 12, 12, 12, 13, 13, 14, 14, 14, 15, 15, 12,
  4362. 10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 11,
  4363. 10, 10, 11, 11, 12, 13, 13, 14, 13, 14, 14, 15, 15, 15, 16, 12,
  4364. 11, 11, 11, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 16, 12,
  4365. 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 17, 17, 12,
  4366. 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 12,
  4367. 12, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 15, 16, 15, 13,
  4368. 12, 13, 12, 13, 14, 14, 14, 14, 15, 16, 16, 16, 17, 17, 16, 12,
  4369. 13, 13, 13, 13, 14, 14, 15, 16, 16, 16, 16, 16, 16, 15, 16, 13,
  4370. 13, 14, 14, 14, 14, 15, 15, 15, 15, 17, 16, 16, 16, 16, 18, 13,
  4371. 15, 14, 14, 14, 15, 15, 16, 16, 16, 18, 17, 17, 17, 19, 17, 13,
  4372. 14, 15, 13, 14, 16, 16, 15, 16, 16, 17, 18, 17, 19, 17, 16, 13,
  4373. 10, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 10
  4374. ];
  4375. Tables.t24l = [
  4376. 4, 5, 7, 8, 9, 10, 10, 11, 11, 12, 12, 12, 12, 12, 13, 10,
  4377. 5, 6, 7, 8, 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 12, 10,
  4378. 7, 7, 8, 9, 9, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 9,
  4379. 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 9,
  4380. 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 9,
  4381. 10, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 9,
  4382. 10, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 9,
  4383. 11, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 10,
  4384. 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 10,
  4385. 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 10,
  4386. 12, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 10,
  4387. 12, 12, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 10,
  4388. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 10,
  4389. 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 10,
  4390. 13, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 10,
  4391. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 6
  4392. ];
  4393. Tables.t32l = [
  4394. 1 + 0, 4 + 1, 4 + 1, 5 + 2, 4 + 1, 6 + 2, 5 + 2, 6 + 3,
  4395. 4 + 1, 5 + 2, 5 + 2, 6 + 3, 5 + 2, 6 + 3, 6 + 3, 6 + 4
  4396. ];
  4397. Tables.t33l = [
  4398. 4 + 0, 4 + 1, 4 + 1, 4 + 2, 4 + 1, 4 + 2, 4 + 2, 4 + 3,
  4399. 4 + 1, 4 + 2, 4 + 2, 4 + 3, 4 + 2, 4 + 3, 4 + 3, 4 + 4
  4400. ];
  4401. Tables.ht = [
  4402. /* xlen, linmax, table, hlen */
  4403. new HuffCodeTab(0, 0, null, null),
  4404. new HuffCodeTab(2, 0, Tables.t1HB, Tables.t1l),
  4405. new HuffCodeTab(3, 0, Tables.t2HB, Tables.t2l),
  4406. new HuffCodeTab(3, 0, Tables.t3HB, Tables.t3l),
  4407. new HuffCodeTab(0, 0, null, null), /* Apparently not used */
  4408. new HuffCodeTab(4, 0, Tables.t5HB, Tables.t5l),
  4409. new HuffCodeTab(4, 0, Tables.t6HB, Tables.t6l),
  4410. new HuffCodeTab(6, 0, Tables.t7HB, Tables.t7l),
  4411. new HuffCodeTab(6, 0, Tables.t8HB, Tables.t8l),
  4412. new HuffCodeTab(6, 0, Tables.t9HB, Tables.t9l),
  4413. new HuffCodeTab(8, 0, Tables.t10HB, Tables.t10l),
  4414. new HuffCodeTab(8, 0, Tables.t11HB, Tables.t11l),
  4415. new HuffCodeTab(8, 0, Tables.t12HB, Tables.t12l),
  4416. new HuffCodeTab(16, 0, Tables.t13HB, Tables.t13l),
  4417. new HuffCodeTab(0, 0, null, Tables.t16_5l), /* Apparently not used */
  4418. new HuffCodeTab(16, 0, Tables.t15HB, Tables.t15l),
  4419. new HuffCodeTab(1, 1, Tables.t16HB, Tables.t16l),
  4420. new HuffCodeTab(2, 3, Tables.t16HB, Tables.t16l),
  4421. new HuffCodeTab(3, 7, Tables.t16HB, Tables.t16l),
  4422. new HuffCodeTab(4, 15, Tables.t16HB, Tables.t16l),
  4423. new HuffCodeTab(6, 63, Tables.t16HB, Tables.t16l),
  4424. new HuffCodeTab(8, 255, Tables.t16HB, Tables.t16l),
  4425. new HuffCodeTab(10, 1023, Tables.t16HB, Tables.t16l),
  4426. new HuffCodeTab(13, 8191, Tables.t16HB, Tables.t16l),
  4427. new HuffCodeTab(4, 15, Tables.t24HB, Tables.t24l),
  4428. new HuffCodeTab(5, 31, Tables.t24HB, Tables.t24l),
  4429. new HuffCodeTab(6, 63, Tables.t24HB, Tables.t24l),
  4430. new HuffCodeTab(7, 127, Tables.t24HB, Tables.t24l),
  4431. new HuffCodeTab(8, 255, Tables.t24HB, Tables.t24l),
  4432. new HuffCodeTab(9, 511, Tables.t24HB, Tables.t24l),
  4433. new HuffCodeTab(11, 2047, Tables.t24HB, Tables.t24l),
  4434. new HuffCodeTab(13, 8191, Tables.t24HB, Tables.t24l),
  4435. new HuffCodeTab(0, 0, Tables.t32HB, Tables.t32l),
  4436. new HuffCodeTab(0, 0, Tables.t33HB, Tables.t33l),
  4437. ];
  4438. /**
  4439. * <CODE>
  4440. * for (i = 0; i < 16*16; i++) [
  4441. * largetbl[i] = ((ht[16].hlen[i]) << 16) + ht[24].hlen[i];
  4442. * ]
  4443. * </CODE>
  4444. *
  4445. */
  4446. Tables.largetbl = [
  4447. 0x010004, 0x050005, 0x070007, 0x090008, 0x0a0009, 0x0a000a, 0x0b000a, 0x0b000b,
  4448. 0x0c000b, 0x0c000c, 0x0c000c, 0x0d000c, 0x0d000c, 0x0d000c, 0x0e000d, 0x0a000a,
  4449. 0x040005, 0x060006, 0x080007, 0x090008, 0x0a0009, 0x0b000a, 0x0b000a, 0x0b000b,
  4450. 0x0c000b, 0x0c000b, 0x0c000c, 0x0d000c, 0x0e000c, 0x0d000c, 0x0e000c, 0x0a000a,
  4451. 0x070007, 0x080007, 0x090008, 0x0a0009, 0x0b0009, 0x0b000a, 0x0c000a, 0x0c000b,
  4452. 0x0d000b, 0x0c000b, 0x0d000b, 0x0d000c, 0x0d000c, 0x0e000c, 0x0e000d, 0x0b0009,
  4453. 0x090008, 0x090008, 0x0a0009, 0x0b0009, 0x0b000a, 0x0c000a, 0x0c000a, 0x0c000b,
  4454. 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x0c0009,
  4455. 0x0a0009, 0x0a0009, 0x0b0009, 0x0b000a, 0x0c000a, 0x0c000a, 0x0d000a, 0x0d000b,
  4456. 0x0d000b, 0x0e000b, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000d, 0x0b0009,
  4457. 0x0a000a, 0x0a0009, 0x0b000a, 0x0b000a, 0x0c000a, 0x0d000a, 0x0d000b, 0x0e000b,
  4458. 0x0d000b, 0x0e000b, 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000c, 0x10000c, 0x0c0009,
  4459. 0x0b000a, 0x0b000a, 0x0b000a, 0x0c000a, 0x0d000a, 0x0d000b, 0x0d000b, 0x0d000b,
  4460. 0x0e000b, 0x0e000c, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x10000d, 0x0c0009,
  4461. 0x0b000b, 0x0b000a, 0x0c000a, 0x0c000a, 0x0d000b, 0x0d000b, 0x0d000b, 0x0e000b,
  4462. 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000c, 0x0f000c, 0x11000d, 0x11000d, 0x0c000a,
  4463. 0x0b000b, 0x0c000b, 0x0c000b, 0x0d000b, 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000b,
  4464. 0x0f000b, 0x0f000c, 0x0f000c, 0x0f000c, 0x10000c, 0x10000d, 0x10000d, 0x0c000a,
  4465. 0x0c000b, 0x0c000b, 0x0c000b, 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000b, 0x0f000c,
  4466. 0x0f000c, 0x0f000c, 0x0f000c, 0x10000c, 0x0f000d, 0x10000d, 0x0f000d, 0x0d000a,
  4467. 0x0c000c, 0x0d000b, 0x0c000b, 0x0d000b, 0x0e000b, 0x0e000c, 0x0e000c, 0x0e000c,
  4468. 0x0f000c, 0x10000c, 0x10000c, 0x10000d, 0x11000d, 0x11000d, 0x10000d, 0x0c000a,
  4469. 0x0d000c, 0x0d000c, 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000c, 0x0f000c, 0x10000c,
  4470. 0x10000c, 0x10000c, 0x10000c, 0x10000d, 0x10000d, 0x0f000d, 0x10000d, 0x0d000a,
  4471. 0x0d000c, 0x0e000c, 0x0e000c, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000c,
  4472. 0x0f000c, 0x11000c, 0x10000d, 0x10000d, 0x10000d, 0x10000d, 0x12000d, 0x0d000a,
  4473. 0x0f000c, 0x0e000c, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x10000c, 0x10000c,
  4474. 0x10000d, 0x12000d, 0x11000d, 0x11000d, 0x11000d, 0x13000d, 0x11000d, 0x0d000a,
  4475. 0x0e000d, 0x0f000c, 0x0d000c, 0x0e000c, 0x10000c, 0x10000c, 0x0f000c, 0x10000d,
  4476. 0x10000d, 0x11000d, 0x12000d, 0x11000d, 0x13000d, 0x11000d, 0x10000d, 0x0d000a,
  4477. 0x0a0009, 0x0a0009, 0x0a0009, 0x0b0009, 0x0b0009, 0x0c0009, 0x0c0009, 0x0c0009,
  4478. 0x0d0009, 0x0d0009, 0x0d0009, 0x0d000a, 0x0d000a, 0x0d000a, 0x0d000a, 0x0a0006
  4479. ];
  4480. /**
  4481. * <CODE>
  4482. * for (i = 0; i < 3*3; i++) [
  4483. * table23[i] = ((ht[2].hlen[i]) << 16) + ht[3].hlen[i];
  4484. * ]
  4485. * </CODE>
  4486. *
  4487. */
  4488. Tables.table23 = [
  4489. 0x010002, 0x040003, 0x070007,
  4490. 0x040004, 0x050004, 0x070007,
  4491. 0x060006, 0x070007, 0x080008
  4492. ];
  4493. /**
  4494. * <CODE>
  4495. * for (i = 0; i < 4*4; i++) [
  4496. * table56[i] = ((ht[5].hlen[i]) << 16) + ht[6].hlen[i];
  4497. * ]
  4498. * </CODE>
  4499. *
  4500. */
  4501. Tables.table56 = [
  4502. 0x010003, 0x040004, 0x070006, 0x080008, 0x040004, 0x050004, 0x080006, 0x090007,
  4503. 0x070005, 0x080006, 0x090007, 0x0a0008, 0x080007, 0x080007, 0x090008, 0x0a0009
  4504. ];
  4505. Tables.bitrate_table = [
  4506. [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, -1], /* MPEG 2 */
  4507. [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1], /* MPEG 1 */
  4508. [0, 8, 16, 24, 32, 40, 48, 56, 64, -1, -1, -1, -1, -1, -1, -1], /* MPEG 2.5 */
  4509. ];
  4510. /**
  4511. * MPEG 2, MPEG 1, MPEG 2.5.
  4512. */
  4513. Tables.samplerate_table = [
  4514. [22050, 24000, 16000, -1],
  4515. [44100, 48000, 32000, -1],
  4516. [11025, 12000, 8000, -1],
  4517. ];
  4518. /**
  4519. * This is the scfsi_band table from 2.4.2.7 of the IS.
  4520. */
  4521. Tables.scfsi_band = [0, 6, 11, 16, 21];
  4522. function MeanBits(meanBits) {
  4523. this.bits = meanBits;
  4524. }
  4525. function VBRQuantize() {
  4526. var qupvt;
  4527. var tak;
  4528. this.setModules = function (_qupvt, _tk) {
  4529. qupvt = _qupvt;
  4530. tak = _tk;
  4531. }
  4532. //TODO
  4533. }
  4534. //package mp3;
  4535. function CalcNoiseResult() {
  4536. /**
  4537. * sum of quantization noise > masking
  4538. */
  4539. this.over_noise = 0.;
  4540. /**
  4541. * sum of all quantization noise
  4542. */
  4543. this.tot_noise = 0.;
  4544. /**
  4545. * max quantization noise
  4546. */
  4547. this.max_noise = 0.;
  4548. /**
  4549. * number of quantization noise > masking
  4550. */
  4551. this.over_count = 0;
  4552. /**
  4553. * SSD-like cost of distorted bands
  4554. */
  4555. this.over_SSD = 0;
  4556. this.bits = 0;
  4557. }
  4558. function LameGlobalFlags() {
  4559. this.class_id = 0;
  4560. /* input description */
  4561. /**
  4562. * number of samples. default=-1
  4563. */
  4564. this.num_samples = 0;
  4565. /**
  4566. * input number of channels. default=2
  4567. */
  4568. this.num_channels = 0;
  4569. /**
  4570. * input_samp_rate in Hz. default=44.1 kHz
  4571. */
  4572. this.in_samplerate = 0;
  4573. /**
  4574. * output_samp_rate. default: LAME picks best value at least not used for
  4575. * MP3 decoding: Remember 44.1 kHz MP3s and AC97
  4576. */
  4577. this.out_samplerate = 0;
  4578. /**
  4579. * scale input by this amount before encoding at least not used for MP3
  4580. * decoding
  4581. */
  4582. this.scale = 0.;
  4583. /**
  4584. * scale input of channel 0 (left) by this amount before encoding
  4585. */
  4586. this.scale_left = 0.;
  4587. /**
  4588. * scale input of channel 1 (right) by this amount before encoding
  4589. */
  4590. this.scale_right = 0.;
  4591. /* general control params */
  4592. /**
  4593. * collect data for a MP3 frame analyzer?
  4594. */
  4595. this.analysis = false;
  4596. /**
  4597. * add Xing VBR tag?
  4598. */
  4599. this.bWriteVbrTag = false;
  4600. /**
  4601. * use lame/mpglib to convert mp3 to wav
  4602. */
  4603. this.decode_only = false;
  4604. /**
  4605. * quality setting 0=best, 9=worst default=5
  4606. */
  4607. this.quality = 0;
  4608. /**
  4609. * see enum default = LAME picks best value
  4610. */
  4611. this.mode = MPEGMode.STEREO;
  4612. /**
  4613. * force M/S mode. requires mode=1
  4614. */
  4615. this.force_ms = false;
  4616. /**
  4617. * use free format? default=0
  4618. */
  4619. this.free_format = false;
  4620. /**
  4621. * find the RG value? default=0
  4622. */
  4623. this.findReplayGain = false;
  4624. /**
  4625. * decode on the fly? default=0
  4626. */
  4627. this.decode_on_the_fly = false;
  4628. /**
  4629. * 1 (default) writes ID3 tags, 0 not
  4630. */
  4631. this.write_id3tag_automatic = false;
  4632. /*
  4633. * set either brate>0 or compression_ratio>0, LAME will compute the value of
  4634. * the variable not set. Default is compression_ratio = 11.025
  4635. */
  4636. /**
  4637. * bitrate
  4638. */
  4639. this.brate = 0;
  4640. /**
  4641. * sizeof(wav file)/sizeof(mp3 file)
  4642. */
  4643. this.compression_ratio = 0.;
  4644. /* frame params */
  4645. /**
  4646. * mark as copyright. default=0
  4647. */
  4648. this.copyright = 0;
  4649. /**
  4650. * mark as original. default=1
  4651. */
  4652. this.original = 0;
  4653. /**
  4654. * the MP3 'private extension' bit. Meaningless
  4655. */
  4656. this.extension = 0;
  4657. /**
  4658. * Input PCM is emphased PCM (for instance from one of the rarely emphased
  4659. * CDs), it is STRONGLY not recommended to use this, because psycho does not
  4660. * take it into account, and last but not least many decoders don't care
  4661. * about these bits
  4662. */
  4663. this.emphasis = 0;
  4664. /**
  4665. * use 2 bytes per frame for a CRC checksum. default=0
  4666. */
  4667. this.error_protection = 0;
  4668. /**
  4669. * enforce ISO spec as much as possible
  4670. */
  4671. this.strict_ISO = false;
  4672. /**
  4673. * use bit reservoir?
  4674. */
  4675. this.disable_reservoir = false;
  4676. /* quantization/noise shaping */
  4677. this.quant_comp = 0;
  4678. this.quant_comp_short = 0;
  4679. this.experimentalY = false;
  4680. this.experimentalZ = 0;
  4681. this.exp_nspsytune = 0;
  4682. this.preset = 0;
  4683. /* VBR control */
  4684. this.VBR = null;
  4685. /**
  4686. * Range [0,...,1[
  4687. */
  4688. this.VBR_q_frac = 0.;
  4689. /**
  4690. * Range [0,...,9]
  4691. */
  4692. this.VBR_q = 0;
  4693. this.VBR_mean_bitrate_kbps = 0;
  4694. this.VBR_min_bitrate_kbps = 0;
  4695. this.VBR_max_bitrate_kbps = 0;
  4696. /**
  4697. * strictly enforce VBR_min_bitrate normaly, it will be violated for analog
  4698. * silence
  4699. */
  4700. this.VBR_hard_min = 0;
  4701. /* resampling and filtering */
  4702. /**
  4703. * freq in Hz. 0=lame choses. -1=no filter
  4704. */
  4705. this.lowpassfreq = 0;
  4706. /**
  4707. * freq in Hz. 0=lame choses. -1=no filter
  4708. */
  4709. this.highpassfreq = 0;
  4710. /**
  4711. * freq width of filter, in Hz (default=15%)
  4712. */
  4713. this.lowpasswidth = 0;
  4714. /**
  4715. * freq width of filter, in Hz (default=15%)
  4716. */
  4717. this.highpasswidth = 0;
  4718. /*
  4719. * psycho acoustics and other arguments which you should not change unless
  4720. * you know what you are doing
  4721. */
  4722. this.maskingadjust = 0.;
  4723. this.maskingadjust_short = 0.;
  4724. /**
  4725. * only use ATH
  4726. */
  4727. this.ATHonly = false;
  4728. /**
  4729. * only use ATH for short blocks
  4730. */
  4731. this.ATHshort = false;
  4732. /**
  4733. * disable ATH
  4734. */
  4735. this.noATH = false;
  4736. /**
  4737. * select ATH formula
  4738. */
  4739. this.ATHtype = 0;
  4740. /**
  4741. * change ATH formula 4 shape
  4742. */
  4743. this.ATHcurve = 0.;
  4744. /**
  4745. * lower ATH by this many db
  4746. */
  4747. this.ATHlower = 0.;
  4748. /**
  4749. * select ATH auto-adjust scheme
  4750. */
  4751. this.athaa_type = 0;
  4752. /**
  4753. * select ATH auto-adjust loudness calc
  4754. */
  4755. this.athaa_loudapprox = 0;
  4756. /**
  4757. * dB, tune active region of auto-level
  4758. */
  4759. this.athaa_sensitivity = 0.;
  4760. this.short_blocks = null;
  4761. /**
  4762. * use temporal masking effect
  4763. */
  4764. this.useTemporal = false;
  4765. this.interChRatio = 0.;
  4766. /**
  4767. * Naoki's adjustment of Mid/Side maskings
  4768. */
  4769. this.msfix = 0.;
  4770. /**
  4771. * 0 off, 1 on
  4772. */
  4773. this.tune = false;
  4774. /**
  4775. * used to pass values for debugging and stuff
  4776. */
  4777. this.tune_value_a = 0.;
  4778. /************************************************************************/
  4779. /* internal variables, do not set... */
  4780. /* provided because they may be of use to calling application */
  4781. /************************************************************************/
  4782. /**
  4783. * 0=MPEG-2/2.5 1=MPEG-1
  4784. */
  4785. this.version = 0;
  4786. this.encoder_delay = 0;
  4787. /**
  4788. * number of samples of padding appended to input
  4789. */
  4790. this.encoder_padding = 0;
  4791. this.framesize = 0;
  4792. /**
  4793. * number of frames encoded
  4794. */
  4795. this.frameNum = 0;
  4796. /**
  4797. * is this struct owned by calling program or lame?
  4798. */
  4799. this.lame_allocated_gfp = 0;
  4800. /**************************************************************************/
  4801. /* more internal variables are stored in this structure: */
  4802. /**************************************************************************/
  4803. this.internal_flags = null;
  4804. }
  4805. function ReplayGain() {
  4806. this.linprebuf = new_float(GainAnalysis.MAX_ORDER * 2);
  4807. /**
  4808. * left input samples, with pre-buffer
  4809. */
  4810. this.linpre = 0;
  4811. this.lstepbuf = new_float(GainAnalysis.MAX_SAMPLES_PER_WINDOW + GainAnalysis.MAX_ORDER);
  4812. /**
  4813. * left "first step" (i.e. post first filter) samples
  4814. */
  4815. this.lstep = 0;
  4816. this.loutbuf = new_float(GainAnalysis.MAX_SAMPLES_PER_WINDOW + GainAnalysis.MAX_ORDER);
  4817. /**
  4818. * left "out" (i.e. post second filter) samples
  4819. */
  4820. this.lout = 0;
  4821. this.rinprebuf = new_float(GainAnalysis.MAX_ORDER * 2);
  4822. /**
  4823. * right input samples ...
  4824. */
  4825. this.rinpre = 0;
  4826. this.rstepbuf = new_float(GainAnalysis.MAX_SAMPLES_PER_WINDOW + GainAnalysis.MAX_ORDER);
  4827. this.rstep = 0;
  4828. this.routbuf = new_float(GainAnalysis.MAX_SAMPLES_PER_WINDOW + GainAnalysis.MAX_ORDER);
  4829. this.rout = 0;
  4830. /**
  4831. * number of samples required to reach number of milliseconds required
  4832. * for RMS window
  4833. */
  4834. this.sampleWindow = 0;
  4835. this.totsamp = 0;
  4836. this.lsum = 0.;
  4837. this.rsum = 0.;
  4838. this.freqindex = 0;
  4839. this.first = 0;
  4840. this.A = new_int(0 | (GainAnalysis.STEPS_per_dB * GainAnalysis.MAX_dB));
  4841. this.B = new_int(0 | (GainAnalysis.STEPS_per_dB * GainAnalysis.MAX_dB));
  4842. }
  4843. function CBRNewIterationLoop(_quantize) {
  4844. var quantize = _quantize;
  4845. this.quantize = quantize;
  4846. this.iteration_loop = function(gfp, pe, ms_ener_ratio, ratio) {
  4847. var gfc = gfp.internal_flags;
  4848. var l3_xmin = new_float(L3Side.SFBMAX);
  4849. var xrpow = new_float(576);
  4850. var targ_bits = new_int(2);
  4851. var mean_bits = 0, max_bits;
  4852. var l3_side = gfc.l3_side;
  4853. var mb = new MeanBits(mean_bits);
  4854. this.quantize.rv.ResvFrameBegin(gfp, mb);
  4855. mean_bits = mb.bits;
  4856. /* quantize! */
  4857. for (var gr = 0; gr < gfc.mode_gr; gr++) {
  4858. /*
  4859. * calculate needed bits
  4860. */
  4861. max_bits = this.quantize.qupvt.on_pe(gfp, pe, targ_bits, mean_bits,
  4862. gr, gr);
  4863. if (gfc.mode_ext == Encoder.MPG_MD_MS_LR) {
  4864. this.quantize.ms_convert(gfc.l3_side, gr);
  4865. this.quantize.qupvt.reduce_side(targ_bits, ms_ener_ratio[gr],
  4866. mean_bits, max_bits);
  4867. }
  4868. for (var ch = 0; ch < gfc.channels_out; ch++) {
  4869. var adjust, masking_lower_db;
  4870. var cod_info = l3_side.tt[gr][ch];
  4871. if (cod_info.block_type != Encoder.SHORT_TYPE) {
  4872. // NORM, START or STOP type
  4873. adjust = 0;
  4874. masking_lower_db = gfc.PSY.mask_adjust - adjust;
  4875. } else {
  4876. adjust = 0;
  4877. masking_lower_db = gfc.PSY.mask_adjust_short - adjust;
  4878. }
  4879. gfc.masking_lower = Math.pow(10.0,
  4880. masking_lower_db * 0.1);
  4881. /*
  4882. * init_outer_loop sets up cod_info, scalefac and xrpow
  4883. */
  4884. this.quantize.init_outer_loop(gfc, cod_info);
  4885. if (this.quantize.init_xrpow(gfc, cod_info, xrpow)) {
  4886. /*
  4887. * xr contains energy we will have to encode calculate the
  4888. * masking abilities find some good quantization in
  4889. * outer_loop
  4890. */
  4891. this.quantize.qupvt.calc_xmin(gfp, ratio[gr][ch], cod_info,
  4892. l3_xmin);
  4893. this.quantize.outer_loop(gfp, cod_info, l3_xmin, xrpow, ch,
  4894. targ_bits[ch]);
  4895. }
  4896. this.quantize.iteration_finish_one(gfc, gr, ch);
  4897. } /* for ch */
  4898. } /* for gr */
  4899. this.quantize.rv.ResvFrameEnd(gfc, mean_bits);
  4900. }
  4901. }
  4902. /**
  4903. * ATH related stuff, if something new ATH related has to be added, please plug
  4904. * it here into the ATH.
  4905. */
  4906. function ATH() {
  4907. /**
  4908. * Method for the auto adjustment.
  4909. */
  4910. this.useAdjust = 0;
  4911. /**
  4912. * factor for tuning the (sample power) point below which adaptive threshold
  4913. * of hearing adjustment occurs
  4914. */
  4915. this.aaSensitivityP = 0.;
  4916. /**
  4917. * Lowering based on peak volume, 1 = no lowering.
  4918. */
  4919. this.adjust = 0.;
  4920. /**
  4921. * Limit for dynamic ATH adjust.
  4922. */
  4923. this.adjustLimit = 0.;
  4924. /**
  4925. * Determined to lower x dB each second.
  4926. */
  4927. this.decay = 0.;
  4928. /**
  4929. * Lowest ATH value.
  4930. */
  4931. this.floor = 0.;
  4932. /**
  4933. * ATH for sfbs in long blocks.
  4934. */
  4935. this.l = new_float(Encoder.SBMAX_l);
  4936. /**
  4937. * ATH for sfbs in short blocks.
  4938. */
  4939. this.s = new_float(Encoder.SBMAX_s);
  4940. /**
  4941. * ATH for partitioned sfb21 in long blocks.
  4942. */
  4943. this.psfb21 = new_float(Encoder.PSFB21);
  4944. /**
  4945. * ATH for partitioned sfb12 in short blocks.
  4946. */
  4947. this.psfb12 = new_float(Encoder.PSFB12);
  4948. /**
  4949. * ATH for long block convolution bands.
  4950. */
  4951. this.cb_l = new_float(Encoder.CBANDS);
  4952. /**
  4953. * ATH for short block convolution bands.
  4954. */
  4955. this.cb_s = new_float(Encoder.CBANDS);
  4956. /**
  4957. * Equal loudness weights (based on ATH).
  4958. */
  4959. this.eql_w = new_float(Encoder.BLKSIZE / 2);
  4960. }
  4961. //package mp3;
  4962. /**
  4963. * Layer III side information.
  4964. *
  4965. * @author Ken
  4966. *
  4967. */
  4968. function ScaleFac(arrL, arrS, arr21, arr12) {
  4969. this.l = new_int(1 + Encoder.SBMAX_l);
  4970. this.s = new_int(1 + Encoder.SBMAX_s);
  4971. this.psfb21 = new_int(1 + Encoder.PSFB21);
  4972. this.psfb12 = new_int(1 + Encoder.PSFB12);
  4973. var l = this.l;
  4974. var s = this.s;
  4975. if (arguments.length == 4) {
  4976. //public ScaleFac(final int[] arrL, final int[] arrS, final int[] arr21,
  4977. // final int[] arr12) {
  4978. this.arrL = arguments[0];
  4979. this.arrS = arguments[1];
  4980. this.arr21 = arguments[2];
  4981. this.arr12 = arguments[3];
  4982. System.arraycopy(this.arrL, 0, l, 0, Math.min(this.arrL.length, this.l.length));
  4983. System.arraycopy(this.arrS, 0, s, 0, Math.min(this.arrS.length, this.s.length));
  4984. System.arraycopy(this.arr21, 0, this.psfb21, 0, Math.min(this.arr21.length, this.psfb21.length));
  4985. System.arraycopy(this.arr12, 0, this.psfb12, 0, Math.min(this.arr12.length, this.psfb12.length));
  4986. }
  4987. }
  4988. /*
  4989. * quantize_pvt source file
  4990. *
  4991. * Copyright (c) 1999-2002 Takehiro Tominaga
  4992. * Copyright (c) 2000-2002 Robert Hegemann
  4993. * Copyright (c) 2001 Naoki Shibata
  4994. * Copyright (c) 2002-2005 Gabriel Bouvigne
  4995. *
  4996. * This library is free software; you can redistribute it and/or
  4997. * modify it under the terms of the GNU Lesser General Public
  4998. * License as published by the Free Software Foundation; either
  4999. * version 2 of the License, or (at your option) any later version.
  5000. *
  5001. * This library is distributed in the hope that it will be useful,
  5002. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5003. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5004. * Library General Public License for more details.
  5005. *
  5006. * You should have received a copy of the GNU Lesser General Public
  5007. * License along with this library; if not, write to the
  5008. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5009. * Boston, MA 02111-1307, USA.
  5010. */
  5011. /* $Id: QuantizePVT.java,v 1.24 2011/05/24 20:48:06 kenchis Exp $ */
  5012. QuantizePVT.Q_MAX = (256 + 1);
  5013. QuantizePVT.Q_MAX2 = 116;
  5014. QuantizePVT.LARGE_BITS = 100000;
  5015. QuantizePVT.IXMAX_VAL = 8206;
  5016. function QuantizePVT() {
  5017. var tak = null;
  5018. var rv = null;
  5019. var psy = null;
  5020. this.setModules = function (_tk, _rv, _psy) {
  5021. tak = _tk;
  5022. rv = _rv;
  5023. psy = _psy;
  5024. };
  5025. function POW20(x) {
  5026. return pow20[x + QuantizePVT.Q_MAX2];
  5027. }
  5028. this.IPOW20 = function (x) {
  5029. return ipow20[x];
  5030. }
  5031. /**
  5032. * smallest such that 1.0+DBL_EPSILON != 1.0
  5033. */
  5034. var DBL_EPSILON = 2.2204460492503131e-016;
  5035. /**
  5036. * ix always <= 8191+15. see count_bits()
  5037. */
  5038. var IXMAX_VAL = QuantizePVT.IXMAX_VAL;
  5039. var PRECALC_SIZE = (IXMAX_VAL + 2);
  5040. var Q_MAX = QuantizePVT.Q_MAX;
  5041. /**
  5042. * <CODE>
  5043. * minimum possible number of
  5044. * -cod_info.global_gain + ((scalefac[] + (cod_info.preflag ? pretab[sfb] : 0))
  5045. * << (cod_info.scalefac_scale + 1)) + cod_info.subblock_gain[cod_info.window[sfb]] * 8;
  5046. *
  5047. * for long block, 0+((15+3)<<2) = 18*4 = 72
  5048. * for short block, 0+(15<<2)+7*8 = 15*4+56 = 116
  5049. * </CODE>
  5050. */
  5051. var Q_MAX2 = QuantizePVT.Q_MAX2;
  5052. var LARGE_BITS = QuantizePVT.LARGE_BITS;
  5053. /**
  5054. * Assuming dynamic range=96dB, this value should be 92
  5055. */
  5056. var NSATHSCALE = 100;
  5057. /**
  5058. * The following table is used to implement the scalefactor partitioning for
  5059. * MPEG2 as described in section 2.4.3.2 of the IS. The indexing corresponds
  5060. * to the way the tables are presented in the IS:
  5061. *
  5062. * [table_number][row_in_table][column of nr_of_sfb]
  5063. */
  5064. this.nr_of_sfb_block = [
  5065. [[6, 5, 5, 5], [9, 9, 9, 9], [6, 9, 9, 9]],
  5066. [[6, 5, 7, 3], [9, 9, 12, 6], [6, 9, 12, 6]],
  5067. [[11, 10, 0, 0], [18, 18, 0, 0], [15, 18, 0, 0]],
  5068. [[7, 7, 7, 0], [12, 12, 12, 0], [6, 15, 12, 0]],
  5069. [[6, 6, 6, 3], [12, 9, 9, 6], [6, 12, 9, 6]],
  5070. [[8, 8, 5, 0], [15, 12, 9, 0], [6, 18, 9, 0]]];
  5071. /**
  5072. * Table B.6: layer3 preemphasis
  5073. */
  5074. var pretab = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
  5075. 2, 2, 3, 3, 3, 2, 0];
  5076. this.pretab = pretab;
  5077. /**
  5078. * Here are MPEG1 Table B.8 and MPEG2 Table B.1 -- Layer III scalefactor
  5079. * bands. <BR>
  5080. * Index into this using a method such as:<BR>
  5081. * idx = fr_ps.header.sampling_frequency + (fr_ps.header.version * 3)
  5082. */
  5083. this.sfBandIndex = [
  5084. // Table B.2.b: 22.05 kHz
  5085. new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  5086. 522, 576],
  5087. [0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192]
  5088. , [0, 0, 0, 0, 0, 0, 0] // sfb21 pseudo sub bands
  5089. , [0, 0, 0, 0, 0, 0, 0] // sfb12 pseudo sub bands
  5090. ),
  5091. /* Table B.2.c: 24 kHz */ /* docs: 332. mpg123(broken): 330 */
  5092. new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 332, 394, 464,
  5093. 540, 576],
  5094. [0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192]
  5095. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  5096. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  5097. ),
  5098. /* Table B.2.a: 16 kHz */
  5099. new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  5100. 522, 576],
  5101. [0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192]
  5102. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  5103. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  5104. ),
  5105. /* Table B.8.b: 44.1 kHz */
  5106. new ScaleFac([0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418,
  5107. 576],
  5108. [0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192]
  5109. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  5110. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  5111. ),
  5112. /* Table B.8.c: 48 kHz */
  5113. new ScaleFac([0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384,
  5114. 576],
  5115. [0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192]
  5116. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  5117. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  5118. ),
  5119. /* Table B.8.a: 32 kHz */
  5120. new ScaleFac([0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550,
  5121. 576],
  5122. [0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192]
  5123. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  5124. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  5125. ),
  5126. /* MPEG-2.5 11.025 kHz */
  5127. new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  5128. 522, 576],
  5129. [0 / 3, 12 / 3, 24 / 3, 36 / 3, 54 / 3, 78 / 3, 108 / 3, 144 / 3, 186 / 3, 240 / 3, 312 / 3,
  5130. 402 / 3, 522 / 3, 576 / 3]
  5131. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  5132. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  5133. ),
  5134. /* MPEG-2.5 12 kHz */
  5135. new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  5136. 522, 576],
  5137. [0 / 3, 12 / 3, 24 / 3, 36 / 3, 54 / 3, 78 / 3, 108 / 3, 144 / 3, 186 / 3, 240 / 3, 312 / 3,
  5138. 402 / 3, 522 / 3, 576 / 3]
  5139. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  5140. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  5141. ),
  5142. /* MPEG-2.5 8 kHz */
  5143. new ScaleFac([0, 12, 24, 36, 48, 60, 72, 88, 108, 132, 160, 192, 232, 280, 336, 400, 476, 566, 568, 570,
  5144. 572, 574, 576],
  5145. [0 / 3, 24 / 3, 48 / 3, 72 / 3, 108 / 3, 156 / 3, 216 / 3, 288 / 3, 372 / 3, 480 / 3, 486 / 3,
  5146. 492 / 3, 498 / 3, 576 / 3]
  5147. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  5148. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  5149. )
  5150. ];
  5151. var pow20 = new_float(Q_MAX + Q_MAX2 + 1);
  5152. var ipow20 = new_float(Q_MAX);
  5153. var pow43 = new_float(PRECALC_SIZE);
  5154. var adj43 = new_float(PRECALC_SIZE);
  5155. this.adj43 = adj43;
  5156. /**
  5157. * <PRE>
  5158. * compute the ATH for each scalefactor band cd range: 0..96db
  5159. *
  5160. * Input: 3.3kHz signal 32767 amplitude (3.3kHz is where ATH is smallest =
  5161. * -5db) longblocks: sfb=12 en0/bw=-11db max_en0 = 1.3db shortblocks: sfb=5
  5162. * -9db 0db
  5163. *
  5164. * Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated) longblocks: amp=1
  5165. * sfb=12 en0/bw=-103 db max_en0 = -92db amp=32767 sfb=12 -12 db -1.4db
  5166. *
  5167. * Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated) shortblocks: amp=1
  5168. * sfb=5 en0/bw= -99 -86 amp=32767 sfb=5 -9 db 4db
  5169. *
  5170. *
  5171. * MAX energy of largest wave at 3.3kHz = 1db AVE energy of largest wave at
  5172. * 3.3kHz = -11db Let's take AVE: -11db = maximum signal in sfb=12. Dynamic
  5173. * range of CD: 96db. Therefor energy of smallest audible wave in sfb=12 =
  5174. * -11 - 96 = -107db = ATH at 3.3kHz.
  5175. *
  5176. * ATH formula for this wave: -5db. To adjust to LAME scaling, we need ATH =
  5177. * ATH_formula - 103 (db) ATH = ATH * 2.5e-10 (ener)
  5178. * </PRE>
  5179. */
  5180. function ATHmdct(gfp, f) {
  5181. var ath = psy.ATHformula(f, gfp);
  5182. ath -= NSATHSCALE;
  5183. /* modify the MDCT scaling for the ATH and convert to energy */
  5184. ath = Math.pow(10.0, ath / 10.0 + gfp.ATHlower);
  5185. return ath;
  5186. }
  5187. function compute_ath(gfp) {
  5188. var ATH_l = gfp.internal_flags.ATH.l;
  5189. var ATH_psfb21 = gfp.internal_flags.ATH.psfb21;
  5190. var ATH_s = gfp.internal_flags.ATH.s;
  5191. var ATH_psfb12 = gfp.internal_flags.ATH.psfb12;
  5192. var gfc = gfp.internal_flags;
  5193. var samp_freq = gfp.out_samplerate;
  5194. for (var sfb = 0; sfb < Encoder.SBMAX_l; sfb++) {
  5195. var start = gfc.scalefac_band.l[sfb];
  5196. var end = gfc.scalefac_band.l[sfb + 1];
  5197. ATH_l[sfb] = Float.MAX_VALUE;
  5198. for (var i = start; i < end; i++) {
  5199. var freq = i * samp_freq / (2 * 576);
  5200. var ATH_f = ATHmdct(gfp, freq);
  5201. /* freq in kHz */
  5202. ATH_l[sfb] = Math.min(ATH_l[sfb], ATH_f);
  5203. }
  5204. }
  5205. for (var sfb = 0; sfb < Encoder.PSFB21; sfb++) {
  5206. var start = gfc.scalefac_band.psfb21[sfb];
  5207. var end = gfc.scalefac_band.psfb21[sfb + 1];
  5208. ATH_psfb21[sfb] = Float.MAX_VALUE;
  5209. for (var i = start; i < end; i++) {
  5210. var freq = i * samp_freq / (2 * 576);
  5211. var ATH_f = ATHmdct(gfp, freq);
  5212. /* freq in kHz */
  5213. ATH_psfb21[sfb] = Math.min(ATH_psfb21[sfb], ATH_f);
  5214. }
  5215. }
  5216. for (var sfb = 0; sfb < Encoder.SBMAX_s; sfb++) {
  5217. var start = gfc.scalefac_band.s[sfb];
  5218. var end = gfc.scalefac_band.s[sfb + 1];
  5219. ATH_s[sfb] = Float.MAX_VALUE;
  5220. for (var i = start; i < end; i++) {
  5221. var freq = i * samp_freq / (2 * 192);
  5222. var ATH_f = ATHmdct(gfp, freq);
  5223. /* freq in kHz */
  5224. ATH_s[sfb] = Math.min(ATH_s[sfb], ATH_f);
  5225. }
  5226. ATH_s[sfb] *= (gfc.scalefac_band.s[sfb + 1] - gfc.scalefac_band.s[sfb]);
  5227. }
  5228. for (var sfb = 0; sfb < Encoder.PSFB12; sfb++) {
  5229. var start = gfc.scalefac_band.psfb12[sfb];
  5230. var end = gfc.scalefac_band.psfb12[sfb + 1];
  5231. ATH_psfb12[sfb] = Float.MAX_VALUE;
  5232. for (var i = start; i < end; i++) {
  5233. var freq = i * samp_freq / (2 * 192);
  5234. var ATH_f = ATHmdct(gfp, freq);
  5235. /* freq in kHz */
  5236. ATH_psfb12[sfb] = Math.min(ATH_psfb12[sfb], ATH_f);
  5237. }
  5238. /* not sure about the following */
  5239. ATH_psfb12[sfb] *= (gfc.scalefac_band.s[13] - gfc.scalefac_band.s[12]);
  5240. }
  5241. /*
  5242. * no-ATH mode: reduce ATH to -200 dB
  5243. */
  5244. if (gfp.noATH) {
  5245. for (var sfb = 0; sfb < Encoder.SBMAX_l; sfb++) {
  5246. ATH_l[sfb] = 1E-20;
  5247. }
  5248. for (var sfb = 0; sfb < Encoder.PSFB21; sfb++) {
  5249. ATH_psfb21[sfb] = 1E-20;
  5250. }
  5251. for (var sfb = 0; sfb < Encoder.SBMAX_s; sfb++) {
  5252. ATH_s[sfb] = 1E-20;
  5253. }
  5254. for (var sfb = 0; sfb < Encoder.PSFB12; sfb++) {
  5255. ATH_psfb12[sfb] = 1E-20;
  5256. }
  5257. }
  5258. /*
  5259. * work in progress, don't rely on it too much
  5260. */
  5261. gfc.ATH.floor = 10. * Math.log10(ATHmdct(gfp, -1.));
  5262. }
  5263. /**
  5264. * initialization for iteration_loop
  5265. */
  5266. this.iteration_init = function (gfp) {
  5267. var gfc = gfp.internal_flags;
  5268. var l3_side = gfc.l3_side;
  5269. var i;
  5270. if (gfc.iteration_init_init == 0) {
  5271. gfc.iteration_init_init = 1;
  5272. l3_side.main_data_begin = 0;
  5273. compute_ath(gfp);
  5274. pow43[0] = 0.0;
  5275. for (i = 1; i < PRECALC_SIZE; i++)
  5276. pow43[i] = Math.pow(i, 4.0 / 3.0);
  5277. for (i = 0; i < PRECALC_SIZE - 1; i++)
  5278. adj43[i] = ((i + 1) - Math.pow(
  5279. 0.5 * (pow43[i] + pow43[i + 1]), 0.75));
  5280. adj43[i] = 0.5;
  5281. for (i = 0; i < Q_MAX; i++)
  5282. ipow20[i] = Math.pow(2.0, (i - 210) * -0.1875);
  5283. for (i = 0; i <= Q_MAX + Q_MAX2; i++)
  5284. pow20[i] = Math.pow(2.0, (i - 210 - Q_MAX2) * 0.25);
  5285. tak.huffman_init(gfc);
  5286. {
  5287. var bass, alto, treble, sfb21;
  5288. i = (gfp.exp_nspsytune >> 2) & 63;
  5289. if (i >= 32)
  5290. i -= 64;
  5291. bass = Math.pow(10, i / 4.0 / 10.0);
  5292. i = (gfp.exp_nspsytune >> 8) & 63;
  5293. if (i >= 32)
  5294. i -= 64;
  5295. alto = Math.pow(10, i / 4.0 / 10.0);
  5296. i = (gfp.exp_nspsytune >> 14) & 63;
  5297. if (i >= 32)
  5298. i -= 64;
  5299. treble = Math.pow(10, i / 4.0 / 10.0);
  5300. /*
  5301. * to be compatible with Naoki's original code, the next 6 bits
  5302. * define only the amount of changing treble for sfb21
  5303. */
  5304. i = (gfp.exp_nspsytune >> 20) & 63;
  5305. if (i >= 32)
  5306. i -= 64;
  5307. sfb21 = treble * Math.pow(10, i / 4.0 / 10.0);
  5308. for (i = 0; i < Encoder.SBMAX_l; i++) {
  5309. var f;
  5310. if (i <= 6)
  5311. f = bass;
  5312. else if (i <= 13)
  5313. f = alto;
  5314. else if (i <= 20)
  5315. f = treble;
  5316. else
  5317. f = sfb21;
  5318. gfc.nsPsy.longfact[i] = f;
  5319. }
  5320. for (i = 0; i < Encoder.SBMAX_s; i++) {
  5321. var f;
  5322. if (i <= 5)
  5323. f = bass;
  5324. else if (i <= 10)
  5325. f = alto;
  5326. else if (i <= 11)
  5327. f = treble;
  5328. else
  5329. f = sfb21;
  5330. gfc.nsPsy.shortfact[i] = f;
  5331. }
  5332. }
  5333. }
  5334. }
  5335. /**
  5336. * allocate bits among 2 channels based on PE<BR>
  5337. * mt 6/99<BR>
  5338. * bugfixes rh 8/01: often allocated more than the allowed 4095 bits
  5339. */
  5340. this.on_pe = function (gfp, pe,
  5341. targ_bits, mean_bits, gr, cbr) {
  5342. var gfc = gfp.internal_flags;
  5343. var tbits = 0, bits;
  5344. var add_bits = new_int(2);
  5345. var ch;
  5346. /* allocate targ_bits for granule */
  5347. var mb = new MeanBits(tbits);
  5348. var extra_bits = rv.ResvMaxBits(gfp, mean_bits, mb, cbr);
  5349. tbits = mb.bits;
  5350. /* maximum allowed bits for this granule */
  5351. var max_bits = tbits + extra_bits;
  5352. if (max_bits > LameInternalFlags.MAX_BITS_PER_GRANULE) {
  5353. // hard limit per granule
  5354. max_bits = LameInternalFlags.MAX_BITS_PER_GRANULE;
  5355. }
  5356. for (bits = 0, ch = 0; ch < gfc.channels_out; ++ch) {
  5357. /******************************************************************
  5358. * allocate bits for each channel
  5359. ******************************************************************/
  5360. targ_bits[ch] = Math.min(LameInternalFlags.MAX_BITS_PER_CHANNEL,
  5361. tbits / gfc.channels_out);
  5362. add_bits[ch] = 0 | (targ_bits[ch] * pe[gr][ch] / 700.0 - targ_bits[ch]);
  5363. /* at most increase bits by 1.5*average */
  5364. if (add_bits[ch] > mean_bits * 3 / 4)
  5365. add_bits[ch] = mean_bits * 3 / 4;
  5366. if (add_bits[ch] < 0)
  5367. add_bits[ch] = 0;
  5368. if (add_bits[ch] + targ_bits[ch] > LameInternalFlags.MAX_BITS_PER_CHANNEL)
  5369. add_bits[ch] = Math.max(0,
  5370. LameInternalFlags.MAX_BITS_PER_CHANNEL - targ_bits[ch]);
  5371. bits += add_bits[ch];
  5372. }
  5373. if (bits > extra_bits) {
  5374. for (ch = 0; ch < gfc.channels_out; ++ch) {
  5375. add_bits[ch] = extra_bits * add_bits[ch] / bits;
  5376. }
  5377. }
  5378. for (ch = 0; ch < gfc.channels_out; ++ch) {
  5379. targ_bits[ch] += add_bits[ch];
  5380. extra_bits -= add_bits[ch];
  5381. }
  5382. for (bits = 0, ch = 0; ch < gfc.channels_out; ++ch) {
  5383. bits += targ_bits[ch];
  5384. }
  5385. if (bits > LameInternalFlags.MAX_BITS_PER_GRANULE) {
  5386. var sum = 0;
  5387. for (ch = 0; ch < gfc.channels_out; ++ch) {
  5388. targ_bits[ch] *= LameInternalFlags.MAX_BITS_PER_GRANULE;
  5389. targ_bits[ch] /= bits;
  5390. sum += targ_bits[ch];
  5391. }
  5392. }
  5393. return max_bits;
  5394. }
  5395. this.reduce_side = function (targ_bits, ms_ener_ratio, mean_bits, max_bits) {
  5396. /*
  5397. * ms_ener_ratio = 0: allocate 66/33 mid/side fac=.33 ms_ener_ratio =.5:
  5398. * allocate 50/50 mid/side fac= 0
  5399. */
  5400. /* 75/25 split is fac=.5 */
  5401. var fac = .33 * (.5 - ms_ener_ratio) / .5;
  5402. if (fac < 0)
  5403. fac = 0;
  5404. if (fac > .5)
  5405. fac = .5;
  5406. /* number of bits to move from side channel to mid channel */
  5407. /* move_bits = fac*targ_bits[1]; */
  5408. var move_bits = 0 | (fac * .5 * (targ_bits[0] + targ_bits[1]));
  5409. if (move_bits > LameInternalFlags.MAX_BITS_PER_CHANNEL - targ_bits[0]) {
  5410. move_bits = LameInternalFlags.MAX_BITS_PER_CHANNEL - targ_bits[0];
  5411. }
  5412. if (move_bits < 0)
  5413. move_bits = 0;
  5414. if (targ_bits[1] >= 125) {
  5415. /* dont reduce side channel below 125 bits */
  5416. if (targ_bits[1] - move_bits > 125) {
  5417. /* if mid channel already has 2x more than average, dont bother */
  5418. /* mean_bits = bits per granule (for both channels) */
  5419. if (targ_bits[0] < mean_bits)
  5420. targ_bits[0] += move_bits;
  5421. targ_bits[1] -= move_bits;
  5422. } else {
  5423. targ_bits[0] += targ_bits[1] - 125;
  5424. targ_bits[1] = 125;
  5425. }
  5426. }
  5427. move_bits = targ_bits[0] + targ_bits[1];
  5428. if (move_bits > max_bits) {
  5429. targ_bits[0] = (max_bits * targ_bits[0]) / move_bits;
  5430. targ_bits[1] = (max_bits * targ_bits[1]) / move_bits;
  5431. }
  5432. };
  5433. /**
  5434. * Robert Hegemann 2001-04-27:
  5435. * this adjusts the ATH, keeping the original noise floor
  5436. * affects the higher frequencies more than the lower ones
  5437. */
  5438. this.athAdjust = function (a, x, athFloor) {
  5439. /*
  5440. * work in progress
  5441. */
  5442. var o = 90.30873362;
  5443. var p = 94.82444863;
  5444. var u = Util.FAST_LOG10_X(x, 10.0);
  5445. var v = a * a;
  5446. var w = 0.0;
  5447. u -= athFloor;
  5448. /* undo scaling */
  5449. if (v > 1E-20)
  5450. w = 1. + Util.FAST_LOG10_X(v, 10.0 / o);
  5451. if (w < 0)
  5452. w = 0.;
  5453. u *= w;
  5454. u += athFloor + o - p;
  5455. /* redo scaling */
  5456. return Math.pow(10., 0.1 * u);
  5457. };
  5458. /**
  5459. * Calculate the allowed distortion for each scalefactor band, as determined
  5460. * by the psychoacoustic model. xmin(sb) = ratio(sb) * en(sb) / bw(sb)
  5461. *
  5462. * returns number of sfb's with energy > ATH
  5463. */
  5464. this.calc_xmin = function (gfp, ratio, cod_info, pxmin) {
  5465. var pxminPos = 0;
  5466. var gfc = gfp.internal_flags;
  5467. var gsfb, j = 0, ath_over = 0;
  5468. var ATH = gfc.ATH;
  5469. var xr = cod_info.xr;
  5470. var enable_athaa_fix = (gfp.VBR == VbrMode.vbr_mtrh) ? 1 : 0;
  5471. var masking_lower = gfc.masking_lower;
  5472. if (gfp.VBR == VbrMode.vbr_mtrh || gfp.VBR == VbrMode.vbr_mt) {
  5473. /* was already done in PSY-Model */
  5474. masking_lower = 1.0;
  5475. }
  5476. for (gsfb = 0; gsfb < cod_info.psy_lmax; gsfb++) {
  5477. var en0, xmin;
  5478. var rh1, rh2;
  5479. var width, l;
  5480. if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh)
  5481. xmin = athAdjust(ATH.adjust, ATH.l[gsfb], ATH.floor);
  5482. else
  5483. xmin = ATH.adjust * ATH.l[gsfb];
  5484. width = cod_info.width[gsfb];
  5485. rh1 = xmin / width;
  5486. rh2 = DBL_EPSILON;
  5487. l = width >> 1;
  5488. en0 = 0.0;
  5489. do {
  5490. var xa, xb;
  5491. xa = xr[j] * xr[j];
  5492. en0 += xa;
  5493. rh2 += (xa < rh1) ? xa : rh1;
  5494. j++;
  5495. xb = xr[j] * xr[j];
  5496. en0 += xb;
  5497. rh2 += (xb < rh1) ? xb : rh1;
  5498. j++;
  5499. } while (--l > 0);
  5500. if (en0 > xmin)
  5501. ath_over++;
  5502. if (gsfb == Encoder.SBPSY_l) {
  5503. var x = xmin * gfc.nsPsy.longfact[gsfb];
  5504. if (rh2 < x) {
  5505. rh2 = x;
  5506. }
  5507. }
  5508. if (enable_athaa_fix != 0) {
  5509. xmin = rh2;
  5510. }
  5511. if (!gfp.ATHonly) {
  5512. var e = ratio.en.l[gsfb];
  5513. if (e > 0.0) {
  5514. var x;
  5515. x = en0 * ratio.thm.l[gsfb] * masking_lower / e;
  5516. if (enable_athaa_fix != 0)
  5517. x *= gfc.nsPsy.longfact[gsfb];
  5518. if (xmin < x)
  5519. xmin = x;
  5520. }
  5521. }
  5522. if (enable_athaa_fix != 0)
  5523. pxmin[pxminPos++] = xmin;
  5524. else
  5525. pxmin[pxminPos++] = xmin * gfc.nsPsy.longfact[gsfb];
  5526. }
  5527. /* end of long block loop */
  5528. /* use this function to determine the highest non-zero coeff */
  5529. var max_nonzero = 575;
  5530. if (cod_info.block_type != Encoder.SHORT_TYPE) {
  5531. // NORM, START or STOP type, but not SHORT
  5532. var k = 576;
  5533. while (k-- != 0 && BitStream.EQ(xr[k], 0)) {
  5534. max_nonzero = k;
  5535. }
  5536. }
  5537. cod_info.max_nonzero_coeff = max_nonzero;
  5538. for (var sfb = cod_info.sfb_smin; gsfb < cod_info.psymax; sfb++, gsfb += 3) {
  5539. var width, b;
  5540. var tmpATH;
  5541. if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh)
  5542. tmpATH = athAdjust(ATH.adjust, ATH.s[sfb], ATH.floor);
  5543. else
  5544. tmpATH = ATH.adjust * ATH.s[sfb];
  5545. width = cod_info.width[gsfb];
  5546. for (b = 0; b < 3; b++) {
  5547. var en0 = 0.0, xmin;
  5548. var rh1, rh2;
  5549. var l = width >> 1;
  5550. rh1 = tmpATH / width;
  5551. rh2 = DBL_EPSILON;
  5552. do {
  5553. var xa, xb;
  5554. xa = xr[j] * xr[j];
  5555. en0 += xa;
  5556. rh2 += (xa < rh1) ? xa : rh1;
  5557. j++;
  5558. xb = xr[j] * xr[j];
  5559. en0 += xb;
  5560. rh2 += (xb < rh1) ? xb : rh1;
  5561. j++;
  5562. } while (--l > 0);
  5563. if (en0 > tmpATH)
  5564. ath_over++;
  5565. if (sfb == Encoder.SBPSY_s) {
  5566. var x = tmpATH * gfc.nsPsy.shortfact[sfb];
  5567. if (rh2 < x) {
  5568. rh2 = x;
  5569. }
  5570. }
  5571. if (enable_athaa_fix != 0)
  5572. xmin = rh2;
  5573. else
  5574. xmin = tmpATH;
  5575. if (!gfp.ATHonly && !gfp.ATHshort) {
  5576. var e = ratio.en.s[sfb][b];
  5577. if (e > 0.0) {
  5578. var x;
  5579. x = en0 * ratio.thm.s[sfb][b] * masking_lower / e;
  5580. if (enable_athaa_fix != 0)
  5581. x *= gfc.nsPsy.shortfact[sfb];
  5582. if (xmin < x)
  5583. xmin = x;
  5584. }
  5585. }
  5586. if (enable_athaa_fix != 0)
  5587. pxmin[pxminPos++] = xmin;
  5588. else
  5589. pxmin[pxminPos++] = xmin * gfc.nsPsy.shortfact[sfb];
  5590. }
  5591. /* b */
  5592. if (gfp.useTemporal) {
  5593. if (pxmin[pxminPos - 3] > pxmin[pxminPos - 3 + 1])
  5594. pxmin[pxminPos - 3 + 1] += (pxmin[pxminPos - 3] - pxmin[pxminPos - 3 + 1])
  5595. * gfc.decay;
  5596. if (pxmin[pxminPos - 3 + 1] > pxmin[pxminPos - 3 + 2])
  5597. pxmin[pxminPos - 3 + 2] += (pxmin[pxminPos - 3 + 1] - pxmin[pxminPos - 3 + 2])
  5598. * gfc.decay;
  5599. }
  5600. }
  5601. /* end of short block sfb loop */
  5602. return ath_over;
  5603. };
  5604. function StartLine(j) {
  5605. this.s = j;
  5606. }
  5607. this.calc_noise_core = function (cod_info, startline, l, step) {
  5608. var noise = 0;
  5609. var j = startline.s;
  5610. var ix = cod_info.l3_enc;
  5611. if (j > cod_info.count1) {
  5612. while ((l--) != 0) {
  5613. var temp;
  5614. temp = cod_info.xr[j];
  5615. j++;
  5616. noise += temp * temp;
  5617. temp = cod_info.xr[j];
  5618. j++;
  5619. noise += temp * temp;
  5620. }
  5621. } else if (j > cod_info.big_values) {
  5622. var ix01 = new_float(2);
  5623. ix01[0] = 0;
  5624. ix01[1] = step;
  5625. while ((l--) != 0) {
  5626. var temp;
  5627. temp = Math.abs(cod_info.xr[j]) - ix01[ix[j]];
  5628. j++;
  5629. noise += temp * temp;
  5630. temp = Math.abs(cod_info.xr[j]) - ix01[ix[j]];
  5631. j++;
  5632. noise += temp * temp;
  5633. }
  5634. } else {
  5635. while ((l--) != 0) {
  5636. var temp;
  5637. temp = Math.abs(cod_info.xr[j]) - pow43[ix[j]] * step;
  5638. j++;
  5639. noise += temp * temp;
  5640. temp = Math.abs(cod_info.xr[j]) - pow43[ix[j]] * step;
  5641. j++;
  5642. noise += temp * temp;
  5643. }
  5644. }
  5645. startline.s = j;
  5646. return noise;
  5647. }
  5648. /**
  5649. * <PRE>
  5650. * -oo dB => -1.00
  5651. * - 6 dB => -0.97
  5652. * - 3 dB => -0.80
  5653. * - 2 dB => -0.64
  5654. * - 1 dB => -0.38
  5655. * 0 dB => 0.00
  5656. * + 1 dB => +0.49
  5657. * + 2 dB => +1.06
  5658. * + 3 dB => +1.68
  5659. * + 6 dB => +3.69
  5660. * +10 dB => +6.45
  5661. * </PRE>
  5662. */
  5663. this.calc_noise = function (cod_info, l3_xmin, distort, res, prev_noise) {
  5664. var distortPos = 0;
  5665. var l3_xminPos = 0;
  5666. var sfb, l, over = 0;
  5667. var over_noise_db = 0;
  5668. /* 0 dB relative to masking */
  5669. var tot_noise_db = 0;
  5670. /* -200 dB relative to masking */
  5671. var max_noise = -20.0;
  5672. var j = 0;
  5673. var scalefac = cod_info.scalefac;
  5674. var scalefacPos = 0;
  5675. res.over_SSD = 0;
  5676. for (sfb = 0; sfb < cod_info.psymax; sfb++) {
  5677. var s = cod_info.global_gain
  5678. - (((scalefac[scalefacPos++]) + (cod_info.preflag != 0 ? pretab[sfb]
  5679. : 0)) << (cod_info.scalefac_scale + 1))
  5680. - cod_info.subblock_gain[cod_info.window[sfb]] * 8;
  5681. var noise = 0.0;
  5682. if (prev_noise != null && (prev_noise.step[sfb] == s)) {
  5683. /* use previously computed values */
  5684. noise = prev_noise.noise[sfb];
  5685. j += cod_info.width[sfb];
  5686. distort[distortPos++] = noise / l3_xmin[l3_xminPos++];
  5687. noise = prev_noise.noise_log[sfb];
  5688. } else {
  5689. var step = POW20(s);
  5690. l = cod_info.width[sfb] >> 1;
  5691. if ((j + cod_info.width[sfb]) > cod_info.max_nonzero_coeff) {
  5692. var usefullsize;
  5693. usefullsize = cod_info.max_nonzero_coeff - j + 1;
  5694. if (usefullsize > 0)
  5695. l = usefullsize >> 1;
  5696. else
  5697. l = 0;
  5698. }
  5699. var sl = new StartLine(j);
  5700. noise = this.calc_noise_core(cod_info, sl, l, step);
  5701. j = sl.s;
  5702. if (prev_noise != null) {
  5703. /* save noise values */
  5704. prev_noise.step[sfb] = s;
  5705. prev_noise.noise[sfb] = noise;
  5706. }
  5707. noise = distort[distortPos++] = noise / l3_xmin[l3_xminPos++];
  5708. /* multiplying here is adding in dB, but can overflow */
  5709. noise = Util.FAST_LOG10(Math.max(noise, 1E-20));
  5710. if (prev_noise != null) {
  5711. /* save noise values */
  5712. prev_noise.noise_log[sfb] = noise;
  5713. }
  5714. }
  5715. if (prev_noise != null) {
  5716. /* save noise values */
  5717. prev_noise.global_gain = cod_info.global_gain;
  5718. }
  5719. tot_noise_db += noise;
  5720. if (noise > 0.0) {
  5721. var tmp;
  5722. tmp = Math.max(0 | (noise * 10 + .5), 1);
  5723. res.over_SSD += tmp * tmp;
  5724. over++;
  5725. /* multiplying here is adding in dB -but can overflow */
  5726. /* over_noise *= noise; */
  5727. over_noise_db += noise;
  5728. }
  5729. max_noise = Math.max(max_noise, noise);
  5730. }
  5731. res.over_count = over;
  5732. res.tot_noise = tot_noise_db;
  5733. res.over_noise = over_noise_db;
  5734. res.max_noise = max_noise;
  5735. return over;
  5736. }
  5737. /**
  5738. * updates plotting data
  5739. *
  5740. * Mark Taylor 2000-??-??
  5741. *
  5742. * Robert Hegemann: moved noise/distortion calc into it
  5743. */
  5744. this.set_pinfo = function (gfp, cod_info, ratio, gr, ch) {
  5745. var gfc = gfp.internal_flags;
  5746. var sfb, sfb2;
  5747. var l;
  5748. var en0, en1;
  5749. var ifqstep = (cod_info.scalefac_scale == 0) ? .5 : 1.0;
  5750. var scalefac = cod_info.scalefac;
  5751. var l3_xmin = new_float(L3Side.SFBMAX);
  5752. var xfsf = new_float(L3Side.SFBMAX);
  5753. var noise = new CalcNoiseResult();
  5754. calc_xmin(gfp, ratio, cod_info, l3_xmin);
  5755. calc_noise(cod_info, l3_xmin, xfsf, noise, null);
  5756. var j = 0;
  5757. sfb2 = cod_info.sfb_lmax;
  5758. if (cod_info.block_type != Encoder.SHORT_TYPE
  5759. && 0 == cod_info.mixed_block_flag)
  5760. sfb2 = 22;
  5761. for (sfb = 0; sfb < sfb2; sfb++) {
  5762. var start = gfc.scalefac_band.l[sfb];
  5763. var end = gfc.scalefac_band.l[sfb + 1];
  5764. var bw = end - start;
  5765. for (en0 = 0.0; j < end; j++)
  5766. en0 += cod_info.xr[j] * cod_info.xr[j];
  5767. en0 /= bw;
  5768. /* convert to MDCT units */
  5769. /* scaling so it shows up on FFT plot */
  5770. en1 = 1e15;
  5771. gfc.pinfo.en[gr][ch][sfb] = en1 * en0;
  5772. gfc.pinfo.xfsf[gr][ch][sfb] = en1 * l3_xmin[sfb] * xfsf[sfb] / bw;
  5773. if (ratio.en.l[sfb] > 0 && !gfp.ATHonly)
  5774. en0 = en0 / ratio.en.l[sfb];
  5775. else
  5776. en0 = 0.0;
  5777. gfc.pinfo.thr[gr][ch][sfb] = en1
  5778. * Math.max(en0 * ratio.thm.l[sfb], gfc.ATH.l[sfb]);
  5779. /* there is no scalefactor bands >= SBPSY_l */
  5780. gfc.pinfo.LAMEsfb[gr][ch][sfb] = 0;
  5781. if (cod_info.preflag != 0 && sfb >= 11)
  5782. gfc.pinfo.LAMEsfb[gr][ch][sfb] = -ifqstep * pretab[sfb];
  5783. if (sfb < Encoder.SBPSY_l) {
  5784. /* scfsi should be decoded by caller side */
  5785. gfc.pinfo.LAMEsfb[gr][ch][sfb] -= ifqstep * scalefac[sfb];
  5786. }
  5787. }
  5788. /* for sfb */
  5789. if (cod_info.block_type == Encoder.SHORT_TYPE) {
  5790. sfb2 = sfb;
  5791. for (sfb = cod_info.sfb_smin; sfb < Encoder.SBMAX_s; sfb++) {
  5792. var start = gfc.scalefac_band.s[sfb];
  5793. var end = gfc.scalefac_band.s[sfb + 1];
  5794. var bw = end - start;
  5795. for (var i = 0; i < 3; i++) {
  5796. for (en0 = 0.0, l = start; l < end; l++) {
  5797. en0 += cod_info.xr[j] * cod_info.xr[j];
  5798. j++;
  5799. }
  5800. en0 = Math.max(en0 / bw, 1e-20);
  5801. /* convert to MDCT units */
  5802. /* scaling so it shows up on FFT plot */
  5803. en1 = 1e15;
  5804. gfc.pinfo.en_s[gr][ch][3 * sfb + i] = en1 * en0;
  5805. gfc.pinfo.xfsf_s[gr][ch][3 * sfb + i] = en1 * l3_xmin[sfb2]
  5806. * xfsf[sfb2] / bw;
  5807. if (ratio.en.s[sfb][i] > 0)
  5808. en0 = en0 / ratio.en.s[sfb][i];
  5809. else
  5810. en0 = 0.0;
  5811. if (gfp.ATHonly || gfp.ATHshort)
  5812. en0 = 0;
  5813. gfc.pinfo.thr_s[gr][ch][3 * sfb + i] = en1
  5814. * Math.max(en0 * ratio.thm.s[sfb][i],
  5815. gfc.ATH.s[sfb]);
  5816. /* there is no scalefactor bands >= SBPSY_s */
  5817. gfc.pinfo.LAMEsfb_s[gr][ch][3 * sfb + i] = -2.0
  5818. * cod_info.subblock_gain[i];
  5819. if (sfb < Encoder.SBPSY_s) {
  5820. gfc.pinfo.LAMEsfb_s[gr][ch][3 * sfb + i] -= ifqstep
  5821. * scalefac[sfb2];
  5822. }
  5823. sfb2++;
  5824. }
  5825. }
  5826. }
  5827. /* block type short */
  5828. gfc.pinfo.LAMEqss[gr][ch] = cod_info.global_gain;
  5829. gfc.pinfo.LAMEmainbits[gr][ch] = cod_info.part2_3_length
  5830. + cod_info.part2_length;
  5831. gfc.pinfo.LAMEsfbits[gr][ch] = cod_info.part2_length;
  5832. gfc.pinfo.over[gr][ch] = noise.over_count;
  5833. gfc.pinfo.max_noise[gr][ch] = noise.max_noise * 10.0;
  5834. gfc.pinfo.over_noise[gr][ch] = noise.over_noise * 10.0;
  5835. gfc.pinfo.tot_noise[gr][ch] = noise.tot_noise * 10.0;
  5836. gfc.pinfo.over_SSD[gr][ch] = noise.over_SSD;
  5837. }
  5838. /**
  5839. * updates plotting data for a whole frame
  5840. *
  5841. * Robert Hegemann 2000-10-21
  5842. */
  5843. function set_frame_pinfo(gfp, ratio) {
  5844. var gfc = gfp.internal_flags;
  5845. gfc.masking_lower = 1.0;
  5846. /*
  5847. * for every granule and channel patch l3_enc and set info
  5848. */
  5849. for (var gr = 0; gr < gfc.mode_gr; gr++) {
  5850. for (var ch = 0; ch < gfc.channels_out; ch++) {
  5851. var cod_info = gfc.l3_side.tt[gr][ch];
  5852. var scalefac_sav = new_int(L3Side.SFBMAX);
  5853. System.arraycopy(cod_info.scalefac, 0, scalefac_sav, 0,
  5854. scalefac_sav.length);
  5855. /*
  5856. * reconstruct the scalefactors in case SCFSI was used
  5857. */
  5858. if (gr == 1) {
  5859. var sfb;
  5860. for (sfb = 0; sfb < cod_info.sfb_lmax; sfb++) {
  5861. if (cod_info.scalefac[sfb] < 0) /* scfsi */
  5862. cod_info.scalefac[sfb] = gfc.l3_side.tt[0][ch].scalefac[sfb];
  5863. }
  5864. }
  5865. set_pinfo(gfp, cod_info, ratio[gr][ch], gr, ch);
  5866. System.arraycopy(scalefac_sav, 0, cod_info.scalefac, 0,
  5867. scalefac_sav.length);
  5868. }
  5869. /* for ch */
  5870. }
  5871. /* for gr */
  5872. }
  5873. }
  5874. function CalcNoiseData() {
  5875. this.global_gain = 0;
  5876. this.sfb_count1 = 0;
  5877. this.step = new_int(39);
  5878. this.noise = new_float(39);
  5879. this.noise_log = new_float(39);
  5880. }
  5881. //package mp3;
  5882. function GrInfo() {
  5883. //float xr[] = new float[576];
  5884. this.xr = new_float(576);
  5885. //int l3_enc[] = new int[576];
  5886. this.l3_enc = new_int(576);
  5887. //int scalefac[] = new int[L3Side.SFBMAX];
  5888. this.scalefac = new_int(L3Side.SFBMAX);
  5889. this.xrpow_max = 0.;
  5890. this.part2_3_length = 0;
  5891. this.big_values = 0;
  5892. this.count1 = 0;
  5893. this.global_gain = 0;
  5894. this.scalefac_compress = 0;
  5895. this.block_type = 0;
  5896. this.mixed_block_flag = 0;
  5897. this.table_select = new_int(3);
  5898. this.subblock_gain = new_int(3 + 1);
  5899. this.region0_count = 0;
  5900. this.region1_count = 0;
  5901. this.preflag = 0;
  5902. this.scalefac_scale = 0;
  5903. this.count1table_select = 0;
  5904. this.part2_length = 0;
  5905. this.sfb_lmax = 0;
  5906. this.sfb_smin = 0;
  5907. this.psy_lmax = 0;
  5908. this.sfbmax = 0;
  5909. this.psymax = 0;
  5910. this.sfbdivide = 0;
  5911. this.width = new_int(L3Side.SFBMAX);
  5912. this.window = new_int(L3Side.SFBMAX);
  5913. this.count1bits = 0;
  5914. /**
  5915. * added for LSF
  5916. */
  5917. this.sfb_partition_table = null;
  5918. this.slen = new_int(4);
  5919. this.max_nonzero_coeff = 0;
  5920. var self = this;
  5921. function clone_int(array) {
  5922. return new Int32Array(array);
  5923. }
  5924. function clone_float(array) {
  5925. return new Float32Array(array);
  5926. }
  5927. this.assign = function (other) {
  5928. self.xr = clone_float(other.xr); //.slice(0); //clone();
  5929. self.l3_enc = clone_int(other.l3_enc); //.slice(0); //clone();
  5930. self.scalefac = clone_int(other.scalefac);//.slice(0); //clone();
  5931. self.xrpow_max = other.xrpow_max;
  5932. self.part2_3_length = other.part2_3_length;
  5933. self.big_values = other.big_values;
  5934. self.count1 = other.count1;
  5935. self.global_gain = other.global_gain;
  5936. self.scalefac_compress = other.scalefac_compress;
  5937. self.block_type = other.block_type;
  5938. self.mixed_block_flag = other.mixed_block_flag;
  5939. self.table_select = clone_int(other.table_select);//.slice(0); //clone();
  5940. self.subblock_gain = clone_int(other.subblock_gain); //.slice(0); //.clone();
  5941. self.region0_count = other.region0_count;
  5942. self.region1_count = other.region1_count;
  5943. self.preflag = other.preflag;
  5944. self.scalefac_scale = other.scalefac_scale;
  5945. self.count1table_select = other.count1table_select;
  5946. self.part2_length = other.part2_length;
  5947. self.sfb_lmax = other.sfb_lmax;
  5948. self.sfb_smin = other.sfb_smin;
  5949. self.psy_lmax = other.psy_lmax;
  5950. self.sfbmax = other.sfbmax;
  5951. self.psymax = other.psymax;
  5952. self.sfbdivide = other.sfbdivide;
  5953. self.width = clone_int(other.width); //.slice(0); //.clone();
  5954. self.window = clone_int(other.window); //.slice(0); //.clone();
  5955. self.count1bits = other.count1bits;
  5956. self.sfb_partition_table = other.sfb_partition_table.slice(0); //.clone();
  5957. self.slen = clone_int(other.slen); //.slice(0); //.clone();
  5958. self.max_nonzero_coeff = other.max_nonzero_coeff;
  5959. }
  5960. }
  5961. var L3Side = {};
  5962. /**
  5963. * max scalefactor band, max(SBMAX_l, SBMAX_s*3, (SBMAX_s-3)*3+8)
  5964. */
  5965. L3Side.SFBMAX = (Encoder.SBMAX_s * 3);
  5966. /*
  5967. * MP3 quantization
  5968. *
  5969. * Copyright (c) 1999-2000 Mark Taylor
  5970. * Copyright (c) 1999-2003 Takehiro Tominaga
  5971. * Copyright (c) 2000-2007 Robert Hegemann
  5972. * Copyright (c) 2001-2005 Gabriel Bouvigne
  5973. *
  5974. * This library is free software; you can redistribute it and/or
  5975. * modify it under the terms of the GNU Lesser General Public
  5976. * License as published by the Free Software Foundation; either
  5977. * version 2 of the License, or (at your option) any later version.
  5978. *
  5979. * This library is distributed in the hope that it will be useful,
  5980. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5981. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5982. * Library General Public License for more details.
  5983. *
  5984. * You should have received a copy of the GNU Lesser General Public
  5985. * License along with this library; if not, write to the
  5986. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5987. * Boston, MA 02111-1307, USA.
  5988. */
  5989. /* $Id: Quantize.java,v 1.24 2011/05/24 20:48:06 kenchis Exp $ */
  5990. //package mp3;
  5991. //import java.util.Arrays;
  5992. function Quantize() {
  5993. var bs;
  5994. this.rv = null;
  5995. var rv;
  5996. this.qupvt = null;
  5997. var qupvt;
  5998. var vbr = new VBRQuantize();
  5999. var tk;
  6000. this.setModules = function (_bs, _rv, _qupvt, _tk) {
  6001. bs = _bs;
  6002. rv = _rv;
  6003. this.rv = _rv;
  6004. qupvt = _qupvt;
  6005. this.qupvt = _qupvt;
  6006. tk = _tk;
  6007. vbr.setModules(qupvt, tk);
  6008. }
  6009. /**
  6010. * convert from L/R <. Mid/Side
  6011. */
  6012. this.ms_convert = function (l3_side, gr) {
  6013. for (var i = 0; i < 576; ++i) {
  6014. var l = l3_side.tt[gr][0].xr[i];
  6015. var r = l3_side.tt[gr][1].xr[i];
  6016. l3_side.tt[gr][0].xr[i] = (l + r) * (Util.SQRT2 * 0.5);
  6017. l3_side.tt[gr][1].xr[i] = (l - r) * (Util.SQRT2 * 0.5);
  6018. }
  6019. };
  6020. /**
  6021. * mt 6/99
  6022. *
  6023. * initializes cod_info, scalefac and xrpow
  6024. *
  6025. * returns 0 if all energies in xr are zero, else 1
  6026. */
  6027. function init_xrpow_core(cod_info, xrpow, upper, sum) {
  6028. sum = 0;
  6029. for (var i = 0; i <= upper; ++i) {
  6030. var tmp = Math.abs(cod_info.xr[i]);
  6031. sum += tmp;
  6032. xrpow[i] = Math.sqrt(tmp * Math.sqrt(tmp));
  6033. if (xrpow[i] > cod_info.xrpow_max)
  6034. cod_info.xrpow_max = xrpow[i];
  6035. }
  6036. return sum;
  6037. }
  6038. this.init_xrpow = function (gfc, cod_info, xrpow) {
  6039. var sum = 0;
  6040. var upper = 0 | cod_info.max_nonzero_coeff;
  6041. cod_info.xrpow_max = 0;
  6042. /*
  6043. * check if there is some energy we have to quantize and calculate xrpow
  6044. * matching our fresh scalefactors
  6045. */
  6046. Arrays.fill(xrpow, upper, 576, 0);
  6047. sum = init_xrpow_core(cod_info, xrpow, upper, sum);
  6048. /*
  6049. * return 1 if we have something to quantize, else 0
  6050. */
  6051. if (sum > 1E-20) {
  6052. var j = 0;
  6053. if ((gfc.substep_shaping & 2) != 0)
  6054. j = 1;
  6055. for (var i = 0; i < cod_info.psymax; i++)
  6056. gfc.pseudohalf[i] = j;
  6057. return true;
  6058. }
  6059. Arrays.fill(cod_info.l3_enc, 0, 576, 0);
  6060. return false;
  6061. }
  6062. /**
  6063. * Gabriel Bouvigne feb/apr 2003<BR>
  6064. * Analog silence detection in partitionned sfb21 or sfb12 for short blocks
  6065. *
  6066. * From top to bottom of sfb, changes to 0 coeffs which are below ath. It
  6067. * stops on the first coeff higher than ath.
  6068. */
  6069. function psfb21_analogsilence(gfc, cod_info) {
  6070. var ath = gfc.ATH;
  6071. var xr = cod_info.xr;
  6072. if (cod_info.block_type != Encoder.SHORT_TYPE) {
  6073. /* NORM, START or STOP type, but not SHORT blocks */
  6074. var stop = false;
  6075. for (var gsfb = Encoder.PSFB21 - 1; gsfb >= 0 && !stop; gsfb--) {
  6076. var start = gfc.scalefac_band.psfb21[gsfb];
  6077. var end = gfc.scalefac_band.psfb21[gsfb + 1];
  6078. var ath21 = qupvt.athAdjust(ath.adjust, ath.psfb21[gsfb],
  6079. ath.floor);
  6080. if (gfc.nsPsy.longfact[21] > 1e-12)
  6081. ath21 *= gfc.nsPsy.longfact[21];
  6082. for (var j = end - 1; j >= start; j--) {
  6083. if (Math.abs(xr[j]) < ath21)
  6084. xr[j] = 0;
  6085. else {
  6086. stop = true;
  6087. break;
  6088. }
  6089. }
  6090. }
  6091. } else {
  6092. /* note: short blocks coeffs are reordered */
  6093. for (var block = 0; block < 3; block++) {
  6094. var stop = false;
  6095. for (var gsfb = Encoder.PSFB12 - 1; gsfb >= 0 && !stop; gsfb--) {
  6096. var start = gfc.scalefac_band.s[12]
  6097. * 3
  6098. + (gfc.scalefac_band.s[13] - gfc.scalefac_band.s[12])
  6099. * block
  6100. + (gfc.scalefac_band.psfb12[gsfb] - gfc.scalefac_band.psfb12[0]);
  6101. var end = start
  6102. + (gfc.scalefac_band.psfb12[gsfb + 1] - gfc.scalefac_band.psfb12[gsfb]);
  6103. var ath12 = qupvt.athAdjust(ath.adjust, ath.psfb12[gsfb],
  6104. ath.floor);
  6105. if (gfc.nsPsy.shortfact[12] > 1e-12)
  6106. ath12 *= gfc.nsPsy.shortfact[12];
  6107. for (var j = end - 1; j >= start; j--) {
  6108. if (Math.abs(xr[j]) < ath12)
  6109. xr[j] = 0;
  6110. else {
  6111. stop = true;
  6112. break;
  6113. }
  6114. }
  6115. }
  6116. }
  6117. }
  6118. }
  6119. this.init_outer_loop = function (gfc, cod_info) {
  6120. /*
  6121. * initialize fresh cod_info
  6122. */
  6123. cod_info.part2_3_length = 0;
  6124. cod_info.big_values = 0;
  6125. cod_info.count1 = 0;
  6126. cod_info.global_gain = 210;
  6127. cod_info.scalefac_compress = 0;
  6128. /* mixed_block_flag, block_type was set in psymodel.c */
  6129. cod_info.table_select[0] = 0;
  6130. cod_info.table_select[1] = 0;
  6131. cod_info.table_select[2] = 0;
  6132. cod_info.subblock_gain[0] = 0;
  6133. cod_info.subblock_gain[1] = 0;
  6134. cod_info.subblock_gain[2] = 0;
  6135. cod_info.subblock_gain[3] = 0;
  6136. /* this one is always 0 */
  6137. cod_info.region0_count = 0;
  6138. cod_info.region1_count = 0;
  6139. cod_info.preflag = 0;
  6140. cod_info.scalefac_scale = 0;
  6141. cod_info.count1table_select = 0;
  6142. cod_info.part2_length = 0;
  6143. cod_info.sfb_lmax = Encoder.SBPSY_l;
  6144. cod_info.sfb_smin = Encoder.SBPSY_s;
  6145. cod_info.psy_lmax = gfc.sfb21_extra ? Encoder.SBMAX_l : Encoder.SBPSY_l;
  6146. cod_info.psymax = cod_info.psy_lmax;
  6147. cod_info.sfbmax = cod_info.sfb_lmax;
  6148. cod_info.sfbdivide = 11;
  6149. for (var sfb = 0; sfb < Encoder.SBMAX_l; sfb++) {
  6150. cod_info.width[sfb] = gfc.scalefac_band.l[sfb + 1]
  6151. - gfc.scalefac_band.l[sfb];
  6152. /* which is always 0. */
  6153. cod_info.window[sfb] = 3;
  6154. }
  6155. if (cod_info.block_type == Encoder.SHORT_TYPE) {
  6156. var ixwork = new_float(576);
  6157. cod_info.sfb_smin = 0;
  6158. cod_info.sfb_lmax = 0;
  6159. if (cod_info.mixed_block_flag != 0) {
  6160. /*
  6161. * MPEG-1: sfbs 0-7 long block, 3-12 short blocks MPEG-2(.5):
  6162. * sfbs 0-5 long block, 3-12 short blocks
  6163. */
  6164. cod_info.sfb_smin = 3;
  6165. cod_info.sfb_lmax = gfc.mode_gr * 2 + 4;
  6166. }
  6167. cod_info.psymax = cod_info.sfb_lmax
  6168. + 3
  6169. * ((gfc.sfb21_extra ? Encoder.SBMAX_s : Encoder.SBPSY_s) - cod_info.sfb_smin);
  6170. cod_info.sfbmax = cod_info.sfb_lmax + 3
  6171. * (Encoder.SBPSY_s - cod_info.sfb_smin);
  6172. cod_info.sfbdivide = cod_info.sfbmax - 18;
  6173. cod_info.psy_lmax = cod_info.sfb_lmax;
  6174. /* re-order the short blocks, for more efficient encoding below */
  6175. /* By Takehiro TOMINAGA */
  6176. /*
  6177. * Within each scalefactor band, data is given for successive time
  6178. * windows, beginning with window 0 and ending with window 2. Within
  6179. * each window, the quantized values are then arranged in order of
  6180. * increasing frequency...
  6181. */
  6182. var ix = gfc.scalefac_band.l[cod_info.sfb_lmax];
  6183. System.arraycopy(cod_info.xr, 0, ixwork, 0, 576);
  6184. for (var sfb = cod_info.sfb_smin; sfb < Encoder.SBMAX_s; sfb++) {
  6185. var start = gfc.scalefac_band.s[sfb];
  6186. var end = gfc.scalefac_band.s[sfb + 1];
  6187. for (var window = 0; window < 3; window++) {
  6188. for (var l = start; l < end; l++) {
  6189. cod_info.xr[ix++] = ixwork[3 * l + window];
  6190. }
  6191. }
  6192. }
  6193. var j = cod_info.sfb_lmax;
  6194. for (var sfb = cod_info.sfb_smin; sfb < Encoder.SBMAX_s; sfb++) {
  6195. cod_info.width[j] = cod_info.width[j + 1] = cod_info.width[j + 2] = gfc.scalefac_band.s[sfb + 1]
  6196. - gfc.scalefac_band.s[sfb];
  6197. cod_info.window[j] = 0;
  6198. cod_info.window[j + 1] = 1;
  6199. cod_info.window[j + 2] = 2;
  6200. j += 3;
  6201. }
  6202. }
  6203. cod_info.count1bits = 0;
  6204. cod_info.sfb_partition_table = qupvt.nr_of_sfb_block[0][0];
  6205. cod_info.slen[0] = 0;
  6206. cod_info.slen[1] = 0;
  6207. cod_info.slen[2] = 0;
  6208. cod_info.slen[3] = 0;
  6209. cod_info.max_nonzero_coeff = 575;
  6210. /*
  6211. * fresh scalefactors are all zero
  6212. */
  6213. Arrays.fill(cod_info.scalefac, 0);
  6214. psfb21_analogsilence(gfc, cod_info);
  6215. };
  6216. function BinSearchDirection(ordinal) {
  6217. this.ordinal = ordinal;
  6218. }
  6219. BinSearchDirection.BINSEARCH_NONE = new BinSearchDirection(0);
  6220. BinSearchDirection.BINSEARCH_UP = new BinSearchDirection(1);
  6221. BinSearchDirection.BINSEARCH_DOWN = new BinSearchDirection(2);
  6222. /**
  6223. * author/date??
  6224. *
  6225. * binary step size search used by outer_loop to get a quantizer step size
  6226. * to start with
  6227. */
  6228. function bin_search_StepSize(gfc, cod_info, desired_rate, ch, xrpow) {
  6229. var nBits;
  6230. var CurrentStep = gfc.CurrentStep[ch];
  6231. var flagGoneOver = false;
  6232. var start = gfc.OldValue[ch];
  6233. var Direction = BinSearchDirection.BINSEARCH_NONE;
  6234. cod_info.global_gain = start;
  6235. desired_rate -= cod_info.part2_length;
  6236. for (; ;) {
  6237. var step;
  6238. nBits = tk.count_bits(gfc, xrpow, cod_info, null);
  6239. if (CurrentStep == 1 || nBits == desired_rate)
  6240. break;
  6241. /* nothing to adjust anymore */
  6242. if (nBits > desired_rate) {
  6243. /* increase Quantize_StepSize */
  6244. if (Direction == BinSearchDirection.BINSEARCH_DOWN)
  6245. flagGoneOver = true;
  6246. if (flagGoneOver)
  6247. CurrentStep /= 2;
  6248. Direction = BinSearchDirection.BINSEARCH_UP;
  6249. step = CurrentStep;
  6250. } else {
  6251. /* decrease Quantize_StepSize */
  6252. if (Direction == BinSearchDirection.BINSEARCH_UP)
  6253. flagGoneOver = true;
  6254. if (flagGoneOver)
  6255. CurrentStep /= 2;
  6256. Direction = BinSearchDirection.BINSEARCH_DOWN;
  6257. step = -CurrentStep;
  6258. }
  6259. cod_info.global_gain += step;
  6260. if (cod_info.global_gain < 0) {
  6261. cod_info.global_gain = 0;
  6262. flagGoneOver = true;
  6263. }
  6264. if (cod_info.global_gain > 255) {
  6265. cod_info.global_gain = 255;
  6266. flagGoneOver = true;
  6267. }
  6268. }
  6269. while (nBits > desired_rate && cod_info.global_gain < 255) {
  6270. cod_info.global_gain++;
  6271. nBits = tk.count_bits(gfc, xrpow, cod_info, null);
  6272. }
  6273. gfc.CurrentStep[ch] = (start - cod_info.global_gain >= 4) ? 4 : 2;
  6274. gfc.OldValue[ch] = cod_info.global_gain;
  6275. cod_info.part2_3_length = nBits;
  6276. return nBits;
  6277. }
  6278. this.trancate_smallspectrums = function (gfc, gi, l3_xmin, work) {
  6279. var distort = new_float(L3Side.SFBMAX);
  6280. if ((0 == (gfc.substep_shaping & 4) && gi.block_type == Encoder.SHORT_TYPE)
  6281. || (gfc.substep_shaping & 0x80) != 0)
  6282. return;
  6283. qupvt.calc_noise(gi, l3_xmin, distort, new CalcNoiseResult(), null);
  6284. for (var j = 0; j < 576; j++) {
  6285. var xr = 0.0;
  6286. if (gi.l3_enc[j] != 0)
  6287. xr = Math.abs(gi.xr[j]);
  6288. work[j] = xr;
  6289. }
  6290. var j = 0;
  6291. var sfb = 8;
  6292. if (gi.block_type == Encoder.SHORT_TYPE)
  6293. sfb = 6;
  6294. do {
  6295. var allowedNoise, trancateThreshold;
  6296. var nsame, start;
  6297. var width = gi.width[sfb];
  6298. j += width;
  6299. if (distort[sfb] >= 1.0)
  6300. continue;
  6301. Arrays.sort(work, j - width, width);
  6302. if (BitStream.EQ(work[j - 1], 0.0))
  6303. continue;
  6304. /* all zero sfb */
  6305. allowedNoise = (1.0 - distort[sfb]) * l3_xmin[sfb];
  6306. trancateThreshold = 0.0;
  6307. start = 0;
  6308. do {
  6309. var noise;
  6310. for (nsame = 1; start + nsame < width; nsame++)
  6311. if (BitStream.NEQ(work[start + j - width], work[start + j
  6312. + nsame - width]))
  6313. break;
  6314. noise = work[start + j - width] * work[start + j - width]
  6315. * nsame;
  6316. if (allowedNoise < noise) {
  6317. if (start != 0)
  6318. trancateThreshold = work[start + j - width - 1];
  6319. break;
  6320. }
  6321. allowedNoise -= noise;
  6322. start += nsame;
  6323. } while (start < width);
  6324. if (BitStream.EQ(trancateThreshold, 0.0))
  6325. continue;
  6326. do {
  6327. if (Math.abs(gi.xr[j - width]) <= trancateThreshold)
  6328. gi.l3_enc[j - width] = 0;
  6329. } while (--width > 0);
  6330. } while (++sfb < gi.psymax);
  6331. gi.part2_3_length = tk.noquant_count_bits(gfc, gi, null);
  6332. };
  6333. /**
  6334. * author/date??
  6335. *
  6336. * Function: Returns zero if there is a scalefac which has not been
  6337. * amplified. Otherwise it returns one.
  6338. */
  6339. function loop_break(cod_info) {
  6340. for (var sfb = 0; sfb < cod_info.sfbmax; sfb++)
  6341. if (cod_info.scalefac[sfb]
  6342. + cod_info.subblock_gain[cod_info.window[sfb]] == 0)
  6343. return false;
  6344. return true;
  6345. }
  6346. /* mt 5/99: Function: Improved calc_noise for a single channel */
  6347. function penalties(noise) {
  6348. return Util.FAST_LOG10((0.368 + 0.632 * noise * noise * noise));
  6349. }
  6350. /**
  6351. * author/date??
  6352. *
  6353. * several different codes to decide which quantization is better
  6354. */
  6355. function get_klemm_noise(distort, gi) {
  6356. var klemm_noise = 1E-37;
  6357. for (var sfb = 0; sfb < gi.psymax; sfb++)
  6358. klemm_noise += penalties(distort[sfb]);
  6359. return Math.max(1e-20, klemm_noise);
  6360. }
  6361. function quant_compare(quant_comp, best, calc, gi, distort) {
  6362. /**
  6363. * noise is given in decibels (dB) relative to masking thesholds.<BR>
  6364. *
  6365. * over_noise: ??? (the previous comment is fully wrong)<BR>
  6366. * tot_noise: ??? (the previous comment is fully wrong)<BR>
  6367. * max_noise: max quantization noise
  6368. */
  6369. var better;
  6370. switch (quant_comp) {
  6371. default:
  6372. case 9:
  6373. {
  6374. if (best.over_count > 0) {
  6375. /* there are distorted sfb */
  6376. better = calc.over_SSD <= best.over_SSD;
  6377. if (calc.over_SSD == best.over_SSD)
  6378. better = calc.bits < best.bits;
  6379. } else {
  6380. /* no distorted sfb */
  6381. better = ((calc.max_noise < 0) && ((calc.max_noise * 10 + calc.bits) <= (best.max_noise * 10 + best.bits)));
  6382. }
  6383. break;
  6384. }
  6385. case 0:
  6386. better = calc.over_count < best.over_count
  6387. || (calc.over_count == best.over_count && calc.over_noise < best.over_noise)
  6388. || (calc.over_count == best.over_count
  6389. && BitStream.EQ(calc.over_noise, best.over_noise) && calc.tot_noise < best.tot_noise);
  6390. break;
  6391. case 8:
  6392. calc.max_noise = get_klemm_noise(distort, gi);
  6393. //$FALL-THROUGH$
  6394. case 1:
  6395. better = calc.max_noise < best.max_noise;
  6396. break;
  6397. case 2:
  6398. better = calc.tot_noise < best.tot_noise;
  6399. break;
  6400. case 3:
  6401. better = (calc.tot_noise < best.tot_noise)
  6402. && (calc.max_noise < best.max_noise);
  6403. break;
  6404. case 4:
  6405. better = (calc.max_noise <= 0.0 && best.max_noise > 0.2)
  6406. || (calc.max_noise <= 0.0 && best.max_noise < 0.0
  6407. && best.max_noise > calc.max_noise - 0.2 && calc.tot_noise < best.tot_noise)
  6408. || (calc.max_noise <= 0.0 && best.max_noise > 0.0
  6409. && best.max_noise > calc.max_noise - 0.2 && calc.tot_noise < best.tot_noise
  6410. + best.over_noise)
  6411. || (calc.max_noise > 0.0 && best.max_noise > -0.05
  6412. && best.max_noise > calc.max_noise - 0.1 && calc.tot_noise
  6413. + calc.over_noise < best.tot_noise
  6414. + best.over_noise)
  6415. || (calc.max_noise > 0.0 && best.max_noise > -0.1
  6416. && best.max_noise > calc.max_noise - 0.15 && calc.tot_noise
  6417. + calc.over_noise + calc.over_noise < best.tot_noise
  6418. + best.over_noise + best.over_noise);
  6419. break;
  6420. case 5:
  6421. better = calc.over_noise < best.over_noise
  6422. || (BitStream.EQ(calc.over_noise, best.over_noise) && calc.tot_noise < best.tot_noise);
  6423. break;
  6424. case 6:
  6425. better = calc.over_noise < best.over_noise
  6426. || (BitStream.EQ(calc.over_noise, best.over_noise) && (calc.max_noise < best.max_noise || (BitStream
  6427. .EQ(calc.max_noise, best.max_noise) && calc.tot_noise <= best.tot_noise)));
  6428. break;
  6429. case 7:
  6430. better = calc.over_count < best.over_count
  6431. || calc.over_noise < best.over_noise;
  6432. break;
  6433. }
  6434. if (best.over_count == 0) {
  6435. /*
  6436. * If no distorted bands, only use this quantization if it is
  6437. * better, and if it uses less bits. Unfortunately, part2_3_length
  6438. * is sometimes a poor estimator of the final size at low bitrates.
  6439. */
  6440. better = better && calc.bits < best.bits;
  6441. }
  6442. return better;
  6443. }
  6444. /**
  6445. * author/date??
  6446. *
  6447. * <PRE>
  6448. * Amplify the scalefactor bands that violate the masking threshold.
  6449. * See ISO 11172-3 Section C.1.5.4.3.5
  6450. *
  6451. * distort[] = noise/masking
  6452. * distort[] > 1 ==> noise is not masked
  6453. * distort[] < 1 ==> noise is masked
  6454. * max_dist = maximum value of distort[]
  6455. *
  6456. * Three algorithms:
  6457. * noise_shaping_amp
  6458. * 0 Amplify all bands with distort[]>1.
  6459. *
  6460. * 1 Amplify all bands with distort[] >= max_dist^(.5);
  6461. * ( 50% in the db scale)
  6462. *
  6463. * 2 Amplify first band with distort[] >= max_dist;
  6464. *
  6465. *
  6466. * For algorithms 0 and 1, if max_dist < 1, then amplify all bands
  6467. * with distort[] >= .95*max_dist. This is to make sure we always
  6468. * amplify at least one band.
  6469. * </PRE>
  6470. */
  6471. function amp_scalefac_bands(gfp, cod_info, distort, xrpow, bRefine) {
  6472. var gfc = gfp.internal_flags;
  6473. var ifqstep34;
  6474. if (cod_info.scalefac_scale == 0) {
  6475. ifqstep34 = 1.29683955465100964055;
  6476. /* 2**(.75*.5) */
  6477. } else {
  6478. ifqstep34 = 1.68179283050742922612;
  6479. /* 2**(.75*1) */
  6480. }
  6481. /* compute maximum value of distort[] */
  6482. var trigger = 0;
  6483. for (var sfb = 0; sfb < cod_info.sfbmax; sfb++) {
  6484. if (trigger < distort[sfb])
  6485. trigger = distort[sfb];
  6486. }
  6487. var noise_shaping_amp = gfc.noise_shaping_amp;
  6488. if (noise_shaping_amp == 3) {
  6489. if (bRefine)
  6490. noise_shaping_amp = 2;
  6491. else
  6492. noise_shaping_amp = 1;
  6493. }
  6494. switch (noise_shaping_amp) {
  6495. case 2:
  6496. /* amplify exactly 1 band */
  6497. break;
  6498. case 1:
  6499. /* amplify bands within 50% of max (on db scale) */
  6500. if (trigger > 1.0)
  6501. trigger = Math.pow(trigger, .5);
  6502. else
  6503. trigger *= .95;
  6504. break;
  6505. case 0:
  6506. default:
  6507. /* ISO algorithm. amplify all bands with distort>1 */
  6508. if (trigger > 1.0)
  6509. trigger = 1.0;
  6510. else
  6511. trigger *= .95;
  6512. break;
  6513. }
  6514. var j = 0;
  6515. for (var sfb = 0; sfb < cod_info.sfbmax; sfb++) {
  6516. var width = cod_info.width[sfb];
  6517. var l;
  6518. j += width;
  6519. if (distort[sfb] < trigger)
  6520. continue;
  6521. if ((gfc.substep_shaping & 2) != 0) {
  6522. gfc.pseudohalf[sfb] = (0 == gfc.pseudohalf[sfb]) ? 1 : 0;
  6523. if (0 == gfc.pseudohalf[sfb] && gfc.noise_shaping_amp == 2)
  6524. return;
  6525. }
  6526. cod_info.scalefac[sfb]++;
  6527. for (l = -width; l < 0; l++) {
  6528. xrpow[j + l] *= ifqstep34;
  6529. if (xrpow[j + l] > cod_info.xrpow_max)
  6530. cod_info.xrpow_max = xrpow[j + l];
  6531. }
  6532. if (gfc.noise_shaping_amp == 2)
  6533. return;
  6534. }
  6535. }
  6536. /**
  6537. * Takehiro Tominaga 2000-xx-xx
  6538. *
  6539. * turns on scalefac scale and adjusts scalefactors
  6540. */
  6541. function inc_scalefac_scale(cod_info, xrpow) {
  6542. var ifqstep34 = 1.29683955465100964055;
  6543. var j = 0;
  6544. for (var sfb = 0; sfb < cod_info.sfbmax; sfb++) {
  6545. var width = cod_info.width[sfb];
  6546. var s = cod_info.scalefac[sfb];
  6547. if (cod_info.preflag != 0)
  6548. s += qupvt.pretab[sfb];
  6549. j += width;
  6550. if ((s & 1) != 0) {
  6551. s++;
  6552. for (var l = -width; l < 0; l++) {
  6553. xrpow[j + l] *= ifqstep34;
  6554. if (xrpow[j + l] > cod_info.xrpow_max)
  6555. cod_info.xrpow_max = xrpow[j + l];
  6556. }
  6557. }
  6558. cod_info.scalefac[sfb] = s >> 1;
  6559. }
  6560. cod_info.preflag = 0;
  6561. cod_info.scalefac_scale = 1;
  6562. }
  6563. /**
  6564. * Takehiro Tominaga 2000-xx-xx
  6565. *
  6566. * increases the subblock gain and adjusts scalefactors
  6567. */
  6568. function inc_subblock_gain(gfc, cod_info, xrpow) {
  6569. var sfb;
  6570. var scalefac = cod_info.scalefac;
  6571. /* subbloc_gain can't do anything in the long block region */
  6572. for (sfb = 0; sfb < cod_info.sfb_lmax; sfb++) {
  6573. if (scalefac[sfb] >= 16)
  6574. return true;
  6575. }
  6576. for (var window = 0; window < 3; window++) {
  6577. var s1 = 0;
  6578. var s2 = 0;
  6579. for (sfb = cod_info.sfb_lmax + window; sfb < cod_info.sfbdivide; sfb += 3) {
  6580. if (s1 < scalefac[sfb])
  6581. s1 = scalefac[sfb];
  6582. }
  6583. for (; sfb < cod_info.sfbmax; sfb += 3) {
  6584. if (s2 < scalefac[sfb])
  6585. s2 = scalefac[sfb];
  6586. }
  6587. if (s1 < 16 && s2 < 8)
  6588. continue;
  6589. if (cod_info.subblock_gain[window] >= 7)
  6590. return true;
  6591. /*
  6592. * even though there is no scalefactor for sfb12 subblock gain
  6593. * affects upper frequencies too, that's why we have to go up to
  6594. * SBMAX_s
  6595. */
  6596. cod_info.subblock_gain[window]++;
  6597. var j = gfc.scalefac_band.l[cod_info.sfb_lmax];
  6598. for (sfb = cod_info.sfb_lmax + window; sfb < cod_info.sfbmax; sfb += 3) {
  6599. var amp;
  6600. var width = cod_info.width[sfb];
  6601. var s = scalefac[sfb];
  6602. s = s - (4 >> cod_info.scalefac_scale);
  6603. if (s >= 0) {
  6604. scalefac[sfb] = s;
  6605. j += width * 3;
  6606. continue;
  6607. }
  6608. scalefac[sfb] = 0;
  6609. {
  6610. var gain = 210 + (s << (cod_info.scalefac_scale + 1));
  6611. amp = qupvt.IPOW20(gain);
  6612. }
  6613. j += width * (window + 1);
  6614. for (var l = -width; l < 0; l++) {
  6615. xrpow[j + l] *= amp;
  6616. if (xrpow[j + l] > cod_info.xrpow_max)
  6617. cod_info.xrpow_max = xrpow[j + l];
  6618. }
  6619. j += width * (3 - window - 1);
  6620. }
  6621. {
  6622. var amp = qupvt.IPOW20(202);
  6623. j += cod_info.width[sfb] * (window + 1);
  6624. for (var l = -cod_info.width[sfb]; l < 0; l++) {
  6625. xrpow[j + l] *= amp;
  6626. if (xrpow[j + l] > cod_info.xrpow_max)
  6627. cod_info.xrpow_max = xrpow[j + l];
  6628. }
  6629. }
  6630. }
  6631. return false;
  6632. }
  6633. /**
  6634. * <PRE>
  6635. * Takehiro Tominaga /date??
  6636. * Robert Hegemann 2000-09-06: made a function of it
  6637. *
  6638. * amplifies scalefactor bands,
  6639. * - if all are already amplified returns 0
  6640. * - if some bands are amplified too much:
  6641. * * try to increase scalefac_scale
  6642. * * if already scalefac_scale was set
  6643. * try on short blocks to increase subblock gain
  6644. * </PRE>
  6645. */
  6646. function balance_noise(gfp, cod_info, distort, xrpow, bRefine) {
  6647. var gfc = gfp.internal_flags;
  6648. amp_scalefac_bands(gfp, cod_info, distort, xrpow, bRefine);
  6649. /*
  6650. * check to make sure we have not amplified too much loop_break returns
  6651. * 0 if there is an unamplified scalefac scale_bitcount returns 0 if no
  6652. * scalefactors are too large
  6653. */
  6654. var status = loop_break(cod_info);
  6655. if (status)
  6656. return false;
  6657. /* all bands amplified */
  6658. /*
  6659. * not all scalefactors have been amplified. so these scalefacs are
  6660. * possibly valid. encode them:
  6661. */
  6662. if (gfc.mode_gr == 2)
  6663. status = tk.scale_bitcount(cod_info);
  6664. else
  6665. status = tk.scale_bitcount_lsf(gfc, cod_info);
  6666. if (!status)
  6667. return true;
  6668. /* amplified some bands not exceeding limits */
  6669. /*
  6670. * some scalefactors are too large. lets try setting scalefac_scale=1
  6671. */
  6672. if (gfc.noise_shaping > 1) {
  6673. Arrays.fill(gfc.pseudohalf, 0);
  6674. if (0 == cod_info.scalefac_scale) {
  6675. inc_scalefac_scale(cod_info, xrpow);
  6676. status = false;
  6677. } else {
  6678. if (cod_info.block_type == Encoder.SHORT_TYPE
  6679. && gfc.subblock_gain > 0) {
  6680. status = (inc_subblock_gain(gfc, cod_info, xrpow) || loop_break(cod_info));
  6681. }
  6682. }
  6683. }
  6684. if (!status) {
  6685. if (gfc.mode_gr == 2)
  6686. status = tk.scale_bitcount(cod_info);
  6687. else
  6688. status = tk.scale_bitcount_lsf(gfc, cod_info);
  6689. }
  6690. return !status;
  6691. }
  6692. /**
  6693. * <PRE>
  6694. * Function: The outer iteration loop controls the masking conditions
  6695. * of all scalefactorbands. It computes the best scalefac and
  6696. * global gain. This module calls the inner iteration loop
  6697. *
  6698. * mt 5/99 completely rewritten to allow for bit reservoir control,
  6699. * mid/side channels with L/R or mid/side masking thresholds,
  6700. * and chooses best quantization instead of last quantization when
  6701. * no distortion free quantization can be found.
  6702. *
  6703. * added VBR support mt 5/99
  6704. *
  6705. * some code shuffle rh 9/00
  6706. * </PRE>
  6707. *
  6708. * @param l3_xmin
  6709. * allowed distortion
  6710. * @param xrpow
  6711. * coloured magnitudes of spectral
  6712. * @param targ_bits
  6713. * maximum allowed bits
  6714. */
  6715. this.outer_loop = function (gfp, cod_info, l3_xmin, xrpow, ch, targ_bits) {
  6716. var gfc = gfp.internal_flags;
  6717. var cod_info_w = new GrInfo();
  6718. var save_xrpow = new_float(576);
  6719. var distort = new_float(L3Side.SFBMAX);
  6720. var best_noise_info = new CalcNoiseResult();
  6721. var better;
  6722. var prev_noise = new CalcNoiseData();
  6723. var best_part2_3_length = 9999999;
  6724. var bEndOfSearch = false;
  6725. var bRefine = false;
  6726. var best_ggain_pass1 = 0;
  6727. bin_search_StepSize(gfc, cod_info, targ_bits, ch, xrpow);
  6728. if (0 == gfc.noise_shaping)
  6729. /* fast mode, no noise shaping, we are ready */
  6730. return 100;
  6731. /* default noise_info.over_count */
  6732. /* compute the distortion in this quantization */
  6733. /* coefficients and thresholds both l/r (or both mid/side) */
  6734. qupvt.calc_noise(cod_info, l3_xmin, distort, best_noise_info,
  6735. prev_noise);
  6736. best_noise_info.bits = cod_info.part2_3_length;
  6737. cod_info_w.assign(cod_info);
  6738. var age = 0;
  6739. System.arraycopy(xrpow, 0, save_xrpow, 0, 576);
  6740. while (!bEndOfSearch) {
  6741. /* BEGIN MAIN LOOP */
  6742. do {
  6743. var noise_info = new CalcNoiseResult();
  6744. var search_limit;
  6745. var maxggain = 255;
  6746. /*
  6747. * When quantization with no distorted bands is found, allow up
  6748. * to X new unsuccesful tries in serial. This gives us more
  6749. * possibilities for different quant_compare modes. Much more
  6750. * than 3 makes not a big difference, it is only slower.
  6751. */
  6752. if ((gfc.substep_shaping & 2) != 0) {
  6753. search_limit = 20;
  6754. } else {
  6755. search_limit = 3;
  6756. }
  6757. /*
  6758. * Check if the last scalefactor band is distorted. in VBR mode
  6759. * we can't get rid of the distortion, so quit now and VBR mode
  6760. * will try again with more bits. (makes a 10% speed increase,
  6761. * the files I tested were binary identical, 2000/05/20 Robert
  6762. * Hegemann) distort[] > 1 means noise > allowed noise
  6763. */
  6764. if (gfc.sfb21_extra) {
  6765. if (distort[cod_info_w.sfbmax] > 1.0)
  6766. break;
  6767. if (cod_info_w.block_type == Encoder.SHORT_TYPE
  6768. && (distort[cod_info_w.sfbmax + 1] > 1.0 || distort[cod_info_w.sfbmax + 2] > 1.0))
  6769. break;
  6770. }
  6771. /* try a new scalefactor conbination on cod_info_w */
  6772. if (!balance_noise(gfp, cod_info_w, distort, xrpow, bRefine))
  6773. break;
  6774. if (cod_info_w.scalefac_scale != 0)
  6775. maxggain = 254;
  6776. /*
  6777. * inner_loop starts with the initial quantization step computed
  6778. * above and slowly increases until the bits < huff_bits. Thus
  6779. * it is important not to start with too large of an inital
  6780. * quantization step. Too small is ok, but inner_loop will take
  6781. * longer
  6782. */
  6783. var huff_bits = targ_bits - cod_info_w.part2_length;
  6784. if (huff_bits <= 0)
  6785. break;
  6786. /*
  6787. * increase quantizer stepsize until needed bits are below
  6788. * maximum
  6789. */
  6790. while ((cod_info_w.part2_3_length = tk.count_bits(gfc, xrpow,
  6791. cod_info_w, prev_noise)) > huff_bits
  6792. && cod_info_w.global_gain <= maxggain)
  6793. cod_info_w.global_gain++;
  6794. if (cod_info_w.global_gain > maxggain)
  6795. break;
  6796. if (best_noise_info.over_count == 0) {
  6797. while ((cod_info_w.part2_3_length = tk.count_bits(gfc,
  6798. xrpow, cod_info_w, prev_noise)) > best_part2_3_length
  6799. && cod_info_w.global_gain <= maxggain)
  6800. cod_info_w.global_gain++;
  6801. if (cod_info_w.global_gain > maxggain)
  6802. break;
  6803. }
  6804. /* compute the distortion in this quantization */
  6805. qupvt.calc_noise(cod_info_w, l3_xmin, distort, noise_info,
  6806. prev_noise);
  6807. noise_info.bits = cod_info_w.part2_3_length;
  6808. /*
  6809. * check if this quantization is better than our saved
  6810. * quantization
  6811. */
  6812. if (cod_info.block_type != Encoder.SHORT_TYPE) {
  6813. // NORM, START or STOP type
  6814. better = gfp.quant_comp;
  6815. } else
  6816. better = gfp.quant_comp_short;
  6817. better = quant_compare(better, best_noise_info, noise_info,
  6818. cod_info_w, distort) ? 1 : 0;
  6819. /* save data so we can restore this quantization later */
  6820. if (better != 0) {
  6821. best_part2_3_length = cod_info.part2_3_length;
  6822. best_noise_info = noise_info;
  6823. cod_info.assign(cod_info_w);
  6824. age = 0;
  6825. /* save data so we can restore this quantization later */
  6826. /* store for later reuse */
  6827. System.arraycopy(xrpow, 0, save_xrpow, 0, 576);
  6828. } else {
  6829. /* early stop? */
  6830. if (gfc.full_outer_loop == 0) {
  6831. if (++age > search_limit
  6832. && best_noise_info.over_count == 0)
  6833. break;
  6834. if ((gfc.noise_shaping_amp == 3) && bRefine && age > 30)
  6835. break;
  6836. if ((gfc.noise_shaping_amp == 3)
  6837. && bRefine
  6838. && (cod_info_w.global_gain - best_ggain_pass1) > 15)
  6839. break;
  6840. }
  6841. }
  6842. } while ((cod_info_w.global_gain + cod_info_w.scalefac_scale) < 255);
  6843. if (gfc.noise_shaping_amp == 3) {
  6844. if (!bRefine) {
  6845. /* refine search */
  6846. cod_info_w.assign(cod_info);
  6847. System.arraycopy(save_xrpow, 0, xrpow, 0, 576);
  6848. age = 0;
  6849. best_ggain_pass1 = cod_info_w.global_gain;
  6850. bRefine = true;
  6851. } else {
  6852. /* search already refined, stop */
  6853. bEndOfSearch = true;
  6854. }
  6855. } else {
  6856. bEndOfSearch = true;
  6857. }
  6858. }
  6859. /*
  6860. * finish up
  6861. */
  6862. if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh)
  6863. /* restore for reuse on next try */
  6864. System.arraycopy(save_xrpow, 0, xrpow, 0, 576);
  6865. /*
  6866. * do the 'substep shaping'
  6867. */
  6868. else if ((gfc.substep_shaping & 1) != 0)
  6869. trancate_smallspectrums(gfc, cod_info, l3_xmin, xrpow);
  6870. return best_noise_info.over_count;
  6871. }
  6872. /**
  6873. * Robert Hegemann 2000-09-06
  6874. *
  6875. * update reservoir status after FINAL quantization/bitrate
  6876. */
  6877. this.iteration_finish_one = function (gfc, gr, ch) {
  6878. var l3_side = gfc.l3_side;
  6879. var cod_info = l3_side.tt[gr][ch];
  6880. /*
  6881. * try some better scalefac storage
  6882. */
  6883. tk.best_scalefac_store(gfc, gr, ch, l3_side);
  6884. /*
  6885. * best huffman_divide may save some bits too
  6886. */
  6887. if (gfc.use_best_huffman == 1)
  6888. tk.best_huffman_divide(gfc, cod_info);
  6889. /*
  6890. * update reservoir status after FINAL quantization/bitrate
  6891. */
  6892. rv.ResvAdjust(gfc, cod_info);
  6893. };
  6894. /**
  6895. *
  6896. * 2000-09-04 Robert Hegemann
  6897. *
  6898. * @param l3_xmin
  6899. * allowed distortion of the scalefactor
  6900. * @param xrpow
  6901. * coloured magnitudes of spectral values
  6902. */
  6903. this.VBR_encode_granule = function (gfp, cod_info, l3_xmin, xrpow, ch, min_bits, max_bits) {
  6904. var gfc = gfp.internal_flags;
  6905. var bst_cod_info = new GrInfo();
  6906. var bst_xrpow = new_float(576);
  6907. var Max_bits = max_bits;
  6908. var real_bits = max_bits + 1;
  6909. var this_bits = (max_bits + min_bits) / 2;
  6910. var dbits, over, found = 0;
  6911. var sfb21_extra = gfc.sfb21_extra;
  6912. Arrays.fill(bst_cod_info.l3_enc, 0);
  6913. /*
  6914. * search within round about 40 bits of optimal
  6915. */
  6916. do {
  6917. if (this_bits > Max_bits - 42)
  6918. gfc.sfb21_extra = false;
  6919. else
  6920. gfc.sfb21_extra = sfb21_extra;
  6921. over = outer_loop(gfp, cod_info, l3_xmin, xrpow, ch, this_bits);
  6922. /*
  6923. * is quantization as good as we are looking for ? in this case: is
  6924. * no scalefactor band distorted?
  6925. */
  6926. if (over <= 0) {
  6927. found = 1;
  6928. /*
  6929. * now we know it can be done with "real_bits" and maybe we can
  6930. * skip some iterations
  6931. */
  6932. real_bits = cod_info.part2_3_length;
  6933. /*
  6934. * store best quantization so far
  6935. */
  6936. bst_cod_info.assign(cod_info);
  6937. System.arraycopy(xrpow, 0, bst_xrpow, 0, 576);
  6938. /*
  6939. * try with fewer bits
  6940. */
  6941. max_bits = real_bits - 32;
  6942. dbits = max_bits - min_bits;
  6943. this_bits = (max_bits + min_bits) / 2;
  6944. } else {
  6945. /*
  6946. * try with more bits
  6947. */
  6948. min_bits = this_bits + 32;
  6949. dbits = max_bits - min_bits;
  6950. this_bits = (max_bits + min_bits) / 2;
  6951. if (found != 0) {
  6952. found = 2;
  6953. /*
  6954. * start again with best quantization so far
  6955. */
  6956. cod_info.assign(bst_cod_info);
  6957. System.arraycopy(bst_xrpow, 0, xrpow, 0, 576);
  6958. }
  6959. }
  6960. } while (dbits > 12);
  6961. gfc.sfb21_extra = sfb21_extra;
  6962. /*
  6963. * found=0 => nothing found, use last one found=1 => we just found the
  6964. * best and left the loop found=2 => we restored a good one and have now
  6965. * l3_enc to restore too
  6966. */
  6967. if (found == 2) {
  6968. System.arraycopy(bst_cod_info.l3_enc, 0, cod_info.l3_enc, 0, 576);
  6969. }
  6970. }
  6971. /**
  6972. * Robert Hegemann 2000-09-05
  6973. *
  6974. * calculates * how many bits are available for analog silent granules * how
  6975. * many bits to use for the lowest allowed bitrate * how many bits each
  6976. * bitrate would provide
  6977. */
  6978. this.get_framebits = function (gfp, frameBits) {
  6979. var gfc = gfp.internal_flags;
  6980. /*
  6981. * always use at least this many bits per granule per channel unless we
  6982. * detect analog silence, see below
  6983. */
  6984. gfc.bitrate_index = gfc.VBR_min_bitrate;
  6985. var bitsPerFrame = bs.getframebits(gfp);
  6986. /*
  6987. * bits for analog silence
  6988. */
  6989. gfc.bitrate_index = 1;
  6990. bitsPerFrame = bs.getframebits(gfp);
  6991. for (var i = 1; i <= gfc.VBR_max_bitrate; i++) {
  6992. gfc.bitrate_index = i;
  6993. var mb = new MeanBits(bitsPerFrame);
  6994. frameBits[i] = rv.ResvFrameBegin(gfp, mb);
  6995. bitsPerFrame = mb.bits;
  6996. }
  6997. };
  6998. /* RH: this one needs to be overhauled sometime */
  6999. /**
  7000. * <PRE>
  7001. * 2000-09-04 Robert Hegemann
  7002. *
  7003. * * converts LR to MS coding when necessary
  7004. * * calculates allowed/adjusted quantization noise amounts
  7005. * * detects analog silent frames
  7006. *
  7007. * some remarks:
  7008. * - lower masking depending on Quality setting
  7009. * - quality control together with adjusted ATH MDCT scaling
  7010. * on lower quality setting allocate more noise from
  7011. * ATH masking, and on higher quality setting allocate
  7012. * less noise from ATH masking.
  7013. * - experiments show that going more than 2dB over GPSYCHO's
  7014. * limits ends up in very annoying artefacts
  7015. * </PRE>
  7016. */
  7017. this.VBR_old_prepare = function (gfp, pe, ms_ener_ratio, ratio, l3_xmin, frameBits, min_bits,
  7018. max_bits, bands) {
  7019. var gfc = gfp.internal_flags;
  7020. var masking_lower_db, adjust = 0.0;
  7021. var analog_silence = 1;
  7022. var bits = 0;
  7023. gfc.bitrate_index = gfc.VBR_max_bitrate;
  7024. var avg = rv.ResvFrameBegin(gfp, new MeanBits(0)) / gfc.mode_gr;
  7025. get_framebits(gfp, frameBits);
  7026. for (var gr = 0; gr < gfc.mode_gr; gr++) {
  7027. var mxb = qupvt.on_pe(gfp, pe, max_bits[gr], avg, gr, 0);
  7028. if (gfc.mode_ext == Encoder.MPG_MD_MS_LR) {
  7029. ms_convert(gfc.l3_side, gr);
  7030. qupvt.reduce_side(max_bits[gr], ms_ener_ratio[gr], avg, mxb);
  7031. }
  7032. for (var ch = 0; ch < gfc.channels_out; ++ch) {
  7033. var cod_info = gfc.l3_side.tt[gr][ch];
  7034. if (cod_info.block_type != Encoder.SHORT_TYPE) {
  7035. // NORM, START or STOP type
  7036. adjust = 1.28 / (1 + Math
  7037. .exp(3.5 - pe[gr][ch] / 300.)) - 0.05;
  7038. masking_lower_db = gfc.PSY.mask_adjust - adjust;
  7039. } else {
  7040. adjust = 2.56 / (1 + Math
  7041. .exp(3.5 - pe[gr][ch] / 300.)) - 0.14;
  7042. masking_lower_db = gfc.PSY.mask_adjust_short - adjust;
  7043. }
  7044. gfc.masking_lower = Math.pow(10.0,
  7045. masking_lower_db * 0.1);
  7046. init_outer_loop(gfc, cod_info);
  7047. bands[gr][ch] = qupvt.calc_xmin(gfp, ratio[gr][ch], cod_info,
  7048. l3_xmin[gr][ch]);
  7049. if (bands[gr][ch] != 0)
  7050. analog_silence = 0;
  7051. min_bits[gr][ch] = 126;
  7052. bits += max_bits[gr][ch];
  7053. }
  7054. }
  7055. for (var gr = 0; gr < gfc.mode_gr; gr++) {
  7056. for (var ch = 0; ch < gfc.channels_out; ch++) {
  7057. if (bits > frameBits[gfc.VBR_max_bitrate]) {
  7058. max_bits[gr][ch] *= frameBits[gfc.VBR_max_bitrate];
  7059. max_bits[gr][ch] /= bits;
  7060. }
  7061. if (min_bits[gr][ch] > max_bits[gr][ch])
  7062. min_bits[gr][ch] = max_bits[gr][ch];
  7063. }
  7064. /* for ch */
  7065. }
  7066. /* for gr */
  7067. return analog_silence;
  7068. };
  7069. this.bitpressure_strategy = function (gfc, l3_xmin, min_bits, max_bits) {
  7070. for (var gr = 0; gr < gfc.mode_gr; gr++) {
  7071. for (var ch = 0; ch < gfc.channels_out; ch++) {
  7072. var gi = gfc.l3_side.tt[gr][ch];
  7073. var pxmin = l3_xmin[gr][ch];
  7074. var pxminPos = 0;
  7075. for (var sfb = 0; sfb < gi.psy_lmax; sfb++)
  7076. pxmin[pxminPos++] *= 1. + .029 * sfb * sfb
  7077. / Encoder.SBMAX_l / Encoder.SBMAX_l;
  7078. if (gi.block_type == Encoder.SHORT_TYPE) {
  7079. for (var sfb = gi.sfb_smin; sfb < Encoder.SBMAX_s; sfb++) {
  7080. pxmin[pxminPos++] *= 1. + .029 * sfb * sfb
  7081. / Encoder.SBMAX_s / Encoder.SBMAX_s;
  7082. pxmin[pxminPos++] *= 1. + .029 * sfb * sfb
  7083. / Encoder.SBMAX_s / Encoder.SBMAX_s;
  7084. pxmin[pxminPos++] *= 1. + .029 * sfb * sfb
  7085. / Encoder.SBMAX_s / Encoder.SBMAX_s;
  7086. }
  7087. }
  7088. max_bits[gr][ch] = 0 | Math.max(min_bits[gr][ch],
  7089. 0.9 * max_bits[gr][ch]);
  7090. }
  7091. }
  7092. };
  7093. this.VBR_new_prepare = function (gfp, pe, ratio, l3_xmin, frameBits, max_bits) {
  7094. var gfc = gfp.internal_flags;
  7095. var analog_silence = 1;
  7096. var avg = 0, bits = 0;
  7097. var maximum_framebits;
  7098. if (!gfp.free_format) {
  7099. gfc.bitrate_index = gfc.VBR_max_bitrate;
  7100. var mb = new MeanBits(avg);
  7101. rv.ResvFrameBegin(gfp, mb);
  7102. avg = mb.bits;
  7103. get_framebits(gfp, frameBits);
  7104. maximum_framebits = frameBits[gfc.VBR_max_bitrate];
  7105. } else {
  7106. gfc.bitrate_index = 0;
  7107. var mb = new MeanBits(avg);
  7108. maximum_framebits = rv.ResvFrameBegin(gfp, mb);
  7109. avg = mb.bits;
  7110. frameBits[0] = maximum_framebits;
  7111. }
  7112. for (var gr = 0; gr < gfc.mode_gr; gr++) {
  7113. qupvt.on_pe(gfp, pe, max_bits[gr], avg, gr, 0);
  7114. if (gfc.mode_ext == Encoder.MPG_MD_MS_LR) {
  7115. ms_convert(gfc.l3_side, gr);
  7116. }
  7117. for (var ch = 0; ch < gfc.channels_out; ++ch) {
  7118. var cod_info = gfc.l3_side.tt[gr][ch];
  7119. gfc.masking_lower = Math.pow(10.0,
  7120. gfc.PSY.mask_adjust * 0.1);
  7121. init_outer_loop(gfc, cod_info);
  7122. if (0 != qupvt.calc_xmin(gfp, ratio[gr][ch], cod_info,
  7123. l3_xmin[gr][ch]))
  7124. analog_silence = 0;
  7125. bits += max_bits[gr][ch];
  7126. }
  7127. }
  7128. for (var gr = 0; gr < gfc.mode_gr; gr++) {
  7129. for (var ch = 0; ch < gfc.channels_out; ch++) {
  7130. if (bits > maximum_framebits) {
  7131. max_bits[gr][ch] *= maximum_framebits;
  7132. max_bits[gr][ch] /= bits;
  7133. }
  7134. }
  7135. /* for ch */
  7136. }
  7137. /* for gr */
  7138. return analog_silence;
  7139. };
  7140. /**
  7141. * calculates target bits for ABR encoding
  7142. *
  7143. * mt 2000/05/31
  7144. */
  7145. this.calc_target_bits = function (gfp, pe, ms_ener_ratio, targ_bits, analog_silence_bits, max_frame_bits) {
  7146. var gfc = gfp.internal_flags;
  7147. var l3_side = gfc.l3_side;
  7148. var res_factor;
  7149. var gr, ch, totbits, mean_bits = 0;
  7150. gfc.bitrate_index = gfc.VBR_max_bitrate;
  7151. var mb = new MeanBits(mean_bits);
  7152. max_frame_bits[0] = rv.ResvFrameBegin(gfp, mb);
  7153. mean_bits = mb.bits;
  7154. gfc.bitrate_index = 1;
  7155. mean_bits = bs.getframebits(gfp) - gfc.sideinfo_len * 8;
  7156. analog_silence_bits[0] = mean_bits / (gfc.mode_gr * gfc.channels_out);
  7157. mean_bits = gfp.VBR_mean_bitrate_kbps * gfp.framesize * 1000;
  7158. if ((gfc.substep_shaping & 1) != 0)
  7159. mean_bits *= 1.09;
  7160. mean_bits /= gfp.out_samplerate;
  7161. mean_bits -= gfc.sideinfo_len * 8;
  7162. mean_bits /= (gfc.mode_gr * gfc.channels_out);
  7163. /**
  7164. * <PRE>
  7165. * res_factor is the percentage of the target bitrate that should
  7166. * be used on average. the remaining bits are added to the
  7167. * bitreservoir and used for difficult to encode frames.
  7168. *
  7169. * Since we are tracking the average bitrate, we should adjust
  7170. * res_factor "on the fly", increasing it if the average bitrate
  7171. * is greater than the requested bitrate, and decreasing it
  7172. * otherwise. Reasonable ranges are from .9 to 1.0
  7173. *
  7174. * Until we get the above suggestion working, we use the following
  7175. * tuning:
  7176. * compression ratio res_factor
  7177. * 5.5 (256kbps) 1.0 no need for bitreservoir
  7178. * 11 (128kbps) .93 7% held for reservoir
  7179. *
  7180. * with linear interpolation for other values.
  7181. * </PRE>
  7182. */
  7183. res_factor = .93 + .07 * (11.0 - gfp.compression_ratio)
  7184. / (11.0 - 5.5);
  7185. if (res_factor < .90)
  7186. res_factor = .90;
  7187. if (res_factor > 1.00)
  7188. res_factor = 1.00;
  7189. for (gr = 0; gr < gfc.mode_gr; gr++) {
  7190. var sum = 0;
  7191. for (ch = 0; ch < gfc.channels_out; ch++) {
  7192. targ_bits[gr][ch] = (int)(res_factor * mean_bits);
  7193. if (pe[gr][ch] > 700) {
  7194. var add_bits = (int)((pe[gr][ch] - 700) / 1.4);
  7195. var cod_info = l3_side.tt[gr][ch];
  7196. targ_bits[gr][ch] = (int)(res_factor * mean_bits);
  7197. /* short blocks use a little extra, no matter what the pe */
  7198. if (cod_info.block_type == Encoder.SHORT_TYPE) {
  7199. if (add_bits < mean_bits / 2)
  7200. add_bits = mean_bits / 2;
  7201. }
  7202. /* at most increase bits by 1.5*average */
  7203. if (add_bits > mean_bits * 3 / 2)
  7204. add_bits = mean_bits * 3 / 2;
  7205. else if (add_bits < 0)
  7206. add_bits = 0;
  7207. targ_bits[gr][ch] += add_bits;
  7208. }
  7209. if (targ_bits[gr][ch] > LameInternalFlags.MAX_BITS_PER_CHANNEL) {
  7210. targ_bits[gr][ch] = LameInternalFlags.MAX_BITS_PER_CHANNEL;
  7211. }
  7212. sum += targ_bits[gr][ch];
  7213. }
  7214. /* for ch */
  7215. if (sum > LameInternalFlags.MAX_BITS_PER_GRANULE) {
  7216. for (ch = 0; ch < gfc.channels_out; ++ch) {
  7217. targ_bits[gr][ch] *= LameInternalFlags.MAX_BITS_PER_GRANULE;
  7218. targ_bits[gr][ch] /= sum;
  7219. }
  7220. }
  7221. }
  7222. /* for gr */
  7223. if (gfc.mode_ext == Encoder.MPG_MD_MS_LR)
  7224. for (gr = 0; gr < gfc.mode_gr; gr++) {
  7225. qupvt.reduce_side(targ_bits[gr], ms_ener_ratio[gr], mean_bits
  7226. * gfc.channels_out,
  7227. LameInternalFlags.MAX_BITS_PER_GRANULE);
  7228. }
  7229. /*
  7230. * sum target bits
  7231. */
  7232. totbits = 0;
  7233. for (gr = 0; gr < gfc.mode_gr; gr++) {
  7234. for (ch = 0; ch < gfc.channels_out; ch++) {
  7235. if (targ_bits[gr][ch] > LameInternalFlags.MAX_BITS_PER_CHANNEL)
  7236. targ_bits[gr][ch] = LameInternalFlags.MAX_BITS_PER_CHANNEL;
  7237. totbits += targ_bits[gr][ch];
  7238. }
  7239. }
  7240. /*
  7241. * repartion target bits if needed
  7242. */
  7243. if (totbits > max_frame_bits[0]) {
  7244. for (gr = 0; gr < gfc.mode_gr; gr++) {
  7245. for (ch = 0; ch < gfc.channels_out; ch++) {
  7246. targ_bits[gr][ch] *= max_frame_bits[0];
  7247. targ_bits[gr][ch] /= totbits;
  7248. }
  7249. }
  7250. }
  7251. }
  7252. }
  7253. /*
  7254. * MP3 window subband -> subband filtering -> mdct routine
  7255. *
  7256. * Copyright (c) 1999-2000 Takehiro Tominaga
  7257. *
  7258. *
  7259. * This library is free software; you can redistribute it and/or
  7260. * modify it under the terms of the GNU Lesser General Public
  7261. * License as published by the Free Software Foundation; either
  7262. * version 2 of the License, or (at your option) any later version.
  7263. *
  7264. * This library is distributed in the hope that it will be useful,
  7265. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7266. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  7267. * Library General Public License for more details.
  7268. *
  7269. * You should have received a copy of the GNU Library General Public
  7270. * License along with this library; if not, write to the
  7271. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  7272. * Boston, MA 02111-1307, USA.
  7273. */
  7274. /*
  7275. * Special Thanks to Patrick De Smet for your advices.
  7276. */
  7277. /* $Id: NewMDCT.java,v 1.11 2011/05/24 20:48:06 kenchis Exp $ */
  7278. //package mp3;
  7279. //import java.util.Arrays;
  7280. function NewMDCT() {
  7281. var enwindow = [
  7282. -4.77e-07 * 0.740951125354959 / 2.384e-06,
  7283. 1.03951e-04 * 0.740951125354959 / 2.384e-06,
  7284. 9.53674e-04 * 0.740951125354959 / 2.384e-06,
  7285. 2.841473e-03 * 0.740951125354959 / 2.384e-06,
  7286. 3.5758972e-02 * 0.740951125354959 / 2.384e-06,
  7287. 3.401756e-03 * 0.740951125354959 / 2.384e-06,
  7288. 9.83715e-04 * 0.740951125354959 / 2.384e-06,
  7289. 9.9182e-05 * 0.740951125354959 / 2.384e-06, /* 15 */
  7290. 1.2398e-05 * 0.740951125354959 / 2.384e-06,
  7291. 1.91212e-04 * 0.740951125354959 / 2.384e-06,
  7292. 2.283096e-03 * 0.740951125354959 / 2.384e-06,
  7293. 1.6994476e-02 * 0.740951125354959 / 2.384e-06,
  7294. -1.8756866e-02 * 0.740951125354959 / 2.384e-06,
  7295. -2.630711e-03 * 0.740951125354959 / 2.384e-06,
  7296. -2.47478e-04 * 0.740951125354959 / 2.384e-06,
  7297. -1.4782e-05 * 0.740951125354959 / 2.384e-06,
  7298. 9.063471690191471e-01, 1.960342806591213e-01,
  7299. -4.77e-07 * 0.773010453362737 / 2.384e-06,
  7300. 1.05858e-04 * 0.773010453362737 / 2.384e-06,
  7301. 9.30786e-04 * 0.773010453362737 / 2.384e-06,
  7302. 2.521515e-03 * 0.773010453362737 / 2.384e-06,
  7303. 3.5694122e-02 * 0.773010453362737 / 2.384e-06,
  7304. 3.643036e-03 * 0.773010453362737 / 2.384e-06,
  7305. 9.91821e-04 * 0.773010453362737 / 2.384e-06,
  7306. 9.6321e-05 * 0.773010453362737 / 2.384e-06, /* 14 */
  7307. 1.1444e-05 * 0.773010453362737 / 2.384e-06,
  7308. 1.65462e-04 * 0.773010453362737 / 2.384e-06,
  7309. 2.110004e-03 * 0.773010453362737 / 2.384e-06,
  7310. 1.6112804e-02 * 0.773010453362737 / 2.384e-06,
  7311. -1.9634247e-02 * 0.773010453362737 / 2.384e-06,
  7312. -2.803326e-03 * 0.773010453362737 / 2.384e-06,
  7313. -2.77042e-04 * 0.773010453362737 / 2.384e-06,
  7314. -1.6689e-05 * 0.773010453362737 / 2.384e-06,
  7315. 8.206787908286602e-01, 3.901806440322567e-01,
  7316. -4.77e-07 * 0.803207531480645 / 2.384e-06,
  7317. 1.07288e-04 * 0.803207531480645 / 2.384e-06,
  7318. 9.02653e-04 * 0.803207531480645 / 2.384e-06,
  7319. 2.174854e-03 * 0.803207531480645 / 2.384e-06,
  7320. 3.5586357e-02 * 0.803207531480645 / 2.384e-06,
  7321. 3.858566e-03 * 0.803207531480645 / 2.384e-06,
  7322. 9.95159e-04 * 0.803207531480645 / 2.384e-06,
  7323. 9.3460e-05 * 0.803207531480645 / 2.384e-06, /* 13 */
  7324. 1.0014e-05 * 0.803207531480645 / 2.384e-06,
  7325. 1.40190e-04 * 0.803207531480645 / 2.384e-06,
  7326. 1.937389e-03 * 0.803207531480645 / 2.384e-06,
  7327. 1.5233517e-02 * 0.803207531480645 / 2.384e-06,
  7328. -2.0506859e-02 * 0.803207531480645 / 2.384e-06,
  7329. -2.974033e-03 * 0.803207531480645 / 2.384e-06,
  7330. -3.07560e-04 * 0.803207531480645 / 2.384e-06,
  7331. -1.8120e-05 * 0.803207531480645 / 2.384e-06,
  7332. 7.416505462720353e-01, 5.805693545089249e-01,
  7333. -4.77e-07 * 0.831469612302545 / 2.384e-06,
  7334. 1.08242e-04 * 0.831469612302545 / 2.384e-06,
  7335. 8.68797e-04 * 0.831469612302545 / 2.384e-06,
  7336. 1.800537e-03 * 0.831469612302545 / 2.384e-06,
  7337. 3.5435200e-02 * 0.831469612302545 / 2.384e-06,
  7338. 4.049301e-03 * 0.831469612302545 / 2.384e-06,
  7339. 9.94205e-04 * 0.831469612302545 / 2.384e-06,
  7340. 9.0599e-05 * 0.831469612302545 / 2.384e-06, /* 12 */
  7341. 9.060e-06 * 0.831469612302545 / 2.384e-06,
  7342. 1.16348e-04 * 0.831469612302545 / 2.384e-06,
  7343. 1.766682e-03 * 0.831469612302545 / 2.384e-06,
  7344. 1.4358521e-02 * 0.831469612302545 / 2.384e-06,
  7345. -2.1372318e-02 * 0.831469612302545 / 2.384e-06,
  7346. -3.14188e-03 * 0.831469612302545 / 2.384e-06,
  7347. -3.39031e-04 * 0.831469612302545 / 2.384e-06,
  7348. -1.9550e-05 * 0.831469612302545 / 2.384e-06,
  7349. 6.681786379192989e-01, 7.653668647301797e-01,
  7350. -4.77e-07 * 0.857728610000272 / 2.384e-06,
  7351. 1.08719e-04 * 0.857728610000272 / 2.384e-06,
  7352. 8.29220e-04 * 0.857728610000272 / 2.384e-06,
  7353. 1.399517e-03 * 0.857728610000272 / 2.384e-06,
  7354. 3.5242081e-02 * 0.857728610000272 / 2.384e-06,
  7355. 4.215240e-03 * 0.857728610000272 / 2.384e-06,
  7356. 9.89437e-04 * 0.857728610000272 / 2.384e-06,
  7357. 8.7261e-05 * 0.857728610000272 / 2.384e-06, /* 11 */
  7358. 8.106e-06 * 0.857728610000272 / 2.384e-06,
  7359. 9.3937e-05 * 0.857728610000272 / 2.384e-06,
  7360. 1.597881e-03 * 0.857728610000272 / 2.384e-06,
  7361. 1.3489246e-02 * 0.857728610000272 / 2.384e-06,
  7362. -2.2228718e-02 * 0.857728610000272 / 2.384e-06,
  7363. -3.306866e-03 * 0.857728610000272 / 2.384e-06,
  7364. -3.71456e-04 * 0.857728610000272 / 2.384e-06,
  7365. -2.1458e-05 * 0.857728610000272 / 2.384e-06,
  7366. 5.993769336819237e-01, 9.427934736519954e-01,
  7367. -4.77e-07 * 0.881921264348355 / 2.384e-06,
  7368. 1.08719e-04 * 0.881921264348355 / 2.384e-06,
  7369. 7.8392e-04 * 0.881921264348355 / 2.384e-06,
  7370. 9.71317e-04 * 0.881921264348355 / 2.384e-06,
  7371. 3.5007000e-02 * 0.881921264348355 / 2.384e-06,
  7372. 4.357815e-03 * 0.881921264348355 / 2.384e-06,
  7373. 9.80854e-04 * 0.881921264348355 / 2.384e-06,
  7374. 8.3923e-05 * 0.881921264348355 / 2.384e-06, /* 10 */
  7375. 7.629e-06 * 0.881921264348355 / 2.384e-06,
  7376. 7.2956e-05 * 0.881921264348355 / 2.384e-06,
  7377. 1.432419e-03 * 0.881921264348355 / 2.384e-06,
  7378. 1.2627602e-02 * 0.881921264348355 / 2.384e-06,
  7379. -2.3074150e-02 * 0.881921264348355 / 2.384e-06,
  7380. -3.467083e-03 * 0.881921264348355 / 2.384e-06,
  7381. -4.04358e-04 * 0.881921264348355 / 2.384e-06,
  7382. -2.3365e-05 * 0.881921264348355 / 2.384e-06,
  7383. 5.345111359507916e-01, 1.111140466039205e+00,
  7384. -9.54e-07 * 0.903989293123443 / 2.384e-06,
  7385. 1.08242e-04 * 0.903989293123443 / 2.384e-06,
  7386. 7.31945e-04 * 0.903989293123443 / 2.384e-06,
  7387. 5.15938e-04 * 0.903989293123443 / 2.384e-06,
  7388. 3.4730434e-02 * 0.903989293123443 / 2.384e-06,
  7389. 4.477024e-03 * 0.903989293123443 / 2.384e-06,
  7390. 9.68933e-04 * 0.903989293123443 / 2.384e-06,
  7391. 8.0585e-05 * 0.903989293123443 / 2.384e-06, /* 9 */
  7392. 6.676e-06 * 0.903989293123443 / 2.384e-06,
  7393. 5.2929e-05 * 0.903989293123443 / 2.384e-06,
  7394. 1.269817e-03 * 0.903989293123443 / 2.384e-06,
  7395. 1.1775017e-02 * 0.903989293123443 / 2.384e-06,
  7396. -2.3907185e-02 * 0.903989293123443 / 2.384e-06,
  7397. -3.622532e-03 * 0.903989293123443 / 2.384e-06,
  7398. -4.38213e-04 * 0.903989293123443 / 2.384e-06,
  7399. -2.5272e-05 * 0.903989293123443 / 2.384e-06,
  7400. 4.729647758913199e-01, 1.268786568327291e+00,
  7401. -9.54e-07 * 0.92387953251128675613 / 2.384e-06,
  7402. 1.06812e-04 * 0.92387953251128675613 / 2.384e-06,
  7403. 6.74248e-04 * 0.92387953251128675613 / 2.384e-06,
  7404. 3.3379e-05 * 0.92387953251128675613 / 2.384e-06,
  7405. 3.4412861e-02 * 0.92387953251128675613 / 2.384e-06,
  7406. 4.573822e-03 * 0.92387953251128675613 / 2.384e-06,
  7407. 9.54151e-04 * 0.92387953251128675613 / 2.384e-06,
  7408. 7.6771e-05 * 0.92387953251128675613 / 2.384e-06,
  7409. 6.199e-06 * 0.92387953251128675613 / 2.384e-06,
  7410. 3.4332e-05 * 0.92387953251128675613 / 2.384e-06,
  7411. 1.111031e-03 * 0.92387953251128675613 / 2.384e-06,
  7412. 1.0933399e-02 * 0.92387953251128675613 / 2.384e-06,
  7413. -2.4725437e-02 * 0.92387953251128675613 / 2.384e-06,
  7414. -3.771782e-03 * 0.92387953251128675613 / 2.384e-06,
  7415. -4.72546e-04 * 0.92387953251128675613 / 2.384e-06,
  7416. -2.7657e-05 * 0.92387953251128675613 / 2.384e-06,
  7417. 4.1421356237309504879e-01, /* tan(PI/8) */
  7418. 1.414213562373095e+00,
  7419. -9.54e-07 * 0.941544065183021 / 2.384e-06,
  7420. 1.05381e-04 * 0.941544065183021 / 2.384e-06,
  7421. 6.10352e-04 * 0.941544065183021 / 2.384e-06,
  7422. -4.75883e-04 * 0.941544065183021 / 2.384e-06,
  7423. 3.4055710e-02 * 0.941544065183021 / 2.384e-06,
  7424. 4.649162e-03 * 0.941544065183021 / 2.384e-06,
  7425. 9.35555e-04 * 0.941544065183021 / 2.384e-06,
  7426. 7.3433e-05 * 0.941544065183021 / 2.384e-06, /* 7 */
  7427. 5.245e-06 * 0.941544065183021 / 2.384e-06,
  7428. 1.7166e-05 * 0.941544065183021 / 2.384e-06,
  7429. 9.56535e-04 * 0.941544065183021 / 2.384e-06,
  7430. 1.0103703e-02 * 0.941544065183021 / 2.384e-06,
  7431. -2.5527000e-02 * 0.941544065183021 / 2.384e-06,
  7432. -3.914356e-03 * 0.941544065183021 / 2.384e-06,
  7433. -5.07355e-04 * 0.941544065183021 / 2.384e-06,
  7434. -3.0041e-05 * 0.941544065183021 / 2.384e-06,
  7435. 3.578057213145241e-01, 1.546020906725474e+00,
  7436. -9.54e-07 * 0.956940335732209 / 2.384e-06,
  7437. 1.02520e-04 * 0.956940335732209 / 2.384e-06,
  7438. 5.39303e-04 * 0.956940335732209 / 2.384e-06,
  7439. -1.011848e-03 * 0.956940335732209 / 2.384e-06,
  7440. 3.3659935e-02 * 0.956940335732209 / 2.384e-06,
  7441. 4.703045e-03 * 0.956940335732209 / 2.384e-06,
  7442. 9.15051e-04 * 0.956940335732209 / 2.384e-06,
  7443. 7.0095e-05 * 0.956940335732209 / 2.384e-06, /* 6 */
  7444. 4.768e-06 * 0.956940335732209 / 2.384e-06,
  7445. 9.54e-07 * 0.956940335732209 / 2.384e-06,
  7446. 8.06808e-04 * 0.956940335732209 / 2.384e-06,
  7447. 9.287834e-03 * 0.956940335732209 / 2.384e-06,
  7448. -2.6310921e-02 * 0.956940335732209 / 2.384e-06,
  7449. -4.048824e-03 * 0.956940335732209 / 2.384e-06,
  7450. -5.42164e-04 * 0.956940335732209 / 2.384e-06,
  7451. -3.2425e-05 * 0.956940335732209 / 2.384e-06,
  7452. 3.033466836073424e-01, 1.662939224605090e+00,
  7453. -1.431e-06 * 0.970031253194544 / 2.384e-06,
  7454. 9.9182e-05 * 0.970031253194544 / 2.384e-06,
  7455. 4.62532e-04 * 0.970031253194544 / 2.384e-06,
  7456. -1.573563e-03 * 0.970031253194544 / 2.384e-06,
  7457. 3.3225536e-02 * 0.970031253194544 / 2.384e-06,
  7458. 4.737377e-03 * 0.970031253194544 / 2.384e-06,
  7459. 8.91685e-04 * 0.970031253194544 / 2.384e-06,
  7460. 6.6280e-05 * 0.970031253194544 / 2.384e-06, /* 5 */
  7461. 4.292e-06 * 0.970031253194544 / 2.384e-06,
  7462. -1.3828e-05 * 0.970031253194544 / 2.384e-06,
  7463. 6.61850e-04 * 0.970031253194544 / 2.384e-06,
  7464. 8.487225e-03 * 0.970031253194544 / 2.384e-06,
  7465. -2.7073860e-02 * 0.970031253194544 / 2.384e-06,
  7466. -4.174709e-03 * 0.970031253194544 / 2.384e-06,
  7467. -5.76973e-04 * 0.970031253194544 / 2.384e-06,
  7468. -3.4809e-05 * 0.970031253194544 / 2.384e-06,
  7469. 2.504869601913055e-01, 1.763842528696710e+00,
  7470. -1.431e-06 * 0.98078528040323 / 2.384e-06,
  7471. 9.5367e-05 * 0.98078528040323 / 2.384e-06,
  7472. 3.78609e-04 * 0.98078528040323 / 2.384e-06,
  7473. -2.161503e-03 * 0.98078528040323 / 2.384e-06,
  7474. 3.2754898e-02 * 0.98078528040323 / 2.384e-06,
  7475. 4.752159e-03 * 0.98078528040323 / 2.384e-06,
  7476. 8.66413e-04 * 0.98078528040323 / 2.384e-06,
  7477. 6.2943e-05 * 0.98078528040323 / 2.384e-06, /* 4 */
  7478. 3.815e-06 * 0.98078528040323 / 2.384e-06,
  7479. -2.718e-05 * 0.98078528040323 / 2.384e-06,
  7480. 5.22137e-04 * 0.98078528040323 / 2.384e-06,
  7481. 7.703304e-03 * 0.98078528040323 / 2.384e-06,
  7482. -2.7815342e-02 * 0.98078528040323 / 2.384e-06,
  7483. -4.290581e-03 * 0.98078528040323 / 2.384e-06,
  7484. -6.11782e-04 * 0.98078528040323 / 2.384e-06,
  7485. -3.7670e-05 * 0.98078528040323 / 2.384e-06,
  7486. 1.989123673796580e-01, 1.847759065022573e+00,
  7487. -1.907e-06 * 0.989176509964781 / 2.384e-06,
  7488. 9.0122e-05 * 0.989176509964781 / 2.384e-06,
  7489. 2.88486e-04 * 0.989176509964781 / 2.384e-06,
  7490. -2.774239e-03 * 0.989176509964781 / 2.384e-06,
  7491. 3.2248020e-02 * 0.989176509964781 / 2.384e-06,
  7492. 4.748821e-03 * 0.989176509964781 / 2.384e-06,
  7493. 8.38757e-04 * 0.989176509964781 / 2.384e-06,
  7494. 5.9605e-05 * 0.989176509964781 / 2.384e-06, /* 3 */
  7495. 3.338e-06 * 0.989176509964781 / 2.384e-06,
  7496. -3.9577e-05 * 0.989176509964781 / 2.384e-06,
  7497. 3.88145e-04 * 0.989176509964781 / 2.384e-06,
  7498. 6.937027e-03 * 0.989176509964781 / 2.384e-06,
  7499. -2.8532982e-02 * 0.989176509964781 / 2.384e-06,
  7500. -4.395962e-03 * 0.989176509964781 / 2.384e-06,
  7501. -6.46591e-04 * 0.989176509964781 / 2.384e-06,
  7502. -4.0531e-05 * 0.989176509964781 / 2.384e-06,
  7503. 1.483359875383474e-01, 1.913880671464418e+00,
  7504. -1.907e-06 * 0.995184726672197 / 2.384e-06,
  7505. 8.4400e-05 * 0.995184726672197 / 2.384e-06,
  7506. 1.91689e-04 * 0.995184726672197 / 2.384e-06,
  7507. -3.411293e-03 * 0.995184726672197 / 2.384e-06,
  7508. 3.1706810e-02 * 0.995184726672197 / 2.384e-06,
  7509. 4.728317e-03 * 0.995184726672197 / 2.384e-06,
  7510. 8.09669e-04 * 0.995184726672197 / 2.384e-06,
  7511. 5.579e-05 * 0.995184726672197 / 2.384e-06,
  7512. 3.338e-06 * 0.995184726672197 / 2.384e-06,
  7513. -5.0545e-05 * 0.995184726672197 / 2.384e-06,
  7514. 2.59876e-04 * 0.995184726672197 / 2.384e-06,
  7515. 6.189346e-03 * 0.995184726672197 / 2.384e-06,
  7516. -2.9224873e-02 * 0.995184726672197 / 2.384e-06,
  7517. -4.489899e-03 * 0.995184726672197 / 2.384e-06,
  7518. -6.80923e-04 * 0.995184726672197 / 2.384e-06,
  7519. -4.3392e-05 * 0.995184726672197 / 2.384e-06,
  7520. 9.849140335716425e-02, 1.961570560806461e+00,
  7521. -2.384e-06 * 0.998795456205172 / 2.384e-06,
  7522. 7.7724e-05 * 0.998795456205172 / 2.384e-06,
  7523. 8.8215e-05 * 0.998795456205172 / 2.384e-06,
  7524. -4.072189e-03 * 0.998795456205172 / 2.384e-06,
  7525. 3.1132698e-02 * 0.998795456205172 / 2.384e-06,
  7526. 4.691124e-03 * 0.998795456205172 / 2.384e-06,
  7527. 7.79152e-04 * 0.998795456205172 / 2.384e-06,
  7528. 5.2929e-05 * 0.998795456205172 / 2.384e-06,
  7529. 2.861e-06 * 0.998795456205172 / 2.384e-06,
  7530. -6.0558e-05 * 0.998795456205172 / 2.384e-06,
  7531. 1.37329e-04 * 0.998795456205172 / 2.384e-06,
  7532. 5.462170e-03 * 0.998795456205172 / 2.384e-06,
  7533. -2.9890060e-02 * 0.998795456205172 / 2.384e-06,
  7534. -4.570484e-03 * 0.998795456205172 / 2.384e-06,
  7535. -7.14302e-04 * 0.998795456205172 / 2.384e-06,
  7536. -4.6253e-05 * 0.998795456205172 / 2.384e-06,
  7537. 4.912684976946725e-02, 1.990369453344394e+00,
  7538. 3.5780907e-02 * Util.SQRT2 * 0.5 / 2.384e-06,
  7539. 1.7876148e-02 * Util.SQRT2 * 0.5 / 2.384e-06,
  7540. 3.134727e-03 * Util.SQRT2 * 0.5 / 2.384e-06,
  7541. 2.457142e-03 * Util.SQRT2 * 0.5 / 2.384e-06,
  7542. 9.71317e-04 * Util.SQRT2 * 0.5 / 2.384e-06,
  7543. 2.18868e-04 * Util.SQRT2 * 0.5 / 2.384e-06,
  7544. 1.01566e-04 * Util.SQRT2 * 0.5 / 2.384e-06,
  7545. 1.3828e-05 * Util.SQRT2 * 0.5 / 2.384e-06,
  7546. 3.0526638e-02 / 2.384e-06, 4.638195e-03 / 2.384e-06,
  7547. 7.47204e-04 / 2.384e-06, 4.9591e-05 / 2.384e-06,
  7548. 4.756451e-03 / 2.384e-06, 2.1458e-05 / 2.384e-06,
  7549. -6.9618e-05 / 2.384e-06, /* 2.384e-06/2.384e-06 */
  7550. ];
  7551. var NS = 12;
  7552. var NL = 36;
  7553. var win = [
  7554. [
  7555. 2.382191739347913e-13,
  7556. 6.423305872147834e-13,
  7557. 9.400849094049688e-13,
  7558. 1.122435026096556e-12,
  7559. 1.183840321267481e-12,
  7560. 1.122435026096556e-12,
  7561. 9.400849094049690e-13,
  7562. 6.423305872147839e-13,
  7563. 2.382191739347918e-13,
  7564. 5.456116108943412e-12,
  7565. 4.878985199565852e-12,
  7566. 4.240448995017367e-12,
  7567. 3.559909094758252e-12,
  7568. 2.858043359288075e-12,
  7569. 2.156177623817898e-12,
  7570. 1.475637723558783e-12,
  7571. 8.371015190102974e-13,
  7572. 2.599706096327376e-13,
  7573. -5.456116108943412e-12,
  7574. -4.878985199565852e-12,
  7575. -4.240448995017367e-12,
  7576. -3.559909094758252e-12,
  7577. -2.858043359288076e-12,
  7578. -2.156177623817898e-12,
  7579. -1.475637723558783e-12,
  7580. -8.371015190102975e-13,
  7581. -2.599706096327376e-13,
  7582. -2.382191739347923e-13,
  7583. -6.423305872147843e-13,
  7584. -9.400849094049696e-13,
  7585. -1.122435026096556e-12,
  7586. -1.183840321267481e-12,
  7587. -1.122435026096556e-12,
  7588. -9.400849094049694e-13,
  7589. -6.423305872147840e-13,
  7590. -2.382191739347918e-13,
  7591. ],
  7592. [
  7593. 2.382191739347913e-13,
  7594. 6.423305872147834e-13,
  7595. 9.400849094049688e-13,
  7596. 1.122435026096556e-12,
  7597. 1.183840321267481e-12,
  7598. 1.122435026096556e-12,
  7599. 9.400849094049688e-13,
  7600. 6.423305872147841e-13,
  7601. 2.382191739347918e-13,
  7602. 5.456116108943413e-12,
  7603. 4.878985199565852e-12,
  7604. 4.240448995017367e-12,
  7605. 3.559909094758253e-12,
  7606. 2.858043359288075e-12,
  7607. 2.156177623817898e-12,
  7608. 1.475637723558782e-12,
  7609. 8.371015190102975e-13,
  7610. 2.599706096327376e-13,
  7611. -5.461314069809755e-12,
  7612. -4.921085770524055e-12,
  7613. -4.343405037091838e-12,
  7614. -3.732668368707687e-12,
  7615. -3.093523840190885e-12,
  7616. -2.430835727329465e-12,
  7617. -1.734679010007751e-12,
  7618. -9.748253656609281e-13,
  7619. -2.797435120168326e-13,
  7620. 0.000000000000000e+00,
  7621. 0.000000000000000e+00,
  7622. 0.000000000000000e+00,
  7623. 0.000000000000000e+00,
  7624. 0.000000000000000e+00,
  7625. 0.000000000000000e+00,
  7626. -2.283748241799531e-13,
  7627. -4.037858874020686e-13,
  7628. -2.146547464825323e-13,
  7629. ],
  7630. [
  7631. 1.316524975873958e-01, /* win[SHORT_TYPE] */
  7632. 4.142135623730950e-01,
  7633. 7.673269879789602e-01,
  7634. 1.091308501069271e+00, /* tantab_l */
  7635. 1.303225372841206e+00,
  7636. 1.569685577117490e+00,
  7637. 1.920982126971166e+00,
  7638. 2.414213562373094e+00,
  7639. 3.171594802363212e+00,
  7640. 4.510708503662055e+00,
  7641. 7.595754112725146e+00,
  7642. 2.290376554843115e+01,
  7643. 0.98480775301220802032, /* cx */
  7644. 0.64278760968653936292,
  7645. 0.34202014332566882393,
  7646. 0.93969262078590842791,
  7647. -0.17364817766693030343,
  7648. -0.76604444311897790243,
  7649. 0.86602540378443870761,
  7650. 0.500000000000000e+00,
  7651. -5.144957554275265e-01, /* ca */
  7652. -4.717319685649723e-01,
  7653. -3.133774542039019e-01,
  7654. -1.819131996109812e-01,
  7655. -9.457419252642064e-02,
  7656. -4.096558288530405e-02,
  7657. -1.419856857247115e-02,
  7658. -3.699974673760037e-03,
  7659. 8.574929257125442e-01, /* cs */
  7660. 8.817419973177052e-01,
  7661. 9.496286491027329e-01,
  7662. 9.833145924917901e-01,
  7663. 9.955178160675857e-01,
  7664. 9.991605581781475e-01,
  7665. 9.998991952444470e-01,
  7666. 9.999931550702802e-01,
  7667. ],
  7668. [
  7669. 0.000000000000000e+00,
  7670. 0.000000000000000e+00,
  7671. 0.000000000000000e+00,
  7672. 0.000000000000000e+00,
  7673. 0.000000000000000e+00,
  7674. 0.000000000000000e+00,
  7675. 2.283748241799531e-13,
  7676. 4.037858874020686e-13,
  7677. 2.146547464825323e-13,
  7678. 5.461314069809755e-12,
  7679. 4.921085770524055e-12,
  7680. 4.343405037091838e-12,
  7681. 3.732668368707687e-12,
  7682. 3.093523840190885e-12,
  7683. 2.430835727329466e-12,
  7684. 1.734679010007751e-12,
  7685. 9.748253656609281e-13,
  7686. 2.797435120168326e-13,
  7687. -5.456116108943413e-12,
  7688. -4.878985199565852e-12,
  7689. -4.240448995017367e-12,
  7690. -3.559909094758253e-12,
  7691. -2.858043359288075e-12,
  7692. -2.156177623817898e-12,
  7693. -1.475637723558782e-12,
  7694. -8.371015190102975e-13,
  7695. -2.599706096327376e-13,
  7696. -2.382191739347913e-13,
  7697. -6.423305872147834e-13,
  7698. -9.400849094049688e-13,
  7699. -1.122435026096556e-12,
  7700. -1.183840321267481e-12,
  7701. -1.122435026096556e-12,
  7702. -9.400849094049688e-13,
  7703. -6.423305872147841e-13,
  7704. -2.382191739347918e-13,
  7705. ]
  7706. ];
  7707. var tantab_l = win[Encoder.SHORT_TYPE];
  7708. var cx = win[Encoder.SHORT_TYPE];
  7709. var ca = win[Encoder.SHORT_TYPE];
  7710. var cs = win[Encoder.SHORT_TYPE];
  7711. /**
  7712. * new IDCT routine written by Takehiro TOMINAGA
  7713. *
  7714. * PURPOSE: Overlapping window on PCM samples<BR>
  7715. *
  7716. * SEMANTICS:<BR>
  7717. * 32 16-bit pcm samples are scaled to fractional 2's complement and
  7718. * concatenated to the end of the window buffer #x#. The updated window
  7719. * buffer #x# is then windowed by the analysis window #c# to produce the
  7720. * windowed sample #z#
  7721. */
  7722. var order = [
  7723. 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29,
  7724. 2, 3, 18, 19, 10, 11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31
  7725. ];
  7726. /**
  7727. * returns sum_j=0^31 a[j]*cos(PI*j*(k+1/2)/32), 0<=k<32
  7728. */
  7729. function window_subband(x1, x1Pos, a) {
  7730. var wp = 10;
  7731. var x2 = x1Pos + 238 - 14 - 286;
  7732. for (var i = -15; i < 0; i++) {
  7733. var w, s, t;
  7734. w = enwindow[wp + -10];
  7735. s = x1[x2 + -224] * w;
  7736. t = x1[x1Pos + 224] * w;
  7737. w = enwindow[wp + -9];
  7738. s += x1[x2 + -160] * w;
  7739. t += x1[x1Pos + 160] * w;
  7740. w = enwindow[wp + -8];
  7741. s += x1[x2 + -96] * w;
  7742. t += x1[x1Pos + 96] * w;
  7743. w = enwindow[wp + -7];
  7744. s += x1[x2 + -32] * w;
  7745. t += x1[x1Pos + 32] * w;
  7746. w = enwindow[wp + -6];
  7747. s += x1[x2 + 32] * w;
  7748. t += x1[x1Pos + -32] * w;
  7749. w = enwindow[wp + -5];
  7750. s += x1[x2 + 96] * w;
  7751. t += x1[x1Pos + -96] * w;
  7752. w = enwindow[wp + -4];
  7753. s += x1[x2 + 160] * w;
  7754. t += x1[x1Pos + -160] * w;
  7755. w = enwindow[wp + -3];
  7756. s += x1[x2 + 224] * w;
  7757. t += x1[x1Pos + -224] * w;
  7758. w = enwindow[wp + -2];
  7759. s += x1[x1Pos + -256] * w;
  7760. t -= x1[x2 + 256] * w;
  7761. w = enwindow[wp + -1];
  7762. s += x1[x1Pos + -192] * w;
  7763. t -= x1[x2 + 192] * w;
  7764. w = enwindow[wp + 0];
  7765. s += x1[x1Pos + -128] * w;
  7766. t -= x1[x2 + 128] * w;
  7767. w = enwindow[wp + 1];
  7768. s += x1[x1Pos + -64] * w;
  7769. t -= x1[x2 + 64] * w;
  7770. w = enwindow[wp + 2];
  7771. s += x1[x1Pos + 0] * w;
  7772. t -= x1[x2 + 0] * w;
  7773. w = enwindow[wp + 3];
  7774. s += x1[x1Pos + 64] * w;
  7775. t -= x1[x2 + -64] * w;
  7776. w = enwindow[wp + 4];
  7777. s += x1[x1Pos + 128] * w;
  7778. t -= x1[x2 + -128] * w;
  7779. w = enwindow[wp + 5];
  7780. s += x1[x1Pos + 192] * w;
  7781. t -= x1[x2 + -192] * w;
  7782. /*
  7783. * this multiplyer could be removed, but it needs more 256 FLOAT
  7784. * data. thinking about the data cache performance, I think we
  7785. * should not use such a huge table. tt 2000/Oct/25
  7786. */
  7787. s *= enwindow[wp + 6];
  7788. w = t - s;
  7789. a[30 + i * 2] = t + s;
  7790. a[31 + i * 2] = enwindow[wp + 7] * w;
  7791. wp += 18;
  7792. x1Pos--;
  7793. x2++;
  7794. }
  7795. {
  7796. var s, t, u, v;
  7797. t = x1[x1Pos + -16] * enwindow[wp + -10];
  7798. s = x1[x1Pos + -32] * enwindow[wp + -2];
  7799. t += (x1[x1Pos + -48] - x1[x1Pos + 16]) * enwindow[wp + -9];
  7800. s += x1[x1Pos + -96] * enwindow[wp + -1];
  7801. t += (x1[x1Pos + -80] + x1[x1Pos + 48]) * enwindow[wp + -8];
  7802. s += x1[x1Pos + -160] * enwindow[wp + 0];
  7803. t += (x1[x1Pos + -112] - x1[x1Pos + 80]) * enwindow[wp + -7];
  7804. s += x1[x1Pos + -224] * enwindow[wp + 1];
  7805. t += (x1[x1Pos + -144] + x1[x1Pos + 112]) * enwindow[wp + -6];
  7806. s -= x1[x1Pos + 32] * enwindow[wp + 2];
  7807. t += (x1[x1Pos + -176] - x1[x1Pos + 144]) * enwindow[wp + -5];
  7808. s -= x1[x1Pos + 96] * enwindow[wp + 3];
  7809. t += (x1[x1Pos + -208] + x1[x1Pos + 176]) * enwindow[wp + -4];
  7810. s -= x1[x1Pos + 160] * enwindow[wp + 4];
  7811. t += (x1[x1Pos + -240] - x1[x1Pos + 208]) * enwindow[wp + -3];
  7812. s -= x1[x1Pos + 224];
  7813. u = s - t;
  7814. v = s + t;
  7815. t = a[14];
  7816. s = a[15] - t;
  7817. a[31] = v + t; /* A0 */
  7818. a[30] = u + s; /* A1 */
  7819. a[15] = u - s; /* A2 */
  7820. a[14] = v - t; /* A3 */
  7821. }
  7822. {
  7823. var xr;
  7824. xr = a[28] - a[0];
  7825. a[0] += a[28];
  7826. a[28] = xr * enwindow[wp + -2 * 18 + 7];
  7827. xr = a[29] - a[1];
  7828. a[1] += a[29];
  7829. a[29] = xr * enwindow[wp + -2 * 18 + 7];
  7830. xr = a[26] - a[2];
  7831. a[2] += a[26];
  7832. a[26] = xr * enwindow[wp + -4 * 18 + 7];
  7833. xr = a[27] - a[3];
  7834. a[3] += a[27];
  7835. a[27] = xr * enwindow[wp + -4 * 18 + 7];
  7836. xr = a[24] - a[4];
  7837. a[4] += a[24];
  7838. a[24] = xr * enwindow[wp + -6 * 18 + 7];
  7839. xr = a[25] - a[5];
  7840. a[5] += a[25];
  7841. a[25] = xr * enwindow[wp + -6 * 18 + 7];
  7842. xr = a[22] - a[6];
  7843. a[6] += a[22];
  7844. a[22] = xr * Util.SQRT2;
  7845. xr = a[23] - a[7];
  7846. a[7] += a[23];
  7847. a[23] = xr * Util.SQRT2 - a[7];
  7848. a[7] -= a[6];
  7849. a[22] -= a[7];
  7850. a[23] -= a[22];
  7851. xr = a[6];
  7852. a[6] = a[31] - xr;
  7853. a[31] = a[31] + xr;
  7854. xr = a[7];
  7855. a[7] = a[30] - xr;
  7856. a[30] = a[30] + xr;
  7857. xr = a[22];
  7858. a[22] = a[15] - xr;
  7859. a[15] = a[15] + xr;
  7860. xr = a[23];
  7861. a[23] = a[14] - xr;
  7862. a[14] = a[14] + xr;
  7863. xr = a[20] - a[8];
  7864. a[8] += a[20];
  7865. a[20] = xr * enwindow[wp + -10 * 18 + 7];
  7866. xr = a[21] - a[9];
  7867. a[9] += a[21];
  7868. a[21] = xr * enwindow[wp + -10 * 18 + 7];
  7869. xr = a[18] - a[10];
  7870. a[10] += a[18];
  7871. a[18] = xr * enwindow[wp + -12 * 18 + 7];
  7872. xr = a[19] - a[11];
  7873. a[11] += a[19];
  7874. a[19] = xr * enwindow[wp + -12 * 18 + 7];
  7875. xr = a[16] - a[12];
  7876. a[12] += a[16];
  7877. a[16] = xr * enwindow[wp + -14 * 18 + 7];
  7878. xr = a[17] - a[13];
  7879. a[13] += a[17];
  7880. a[17] = xr * enwindow[wp + -14 * 18 + 7];
  7881. xr = -a[20] + a[24];
  7882. a[20] += a[24];
  7883. a[24] = xr * enwindow[wp + -12 * 18 + 7];
  7884. xr = -a[21] + a[25];
  7885. a[21] += a[25];
  7886. a[25] = xr * enwindow[wp + -12 * 18 + 7];
  7887. xr = a[4] - a[8];
  7888. a[4] += a[8];
  7889. a[8] = xr * enwindow[wp + -12 * 18 + 7];
  7890. xr = a[5] - a[9];
  7891. a[5] += a[9];
  7892. a[9] = xr * enwindow[wp + -12 * 18 + 7];
  7893. xr = a[0] - a[12];
  7894. a[0] += a[12];
  7895. a[12] = xr * enwindow[wp + -4 * 18 + 7];
  7896. xr = a[1] - a[13];
  7897. a[1] += a[13];
  7898. a[13] = xr * enwindow[wp + -4 * 18 + 7];
  7899. xr = a[16] - a[28];
  7900. a[16] += a[28];
  7901. a[28] = xr * enwindow[wp + -4 * 18 + 7];
  7902. xr = -a[17] + a[29];
  7903. a[17] += a[29];
  7904. a[29] = xr * enwindow[wp + -4 * 18 + 7];
  7905. xr = Util.SQRT2 * (a[2] - a[10]);
  7906. a[2] += a[10];
  7907. a[10] = xr;
  7908. xr = Util.SQRT2 * (a[3] - a[11]);
  7909. a[3] += a[11];
  7910. a[11] = xr;
  7911. xr = Util.SQRT2 * (-a[18] + a[26]);
  7912. a[18] += a[26];
  7913. a[26] = xr - a[18];
  7914. xr = Util.SQRT2 * (-a[19] + a[27]);
  7915. a[19] += a[27];
  7916. a[27] = xr - a[19];
  7917. xr = a[2];
  7918. a[19] -= a[3];
  7919. a[3] -= xr;
  7920. a[2] = a[31] - xr;
  7921. a[31] += xr;
  7922. xr = a[3];
  7923. a[11] -= a[19];
  7924. a[18] -= xr;
  7925. a[3] = a[30] - xr;
  7926. a[30] += xr;
  7927. xr = a[18];
  7928. a[27] -= a[11];
  7929. a[19] -= xr;
  7930. a[18] = a[15] - xr;
  7931. a[15] += xr;
  7932. xr = a[19];
  7933. a[10] -= xr;
  7934. a[19] = a[14] - xr;
  7935. a[14] += xr;
  7936. xr = a[10];
  7937. a[11] -= xr;
  7938. a[10] = a[23] - xr;
  7939. a[23] += xr;
  7940. xr = a[11];
  7941. a[26] -= xr;
  7942. a[11] = a[22] - xr;
  7943. a[22] += xr;
  7944. xr = a[26];
  7945. a[27] -= xr;
  7946. a[26] = a[7] - xr;
  7947. a[7] += xr;
  7948. xr = a[27];
  7949. a[27] = a[6] - xr;
  7950. a[6] += xr;
  7951. xr = Util.SQRT2 * (a[0] - a[4]);
  7952. a[0] += a[4];
  7953. a[4] = xr;
  7954. xr = Util.SQRT2 * (a[1] - a[5]);
  7955. a[1] += a[5];
  7956. a[5] = xr;
  7957. xr = Util.SQRT2 * (a[16] - a[20]);
  7958. a[16] += a[20];
  7959. a[20] = xr;
  7960. xr = Util.SQRT2 * (a[17] - a[21]);
  7961. a[17] += a[21];
  7962. a[21] = xr;
  7963. xr = -Util.SQRT2 * (a[8] - a[12]);
  7964. a[8] += a[12];
  7965. a[12] = xr - a[8];
  7966. xr = -Util.SQRT2 * (a[9] - a[13]);
  7967. a[9] += a[13];
  7968. a[13] = xr - a[9];
  7969. xr = -Util.SQRT2 * (a[25] - a[29]);
  7970. a[25] += a[29];
  7971. a[29] = xr - a[25];
  7972. xr = -Util.SQRT2 * (a[24] + a[28]);
  7973. a[24] -= a[28];
  7974. a[28] = xr - a[24];
  7975. xr = a[24] - a[16];
  7976. a[24] = xr;
  7977. xr = a[20] - xr;
  7978. a[20] = xr;
  7979. xr = a[28] - xr;
  7980. a[28] = xr;
  7981. xr = a[25] - a[17];
  7982. a[25] = xr;
  7983. xr = a[21] - xr;
  7984. a[21] = xr;
  7985. xr = a[29] - xr;
  7986. a[29] = xr;
  7987. xr = a[17] - a[1];
  7988. a[17] = xr;
  7989. xr = a[9] - xr;
  7990. a[9] = xr;
  7991. xr = a[25] - xr;
  7992. a[25] = xr;
  7993. xr = a[5] - xr;
  7994. a[5] = xr;
  7995. xr = a[21] - xr;
  7996. a[21] = xr;
  7997. xr = a[13] - xr;
  7998. a[13] = xr;
  7999. xr = a[29] - xr;
  8000. a[29] = xr;
  8001. xr = a[1] - a[0];
  8002. a[1] = xr;
  8003. xr = a[16] - xr;
  8004. a[16] = xr;
  8005. xr = a[17] - xr;
  8006. a[17] = xr;
  8007. xr = a[8] - xr;
  8008. a[8] = xr;
  8009. xr = a[9] - xr;
  8010. a[9] = xr;
  8011. xr = a[24] - xr;
  8012. a[24] = xr;
  8013. xr = a[25] - xr;
  8014. a[25] = xr;
  8015. xr = a[4] - xr;
  8016. a[4] = xr;
  8017. xr = a[5] - xr;
  8018. a[5] = xr;
  8019. xr = a[20] - xr;
  8020. a[20] = xr;
  8021. xr = a[21] - xr;
  8022. a[21] = xr;
  8023. xr = a[12] - xr;
  8024. a[12] = xr;
  8025. xr = a[13] - xr;
  8026. a[13] = xr;
  8027. xr = a[28] - xr;
  8028. a[28] = xr;
  8029. xr = a[29] - xr;
  8030. a[29] = xr;
  8031. xr = a[0];
  8032. a[0] += a[31];
  8033. a[31] -= xr;
  8034. xr = a[1];
  8035. a[1] += a[30];
  8036. a[30] -= xr;
  8037. xr = a[16];
  8038. a[16] += a[15];
  8039. a[15] -= xr;
  8040. xr = a[17];
  8041. a[17] += a[14];
  8042. a[14] -= xr;
  8043. xr = a[8];
  8044. a[8] += a[23];
  8045. a[23] -= xr;
  8046. xr = a[9];
  8047. a[9] += a[22];
  8048. a[22] -= xr;
  8049. xr = a[24];
  8050. a[24] += a[7];
  8051. a[7] -= xr;
  8052. xr = a[25];
  8053. a[25] += a[6];
  8054. a[6] -= xr;
  8055. xr = a[4];
  8056. a[4] += a[27];
  8057. a[27] -= xr;
  8058. xr = a[5];
  8059. a[5] += a[26];
  8060. a[26] -= xr;
  8061. xr = a[20];
  8062. a[20] += a[11];
  8063. a[11] -= xr;
  8064. xr = a[21];
  8065. a[21] += a[10];
  8066. a[10] -= xr;
  8067. xr = a[12];
  8068. a[12] += a[19];
  8069. a[19] -= xr;
  8070. xr = a[13];
  8071. a[13] += a[18];
  8072. a[18] -= xr;
  8073. xr = a[28];
  8074. a[28] += a[3];
  8075. a[3] -= xr;
  8076. xr = a[29];
  8077. a[29] += a[2];
  8078. a[2] -= xr;
  8079. }
  8080. }
  8081. /**
  8082. * Function: Calculation of the MDCT In the case of long blocks (type 0,1,3)
  8083. * there are 36 coefficents in the time domain and 18 in the frequency
  8084. * domain.<BR>
  8085. * In the case of short blocks (type 2) there are 3 transformations with
  8086. * short length. This leads to 12 coefficents in the time and 6 in the
  8087. * frequency domain. In this case the results are stored side by side in the
  8088. * vector out[].
  8089. *
  8090. * New layer3
  8091. */
  8092. function mdct_short(inout, inoutPos) {
  8093. for (var l = 0; l < 3; l++) {
  8094. var tc0, tc1, tc2, ts0, ts1, ts2;
  8095. ts0 = inout[inoutPos + 2 * 3] * win[Encoder.SHORT_TYPE][0]
  8096. - inout[inoutPos + 5 * 3];
  8097. tc0 = inout[inoutPos + 0 * 3] * win[Encoder.SHORT_TYPE][2]
  8098. - inout[inoutPos + 3 * 3];
  8099. tc1 = ts0 + tc0;
  8100. tc2 = ts0 - tc0;
  8101. ts0 = inout[inoutPos + 5 * 3] * win[Encoder.SHORT_TYPE][0]
  8102. + inout[inoutPos + 2 * 3];
  8103. tc0 = inout[inoutPos + 3 * 3] * win[Encoder.SHORT_TYPE][2]
  8104. + inout[inoutPos + 0 * 3];
  8105. ts1 = ts0 + tc0;
  8106. ts2 = -ts0 + tc0;
  8107. tc0 = (inout[inoutPos + 1 * 3] * win[Encoder.SHORT_TYPE][1] - inout[inoutPos + 4 * 3]) * 2.069978111953089e-11;
  8108. /*
  8109. * tritab_s [ 1 ]
  8110. */
  8111. ts0 = (inout[inoutPos + 4 * 3] * win[Encoder.SHORT_TYPE][1] + inout[inoutPos + 1 * 3]) * 2.069978111953089e-11;
  8112. /*
  8113. * tritab_s [ 1 ]
  8114. */
  8115. inout[inoutPos + 3 * 0] = tc1 * 1.907525191737280e-11 + tc0;
  8116. /*
  8117. * tritab_s[ 2 ]
  8118. */
  8119. inout[inoutPos + 3 * 5] = -ts1 * 1.907525191737280e-11 + ts0;
  8120. /*
  8121. * tritab_s[0 ]
  8122. */
  8123. tc2 = tc2 * 0.86602540378443870761 * 1.907525191737281e-11;
  8124. /*
  8125. * tritab_s[ 2]
  8126. */
  8127. ts1 = ts1 * 0.5 * 1.907525191737281e-11 + ts0;
  8128. inout[inoutPos + 3 * 1] = tc2 - ts1;
  8129. inout[inoutPos + 3 * 2] = tc2 + ts1;
  8130. tc1 = tc1 * 0.5 * 1.907525191737281e-11 - tc0;
  8131. ts2 = ts2 * 0.86602540378443870761 * 1.907525191737281e-11;
  8132. /*
  8133. * tritab_s[ 0]
  8134. */
  8135. inout[inoutPos + 3 * 3] = tc1 + ts2;
  8136. inout[inoutPos + 3 * 4] = tc1 - ts2;
  8137. inoutPos++;
  8138. }
  8139. }
  8140. function mdct_long(out, outPos, _in) {
  8141. var ct, st;
  8142. {
  8143. var tc1, tc2, tc3, tc4, ts5, ts6, ts7, ts8;
  8144. /* 1,2, 5,6, 9,10, 13,14, 17 */
  8145. tc1 = _in[17] - _in[9];
  8146. tc3 = _in[15] - _in[11];
  8147. tc4 = _in[14] - _in[12];
  8148. ts5 = _in[0] + _in[8];
  8149. ts6 = _in[1] + _in[7];
  8150. ts7 = _in[2] + _in[6];
  8151. ts8 = _in[3] + _in[5];
  8152. out[outPos + 17] = (ts5 + ts7 - ts8) - (ts6 - _in[4]);
  8153. st = (ts5 + ts7 - ts8) * cx[12 + 7] + (ts6 - _in[4]);
  8154. ct = (tc1 - tc3 - tc4) * cx[12 + 6];
  8155. out[outPos + 5] = ct + st;
  8156. out[outPos + 6] = ct - st;
  8157. tc2 = (_in[16] - _in[10]) * cx[12 + 6];
  8158. ts6 = ts6 * cx[12 + 7] + _in[4];
  8159. ct = tc1 * cx[12 + 0] + tc2 + tc3 * cx[12 + 1] + tc4 * cx[12 + 2];
  8160. st = -ts5 * cx[12 + 4] + ts6 - ts7 * cx[12 + 5] + ts8 * cx[12 + 3];
  8161. out[outPos + 1] = ct + st;
  8162. out[outPos + 2] = ct - st;
  8163. ct = tc1 * cx[12 + 1] - tc2 - tc3 * cx[12 + 2] + tc4 * cx[12 + 0];
  8164. st = -ts5 * cx[12 + 5] + ts6 - ts7 * cx[12 + 3] + ts8 * cx[12 + 4];
  8165. out[outPos + 9] = ct + st;
  8166. out[outPos + 10] = ct - st;
  8167. ct = tc1 * cx[12 + 2] - tc2 + tc3 * cx[12 + 0] - tc4 * cx[12 + 1];
  8168. st = ts5 * cx[12 + 3] - ts6 + ts7 * cx[12 + 4] - ts8 * cx[12 + 5];
  8169. out[outPos + 13] = ct + st;
  8170. out[outPos + 14] = ct - st;
  8171. }
  8172. {
  8173. var ts1, ts2, ts3, ts4, tc5, tc6, tc7, tc8;
  8174. ts1 = _in[8] - _in[0];
  8175. ts3 = _in[6] - _in[2];
  8176. ts4 = _in[5] - _in[3];
  8177. tc5 = _in[17] + _in[9];
  8178. tc6 = _in[16] + _in[10];
  8179. tc7 = _in[15] + _in[11];
  8180. tc8 = _in[14] + _in[12];
  8181. out[outPos + 0] = (tc5 + tc7 + tc8) + (tc6 + _in[13]);
  8182. ct = (tc5 + tc7 + tc8) * cx[12 + 7] - (tc6 + _in[13]);
  8183. st = (ts1 - ts3 + ts4) * cx[12 + 6];
  8184. out[outPos + 11] = ct + st;
  8185. out[outPos + 12] = ct - st;
  8186. ts2 = (_in[7] - _in[1]) * cx[12 + 6];
  8187. tc6 = _in[13] - tc6 * cx[12 + 7];
  8188. ct = tc5 * cx[12 + 3] - tc6 + tc7 * cx[12 + 4] + tc8 * cx[12 + 5];
  8189. st = ts1 * cx[12 + 2] + ts2 + ts3 * cx[12 + 0] + ts4 * cx[12 + 1];
  8190. out[outPos + 3] = ct + st;
  8191. out[outPos + 4] = ct - st;
  8192. ct = -tc5 * cx[12 + 5] + tc6 - tc7 * cx[12 + 3] - tc8 * cx[12 + 4];
  8193. st = ts1 * cx[12 + 1] + ts2 - ts3 * cx[12 + 2] - ts4 * cx[12 + 0];
  8194. out[outPos + 7] = ct + st;
  8195. out[outPos + 8] = ct - st;
  8196. ct = -tc5 * cx[12 + 4] + tc6 - tc7 * cx[12 + 5] - tc8 * cx[12 + 3];
  8197. st = ts1 * cx[12 + 0] - ts2 + ts3 * cx[12 + 1] - ts4 * cx[12 + 2];
  8198. out[outPos + 15] = ct + st;
  8199. out[outPos + 16] = ct - st;
  8200. }
  8201. }
  8202. this.mdct_sub48 = function(gfc, w0, w1) {
  8203. var wk = w0;
  8204. var wkPos = 286;
  8205. /* thinking cache performance, ch->gr loop is better than gr->ch loop */
  8206. for (var ch = 0; ch < gfc.channels_out; ch++) {
  8207. for (var gr = 0; gr < gfc.mode_gr; gr++) {
  8208. var band;
  8209. var gi = (gfc.l3_side.tt[gr][ch]);
  8210. var mdct_enc = gi.xr;
  8211. var mdct_encPos = 0;
  8212. var samp = gfc.sb_sample[ch][1 - gr];
  8213. var sampPos = 0;
  8214. for (var k = 0; k < 18 / 2; k++) {
  8215. window_subband(wk, wkPos, samp[sampPos]);
  8216. window_subband(wk, wkPos + 32, samp[sampPos + 1]);
  8217. sampPos += 2;
  8218. wkPos += 64;
  8219. /*
  8220. * Compensate for inversion in the analysis filter
  8221. */
  8222. for (band = 1; band < 32; band += 2) {
  8223. samp[sampPos - 1][band] *= -1;
  8224. }
  8225. }
  8226. /*
  8227. * Perform imdct of 18 previous subband samples + 18 current
  8228. * subband samples
  8229. */
  8230. for (band = 0; band < 32; band++, mdct_encPos += 18) {
  8231. var type = gi.block_type;
  8232. var band0 = gfc.sb_sample[ch][gr];
  8233. var band1 = gfc.sb_sample[ch][1 - gr];
  8234. if (gi.mixed_block_flag != 0 && band < 2)
  8235. type = 0;
  8236. if (gfc.amp_filter[band] < 1e-12) {
  8237. Arrays.fill(mdct_enc, mdct_encPos + 0,
  8238. mdct_encPos + 18, 0);
  8239. } else {
  8240. if (gfc.amp_filter[band] < 1.0) {
  8241. for (var k = 0; k < 18; k++)
  8242. band1[k][order[band]] *= gfc.amp_filter[band];
  8243. }
  8244. if (type == Encoder.SHORT_TYPE) {
  8245. for (var k = -NS / 4; k < 0; k++) {
  8246. var w = win[Encoder.SHORT_TYPE][k + 3];
  8247. mdct_enc[mdct_encPos + k * 3 + 9] = band0[9 + k][order[band]]
  8248. * w - band0[8 - k][order[band]];
  8249. mdct_enc[mdct_encPos + k * 3 + 18] = band0[14 - k][order[band]]
  8250. * w + band0[15 + k][order[band]];
  8251. mdct_enc[mdct_encPos + k * 3 + 10] = band0[15 + k][order[band]]
  8252. * w - band0[14 - k][order[band]];
  8253. mdct_enc[mdct_encPos + k * 3 + 19] = band1[2 - k][order[band]]
  8254. * w + band1[3 + k][order[band]];
  8255. mdct_enc[mdct_encPos + k * 3 + 11] = band1[3 + k][order[band]]
  8256. * w - band1[2 - k][order[band]];
  8257. mdct_enc[mdct_encPos + k * 3 + 20] = band1[8 - k][order[band]]
  8258. * w + band1[9 + k][order[band]];
  8259. }
  8260. mdct_short(mdct_enc, mdct_encPos);
  8261. } else {
  8262. var work = new_float(18);
  8263. for (var k = -NL / 4; k < 0; k++) {
  8264. var a, b;
  8265. a = win[type][k + 27]
  8266. * band1[k + 9][order[band]]
  8267. + win[type][k + 36]
  8268. * band1[8 - k][order[band]];
  8269. b = win[type][k + 9]
  8270. * band0[k + 9][order[band]]
  8271. - win[type][k + 18]
  8272. * band0[8 - k][order[band]];
  8273. work[k + 9] = a - b * tantab_l[3 + k + 9];
  8274. work[k + 18] = a * tantab_l[3 + k + 9] + b;
  8275. }
  8276. mdct_long(mdct_enc, mdct_encPos, work);
  8277. }
  8278. }
  8279. /*
  8280. * Perform aliasing reduction butterfly
  8281. */
  8282. if (type != Encoder.SHORT_TYPE && band != 0) {
  8283. for (var k = 7; k >= 0; --k) {
  8284. var bu, bd;
  8285. bu = mdct_enc[mdct_encPos + k] * ca[20 + k]
  8286. + mdct_enc[mdct_encPos + -1 - k]
  8287. * cs[28 + k];
  8288. bd = mdct_enc[mdct_encPos + k] * cs[28 + k]
  8289. - mdct_enc[mdct_encPos + -1 - k]
  8290. * ca[20 + k];
  8291. mdct_enc[mdct_encPos + -1 - k] = bu;
  8292. mdct_enc[mdct_encPos + k] = bd;
  8293. }
  8294. }
  8295. }
  8296. }
  8297. wk = w1;
  8298. wkPos = 286;
  8299. if (gfc.mode_gr == 1) {
  8300. for (var i = 0; i < 18; i++) {
  8301. System.arraycopy(gfc.sb_sample[ch][1][i], 0,
  8302. gfc.sb_sample[ch][0][i], 0, 32);
  8303. }
  8304. }
  8305. }
  8306. }
  8307. }
  8308. //package mp3;
  8309. function III_psy_ratio() {
  8310. this.thm = new III_psy_xmin();
  8311. this.en = new III_psy_xmin();
  8312. }
  8313. /**
  8314. * ENCDELAY The encoder delay.
  8315. *
  8316. * Minimum allowed is MDCTDELAY (see below)
  8317. *
  8318. * The first 96 samples will be attenuated, so using a value less than 96
  8319. * will result in corrupt data for the first 96-ENCDELAY samples.
  8320. *
  8321. * suggested: 576 set to 1160 to sync with FhG.
  8322. */
  8323. Encoder.ENCDELAY = 576;
  8324. /**
  8325. * make sure there is at least one complete frame after the last frame
  8326. * containing real data
  8327. *
  8328. * Using a value of 288 would be sufficient for a a very sophisticated
  8329. * decoder that can decode granule-by-granule instead of frame by frame. But
  8330. * lets not assume this, and assume the decoder will not decode frame N
  8331. * unless it also has data for frame N+1
  8332. */
  8333. Encoder.POSTDELAY = 1152;
  8334. /**
  8335. * delay of the MDCT used in mdct.c original ISO routines had a delay of
  8336. * 528! Takehiro's routines:
  8337. */
  8338. Encoder.MDCTDELAY = 48;
  8339. Encoder.FFTOFFSET = (224 + Encoder.MDCTDELAY);
  8340. /**
  8341. * Most decoders, including the one we use, have a delay of 528 samples.
  8342. */
  8343. Encoder.DECDELAY = 528;
  8344. /**
  8345. * number of subbands
  8346. */
  8347. Encoder.SBLIMIT = 32;
  8348. /**
  8349. * parition bands bands
  8350. */
  8351. Encoder.CBANDS = 64;
  8352. /**
  8353. * number of critical bands/scale factor bands where masking is computed
  8354. */
  8355. Encoder.SBPSY_l = 21;
  8356. Encoder.SBPSY_s = 12;
  8357. /**
  8358. * total number of scalefactor bands encoded
  8359. */
  8360. Encoder.SBMAX_l = 22;
  8361. Encoder.SBMAX_s = 13;
  8362. Encoder.PSFB21 = 6;
  8363. Encoder.PSFB12 = 6;
  8364. /**
  8365. * FFT sizes
  8366. */
  8367. Encoder.BLKSIZE = 1024;
  8368. Encoder.HBLKSIZE = (Encoder.BLKSIZE / 2 + 1);
  8369. Encoder.BLKSIZE_s = 256;
  8370. Encoder.HBLKSIZE_s = (Encoder.BLKSIZE_s / 2 + 1);
  8371. Encoder.NORM_TYPE = 0;
  8372. Encoder.START_TYPE = 1;
  8373. Encoder.SHORT_TYPE = 2;
  8374. Encoder.STOP_TYPE = 3;
  8375. /**
  8376. * <PRE>
  8377. * Mode Extention:
  8378. * When we are in stereo mode, there are 4 possible methods to store these
  8379. * two channels. The stereo modes -m? are using a subset of them.
  8380. *
  8381. * -ms: MPG_MD_LR_LR
  8382. * -mj: MPG_MD_LR_LR and MPG_MD_MS_LR
  8383. * -mf: MPG_MD_MS_LR
  8384. * -mi: all
  8385. * </PRE>
  8386. */
  8387. Encoder.MPG_MD_LR_LR = 0;
  8388. Encoder.MPG_MD_LR_I = 1;
  8389. Encoder.MPG_MD_MS_LR = 2;
  8390. Encoder.MPG_MD_MS_I = 3;
  8391. Encoder.fircoef = [-0.0207887 * 5, -0.0378413 * 5,
  8392. -0.0432472 * 5, -0.031183 * 5, 7.79609e-18 * 5, 0.0467745 * 5,
  8393. 0.10091 * 5, 0.151365 * 5, 0.187098 * 5];
  8394. function Encoder() {
  8395. var FFTOFFSET = Encoder.FFTOFFSET;
  8396. var MPG_MD_MS_LR = Encoder.MPG_MD_MS_LR;
  8397. //BitStream bs;
  8398. //PsyModel psy;
  8399. //VBRTag vbr;
  8400. //QuantizePVT qupvt;
  8401. var bs = null;
  8402. this.psy = null;
  8403. var psy = null;
  8404. var vbr = null;
  8405. var qupvt = null;
  8406. //public final void setModules(BitStream bs, PsyModel psy, QuantizePVT qupvt,
  8407. // VBRTag vbr) {
  8408. this.setModules = function (_bs, _psy, _qupvt, _vbr) {
  8409. bs = _bs;
  8410. this.psy = _psy;
  8411. psy = _psy;
  8412. vbr = _vbr;
  8413. qupvt = _qupvt;
  8414. };
  8415. var newMDCT = new NewMDCT();
  8416. /***********************************************************************
  8417. *
  8418. * encoder and decoder delays
  8419. *
  8420. ***********************************************************************/
  8421. /**
  8422. * <PRE>
  8423. * layer III enc->dec delay: 1056 (1057?) (observed)
  8424. * layer II enc->dec delay: 480 (481?) (observed)
  8425. *
  8426. * polyphase 256-16 (dec or enc) = 240
  8427. * mdct 256+32 (9*32) (dec or enc) = 288
  8428. * total: 512+16
  8429. *
  8430. * My guess is that delay of polyphase filterbank is actualy 240.5
  8431. * (there are technical reasons for this, see postings in mp3encoder).
  8432. * So total Encode+Decode delay = ENCDELAY + 528 + 1
  8433. * </PRE>
  8434. */
  8435. /**
  8436. * auto-adjust of ATH, useful for low volume Gabriel Bouvigne 3 feb 2001
  8437. *
  8438. * modifies some values in gfp.internal_flags.ATH (gfc.ATH)
  8439. */
  8440. //private void adjust_ATH(final LameInternalFlags gfc) {
  8441. function adjust_ATH(gfc) {
  8442. var gr2_max, max_pow;
  8443. if (gfc.ATH.useAdjust == 0) {
  8444. gfc.ATH.adjust = 1.0;
  8445. /* no adjustment */
  8446. return;
  8447. }
  8448. /* jd - 2001 mar 12, 27, jun 30 */
  8449. /* loudness based on equal loudness curve; */
  8450. /* use granule with maximum combined loudness */
  8451. max_pow = gfc.loudness_sq[0][0];
  8452. gr2_max = gfc.loudness_sq[1][0];
  8453. if (gfc.channels_out == 2) {
  8454. max_pow += gfc.loudness_sq[0][1];
  8455. gr2_max += gfc.loudness_sq[1][1];
  8456. } else {
  8457. max_pow += max_pow;
  8458. gr2_max += gr2_max;
  8459. }
  8460. if (gfc.mode_gr == 2) {
  8461. max_pow = Math.max(max_pow, gr2_max);
  8462. }
  8463. max_pow *= 0.5;
  8464. /* max_pow approaches 1.0 for full band noise */
  8465. /* jd - 2001 mar 31, jun 30 */
  8466. /* user tuning of ATH adjustment region */
  8467. max_pow *= gfc.ATH.aaSensitivityP;
  8468. /*
  8469. * adjust ATH depending on range of maximum value
  8470. */
  8471. /* jd - 2001 feb27, mar12,20, jun30, jul22 */
  8472. /* continuous curves based on approximation */
  8473. /* to GB's original values. */
  8474. /* For an increase in approximate loudness, */
  8475. /* set ATH adjust to adjust_limit immediately */
  8476. /* after a delay of one frame. */
  8477. /* For a loudness decrease, reduce ATH adjust */
  8478. /* towards adjust_limit gradually. */
  8479. /* max_pow is a loudness squared or a power. */
  8480. if (max_pow > 0.03125) { /* ((1 - 0.000625)/ 31.98) from curve below */
  8481. if (gfc.ATH.adjust >= 1.0) {
  8482. gfc.ATH.adjust = 1.0;
  8483. } else {
  8484. /* preceding frame has lower ATH adjust; */
  8485. /* ascend only to the preceding adjust_limit */
  8486. /* in case there is leading low volume */
  8487. if (gfc.ATH.adjust < gfc.ATH.adjustLimit) {
  8488. gfc.ATH.adjust = gfc.ATH.adjustLimit;
  8489. }
  8490. }
  8491. gfc.ATH.adjustLimit = 1.0;
  8492. } else { /* adjustment curve */
  8493. /* about 32 dB maximum adjust (0.000625) */
  8494. var adj_lim_new = 31.98 * max_pow + 0.000625;
  8495. if (gfc.ATH.adjust >= adj_lim_new) { /* descend gradually */
  8496. gfc.ATH.adjust *= adj_lim_new * 0.075 + 0.925;
  8497. if (gfc.ATH.adjust < adj_lim_new) { /* stop descent */
  8498. gfc.ATH.adjust = adj_lim_new;
  8499. }
  8500. } else { /* ascend */
  8501. if (gfc.ATH.adjustLimit >= adj_lim_new) {
  8502. gfc.ATH.adjust = adj_lim_new;
  8503. } else {
  8504. /* preceding frame has lower ATH adjust; */
  8505. /* ascend only to the preceding adjust_limit */
  8506. if (gfc.ATH.adjust < gfc.ATH.adjustLimit) {
  8507. gfc.ATH.adjust = gfc.ATH.adjustLimit;
  8508. }
  8509. }
  8510. }
  8511. gfc.ATH.adjustLimit = adj_lim_new;
  8512. }
  8513. }
  8514. /**
  8515. * <PRE>
  8516. * some simple statistics
  8517. *
  8518. * bitrate index 0: free bitrate . not allowed in VBR mode
  8519. * : bitrates, kbps depending on MPEG version
  8520. * bitrate index 15: forbidden
  8521. *
  8522. * mode_ext:
  8523. * 0: LR
  8524. * 1: LR-i
  8525. * 2: MS
  8526. * 3: MS-i
  8527. * </PRE>
  8528. */
  8529. function updateStats(gfc) {
  8530. var gr, ch;
  8531. /* count bitrate indices */
  8532. gfc.bitrate_stereoMode_Hist[gfc.bitrate_index][4]++;
  8533. gfc.bitrate_stereoMode_Hist[15][4]++;
  8534. /* count 'em for every mode extension in case of 2 channel encoding */
  8535. if (gfc.channels_out == 2) {
  8536. gfc.bitrate_stereoMode_Hist[gfc.bitrate_index][gfc.mode_ext]++;
  8537. gfc.bitrate_stereoMode_Hist[15][gfc.mode_ext]++;
  8538. }
  8539. for (gr = 0; gr < gfc.mode_gr; ++gr) {
  8540. for (ch = 0; ch < gfc.channels_out; ++ch) {
  8541. var bt = gfc.l3_side.tt[gr][ch].block_type | 0;
  8542. if (gfc.l3_side.tt[gr][ch].mixed_block_flag != 0)
  8543. bt = 4;
  8544. gfc.bitrate_blockType_Hist[gfc.bitrate_index][bt]++;
  8545. gfc.bitrate_blockType_Hist[gfc.bitrate_index][5]++;
  8546. gfc.bitrate_blockType_Hist[15][bt]++;
  8547. gfc.bitrate_blockType_Hist[15][5]++;
  8548. }
  8549. }
  8550. }
  8551. function lame_encode_frame_init(gfp, inbuf) {
  8552. var gfc = gfp.internal_flags;
  8553. var ch, gr;
  8554. if (gfc.lame_encode_frame_init == 0) {
  8555. /* prime the MDCT/polyphase filterbank with a short block */
  8556. var i, j;
  8557. var primebuff0 = new_float(286 + 1152 + 576);
  8558. var primebuff1 = new_float(286 + 1152 + 576);
  8559. gfc.lame_encode_frame_init = 1;
  8560. for (i = 0, j = 0; i < 286 + 576 * (1 + gfc.mode_gr); ++i) {
  8561. if (i < 576 * gfc.mode_gr) {
  8562. primebuff0[i] = 0;
  8563. if (gfc.channels_out == 2)
  8564. primebuff1[i] = 0;
  8565. } else {
  8566. primebuff0[i] = inbuf[0][j];
  8567. if (gfc.channels_out == 2)
  8568. primebuff1[i] = inbuf[1][j];
  8569. ++j;
  8570. }
  8571. }
  8572. /* polyphase filtering / mdct */
  8573. for (gr = 0; gr < gfc.mode_gr; gr++) {
  8574. for (ch = 0; ch < gfc.channels_out; ch++) {
  8575. gfc.l3_side.tt[gr][ch].block_type = Encoder.SHORT_TYPE;
  8576. }
  8577. }
  8578. newMDCT.mdct_sub48(gfc, primebuff0, primebuff1);
  8579. /* check FFT will not use a negative starting offset */
  8580. /* check if we have enough data for FFT */
  8581. /* check if we have enough data for polyphase filterbank */
  8582. }
  8583. }
  8584. /**
  8585. * <PRE>
  8586. * encodeframe() Layer 3
  8587. *
  8588. * encode a single frame
  8589. *
  8590. *
  8591. * lame_encode_frame()
  8592. *
  8593. *
  8594. * gr 0 gr 1
  8595. * inbuf: |--------------|--------------|--------------|
  8596. *
  8597. *
  8598. * Polyphase (18 windows, each shifted 32)
  8599. * gr 0:
  8600. * window1 <----512---.
  8601. * window18 <----512---.
  8602. *
  8603. * gr 1:
  8604. * window1 <----512---.
  8605. * window18 <----512---.
  8606. *
  8607. *
  8608. *
  8609. * MDCT output: |--------------|--------------|--------------|
  8610. *
  8611. * FFT's <---------1024---------.
  8612. * <---------1024-------.
  8613. *
  8614. *
  8615. *
  8616. * inbuf = buffer of PCM data size=MP3 framesize
  8617. * encoder acts on inbuf[ch][0], but output is delayed by MDCTDELAY
  8618. * so the MDCT coefficints are from inbuf[ch][-MDCTDELAY]
  8619. *
  8620. * psy-model FFT has a 1 granule delay, so we feed it data for the
  8621. * next granule.
  8622. * FFT is centered over granule: 224+576+224
  8623. * So FFT starts at: 576-224-MDCTDELAY
  8624. *
  8625. * MPEG2: FFT ends at: BLKSIZE+576-224-MDCTDELAY (1328)
  8626. * MPEG1: FFT ends at: BLKSIZE+2*576-224-MDCTDELAY (1904)
  8627. *
  8628. * MPEG2: polyphase first window: [0..511]
  8629. * 18th window: [544..1055] (1056)
  8630. * MPEG1: 36th window: [1120..1631] (1632)
  8631. * data needed: 512+framesize-32
  8632. *
  8633. * A close look newmdct.c shows that the polyphase filterbank
  8634. * only uses data from [0..510] for each window. Perhaps because the window
  8635. * used by the filterbank is zero for the last point, so Takehiro's
  8636. * code doesn't bother to compute with it.
  8637. *
  8638. * FFT starts at 576-224-MDCTDELAY (304) = 576-FFTOFFSET
  8639. *
  8640. * </PRE>
  8641. */
  8642. this.lame_encode_mp3_frame = function (gfp, inbuf_l, inbuf_r, mp3buf, mp3bufPos, mp3buf_size) {
  8643. var mp3count;
  8644. var masking_LR = new_array_n([2, 2]);
  8645. /*
  8646. * LR masking &
  8647. * energy
  8648. */
  8649. masking_LR[0][0] = new III_psy_ratio();
  8650. masking_LR[0][1] = new III_psy_ratio();
  8651. masking_LR[1][0] = new III_psy_ratio();
  8652. masking_LR[1][1] = new III_psy_ratio();
  8653. var masking_MS = new_array_n([2, 2]);
  8654. /* MS masking & energy */
  8655. masking_MS[0][0] = new III_psy_ratio();
  8656. masking_MS[0][1] = new III_psy_ratio();
  8657. masking_MS[1][0] = new III_psy_ratio();
  8658. masking_MS[1][1] = new III_psy_ratio();
  8659. //III_psy_ratio masking[][];
  8660. var masking;
  8661. /* pointer to selected maskings */
  8662. var inbuf = [null, null];
  8663. var gfc = gfp.internal_flags;
  8664. var tot_ener = new_float_n([2, 4]);
  8665. var ms_ener_ratio = [.5, .5];
  8666. var pe = [[0., 0.], [0., 0.]];
  8667. var pe_MS = [[0., 0.], [0., 0.]];
  8668. //float[][] pe_use;
  8669. var pe_use;
  8670. var ch, gr;
  8671. inbuf[0] = inbuf_l;
  8672. inbuf[1] = inbuf_r;
  8673. if (gfc.lame_encode_frame_init == 0) {
  8674. /* first run? */
  8675. lame_encode_frame_init(gfp, inbuf);
  8676. }
  8677. /********************** padding *****************************/
  8678. /**
  8679. * <PRE>
  8680. * padding method as described in
  8681. * "MPEG-Layer3 / Bitstream Syntax and Decoding"
  8682. * by Martin Sieler, Ralph Sperschneider
  8683. *
  8684. * note: there is no padding for the very first frame
  8685. *
  8686. * Robert Hegemann 2000-06-22
  8687. * </PRE>
  8688. */
  8689. gfc.padding = 0;
  8690. if ((gfc.slot_lag -= gfc.frac_SpF) < 0) {
  8691. gfc.slot_lag += gfp.out_samplerate;
  8692. gfc.padding = 1;
  8693. }
  8694. /****************************************
  8695. * Stage 1: psychoacoustic model *
  8696. ****************************************/
  8697. if (gfc.psymodel != 0) {
  8698. /*
  8699. * psychoacoustic model psy model has a 1 granule (576) delay that
  8700. * we must compensate for (mt 6/99).
  8701. */
  8702. var ret;
  8703. var bufp = [null, null];
  8704. /* address of beginning of left & right granule */
  8705. var bufpPos = 0;
  8706. /* address of beginning of left & right granule */
  8707. var blocktype = new_int(2);
  8708. for (gr = 0; gr < gfc.mode_gr; gr++) {
  8709. for (ch = 0; ch < gfc.channels_out; ch++) {
  8710. bufp[ch] = inbuf[ch];
  8711. bufpPos = 576 + gr * 576 - Encoder.FFTOFFSET;
  8712. }
  8713. if (gfp.VBR == VbrMode.vbr_mtrh || gfp.VBR == VbrMode.vbr_mt) {
  8714. ret = psy.L3psycho_anal_vbr(gfp, bufp, bufpPos, gr,
  8715. masking_LR, masking_MS, pe[gr], pe_MS[gr],
  8716. tot_ener[gr], blocktype);
  8717. } else {
  8718. ret = psy.L3psycho_anal_ns(gfp, bufp, bufpPos, gr,
  8719. masking_LR, masking_MS, pe[gr], pe_MS[gr],
  8720. tot_ener[gr], blocktype);
  8721. }
  8722. if (ret != 0)
  8723. return -4;
  8724. if (gfp.mode == MPEGMode.JOINT_STEREO) {
  8725. ms_ener_ratio[gr] = tot_ener[gr][2] + tot_ener[gr][3];
  8726. if (ms_ener_ratio[gr] > 0)
  8727. ms_ener_ratio[gr] = tot_ener[gr][3] / ms_ener_ratio[gr];
  8728. }
  8729. /* block type flags */
  8730. for (ch = 0; ch < gfc.channels_out; ch++) {
  8731. var cod_info = gfc.l3_side.tt[gr][ch];
  8732. cod_info.block_type = blocktype[ch];
  8733. cod_info.mixed_block_flag = 0;
  8734. }
  8735. }
  8736. } else {
  8737. /* no psy model */
  8738. for (gr = 0; gr < gfc.mode_gr; gr++)
  8739. for (ch = 0; ch < gfc.channels_out; ch++) {
  8740. gfc.l3_side.tt[gr][ch].block_type = Encoder.NORM_TYPE;
  8741. gfc.l3_side.tt[gr][ch].mixed_block_flag = 0;
  8742. pe_MS[gr][ch] = pe[gr][ch] = 700;
  8743. }
  8744. }
  8745. /* auto-adjust of ATH, useful for low volume */
  8746. adjust_ATH(gfc);
  8747. /****************************************
  8748. * Stage 2: MDCT *
  8749. ****************************************/
  8750. /* polyphase filtering / mdct */
  8751. newMDCT.mdct_sub48(gfc, inbuf[0], inbuf[1]);
  8752. /****************************************
  8753. * Stage 3: MS/LR decision *
  8754. ****************************************/
  8755. /* Here will be selected MS or LR coding of the 2 stereo channels */
  8756. gfc.mode_ext = Encoder.MPG_MD_LR_LR;
  8757. if (gfp.force_ms) {
  8758. gfc.mode_ext = Encoder.MPG_MD_MS_LR;
  8759. } else if (gfp.mode == MPEGMode.JOINT_STEREO) {
  8760. /*
  8761. * ms_ratio = is scaled, for historical reasons, to look like a
  8762. * ratio of side_channel / total. 0 = signal is 100% mono .5 = L & R
  8763. * uncorrelated
  8764. */
  8765. /**
  8766. * <PRE>
  8767. * [0] and [1] are the results for the two granules in MPEG-1,
  8768. * in MPEG-2 it's only a faked averaging of the same value
  8769. * _prev is the value of the last granule of the previous frame
  8770. * _next is the value of the first granule of the next frame
  8771. * </PRE>
  8772. */
  8773. var sum_pe_MS = 0.;
  8774. var sum_pe_LR = 0.;
  8775. for (gr = 0; gr < gfc.mode_gr; gr++) {
  8776. for (ch = 0; ch < gfc.channels_out; ch++) {
  8777. sum_pe_MS += pe_MS[gr][ch];
  8778. sum_pe_LR += pe[gr][ch];
  8779. }
  8780. }
  8781. /* based on PE: M/S coding would not use much more bits than L/R */
  8782. if (sum_pe_MS <= 1.00 * sum_pe_LR) {
  8783. var gi0 = gfc.l3_side.tt[0];
  8784. var gi1 = gfc.l3_side.tt[gfc.mode_gr - 1];
  8785. if (gi0[0].block_type == gi0[1].block_type
  8786. && gi1[0].block_type == gi1[1].block_type) {
  8787. gfc.mode_ext = Encoder.MPG_MD_MS_LR;
  8788. }
  8789. }
  8790. }
  8791. /* bit and noise allocation */
  8792. if (gfc.mode_ext == MPG_MD_MS_LR) {
  8793. masking = masking_MS;
  8794. /* use MS masking */
  8795. pe_use = pe_MS;
  8796. } else {
  8797. masking = masking_LR;
  8798. /* use LR masking */
  8799. pe_use = pe;
  8800. }
  8801. /* copy data for MP3 frame analyzer */
  8802. if (gfp.analysis && gfc.pinfo != null) {
  8803. for (gr = 0; gr < gfc.mode_gr; gr++) {
  8804. for (ch = 0; ch < gfc.channels_out; ch++) {
  8805. gfc.pinfo.ms_ratio[gr] = gfc.ms_ratio[gr];
  8806. gfc.pinfo.ms_ener_ratio[gr] = ms_ener_ratio[gr];
  8807. gfc.pinfo.blocktype[gr][ch] = gfc.l3_side.tt[gr][ch].block_type;
  8808. gfc.pinfo.pe[gr][ch] = pe_use[gr][ch];
  8809. System.arraycopy(gfc.l3_side.tt[gr][ch].xr, 0,
  8810. gfc.pinfo.xr[gr][ch], 0, 576);
  8811. /*
  8812. * in psymodel, LR and MS data was stored in pinfo. switch
  8813. * to MS data:
  8814. */
  8815. if (gfc.mode_ext == MPG_MD_MS_LR) {
  8816. gfc.pinfo.ers[gr][ch] = gfc.pinfo.ers[gr][ch + 2];
  8817. System.arraycopy(gfc.pinfo.energy[gr][ch + 2], 0,
  8818. gfc.pinfo.energy[gr][ch], 0,
  8819. gfc.pinfo.energy[gr][ch].length);
  8820. }
  8821. }
  8822. }
  8823. }
  8824. /****************************************
  8825. * Stage 4: quantization loop *
  8826. ****************************************/
  8827. if (gfp.VBR == VbrMode.vbr_off || gfp.VBR == VbrMode.vbr_abr) {
  8828. var i;
  8829. var f;
  8830. for (i = 0; i < 18; i++)
  8831. gfc.nsPsy.pefirbuf[i] = gfc.nsPsy.pefirbuf[i + 1];
  8832. f = 0.0;
  8833. for (gr = 0; gr < gfc.mode_gr; gr++)
  8834. for (ch = 0; ch < gfc.channels_out; ch++)
  8835. f += pe_use[gr][ch];
  8836. gfc.nsPsy.pefirbuf[18] = f;
  8837. f = gfc.nsPsy.pefirbuf[9];
  8838. for (i = 0; i < 9; i++)
  8839. f += (gfc.nsPsy.pefirbuf[i] + gfc.nsPsy.pefirbuf[18 - i])
  8840. * Encoder.fircoef[i];
  8841. f = (670 * 5 * gfc.mode_gr * gfc.channels_out) / f;
  8842. for (gr = 0; gr < gfc.mode_gr; gr++) {
  8843. for (ch = 0; ch < gfc.channels_out; ch++) {
  8844. pe_use[gr][ch] *= f;
  8845. }
  8846. }
  8847. }
  8848. gfc.iteration_loop.iteration_loop(gfp, pe_use, ms_ener_ratio, masking);
  8849. /****************************************
  8850. * Stage 5: bitstream formatting *
  8851. ****************************************/
  8852. /* write the frame to the bitstream */
  8853. bs.format_bitstream(gfp);
  8854. /* copy mp3 bit buffer into array */
  8855. mp3count = bs.copy_buffer(gfc, mp3buf, mp3bufPos, mp3buf_size, 1);
  8856. if (gfp.bWriteVbrTag)
  8857. vbr.addVbrFrame(gfp);
  8858. if (gfp.analysis && gfc.pinfo != null) {
  8859. for (ch = 0; ch < gfc.channels_out; ch++) {
  8860. var j;
  8861. for (j = 0; j < FFTOFFSET; j++)
  8862. gfc.pinfo.pcmdata[ch][j] = gfc.pinfo.pcmdata[ch][j
  8863. + gfp.framesize];
  8864. for (j = FFTOFFSET; j < 1600; j++) {
  8865. gfc.pinfo.pcmdata[ch][j] = inbuf[ch][j - FFTOFFSET];
  8866. }
  8867. }
  8868. qupvt.set_frame_pinfo(gfp, masking);
  8869. }
  8870. updateStats(gfc);
  8871. return mp3count;
  8872. }
  8873. }
  8874. //package mp3;
  8875. function VBRSeekInfo() {
  8876. /**
  8877. * What we have seen so far.
  8878. */
  8879. this.sum = 0;
  8880. /**
  8881. * How many frames we have seen in this chunk.
  8882. */
  8883. this.seen = 0;
  8884. /**
  8885. * How many frames we want to collect into one chunk.
  8886. */
  8887. this.want = 0;
  8888. /**
  8889. * Actual position in our bag.
  8890. */
  8891. this.pos = 0;
  8892. /**
  8893. * Size of our bag.
  8894. */
  8895. this.size = 0;
  8896. /**
  8897. * Pointer to our bag.
  8898. */
  8899. this.bag = null;
  8900. this.nVbrNumFrames = 0;
  8901. this.nBytesWritten = 0;
  8902. /* VBR tag data */
  8903. this.TotalFrameSize = 0;
  8904. }
  8905. function IIISideInfo() {
  8906. this.tt = [[null, null], [null, null]];
  8907. this.main_data_begin = 0;
  8908. this.private_bits = 0;
  8909. this.resvDrain_pre = 0;
  8910. this.resvDrain_post = 0;
  8911. this.scfsi = [new_int(4), new_int(4)];
  8912. for (var gr = 0; gr < 2; gr++) {
  8913. for (var ch = 0; ch < 2; ch++) {
  8914. this.tt[gr][ch] = new GrInfo();
  8915. }
  8916. }
  8917. }
  8918. //package mp3;
  8919. /**
  8920. * Variables used for --nspsytune
  8921. *
  8922. * @author Ken
  8923. *
  8924. */
  8925. function NsPsy() {
  8926. this.last_en_subshort = new_float_n([4, 9]);
  8927. this.lastAttacks = new_int(4);
  8928. this.pefirbuf = new_float(19);
  8929. this.longfact = new_float(Encoder.SBMAX_l);
  8930. this.shortfact = new_float(Encoder.SBMAX_s);
  8931. /**
  8932. * short block tuning
  8933. */
  8934. this.attackthre = 0.;
  8935. this.attackthre_s = 0.;
  8936. }
  8937. function III_psy_xmin() {
  8938. this.l = new_float(Encoder.SBMAX_l);
  8939. this.s = new_float_n([Encoder.SBMAX_s, 3]);
  8940. var self = this;
  8941. this.assign = function (iii_psy_xmin) {
  8942. System.arraycopy(iii_psy_xmin.l, 0, self.l, 0, Encoder.SBMAX_l);
  8943. for (var i = 0; i < Encoder.SBMAX_s; i++) {
  8944. for (var j = 0; j < 3; j++) {
  8945. self.s[i][j] = iii_psy_xmin.s[i][j];
  8946. }
  8947. }
  8948. }
  8949. }
  8950. LameInternalFlags.MFSIZE = (3 * 1152 + Encoder.ENCDELAY - Encoder.MDCTDELAY);
  8951. LameInternalFlags.MAX_HEADER_BUF = 256;
  8952. LameInternalFlags.MAX_BITS_PER_CHANNEL = 4095;
  8953. LameInternalFlags.MAX_BITS_PER_GRANULE = 7680;
  8954. LameInternalFlags.BPC = 320;
  8955. function LameInternalFlags() {
  8956. var MAX_HEADER_LEN = 40;
  8957. /********************************************************************
  8958. * internal variables NOT set by calling program, and should not be *
  8959. * modified by the calling program *
  8960. ********************************************************************/
  8961. /**
  8962. * Some remarks to the Class_ID field: The Class ID is an Identifier for a
  8963. * pointer to this struct. It is very unlikely that a pointer to
  8964. * lame_global_flags has the same 32 bits in it's structure (large and other
  8965. * special properties, for instance prime).
  8966. *
  8967. * To test that the structure is right and initialized, use: if ( gfc .
  8968. * Class_ID == LAME_ID ) ... Other remark: If you set a flag to 0 for uninit
  8969. * data and 1 for init data, the right test should be "if (flag == 1)" and
  8970. * NOT "if (flag)". Unintended modification of this element will be
  8971. * otherwise misinterpreted as an init.
  8972. */
  8973. this.Class_ID = 0;
  8974. this.lame_encode_frame_init = 0;
  8975. this.iteration_init_init = 0;
  8976. this.fill_buffer_resample_init = 0;
  8977. //public float mfbuf[][] = new float[2][MFSIZE];
  8978. this.mfbuf = new_float_n([2, LameInternalFlags.MFSIZE]);
  8979. /**
  8980. * granules per frame
  8981. */
  8982. this.mode_gr = 0;
  8983. /**
  8984. * number of channels in the input data stream (PCM or decoded PCM)
  8985. */
  8986. this.channels_in = 0;
  8987. /**
  8988. * number of channels in the output data stream (not used for decoding)
  8989. */
  8990. this.channels_out = 0;
  8991. /**
  8992. * input_samp_rate/output_samp_rate
  8993. */
  8994. //public double resample_ratio;
  8995. this.resample_ratio = 0.;
  8996. this.mf_samples_to_encode = 0;
  8997. this.mf_size = 0;
  8998. /**
  8999. * min bitrate index
  9000. */
  9001. this.VBR_min_bitrate = 0;
  9002. /**
  9003. * max bitrate index
  9004. */
  9005. this.VBR_max_bitrate = 0;
  9006. this.bitrate_index = 0;
  9007. this.samplerate_index = 0;
  9008. this.mode_ext = 0;
  9009. /* lowpass and highpass filter control */
  9010. /**
  9011. * normalized frequency bounds of passband
  9012. */
  9013. this.lowpass1 = 0.;
  9014. this.lowpass2 = 0.;
  9015. /**
  9016. * normalized frequency bounds of passband
  9017. */
  9018. this.highpass1 = 0.;
  9019. this.highpass2 = 0.;
  9020. /**
  9021. * 0 = none 1 = ISO AAC model 2 = allow scalefac_select=1
  9022. */
  9023. this.noise_shaping = 0;
  9024. /**
  9025. * 0 = ISO model: amplify all distorted bands<BR>
  9026. * 1 = amplify within 50% of max (on db scale)<BR>
  9027. * 2 = amplify only most distorted band<BR>
  9028. * 3 = method 1 and refine with method 2<BR>
  9029. */
  9030. this.noise_shaping_amp = 0;
  9031. /**
  9032. * 0 = no substep<BR>
  9033. * 1 = use substep shaping at last step(VBR only)<BR>
  9034. * (not implemented yet)<BR>
  9035. * 2 = use substep inside loop<BR>
  9036. * 3 = use substep inside loop and last step<BR>
  9037. */
  9038. this.substep_shaping = 0;
  9039. /**
  9040. * 1 = gpsycho. 0 = none
  9041. */
  9042. this.psymodel = 0;
  9043. /**
  9044. * 0 = stop at over=0, all scalefacs amplified or<BR>
  9045. * a scalefac has reached max value<BR>
  9046. * 1 = stop when all scalefacs amplified or a scalefac has reached max value<BR>
  9047. * 2 = stop when all scalefacs amplified
  9048. */
  9049. this.noise_shaping_stop = 0;
  9050. /**
  9051. * 0 = no, 1 = yes
  9052. */
  9053. this.subblock_gain = 0;
  9054. /**
  9055. * 0 = no. 1=outside loop 2=inside loop(slow)
  9056. */
  9057. this.use_best_huffman = 0;
  9058. /**
  9059. * 0 = stop early after 0 distortion found. 1 = full search
  9060. */
  9061. this.full_outer_loop = 0;
  9062. //public IIISideInfo l3_side = new IIISideInfo();
  9063. this.l3_side = new IIISideInfo();
  9064. this.ms_ratio = new_float(2);
  9065. /* used for padding */
  9066. /**
  9067. * padding for the current frame?
  9068. */
  9069. this.padding = 0;
  9070. this.frac_SpF = 0;
  9071. this.slot_lag = 0;
  9072. /**
  9073. * optional ID3 tags
  9074. */
  9075. //public ID3TagSpec tag_spec;
  9076. this.tag_spec = null;
  9077. this.nMusicCRC = 0;
  9078. /* variables used by Quantize */
  9079. //public int OldValue[] = new int[2];
  9080. this.OldValue = new_int(2);
  9081. //public int CurrentStep[] = new int[2];
  9082. this.CurrentStep = new_int(2);
  9083. this.masking_lower = 0.;
  9084. //public int bv_scf[] = new int[576];
  9085. this.bv_scf = new_int(576);
  9086. //public int pseudohalf[] = new int[L3Side.SFBMAX];
  9087. this.pseudohalf = new_int(L3Side.SFBMAX);
  9088. /**
  9089. * will be set in lame_init_params
  9090. */
  9091. this.sfb21_extra = false;
  9092. /* BPC = maximum number of filter convolution windows to precompute */
  9093. //public float[][] inbuf_old = new float[2][];
  9094. this.inbuf_old = new Array(2);
  9095. //public float[][] blackfilt = new float[2 * BPC + 1][];
  9096. this.blackfilt = new Array(2 * LameInternalFlags.BPC + 1);
  9097. //public double itime[] = new double[2];
  9098. this.itime = new_double(2);
  9099. this.sideinfo_len = 0;
  9100. /* variables for newmdct.c */
  9101. //public float sb_sample[][][][] = new float[2][2][18][Encoder.SBLIMIT];
  9102. this.sb_sample = new_float_n([2, 2, 18, Encoder.SBLIMIT]);
  9103. this.amp_filter = new_float(32);
  9104. /* variables for BitStream */
  9105. /**
  9106. * <PRE>
  9107. * mpeg1: buffer=511 bytes smallest frame: 96-38(sideinfo)=58
  9108. * max number of frames in reservoir: 8
  9109. * mpeg2: buffer=255 bytes. smallest frame: 24-23bytes=1
  9110. * with VBR, if you are encoding all silence, it is possible to
  9111. * have 8kbs/24khz frames with 1byte of data each, which means we need
  9112. * to buffer up to 255 headers!
  9113. * </PRE>
  9114. */
  9115. /**
  9116. * also, max_header_buf has to be a power of two
  9117. */
  9118. /**
  9119. * max size of header is 38
  9120. */
  9121. function Header() {
  9122. this.write_timing = 0;
  9123. this.ptr = 0;
  9124. //public byte buf[] = new byte[MAX_HEADER_LEN];
  9125. this.buf = new_byte(MAX_HEADER_LEN);
  9126. }
  9127. this.header = new Array(LameInternalFlags.MAX_HEADER_BUF);
  9128. this.h_ptr = 0;
  9129. this.w_ptr = 0;
  9130. this.ancillary_flag = 0;
  9131. /* variables for Reservoir */
  9132. /**
  9133. * in bits
  9134. */
  9135. this.ResvSize = 0;
  9136. /**
  9137. * in bits
  9138. */
  9139. this.ResvMax = 0;
  9140. //public ScaleFac scalefac_band = new ScaleFac();
  9141. this.scalefac_band = new ScaleFac();
  9142. /* daa from PsyModel */
  9143. /* The static variables "r", "phi_sav", "new", "old" and "oldest" have */
  9144. /* to be remembered for the unpredictability measure. For "r" and */
  9145. /* "phi_sav", the first index from the left is the channel select and */
  9146. /* the second index is the "age" of the data. */
  9147. this.minval_l = new_float(Encoder.CBANDS);
  9148. this.minval_s = new_float(Encoder.CBANDS);
  9149. this.nb_1 = new_float_n([4, Encoder.CBANDS]);
  9150. this.nb_2 = new_float_n([4, Encoder.CBANDS]);
  9151. this.nb_s1 = new_float_n([4, Encoder.CBANDS]);
  9152. this.nb_s2 = new_float_n([4, Encoder.CBANDS]);
  9153. this.s3_ss = null;
  9154. this.s3_ll = null;
  9155. this.decay = 0.;
  9156. //public III_psy_xmin[] thm = new III_psy_xmin[4];
  9157. //public III_psy_xmin[] en = new III_psy_xmin[4];
  9158. this.thm = new Array(4);
  9159. this.en = new Array(4);
  9160. /**
  9161. * fft and energy calculation
  9162. */
  9163. this.tot_ener = new_float(4);
  9164. /* loudness calculation (for adaptive threshold of hearing) */
  9165. /**
  9166. * loudness^2 approx. per granule and channel
  9167. */
  9168. this.loudness_sq = new_float_n([2, 2]);
  9169. /**
  9170. * account for granule delay of L3psycho_anal
  9171. */
  9172. this.loudness_sq_save = new_float(2);
  9173. /**
  9174. * Scale Factor Bands
  9175. */
  9176. this.mld_l = new_float(Encoder.SBMAX_l);
  9177. this.mld_s = new_float(Encoder.SBMAX_s);
  9178. this.bm_l = new_int(Encoder.SBMAX_l);
  9179. this.bo_l = new_int(Encoder.SBMAX_l);
  9180. this.bm_s = new_int(Encoder.SBMAX_s);
  9181. this.bo_s = new_int(Encoder.SBMAX_s);
  9182. this.npart_l = 0;
  9183. this.npart_s = 0;
  9184. this.s3ind = new_int_n([Encoder.CBANDS, 2]);
  9185. this.s3ind_s = new_int_n([Encoder.CBANDS, 2]);
  9186. this.numlines_s = new_int(Encoder.CBANDS);
  9187. this.numlines_l = new_int(Encoder.CBANDS);
  9188. this.rnumlines_l = new_float(Encoder.CBANDS);
  9189. this.mld_cb_l = new_float(Encoder.CBANDS);
  9190. this.mld_cb_s = new_float(Encoder.CBANDS);
  9191. this.numlines_s_num1 = 0;
  9192. this.numlines_l_num1 = 0;
  9193. /* ratios */
  9194. this.pe = new_float(4);
  9195. this.ms_ratio_s_old = 0.;
  9196. this.ms_ratio_l_old = 0.;
  9197. this.ms_ener_ratio_old = 0.;
  9198. /**
  9199. * block type
  9200. */
  9201. this.blocktype_old = new_int(2);
  9202. /**
  9203. * variables used for --nspsytune
  9204. */
  9205. this.nsPsy = new NsPsy();
  9206. /**
  9207. * used for Xing VBR header
  9208. */
  9209. this.VBR_seek_table = new VBRSeekInfo();
  9210. /**
  9211. * all ATH related stuff
  9212. */
  9213. //public ATH ATH;
  9214. this.ATH = null;
  9215. this.PSY = null;
  9216. this.nogap_total = 0;
  9217. this.nogap_current = 0;
  9218. /* ReplayGain */
  9219. this.decode_on_the_fly = true;
  9220. this.findReplayGain = true;
  9221. this.findPeakSample = true;
  9222. this.PeakSample = 0.;
  9223. this.RadioGain = 0;
  9224. this.AudiophileGain = 0;
  9225. //public ReplayGain rgdata;
  9226. this.rgdata = null;
  9227. /**
  9228. * gain change required for preventing clipping
  9229. */
  9230. this.noclipGainChange = 0;
  9231. /**
  9232. * user-specified scale factor required for preventing clipping
  9233. */
  9234. this.noclipScale = 0.;
  9235. /* simple statistics */
  9236. this.bitrate_stereoMode_Hist = new_int_n([16, 4 + 1]);
  9237. /**
  9238. * norm/start/short/stop/mixed(short)/sum
  9239. */
  9240. this.bitrate_blockType_Hist = new_int_n([16, 4 + 1 + 1]);
  9241. //public PlottingData pinfo;
  9242. //public MPGLib.mpstr_tag hip;
  9243. this.pinfo = null;
  9244. this.hip = null;
  9245. this.in_buffer_nsamples = 0;
  9246. //public float[] in_buffer_0;
  9247. //public float[] in_buffer_1;
  9248. this.in_buffer_0 = null;
  9249. this.in_buffer_1 = null;
  9250. //public IIterationLoop iteration_loop;
  9251. this.iteration_loop = null;
  9252. for (var i = 0; i < this.en.length; i++) {
  9253. this.en[i] = new III_psy_xmin();
  9254. }
  9255. for (var i = 0; i < this.thm.length; i++) {
  9256. this.thm[i] = new III_psy_xmin();
  9257. }
  9258. for (var i = 0; i < this.header.length; i++) {
  9259. this.header[i] = new Header();
  9260. }
  9261. }
  9262. function FFT() {
  9263. var window = new_float(Encoder.BLKSIZE);
  9264. var window_s = new_float(Encoder.BLKSIZE_s / 2);
  9265. var costab = [
  9266. 9.238795325112867e-01, 3.826834323650898e-01,
  9267. 9.951847266721969e-01, 9.801714032956060e-02,
  9268. 9.996988186962042e-01, 2.454122852291229e-02,
  9269. 9.999811752826011e-01, 6.135884649154475e-03
  9270. ];
  9271. function fht(fz, fzPos, n) {
  9272. var tri = 0;
  9273. var k4;
  9274. var fi;
  9275. var gi;
  9276. n <<= 1;
  9277. /* to get BLKSIZE, because of 3DNow! ASM routine */
  9278. var fn = fzPos + n;
  9279. k4 = 4;
  9280. do {
  9281. var s1, c1;
  9282. var i, k1, k2, k3, kx;
  9283. kx = k4 >> 1;
  9284. k1 = k4;
  9285. k2 = k4 << 1;
  9286. k3 = k2 + k1;
  9287. k4 = k2 << 1;
  9288. fi = fzPos;
  9289. gi = fi + kx;
  9290. do {
  9291. var f0, f1, f2, f3;
  9292. f1 = fz[fi + 0] - fz[fi + k1];
  9293. f0 = fz[fi + 0] + fz[fi + k1];
  9294. f3 = fz[fi + k2] - fz[fi + k3];
  9295. f2 = fz[fi + k2] + fz[fi + k3];
  9296. fz[fi + k2] = f0 - f2;
  9297. fz[fi + 0] = f0 + f2;
  9298. fz[fi + k3] = f1 - f3;
  9299. fz[fi + k1] = f1 + f3;
  9300. f1 = fz[gi + 0] - fz[gi + k1];
  9301. f0 = fz[gi + 0] + fz[gi + k1];
  9302. f3 = (Util.SQRT2 * fz[gi + k3]);
  9303. f2 = (Util.SQRT2 * fz[gi + k2]);
  9304. fz[gi + k2] = f0 - f2;
  9305. fz[gi + 0] = f0 + f2;
  9306. fz[gi + k3] = f1 - f3;
  9307. fz[gi + k1] = f1 + f3;
  9308. gi += k4;
  9309. fi += k4;
  9310. } while (fi < fn);
  9311. c1 = costab[tri + 0];
  9312. s1 = costab[tri + 1];
  9313. for (i = 1; i < kx; i++) {
  9314. var c2, s2;
  9315. c2 = 1 - (2 * s1) * s1;
  9316. s2 = (2 * s1) * c1;
  9317. fi = fzPos + i;
  9318. gi = fzPos + k1 - i;
  9319. do {
  9320. var a, b, g0, f0, f1, g1, f2, g2, f3, g3;
  9321. b = s2 * fz[fi + k1] - c2 * fz[gi + k1];
  9322. a = c2 * fz[fi + k1] + s2 * fz[gi + k1];
  9323. f1 = fz[fi + 0] - a;
  9324. f0 = fz[fi + 0] + a;
  9325. g1 = fz[gi + 0] - b;
  9326. g0 = fz[gi + 0] + b;
  9327. b = s2 * fz[fi + k3] - c2 * fz[gi + k3];
  9328. a = c2 * fz[fi + k3] + s2 * fz[gi + k3];
  9329. f3 = fz[fi + k2] - a;
  9330. f2 = fz[fi + k2] + a;
  9331. g3 = fz[gi + k2] - b;
  9332. g2 = fz[gi + k2] + b;
  9333. b = s1 * f2 - c1 * g3;
  9334. a = c1 * f2 + s1 * g3;
  9335. fz[fi + k2] = f0 - a;
  9336. fz[fi + 0] = f0 + a;
  9337. fz[gi + k3] = g1 - b;
  9338. fz[gi + k1] = g1 + b;
  9339. b = c1 * g2 - s1 * f3;
  9340. a = s1 * g2 + c1 * f3;
  9341. fz[gi + k2] = g0 - a;
  9342. fz[gi + 0] = g0 + a;
  9343. fz[fi + k3] = f1 - b;
  9344. fz[fi + k1] = f1 + b;
  9345. gi += k4;
  9346. fi += k4;
  9347. } while (fi < fn);
  9348. c2 = c1;
  9349. c1 = c2 * costab[tri + 0] - s1 * costab[tri + 1];
  9350. s1 = c2 * costab[tri + 1] + s1 * costab[tri + 0];
  9351. }
  9352. tri += 2;
  9353. } while (k4 < n);
  9354. }
  9355. var rv_tbl = [0x00, 0x80, 0x40,
  9356. 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10,
  9357. 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70,
  9358. 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28,
  9359. 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58,
  9360. 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04,
  9361. 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64,
  9362. 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34,
  9363. 0xb4, 0x74, 0xf4, 0x0c, 0x8c, 0x4c,
  9364. 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c,
  9365. 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c,
  9366. 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22,
  9367. 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52,
  9368. 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a,
  9369. 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a,
  9370. 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a,
  9371. 0xba, 0x7a, 0xfa, 0x06, 0x86, 0x46,
  9372. 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16,
  9373. 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76,
  9374. 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e,
  9375. 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e,
  9376. 0xde, 0x3e, 0xbe, 0x7e, 0xfe];
  9377. this.fft_short = function (gfc, x_real, chn, buffer, bufPos) {
  9378. for (var b = 0; b < 3; b++) {
  9379. var x = Encoder.BLKSIZE_s / 2;
  9380. var k = 0xffff & ((576 / 3) * (b + 1));
  9381. var j = Encoder.BLKSIZE_s / 8 - 1;
  9382. do {
  9383. var f0, f1, f2, f3, w;
  9384. var i = rv_tbl[j << 2] & 0xff;
  9385. f0 = window_s[i] * buffer[chn][bufPos + i + k];
  9386. w = window_s[0x7f - i] * buffer[chn][bufPos + i + k + 0x80];
  9387. f1 = f0 - w;
  9388. f0 = f0 + w;
  9389. f2 = window_s[i + 0x40] * buffer[chn][bufPos + i + k + 0x40];
  9390. w = window_s[0x3f - i] * buffer[chn][bufPos + i + k + 0xc0];
  9391. f3 = f2 - w;
  9392. f2 = f2 + w;
  9393. x -= 4;
  9394. x_real[b][x + 0] = f0 + f2;
  9395. x_real[b][x + 2] = f0 - f2;
  9396. x_real[b][x + 1] = f1 + f3;
  9397. x_real[b][x + 3] = f1 - f3;
  9398. f0 = window_s[i + 0x01] * buffer[chn][bufPos + i + k + 0x01];
  9399. w = window_s[0x7e - i] * buffer[chn][bufPos + i + k + 0x81];
  9400. f1 = f0 - w;
  9401. f0 = f0 + w;
  9402. f2 = window_s[i + 0x41] * buffer[chn][bufPos + i + k + 0x41];
  9403. w = window_s[0x3e - i] * buffer[chn][bufPos + i + k + 0xc1];
  9404. f3 = f2 - w;
  9405. f2 = f2 + w;
  9406. x_real[b][x + Encoder.BLKSIZE_s / 2 + 0] = f0 + f2;
  9407. x_real[b][x + Encoder.BLKSIZE_s / 2 + 2] = f0 - f2;
  9408. x_real[b][x + Encoder.BLKSIZE_s / 2 + 1] = f1 + f3;
  9409. x_real[b][x + Encoder.BLKSIZE_s / 2 + 3] = f1 - f3;
  9410. } while (--j >= 0);
  9411. fht(x_real[b], x, Encoder.BLKSIZE_s / 2);
  9412. /* BLKSIZE_s/2 because of 3DNow! ASM routine */
  9413. /* BLKSIZE/2 because of 3DNow! ASM routine */
  9414. }
  9415. }
  9416. this.fft_long = function (gfc, y, chn, buffer, bufPos) {
  9417. var jj = Encoder.BLKSIZE / 8 - 1;
  9418. var x = Encoder.BLKSIZE / 2;
  9419. do {
  9420. var f0, f1, f2, f3, w;
  9421. var i = rv_tbl[jj] & 0xff;
  9422. f0 = window[i] * buffer[chn][bufPos + i];
  9423. w = window[i + 0x200] * buffer[chn][bufPos + i + 0x200];
  9424. f1 = f0 - w;
  9425. f0 = f0 + w;
  9426. f2 = window[i + 0x100] * buffer[chn][bufPos + i + 0x100];
  9427. w = window[i + 0x300] * buffer[chn][bufPos + i + 0x300];
  9428. f3 = f2 - w;
  9429. f2 = f2 + w;
  9430. x -= 4;
  9431. y[x + 0] = f0 + f2;
  9432. y[x + 2] = f0 - f2;
  9433. y[x + 1] = f1 + f3;
  9434. y[x + 3] = f1 - f3;
  9435. f0 = window[i + 0x001] * buffer[chn][bufPos + i + 0x001];
  9436. w = window[i + 0x201] * buffer[chn][bufPos + i + 0x201];
  9437. f1 = f0 - w;
  9438. f0 = f0 + w;
  9439. f2 = window[i + 0x101] * buffer[chn][bufPos + i + 0x101];
  9440. w = window[i + 0x301] * buffer[chn][bufPos + i + 0x301];
  9441. f3 = f2 - w;
  9442. f2 = f2 + w;
  9443. y[x + Encoder.BLKSIZE / 2 + 0] = f0 + f2;
  9444. y[x + Encoder.BLKSIZE / 2 + 2] = f0 - f2;
  9445. y[x + Encoder.BLKSIZE / 2 + 1] = f1 + f3;
  9446. y[x + Encoder.BLKSIZE / 2 + 3] = f1 - f3;
  9447. } while (--jj >= 0);
  9448. fht(y, x, Encoder.BLKSIZE / 2);
  9449. /* BLKSIZE/2 because of 3DNow! ASM routine */
  9450. }
  9451. this.init_fft = function (gfc) {
  9452. /* The type of window used here will make no real difference, but */
  9453. /*
  9454. * in the interest of merging nspsytune stuff - switch to blackman
  9455. * window
  9456. */
  9457. for (var i = 0; i < Encoder.BLKSIZE; i++)
  9458. /* blackman window */
  9459. window[i] = (0.42 - 0.5 * Math.cos(2 * Math.PI * (i + .5)
  9460. / Encoder.BLKSIZE) + 0.08 * Math.cos(4 * Math.PI * (i + .5)
  9461. / Encoder.BLKSIZE));
  9462. for (var i = 0; i < Encoder.BLKSIZE_s / 2; i++)
  9463. window_s[i] = (0.5 * (1.0 - Math.cos(2.0 * Math.PI
  9464. * (i + 0.5) / Encoder.BLKSIZE_s)));
  9465. }
  9466. }
  9467. /*
  9468. * psymodel.c
  9469. *
  9470. * Copyright (c) 1999-2000 Mark Taylor
  9471. * Copyright (c) 2001-2002 Naoki Shibata
  9472. * Copyright (c) 2000-2003 Takehiro Tominaga
  9473. * Copyright (c) 2000-2008 Robert Hegemann
  9474. * Copyright (c) 2000-2005 Gabriel Bouvigne
  9475. * Copyright (c) 2000-2005 Alexander Leidinger
  9476. *
  9477. * This library is free software; you can redistribute it and/or
  9478. * modify it under the terms of the GNU Lesser General Public
  9479. * License as published by the Free Software Foundation; either
  9480. * version 2 of the License, or (at your option) any later version.
  9481. *
  9482. * This library is distributed in the hope that it will be useful,
  9483. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9484. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9485. * Library General Public License for more details.
  9486. *
  9487. * You should have received a copy of the GNU Lesser General Public
  9488. * License along with this library; if not, write to the
  9489. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  9490. * Boston, MA 02111-1307, USA.
  9491. */
  9492. /* $Id: PsyModel.java,v 1.27 2011/05/24 20:48:06 kenchis Exp $ */
  9493. /*
  9494. PSYCHO ACOUSTICS
  9495. This routine computes the psycho acoustics, delayed by one granule.
  9496. Input: buffer of PCM data (1024 samples).
  9497. This window should be centered over the 576 sample granule window.
  9498. The routine will compute the psycho acoustics for
  9499. this granule, but return the psycho acoustics computed
  9500. for the *previous* granule. This is because the block
  9501. type of the previous granule can only be determined
  9502. after we have computed the psycho acoustics for the following
  9503. granule.
  9504. Output: maskings and energies for each scalefactor band.
  9505. block type, PE, and some correlation measures.
  9506. The PE is used by CBR modes to determine if extra bits
  9507. from the bit reservoir should be used. The correlation
  9508. measures are used to determine mid/side or regular stereo.
  9509. */
  9510. /*
  9511. Notation:
  9512. barks: a non-linear frequency scale. Mapping from frequency to
  9513. barks is given by freq2bark()
  9514. scalefactor bands: The spectrum (frequencies) are broken into
  9515. SBMAX "scalefactor bands". Thes bands
  9516. are determined by the MPEG ISO spec. In
  9517. the noise shaping/quantization code, we allocate
  9518. bits among the partition bands to achieve the
  9519. best possible quality
  9520. partition bands: The spectrum is also broken into about
  9521. 64 "partition bands". Each partition
  9522. band is about .34 barks wide. There are about 2-5
  9523. partition bands for each scalefactor band.
  9524. LAME computes all psycho acoustic information for each partition
  9525. band. Then at the end of the computations, this information
  9526. is mapped to scalefactor bands. The energy in each scalefactor
  9527. band is taken as the sum of the energy in all partition bands
  9528. which overlap the scalefactor band. The maskings can be computed
  9529. in the same way (and thus represent the average masking in that band)
  9530. or by taking the minmum value multiplied by the number of
  9531. partition bands used (which represents a minimum masking in that band).
  9532. */
  9533. /*
  9534. The general outline is as follows:
  9535. 1. compute the energy in each partition band
  9536. 2. compute the tonality in each partition band
  9537. 3. compute the strength of each partion band "masker"
  9538. 4. compute the masking (via the spreading function applied to each masker)
  9539. 5. Modifications for mid/side masking.
  9540. Each partition band is considiered a "masker". The strength
  9541. of the i'th masker in band j is given by:
  9542. s3(bark(i)-bark(j))*strength(i)
  9543. The strength of the masker is a function of the energy and tonality.
  9544. The more tonal, the less masking. LAME uses a simple linear formula
  9545. (controlled by NMT and TMN) which says the strength is given by the
  9546. energy divided by a linear function of the tonality.
  9547. */
  9548. /*
  9549. s3() is the "spreading function". It is given by a formula
  9550. determined via listening tests.
  9551. The total masking in the j'th partition band is the sum over
  9552. all maskings i. It is thus given by the convolution of
  9553. the strength with s3(), the "spreading function."
  9554. masking(j) = sum_over_i s3(i-j)*strength(i) = s3 o strength
  9555. where "o" = convolution operator. s3 is given by a formula determined
  9556. via listening tests. It is normalized so that s3 o 1 = 1.
  9557. Note: instead of a simple convolution, LAME also has the
  9558. option of using "additive masking"
  9559. The most critical part is step 2, computing the tonality of each
  9560. partition band. LAME has two tonality estimators. The first
  9561. is based on the ISO spec, and measures how predictiable the
  9562. signal is over time. The more predictable, the more tonal.
  9563. The second measure is based on looking at the spectrum of
  9564. a single granule. The more peaky the spectrum, the more
  9565. tonal. By most indications, the latter approach is better.
  9566. Finally, in step 5, the maskings for the mid and side
  9567. channel are possibly increased. Under certain circumstances,
  9568. noise in the mid & side channels is assumed to also
  9569. be masked by strong maskers in the L or R channels.
  9570. Other data computed by the psy-model:
  9571. ms_ratio side-channel / mid-channel masking ratio (for previous granule)
  9572. ms_ratio_next side-channel / mid-channel masking ratio for this granule
  9573. percep_entropy[2] L and R values (prev granule) of PE - A measure of how
  9574. much pre-echo is in the previous granule
  9575. percep_entropy_MS[2] mid and side channel values (prev granule) of percep_entropy
  9576. energy[4] L,R,M,S energy in each channel, prev granule
  9577. blocktype_d[2] block type to use for previous granule
  9578. */
  9579. //package mp3;
  9580. //import java.util.Arrays;
  9581. function PsyModel() {
  9582. var fft = new FFT();
  9583. var LOG10 = 2.30258509299404568402;
  9584. var rpelev = 2;
  9585. var rpelev2 = 16;
  9586. var rpelev_s = 2;
  9587. var rpelev2_s = 16;
  9588. /* size of each partition band, in barks: */
  9589. var DELBARK = .34;
  9590. /* tuned for output level (sensitive to energy scale) */
  9591. var VO_SCALE = (1. / (14752 * 14752) / (Encoder.BLKSIZE / 2));
  9592. var temporalmask_sustain_sec = 0.01;
  9593. var NS_PREECHO_ATT0 = 0.8;
  9594. var NS_PREECHO_ATT1 = 0.6;
  9595. var NS_PREECHO_ATT2 = 0.3;
  9596. var NS_MSFIX = 3.5;
  9597. var NSATTACKTHRE = 4.4;
  9598. var NSATTACKTHRE_S = 25;
  9599. var NSFIRLEN = 21;
  9600. /* size of each partition band, in barks: */
  9601. var LN_TO_LOG10 = 0.2302585093;
  9602. function NON_LINEAR_SCALE_ENERGY(x) {
  9603. return x;
  9604. }
  9605. /**
  9606. * <PRE>
  9607. * L3psycho_anal. Compute psycho acoustics.
  9608. *
  9609. * Data returned to the calling program must be delayed by one
  9610. * granule.
  9611. *
  9612. * This is done in two places.
  9613. * If we do not need to know the blocktype, the copying
  9614. * can be done here at the top of the program: we copy the data for
  9615. * the last granule (computed during the last call) before it is
  9616. * overwritten with the new data. It looks like this:
  9617. *
  9618. * 0. static psymodel_data
  9619. * 1. calling_program_data = psymodel_data
  9620. * 2. compute psymodel_data
  9621. *
  9622. * For data which needs to know the blocktype, the copying must be
  9623. * done at the end of this loop, and the old values must be saved:
  9624. *
  9625. * 0. static psymodel_data_old
  9626. * 1. compute psymodel_data
  9627. * 2. compute possible block type of this granule
  9628. * 3. compute final block type of previous granule based on #2.
  9629. * 4. calling_program_data = psymodel_data_old
  9630. * 5. psymodel_data_old = psymodel_data
  9631. * psycho_loudness_approx
  9632. * jd - 2001 mar 12
  9633. * in: energy - BLKSIZE/2 elements of frequency magnitudes ^ 2
  9634. * gfp - uses out_samplerate, ATHtype (also needed for ATHformula)
  9635. * returns: loudness^2 approximation, a positive value roughly tuned for a value
  9636. * of 1.0 for signals near clipping.
  9637. * notes: When calibrated, feeding this function binary white noise at sample
  9638. * values +32767 or -32768 should return values that approach 3.
  9639. * ATHformula is used to approximate an equal loudness curve.
  9640. * future: Data indicates that the shape of the equal loudness curve varies
  9641. * with intensity. This function might be improved by using an equal
  9642. * loudness curve shaped for typical playback levels (instead of the
  9643. * ATH, that is shaped for the threshold). A flexible realization might
  9644. * simply bend the existing ATH curve to achieve the desired shape.
  9645. * However, the potential gain may not be enough to justify an effort.
  9646. * </PRE>
  9647. */
  9648. function psycho_loudness_approx(energy, gfc) {
  9649. var loudness_power = 0.0;
  9650. /* apply weights to power in freq. bands */
  9651. for (var i = 0; i < Encoder.BLKSIZE / 2; ++i)
  9652. loudness_power += energy[i] * gfc.ATH.eql_w[i];
  9653. loudness_power *= VO_SCALE;
  9654. return loudness_power;
  9655. }
  9656. function compute_ffts(gfp, fftenergy, fftenergy_s, wsamp_l, wsamp_lPos, wsamp_s, wsamp_sPos, gr_out, chn, buffer, bufPos) {
  9657. var gfc = gfp.internal_flags;
  9658. if (chn < 2) {
  9659. fft.fft_long(gfc, wsamp_l[wsamp_lPos], chn, buffer, bufPos);
  9660. fft.fft_short(gfc, wsamp_s[wsamp_sPos], chn, buffer, bufPos);
  9661. }
  9662. /* FFT data for mid and side channel is derived from L & R */
  9663. else if (chn == 2) {
  9664. for (var j = Encoder.BLKSIZE - 1; j >= 0; --j) {
  9665. var l = wsamp_l[wsamp_lPos + 0][j];
  9666. var r = wsamp_l[wsamp_lPos + 1][j];
  9667. wsamp_l[wsamp_lPos + 0][j] = (l + r) * Util.SQRT2 * 0.5;
  9668. wsamp_l[wsamp_lPos + 1][j] = (l - r) * Util.SQRT2 * 0.5;
  9669. }
  9670. for (var b = 2; b >= 0; --b) {
  9671. for (var j = Encoder.BLKSIZE_s - 1; j >= 0; --j) {
  9672. var l = wsamp_s[wsamp_sPos + 0][b][j];
  9673. var r = wsamp_s[wsamp_sPos + 1][b][j];
  9674. wsamp_s[wsamp_sPos + 0][b][j] = (l + r) * Util.SQRT2 * 0.5;
  9675. wsamp_s[wsamp_sPos + 1][b][j] = (l - r) * Util.SQRT2 * 0.5;
  9676. }
  9677. }
  9678. }
  9679. /*********************************************************************
  9680. * compute energies
  9681. *********************************************************************/
  9682. fftenergy[0] = NON_LINEAR_SCALE_ENERGY(wsamp_l[wsamp_lPos + 0][0]);
  9683. fftenergy[0] *= fftenergy[0];
  9684. for (var j = Encoder.BLKSIZE / 2 - 1; j >= 0; --j) {
  9685. var re = (wsamp_l[wsamp_lPos + 0])[Encoder.BLKSIZE / 2 - j];
  9686. var im = (wsamp_l[wsamp_lPos + 0])[Encoder.BLKSIZE / 2 + j];
  9687. fftenergy[Encoder.BLKSIZE / 2 - j] = NON_LINEAR_SCALE_ENERGY((re
  9688. * re + im * im) * 0.5);
  9689. }
  9690. for (var b = 2; b >= 0; --b) {
  9691. fftenergy_s[b][0] = (wsamp_s[wsamp_sPos + 0])[b][0];
  9692. fftenergy_s[b][0] *= fftenergy_s[b][0];
  9693. for (var j = Encoder.BLKSIZE_s / 2 - 1; j >= 0; --j) {
  9694. var re = (wsamp_s[wsamp_sPos + 0])[b][Encoder.BLKSIZE_s
  9695. / 2 - j];
  9696. var im = (wsamp_s[wsamp_sPos + 0])[b][Encoder.BLKSIZE_s
  9697. / 2 + j];
  9698. fftenergy_s[b][Encoder.BLKSIZE_s / 2 - j] = NON_LINEAR_SCALE_ENERGY((re
  9699. * re + im * im) * 0.5);
  9700. }
  9701. }
  9702. /* total energy */
  9703. {
  9704. var totalenergy = 0.0;
  9705. for (var j = 11; j < Encoder.HBLKSIZE; j++)
  9706. totalenergy += fftenergy[j];
  9707. gfc.tot_ener[chn] = totalenergy;
  9708. }
  9709. if (gfp.analysis) {
  9710. for (var j = 0; j < Encoder.HBLKSIZE; j++) {
  9711. gfc.pinfo.energy[gr_out][chn][j] = gfc.pinfo.energy_save[chn][j];
  9712. gfc.pinfo.energy_save[chn][j] = fftenergy[j];
  9713. }
  9714. gfc.pinfo.pe[gr_out][chn] = gfc.pe[chn];
  9715. }
  9716. /*********************************************************************
  9717. * compute loudness approximation (used for ATH auto-level adjustment)
  9718. *********************************************************************/
  9719. if (gfp.athaa_loudapprox == 2 && chn < 2) {
  9720. // no loudness for mid/side ch
  9721. gfc.loudness_sq[gr_out][chn] = gfc.loudness_sq_save[chn];
  9722. gfc.loudness_sq_save[chn] = psycho_loudness_approx(fftenergy, gfc);
  9723. }
  9724. }
  9725. /* mask_add optimization */
  9726. /* init the limit values used to avoid computing log in mask_add when it is not necessary */
  9727. /**
  9728. * <PRE>
  9729. * For example, with i = 10*log10(m2/m1)/10*16 (= log10(m2/m1)*16)
  9730. *
  9731. * abs(i)>8 is equivalent (as i is an integer) to
  9732. * abs(i)>=9
  9733. * i>=9 || i<=-9
  9734. * equivalent to (as i is the biggest integer smaller than log10(m2/m1)*16
  9735. * or the smallest integer bigger than log10(m2/m1)*16 depending on the sign of log10(m2/m1)*16)
  9736. * log10(m2/m1)>=9/16 || log10(m2/m1)<=-9/16
  9737. * exp10 is strictly increasing thus this is equivalent to
  9738. * m2/m1 >= 10^(9/16) || m2/m1<=10^(-9/16) which are comparisons to constants
  9739. * </PRE>
  9740. */
  9741. /**
  9742. * as in if(i>8)
  9743. */
  9744. var I1LIMIT = 8;
  9745. /**
  9746. * as in if(i>24) . changed 23
  9747. */
  9748. var I2LIMIT = 23;
  9749. /**
  9750. * as in if(m<15)
  9751. */
  9752. var MLIMIT = 15;
  9753. var ma_max_i1;
  9754. var ma_max_i2;
  9755. var ma_max_m;
  9756. /**
  9757. * This is the masking table:<BR>
  9758. * According to tonality, values are going from 0dB (TMN) to 9.3dB (NMT).<BR>
  9759. * After additive masking computation, 8dB are added, so final values are
  9760. * going from 8dB to 17.3dB
  9761. *
  9762. * pow(10, -0.0..-0.6)
  9763. */
  9764. var tab = [1.0, 0.79433, 0.63096, 0.63096,
  9765. 0.63096, 0.63096, 0.63096, 0.25119, 0.11749];
  9766. function init_mask_add_max_values() {
  9767. ma_max_i1 = Math.pow(10, (I1LIMIT + 1) / 16.0);
  9768. ma_max_i2 = Math.pow(10, (I2LIMIT + 1) / 16.0);
  9769. ma_max_m = Math.pow(10, (MLIMIT) / 10.0);
  9770. }
  9771. var table1 = [3.3246 * 3.3246,
  9772. 3.23837 * 3.23837, 3.15437 * 3.15437, 3.00412 * 3.00412,
  9773. 2.86103 * 2.86103, 2.65407 * 2.65407, 2.46209 * 2.46209,
  9774. 2.284 * 2.284, 2.11879 * 2.11879, 1.96552 * 1.96552,
  9775. 1.82335 * 1.82335, 1.69146 * 1.69146, 1.56911 * 1.56911,
  9776. 1.46658 * 1.46658, 1.37074 * 1.37074, 1.31036 * 1.31036,
  9777. 1.25264 * 1.25264, 1.20648 * 1.20648, 1.16203 * 1.16203,
  9778. 1.12765 * 1.12765, 1.09428 * 1.09428, 1.0659 * 1.0659,
  9779. 1.03826 * 1.03826, 1.01895 * 1.01895, 1];
  9780. var table2 = [1.33352 * 1.33352,
  9781. 1.35879 * 1.35879, 1.38454 * 1.38454, 1.39497 * 1.39497,
  9782. 1.40548 * 1.40548, 1.3537 * 1.3537, 1.30382 * 1.30382,
  9783. 1.22321 * 1.22321, 1.14758 * 1.14758, 1];
  9784. var table3 = [2.35364 * 2.35364,
  9785. 2.29259 * 2.29259, 2.23313 * 2.23313, 2.12675 * 2.12675,
  9786. 2.02545 * 2.02545, 1.87894 * 1.87894, 1.74303 * 1.74303,
  9787. 1.61695 * 1.61695, 1.49999 * 1.49999, 1.39148 * 1.39148,
  9788. 1.29083 * 1.29083, 1.19746 * 1.19746, 1.11084 * 1.11084,
  9789. 1.03826 * 1.03826];
  9790. /**
  9791. * addition of simultaneous masking Naoki Shibata 2000/7
  9792. */
  9793. function mask_add(m1, m2, kk, b, gfc, shortblock) {
  9794. var ratio;
  9795. if (m2 > m1) {
  9796. if (m2 < (m1 * ma_max_i2))
  9797. ratio = m2 / m1;
  9798. else
  9799. return (m1 + m2);
  9800. } else {
  9801. if (m1 >= (m2 * ma_max_i2))
  9802. return (m1 + m2);
  9803. ratio = m1 / m2;
  9804. }
  9805. /* Should always be true, just checking */
  9806. m1 += m2;
  9807. //if (((long)(b + 3) & 0xffffffff) <= 3 + 3) {
  9808. if ((b + 3) <= 3 + 3) {
  9809. /* approximately, 1 bark = 3 partitions */
  9810. /* 65% of the cases */
  9811. /* originally 'if(i > 8)' */
  9812. if (ratio >= ma_max_i1) {
  9813. /* 43% of the total */
  9814. return m1;
  9815. }
  9816. /* 22% of the total */
  9817. var i = 0 | (Util.FAST_LOG10_X(ratio, 16.0));
  9818. return m1 * table2[i];
  9819. }
  9820. /**
  9821. * <PRE>
  9822. * m<15 equ log10((m1+m2)/gfc.ATH.cb[k])<1.5
  9823. * equ (m1+m2)/gfc.ATH.cb[k]<10^1.5
  9824. * equ (m1+m2)<10^1.5 * gfc.ATH.cb[k]
  9825. * </PRE>
  9826. */
  9827. var i = 0 | Util.FAST_LOG10_X(ratio, 16.0);
  9828. if (shortblock != 0) {
  9829. m2 = gfc.ATH.cb_s[kk] * gfc.ATH.adjust;
  9830. } else {
  9831. m2 = gfc.ATH.cb_l[kk] * gfc.ATH.adjust;
  9832. }
  9833. if (m1 < ma_max_m * m2) {
  9834. /* 3% of the total */
  9835. /* Originally if (m > 0) { */
  9836. if (m1 > m2) {
  9837. var f, r;
  9838. f = 1.0;
  9839. if (i <= 13)
  9840. f = table3[i];
  9841. r = Util.FAST_LOG10_X(m1 / m2, 10.0 / 15.0);
  9842. return m1 * ((table1[i] - f) * r + f);
  9843. }
  9844. if (i > 13)
  9845. return m1;
  9846. return m1 * table3[i];
  9847. }
  9848. /* 10% of total */
  9849. return m1 * table1[i];
  9850. }
  9851. var table2_ = [1.33352 * 1.33352,
  9852. 1.35879 * 1.35879, 1.38454 * 1.38454, 1.39497 * 1.39497,
  9853. 1.40548 * 1.40548, 1.3537 * 1.3537, 1.30382 * 1.30382,
  9854. 1.22321 * 1.22321, 1.14758 * 1.14758, 1];
  9855. /**
  9856. * addition of simultaneous masking Naoki Shibata 2000/7
  9857. */
  9858. function vbrpsy_mask_add(m1, m2, b) {
  9859. var ratio;
  9860. if (m1 < 0) {
  9861. m1 = 0;
  9862. }
  9863. if (m2 < 0) {
  9864. m2 = 0;
  9865. }
  9866. if (m1 <= 0) {
  9867. return m2;
  9868. }
  9869. if (m2 <= 0) {
  9870. return m1;
  9871. }
  9872. if (m2 > m1) {
  9873. ratio = m2 / m1;
  9874. } else {
  9875. ratio = m1 / m2;
  9876. }
  9877. if (-2 <= b && b <= 2) {
  9878. /* approximately, 1 bark = 3 partitions */
  9879. /* originally 'if(i > 8)' */
  9880. if (ratio >= ma_max_i1) {
  9881. return m1 + m2;
  9882. } else {
  9883. var i = 0 | (Util.FAST_LOG10_X(ratio, 16.0));
  9884. return (m1 + m2) * table2_[i];
  9885. }
  9886. }
  9887. if (ratio < ma_max_i2) {
  9888. return m1 + m2;
  9889. }
  9890. if (m1 < m2) {
  9891. m1 = m2;
  9892. }
  9893. return m1;
  9894. }
  9895. /**
  9896. * compute interchannel masking effects
  9897. */
  9898. function calc_interchannel_masking(gfp, ratio) {
  9899. var gfc = gfp.internal_flags;
  9900. if (gfc.channels_out > 1) {
  9901. for (var sb = 0; sb < Encoder.SBMAX_l; sb++) {
  9902. var l = gfc.thm[0].l[sb];
  9903. var r = gfc.thm[1].l[sb];
  9904. gfc.thm[0].l[sb] += r * ratio;
  9905. gfc.thm[1].l[sb] += l * ratio;
  9906. }
  9907. for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {
  9908. for (var sblock = 0; sblock < 3; sblock++) {
  9909. var l = gfc.thm[0].s[sb][sblock];
  9910. var r = gfc.thm[1].s[sb][sblock];
  9911. gfc.thm[0].s[sb][sblock] += r * ratio;
  9912. gfc.thm[1].s[sb][sblock] += l * ratio;
  9913. }
  9914. }
  9915. }
  9916. }
  9917. /**
  9918. * compute M/S thresholds from Johnston & Ferreira 1992 ICASSP paper
  9919. */
  9920. function msfix1(gfc) {
  9921. for (var sb = 0; sb < Encoder.SBMAX_l; sb++) {
  9922. /* use this fix if L & R masking differs by 2db or less */
  9923. /* if db = 10*log10(x2/x1) < 2 */
  9924. /* if (x2 < 1.58*x1) { */
  9925. if (gfc.thm[0].l[sb] > 1.58 * gfc.thm[1].l[sb]
  9926. || gfc.thm[1].l[sb] > 1.58 * gfc.thm[0].l[sb])
  9927. continue;
  9928. var mld = gfc.mld_l[sb] * gfc.en[3].l[sb];
  9929. var rmid = Math.max(gfc.thm[2].l[sb],
  9930. Math.min(gfc.thm[3].l[sb], mld));
  9931. mld = gfc.mld_l[sb] * gfc.en[2].l[sb];
  9932. var rside = Math.max(gfc.thm[3].l[sb],
  9933. Math.min(gfc.thm[2].l[sb], mld));
  9934. gfc.thm[2].l[sb] = rmid;
  9935. gfc.thm[3].l[sb] = rside;
  9936. }
  9937. for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {
  9938. for (var sblock = 0; sblock < 3; sblock++) {
  9939. if (gfc.thm[0].s[sb][sblock] > 1.58 * gfc.thm[1].s[sb][sblock]
  9940. || gfc.thm[1].s[sb][sblock] > 1.58 * gfc.thm[0].s[sb][sblock])
  9941. continue;
  9942. var mld = gfc.mld_s[sb] * gfc.en[3].s[sb][sblock];
  9943. var rmid = Math.max(gfc.thm[2].s[sb][sblock],
  9944. Math.min(gfc.thm[3].s[sb][sblock], mld));
  9945. mld = gfc.mld_s[sb] * gfc.en[2].s[sb][sblock];
  9946. var rside = Math.max(gfc.thm[3].s[sb][sblock],
  9947. Math.min(gfc.thm[2].s[sb][sblock], mld));
  9948. gfc.thm[2].s[sb][sblock] = rmid;
  9949. gfc.thm[3].s[sb][sblock] = rside;
  9950. }
  9951. }
  9952. }
  9953. /**
  9954. * Adjust M/S maskings if user set "msfix"
  9955. *
  9956. * Naoki Shibata 2000
  9957. */
  9958. function ns_msfix(gfc, msfix, athadjust) {
  9959. var msfix2 = msfix;
  9960. var athlower = Math.pow(10, athadjust);
  9961. msfix *= 2.0;
  9962. msfix2 *= 2.0;
  9963. for (var sb = 0; sb < Encoder.SBMAX_l; sb++) {
  9964. var thmLR, thmM, thmS, ath;
  9965. ath = (gfc.ATH.cb_l[gfc.bm_l[sb]]) * athlower;
  9966. thmLR = Math.min(Math.max(gfc.thm[0].l[sb], ath),
  9967. Math.max(gfc.thm[1].l[sb], ath));
  9968. thmM = Math.max(gfc.thm[2].l[sb], ath);
  9969. thmS = Math.max(gfc.thm[3].l[sb], ath);
  9970. if (thmLR * msfix < thmM + thmS) {
  9971. var f = thmLR * msfix2 / (thmM + thmS);
  9972. thmM *= f;
  9973. thmS *= f;
  9974. }
  9975. gfc.thm[2].l[sb] = Math.min(thmM, gfc.thm[2].l[sb]);
  9976. gfc.thm[3].l[sb] = Math.min(thmS, gfc.thm[3].l[sb]);
  9977. }
  9978. athlower *= ( Encoder.BLKSIZE_s / Encoder.BLKSIZE);
  9979. for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {
  9980. for (var sblock = 0; sblock < 3; sblock++) {
  9981. var thmLR, thmM, thmS, ath;
  9982. ath = (gfc.ATH.cb_s[gfc.bm_s[sb]]) * athlower;
  9983. thmLR = Math.min(Math.max(gfc.thm[0].s[sb][sblock], ath),
  9984. Math.max(gfc.thm[1].s[sb][sblock], ath));
  9985. thmM = Math.max(gfc.thm[2].s[sb][sblock], ath);
  9986. thmS = Math.max(gfc.thm[3].s[sb][sblock], ath);
  9987. if (thmLR * msfix < thmM + thmS) {
  9988. var f = thmLR * msfix / (thmM + thmS);
  9989. thmM *= f;
  9990. thmS *= f;
  9991. }
  9992. gfc.thm[2].s[sb][sblock] = Math.min(gfc.thm[2].s[sb][sblock],
  9993. thmM);
  9994. gfc.thm[3].s[sb][sblock] = Math.min(gfc.thm[3].s[sb][sblock],
  9995. thmS);
  9996. }
  9997. }
  9998. }
  9999. /**
  10000. * short block threshold calculation (part 2)
  10001. *
  10002. * partition band bo_s[sfb] is at the transition from scalefactor band sfb
  10003. * to the next one sfb+1; enn and thmm have to be split between them
  10004. */
  10005. function convert_partition2scalefac_s(gfc, eb, thr, chn, sblock) {
  10006. var sb, b;
  10007. var enn = 0.0;
  10008. var thmm = 0.0;
  10009. for (sb = b = 0; sb < Encoder.SBMAX_s; ++b, ++sb) {
  10010. var bo_s_sb = gfc.bo_s[sb];
  10011. var npart_s = gfc.npart_s;
  10012. var b_lim = bo_s_sb < npart_s ? bo_s_sb : npart_s;
  10013. while (b < b_lim) {
  10014. // iff failed, it may indicate some index error elsewhere
  10015. enn += eb[b];
  10016. thmm += thr[b];
  10017. b++;
  10018. }
  10019. gfc.en[chn].s[sb][sblock] = enn;
  10020. gfc.thm[chn].s[sb][sblock] = thmm;
  10021. if (b >= npart_s) {
  10022. ++sb;
  10023. break;
  10024. }
  10025. // iff failed, it may indicate some index error elsewhere
  10026. {
  10027. /* at transition sfb . sfb+1 */
  10028. var w_curr = gfc.PSY.bo_s_weight[sb];
  10029. var w_next = 1.0 - w_curr;
  10030. enn = w_curr * eb[b];
  10031. thmm = w_curr * thr[b];
  10032. gfc.en[chn].s[sb][sblock] += enn;
  10033. gfc.thm[chn].s[sb][sblock] += thmm;
  10034. enn = w_next * eb[b];
  10035. thmm = w_next * thr[b];
  10036. }
  10037. }
  10038. /* zero initialize the rest */
  10039. for (; sb < Encoder.SBMAX_s; ++sb) {
  10040. gfc.en[chn].s[sb][sblock] = 0;
  10041. gfc.thm[chn].s[sb][sblock] = 0;
  10042. }
  10043. }
  10044. /**
  10045. * longblock threshold calculation (part 2)
  10046. */
  10047. function convert_partition2scalefac_l(gfc, eb, thr, chn) {
  10048. var sb, b;
  10049. var enn = 0.0;
  10050. var thmm = 0.0;
  10051. for (sb = b = 0; sb < Encoder.SBMAX_l; ++b, ++sb) {
  10052. var bo_l_sb = gfc.bo_l[sb];
  10053. var npart_l = gfc.npart_l;
  10054. var b_lim = bo_l_sb < npart_l ? bo_l_sb : npart_l;
  10055. while (b < b_lim) {
  10056. // iff failed, it may indicate some index error elsewhere
  10057. enn += eb[b];
  10058. thmm += thr[b];
  10059. b++;
  10060. }
  10061. gfc.en[chn].l[sb] = enn;
  10062. gfc.thm[chn].l[sb] = thmm;
  10063. if (b >= npart_l) {
  10064. ++sb;
  10065. break;
  10066. }
  10067. {
  10068. /* at transition sfb . sfb+1 */
  10069. var w_curr = gfc.PSY.bo_l_weight[sb];
  10070. var w_next = 1.0 - w_curr;
  10071. enn = w_curr * eb[b];
  10072. thmm = w_curr * thr[b];
  10073. gfc.en[chn].l[sb] += enn;
  10074. gfc.thm[chn].l[sb] += thmm;
  10075. enn = w_next * eb[b];
  10076. thmm = w_next * thr[b];
  10077. }
  10078. }
  10079. /* zero initialize the rest */
  10080. for (; sb < Encoder.SBMAX_l; ++sb) {
  10081. gfc.en[chn].l[sb] = 0;
  10082. gfc.thm[chn].l[sb] = 0;
  10083. }
  10084. }
  10085. function compute_masking_s(gfp, fftenergy_s, eb, thr, chn, sblock) {
  10086. var gfc = gfp.internal_flags;
  10087. var j, b;
  10088. for (b = j = 0; b < gfc.npart_s; ++b) {
  10089. var ebb = 0, m = 0;
  10090. var n = gfc.numlines_s[b];
  10091. for (var i = 0; i < n; ++i, ++j) {
  10092. var el = fftenergy_s[sblock][j];
  10093. ebb += el;
  10094. if (m < el)
  10095. m = el;
  10096. }
  10097. eb[b] = ebb;
  10098. }
  10099. for (j = b = 0; b < gfc.npart_s; b++) {
  10100. var kk = gfc.s3ind_s[b][0];
  10101. var ecb = gfc.s3_ss[j++] * eb[kk];
  10102. ++kk;
  10103. while (kk <= gfc.s3ind_s[b][1]) {
  10104. ecb += gfc.s3_ss[j] * eb[kk];
  10105. ++j;
  10106. ++kk;
  10107. }
  10108. { /* limit calculated threshold by previous granule */
  10109. var x = rpelev_s * gfc.nb_s1[chn][b];
  10110. thr[b] = Math.min(ecb, x);
  10111. }
  10112. if (gfc.blocktype_old[chn & 1] == Encoder.SHORT_TYPE) {
  10113. /* limit calculated threshold by even older granule */
  10114. var x = rpelev2_s * gfc.nb_s2[chn][b];
  10115. var y = thr[b];
  10116. thr[b] = Math.min(x, y);
  10117. }
  10118. gfc.nb_s2[chn][b] = gfc.nb_s1[chn][b];
  10119. gfc.nb_s1[chn][b] = ecb;
  10120. }
  10121. for (; b <= Encoder.CBANDS; ++b) {
  10122. eb[b] = 0;
  10123. thr[b] = 0;
  10124. }
  10125. }
  10126. function block_type_set(gfp, uselongblock, blocktype_d, blocktype) {
  10127. var gfc = gfp.internal_flags;
  10128. if (gfp.short_blocks == ShortBlock.short_block_coupled
  10129. /* force both channels to use the same block type */
  10130. /* this is necessary if the frame is to be encoded in ms_stereo. */
  10131. /* But even without ms_stereo, FhG does this */
  10132. && !(uselongblock[0] != 0 && uselongblock[1] != 0))
  10133. uselongblock[0] = uselongblock[1] = 0;
  10134. /*
  10135. * update the blocktype of the previous granule, since it depends on
  10136. * what happend in this granule
  10137. */
  10138. for (var chn = 0; chn < gfc.channels_out; chn++) {
  10139. blocktype[chn] = Encoder.NORM_TYPE;
  10140. /* disable short blocks */
  10141. if (gfp.short_blocks == ShortBlock.short_block_dispensed)
  10142. uselongblock[chn] = 1;
  10143. if (gfp.short_blocks == ShortBlock.short_block_forced)
  10144. uselongblock[chn] = 0;
  10145. if (uselongblock[chn] != 0) {
  10146. /* no attack : use long blocks */
  10147. if (gfc.blocktype_old[chn] == Encoder.SHORT_TYPE)
  10148. blocktype[chn] = Encoder.STOP_TYPE;
  10149. } else {
  10150. /* attack : use short blocks */
  10151. blocktype[chn] = Encoder.SHORT_TYPE;
  10152. if (gfc.blocktype_old[chn] == Encoder.NORM_TYPE) {
  10153. gfc.blocktype_old[chn] = Encoder.START_TYPE;
  10154. }
  10155. if (gfc.blocktype_old[chn] == Encoder.STOP_TYPE)
  10156. gfc.blocktype_old[chn] = Encoder.SHORT_TYPE;
  10157. }
  10158. blocktype_d[chn] = gfc.blocktype_old[chn];
  10159. // value returned to calling program
  10160. gfc.blocktype_old[chn] = blocktype[chn];
  10161. // save for next call to l3psy_anal
  10162. }
  10163. }
  10164. function NS_INTERP(x, y, r) {
  10165. /* was pow((x),(r))*pow((y),1-(r)) */
  10166. if (r >= 1.0) {
  10167. /* 99.7% of the time */
  10168. return x;
  10169. }
  10170. if (r <= 0.0)
  10171. return y;
  10172. if (y > 0.0) {
  10173. /* rest of the time */
  10174. return (Math.pow(x / y, r) * y);
  10175. }
  10176. /* never happens */
  10177. return 0.0;
  10178. }
  10179. /**
  10180. * these values are tuned only for 44.1kHz...
  10181. */
  10182. var regcoef_s = [11.8, 13.6, 17.2, 32, 46.5,
  10183. 51.3, 57.5, 67.1, 71.5, 84.6, 97.6, 130,
  10184. /* 255.8 */
  10185. ];
  10186. function pecalc_s(mr, masking_lower) {
  10187. var pe_s = 1236.28 / 4;
  10188. for (var sb = 0; sb < Encoder.SBMAX_s - 1; sb++) {
  10189. for (var sblock = 0; sblock < 3; sblock++) {
  10190. var thm = mr.thm.s[sb][sblock];
  10191. if (thm > 0.0) {
  10192. var x = thm * masking_lower;
  10193. var en = mr.en.s[sb][sblock];
  10194. if (en > x) {
  10195. if (en > x * 1e10) {
  10196. pe_s += regcoef_s[sb] * (10.0 * LOG10);
  10197. } else {
  10198. pe_s += regcoef_s[sb] * Util.FAST_LOG10(en / x);
  10199. }
  10200. }
  10201. }
  10202. }
  10203. }
  10204. return pe_s;
  10205. }
  10206. /**
  10207. * these values are tuned only for 44.1kHz...
  10208. */
  10209. var regcoef_l = [6.8, 5.8, 5.8, 6.4, 6.5, 9.9,
  10210. 12.1, 14.4, 15, 18.9, 21.6, 26.9, 34.2, 40.2, 46.8, 56.5,
  10211. 60.7, 73.9, 85.7, 93.4, 126.1,
  10212. /* 241.3 */
  10213. ];
  10214. function pecalc_l(mr, masking_lower) {
  10215. var pe_l = 1124.23 / 4;
  10216. for (var sb = 0; sb < Encoder.SBMAX_l - 1; sb++) {
  10217. var thm = mr.thm.l[sb];
  10218. if (thm > 0.0) {
  10219. var x = thm * masking_lower;
  10220. var en = mr.en.l[sb];
  10221. if (en > x) {
  10222. if (en > x * 1e10) {
  10223. pe_l += regcoef_l[sb] * (10.0 * LOG10);
  10224. } else {
  10225. pe_l += regcoef_l[sb] * Util.FAST_LOG10(en / x);
  10226. }
  10227. }
  10228. }
  10229. }
  10230. return pe_l;
  10231. }
  10232. function calc_energy(gfc, fftenergy, eb, max, avg) {
  10233. var b, j;
  10234. for (b = j = 0; b < gfc.npart_l; ++b) {
  10235. var ebb = 0, m = 0;
  10236. var i;
  10237. for (i = 0; i < gfc.numlines_l[b]; ++i, ++j) {
  10238. var el = fftenergy[j];
  10239. ebb += el;
  10240. if (m < el)
  10241. m = el;
  10242. }
  10243. eb[b] = ebb;
  10244. max[b] = m;
  10245. avg[b] = ebb * gfc.rnumlines_l[b];
  10246. }
  10247. }
  10248. function calc_mask_index_l(gfc, max, avg, mask_idx) {
  10249. var last_tab_entry = tab.length - 1;
  10250. var b = 0;
  10251. var a = avg[b] + avg[b + 1];
  10252. if (a > 0.0) {
  10253. var m = max[b];
  10254. if (m < max[b + 1])
  10255. m = max[b + 1];
  10256. a = 20.0 * (m * 2.0 - a)
  10257. / (a * (gfc.numlines_l[b] + gfc.numlines_l[b + 1] - 1));
  10258. var k = 0 | a;
  10259. if (k > last_tab_entry)
  10260. k = last_tab_entry;
  10261. mask_idx[b] = k;
  10262. } else {
  10263. mask_idx[b] = 0;
  10264. }
  10265. for (b = 1; b < gfc.npart_l - 1; b++) {
  10266. a = avg[b - 1] + avg[b] + avg[b + 1];
  10267. if (a > 0.0) {
  10268. var m = max[b - 1];
  10269. if (m < max[b])
  10270. m = max[b];
  10271. if (m < max[b + 1])
  10272. m = max[b + 1];
  10273. a = 20.0
  10274. * (m * 3.0 - a)
  10275. / (a * (gfc.numlines_l[b - 1] + gfc.numlines_l[b]
  10276. + gfc.numlines_l[b + 1] - 1));
  10277. var k = 0 | a;
  10278. if (k > last_tab_entry)
  10279. k = last_tab_entry;
  10280. mask_idx[b] = k;
  10281. } else {
  10282. mask_idx[b] = 0;
  10283. }
  10284. }
  10285. a = avg[b - 1] + avg[b];
  10286. if (a > 0.0) {
  10287. var m = max[b - 1];
  10288. if (m < max[b])
  10289. m = max[b];
  10290. a = 20.0 * (m * 2.0 - a)
  10291. / (a * (gfc.numlines_l[b - 1] + gfc.numlines_l[b] - 1));
  10292. var k = 0 | a;
  10293. if (k > last_tab_entry)
  10294. k = last_tab_entry;
  10295. mask_idx[b] = k;
  10296. } else {
  10297. mask_idx[b] = 0;
  10298. }
  10299. }
  10300. var fircoef = [
  10301. -8.65163e-18 * 2, -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,
  10302. -3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2, 0.0931738 * 2,
  10303. -5.52212e-17 * 2, -0.313819 * 2
  10304. ];
  10305. this.L3psycho_anal_ns = function (gfp, buffer, bufPos, gr_out, masking_ratio, masking_MS_ratio, percep_entropy, percep_MS_entropy, energy, blocktype_d) {
  10306. /*
  10307. * to get a good cache performance, one has to think about the sequence,
  10308. * in which the variables are used.
  10309. */
  10310. var gfc = gfp.internal_flags;
  10311. /* fft and energy calculation */
  10312. var wsamp_L = new_float_n([2, Encoder.BLKSIZE]);
  10313. var wsamp_S = new_float_n([2, 3, Encoder.BLKSIZE_s]);
  10314. /* convolution */
  10315. var eb_l = new_float(Encoder.CBANDS + 1);
  10316. var eb_s = new_float(Encoder.CBANDS + 1);
  10317. var thr = new_float(Encoder.CBANDS + 2);
  10318. /* block type */
  10319. var blocktype = new_int(2), uselongblock = new_int(2);
  10320. /* usual variables like loop indices, etc.. */
  10321. var numchn, chn;
  10322. var b, i, j, k;
  10323. var sb, sblock;
  10324. /* variables used for --nspsytune */
  10325. var ns_hpfsmpl = new_float_n([2, 576]);
  10326. var pcfact;
  10327. var mask_idx_l = new_int(Encoder.CBANDS + 2), mask_idx_s = new_int(Encoder.CBANDS + 2);
  10328. Arrays.fill(mask_idx_s, 0);
  10329. numchn = gfc.channels_out;
  10330. /* chn=2 and 3 = Mid and Side channels */
  10331. if (gfp.mode == MPEGMode.JOINT_STEREO)
  10332. numchn = 4;
  10333. if (gfp.VBR == VbrMode.vbr_off)
  10334. pcfact = gfc.ResvMax == 0 ? 0 : ( gfc.ResvSize)
  10335. / gfc.ResvMax * 0.5;
  10336. else if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh
  10337. || gfp.VBR == VbrMode.vbr_mt) {
  10338. pcfact = 0.6;
  10339. } else
  10340. pcfact = 1.0;
  10341. /**********************************************************************
  10342. * Apply HPF of fs/4 to the input signal. This is used for attack
  10343. * detection / handling.
  10344. **********************************************************************/
  10345. /* Don't copy the input buffer into a temporary buffer */
  10346. /* unroll the loop 2 times */
  10347. for (chn = 0; chn < gfc.channels_out; chn++) {
  10348. /* apply high pass filter of fs/4 */
  10349. var firbuf = buffer[chn];
  10350. var firbufPos = bufPos + 576 - 350 - NSFIRLEN + 192;
  10351. for (i = 0; i < 576; i++) {
  10352. var sum1, sum2;
  10353. sum1 = firbuf[firbufPos + i + 10];
  10354. sum2 = 0.0;
  10355. for (j = 0; j < ((NSFIRLEN - 1) / 2) - 1; j += 2) {
  10356. sum1 += fircoef[j]
  10357. * (firbuf[firbufPos + i + j] + firbuf[firbufPos + i
  10358. + NSFIRLEN - j]);
  10359. sum2 += fircoef[j + 1]
  10360. * (firbuf[firbufPos + i + j + 1] + firbuf[firbufPos
  10361. + i + NSFIRLEN - j - 1]);
  10362. }
  10363. ns_hpfsmpl[chn][i] = sum1 + sum2;
  10364. }
  10365. masking_ratio[gr_out][chn].en.assign(gfc.en[chn]);
  10366. masking_ratio[gr_out][chn].thm.assign(gfc.thm[chn]);
  10367. if (numchn > 2) {
  10368. /* MS maskings */
  10369. /* percep_MS_entropy [chn-2] = gfc . pe [chn]; */
  10370. masking_MS_ratio[gr_out][chn].en.assign(gfc.en[chn + 2]);
  10371. masking_MS_ratio[gr_out][chn].thm.assign(gfc.thm[chn + 2]);
  10372. }
  10373. }
  10374. for (chn = 0; chn < numchn; chn++) {
  10375. var wsamp_l;
  10376. var wsamp_s;
  10377. var en_subshort = new_float(12);
  10378. var en_short = [0, 0, 0, 0];
  10379. var attack_intensity = new_float(12);
  10380. var ns_uselongblock = 1;
  10381. var attackThreshold;
  10382. var max = new_float(Encoder.CBANDS), avg = new_float(Encoder.CBANDS);
  10383. var ns_attacks = [0, 0, 0, 0];
  10384. var fftenergy = new_float(Encoder.HBLKSIZE);
  10385. var fftenergy_s = new_float_n([3, Encoder.HBLKSIZE_s]);
  10386. /*
  10387. * rh 20040301: the following loops do access one off the limits so
  10388. * I increase the array dimensions by one and initialize the
  10389. * accessed values to zero
  10390. */
  10391. /***************************************************************
  10392. * determine the block type (window type)
  10393. ***************************************************************/
  10394. /* calculate energies of each sub-shortblocks */
  10395. for (i = 0; i < 3; i++) {
  10396. en_subshort[i] = gfc.nsPsy.last_en_subshort[chn][i + 6];
  10397. attack_intensity[i] = en_subshort[i]
  10398. / gfc.nsPsy.last_en_subshort[chn][i + 4];
  10399. en_short[0] += en_subshort[i];
  10400. }
  10401. if (chn == 2) {
  10402. for (i = 0; i < 576; i++) {
  10403. var l, r;
  10404. l = ns_hpfsmpl[0][i];
  10405. r = ns_hpfsmpl[1][i];
  10406. ns_hpfsmpl[0][i] = l + r;
  10407. ns_hpfsmpl[1][i] = l - r;
  10408. }
  10409. }
  10410. {
  10411. var pf = ns_hpfsmpl[chn & 1];
  10412. var pfPos = 0;
  10413. for (i = 0; i < 9; i++) {
  10414. var pfe = pfPos + 576 / 9;
  10415. var p = 1.;
  10416. for (; pfPos < pfe; pfPos++)
  10417. if (p < Math.abs(pf[pfPos]))
  10418. p = Math.abs(pf[pfPos]);
  10419. gfc.nsPsy.last_en_subshort[chn][i] = en_subshort[i + 3] = p;
  10420. en_short[1 + i / 3] += p;
  10421. if (p > en_subshort[i + 3 - 2]) {
  10422. p = p / en_subshort[i + 3 - 2];
  10423. } else if (en_subshort[i + 3 - 2] > p * 10.0) {
  10424. p = en_subshort[i + 3 - 2] / (p * 10.0);
  10425. } else
  10426. p = 0.0;
  10427. attack_intensity[i + 3] = p;
  10428. }
  10429. }
  10430. if (gfp.analysis) {
  10431. var x = attack_intensity[0];
  10432. for (i = 1; i < 12; i++)
  10433. if (x < attack_intensity[i])
  10434. x = attack_intensity[i];
  10435. gfc.pinfo.ers[gr_out][chn] = gfc.pinfo.ers_save[chn];
  10436. gfc.pinfo.ers_save[chn] = x;
  10437. }
  10438. /* compare energies between sub-shortblocks */
  10439. attackThreshold = (chn == 3) ? gfc.nsPsy.attackthre_s
  10440. : gfc.nsPsy.attackthre;
  10441. for (i = 0; i < 12; i++)
  10442. if (0 == ns_attacks[i / 3]
  10443. && attack_intensity[i] > attackThreshold)
  10444. ns_attacks[i / 3] = (i % 3) + 1;
  10445. /*
  10446. * should have energy change between short blocks, in order to avoid
  10447. * periodic signals
  10448. */
  10449. for (i = 1; i < 4; i++) {
  10450. var ratio;
  10451. if (en_short[i - 1] > en_short[i]) {
  10452. ratio = en_short[i - 1] / en_short[i];
  10453. } else {
  10454. ratio = en_short[i] / en_short[i - 1];
  10455. }
  10456. if (ratio < 1.7) {
  10457. ns_attacks[i] = 0;
  10458. if (i == 1)
  10459. ns_attacks[0] = 0;
  10460. }
  10461. }
  10462. if (ns_attacks[0] != 0 && gfc.nsPsy.lastAttacks[chn] != 0)
  10463. ns_attacks[0] = 0;
  10464. if (gfc.nsPsy.lastAttacks[chn] == 3
  10465. || (ns_attacks[0] + ns_attacks[1] + ns_attacks[2] + ns_attacks[3]) != 0) {
  10466. ns_uselongblock = 0;
  10467. if (ns_attacks[1] != 0 && ns_attacks[0] != 0)
  10468. ns_attacks[1] = 0;
  10469. if (ns_attacks[2] != 0 && ns_attacks[1] != 0)
  10470. ns_attacks[2] = 0;
  10471. if (ns_attacks[3] != 0 && ns_attacks[2] != 0)
  10472. ns_attacks[3] = 0;
  10473. }
  10474. if (chn < 2) {
  10475. uselongblock[chn] = ns_uselongblock;
  10476. } else {
  10477. if (ns_uselongblock == 0) {
  10478. uselongblock[0] = uselongblock[1] = 0;
  10479. }
  10480. }
  10481. /*
  10482. * there is a one granule delay. Copy maskings computed last call
  10483. * into masking_ratio to return to calling program.
  10484. */
  10485. energy[chn] = gfc.tot_ener[chn];
  10486. /*********************************************************************
  10487. * compute FFTs
  10488. *********************************************************************/
  10489. wsamp_s = wsamp_S;
  10490. wsamp_l = wsamp_L;
  10491. compute_ffts(gfp, fftenergy, fftenergy_s, wsamp_l, (chn & 1),
  10492. wsamp_s, (chn & 1), gr_out, chn, buffer, bufPos);
  10493. /*********************************************************************
  10494. * Calculate the energy and the tonality of each partition.
  10495. *********************************************************************/
  10496. calc_energy(gfc, fftenergy, eb_l, max, avg);
  10497. calc_mask_index_l(gfc, max, avg, mask_idx_l);
  10498. /* compute masking thresholds for short blocks */
  10499. for (sblock = 0; sblock < 3; sblock++) {
  10500. var enn, thmm;
  10501. compute_masking_s(gfp, fftenergy_s, eb_s, thr, chn, sblock);
  10502. convert_partition2scalefac_s(gfc, eb_s, thr, chn, sblock);
  10503. /**** short block pre-echo control ****/
  10504. for (sb = 0; sb < Encoder.SBMAX_s; sb++) {
  10505. thmm = gfc.thm[chn].s[sb][sblock];
  10506. thmm *= NS_PREECHO_ATT0;
  10507. if (ns_attacks[sblock] >= 2 || ns_attacks[sblock + 1] == 1) {
  10508. var idx = (sblock != 0) ? sblock - 1 : 2;
  10509. var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,
  10510. NS_PREECHO_ATT1 * pcfact);
  10511. thmm = Math.min(thmm, p);
  10512. }
  10513. if (ns_attacks[sblock] == 1) {
  10514. var idx = (sblock != 0) ? sblock - 1 : 2;
  10515. var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,
  10516. NS_PREECHO_ATT2 * pcfact);
  10517. thmm = Math.min(thmm, p);
  10518. } else if ((sblock != 0 && ns_attacks[sblock - 1] == 3)
  10519. || (sblock == 0 && gfc.nsPsy.lastAttacks[chn] == 3)) {
  10520. var idx = (sblock != 2) ? sblock + 1 : 0;
  10521. var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,
  10522. NS_PREECHO_ATT2 * pcfact);
  10523. thmm = Math.min(thmm, p);
  10524. }
  10525. /* pulse like signal detection for fatboy.wav and so on */
  10526. enn = en_subshort[sblock * 3 + 3]
  10527. + en_subshort[sblock * 3 + 4]
  10528. + en_subshort[sblock * 3 + 5];
  10529. if (en_subshort[sblock * 3 + 5] * 6 < enn) {
  10530. thmm *= 0.5;
  10531. if (en_subshort[sblock * 3 + 4] * 6 < enn)
  10532. thmm *= 0.5;
  10533. }
  10534. gfc.thm[chn].s[sb][sblock] = thmm;
  10535. }
  10536. }
  10537. gfc.nsPsy.lastAttacks[chn] = ns_attacks[2];
  10538. /*********************************************************************
  10539. * convolve the partitioned energy and unpredictability with the
  10540. * spreading function, s3_l[b][k]
  10541. ********************************************************************/
  10542. k = 0;
  10543. {
  10544. for (b = 0; b < gfc.npart_l; b++) {
  10545. /*
  10546. * convolve the partitioned energy with the spreading
  10547. * function
  10548. */
  10549. var kk = gfc.s3ind[b][0];
  10550. var eb2 = eb_l[kk] * tab[mask_idx_l[kk]];
  10551. var ecb = gfc.s3_ll[k++] * eb2;
  10552. while (++kk <= gfc.s3ind[b][1]) {
  10553. eb2 = eb_l[kk] * tab[mask_idx_l[kk]];
  10554. ecb = mask_add(ecb, gfc.s3_ll[k++] * eb2, kk, kk - b,
  10555. gfc, 0);
  10556. }
  10557. ecb *= 0.158489319246111;
  10558. /* pow(10,-0.8) */
  10559. /**** long block pre-echo control ****/
  10560. /**
  10561. * <PRE>
  10562. * dont use long block pre-echo control if previous granule was
  10563. * a short block. This is to avoid the situation:
  10564. * frame0: quiet (very low masking)
  10565. * frame1: surge (triggers short blocks)
  10566. * frame2: regular frame. looks like pre-echo when compared to
  10567. * frame0, but all pre-echo was in frame1.
  10568. * </PRE>
  10569. */
  10570. /*
  10571. * chn=0,1 L and R channels
  10572. *
  10573. * chn=2,3 S and M channels.
  10574. */
  10575. if (gfc.blocktype_old[chn & 1] == Encoder.SHORT_TYPE)
  10576. thr[b] = ecb;
  10577. else
  10578. thr[b] = NS_INTERP(
  10579. Math.min(ecb, Math.min(rpelev
  10580. * gfc.nb_1[chn][b], rpelev2
  10581. * gfc.nb_2[chn][b])), ecb, pcfact);
  10582. gfc.nb_2[chn][b] = gfc.nb_1[chn][b];
  10583. gfc.nb_1[chn][b] = ecb;
  10584. }
  10585. }
  10586. for (; b <= Encoder.CBANDS; ++b) {
  10587. eb_l[b] = 0;
  10588. thr[b] = 0;
  10589. }
  10590. /* compute masking thresholds for long blocks */
  10591. convert_partition2scalefac_l(gfc, eb_l, thr, chn);
  10592. }
  10593. /* end loop over chn */
  10594. if (gfp.mode == MPEGMode.STEREO || gfp.mode == MPEGMode.JOINT_STEREO) {
  10595. if (gfp.interChRatio > 0.0) {
  10596. calc_interchannel_masking(gfp, gfp.interChRatio);
  10597. }
  10598. }
  10599. if (gfp.mode == MPEGMode.JOINT_STEREO) {
  10600. var msfix;
  10601. msfix1(gfc);
  10602. msfix = gfp.msfix;
  10603. if (Math.abs(msfix) > 0.0)
  10604. ns_msfix(gfc, msfix, gfp.ATHlower * gfc.ATH.adjust);
  10605. }
  10606. /***************************************************************
  10607. * determine final block type
  10608. ***************************************************************/
  10609. block_type_set(gfp, uselongblock, blocktype_d, blocktype);
  10610. /*********************************************************************
  10611. * compute the value of PE to return ... no delay and advance
  10612. *********************************************************************/
  10613. for (chn = 0; chn < numchn; chn++) {
  10614. var ppe;
  10615. var ppePos = 0;
  10616. var type;
  10617. var mr;
  10618. if (chn > 1) {
  10619. ppe = percep_MS_entropy;
  10620. ppePos = -2;
  10621. type = Encoder.NORM_TYPE;
  10622. if (blocktype_d[0] == Encoder.SHORT_TYPE
  10623. || blocktype_d[1] == Encoder.SHORT_TYPE)
  10624. type = Encoder.SHORT_TYPE;
  10625. mr = masking_MS_ratio[gr_out][chn - 2];
  10626. } else {
  10627. ppe = percep_entropy;
  10628. ppePos = 0;
  10629. type = blocktype_d[chn];
  10630. mr = masking_ratio[gr_out][chn];
  10631. }
  10632. if (type == Encoder.SHORT_TYPE)
  10633. ppe[ppePos + chn] = pecalc_s(mr, gfc.masking_lower);
  10634. else
  10635. ppe[ppePos + chn] = pecalc_l(mr, gfc.masking_lower);
  10636. if (gfp.analysis)
  10637. gfc.pinfo.pe[gr_out][chn] = ppe[ppePos + chn];
  10638. }
  10639. return 0;
  10640. }
  10641. function vbrpsy_compute_fft_l(gfp, buffer, bufPos, chn, gr_out, fftenergy, wsamp_l, wsamp_lPos) {
  10642. var gfc = gfp.internal_flags;
  10643. if (chn < 2) {
  10644. fft.fft_long(gfc, wsamp_l[wsamp_lPos], chn, buffer, bufPos);
  10645. } else if (chn == 2) {
  10646. /* FFT data for mid and side channel is derived from L & R */
  10647. for (var j = Encoder.BLKSIZE - 1; j >= 0; --j) {
  10648. var l = wsamp_l[wsamp_lPos + 0][j];
  10649. var r = wsamp_l[wsamp_lPos + 1][j];
  10650. wsamp_l[wsamp_lPos + 0][j] = (l + r) * Util.SQRT2 * 0.5;
  10651. wsamp_l[wsamp_lPos + 1][j] = (l - r) * Util.SQRT2 * 0.5;
  10652. }
  10653. }
  10654. /*********************************************************************
  10655. * compute energies
  10656. *********************************************************************/
  10657. fftenergy[0] = NON_LINEAR_SCALE_ENERGY(wsamp_l[wsamp_lPos + 0][0]);
  10658. fftenergy[0] *= fftenergy[0];
  10659. for (var j = Encoder.BLKSIZE / 2 - 1; j >= 0; --j) {
  10660. var re = wsamp_l[wsamp_lPos + 0][Encoder.BLKSIZE / 2 - j];
  10661. var im = wsamp_l[wsamp_lPos + 0][Encoder.BLKSIZE / 2 + j];
  10662. fftenergy[Encoder.BLKSIZE / 2 - j] = NON_LINEAR_SCALE_ENERGY((re
  10663. * re + im * im) * 0.5);
  10664. }
  10665. /* total energy */
  10666. {
  10667. var totalenergy = 0.0;
  10668. for (var j = 11; j < Encoder.HBLKSIZE; j++)
  10669. totalenergy += fftenergy[j];
  10670. gfc.tot_ener[chn] = totalenergy;
  10671. }
  10672. if (gfp.analysis) {
  10673. for (var j = 0; j < Encoder.HBLKSIZE; j++) {
  10674. gfc.pinfo.energy[gr_out][chn][j] = gfc.pinfo.energy_save[chn][j];
  10675. gfc.pinfo.energy_save[chn][j] = fftenergy[j];
  10676. }
  10677. gfc.pinfo.pe[gr_out][chn] = gfc.pe[chn];
  10678. }
  10679. }
  10680. function vbrpsy_compute_fft_s(gfp, buffer, bufPos, chn, sblock, fftenergy_s, wsamp_s, wsamp_sPos) {
  10681. var gfc = gfp.internal_flags;
  10682. if (sblock == 0 && chn < 2) {
  10683. fft.fft_short(gfc, wsamp_s[wsamp_sPos], chn, buffer, bufPos);
  10684. }
  10685. if (chn == 2) {
  10686. /* FFT data for mid and side channel is derived from L & R */
  10687. for (var j = Encoder.BLKSIZE_s - 1; j >= 0; --j) {
  10688. var l = wsamp_s[wsamp_sPos + 0][sblock][j];
  10689. var r = wsamp_s[wsamp_sPos + 1][sblock][j];
  10690. wsamp_s[wsamp_sPos + 0][sblock][j] = (l + r) * Util.SQRT2 * 0.5;
  10691. wsamp_s[wsamp_sPos + 1][sblock][j] = (l - r) * Util.SQRT2 * 0.5;
  10692. }
  10693. }
  10694. /*********************************************************************
  10695. * compute energies
  10696. *********************************************************************/
  10697. fftenergy_s[sblock][0] = wsamp_s[wsamp_sPos + 0][sblock][0];
  10698. fftenergy_s[sblock][0] *= fftenergy_s[sblock][0];
  10699. for (var j = Encoder.BLKSIZE_s / 2 - 1; j >= 0; --j) {
  10700. var re = wsamp_s[wsamp_sPos + 0][sblock][Encoder.BLKSIZE_s / 2 - j];
  10701. var im = wsamp_s[wsamp_sPos + 0][sblock][Encoder.BLKSIZE_s / 2 + j];
  10702. fftenergy_s[sblock][Encoder.BLKSIZE_s / 2 - j] = NON_LINEAR_SCALE_ENERGY((re
  10703. * re + im * im) * 0.5);
  10704. }
  10705. }
  10706. /**
  10707. * compute loudness approximation (used for ATH auto-level adjustment)
  10708. */
  10709. function vbrpsy_compute_loudness_approximation_l(gfp, gr_out, chn, fftenergy) {
  10710. var gfc = gfp.internal_flags;
  10711. if (gfp.athaa_loudapprox == 2 && chn < 2) {
  10712. // no loudness for mid/side ch
  10713. gfc.loudness_sq[gr_out][chn] = gfc.loudness_sq_save[chn];
  10714. gfc.loudness_sq_save[chn] = psycho_loudness_approx(fftenergy, gfc);
  10715. }
  10716. }
  10717. var fircoef_ = [-8.65163e-18 * 2,
  10718. -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,
  10719. -3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2,
  10720. 0.0931738 * 2, -5.52212e-17 * 2, -0.313819 * 2];
  10721. /**
  10722. * Apply HPF of fs/4 to the input signal. This is used for attack detection
  10723. * / handling.
  10724. */
  10725. function vbrpsy_attack_detection(gfp, buffer, bufPos, gr_out, masking_ratio, masking_MS_ratio, energy, sub_short_factor, ns_attacks, uselongblock) {
  10726. var ns_hpfsmpl = new_float_n([2, 576]);
  10727. var gfc = gfp.internal_flags;
  10728. var n_chn_out = gfc.channels_out;
  10729. /* chn=2 and 3 = Mid and Side channels */
  10730. var n_chn_psy = (gfp.mode == MPEGMode.JOINT_STEREO) ? 4 : n_chn_out;
  10731. /* Don't copy the input buffer into a temporary buffer */
  10732. /* unroll the loop 2 times */
  10733. for (var chn = 0; chn < n_chn_out; chn++) {
  10734. /* apply high pass filter of fs/4 */
  10735. firbuf = buffer[chn];
  10736. var firbufPos = bufPos + 576 - 350 - NSFIRLEN + 192;
  10737. for (var i = 0; i < 576; i++) {
  10738. var sum1, sum2;
  10739. sum1 = firbuf[firbufPos + i + 10];
  10740. sum2 = 0.0;
  10741. for (var j = 0; j < ((NSFIRLEN - 1) / 2) - 1; j += 2) {
  10742. sum1 += fircoef_[j]
  10743. * (firbuf[firbufPos + i + j] + firbuf[firbufPos + i
  10744. + NSFIRLEN - j]);
  10745. sum2 += fircoef_[j + 1]
  10746. * (firbuf[firbufPos + i + j + 1] + firbuf[firbufPos
  10747. + i + NSFIRLEN - j - 1]);
  10748. }
  10749. ns_hpfsmpl[chn][i] = sum1 + sum2;
  10750. }
  10751. masking_ratio[gr_out][chn].en.assign(gfc.en[chn]);
  10752. masking_ratio[gr_out][chn].thm.assign(gfc.thm[chn]);
  10753. if (n_chn_psy > 2) {
  10754. /* MS maskings */
  10755. /* percep_MS_entropy [chn-2] = gfc . pe [chn]; */
  10756. masking_MS_ratio[gr_out][chn].en.assign(gfc.en[chn + 2]);
  10757. masking_MS_ratio[gr_out][chn].thm.assign(gfc.thm[chn + 2]);
  10758. }
  10759. }
  10760. for (var chn = 0; chn < n_chn_psy; chn++) {
  10761. var attack_intensity = new_float(12);
  10762. var en_subshort = new_float(12);
  10763. var en_short = [0, 0, 0, 0];
  10764. var pf = ns_hpfsmpl[chn & 1];
  10765. var pfPos = 0;
  10766. var attackThreshold = (chn == 3) ? gfc.nsPsy.attackthre_s
  10767. : gfc.nsPsy.attackthre;
  10768. var ns_uselongblock = 1;
  10769. if (chn == 2) {
  10770. for (var i = 0, j = 576; j > 0; ++i, --j) {
  10771. var l = ns_hpfsmpl[0][i];
  10772. var r = ns_hpfsmpl[1][i];
  10773. ns_hpfsmpl[0][i] = l + r;
  10774. ns_hpfsmpl[1][i] = l - r;
  10775. }
  10776. }
  10777. /***************************************************************
  10778. * determine the block type (window type)
  10779. ***************************************************************/
  10780. /* calculate energies of each sub-shortblocks */
  10781. for (var i = 0; i < 3; i++) {
  10782. en_subshort[i] = gfc.nsPsy.last_en_subshort[chn][i + 6];
  10783. attack_intensity[i] = en_subshort[i]
  10784. / gfc.nsPsy.last_en_subshort[chn][i + 4];
  10785. en_short[0] += en_subshort[i];
  10786. }
  10787. for (var i = 0; i < 9; i++) {
  10788. var pfe = pfPos + 576 / 9;
  10789. var p = 1.;
  10790. for (; pfPos < pfe; pfPos++)
  10791. if (p < Math.abs(pf[pfPos]))
  10792. p = Math.abs(pf[pfPos]);
  10793. gfc.nsPsy.last_en_subshort[chn][i] = en_subshort[i + 3] = p;
  10794. en_short[1 + i / 3] += p;
  10795. if (p > en_subshort[i + 3 - 2]) {
  10796. p = p / en_subshort[i + 3 - 2];
  10797. } else if (en_subshort[i + 3 - 2] > p * 10.0) {
  10798. p = en_subshort[i + 3 - 2] / (p * 10.0);
  10799. } else {
  10800. p = 0.0;
  10801. }
  10802. attack_intensity[i + 3] = p;
  10803. }
  10804. /* pulse like signal detection for fatboy.wav and so on */
  10805. for (var i = 0; i < 3; ++i) {
  10806. var enn = en_subshort[i * 3 + 3]
  10807. + en_subshort[i * 3 + 4] + en_subshort[i * 3 + 5];
  10808. var factor = 1.;
  10809. if (en_subshort[i * 3 + 5] * 6 < enn) {
  10810. factor *= 0.5;
  10811. if (en_subshort[i * 3 + 4] * 6 < enn) {
  10812. factor *= 0.5;
  10813. }
  10814. }
  10815. sub_short_factor[chn][i] = factor;
  10816. }
  10817. if (gfp.analysis) {
  10818. var x = attack_intensity[0];
  10819. for (var i = 1; i < 12; i++) {
  10820. if (x < attack_intensity[i]) {
  10821. x = attack_intensity[i];
  10822. }
  10823. }
  10824. gfc.pinfo.ers[gr_out][chn] = gfc.pinfo.ers_save[chn];
  10825. gfc.pinfo.ers_save[chn] = x;
  10826. }
  10827. /* compare energies between sub-shortblocks */
  10828. for (var i = 0; i < 12; i++) {
  10829. if (0 == ns_attacks[chn][i / 3]
  10830. && attack_intensity[i] > attackThreshold) {
  10831. ns_attacks[chn][i / 3] = (i % 3) + 1;
  10832. }
  10833. }
  10834. /*
  10835. * should have energy change between short blocks, in order to avoid
  10836. * periodic signals
  10837. */
  10838. /* Good samples to show the effect are Trumpet test songs */
  10839. /*
  10840. * GB: tuned (1) to avoid too many short blocks for test sample
  10841. * TRUMPET
  10842. */
  10843. /*
  10844. * RH: tuned (2) to let enough short blocks through for test sample
  10845. * FSOL and SNAPS
  10846. */
  10847. for (var i = 1; i < 4; i++) {
  10848. var u = en_short[i - 1];
  10849. var v = en_short[i];
  10850. var m = Math.max(u, v);
  10851. if (m < 40000) { /* (2) */
  10852. if (u < 1.7 * v && v < 1.7 * u) { /* (1) */
  10853. if (i == 1 && ns_attacks[chn][0] <= ns_attacks[chn][i]) {
  10854. ns_attacks[chn][0] = 0;
  10855. }
  10856. ns_attacks[chn][i] = 0;
  10857. }
  10858. }
  10859. }
  10860. if (ns_attacks[chn][0] <= gfc.nsPsy.lastAttacks[chn]) {
  10861. ns_attacks[chn][0] = 0;
  10862. }
  10863. if (gfc.nsPsy.lastAttacks[chn] == 3
  10864. || (ns_attacks[chn][0] + ns_attacks[chn][1]
  10865. + ns_attacks[chn][2] + ns_attacks[chn][3]) != 0) {
  10866. ns_uselongblock = 0;
  10867. if (ns_attacks[chn][1] != 0 && ns_attacks[chn][0] != 0) {
  10868. ns_attacks[chn][1] = 0;
  10869. }
  10870. if (ns_attacks[chn][2] != 0 && ns_attacks[chn][1] != 0) {
  10871. ns_attacks[chn][2] = 0;
  10872. }
  10873. if (ns_attacks[chn][3] != 0 && ns_attacks[chn][2] != 0) {
  10874. ns_attacks[chn][3] = 0;
  10875. }
  10876. }
  10877. if (chn < 2) {
  10878. uselongblock[chn] = ns_uselongblock;
  10879. } else {
  10880. if (ns_uselongblock == 0) {
  10881. uselongblock[0] = uselongblock[1] = 0;
  10882. }
  10883. }
  10884. /*
  10885. * there is a one granule delay. Copy maskings computed last call
  10886. * into masking_ratio to return to calling program.
  10887. */
  10888. energy[chn] = gfc.tot_ener[chn];
  10889. }
  10890. }
  10891. function vbrpsy_skip_masking_s(gfc, chn, sblock) {
  10892. if (sblock == 0) {
  10893. for (var b = 0; b < gfc.npart_s; b++) {
  10894. gfc.nb_s2[chn][b] = gfc.nb_s1[chn][b];
  10895. gfc.nb_s1[chn][b] = 0;
  10896. }
  10897. }
  10898. }
  10899. function vbrpsy_skip_masking_l(gfc, chn) {
  10900. for (var b = 0; b < gfc.npart_l; b++) {
  10901. gfc.nb_2[chn][b] = gfc.nb_1[chn][b];
  10902. gfc.nb_1[chn][b] = 0;
  10903. }
  10904. }
  10905. function psyvbr_calc_mask_index_s(gfc, max, avg, mask_idx) {
  10906. var last_tab_entry = tab.length - 1;
  10907. var b = 0;
  10908. var a = avg[b] + avg[b + 1];
  10909. if (a > 0.0) {
  10910. var m = max[b];
  10911. if (m < max[b + 1])
  10912. m = max[b + 1];
  10913. a = 20.0 * (m * 2.0 - a)
  10914. / (a * (gfc.numlines_s[b] + gfc.numlines_s[b + 1] - 1));
  10915. var k = 0 | a;
  10916. if (k > last_tab_entry)
  10917. k = last_tab_entry;
  10918. mask_idx[b] = k;
  10919. } else {
  10920. mask_idx[b] = 0;
  10921. }
  10922. for (b = 1; b < gfc.npart_s - 1; b++) {
  10923. a = avg[b - 1] + avg[b] + avg[b + 1];
  10924. if (a > 0.0) {
  10925. var m = max[b - 1];
  10926. if (m < max[b])
  10927. m = max[b];
  10928. if (m < max[b + 1])
  10929. m = max[b + 1];
  10930. a = 20.0
  10931. * (m * 3.0 - a)
  10932. / (a * (gfc.numlines_s[b - 1] + gfc.numlines_s[b]
  10933. + gfc.numlines_s[b + 1] - 1));
  10934. var k = 0 | a;
  10935. if (k > last_tab_entry)
  10936. k = last_tab_entry;
  10937. mask_idx[b] = k;
  10938. } else {
  10939. mask_idx[b] = 0;
  10940. }
  10941. }
  10942. a = avg[b - 1] + avg[b];
  10943. if (a > 0.0) {
  10944. var m = max[b - 1];
  10945. if (m < max[b])
  10946. m = max[b];
  10947. a = 20.0 * (m * 2.0 - a)
  10948. / (a * (gfc.numlines_s[b - 1] + gfc.numlines_s[b] - 1));
  10949. var k = 0 | a;
  10950. if (k > last_tab_entry)
  10951. k = last_tab_entry;
  10952. mask_idx[b] = k;
  10953. } else {
  10954. mask_idx[b] = 0;
  10955. }
  10956. }
  10957. function vbrpsy_compute_masking_s(gfp, fftenergy_s, eb, thr, chn, sblock) {
  10958. var gfc = gfp.internal_flags;
  10959. var max = new float[Encoder.CBANDS], avg = new_float(Encoder.CBANDS);
  10960. var i, j, b;
  10961. var mask_idx_s = new int[Encoder.CBANDS];
  10962. for (b = j = 0; b < gfc.npart_s; ++b) {
  10963. var ebb = 0, m = 0;
  10964. var n = gfc.numlines_s[b];
  10965. for (i = 0; i < n; ++i, ++j) {
  10966. var el = fftenergy_s[sblock][j];
  10967. ebb += el;
  10968. if (m < el)
  10969. m = el;
  10970. }
  10971. eb[b] = ebb;
  10972. max[b] = m;
  10973. avg[b] = ebb / n;
  10974. }
  10975. for (; b < Encoder.CBANDS; ++b) {
  10976. max[b] = 0;
  10977. avg[b] = 0;
  10978. }
  10979. psyvbr_calc_mask_index_s(gfc, max, avg, mask_idx_s);
  10980. for (j = b = 0; b < gfc.npart_s; b++) {
  10981. var kk = gfc.s3ind_s[b][0];
  10982. var last = gfc.s3ind_s[b][1];
  10983. var dd, dd_n;
  10984. var x, ecb, avg_mask;
  10985. dd = mask_idx_s[kk];
  10986. dd_n = 1;
  10987. ecb = gfc.s3_ss[j] * eb[kk] * tab[mask_idx_s[kk]];
  10988. ++j;
  10989. ++kk;
  10990. while (kk <= last) {
  10991. dd += mask_idx_s[kk];
  10992. dd_n += 1;
  10993. x = gfc.s3_ss[j] * eb[kk] * tab[mask_idx_s[kk]];
  10994. ecb = vbrpsy_mask_add(ecb, x, kk - b);
  10995. ++j;
  10996. ++kk;
  10997. }
  10998. dd = (1 + 2 * dd) / (2 * dd_n);
  10999. avg_mask = tab[dd] * 0.5;
  11000. ecb *= avg_mask;
  11001. thr[b] = ecb;
  11002. gfc.nb_s2[chn][b] = gfc.nb_s1[chn][b];
  11003. gfc.nb_s1[chn][b] = ecb;
  11004. {
  11005. /*
  11006. * if THR exceeds EB, the quantization routines will take the
  11007. * difference from other bands. in case of strong tonal samples
  11008. * (tonaltest.wav) this leads to heavy distortions. that's why
  11009. * we limit THR here.
  11010. */
  11011. x = max[b];
  11012. x *= gfc.minval_s[b];
  11013. x *= avg_mask;
  11014. if (thr[b] > x) {
  11015. thr[b] = x;
  11016. }
  11017. }
  11018. if (gfc.masking_lower > 1) {
  11019. thr[b] *= gfc.masking_lower;
  11020. }
  11021. if (thr[b] > eb[b]) {
  11022. thr[b] = eb[b];
  11023. }
  11024. if (gfc.masking_lower < 1) {
  11025. thr[b] *= gfc.masking_lower;
  11026. }
  11027. }
  11028. for (; b < Encoder.CBANDS; ++b) {
  11029. eb[b] = 0;
  11030. thr[b] = 0;
  11031. }
  11032. }
  11033. function vbrpsy_compute_masking_l(gfc, fftenergy, eb_l, thr, chn) {
  11034. var max = new_float(Encoder.CBANDS), avg = new_float(Encoder.CBANDS);
  11035. var mask_idx_l = new_int(Encoder.CBANDS + 2);
  11036. var b;
  11037. /*********************************************************************
  11038. * Calculate the energy and the tonality of each partition.
  11039. *********************************************************************/
  11040. calc_energy(gfc, fftenergy, eb_l, max, avg);
  11041. calc_mask_index_l(gfc, max, avg, mask_idx_l);
  11042. /*********************************************************************
  11043. * convolve the partitioned energy and unpredictability with the
  11044. * spreading function, s3_l[b][k]
  11045. ********************************************************************/
  11046. var k = 0;
  11047. for (b = 0; b < gfc.npart_l; b++) {
  11048. var x, ecb, avg_mask, t;
  11049. /* convolve the partitioned energy with the spreading function */
  11050. var kk = gfc.s3ind[b][0];
  11051. var last = gfc.s3ind[b][1];
  11052. var dd = 0, dd_n = 0;
  11053. dd = mask_idx_l[kk];
  11054. dd_n += 1;
  11055. ecb = gfc.s3_ll[k] * eb_l[kk] * tab[mask_idx_l[kk]];
  11056. ++k;
  11057. ++kk;
  11058. while (kk <= last) {
  11059. dd += mask_idx_l[kk];
  11060. dd_n += 1;
  11061. x = gfc.s3_ll[k] * eb_l[kk] * tab[mask_idx_l[kk]];
  11062. t = vbrpsy_mask_add(ecb, x, kk - b);
  11063. ecb = t;
  11064. ++k;
  11065. ++kk;
  11066. }
  11067. dd = (1 + 2 * dd) / (2 * dd_n);
  11068. avg_mask = tab[dd] * 0.5;
  11069. ecb *= avg_mask;
  11070. /**** long block pre-echo control ****/
  11071. /**
  11072. * <PRE>
  11073. * dont use long block pre-echo control if previous granule was
  11074. * a short block. This is to avoid the situation:
  11075. * frame0: quiet (very low masking)
  11076. * frame1: surge (triggers short blocks)
  11077. * frame2: regular frame. looks like pre-echo when compared to
  11078. * frame0, but all pre-echo was in frame1.
  11079. * </PRE>
  11080. */
  11081. /*
  11082. * chn=0,1 L and R channels chn=2,3 S and M channels.
  11083. */
  11084. if (gfc.blocktype_old[chn & 0x01] == Encoder.SHORT_TYPE) {
  11085. var ecb_limit = rpelev * gfc.nb_1[chn][b];
  11086. if (ecb_limit > 0) {
  11087. thr[b] = Math.min(ecb, ecb_limit);
  11088. } else {
  11089. /**
  11090. * <PRE>
  11091. * Robert 071209:
  11092. * Because we don't calculate long block psy when we know a granule
  11093. * should be of short blocks, we don't have any clue how the granule
  11094. * before would have looked like as a long block. So we have to guess
  11095. * a little bit for this END_TYPE block.
  11096. * Most of the time we get away with this sloppyness. (fingers crossed :)
  11097. * The speed increase is worth it.
  11098. * </PRE>
  11099. */
  11100. thr[b] = Math.min(ecb, eb_l[b] * NS_PREECHO_ATT2);
  11101. }
  11102. } else {
  11103. var ecb_limit_2 = rpelev2 * gfc.nb_2[chn][b];
  11104. var ecb_limit_1 = rpelev * gfc.nb_1[chn][b];
  11105. var ecb_limit;
  11106. if (ecb_limit_2 <= 0) {
  11107. ecb_limit_2 = ecb;
  11108. }
  11109. if (ecb_limit_1 <= 0) {
  11110. ecb_limit_1 = ecb;
  11111. }
  11112. if (gfc.blocktype_old[chn & 0x01] == Encoder.NORM_TYPE) {
  11113. ecb_limit = Math.min(ecb_limit_1, ecb_limit_2);
  11114. } else {
  11115. ecb_limit = ecb_limit_1;
  11116. }
  11117. thr[b] = Math.min(ecb, ecb_limit);
  11118. }
  11119. gfc.nb_2[chn][b] = gfc.nb_1[chn][b];
  11120. gfc.nb_1[chn][b] = ecb;
  11121. {
  11122. /*
  11123. * if THR exceeds EB, the quantization routines will take the
  11124. * difference from other bands. in case of strong tonal samples
  11125. * (tonaltest.wav) this leads to heavy distortions. that's why
  11126. * we limit THR here.
  11127. */
  11128. x = max[b];
  11129. x *= gfc.minval_l[b];
  11130. x *= avg_mask;
  11131. if (thr[b] > x) {
  11132. thr[b] = x;
  11133. }
  11134. }
  11135. if (gfc.masking_lower > 1) {
  11136. thr[b] *= gfc.masking_lower;
  11137. }
  11138. if (thr[b] > eb_l[b]) {
  11139. thr[b] = eb_l[b];
  11140. }
  11141. if (gfc.masking_lower < 1) {
  11142. thr[b] *= gfc.masking_lower;
  11143. }
  11144. }
  11145. for (; b < Encoder.CBANDS; ++b) {
  11146. eb_l[b] = 0;
  11147. thr[b] = 0;
  11148. }
  11149. }
  11150. function vbrpsy_compute_block_type(gfp, uselongblock) {
  11151. var gfc = gfp.internal_flags;
  11152. if (gfp.short_blocks == ShortBlock.short_block_coupled
  11153. /* force both channels to use the same block type */
  11154. /* this is necessary if the frame is to be encoded in ms_stereo. */
  11155. /* But even without ms_stereo, FhG does this */
  11156. && !(uselongblock[0] != 0 && uselongblock[1] != 0))
  11157. uselongblock[0] = uselongblock[1] = 0;
  11158. for (var chn = 0; chn < gfc.channels_out; chn++) {
  11159. /* disable short blocks */
  11160. if (gfp.short_blocks == ShortBlock.short_block_dispensed) {
  11161. uselongblock[chn] = 1;
  11162. }
  11163. if (gfp.short_blocks == ShortBlock.short_block_forced) {
  11164. uselongblock[chn] = 0;
  11165. }
  11166. }
  11167. }
  11168. function vbrpsy_apply_block_type(gfp, uselongblock, blocktype_d) {
  11169. var gfc = gfp.internal_flags;
  11170. /*
  11171. * update the blocktype of the previous granule, since it depends on
  11172. * what happend in this granule
  11173. */
  11174. for (var chn = 0; chn < gfc.channels_out; chn++) {
  11175. var blocktype = Encoder.NORM_TYPE;
  11176. /* disable short blocks */
  11177. if (uselongblock[chn] != 0) {
  11178. /* no attack : use long blocks */
  11179. if (gfc.blocktype_old[chn] == Encoder.SHORT_TYPE)
  11180. blocktype = Encoder.STOP_TYPE;
  11181. } else {
  11182. /* attack : use short blocks */
  11183. blocktype = Encoder.SHORT_TYPE;
  11184. if (gfc.blocktype_old[chn] == Encoder.NORM_TYPE) {
  11185. gfc.blocktype_old[chn] = Encoder.START_TYPE;
  11186. }
  11187. if (gfc.blocktype_old[chn] == Encoder.STOP_TYPE)
  11188. gfc.blocktype_old[chn] = Encoder.SHORT_TYPE;
  11189. }
  11190. blocktype_d[chn] = gfc.blocktype_old[chn];
  11191. // value returned to calling program
  11192. gfc.blocktype_old[chn] = blocktype;
  11193. // save for next call to l3psy_anal
  11194. }
  11195. }
  11196. /**
  11197. * compute M/S thresholds from Johnston & Ferreira 1992 ICASSP paper
  11198. */
  11199. function vbrpsy_compute_MS_thresholds(eb, thr, cb_mld, ath_cb, athadjust, msfix, n) {
  11200. var msfix2 = msfix * 2;
  11201. var athlower = msfix > 0 ? Math.pow(10, athadjust) : 1;
  11202. var rside, rmid;
  11203. for (var b = 0; b < n; ++b) {
  11204. var ebM = eb[2][b];
  11205. var ebS = eb[3][b];
  11206. var thmL = thr[0][b];
  11207. var thmR = thr[1][b];
  11208. var thmM = thr[2][b];
  11209. var thmS = thr[3][b];
  11210. /* use this fix if L & R masking differs by 2db or less */
  11211. if (thmL <= 1.58 * thmR && thmR <= 1.58 * thmL) {
  11212. var mld_m = cb_mld[b] * ebS;
  11213. var mld_s = cb_mld[b] * ebM;
  11214. rmid = Math.max(thmM, Math.min(thmS, mld_m));
  11215. rside = Math.max(thmS, Math.min(thmM, mld_s));
  11216. } else {
  11217. rmid = thmM;
  11218. rside = thmS;
  11219. }
  11220. if (msfix > 0) {
  11221. /***************************************************************/
  11222. /* Adjust M/S maskings if user set "msfix" */
  11223. /***************************************************************/
  11224. /* Naoki Shibata 2000 */
  11225. var thmLR, thmMS;
  11226. var ath = ath_cb[b] * athlower;
  11227. thmLR = Math.min(Math.max(thmL, ath), Math.max(thmR, ath));
  11228. thmM = Math.max(rmid, ath);
  11229. thmS = Math.max(rside, ath);
  11230. thmMS = thmM + thmS;
  11231. if (thmMS > 0 && (thmLR * msfix2) < thmMS) {
  11232. var f = thmLR * msfix2 / thmMS;
  11233. thmM *= f;
  11234. thmS *= f;
  11235. }
  11236. rmid = Math.min(thmM, rmid);
  11237. rside = Math.min(thmS, rside);
  11238. }
  11239. if (rmid > ebM) {
  11240. rmid = ebM;
  11241. }
  11242. if (rside > ebS) {
  11243. rside = ebS;
  11244. }
  11245. thr[2][b] = rmid;
  11246. thr[3][b] = rside;
  11247. }
  11248. }
  11249. this.L3psycho_anal_vbr = function (gfp, buffer, bufPos, gr_out, masking_ratio, masking_MS_ratio, percep_entropy, percep_MS_entropy, energy, blocktype_d) {
  11250. var gfc = gfp.internal_flags;
  11251. /* fft and energy calculation */
  11252. var wsamp_l;
  11253. var wsamp_s;
  11254. var fftenergy = new_float(Encoder.HBLKSIZE);
  11255. var fftenergy_s = new_float_n([3, Encoder.HBLKSIZE_s]);
  11256. var wsamp_L = new_float_n([2, Encoder.BLKSIZE]);
  11257. var wsamp_S = new_float_n([2, 3, Encoder.BLKSIZE_s]);
  11258. var eb = new_float_n([4, Encoder.CBANDS]), thr = new_float_n([4, Encoder.CBANDS]);
  11259. var sub_short_factor = new_float_n([4, 3]);
  11260. var pcfact = 0.6;
  11261. /* block type */
  11262. var ns_attacks = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0],
  11263. [0, 0, 0, 0]];
  11264. var uselongblock = new_int(2);
  11265. /* usual variables like loop indices, etc.. */
  11266. /* chn=2 and 3 = Mid and Side channels */
  11267. var n_chn_psy = (gfp.mode == MPEGMode.JOINT_STEREO) ? 4
  11268. : gfc.channels_out;
  11269. vbrpsy_attack_detection(gfp, buffer, bufPos, gr_out, masking_ratio,
  11270. masking_MS_ratio, energy, sub_short_factor, ns_attacks,
  11271. uselongblock);
  11272. vbrpsy_compute_block_type(gfp, uselongblock);
  11273. /* LONG BLOCK CASE */
  11274. {
  11275. for (var chn = 0; chn < n_chn_psy; chn++) {
  11276. var ch01 = chn & 0x01;
  11277. wsamp_l = wsamp_L;
  11278. vbrpsy_compute_fft_l(gfp, buffer, bufPos, chn, gr_out,
  11279. fftenergy, wsamp_l, ch01);
  11280. vbrpsy_compute_loudness_approximation_l(gfp, gr_out, chn,
  11281. fftenergy);
  11282. if (uselongblock[ch01] != 0) {
  11283. vbrpsy_compute_masking_l(gfc, fftenergy, eb[chn], thr[chn],
  11284. chn);
  11285. } else {
  11286. vbrpsy_skip_masking_l(gfc, chn);
  11287. }
  11288. }
  11289. if ((uselongblock[0] + uselongblock[1]) == 2) {
  11290. /* M/S channel */
  11291. if (gfp.mode == MPEGMode.JOINT_STEREO) {
  11292. vbrpsy_compute_MS_thresholds(eb, thr, gfc.mld_cb_l,
  11293. gfc.ATH.cb_l, gfp.ATHlower * gfc.ATH.adjust,
  11294. gfp.msfix, gfc.npart_l);
  11295. }
  11296. }
  11297. /* TODO: apply adaptive ATH masking here ?? */
  11298. for (var chn = 0; chn < n_chn_psy; chn++) {
  11299. var ch01 = chn & 0x01;
  11300. if (uselongblock[ch01] != 0) {
  11301. convert_partition2scalefac_l(gfc, eb[chn], thr[chn], chn);
  11302. }
  11303. }
  11304. }
  11305. /* SHORT BLOCKS CASE */
  11306. {
  11307. for (var sblock = 0; sblock < 3; sblock++) {
  11308. for (var chn = 0; chn < n_chn_psy; ++chn) {
  11309. var ch01 = chn & 0x01;
  11310. if (uselongblock[ch01] != 0) {
  11311. vbrpsy_skip_masking_s(gfc, chn, sblock);
  11312. } else {
  11313. /* compute masking thresholds for short blocks */
  11314. wsamp_s = wsamp_S;
  11315. vbrpsy_compute_fft_s(gfp, buffer, bufPos, chn, sblock,
  11316. fftenergy_s, wsamp_s, ch01);
  11317. vbrpsy_compute_masking_s(gfp, fftenergy_s, eb[chn],
  11318. thr[chn], chn, sblock);
  11319. }
  11320. }
  11321. if ((uselongblock[0] + uselongblock[1]) == 0) {
  11322. /* M/S channel */
  11323. if (gfp.mode == MPEGMode.JOINT_STEREO) {
  11324. vbrpsy_compute_MS_thresholds(eb, thr, gfc.mld_cb_s,
  11325. gfc.ATH.cb_s, gfp.ATHlower * gfc.ATH.adjust,
  11326. gfp.msfix, gfc.npart_s);
  11327. }
  11328. /* L/R channel */
  11329. }
  11330. /* TODO: apply adaptive ATH masking here ?? */
  11331. for (var chn = 0; chn < n_chn_psy; ++chn) {
  11332. var ch01 = chn & 0x01;
  11333. if (0 == uselongblock[ch01]) {
  11334. convert_partition2scalefac_s(gfc, eb[chn], thr[chn],
  11335. chn, sblock);
  11336. }
  11337. }
  11338. }
  11339. /**** short block pre-echo control ****/
  11340. for (var chn = 0; chn < n_chn_psy; chn++) {
  11341. var ch01 = chn & 0x01;
  11342. if (uselongblock[ch01] != 0) {
  11343. continue;
  11344. }
  11345. for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {
  11346. var new_thmm = new_float(3);
  11347. for (var sblock = 0; sblock < 3; sblock++) {
  11348. var thmm = gfc.thm[chn].s[sb][sblock];
  11349. thmm *= NS_PREECHO_ATT0;
  11350. if (ns_attacks[chn][sblock] >= 2
  11351. || ns_attacks[chn][sblock + 1] == 1) {
  11352. var idx = (sblock != 0) ? sblock - 1 : 2;
  11353. var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,
  11354. NS_PREECHO_ATT1 * pcfact);
  11355. thmm = Math.min(thmm, p);
  11356. } else if (ns_attacks[chn][sblock] == 1) {
  11357. var idx = (sblock != 0) ? sblock - 1 : 2;
  11358. var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,
  11359. NS_PREECHO_ATT2 * pcfact);
  11360. thmm = Math.min(thmm, p);
  11361. } else if ((sblock != 0 && ns_attacks[chn][sblock - 1] == 3)
  11362. || (sblock == 0 && gfc.nsPsy.lastAttacks[chn] == 3)) {
  11363. var idx = (sblock != 2) ? sblock + 1 : 0;
  11364. var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,
  11365. NS_PREECHO_ATT2 * pcfact);
  11366. thmm = Math.min(thmm, p);
  11367. }
  11368. /* pulse like signal detection for fatboy.wav and so on */
  11369. thmm *= sub_short_factor[chn][sblock];
  11370. new_thmm[sblock] = thmm;
  11371. }
  11372. for (var sblock = 0; sblock < 3; sblock++) {
  11373. gfc.thm[chn].s[sb][sblock] = new_thmm[sblock];
  11374. }
  11375. }
  11376. }
  11377. }
  11378. for (var chn = 0; chn < n_chn_psy; chn++) {
  11379. gfc.nsPsy.lastAttacks[chn] = ns_attacks[chn][2];
  11380. }
  11381. /***************************************************************
  11382. * determine final block type
  11383. ***************************************************************/
  11384. vbrpsy_apply_block_type(gfp, uselongblock, blocktype_d);
  11385. /*********************************************************************
  11386. * compute the value of PE to return ... no delay and advance
  11387. *********************************************************************/
  11388. for (var chn = 0; chn < n_chn_psy; chn++) {
  11389. var ppe;
  11390. var ppePos;
  11391. var type;
  11392. var mr;
  11393. if (chn > 1) {
  11394. ppe = percep_MS_entropy;
  11395. ppePos = -2;
  11396. type = Encoder.NORM_TYPE;
  11397. if (blocktype_d[0] == Encoder.SHORT_TYPE
  11398. || blocktype_d[1] == Encoder.SHORT_TYPE)
  11399. type = Encoder.SHORT_TYPE;
  11400. mr = masking_MS_ratio[gr_out][chn - 2];
  11401. } else {
  11402. ppe = percep_entropy;
  11403. ppePos = 0;
  11404. type = blocktype_d[chn];
  11405. mr = masking_ratio[gr_out][chn];
  11406. }
  11407. if (type == Encoder.SHORT_TYPE) {
  11408. ppe[ppePos + chn] = pecalc_s(mr, gfc.masking_lower);
  11409. } else {
  11410. ppe[ppePos + chn] = pecalc_l(mr, gfc.masking_lower);
  11411. }
  11412. if (gfp.analysis) {
  11413. gfc.pinfo.pe[gr_out][chn] = ppe[ppePos + chn];
  11414. }
  11415. }
  11416. return 0;
  11417. }
  11418. function s3_func_x(bark, hf_slope) {
  11419. var tempx = bark, tempy;
  11420. if (tempx >= 0) {
  11421. tempy = -tempx * 27;
  11422. } else {
  11423. tempy = tempx * hf_slope;
  11424. }
  11425. if (tempy <= -72.0) {
  11426. return 0;
  11427. }
  11428. return Math.exp(tempy * LN_TO_LOG10);
  11429. }
  11430. function norm_s3_func_x(hf_slope) {
  11431. var lim_a = 0, lim_b = 0;
  11432. {
  11433. var x = 0, l, h;
  11434. for (x = 0; s3_func_x(x, hf_slope) > 1e-20; x -= 1)
  11435. ;
  11436. l = x;
  11437. h = 0;
  11438. while (Math.abs(h - l) > 1e-12) {
  11439. x = (h + l) / 2;
  11440. if (s3_func_x(x, hf_slope) > 0) {
  11441. h = x;
  11442. } else {
  11443. l = x;
  11444. }
  11445. }
  11446. lim_a = l;
  11447. }
  11448. {
  11449. var x = 0, l, h;
  11450. for (x = 0; s3_func_x(x, hf_slope) > 1e-20; x += 1)
  11451. ;
  11452. l = 0;
  11453. h = x;
  11454. while (Math.abs(h - l) > 1e-12) {
  11455. x = (h + l) / 2;
  11456. if (s3_func_x(x, hf_slope) > 0) {
  11457. l = x;
  11458. } else {
  11459. h = x;
  11460. }
  11461. }
  11462. lim_b = h;
  11463. }
  11464. {
  11465. var sum = 0;
  11466. var m = 1000;
  11467. var i;
  11468. for (i = 0; i <= m; ++i) {
  11469. var x = lim_a + i * (lim_b - lim_a) / m;
  11470. var y = s3_func_x(x, hf_slope);
  11471. sum += y;
  11472. }
  11473. {
  11474. var norm = (m + 1) / (sum * (lim_b - lim_a));
  11475. /* printf( "norm = %lf\n",norm); */
  11476. return norm;
  11477. }
  11478. }
  11479. }
  11480. /**
  11481. * The spreading function. Values returned in units of energy
  11482. */
  11483. function s3_func(bark) {
  11484. var tempx, x, tempy, temp;
  11485. tempx = bark;
  11486. if (tempx >= 0)
  11487. tempx *= 3;
  11488. else
  11489. tempx *= 1.5;
  11490. if (tempx >= 0.5 && tempx <= 2.5) {
  11491. temp = tempx - 0.5;
  11492. x = 8.0 * (temp * temp - 2.0 * temp);
  11493. } else
  11494. x = 0.0;
  11495. tempx += 0.474;
  11496. tempy = 15.811389 + 7.5 * tempx - 17.5
  11497. * Math.sqrt(1.0 + tempx * tempx);
  11498. if (tempy <= -60.0)
  11499. return 0.0;
  11500. tempx = Math.exp((x + tempy) * LN_TO_LOG10);
  11501. /**
  11502. * <PRE>
  11503. * Normalization. The spreading function should be normalized so that:
  11504. * +inf
  11505. * /
  11506. * | s3 [ bark ] d(bark) = 1
  11507. * /
  11508. * -inf
  11509. * </PRE>
  11510. */
  11511. tempx /= .6609193;
  11512. return tempx;
  11513. }
  11514. /**
  11515. * see for example "Zwicker: Psychoakustik, 1982; ISBN 3-540-11401-7
  11516. */
  11517. function freq2bark(freq) {
  11518. /* input: freq in hz output: barks */
  11519. if (freq < 0)
  11520. freq = 0;
  11521. freq = freq * 0.001;
  11522. return 13.0 * Math.atan(.76 * freq) + 3.5
  11523. * Math.atan(freq * freq / (7.5 * 7.5));
  11524. }
  11525. function init_numline(numlines, bo, bm, bval, bval_width, mld, bo_w, sfreq, blksize, scalepos, deltafreq, sbmax) {
  11526. var b_frq = new_float(Encoder.CBANDS + 1);
  11527. var sample_freq_frac = sfreq / (sbmax > 15 ? 2 * 576 : 2 * 192);
  11528. var partition = new_int(Encoder.HBLKSIZE);
  11529. var i;
  11530. sfreq /= blksize;
  11531. var j = 0;
  11532. var ni = 0;
  11533. /* compute numlines, the number of spectral lines in each partition band */
  11534. /* each partition band should be about DELBARK wide. */
  11535. for (i = 0; i < Encoder.CBANDS; i++) {
  11536. var bark1;
  11537. var j2;
  11538. bark1 = freq2bark(sfreq * j);
  11539. b_frq[i] = sfreq * j;
  11540. for (j2 = j; freq2bark(sfreq * j2) - bark1 < DELBARK
  11541. && j2 <= blksize / 2; j2++)
  11542. ;
  11543. numlines[i] = j2 - j;
  11544. ni = i + 1;
  11545. while (j < j2) {
  11546. partition[j++] = i;
  11547. }
  11548. if (j > blksize / 2) {
  11549. j = blksize / 2;
  11550. ++i;
  11551. break;
  11552. }
  11553. }
  11554. b_frq[i] = sfreq * j;
  11555. for (var sfb = 0; sfb < sbmax; sfb++) {
  11556. var i1, i2, start, end;
  11557. var arg;
  11558. start = scalepos[sfb];
  11559. end = scalepos[sfb + 1];
  11560. i1 = 0 | Math.floor(.5 + deltafreq * (start - .5));
  11561. if (i1 < 0)
  11562. i1 = 0;
  11563. i2 = 0 | Math.floor(.5 + deltafreq * (end - .5));
  11564. if (i2 > blksize / 2)
  11565. i2 = blksize / 2;
  11566. bm[sfb] = (partition[i1] + partition[i2]) / 2;
  11567. bo[sfb] = partition[i2];
  11568. var f_tmp = sample_freq_frac * end;
  11569. /*
  11570. * calculate how much of this band belongs to current scalefactor
  11571. * band
  11572. */
  11573. bo_w[sfb] = (f_tmp - b_frq[bo[sfb]])
  11574. / (b_frq[bo[sfb] + 1] - b_frq[bo[sfb]]);
  11575. if (bo_w[sfb] < 0) {
  11576. bo_w[sfb] = 0;
  11577. } else {
  11578. if (bo_w[sfb] > 1) {
  11579. bo_w[sfb] = 1;
  11580. }
  11581. }
  11582. /* setup stereo demasking thresholds */
  11583. /* formula reverse enginerred from plot in paper */
  11584. arg = freq2bark(sfreq * scalepos[sfb] * deltafreq);
  11585. arg = ( Math.min(arg, 15.5) / 15.5);
  11586. mld[sfb] = Math.pow(10.0,
  11587. 1.25 * (1 - Math.cos(Math.PI * arg)) - 2.5);
  11588. }
  11589. /* compute bark values of each critical band */
  11590. j = 0;
  11591. for (var k = 0; k < ni; k++) {
  11592. var w = numlines[k];
  11593. var bark1, bark2;
  11594. bark1 = freq2bark(sfreq * (j));
  11595. bark2 = freq2bark(sfreq * (j + w - 1));
  11596. bval[k] = .5 * (bark1 + bark2);
  11597. bark1 = freq2bark(sfreq * (j - .5));
  11598. bark2 = freq2bark(sfreq * (j + w - .5));
  11599. bval_width[k] = bark2 - bark1;
  11600. j += w;
  11601. }
  11602. return ni;
  11603. }
  11604. function init_s3_values(s3ind, npart, bval, bval_width, norm, use_old_s3) {
  11605. var s3 = new_float_n([Encoder.CBANDS, Encoder.CBANDS]);
  11606. /*
  11607. * The s3 array is not linear in the bark scale.
  11608. *
  11609. * bval[x] should be used to get the bark value.
  11610. */
  11611. var j;
  11612. var numberOfNoneZero = 0;
  11613. /**
  11614. * <PRE>
  11615. * s[i][j], the value of the spreading function,
  11616. * centered at band j (masker), for band i (maskee)
  11617. *
  11618. * i.e.: sum over j to spread into signal barkval=i
  11619. * NOTE: i and j are used opposite as in the ISO docs
  11620. * </PRE>
  11621. */
  11622. if (use_old_s3) {
  11623. for (var i = 0; i < npart; i++) {
  11624. for (j = 0; j < npart; j++) {
  11625. var v = s3_func(bval[i] - bval[j]) * bval_width[j];
  11626. s3[i][j] = v * norm[i];
  11627. }
  11628. }
  11629. } else {
  11630. for (j = 0; j < npart; j++) {
  11631. var hf_slope = 15 + Math.min(21 / bval[j], 12);
  11632. var s3_x_norm = norm_s3_func_x(hf_slope);
  11633. for (var i = 0; i < npart; i++) {
  11634. var v = s3_x_norm
  11635. * s3_func_x(bval[i] - bval[j], hf_slope)
  11636. * bval_width[j];
  11637. s3[i][j] = v * norm[i];
  11638. }
  11639. }
  11640. }
  11641. for (var i = 0; i < npart; i++) {
  11642. for (j = 0; j < npart; j++) {
  11643. if (s3[i][j] > 0.0)
  11644. break;
  11645. }
  11646. s3ind[i][0] = j;
  11647. for (j = npart - 1; j > 0; j--) {
  11648. if (s3[i][j] > 0.0)
  11649. break;
  11650. }
  11651. s3ind[i][1] = j;
  11652. numberOfNoneZero += (s3ind[i][1] - s3ind[i][0] + 1);
  11653. }
  11654. var p = new_float(numberOfNoneZero);
  11655. var k = 0;
  11656. for (var i = 0; i < npart; i++)
  11657. for (j = s3ind[i][0]; j <= s3ind[i][1]; j++)
  11658. p[k++] = s3[i][j];
  11659. return p;
  11660. }
  11661. function stereo_demask(f) {
  11662. /* setup stereo demasking thresholds */
  11663. /* formula reverse enginerred from plot in paper */
  11664. var arg = freq2bark(f);
  11665. arg = (Math.min(arg, 15.5) / 15.5);
  11666. return Math.pow(10.0,
  11667. 1.25 * (1 - Math.cos(Math.PI * arg)) - 2.5);
  11668. }
  11669. /**
  11670. * NOTE: the bitrate reduction from the inter-channel masking effect is low
  11671. * compared to the chance of getting annyoing artefacts. L3psycho_anal_vbr
  11672. * does not use this feature. (Robert 071216)
  11673. */
  11674. this.psymodel_init = function (gfp) {
  11675. var gfc = gfp.internal_flags;
  11676. var i;
  11677. var useOldS3 = true;
  11678. var bvl_a = 13, bvl_b = 24;
  11679. var snr_l_a = 0, snr_l_b = 0;
  11680. var snr_s_a = -8.25, snr_s_b = -4.5;
  11681. var bval = new_float(Encoder.CBANDS);
  11682. var bval_width = new_float(Encoder.CBANDS);
  11683. var norm = new_float(Encoder.CBANDS);
  11684. var sfreq = gfp.out_samplerate;
  11685. switch (gfp.experimentalZ) {
  11686. default:
  11687. case 0:
  11688. useOldS3 = true;
  11689. break;
  11690. case 1:
  11691. useOldS3 = (gfp.VBR == VbrMode.vbr_mtrh || gfp.VBR == VbrMode.vbr_mt) ? false
  11692. : true;
  11693. break;
  11694. case 2:
  11695. useOldS3 = false;
  11696. break;
  11697. case 3:
  11698. bvl_a = 8;
  11699. snr_l_a = -1.75;
  11700. snr_l_b = -0.0125;
  11701. snr_s_a = -8.25;
  11702. snr_s_b = -2.25;
  11703. break;
  11704. }
  11705. gfc.ms_ener_ratio_old = .25;
  11706. gfc.blocktype_old[0] = gfc.blocktype_old[1] = Encoder.NORM_TYPE;
  11707. // the vbr header is long blocks
  11708. for (i = 0; i < 4; ++i) {
  11709. for (var j = 0; j < Encoder.CBANDS; ++j) {
  11710. gfc.nb_1[i][j] = 1e20;
  11711. gfc.nb_2[i][j] = 1e20;
  11712. gfc.nb_s1[i][j] = gfc.nb_s2[i][j] = 1.0;
  11713. }
  11714. for (var sb = 0; sb < Encoder.SBMAX_l; sb++) {
  11715. gfc.en[i].l[sb] = 1e20;
  11716. gfc.thm[i].l[sb] = 1e20;
  11717. }
  11718. for (var j = 0; j < 3; ++j) {
  11719. for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {
  11720. gfc.en[i].s[sb][j] = 1e20;
  11721. gfc.thm[i].s[sb][j] = 1e20;
  11722. }
  11723. gfc.nsPsy.lastAttacks[i] = 0;
  11724. }
  11725. for (var j = 0; j < 9; j++)
  11726. gfc.nsPsy.last_en_subshort[i][j] = 10.;
  11727. }
  11728. /* init. for loudness approx. -jd 2001 mar 27 */
  11729. gfc.loudness_sq_save[0] = gfc.loudness_sq_save[1] = 0.0;
  11730. /*************************************************************************
  11731. * now compute the psychoacoustic model specific constants
  11732. ************************************************************************/
  11733. /* compute numlines, bo, bm, bval, bval_width, mld */
  11734. gfc.npart_l = init_numline(gfc.numlines_l, gfc.bo_l, gfc.bm_l, bval,
  11735. bval_width, gfc.mld_l, gfc.PSY.bo_l_weight, sfreq,
  11736. Encoder.BLKSIZE, gfc.scalefac_band.l, Encoder.BLKSIZE
  11737. / (2.0 * 576), Encoder.SBMAX_l);
  11738. /* compute the spreading function */
  11739. for (i = 0; i < gfc.npart_l; i++) {
  11740. var snr = snr_l_a;
  11741. if (bval[i] >= bvl_a) {
  11742. snr = snr_l_b * (bval[i] - bvl_a) / (bvl_b - bvl_a) + snr_l_a
  11743. * (bvl_b - bval[i]) / (bvl_b - bvl_a);
  11744. }
  11745. norm[i] = Math.pow(10.0, snr / 10.0);
  11746. if (gfc.numlines_l[i] > 0) {
  11747. gfc.rnumlines_l[i] = 1.0 / gfc.numlines_l[i];
  11748. } else {
  11749. gfc.rnumlines_l[i] = 0;
  11750. }
  11751. }
  11752. gfc.s3_ll = init_s3_values(gfc.s3ind, gfc.npart_l, bval, bval_width,
  11753. norm, useOldS3);
  11754. /* compute long block specific values, ATH and MINVAL */
  11755. var j = 0;
  11756. for (i = 0; i < gfc.npart_l; i++) {
  11757. var x;
  11758. /* ATH */
  11759. x = Float.MAX_VALUE;
  11760. for (var k = 0; k < gfc.numlines_l[i]; k++, j++) {
  11761. var freq = sfreq * j / (1000.0 * Encoder.BLKSIZE);
  11762. var level;
  11763. /*
  11764. * ATH below 100 Hz constant, not further climbing
  11765. */
  11766. level = this.ATHformula(freq * 1000, gfp) - 20;
  11767. // scale to FFT units; returned value is in dB
  11768. level = Math.pow(10., 0.1 * level);
  11769. // convert from dB . energy
  11770. level *= gfc.numlines_l[i];
  11771. if (x > level)
  11772. x = level;
  11773. }
  11774. gfc.ATH.cb_l[i] = x;
  11775. /*
  11776. * MINVAL. For low freq, the strength of the masking is limited by
  11777. * minval this is an ISO MPEG1 thing, dont know if it is really
  11778. * needed
  11779. */
  11780. /*
  11781. * FIXME: it does work to reduce low-freq problems in S53-Wind-Sax
  11782. * and lead-voice samples, but introduces some 3 kbps bit bloat too.
  11783. * TODO: Further refinement of the shape of this hack.
  11784. */
  11785. x = -20 + bval[i] * 20 / 10;
  11786. if (x > 6) {
  11787. x = 100;
  11788. }
  11789. if (x < -15) {
  11790. x = -15;
  11791. }
  11792. x -= 8.;
  11793. gfc.minval_l[i] = (Math.pow(10.0, x / 10.) * gfc.numlines_l[i]);
  11794. }
  11795. /************************************************************************
  11796. * do the same things for short blocks
  11797. ************************************************************************/
  11798. gfc.npart_s = init_numline(gfc.numlines_s, gfc.bo_s, gfc.bm_s, bval,
  11799. bval_width, gfc.mld_s, gfc.PSY.bo_s_weight, sfreq,
  11800. Encoder.BLKSIZE_s, gfc.scalefac_band.s, Encoder.BLKSIZE_s
  11801. / (2.0 * 192), Encoder.SBMAX_s);
  11802. /* SNR formula. short block is normalized by SNR. is it still right ? */
  11803. j = 0;
  11804. for (i = 0; i < gfc.npart_s; i++) {
  11805. var x;
  11806. var snr = snr_s_a;
  11807. if (bval[i] >= bvl_a) {
  11808. snr = snr_s_b * (bval[i] - bvl_a) / (bvl_b - bvl_a) + snr_s_a
  11809. * (bvl_b - bval[i]) / (bvl_b - bvl_a);
  11810. }
  11811. norm[i] = Math.pow(10.0, snr / 10.0);
  11812. /* ATH */
  11813. x = Float.MAX_VALUE;
  11814. for (var k = 0; k < gfc.numlines_s[i]; k++, j++) {
  11815. var freq = sfreq * j / (1000.0 * Encoder.BLKSIZE_s);
  11816. var level;
  11817. /* freq = Min(.1,freq); */
  11818. /*
  11819. * ATH below 100 Hz constant, not
  11820. * further climbing
  11821. */
  11822. level = this.ATHformula(freq * 1000, gfp) - 20;
  11823. // scale to FFT units; returned value is in dB
  11824. level = Math.pow(10., 0.1 * level);
  11825. // convert from dB . energy
  11826. level *= gfc.numlines_s[i];
  11827. if (x > level)
  11828. x = level;
  11829. }
  11830. gfc.ATH.cb_s[i] = x;
  11831. /*
  11832. * MINVAL. For low freq, the strength of the masking is limited by
  11833. * minval this is an ISO MPEG1 thing, dont know if it is really
  11834. * needed
  11835. */
  11836. x = (-7.0 + bval[i] * 7.0 / 12.0);
  11837. if (bval[i] > 12) {
  11838. x *= 1 + Math.log(1 + x) * 3.1;
  11839. }
  11840. if (bval[i] < 12) {
  11841. x *= 1 + Math.log(1 - x) * 2.3;
  11842. }
  11843. if (x < -15) {
  11844. x = -15;
  11845. }
  11846. x -= 8;
  11847. gfc.minval_s[i] = Math.pow(10.0, x / 10)
  11848. * gfc.numlines_s[i];
  11849. }
  11850. gfc.s3_ss = init_s3_values(gfc.s3ind_s, gfc.npart_s, bval, bval_width,
  11851. norm, useOldS3);
  11852. init_mask_add_max_values();
  11853. fft.init_fft(gfc);
  11854. /* setup temporal masking */
  11855. gfc.decay = Math.exp(-1.0 * LOG10
  11856. / (temporalmask_sustain_sec * sfreq / 192.0));
  11857. {
  11858. var msfix;
  11859. msfix = NS_MSFIX;
  11860. if ((gfp.exp_nspsytune & 2) != 0)
  11861. msfix = 1.0;
  11862. if (Math.abs(gfp.msfix) > 0.0)
  11863. msfix = gfp.msfix;
  11864. gfp.msfix = msfix;
  11865. /*
  11866. * spread only from npart_l bands. Normally, we use the spreading
  11867. * function to convolve from npart_l down to npart_l bands
  11868. */
  11869. for (var b = 0; b < gfc.npart_l; b++)
  11870. if (gfc.s3ind[b][1] > gfc.npart_l - 1)
  11871. gfc.s3ind[b][1] = gfc.npart_l - 1;
  11872. }
  11873. /*
  11874. * prepare for ATH auto adjustment: we want to decrease the ATH by 12 dB
  11875. * per second
  11876. */
  11877. var frame_duration = (576. * gfc.mode_gr / sfreq);
  11878. gfc.ATH.decay = Math.pow(10., -12. / 10. * frame_duration);
  11879. gfc.ATH.adjust = 0.01;
  11880. /* minimum, for leading low loudness */
  11881. gfc.ATH.adjustLimit = 1.0;
  11882. /* on lead, allow adjust up to maximum */
  11883. if (gfp.ATHtype != -1) {
  11884. /* compute equal loudness weights (eql_w) */
  11885. var freq;
  11886. var freq_inc = gfp.out_samplerate
  11887. / (Encoder.BLKSIZE);
  11888. var eql_balance = 0.0;
  11889. freq = 0.0;
  11890. for (i = 0; i < Encoder.BLKSIZE / 2; ++i) {
  11891. /* convert ATH dB to relative power (not dB) */
  11892. /* to determine eql_w */
  11893. freq += freq_inc;
  11894. gfc.ATH.eql_w[i] = 1. / Math.pow(10, this.ATHformula(freq, gfp) / 10);
  11895. eql_balance += gfc.ATH.eql_w[i];
  11896. }
  11897. eql_balance = 1.0 / eql_balance;
  11898. for (i = Encoder.BLKSIZE / 2; --i >= 0;) { /* scale weights */
  11899. gfc.ATH.eql_w[i] *= eql_balance;
  11900. }
  11901. }
  11902. {
  11903. for (var b = j = 0; b < gfc.npart_s; ++b) {
  11904. for (i = 0; i < gfc.numlines_s[b]; ++i) {
  11905. ++j;
  11906. }
  11907. }
  11908. for (var b = j = 0; b < gfc.npart_l; ++b) {
  11909. for (i = 0; i < gfc.numlines_l[b]; ++i) {
  11910. ++j;
  11911. }
  11912. }
  11913. }
  11914. j = 0;
  11915. for (i = 0; i < gfc.npart_l; i++) {
  11916. var freq = sfreq * (j + gfc.numlines_l[i] / 2) / (1.0 * Encoder.BLKSIZE);
  11917. gfc.mld_cb_l[i] = stereo_demask(freq);
  11918. j += gfc.numlines_l[i];
  11919. }
  11920. for (; i < Encoder.CBANDS; ++i) {
  11921. gfc.mld_cb_l[i] = 1;
  11922. }
  11923. j = 0;
  11924. for (i = 0; i < gfc.npart_s; i++) {
  11925. var freq = sfreq * (j + gfc.numlines_s[i] / 2) / (1.0 * Encoder.BLKSIZE_s);
  11926. gfc.mld_cb_s[i] = stereo_demask(freq);
  11927. j += gfc.numlines_s[i];
  11928. }
  11929. for (; i < Encoder.CBANDS; ++i) {
  11930. gfc.mld_cb_s[i] = 1;
  11931. }
  11932. return 0;
  11933. }
  11934. /**
  11935. * Those ATH formulas are returning their minimum value for input = -1
  11936. */
  11937. function ATHformula_GB(f, value) {
  11938. /**
  11939. * <PRE>
  11940. * from Painter & Spanias
  11941. * modified by Gabriel Bouvigne to better fit the reality
  11942. * ath = 3.640 * pow(f,-0.8)
  11943. * - 6.800 * exp(-0.6*pow(f-3.4,2.0))
  11944. * + 6.000 * exp(-0.15*pow(f-8.7,2.0))
  11945. * + 0.6* 0.001 * pow(f,4.0);
  11946. *
  11947. *
  11948. * In the past LAME was using the Painter &Spanias formula.
  11949. * But we had some recurrent problems with HF content.
  11950. * We measured real ATH values, and found the older formula
  11951. * to be inaccurate in the higher part. So we made this new
  11952. * formula and this solved most of HF problematic test cases.
  11953. * The tradeoff is that in VBR mode it increases a lot the
  11954. * bitrate.
  11955. * </PRE>
  11956. */
  11957. /*
  11958. * This curve can be adjusted according to the VBR scale: it adjusts
  11959. * from something close to Painter & Spanias on V9 up to Bouvigne's
  11960. * formula for V0. This way the VBR bitrate is more balanced according
  11961. * to the -V value.
  11962. */
  11963. // the following Hack allows to ask for the lowest value
  11964. if (f < -.3)
  11965. f = 3410;
  11966. // convert to khz
  11967. f /= 1000;
  11968. f = Math.max(0.1, f);
  11969. var ath = 3.640 * Math.pow(f, -0.8) - 6.800
  11970. * Math.exp(-0.6 * Math.pow(f - 3.4, 2.0)) + 6.000
  11971. * Math.exp(-0.15 * Math.pow(f - 8.7, 2.0))
  11972. + (0.6 + 0.04 * value) * 0.001 * Math.pow(f, 4.0);
  11973. return ath;
  11974. }
  11975. this.ATHformula = function (f, gfp) {
  11976. var ath;
  11977. switch (gfp.ATHtype) {
  11978. case 0:
  11979. ath = ATHformula_GB(f, 9);
  11980. break;
  11981. case 1:
  11982. // over sensitive, should probably be removed
  11983. ath = ATHformula_GB(f, -1);
  11984. break;
  11985. case 2:
  11986. ath = ATHformula_GB(f, 0);
  11987. break;
  11988. case 3:
  11989. // modification of GB formula by Roel
  11990. ath = ATHformula_GB(f, 1) + 6;
  11991. break;
  11992. case 4:
  11993. ath = ATHformula_GB(f, gfp.ATHcurve);
  11994. break;
  11995. default:
  11996. ath = ATHformula_GB(f, 0);
  11997. break;
  11998. }
  11999. return ath;
  12000. }
  12001. }
  12002. function Lame() {
  12003. var self = this;
  12004. var LAME_MAXALBUMART = (128 * 1024);
  12005. Lame.V9 = 410;
  12006. Lame.V8 = 420;
  12007. Lame.V7 = 430;
  12008. Lame.V6 = 440;
  12009. Lame.V5 = 450;
  12010. Lame.V4 = 460;
  12011. Lame.V3 = 470;
  12012. Lame.V2 = 480;
  12013. Lame.V1 = 490;
  12014. Lame.V0 = 500;
  12015. /* still there for compatibility */
  12016. Lame.R3MIX = 1000;
  12017. Lame.STANDARD = 1001;
  12018. Lame.EXTREME = 1002;
  12019. Lame.INSANE = 1003;
  12020. Lame.STANDARD_FAST = 1004;
  12021. Lame.EXTREME_FAST = 1005;
  12022. Lame.MEDIUM = 1006;
  12023. Lame.MEDIUM_FAST = 1007;
  12024. /**
  12025. * maximum size of mp3buffer needed if you encode at most 1152 samples for
  12026. * each call to lame_encode_buffer. see lame_encode_buffer() below
  12027. * (LAME_MAXMP3BUFFER is now obsolete)
  12028. */
  12029. var LAME_MAXMP3BUFFER = (16384 + LAME_MAXALBUMART);
  12030. Lame.LAME_MAXMP3BUFFER = LAME_MAXMP3BUFFER;
  12031. var ga;
  12032. var bs;
  12033. var p;
  12034. var qupvt;
  12035. var qu;
  12036. var psy = new PsyModel();
  12037. var vbr;
  12038. var ver;
  12039. var id3;
  12040. var mpglib;
  12041. this.enc = new Encoder();
  12042. this.setModules = function (_ga, _bs, _p, _qupvt, _qu, _vbr, _ver, _id3, _mpglib) {
  12043. ga = _ga;
  12044. bs = _bs;
  12045. p = _p;
  12046. qupvt = _qupvt;
  12047. qu = _qu;
  12048. vbr = _vbr;
  12049. ver = _ver;
  12050. id3 = _id3;
  12051. mpglib = _mpglib;
  12052. this.enc.setModules(bs, psy, qupvt, vbr);
  12053. }
  12054. /**
  12055. * PSY Model related stuff
  12056. */
  12057. function PSY() {
  12058. /**
  12059. * The dbQ stuff.
  12060. */
  12061. this.mask_adjust = 0.;
  12062. /**
  12063. * The dbQ stuff.
  12064. */
  12065. this.mask_adjust_short = 0.;
  12066. /* at transition from one scalefactor band to next */
  12067. /**
  12068. * Band weight long scalefactor bands.
  12069. */
  12070. this.bo_l_weight = new_float(Encoder.SBMAX_l);
  12071. /**
  12072. * Band weight short scalefactor bands.
  12073. */
  12074. this.bo_s_weight = new_float(Encoder.SBMAX_s);
  12075. }
  12076. function LowPassHighPass() {
  12077. this.lowerlimit = 0.;
  12078. }
  12079. function BandPass(bitrate, lPass) {
  12080. this.lowpass = lPass;
  12081. }
  12082. var LAME_ID = 0xFFF88E3B;
  12083. function lame_init_old(gfp) {
  12084. var gfc;
  12085. gfp.class_id = LAME_ID;
  12086. gfc = gfp.internal_flags = new LameInternalFlags();
  12087. /* Global flags. set defaults here for non-zero values */
  12088. /* see lame.h for description */
  12089. /*
  12090. * set integer values to -1 to mean that LAME will compute the best
  12091. * value, UNLESS the calling program as set it (and the value is no
  12092. * longer -1)
  12093. */
  12094. gfp.mode = MPEGMode.NOT_SET;
  12095. gfp.original = 1;
  12096. gfp.in_samplerate = 44100;
  12097. gfp.num_channels = 2;
  12098. gfp.num_samples = -1;
  12099. gfp.bWriteVbrTag = true;
  12100. gfp.quality = -1;
  12101. gfp.short_blocks = null;
  12102. gfc.subblock_gain = -1;
  12103. gfp.lowpassfreq = 0;
  12104. gfp.highpassfreq = 0;
  12105. gfp.lowpasswidth = -1;
  12106. gfp.highpasswidth = -1;
  12107. gfp.VBR = VbrMode.vbr_off;
  12108. gfp.VBR_q = 4;
  12109. gfp.ATHcurve = -1;
  12110. gfp.VBR_mean_bitrate_kbps = 128;
  12111. gfp.VBR_min_bitrate_kbps = 0;
  12112. gfp.VBR_max_bitrate_kbps = 0;
  12113. gfp.VBR_hard_min = 0;
  12114. gfc.VBR_min_bitrate = 1;
  12115. /* not 0 ????? */
  12116. gfc.VBR_max_bitrate = 13;
  12117. /* not 14 ????? */
  12118. gfp.quant_comp = -1;
  12119. gfp.quant_comp_short = -1;
  12120. gfp.msfix = -1;
  12121. gfc.resample_ratio = 1;
  12122. gfc.OldValue[0] = 180;
  12123. gfc.OldValue[1] = 180;
  12124. gfc.CurrentStep[0] = 4;
  12125. gfc.CurrentStep[1] = 4;
  12126. gfc.masking_lower = 1;
  12127. gfc.nsPsy.attackthre = -1;
  12128. gfc.nsPsy.attackthre_s = -1;
  12129. gfp.scale = -1;
  12130. gfp.athaa_type = -1;
  12131. gfp.ATHtype = -1;
  12132. /* default = -1 = set in lame_init_params */
  12133. gfp.athaa_loudapprox = -1;
  12134. /* 1 = flat loudness approx. (total energy) */
  12135. /* 2 = equal loudness curve */
  12136. gfp.athaa_sensitivity = 0.0;
  12137. /* no offset */
  12138. gfp.useTemporal = null;
  12139. gfp.interChRatio = -1;
  12140. /*
  12141. * The reason for int mf_samples_to_encode = ENCDELAY + POSTDELAY;
  12142. * ENCDELAY = internal encoder delay. And then we have to add
  12143. * POSTDELAY=288 because of the 50% MDCT overlap. A 576 MDCT granule
  12144. * decodes to 1152 samples. To synthesize the 576 samples centered under
  12145. * this granule we need the previous granule for the first 288 samples
  12146. * (no problem), and the next granule for the next 288 samples (not
  12147. * possible if this is last granule). So we need to pad with 288 samples
  12148. * to make sure we can encode the 576 samples we are interested in.
  12149. */
  12150. gfc.mf_samples_to_encode = Encoder.ENCDELAY + Encoder.POSTDELAY;
  12151. gfp.encoder_padding = 0;
  12152. gfc.mf_size = Encoder.ENCDELAY - Encoder.MDCTDELAY;
  12153. /*
  12154. * we pad input with this many 0's
  12155. */
  12156. gfp.findReplayGain = false;
  12157. gfp.decode_on_the_fly = false;
  12158. gfc.decode_on_the_fly = false;
  12159. gfc.findReplayGain = false;
  12160. gfc.findPeakSample = false;
  12161. gfc.RadioGain = 0;
  12162. gfc.AudiophileGain = 0;
  12163. gfc.noclipGainChange = 0;
  12164. gfc.noclipScale = -1.0;
  12165. gfp.preset = 0;
  12166. gfp.write_id3tag_automatic = true;
  12167. return 0;
  12168. }
  12169. this.lame_init = function () {
  12170. var gfp = new LameGlobalFlags();
  12171. var ret = lame_init_old(gfp);
  12172. if (ret != 0) {
  12173. return null;
  12174. }
  12175. gfp.lame_allocated_gfp = 1;
  12176. return gfp;
  12177. }
  12178. function filter_coef(x) {
  12179. if (x > 1.0)
  12180. return 0.0;
  12181. if (x <= 0.0)
  12182. return 1.0;
  12183. return Math.cos(Math.PI / 2 * x);
  12184. }
  12185. this.nearestBitrateFullIndex = function (bitrate) {
  12186. /* borrowed from DM abr presets */
  12187. var full_bitrate_table = [8, 16, 24, 32, 40, 48, 56, 64, 80,
  12188. 96, 112, 128, 160, 192, 224, 256, 320];
  12189. var lower_range = 0, lower_range_kbps = 0, upper_range = 0, upper_range_kbps = 0;
  12190. /* We assume specified bitrate will be 320kbps */
  12191. upper_range_kbps = full_bitrate_table[16];
  12192. upper_range = 16;
  12193. lower_range_kbps = full_bitrate_table[16];
  12194. lower_range = 16;
  12195. /*
  12196. * Determine which significant bitrates the value specified falls
  12197. * between, if loop ends without breaking then we were correct above
  12198. * that the value was 320
  12199. */
  12200. for (var b = 0; b < 16; b++) {
  12201. if ((Math.max(bitrate, full_bitrate_table[b + 1])) != bitrate) {
  12202. upper_range_kbps = full_bitrate_table[b + 1];
  12203. upper_range = b + 1;
  12204. lower_range_kbps = full_bitrate_table[b];
  12205. lower_range = (b);
  12206. break;
  12207. /* We found upper range */
  12208. }
  12209. }
  12210. /* Determine which range the value specified is closer to */
  12211. if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps)) {
  12212. return lower_range;
  12213. }
  12214. return upper_range;
  12215. }
  12216. function optimum_samplefreq(lowpassfreq, input_samplefreq) {
  12217. /*
  12218. * Rules:
  12219. *
  12220. * - if possible, sfb21 should NOT be used
  12221. */
  12222. var suggested_samplefreq = 44100;
  12223. if (input_samplefreq >= 48000)
  12224. suggested_samplefreq = 48000;
  12225. else if (input_samplefreq >= 44100)
  12226. suggested_samplefreq = 44100;
  12227. else if (input_samplefreq >= 32000)
  12228. suggested_samplefreq = 32000;
  12229. else if (input_samplefreq >= 24000)
  12230. suggested_samplefreq = 24000;
  12231. else if (input_samplefreq >= 22050)
  12232. suggested_samplefreq = 22050;
  12233. else if (input_samplefreq >= 16000)
  12234. suggested_samplefreq = 16000;
  12235. else if (input_samplefreq >= 12000)
  12236. suggested_samplefreq = 12000;
  12237. else if (input_samplefreq >= 11025)
  12238. suggested_samplefreq = 11025;
  12239. else if (input_samplefreq >= 8000)
  12240. suggested_samplefreq = 8000;
  12241. if (lowpassfreq == -1)
  12242. return suggested_samplefreq;
  12243. if (lowpassfreq <= 15960)
  12244. suggested_samplefreq = 44100;
  12245. if (lowpassfreq <= 15250)
  12246. suggested_samplefreq = 32000;
  12247. if (lowpassfreq <= 11220)
  12248. suggested_samplefreq = 24000;
  12249. if (lowpassfreq <= 9970)
  12250. suggested_samplefreq = 22050;
  12251. if (lowpassfreq <= 7230)
  12252. suggested_samplefreq = 16000;
  12253. if (lowpassfreq <= 5420)
  12254. suggested_samplefreq = 12000;
  12255. if (lowpassfreq <= 4510)
  12256. suggested_samplefreq = 11025;
  12257. if (lowpassfreq <= 3970)
  12258. suggested_samplefreq = 8000;
  12259. if (input_samplefreq < suggested_samplefreq) {
  12260. /*
  12261. * choose a valid MPEG sample frequency above the input sample
  12262. * frequency to avoid SFB21/12 bitrate bloat rh 061115
  12263. */
  12264. if (input_samplefreq > 44100) {
  12265. return 48000;
  12266. }
  12267. if (input_samplefreq > 32000) {
  12268. return 44100;
  12269. }
  12270. if (input_samplefreq > 24000) {
  12271. return 32000;
  12272. }
  12273. if (input_samplefreq > 22050) {
  12274. return 24000;
  12275. }
  12276. if (input_samplefreq > 16000) {
  12277. return 22050;
  12278. }
  12279. if (input_samplefreq > 12000) {
  12280. return 16000;
  12281. }
  12282. if (input_samplefreq > 11025) {
  12283. return 12000;
  12284. }
  12285. if (input_samplefreq > 8000) {
  12286. return 11025;
  12287. }
  12288. return 8000;
  12289. }
  12290. return suggested_samplefreq;
  12291. }
  12292. /**
  12293. * convert samp freq in Hz to index
  12294. */
  12295. function SmpFrqIndex(sample_freq, gpf) {
  12296. switch (sample_freq) {
  12297. case 44100:
  12298. gpf.version = 1;
  12299. return 0;
  12300. case 48000:
  12301. gpf.version = 1;
  12302. return 1;
  12303. case 32000:
  12304. gpf.version = 1;
  12305. return 2;
  12306. case 22050:
  12307. gpf.version = 0;
  12308. return 0;
  12309. case 24000:
  12310. gpf.version = 0;
  12311. return 1;
  12312. case 16000:
  12313. gpf.version = 0;
  12314. return 2;
  12315. case 11025:
  12316. gpf.version = 0;
  12317. return 0;
  12318. case 12000:
  12319. gpf.version = 0;
  12320. return 1;
  12321. case 8000:
  12322. gpf.version = 0;
  12323. return 2;
  12324. default:
  12325. gpf.version = 0;
  12326. return -1;
  12327. }
  12328. }
  12329. /**
  12330. * @param bRate
  12331. * legal rates from 8 to 320
  12332. */
  12333. function FindNearestBitrate(bRate, version, samplerate) {
  12334. /* MPEG-1 or MPEG-2 LSF */
  12335. if (samplerate < 16000)
  12336. version = 2;
  12337. var bitrate = Tables.bitrate_table[version][1];
  12338. for (var i = 2; i <= 14; i++) {
  12339. if (Tables.bitrate_table[version][i] > 0) {
  12340. if (Math.abs(Tables.bitrate_table[version][i] - bRate) < Math
  12341. .abs(bitrate - bRate))
  12342. bitrate = Tables.bitrate_table[version][i];
  12343. }
  12344. }
  12345. return bitrate;
  12346. }
  12347. /**
  12348. * @param bRate
  12349. * legal rates from 32 to 448 kbps
  12350. * @param version
  12351. * MPEG-1 or MPEG-2/2.5 LSF
  12352. */
  12353. function BitrateIndex(bRate, version, samplerate) {
  12354. /* convert bitrate in kbps to index */
  12355. if (samplerate < 16000)
  12356. version = 2;
  12357. for (var i = 0; i <= 14; i++) {
  12358. if (Tables.bitrate_table[version][i] > 0) {
  12359. if (Tables.bitrate_table[version][i] == bRate) {
  12360. return i;
  12361. }
  12362. }
  12363. }
  12364. return -1;
  12365. }
  12366. function optimum_bandwidth(lh, bitrate) {
  12367. /**
  12368. * <PRE>
  12369. * Input:
  12370. * bitrate total bitrate in kbps
  12371. *
  12372. * Output:
  12373. * lowerlimit: best lowpass frequency limit for input filter in Hz
  12374. * upperlimit: best highpass frequency limit for input filter in Hz
  12375. * </PRE>
  12376. */
  12377. var freq_map = [new BandPass(8, 2000),
  12378. new BandPass(16, 3700), new BandPass(24, 3900),
  12379. new BandPass(32, 5500), new BandPass(40, 7000),
  12380. new BandPass(48, 7500), new BandPass(56, 10000),
  12381. new BandPass(64, 11000), new BandPass(80, 13500),
  12382. new BandPass(96, 15100), new BandPass(112, 15600),
  12383. new BandPass(128, 17000), new BandPass(160, 17500),
  12384. new BandPass(192, 18600), new BandPass(224, 19400),
  12385. new BandPass(256, 19700), new BandPass(320, 20500)];
  12386. var table_index = self.nearestBitrateFullIndex(bitrate);
  12387. lh.lowerlimit = freq_map[table_index].lowpass;
  12388. }
  12389. function lame_init_params_ppflt(gfp) {
  12390. var gfc = gfp.internal_flags;
  12391. /***************************************************************/
  12392. /* compute info needed for polyphase filter (filter type==0, default) */
  12393. /***************************************************************/
  12394. var lowpass_band = 32;
  12395. var highpass_band = -1;
  12396. if (gfc.lowpass1 > 0) {
  12397. var minband = 999;
  12398. for (var band = 0; band <= 31; band++) {
  12399. var freq = (band / 31.0);
  12400. /* this band and above will be zeroed: */
  12401. if (freq >= gfc.lowpass2) {
  12402. lowpass_band = Math.min(lowpass_band, band);
  12403. }
  12404. if (gfc.lowpass1 < freq && freq < gfc.lowpass2) {
  12405. minband = Math.min(minband, band);
  12406. }
  12407. }
  12408. /*
  12409. * compute the *actual* transition band implemented by the polyphase
  12410. * filter
  12411. */
  12412. if (minband == 999) {
  12413. gfc.lowpass1 = (lowpass_band - .75) / 31.0;
  12414. } else {
  12415. gfc.lowpass1 = (minband - .75) / 31.0;
  12416. }
  12417. gfc.lowpass2 = lowpass_band / 31.0;
  12418. }
  12419. /*
  12420. * make sure highpass filter is within 90% of what the effective
  12421. * highpass frequency will be
  12422. */
  12423. if (gfc.highpass2 > 0) {
  12424. if (gfc.highpass2 < .9 * (.75 / 31.0)) {
  12425. gfc.highpass1 = 0;
  12426. gfc.highpass2 = 0;
  12427. System.err.println("Warning: highpass filter disabled. "
  12428. + "highpass frequency too small\n");
  12429. }
  12430. }
  12431. if (gfc.highpass2 > 0) {
  12432. var maxband = -1;
  12433. for (var band = 0; band <= 31; band++) {
  12434. var freq = band / 31.0;
  12435. /* this band and below will be zereod */
  12436. if (freq <= gfc.highpass1) {
  12437. highpass_band = Math.max(highpass_band, band);
  12438. }
  12439. if (gfc.highpass1 < freq && freq < gfc.highpass2) {
  12440. maxband = Math.max(maxband, band);
  12441. }
  12442. }
  12443. /*
  12444. * compute the *actual* transition band implemented by the polyphase
  12445. * filter
  12446. */
  12447. gfc.highpass1 = highpass_band / 31.0;
  12448. if (maxband == -1) {
  12449. gfc.highpass2 = (highpass_band + .75) / 31.0;
  12450. } else {
  12451. gfc.highpass2 = (maxband + .75) / 31.0;
  12452. }
  12453. }
  12454. for (var band = 0; band < 32; band++) {
  12455. var fc1, fc2;
  12456. var freq = band / 31.0;
  12457. if (gfc.highpass2 > gfc.highpass1) {
  12458. fc1 = filter_coef((gfc.highpass2 - freq)
  12459. / (gfc.highpass2 - gfc.highpass1 + 1e-20));
  12460. } else {
  12461. fc1 = 1.0;
  12462. }
  12463. if (gfc.lowpass2 > gfc.lowpass1) {
  12464. fc2 = filter_coef((freq - gfc.lowpass1)
  12465. / (gfc.lowpass2 - gfc.lowpass1 + 1e-20));
  12466. } else {
  12467. fc2 = 1.0;
  12468. }
  12469. gfc.amp_filter[band] = (fc1 * fc2);
  12470. }
  12471. }
  12472. function lame_init_qval(gfp) {
  12473. var gfc = gfp.internal_flags;
  12474. switch (gfp.quality) {
  12475. default:
  12476. case 9: /* no psymodel, no noise shaping */
  12477. gfc.psymodel = 0;
  12478. gfc.noise_shaping = 0;
  12479. gfc.noise_shaping_amp = 0;
  12480. gfc.noise_shaping_stop = 0;
  12481. gfc.use_best_huffman = 0;
  12482. gfc.full_outer_loop = 0;
  12483. break;
  12484. case 8:
  12485. gfp.quality = 7;
  12486. //$FALL-THROUGH$
  12487. case 7:
  12488. /*
  12489. * use psymodel (for short block and m/s switching), but no noise
  12490. * shapping
  12491. */
  12492. gfc.psymodel = 1;
  12493. gfc.noise_shaping = 0;
  12494. gfc.noise_shaping_amp = 0;
  12495. gfc.noise_shaping_stop = 0;
  12496. gfc.use_best_huffman = 0;
  12497. gfc.full_outer_loop = 0;
  12498. break;
  12499. case 6:
  12500. gfc.psymodel = 1;
  12501. if (gfc.noise_shaping == 0)
  12502. gfc.noise_shaping = 1;
  12503. gfc.noise_shaping_amp = 0;
  12504. gfc.noise_shaping_stop = 0;
  12505. if (gfc.subblock_gain == -1)
  12506. gfc.subblock_gain = 1;
  12507. gfc.use_best_huffman = 0;
  12508. gfc.full_outer_loop = 0;
  12509. break;
  12510. case 5:
  12511. gfc.psymodel = 1;
  12512. if (gfc.noise_shaping == 0)
  12513. gfc.noise_shaping = 1;
  12514. gfc.noise_shaping_amp = 0;
  12515. gfc.noise_shaping_stop = 0;
  12516. if (gfc.subblock_gain == -1)
  12517. gfc.subblock_gain = 1;
  12518. gfc.use_best_huffman = 0;
  12519. gfc.full_outer_loop = 0;
  12520. break;
  12521. case 4:
  12522. gfc.psymodel = 1;
  12523. if (gfc.noise_shaping == 0)
  12524. gfc.noise_shaping = 1;
  12525. gfc.noise_shaping_amp = 0;
  12526. gfc.noise_shaping_stop = 0;
  12527. if (gfc.subblock_gain == -1)
  12528. gfc.subblock_gain = 1;
  12529. gfc.use_best_huffman = 1;
  12530. gfc.full_outer_loop = 0;
  12531. break;
  12532. case 3:
  12533. gfc.psymodel = 1;
  12534. if (gfc.noise_shaping == 0)
  12535. gfc.noise_shaping = 1;
  12536. gfc.noise_shaping_amp = 1;
  12537. gfc.noise_shaping_stop = 1;
  12538. if (gfc.subblock_gain == -1)
  12539. gfc.subblock_gain = 1;
  12540. gfc.use_best_huffman = 1;
  12541. gfc.full_outer_loop = 0;
  12542. break;
  12543. case 2:
  12544. gfc.psymodel = 1;
  12545. if (gfc.noise_shaping == 0)
  12546. gfc.noise_shaping = 1;
  12547. if (gfc.substep_shaping == 0)
  12548. gfc.substep_shaping = 2;
  12549. gfc.noise_shaping_amp = 1;
  12550. gfc.noise_shaping_stop = 1;
  12551. if (gfc.subblock_gain == -1)
  12552. gfc.subblock_gain = 1;
  12553. gfc.use_best_huffman = 1;
  12554. /* inner loop */
  12555. gfc.full_outer_loop = 0;
  12556. break;
  12557. case 1:
  12558. gfc.psymodel = 1;
  12559. if (gfc.noise_shaping == 0)
  12560. gfc.noise_shaping = 1;
  12561. if (gfc.substep_shaping == 0)
  12562. gfc.substep_shaping = 2;
  12563. gfc.noise_shaping_amp = 2;
  12564. gfc.noise_shaping_stop = 1;
  12565. if (gfc.subblock_gain == -1)
  12566. gfc.subblock_gain = 1;
  12567. gfc.use_best_huffman = 1;
  12568. gfc.full_outer_loop = 0;
  12569. break;
  12570. case 0:
  12571. gfc.psymodel = 1;
  12572. if (gfc.noise_shaping == 0)
  12573. gfc.noise_shaping = 1;
  12574. if (gfc.substep_shaping == 0)
  12575. gfc.substep_shaping = 2;
  12576. gfc.noise_shaping_amp = 2;
  12577. gfc.noise_shaping_stop = 1;
  12578. if (gfc.subblock_gain == -1)
  12579. gfc.subblock_gain = 1;
  12580. gfc.use_best_huffman = 1;
  12581. /*
  12582. * type 2 disabled because of it slowness, in favor of full outer
  12583. * loop search
  12584. */
  12585. gfc.full_outer_loop = 0;
  12586. /*
  12587. * full outer loop search disabled because of audible distortions it
  12588. * may generate rh 060629
  12589. */
  12590. break;
  12591. }
  12592. }
  12593. function lame_init_bitstream(gfp) {
  12594. var gfc = gfp.internal_flags;
  12595. gfp.frameNum = 0;
  12596. if (gfp.write_id3tag_automatic) {
  12597. id3.id3tag_write_v2(gfp);
  12598. }
  12599. /* initialize histogram data optionally used by frontend */
  12600. gfc.bitrate_stereoMode_Hist = new_int_n([16, 4 + 1]);
  12601. gfc.bitrate_blockType_Hist = new_int_n([16, 4 + 1 + 1]);
  12602. gfc.PeakSample = 0.0;
  12603. /* Write initial VBR Header to bitstream and init VBR data */
  12604. if (gfp.bWriteVbrTag)
  12605. vbr.InitVbrTag(gfp);
  12606. }
  12607. /********************************************************************
  12608. * initialize internal params based on data in gf (globalflags struct filled
  12609. * in by calling program)
  12610. *
  12611. * OUTLINE:
  12612. *
  12613. * We first have some complex code to determine bitrate, output samplerate
  12614. * and mode. It is complicated by the fact that we allow the user to set
  12615. * some or all of these parameters, and need to determine best possible
  12616. * values for the rest of them:
  12617. *
  12618. * 1. set some CPU related flags 2. check if we are mono.mono, stereo.mono
  12619. * or stereo.stereo 3. compute bitrate and output samplerate: user may have
  12620. * set compression ratio user may have set a bitrate user may have set a
  12621. * output samplerate 4. set some options which depend on output samplerate
  12622. * 5. compute the actual compression ratio 6. set mode based on compression
  12623. * ratio
  12624. *
  12625. * The remaining code is much simpler - it just sets options based on the
  12626. * mode & compression ratio:
  12627. *
  12628. * set allow_diff_short based on mode select lowpass filter based on
  12629. * compression ratio & mode set the bitrate index, and min/max bitrates for
  12630. * VBR modes disable VBR tag if it is not appropriate initialize the
  12631. * bitstream initialize scalefac_band data set sideinfo_len (based on
  12632. * channels, CRC, out_samplerate) write an id3v2 tag into the bitstream
  12633. * write VBR tag into the bitstream set mpeg1/2 flag estimate the number of
  12634. * frames (based on a lot of data)
  12635. *
  12636. * now we set more flags: nspsytune: see code VBR modes see code CBR/ABR see
  12637. * code
  12638. *
  12639. * Finally, we set the algorithm flags based on the gfp.quality value
  12640. * lame_init_qval(gfp);
  12641. *
  12642. ********************************************************************/
  12643. this.lame_init_params = function (gfp) {
  12644. var gfc = gfp.internal_flags;
  12645. gfc.Class_ID = 0;
  12646. if (gfc.ATH == null)
  12647. gfc.ATH = new ATH();
  12648. if (gfc.PSY == null)
  12649. gfc.PSY = new PSY();
  12650. if (gfc.rgdata == null)
  12651. gfc.rgdata = new ReplayGain();
  12652. gfc.channels_in = gfp.num_channels;
  12653. if (gfc.channels_in == 1)
  12654. gfp.mode = MPEGMode.MONO;
  12655. gfc.channels_out = (gfp.mode == MPEGMode.MONO) ? 1 : 2;
  12656. gfc.mode_ext = Encoder.MPG_MD_MS_LR;
  12657. if (gfp.mode == MPEGMode.MONO)
  12658. gfp.force_ms = false;
  12659. /*
  12660. * don't allow forced mid/side stereo for mono output
  12661. */
  12662. if (gfp.VBR == VbrMode.vbr_off && gfp.VBR_mean_bitrate_kbps != 128
  12663. && gfp.brate == 0)
  12664. gfp.brate = gfp.VBR_mean_bitrate_kbps;
  12665. if (gfp.VBR == VbrMode.vbr_off || gfp.VBR == VbrMode.vbr_mtrh
  12666. || gfp.VBR == VbrMode.vbr_mt) {
  12667. /* these modes can handle free format condition */
  12668. } else {
  12669. gfp.free_format = false;
  12670. /* mode can't be mixed with free format */
  12671. }
  12672. if (gfp.VBR == VbrMode.vbr_off && gfp.brate == 0) {
  12673. /* no bitrate or compression ratio specified, use 11.025 */
  12674. if (BitStream.EQ(gfp.compression_ratio, 0))
  12675. gfp.compression_ratio = 11.025;
  12676. /*
  12677. * rate to compress a CD down to exactly 128000 bps
  12678. */
  12679. }
  12680. /* find bitrate if user specify a compression ratio */
  12681. if (gfp.VBR == VbrMode.vbr_off && gfp.compression_ratio > 0) {
  12682. if (gfp.out_samplerate == 0)
  12683. gfp.out_samplerate = map2MP3Frequency((int)(0.97 * gfp.in_samplerate));
  12684. /*
  12685. * round up with a margin of 3 %
  12686. */
  12687. /*
  12688. * choose a bitrate for the output samplerate which achieves
  12689. * specified compression ratio
  12690. */
  12691. gfp.brate = 0 | (gfp.out_samplerate * 16 * gfc.channels_out / (1.e3 * gfp.compression_ratio));
  12692. /* we need the version for the bitrate table look up */
  12693. gfc.samplerate_index = SmpFrqIndex(gfp.out_samplerate, gfp);
  12694. if (!gfp.free_format) /*
  12695. * for non Free Format find the nearest allowed
  12696. * bitrate
  12697. */
  12698. gfp.brate = FindNearestBitrate(gfp.brate, gfp.version,
  12699. gfp.out_samplerate);
  12700. }
  12701. if (gfp.out_samplerate != 0) {
  12702. if (gfp.out_samplerate < 16000) {
  12703. gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps,
  12704. 8);
  12705. gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps,
  12706. 64);
  12707. } else if (gfp.out_samplerate < 32000) {
  12708. gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps,
  12709. 8);
  12710. gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps,
  12711. 160);
  12712. } else {
  12713. gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps,
  12714. 32);
  12715. gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps,
  12716. 320);
  12717. }
  12718. }
  12719. /****************************************************************/
  12720. /* if a filter has not been enabled, see if we should add one: */
  12721. /****************************************************************/
  12722. if (gfp.lowpassfreq == 0) {
  12723. var lowpass = 16000.;
  12724. switch (gfp.VBR) {
  12725. case VbrMode.vbr_off:
  12726. {
  12727. var lh = new LowPassHighPass();
  12728. optimum_bandwidth(lh, gfp.brate);
  12729. lowpass = lh.lowerlimit;
  12730. break;
  12731. }
  12732. case VbrMode.vbr_abr:
  12733. {
  12734. var lh = new LowPassHighPass();
  12735. optimum_bandwidth(lh, gfp.VBR_mean_bitrate_kbps);
  12736. lowpass = lh.lowerlimit;
  12737. break;
  12738. }
  12739. case VbrMode.vbr_rh:
  12740. {
  12741. var x = [19500, 19000, 18600, 18000, 17500, 16000,
  12742. 15600, 14900, 12500, 10000, 3950];
  12743. if (0 <= gfp.VBR_q && gfp.VBR_q <= 9) {
  12744. var a = x[gfp.VBR_q], b = x[gfp.VBR_q + 1], m = gfp.VBR_q_frac;
  12745. lowpass = linear_int(a, b, m);
  12746. } else {
  12747. lowpass = 19500;
  12748. }
  12749. break;
  12750. }
  12751. default:
  12752. {
  12753. var x = [19500, 19000, 18500, 18000, 17500, 16500,
  12754. 15500, 14500, 12500, 9500, 3950];
  12755. if (0 <= gfp.VBR_q && gfp.VBR_q <= 9) {
  12756. var a = x[gfp.VBR_q], b = x[gfp.VBR_q + 1], m = gfp.VBR_q_frac;
  12757. lowpass = linear_int(a, b, m);
  12758. } else {
  12759. lowpass = 19500;
  12760. }
  12761. }
  12762. }
  12763. if (gfp.mode == MPEGMode.MONO
  12764. && (gfp.VBR == VbrMode.vbr_off || gfp.VBR == VbrMode.vbr_abr))
  12765. lowpass *= 1.5;
  12766. gfp.lowpassfreq = lowpass | 0;
  12767. }
  12768. if (gfp.out_samplerate == 0) {
  12769. if (2 * gfp.lowpassfreq > gfp.in_samplerate) {
  12770. gfp.lowpassfreq = gfp.in_samplerate / 2;
  12771. }
  12772. gfp.out_samplerate = optimum_samplefreq(gfp.lowpassfreq | 0,
  12773. gfp.in_samplerate);
  12774. }
  12775. gfp.lowpassfreq = Math.min(20500, gfp.lowpassfreq);
  12776. gfp.lowpassfreq = Math.min(gfp.out_samplerate / 2, gfp.lowpassfreq);
  12777. if (gfp.VBR == VbrMode.vbr_off) {
  12778. gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out
  12779. / (1.e3 * gfp.brate);
  12780. }
  12781. if (gfp.VBR == VbrMode.vbr_abr) {
  12782. gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out
  12783. / (1.e3 * gfp.VBR_mean_bitrate_kbps);
  12784. }
  12785. /*
  12786. * do not compute ReplayGain values and do not find the peak sample if
  12787. * we can't store them
  12788. */
  12789. if (!gfp.bWriteVbrTag) {
  12790. gfp.findReplayGain = false;
  12791. gfp.decode_on_the_fly = false;
  12792. gfc.findPeakSample = false;
  12793. }
  12794. gfc.findReplayGain = gfp.findReplayGain;
  12795. gfc.decode_on_the_fly = gfp.decode_on_the_fly;
  12796. if (gfc.decode_on_the_fly)
  12797. gfc.findPeakSample = true;
  12798. if (gfc.findReplayGain) {
  12799. if (ga.InitGainAnalysis(gfc.rgdata, gfp.out_samplerate) == GainAnalysis.INIT_GAIN_ANALYSIS_ERROR) {
  12800. gfp.internal_flags = null;
  12801. return -6;
  12802. }
  12803. }
  12804. if (gfc.decode_on_the_fly && !gfp.decode_only) {
  12805. if (gfc.hip != null) {
  12806. mpglib.hip_decode_exit(gfc.hip);
  12807. }
  12808. gfc.hip = mpglib.hip_decode_init();
  12809. }
  12810. gfc.mode_gr = gfp.out_samplerate <= 24000 ? 1 : 2;
  12811. /*
  12812. * Number of granules per frame
  12813. */
  12814. gfp.framesize = 576 * gfc.mode_gr;
  12815. gfp.encoder_delay = Encoder.ENCDELAY;
  12816. gfc.resample_ratio = gfp.in_samplerate / gfp.out_samplerate;
  12817. /**
  12818. * <PRE>
  12819. * sample freq bitrate compression ratio
  12820. * [kHz] [kbps/channel] for 16 bit input
  12821. * 44.1 56 12.6
  12822. * 44.1 64 11.025
  12823. * 44.1 80 8.82
  12824. * 22.05 24 14.7
  12825. * 22.05 32 11.025
  12826. * 22.05 40 8.82
  12827. * 16 16 16.0
  12828. * 16 24 10.667
  12829. * </PRE>
  12830. */
  12831. /**
  12832. * <PRE>
  12833. * For VBR, take a guess at the compression_ratio.
  12834. * For example:
  12835. *
  12836. * VBR_q compression like
  12837. * - 4.4 320 kbps/44 kHz
  12838. * 0...1 5.5 256 kbps/44 kHz
  12839. * 2 7.3 192 kbps/44 kHz
  12840. * 4 8.8 160 kbps/44 kHz
  12841. * 6 11 128 kbps/44 kHz
  12842. * 9 14.7 96 kbps
  12843. *
  12844. * for lower bitrates, downsample with --resample
  12845. * </PRE>
  12846. */
  12847. switch (gfp.VBR) {
  12848. case VbrMode.vbr_mt:
  12849. case VbrMode.vbr_rh:
  12850. case VbrMode.vbr_mtrh:
  12851. {
  12852. /* numbers are a bit strange, but they determine the lowpass value */
  12853. var cmp = [5.7, 6.5, 7.3, 8.2, 10, 11.9, 13, 14,
  12854. 15, 16.5];
  12855. gfp.compression_ratio = cmp[gfp.VBR_q];
  12856. }
  12857. break;
  12858. case VbrMode.vbr_abr:
  12859. gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out
  12860. / (1.e3 * gfp.VBR_mean_bitrate_kbps);
  12861. break;
  12862. default:
  12863. gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out
  12864. / (1.e3 * gfp.brate);
  12865. break;
  12866. }
  12867. /*
  12868. * mode = -1 (not set by user) or mode = MONO (because of only 1 input
  12869. * channel). If mode has not been set, then select J-STEREO
  12870. */
  12871. if (gfp.mode == MPEGMode.NOT_SET) {
  12872. gfp.mode = MPEGMode.JOINT_STEREO;
  12873. }
  12874. /* apply user driven high pass filter */
  12875. if (gfp.highpassfreq > 0) {
  12876. gfc.highpass1 = 2. * gfp.highpassfreq;
  12877. if (gfp.highpasswidth >= 0)
  12878. gfc.highpass2 = 2. * (gfp.highpassfreq + gfp.highpasswidth);
  12879. else
  12880. /* 0% above on default */
  12881. gfc.highpass2 = (1 + 0.00) * 2. * gfp.highpassfreq;
  12882. gfc.highpass1 /= gfp.out_samplerate;
  12883. gfc.highpass2 /= gfp.out_samplerate;
  12884. } else {
  12885. gfc.highpass1 = 0;
  12886. gfc.highpass2 = 0;
  12887. }
  12888. /* apply user driven low pass filter */
  12889. if (gfp.lowpassfreq > 0) {
  12890. gfc.lowpass2 = 2. * gfp.lowpassfreq;
  12891. if (gfp.lowpasswidth >= 0) {
  12892. gfc.lowpass1 = 2. * (gfp.lowpassfreq - gfp.lowpasswidth);
  12893. if (gfc.lowpass1 < 0) /* has to be >= 0 */
  12894. gfc.lowpass1 = 0;
  12895. } else { /* 0% below on default */
  12896. gfc.lowpass1 = (1 - 0.00) * 2. * gfp.lowpassfreq;
  12897. }
  12898. gfc.lowpass1 /= gfp.out_samplerate;
  12899. gfc.lowpass2 /= gfp.out_samplerate;
  12900. } else {
  12901. gfc.lowpass1 = 0;
  12902. gfc.lowpass2 = 0;
  12903. }
  12904. /**********************************************************************/
  12905. /* compute info needed for polyphase filter (filter type==0, default) */
  12906. /**********************************************************************/
  12907. lame_init_params_ppflt(gfp);
  12908. /*******************************************************
  12909. * samplerate and bitrate index
  12910. *******************************************************/
  12911. gfc.samplerate_index = SmpFrqIndex(gfp.out_samplerate, gfp);
  12912. if (gfc.samplerate_index < 0) {
  12913. gfp.internal_flags = null;
  12914. return -1;
  12915. }
  12916. if (gfp.VBR == VbrMode.vbr_off) {
  12917. if (gfp.free_format) {
  12918. gfc.bitrate_index = 0;
  12919. } else {
  12920. gfp.brate = FindNearestBitrate(gfp.brate, gfp.version,
  12921. gfp.out_samplerate);
  12922. gfc.bitrate_index = BitrateIndex(gfp.brate, gfp.version,
  12923. gfp.out_samplerate);
  12924. if (gfc.bitrate_index <= 0) {
  12925. gfp.internal_flags = null;
  12926. return -1;
  12927. }
  12928. }
  12929. } else {
  12930. gfc.bitrate_index = 1;
  12931. }
  12932. /* for CBR, we will write an "info" tag. */
  12933. if (gfp.analysis)
  12934. gfp.bWriteVbrTag = false;
  12935. /* some file options not allowed if output is: not specified or stdout */
  12936. if (gfc.pinfo != null)
  12937. gfp.bWriteVbrTag = false;
  12938. /* disable Xing VBR tag */
  12939. bs.init_bit_stream_w(gfc);
  12940. var j = gfc.samplerate_index + (3 * gfp.version) + 6
  12941. * (gfp.out_samplerate < 16000 ? 1 : 0);
  12942. for (var i = 0; i < Encoder.SBMAX_l + 1; i++)
  12943. gfc.scalefac_band.l[i] = qupvt.sfBandIndex[j].l[i];
  12944. for (var i = 0; i < Encoder.PSFB21 + 1; i++) {
  12945. var size = (gfc.scalefac_band.l[22] - gfc.scalefac_band.l[21])
  12946. / Encoder.PSFB21;
  12947. var start = gfc.scalefac_band.l[21] + i * size;
  12948. gfc.scalefac_band.psfb21[i] = start;
  12949. }
  12950. gfc.scalefac_band.psfb21[Encoder.PSFB21] = 576;
  12951. for (var i = 0; i < Encoder.SBMAX_s + 1; i++)
  12952. gfc.scalefac_band.s[i] = qupvt.sfBandIndex[j].s[i];
  12953. for (var i = 0; i < Encoder.PSFB12 + 1; i++) {
  12954. var size = (gfc.scalefac_band.s[13] - gfc.scalefac_band.s[12])
  12955. / Encoder.PSFB12;
  12956. var start = gfc.scalefac_band.s[12] + i * size;
  12957. gfc.scalefac_band.psfb12[i] = start;
  12958. }
  12959. gfc.scalefac_band.psfb12[Encoder.PSFB12] = 192;
  12960. /* determine the mean bitrate for main data */
  12961. if (gfp.version == 1) /* MPEG 1 */
  12962. gfc.sideinfo_len = (gfc.channels_out == 1) ? 4 + 17 : 4 + 32;
  12963. else
  12964. /* MPEG 2 */
  12965. gfc.sideinfo_len = (gfc.channels_out == 1) ? 4 + 9 : 4 + 17;
  12966. if (gfp.error_protection)
  12967. gfc.sideinfo_len += 2;
  12968. lame_init_bitstream(gfp);
  12969. gfc.Class_ID = LAME_ID;
  12970. {
  12971. var k;
  12972. for (k = 0; k < 19; k++)
  12973. gfc.nsPsy.pefirbuf[k] = 700 * gfc.mode_gr * gfc.channels_out;
  12974. if (gfp.ATHtype == -1)
  12975. gfp.ATHtype = 4;
  12976. }
  12977. switch (gfp.VBR) {
  12978. case VbrMode.vbr_mt:
  12979. gfp.VBR = VbrMode.vbr_mtrh;
  12980. //$FALL-THROUGH$
  12981. case VbrMode.vbr_mtrh:
  12982. {
  12983. if (gfp.useTemporal == null) {
  12984. gfp.useTemporal = false;
  12985. /* off by default for this VBR mode */
  12986. }
  12987. p.apply_preset(gfp, 500 - (gfp.VBR_q * 10), 0);
  12988. /**
  12989. * <PRE>
  12990. * The newer VBR code supports only a limited
  12991. * subset of quality levels:
  12992. * 9-5=5 are the same, uses x^3/4 quantization
  12993. * 4-0=0 are the same 5 plus best huffman divide code
  12994. * </PRE>
  12995. */
  12996. if (gfp.quality < 0)
  12997. gfp.quality = LAME_DEFAULT_QUALITY;
  12998. if (gfp.quality < 5)
  12999. gfp.quality = 0;
  13000. if (gfp.quality > 5)
  13001. gfp.quality = 5;
  13002. gfc.PSY.mask_adjust = gfp.maskingadjust;
  13003. gfc.PSY.mask_adjust_short = gfp.maskingadjust_short;
  13004. /*
  13005. * sfb21 extra only with MPEG-1 at higher sampling rates
  13006. */
  13007. if (gfp.experimentalY)
  13008. gfc.sfb21_extra = false;
  13009. else
  13010. gfc.sfb21_extra = (gfp.out_samplerate > 44000);
  13011. gfc.iteration_loop = new VBRNewIterationLoop(qu);
  13012. break;
  13013. }
  13014. case VbrMode.vbr_rh:
  13015. {
  13016. p.apply_preset(gfp, 500 - (gfp.VBR_q * 10), 0);
  13017. gfc.PSY.mask_adjust = gfp.maskingadjust;
  13018. gfc.PSY.mask_adjust_short = gfp.maskingadjust_short;
  13019. /*
  13020. * sfb21 extra only with MPEG-1 at higher sampling rates
  13021. */
  13022. if (gfp.experimentalY)
  13023. gfc.sfb21_extra = false;
  13024. else
  13025. gfc.sfb21_extra = (gfp.out_samplerate > 44000);
  13026. /*
  13027. * VBR needs at least the output of GPSYCHO, so we have to garantee
  13028. * that by setting a minimum quality level, actually level 6 does
  13029. * it. down to level 6
  13030. */
  13031. if (gfp.quality > 6)
  13032. gfp.quality = 6;
  13033. if (gfp.quality < 0)
  13034. gfp.quality = LAME_DEFAULT_QUALITY;
  13035. gfc.iteration_loop = new VBROldIterationLoop(qu);
  13036. break;
  13037. }
  13038. default: /* cbr/abr */
  13039. {
  13040. var vbrmode;
  13041. /*
  13042. * no sfb21 extra with CBR code
  13043. */
  13044. gfc.sfb21_extra = false;
  13045. if (gfp.quality < 0)
  13046. gfp.quality = LAME_DEFAULT_QUALITY;
  13047. vbrmode = gfp.VBR;
  13048. if (vbrmode == VbrMode.vbr_off)
  13049. gfp.VBR_mean_bitrate_kbps = gfp.brate;
  13050. /* second, set parameters depending on bitrate */
  13051. p.apply_preset(gfp, gfp.VBR_mean_bitrate_kbps, 0);
  13052. gfp.VBR = vbrmode;
  13053. gfc.PSY.mask_adjust = gfp.maskingadjust;
  13054. gfc.PSY.mask_adjust_short = gfp.maskingadjust_short;
  13055. if (vbrmode == VbrMode.vbr_off) {
  13056. gfc.iteration_loop = new CBRNewIterationLoop(qu);
  13057. } else {
  13058. gfc.iteration_loop = new ABRIterationLoop(qu);
  13059. }
  13060. break;
  13061. }
  13062. }
  13063. /* initialize default values common for all modes */
  13064. if (gfp.VBR != VbrMode.vbr_off) { /* choose a min/max bitrate for VBR */
  13065. /* if the user didn't specify VBR_max_bitrate: */
  13066. gfc.VBR_min_bitrate = 1;
  13067. /*
  13068. * default: allow 8 kbps (MPEG-2) or 32 kbps (MPEG-1)
  13069. */
  13070. gfc.VBR_max_bitrate = 14;
  13071. /*
  13072. * default: allow 160 kbps (MPEG-2) or 320 kbps (MPEG-1)
  13073. */
  13074. if (gfp.out_samplerate < 16000)
  13075. gfc.VBR_max_bitrate = 8;
  13076. /* default: allow 64 kbps (MPEG-2.5) */
  13077. if (gfp.VBR_min_bitrate_kbps != 0) {
  13078. gfp.VBR_min_bitrate_kbps = FindNearestBitrate(
  13079. gfp.VBR_min_bitrate_kbps, gfp.version,
  13080. gfp.out_samplerate);
  13081. gfc.VBR_min_bitrate = BitrateIndex(gfp.VBR_min_bitrate_kbps,
  13082. gfp.version, gfp.out_samplerate);
  13083. if (gfc.VBR_min_bitrate < 0)
  13084. return -1;
  13085. }
  13086. if (gfp.VBR_max_bitrate_kbps != 0) {
  13087. gfp.VBR_max_bitrate_kbps = FindNearestBitrate(
  13088. gfp.VBR_max_bitrate_kbps, gfp.version,
  13089. gfp.out_samplerate);
  13090. gfc.VBR_max_bitrate = BitrateIndex(gfp.VBR_max_bitrate_kbps,
  13091. gfp.version, gfp.out_samplerate);
  13092. if (gfc.VBR_max_bitrate < 0)
  13093. return -1;
  13094. }
  13095. gfp.VBR_min_bitrate_kbps = Tables.bitrate_table[gfp.version][gfc.VBR_min_bitrate];
  13096. gfp.VBR_max_bitrate_kbps = Tables.bitrate_table[gfp.version][gfc.VBR_max_bitrate];
  13097. gfp.VBR_mean_bitrate_kbps = Math.min(
  13098. Tables.bitrate_table[gfp.version][gfc.VBR_max_bitrate],
  13099. gfp.VBR_mean_bitrate_kbps);
  13100. gfp.VBR_mean_bitrate_kbps = Math.max(
  13101. Tables.bitrate_table[gfp.version][gfc.VBR_min_bitrate],
  13102. gfp.VBR_mean_bitrate_kbps);
  13103. }
  13104. /* just another daily changing developer switch */
  13105. if (gfp.tune) {
  13106. gfc.PSY.mask_adjust += gfp.tune_value_a;
  13107. gfc.PSY.mask_adjust_short += gfp.tune_value_a;
  13108. }
  13109. /* initialize internal qval settings */
  13110. lame_init_qval(gfp);
  13111. /*
  13112. * automatic ATH adjustment on
  13113. */
  13114. if (gfp.athaa_type < 0)
  13115. gfc.ATH.useAdjust = 3;
  13116. else
  13117. gfc.ATH.useAdjust = gfp.athaa_type;
  13118. /* initialize internal adaptive ATH settings -jd */
  13119. gfc.ATH.aaSensitivityP = Math.pow(10.0, gfp.athaa_sensitivity
  13120. / -10.0);
  13121. if (gfp.short_blocks == null) {
  13122. gfp.short_blocks = ShortBlock.short_block_allowed;
  13123. }
  13124. /*
  13125. * Note Jan/2003: Many hardware decoders cannot handle short blocks in
  13126. * regular stereo mode unless they are coupled (same type in both
  13127. * channels) it is a rare event (1 frame per min. or so) that LAME would
  13128. * use uncoupled short blocks, so lets turn them off until we decide how
  13129. * to handle this. No other encoders allow uncoupled short blocks, even
  13130. * though it is in the standard.
  13131. */
  13132. /*
  13133. * rh 20040217: coupling makes no sense for mono and dual-mono streams
  13134. */
  13135. if (gfp.short_blocks == ShortBlock.short_block_allowed
  13136. && (gfp.mode == MPEGMode.JOINT_STEREO || gfp.mode == MPEGMode.STEREO)) {
  13137. gfp.short_blocks = ShortBlock.short_block_coupled;
  13138. }
  13139. if (gfp.quant_comp < 0)
  13140. gfp.quant_comp = 1;
  13141. if (gfp.quant_comp_short < 0)
  13142. gfp.quant_comp_short = 0;
  13143. if (gfp.msfix < 0)
  13144. gfp.msfix = 0;
  13145. /* select psychoacoustic model */
  13146. gfp.exp_nspsytune = gfp.exp_nspsytune | 1;
  13147. if (gfp.internal_flags.nsPsy.attackthre < 0)
  13148. gfp.internal_flags.nsPsy.attackthre = PsyModel.NSATTACKTHRE;
  13149. if (gfp.internal_flags.nsPsy.attackthre_s < 0)
  13150. gfp.internal_flags.nsPsy.attackthre_s = PsyModel.NSATTACKTHRE_S;
  13151. if (gfp.scale < 0)
  13152. gfp.scale = 1;
  13153. if (gfp.ATHtype < 0)
  13154. gfp.ATHtype = 4;
  13155. if (gfp.ATHcurve < 0)
  13156. gfp.ATHcurve = 4;
  13157. if (gfp.athaa_loudapprox < 0)
  13158. gfp.athaa_loudapprox = 2;
  13159. if (gfp.interChRatio < 0)
  13160. gfp.interChRatio = 0;
  13161. if (gfp.useTemporal == null)
  13162. gfp.useTemporal = true;
  13163. /* on by default */
  13164. /*
  13165. * padding method as described in
  13166. * "MPEG-Layer3 / Bitstream Syntax and Decoding" by Martin Sieler, Ralph
  13167. * Sperschneider
  13168. *
  13169. * note: there is no padding for the very first frame
  13170. *
  13171. * Robert Hegemann 2000-06-22
  13172. */
  13173. gfc.slot_lag = gfc.frac_SpF = 0;
  13174. if (gfp.VBR == VbrMode.vbr_off)
  13175. gfc.slot_lag = gfc.frac_SpF = (((gfp.version + 1) * 72000 * gfp.brate) % gfp.out_samplerate) | 0;
  13176. qupvt.iteration_init(gfp);
  13177. psy.psymodel_init(gfp);
  13178. return 0;
  13179. }
  13180. function update_inbuffer_size(gfc, nsamples) {
  13181. if (gfc.in_buffer_0 == null || gfc.in_buffer_nsamples < nsamples) {
  13182. gfc.in_buffer_0 = new_float(nsamples);
  13183. gfc.in_buffer_1 = new_float(nsamples);
  13184. gfc.in_buffer_nsamples = nsamples;
  13185. }
  13186. }
  13187. this.lame_encode_flush = function (gfp, mp3buffer, mp3bufferPos, mp3buffer_size) {
  13188. var gfc = gfp.internal_flags;
  13189. var buffer = new_short_n([2, 1152]);
  13190. var imp3 = 0, mp3count, mp3buffer_size_remaining;
  13191. /*
  13192. * we always add POSTDELAY=288 padding to make sure granule with real
  13193. * data can be complety decoded (because of 50% overlap with next
  13194. * granule
  13195. */
  13196. var end_padding;
  13197. var frames_left;
  13198. var samples_to_encode = gfc.mf_samples_to_encode - Encoder.POSTDELAY;
  13199. var mf_needed = calcNeeded(gfp);
  13200. /* Was flush already called? */
  13201. if (gfc.mf_samples_to_encode < 1) {
  13202. return 0;
  13203. }
  13204. mp3count = 0;
  13205. if (gfp.in_samplerate != gfp.out_samplerate) {
  13206. /*
  13207. * delay due to resampling; needs to be fixed, if resampling code
  13208. * gets changed
  13209. */
  13210. samples_to_encode += 16. * gfp.out_samplerate / gfp.in_samplerate;
  13211. }
  13212. end_padding = gfp.framesize - (samples_to_encode % gfp.framesize);
  13213. if (end_padding < 576)
  13214. end_padding += gfp.framesize;
  13215. gfp.encoder_padding = end_padding;
  13216. frames_left = (samples_to_encode + end_padding) / gfp.framesize;
  13217. /*
  13218. * send in a frame of 0 padding until all internal sample buffers are
  13219. * flushed
  13220. */
  13221. while (frames_left > 0 && imp3 >= 0) {
  13222. var bunch = mf_needed - gfc.mf_size;
  13223. var frame_num = gfp.frameNum;
  13224. bunch *= gfp.in_samplerate;
  13225. bunch /= gfp.out_samplerate;
  13226. if (bunch > 1152)
  13227. bunch = 1152;
  13228. if (bunch < 1)
  13229. bunch = 1;
  13230. mp3buffer_size_remaining = mp3buffer_size - mp3count;
  13231. /* if user specifed buffer size = 0, dont check size */
  13232. if (mp3buffer_size == 0)
  13233. mp3buffer_size_remaining = 0;
  13234. imp3 = this.lame_encode_buffer(gfp, buffer[0], buffer[1], bunch,
  13235. mp3buffer, mp3bufferPos, mp3buffer_size_remaining);
  13236. mp3bufferPos += imp3;
  13237. mp3count += imp3;
  13238. frames_left -= (frame_num != gfp.frameNum) ? 1 : 0;
  13239. }
  13240. /*
  13241. * Set gfc.mf_samples_to_encode to 0, so we may detect and break loops
  13242. * calling it more than once in a row.
  13243. */
  13244. gfc.mf_samples_to_encode = 0;
  13245. if (imp3 < 0) {
  13246. /* some type of fatal error */
  13247. return imp3;
  13248. }
  13249. mp3buffer_size_remaining = mp3buffer_size - mp3count;
  13250. /* if user specifed buffer size = 0, dont check size */
  13251. if (mp3buffer_size == 0)
  13252. mp3buffer_size_remaining = 0;
  13253. /* mp3 related stuff. bit buffer might still contain some mp3 data */
  13254. bs.flush_bitstream(gfp);
  13255. imp3 = bs.copy_buffer(gfc, mp3buffer, mp3bufferPos,
  13256. mp3buffer_size_remaining, 1);
  13257. if (imp3 < 0) {
  13258. /* some type of fatal error */
  13259. return imp3;
  13260. }
  13261. mp3bufferPos += imp3;
  13262. mp3count += imp3;
  13263. mp3buffer_size_remaining = mp3buffer_size - mp3count;
  13264. /* if user specifed buffer size = 0, dont check size */
  13265. if (mp3buffer_size == 0)
  13266. mp3buffer_size_remaining = 0;
  13267. if (gfp.write_id3tag_automatic) {
  13268. /* write a id3 tag to the bitstream */
  13269. id3.id3tag_write_v1(gfp);
  13270. imp3 = bs.copy_buffer(gfc, mp3buffer, mp3bufferPos,
  13271. mp3buffer_size_remaining, 0);
  13272. if (imp3 < 0) {
  13273. return imp3;
  13274. }
  13275. mp3count += imp3;
  13276. }
  13277. return mp3count;
  13278. };
  13279. this.lame_encode_buffer = function (gfp, buffer_l, buffer_r, nsamples, mp3buf, mp3bufPos, mp3buf_size) {
  13280. var gfc = gfp.internal_flags;
  13281. var in_buffer = [null, null];
  13282. if (gfc.Class_ID != LAME_ID)
  13283. return -3;
  13284. if (nsamples == 0)
  13285. return 0;
  13286. update_inbuffer_size(gfc, nsamples);
  13287. in_buffer[0] = gfc.in_buffer_0;
  13288. in_buffer[1] = gfc.in_buffer_1;
  13289. /* make a copy of input buffer, changing type to sample_t */
  13290. for (var i = 0; i < nsamples; i++) {
  13291. in_buffer[0][i] = buffer_l[i];
  13292. if (gfc.channels_in > 1)
  13293. in_buffer[1][i] = buffer_r[i];
  13294. }
  13295. return lame_encode_buffer_sample(gfp, in_buffer[0], in_buffer[1],
  13296. nsamples, mp3buf, mp3bufPos, mp3buf_size);
  13297. }
  13298. function calcNeeded(gfp) {
  13299. var mf_needed = Encoder.BLKSIZE + gfp.framesize - Encoder.FFTOFFSET;
  13300. /*
  13301. * amount needed for FFT
  13302. */
  13303. mf_needed = Math.max(mf_needed, 512 + gfp.framesize - 32);
  13304. return mf_needed;
  13305. }
  13306. function lame_encode_buffer_sample(gfp, buffer_l, buffer_r, nsamples, mp3buf, mp3bufPos, mp3buf_size) {
  13307. var gfc = gfp.internal_flags;
  13308. var mp3size = 0, ret, i, ch, mf_needed;
  13309. var mp3out;
  13310. var mfbuf = [null, null];
  13311. var in_buffer = [null, null];
  13312. if (gfc.Class_ID != LAME_ID)
  13313. return -3;
  13314. if (nsamples == 0)
  13315. return 0;
  13316. /* copy out any tags that may have been written into bitstream */
  13317. mp3out = bs.copy_buffer(gfc, mp3buf, mp3bufPos, mp3buf_size, 0);
  13318. if (mp3out < 0)
  13319. return mp3out;
  13320. /* not enough buffer space */
  13321. mp3bufPos += mp3out;
  13322. mp3size += mp3out;
  13323. in_buffer[0] = buffer_l;
  13324. in_buffer[1] = buffer_r;
  13325. /* Apply user defined re-scaling */
  13326. /* user selected scaling of the samples */
  13327. if (BitStream.NEQ(gfp.scale, 0) && BitStream.NEQ(gfp.scale, 1.0)) {
  13328. for (i = 0; i < nsamples; ++i) {
  13329. in_buffer[0][i] *= gfp.scale;
  13330. if (gfc.channels_out == 2)
  13331. in_buffer[1][i] *= gfp.scale;
  13332. }
  13333. }
  13334. /* user selected scaling of the channel 0 (left) samples */
  13335. if (BitStream.NEQ(gfp.scale_left, 0)
  13336. && BitStream.NEQ(gfp.scale_left, 1.0)) {
  13337. for (i = 0; i < nsamples; ++i) {
  13338. in_buffer[0][i] *= gfp.scale_left;
  13339. }
  13340. }
  13341. /* user selected scaling of the channel 1 (right) samples */
  13342. if (BitStream.NEQ(gfp.scale_right, 0)
  13343. && BitStream.NEQ(gfp.scale_right, 1.0)) {
  13344. for (i = 0; i < nsamples; ++i) {
  13345. in_buffer[1][i] *= gfp.scale_right;
  13346. }
  13347. }
  13348. /* Downsample to Mono if 2 channels in and 1 channel out */
  13349. if (gfp.num_channels == 2 && gfc.channels_out == 1) {
  13350. for (i = 0; i < nsamples; ++i) {
  13351. in_buffer[0][i] = 0.5 * ( in_buffer[0][i] + in_buffer[1][i]);
  13352. in_buffer[1][i] = 0.0;
  13353. }
  13354. }
  13355. mf_needed = calcNeeded(gfp);
  13356. mfbuf[0] = gfc.mfbuf[0];
  13357. mfbuf[1] = gfc.mfbuf[1];
  13358. var in_bufferPos = 0;
  13359. while (nsamples > 0) {
  13360. var in_buffer_ptr = [null, null];
  13361. var n_in = 0;
  13362. /* number of input samples processed with fill_buffer */
  13363. var n_out = 0;
  13364. /* number of samples output with fill_buffer */
  13365. /* n_in <> n_out if we are resampling */
  13366. in_buffer_ptr[0] = in_buffer[0];
  13367. in_buffer_ptr[1] = in_buffer[1];
  13368. /* copy in new samples into mfbuf, with resampling */
  13369. var inOut = new InOut();
  13370. fill_buffer(gfp, mfbuf, in_buffer_ptr, in_bufferPos, nsamples,
  13371. inOut);
  13372. n_in = inOut.n_in;
  13373. n_out = inOut.n_out;
  13374. /* compute ReplayGain of resampled input if requested */
  13375. if (gfc.findReplayGain && !gfc.decode_on_the_fly)
  13376. if (ga.AnalyzeSamples(gfc.rgdata, mfbuf[0], gfc.mf_size,
  13377. mfbuf[1], gfc.mf_size, n_out, gfc.channels_out) == GainAnalysis.GAIN_ANALYSIS_ERROR)
  13378. return -6;
  13379. /* update in_buffer counters */
  13380. nsamples -= n_in;
  13381. in_bufferPos += n_in;
  13382. if (gfc.channels_out == 2)
  13383. ;// in_bufferPos += n_in;
  13384. /* update mfbuf[] counters */
  13385. gfc.mf_size += n_out;
  13386. /*
  13387. * lame_encode_flush may have set gfc.mf_sample_to_encode to 0 so we
  13388. * have to reinitialize it here when that happened.
  13389. */
  13390. if (gfc.mf_samples_to_encode < 1) {
  13391. gfc.mf_samples_to_encode = Encoder.ENCDELAY + Encoder.POSTDELAY;
  13392. }
  13393. gfc.mf_samples_to_encode += n_out;
  13394. if (gfc.mf_size >= mf_needed) {
  13395. /* encode the frame. */
  13396. /* mp3buf = pointer to current location in buffer */
  13397. /* mp3buf_size = size of original mp3 output buffer */
  13398. /* = 0 if we should not worry about the */
  13399. /* buffer size because calling program is */
  13400. /* to lazy to compute it */
  13401. /* mp3size = size of data written to buffer so far */
  13402. /* mp3buf_size-mp3size = amount of space avalable */
  13403. var buf_size = mp3buf_size - mp3size;
  13404. if (mp3buf_size == 0)
  13405. buf_size = 0;
  13406. ret = lame_encode_frame(gfp, mfbuf[0], mfbuf[1], mp3buf,
  13407. mp3bufPos, buf_size);
  13408. if (ret < 0)
  13409. return ret;
  13410. mp3bufPos += ret;
  13411. mp3size += ret;
  13412. /* shift out old samples */
  13413. gfc.mf_size -= gfp.framesize;
  13414. gfc.mf_samples_to_encode -= gfp.framesize;
  13415. for (ch = 0; ch < gfc.channels_out; ch++)
  13416. for (i = 0; i < gfc.mf_size; i++)
  13417. mfbuf[ch][i] = mfbuf[ch][i + gfp.framesize];
  13418. }
  13419. }
  13420. return mp3size;
  13421. }
  13422. function lame_encode_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3bufPos, mp3buf_size) {
  13423. var ret = self.enc.lame_encode_mp3_frame(gfp, inbuf_l, inbuf_r, mp3buf,
  13424. mp3bufPos, mp3buf_size);
  13425. gfp.frameNum++;
  13426. return ret;
  13427. }
  13428. function InOut() {
  13429. this.n_in = 0;
  13430. this.n_out = 0;
  13431. }
  13432. function NumUsed() {
  13433. this.num_used = 0;
  13434. }
  13435. /**
  13436. * Greatest common divisor.
  13437. * <p>
  13438. * Joint work of Euclid and M. Hendry
  13439. */
  13440. function gcd(i, j) {
  13441. return j != 0 ? gcd(j, i % j) : i;
  13442. }
  13443. /**
  13444. * Resampling via FIR filter, blackman window.
  13445. */
  13446. function blackman(x, fcn, l) {
  13447. /*
  13448. * This algorithm from: SIGNAL PROCESSING ALGORITHMS IN FORTRAN AND C
  13449. * S.D. Stearns and R.A. David, Prentice-Hall, 1992
  13450. */
  13451. var wcn = (Math.PI * fcn);
  13452. x /= l;
  13453. if (x < 0)
  13454. x = 0;
  13455. if (x > 1)
  13456. x = 1;
  13457. var x2 = x - .5;
  13458. var bkwn = 0.42 - 0.5 * Math.cos(2 * x * Math.PI) + 0.08 * Math.cos(4 * x * Math.PI);
  13459. if (Math.abs(x2) < 1e-9)
  13460. return (wcn / Math.PI);
  13461. else
  13462. return (bkwn * Math.sin(l * wcn * x2) / (Math.PI * l * x2));
  13463. }
  13464. function fill_buffer_resample(gfp, outbuf, outbufPos, desired_len, inbuf, in_bufferPos, len, num_used, ch) {
  13465. var gfc = gfp.internal_flags;
  13466. var i, j = 0, k;
  13467. /* number of convolution functions to pre-compute */
  13468. var bpc = gfp.out_samplerate
  13469. / gcd(gfp.out_samplerate, gfp.in_samplerate);
  13470. if (bpc > LameInternalFlags.BPC)
  13471. bpc = LameInternalFlags.BPC;
  13472. var intratio = (Math.abs(gfc.resample_ratio
  13473. - Math.floor(.5 + gfc.resample_ratio)) < .0001) ? 1 : 0;
  13474. var fcn = 1.00 / gfc.resample_ratio;
  13475. if (fcn > 1.00)
  13476. fcn = 1.00;
  13477. var filter_l = 31;
  13478. if (0 == filter_l % 2)
  13479. --filter_l;
  13480. /* must be odd */
  13481. filter_l += intratio;
  13482. /* unless resample_ratio=int, it must be even */
  13483. var BLACKSIZE = filter_l + 1;
  13484. /* size of data needed for FIR */
  13485. if (gfc.fill_buffer_resample_init == 0) {
  13486. gfc.inbuf_old[0] = new_float(BLACKSIZE);
  13487. gfc.inbuf_old[1] = new_float(BLACKSIZE);
  13488. for (i = 0; i <= 2 * bpc; ++i)
  13489. gfc.blackfilt[i] = new_float(BLACKSIZE);
  13490. gfc.itime[0] = 0;
  13491. gfc.itime[1] = 0;
  13492. /* precompute blackman filter coefficients */
  13493. for (j = 0; j <= 2 * bpc; j++) {
  13494. var sum = 0.;
  13495. var offset = (j - bpc) / (2. * bpc);
  13496. for (i = 0; i <= filter_l; i++)
  13497. sum += gfc.blackfilt[j][i] = blackman(i - offset, fcn,
  13498. filter_l);
  13499. for (i = 0; i <= filter_l; i++)
  13500. gfc.blackfilt[j][i] /= sum;
  13501. }
  13502. gfc.fill_buffer_resample_init = 1;
  13503. }
  13504. var inbuf_old = gfc.inbuf_old[ch];
  13505. /* time of j'th element in inbuf = itime + j/ifreq; */
  13506. /* time of k'th element in outbuf = j/ofreq */
  13507. for (k = 0; k < desired_len; k++) {
  13508. var time0;
  13509. var joff;
  13510. time0 = k * gfc.resample_ratio;
  13511. /* time of k'th output sample */
  13512. j = 0 | Math.floor(time0 - gfc.itime[ch]);
  13513. /* check if we need more input data */
  13514. if ((filter_l + j - filter_l / 2) >= len)
  13515. break;
  13516. /* blackman filter. by default, window centered at j+.5(filter_l%2) */
  13517. /* but we want a window centered at time0. */
  13518. var offset = (time0 - gfc.itime[ch] - (j + .5 * (filter_l % 2)));
  13519. /* find the closest precomputed window for this offset: */
  13520. joff = 0 | Math.floor((offset * 2 * bpc) + bpc + .5);
  13521. var xvalue = 0.;
  13522. for (i = 0; i <= filter_l; ++i) {
  13523. /* force integer index */
  13524. var j2 = 0 | (i + j - filter_l / 2);
  13525. var y;
  13526. y = (j2 < 0) ? inbuf_old[BLACKSIZE + j2] : inbuf[in_bufferPos
  13527. + j2];
  13528. xvalue += y * gfc.blackfilt[joff][i];
  13529. }
  13530. outbuf[outbufPos + k] = xvalue;
  13531. }
  13532. /* k = number of samples added to outbuf */
  13533. /* last k sample used data from [j-filter_l/2,j+filter_l-filter_l/2] */
  13534. /* how many samples of input data were used: */
  13535. num_used.num_used = Math.min(len, filter_l + j - filter_l / 2);
  13536. /*
  13537. * adjust our input time counter. Incriment by the number of samples
  13538. * used, then normalize so that next output sample is at time 0, next
  13539. * input buffer is at time itime[ch]
  13540. */
  13541. gfc.itime[ch] += num_used.num_used - k * gfc.resample_ratio;
  13542. /* save the last BLACKSIZE samples into the inbuf_old buffer */
  13543. if (num_used.num_used >= BLACKSIZE) {
  13544. for (i = 0; i < BLACKSIZE; i++)
  13545. inbuf_old[i] = inbuf[in_bufferPos + num_used.num_used + i
  13546. - BLACKSIZE];
  13547. } else {
  13548. /* shift in num_used.num_used samples into inbuf_old */
  13549. var n_shift = BLACKSIZE - num_used.num_used;
  13550. /*
  13551. * number of samples to
  13552. * shift
  13553. */
  13554. /*
  13555. * shift n_shift samples by num_used.num_used, to make room for the
  13556. * num_used new samples
  13557. */
  13558. for (i = 0; i < n_shift; ++i)
  13559. inbuf_old[i] = inbuf_old[i + num_used.num_used];
  13560. /* shift in the num_used.num_used samples */
  13561. for (j = 0; i < BLACKSIZE; ++i, ++j)
  13562. inbuf_old[i] = inbuf[in_bufferPos + j];
  13563. }
  13564. return k;
  13565. /* return the number samples created at the new samplerate */
  13566. }
  13567. function fill_buffer(gfp, mfbuf, in_buffer, in_bufferPos, nsamples, io) {
  13568. var gfc = gfp.internal_flags;
  13569. /* copy in new samples into mfbuf, with resampling if necessary */
  13570. if ((gfc.resample_ratio < .9999) || (gfc.resample_ratio > 1.0001)) {
  13571. for (var ch = 0; ch < gfc.channels_out; ch++) {
  13572. var numUsed = new NumUsed();
  13573. io.n_out = fill_buffer_resample(gfp, mfbuf[ch], gfc.mf_size,
  13574. gfp.framesize, in_buffer[ch], in_bufferPos, nsamples,
  13575. numUsed, ch);
  13576. io.n_in = numUsed.num_used;
  13577. }
  13578. } else {
  13579. io.n_out = Math.min(gfp.framesize, nsamples);
  13580. io.n_in = io.n_out;
  13581. for (var i = 0; i < io.n_out; ++i) {
  13582. mfbuf[0][gfc.mf_size + i] = in_buffer[0][in_bufferPos + i];
  13583. if (gfc.channels_out == 2)
  13584. mfbuf[1][gfc.mf_size + i] = in_buffer[1][in_bufferPos + i];
  13585. }
  13586. }
  13587. }
  13588. }
  13589. function GetAudio() {
  13590. var parse;
  13591. var mpg;
  13592. this.setModules = function (parse2, mpg2) {
  13593. parse = parse2;
  13594. mpg = mpg2;
  13595. }
  13596. }
  13597. function Parse() {
  13598. var ver;
  13599. var id3;
  13600. var pre;
  13601. this.setModules = function (ver2, id32, pre2) {
  13602. ver = ver2;
  13603. id3 = id32;
  13604. pre = pre2;
  13605. }
  13606. }
  13607. function MPGLib() {
  13608. }
  13609. function ID3Tag() {
  13610. var bits;
  13611. var ver;
  13612. this.setModules = function (_bits, _ver) {
  13613. bits = _bits;
  13614. ver = _ver;
  13615. }
  13616. }
  13617. function Mp3Encoder(channels, samplerate, kbps) {
  13618. if (arguments.length != 3) {
  13619. console.error('WARN: Mp3Encoder(channels, samplerate, kbps) not specified');
  13620. channels = 1;
  13621. samplerate = 44100;
  13622. kbps = 128;
  13623. }
  13624. var lame = new Lame();
  13625. var gaud = new GetAudio();
  13626. var ga = new GainAnalysis();
  13627. var bs = new BitStream();
  13628. var p = new Presets();
  13629. var qupvt = new QuantizePVT();
  13630. var qu = new Quantize();
  13631. var vbr = new VBRTag();
  13632. var ver = new Version();
  13633. var id3 = new ID3Tag();
  13634. var rv = new Reservoir();
  13635. var tak = new Takehiro();
  13636. var parse = new Parse();
  13637. var mpg = new MPGLib();
  13638. lame.setModules(ga, bs, p, qupvt, qu, vbr, ver, id3, mpg);
  13639. bs.setModules(ga, mpg, ver, vbr);
  13640. id3.setModules(bs, ver);
  13641. p.setModules(lame);
  13642. qu.setModules(bs, rv, qupvt, tak);
  13643. qupvt.setModules(tak, rv, lame.enc.psy);
  13644. rv.setModules(bs);
  13645. tak.setModules(qupvt);
  13646. vbr.setModules(lame, bs, ver);
  13647. gaud.setModules(parse, mpg);
  13648. parse.setModules(ver, id3, p);
  13649. var gfp = lame.lame_init();
  13650. gfp.num_channels = channels;
  13651. gfp.in_samplerate = samplerate;
  13652. gfp.brate = kbps;
  13653. gfp.mode = MPEGMode.STEREO;
  13654. gfp.quality = 3;
  13655. gfp.bWriteVbrTag = false;
  13656. gfp.disable_reservoir = true;
  13657. gfp.write_id3tag_automatic = false;
  13658. var retcode = lame.lame_init_params(gfp);
  13659. var maxSamples = 1152;
  13660. var mp3buf_size = 0 | (1.25 * maxSamples + 7200);
  13661. var mp3buf = new_byte(mp3buf_size);
  13662. this.encodeBuffer = function (left, right) {
  13663. if (channels == 1) {
  13664. right = left;
  13665. }
  13666. if (left.length > maxSamples) {
  13667. maxSamples = left.length;
  13668. mp3buf_size = 0 | (1.25 * maxSamples + 7200);
  13669. mp3buf = new_byte(mp3buf_size);
  13670. }
  13671. var _sz = lame.lame_encode_buffer(gfp, left, right, left.length, mp3buf, 0, mp3buf_size);
  13672. return new Int8Array(mp3buf.subarray(0, _sz));
  13673. };
  13674. this.flush = function () {
  13675. var _sz = lame.lame_encode_flush(gfp, mp3buf, 0, mp3buf_size);
  13676. return new Int8Array(mp3buf.subarray(0, _sz));
  13677. };
  13678. }
  13679. function WavHeader() {
  13680. this.dataOffset = 0;
  13681. this.dataLen = 0;
  13682. this.channels = 0;
  13683. this.sampleRate = 0;
  13684. }
  13685. function fourccToInt(fourcc) {
  13686. return fourcc.charCodeAt(0) << 24 | fourcc.charCodeAt(1) << 16 | fourcc.charCodeAt(2) << 8 | fourcc.charCodeAt(3);
  13687. }
  13688. WavHeader.RIFF = fourccToInt("RIFF");
  13689. WavHeader.WAVE = fourccToInt("WAVE");
  13690. WavHeader.fmt_ = fourccToInt("fmt ");
  13691. WavHeader.data = fourccToInt("data");
  13692. WavHeader.readHeader = function (dataView) {
  13693. var w = new WavHeader();
  13694. var header = dataView.getUint32(0, false);
  13695. if (WavHeader.RIFF != header) {
  13696. return;
  13697. }
  13698. var fileLen = dataView.getUint32(4, true);
  13699. if (WavHeader.WAVE != dataView.getUint32(8, false)) {
  13700. return;
  13701. }
  13702. if (WavHeader.fmt_ != dataView.getUint32(12, false)) {
  13703. return;
  13704. }
  13705. var fmtLen = dataView.getUint32(16, true);
  13706. var pos = 16 + 4;
  13707. switch (fmtLen) {
  13708. case 16:
  13709. case 18:
  13710. w.channels = dataView.getUint16(pos + 2, true);
  13711. w.sampleRate = dataView.getUint32(pos + 4, true);
  13712. break;
  13713. default:
  13714. throw 'extended fmt chunk not implemented';
  13715. }
  13716. pos += fmtLen;
  13717. var data = WavHeader.data;
  13718. var len = 0;
  13719. while (data != header) {
  13720. header = dataView.getUint32(pos, false);
  13721. len = dataView.getUint32(pos + 4, true);
  13722. if (data == header) {
  13723. break;
  13724. }
  13725. pos += (len + 8);
  13726. }
  13727. w.dataLen = len;
  13728. w.dataOffset = pos + 8;
  13729. return w;
  13730. };
  13731. L3Side.SFBMAX = (Encoder.SBMAX_s * 3);
  13732. //testFullLength();
  13733. lamejs.Mp3Encoder = Mp3Encoder;
  13734. lamejs.WavHeader = WavHeader;
  13735. }
  13736. //fs=require('fs');
  13737. lamejs();
  13738. window.lamejs = lamejs;