rfc3951.txt 364 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866
  1. Network Working Group S. Andersen
  2. Request for Comments: 3951 Aalborg University
  3. Category: Experimental A. Duric
  4. Telio
  5. H. Astrom
  6. R. Hagen
  7. W. Kleijn
  8. J. Linden
  9. Global IP Sound
  10. December 2004
  11. Internet Low Bit Rate Codec (iLBC)
  12. Status of this Memo
  13. This memo defines an Experimental Protocol for the Internet
  14. community. It does not specify an Internet standard of any kind.
  15. Discussion and suggestions for improvement are requested.
  16. Distribution of this memo is unlimited.
  17. Copyright Notice
  18. Copyright (C) The Internet Society (2004).
  19. Abstract
  20. This document specifies a speech codec suitable for robust voice
  21. communication over IP. The codec is developed by Global IP Sound
  22. (GIPS). It is designed for narrow band speech and results in a
  23. payload bit rate of 13.33 kbit/s for 30 ms frames and 15.20 kbit/s
  24. for 20 ms frames. The codec enables graceful speech quality
  25. degradation in the case of lost frames, which occurs in connection
  26. with lost or delayed IP packets.
  27. Andersen, et al. Experimental [Page 1]
  28. RFC 3951 Internet Low Bit Rate Codec December 2004
  29. Table of Contents
  30. 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 4
  31. 2. Outline of the Codec . . . . . . . . . . . . . . . . . . . . . 5
  32. 2.1. Encoder. . . . . . . . . . . . . . . . . . . . . . . . . 5
  33. 2.2. Decoder. . . . . . . . . . . . . . . . . . . . . . . . . 7
  34. 3. Encoder Principles . . . . . . . . . . . . . . . . . . . . . . 7
  35. 3.1. Pre-processing . . . . . . . . . . . . . . . . . . . . . 9
  36. 3.2. LPC Analysis and Quantization. . . . . . . . . . . . . . 9
  37. 3.2.1. Computation of Autocorrelation Coefficients. . . 10
  38. 3.2.2. Computation of LPC Coefficients. . . . . . . . . 11
  39. 3.2.3. Computation of LSF Coefficients from LPC
  40. Coefficients . . . . . . . . . . . . . . . . . . 11
  41. 3.2.4. Quantization of LSF Coefficients . . . . . . . . 12
  42. 3.2.5. Stability Check of LSF Coefficients. . . . . . . 13
  43. 3.2.6. Interpolation of LSF Coefficients. . . . . . . . 13
  44. 3.2.7. LPC Analysis and Quantization for 20 ms Frames . 14
  45. 3.3. Calculation of the Residual. . . . . . . . . . . . . . . 15
  46. 3.4. Perceptual Weighting Filter. . . . . . . . . . . . . . . 15
  47. 3.5. Start State Encoder. . . . . . . . . . . . . . . . . . . 15
  48. 3.5.1. Start State Estimation . . . . . . . . . . . . . 16
  49. 3.5.2. All-Pass Filtering and Scale Quantization. . . . 17
  50. 3.5.3. Scalar Quantization. . . . . . . . . . . . . . . 18
  51. 3.6. Encoding the Remaining Samples . . . . . . . . . . . . . 19
  52. 3.6.1. Codebook Memory. . . . . . . . . . . . . . . . . 20
  53. 3.6.2. Perceptual Weighting of Codebook Memory
  54. and Target . . . . . . . . . . . . . . . . . . . 22
  55. 3.6.3. Codebook Creation. . . . . . . . . . . . . . . . 23
  56. 3.6.3.1. Creation of a Base Codebook . . . . . . 23
  57. 3.6.3.2. Codebook Expansion. . . . . . . . . . . 24
  58. 3.6.3.3. Codebook Augmentation . . . . . . . . . 24
  59. 3.6.4. Codebook Search. . . . . . . . . . . . . . . . . 26
  60. 3.6.4.1. Codebook Search at Each Stage . . . . . 26
  61. 3.6.4.2. Gain Quantization at Each Stage . . . . 27
  62. 3.6.4.3. Preparation of Target for Next Stage. . 28
  63. 3.7. Gain Correction Encoding . . . . . . . . . . . . . . . . 28
  64. 3.8. Bitstream Definition . . . . . . . . . . . . . . . . . . 29
  65. 4. Decoder Principles . . . . . . . . . . . . . . . . . . . . . . 32
  66. 4.1. LPC Filter Reconstruction. . . . . . . . . . . . . . . . 33
  67. 4.2. Start State Reconstruction . . . . . . . . . . . . . . . 33
  68. 4.3. Excitation Decoding Loop . . . . . . . . . . . . . . . . 34
  69. 4.4. Multistage Adaptive Codebook Decoding. . . . . . . . . . 35
  70. 4.4.1. Construction of the Decoded Excitation Signal. . 35
  71. 4.5. Packet Loss Concealment. . . . . . . . . . . . . . . . . 35
  72. 4.5.1. Block Received Correctly and Previous Block
  73. Also Received. . . . . . . . . . . . . . . . . . 35
  74. 4.5.2. Block Not Received . . . . . . . . . . . . . . . 36
  75. Andersen, et al. Experimental [Page 2]
  76. RFC 3951 Internet Low Bit Rate Codec December 2004
  77. 4.5.3. Block Received Correctly When Previous Block
  78. Not Received . . . . . . . . . . . . . . . . . . 36
  79. 4.6. Enhancement. . . . . . . . . . . . . . . . . . . . . . . 37
  80. 4.6.1. Estimating the Pitch . . . . . . . . . . . . . . 39
  81. 4.6.2. Determination of the Pitch-Synchronous
  82. Sequences. . . . . . . . . . . . . . . . . . . . 39
  83. 4.6.3. Calculation of the Smoothed Excitation . . . . . 41
  84. 4.6.4. Enhancer Criterion . . . . . . . . . . . . . . . 41
  85. 4.6.5. Enhancing the Excitation . . . . . . . . . . . . 42
  86. 4.7. Synthesis Filtering. . . . . . . . . . . . . . . . . . . 43
  87. 4.8. Post Filtering . . . . . . . . . . . . . . . . . . . . . 43
  88. 5. Security Considerations. . . . . . . . . . . . . . . . . . . . 43
  89. 6. Evaluation of the iLBC Implementations . . . . . . . . . . . . 43
  90. 7. References . . . . . . . . . . . . . . . . . . . . . . . . . . 43
  91. 7.1. Normative References . . . . . . . . . . . . . . . . . . 43
  92. 7.2. Informative References . . . . . . . . . . . . . . . . . 44
  93. 8. ACKNOWLEDGEMENTS . . . . . . . . . . . . . . . . . . . . . . . 44
  94. APPENDIX A: Reference Implementation . . . . . . . . . . . . . . . 45
  95. A.1. iLBC_test.c. . . . . . . . . . . . . . . . . . . . . . . 46
  96. A.2 iLBC_encode.h. . . . . . . . . . . . . . . . . . . . . . 52
  97. A.3. iLBC_encode.c. . . . . . . . . . . . . . . . . . . . . . 53
  98. A.4. iLBC_decode.h. . . . . . . . . . . . . . . . . . . . . . 63
  99. A.5. iLBC_decode.c. . . . . . . . . . . . . . . . . . . . . . 64
  100. A.6. iLBC_define.h. . . . . . . . . . . . . . . . . . . . . . 76
  101. A.7. constants.h. . . . . . . . . . . . . . . . . . . . . . . 80
  102. A.8. constants.c. . . . . . . . . . . . . . . . . . . . . . . 82
  103. A.9. anaFilter.h. . . . . . . . . . . . . . . . . . . . . . . 96
  104. A.10. anaFilter.c. . . . . . . . . . . . . . . . . . . . . . . 97
  105. A.11. createCB.h . . . . . . . . . . . . . . . . . . . . . . . 98
  106. A.12. createCB.c . . . . . . . . . . . . . . . . . . . . . . . 99
  107. A.13. doCPLC.h . . . . . . . . . . . . . . . . . . . . . . . .104
  108. A.14. doCPLC.c . . . . . . . . . . . . . . . . . . . . . . . .104
  109. A.15. enhancer.h . . . . . . . . . . . . . . . . . . . . . . .109
  110. A.16. enhancer.c . . . . . . . . . . . . . . . . . . . . . . .110
  111. A.17. filter.h . . . . . . . . . . . . . . . . . . . . . . . .123
  112. A.18. filter.c . . . . . . . . . . . . . . . . . . . . . . . .125
  113. A.19. FrameClassify.h. . . . . . . . . . . . . . . . . . . . .128
  114. A.20. FrameClassify.c. . . . . . . . . . . . . . . . . . . . .129
  115. A.21. gainquant.h. . . . . . . . . . . . . . . . . . . . . . .131
  116. A.22. gainquant.c. . . . . . . . . . . . . . . . . . . . . . .131
  117. A.23. getCBvec.h . . . . . . . . . . . . . . . . . . . . . . .134
  118. A.24. getCBvec.c . . . . . . . . . . . . . . . . . . . . . . .134
  119. A.25. helpfun.h. . . . . . . . . . . . . . . . . . . . . . . .138
  120. A.26. helpfun.c. . . . . . . . . . . . . . . . . . . . . . . .140
  121. A.27. hpInput.h. . . . . . . . . . . . . . . . . . . . . . . .146
  122. A.28. hpInput.c. . . . . . . . . . . . . . . . . . . . . . . .146
  123. A.29. hpOutput.h . . . . . . . . . . . . . . . . . . . . . . .148
  124. A.30. hpOutput.c . . . . . . . . . . . . . . . . . . . . . . .148
  125. Andersen, et al. Experimental [Page 3]
  126. RFC 3951 Internet Low Bit Rate Codec December 2004
  127. A.31. iCBConstruct.h . . . . . . . . . . . . . . . . . . . . .149
  128. A.32. iCBConstruct.c . . . . . . . . . . . . . . . . . . . . .150
  129. A.33. iCBSearch.h. . . . . . . . . . . . . . . . . . . . . . .152
  130. A.34. iCBSearch.c. . . . . . . . . . . . . . . . . . . . . . .153
  131. A.35. LPCdecode.h. . . . . . . . . . . . . . . . . . . . . . .163
  132. A.36. LPCdecode.c. . . . . . . . . . . . . . . . . . . . . . .164
  133. A.37. LPCencode.h. . . . . . . . . . . . . . . . . . . . . . .167
  134. A.38. LPCencode.c. . . . . . . . . . . . . . . . . . . . . . .167
  135. A.39. lsf.h. . . . . . . . . . . . . . . . . . . . . . . . . .172
  136. A.40. lsf.c. . . . . . . . . . . . . . . . . . . . . . . . . .172
  137. A.41. packing.h. . . . . . . . . . . . . . . . . . . . . . . .178
  138. A.42. packing.c. . . . . . . . . . . . . . . . . . . . . . . .179
  139. A.43. StateConstructW.h. . . . . . . . . . . . . . . . . . . .182
  140. A.44. StateConstructW.c. . . . . . . . . . . . . . . . . . . .183
  141. A.45. StateSearchW.h . . . . . . . . . . . . . . . . . . . . .185
  142. A.46. StateSearchW.c . . . . . . . . . . . . . . . . . . . . .186
  143. A.47. syntFilter.h . . . . . . . . . . . . . . . . . . . . . .190
  144. A.48. syntFilter.c . . . . . . . . . . . . . . . . . . . . . .190
  145. Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . .192
  146. Full Copyright Statement . . . . . . . . . . . . . . . . . . . . .194
  147. 1. Introduction
  148. This document contains the description of an algorithm for the coding
  149. of speech signals sampled at 8 kHz. The algorithm, called iLBC, uses
  150. a block-independent linear-predictive coding (LPC) algorithm and has
  151. support for two basic frame lengths: 20 ms at 15.2 kbit/s and 30 ms
  152. at 13.33 kbit/s. When the codec operates at block lengths of 20 ms,
  153. it produces 304 bits per block, which SHOULD be packetized as in [1].
  154. Similarly, for block lengths of 30 ms it produces 400 bits per block,
  155. which SHOULD be packetized as in [1]. The two modes for the
  156. different frame sizes operate in a very similar way. When they
  157. differ it is explicitly stated in the text, usually with the notation
  158. x/y, where x refers to the 20 ms mode and y refers to the 30 ms mode.
  159. The described algorithm results in a speech coding system with a
  160. controlled response to packet losses similar to what is known from
  161. pulse code modulation (PCM) with packet loss concealment (PLC), such
  162. as the ITU-T G.711 standard [4], which operates at a fixed bit rate
  163. of 64 kbit/s. At the same time, the described algorithm enables
  164. fixed bit rate coding with a quality-versus-bit rate tradeoff close
  165. to state-of-the-art. A suitable RTP payload format for the iLBC
  166. codec is specified in [1].
  167. Some of the applications for which this coder is suitable are real
  168. time communications such as telephony and videoconferencing,
  169. streaming audio, archival, and messaging.
  170. Andersen, et al. Experimental [Page 4]
  171. RFC 3951 Internet Low Bit Rate Codec December 2004
  172. Cable Television Laboratories (CableLabs(R)) has adopted iLBC as a
  173. mandatory PacketCable(TM) audio codec standard for VoIP over Cable
  174. applications [3].
  175. This document is organized as follows. Section 2 gives a brief
  176. outline of the codec. The specific encoder and decoder algorithms
  177. are explained in sections 3 and 4, respectively. Appendix A provides
  178. a c-code reference implementation.
  179. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
  180. "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
  181. document are to be interpreted as described in BCP 14, RFC 2119 [2].
  182. 2. Outline of the Codec
  183. The codec consists of an encoder and a decoder as described in
  184. sections 2.1 and 2.2, respectively.
  185. The essence of the codec is LPC and block-based coding of the LPC
  186. residual signal. For each 160/240 (20 ms/30 ms) sample block, the
  187. following major steps are performed: A set of LPC filters are
  188. computed, and the speech signal is filtered through them to produce
  189. the residual signal. The codec uses scalar quantization of the
  190. dominant part, in terms of energy, of the residual signal for the
  191. block. The dominant state is of length 57/58 (20 ms/30 ms) samples
  192. and forms a start state for dynamic codebooks constructed from the
  193. already coded parts of the residual signal. These dynamic codebooks
  194. are used to code the remaining parts of the residual signal. By this
  195. method, coding independence between blocks is achieved, resulting in
  196. elimination of propagation of perceptual degradations due to packet
  197. loss. The method facilitates high-quality packet loss concealment
  198. (PLC).
  199. 2.1. Encoder
  200. The input to the encoder SHOULD be 16 bit uniform PCM sampled at 8
  201. kHz. It SHOULD be partitioned into blocks of BLOCKL=160/240 samples
  202. for the 20/30 ms frame size. Each block is divided into NSUB=4/6
  203. consecutive sub-blocks of SUBL=40 samples each. For 30 ms frame
  204. size, the encoder performs two LPC_FILTERORDER=10 linear-predictive
  205. coding (LPC) analyses. The first analysis applies a smooth window
  206. centered over the second sub-block and extending to the middle of the
  207. fifth sub-block. The second LPC analysis applies a smooth asymmetric
  208. window centered over the fifth sub-block and extending to the end of
  209. the sixth sub-block. For 20 ms frame size, one LPC_FILTERORDER=10
  210. linear-predictive coding (LPC) analysis is performed with a smooth
  211. window centered over the third sub-frame.
  212. Andersen, et al. Experimental [Page 5]
  213. RFC 3951 Internet Low Bit Rate Codec December 2004
  214. For each of the LPC analyses, a set of line-spectral frequencies
  215. (LSFs) are obtained, quantized, and interpolated to obtain LSF
  216. coefficients for each sub-block. Subsequently, the LPC residual is
  217. computed by using the quantized and interpolated LPC analysis
  218. filters.
  219. The two consecutive sub-blocks of the residual exhibiting the maximal
  220. weighted energy are identified. Within these two sub-blocks, the
  221. start state (segment) is selected from two choices: the first 57/58
  222. samples or the last 57/58 samples of the two consecutive sub-blocks.
  223. The selected segment is the one of higher energy. The start state is
  224. encoded with scalar quantization.
  225. A dynamic codebook encoding procedure is used to encode 1) the 23/22
  226. (20 ms/30 ms) remaining samples in the two sub-blocks containing the
  227. start state; 2) the sub-blocks after the start state in time; and 3)
  228. the sub-blocks before the start state in time. Thus, the encoding
  229. target can be either the 23/22 samples remaining of the two sub-
  230. blocks containing the start state or a 40-sample sub-block. This
  231. target can consist of samples indexed forward in time or backward in
  232. time, depending on the location of the start state.
  233. The codebook coding is based on an adaptive codebook built from a
  234. codebook memory that contains decoded LPC excitation samples from the
  235. already encoded part of the block. These samples are indexed in the
  236. same time direction as the target vector, ending at the sample
  237. instant prior to the first sample instant represented in the target
  238. vector. The codebook is used in CB_NSTAGES=3 stages in a successive
  239. refinement approach, and the resulting three code vector gains are
  240. encoded with 5-, 4-, and 3-bit scalar quantization, respectively.
  241. The codebook search method employs noise shaping derived from the LPC
  242. filters, and the main decision criterion is to minimize the squared
  243. error between the target vector and the code vectors. Each code
  244. vector in this codebook comes from one of CB_EXPAND=2 codebook
  245. sections. The first section is filled with delayed, already encoded
  246. residual vectors. The code vectors of the second codebook section
  247. are constructed by predefined linear combinations of vectors in the
  248. first section of the codebook.
  249. As codebook encoding with squared-error matching is known to produce
  250. a coded signal of less power than does the scalar quantized start
  251. state signal, a gain re-scaling method is implemented by a refined
  252. search for a better set of codebook gains in terms of power matching
  253. after encoding. This is done by searching for a higher value of the
  254. gain factor for the first stage codebook, as the subsequent stage
  255. codebook gains are scaled by the first stage gain.
  256. Andersen, et al. Experimental [Page 6]
  257. RFC 3951 Internet Low Bit Rate Codec December 2004
  258. 2.2. Decoder
  259. Typically for packet communications, a jitter buffer placed at the
  260. receiving end decides whether the packet containing an encoded signal
  261. block has been received or lost. This logic is not part of the codec
  262. described here. For each encoded signal block received the decoder
  263. performs a decoding. For each lost signal block, the decoder
  264. performs a PLC operation.
  265. The decoding for each block starts by decoding and interpolating the
  266. LPC coefficients. Subsequently the start state is decoded.
  267. For codebook-encoded segments, each segment is decoded by
  268. constructing the three code vectors given by the received codebook
  269. indices in the same way that the code vectors were constructed in the
  270. encoder. The three gain factors are also decoded and the resulting
  271. decoded signal is given by the sum of the three codebook vectors
  272. scaled with respective gain.
  273. An enhancement algorithm is applied to the reconstructed excitation
  274. signal. This enhancement augments the periodicity of voiced speech
  275. regions. The enhancement is optimized under the constraint that the
  276. modification signal (defined as the difference between the enhanced
  277. excitation and the excitation signal prior to enhancement) has a
  278. short-time energy that does not exceed a preset fraction of the
  279. short-time energy of the excitation signal prior to enhancement.
  280. A packet loss concealment (PLC) operation is easily embedded in the
  281. decoder. The PLC operation can, e.g., be based on repeating LPC
  282. filters and obtaining the LPC residual signal by using a long-term
  283. prediction estimate from previous residual blocks.
  284. 3. Encoder Principles
  285. The following block diagram is an overview of all the components of
  286. the iLBC encoding procedure. The description of the blocks contains
  287. references to the section where that particular procedure is further
  288. described.
  289. Andersen, et al. Experimental [Page 7]
  290. RFC 3951 Internet Low Bit Rate Codec December 2004
  291. +-----------+ +---------+ +---------+
  292. speech -> | 1. Pre P | -> | 2. LPC | -> | 3. Ana | ->
  293. +-----------+ +---------+ +---------+
  294. +---------------+ +--------------+
  295. -> | 4. Start Sel | ->| 5. Scalar Qu | ->
  296. +---------------+ +--------------+
  297. +--------------+ +---------------+
  298. -> |6. CB Search | -> | 7. Packetize | -> payload
  299. | +--------------+ | +---------------+
  300. ----<---------<------
  301. sub-frame 0..2/4 (20 ms/30 ms)
  302. Figure 3.1. Flow chart of the iLBC encoder
  303. 1. Pre-process speech with a HP filter, if needed (section 3.1).
  304. 2. Compute LPC parameters, quantize, and interpolate (section 3.2).
  305. 3. Use analysis filters on speech to compute residual (section 3.3).
  306. 4. Select position of 57/58-sample start state (section 3.5).
  307. 5. Quantize the 57/58-sample start state with scalar quantization
  308. (section 3.5).
  309. 6. Search the codebook for each sub-frame. Start with 23/22 sample
  310. block, then encode sub-blocks forward in time, and then encode
  311. sub-blocks backward in time. For each block, the steps in Figure
  312. 3.4 are performed (section 3.6).
  313. 7. Packetize the bits into the payload specified in Table 3.2.
  314. The input to the encoder SHOULD be 16-bit uniform PCM sampled at 8
  315. kHz. Also it SHOULD be partitioned into blocks of BLOCKL=160/240
  316. samples. Each block input to the encoder is divided into NSUB=4/6
  317. consecutive sub-blocks of SUBL=40 samples each.
  318. Andersen, et al. Experimental [Page 8]
  319. RFC 3951 Internet Low Bit Rate Codec December 2004
  320. 0 39 79 119 159
  321. +---------------------------------------+
  322. | 1 | 2 | 3 | 4 |
  323. +---------------------------------------+
  324. 20 ms frame
  325. 0 39 79 119 159 199 239
  326. +-----------------------------------------------------------+
  327. | 1 | 2 | 3 | 4 | 5 | 6 |
  328. +-----------------------------------------------------------+
  329. 30 ms frame
  330. Figure 3.2. One input block to the encoder for 20 ms (with four sub-
  331. frames) and 30 ms (with six sub-frames).
  332. 3.1. Pre-processing
  333. In some applications, the recorded speech signal contains DC level
  334. and/or 50/60 Hz noise. If these components have not been removed
  335. prior to the encoder call, they should be removed by a high-pass
  336. filter. A reference implementation of this, using a filter with a
  337. cutoff frequency of 90 Hz, can be found in Appendix A.28.
  338. 3.2. LPC Analysis and Quantization
  339. The input to the LPC analysis module is a possibly high-pass filtered
  340. speech buffer, speech_hp, that contains 240/300 (LPC_LOOKBACK +
  341. BLOCKL = 80/60 + 160/240 = 240/300) speech samples, where samples 0
  342. through 79/59 are from the previous block and samples 80/60 through
  343. 239/299 are from the current block. No look-ahead into the next
  344. block is used. For the very first block processed, the look-back
  345. samples are assumed to be zeros.
  346. For each input block, the LPC analysis calculates one/two set(s) of
  347. LPC_FILTERORDER=10 LPC filter coefficients using the autocorrelation
  348. method and the Levinson-Durbin recursion. These coefficients are
  349. converted to the Line Spectrum Frequency representation. In the 20
  350. ms case, the single lsf set represents the spectral characteristics
  351. as measured at the center of the third sub-block. For 30 ms frames,
  352. the first set, lsf1, represents the spectral properties of the input
  353. signal at the center of the second sub-block, and the other set,
  354. lsf2, represents the spectral characteristics as measured at the
  355. center of the fifth sub-block. The details of the computation for 30
  356. ms frames are described in sections 3.2.1 through 3.2.6. Section
  357. 3.2.7 explains how the LPC Analysis and Quantization differs for 20
  358. ms frames.
  359. Andersen, et al. Experimental [Page 9]
  360. RFC 3951 Internet Low Bit Rate Codec December 2004
  361. 3.2.1. Computation of Autocorrelation Coefficients
  362. The first step in the LPC analysis procedure is to calculate
  363. autocorrelation coefficients by using windowed speech samples. This
  364. windowing is the only difference in the LPC analysis procedure for
  365. the two sets of coefficients. For the first set, a 240-sample-long
  366. standard symmetric Hanning window is applied to samples 0 through 239
  367. of the input data. The first window, lpc_winTbl, is defined as
  368. lpc_winTbl[i]= 0.5 * (1.0 - cos((2*PI*(i+1))/(BLOCKL+1)));
  369. i=0,...,119
  370. lpc_winTbl[i] = winTbl[BLOCKL - i - 1]; i=120,...,239
  371. The windowed speech speech_hp_win1 is then obtained by multiplying
  372. the first 240 samples of the input speech buffer with the window
  373. coefficients:
  374. speech_hp_win1[i] = speech_hp[i] * lpc_winTbl[i];
  375. i=0,...,BLOCKL-1
  376. From these 240 windowed speech samples, 11 (LPC_FILTERORDER + 1)
  377. autocorrelation coefficients, acf1, are calculated:
  378. acf1[lag] += speech_hp_win1[n] * speech_hp_win1[n + lag];
  379. lag=0,...,LPC_FILTERORDER; n=0,...,BLOCKL-lag-1
  380. In order to make the analysis more robust against numerical precision
  381. problems, a spectral smoothing procedure is applied by windowing the
  382. autocorrelation coefficients before the LPC coefficients are
  383. computed. Also, a white noise floor is added to the autocorrelation
  384. function by multiplying coefficient zero by 1.0001 (40dB below the
  385. energy of the windowed speech signal). These two steps are
  386. implemented by multiplying the autocorrelation coefficients with the
  387. following window:
  388. lpc_lagwinTbl[0] = 1.0001;
  389. lpc_lagwinTbl[i] = exp(-0.5 * ((2 * PI * 60.0 * i) /FS)^2);
  390. i=1,...,LPC_FILTERORDER
  391. where FS=8000 is the sampling frequency
  392. Then, the windowed acf function acf1_win is obtained by
  393. acf1_win[i] = acf1[i] * lpc_lagwinTbl[i];
  394. i=0,...,LPC_FILTERORDER
  395. The second set of autocorrelation coefficients, acf2_win, are
  396. obtained in a similar manner. The window, lpc_asymwinTbl, is applied
  397. to samples 60 through 299, i.e., the entire current block. The
  398. Andersen, et al. Experimental [Page 10]
  399. RFC 3951 Internet Low Bit Rate Codec December 2004
  400. window consists of two segments, the first (samples 0 to 219) being
  401. half a Hanning window with length 440 and the second a quarter of a
  402. cycle of a cosine wave. By using this asymmetric window, an LPC
  403. analysis centered in the fifth sub-block is obtained without the need
  404. for any look-ahead, which would add delay. The asymmetric window is
  405. defined as
  406. lpc_asymwinTbl[i] = (sin(PI * (i + 1) / 441))^2; i=0,...,219
  407. lpc_asymwinTbl[i] = cos((i - 220) * PI / 40); i=220,...,239
  408. and the windowed speech is computed by
  409. speech_hp_win2[i] = speech_hp[i + LPC_LOOKBACK] *
  410. lpc_asymwinTbl[i]; i=0,....BLOCKL-1
  411. The windowed autocorrelation coefficients are then obtained in
  412. exactly the same way as for the first analysis instance.
  413. The generation of the windows lpc_winTbl, lpc_asymwinTbl, and
  414. lpc_lagwinTbl are typically done in advance, and the arrays are
  415. stored in ROM rather than repeating the calculation for every block.
  416. 3.2.2. Computation of LPC Coefficients
  417. From the 2 x 11 smoothed autocorrelation coefficients, acf1_win and
  418. acf2_win, the 2 x 11 LPC coefficients, lp1 and lp2, are calculated
  419. in the same way for both analysis locations by using the well known
  420. Levinson-Durbin recursion. The first LPC coefficient is always 1.0,
  421. resulting in ten unique coefficients.
  422. After determining the LPC coefficients, a bandwidth expansion
  423. procedure is applied to smooth the spectral peaks in the
  424. short-term spectrum. The bandwidth addition is obtained by the
  425. following modification of the LPC coefficients:
  426. lp1_bw[i] = lp1[i] * chirp^i; i=0,...,LPC_FILTERORDER
  427. lp2_bw[i] = lp2[i] * chirp^i; i=0,...,LPC_FILTERORDER
  428. where "chirp" is a real number between 0 and 1. It is RECOMMENDED to
  429. use a value of 0.9.
  430. 3.2.3. Computation of LSF Coefficients from LPC Coefficients
  431. Thus far, two sets of LPC coefficients that represent the short-term
  432. spectral characteristics of the speech signal for two different time
  433. locations within the current block have been determined. These
  434. coefficients SHOULD be quantized and interpolated. Before this is
  435. Andersen, et al. Experimental [Page 11]
  436. RFC 3951 Internet Low Bit Rate Codec December 2004
  437. done, it is advantageous to convert the LPC parameters into another
  438. type of representation called Line Spectral Frequencies (LSF). The
  439. LSF parameters are used because they are better suited for
  440. quantization and interpolation than the regular LPC coefficients.
  441. Many computationally efficient methods for calculating the LSFs from
  442. the LPC coefficients have been proposed in the literature. The
  443. detailed implementation of one applicable method can be found in
  444. Appendix A.26. The two arrays of LSF coefficients obtained, lsf1 and
  445. lsf2, are of dimension 10 (LPC_FILTERORDER).
  446. 3.2.4. Quantization of LSF Coefficients
  447. Because the LPC filters defined by the two sets of LSFs are also
  448. needed in the decoder, the LSF parameters need to be quantized and
  449. transmitted as side information. The total number of bits required
  450. to represent the quantization of the two LSF representations for one
  451. block of speech is 40, with 20 bits used for each of lsf1 and lsf2.
  452. For computational and storage reasons, the LSF vectors are quantized
  453. using three-split vector quantization (VQ). That is, the LSF vectors
  454. are split into three sub-vectors that are each quantized with a
  455. regular VQ. The quantized versions of lsf1 and lsf2, qlsf1 and
  456. qlsf2, are obtained by using the same memoryless split VQ. The
  457. length of each of these two LSF vectors is 10, and they are split
  458. into three sub-vectors containing 3, 3, and 4 values, respectively.
  459. For each of the sub-vectors, a separate codebook of quantized values
  460. has been designed with a standard VQ training method for a large
  461. database containing speech from a large number of speakers recorded
  462. under various conditions. The size of each of the three codebooks
  463. associated with the split definitions above is
  464. int size_lsfCbTbl[LSF_NSPLIT] = {64,128,128};
  465. The actual values of the vector quantization codebook that must be
  466. used can be found in the reference code of Appendix A. Both sets of
  467. LSF coefficients, lsf1 and lsf2, are quantized with a standard
  468. memoryless split vector quantization (VQ) structure using the squared
  469. error criterion in the LSF domain. The split VQ quantization
  470. consists of the following steps:
  471. 1) Quantize the first three LSF coefficients (1 - 3) with a VQ
  472. codebook of size 64.
  473. 2) Quantize the next three LSF coefficients 4 - 6 with VQ a codebook
  474. of size 128.
  475. 3) Quantize the last four LSF coefficients (7 - 10) with a VQ
  476. codebook of size 128.
  477. Andersen, et al. Experimental [Page 12]
  478. RFC 3951 Internet Low Bit Rate Codec December 2004
  479. This procedure, repeated for lsf1 and lsf2, gives six quantization
  480. indices and the quantized sets of LSF coefficients qlsf1 and qlsf2.
  481. Each set of three indices is encoded with 6 + 7 + 7 = 20 bits. The
  482. total number of bits used for LSF quantization in a block is thus 40
  483. bits.
  484. 3.2.5. Stability Check of LSF Coefficients
  485. The LSF representation of the LPC filter has the convenient property
  486. that the coefficients are ordered by increasing value, i.e., lsf(n-1)
  487. < lsf(n), 0 < n < 10, if the corresponding synthesis filter is
  488. stable. As we are employing a split VQ scheme, it is possible that
  489. at the split boundaries the LSF coefficients are not ordered
  490. correctly and hence that the corresponding LP filter is unstable. To
  491. ensure that the filter used is stable, a stability check is performed
  492. for the quantized LSF vectors. If it turns out that the coefficients
  493. are not ordered appropriately (with a safety margin of 50 Hz to
  494. ensure that formant peaks are not too narrow), they will be moved
  495. apart. The detailed method for this can be found in Appendix A.40.
  496. The same procedure is performed in the decoder. This ensures that
  497. exactly the same LSF representations are used in both encoder and
  498. decoder.
  499. 3.2.6. Interpolation of LSF Coefficients
  500. From the two sets of LSF coefficients that are computed for each
  501. block of speech, different LSFs are obtained for each sub-block by
  502. means of interpolation. This procedure is performed for the original
  503. LSFs (lsf1 and lsf2), as well as the quantized versions qlsf1 and
  504. qlsf2, as both versions are used in the encoder. Here follows a
  505. brief summary of the interpolation scheme; the details are found in
  506. the c-code of Appendix A. In the first sub-block, the average of the
  507. second LSF vector from the previous block and the first LSF vector in
  508. the current block is used. For sub-blocks two through five, the LSFs
  509. used are obtained by linear interpolation from lsf1 (and qlsf1) to
  510. lsf2 (and qlsf2), with lsf1 used in sub-block two and lsf2 in sub-
  511. block five. In the last sub-block, lsf2 is used. For the very first
  512. block it is assumed that the last LSF vector of the previous block is
  513. equal to a predefined vector, lsfmeanTbl, obtained by calculating the
  514. mean LSF vector of the LSF design database.
  515. lsfmeanTbl[LPC_FILTERORDER] = {0.281738, 0.445801, 0.663330,
  516. 0.962524, 1.251831, 1.533081, 1.850586, 2.137817,
  517. 2.481445, 2.777344}
  518. Andersen, et al. Experimental [Page 13]
  519. RFC 3951 Internet Low Bit Rate Codec December 2004
  520. The interpolation method is standard linear interpolation in the LSF
  521. domain. The interpolated LSF values are converted to LPC
  522. coefficients for each sub-block. The unquantized and quantized LPC
  523. coefficients form two sets of filters respectively. The unquantized
  524. analysis filter for sub-block k is defined as follows
  525. ___
  526. \
  527. Ak(z)= 1 + > ak(i)*z^(-i)
  528. /__
  529. i=1...LPC_FILTERORDER
  530. The quantized analysis filter for sub-block k is defined as follows
  531. ___
  532. \
  533. A~k(z)= 1 + > a~k(i)*z^(-i)
  534. /__
  535. i=1...LPC_FILTERORDER
  536. A reference implementation of the lsf encoding is given in Appendix
  537. A.38. A reference implementation of the corresponding decoding can
  538. be found in Appendix A.36.
  539. 3.2.7. LPC Analysis and Quantization for 20 ms Frames
  540. As previously stated, the codec only calculates one set of LPC
  541. parameters for the 20 ms frame size as opposed to two sets for 30 ms
  542. frames. A single set of autocorrelation coefficients is calculated
  543. on the LPC_LOOKBACK + BLOCKL = 80 + 160 = 240 samples. These samples
  544. are windowed with the asymmetric window lpc_asymwinTbl, centered over
  545. the third sub-frame, to form speech_hp_win. Autocorrelation
  546. coefficients, acf, are calculated on the 240 samples in speech_hp_win
  547. and then windowed exactly as in section 3.2.1 (resulting in
  548. acf_win).
  549. This single set of windowed autocorrelation coefficients is used to
  550. calculate LPC coefficients, LSF coefficients, and quantized LSF
  551. coefficients in exactly the same manner as in sections 3.2.3 through
  552. 3.2.4. As for the 30 ms frame size, the ten LSF coefficients are
  553. divided into three sub-vectors of size 3, 3, and 4 and quantized by
  554. using the same scheme and codebook as in section 3.2.4 to finally get
  555. 3 quantization indices. The quantized LSF coefficients are
  556. stabilized with the algorithm described in section 3.2.5.
  557. From the set of LSF coefficients computed for this block and those
  558. from the previous block, different LSFs are obtained for each sub-
  559. block by means of interpolation. The interpolation is done linearly
  560. in the LSF domain over the four sub-blocks, so that the n-th sub-
  561. Andersen, et al. Experimental [Page 14]
  562. RFC 3951 Internet Low Bit Rate Codec December 2004
  563. frame uses the weight (4-n)/4 for the LSF from old frame and the
  564. weight n/4 of the LSF from the current frame. For the very first
  565. block the mean LSF, lsfmeanTbl, is used as the LSF from the previous
  566. block. Similarly as seen in section 3.2.6, both unquantized, A(z),
  567. and quantized, A~(z), analysis filters are calculated for each of the
  568. four sub-blocks.
  569. 3.3. Calculation of the Residual
  570. The block of speech samples is filtered by the quantized and
  571. interpolated LPC analysis filters to yield the residual signal. In
  572. particular, the corresponding LPC analysis filter for each 40 sample
  573. sub-block is used to filter the speech samples for the same sub-
  574. block. The filter memory at the end of each sub-block is carried
  575. over to the LPC filter of the next sub-block. The signal at the
  576. output of each LP analysis filter constitutes the residual signal for
  577. the corresponding sub-block.
  578. A reference implementation of the LPC analysis filters is given in
  579. Appendix A.10.
  580. 3.4. Perceptual Weighting Filter
  581. In principle any good design of a perceptual weighting filter can be
  582. applied in the encoder without compromising this codec definition.
  583. However, it is RECOMMENDED to use the perceptual weighting filter Wk
  584. for sub-block k specified below:
  585. Wk(z)=1/Ak(z/LPC_CHIRP_WEIGHTDENUM), where
  586. LPC_CHIRP_WEIGHTDENUM = 0.4222
  587. This is a simple design with low complexity that is applied in the
  588. LPC residual domain. Here Ak(z) is the filter obtained for sub-block
  589. k from unquantized but interpolated LSF coefficients.
  590. 3.5. Start State Encoder
  591. The start state is quantized by using a common 6-bit scalar quantizer
  592. for the block and a 3-bit scalar quantizer operating on scaled
  593. samples in the weighted speech domain. In the following we describe
  594. the state encoding in greater detail.
  595. Andersen, et al. Experimental [Page 15]
  596. RFC 3951 Internet Low Bit Rate Codec December 2004
  597. 3.5.1. Start State Estimation
  598. The two sub-blocks containing the start state are determined by
  599. finding the two consecutive sub-blocks in the block having the
  600. highest power. Advantageously, down-weighting is used in the
  601. beginning and end of the sub-frames, i.e., the following measure is
  602. computed (NSUB=4/6 for 20/30 ms frame size):
  603. nsub=1,...,NSUB-1
  604. ssqn[nsub] = 0.0;
  605. for (i=(nsub-1)*SUBL; i<(nsub-1)*SUBL+5; i++)
  606. ssqn[nsub] += sampEn_win[i-(nsub-1)*SUBL]*
  607. residual[i]*residual[i];
  608. for (i=(nsub-1)*SUBL+5; i<(nsub+1)*SUBL-5; i++)
  609. ssqn[nsub] += residual[i]*residual[i];
  610. for (i=(nsub+1)*SUBL-5; i<(nsub+1)*SUBL; i++)
  611. ssqn[nsub] += sampEn_win[(nsub+1)*SUBL-i-1]*
  612. residual[i]*residual[i];
  613. where sampEn_win[5]={1/6, 2/6, 3/6, 4/6, 5/6}; MAY be used. The
  614. sub-frame number corresponding to the maximum value of
  615. ssqEn_win[nsub-1]*ssqn[nsub] is selected as the start state
  616. indicator. A weighting of ssqEn_win[]={0.8,0.9,1.0,0.9,0.8} for 30
  617. ms frames and ssqEn_win[]={0.9,1.0,0.9} for 20 ms frames; MAY
  618. advantageously be used to bias the start state towards the middle of
  619. the frame.
  620. For 20 ms frames there are three possible positions for the two-sub-
  621. block length maximum power segment; the start state position is
  622. encoded with 2 bits. The start state position, start, MUST be
  623. encoded as
  624. start=1: start state in sub-frame 0 and 1
  625. start=2: start state in sub-frame 1 and 2
  626. start=3: start state in sub-frame 2 and 3
  627. For 30 ms frames there are five possible positions of the two-sub-
  628. block length maximum power segment, the start state position is
  629. encoded with 3 bits. The start state position, start, MUST be
  630. encoded as
  631. start=1: start state in sub-frame 0 and 1
  632. start=2: start state in sub-frame 1 and 2
  633. start=3: start state in sub-frame 2 and 3
  634. start=4: start state in sub-frame 3 and 4
  635. start=5: start state in sub-frame 4 and 5
  636. Andersen, et al. Experimental [Page 16]
  637. RFC 3951 Internet Low Bit Rate Codec December 2004
  638. Hence, in both cases, index 0 is not used. In order to shorten the
  639. start state for bit rate efficiency, the start state is brought down
  640. to STATE_SHORT_LEN=57 samples for 20 ms frames and STATE_SHORT_LEN=58
  641. samples for 30 ms frames. The power of the first 23/22 and last
  642. 23/22 samples of the two sub-frame blocks identified above is
  643. computed as the sum of the squared signal sample values, and the
  644. 23/22-sample segment with the lowest power is excluded from the start
  645. state. One bit is transmitted to indicate which of the two possible
  646. 57/58 sample segments is used. The start state position within the
  647. two sub-frames determined above, state_first, MUST be encoded as
  648. state_first=1: start state is first STATE_SHORT_LEN samples
  649. state_first=0: start state is last STATE_SHORT_LEN samples
  650. 3.5.2. All-Pass Filtering and Scale Quantization
  651. The block of residual samples in the start state is first filtered by
  652. an all-pass filter with the quantized LPC coefficients as denominator
  653. and reversed quantized LPC coefficients as numerator. The purpose of
  654. this phase-dispersion filter is to get a more even distribution of
  655. the sample values in the residual signal. The filtering is performed
  656. by circular convolution, where the initial filter memory is set to
  657. zero.
  658. res(0..(STATE_SHORT_LEN-1)) = uncoded start state residual
  659. res((STATE_SHORT_LEN)..(2*STATE_SHORT_LEN-1)) = 0
  660. Pk(z) = A~rk(z)/A~k(z), where
  661. ___
  662. \
  663. A~rk(z)= z^(-LPC_FILTERORDER)+>a~k(i+1)*z^(i-(LPC_FILTERORDER-1))
  664. /__
  665. i=0...(LPC_FILTERORDER-1)
  666. and A~k(z) is taken from the block where the start state begins
  667. res -> Pk(z) -> filtered
  668. ccres(k) = filtered(k) + filtered(k+STATE_SHORT_LEN),
  669. k=0..(STATE_SHORT_LEN-1)
  670. The all-pass filtered block is searched for its largest magnitude
  671. sample. The 10-logarithm of this magnitude is quantized with a 6-bit
  672. quantizer, state_frgqTbl, by finding the nearest representation.
  673. Andersen, et al. Experimental [Page 17]
  674. RFC 3951 Internet Low Bit Rate Codec December 2004
  675. This results in an index, idxForMax, corresponding to a quantized
  676. value, qmax. The all-pass filtered residual samples in the block are
  677. then multiplied with a scaling factor scal=4.5/(10^qmax) to yield
  678. normalized samples.
  679. state_frgqTbl[64] = {1.000085, 1.071695, 1.140395, 1.206868,
  680. 1.277188, 1.351503, 1.429380, 1.500727, 1.569049,
  681. 1.639599, 1.707071, 1.781531, 1.840799, 1.901550,
  682. 1.956695, 2.006750, 2.055474, 2.102787, 2.142819,
  683. 2.183592, 2.217962, 2.257177, 2.295739, 2.332967,
  684. 2.369248, 2.402792, 2.435080, 2.468598, 2.503394,
  685. 2.539284, 2.572944, 2.605036, 2.636331, 2.668939,
  686. 2.698780, 2.729101, 2.759786, 2.789834, 2.818679,
  687. 2.848074, 2.877470, 2.906899, 2.936655, 2.967804,
  688. 3.000115, 3.033367, 3.066355, 3.104231, 3.141499,
  689. 3.183012, 3.222952, 3.265433, 3.308441, 3.350823,
  690. 3.395275, 3.442793, 3.490801, 3.542514, 3.604064,
  691. 3.666050, 3.740994, 3.830749, 3.938770, 4.101764}
  692. 3.5.3. Scalar Quantization
  693. The normalized samples are quantized in the perceptually weighted
  694. speech domain by a sample-by-sample scalar DPCM quantization as
  695. depicted in Figure 3.3. Each sample in the block is filtered by a
  696. weighting filter Wk(z), specified in section 3.4, to form a weighted
  697. speech sample x[n]. The target sample d[n] is formed by subtracting
  698. a predicted sample y[n], where the prediction filter is given by
  699. Pk(z) = 1 - 1 / Wk(z).
  700. +-------+ x[n] + d[n] +-----------+ u[n]
  701. residual -->| Wk(z) |-------->(+)---->| Quantizer |------> quantized
  702. +-------+ - /|\ +-----------+ | residual
  703. | \|/
  704. y[n] +--------------------->(+)
  705. | |
  706. | +------+ |
  707. +--------| Pk(z)|<------+
  708. +------+
  709. Figure 3.3. Quantization of start state samples by DPCM in weighted
  710. speech domain.
  711. The coded state sample u[n] is obtained by quantizing d[n] with a 3-
  712. bit quantizer with quantization table state_sq3Tbl.
  713. state_sq3Tbl[8] = {-3.719849, -2.177490, -1.130005, -0.309692,
  714. 0.444214, 1.329712, 2.436279, 3.983887}
  715. Andersen, et al. Experimental [Page 18]
  716. RFC 3951 Internet Low Bit Rate Codec December 2004
  717. The quantized samples are transformed back to the residual domain by
  718. 1) scaling with 1/scal; 2) time-reversing the scaled samples; 3)
  719. filtering the time-reversed samples by the same all-pass filter, as
  720. in section 3.5.2, by using circular convolution; and 4) time-
  721. reversing the filtered samples. (More detail is in section 4.2.)
  722. A reference implementation of the start-state encoding can be found
  723. in Appendix A.46.
  724. 3.6. Encoding the Remaining Samples
  725. A dynamic codebook is used to encode 1) the 23/22 remaining samples
  726. in the two sub-blocks containing the start state; 2) the sub-blocks
  727. after the start state in time; and 3) the sub-blocks before the start
  728. state in time. Thus, the encoding target can be either the 23/22
  729. samples remaining of the 2 sub-blocks containing the start state, or
  730. a 40-sample sub-block. This target can consist of samples that are
  731. indexed forward in time or backward in time, depending on the
  732. location of the start state. The length of the target is denoted by
  733. lTarget.
  734. The coding is based on an adaptive codebook that is built from a
  735. codebook memory that contains decoded LPC excitation samples from the
  736. already encoded part of the block. These samples are indexed in the
  737. same time direction as is the target vector and end at the sample
  738. instant prior to the first sample instant represented in the target
  739. vector. The codebook memory has length lMem, which is equal to
  740. CB_MEML=147 for the two/four 40-sample sub-blocks and 85 for the
  741. 23/22-sample sub-block.
  742. The following figure shows an overview of the encoding procedure.
  743. +------------+ +---------------+ +-------------+
  744. -> | 1. Decode | -> | 2. Mem setup | -> | 3. Perc. W. | ->
  745. +------------+ +---------------+ +-------------+
  746. +------------+ +-----------------+
  747. -> | 4. Search | -> | 5. Upd. Target | ------------------>
  748. | +------------+ +------------------ |
  749. ----<-------------<-----------<----------
  750. stage=0..2
  751. +----------------+
  752. -> | 6. Recalc G[0] | ---------------> gains and CB indices
  753. +----------------+
  754. Figure 3.4. Flow chart of the codebook search in the iLBC encoder.
  755. Andersen, et al. Experimental [Page 19]
  756. RFC 3951 Internet Low Bit Rate Codec December 2004
  757. 1. Decode the part of the residual that has been encoded so far,
  758. using the codebook without perceptual weighting.
  759. 2. Set up the memory by taking data from the decoded residual. This
  760. memory is used to construct codebooks. For blocks preceding the
  761. start state, both the decoded residual and the target are time
  762. reversed (section 3.6.1).
  763. 3. Filter the memory + target with the perceptual weighting filter
  764. (section 3.6.2).
  765. 4. Search for the best match between the target and the codebook
  766. vector. Compute the optimal gain for this match and quantize that
  767. gain (section 3.6.4).
  768. 5. Update the perceptually weighted target by subtracting the
  769. contribution from the selected codebook vector from the
  770. perceptually weighted memory (quantized gain times selected
  771. vector). Repeat 4 and 5 for the two additional stages.
  772. 6. Calculate the energy loss due to encoding of the residual. If
  773. needed, compensate for this loss by an upscaling and
  774. requantization of the gain for the first stage (section 3.7).
  775. The following sections provide an in-depth description of the
  776. different blocks of Figure 3.4.
  777. 3.6.1. Codebook Memory
  778. The codebook memory is based on the already encoded sub-blocks, so
  779. the available data for encoding increases for each new sub-block that
  780. has been encoded. Until enough sub-blocks have been encoded to fill
  781. the codebook memory with data, it is padded with zeros. The
  782. following figure shows an example of the order in which the sub-
  783. blocks are encoded for the 30 ms frame size if the start state is
  784. located in the last 58 samples of sub-block 2 and 3.
  785. +-----------------------------------------------------+
  786. | 5 | 1 |///|////////| 2 | 3 | 4 |
  787. +-----------------------------------------------------+
  788. Figure 3.5. The order from 1 to 5 in which the sub-blocks are
  789. encoded. The slashed area is the start state.
  790. Andersen, et al. Experimental [Page 20]
  791. RFC 3951 Internet Low Bit Rate Codec December 2004
  792. The first target sub-block to be encoded is number 1, and the
  793. corresponding codebook memory is shown in the following figure. As
  794. the target vector comes before the start state in time, the codebook
  795. memory and target vector are time reversed; thus, after the block has
  796. been time reversed the search algorithm can be reused. As only the
  797. start state has been encoded so far, the last samples of the codebook
  798. memory are padded with zeros.
  799. +-------------------------
  800. |zeros|\\\\\\\\|\\\\| 1 |
  801. +-------------------------
  802. Figure 3.6. The codebook memory, length lMem=85 samples, and the
  803. target vector 1, length 22 samples.
  804. The next step is to encode sub-block 2 by using the memory that now
  805. has increased since sub-block 1 has been encoded. The following
  806. figure shows the codebook memory for encoding of sub-block 2.
  807. +-----------------------------------
  808. | zeros | 1 |///|////////| 2 |
  809. +-----------------------------------
  810. Figure 3.7. The codebook memory, length lMem=147 samples, and the
  811. target vector 2, length 40 samples.
  812. The next step is to encode sub-block 3 by using the memory which has
  813. been increased yet again since sub-blocks 1 and 2 have been encoded,
  814. but the sub-block still has to be padded with a few zeros. The
  815. following figure shows the codebook memory for encoding of sub-block
  816. 3.
  817. +------------------------------------------
  818. |zeros| 1 |///|////////| 2 | 3 |
  819. +------------------------------------------
  820. Figure 3.8. The codebook memory, length lMem=147 samples, and the
  821. target vector 3, length 40 samples.
  822. The next step is to encode sub-block 4 by using the memory which now
  823. has increased yet again since sub-blocks 1, 2, and 3 have been
  824. encoded. This time, the memory does not have to be padded with
  825. zeros. The following figure shows the codebook memory for encoding
  826. of sub-block 4.
  827. Andersen, et al. Experimental [Page 21]
  828. RFC 3951 Internet Low Bit Rate Codec December 2004
  829. +------------------------------------------
  830. |1|///|////////| 2 | 3 | 4 |
  831. +------------------------------------------
  832. Figure 3.9. The codebook memory, length lMem=147 samples, and the
  833. target vector 4, length 40 samples.
  834. The final target sub-block to be encoded is number 5, and the
  835. following figure shows the corresponding codebook memory. As the
  836. target vector comes before the start state in time, the codebook
  837. memory and target vector are time reversed.
  838. +-------------------------------------------
  839. | 3 | 2 |\\\\\\\\|\\\\| 1 | 5 |
  840. +-------------------------------------------
  841. Figure 3.10. The codebook memory, length lMem=147 samples, and the
  842. target vector 5, length 40 samples.
  843. For the case of 20 ms frames, the encoding procedure looks almost
  844. exactly the same. The only difference is that the size of the start
  845. state is 57 samples and that there are only three sub-blocks to be
  846. encoded. The encoding order is the same as above, starting with the
  847. 23-sample target and then encoding the two remaining 40-sample sub-
  848. blocks, first going forward in time and then going backward in time
  849. relative to the start state.
  850. 3.6.2. Perceptual Weighting of Codebook Memory and Target
  851. To provide a perceptual weighting of the coding error, a
  852. concatenation of the codebook memory and the target to be coded is
  853. all-pole filtered with the perceptual weighting filter specified in
  854. section 3.4. The filter state of the weighting filter is set to
  855. zero.
  856. in(0..(lMem-1)) = unweighted codebook memory
  857. in(lMem..(lMem+lTarget-1)) = unweighted target signal
  858. in -> Wk(z) -> filtered,
  859. where Wk(z) is taken from the sub-block of the target
  860. weighted codebook memory = filtered(0..(lMem-1))
  861. weighted target signal = filtered(lMem..(lMem+lTarget-1))
  862. The codebook search is done with the weighted codebook memory and the
  863. weighted target, whereas the decoding and the codebook memory update
  864. uses the unweighted codebook memory.
  865. Andersen, et al. Experimental [Page 22]
  866. RFC 3951 Internet Low Bit Rate Codec December 2004
  867. 3.6.3. Codebook Creation
  868. The codebook for the search is created from the perceptually weighted
  869. codebook memory. It consists of two sections, where the first is
  870. referred to as the base codebook and the second as the expanded
  871. codebook, as it is created by linear combinations of the first. Each
  872. of these two sections also has a subsection referred to as the
  873. augmented codebook. The augmented codebook is only created and used
  874. for the coding of the 40-sample sub-blocks and not for the 23/22-
  875. sample sub-block case. The codebook size used for the different
  876. sub-blocks and different stages are summarized in the table below.
  877. Stage
  878. 1 2 & 3
  879. --------------------------------------------
  880. 22 128 (64+0)*2 128 (64+0)*2
  881. Sub- 1:st 40 256 (108+20)*2 128 (44+20)*2
  882. Blocks 2:nd 40 256 (108+20)*2 256 (108+20)*2
  883. 3:rd 40 256 (108+20)*2 256 (108+20)*2
  884. 4:th 40 256 (108+20)*2 256 (108+20)*2
  885. Table 3.1. Codebook sizes for the 30 ms mode.
  886. Table 3.1 shows the codebook size for the different sub-blocks and
  887. stages for 30 ms frames. Inside the parentheses it shows how the
  888. number of codebook vectors is distributed, within the two sections,
  889. between the base/expanded codebook and the augmented base/expanded
  890. codebook. It should be interpreted in the following way:
  891. (base/expanded cb + augmented base/expanded cb). The total number of
  892. codebook vectors for a specific sub-block and stage is given by the
  893. following formula:
  894. Tot. cb vectors = base cb + aug. base cb + exp. cb + aug. exp. cb
  895. The corresponding values to Figure 3.1 for 20 ms frames are only
  896. slightly modified. The short sub-block is 23 instead of 22 samples,
  897. and the 3:rd and 4:th sub-frame are not present.
  898. 3.6.3.1. Creation of a Base Codebook
  899. The base codebook is given by the perceptually weighted codebook
  900. memory that is mentioned in section 3.5.3. The different codebook
  901. vectors are given by sliding a window of length 23/22 or 40, given by
  902. variable lTarget, over the lMem-long perceptually weighted codebook
  903. memory. The indices are ordered so that the codebook vector
  904. containing sample (lMem-lTarget-n) to (lMem-n-1) of the codebook
  905. Andersen, et al. Experimental [Page 23]
  906. RFC 3951 Internet Low Bit Rate Codec December 2004
  907. memory vector has index n, where n=0..lMem-lTarget. Thus the total
  908. number of base codebook vectors is lMem-lTarget+1, and the indices
  909. are ordered from sample delay lTarget (23/22 or 40) to lMem+1 (86 or
  910. 148).
  911. 3.6.3.2. Codebook Expansion
  912. The base codebook is expanded by a factor of 2, creating an
  913. additional section in the codebook. This new section is obtained by
  914. filtering the base codebook, base_cb, with a FIR filter with filter
  915. length CB_FILTERLEN=8. The construction of the expanded codebook
  916. compensates for the delay of four samples introduced by the FIR
  917. filter.
  918. cbfiltersTbl[CB_FILTERLEN]={-0.033691, 0.083740, -0.144043,
  919. 0.713379, 0.806152, -0.184326,
  920. 0.108887, -0.034180};
  921. ___
  922. \
  923. exp_cb(k)= + > cbfiltersTbl(i)*x(k-i+4)
  924. /__
  925. i=0...(LPC_FILTERORDER-1)
  926. where x(j) = base_cb(j) for j=0..lMem-1 and 0 otherwise
  927. The individual codebook vectors of the new filtered codebook, exp_cb,
  928. and their indices are obtained in the same fashion as described above
  929. for the base codebook.
  930. 3.6.3.3. Codebook Augmentation
  931. For cases where encoding entire sub-blocks, i.e., cbveclen=40, the
  932. base and expanded codebooks are augmented to increase codebook
  933. richness. The codebooks are augmented by vectors produced by
  934. interpolation of segments. The base and expanded codebook,
  935. constructed above, consists of vectors corresponding to sample delays
  936. in the range from cbveclen to lMem. The codebook augmentation
  937. attempts to augment these codebooks with vectors corresponding to
  938. sample delays from 20 to 39. However, not all of these samples are
  939. present in the base codebook and expanded codebook, respectively.
  940. Therefore, the augmentation vectors are constructed as linear
  941. combinations between samples corresponding to sample delays in the
  942. range 20 to 39. The general idea of this procedure is presented in
  943. the following figures and text. The procedure is performed for both
  944. the base codebook and the expanded codebook.
  945. Andersen, et al. Experimental [Page 24]
  946. RFC 3951 Internet Low Bit Rate Codec December 2004
  947. - - ------------------------|
  948. codebook memory |
  949. - - ------------------------|
  950. |-5-|---15---|-5-|
  951. pi pp po
  952. | | Codebook vector
  953. |---15---|-5-|-----20-----| <- corresponding to
  954. i ii iii sample delay 20
  955. Figure 3.11. Generation of the first augmented codebook.
  956. Figure 3.11 shows the codebook memory with pointers pi, pp, and po,
  957. where pi points to sample 25, pp to sample 20, and po to sample 5.
  958. Below the codebook memory, the augmented codebook vector
  959. corresponding to sample delay 20 is drawn. Segment i consists of
  960. fifteen samples from pointer pp and forward in time. Segment ii
  961. consists of five interpolated samples from pi and forward and from po
  962. and forward. The samples are linearly interpolated with weights
  963. [0.0, 0.2, 0.4, 0.6, 0.8] for pi and weights [1.0, 0.8, 0.6, 0.4,
  964. 0.2] for po. Segment iii consists of twenty samples from pp and
  965. forward. The augmented codebook vector corresponding to sample delay
  966. 21 is produced by moving pointers pp and pi one sample backward in
  967. time. This gives us the following figure.
  968. - - ------------------------|
  969. codebook memory |
  970. - - ------------------------|
  971. |-5-|---16---|-5-|
  972. pi pp po
  973. | | Codebook vector
  974. |---16---|-5-|-----19-----| <- corresponding to
  975. i ii iii sample delay 21
  976. Figure 3.12. Generation of the second augmented codebook.
  977. Figure 3.12 shows the codebook memory with pointers pi, pp and po
  978. where pi points to sample 26, pp to sample 21, and po to sample 5.
  979. Below the codebook memory, the augmented codebook vector
  980. corresponding to sample delay 21 is drawn. Segment i now consists of
  981. sixteen samples from pp and forward. Segment ii consists of five
  982. interpolated samples from pi and forward and from po and forward, and
  983. the interpolation weights are the same throughout the procedure.
  984. Segment iii consists of nineteen samples from pp and forward. The
  985. same procedure of moving the two pointers is continued until the last
  986. augmented vector corresponding to sample delay 39 has been created.
  987. This gives a total of twenty new codebook vectors to each of the two
  988. Andersen, et al. Experimental [Page 25]
  989. RFC 3951 Internet Low Bit Rate Codec December 2004
  990. sections. Thus the total number of codebook vectors for each of the
  991. two sections, when including the augmented codebook, becomes lMem-
  992. SUBL+1+SUBL/2. This is provided that augmentation is evoked, i.e.,
  993. that lTarget=SUBL.
  994. 3.6.4. Codebook Search
  995. The codebook search uses the codebooks described in the sections
  996. above to find the best match of the perceptually weighted target, see
  997. section 3.6.2. The search method is a multi-stage gain-shape
  998. matching performed as follows. At each stage the best shape vector
  999. is identified, then the gain is calculated and quantized, and finally
  1000. the target is updated in preparation for the next codebook search
  1001. stage. The number of stages is CB_NSTAGES=3.
  1002. If the target is the 23/22-sample vector the codebooks are indexed so
  1003. that the base codebook is followed by the expanded codebook. If the
  1004. target is 40 samples the order is as follows: base codebook,
  1005. augmented base codebook, expanded codebook, and augmented expanded
  1006. codebook. The size of each codebook section and its corresponding
  1007. augmented section is given by Table 3.1 in section 3.6.3.
  1008. For example, when the second 40-sample sub-block is coded, indices 0
  1009. - 107 correspond to the base codebook, 108 - 127 correspond to the
  1010. augmented base codebook, 128 - 235 correspond to the expanded
  1011. codebook, and indices 236 - 255 correspond to the augmented expanded
  1012. codebook. The indices are divided in the same fashion for all stages
  1013. in the example. Only in the case of coding the first 40-sample sub-
  1014. block is there a difference between stages (see Table 3.1).
  1015. 3.6.4.1. Codebook Search at Each Stage
  1016. The codebooks are searched to find the best match to the target at
  1017. each stage. When the best match is found, the target is updated and
  1018. the next-stage search is started. The three chosen codebook vectors
  1019. and their corresponding gains constitute the encoded sub-block. The
  1020. best match is decided by the following three criteria:
  1021. 1. Compute the measure
  1022. (target*cbvec)^2 / ||cbvec||^2
  1023. for all codebook vectors, cbvec, and choose the codebook vector
  1024. maximizing the measure. The expression (target*cbvec) is the dot
  1025. product between the target vector to be coded and the codebook vector
  1026. for which we compute the measure. The norm, ||x||, is defined as the
  1027. square root of (x*x).
  1028. Andersen, et al. Experimental [Page 26]
  1029. RFC 3951 Internet Low Bit Rate Codec December 2004
  1030. 2. The absolute value of the gain, corresponding to the chosen
  1031. codebook vector, cbvec, must be smaller than a fixed limit,
  1032. CB_MAXGAIN=1.3:
  1033. |gain| < CB_MAXGAIN
  1034. where the gain is computed in the following way:
  1035. gain = (target*cbvec) / ||cbvec||^2
  1036. 3. For the first stage, the dot product of the chosen codebook vector
  1037. and target must be positive:
  1038. target*cbvec > 0
  1039. In practice the above criteria are used in a sequential search
  1040. through all codebook vectors. The best match is found by registering
  1041. a new max measure and index whenever the previously registered max
  1042. measure is surpassed and all other criteria are fulfilled. If none
  1043. of the codebook vectors fulfill (2) and (3), the first codebook
  1044. vector is selected.
  1045. 3.6.4.2. Gain Quantization at Each Stage
  1046. The gain follows as a result of the computation
  1047. gain = (target*cbvec) / ||cbvec||^2
  1048. for the optimal codebook vector found by the procedure in section
  1049. 3.6.4.1.
  1050. The three stages quantize the gain, using 5, 4, and 3 bits,
  1051. respectively. In the first stage, the gain is limited to positive
  1052. values. This gain is quantized by finding the nearest value in the
  1053. quantization table gain_sq5Tbl.
  1054. gain_sq5Tbl[32]={0.037476, 0.075012, 0.112488, 0.150024, 0.187500,
  1055. 0.224976, 0.262512, 0.299988, 0.337524, 0.375000,
  1056. 0.412476, 0.450012, 0.487488, 0.525024, 0.562500,
  1057. 0.599976, 0.637512, 0.674988, 0.712524, 0.750000,
  1058. 0.787476, 0.825012, 0.862488, 0.900024, 0.937500,
  1059. 0.974976, 1.012512, 1.049988, 1.087524, 1.125000,
  1060. 1.162476, 1.200012}
  1061. The gains of the subsequent two stages can be either positive or
  1062. negative. The gains are quantized by using a quantization table
  1063. times a scale factor. The second stage uses the table gain_sq4Tbl,
  1064. and the third stage uses gain_sq3Tbl. The scale factor equates 0.1
  1065. Andersen, et al. Experimental [Page 27]
  1066. RFC 3951 Internet Low Bit Rate Codec December 2004
  1067. or the absolute value of the quantized gain representation value
  1068. obtained in the previous stage, whichever is larger. Again, the
  1069. resulting gain index is the index to the nearest value of the
  1070. quantization table times the scale factor.
  1071. gainQ = scaleFact * gain_sqXTbl[index]
  1072. gain_sq4Tbl[16]={-1.049988, -0.900024, -0.750000, -0.599976,
  1073. -0.450012, -0.299988, -0.150024, 0.000000, 0.150024,
  1074. 0.299988, 0.450012, 0.599976, 0.750000, 0.900024,
  1075. 1.049988, 1.200012}
  1076. gain_sq3Tbl[8]={-1.000000, -0.659973, -0.330017,0.000000,
  1077. 0.250000, 0.500000, 0.750000, 1.00000}
  1078. 3.6.4.3. Preparation of Target for Next Stage
  1079. Before performing the search for the next stage, the perceptually
  1080. weighted target vector is updated by subtracting from it the selected
  1081. codebook vector (from the perceptually weighted codebook) times the
  1082. corresponding quantized gain.
  1083. target[i] = target[i] - gainQ * selected_vec[i];
  1084. A reference implementation of the codebook encoding is found in
  1085. Appendix A.34.
  1086. 3.7. Gain Correction Encoding
  1087. The start state is quantized in a relatively model independent manner
  1088. using 3 bits per sample. In contrast, the remaining parts of the
  1089. block are encoded by using an adaptive codebook. This codebook will
  1090. produce high matching accuracy whenever there is a high correlation
  1091. between the target and the best codebook vector. For unvoiced speech
  1092. segments and background noises, this is not necessarily so, which,
  1093. due to the nature of the squared error criterion, results in a coded
  1094. signal with less power than the target signal. As the coded start
  1095. state has good power matching to the target, the result is a power
  1096. fluctuation within the encoded frame. Perceptually, the main problem
  1097. with this is that the time envelope of the signal energy becomes
  1098. unsteady. To overcome this problem, the gains for the codebooks are
  1099. re-scaled after the codebook encoding by searching for a new gain
  1100. factor for the first stage codebook that provides better power
  1101. matching.
  1102. First, the energy for the target signal, tene, is computed along with
  1103. the energy for the coded signal, cene, given by the addition of the
  1104. three gain scaled codebook vectors. Because the gains of the second
  1105. Andersen, et al. Experimental [Page 28]
  1106. RFC 3951 Internet Low Bit Rate Codec December 2004
  1107. and third stage scale with the gain of the first stage, when the
  1108. first stage gain is changed from gain[0] to gain_sq5Tbl[i] the energy
  1109. of the coded signal changes from cene to
  1110. cene*(gain_sq5Tbl[i]*gain_sq5Tbl[i])/(gain[0]*gain[0])
  1111. where gain[0] is the gain for the first stage found in the original
  1112. codebook search. A refined search is performed by testing the gain
  1113. indices i=0 to 31, and as long as the new codebook energy as given
  1114. above is less than tene, the gain index for stage 1 is increased. A
  1115. restriction is applied so that the new gain value for stage 1 cannot
  1116. be more than two times higher than the original value found in the
  1117. codebook search. Note that by using this method we do not change the
  1118. shape of the encoded vector, only the gain or amplitude.
  1119. 3.8. Bitstream Definition
  1120. The total number of bits used to describe one frame of 20 ms speech
  1121. is 304, which fits in 38 bytes and results in a bit rate of 15.20
  1122. kbit/s. For the case of a frame length of 30 ms speech, the total
  1123. number of bits used is 400, which fits in 50 bytes and results in a
  1124. bit rate of 13.33 kbit/s. In the bitstream definition, the bits are
  1125. distributed into three classes according to their bit error or loss
  1126. sensitivity. The most sensitive bits (class 1) are placed first in
  1127. the bitstream for each frame. The less sensitive bits (class 2) are
  1128. placed after the class 1 bits. The least sensitive bits (class 3)
  1129. are placed at the end of the bitstream for each frame.
  1130. In the 20/30 ms frame length cases for each class, the following hold
  1131. true: The class 1 bits occupy a total of 6/8 bytes (48/64 bits), the
  1132. class 2 bits occupy 8/12 bytes (64/96 bits), and the class 3 bits
  1133. occupy 24/30 bytes (191/239 bits). This distribution of the bits
  1134. enables the use of uneven level protection (ULP) as is exploited in
  1135. the payload format definition for iLBC [1]. The detailed bit
  1136. allocation is shown in the table below. When a quantization index is
  1137. distributed between more classes, the more significant bits belong to
  1138. the lowest class.
  1139. Andersen, et al. Experimental [Page 29]
  1140. RFC 3951 Internet Low Bit Rate Codec December 2004
  1141. Bitstream structure:
  1142. ------------------------------------------------------------------+
  1143. Parameter | Bits Class <1,2,3> |
  1144. | 20 ms frame | 30 ms frame |
  1145. ----------------------------------+---------------+---------------+
  1146. Split 1 | 6 <6,0,0> | 6 <6,0,0> |
  1147. LSF 1 Split 2 | 7 <7,0,0> | 7 <7,0,0> |
  1148. LSF Split 3 | 7 <7,0,0> | 7 <7,0,0> |
  1149. ------------------+---------------+---------------+
  1150. Split 1 | NA (Not Appl.)| 6 <6,0,0> |
  1151. LSF 2 Split 2 | NA | 7 <7,0,0> |
  1152. Split 3 | NA | 7 <7,0,0> |
  1153. ------------------+---------------+---------------+
  1154. Sum | 20 <20,0,0> | 40 <40,0,0> |
  1155. ----------------------------------+---------------+---------------+
  1156. Block Class | 2 <2,0,0> | 3 <3,0,0> |
  1157. ----------------------------------+---------------+---------------+
  1158. Position 22 sample segment | 1 <1,0,0> | 1 <1,0,0> |
  1159. ----------------------------------+---------------+---------------+
  1160. Scale Factor State Coder | 6 <6,0,0> | 6 <6,0,0> |
  1161. ----------------------------------+---------------+---------------+
  1162. Sample 0 | 3 <0,1,2> | 3 <0,1,2> |
  1163. Quantized Sample 1 | 3 <0,1,2> | 3 <0,1,2> |
  1164. Residual : | : : | : : |
  1165. State : | : : | : : |
  1166. Samples : | : : | : : |
  1167. Sample 56 | 3 <0,1,2> | 3 <0,1,2> |
  1168. Sample 57 | NA | 3 <0,1,2> |
  1169. ------------------+---------------+---------------+
  1170. Sum | 171 <0,57,114>| 174 <0,58,116>|
  1171. ----------------------------------+---------------+---------------+
  1172. Stage 1 | 7 <6,0,1> | 7 <4,2,1> |
  1173. CB for 22/23 Stage 2 | 7 <0,0,7> | 7 <0,0,7> |
  1174. sample block Stage 3 | 7 <0,0,7> | 7 <0,0,7> |
  1175. ------------------+---------------+---------------+
  1176. Sum | 21 <6,0,15> | 21 <4,2,15> |
  1177. ----------------------------------+---------------+---------------+
  1178. Stage 1 | 5 <2,0,3> | 5 <1,1,3> |
  1179. Gain for 22/23 Stage 2 | 4 <1,1,2> | 4 <1,1,2> |
  1180. sample block Stage 3 | 3 <0,0,3> | 3 <0,0,3> |
  1181. ------------------+---------------+---------------+
  1182. Sum | 12 <3,1,8> | 12 <2,2,8> |
  1183. ----------------------------------+---------------+---------------+
  1184. Stage 1 | 8 <7,0,1> | 8 <6,1,1> |
  1185. sub-block 1 Stage 2 | 7 <0,0,7> | 7 <0,0,7> |
  1186. Stage 3 | 7 <0,0,7> | 7 <0,0,7> |
  1187. ------------------+---------------+---------------+
  1188. Andersen, et al. Experimental [Page 30]
  1189. RFC 3951 Internet Low Bit Rate Codec December 2004
  1190. Stage 1 | 8 <0,0,8> | 8 <0,7,1> |
  1191. sub-block 2 Stage 2 | 8 <0,0,8> | 8 <0,0,8> |
  1192. Indices Stage 3 | 8 <0,0,8> | 8 <0,0,8> |
  1193. for CB ------------------+---------------+---------------+
  1194. sub-blocks Stage 1 | NA | 8 <0,7,1> |
  1195. sub-block 3 Stage 2 | NA | 8 <0,0,8> |
  1196. Stage 3 | NA | 8 <0,0,8> |
  1197. ------------------+---------------+---------------+
  1198. Stage 1 | NA | 8 <0,7,1> |
  1199. sub-block 4 Stage 2 | NA | 8 <0,0,8> |
  1200. Stage 3 | NA | 8 <0,0,8> |
  1201. ------------------+---------------+---------------+
  1202. Sum | 46 <7,0,39> | 94 <6,22,66> |
  1203. ----------------------------------+---------------+---------------+
  1204. Stage 1 | 5 <1,2,2> | 5 <1,2,2> |
  1205. sub-block 1 Stage 2 | 4 <1,1,2> | 4 <1,2,1> |
  1206. Stage 3 | 3 <0,0,3> | 3 <0,0,3> |
  1207. ------------------+---------------+---------------+
  1208. Stage 1 | 5 <1,1,3> | 5 <0,2,3> |
  1209. sub-block 2 Stage 2 | 4 <0,2,2> | 4 <0,2,2> |
  1210. Stage 3 | 3 <0,0,3> | 3 <0,0,3> |
  1211. Gains for ------------------+---------------+---------------+
  1212. sub-blocks Stage 1 | NA | 5 <0,1,4> |
  1213. sub-block 3 Stage 2 | NA | 4 <0,1,3> |
  1214. Stage 3 | NA | 3 <0,0,3> |
  1215. ------------------+---------------+---------------+
  1216. Stage 1 | NA | 5 <0,1,4> |
  1217. sub-block 4 Stage 2 | NA | 4 <0,1,3> |
  1218. Stage 3 | NA | 3 <0,0,3> |
  1219. ------------------+---------------+---------------+
  1220. Sum | 24 <3,6,15> | 48 <2,12,34> |
  1221. ----------------------------------+---------------+---------------+
  1222. Empty frame indicator | 1 <0,0,1> | 1 <0,0,1> |
  1223. -------------------------------------------------------------------
  1224. SUM 304 <48,64,192> 400 <64,96,240>
  1225. Table 3.2. The bitstream definition for iLBC for both the 20 ms
  1226. frame size mode and the 30 ms frame size mode.
  1227. When packetized into the payload, the bits MUST be sorted as follows:
  1228. All the class 1 bits in the order (from top to bottom) as specified
  1229. in the table, all the class 2 bits (from top to bottom), and all the
  1230. class 3 bits in the same sequential order. The last bit, the empty
  1231. frame indicator, SHOULD be set to zero by the encoder. If this bit
  1232. is set to 1 the decoder SHOULD treat the data as a lost frame. For
  1233. example, this bit can be set to 1 to indicate lost frame for file
  1234. storage format, as in [1].
  1235. Andersen, et al. Experimental [Page 31]
  1236. RFC 3951 Internet Low Bit Rate Codec December 2004
  1237. 4. Decoder Principles
  1238. This section describes the principles of each component of the
  1239. decoder algorithm.
  1240. +-------------+ +--------+ +---------------+
  1241. payload -> | 1. Get para | -> | 2. LPC | -> | 3. Sc Dequant | ->
  1242. +-------------+ +--------+ +---------------+
  1243. +-------------+ +------------------+
  1244. -> | 4. Mem setup| -> | 5. Construct res |------->
  1245. | +-------------+ +------------------- |
  1246. ---------<-----------<-----------<------------
  1247. Sub-frame 0...2/4 (20 ms/30 ms)
  1248. +----------------+ +----------+
  1249. -> | 6. Enhance res | -> | 7. Synth | ------------>
  1250. +----------------+ +----------+
  1251. +-----------------+
  1252. -> | 8. Post Process | ----------------> decoded speech
  1253. +-----------------+
  1254. Figure 4.1. Flow chart of the iLBC decoder. If a frame was lost,
  1255. steps 1 to 5 SHOULD be replaced by a PLC algorithm.
  1256. 1. Extract the parameters from the bitstream.
  1257. 2. Decode the LPC and interpolate (section 4.1).
  1258. 3. Construct the 57/58-sample start state (section 4.2).
  1259. 4. Set up the memory by using data from the decoded residual. This
  1260. memory is used for codebook construction. For blocks preceding
  1261. the start state, both the decoded residual and the target are time
  1262. reversed. Sub-frames are decoded in the same order as they were
  1263. encoded.
  1264. 5. Construct the residuals of this sub-frame (gain[0]*cbvec[0] +
  1265. gain[1]*cbvec[1] + gain[2]*cbvec[2]). Repeat 4 and 5 until the
  1266. residual of all sub-blocks has been constructed.
  1267. 6. Enhance the residual with the post filter (section 4.6).
  1268. 7. Synthesis of the residual (section 4.7).
  1269. 8. Post process with HP filter, if desired (section 4.8).
  1270. Andersen, et al. Experimental [Page 32]
  1271. RFC 3951 Internet Low Bit Rate Codec December 2004
  1272. 4.1. LPC Filter Reconstruction
  1273. The decoding of the LP filter parameters is very straightforward.
  1274. For a set of three/six indices, the corresponding LSF vector(s) are
  1275. found by simple table lookup. For each of the LSF vectors, the three
  1276. split vectors are concatenated to obtain qlsf1 and qlsf2,
  1277. respectively (in the 20 ms mode only one LSF vector, qlsf, is
  1278. constructed). The next step is the stability check described in
  1279. section 3.2.5 followed by the interpolation scheme described in
  1280. section 3.2.6 (3.2.7 for 20 ms frames). The only difference is that
  1281. only the quantized LSFs are known at the decoder, and hence the
  1282. unquantized LSFs are not processed.
  1283. A reference implementation of the LPC filter reconstruction is given
  1284. in Appendix A.36.
  1285. 4.2. Start State Reconstruction
  1286. The scalar encoded STATE_SHORT_LEN=58 (STATE_SHORT_LEN=57 in the 20
  1287. ms mode) state samples are reconstructed by 1) forming a set of
  1288. samples (by table lookup) from the index stream idxVec[n], 2)
  1289. multiplying the set with 1/scal=(10^qmax)/4.5, 3) time reversing the
  1290. 57/58 samples, 4) filtering the time reversed block with the
  1291. dispersion (all-pass) filter used in the encoder (as described in
  1292. section 3.5.2); this compensates for the phase distortion of the
  1293. earlier filter operation, and 5 reversing the 57/58 samples from the
  1294. previous step.
  1295. in(0..(STATE_SHORT_LEN-1)) = time reversed samples from table
  1296. look-up,
  1297. idxVecDec((STATE_SHORT_LEN-1)..0)
  1298. in(STATE_SHORT_LEN..(2*STATE_SHORT_LEN-1)) = 0
  1299. Pk(z) = A~rk(z)/A~k(z), where
  1300. ___
  1301. \
  1302. A~rk(z)= z^(-LPC_FILTERORDER) + > a~ki*z^(i-(LPC_FILTERORDER-1))
  1303. /__
  1304. i=0...(LPC_FILTERORDER-1)
  1305. and A~k(z) is taken from the block where the start state begins
  1306. in -> Pk(z) -> filtered
  1307. out(k) = filtered(STATE_SHORT_LEN-1-k) +
  1308. filtered(2*STATE_SHORT_LEN-1-k),
  1309. k=0..(STATE_SHORT_LEN-1)
  1310. Andersen, et al. Experimental [Page 33]
  1311. RFC 3951 Internet Low Bit Rate Codec December 2004
  1312. The remaining 23/22 samples in the state are reconstructed by the
  1313. same adaptive codebook technique described in section 4.3. The
  1314. location bit determines whether these are the first or the last 23/22
  1315. samples of the 80-sample state vector. If the remaining 23/22
  1316. samples are the first samples, then the scalar encoded
  1317. STATE_SHORT_LEN state samples are time-reversed before initialization
  1318. of the adaptive codebook memory vector.
  1319. A reference implementation of the start state reconstruction is given
  1320. in Appendix A.44.
  1321. 4.3. Excitation Decoding Loop
  1322. The decoding of the LPC excitation vector proceeds in the same order
  1323. in which the residual was encoded at the encoder. That is, after the
  1324. decoding of the entire 80-sample state vector, the forward sub-blocks
  1325. (corresponding to samples occurring after the state vector samples)
  1326. are decoded, and then the backward sub-blocks (corresponding to
  1327. samples occurring before the state vector) are decoded, resulting in
  1328. a fully decoded block of excitation signal samples.
  1329. In particular, each sub-block is decoded by using the multistage
  1330. adaptive codebook decoding module described in section 4.4. This
  1331. module relies upon an adaptive codebook memory constructed before
  1332. each run of the adaptive codebook decoding. The construction of the
  1333. adaptive codebook memory in the decoder is identical to the method
  1334. outlined in section 3.6.3, except that it is done on the codebook
  1335. memory without perceptual weighting.
  1336. For the initial forward sub-block, the last STATE_LEN=80 samples of
  1337. the length CB_LMEM=147 adaptive codebook memory are filled with the
  1338. samples of the state vector. For subsequent forward sub-blocks, the
  1339. first SUBL=40 samples of the adaptive codebook memory are discarded,
  1340. the remaining samples are shifted by SUBL samples toward the
  1341. beginning of the vector, and the newly decoded SUBL=40 samples are
  1342. placed at the end of the adaptive codebook memory. For backward
  1343. sub-blocks, the construction is similar, except that every vector of
  1344. samples involved is first time reversed.
  1345. A reference implementation of the excitation decoding loop is found
  1346. in Appendix A.5.
  1347. Andersen, et al. Experimental [Page 34]
  1348. RFC 3951 Internet Low Bit Rate Codec December 2004
  1349. 4.4. Multistage Adaptive Codebook Decoding
  1350. The Multistage Adaptive Codebook Decoding module is used at both the
  1351. sender (encoder) and the receiver (decoder) ends to produce a
  1352. synthetic signal in the residual domain that is eventually used to
  1353. produce synthetic speech. The module takes the index values used to
  1354. construct vectors that are scaled and summed together to produce a
  1355. synthetic signal that is the output of the module.
  1356. 4.4.1. Construction of the Decoded Excitation Signal
  1357. The unpacked index values provided at the input to the module are
  1358. references to extended codebooks, which are constructed as described
  1359. in section 3.6.3, except that they are based on the codebook memory
  1360. without the perceptual weighting. The unpacked three indices are
  1361. used to look up three codebook vectors. The unpacked three gain
  1362. indices are used to decode the corresponding 3 gains. In this
  1363. decoding, the successive rescaling, as described in section 3.6.4.2,
  1364. is applied.
  1365. A reference implementation of the adaptive codebook decoding is
  1366. listed in Appendix A.32.
  1367. 4.5. Packet Loss Concealment
  1368. If packet loss occurs, the decoder receives a signal saying that
  1369. information regarding a block is lost. For such blocks it is
  1370. RECOMMENDED to use a Packet Loss Concealment (PLC) unit to create a
  1371. decoded signal that masks the effect of that packet loss. In the
  1372. following we will describe an example of a PLC unit that can be used
  1373. with the iLBC codec. As the PLC unit is used only at the decoder,
  1374. the PLC unit does not affect interoperability between
  1375. implementations. Other PLC implementations MAY therefore be used.
  1376. The PLC described operates on the LP filters and the excitation
  1377. signals and is based on the following principles:
  1378. 4.5.1. Block Received Correctly and Previous Block Also Received
  1379. If the block is received correctly, the PLC only records state
  1380. information of the current block that can be used in case the next
  1381. block is lost. The LP filter coefficients for each sub-block and the
  1382. entire decoded excitation signal are all saved in the decoder state
  1383. structure. All of this information will be needed if the following
  1384. block is lost.
  1385. Andersen, et al. Experimental [Page 35]
  1386. RFC 3951 Internet Low Bit Rate Codec December 2004
  1387. 4.5.2. Block Not Received
  1388. If the block is not received, the block substitution is based on a
  1389. pitch-synchronous repetition of the excitation signal, which is
  1390. filtered by the last LP filter of the previous block. The previous
  1391. block's information is stored in the decoder state structure.
  1392. A correlation analysis is performed on the previous block's
  1393. excitation signal in order to detect the amount of pitch periodicity
  1394. and a pitch value. The correlation measure is also used to decide on
  1395. the voicing level (the degree to which the previous block's
  1396. excitation was a voiced or roughly periodic signal). The excitation
  1397. in the previous block is used to create an excitation for the block
  1398. to be substituted, such that the pitch of the previous block is
  1399. maintained. Therefore, the new excitation is constructed in a
  1400. pitch-synchronous manner. In order to avoid a buzzy-sounding
  1401. substituted block, a random excitation is mixed with the new pitch
  1402. periodic excitation, and the relative use of the two components is
  1403. computed from the correlation measure (voicing level).
  1404. For the block to be substituted, the newly constructed excitation
  1405. signal is then passed through the LP filter to produce the speech
  1406. that will be substituted for the lost block.
  1407. For several consecutive lost blocks, the packet loss concealment
  1408. continues in a similar manner. The correlation measure of the last
  1409. block received is still used along with the same pitch value. The LP
  1410. filters of the last block received are also used again. The energy
  1411. of the substituted excitation for consecutive lost blocks is
  1412. decreased, leading to a dampened excitation, and therefore to
  1413. dampened speech.
  1414. 4.5.3. Block Received Correctly When Previous Block Not Received
  1415. For the case in which a block is received correctly when the previous
  1416. block was not, the correctly received block's directly decoded speech
  1417. (based solely on the received block) is not used as the actual
  1418. output. The reason for this is that the directly decoded speech does
  1419. not necessarily smoothly merge into the synthetic speech generated
  1420. for the previous lost block. If the two signals are not smoothly
  1421. merged, an audible discontinuity is accidentally produced.
  1422. Therefore, a correlation analysis between the two blocks of
  1423. excitation signal (the excitation of the previous concealed block and
  1424. that of the current received block) is performed to find the best
  1425. phase match. Then a simple overlap-add procedure is performed to
  1426. merge the previous excitation smoothly into the current block's
  1427. excitation.
  1428. Andersen, et al. Experimental [Page 36]
  1429. RFC 3951 Internet Low Bit Rate Codec December 2004
  1430. The exact implementation of the packet loss concealment does not
  1431. influence interoperability of the codec.
  1432. A reference implementation of the packet loss concealment is
  1433. suggested in Appendix A.14. Exact compliance with this suggested
  1434. algorithm is not needed for a reference implementation to be fully
  1435. compatible with the overall codec specification.
  1436. 4.6. Enhancement
  1437. The decoder contains an enhancement unit that operates on the
  1438. reconstructed excitation signal. The enhancement unit increases the
  1439. perceptual quality of the reconstructed signal by reducing the
  1440. speech-correlated noise in the voiced speech segments. Compared to
  1441. traditional postfilters, the enhancer has an advantage in that it can
  1442. only modify the excitation signal slightly. This means that there is
  1443. no risk of over enhancement. The enhancer works very similarly for
  1444. both the 20 ms frame size mode and the 30 ms frame size mode.
  1445. For the mode with 20 ms frame size, the enhancer uses a memory of six
  1446. 80-sample excitation blocks prior in time plus the two new 80-sample
  1447. excitation blocks. For each block of 160 new unenhanced excitation
  1448. samples, 160 enhanced excitation samples are produced. The enhanced
  1449. excitation is 40-sample delayed compared to the unenhanced
  1450. excitation, as the enhancer algorithm uses lookahead.
  1451. For the mode with 30 ms frame size, the enhancer uses a memory of
  1452. five 80-sample excitation blocks prior in time plus the three new
  1453. 80-sample excitation blocks. For each block of 240 new unenhanced
  1454. excitation samples, 240 enhanced excitation samples are produced.
  1455. The enhanced excitation is 80-sample delayed compared to the
  1456. unenhanced excitation, as the enhancer algorithm uses lookahead.
  1457. Outline of Enhancer
  1458. The speech enhancement unit operates on sub-blocks of 80 samples,
  1459. which means that there are two/three 80 sample sub-blocks per frame.
  1460. Each of these two/three sub-blocks is enhanced separately, but in an
  1461. analogous manner.
  1462. Andersen, et al. Experimental [Page 37]
  1463. RFC 3951 Internet Low Bit Rate Codec December 2004
  1464. unenhanced residual
  1465. |
  1466. | +---------------+ +--------------+
  1467. +-> | 1. Pitch Est | -> | 2. Find PSSQ | -------->
  1468. +---------------+ | +--------------+
  1469. +-----<-------<------<--+
  1470. +------------+ enh block 0..1/2 |
  1471. -> | 3. Smooth | |
  1472. +------------+ |
  1473. \ |
  1474. /\ |
  1475. / \ Already |
  1476. / 4. \----------->----------->-----------+ |
  1477. \Crit/ Fulfilled | |
  1478. \? / v |
  1479. \/ | |
  1480. \ +-----------------+ +---------+ | |
  1481. Not +->| 5. Use Constr. | -> | 6. Mix | ----->
  1482. Fulfilled +-----------------+ +---------+
  1483. ---------------> enhanced residual
  1484. Figure 4.2. Flow chart of the enhancer.
  1485. 1. Pitch estimation of each of the two/three new 80-sample blocks.
  1486. 2. Find the pitch-period-synchronous sequence n (for block k) by a
  1487. search around the estimated pitch value. Do this for n=1,2,3,
  1488. -1,-2,-3.
  1489. 3. Calculate the smoothed residual generated by the six pitch-
  1490. period-synchronous sequences from prior step.
  1491. 4. Check if the smoothed residual satisfies the criterion (section
  1492. 4.6.4).
  1493. 5. Use constraint to calculate mixing factor (section 4.6.5).
  1494. 6. Mix smoothed signal with unenhanced residual (pssq(n) n=0).
  1495. The main idea of the enhancer is to find three 80 sample blocks
  1496. before and three 80-sample blocks after the analyzed unenhanced sub-
  1497. block and to use these to improve the quality of the excitation in
  1498. that sub-block. The six blocks are chosen so that they have the
  1499. highest possible correlation with the unenhanced sub-block that is
  1500. being enhanced. In other words, the six blocks are pitch-period-
  1501. synchronous sequences to the unenhanced sub-block.
  1502. Andersen, et al. Experimental [Page 38]
  1503. RFC 3951 Internet Low Bit Rate Codec December 2004
  1504. A linear combination of the six pitch-period-synchronous sequences is
  1505. calculated that approximates the sub-block. If the squared error
  1506. between the approximation and the unenhanced sub-block is small
  1507. enough, the enhanced residual is set equal to this approximation.
  1508. For the cases when the squared error criterion is not fulfilled, a
  1509. linear combination of the approximation and the unenhanced residual
  1510. forms the enhanced residual.
  1511. 4.6.1. Estimating the Pitch
  1512. Pitch estimates are needed to determine the locations of the pitch-
  1513. period-synchronous sequences in a complexity-efficient way. For each
  1514. of the new two/three sub-blocks, a pitch estimate is calculated by
  1515. finding the maximum correlation in the range from lag 20 to lag 120.
  1516. These pitch estimates are used to narrow down the search for the best
  1517. possible pitch-period-synchronous sequences.
  1518. 4.6.2. Determination of the Pitch-Synchronous Sequences
  1519. Upon receiving the pitch estimates from the prior step, the enhancer
  1520. analyzes and enhances one 80-sample sub-block at a time. The pitch-
  1521. period-synchronous-sequences pssq(n) can be viewed as vectors of
  1522. length 80 samples each shifted n*lag samples from the current sub-
  1523. block. The six pitch-period-synchronous-sequences, pssq(-3) to
  1524. pssq(-1) and pssq(1) to pssq(3), are found one at a time by the steps
  1525. below:
  1526. 1) Calculate the estimate of the position of the pssq(n). For
  1527. pssq(n) in front of pssq(0) (n > 0), the location of the pssq(n)
  1528. is estimated by moving one pitch estimate forward in time from the
  1529. exact location of pssq(n-1). Similarly, pssq(n) behind pssq(0) (n
  1530. < 0) is estimated by moving one pitch estimate backward in time
  1531. from the exact location of pssq(n+1). If the estimated pssq(n)
  1532. vector location is totally within the enhancer memory (Figure
  1533. 4.3), steps 2, 3, and 4 are performed, otherwise the pssq(n) is
  1534. set to zeros.
  1535. 2) Compute the correlation between the unenhanced excitation and
  1536. vectors around the estimated location interval of pssq(n). The
  1537. correlation is calculated in the interval estimated location +/- 2
  1538. samples. This results in five correlation values.
  1539. 3) The five correlation values are upsampled by a factor of 4, by
  1540. using four simple upsampling filters (MA filters with coefficients
  1541. upsFilter1.. upsFilter4). Within these the maximum value is
  1542. found, which specifies the best pitch-period with a resolution of
  1543. a quarter of a sample.
  1544. Andersen, et al. Experimental [Page 39]
  1545. RFC 3951 Internet Low Bit Rate Codec December 2004
  1546. upsFilter1[7]={0.000000 0.000000 0.000000 1.000000
  1547. 0.000000 0.000000 0.000000}
  1548. upsFilter2[7]={0.015625 -0.076904 0.288330 0.862061
  1549. -0.106445 0.018799 -0.015625}
  1550. upsFilter3[7]={0.023682 -0.124268 0.601563 0.601563
  1551. -0.124268 0.023682 -0.023682}
  1552. upsFilter4[7]={0.018799 -0.106445 0.862061 0.288330
  1553. -0.076904 0.015625 -0.018799}
  1554. 4) Generate the pssq(n) vector by upsampling of the excitation memory
  1555. and extracting the sequence that corresponds to the lag delay that
  1556. was calculated in prior step.
  1557. With the steps above, all the pssq(n) can be found in an iterative
  1558. manner, first moving backward in time from pssq(0) and then forward
  1559. in time from pssq(0).
  1560. 0 159 319 479 639
  1561. +---------------------------------------------------------------+
  1562. | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 |
  1563. +---------------------------------------------------------------+
  1564. |pssq 0 |
  1565. |pssq -1| |pssq 1 |
  1566. |pssq -2| |pssq 2 |
  1567. |pssq -3| |pssq 3 |
  1568. Figure 4.3. Enhancement for 20 ms frame size.
  1569. Figure 4.3 depicts pitch-period-synchronous sequences in the
  1570. enhancement of the first 80 sample block in the 20 ms frame size
  1571. mode. The unenhanced signal input is stored in the last two sub-
  1572. blocks (1 - 2), and the six other sub-blocks contain unenhanced
  1573. residual prior-in-time. We perform the enhancement algorithm on two
  1574. blocks of 80 samples, where the first of the two blocks consists of
  1575. the last 40 samples of sub-block 0 and the first 40 samples of sub-
  1576. block 1. The second 80-sample block consists of the last 40 samples
  1577. of sub-block 1 and the first 40 samples of sub-block 2.
  1578. Andersen, et al. Experimental [Page 40]
  1579. RFC 3951 Internet Low Bit Rate Codec December 2004
  1580. 0 159 319 479 639
  1581. +---------------------------------------------------------------+
  1582. | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 |
  1583. +---------------------------------------------------------------+
  1584. |pssq 0 |
  1585. |pssq -1| |pssq 1 |
  1586. |pssq -2| |pssq 2 |
  1587. |pssq -3| |pssq 3 |
  1588. Figure 4.4. Enhancement for 30 ms frame size.
  1589. Figure 4.4 depicts pitch-period-synchronous sequences in the
  1590. enhancement of the first 80-sample block in the 30 ms frame size
  1591. mode. The unenhanced signal input is stored in the last three sub-
  1592. blocks (1 - 3). The five other sub-blocks contain unenhanced
  1593. residual prior-in-time. The enhancement algorithm is performed on
  1594. the three 80 sample sub-blocks 0, 1, and 2.
  1595. 4.6.3. Calculation of the Smoothed Excitation
  1596. A linear combination of the six pssq(n) (n!=0) form a smoothed
  1597. approximation, z, of pssq(0). Most of the weight is put on the
  1598. sequences that are close to pssq(0), as these are likely to be most
  1599. similar to pssq(0). The smoothed vector is also rescaled so that the
  1600. energy of z is the same as the energy of pssq(0).
  1601. ___
  1602. \
  1603. y = > pssq(i) * pssq_weight(i)
  1604. /__
  1605. i=-3,-2,-1,1,2,3
  1606. pssq_weight(i) = 0.5*(1-cos(2*pi*(i+4)/(2*3+2)))
  1607. z = C * y, where C = ||pssq(0)||/||y||
  1608. 4.6.4. Enhancer Criterion
  1609. The criterion of the enhancer is that the enhanced excitation is not
  1610. allowed to differ much from the unenhanced excitation. This
  1611. criterion is checked for each 80-sample sub-block.
  1612. e < (b * ||pssq(0)||^2), where b=0.05 and (Constraint 1)
  1613. e = (pssq(0)-z)*(pssq(0)-z), and "*" means the dot product
  1614. Andersen, et al. Experimental [Page 41]
  1615. RFC 3951 Internet Low Bit Rate Codec December 2004
  1616. 4.6.5. Enhancing the excitation
  1617. From the criterion in the previous section, it is clear that the
  1618. excitation is not allowed to change much. The purpose of this
  1619. constraint is to prevent the creation of an enhanced signal
  1620. significantly different from the original signal. This also means
  1621. that the constraint limits the numerical size of the errors that the
  1622. enhancement procedure can make. That is especially important in
  1623. unvoiced segments and background noise segments for which increased
  1624. periodicity could lead to lower perceived quality.
  1625. When the constraint in the prior section is not met, the enhanced
  1626. residual is instead calculated through a constrained optimization by
  1627. using the Lagrange multiplier technique. The new constraint is that
  1628. e = (b * ||pssq(0)||^2) (Constraint 2)
  1629. We distinguish two solution regions for the optimization: 1) the
  1630. region where the first constraint is fulfilled and 2) the region
  1631. where the first constraint is not fulfilled and the second constraint
  1632. must be used.
  1633. In the first case, where the second constraint is not needed, the
  1634. optimized re-estimated vector is simply z, the energy-scaled version
  1635. of y.
  1636. In the second case, where the second constraint is activated and
  1637. becomes an equality constraint, we have
  1638. z= A*y + B*pssq(0)
  1639. where
  1640. A = sqrt((b-b^2/4)*(w00*w00)/ (w11*w00 + w10*w10)) and
  1641. w11 = pssq(0)*pssq(0)
  1642. w00 = y*y
  1643. w10 = y*pssq(0) (* symbolizes the dot product)
  1644. and
  1645. B = 1 - b/2 - A * w10/w00
  1646. Appendix A.16 contains a listing of a reference implementation for
  1647. the enhancement method.
  1648. Andersen, et al. Experimental [Page 42]
  1649. RFC 3951 Internet Low Bit Rate Codec December 2004
  1650. 4.7. Synthesis Filtering
  1651. Upon decoding or PLC of the LP excitation block, the decoded speech
  1652. block is obtained by running the decoded LP synthesis filter,
  1653. 1/A~k(z), over the block. The synthesis filters have to be shifted
  1654. to compensate for the delay in the enhancer. For 20 ms frame size
  1655. mode, they SHOULD be shifted one 40-sample sub-block, and for 30 ms
  1656. frame size mode, they SHOULD be shifted two 40-sample sub-blocks.
  1657. The LP coefficients SHOULD be changed at the first sample of every
  1658. sub-block while keeping the filter state. For PLC blocks, one
  1659. solution is to apply the last LP coefficients of the last decoded
  1660. speech block for all sub-blocks.
  1661. The reference implementation for the synthesis filtering can be found
  1662. in Appendix A.48.
  1663. 4.8. Post Filtering
  1664. If desired, the decoded block can be filtered by a high-pass filter.
  1665. This removes the low frequencies of the decoded signal. A reference
  1666. implementation of this, with cutoff at 65 Hz, is shown in Appendix
  1667. A.30.
  1668. 5. Security Considerations
  1669. This algorithm for the coding of speech signals is not subject to any
  1670. known security consideration; however, its RTP payload format [1] is
  1671. subject to several considerations, which are addressed there.
  1672. Confidentiality of the media streams is achieved by encryption;
  1673. therefore external mechanisms, such as SRTP [5], MAY be used for that
  1674. purpose.
  1675. 6. Evaluation of the iLBC Implementations
  1676. It is possible and suggested to evaluate certain iLBC implementation
  1677. by utilizing methodology and tools available at
  1678. http://www.ilbcfreeware.org/evaluation.html
  1679. 7. References
  1680. 7.1. Normative References
  1681. [1] Duric, A. and S. Andersen, "Real-time Transport Protocol (RTP)
  1682. Payload Format for internet Low Bit Rate Codec (iLBC) Speech",
  1683. RFC 3952, December 2004.
  1684. [2] Bradner, S., "Key words for use in RFCs to Indicate Requirement
  1685. Levels", BCP 14, RFC 2119, March 1997.
  1686. Andersen, et al. Experimental [Page 43]
  1687. RFC 3951 Internet Low Bit Rate Codec December 2004
  1688. [3] PacketCable(TM) Audio/Video Codecs Specification, Cable
  1689. Television Laboratories, Inc.
  1690. 7.2. Informative References
  1691. [4] ITU-T Recommendation G.711, available online from the ITU
  1692. bookstore at http://www.itu.int.
  1693. [5] Baugher, M., McGrew, D., Naslund, M., Carrara, E., and K. Norman,
  1694. "The Secure Real Time Transport Protocol (SRTP)", RFC 3711, March
  1695. 2004.
  1696. 8. Acknowledgements
  1697. This extensive work, besides listed authors, has the following
  1698. authors, who could not have been listed among "official" authors (due
  1699. to IESG restrictions in the number of authors who can be listed):
  1700. Manohar N. Murthi (Department of Electrical and Computer
  1701. Engineering, University of Miami), Fredrik Galschiodt, Julian
  1702. Spittka, and Jan Skoglund (Global IP Sound).
  1703. The authors are deeply indebted to the following people and thank
  1704. them sincerely:
  1705. Henry Sinnreich, Patrik Faltstrom, Alan Johnston, and Jean-
  1706. Francois Mule for great support of the iLBC initiative and for
  1707. valuable feedback and comments.
  1708. Peter Vary, Frank Mertz, and Christoph Erdmann (RWTH Aachen);
  1709. Vladimir Cuperman (Niftybox LLC); Thomas Eriksson (Chalmers Univ
  1710. of Tech), and Gernot Kubin (TU Graz), for thorough review of the
  1711. iLBC document and their valuable feedback and remarks.
  1712. Andersen, et al. Experimental [Page 44]
  1713. RFC 3951 Internet Low Bit Rate Codec December 2004
  1714. APPENDIX A. Reference Implementation
  1715. This appendix contains the complete c-code for a reference
  1716. implementation of encoder and decoder for the specified codec.
  1717. The c-code consists of the following files with highest-level
  1718. functions:
  1719. iLBC_test.c: main function for evaluation purpose
  1720. iLBC_encode.h: encoder header
  1721. iLBC_encode.c: encoder function
  1722. iLBC_decode.h: decoder header
  1723. iLBC_decode.c: decoder function
  1724. The following files contain global defines and constants:
  1725. iLBC_define.h: global defines
  1726. constants.h: global constants header
  1727. constants.c: global constants memory allocations
  1728. The following files contain subroutines:
  1729. anaFilter.h: lpc analysis filter header
  1730. anaFilter.c: lpc analysis filter function
  1731. createCB.h: codebook construction header
  1732. createCB.c: codebook construction function
  1733. doCPLC.h: packet loss concealment header
  1734. doCPLC.c: packet loss concealment function
  1735. enhancer.h: signal enhancement header
  1736. enhancer.c: signal enhancement function
  1737. filter.h: general filter header
  1738. filter.c: general filter functions
  1739. FrameClassify.h: start state classification header
  1740. FrameClassify.c: start state classification function
  1741. gainquant.h: gain quantization header
  1742. gainquant.c: gain quantization function
  1743. getCBvec.h: codebook vector construction header
  1744. getCBvec.c: codebook vector construction function
  1745. helpfun.h: general purpose header
  1746. helpfun.c: general purpose functions
  1747. hpInput.h: input high pass filter header
  1748. hpInput.c: input high pass filter function
  1749. hpOutput.h: output high pass filter header
  1750. hpOutput.c: output high pass filter function
  1751. iCBConstruct.h: excitation decoding header
  1752. iCBConstruct.c: excitation decoding function
  1753. iCBSearch.h: excitation encoding header
  1754. iCBSearch.c: excitation encoding function
  1755. Andersen, et al. Experimental [Page 45]
  1756. RFC 3951 Internet Low Bit Rate Codec December 2004
  1757. LPCdecode.h: lpc decoding header
  1758. LPCdecode.c: lpc decoding function
  1759. LPCencode.h: lpc encoding header
  1760. LPCencode.c: lpc encoding function
  1761. lsf.h: line spectral frequencies header
  1762. lsf.c: line spectral frequencies functions
  1763. packing.h: bitstream packetization header
  1764. packing.c: bitstream packetization functions
  1765. StateConstructW.h: state decoding header
  1766. StateConstructW.c: state decoding functions
  1767. StateSearchW.h: state encoding header
  1768. StateSearchW.c: state encoding function
  1769. syntFilter.h: lpc synthesis filter header
  1770. syntFilter.c: lpc synthesis filter function
  1771. The implementation is portable and should work on many different
  1772. platforms. However, it is not difficult to optimize the
  1773. implementation on particular platforms, an exercise left to the
  1774. reader.
  1775. A.1. iLBC_test.c
  1776. /******************************************************************
  1777. iLBC Speech Coder ANSI-C Source Code
  1778. iLBC_test.c
  1779. Copyright (C) The Internet Society (2004).
  1780. All Rights Reserved.
  1781. ******************************************************************/
  1782. #include <math.h>
  1783. #include <stdlib.h>
  1784. #include <stdio.h>
  1785. #include <string.h>
  1786. #include "iLBC_define.h"
  1787. #include "iLBC_encode.h"
  1788. #include "iLBC_decode.h"
  1789. /* Runtime statistics */
  1790. #include <time.h>
  1791. #define ILBCNOOFWORDS_MAX (NO_OF_BYTES_30MS/2)
  1792. /*----------------------------------------------------------------*
  1793. * Encoder interface function
  1794. Andersen, et al. Experimental [Page 46]
  1795. RFC 3951 Internet Low Bit Rate Codec December 2004
  1796. *---------------------------------------------------------------*/
  1797. short encode( /* (o) Number of bytes encoded */
  1798. iLBC_Enc_Inst_t *iLBCenc_inst,
  1799. /* (i/o) Encoder instance */
  1800. short *encoded_data, /* (o) The encoded bytes */
  1801. short *data /* (i) The signal block to encode*/
  1802. ){
  1803. float block[BLOCKL_MAX];
  1804. int k;
  1805. /* convert signal to float */
  1806. for (k=0; k<iLBCenc_inst->blockl; k++)
  1807. block[k] = (float)data[k];
  1808. /* do the actual encoding */
  1809. iLBC_encode((unsigned char *)encoded_data, block, iLBCenc_inst);
  1810. return (iLBCenc_inst->no_of_bytes);
  1811. }
  1812. /*----------------------------------------------------------------*
  1813. * Decoder interface function
  1814. *---------------------------------------------------------------*/
  1815. short decode( /* (o) Number of decoded samples */
  1816. iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) Decoder instance */
  1817. short *decoded_data, /* (o) Decoded signal block*/
  1818. short *encoded_data, /* (i) Encoded bytes */
  1819. short mode /* (i) 0=PL, 1=Normal */
  1820. ){
  1821. int k;
  1822. float decblock[BLOCKL_MAX], dtmp;
  1823. /* check if mode is valid */
  1824. if (mode<0 || mode>1) {
  1825. printf("\nERROR - Wrong mode - 0, 1 allowed\n"); exit(3);}
  1826. /* do actual decoding of block */
  1827. iLBC_decode(decblock, (unsigned char *)encoded_data,
  1828. iLBCdec_inst, mode);
  1829. /* convert to short */
  1830. Andersen, et al. Experimental [Page 47]
  1831. RFC 3951 Internet Low Bit Rate Codec December 2004
  1832. for (k=0; k<iLBCdec_inst->blockl; k++){
  1833. dtmp=decblock[k];
  1834. if (dtmp<MIN_SAMPLE)
  1835. dtmp=MIN_SAMPLE;
  1836. else if (dtmp>MAX_SAMPLE)
  1837. dtmp=MAX_SAMPLE;
  1838. decoded_data[k] = (short) dtmp;
  1839. }
  1840. return (iLBCdec_inst->blockl);
  1841. }
  1842. /*---------------------------------------------------------------*
  1843. * Main program to test iLBC encoding and decoding
  1844. *
  1845. * Usage:
  1846. * exefile_name.exe <infile> <bytefile> <outfile> <channel>
  1847. *
  1848. * <infile> : Input file, speech for encoder (16-bit pcm file)
  1849. * <bytefile> : Bit stream output from the encoder
  1850. * <outfile> : Output file, decoded speech (16-bit pcm file)
  1851. * <channel> : Bit error file, optional (16-bit)
  1852. * 1 - Packet received correctly
  1853. * 0 - Packet Lost
  1854. *
  1855. *--------------------------------------------------------------*/
  1856. int main(int argc, char* argv[])
  1857. {
  1858. /* Runtime statistics */
  1859. float starttime;
  1860. float runtime;
  1861. float outtime;
  1862. FILE *ifileid,*efileid,*ofileid, *cfileid;
  1863. short data[BLOCKL_MAX];
  1864. short encoded_data[ILBCNOOFWORDS_MAX], decoded_data[BLOCKL_MAX];
  1865. int len;
  1866. short pli, mode;
  1867. int blockcount = 0;
  1868. int packetlosscount = 0;
  1869. /* Create structs */
  1870. iLBC_Enc_Inst_t Enc_Inst;
  1871. iLBC_Dec_Inst_t Dec_Inst;
  1872. Andersen, et al. Experimental [Page 48]
  1873. RFC 3951 Internet Low Bit Rate Codec December 2004
  1874. /* get arguments and open files */
  1875. if ((argc!=5) && (argc!=6)) {
  1876. fprintf(stderr,
  1877. "\n*-----------------------------------------------*\n");
  1878. fprintf(stderr,
  1879. " %s <20,30> input encoded decoded (channel)\n\n",
  1880. argv[0]);
  1881. fprintf(stderr,
  1882. " mode : Frame size for the encoding/decoding\n");
  1883. fprintf(stderr,
  1884. " 20 - 20 ms\n");
  1885. fprintf(stderr,
  1886. " 30 - 30 ms\n");
  1887. fprintf(stderr,
  1888. " input : Speech for encoder (16-bit pcm file)\n");
  1889. fprintf(stderr,
  1890. " encoded : Encoded bit stream\n");
  1891. fprintf(stderr,
  1892. " decoded : Decoded speech (16-bit pcm file)\n");
  1893. fprintf(stderr,
  1894. " channel : Packet loss pattern, optional (16-bit)\n");
  1895. fprintf(stderr,
  1896. " 1 - Packet received correctly\n");
  1897. fprintf(stderr,
  1898. " 0 - Packet Lost\n");
  1899. fprintf(stderr,
  1900. "*-----------------------------------------------*\n\n");
  1901. exit(1);
  1902. }
  1903. mode=atoi(argv[1]);
  1904. if (mode != 20 && mode != 30) {
  1905. fprintf(stderr,"Wrong mode %s, must be 20, or 30\n",
  1906. argv[1]);
  1907. exit(2);
  1908. }
  1909. if ( (ifileid=fopen(argv[2],"rb")) == NULL) {
  1910. fprintf(stderr,"Cannot open input file %s\n", argv[2]);
  1911. exit(2);}
  1912. if ( (efileid=fopen(argv[3],"wb")) == NULL) {
  1913. fprintf(stderr, "Cannot open encoded file %s\n",
  1914. argv[3]); exit(1);}
  1915. if ( (ofileid=fopen(argv[4],"wb")) == NULL) {
  1916. fprintf(stderr, "Cannot open decoded file %s\n",
  1917. argv[4]); exit(1);}
  1918. if (argc==6) {
  1919. if( (cfileid=fopen(argv[5],"rb")) == NULL) {
  1920. fprintf(stderr, "Cannot open channel file %s\n",
  1921. Andersen, et al. Experimental [Page 49]
  1922. RFC 3951 Internet Low Bit Rate Codec December 2004
  1923. argv[5]);
  1924. exit(1);
  1925. }
  1926. } else {
  1927. cfileid=NULL;
  1928. }
  1929. /* print info */
  1930. fprintf(stderr, "\n");
  1931. fprintf(stderr,
  1932. "*---------------------------------------------------*\n");
  1933. fprintf(stderr,
  1934. "* *\n");
  1935. fprintf(stderr,
  1936. "* iLBC test program *\n");
  1937. fprintf(stderr,
  1938. "* *\n");
  1939. fprintf(stderr,
  1940. "* *\n");
  1941. fprintf(stderr,
  1942. "*---------------------------------------------------*\n");
  1943. fprintf(stderr,"\nMode : %2d ms\n", mode);
  1944. fprintf(stderr,"Input file : %s\n", argv[2]);
  1945. fprintf(stderr,"Encoded file : %s\n", argv[3]);
  1946. fprintf(stderr,"Output file : %s\n", argv[4]);
  1947. if (argc==6) {
  1948. fprintf(stderr,"Channel file : %s\n", argv[5]);
  1949. }
  1950. fprintf(stderr,"\n");
  1951. /* Initialization */
  1952. initEncode(&Enc_Inst, mode);
  1953. initDecode(&Dec_Inst, mode, 1);
  1954. /* Runtime statistics */
  1955. starttime=clock()/(float)CLOCKS_PER_SEC;
  1956. /* loop over input blocks */
  1957. while (fread(data,sizeof(short),Enc_Inst.blockl,ifileid)==
  1958. Enc_Inst.blockl) {
  1959. blockcount++;
  1960. /* encoding */
  1961. Andersen, et al. Experimental [Page 50]
  1962. RFC 3951 Internet Low Bit Rate Codec December 2004
  1963. fprintf(stderr, "--- Encoding block %i --- ",blockcount);
  1964. len=encode(&Enc_Inst, encoded_data, data);
  1965. fprintf(stderr, "\r");
  1966. /* write byte file */
  1967. fwrite(encoded_data, sizeof(unsigned char), len, efileid);
  1968. /* get channel data if provided */
  1969. if (argc==6) {
  1970. if (fread(&pli, sizeof(short), 1, cfileid)) {
  1971. if ((pli!=0)&&(pli!=1)) {
  1972. fprintf(stderr, "Error in channel file\n");
  1973. exit(0);
  1974. }
  1975. if (pli==0) {
  1976. /* Packet loss -> remove info from frame */
  1977. memset(encoded_data, 0,
  1978. sizeof(short)*ILBCNOOFWORDS_MAX);
  1979. packetlosscount++;
  1980. }
  1981. } else {
  1982. fprintf(stderr, "Error. Channel file too short\n");
  1983. exit(0);
  1984. }
  1985. } else {
  1986. pli=1;
  1987. }
  1988. /* decoding */
  1989. fprintf(stderr, "--- Decoding block %i --- ",blockcount);
  1990. len=decode(&Dec_Inst, decoded_data, encoded_data, pli);
  1991. fprintf(stderr, "\r");
  1992. /* write output file */
  1993. fwrite(decoded_data,sizeof(short),len,ofileid);
  1994. }
  1995. /* Runtime statistics */
  1996. runtime = (float)(clock()/(float)CLOCKS_PER_SEC-starttime);
  1997. outtime = (float)((float)blockcount*(float)mode/1000.0);
  1998. printf("\n\nLength of speech file: %.1f s\n", outtime);
  1999. printf("Packet loss : %.1f%%\n",
  2000. 100.0*(float)packetlosscount/(float)blockcount);
  2001. Andersen, et al. Experimental [Page 51]
  2002. RFC 3951 Internet Low Bit Rate Codec December 2004
  2003. printf("Time to run iLBC :");
  2004. printf(" %.1f s (%.1f %% of realtime)\n\n", runtime,
  2005. (100*runtime/outtime));
  2006. /* close files */
  2007. fclose(ifileid); fclose(efileid); fclose(ofileid);
  2008. if (argc==6) {
  2009. fclose(cfileid);
  2010. }
  2011. return(0);
  2012. }
  2013. A.2. iLBC_encode.h
  2014. /******************************************************************
  2015. iLBC Speech Coder ANSI-C Source Code
  2016. iLBC_encode.h
  2017. Copyright (C) The Internet Society (2004).
  2018. All Rights Reserved.
  2019. ******************************************************************/
  2020. #ifndef __iLBC_ILBCENCODE_H
  2021. #define __iLBC_ILBCENCODE_H
  2022. #include "iLBC_define.h"
  2023. short initEncode( /* (o) Number of bytes
  2024. encoded */
  2025. iLBC_Enc_Inst_t *iLBCenc_inst, /* (i/o) Encoder instance */
  2026. int mode /* (i) frame size mode */
  2027. );
  2028. void iLBC_encode(
  2029. unsigned char *bytes, /* (o) encoded data bits iLBC */
  2030. float *block, /* (o) speech vector to
  2031. encode */
  2032. iLBC_Enc_Inst_t *iLBCenc_inst /* (i/o) the general encoder
  2033. state */
  2034. );
  2035. #endif
  2036. Andersen, et al. Experimental [Page 52]
  2037. RFC 3951 Internet Low Bit Rate Codec December 2004
  2038. A.3. iLBC_encode.c
  2039. /******************************************************************
  2040. iLBC Speech Coder ANSI-C Source Code
  2041. iLBC_encode.c
  2042. Copyright (C) The Internet Society (2004).
  2043. All Rights Reserved.
  2044. ******************************************************************/
  2045. #include <math.h>
  2046. #include <stdlib.h>
  2047. #include <string.h>
  2048. #include "iLBC_define.h"
  2049. #include "LPCencode.h"
  2050. #include "FrameClassify.h"
  2051. #include "StateSearchW.h"
  2052. #include "StateConstructW.h"
  2053. #include "helpfun.h"
  2054. #include "constants.h"
  2055. #include "packing.h"
  2056. #include "iCBSearch.h"
  2057. #include "iCBConstruct.h"
  2058. #include "hpInput.h"
  2059. #include "anaFilter.h"
  2060. #include "syntFilter.h"
  2061. /*----------------------------------------------------------------*
  2062. * Initiation of encoder instance.
  2063. *---------------------------------------------------------------*/
  2064. short initEncode( /* (o) Number of bytes
  2065. encoded */
  2066. iLBC_Enc_Inst_t *iLBCenc_inst, /* (i/o) Encoder instance */
  2067. int mode /* (i) frame size mode */
  2068. ){
  2069. iLBCenc_inst->mode = mode;
  2070. if (mode==30) {
  2071. iLBCenc_inst->blockl = BLOCKL_30MS;
  2072. iLBCenc_inst->nsub = NSUB_30MS;
  2073. iLBCenc_inst->nasub = NASUB_30MS;
  2074. iLBCenc_inst->lpc_n = LPC_N_30MS;
  2075. iLBCenc_inst->no_of_bytes = NO_OF_BYTES_30MS;
  2076. iLBCenc_inst->no_of_words = NO_OF_WORDS_30MS;
  2077. Andersen, et al. Experimental [Page 53]
  2078. RFC 3951 Internet Low Bit Rate Codec December 2004
  2079. iLBCenc_inst->state_short_len=STATE_SHORT_LEN_30MS;
  2080. /* ULP init */
  2081. iLBCenc_inst->ULP_inst=&ULP_30msTbl;
  2082. }
  2083. else if (mode==20) {
  2084. iLBCenc_inst->blockl = BLOCKL_20MS;
  2085. iLBCenc_inst->nsub = NSUB_20MS;
  2086. iLBCenc_inst->nasub = NASUB_20MS;
  2087. iLBCenc_inst->lpc_n = LPC_N_20MS;
  2088. iLBCenc_inst->no_of_bytes = NO_OF_BYTES_20MS;
  2089. iLBCenc_inst->no_of_words = NO_OF_WORDS_20MS;
  2090. iLBCenc_inst->state_short_len=STATE_SHORT_LEN_20MS;
  2091. /* ULP init */
  2092. iLBCenc_inst->ULP_inst=&ULP_20msTbl;
  2093. }
  2094. else {
  2095. exit(2);
  2096. }
  2097. memset((*iLBCenc_inst).anaMem, 0,
  2098. LPC_FILTERORDER*sizeof(float));
  2099. memcpy((*iLBCenc_inst).lsfold, lsfmeanTbl,
  2100. LPC_FILTERORDER*sizeof(float));
  2101. memcpy((*iLBCenc_inst).lsfdeqold, lsfmeanTbl,
  2102. LPC_FILTERORDER*sizeof(float));
  2103. memset((*iLBCenc_inst).lpc_buffer, 0,
  2104. (LPC_LOOKBACK+BLOCKL_MAX)*sizeof(float));
  2105. memset((*iLBCenc_inst).hpimem, 0, 4*sizeof(float));
  2106. return (iLBCenc_inst->no_of_bytes);
  2107. }
  2108. /*----------------------------------------------------------------*
  2109. * main encoder function
  2110. *---------------------------------------------------------------*/
  2111. void iLBC_encode(
  2112. unsigned char *bytes, /* (o) encoded data bits iLBC */
  2113. float *block, /* (o) speech vector to
  2114. encode */
  2115. iLBC_Enc_Inst_t *iLBCenc_inst /* (i/o) the general encoder
  2116. state */
  2117. ){
  2118. float data[BLOCKL_MAX];
  2119. float residual[BLOCKL_MAX], reverseResidual[BLOCKL_MAX];
  2120. int start, idxForMax, idxVec[STATE_LEN];
  2121. Andersen, et al. Experimental [Page 54]
  2122. RFC 3951 Internet Low Bit Rate Codec December 2004
  2123. float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
  2124. int n, k, meml_gotten, Nfor, Nback, i, pos;
  2125. int gain_index[CB_NSTAGES*NASUB_MAX],
  2126. extra_gain_index[CB_NSTAGES];
  2127. int cb_index[CB_NSTAGES*NASUB_MAX],extra_cb_index[CB_NSTAGES];
  2128. int lsf_i[LSF_NSPLIT*LPC_N_MAX];
  2129. unsigned char *pbytes;
  2130. int diff, start_pos, state_first;
  2131. float en1, en2;
  2132. int index, ulp, firstpart;
  2133. int subcount, subframe;
  2134. float weightState[LPC_FILTERORDER];
  2135. float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
  2136. float weightdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
  2137. float decresidual[BLOCKL_MAX];
  2138. /* high pass filtering of input signal if such is not done
  2139. prior to calling this function */
  2140. hpInput(block, iLBCenc_inst->blockl,
  2141. data, (*iLBCenc_inst).hpimem);
  2142. /* otherwise simply copy */
  2143. /*memcpy(data,block,iLBCenc_inst->blockl*sizeof(float));*/
  2144. /* LPC of hp filtered input data */
  2145. LPCencode(syntdenum, weightdenum, lsf_i, data, iLBCenc_inst);
  2146. /* inverse filter to get residual */
  2147. for (n=0; n<iLBCenc_inst->nsub; n++) {
  2148. anaFilter(&data[n*SUBL], &syntdenum[n*(LPC_FILTERORDER+1)],
  2149. SUBL, &residual[n*SUBL], iLBCenc_inst->anaMem);
  2150. }
  2151. /* find state location */
  2152. start = FrameClassify(iLBCenc_inst, residual);
  2153. /* check if state should be in first or last part of the
  2154. two subframes */
  2155. diff = STATE_LEN - iLBCenc_inst->state_short_len;
  2156. en1 = 0;
  2157. index = (start-1)*SUBL;
  2158. Andersen, et al. Experimental [Page 55]
  2159. RFC 3951 Internet Low Bit Rate Codec December 2004
  2160. for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
  2161. en1 += residual[index+i]*residual[index+i];
  2162. }
  2163. en2 = 0;
  2164. index = (start-1)*SUBL+diff;
  2165. for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
  2166. en2 += residual[index+i]*residual[index+i];
  2167. }
  2168. if (en1 > en2) {
  2169. state_first = 1;
  2170. start_pos = (start-1)*SUBL;
  2171. } else {
  2172. state_first = 0;
  2173. start_pos = (start-1)*SUBL + diff;
  2174. }
  2175. /* scalar quantization of state */
  2176. StateSearchW(iLBCenc_inst, &residual[start_pos],
  2177. &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
  2178. &weightdenum[(start-1)*(LPC_FILTERORDER+1)], &idxForMax,
  2179. idxVec, iLBCenc_inst->state_short_len, state_first);
  2180. StateConstructW(idxForMax, idxVec,
  2181. &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
  2182. &decresidual[start_pos], iLBCenc_inst->state_short_len);
  2183. /* predictive quantization in state */
  2184. if (state_first) { /* put adaptive part in the end */
  2185. /* setup memory */
  2186. memset(mem, 0,
  2187. (CB_MEML-iLBCenc_inst->state_short_len)*sizeof(float));
  2188. memcpy(mem+CB_MEML-iLBCenc_inst->state_short_len,
  2189. decresidual+start_pos,
  2190. iLBCenc_inst->state_short_len*sizeof(float));
  2191. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  2192. /* encode sub-frames */
  2193. iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
  2194. &residual[start_pos+iLBCenc_inst->state_short_len],
  2195. mem+CB_MEML-stMemLTbl,
  2196. stMemLTbl, diff, CB_NSTAGES,
  2197. Andersen, et al. Experimental [Page 56]
  2198. RFC 3951 Internet Low Bit Rate Codec December 2004
  2199. &weightdenum[start*(LPC_FILTERORDER+1)],
  2200. weightState, 0);
  2201. /* construct decoded vector */
  2202. iCBConstruct(
  2203. &decresidual[start_pos+iLBCenc_inst->state_short_len],
  2204. extra_cb_index, extra_gain_index,
  2205. mem+CB_MEML-stMemLTbl,
  2206. stMemLTbl, diff, CB_NSTAGES);
  2207. }
  2208. else { /* put adaptive part in the beginning */
  2209. /* create reversed vectors for prediction */
  2210. for (k=0; k<diff; k++) {
  2211. reverseResidual[k] = residual[(start+1)*SUBL-1
  2212. -(k+iLBCenc_inst->state_short_len)];
  2213. }
  2214. /* setup memory */
  2215. meml_gotten = iLBCenc_inst->state_short_len;
  2216. for (k=0; k<meml_gotten; k++) {
  2217. mem[CB_MEML-1-k] = decresidual[start_pos + k];
  2218. }
  2219. memset(mem, 0, (CB_MEML-k)*sizeof(float));
  2220. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  2221. /* encode sub-frames */
  2222. iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
  2223. reverseResidual, mem+CB_MEML-stMemLTbl, stMemLTbl,
  2224. diff, CB_NSTAGES,
  2225. &weightdenum[(start-1)*(LPC_FILTERORDER+1)],
  2226. weightState, 0);
  2227. /* construct decoded vector */
  2228. iCBConstruct(reverseDecresidual, extra_cb_index,
  2229. extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
  2230. diff, CB_NSTAGES);
  2231. /* get decoded residual from reversed vector */
  2232. for (k=0; k<diff; k++) {
  2233. decresidual[start_pos-1-k] = reverseDecresidual[k];
  2234. Andersen, et al. Experimental [Page 57]
  2235. RFC 3951 Internet Low Bit Rate Codec December 2004
  2236. }
  2237. }
  2238. /* counter for predicted sub-frames */
  2239. subcount=0;
  2240. /* forward prediction of sub-frames */
  2241. Nfor = iLBCenc_inst->nsub-start-1;
  2242. if ( Nfor > 0 ) {
  2243. /* setup memory */
  2244. memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float));
  2245. memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL,
  2246. STATE_LEN*sizeof(float));
  2247. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  2248. /* loop over sub-frames to encode */
  2249. for (subframe=0; subframe<Nfor; subframe++) {
  2250. /* encode sub-frame */
  2251. iCBSearch(iLBCenc_inst, cb_index+subcount*CB_NSTAGES,
  2252. gain_index+subcount*CB_NSTAGES,
  2253. &residual[(start+1+subframe)*SUBL],
  2254. mem+CB_MEML-memLfTbl[subcount],
  2255. memLfTbl[subcount], SUBL, CB_NSTAGES,
  2256. &weightdenum[(start+1+subframe)*
  2257. (LPC_FILTERORDER+1)],
  2258. weightState, subcount+1);
  2259. /* construct decoded vector */
  2260. iCBConstruct(&decresidual[(start+1+subframe)*SUBL],
  2261. cb_index+subcount*CB_NSTAGES,
  2262. gain_index+subcount*CB_NSTAGES,
  2263. mem+CB_MEML-memLfTbl[subcount],
  2264. memLfTbl[subcount], SUBL, CB_NSTAGES);
  2265. /* update memory */
  2266. memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
  2267. memcpy(mem+CB_MEML-SUBL,
  2268. Andersen, et al. Experimental [Page 58]
  2269. RFC 3951 Internet Low Bit Rate Codec December 2004
  2270. &decresidual[(start+1+subframe)*SUBL],
  2271. SUBL*sizeof(float));
  2272. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  2273. subcount++;
  2274. }
  2275. }
  2276. /* backward prediction of sub-frames */
  2277. Nback = start-1;
  2278. if ( Nback > 0 ) {
  2279. /* create reverse order vectors */
  2280. for (n=0; n<Nback; n++) {
  2281. for (k=0; k<SUBL; k++) {
  2282. reverseResidual[n*SUBL+k] =
  2283. residual[(start-1)*SUBL-1-n*SUBL-k];
  2284. reverseDecresidual[n*SUBL+k] =
  2285. decresidual[(start-1)*SUBL-1-n*SUBL-k];
  2286. }
  2287. }
  2288. /* setup memory */
  2289. meml_gotten = SUBL*(iLBCenc_inst->nsub+1-start);
  2290. if ( meml_gotten > CB_MEML ) {
  2291. meml_gotten=CB_MEML;
  2292. }
  2293. for (k=0; k<meml_gotten; k++) {
  2294. mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
  2295. }
  2296. memset(mem, 0, (CB_MEML-k)*sizeof(float));
  2297. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  2298. /* loop over sub-frames to encode */
  2299. for (subframe=0; subframe<Nback; subframe++) {
  2300. /* encode sub-frame */
  2301. iCBSearch(iLBCenc_inst, cb_index+subcount*CB_NSTAGES,
  2302. Andersen, et al. Experimental [Page 59]
  2303. RFC 3951 Internet Low Bit Rate Codec December 2004
  2304. gain_index+subcount*CB_NSTAGES,
  2305. &reverseResidual[subframe*SUBL],
  2306. mem+CB_MEML-memLfTbl[subcount],
  2307. memLfTbl[subcount], SUBL, CB_NSTAGES,
  2308. &weightdenum[(start-2-subframe)*
  2309. (LPC_FILTERORDER+1)],
  2310. weightState, subcount+1);
  2311. /* construct decoded vector */
  2312. iCBConstruct(&reverseDecresidual[subframe*SUBL],
  2313. cb_index+subcount*CB_NSTAGES,
  2314. gain_index+subcount*CB_NSTAGES,
  2315. mem+CB_MEML-memLfTbl[subcount],
  2316. memLfTbl[subcount], SUBL, CB_NSTAGES);
  2317. /* update memory */
  2318. memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
  2319. memcpy(mem+CB_MEML-SUBL,
  2320. &reverseDecresidual[subframe*SUBL],
  2321. SUBL*sizeof(float));
  2322. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  2323. subcount++;
  2324. }
  2325. /* get decoded residual from reversed vector */
  2326. for (i=0; i<SUBL*Nback; i++) {
  2327. decresidual[SUBL*Nback - i - 1] =
  2328. reverseDecresidual[i];
  2329. }
  2330. }
  2331. /* end encoding part */
  2332. /* adjust index */
  2333. index_conv_enc(cb_index);
  2334. /* pack bytes */
  2335. pbytes=bytes;
  2336. pos=0;
  2337. /* loop over the 3 ULP classes */
  2338. for (ulp=0; ulp<3; ulp++) {
  2339. Andersen, et al. Experimental [Page 60]
  2340. RFC 3951 Internet Low Bit Rate Codec December 2004
  2341. /* LSF */
  2342. for (k=0; k<LSF_NSPLIT*iLBCenc_inst->lpc_n; k++) {
  2343. packsplit(&lsf_i[k], &firstpart, &lsf_i[k],
  2344. iLBCenc_inst->ULP_inst->lsf_bits[k][ulp],
  2345. iLBCenc_inst->ULP_inst->lsf_bits[k][ulp]+
  2346. iLBCenc_inst->ULP_inst->lsf_bits[k][ulp+1]+
  2347. iLBCenc_inst->ULP_inst->lsf_bits[k][ulp+2]);
  2348. dopack( &pbytes, firstpart,
  2349. iLBCenc_inst->ULP_inst->lsf_bits[k][ulp], &pos);
  2350. }
  2351. /* Start block info */
  2352. packsplit(&start, &firstpart, &start,
  2353. iLBCenc_inst->ULP_inst->start_bits[ulp],
  2354. iLBCenc_inst->ULP_inst->start_bits[ulp]+
  2355. iLBCenc_inst->ULP_inst->start_bits[ulp+1]+
  2356. iLBCenc_inst->ULP_inst->start_bits[ulp+2]);
  2357. dopack( &pbytes, firstpart,
  2358. iLBCenc_inst->ULP_inst->start_bits[ulp], &pos);
  2359. packsplit(&state_first, &firstpart, &state_first,
  2360. iLBCenc_inst->ULP_inst->startfirst_bits[ulp],
  2361. iLBCenc_inst->ULP_inst->startfirst_bits[ulp]+
  2362. iLBCenc_inst->ULP_inst->startfirst_bits[ulp+1]+
  2363. iLBCenc_inst->ULP_inst->startfirst_bits[ulp+2]);
  2364. dopack( &pbytes, firstpart,
  2365. iLBCenc_inst->ULP_inst->startfirst_bits[ulp], &pos);
  2366. packsplit(&idxForMax, &firstpart, &idxForMax,
  2367. iLBCenc_inst->ULP_inst->scale_bits[ulp],
  2368. iLBCenc_inst->ULP_inst->scale_bits[ulp]+
  2369. iLBCenc_inst->ULP_inst->scale_bits[ulp+1]+
  2370. iLBCenc_inst->ULP_inst->scale_bits[ulp+2]);
  2371. dopack( &pbytes, firstpart,
  2372. iLBCenc_inst->ULP_inst->scale_bits[ulp], &pos);
  2373. for (k=0; k<iLBCenc_inst->state_short_len; k++) {
  2374. packsplit(idxVec+k, &firstpart, idxVec+k,
  2375. iLBCenc_inst->ULP_inst->state_bits[ulp],
  2376. iLBCenc_inst->ULP_inst->state_bits[ulp]+
  2377. iLBCenc_inst->ULP_inst->state_bits[ulp+1]+
  2378. iLBCenc_inst->ULP_inst->state_bits[ulp+2]);
  2379. dopack( &pbytes, firstpart,
  2380. iLBCenc_inst->ULP_inst->state_bits[ulp], &pos);
  2381. }
  2382. Andersen, et al. Experimental [Page 61]
  2383. RFC 3951 Internet Low Bit Rate Codec December 2004
  2384. /* 23/22 (20ms/30ms) sample block */
  2385. for (k=0;k<CB_NSTAGES;k++) {
  2386. packsplit(extra_cb_index+k, &firstpart,
  2387. extra_cb_index+k,
  2388. iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
  2389. iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp]+
  2390. iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp+1]+
  2391. iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp+2]);
  2392. dopack( &pbytes, firstpart,
  2393. iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
  2394. &pos);
  2395. }
  2396. for (k=0;k<CB_NSTAGES;k++) {
  2397. packsplit(extra_gain_index+k, &firstpart,
  2398. extra_gain_index+k,
  2399. iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
  2400. iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp]+
  2401. iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp+1]+
  2402. iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp+2]);
  2403. dopack( &pbytes, firstpart,
  2404. iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
  2405. &pos);
  2406. }
  2407. /* The two/four (20ms/30ms) 40 sample sub-blocks */
  2408. for (i=0; i<iLBCenc_inst->nasub; i++) {
  2409. for (k=0; k<CB_NSTAGES; k++) {
  2410. packsplit(cb_index+i*CB_NSTAGES+k, &firstpart,
  2411. cb_index+i*CB_NSTAGES+k,
  2412. iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
  2413. iLBCenc_inst->ULP_inst->cb_index[i][k][ulp]+
  2414. iLBCenc_inst->ULP_inst->cb_index[i][k][ulp+1]+
  2415. iLBCenc_inst->ULP_inst->cb_index[i][k][ulp+2]);
  2416. dopack( &pbytes, firstpart,
  2417. iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
  2418. &pos);
  2419. }
  2420. }
  2421. for (i=0; i<iLBCenc_inst->nasub; i++) {
  2422. for (k=0; k<CB_NSTAGES; k++) {
  2423. packsplit(gain_index+i*CB_NSTAGES+k, &firstpart,
  2424. gain_index+i*CB_NSTAGES+k,
  2425. iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
  2426. iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp]+
  2427. Andersen, et al. Experimental [Page 62]
  2428. RFC 3951 Internet Low Bit Rate Codec December 2004
  2429. iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp+1]+
  2430. iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp+2]);
  2431. dopack( &pbytes, firstpart,
  2432. iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
  2433. &pos);
  2434. }
  2435. }
  2436. }
  2437. /* set the last bit to zero (otherwise the decoder
  2438. will treat it as a lost frame) */
  2439. dopack( &pbytes, 0, 1, &pos);
  2440. }
  2441. A.4. iLBC_decode.h
  2442. /******************************************************************
  2443. iLBC Speech Coder ANSI-C Source Code
  2444. iLBC_decode.h
  2445. Copyright (C) The Internet Society (2004).
  2446. All Rights Reserved.
  2447. ******************************************************************/
  2448. #ifndef __iLBC_ILBCDECODE_H
  2449. #define __iLBC_ILBCDECODE_H
  2450. #include "iLBC_define.h"
  2451. short initDecode( /* (o) Number of decoded
  2452. samples */
  2453. iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) Decoder instance */
  2454. int mode, /* (i) frame size mode */
  2455. int use_enhancer /* (i) 1 to use enhancer
  2456. 0 to run without
  2457. enhancer */
  2458. );
  2459. void iLBC_decode(
  2460. float *decblock, /* (o) decoded signal block */
  2461. unsigned char *bytes, /* (i) encoded signal bits */
  2462. iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) the decoder state
  2463. structure */
  2464. int mode /* (i) 0: bad packet, PLC,
  2465. 1: normal */
  2466. Andersen, et al. Experimental [Page 63]
  2467. RFC 3951 Internet Low Bit Rate Codec December 2004
  2468. );
  2469. #endif
  2470. A.5. iLBC_decode.c
  2471. /******************************************************************
  2472. iLBC Speech Coder ANSI-C Source Code
  2473. iLBC_decode.c
  2474. Copyright (C) The Internet Society (2004).
  2475. All Rights Reserved.
  2476. ******************************************************************/
  2477. #include <math.h>
  2478. #include <stdlib.h>
  2479. #include "iLBC_define.h"
  2480. #include "StateConstructW.h"
  2481. #include "LPCdecode.h"
  2482. #include "iCBConstruct.h"
  2483. #include "doCPLC.h"
  2484. #include "helpfun.h"
  2485. #include "constants.h"
  2486. #include "packing.h"
  2487. #include "string.h"
  2488. #include "enhancer.h"
  2489. #include "hpOutput.h"
  2490. #include "syntFilter.h"
  2491. /*----------------------------------------------------------------*
  2492. * Initiation of decoder instance.
  2493. *---------------------------------------------------------------*/
  2494. short initDecode( /* (o) Number of decoded
  2495. samples */
  2496. iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) Decoder instance */
  2497. int mode, /* (i) frame size mode */
  2498. int use_enhancer /* (i) 1 to use enhancer
  2499. 0 to run without
  2500. enhancer */
  2501. ){
  2502. int i;
  2503. iLBCdec_inst->mode = mode;
  2504. Andersen, et al. Experimental [Page 64]
  2505. RFC 3951 Internet Low Bit Rate Codec December 2004
  2506. if (mode==30) {
  2507. iLBCdec_inst->blockl = BLOCKL_30MS;
  2508. iLBCdec_inst->nsub = NSUB_30MS;
  2509. iLBCdec_inst->nasub = NASUB_30MS;
  2510. iLBCdec_inst->lpc_n = LPC_N_30MS;
  2511. iLBCdec_inst->no_of_bytes = NO_OF_BYTES_30MS;
  2512. iLBCdec_inst->no_of_words = NO_OF_WORDS_30MS;
  2513. iLBCdec_inst->state_short_len=STATE_SHORT_LEN_30MS;
  2514. /* ULP init */
  2515. iLBCdec_inst->ULP_inst=&ULP_30msTbl;
  2516. }
  2517. else if (mode==20) {
  2518. iLBCdec_inst->blockl = BLOCKL_20MS;
  2519. iLBCdec_inst->nsub = NSUB_20MS;
  2520. iLBCdec_inst->nasub = NASUB_20MS;
  2521. iLBCdec_inst->lpc_n = LPC_N_20MS;
  2522. iLBCdec_inst->no_of_bytes = NO_OF_BYTES_20MS;
  2523. iLBCdec_inst->no_of_words = NO_OF_WORDS_20MS;
  2524. iLBCdec_inst->state_short_len=STATE_SHORT_LEN_20MS;
  2525. /* ULP init */
  2526. iLBCdec_inst->ULP_inst=&ULP_20msTbl;
  2527. }
  2528. else {
  2529. exit(2);
  2530. }
  2531. memset(iLBCdec_inst->syntMem, 0,
  2532. LPC_FILTERORDER*sizeof(float));
  2533. memcpy((*iLBCdec_inst).lsfdeqold, lsfmeanTbl,
  2534. LPC_FILTERORDER*sizeof(float));
  2535. memset(iLBCdec_inst->old_syntdenum, 0,
  2536. ((LPC_FILTERORDER + 1)*NSUB_MAX)*sizeof(float));
  2537. for (i=0; i<NSUB_MAX; i++)
  2538. iLBCdec_inst->old_syntdenum[i*(LPC_FILTERORDER+1)]=1.0;
  2539. iLBCdec_inst->last_lag = 20;
  2540. iLBCdec_inst->prevLag = 120;
  2541. iLBCdec_inst->per = 0.0;
  2542. iLBCdec_inst->consPLICount = 0;
  2543. iLBCdec_inst->prevPLI = 0;
  2544. iLBCdec_inst->prevLpc[0] = 1.0;
  2545. memset(iLBCdec_inst->prevLpc+1,0,
  2546. LPC_FILTERORDER*sizeof(float));
  2547. memset(iLBCdec_inst->prevResidual, 0, BLOCKL_MAX*sizeof(float));
  2548. iLBCdec_inst->seed=777;
  2549. Andersen, et al. Experimental [Page 65]
  2550. RFC 3951 Internet Low Bit Rate Codec December 2004
  2551. memset(iLBCdec_inst->hpomem, 0, 4*sizeof(float));
  2552. iLBCdec_inst->use_enhancer = use_enhancer;
  2553. memset(iLBCdec_inst->enh_buf, 0, ENH_BUFL*sizeof(float));
  2554. for (i=0;i<ENH_NBLOCKS_TOT;i++)
  2555. iLBCdec_inst->enh_period[i]=(float)40.0;
  2556. iLBCdec_inst->prev_enh_pl = 0;
  2557. return (iLBCdec_inst->blockl);
  2558. }
  2559. /*----------------------------------------------------------------*
  2560. * frame residual decoder function (subrutine to iLBC_decode)
  2561. *---------------------------------------------------------------*/
  2562. void Decode(
  2563. iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) the decoder state
  2564. structure */
  2565. float *decresidual, /* (o) decoded residual frame */
  2566. int start, /* (i) location of start
  2567. state */
  2568. int idxForMax, /* (i) codebook index for the
  2569. maximum value */
  2570. int *idxVec, /* (i) codebook indexes for the
  2571. samples in the start
  2572. state */
  2573. float *syntdenum, /* (i) the decoded synthesis
  2574. filter coefficients */
  2575. int *cb_index, /* (i) the indexes for the
  2576. adaptive codebook */
  2577. int *gain_index, /* (i) the indexes for the
  2578. corresponding gains */
  2579. int *extra_cb_index, /* (i) the indexes for the
  2580. adaptive codebook part
  2581. of start state */
  2582. int *extra_gain_index, /* (i) the indexes for the
  2583. corresponding gains */
  2584. int state_first /* (i) 1 if non adaptive part
  2585. of start state comes
  2586. first 0 if that part
  2587. comes last */
  2588. ){
  2589. float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
  2590. int k, meml_gotten, Nfor, Nback, i;
  2591. int diff, start_pos;
  2592. int subcount, subframe;
  2593. Andersen, et al. Experimental [Page 66]
  2594. RFC 3951 Internet Low Bit Rate Codec December 2004
  2595. diff = STATE_LEN - iLBCdec_inst->state_short_len;
  2596. if (state_first == 1) {
  2597. start_pos = (start-1)*SUBL;
  2598. } else {
  2599. start_pos = (start-1)*SUBL + diff;
  2600. }
  2601. /* decode scalar part of start state */
  2602. StateConstructW(idxForMax, idxVec,
  2603. &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
  2604. &decresidual[start_pos], iLBCdec_inst->state_short_len);
  2605. if (state_first) { /* put adaptive part in the end */
  2606. /* setup memory */
  2607. memset(mem, 0,
  2608. (CB_MEML-iLBCdec_inst->state_short_len)*sizeof(float));
  2609. memcpy(mem+CB_MEML-iLBCdec_inst->state_short_len,
  2610. decresidual+start_pos,
  2611. iLBCdec_inst->state_short_len*sizeof(float));
  2612. /* construct decoded vector */
  2613. iCBConstruct(
  2614. &decresidual[start_pos+iLBCdec_inst->state_short_len],
  2615. extra_cb_index, extra_gain_index, mem+CB_MEML-stMemLTbl,
  2616. stMemLTbl, diff, CB_NSTAGES);
  2617. }
  2618. else {/* put adaptive part in the beginning */
  2619. /* create reversed vectors for prediction */
  2620. for (k=0; k<diff; k++) {
  2621. reverseDecresidual[k] =
  2622. decresidual[(start+1)*SUBL-1-
  2623. (k+iLBCdec_inst->state_short_len)];
  2624. }
  2625. /* setup memory */
  2626. meml_gotten = iLBCdec_inst->state_short_len;
  2627. for (k=0; k<meml_gotten; k++){
  2628. mem[CB_MEML-1-k] = decresidual[start_pos + k];
  2629. Andersen, et al. Experimental [Page 67]
  2630. RFC 3951 Internet Low Bit Rate Codec December 2004
  2631. }
  2632. memset(mem, 0, (CB_MEML-k)*sizeof(float));
  2633. /* construct decoded vector */
  2634. iCBConstruct(reverseDecresidual, extra_cb_index,
  2635. extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
  2636. diff, CB_NSTAGES);
  2637. /* get decoded residual from reversed vector */
  2638. for (k=0; k<diff; k++) {
  2639. decresidual[start_pos-1-k] = reverseDecresidual[k];
  2640. }
  2641. }
  2642. /* counter for predicted sub-frames */
  2643. subcount=0;
  2644. /* forward prediction of sub-frames */
  2645. Nfor = iLBCdec_inst->nsub-start-1;
  2646. if ( Nfor > 0 ){
  2647. /* setup memory */
  2648. memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float));
  2649. memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL,
  2650. STATE_LEN*sizeof(float));
  2651. /* loop over sub-frames to encode */
  2652. for (subframe=0; subframe<Nfor; subframe++) {
  2653. /* construct decoded vector */
  2654. iCBConstruct(&decresidual[(start+1+subframe)*SUBL],
  2655. cb_index+subcount*CB_NSTAGES,
  2656. gain_index+subcount*CB_NSTAGES,
  2657. mem+CB_MEML-memLfTbl[subcount],
  2658. memLfTbl[subcount], SUBL, CB_NSTAGES);
  2659. /* update memory */
  2660. memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
  2661. memcpy(mem+CB_MEML-SUBL,
  2662. Andersen, et al. Experimental [Page 68]
  2663. RFC 3951 Internet Low Bit Rate Codec December 2004
  2664. &decresidual[(start+1+subframe)*SUBL],
  2665. SUBL*sizeof(float));
  2666. subcount++;
  2667. }
  2668. }
  2669. /* backward prediction of sub-frames */
  2670. Nback = start-1;
  2671. if ( Nback > 0 ) {
  2672. /* setup memory */
  2673. meml_gotten = SUBL*(iLBCdec_inst->nsub+1-start);
  2674. if ( meml_gotten > CB_MEML ) {
  2675. meml_gotten=CB_MEML;
  2676. }
  2677. for (k=0; k<meml_gotten; k++) {
  2678. mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
  2679. }
  2680. memset(mem, 0, (CB_MEML-k)*sizeof(float));
  2681. /* loop over subframes to decode */
  2682. for (subframe=0; subframe<Nback; subframe++) {
  2683. /* construct decoded vector */
  2684. iCBConstruct(&reverseDecresidual[subframe*SUBL],
  2685. cb_index+subcount*CB_NSTAGES,
  2686. gain_index+subcount*CB_NSTAGES,
  2687. mem+CB_MEML-memLfTbl[subcount], memLfTbl[subcount],
  2688. SUBL, CB_NSTAGES);
  2689. /* update memory */
  2690. memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
  2691. memcpy(mem+CB_MEML-SUBL,
  2692. &reverseDecresidual[subframe*SUBL],
  2693. SUBL*sizeof(float));
  2694. subcount++;
  2695. }
  2696. Andersen, et al. Experimental [Page 69]
  2697. RFC 3951 Internet Low Bit Rate Codec December 2004
  2698. /* get decoded residual from reversed vector */
  2699. for (i=0; i<SUBL*Nback; i++)
  2700. decresidual[SUBL*Nback - i - 1] =
  2701. reverseDecresidual[i];
  2702. }
  2703. }
  2704. /*----------------------------------------------------------------*
  2705. * main decoder function
  2706. *---------------------------------------------------------------*/
  2707. void iLBC_decode(
  2708. float *decblock, /* (o) decoded signal block */
  2709. unsigned char *bytes, /* (i) encoded signal bits */
  2710. iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) the decoder state
  2711. structure */
  2712. int mode /* (i) 0: bad packet, PLC,
  2713. 1: normal */
  2714. ){
  2715. float data[BLOCKL_MAX];
  2716. float lsfdeq[LPC_FILTERORDER*LPC_N_MAX];
  2717. float PLCresidual[BLOCKL_MAX], PLClpc[LPC_FILTERORDER + 1];
  2718. float zeros[BLOCKL_MAX], one[LPC_FILTERORDER + 1];
  2719. int k, i, start, idxForMax, pos, lastpart, ulp;
  2720. int lag, ilag;
  2721. float cc, maxcc;
  2722. int idxVec[STATE_LEN];
  2723. int check;
  2724. int gain_index[NASUB_MAX*CB_NSTAGES],
  2725. extra_gain_index[CB_NSTAGES];
  2726. int cb_index[CB_NSTAGES*NASUB_MAX], extra_cb_index[CB_NSTAGES];
  2727. int lsf_i[LSF_NSPLIT*LPC_N_MAX];
  2728. int state_first;
  2729. int last_bit;
  2730. unsigned char *pbytes;
  2731. float weightdenum[(LPC_FILTERORDER + 1)*NSUB_MAX];
  2732. int order_plus_one;
  2733. float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
  2734. float decresidual[BLOCKL_MAX];
  2735. if (mode>0) { /* the data are good */
  2736. /* decode data */
  2737. pbytes=bytes;
  2738. pos=0;
  2739. Andersen, et al. Experimental [Page 70]
  2740. RFC 3951 Internet Low Bit Rate Codec December 2004
  2741. /* Set everything to zero before decoding */
  2742. for (k=0; k<LSF_NSPLIT*LPC_N_MAX; k++) {
  2743. lsf_i[k]=0;
  2744. }
  2745. start=0;
  2746. state_first=0;
  2747. idxForMax=0;
  2748. for (k=0; k<iLBCdec_inst->state_short_len; k++) {
  2749. idxVec[k]=0;
  2750. }
  2751. for (k=0; k<CB_NSTAGES; k++) {
  2752. extra_cb_index[k]=0;
  2753. }
  2754. for (k=0; k<CB_NSTAGES; k++) {
  2755. extra_gain_index[k]=0;
  2756. }
  2757. for (i=0; i<iLBCdec_inst->nasub; i++) {
  2758. for (k=0; k<CB_NSTAGES; k++) {
  2759. cb_index[i*CB_NSTAGES+k]=0;
  2760. }
  2761. }
  2762. for (i=0; i<iLBCdec_inst->nasub; i++) {
  2763. for (k=0; k<CB_NSTAGES; k++) {
  2764. gain_index[i*CB_NSTAGES+k]=0;
  2765. }
  2766. }
  2767. /* loop over ULP classes */
  2768. for (ulp=0; ulp<3; ulp++) {
  2769. /* LSF */
  2770. for (k=0; k<LSF_NSPLIT*iLBCdec_inst->lpc_n; k++){
  2771. unpack( &pbytes, &lastpart,
  2772. iLBCdec_inst->ULP_inst->lsf_bits[k][ulp], &pos);
  2773. packcombine(&lsf_i[k], lastpart,
  2774. iLBCdec_inst->ULP_inst->lsf_bits[k][ulp]);
  2775. }
  2776. /* Start block info */
  2777. unpack( &pbytes, &lastpart,
  2778. iLBCdec_inst->ULP_inst->start_bits[ulp], &pos);
  2779. packcombine(&start, lastpart,
  2780. iLBCdec_inst->ULP_inst->start_bits[ulp]);
  2781. unpack( &pbytes, &lastpart,
  2782. Andersen, et al. Experimental [Page 71]
  2783. RFC 3951 Internet Low Bit Rate Codec December 2004
  2784. iLBCdec_inst->ULP_inst->startfirst_bits[ulp], &pos);
  2785. packcombine(&state_first, lastpart,
  2786. iLBCdec_inst->ULP_inst->startfirst_bits[ulp]);
  2787. unpack( &pbytes, &lastpart,
  2788. iLBCdec_inst->ULP_inst->scale_bits[ulp], &pos);
  2789. packcombine(&idxForMax, lastpart,
  2790. iLBCdec_inst->ULP_inst->scale_bits[ulp]);
  2791. for (k=0; k<iLBCdec_inst->state_short_len; k++) {
  2792. unpack( &pbytes, &lastpart,
  2793. iLBCdec_inst->ULP_inst->state_bits[ulp], &pos);
  2794. packcombine(idxVec+k, lastpart,
  2795. iLBCdec_inst->ULP_inst->state_bits[ulp]);
  2796. }
  2797. /* 23/22 (20ms/30ms) sample block */
  2798. for (k=0; k<CB_NSTAGES; k++) {
  2799. unpack( &pbytes, &lastpart,
  2800. iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp],
  2801. &pos);
  2802. packcombine(extra_cb_index+k, lastpart,
  2803. iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp]);
  2804. }
  2805. for (k=0; k<CB_NSTAGES; k++) {
  2806. unpack( &pbytes, &lastpart,
  2807. iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp],
  2808. &pos);
  2809. packcombine(extra_gain_index+k, lastpart,
  2810. iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp]);
  2811. }
  2812. /* The two/four (20ms/30ms) 40 sample sub-blocks */
  2813. for (i=0; i<iLBCdec_inst->nasub; i++) {
  2814. for (k=0; k<CB_NSTAGES; k++) {
  2815. unpack( &pbytes, &lastpart,
  2816. iLBCdec_inst->ULP_inst->cb_index[i][k][ulp],
  2817. &pos);
  2818. packcombine(cb_index+i*CB_NSTAGES+k, lastpart,
  2819. iLBCdec_inst->ULP_inst->cb_index[i][k][ulp]);
  2820. }
  2821. }
  2822. for (i=0; i<iLBCdec_inst->nasub; i++) {
  2823. for (k=0; k<CB_NSTAGES; k++) {
  2824. unpack( &pbytes, &lastpart,
  2825. Andersen, et al. Experimental [Page 72]
  2826. RFC 3951 Internet Low Bit Rate Codec December 2004
  2827. iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp],
  2828. &pos);
  2829. packcombine(gain_index+i*CB_NSTAGES+k, lastpart,
  2830. iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp]);
  2831. }
  2832. }
  2833. }
  2834. /* Extract last bit. If it is 1 this indicates an
  2835. empty/lost frame */
  2836. unpack( &pbytes, &last_bit, 1, &pos);
  2837. /* Check for bit errors or empty/lost frames */
  2838. if (start<1)
  2839. mode = 0;
  2840. if (iLBCdec_inst->mode==20 && start>3)
  2841. mode = 0;
  2842. if (iLBCdec_inst->mode==30 && start>5)
  2843. mode = 0;
  2844. if (last_bit==1)
  2845. mode = 0;
  2846. if (mode==1) { /* No bit errors was detected,
  2847. continue decoding */
  2848. /* adjust index */
  2849. index_conv_dec(cb_index);
  2850. /* decode the lsf */
  2851. SimplelsfDEQ(lsfdeq, lsf_i, iLBCdec_inst->lpc_n);
  2852. check=LSF_check(lsfdeq, LPC_FILTERORDER,
  2853. iLBCdec_inst->lpc_n);
  2854. DecoderInterpolateLSF(syntdenum, weightdenum,
  2855. lsfdeq, LPC_FILTERORDER, iLBCdec_inst);
  2856. Decode(iLBCdec_inst, decresidual, start, idxForMax,
  2857. idxVec, syntdenum, cb_index, gain_index,
  2858. extra_cb_index, extra_gain_index,
  2859. state_first);
  2860. /* preparing the plc for a future loss! */
  2861. doThePLC(PLCresidual, PLClpc, 0, decresidual,
  2862. syntdenum +
  2863. (LPC_FILTERORDER + 1)*(iLBCdec_inst->nsub - 1),
  2864. (*iLBCdec_inst).last_lag, iLBCdec_inst);
  2865. Andersen, et al. Experimental [Page 73]
  2866. RFC 3951 Internet Low Bit Rate Codec December 2004
  2867. memcpy(decresidual, PLCresidual,
  2868. iLBCdec_inst->blockl*sizeof(float));
  2869. }
  2870. }
  2871. if (mode == 0) {
  2872. /* the data is bad (either a PLC call
  2873. * was made or a severe bit error was detected)
  2874. */
  2875. /* packet loss conceal */
  2876. memset(zeros, 0, BLOCKL_MAX*sizeof(float));
  2877. one[0] = 1;
  2878. memset(one+1, 0, LPC_FILTERORDER*sizeof(float));
  2879. start=0;
  2880. doThePLC(PLCresidual, PLClpc, 1, zeros, one,
  2881. (*iLBCdec_inst).last_lag, iLBCdec_inst);
  2882. memcpy(decresidual, PLCresidual,
  2883. iLBCdec_inst->blockl*sizeof(float));
  2884. order_plus_one = LPC_FILTERORDER + 1;
  2885. for (i = 0; i < iLBCdec_inst->nsub; i++) {
  2886. memcpy(syntdenum+(i*order_plus_one), PLClpc,
  2887. order_plus_one*sizeof(float));
  2888. }
  2889. }
  2890. if (iLBCdec_inst->use_enhancer == 1) {
  2891. /* post filtering */
  2892. iLBCdec_inst->last_lag =
  2893. enhancerInterface(data, decresidual, iLBCdec_inst);
  2894. /* synthesis filtering */
  2895. if (iLBCdec_inst->mode==20) {
  2896. /* Enhancer has 40 samples delay */
  2897. i=0;
  2898. syntFilter(data + i*SUBL,
  2899. iLBCdec_inst->old_syntdenum +
  2900. (i+iLBCdec_inst->nsub-1)*(LPC_FILTERORDER+1),
  2901. SUBL, iLBCdec_inst->syntMem);
  2902. Andersen, et al. Experimental [Page 74]
  2903. RFC 3951 Internet Low Bit Rate Codec December 2004
  2904. for (i=1; i < iLBCdec_inst->nsub; i++) {
  2905. syntFilter(data + i*SUBL,
  2906. syntdenum + (i-1)*(LPC_FILTERORDER+1),
  2907. SUBL, iLBCdec_inst->syntMem);
  2908. }
  2909. } else if (iLBCdec_inst->mode==30) {
  2910. /* Enhancer has 80 samples delay */
  2911. for (i=0; i < 2; i++) {
  2912. syntFilter(data + i*SUBL,
  2913. iLBCdec_inst->old_syntdenum +
  2914. (i+iLBCdec_inst->nsub-2)*(LPC_FILTERORDER+1),
  2915. SUBL, iLBCdec_inst->syntMem);
  2916. }
  2917. for (i=2; i < iLBCdec_inst->nsub; i++) {
  2918. syntFilter(data + i*SUBL,
  2919. syntdenum + (i-2)*(LPC_FILTERORDER+1), SUBL,
  2920. iLBCdec_inst->syntMem);
  2921. }
  2922. }
  2923. } else {
  2924. /* Find last lag */
  2925. lag = 20;
  2926. maxcc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
  2927. &decresidual[BLOCKL_MAX-ENH_BLOCKL-lag], ENH_BLOCKL);
  2928. for (ilag=21; ilag<120; ilag++) {
  2929. cc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
  2930. &decresidual[BLOCKL_MAX-ENH_BLOCKL-ilag],
  2931. ENH_BLOCKL);
  2932. if (cc > maxcc) {
  2933. maxcc = cc;
  2934. lag = ilag;
  2935. }
  2936. }
  2937. iLBCdec_inst->last_lag = lag;
  2938. /* copy data and run synthesis filter */
  2939. memcpy(data, decresidual,
  2940. iLBCdec_inst->blockl*sizeof(float));
  2941. for (i=0; i < iLBCdec_inst->nsub; i++) {
  2942. syntFilter(data + i*SUBL,
  2943. syntdenum + i*(LPC_FILTERORDER+1), SUBL,
  2944. iLBCdec_inst->syntMem);
  2945. }
  2946. Andersen, et al. Experimental [Page 75]
  2947. RFC 3951 Internet Low Bit Rate Codec December 2004
  2948. }
  2949. /* high pass filtering on output if desired, otherwise
  2950. copy to out */
  2951. hpOutput(data, iLBCdec_inst->blockl,
  2952. decblock,iLBCdec_inst->hpomem);
  2953. /* memcpy(decblock,data,iLBCdec_inst->blockl*sizeof(float));*/
  2954. memcpy(iLBCdec_inst->old_syntdenum, syntdenum,
  2955. iLBCdec_inst->nsub*(LPC_FILTERORDER+1)*sizeof(float));
  2956. iLBCdec_inst->prev_enh_pl=0;
  2957. if (mode==0) { /* PLC was used */
  2958. iLBCdec_inst->prev_enh_pl=1;
  2959. }
  2960. }
  2961. A.6. iLBC_define.h
  2962. /******************************************************************
  2963. iLBC Speech Coder ANSI-C Source Code
  2964. iLBC_define.h
  2965. Copyright (C) The Internet Society (2004).
  2966. All Rights Reserved.
  2967. ******************************************************************/
  2968. #include <string.h>
  2969. #ifndef __iLBC_ILBCDEFINE_H
  2970. #define __iLBC_ILBCDEFINE_H
  2971. /* general codec settings */
  2972. #define FS (float)8000.0
  2973. #define BLOCKL_20MS 160
  2974. #define BLOCKL_30MS 240
  2975. #define BLOCKL_MAX 240
  2976. #define NSUB_20MS 4
  2977. #define NSUB_30MS 6
  2978. #define NSUB_MAX 6
  2979. #define NASUB_20MS 2
  2980. Andersen, et al. Experimental [Page 76]
  2981. RFC 3951 Internet Low Bit Rate Codec December 2004
  2982. #define NASUB_30MS 4
  2983. #define NASUB_MAX 4
  2984. #define SUBL 40
  2985. #define STATE_LEN 80
  2986. #define STATE_SHORT_LEN_30MS 58
  2987. #define STATE_SHORT_LEN_20MS 57
  2988. /* LPC settings */
  2989. #define LPC_FILTERORDER 10
  2990. #define LPC_CHIRP_SYNTDENUM (float)0.9025
  2991. #define LPC_CHIRP_WEIGHTDENUM (float)0.4222
  2992. #define LPC_LOOKBACK 60
  2993. #define LPC_N_20MS 1
  2994. #define LPC_N_30MS 2
  2995. #define LPC_N_MAX 2
  2996. #define LPC_ASYMDIFF 20
  2997. #define LPC_BW (float)60.0
  2998. #define LPC_WN (float)1.0001
  2999. #define LSF_NSPLIT 3
  3000. #define LSF_NUMBER_OF_STEPS 4
  3001. #define LPC_HALFORDER (LPC_FILTERORDER/2)
  3002. /* cb settings */
  3003. #define CB_NSTAGES 3
  3004. #define CB_EXPAND 2
  3005. #define CB_MEML 147
  3006. #define CB_FILTERLEN 2*4
  3007. #define CB_HALFFILTERLEN 4
  3008. #define CB_RESRANGE 34
  3009. #define CB_MAXGAIN (float)1.3
  3010. /* enhancer */
  3011. #define ENH_BLOCKL 80 /* block length */
  3012. #define ENH_BLOCKL_HALF (ENH_BLOCKL/2)
  3013. #define ENH_HL 3 /* 2*ENH_HL+1 is number blocks
  3014. in said second sequence */
  3015. #define ENH_SLOP 2 /* max difference estimated and
  3016. correct pitch period */
  3017. #define ENH_PLOCSL 20 /* pitch-estimates and pitch-
  3018. locations buffer length */
  3019. #define ENH_OVERHANG 2
  3020. #define ENH_UPS0 4 /* upsampling rate */
  3021. #define ENH_FL0 3 /* 2*FLO+1 is the length of
  3022. each filter */
  3023. #define ENH_VECTL (ENH_BLOCKL+2*ENH_FL0)
  3024. Andersen, et al. Experimental [Page 77]
  3025. RFC 3951 Internet Low Bit Rate Codec December 2004
  3026. #define ENH_CORRDIM (2*ENH_SLOP+1)
  3027. #define ENH_NBLOCKS (BLOCKL_MAX/ENH_BLOCKL)
  3028. #define ENH_NBLOCKS_EXTRA 5
  3029. #define ENH_NBLOCKS_TOT 8 /* ENH_NBLOCKS +
  3030. ENH_NBLOCKS_EXTRA */
  3031. #define ENH_BUFL (ENH_NBLOCKS_TOT)*ENH_BLOCKL
  3032. #define ENH_ALPHA0 (float)0.05
  3033. /* Down sampling */
  3034. #define FILTERORDER_DS 7
  3035. #define DELAY_DS 3
  3036. #define FACTOR_DS 2
  3037. /* bit stream defs */
  3038. #define NO_OF_BYTES_20MS 38
  3039. #define NO_OF_BYTES_30MS 50
  3040. #define NO_OF_WORDS_20MS 19
  3041. #define NO_OF_WORDS_30MS 25
  3042. #define STATE_BITS 3
  3043. #define BYTE_LEN 8
  3044. #define ULP_CLASSES 3
  3045. /* help parameters */
  3046. #define FLOAT_MAX (float)1.0e37
  3047. #define EPS (float)2.220446049250313e-016
  3048. #define PI (float)3.14159265358979323846
  3049. #define MIN_SAMPLE -32768
  3050. #define MAX_SAMPLE 32767
  3051. #define TWO_PI (float)6.283185307
  3052. #define PI2 (float)0.159154943
  3053. /* type definition encoder instance */
  3054. typedef struct iLBC_ULP_Inst_t_ {
  3055. int lsf_bits[6][ULP_CLASSES+2];
  3056. int start_bits[ULP_CLASSES+2];
  3057. int startfirst_bits[ULP_CLASSES+2];
  3058. int scale_bits[ULP_CLASSES+2];
  3059. int state_bits[ULP_CLASSES+2];
  3060. int extra_cb_index[CB_NSTAGES][ULP_CLASSES+2];
  3061. int extra_cb_gain[CB_NSTAGES][ULP_CLASSES+2];
  3062. int cb_index[NSUB_MAX][CB_NSTAGES][ULP_CLASSES+2];
  3063. int cb_gain[NSUB_MAX][CB_NSTAGES][ULP_CLASSES+2];
  3064. } iLBC_ULP_Inst_t;
  3065. /* type definition encoder instance */
  3066. Andersen, et al. Experimental [Page 78]
  3067. RFC 3951 Internet Low Bit Rate Codec December 2004
  3068. typedef struct iLBC_Enc_Inst_t_ {
  3069. /* flag for frame size mode */
  3070. int mode;
  3071. /* basic parameters for different frame sizes */
  3072. int blockl;
  3073. int nsub;
  3074. int nasub;
  3075. int no_of_bytes, no_of_words;
  3076. int lpc_n;
  3077. int state_short_len;
  3078. const iLBC_ULP_Inst_t *ULP_inst;
  3079. /* analysis filter state */
  3080. float anaMem[LPC_FILTERORDER];
  3081. /* old lsf parameters for interpolation */
  3082. float lsfold[LPC_FILTERORDER];
  3083. float lsfdeqold[LPC_FILTERORDER];
  3084. /* signal buffer for LP analysis */
  3085. float lpc_buffer[LPC_LOOKBACK + BLOCKL_MAX];
  3086. /* state of input HP filter */
  3087. float hpimem[4];
  3088. } iLBC_Enc_Inst_t;
  3089. /* type definition decoder instance */
  3090. typedef struct iLBC_Dec_Inst_t_ {
  3091. /* flag for frame size mode */
  3092. int mode;
  3093. /* basic parameters for different frame sizes */
  3094. int blockl;
  3095. int nsub;
  3096. int nasub;
  3097. int no_of_bytes, no_of_words;
  3098. int lpc_n;
  3099. int state_short_len;
  3100. const iLBC_ULP_Inst_t *ULP_inst;
  3101. /* synthesis filter state */
  3102. float syntMem[LPC_FILTERORDER];
  3103. /* old LSF for interpolation */
  3104. Andersen, et al. Experimental [Page 79]
  3105. RFC 3951 Internet Low Bit Rate Codec December 2004
  3106. float lsfdeqold[LPC_FILTERORDER];
  3107. /* pitch lag estimated in enhancer and used in PLC */
  3108. int last_lag;
  3109. /* PLC state information */
  3110. int prevLag, consPLICount, prevPLI, prev_enh_pl;
  3111. float prevLpc[LPC_FILTERORDER+1];
  3112. float prevResidual[NSUB_MAX*SUBL];
  3113. float per;
  3114. unsigned long seed;
  3115. /* previous synthesis filter parameters */
  3116. float old_syntdenum[(LPC_FILTERORDER + 1)*NSUB_MAX];
  3117. /* state of output HP filter */
  3118. float hpomem[4];
  3119. /* enhancer state information */
  3120. int use_enhancer;
  3121. float enh_buf[ENH_BUFL];
  3122. float enh_period[ENH_NBLOCKS_TOT];
  3123. } iLBC_Dec_Inst_t;
  3124. #endif
  3125. A.7. constants.h
  3126. /******************************************************************
  3127. iLBC Speech Coder ANSI-C Source Code
  3128. constants.h
  3129. Copyright (C) The Internet Society (2004).
  3130. All Rights Reserved.
  3131. ******************************************************************/
  3132. #ifndef __iLBC_CONSTANTS_H
  3133. #define __iLBC_CONSTANTS_H
  3134. #include "iLBC_define.h"
  3135. /* ULP bit allocation */
  3136. Andersen, et al. Experimental [Page 80]
  3137. RFC 3951 Internet Low Bit Rate Codec December 2004
  3138. extern const iLBC_ULP_Inst_t ULP_20msTbl;
  3139. extern const iLBC_ULP_Inst_t ULP_30msTbl;
  3140. /* high pass filters */
  3141. extern float hpi_zero_coefsTbl[];
  3142. extern float hpi_pole_coefsTbl[];
  3143. extern float hpo_zero_coefsTbl[];
  3144. extern float hpo_pole_coefsTbl[];
  3145. /* low pass filters */
  3146. extern float lpFilt_coefsTbl[];
  3147. /* LPC analysis and quantization */
  3148. extern float lpc_winTbl[];
  3149. extern float lpc_asymwinTbl[];
  3150. extern float lpc_lagwinTbl[];
  3151. extern float lsfCbTbl[];
  3152. extern float lsfmeanTbl[];
  3153. extern int dim_lsfCbTbl[];
  3154. extern int size_lsfCbTbl[];
  3155. extern float lsf_weightTbl_30ms[];
  3156. extern float lsf_weightTbl_20ms[];
  3157. /* state quantization tables */
  3158. extern float state_sq3Tbl[];
  3159. extern float state_frgqTbl[];
  3160. /* gain quantization tables */
  3161. extern float gain_sq3Tbl[];
  3162. extern float gain_sq4Tbl[];
  3163. extern float gain_sq5Tbl[];
  3164. /* adaptive codebook definitions */
  3165. extern int search_rangeTbl[5][CB_NSTAGES];
  3166. extern int memLfTbl[];
  3167. extern int stMemLTbl;
  3168. extern float cbfiltersTbl[CB_FILTERLEN];
  3169. /* enhancer definitions */
  3170. extern float polyphaserTbl[];
  3171. extern float enh_plocsTbl[];
  3172. Andersen, et al. Experimental [Page 81]
  3173. RFC 3951 Internet Low Bit Rate Codec December 2004
  3174. #endif
  3175. A.8. constants.c
  3176. /******************************************************************
  3177. iLBC Speech Coder ANSI-C Source Code
  3178. constants.c
  3179. Copyright (C) The Internet Society (2004).
  3180. All Rights Reserved.
  3181. ******************************************************************/
  3182. #include "iLBC_define.h"
  3183. /* ULP bit allocation */
  3184. /* 20 ms frame */
  3185. const iLBC_ULP_Inst_t ULP_20msTbl = {
  3186. /* LSF */
  3187. { {6,0,0,0,0}, {7,0,0,0,0}, {7,0,0,0,0},
  3188. {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}},
  3189. /* Start state location, gain and samples */
  3190. {2,0,0,0,0},
  3191. {1,0,0,0,0},
  3192. {6,0,0,0,0},
  3193. {0,1,2,0,0},
  3194. /* extra CB index and extra CB gain */
  3195. {{6,0,1,0,0}, {0,0,7,0,0}, {0,0,7,0,0}},
  3196. {{2,0,3,0,0}, {1,1,2,0,0}, {0,0,3,0,0}},
  3197. /* CB index and CB gain */
  3198. { {{7,0,1,0,0}, {0,0,7,0,0}, {0,0,7,0,0}},
  3199. {{0,0,8,0,0}, {0,0,8,0,0}, {0,0,8,0,0}},
  3200. {{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}},
  3201. {{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}}},
  3202. { {{1,2,2,0,0}, {1,1,2,0,0}, {0,0,3,0,0}},
  3203. {{1,1,3,0,0}, {0,2,2,0,0}, {0,0,3,0,0}},
  3204. {{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}},
  3205. {{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}}}
  3206. };
  3207. /* 30 ms frame */
  3208. const iLBC_ULP_Inst_t ULP_30msTbl = {
  3209. /* LSF */
  3210. Andersen, et al. Experimental [Page 82]
  3211. RFC 3951 Internet Low Bit Rate Codec December 2004
  3212. { {6,0,0,0,0}, {7,0,0,0,0}, {7,0,0,0,0},
  3213. {6,0,0,0,0}, {7,0,0,0,0}, {7,0,0,0,0}},
  3214. /* Start state location, gain and samples */
  3215. {3,0,0,0,0},
  3216. {1,0,0,0,0},
  3217. {6,0,0,0,0},
  3218. {0,1,2,0,0},
  3219. /* extra CB index and extra CB gain */
  3220. {{4,2,1,0,0}, {0,0,7,0,0}, {0,0,7,0,0}},
  3221. {{1,1,3,0,0}, {1,1,2,0,0}, {0,0,3,0,0}},
  3222. /* CB index and CB gain */
  3223. { {{6,1,1,0,0}, {0,0,7,0,0}, {0,0,7,0,0}},
  3224. {{0,7,1,0,0}, {0,0,8,0,0}, {0,0,8,0,0}},
  3225. {{0,7,1,0,0}, {0,0,8,0,0}, {0,0,8,0,0}},
  3226. {{0,7,1,0,0}, {0,0,8,0,0}, {0,0,8,0,0}}},
  3227. { {{1,2,2,0,0}, {1,2,1,0,0}, {0,0,3,0,0}},
  3228. {{0,2,3,0,0}, {0,2,2,0,0}, {0,0,3,0,0}},
  3229. {{0,1,4,0,0}, {0,1,3,0,0}, {0,0,3,0,0}},
  3230. {{0,1,4,0,0}, {0,1,3,0,0}, {0,0,3,0,0}}}
  3231. };
  3232. /* HP Filters */
  3233. float hpi_zero_coefsTbl[3] = {
  3234. (float)0.92727436, (float)-1.8544941, (float)0.92727436
  3235. };
  3236. float hpi_pole_coefsTbl[3] = {
  3237. (float)1.0, (float)-1.9059465, (float)0.9114024
  3238. };
  3239. float hpo_zero_coefsTbl[3] = {
  3240. (float)0.93980581, (float)-1.8795834, (float)0.93980581
  3241. };
  3242. float hpo_pole_coefsTbl[3] = {
  3243. (float)1.0, (float)-1.9330735, (float)0.93589199
  3244. };
  3245. /* LP Filter */
  3246. float lpFilt_coefsTbl[FILTERORDER_DS]={
  3247. (float)-0.066650, (float)0.125000, (float)0.316650,
  3248. (float)0.414063, (float)0.316650,
  3249. (float)0.125000, (float)-0.066650
  3250. };
  3251. /* State quantization tables */
  3252. float state_sq3Tbl[8] = {
  3253. (float)-3.719849, (float)-2.177490, (float)-1.130005,
  3254. Andersen, et al. Experimental [Page 83]
  3255. RFC 3951 Internet Low Bit Rate Codec December 2004
  3256. (float)-0.309692, (float)0.444214, (float)1.329712,
  3257. (float)2.436279, (float)3.983887
  3258. };
  3259. float state_frgqTbl[64] = {
  3260. (float)1.000085, (float)1.071695, (float)1.140395,
  3261. (float)1.206868, (float)1.277188, (float)1.351503,
  3262. (float)1.429380, (float)1.500727, (float)1.569049,
  3263. (float)1.639599, (float)1.707071, (float)1.781531,
  3264. (float)1.840799, (float)1.901550, (float)1.956695,
  3265. (float)2.006750, (float)2.055474, (float)2.102787,
  3266. (float)2.142819, (float)2.183592, (float)2.217962,
  3267. (float)2.257177, (float)2.295739, (float)2.332967,
  3268. (float)2.369248, (float)2.402792, (float)2.435080,
  3269. (float)2.468598, (float)2.503394, (float)2.539284,
  3270. (float)2.572944, (float)2.605036, (float)2.636331,
  3271. (float)2.668939, (float)2.698780, (float)2.729101,
  3272. (float)2.759786, (float)2.789834, (float)2.818679,
  3273. (float)2.848074, (float)2.877470, (float)2.906899,
  3274. (float)2.936655, (float)2.967804, (float)3.000115,
  3275. (float)3.033367, (float)3.066355, (float)3.104231,
  3276. (float)3.141499, (float)3.183012, (float)3.222952,
  3277. (float)3.265433, (float)3.308441, (float)3.350823,
  3278. (float)3.395275, (float)3.442793, (float)3.490801,
  3279. (float)3.542514, (float)3.604064, (float)3.666050,
  3280. (float)3.740994, (float)3.830749, (float)3.938770,
  3281. (float)4.101764
  3282. };
  3283. /* CB tables */
  3284. int search_rangeTbl[5][CB_NSTAGES]={{58,58,58}, {108,44,44},
  3285. {108,108,108}, {108,108,108}, {108,108,108}};
  3286. int stMemLTbl=85;
  3287. int memLfTbl[NASUB_MAX]={147,147,147,147};
  3288. /* expansion filter(s) */
  3289. float cbfiltersTbl[CB_FILTERLEN]={
  3290. (float)-0.034180, (float)0.108887, (float)-0.184326,
  3291. (float)0.806152, (float)0.713379, (float)-0.144043,
  3292. (float)0.083740, (float)-0.033691
  3293. };
  3294. /* Gain Quantization */
  3295. float gain_sq3Tbl[8]={
  3296. (float)-1.000000, (float)-0.659973, (float)-0.330017,
  3297. Andersen, et al. Experimental [Page 84]
  3298. RFC 3951 Internet Low Bit Rate Codec December 2004
  3299. (float)0.000000, (float)0.250000, (float)0.500000,
  3300. (float)0.750000, (float)1.00000};
  3301. float gain_sq4Tbl[16]={
  3302. (float)-1.049988, (float)-0.900024, (float)-0.750000,
  3303. (float)-0.599976, (float)-0.450012, (float)-0.299988,
  3304. (float)-0.150024, (float)0.000000, (float)0.150024,
  3305. (float)0.299988, (float)0.450012, (float)0.599976,
  3306. (float)0.750000, (float)0.900024, (float)1.049988,
  3307. (float)1.200012};
  3308. float gain_sq5Tbl[32]={
  3309. (float)0.037476, (float)0.075012, (float)0.112488,
  3310. (float)0.150024, (float)0.187500, (float)0.224976,
  3311. (float)0.262512, (float)0.299988, (float)0.337524,
  3312. (float)0.375000, (float)0.412476, (float)0.450012,
  3313. (float)0.487488, (float)0.525024, (float)0.562500,
  3314. (float)0.599976, (float)0.637512, (float)0.674988,
  3315. (float)0.712524, (float)0.750000, (float)0.787476,
  3316. (float)0.825012, (float)0.862488, (float)0.900024,
  3317. (float)0.937500, (float)0.974976, (float)1.012512,
  3318. (float)1.049988, (float)1.087524, (float)1.125000,
  3319. (float)1.162476, (float)1.200012};
  3320. /* Enhancer - Upsamling a factor 4 (ENH_UPS0 = 4) */
  3321. float polyphaserTbl[ENH_UPS0*(2*ENH_FL0+1)]={
  3322. (float)0.000000, (float)0.000000, (float)0.000000,
  3323. (float)1.000000,
  3324. (float)0.000000, (float)0.000000, (float)0.000000,
  3325. (float)0.015625, (float)-0.076904, (float)0.288330,
  3326. (float)0.862061,
  3327. (float)-0.106445, (float)0.018799, (float)-0.015625,
  3328. (float)0.023682, (float)-0.124268, (float)0.601563,
  3329. (float)0.601563,
  3330. (float)-0.124268, (float)0.023682, (float)-0.023682,
  3331. (float)0.018799, (float)-0.106445, (float)0.862061,
  3332. (float)0.288330,
  3333. (float)-0.076904, (float)0.015625, (float)-0.018799};
  3334. float enh_plocsTbl[ENH_NBLOCKS_TOT] = {(float)40.0, (float)120.0,
  3335. (float)200.0, (float)280.0, (float)360.0,
  3336. (float)440.0, (float)520.0, (float)600.0};
  3337. /* LPC analysis and quantization */
  3338. int dim_lsfCbTbl[LSF_NSPLIT] = {3, 3, 4};
  3339. int size_lsfCbTbl[LSF_NSPLIT] = {64,128,128};
  3340. Andersen, et al. Experimental [Page 85]
  3341. RFC 3951 Internet Low Bit Rate Codec December 2004
  3342. float lsfmeanTbl[LPC_FILTERORDER] = {
  3343. (float)0.281738, (float)0.445801, (float)0.663330,
  3344. (float)0.962524, (float)1.251831, (float)1.533081,
  3345. (float)1.850586, (float)2.137817, (float)2.481445,
  3346. (float)2.777344};
  3347. float lsf_weightTbl_30ms[6] = {(float)(1.0/2.0), (float)1.0,
  3348. (float)(2.0/3.0),
  3349. (float)(1.0/3.0), (float)0.0, (float)0.0};
  3350. float lsf_weightTbl_20ms[4] = {(float)(3.0/4.0), (float)(2.0/4.0),
  3351. (float)(1.0/4.0), (float)(0.0)};
  3352. /* Hanning LPC window */
  3353. float lpc_winTbl[BLOCKL_MAX]={
  3354. (float)0.000183, (float)0.000671, (float)0.001526,
  3355. (float)0.002716, (float)0.004242, (float)0.006104,
  3356. (float)0.008301, (float)0.010834, (float)0.013702,
  3357. (float)0.016907, (float)0.020416, (float)0.024261,
  3358. (float)0.028442, (float)0.032928, (float)0.037750,
  3359. (float)0.042877, (float)0.048309, (float)0.054047,
  3360. (float)0.060089, (float)0.066437, (float)0.073090,
  3361. (float)0.080017, (float)0.087219, (float)0.094727,
  3362. (float)0.102509, (float)0.110535, (float)0.118835,
  3363. (float)0.127411, (float)0.136230, (float)0.145294,
  3364. (float)0.154602, (float)0.164154, (float)0.173920,
  3365. (float)0.183899, (float)0.194122, (float)0.204529,
  3366. (float)0.215149, (float)0.225952, (float)0.236938,
  3367. (float)0.248108, (float)0.259460, (float)0.270966,
  3368. (float)0.282654, (float)0.294464, (float)0.306396,
  3369. (float)0.318481, (float)0.330688, (float)0.343018,
  3370. (float)0.355438, (float)0.367981, (float)0.380585,
  3371. (float)0.393280, (float)0.406067, (float)0.418884,
  3372. (float)0.431763, (float)0.444702, (float)0.457672,
  3373. (float)0.470673, (float)0.483704, (float)0.496735,
  3374. (float)0.509766, (float)0.522797, (float)0.535828,
  3375. (float)0.548798, (float)0.561768, (float)0.574677,
  3376. (float)0.587524, (float)0.600342, (float)0.613068,
  3377. (float)0.625732, (float)0.638306, (float)0.650787,
  3378. (float)0.663147, (float)0.675415, (float)0.687561,
  3379. (float)0.699585, (float)0.711487, (float)0.723206,
  3380. (float)0.734802, (float)0.746216, (float)0.757477,
  3381. (float)0.768585, (float)0.779480, (float)0.790192,
  3382. (float)0.800720, (float)0.811005, (float)0.821106,
  3383. (float)0.830994, (float)0.840668, (float)0.850067,
  3384. (float)0.859253, (float)0.868225, (float)0.876892,
  3385. (float)0.885345, (float)0.893524, (float)0.901428,
  3386. (float)0.909058, (float)0.916412, (float)0.923492,
  3387. Andersen, et al. Experimental [Page 86]
  3388. RFC 3951 Internet Low Bit Rate Codec December 2004
  3389. (float)0.930267, (float)0.936768, (float)0.942963,
  3390. (float)0.948853, (float)0.954437, (float)0.959717,
  3391. (float)0.964691, (float)0.969360, (float)0.973694,
  3392. (float)0.977692, (float)0.981384, (float)0.984741,
  3393. (float)0.987762, (float)0.990479, (float)0.992828,
  3394. (float)0.994873, (float)0.996552, (float)0.997925,
  3395. (float)0.998932, (float)0.999603, (float)0.999969,
  3396. (float)0.999969, (float)0.999603, (float)0.998932,
  3397. (float)0.997925, (float)0.996552, (float)0.994873,
  3398. (float)0.992828, (float)0.990479, (float)0.987762,
  3399. (float)0.984741, (float)0.981384, (float)0.977692,
  3400. (float)0.973694, (float)0.969360, (float)0.964691,
  3401. (float)0.959717, (float)0.954437, (float)0.948853,
  3402. (float)0.942963, (float)0.936768, (float)0.930267,
  3403. (float)0.923492, (float)0.916412, (float)0.909058,
  3404. (float)0.901428, (float)0.893524, (float)0.885345,
  3405. (float)0.876892, (float)0.868225, (float)0.859253,
  3406. (float)0.850067, (float)0.840668, (float)0.830994,
  3407. (float)0.821106, (float)0.811005, (float)0.800720,
  3408. (float)0.790192, (float)0.779480, (float)0.768585,
  3409. (float)0.757477, (float)0.746216, (float)0.734802,
  3410. (float)0.723206, (float)0.711487, (float)0.699585,
  3411. (float)0.687561, (float)0.675415, (float)0.663147,
  3412. (float)0.650787, (float)0.638306, (float)0.625732,
  3413. (float)0.613068, (float)0.600342, (float)0.587524,
  3414. (float)0.574677, (float)0.561768, (float)0.548798,
  3415. (float)0.535828, (float)0.522797, (float)0.509766,
  3416. (float)0.496735, (float)0.483704, (float)0.470673,
  3417. (float)0.457672, (float)0.444702, (float)0.431763,
  3418. (float)0.418884, (float)0.406067, (float)0.393280,
  3419. (float)0.380585, (float)0.367981, (float)0.355438,
  3420. (float)0.343018, (float)0.330688, (float)0.318481,
  3421. (float)0.306396, (float)0.294464, (float)0.282654,
  3422. (float)0.270966, (float)0.259460, (float)0.248108,
  3423. (float)0.236938, (float)0.225952, (float)0.215149,
  3424. (float)0.204529, (float)0.194122, (float)0.183899,
  3425. (float)0.173920, (float)0.164154, (float)0.154602,
  3426. (float)0.145294, (float)0.136230, (float)0.127411,
  3427. (float)0.118835, (float)0.110535, (float)0.102509,
  3428. (float)0.094727, (float)0.087219, (float)0.080017,
  3429. (float)0.073090, (float)0.066437, (float)0.060089,
  3430. (float)0.054047, (float)0.048309, (float)0.042877,
  3431. (float)0.037750, (float)0.032928, (float)0.028442,
  3432. (float)0.024261, (float)0.020416, (float)0.016907,
  3433. (float)0.013702, (float)0.010834, (float)0.008301,
  3434. (float)0.006104, (float)0.004242, (float)0.002716,
  3435. (float)0.001526, (float)0.000671, (float)0.000183
  3436. };
  3437. Andersen, et al. Experimental [Page 87]
  3438. RFC 3951 Internet Low Bit Rate Codec December 2004
  3439. /* Asymmetric LPC window */
  3440. float lpc_asymwinTbl[BLOCKL_MAX]={
  3441. (float)0.000061, (float)0.000214, (float)0.000458,
  3442. (float)0.000824, (float)0.001282, (float)0.001831,
  3443. (float)0.002472, (float)0.003235, (float)0.004120,
  3444. (float)0.005066, (float)0.006134, (float)0.007294,
  3445. (float)0.008545, (float)0.009918, (float)0.011383,
  3446. (float)0.012939, (float)0.014587, (float)0.016357,
  3447. (float)0.018219, (float)0.020172, (float)0.022217,
  3448. (float)0.024353, (float)0.026611, (float)0.028961,
  3449. (float)0.031372, (float)0.033905, (float)0.036530,
  3450. (float)0.039276, (float)0.042084, (float)0.044983,
  3451. (float)0.047974, (float)0.051086, (float)0.054260,
  3452. (float)0.057526, (float)0.060883, (float)0.064331,
  3453. (float)0.067871, (float)0.071503, (float)0.075226,
  3454. (float)0.079010, (float)0.082916, (float)0.086884,
  3455. (float)0.090942, (float)0.095062, (float)0.099304,
  3456. (float)0.103607, (float)0.107971, (float)0.112427,
  3457. (float)0.116974, (float)0.121582, (float)0.126282,
  3458. (float)0.131073, (float)0.135895, (float)0.140839,
  3459. (float)0.145813, (float)0.150879, (float)0.156006,
  3460. (float)0.161224, (float)0.166504, (float)0.171844,
  3461. (float)0.177246, (float)0.182709, (float)0.188263,
  3462. (float)0.193848, (float)0.199524, (float)0.205231,
  3463. (float)0.211029, (float)0.216858, (float)0.222778,
  3464. (float)0.228729, (float)0.234741, (float)0.240814,
  3465. (float)0.246918, (float)0.253082, (float)0.259308,
  3466. (float)0.265564, (float)0.271881, (float)0.278259,
  3467. (float)0.284668, (float)0.291107, (float)0.297607,
  3468. (float)0.304138, (float)0.310730, (float)0.317322,
  3469. (float)0.323975, (float)0.330658, (float)0.337372,
  3470. (float)0.344147, (float)0.350922, (float)0.357727,
  3471. (float)0.364594, (float)0.371460, (float)0.378357,
  3472. (float)0.385284, (float)0.392212, (float)0.399170,
  3473. (float)0.406158, (float)0.413177, (float)0.420197,
  3474. (float)0.427246, (float)0.434296, (float)0.441376,
  3475. (float)0.448456, (float)0.455536, (float)0.462646,
  3476. (float)0.469757, (float)0.476868, (float)0.483978,
  3477. (float)0.491089, (float)0.498230, (float)0.505341,
  3478. (float)0.512451, (float)0.519592, (float)0.526703,
  3479. (float)0.533813, (float)0.540924, (float)0.548004,
  3480. (float)0.555084, (float)0.562164, (float)0.569244,
  3481. (float)0.576294, (float)0.583313, (float)0.590332,
  3482. (float)0.597321, (float)0.604309, (float)0.611267,
  3483. (float)0.618195, (float)0.625092, (float)0.631989,
  3484. (float)0.638855, (float)0.645660, (float)0.652466,
  3485. (float)0.659241, (float)0.665985, (float)0.672668,
  3486. (float)0.679352, (float)0.685974, (float)0.692566,
  3487. Andersen, et al. Experimental [Page 88]
  3488. RFC 3951 Internet Low Bit Rate Codec December 2004
  3489. (float)0.699127, (float)0.705658, (float)0.712128,
  3490. (float)0.718536, (float)0.724945, (float)0.731262,
  3491. (float)0.737549, (float)0.743805, (float)0.750000,
  3492. (float)0.756134, (float)0.762238, (float)0.768280,
  3493. (float)0.774261, (float)0.780182, (float)0.786072,
  3494. (float)0.791870, (float)0.797638, (float)0.803314,
  3495. (float)0.808960, (float)0.814514, (float)0.820038,
  3496. (float)0.825470, (float)0.830841, (float)0.836151,
  3497. (float)0.841400, (float)0.846558, (float)0.851654,
  3498. (float)0.856689, (float)0.861633, (float)0.866516,
  3499. (float)0.871338, (float)0.876068, (float)0.880737,
  3500. (float)0.885315, (float)0.889801, (float)0.894226,
  3501. (float)0.898560, (float)0.902832, (float)0.907013,
  3502. (float)0.911102, (float)0.915100, (float)0.919037,
  3503. (float)0.922882, (float)0.926636, (float)0.930328,
  3504. (float)0.933899, (float)0.937408, (float)0.940796,
  3505. (float)0.944122, (float)0.947357, (float)0.950470,
  3506. (float)0.953522, (float)0.956482, (float)0.959351,
  3507. (float)0.962097, (float)0.964783, (float)0.967377,
  3508. (float)0.969849, (float)0.972229, (float)0.974518,
  3509. (float)0.976715, (float)0.978821, (float)0.980835,
  3510. (float)0.982727, (float)0.984528, (float)0.986237,
  3511. (float)0.987854, (float)0.989380, (float)0.990784,
  3512. (float)0.992096, (float)0.993317, (float)0.994415,
  3513. (float)0.995422, (float)0.996338, (float)0.997162,
  3514. (float)0.997864, (float)0.998474, (float)0.998962,
  3515. (float)0.999390, (float)0.999695, (float)0.999878,
  3516. (float)0.999969, (float)0.999969, (float)0.996918,
  3517. (float)0.987701, (float)0.972382, (float)0.951050,
  3518. (float)0.923889, (float)0.891022, (float)0.852631,
  3519. (float)0.809021, (float)0.760406, (float)0.707092,
  3520. (float)0.649445, (float)0.587799, (float)0.522491,
  3521. (float)0.453979, (float)0.382690, (float)0.309021,
  3522. (float)0.233459, (float)0.156433, (float)0.078461
  3523. };
  3524. /* Lag window for LPC */
  3525. float lpc_lagwinTbl[LPC_FILTERORDER + 1]={
  3526. (float)1.000100, (float)0.998890, (float)0.995569,
  3527. (float)0.990057, (float)0.982392,
  3528. (float)0.972623, (float)0.960816, (float)0.947047,
  3529. (float)0.931405, (float)0.913989, (float)0.894909};
  3530. /* LSF quantization*/
  3531. float lsfCbTbl[64 * 3 + 128 * 3 + 128 * 4] = {
  3532. (float)0.155396, (float)0.273193, (float)0.451172,
  3533. (float)0.390503, (float)0.648071, (float)1.002075,
  3534. (float)0.440186, (float)0.692261, (float)0.955688,
  3535. Andersen, et al. Experimental [Page 89]
  3536. RFC 3951 Internet Low Bit Rate Codec December 2004
  3537. (float)0.343628, (float)0.642334, (float)1.071533,
  3538. (float)0.318359, (float)0.491577, (float)0.670532,
  3539. (float)0.193115, (float)0.375488, (float)0.725708,
  3540. (float)0.364136, (float)0.510376, (float)0.658691,
  3541. (float)0.297485, (float)0.527588, (float)0.842529,
  3542. (float)0.227173, (float)0.365967, (float)0.563110,
  3543. (float)0.244995, (float)0.396729, (float)0.636475,
  3544. (float)0.169434, (float)0.300171, (float)0.520264,
  3545. (float)0.312866, (float)0.464478, (float)0.643188,
  3546. (float)0.248535, (float)0.429932, (float)0.626099,
  3547. (float)0.236206, (float)0.491333, (float)0.817139,
  3548. (float)0.334961, (float)0.625122, (float)0.895752,
  3549. (float)0.343018, (float)0.518555, (float)0.698608,
  3550. (float)0.372803, (float)0.659790, (float)0.945435,
  3551. (float)0.176880, (float)0.316528, (float)0.581421,
  3552. (float)0.416382, (float)0.625977, (float)0.805176,
  3553. (float)0.303223, (float)0.568726, (float)0.915039,
  3554. (float)0.203613, (float)0.351440, (float)0.588135,
  3555. (float)0.221191, (float)0.375000, (float)0.614746,
  3556. (float)0.199951, (float)0.323364, (float)0.476074,
  3557. (float)0.300781, (float)0.433350, (float)0.566895,
  3558. (float)0.226196, (float)0.354004, (float)0.507568,
  3559. (float)0.300049, (float)0.508179, (float)0.711670,
  3560. (float)0.312012, (float)0.492676, (float)0.763428,
  3561. (float)0.329956, (float)0.541016, (float)0.795776,
  3562. (float)0.373779, (float)0.604614, (float)0.928833,
  3563. (float)0.210571, (float)0.452026, (float)0.755249,
  3564. (float)0.271118, (float)0.473267, (float)0.662476,
  3565. (float)0.285522, (float)0.436890, (float)0.634399,
  3566. (float)0.246704, (float)0.565552, (float)0.859009,
  3567. (float)0.270508, (float)0.406250, (float)0.553589,
  3568. (float)0.361450, (float)0.578491, (float)0.813843,
  3569. (float)0.342651, (float)0.482788, (float)0.622437,
  3570. (float)0.340332, (float)0.549438, (float)0.743164,
  3571. (float)0.200439, (float)0.336304, (float)0.540894,
  3572. (float)0.407837, (float)0.644775, (float)0.895142,
  3573. (float)0.294678, (float)0.454834, (float)0.699097,
  3574. (float)0.193115, (float)0.344482, (float)0.643188,
  3575. (float)0.275757, (float)0.420776, (float)0.598755,
  3576. (float)0.380493, (float)0.608643, (float)0.861084,
  3577. (float)0.222778, (float)0.426147, (float)0.676514,
  3578. (float)0.407471, (float)0.700195, (float)1.053101,
  3579. (float)0.218384, (float)0.377197, (float)0.669922,
  3580. (float)0.313232, (float)0.454102, (float)0.600952,
  3581. (float)0.347412, (float)0.571533, (float)0.874146,
  3582. (float)0.238037, (float)0.405396, (float)0.729492,
  3583. (float)0.223877, (float)0.412964, (float)0.822021,
  3584. (float)0.395264, (float)0.582153, (float)0.743896,
  3585. Andersen, et al. Experimental [Page 90]
  3586. RFC 3951 Internet Low Bit Rate Codec December 2004
  3587. (float)0.247925, (float)0.485596, (float)0.720581,
  3588. (float)0.229126, (float)0.496582, (float)0.907715,
  3589. (float)0.260132, (float)0.566895, (float)1.012695,
  3590. (float)0.337402, (float)0.611572, (float)0.978149,
  3591. (float)0.267822, (float)0.447632, (float)0.769287,
  3592. (float)0.250610, (float)0.381714, (float)0.530029,
  3593. (float)0.430054, (float)0.805054, (float)1.221924,
  3594. (float)0.382568, (float)0.544067, (float)0.701660,
  3595. (float)0.383545, (float)0.710327, (float)1.149170,
  3596. (float)0.271362, (float)0.529053, (float)0.775513,
  3597. (float)0.246826, (float)0.393555, (float)0.588623,
  3598. (float)0.266846, (float)0.422119, (float)0.676758,
  3599. (float)0.311523, (float)0.580688, (float)0.838623,
  3600. (float)1.331177, (float)1.576782, (float)1.779541,
  3601. (float)1.160034, (float)1.401978, (float)1.768188,
  3602. (float)1.161865, (float)1.525146, (float)1.715332,
  3603. (float)0.759521, (float)0.913940, (float)1.119873,
  3604. (float)0.947144, (float)1.121338, (float)1.282471,
  3605. (float)1.015015, (float)1.557007, (float)1.804932,
  3606. (float)1.172974, (float)1.402100, (float)1.692627,
  3607. (float)1.087524, (float)1.474243, (float)1.665405,
  3608. (float)0.899536, (float)1.105225, (float)1.406250,
  3609. (float)1.148438, (float)1.484741, (float)1.796265,
  3610. (float)0.785645, (float)1.209839, (float)1.567749,
  3611. (float)0.867798, (float)1.166504, (float)1.450684,
  3612. (float)0.922485, (float)1.229858, (float)1.420898,
  3613. (float)0.791260, (float)1.123291, (float)1.409546,
  3614. (float)0.788940, (float)0.966064, (float)1.340332,
  3615. (float)1.051147, (float)1.272827, (float)1.556641,
  3616. (float)0.866821, (float)1.181152, (float)1.538818,
  3617. (float)0.906738, (float)1.373535, (float)1.607910,
  3618. (float)1.244751, (float)1.581421, (float)1.933838,
  3619. (float)0.913940, (float)1.337280, (float)1.539673,
  3620. (float)0.680542, (float)0.959229, (float)1.662720,
  3621. (float)0.887207, (float)1.430542, (float)1.800781,
  3622. (float)0.912598, (float)1.433594, (float)1.683960,
  3623. (float)0.860474, (float)1.060303, (float)1.455322,
  3624. (float)1.005127, (float)1.381104, (float)1.706909,
  3625. (float)0.800781, (float)1.363892, (float)1.829102,
  3626. (float)0.781860, (float)1.124390, (float)1.505981,
  3627. (float)1.003662, (float)1.471436, (float)1.684692,
  3628. (float)0.981323, (float)1.309570, (float)1.618042,
  3629. (float)1.228760, (float)1.554321, (float)1.756470,
  3630. (float)0.734375, (float)0.895752, (float)1.225586,
  3631. (float)0.841797, (float)1.055664, (float)1.249268,
  3632. (float)0.920166, (float)1.119385, (float)1.486206,
  3633. (float)0.894409, (float)1.539063, (float)1.828979,
  3634. (float)1.283691, (float)1.543335, (float)1.858276,
  3635. Andersen, et al. Experimental [Page 91]
  3636. RFC 3951 Internet Low Bit Rate Codec December 2004
  3637. (float)0.676025, (float)0.933105, (float)1.490845,
  3638. (float)0.821289, (float)1.491821, (float)1.739868,
  3639. (float)0.923218, (float)1.144653, (float)1.580566,
  3640. (float)1.057251, (float)1.345581, (float)1.635864,
  3641. (float)0.888672, (float)1.074951, (float)1.353149,
  3642. (float)0.942749, (float)1.195435, (float)1.505493,
  3643. (float)1.492310, (float)1.788086, (float)2.039673,
  3644. (float)1.070313, (float)1.634399, (float)1.860962,
  3645. (float)1.253296, (float)1.488892, (float)1.686035,
  3646. (float)0.647095, (float)0.864014, (float)1.401855,
  3647. (float)0.866699, (float)1.254883, (float)1.453369,
  3648. (float)1.063965, (float)1.532593, (float)1.731323,
  3649. (float)1.167847, (float)1.521484, (float)1.884033,
  3650. (float)0.956055, (float)1.502075, (float)1.745605,
  3651. (float)0.928711, (float)1.288574, (float)1.479614,
  3652. (float)1.088013, (float)1.380737, (float)1.570801,
  3653. (float)0.905029, (float)1.186768, (float)1.371948,
  3654. (float)1.057861, (float)1.421021, (float)1.617432,
  3655. (float)1.108276, (float)1.312500, (float)1.501465,
  3656. (float)0.979492, (float)1.416992, (float)1.624268,
  3657. (float)1.276001, (float)1.661011, (float)2.007935,
  3658. (float)0.993042, (float)1.168579, (float)1.331665,
  3659. (float)0.778198, (float)0.944946, (float)1.235962,
  3660. (float)1.223755, (float)1.491333, (float)1.815674,
  3661. (float)0.852661, (float)1.350464, (float)1.722290,
  3662. (float)1.134766, (float)1.593140, (float)1.787354,
  3663. (float)1.051392, (float)1.339722, (float)1.531006,
  3664. (float)0.803589, (float)1.271240, (float)1.652100,
  3665. (float)0.755737, (float)1.143555, (float)1.639404,
  3666. (float)0.700928, (float)0.837280, (float)1.130371,
  3667. (float)0.942749, (float)1.197876, (float)1.669800,
  3668. (float)0.993286, (float)1.378296, (float)1.566528,
  3669. (float)0.801025, (float)1.095337, (float)1.298950,
  3670. (float)0.739990, (float)1.032959, (float)1.383667,
  3671. (float)0.845703, (float)1.072266, (float)1.543823,
  3672. (float)0.915649, (float)1.072266, (float)1.224487,
  3673. (float)1.021973, (float)1.226196, (float)1.481323,
  3674. (float)0.999878, (float)1.204102, (float)1.555908,
  3675. (float)0.722290, (float)0.913940, (float)1.340210,
  3676. (float)0.673340, (float)0.835938, (float)1.259521,
  3677. (float)0.832397, (float)1.208374, (float)1.394165,
  3678. (float)0.962158, (float)1.576172, (float)1.912842,
  3679. (float)1.166748, (float)1.370850, (float)1.556763,
  3680. (float)0.946289, (float)1.138550, (float)1.400391,
  3681. (float)1.035034, (float)1.218262, (float)1.386475,
  3682. (float)1.393799, (float)1.717773, (float)2.000244,
  3683. (float)0.972656, (float)1.260986, (float)1.760620,
  3684. (float)1.028198, (float)1.288452, (float)1.484619,
  3685. Andersen, et al. Experimental [Page 92]
  3686. RFC 3951 Internet Low Bit Rate Codec December 2004
  3687. (float)0.773560, (float)1.258057, (float)1.756714,
  3688. (float)1.080322, (float)1.328003, (float)1.742676,
  3689. (float)0.823975, (float)1.450806, (float)1.917725,
  3690. (float)0.859009, (float)1.016602, (float)1.191895,
  3691. (float)0.843994, (float)1.131104, (float)1.645020,
  3692. (float)1.189697, (float)1.702759, (float)1.894409,
  3693. (float)1.346680, (float)1.763184, (float)2.066040,
  3694. (float)0.980469, (float)1.253784, (float)1.441650,
  3695. (float)1.338135, (float)1.641968, (float)1.932739,
  3696. (float)1.223267, (float)1.424194, (float)1.626465,
  3697. (float)0.765747, (float)1.004150, (float)1.579102,
  3698. (float)1.042847, (float)1.269165, (float)1.647461,
  3699. (float)0.968750, (float)1.257568, (float)1.555786,
  3700. (float)0.826294, (float)0.993408, (float)1.275146,
  3701. (float)0.742310, (float)0.950439, (float)1.430542,
  3702. (float)1.054321, (float)1.439819, (float)1.828003,
  3703. (float)1.072998, (float)1.261719, (float)1.441895,
  3704. (float)0.859375, (float)1.036377, (float)1.314819,
  3705. (float)0.895752, (float)1.267212, (float)1.605591,
  3706. (float)0.805420, (float)0.962891, (float)1.142334,
  3707. (float)0.795654, (float)1.005493, (float)1.468506,
  3708. (float)1.105347, (float)1.313843, (float)1.584839,
  3709. (float)0.792236, (float)1.221802, (float)1.465698,
  3710. (float)1.170532, (float)1.467651, (float)1.664063,
  3711. (float)0.838257, (float)1.153198, (float)1.342163,
  3712. (float)0.968018, (float)1.198242, (float)1.391235,
  3713. (float)1.250122, (float)1.623535, (float)1.823608,
  3714. (float)0.711670, (float)1.058350, (float)1.512085,
  3715. (float)1.204834, (float)1.454468, (float)1.739136,
  3716. (float)1.137451, (float)1.421753, (float)1.620117,
  3717. (float)0.820435, (float)1.322754, (float)1.578247,
  3718. (float)0.798706, (float)1.005005, (float)1.213867,
  3719. (float)0.980713, (float)1.324951, (float)1.512939,
  3720. (float)1.112305, (float)1.438843, (float)1.735596,
  3721. (float)1.135498, (float)1.356689, (float)1.635742,
  3722. (float)1.101318, (float)1.387451, (float)1.686523,
  3723. (float)0.849854, (float)1.276978, (float)1.523438,
  3724. (float)1.377930, (float)1.627563, (float)1.858154,
  3725. (float)0.884888, (float)1.095459, (float)1.287476,
  3726. (float)1.289795, (float)1.505859, (float)1.756592,
  3727. (float)0.817505, (float)1.384155, (float)1.650513,
  3728. (float)1.446655, (float)1.702148, (float)1.931885,
  3729. (float)0.835815, (float)1.023071, (float)1.385376,
  3730. (float)0.916626, (float)1.139038, (float)1.335327,
  3731. (float)0.980103, (float)1.174072, (float)1.453735,
  3732. (float)1.705688, (float)2.153809, (float)2.398315, (float)2.743408,
  3733. (float)1.797119, (float)2.016846, (float)2.445679, (float)2.701904,
  3734. (float)1.990356, (float)2.219116, (float)2.576416, (float)2.813477,
  3735. Andersen, et al. Experimental [Page 93]
  3736. RFC 3951 Internet Low Bit Rate Codec December 2004
  3737. (float)1.849365, (float)2.190918, (float)2.611572, (float)2.835083,
  3738. (float)1.657959, (float)1.854370, (float)2.159058, (float)2.726196,
  3739. (float)1.437744, (float)1.897705, (float)2.253174, (float)2.655396,
  3740. (float)2.028687, (float)2.247314, (float)2.542358, (float)2.875854,
  3741. (float)1.736938, (float)1.922119, (float)2.185913, (float)2.743408,
  3742. (float)1.521606, (float)1.870972, (float)2.526855, (float)2.786987,
  3743. (float)1.841431, (float)2.050659, (float)2.463623, (float)2.857666,
  3744. (float)1.590088, (float)2.067261, (float)2.427979, (float)2.794434,
  3745. (float)1.746826, (float)2.057373, (float)2.320190, (float)2.800781,
  3746. (float)1.734619, (float)1.940552, (float)2.306030, (float)2.826416,
  3747. (float)1.786255, (float)2.204468, (float)2.457520, (float)2.795288,
  3748. (float)1.861084, (float)2.170532, (float)2.414551, (float)2.763672,
  3749. (float)2.001465, (float)2.307617, (float)2.552734, (float)2.811890,
  3750. (float)1.784424, (float)2.124146, (float)2.381592, (float)2.645508,
  3751. (float)1.888794, (float)2.135864, (float)2.418579, (float)2.861206,
  3752. (float)2.301147, (float)2.531250, (float)2.724976, (float)2.913086,
  3753. (float)1.837769, (float)2.051270, (float)2.261963, (float)2.553223,
  3754. (float)2.012939, (float)2.221191, (float)2.440186, (float)2.678101,
  3755. (float)1.429565, (float)1.858276, (float)2.582275, (float)2.845703,
  3756. (float)1.622803, (float)1.897705, (float)2.367310, (float)2.621094,
  3757. (float)1.581543, (float)1.960449, (float)2.515869, (float)2.736450,
  3758. (float)1.419434, (float)1.933960, (float)2.394653, (float)2.746704,
  3759. (float)1.721924, (float)2.059570, (float)2.421753, (float)2.769653,
  3760. (float)1.911011, (float)2.220703, (float)2.461060, (float)2.740723,
  3761. (float)1.581177, (float)1.860840, (float)2.516968, (float)2.874634,
  3762. (float)1.870361, (float)2.098755, (float)2.432373, (float)2.656494,
  3763. (float)2.059692, (float)2.279785, (float)2.495605, (float)2.729370,
  3764. (float)1.815674, (float)2.181519, (float)2.451538, (float)2.680542,
  3765. (float)1.407959, (float)1.768311, (float)2.343018, (float)2.668091,
  3766. (float)2.168701, (float)2.394653, (float)2.604736, (float)2.829346,
  3767. (float)1.636230, (float)1.865723, (float)2.329102, (float)2.824219,
  3768. (float)1.878906, (float)2.139526, (float)2.376709, (float)2.679810,
  3769. (float)1.765381, (float)1.971802, (float)2.195435, (float)2.586914,
  3770. (float)2.164795, (float)2.410889, (float)2.673706, (float)2.903198,
  3771. (float)2.071899, (float)2.331055, (float)2.645874, (float)2.907104,
  3772. (float)2.026001, (float)2.311523, (float)2.594849, (float)2.863892,
  3773. (float)1.948975, (float)2.180786, (float)2.514893, (float)2.797852,
  3774. (float)1.881836, (float)2.130859, (float)2.478149, (float)2.804199,
  3775. (float)2.238159, (float)2.452759, (float)2.652832, (float)2.868286,
  3776. (float)1.897949, (float)2.101685, (float)2.524292, (float)2.880127,
  3777. (float)1.856445, (float)2.074585, (float)2.541016, (float)2.791748,
  3778. (float)1.695557, (float)2.199097, (float)2.506226, (float)2.742676,
  3779. (float)1.612671, (float)1.877075, (float)2.435425, (float)2.732910,
  3780. (float)1.568848, (float)1.786499, (float)2.194580, (float)2.768555,
  3781. (float)1.953369, (float)2.164551, (float)2.486938, (float)2.874023,
  3782. (float)1.388306, (float)1.725342, (float)2.384521, (float)2.771851,
  3783. (float)2.115356, (float)2.337769, (float)2.592896, (float)2.864014,
  3784. (float)1.905762, (float)2.111328, (float)2.363525, (float)2.789307,
  3785. Andersen, et al. Experimental [Page 94]
  3786. RFC 3951 Internet Low Bit Rate Codec December 2004
  3787. (float)1.882568, (float)2.332031, (float)2.598267, (float)2.827637,
  3788. (float)1.683594, (float)2.088745, (float)2.361938, (float)2.608643,
  3789. (float)1.874023, (float)2.182129, (float)2.536133, (float)2.766968,
  3790. (float)1.861938, (float)2.070435, (float)2.309692, (float)2.700562,
  3791. (float)1.722168, (float)2.107422, (float)2.477295, (float)2.837646,
  3792. (float)1.926880, (float)2.184692, (float)2.442627, (float)2.663818,
  3793. (float)2.123901, (float)2.337280, (float)2.553101, (float)2.777466,
  3794. (float)1.588135, (float)1.911499, (float)2.212769, (float)2.543945,
  3795. (float)2.053955, (float)2.370850, (float)2.712158, (float)2.939941,
  3796. (float)2.210449, (float)2.519653, (float)2.770386, (float)2.958618,
  3797. (float)2.199463, (float)2.474731, (float)2.718262, (float)2.919922,
  3798. (float)1.960083, (float)2.175415, (float)2.608032, (float)2.888794,
  3799. (float)1.953735, (float)2.185181, (float)2.428223, (float)2.809570,
  3800. (float)1.615234, (float)2.036499, (float)2.576538, (float)2.834595,
  3801. (float)1.621094, (float)2.028198, (float)2.431030, (float)2.664673,
  3802. (float)1.824951, (float)2.267456, (float)2.514526, (float)2.747925,
  3803. (float)1.994263, (float)2.229126, (float)2.475220, (float)2.833984,
  3804. (float)1.746338, (float)2.011353, (float)2.588257, (float)2.826904,
  3805. (float)1.562866, (float)2.135986, (float)2.471680, (float)2.687256,
  3806. (float)1.748901, (float)2.083496, (float)2.460938, (float)2.686279,
  3807. (float)1.758057, (float)2.131470, (float)2.636597, (float)2.891602,
  3808. (float)2.071289, (float)2.299072, (float)2.550781, (float)2.814331,
  3809. (float)1.839600, (float)2.094360, (float)2.496460, (float)2.723999,
  3810. (float)1.882202, (float)2.088257, (float)2.636841, (float)2.923096,
  3811. (float)1.957886, (float)2.153198, (float)2.384399, (float)2.615234,
  3812. (float)1.992920, (float)2.351196, (float)2.654419, (float)2.889771,
  3813. (float)2.012817, (float)2.262451, (float)2.643799, (float)2.903076,
  3814. (float)2.025635, (float)2.254761, (float)2.508423, (float)2.784058,
  3815. (float)2.316040, (float)2.589355, (float)2.794189, (float)2.963623,
  3816. (float)1.741211, (float)2.279541, (float)2.578491, (float)2.816284,
  3817. (float)1.845337, (float)2.055786, (float)2.348511, (float)2.822021,
  3818. (float)1.679932, (float)1.926514, (float)2.499756, (float)2.835693,
  3819. (float)1.722534, (float)1.946899, (float)2.448486, (float)2.728760,
  3820. (float)1.829834, (float)2.043213, (float)2.580444, (float)2.867676,
  3821. (float)1.676636, (float)2.071655, (float)2.322510, (float)2.704834,
  3822. (float)1.791504, (float)2.113525, (float)2.469727, (float)2.784058,
  3823. (float)1.977051, (float)2.215088, (float)2.497437, (float)2.726929,
  3824. (float)1.800171, (float)2.106689, (float)2.357788, (float)2.738892,
  3825. (float)1.827759, (float)2.170166, (float)2.525879, (float)2.852417,
  3826. (float)1.918335, (float)2.132813, (float)2.488403, (float)2.728149,
  3827. (float)1.916748, (float)2.225098, (float)2.542603, (float)2.857666,
  3828. (float)1.761230, (float)1.976074, (float)2.507446, (float)2.884521,
  3829. (float)2.053711, (float)2.367432, (float)2.608032, (float)2.837646,
  3830. (float)1.595337, (float)2.000977, (float)2.307129, (float)2.578247,
  3831. (float)1.470581, (float)2.031250, (float)2.375854, (float)2.647583,
  3832. (float)1.801392, (float)2.128052, (float)2.399780, (float)2.822876,
  3833. (float)1.853638, (float)2.066650, (float)2.429199, (float)2.751465,
  3834. (float)1.956299, (float)2.163696, (float)2.394775, (float)2.734253,
  3835. Andersen, et al. Experimental [Page 95]
  3836. RFC 3951 Internet Low Bit Rate Codec December 2004
  3837. (float)1.963623, (float)2.275757, (float)2.585327, (float)2.865234,
  3838. (float)1.887451, (float)2.105469, (float)2.331787, (float)2.587402,
  3839. (float)2.120117, (float)2.443359, (float)2.733887, (float)2.941406,
  3840. (float)1.506348, (float)1.766968, (float)2.400513, (float)2.851807,
  3841. (float)1.664551, (float)1.981079, (float)2.375732, (float)2.774414,
  3842. (float)1.720703, (float)1.978882, (float)2.391479, (float)2.640991,
  3843. (float)1.483398, (float)1.814819, (float)2.434448, (float)2.722290,
  3844. (float)1.769043, (float)2.136597, (float)2.563721, (float)2.774414,
  3845. (float)1.810791, (float)2.049316, (float)2.373901, (float)2.613647,
  3846. (float)1.788330, (float)2.005981, (float)2.359131, (float)2.723145,
  3847. (float)1.785156, (float)1.993164, (float)2.399780, (float)2.832520,
  3848. (float)1.695313, (float)2.022949, (float)2.522583, (float)2.745117,
  3849. (float)1.584106, (float)1.965576, (float)2.299927, (float)2.715576,
  3850. (float)1.894897, (float)2.249878, (float)2.655884, (float)2.897705,
  3851. (float)1.720581, (float)1.995728, (float)2.299438, (float)2.557007,
  3852. (float)1.619385, (float)2.173950, (float)2.574219, (float)2.787964,
  3853. (float)1.883179, (float)2.220459, (float)2.474365, (float)2.825073,
  3854. (float)1.447632, (float)2.045044, (float)2.555542, (float)2.744873,
  3855. (float)1.502686, (float)2.156616, (float)2.653320, (float)2.846558,
  3856. (float)1.711548, (float)1.944092, (float)2.282959, (float)2.685791,
  3857. (float)1.499756, (float)1.867554, (float)2.341064, (float)2.578857,
  3858. (float)1.916870, (float)2.135132, (float)2.568237, (float)2.826050,
  3859. (float)1.498047, (float)1.711182, (float)2.223267, (float)2.755127,
  3860. (float)1.808716, (float)1.997559, (float)2.256470, (float)2.758545,
  3861. (float)2.088501, (float)2.402710, (float)2.667358, (float)2.890259,
  3862. (float)1.545044, (float)1.819214, (float)2.324097, (float)2.692993,
  3863. (float)1.796021, (float)2.012573, (float)2.505737, (float)2.784912,
  3864. (float)1.786499, (float)2.041748, (float)2.290405, (float)2.650757,
  3865. (float)1.938232, (float)2.264404, (float)2.529053, (float)2.796143
  3866. };
  3867. A.9. anaFilter.h
  3868. /******************************************************************
  3869. iLBC Speech Coder ANSI-C Source Code
  3870. anaFilter.h
  3871. Copyright (C) The Internet Society (2004).
  3872. All Rights Reserved.
  3873. ******************************************************************/
  3874. #ifndef __iLBC_ANAFILTER_H
  3875. #define __iLBC_ANAFILTER_H
  3876. void anaFilter(
  3877. Andersen, et al. Experimental [Page 96]
  3878. RFC 3951 Internet Low Bit Rate Codec December 2004
  3879. float *In, /* (i) Signal to be filtered */
  3880. float *a, /* (i) LP parameters */
  3881. int len,/* (i) Length of signal */
  3882. float *Out, /* (o) Filtered signal */
  3883. float *mem /* (i/o) Filter state */
  3884. );
  3885. #endif
  3886. A.10. anaFilter.c
  3887. /******************************************************************
  3888. iLBC Speech Coder ANSI-C Source Code
  3889. anaFilter.c
  3890. Copyright (C) The Internet Society (2004).
  3891. All Rights Reserved.
  3892. ******************************************************************/
  3893. #include <string.h>
  3894. #include "iLBC_define.h"
  3895. /*----------------------------------------------------------------*
  3896. * LP analysis filter.
  3897. *---------------------------------------------------------------*/
  3898. void anaFilter(
  3899. float *In, /* (i) Signal to be filtered */
  3900. float *a, /* (i) LP parameters */
  3901. int len,/* (i) Length of signal */
  3902. float *Out, /* (o) Filtered signal */
  3903. float *mem /* (i/o) Filter state */
  3904. ){
  3905. int i, j;
  3906. float *po, *pi, *pm, *pa;
  3907. po = Out;
  3908. /* Filter first part using memory from past */
  3909. for (i=0; i<LPC_FILTERORDER; i++) {
  3910. pi = &In[i];
  3911. pm = &mem[LPC_FILTERORDER-1];
  3912. pa = a;
  3913. *po=0.0;
  3914. Andersen, et al. Experimental [Page 97]
  3915. RFC 3951 Internet Low Bit Rate Codec December 2004
  3916. for (j=0; j<=i; j++) {
  3917. *po+=(*pa++)*(*pi--);
  3918. }
  3919. for (j=i+1; j<LPC_FILTERORDER+1; j++) {
  3920. *po+=(*pa++)*(*pm--);
  3921. }
  3922. po++;
  3923. }
  3924. /* Filter last part where the state is entirely
  3925. in the input vector */
  3926. for (i=LPC_FILTERORDER; i<len; i++) {
  3927. pi = &In[i];
  3928. pa = a;
  3929. *po=0.0;
  3930. for (j=0; j<LPC_FILTERORDER+1; j++) {
  3931. *po+=(*pa++)*(*pi--);
  3932. }
  3933. po++;
  3934. }
  3935. /* Update state vector */
  3936. memcpy(mem, &In[len-LPC_FILTERORDER],
  3937. LPC_FILTERORDER*sizeof(float));
  3938. }
  3939. A.11. createCB.h
  3940. /******************************************************************
  3941. iLBC Speech Coder ANSI-C Source Code
  3942. createCB.h
  3943. Copyright (C) The Internet Society (2004).
  3944. All Rights Reserved.
  3945. ******************************************************************/
  3946. #ifndef __iLBC_CREATECB_H
  3947. #define __iLBC_CREATECB_H
  3948. void filteredCBvecs(
  3949. float *cbvectors, /* (o) Codebook vector for the
  3950. higher section */
  3951. Andersen, et al. Experimental [Page 98]
  3952. RFC 3951 Internet Low Bit Rate Codec December 2004
  3953. float *mem, /* (i) Buffer to create codebook
  3954. vectors from */
  3955. int lMem /* (i) Length of buffer */
  3956. );
  3957. void searchAugmentedCB(
  3958. int low, /* (i) Start index for the search */
  3959. int high, /* (i) End index for the search */
  3960. int stage, /* (i) Current stage */
  3961. int startIndex, /* (i) CB index for the first
  3962. augmented vector */
  3963. float *target, /* (i) Target vector for encoding */
  3964. float *buffer, /* (i) Pointer to the end of the
  3965. buffer for augmented codebook
  3966. construction */
  3967. float *max_measure, /* (i/o) Currently maximum measure */
  3968. int *best_index,/* (o) Currently the best index */
  3969. float *gain, /* (o) Currently the best gain */
  3970. float *energy, /* (o) Energy of augmented
  3971. codebook vectors */
  3972. float *invenergy/* (o) Inv energy of aug codebook
  3973. vectors */
  3974. );
  3975. void createAugmentedVec(
  3976. int index, /* (i) Index for the aug vector
  3977. to be created */
  3978. float *buffer, /* (i) Pointer to the end of the
  3979. buffer for augmented codebook
  3980. construction */
  3981. float *cbVec /* (o) The construced codebook vector */
  3982. );
  3983. #endif
  3984. A.12. createCB.c
  3985. /******************************************************************
  3986. iLBC Speech Coder ANSI-C Source Code
  3987. createCB.c
  3988. Copyright (C) The Internet Society (2004).
  3989. All Rights Reserved.
  3990. ******************************************************************/
  3991. Andersen, et al. Experimental [Page 99]
  3992. RFC 3951 Internet Low Bit Rate Codec December 2004
  3993. #include "iLBC_define.h"
  3994. #include "constants.h"
  3995. #include <string.h>
  3996. #include <math.h>
  3997. /*----------------------------------------------------------------*
  3998. * Construct an additional codebook vector by filtering the
  3999. * initial codebook buffer. This vector is then used to expand
  4000. * the codebook with an additional section.
  4001. *---------------------------------------------------------------*/
  4002. void filteredCBvecs(
  4003. float *cbvectors, /* (o) Codebook vectors for the
  4004. higher section */
  4005. float *mem, /* (i) Buffer to create codebook
  4006. vector from */
  4007. int lMem /* (i) Length of buffer */
  4008. ){
  4009. int j, k;
  4010. float *pp, *pp1;
  4011. float tempbuff2[CB_MEML+CB_FILTERLEN];
  4012. float *pos;
  4013. memset(tempbuff2, 0, (CB_HALFFILTERLEN-1)*sizeof(float));
  4014. memcpy(&tempbuff2[CB_HALFFILTERLEN-1], mem, lMem*sizeof(float));
  4015. memset(&tempbuff2[lMem+CB_HALFFILTERLEN-1], 0,
  4016. (CB_HALFFILTERLEN+1)*sizeof(float));
  4017. /* Create codebook vector for higher section by filtering */
  4018. /* do filtering */
  4019. pos=cbvectors;
  4020. memset(pos, 0, lMem*sizeof(float));
  4021. for (k=0; k<lMem; k++) {
  4022. pp=&tempbuff2[k];
  4023. pp1=&cbfiltersTbl[CB_FILTERLEN-1];
  4024. for (j=0;j<CB_FILTERLEN;j++) {
  4025. (*pos)+=(*pp++)*(*pp1--);
  4026. }
  4027. pos++;
  4028. }
  4029. }
  4030. /*----------------------------------------------------------------*
  4031. * Search the augmented part of the codebook to find the best
  4032. * measure.
  4033. *----------------------------------------------------------------*/
  4034. Andersen, et al. Experimental [Page 100]
  4035. RFC 3951 Internet Low Bit Rate Codec December 2004
  4036. void searchAugmentedCB(
  4037. int low, /* (i) Start index for the search */
  4038. int high, /* (i) End index for the search */
  4039. int stage, /* (i) Current stage */
  4040. int startIndex, /* (i) Codebook index for the first
  4041. aug vector */
  4042. float *target, /* (i) Target vector for encoding */
  4043. float *buffer, /* (i) Pointer to the end of the buffer for
  4044. augmented codebook construction */
  4045. float *max_measure, /* (i/o) Currently maximum measure */
  4046. int *best_index,/* (o) Currently the best index */
  4047. float *gain, /* (o) Currently the best gain */
  4048. float *energy, /* (o) Energy of augmented codebook
  4049. vectors */
  4050. float *invenergy/* (o) Inv energy of augmented codebook
  4051. vectors */
  4052. ) {
  4053. int icount, ilow, j, tmpIndex;
  4054. float *pp, *ppo, *ppi, *ppe, crossDot, alfa;
  4055. float weighted, measure, nrjRecursive;
  4056. float ftmp;
  4057. /* Compute the energy for the first (low-5)
  4058. noninterpolated samples */
  4059. nrjRecursive = (float) 0.0;
  4060. pp = buffer - low + 1;
  4061. for (j=0; j<(low-5); j++) {
  4062. nrjRecursive += ( (*pp)*(*pp) );
  4063. pp++;
  4064. }
  4065. ppe = buffer - low;
  4066. for (icount=low; icount<=high; icount++) {
  4067. /* Index of the codebook vector used for retrieving
  4068. energy values */
  4069. tmpIndex = startIndex+icount-20;
  4070. ilow = icount-4;
  4071. /* Update the energy recursively to save complexity */
  4072. nrjRecursive = nrjRecursive + (*ppe)*(*ppe);
  4073. ppe--;
  4074. energy[tmpIndex] = nrjRecursive;
  4075. /* Compute cross dot product for the first (low-5)
  4076. samples */
  4077. Andersen, et al. Experimental [Page 101]
  4078. RFC 3951 Internet Low Bit Rate Codec December 2004
  4079. crossDot = (float) 0.0;
  4080. pp = buffer-icount;
  4081. for (j=0; j<ilow; j++) {
  4082. crossDot += target[j]*(*pp++);
  4083. }
  4084. /* interpolation */
  4085. alfa = (float) 0.2;
  4086. ppo = buffer-4;
  4087. ppi = buffer-icount-4;
  4088. for (j=ilow; j<icount; j++) {
  4089. weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
  4090. ppo++;
  4091. ppi++;
  4092. energy[tmpIndex] += weighted*weighted;
  4093. crossDot += target[j]*weighted;
  4094. alfa += (float)0.2;
  4095. }
  4096. /* Compute energy and cross dot product for the
  4097. remaining samples */
  4098. pp = buffer - icount;
  4099. for (j=icount; j<SUBL; j++) {
  4100. energy[tmpIndex] += (*pp)*(*pp);
  4101. crossDot += target[j]*(*pp++);
  4102. }
  4103. if (energy[tmpIndex]>0.0) {
  4104. invenergy[tmpIndex]=(float)1.0/(energy[tmpIndex]+EPS);
  4105. } else {
  4106. invenergy[tmpIndex] = (float) 0.0;
  4107. }
  4108. if (stage==0) {
  4109. measure = (float)-10000000.0;
  4110. if (crossDot > 0.0) {
  4111. measure = crossDot*crossDot*invenergy[tmpIndex];
  4112. }
  4113. }
  4114. else {
  4115. measure = crossDot*crossDot*invenergy[tmpIndex];
  4116. }
  4117. /* check if measure is better */
  4118. ftmp = crossDot*invenergy[tmpIndex];
  4119. if ((measure>*max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
  4120. Andersen, et al. Experimental [Page 102]
  4121. RFC 3951 Internet Low Bit Rate Codec December 2004
  4122. *best_index = tmpIndex;
  4123. *max_measure = measure;
  4124. *gain = ftmp;
  4125. }
  4126. }
  4127. }
  4128. /*----------------------------------------------------------------*
  4129. * Recreate a specific codebook vector from the augmented part.
  4130. *
  4131. *----------------------------------------------------------------*/
  4132. void createAugmentedVec(
  4133. int index, /* (i) Index for the augmented vector
  4134. to be created */
  4135. float *buffer, /* (i) Pointer to the end of the buffer for
  4136. augmented codebook construction */
  4137. float *cbVec/* (o) The construced codebook vector */
  4138. ) {
  4139. int ilow, j;
  4140. float *pp, *ppo, *ppi, alfa, alfa1, weighted;
  4141. ilow = index-5;
  4142. /* copy the first noninterpolated part */
  4143. pp = buffer-index;
  4144. memcpy(cbVec,pp,sizeof(float)*index);
  4145. /* interpolation */
  4146. alfa1 = (float)0.2;
  4147. alfa = 0.0;
  4148. ppo = buffer-5;
  4149. ppi = buffer-index-5;
  4150. for (j=ilow; j<index; j++) {
  4151. weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
  4152. ppo++;
  4153. ppi++;
  4154. cbVec[j] = weighted;
  4155. alfa += alfa1;
  4156. }
  4157. /* copy the second noninterpolated part */
  4158. pp = buffer - index;
  4159. memcpy(cbVec+index,pp,sizeof(float)*(SUBL-index));
  4160. Andersen, et al. Experimental [Page 103]
  4161. RFC 3951 Internet Low Bit Rate Codec December 2004
  4162. }
  4163. A.13. doCPLC.h
  4164. /******************************************************************
  4165. iLBC Speech Coder ANSI-C Source Code
  4166. doCPLC.h
  4167. Copyright (C) The Internet Society (2004).
  4168. All Rights Reserved.
  4169. ******************************************************************/
  4170. #ifndef __iLBC_DOLPC_H
  4171. #define __iLBC_DOLPC_H
  4172. void doThePLC(
  4173. float *PLCresidual, /* (o) concealed residual */
  4174. float *PLClpc, /* (o) concealed LP parameters */
  4175. int PLI, /* (i) packet loss indicator
  4176. 0 - no PL, 1 = PL */
  4177. float *decresidual, /* (i) decoded residual */
  4178. float *lpc, /* (i) decoded LPC (only used for no PL) */
  4179. int inlag, /* (i) pitch lag */
  4180. iLBC_Dec_Inst_t *iLBCdec_inst
  4181. /* (i/o) decoder instance */
  4182. );
  4183. #endif
  4184. A.14. doCPLC.c
  4185. /******************************************************************
  4186. iLBC Speech Coder ANSI-C Source Code
  4187. doCPLC.c
  4188. Copyright (C) The Internet Society (2004).
  4189. All Rights Reserved.
  4190. ******************************************************************/
  4191. #include <math.h>
  4192. #include <string.h>
  4193. #include <stdio.h>
  4194. Andersen, et al. Experimental [Page 104]
  4195. RFC 3951 Internet Low Bit Rate Codec December 2004
  4196. #include "iLBC_define.h"
  4197. /*----------------------------------------------------------------*
  4198. * Compute cross correlation and pitch gain for pitch prediction
  4199. * of last subframe at given lag.
  4200. *---------------------------------------------------------------*/
  4201. void compCorr(
  4202. float *cc, /* (o) cross correlation coefficient */
  4203. float *gc, /* (o) gain */
  4204. float *pm,
  4205. float *buffer, /* (i) signal buffer */
  4206. int lag, /* (i) pitch lag */
  4207. int bLen, /* (i) length of buffer */
  4208. int sRange /* (i) correlation search length */
  4209. ){
  4210. int i;
  4211. float ftmp1, ftmp2, ftmp3;
  4212. /* Guard against getting outside buffer */
  4213. if ((bLen-sRange-lag)<0) {
  4214. sRange=bLen-lag;
  4215. }
  4216. ftmp1 = 0.0;
  4217. ftmp2 = 0.0;
  4218. ftmp3 = 0.0;
  4219. for (i=0; i<sRange; i++) {
  4220. ftmp1 += buffer[bLen-sRange+i] *
  4221. buffer[bLen-sRange+i-lag];
  4222. ftmp2 += buffer[bLen-sRange+i-lag] *
  4223. buffer[bLen-sRange+i-lag];
  4224. ftmp3 += buffer[bLen-sRange+i] *
  4225. buffer[bLen-sRange+i];
  4226. }
  4227. if (ftmp2 > 0.0) {
  4228. *cc = ftmp1*ftmp1/ftmp2;
  4229. *gc = (float)fabs(ftmp1/ftmp2);
  4230. *pm=(float)fabs(ftmp1)/
  4231. ((float)sqrt(ftmp2)*(float)sqrt(ftmp3));
  4232. }
  4233. else {
  4234. *cc = 0.0;
  4235. *gc = 0.0;
  4236. *pm=0.0;
  4237. }
  4238. }
  4239. Andersen, et al. Experimental [Page 105]
  4240. RFC 3951 Internet Low Bit Rate Codec December 2004
  4241. /*----------------------------------------------------------------*
  4242. * Packet loss concealment routine. Conceals a residual signal
  4243. * and LP parameters. If no packet loss, update state.
  4244. *---------------------------------------------------------------*/
  4245. void doThePLC(
  4246. float *PLCresidual, /* (o) concealed residual */
  4247. float *PLClpc, /* (o) concealed LP parameters */
  4248. int PLI, /* (i) packet loss indicator
  4249. 0 - no PL, 1 = PL */
  4250. float *decresidual, /* (i) decoded residual */
  4251. float *lpc, /* (i) decoded LPC (only used for no PL) */
  4252. int inlag, /* (i) pitch lag */
  4253. iLBC_Dec_Inst_t *iLBCdec_inst
  4254. /* (i/o) decoder instance */
  4255. ){
  4256. int lag=20, randlag;
  4257. float gain, maxcc;
  4258. float use_gain;
  4259. float gain_comp, maxcc_comp, per, max_per;
  4260. int i, pick, use_lag;
  4261. float ftmp, randvec[BLOCKL_MAX], pitchfact, energy;
  4262. /* Packet Loss */
  4263. if (PLI == 1) {
  4264. iLBCdec_inst->consPLICount += 1;
  4265. /* if previous frame not lost,
  4266. determine pitch pred. gain */
  4267. if (iLBCdec_inst->prevPLI != 1) {
  4268. /* Search around the previous lag to find the
  4269. best pitch period */
  4270. lag=inlag-3;
  4271. compCorr(&maxcc, &gain, &max_per,
  4272. iLBCdec_inst->prevResidual,
  4273. lag, iLBCdec_inst->blockl, 60);
  4274. for (i=inlag-2;i<=inlag+3;i++) {
  4275. compCorr(&maxcc_comp, &gain_comp, &per,
  4276. iLBCdec_inst->prevResidual,
  4277. i, iLBCdec_inst->blockl, 60);
  4278. if (maxcc_comp>maxcc) {
  4279. maxcc=maxcc_comp;
  4280. Andersen, et al. Experimental [Page 106]
  4281. RFC 3951 Internet Low Bit Rate Codec December 2004
  4282. gain=gain_comp;
  4283. lag=i;
  4284. max_per=per;
  4285. }
  4286. }
  4287. }
  4288. /* previous frame lost, use recorded lag and periodicity */
  4289. else {
  4290. lag=iLBCdec_inst->prevLag;
  4291. max_per=iLBCdec_inst->per;
  4292. }
  4293. /* downscaling */
  4294. use_gain=1.0;
  4295. if (iLBCdec_inst->consPLICount*iLBCdec_inst->blockl>320)
  4296. use_gain=(float)0.9;
  4297. else if (iLBCdec_inst->consPLICount*
  4298. iLBCdec_inst->blockl>2*320)
  4299. use_gain=(float)0.7;
  4300. else if (iLBCdec_inst->consPLICount*
  4301. iLBCdec_inst->blockl>3*320)
  4302. use_gain=(float)0.5;
  4303. else if (iLBCdec_inst->consPLICount*
  4304. iLBCdec_inst->blockl>4*320)
  4305. use_gain=(float)0.0;
  4306. /* mix noise and pitch repeatition */
  4307. ftmp=(float)sqrt(max_per);
  4308. if (ftmp>(float)0.7)
  4309. pitchfact=(float)1.0;
  4310. else if (ftmp>(float)0.4)
  4311. pitchfact=(ftmp-(float)0.4)/((float)0.7-(float)0.4);
  4312. else
  4313. pitchfact=0.0;
  4314. /* avoid repetition of same pitch cycle */
  4315. use_lag=lag;
  4316. if (lag<80) {
  4317. use_lag=2*lag;
  4318. }
  4319. /* compute concealed residual */
  4320. Andersen, et al. Experimental [Page 107]
  4321. RFC 3951 Internet Low Bit Rate Codec December 2004
  4322. energy = 0.0;
  4323. for (i=0; i<iLBCdec_inst->blockl; i++) {
  4324. /* noise component */
  4325. iLBCdec_inst->seed=(iLBCdec_inst->seed*69069L+1) &
  4326. (0x80000000L-1);
  4327. randlag = 50 + ((signed long) iLBCdec_inst->seed)%70;
  4328. pick = i - randlag;
  4329. if (pick < 0) {
  4330. randvec[i] =
  4331. iLBCdec_inst->prevResidual[
  4332. iLBCdec_inst->blockl+pick];
  4333. } else {
  4334. randvec[i] = randvec[pick];
  4335. }
  4336. /* pitch repeatition component */
  4337. pick = i - use_lag;
  4338. if (pick < 0) {
  4339. PLCresidual[i] =
  4340. iLBCdec_inst->prevResidual[
  4341. iLBCdec_inst->blockl+pick];
  4342. } else {
  4343. PLCresidual[i] = PLCresidual[pick];
  4344. }
  4345. /* mix random and periodicity component */
  4346. if (i<80)
  4347. PLCresidual[i] = use_gain*(pitchfact *
  4348. PLCresidual[i] +
  4349. ((float)1.0 - pitchfact) * randvec[i]);
  4350. else if (i<160)
  4351. PLCresidual[i] = (float)0.95*use_gain*(pitchfact *
  4352. PLCresidual[i] +
  4353. ((float)1.0 - pitchfact) * randvec[i]);
  4354. else
  4355. PLCresidual[i] = (float)0.9*use_gain*(pitchfact *
  4356. PLCresidual[i] +
  4357. ((float)1.0 - pitchfact) * randvec[i]);
  4358. energy += PLCresidual[i] * PLCresidual[i];
  4359. }
  4360. /* less than 30 dB, use only noise */
  4361. Andersen, et al. Experimental [Page 108]
  4362. RFC 3951 Internet Low Bit Rate Codec December 2004
  4363. if (sqrt(energy/(float)iLBCdec_inst->blockl) < 30.0) {
  4364. gain=0.0;
  4365. for (i=0; i<iLBCdec_inst->blockl; i++) {
  4366. PLCresidual[i] = randvec[i];
  4367. }
  4368. }
  4369. /* use old LPC */
  4370. memcpy(PLClpc,iLBCdec_inst->prevLpc,
  4371. (LPC_FILTERORDER+1)*sizeof(float));
  4372. }
  4373. /* no packet loss, copy input */
  4374. else {
  4375. memcpy(PLCresidual, decresidual,
  4376. iLBCdec_inst->blockl*sizeof(float));
  4377. memcpy(PLClpc, lpc, (LPC_FILTERORDER+1)*sizeof(float));
  4378. iLBCdec_inst->consPLICount = 0;
  4379. }
  4380. /* update state */
  4381. if (PLI) {
  4382. iLBCdec_inst->prevLag = lag;
  4383. iLBCdec_inst->per=max_per;
  4384. }
  4385. iLBCdec_inst->prevPLI = PLI;
  4386. memcpy(iLBCdec_inst->prevLpc, PLClpc,
  4387. (LPC_FILTERORDER+1)*sizeof(float));
  4388. memcpy(iLBCdec_inst->prevResidual, PLCresidual,
  4389. iLBCdec_inst->blockl*sizeof(float));
  4390. }
  4391. A.15. enhancer.h
  4392. /******************************************************************
  4393. iLBC Speech Coder ANSI-C Source Code
  4394. enhancer.h
  4395. Copyright (C) The Internet Society (2004).
  4396. All Rights Reserved.
  4397. Andersen, et al. Experimental [Page 109]
  4398. RFC 3951 Internet Low Bit Rate Codec December 2004
  4399. ******************************************************************/
  4400. #ifndef __ENHANCER_H
  4401. #define __ENHANCER_H
  4402. #include "iLBC_define.h"
  4403. float xCorrCoef(
  4404. float *target, /* (i) first array */
  4405. float *regressor, /* (i) second array */
  4406. int subl /* (i) dimension arrays */
  4407. );
  4408. int enhancerInterface(
  4409. float *out, /* (o) the enhanced recidual signal */
  4410. float *in, /* (i) the recidual signal to enhance */
  4411. iLBC_Dec_Inst_t *iLBCdec_inst
  4412. /* (i/o) the decoder state structure */
  4413. );
  4414. #endif
  4415. A.16. enhancer.c
  4416. /******************************************************************
  4417. iLBC Speech Coder ANSI-C Source Code
  4418. enhancer.c
  4419. Copyright (C) The Internet Society (2004).
  4420. All Rights Reserved.
  4421. ******************************************************************/
  4422. #include <math.h>
  4423. #include <string.h>
  4424. #include "iLBC_define.h"
  4425. #include "constants.h"
  4426. #include "filter.h"
  4427. /*----------------------------------------------------------------*
  4428. * Find index in array such that the array element with said
  4429. * index is the element of said array closest to "value"
  4430. * according to the squared-error criterion
  4431. *---------------------------------------------------------------*/
  4432. void NearestNeighbor(
  4433. Andersen, et al. Experimental [Page 110]
  4434. RFC 3951 Internet Low Bit Rate Codec December 2004
  4435. int *index, /* (o) index of array element closest
  4436. to value */
  4437. float *array, /* (i) data array */
  4438. float value,/* (i) value */
  4439. int arlength/* (i) dimension of data array */
  4440. ){
  4441. int i;
  4442. float bestcrit,crit;
  4443. crit=array[0]-value;
  4444. bestcrit=crit*crit;
  4445. *index=0;
  4446. for (i=1; i<arlength; i++) {
  4447. crit=array[i]-value;
  4448. crit=crit*crit;
  4449. if (crit<bestcrit) {
  4450. bestcrit=crit;
  4451. *index=i;
  4452. }
  4453. }
  4454. }
  4455. /*----------------------------------------------------------------*
  4456. * compute cross correlation between sequences
  4457. *---------------------------------------------------------------*/
  4458. void mycorr1(
  4459. float* corr, /* (o) correlation of seq1 and seq2 */
  4460. float* seq1, /* (i) first sequence */
  4461. int dim1, /* (i) dimension first seq1 */
  4462. const float *seq2, /* (i) second sequence */
  4463. int dim2 /* (i) dimension seq2 */
  4464. ){
  4465. int i,j;
  4466. for (i=0; i<=dim1-dim2; i++) {
  4467. corr[i]=0.0;
  4468. for (j=0; j<dim2; j++) {
  4469. corr[i] += seq1[i+j] * seq2[j];
  4470. }
  4471. }
  4472. }
  4473. /*----------------------------------------------------------------*
  4474. * upsample finite array assuming zeros outside bounds
  4475. *---------------------------------------------------------------*/
  4476. Andersen, et al. Experimental [Page 111]
  4477. RFC 3951 Internet Low Bit Rate Codec December 2004
  4478. void enh_upsample(
  4479. float* useq1, /* (o) upsampled output sequence */
  4480. float* seq1,/* (i) unupsampled sequence */
  4481. int dim1, /* (i) dimension seq1 */
  4482. int hfl /* (i) polyphase filter length=2*hfl+1 */
  4483. ){
  4484. float *pu,*ps;
  4485. int i,j,k,q,filterlength,hfl2;
  4486. const float *polyp[ENH_UPS0]; /* pointers to
  4487. polyphase columns */
  4488. const float *pp;
  4489. /* define pointers for filter */
  4490. filterlength=2*hfl+1;
  4491. if ( filterlength > dim1 ) {
  4492. hfl2=(int) (dim1/2);
  4493. for (j=0; j<ENH_UPS0; j++) {
  4494. polyp[j]=polyphaserTbl+j*filterlength+hfl-hfl2;
  4495. }
  4496. hfl=hfl2;
  4497. filterlength=2*hfl+1;
  4498. }
  4499. else {
  4500. for (j=0; j<ENH_UPS0; j++) {
  4501. polyp[j]=polyphaserTbl+j*filterlength;
  4502. }
  4503. }
  4504. /* filtering: filter overhangs left side of sequence */
  4505. pu=useq1;
  4506. for (i=hfl; i<filterlength; i++) {
  4507. for (j=0; j<ENH_UPS0; j++) {
  4508. *pu=0.0;
  4509. pp = polyp[j];
  4510. ps = seq1+i;
  4511. for (k=0; k<=i; k++) {
  4512. *pu += *ps-- * *pp++;
  4513. }
  4514. pu++;
  4515. }
  4516. }
  4517. /* filtering: simple convolution=inner products */
  4518. for (i=filterlength; i<dim1; i++) {
  4519. Andersen, et al. Experimental [Page 112]
  4520. RFC 3951 Internet Low Bit Rate Codec December 2004
  4521. for (j=0;j<ENH_UPS0; j++){
  4522. *pu=0.0;
  4523. pp = polyp[j];
  4524. ps = seq1+i;
  4525. for (k=0; k<filterlength; k++) {
  4526. *pu += *ps-- * *pp++;
  4527. }
  4528. pu++;
  4529. }
  4530. }
  4531. /* filtering: filter overhangs right side of sequence */
  4532. for (q=1; q<=hfl; q++) {
  4533. for (j=0; j<ENH_UPS0; j++) {
  4534. *pu=0.0;
  4535. pp = polyp[j]+q;
  4536. ps = seq1+dim1-1;
  4537. for (k=0; k<filterlength-q; k++) {
  4538. *pu += *ps-- * *pp++;
  4539. }
  4540. pu++;
  4541. }
  4542. }
  4543. }
  4544. /*----------------------------------------------------------------*
  4545. * find segment starting near idata+estSegPos that has highest
  4546. * correlation with idata+centerStartPos through
  4547. * idata+centerStartPos+ENH_BLOCKL-1 segment is found at a
  4548. * resolution of ENH_UPSO times the original of the original
  4549. * sampling rate
  4550. *---------------------------------------------------------------*/
  4551. void refiner(
  4552. float *seg, /* (o) segment array */
  4553. float *updStartPos, /* (o) updated start point */
  4554. float* idata, /* (i) original data buffer */
  4555. int idatal, /* (i) dimension of idata */
  4556. int centerStartPos, /* (i) beginning center segment */
  4557. float estSegPos,/* (i) estimated beginning other segment */
  4558. float period /* (i) estimated pitch period */
  4559. ){
  4560. int estSegPosRounded,searchSegStartPos,searchSegEndPos,corrdim;
  4561. int tloc,tloc2,i,st,en,fraction;
  4562. float vect[ENH_VECTL],corrVec[ENH_CORRDIM],maxv;
  4563. float corrVecUps[ENH_CORRDIM*ENH_UPS0];
  4564. Andersen, et al. Experimental [Page 113]
  4565. RFC 3951 Internet Low Bit Rate Codec December 2004
  4566. /* defining array bounds */
  4567. estSegPosRounded=(int)(estSegPos - 0.5);
  4568. searchSegStartPos=estSegPosRounded-ENH_SLOP;
  4569. if (searchSegStartPos<0) {
  4570. searchSegStartPos=0;
  4571. }
  4572. searchSegEndPos=estSegPosRounded+ENH_SLOP;
  4573. if (searchSegEndPos+ENH_BLOCKL >= idatal) {
  4574. searchSegEndPos=idatal-ENH_BLOCKL-1;
  4575. }
  4576. corrdim=searchSegEndPos-searchSegStartPos+1;
  4577. /* compute upsampled correlation (corr33) and find
  4578. location of max */
  4579. mycorr1(corrVec,idata+searchSegStartPos,
  4580. corrdim+ENH_BLOCKL-1,idata+centerStartPos,ENH_BLOCKL);
  4581. enh_upsample(corrVecUps,corrVec,corrdim,ENH_FL0);
  4582. tloc=0; maxv=corrVecUps[0];
  4583. for (i=1; i<ENH_UPS0*corrdim; i++) {
  4584. if (corrVecUps[i]>maxv) {
  4585. tloc=i;
  4586. maxv=corrVecUps[i];
  4587. }
  4588. }
  4589. /* make vector can be upsampled without ever running outside
  4590. bounds */
  4591. *updStartPos= (float)searchSegStartPos +
  4592. (float)tloc/(float)ENH_UPS0+(float)1.0;
  4593. tloc2=(int)(tloc/ENH_UPS0);
  4594. if (tloc>tloc2*ENH_UPS0) {
  4595. tloc2++;
  4596. }
  4597. st=searchSegStartPos+tloc2-ENH_FL0;
  4598. if (st<0) {
  4599. memset(vect,0,-st*sizeof(float));
  4600. memcpy(&vect[-st],idata, (ENH_VECTL+st)*sizeof(float));
  4601. }
  4602. else {
  4603. Andersen, et al. Experimental [Page 114]
  4604. RFC 3951 Internet Low Bit Rate Codec December 2004
  4605. en=st+ENH_VECTL;
  4606. if (en>idatal) {
  4607. memcpy(vect, &idata[st],
  4608. (ENH_VECTL-(en-idatal))*sizeof(float));
  4609. memset(&vect[ENH_VECTL-(en-idatal)], 0,
  4610. (en-idatal)*sizeof(float));
  4611. }
  4612. else {
  4613. memcpy(vect, &idata[st], ENH_VECTL*sizeof(float));
  4614. }
  4615. }
  4616. fraction=tloc2*ENH_UPS0-tloc;
  4617. /* compute the segment (this is actually a convolution) */
  4618. mycorr1(seg,vect,ENH_VECTL,polyphaserTbl+(2*ENH_FL0+1)*fraction,
  4619. 2*ENH_FL0+1);
  4620. }
  4621. /*----------------------------------------------------------------*
  4622. * find the smoothed output data
  4623. *---------------------------------------------------------------*/
  4624. void smath(
  4625. float *odata, /* (o) smoothed output */
  4626. float *sseq,/* (i) said second sequence of waveforms */
  4627. int hl, /* (i) 2*hl+1 is sseq dimension */
  4628. float alpha0/* (i) max smoothing energy fraction */
  4629. ){
  4630. int i,k;
  4631. float w00,w10,w11,A,B,C,*psseq,err,errs;
  4632. float surround[BLOCKL_MAX]; /* shape contributed by other than
  4633. current */
  4634. float wt[2*ENH_HL+1]; /* waveform weighting to get
  4635. surround shape */
  4636. float denom;
  4637. /* create shape of contribution from all waveforms except the
  4638. current one */
  4639. for (i=1; i<=2*hl+1; i++) {
  4640. wt[i-1] = (float)0.5*(1 - (float)cos(2*PI*i/(2*hl+2)));
  4641. }
  4642. wt[hl]=0.0; /* for clarity, not used */
  4643. for (i=0; i<ENH_BLOCKL; i++) {
  4644. surround[i]=sseq[i]*wt[0];
  4645. }
  4646. Andersen, et al. Experimental [Page 115]
  4647. RFC 3951 Internet Low Bit Rate Codec December 2004
  4648. for (k=1; k<hl; k++) {
  4649. psseq=sseq+k*ENH_BLOCKL;
  4650. for(i=0;i<ENH_BLOCKL; i++) {
  4651. surround[i]+=psseq[i]*wt[k];
  4652. }
  4653. }
  4654. for (k=hl+1; k<=2*hl; k++) {
  4655. psseq=sseq+k*ENH_BLOCKL;
  4656. for(i=0;i<ENH_BLOCKL; i++) {
  4657. surround[i]+=psseq[i]*wt[k];
  4658. }
  4659. }
  4660. /* compute some inner products */
  4661. w00 = w10 = w11 = 0.0;
  4662. psseq=sseq+hl*ENH_BLOCKL; /* current block */
  4663. for (i=0; i<ENH_BLOCKL;i++) {
  4664. w00+=psseq[i]*psseq[i];
  4665. w11+=surround[i]*surround[i];
  4666. w10+=surround[i]*psseq[i];
  4667. }
  4668. if (fabs(w11) < 1.0) {
  4669. w11=1.0;
  4670. }
  4671. C = (float)sqrt( w00/w11);
  4672. /* first try enhancement without power-constraint */
  4673. errs=0.0;
  4674. psseq=sseq+hl*ENH_BLOCKL;
  4675. for (i=0; i<ENH_BLOCKL; i++) {
  4676. odata[i]=C*surround[i];
  4677. err=psseq[i]-odata[i];
  4678. errs+=err*err;
  4679. }
  4680. /* if constraint violated by first try, add constraint */
  4681. if (errs > alpha0 * w00) {
  4682. if ( w00 < 1) {
  4683. w00=1;
  4684. }
  4685. denom = (w11*w00-w10*w10)/(w00*w00);
  4686. if (denom > 0.0001) { /* eliminates numerical problems
  4687. for if smooth */
  4688. Andersen, et al. Experimental [Page 116]
  4689. RFC 3951 Internet Low Bit Rate Codec December 2004
  4690. A = (float)sqrt( (alpha0- alpha0*alpha0/4)/denom);
  4691. B = -alpha0/2 - A * w10/w00;
  4692. B = B+1;
  4693. }
  4694. else { /* essentially no difference between cycles;
  4695. smoothing not needed */
  4696. A= 0.0;
  4697. B= 1.0;
  4698. }
  4699. /* create smoothed sequence */
  4700. psseq=sseq+hl*ENH_BLOCKL;
  4701. for (i=0; i<ENH_BLOCKL; i++) {
  4702. odata[i]=A*surround[i]+B*psseq[i];
  4703. }
  4704. }
  4705. }
  4706. /*----------------------------------------------------------------*
  4707. * get the pitch-synchronous sample sequence
  4708. *---------------------------------------------------------------*/
  4709. void getsseq(
  4710. float *sseq, /* (o) the pitch-synchronous sequence */
  4711. float *idata, /* (i) original data */
  4712. int idatal, /* (i) dimension of data */
  4713. int centerStartPos, /* (i) where current block starts */
  4714. float *period, /* (i) rough-pitch-period array */
  4715. float *plocs, /* (i) where periods of period array
  4716. are taken */
  4717. int periodl, /* (i) dimension period array */
  4718. int hl /* (i) 2*hl+1 is the number of sequences */
  4719. ){
  4720. int i,centerEndPos,q;
  4721. float blockStartPos[2*ENH_HL+1];
  4722. int lagBlock[2*ENH_HL+1];
  4723. float plocs2[ENH_PLOCSL];
  4724. float *psseq;
  4725. centerEndPos=centerStartPos+ENH_BLOCKL-1;
  4726. /* present */
  4727. NearestNeighbor(lagBlock+hl,plocs,
  4728. (float)0.5*(centerStartPos+centerEndPos),periodl);
  4729. blockStartPos[hl]=(float)centerStartPos;
  4730. Andersen, et al. Experimental [Page 117]
  4731. RFC 3951 Internet Low Bit Rate Codec December 2004
  4732. psseq=sseq+ENH_BLOCKL*hl;
  4733. memcpy(psseq, idata+centerStartPos, ENH_BLOCKL*sizeof(float));
  4734. /* past */
  4735. for (q=hl-1; q>=0; q--) {
  4736. blockStartPos[q]=blockStartPos[q+1]-period[lagBlock[q+1]];
  4737. NearestNeighbor(lagBlock+q,plocs,
  4738. blockStartPos[q]+
  4739. ENH_BLOCKL_HALF-period[lagBlock[q+1]], periodl);
  4740. if (blockStartPos[q]-ENH_OVERHANG>=0) {
  4741. refiner(sseq+q*ENH_BLOCKL, blockStartPos+q, idata,
  4742. idatal, centerStartPos, blockStartPos[q],
  4743. period[lagBlock[q+1]]);
  4744. } else {
  4745. psseq=sseq+q*ENH_BLOCKL;
  4746. memset(psseq, 0, ENH_BLOCKL*sizeof(float));
  4747. }
  4748. }
  4749. /* future */
  4750. for (i=0; i<periodl; i++) {
  4751. plocs2[i]=plocs[i]-period[i];
  4752. }
  4753. for (q=hl+1; q<=2*hl; q++) {
  4754. NearestNeighbor(lagBlock+q,plocs2,
  4755. blockStartPos[q-1]+ENH_BLOCKL_HALF,periodl);
  4756. blockStartPos[q]=blockStartPos[q-1]+period[lagBlock[q]];
  4757. if (blockStartPos[q]+ENH_BLOCKL+ENH_OVERHANG<idatal) {
  4758. refiner(sseq+ENH_BLOCKL*q, blockStartPos+q, idata,
  4759. idatal, centerStartPos, blockStartPos[q],
  4760. period[lagBlock[q]]);
  4761. }
  4762. else {
  4763. psseq=sseq+q*ENH_BLOCKL;
  4764. memset(psseq, 0, ENH_BLOCKL*sizeof(float));
  4765. }
  4766. }
  4767. }
  4768. /*----------------------------------------------------------------*
  4769. * perform enhancement on idata+centerStartPos through
  4770. * idata+centerStartPos+ENH_BLOCKL-1
  4771. *---------------------------------------------------------------*/
  4772. Andersen, et al. Experimental [Page 118]
  4773. RFC 3951 Internet Low Bit Rate Codec December 2004
  4774. void enhancer(
  4775. float *odata, /* (o) smoothed block, dimension blockl */
  4776. float *idata, /* (i) data buffer used for enhancing */
  4777. int idatal, /* (i) dimension idata */
  4778. int centerStartPos, /* (i) first sample current block
  4779. within idata */
  4780. float alpha0, /* (i) max correction-energy-fraction
  4781. (in [0,1]) */
  4782. float *period, /* (i) pitch period array */
  4783. float *plocs, /* (i) locations where period array
  4784. values valid */
  4785. int periodl /* (i) dimension of period and plocs */
  4786. ){
  4787. float sseq[(2*ENH_HL+1)*ENH_BLOCKL];
  4788. /* get said second sequence of segments */
  4789. getsseq(sseq,idata,idatal,centerStartPos,period,
  4790. plocs,periodl,ENH_HL);
  4791. /* compute the smoothed output from said second sequence */
  4792. smath(odata,sseq,ENH_HL,alpha0);
  4793. }
  4794. /*----------------------------------------------------------------*
  4795. * cross correlation
  4796. *---------------------------------------------------------------*/
  4797. float xCorrCoef(
  4798. float *target, /* (i) first array */
  4799. float *regressor, /* (i) second array */
  4800. int subl /* (i) dimension arrays */
  4801. ){
  4802. int i;
  4803. float ftmp1, ftmp2;
  4804. ftmp1 = 0.0;
  4805. ftmp2 = 0.0;
  4806. for (i=0; i<subl; i++) {
  4807. ftmp1 += target[i]*regressor[i];
  4808. ftmp2 += regressor[i]*regressor[i];
  4809. }
  4810. if (ftmp1 > 0.0) {
  4811. return (float)(ftmp1*ftmp1/ftmp2);
  4812. }
  4813. Andersen, et al. Experimental [Page 119]
  4814. RFC 3951 Internet Low Bit Rate Codec December 2004
  4815. else {
  4816. return (float)0.0;
  4817. }
  4818. }
  4819. /*----------------------------------------------------------------*
  4820. * interface for enhancer
  4821. *---------------------------------------------------------------*/
  4822. int enhancerInterface(
  4823. float *out, /* (o) enhanced signal */
  4824. float *in, /* (i) unenhanced signal */
  4825. iLBC_Dec_Inst_t *iLBCdec_inst /* (i) buffers etc */
  4826. ){
  4827. float *enh_buf, *enh_period;
  4828. int iblock, isample;
  4829. int lag=0, ilag, i, ioffset;
  4830. float cc, maxcc;
  4831. float ftmp1, ftmp2;
  4832. float *inPtr, *enh_bufPtr1, *enh_bufPtr2;
  4833. float plc_pred[ENH_BLOCKL];
  4834. float lpState[6], downsampled[(ENH_NBLOCKS*ENH_BLOCKL+120)/2];
  4835. int inLen=ENH_NBLOCKS*ENH_BLOCKL+120;
  4836. int start, plc_blockl, inlag;
  4837. enh_buf=iLBCdec_inst->enh_buf;
  4838. enh_period=iLBCdec_inst->enh_period;
  4839. memmove(enh_buf, &enh_buf[iLBCdec_inst->blockl],
  4840. (ENH_BUFL-iLBCdec_inst->blockl)*sizeof(float));
  4841. memcpy(&enh_buf[ENH_BUFL-iLBCdec_inst->blockl], in,
  4842. iLBCdec_inst->blockl*sizeof(float));
  4843. if (iLBCdec_inst->mode==30)
  4844. plc_blockl=ENH_BLOCKL;
  4845. else
  4846. plc_blockl=40;
  4847. /* when 20 ms frame, move processing one block */
  4848. ioffset=0;
  4849. if (iLBCdec_inst->mode==20) ioffset=1;
  4850. i=3-ioffset;
  4851. memmove(enh_period, &enh_period[i],
  4852. (ENH_NBLOCKS_TOT-i)*sizeof(float));
  4853. Andersen, et al. Experimental [Page 120]
  4854. RFC 3951 Internet Low Bit Rate Codec December 2004
  4855. /* Set state information to the 6 samples right before
  4856. the samples to be downsampled. */
  4857. memcpy(lpState,
  4858. enh_buf+(ENH_NBLOCKS_EXTRA+ioffset)*ENH_BLOCKL-126,
  4859. 6*sizeof(float));
  4860. /* Down sample a factor 2 to save computations */
  4861. DownSample(enh_buf+(ENH_NBLOCKS_EXTRA+ioffset)*ENH_BLOCKL-120,
  4862. lpFilt_coefsTbl, inLen-ioffset*ENH_BLOCKL,
  4863. lpState, downsampled);
  4864. /* Estimate the pitch in the down sampled domain. */
  4865. for (iblock = 0; iblock<ENH_NBLOCKS-ioffset; iblock++) {
  4866. lag = 10;
  4867. maxcc = xCorrCoef(downsampled+60+iblock*
  4868. ENH_BLOCKL_HALF, downsampled+60+iblock*
  4869. ENH_BLOCKL_HALF-lag, ENH_BLOCKL_HALF);
  4870. for (ilag=11; ilag<60; ilag++) {
  4871. cc = xCorrCoef(downsampled+60+iblock*
  4872. ENH_BLOCKL_HALF, downsampled+60+iblock*
  4873. ENH_BLOCKL_HALF-ilag, ENH_BLOCKL_HALF);
  4874. if (cc > maxcc) {
  4875. maxcc = cc;
  4876. lag = ilag;
  4877. }
  4878. }
  4879. /* Store the estimated lag in the non-downsampled domain */
  4880. enh_period[iblock+ENH_NBLOCKS_EXTRA+ioffset] = (float)lag*2;
  4881. }
  4882. /* PLC was performed on the previous packet */
  4883. if (iLBCdec_inst->prev_enh_pl==1) {
  4884. inlag=(int)enh_period[ENH_NBLOCKS_EXTRA+ioffset];
  4885. lag = inlag-1;
  4886. maxcc = xCorrCoef(in, in+lag, plc_blockl);
  4887. for (ilag=inlag; ilag<=inlag+1; ilag++) {
  4888. cc = xCorrCoef(in, in+ilag, plc_blockl);
  4889. Andersen, et al. Experimental [Page 121]
  4890. RFC 3951 Internet Low Bit Rate Codec December 2004
  4891. if (cc > maxcc) {
  4892. maxcc = cc;
  4893. lag = ilag;
  4894. }
  4895. }
  4896. enh_period[ENH_NBLOCKS_EXTRA+ioffset-1]=(float)lag;
  4897. /* compute new concealed residual for the old lookahead,
  4898. mix the forward PLC with a backward PLC from
  4899. the new frame */
  4900. inPtr=&in[lag-1];
  4901. enh_bufPtr1=&plc_pred[plc_blockl-1];
  4902. if (lag>plc_blockl) {
  4903. start=plc_blockl;
  4904. } else {
  4905. start=lag;
  4906. }
  4907. for (isample = start; isample>0; isample--) {
  4908. *enh_bufPtr1-- = *inPtr--;
  4909. }
  4910. enh_bufPtr2=&enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl];
  4911. for (isample = (plc_blockl-1-lag); isample>=0; isample--) {
  4912. *enh_bufPtr1-- = *enh_bufPtr2--;
  4913. }
  4914. /* limit energy change */
  4915. ftmp2=0.0;
  4916. ftmp1=0.0;
  4917. for (i=0;i<plc_blockl;i++) {
  4918. ftmp2+=enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl-i]*
  4919. enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl-i];
  4920. ftmp1+=plc_pred[i]*plc_pred[i];
  4921. }
  4922. ftmp1=(float)sqrt(ftmp1/(float)plc_blockl);
  4923. ftmp2=(float)sqrt(ftmp2/(float)plc_blockl);
  4924. if (ftmp1>(float)2.0*ftmp2 && ftmp1>0.0) {
  4925. for (i=0;i<plc_blockl-10;i++) {
  4926. plc_pred[i]*=(float)2.0*ftmp2/ftmp1;
  4927. }
  4928. for (i=plc_blockl-10;i<plc_blockl;i++) {
  4929. plc_pred[i]*=(float)(i-plc_blockl+10)*
  4930. ((float)1.0-(float)2.0*ftmp2/ftmp1)/(float)(10)+
  4931. Andersen, et al. Experimental [Page 122]
  4932. RFC 3951 Internet Low Bit Rate Codec December 2004
  4933. (float)2.0*ftmp2/ftmp1;
  4934. }
  4935. }
  4936. enh_bufPtr1=&enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl];
  4937. for (i=0; i<plc_blockl; i++) {
  4938. ftmp1 = (float) (i+1) / (float) (plc_blockl+1);
  4939. *enh_bufPtr1 *= ftmp1;
  4940. *enh_bufPtr1 += ((float)1.0-ftmp1)*
  4941. plc_pred[plc_blockl-1-i];
  4942. enh_bufPtr1--;
  4943. }
  4944. }
  4945. if (iLBCdec_inst->mode==20) {
  4946. /* Enhancer with 40 samples delay */
  4947. for (iblock = 0; iblock<2; iblock++) {
  4948. enhancer(out+iblock*ENH_BLOCKL, enh_buf,
  4949. ENH_BUFL, (5+iblock)*ENH_BLOCKL+40,
  4950. ENH_ALPHA0, enh_period, enh_plocsTbl,
  4951. ENH_NBLOCKS_TOT);
  4952. }
  4953. } else if (iLBCdec_inst->mode==30) {
  4954. /* Enhancer with 80 samples delay */
  4955. for (iblock = 0; iblock<3; iblock++) {
  4956. enhancer(out+iblock*ENH_BLOCKL, enh_buf,
  4957. ENH_BUFL, (4+iblock)*ENH_BLOCKL,
  4958. ENH_ALPHA0, enh_period, enh_plocsTbl,
  4959. ENH_NBLOCKS_TOT);
  4960. }
  4961. }
  4962. return (lag*2);
  4963. }
  4964. A.17. filter.h
  4965. /******************************************************************
  4966. iLBC Speech Coder ANSI-C Source Code
  4967. filter.h
  4968. Copyright (C) The Internet Society (2004).
  4969. All Rights Reserved.
  4970. ******************************************************************/
  4971. Andersen, et al. Experimental [Page 123]
  4972. RFC 3951 Internet Low Bit Rate Codec December 2004
  4973. #ifndef __iLBC_FILTER_H
  4974. #define __iLBC_FILTER_H
  4975. void AllPoleFilter(
  4976. float *InOut, /* (i/o) on entrance InOut[-orderCoef] to
  4977. InOut[-1] contain the state of the
  4978. filter (delayed samples). InOut[0] to
  4979. InOut[lengthInOut-1] contain the filter
  4980. input, on en exit InOut[-orderCoef] to
  4981. InOut[-1] is unchanged and InOut[0] to
  4982. InOut[lengthInOut-1] contain filtered
  4983. samples */
  4984. float *Coef,/* (i) filter coefficients, Coef[0] is assumed
  4985. to be 1.0 */
  4986. int lengthInOut,/* (i) number of input/output samples */
  4987. int orderCoef /* (i) number of filter coefficients */
  4988. );
  4989. void AllZeroFilter(
  4990. float *In, /* (i) In[0] to In[lengthInOut-1] contain
  4991. filter input samples */
  4992. float *Coef,/* (i) filter coefficients (Coef[0] is assumed
  4993. to be 1.0) */
  4994. int lengthInOut,/* (i) number of input/output samples */
  4995. int orderCoef, /* (i) number of filter coefficients */
  4996. float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
  4997. contain the filter state, on exit Out[0]
  4998. to Out[lengthInOut-1] contain filtered
  4999. samples */
  5000. );
  5001. void ZeroPoleFilter(
  5002. float *In, /* (i) In[0] to In[lengthInOut-1] contain filter
  5003. input samples In[-orderCoef] to In[-1]
  5004. contain state of all-zero section */
  5005. float *ZeroCoef,/* (i) filter coefficients for all-zero
  5006. section (ZeroCoef[0] is assumed to
  5007. be 1.0) */
  5008. float *PoleCoef,/* (i) filter coefficients for all-pole section
  5009. (ZeroCoef[0] is assumed to be 1.0) */
  5010. int lengthInOut,/* (i) number of input/output samples */
  5011. int orderCoef, /* (i) number of filter coefficients */
  5012. float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
  5013. contain state of all-pole section. On
  5014. exit Out[0] to Out[lengthInOut-1]
  5015. contain filtered samples */
  5016. );
  5017. Andersen, et al. Experimental [Page 124]
  5018. RFC 3951 Internet Low Bit Rate Codec December 2004
  5019. void DownSample (
  5020. float *In, /* (i) input samples */
  5021. float *Coef, /* (i) filter coefficients */
  5022. int lengthIn, /* (i) number of input samples */
  5023. float *state, /* (i) filter state */
  5024. float *Out /* (o) downsampled output */
  5025. );
  5026. #endif
  5027. A.18. filter.c
  5028. /******************************************************************
  5029. iLBC Speech Coder ANSI-C Source Code
  5030. filter.c
  5031. Copyright (C) The Internet Society (2004).
  5032. All Rights Reserved.
  5033. ******************************************************************/
  5034. #include "iLBC_define.h"
  5035. /*----------------------------------------------------------------*
  5036. * all-pole filter
  5037. *---------------------------------------------------------------*/
  5038. void AllPoleFilter(
  5039. float *InOut, /* (i/o) on entrance InOut[-orderCoef] to
  5040. InOut[-1] contain the state of the
  5041. filter (delayed samples). InOut[0] to
  5042. InOut[lengthInOut-1] contain the filter
  5043. input, on en exit InOut[-orderCoef] to
  5044. InOut[-1] is unchanged and InOut[0] to
  5045. InOut[lengthInOut-1] contain filtered
  5046. samples */
  5047. float *Coef,/* (i) filter coefficients, Coef[0] is assumed
  5048. to be 1.0 */
  5049. int lengthInOut,/* (i) number of input/output samples */
  5050. int orderCoef /* (i) number of filter coefficients */
  5051. ){
  5052. int n,k;
  5053. for(n=0;n<lengthInOut;n++){
  5054. for(k=1;k<=orderCoef;k++){
  5055. *InOut -= Coef[k]*InOut[-k];
  5056. Andersen, et al. Experimental [Page 125]
  5057. RFC 3951 Internet Low Bit Rate Codec December 2004
  5058. }
  5059. InOut++;
  5060. }
  5061. }
  5062. /*----------------------------------------------------------------*
  5063. * all-zero filter
  5064. *---------------------------------------------------------------*/
  5065. void AllZeroFilter(
  5066. float *In, /* (i) In[0] to In[lengthInOut-1] contain
  5067. filter input samples */
  5068. float *Coef,/* (i) filter coefficients (Coef[0] is assumed
  5069. to be 1.0) */
  5070. int lengthInOut,/* (i) number of input/output samples */
  5071. int orderCoef, /* (i) number of filter coefficients */
  5072. float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
  5073. contain the filter state, on exit Out[0]
  5074. to Out[lengthInOut-1] contain filtered
  5075. samples */
  5076. ){
  5077. int n,k;
  5078. for(n=0;n<lengthInOut;n++){
  5079. *Out = Coef[0]*In[0];
  5080. for(k=1;k<=orderCoef;k++){
  5081. *Out += Coef[k]*In[-k];
  5082. }
  5083. Out++;
  5084. In++;
  5085. }
  5086. }
  5087. /*----------------------------------------------------------------*
  5088. * pole-zero filter
  5089. *---------------------------------------------------------------*/
  5090. void ZeroPoleFilter(
  5091. float *In, /* (i) In[0] to In[lengthInOut-1] contain
  5092. filter input samples In[-orderCoef] to
  5093. In[-1] contain state of all-zero
  5094. section */
  5095. float *ZeroCoef,/* (i) filter coefficients for all-zero
  5096. section (ZeroCoef[0] is assumed to
  5097. be 1.0) */
  5098. float *PoleCoef,/* (i) filter coefficients for all-pole section
  5099. (ZeroCoef[0] is assumed to be 1.0) */
  5100. int lengthInOut,/* (i) number of input/output samples */
  5101. Andersen, et al. Experimental [Page 126]
  5102. RFC 3951 Internet Low Bit Rate Codec December 2004
  5103. int orderCoef, /* (i) number of filter coefficients */
  5104. float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
  5105. contain state of all-pole section. On
  5106. exit Out[0] to Out[lengthInOut-1]
  5107. contain filtered samples */
  5108. ){
  5109. AllZeroFilter(In,ZeroCoef,lengthInOut,orderCoef,Out);
  5110. AllPoleFilter(Out,PoleCoef,lengthInOut,orderCoef);
  5111. }
  5112. /*----------------------------------------------------------------*
  5113. * downsample (LP filter and decimation)
  5114. *---------------------------------------------------------------*/
  5115. void DownSample (
  5116. float *In, /* (i) input samples */
  5117. float *Coef, /* (i) filter coefficients */
  5118. int lengthIn, /* (i) number of input samples */
  5119. float *state, /* (i) filter state */
  5120. float *Out /* (o) downsampled output */
  5121. ){
  5122. float o;
  5123. float *Out_ptr = Out;
  5124. float *Coef_ptr, *In_ptr;
  5125. float *state_ptr;
  5126. int i, j, stop;
  5127. /* LP filter and decimate at the same time */
  5128. for (i = DELAY_DS; i < lengthIn; i+=FACTOR_DS)
  5129. {
  5130. Coef_ptr = &Coef[0];
  5131. In_ptr = &In[i];
  5132. state_ptr = &state[FILTERORDER_DS-2];
  5133. o = (float)0.0;
  5134. stop = (i < FILTERORDER_DS) ? i + 1 : FILTERORDER_DS;
  5135. for (j = 0; j < stop; j++)
  5136. {
  5137. o += *Coef_ptr++ * (*In_ptr--);
  5138. }
  5139. for (j = i + 1; j < FILTERORDER_DS; j++)
  5140. {
  5141. o += *Coef_ptr++ * (*state_ptr--);
  5142. }
  5143. Andersen, et al. Experimental [Page 127]
  5144. RFC 3951 Internet Low Bit Rate Codec December 2004
  5145. *Out_ptr++ = o;
  5146. }
  5147. /* Get the last part (use zeros as input for the future) */
  5148. for (i=(lengthIn+FACTOR_DS); i<(lengthIn+DELAY_DS);
  5149. i+=FACTOR_DS) {
  5150. o=(float)0.0;
  5151. if (i<lengthIn) {
  5152. Coef_ptr = &Coef[0];
  5153. In_ptr = &In[i];
  5154. for (j=0; j<FILTERORDER_DS; j++) {
  5155. o += *Coef_ptr++ * (*Out_ptr--);
  5156. }
  5157. } else {
  5158. Coef_ptr = &Coef[i-lengthIn];
  5159. In_ptr = &In[lengthIn-1];
  5160. for (j=0; j<FILTERORDER_DS-(i-lengthIn); j++) {
  5161. o += *Coef_ptr++ * (*In_ptr--);
  5162. }
  5163. }
  5164. *Out_ptr++ = o;
  5165. }
  5166. }
  5167. A.19. FrameClassify.h
  5168. /******************************************************************
  5169. iLBC Speech Coder ANSI-C Source Code
  5170. FrameClassify.h
  5171. Copyright (C) The Internet Society (2004).
  5172. All Rights Reserved.
  5173. ******************************************************************/
  5174. #ifndef __iLBC_FRAMECLASSIFY_H
  5175. #define __iLBC_FRAMECLASSIFY_H
  5176. int FrameClassify( /* index to the max-energy sub-frame */
  5177. iLBC_Enc_Inst_t *iLBCenc_inst,
  5178. /* (i/o) the encoder state structure */
  5179. float *residual /* (i) lpc residual signal */
  5180. );
  5181. Andersen, et al. Experimental [Page 128]
  5182. RFC 3951 Internet Low Bit Rate Codec December 2004
  5183. #endif
  5184. A.20. FrameClassify.c
  5185. /******************************************************************
  5186. iLBC Speech Coder ANSI-C Source Code
  5187. FrameClassify.c
  5188. Copyright (C) The Internet Society (2004).
  5189. All Rights Reserved.
  5190. ******************************************************************/
  5191. #include "iLBC_define.h"
  5192. /*---------------------------------------------------------------*
  5193. * Classification of subframes to localize start state
  5194. *--------------------------------------------------------------*/
  5195. int FrameClassify( /* index to the max-energy sub-frame */
  5196. iLBC_Enc_Inst_t *iLBCenc_inst,
  5197. /* (i/o) the encoder state structure */
  5198. float *residual /* (i) lpc residual signal */
  5199. ) {
  5200. float max_ssqEn, fssqEn[NSUB_MAX], bssqEn[NSUB_MAX], *pp;
  5201. int n, l, max_ssqEn_n;
  5202. const float ssqEn_win[NSUB_MAX-1]={(float)0.8,(float)0.9,
  5203. (float)1.0,(float)0.9,(float)0.8};
  5204. const float sampEn_win[5]={(float)1.0/(float)6.0,
  5205. (float)2.0/(float)6.0, (float)3.0/(float)6.0,
  5206. (float)4.0/(float)6.0, (float)5.0/(float)6.0};
  5207. /* init the front and back energies to zero */
  5208. memset(fssqEn, 0, NSUB_MAX*sizeof(float));
  5209. memset(bssqEn, 0, NSUB_MAX*sizeof(float));
  5210. /* Calculate front of first seqence */
  5211. n=0;
  5212. pp=residual;
  5213. for (l=0; l<5; l++) {
  5214. fssqEn[n] += sampEn_win[l] * (*pp) * (*pp);
  5215. pp++;
  5216. }
  5217. for (l=5; l<SUBL; l++) {
  5218. Andersen, et al. Experimental [Page 129]
  5219. RFC 3951 Internet Low Bit Rate Codec December 2004
  5220. fssqEn[n] += (*pp) * (*pp);
  5221. pp++;
  5222. }
  5223. /* Calculate front and back of all middle sequences */
  5224. for (n=1; n<iLBCenc_inst->nsub-1; n++) {
  5225. pp=residual+n*SUBL;
  5226. for (l=0; l<5; l++) {
  5227. fssqEn[n] += sampEn_win[l] * (*pp) * (*pp);
  5228. bssqEn[n] += (*pp) * (*pp);
  5229. pp++;
  5230. }
  5231. for (l=5; l<SUBL-5; l++) {
  5232. fssqEn[n] += (*pp) * (*pp);
  5233. bssqEn[n] += (*pp) * (*pp);
  5234. pp++;
  5235. }
  5236. for (l=SUBL-5; l<SUBL; l++) {
  5237. fssqEn[n] += (*pp) * (*pp);
  5238. bssqEn[n] += sampEn_win[SUBL-l-1] * (*pp) * (*pp);
  5239. pp++;
  5240. }
  5241. }
  5242. /* Calculate back of last seqence */
  5243. n=iLBCenc_inst->nsub-1;
  5244. pp=residual+n*SUBL;
  5245. for (l=0; l<SUBL-5; l++) {
  5246. bssqEn[n] += (*pp) * (*pp);
  5247. pp++;
  5248. }
  5249. for (l=SUBL-5; l<SUBL; l++) {
  5250. bssqEn[n] += sampEn_win[SUBL-l-1] * (*pp) * (*pp);
  5251. pp++;
  5252. }
  5253. /* find the index to the weighted 80 sample with
  5254. most energy */
  5255. if (iLBCenc_inst->mode==20) l=1;
  5256. else l=0;
  5257. max_ssqEn=(fssqEn[0]+bssqEn[1])*ssqEn_win[l];
  5258. max_ssqEn_n=1;
  5259. for (n=2; n<iLBCenc_inst->nsub; n++) {
  5260. Andersen, et al. Experimental [Page 130]
  5261. RFC 3951 Internet Low Bit Rate Codec December 2004
  5262. l++;
  5263. if ((fssqEn[n-1]+bssqEn[n])*ssqEn_win[l] > max_ssqEn) {
  5264. max_ssqEn=(fssqEn[n-1]+bssqEn[n]) *
  5265. ssqEn_win[l];
  5266. max_ssqEn_n=n;
  5267. }
  5268. }
  5269. return max_ssqEn_n;
  5270. }
  5271. A.21. gainquant.h
  5272. /******************************************************************
  5273. iLBC Speech Coder ANSI-C Source Code
  5274. gainquant.h
  5275. Copyright (C) The Internet Society (2004).
  5276. All Rights Reserved.
  5277. ******************************************************************/
  5278. #ifndef __iLBC_GAINQUANT_H
  5279. #define __iLBC_GAINQUANT_H
  5280. float gainquant(/* (o) quantized gain value */
  5281. float in, /* (i) gain value */
  5282. float maxIn,/* (i) maximum of gain value */
  5283. int cblen, /* (i) number of quantization indices */
  5284. int *index /* (o) quantization index */
  5285. );
  5286. float gaindequant( /* (o) quantized gain value */
  5287. int index, /* (i) quantization index */
  5288. float maxIn,/* (i) maximum of unquantized gain */
  5289. int cblen /* (i) number of quantization indices */
  5290. );
  5291. #endif
  5292. A.22. gainquant.c
  5293. /******************************************************************
  5294. iLBC Speech Coder ANSI-C Source Code
  5295. Andersen, et al. Experimental [Page 131]
  5296. RFC 3951 Internet Low Bit Rate Codec December 2004
  5297. gainquant.c
  5298. Copyright (C) The Internet Society (2004).
  5299. All Rights Reserved.
  5300. ******************************************************************/
  5301. #include <string.h>
  5302. #include <math.h>
  5303. #include "constants.h"
  5304. #include "filter.h"
  5305. /*----------------------------------------------------------------*
  5306. * quantizer for the gain in the gain-shape coding of residual
  5307. *---------------------------------------------------------------*/
  5308. float gainquant(/* (o) quantized gain value */
  5309. float in, /* (i) gain value */
  5310. float maxIn,/* (i) maximum of gain value */
  5311. int cblen, /* (i) number of quantization indices */
  5312. int *index /* (o) quantization index */
  5313. ){
  5314. int i, tindex;
  5315. float minmeasure,measure, *cb, scale;
  5316. /* ensure a lower bound on the scaling factor */
  5317. scale=maxIn;
  5318. if (scale<0.1) {
  5319. scale=(float)0.1;
  5320. }
  5321. /* select the quantization table */
  5322. if (cblen == 8) {
  5323. cb = gain_sq3Tbl;
  5324. } else if (cblen == 16) {
  5325. cb = gain_sq4Tbl;
  5326. } else {
  5327. cb = gain_sq5Tbl;
  5328. }
  5329. /* select the best index in the quantization table */
  5330. minmeasure=10000000.0;
  5331. tindex=0;
  5332. for (i=0; i<cblen; i++) {
  5333. Andersen, et al. Experimental [Page 132]
  5334. RFC 3951 Internet Low Bit Rate Codec December 2004
  5335. measure=(in-scale*cb[i])*(in-scale*cb[i]);
  5336. if (measure<minmeasure) {
  5337. tindex=i;
  5338. minmeasure=measure;
  5339. }
  5340. }
  5341. *index=tindex;
  5342. /* return the quantized value */
  5343. return scale*cb[tindex];
  5344. }
  5345. /*----------------------------------------------------------------*
  5346. * decoder for quantized gains in the gain-shape coding of
  5347. * residual
  5348. *---------------------------------------------------------------*/
  5349. float gaindequant( /* (o) quantized gain value */
  5350. int index, /* (i) quantization index */
  5351. float maxIn,/* (i) maximum of unquantized gain */
  5352. int cblen /* (i) number of quantization indices */
  5353. ){
  5354. float scale;
  5355. /* obtain correct scale factor */
  5356. scale=(float)fabs(maxIn);
  5357. if (scale<0.1) {
  5358. scale=(float)0.1;
  5359. }
  5360. /* select the quantization table and return the decoded value */
  5361. if (cblen==8) {
  5362. return scale*gain_sq3Tbl[index];
  5363. } else if (cblen==16) {
  5364. return scale*gain_sq4Tbl[index];
  5365. }
  5366. else if (cblen==32) {
  5367. return scale*gain_sq5Tbl[index];
  5368. }
  5369. return 0.0;
  5370. }
  5371. Andersen, et al. Experimental [Page 133]
  5372. RFC 3951 Internet Low Bit Rate Codec December 2004
  5373. A.23. getCBvec.h
  5374. /******************************************************************
  5375. iLBC Speech Coder ANSI-C Source Code
  5376. getCBvec.h
  5377. Copyright (C) The Internet Society (2004).
  5378. All Rights Reserved.
  5379. ******************************************************************/
  5380. #ifndef __iLBC_GETCBVEC_H
  5381. #define __iLBC_GETCBVEC_H
  5382. void getCBvec(
  5383. float *cbvec, /* (o) Constructed codebook vector */
  5384. float *mem, /* (i) Codebook buffer */
  5385. int index, /* (i) Codebook index */
  5386. int lMem, /* (i) Length of codebook buffer */
  5387. int cbveclen/* (i) Codebook vector length */
  5388. );
  5389. #endif
  5390. A.24. getCBvec.c
  5391. /******************************************************************
  5392. iLBC Speech Coder ANSI-C Source Code
  5393. getCBvec.c
  5394. Copyright (C) The Internet Society (2004).
  5395. All Rights Reserved.
  5396. ******************************************************************/
  5397. #include "iLBC_define.h"
  5398. #include "constants.h"
  5399. #include <string.h>
  5400. /*----------------------------------------------------------------*
  5401. * Construct codebook vector for given index.
  5402. *---------------------------------------------------------------*/
  5403. void getCBvec(
  5404. Andersen, et al. Experimental [Page 134]
  5405. RFC 3951 Internet Low Bit Rate Codec December 2004
  5406. float *cbvec, /* (o) Constructed codebook vector */
  5407. float *mem, /* (i) Codebook buffer */
  5408. int index, /* (i) Codebook index */
  5409. int lMem, /* (i) Length of codebook buffer */
  5410. int cbveclen/* (i) Codebook vector length */
  5411. ){
  5412. int j, k, n, memInd, sFilt;
  5413. float tmpbuf[CB_MEML];
  5414. int base_size;
  5415. int ilow, ihigh;
  5416. float alfa, alfa1;
  5417. /* Determine size of codebook sections */
  5418. base_size=lMem-cbveclen+1;
  5419. if (cbveclen==SUBL) {
  5420. base_size+=cbveclen/2;
  5421. }
  5422. /* No filter -> First codebook section */
  5423. if (index<lMem-cbveclen+1) {
  5424. /* first non-interpolated vectors */
  5425. k=index+cbveclen;
  5426. /* get vector */
  5427. memcpy(cbvec, mem+lMem-k, cbveclen*sizeof(float));
  5428. } else if (index < base_size) {
  5429. k=2*(index-(lMem-cbveclen+1))+cbveclen;
  5430. ihigh=k/2;
  5431. ilow=ihigh-5;
  5432. /* Copy first noninterpolated part */
  5433. memcpy(cbvec, mem+lMem-k/2, ilow*sizeof(float));
  5434. /* interpolation */
  5435. alfa1=(float)0.2;
  5436. alfa=0.0;
  5437. for (j=ilow; j<ihigh; j++) {
  5438. cbvec[j]=((float)1.0-alfa)*mem[lMem-k/2+j]+
  5439. alfa*mem[lMem-k+j];
  5440. Andersen, et al. Experimental [Page 135]
  5441. RFC 3951 Internet Low Bit Rate Codec December 2004
  5442. alfa+=alfa1;
  5443. }
  5444. /* Copy second noninterpolated part */
  5445. memcpy(cbvec+ihigh, mem+lMem-k+ihigh,
  5446. (cbveclen-ihigh)*sizeof(float));
  5447. }
  5448. /* Higher codebook section based on filtering */
  5449. else {
  5450. /* first non-interpolated vectors */
  5451. if (index-base_size<lMem-cbveclen+1) {
  5452. float tempbuff2[CB_MEML+CB_FILTERLEN+1];
  5453. float *pos;
  5454. float *pp, *pp1;
  5455. memset(tempbuff2, 0,
  5456. CB_HALFFILTERLEN*sizeof(float));
  5457. memcpy(&tempbuff2[CB_HALFFILTERLEN], mem,
  5458. lMem*sizeof(float));
  5459. memset(&tempbuff2[lMem+CB_HALFFILTERLEN], 0,
  5460. (CB_HALFFILTERLEN+1)*sizeof(float));
  5461. k=index-base_size+cbveclen;
  5462. sFilt=lMem-k;
  5463. memInd=sFilt+1-CB_HALFFILTERLEN;
  5464. /* do filtering */
  5465. pos=cbvec;
  5466. memset(pos, 0, cbveclen*sizeof(float));
  5467. for (n=0; n<cbveclen; n++) {
  5468. pp=&tempbuff2[memInd+n+CB_HALFFILTERLEN];
  5469. pp1=&cbfiltersTbl[CB_FILTERLEN-1];
  5470. for (j=0; j<CB_FILTERLEN; j++) {
  5471. (*pos)+=(*pp++)*(*pp1--);
  5472. }
  5473. pos++;
  5474. }
  5475. }
  5476. /* interpolated vectors */
  5477. else {
  5478. Andersen, et al. Experimental [Page 136]
  5479. RFC 3951 Internet Low Bit Rate Codec December 2004
  5480. float tempbuff2[CB_MEML+CB_FILTERLEN+1];
  5481. float *pos;
  5482. float *pp, *pp1;
  5483. int i;
  5484. memset(tempbuff2, 0,
  5485. CB_HALFFILTERLEN*sizeof(float));
  5486. memcpy(&tempbuff2[CB_HALFFILTERLEN], mem,
  5487. lMem*sizeof(float));
  5488. memset(&tempbuff2[lMem+CB_HALFFILTERLEN], 0,
  5489. (CB_HALFFILTERLEN+1)*sizeof(float));
  5490. k=2*(index-base_size-
  5491. (lMem-cbveclen+1))+cbveclen;
  5492. sFilt=lMem-k;
  5493. memInd=sFilt+1-CB_HALFFILTERLEN;
  5494. /* do filtering */
  5495. pos=&tmpbuf[sFilt];
  5496. memset(pos, 0, k*sizeof(float));
  5497. for (i=0; i<k; i++) {
  5498. pp=&tempbuff2[memInd+i+CB_HALFFILTERLEN];
  5499. pp1=&cbfiltersTbl[CB_FILTERLEN-1];
  5500. for (j=0; j<CB_FILTERLEN; j++) {
  5501. (*pos)+=(*pp++)*(*pp1--);
  5502. }
  5503. pos++;
  5504. }
  5505. ihigh=k/2;
  5506. ilow=ihigh-5;
  5507. /* Copy first noninterpolated part */
  5508. memcpy(cbvec, tmpbuf+lMem-k/2,
  5509. ilow*sizeof(float));
  5510. /* interpolation */
  5511. alfa1=(float)0.2;
  5512. alfa=0.0;
  5513. for (j=ilow; j<ihigh; j++) {
  5514. cbvec[j]=((float)1.0-alfa)*
  5515. tmpbuf[lMem-k/2+j]+alfa*tmpbuf[lMem-k+j];
  5516. alfa+=alfa1;
  5517. }
  5518. Andersen, et al. Experimental [Page 137]
  5519. RFC 3951 Internet Low Bit Rate Codec December 2004
  5520. /* Copy second noninterpolated part */
  5521. memcpy(cbvec+ihigh, tmpbuf+lMem-k+ihigh,
  5522. (cbveclen-ihigh)*sizeof(float));
  5523. }
  5524. }
  5525. }
  5526. A.25. helpfun.h
  5527. /******************************************************************
  5528. iLBC Speech Coder ANSI-C Source Code
  5529. helpfun.h
  5530. Copyright (C) The Internet Society (2004).
  5531. All Rights Reserved.
  5532. ******************************************************************/
  5533. #ifndef __iLBC_HELPFUN_H
  5534. #define __iLBC_HELPFUN_H
  5535. void autocorr(
  5536. float *r, /* (o) autocorrelation vector */
  5537. const float *x, /* (i) data vector */
  5538. int N, /* (i) length of data vector */
  5539. int order /* largest lag for calculated
  5540. autocorrelations */
  5541. );
  5542. void window(
  5543. float *z, /* (o) the windowed data */
  5544. const float *x, /* (i) the original data vector */
  5545. const float *y, /* (i) the window */
  5546. int N /* (i) length of all vectors */
  5547. );
  5548. void levdurb(
  5549. float *a, /* (o) lpc coefficient vector starting
  5550. with 1.0 */
  5551. float *k, /* (o) reflection coefficients */
  5552. float *r, /* (i) autocorrelation vector */
  5553. int order /* (i) order of lpc filter */
  5554. );
  5555. void interpolate(
  5556. Andersen, et al. Experimental [Page 138]
  5557. RFC 3951 Internet Low Bit Rate Codec December 2004
  5558. float *out, /* (o) the interpolated vector */
  5559. float *in1, /* (i) the first vector for the
  5560. interpolation */
  5561. float *in2, /* (i) the second vector for the
  5562. interpolation */
  5563. float coef, /* (i) interpolation weights */
  5564. int length /* (i) length of all vectors */
  5565. );
  5566. void bwexpand(
  5567. float *out, /* (o) the bandwidth expanded lpc
  5568. coefficients */
  5569. float *in, /* (i) the lpc coefficients before bandwidth
  5570. expansion */
  5571. float coef, /* (i) the bandwidth expansion factor */
  5572. int length /* (i) the length of lpc coefficient vectors */
  5573. );
  5574. void vq(
  5575. float *Xq, /* (o) the quantized vector */
  5576. int *index, /* (o) the quantization index */
  5577. const float *CB,/* (i) the vector quantization codebook */
  5578. float *X, /* (i) the vector to quantize */
  5579. int n_cb, /* (i) the number of vectors in the codebook */
  5580. int dim /* (i) the dimension of all vectors */
  5581. );
  5582. void SplitVQ(
  5583. float *qX, /* (o) the quantized vector */
  5584. int *index, /* (o) a vector of indexes for all vector
  5585. codebooks in the split */
  5586. float *X, /* (i) the vector to quantize */
  5587. const float *CB,/* (i) the quantizer codebook */
  5588. int nsplit, /* the number of vector splits */
  5589. const int *dim, /* the dimension of X and qX */
  5590. const int *cbsize /* the number of vectors in the codebook */
  5591. );
  5592. void sort_sq(
  5593. float *xq, /* (o) the quantized value */
  5594. int *index, /* (o) the quantization index */
  5595. float x, /* (i) the value to quantize */
  5596. const float *cb,/* (i) the quantization codebook */
  5597. int cb_size /* (i) the size of the quantization codebook */
  5598. );
  5599. int LSF_check( /* (o) 1 for stable lsf vectors and 0 for
  5600. Andersen, et al. Experimental [Page 139]
  5601. RFC 3951 Internet Low Bit Rate Codec December 2004
  5602. nonstable ones */
  5603. float *lsf, /* (i) a table of lsf vectors */
  5604. int dim, /* (i) the dimension of each lsf vector */
  5605. int NoAn /* (i) the number of lsf vectors in the
  5606. table */
  5607. );
  5608. #endif
  5609. A.26. helpfun.c
  5610. /******************************************************************
  5611. iLBC Speech Coder ANSI-C Source Code
  5612. helpfun.c
  5613. Copyright (C) The Internet Society (2004).
  5614. All Rights Reserved.
  5615. ******************************************************************/
  5616. #include <math.h>
  5617. #include "iLBC_define.h"
  5618. #include "constants.h"
  5619. /*----------------------------------------------------------------*
  5620. * calculation of auto correlation
  5621. *---------------------------------------------------------------*/
  5622. void autocorr(
  5623. float *r, /* (o) autocorrelation vector */
  5624. const float *x, /* (i) data vector */
  5625. int N, /* (i) length of data vector */
  5626. int order /* largest lag for calculated
  5627. autocorrelations */
  5628. ){
  5629. int lag, n;
  5630. float sum;
  5631. for (lag = 0; lag <= order; lag++) {
  5632. sum = 0;
  5633. for (n = 0; n < N - lag; n++) {
  5634. sum += x[n] * x[n+lag];
  5635. }
  5636. r[lag] = sum;
  5637. }
  5638. Andersen, et al. Experimental [Page 140]
  5639. RFC 3951 Internet Low Bit Rate Codec December 2004
  5640. }
  5641. /*----------------------------------------------------------------*
  5642. * window multiplication
  5643. *---------------------------------------------------------------*/
  5644. void window(
  5645. float *z, /* (o) the windowed data */
  5646. const float *x, /* (i) the original data vector */
  5647. const float *y, /* (i) the window */
  5648. int N /* (i) length of all vectors */
  5649. ){
  5650. int i;
  5651. for (i = 0; i < N; i++) {
  5652. z[i] = x[i] * y[i];
  5653. }
  5654. }
  5655. /*----------------------------------------------------------------*
  5656. * levinson-durbin solution for lpc coefficients
  5657. *---------------------------------------------------------------*/
  5658. void levdurb(
  5659. float *a, /* (o) lpc coefficient vector starting
  5660. with 1.0 */
  5661. float *k, /* (o) reflection coefficients */
  5662. float *r, /* (i) autocorrelation vector */
  5663. int order /* (i) order of lpc filter */
  5664. ){
  5665. float sum, alpha;
  5666. int m, m_h, i;
  5667. a[0] = 1.0;
  5668. if (r[0] < EPS) { /* if r[0] <= 0, set LPC coeff. to zero */
  5669. for (i = 0; i < order; i++) {
  5670. k[i] = 0;
  5671. a[i+1] = 0;
  5672. }
  5673. } else {
  5674. a[1] = k[0] = -r[1]/r[0];
  5675. alpha = r[0] + r[1] * k[0];
  5676. for (m = 1; m < order; m++){
  5677. sum = r[m + 1];
  5678. for (i = 0; i < m; i++){
  5679. sum += a[i+1] * r[m - i];
  5680. }
  5681. Andersen, et al. Experimental [Page 141]
  5682. RFC 3951 Internet Low Bit Rate Codec December 2004
  5683. k[m] = -sum / alpha;
  5684. alpha += k[m] * sum;
  5685. m_h = (m + 1) >> 1;
  5686. for (i = 0; i < m_h; i++){
  5687. sum = a[i+1] + k[m] * a[m - i];
  5688. a[m - i] += k[m] * a[i+1];
  5689. a[i+1] = sum;
  5690. }
  5691. a[m+1] = k[m];
  5692. }
  5693. }
  5694. }
  5695. /*----------------------------------------------------------------*
  5696. * interpolation between vectors
  5697. *---------------------------------------------------------------*/
  5698. void interpolate(
  5699. float *out, /* (o) the interpolated vector */
  5700. float *in1, /* (i) the first vector for the
  5701. interpolation */
  5702. float *in2, /* (i) the second vector for the
  5703. interpolation */
  5704. float coef, /* (i) interpolation weights */
  5705. int length /* (i) length of all vectors */
  5706. ){
  5707. int i;
  5708. float invcoef;
  5709. invcoef = (float)1.0 - coef;
  5710. for (i = 0; i < length; i++) {
  5711. out[i] = coef * in1[i] + invcoef * in2[i];
  5712. }
  5713. }
  5714. /*----------------------------------------------------------------*
  5715. * lpc bandwidth expansion
  5716. *---------------------------------------------------------------*/
  5717. void bwexpand(
  5718. float *out, /* (o) the bandwidth expanded lpc
  5719. coefficients */
  5720. float *in, /* (i) the lpc coefficients before bandwidth
  5721. expansion */
  5722. float coef, /* (i) the bandwidth expansion factor */
  5723. int length /* (i) the length of lpc coefficient vectors */
  5724. ){
  5725. int i;
  5726. Andersen, et al. Experimental [Page 142]
  5727. RFC 3951 Internet Low Bit Rate Codec December 2004
  5728. float chirp;
  5729. chirp = coef;
  5730. out[0] = in[0];
  5731. for (i = 1; i < length; i++) {
  5732. out[i] = chirp * in[i];
  5733. chirp *= coef;
  5734. }
  5735. }
  5736. /*----------------------------------------------------------------*
  5737. * vector quantization
  5738. *---------------------------------------------------------------*/
  5739. void vq(
  5740. float *Xq, /* (o) the quantized vector */
  5741. int *index, /* (o) the quantization index */
  5742. const float *CB,/* (i) the vector quantization codebook */
  5743. float *X, /* (i) the vector to quantize */
  5744. int n_cb, /* (i) the number of vectors in the codebook */
  5745. int dim /* (i) the dimension of all vectors */
  5746. ){
  5747. int i, j;
  5748. int pos, minindex;
  5749. float dist, tmp, mindist;
  5750. pos = 0;
  5751. mindist = FLOAT_MAX;
  5752. minindex = 0;
  5753. for (j = 0; j < n_cb; j++) {
  5754. dist = X[0] - CB[pos];
  5755. dist *= dist;
  5756. for (i = 1; i < dim; i++) {
  5757. tmp = X[i] - CB[pos + i];
  5758. dist += tmp*tmp;
  5759. }
  5760. if (dist < mindist) {
  5761. mindist = dist;
  5762. minindex = j;
  5763. }
  5764. pos += dim;
  5765. }
  5766. for (i = 0; i < dim; i++) {
  5767. Xq[i] = CB[minindex*dim + i];
  5768. }
  5769. *index = minindex;
  5770. Andersen, et al. Experimental [Page 143]
  5771. RFC 3951 Internet Low Bit Rate Codec December 2004
  5772. }
  5773. /*----------------------------------------------------------------*
  5774. * split vector quantization
  5775. *---------------------------------------------------------------*/
  5776. void SplitVQ(
  5777. float *qX, /* (o) the quantized vector */
  5778. int *index, /* (o) a vector of indexes for all vector
  5779. codebooks in the split */
  5780. float *X, /* (i) the vector to quantize */
  5781. const float *CB,/* (i) the quantizer codebook */
  5782. int nsplit, /* the number of vector splits */
  5783. const int *dim, /* the dimension of X and qX */
  5784. const int *cbsize /* the number of vectors in the codebook */
  5785. ){
  5786. int cb_pos, X_pos, i;
  5787. cb_pos = 0;
  5788. X_pos= 0;
  5789. for (i = 0; i < nsplit; i++) {
  5790. vq(qX + X_pos, index + i, CB + cb_pos, X + X_pos,
  5791. cbsize[i], dim[i]);
  5792. X_pos += dim[i];
  5793. cb_pos += dim[i] * cbsize[i];
  5794. }
  5795. }
  5796. /*----------------------------------------------------------------*
  5797. * scalar quantization
  5798. *---------------------------------------------------------------*/
  5799. void sort_sq(
  5800. float *xq, /* (o) the quantized value */
  5801. int *index, /* (o) the quantization index */
  5802. float x, /* (i) the value to quantize */
  5803. const float *cb,/* (i) the quantization codebook */
  5804. int cb_size /* (i) the size of the quantization codebook */
  5805. ){
  5806. int i;
  5807. if (x <= cb[0]) {
  5808. *index = 0;
  5809. *xq = cb[0];
  5810. } else {
  5811. i = 0;
  5812. while ((x > cb[i]) && i < cb_size - 1) {
  5813. i++;
  5814. Andersen, et al. Experimental [Page 144]
  5815. RFC 3951 Internet Low Bit Rate Codec December 2004
  5816. }
  5817. if (x > ((cb[i] + cb[i - 1])/2)) {
  5818. *index = i;
  5819. *xq = cb[i];
  5820. } else {
  5821. *index = i - 1;
  5822. *xq = cb[i - 1];
  5823. }
  5824. }
  5825. }
  5826. /*----------------------------------------------------------------*
  5827. * check for stability of lsf coefficients
  5828. *---------------------------------------------------------------*/
  5829. int LSF_check( /* (o) 1 for stable lsf vectors and 0 for
  5830. nonstable ones */
  5831. float *lsf, /* (i) a table of lsf vectors */
  5832. int dim, /* (i) the dimension of each lsf vector */
  5833. int NoAn /* (i) the number of lsf vectors in the
  5834. table */
  5835. ){
  5836. int k,n,m, Nit=2, change=0,pos;
  5837. float tmp;
  5838. static float eps=(float)0.039; /* 50 Hz */
  5839. static float eps2=(float)0.0195;
  5840. static float maxlsf=(float)3.14; /* 4000 Hz */
  5841. static float minlsf=(float)0.01; /* 0 Hz */
  5842. /* LSF separation check*/
  5843. for (n=0; n<Nit; n++) { /* Run through a couple of times */
  5844. for (m=0; m<NoAn; m++) { /* Number of analyses per frame */
  5845. for (k=0; k<(dim-1); k++) {
  5846. pos=m*dim+k;
  5847. if ((lsf[pos+1]-lsf[pos])<eps) {
  5848. if (lsf[pos+1]<lsf[pos]) {
  5849. tmp=lsf[pos+1];
  5850. lsf[pos+1]= lsf[pos]+eps2;
  5851. lsf[pos]= lsf[pos+1]-eps2;
  5852. } else {
  5853. lsf[pos]-=eps2;
  5854. lsf[pos+1]+=eps2;
  5855. }
  5856. change=1;
  5857. Andersen, et al. Experimental [Page 145]
  5858. RFC 3951 Internet Low Bit Rate Codec December 2004
  5859. }
  5860. if (lsf[pos]<minlsf) {
  5861. lsf[pos]=minlsf;
  5862. change=1;
  5863. }
  5864. if (lsf[pos]>maxlsf) {
  5865. lsf[pos]=maxlsf;
  5866. change=1;
  5867. }
  5868. }
  5869. }
  5870. }
  5871. return change;
  5872. }
  5873. A.27. hpInput.h
  5874. /******************************************************************
  5875. iLBC Speech Coder ANSI-C Source Code
  5876. hpInput.h
  5877. Copyright (C) The Internet Society (2004).
  5878. All Rights Reserved.
  5879. ******************************************************************/
  5880. #ifndef __iLBC_HPINPUT_H
  5881. #define __iLBC_HPINPUT_H
  5882. void hpInput(
  5883. float *In, /* (i) vector to filter */
  5884. int len, /* (i) length of vector to filter */
  5885. float *Out, /* (o) the resulting filtered vector */
  5886. float *mem /* (i/o) the filter state */
  5887. );
  5888. #endif
  5889. A.28. hpInput.c
  5890. /******************************************************************
  5891. iLBC Speech Coder ANSI-C Source Code
  5892. Andersen, et al. Experimental [Page 146]
  5893. RFC 3951 Internet Low Bit Rate Codec December 2004
  5894. hpInput.c
  5895. Copyright (C) The Internet Society (2004).
  5896. All Rights Reserved.
  5897. ******************************************************************/
  5898. #include "constants.h"
  5899. /*----------------------------------------------------------------*
  5900. * Input high-pass filter
  5901. *---------------------------------------------------------------*/
  5902. void hpInput(
  5903. float *In, /* (i) vector to filter */
  5904. int len, /* (i) length of vector to filter */
  5905. float *Out, /* (o) the resulting filtered vector */
  5906. float *mem /* (i/o) the filter state */
  5907. ){
  5908. int i;
  5909. float *pi, *po;
  5910. /* all-zero section*/
  5911. pi = &In[0];
  5912. po = &Out[0];
  5913. for (i=0; i<len; i++) {
  5914. *po = hpi_zero_coefsTbl[0] * (*pi);
  5915. *po += hpi_zero_coefsTbl[1] * mem[0];
  5916. *po += hpi_zero_coefsTbl[2] * mem[1];
  5917. mem[1] = mem[0];
  5918. mem[0] = *pi;
  5919. po++;
  5920. pi++;
  5921. }
  5922. /* all-pole section*/
  5923. po = &Out[0];
  5924. for (i=0; i<len; i++) {
  5925. *po -= hpi_pole_coefsTbl[1] * mem[2];
  5926. *po -= hpi_pole_coefsTbl[2] * mem[3];
  5927. mem[3] = mem[2];
  5928. mem[2] = *po;
  5929. po++;
  5930. Andersen, et al. Experimental [Page 147]
  5931. RFC 3951 Internet Low Bit Rate Codec December 2004
  5932. }
  5933. }
  5934. A.29. hpOutput.h
  5935. /******************************************************************
  5936. iLBC Speech Coder ANSI-C Source Code
  5937. hpOutput.h
  5938. Copyright (C) The Internet Society (2004).
  5939. All Rights Reserved.
  5940. ******************************************************************/
  5941. #ifndef __iLBC_HPOUTPUT_H
  5942. #define __iLBC_HPOUTPUT_H
  5943. void hpOutput(
  5944. float *In, /* (i) vector to filter */
  5945. int len,/* (i) length of vector to filter */
  5946. float *Out, /* (o) the resulting filtered vector */
  5947. float *mem /* (i/o) the filter state */
  5948. );
  5949. #endif
  5950. A.30. hpOutput.c
  5951. /******************************************************************
  5952. iLBC Speech Coder ANSI-C Source Code
  5953. hpOutput.c
  5954. Copyright (C) The Internet Society (2004).
  5955. All Rights Reserved.
  5956. ******************************************************************/
  5957. #include "constants.h"
  5958. /*----------------------------------------------------------------*
  5959. * Output high-pass filter
  5960. *---------------------------------------------------------------*/
  5961. void hpOutput(
  5962. Andersen, et al. Experimental [Page 148]
  5963. RFC 3951 Internet Low Bit Rate Codec December 2004
  5964. float *In, /* (i) vector to filter */
  5965. int len,/* (i) length of vector to filter */
  5966. float *Out, /* (o) the resulting filtered vector */
  5967. float *mem /* (i/o) the filter state */
  5968. ){
  5969. int i;
  5970. float *pi, *po;
  5971. /* all-zero section*/
  5972. pi = &In[0];
  5973. po = &Out[0];
  5974. for (i=0; i<len; i++) {
  5975. *po = hpo_zero_coefsTbl[0] * (*pi);
  5976. *po += hpo_zero_coefsTbl[1] * mem[0];
  5977. *po += hpo_zero_coefsTbl[2] * mem[1];
  5978. mem[1] = mem[0];
  5979. mem[0] = *pi;
  5980. po++;
  5981. pi++;
  5982. }
  5983. /* all-pole section*/
  5984. po = &Out[0];
  5985. for (i=0; i<len; i++) {
  5986. *po -= hpo_pole_coefsTbl[1] * mem[2];
  5987. *po -= hpo_pole_coefsTbl[2] * mem[3];
  5988. mem[3] = mem[2];
  5989. mem[2] = *po;
  5990. po++;
  5991. }
  5992. }
  5993. A.31. iCBConstruct.h
  5994. /******************************************************************
  5995. iLBC Speech Coder ANSI-C Source Code
  5996. iCBConstruct.h
  5997. Copyright (C) The Internet Society (2004).
  5998. All Rights Reserved.
  5999. Andersen, et al. Experimental [Page 149]
  6000. RFC 3951 Internet Low Bit Rate Codec December 2004
  6001. ******************************************************************/
  6002. #ifndef __iLBC_ICBCONSTRUCT_H
  6003. #define __iLBC_ICBCONSTRUCT_H
  6004. void index_conv_enc(
  6005. int *index /* (i/o) Codebook indexes */
  6006. );
  6007. void index_conv_dec(
  6008. int *index /* (i/o) Codebook indexes */
  6009. );
  6010. void iCBConstruct(
  6011. float *decvector, /* (o) Decoded vector */
  6012. int *index, /* (i) Codebook indices */
  6013. int *gain_index,/* (i) Gain quantization indices */
  6014. float *mem, /* (i) Buffer for codevector construction */
  6015. int lMem, /* (i) Length of buffer */
  6016. int veclen, /* (i) Length of vector */
  6017. int nStages /* (i) Number of codebook stages */
  6018. );
  6019. #endif
  6020. A.32. iCBConstruct.c
  6021. /******************************************************************
  6022. iLBC Speech Coder ANSI-C Source Code
  6023. iCBConstruct.c
  6024. Copyright (C) The Internet Society (2004).
  6025. All Rights Reserved.
  6026. ******************************************************************/
  6027. #include <math.h>
  6028. #include "iLBC_define.h"
  6029. #include "gainquant.h"
  6030. #include "getCBvec.h"
  6031. /*----------------------------------------------------------------*
  6032. * Convert the codebook indexes to make the search easier
  6033. *---------------------------------------------------------------*/
  6034. Andersen, et al. Experimental [Page 150]
  6035. RFC 3951 Internet Low Bit Rate Codec December 2004
  6036. void index_conv_enc(
  6037. int *index /* (i/o) Codebook indexes */
  6038. ){
  6039. int k;
  6040. for (k=1; k<CB_NSTAGES; k++) {
  6041. if ((index[k]>=108)&&(index[k]<172)) {
  6042. index[k]-=64;
  6043. } else if (index[k]>=236) {
  6044. index[k]-=128;
  6045. } else {
  6046. /* ERROR */
  6047. }
  6048. }
  6049. }
  6050. void index_conv_dec(
  6051. int *index /* (i/o) Codebook indexes */
  6052. ){
  6053. int k;
  6054. for (k=1; k<CB_NSTAGES; k++) {
  6055. if ((index[k]>=44)&&(index[k]<108)) {
  6056. index[k]+=64;
  6057. } else if ((index[k]>=108)&&(index[k]<128)) {
  6058. index[k]+=128;
  6059. } else {
  6060. /* ERROR */
  6061. }
  6062. }
  6063. }
  6064. /*----------------------------------------------------------------*
  6065. * Construct decoded vector from codebook and gains.
  6066. *---------------------------------------------------------------*/
  6067. void iCBConstruct(
  6068. float *decvector, /* (o) Decoded vector */
  6069. int *index, /* (i) Codebook indices */
  6070. int *gain_index,/* (i) Gain quantization indices */
  6071. float *mem, /* (i) Buffer for codevector construction */
  6072. int lMem, /* (i) Length of buffer */
  6073. int veclen, /* (i) Length of vector */
  6074. int nStages /* (i) Number of codebook stages */
  6075. ){
  6076. int j,k;
  6077. Andersen, et al. Experimental [Page 151]
  6078. RFC 3951 Internet Low Bit Rate Codec December 2004
  6079. float gain[CB_NSTAGES];
  6080. float cbvec[SUBL];
  6081. /* gain de-quantization */
  6082. gain[0] = gaindequant(gain_index[0], 1.0, 32);
  6083. if (nStages > 1) {
  6084. gain[1] = gaindequant(gain_index[1],
  6085. (float)fabs(gain[0]), 16);
  6086. }
  6087. if (nStages > 2) {
  6088. gain[2] = gaindequant(gain_index[2],
  6089. (float)fabs(gain[1]), 8);
  6090. }
  6091. /* codebook vector construction and construction of
  6092. total vector */
  6093. getCBvec(cbvec, mem, index[0], lMem, veclen);
  6094. for (j=0;j<veclen;j++){
  6095. decvector[j] = gain[0]*cbvec[j];
  6096. }
  6097. if (nStages > 1) {
  6098. for (k=1; k<nStages; k++) {
  6099. getCBvec(cbvec, mem, index[k], lMem, veclen);
  6100. for (j=0;j<veclen;j++) {
  6101. decvector[j] += gain[k]*cbvec[j];
  6102. }
  6103. }
  6104. }
  6105. }
  6106. A.33. iCBSearch.h
  6107. /******************************************************************
  6108. iLBC Speech Coder ANSI-C Source Code
  6109. iCBSearch.h
  6110. Copyright (C) The Internet Society (2004).
  6111. All Rights Reserved.
  6112. ******************************************************************/
  6113. #ifndef __iLBC_ICBSEARCH_H
  6114. #define __iLBC_ICBSEARCH_H
  6115. Andersen, et al. Experimental [Page 152]
  6116. RFC 3951 Internet Low Bit Rate Codec December 2004
  6117. void iCBSearch(
  6118. iLBC_Enc_Inst_t *iLBCenc_inst,
  6119. /* (i) the encoder state structure */
  6120. int *index, /* (o) Codebook indices */
  6121. int *gain_index,/* (o) Gain quantization indices */
  6122. float *intarget,/* (i) Target vector for encoding */
  6123. float *mem, /* (i) Buffer for codebook construction */
  6124. int lMem, /* (i) Length of buffer */
  6125. int lTarget, /* (i) Length of vector */
  6126. int nStages, /* (i) Number of codebook stages */
  6127. float *weightDenum, /* (i) weighting filter coefficients */
  6128. float *weightState, /* (i) weighting filter state */
  6129. int block /* (i) the sub-block number */
  6130. );
  6131. #endif
  6132. A.34. iCBSearch.c
  6133. /******************************************************************
  6134. iLBC Speech Coder ANSI-C Source Code
  6135. iCBSearch.c
  6136. Copyright (C) The Internet Society (2004).
  6137. All Rights Reserved.
  6138. ******************************************************************/
  6139. #include <math.h>
  6140. #include <string.h>
  6141. #include "iLBC_define.h"
  6142. #include "gainquant.h"
  6143. #include "createCB.h"
  6144. #include "filter.h"
  6145. #include "constants.h"
  6146. /*----------------------------------------------------------------*
  6147. * Search routine for codebook encoding and gain quantization.
  6148. *---------------------------------------------------------------*/
  6149. void iCBSearch(
  6150. iLBC_Enc_Inst_t *iLBCenc_inst,
  6151. /* (i) the encoder state structure */
  6152. int *index, /* (o) Codebook indices */
  6153. int *gain_index,/* (o) Gain quantization indices */
  6154. Andersen, et al. Experimental [Page 153]
  6155. RFC 3951 Internet Low Bit Rate Codec December 2004
  6156. float *intarget,/* (i) Target vector for encoding */
  6157. float *mem, /* (i) Buffer for codebook construction */
  6158. int lMem, /* (i) Length of buffer */
  6159. int lTarget, /* (i) Length of vector */
  6160. int nStages, /* (i) Number of codebook stages */
  6161. float *weightDenum, /* (i) weighting filter coefficients */
  6162. float *weightState, /* (i) weighting filter state */
  6163. int block /* (i) the sub-block number */
  6164. ){
  6165. int i, j, icount, stage, best_index, range, counter;
  6166. float max_measure, gain, measure, crossDot, ftmp;
  6167. float gains[CB_NSTAGES];
  6168. float target[SUBL];
  6169. int base_index, sInd, eInd, base_size;
  6170. int sIndAug=0, eIndAug=0;
  6171. float buf[CB_MEML+SUBL+2*LPC_FILTERORDER];
  6172. float invenergy[CB_EXPAND*128], energy[CB_EXPAND*128];
  6173. float *pp, *ppi=0, *ppo=0, *ppe=0;
  6174. float cbvectors[CB_MEML];
  6175. float tene, cene, cvec[SUBL];
  6176. float aug_vec[SUBL];
  6177. memset(cvec,0,SUBL*sizeof(float));
  6178. /* Determine size of codebook sections */
  6179. base_size=lMem-lTarget+1;
  6180. if (lTarget==SUBL) {
  6181. base_size=lMem-lTarget+1+lTarget/2;
  6182. }
  6183. /* setup buffer for weighting */
  6184. memcpy(buf,weightState,sizeof(float)*LPC_FILTERORDER);
  6185. memcpy(buf+LPC_FILTERORDER,mem,lMem*sizeof(float));
  6186. memcpy(buf+LPC_FILTERORDER+lMem,intarget,lTarget*sizeof(float));
  6187. /* weighting */
  6188. AllPoleFilter(buf+LPC_FILTERORDER, weightDenum,
  6189. lMem+lTarget, LPC_FILTERORDER);
  6190. /* Construct the codebook and target needed */
  6191. memcpy(target, buf+LPC_FILTERORDER+lMem, lTarget*sizeof(float));
  6192. tene=0.0;
  6193. Andersen, et al. Experimental [Page 154]
  6194. RFC 3951 Internet Low Bit Rate Codec December 2004
  6195. for (i=0; i<lTarget; i++) {
  6196. tene+=target[i]*target[i];
  6197. }
  6198. /* Prepare search over one more codebook section. This section
  6199. is created by filtering the original buffer with a filter. */
  6200. filteredCBvecs(cbvectors, buf+LPC_FILTERORDER, lMem);
  6201. /* The Main Loop over stages */
  6202. for (stage=0; stage<nStages; stage++) {
  6203. range = search_rangeTbl[block][stage];
  6204. /* initialize search measure */
  6205. max_measure = (float)-10000000.0;
  6206. gain = (float)0.0;
  6207. best_index = 0;
  6208. /* Compute cross dot product between the target
  6209. and the CB memory */
  6210. crossDot=0.0;
  6211. pp=buf+LPC_FILTERORDER+lMem-lTarget;
  6212. for (j=0; j<lTarget; j++) {
  6213. crossDot += target[j]*(*pp++);
  6214. }
  6215. if (stage==0) {
  6216. /* Calculate energy in the first block of
  6217. 'lTarget' samples. */
  6218. ppe = energy;
  6219. ppi = buf+LPC_FILTERORDER+lMem-lTarget-1;
  6220. ppo = buf+LPC_FILTERORDER+lMem-1;
  6221. *ppe=0.0;
  6222. pp=buf+LPC_FILTERORDER+lMem-lTarget;
  6223. for (j=0; j<lTarget; j++) {
  6224. *ppe+=(*pp)*(*pp++);
  6225. }
  6226. if (*ppe>0.0) {
  6227. invenergy[0] = (float) 1.0 / (*ppe + EPS);
  6228. } else {
  6229. invenergy[0] = (float) 0.0;
  6230. Andersen, et al. Experimental [Page 155]
  6231. RFC 3951 Internet Low Bit Rate Codec December 2004
  6232. }
  6233. ppe++;
  6234. measure=(float)-10000000.0;
  6235. if (crossDot > 0.0) {
  6236. measure = crossDot*crossDot*invenergy[0];
  6237. }
  6238. }
  6239. else {
  6240. measure = crossDot*crossDot*invenergy[0];
  6241. }
  6242. /* check if measure is better */
  6243. ftmp = crossDot*invenergy[0];
  6244. if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
  6245. best_index = 0;
  6246. max_measure = measure;
  6247. gain = ftmp;
  6248. }
  6249. /* loop over the main first codebook section,
  6250. full search */
  6251. for (icount=1; icount<range; icount++) {
  6252. /* calculate measure */
  6253. crossDot=0.0;
  6254. pp = buf+LPC_FILTERORDER+lMem-lTarget-icount;
  6255. for (j=0; j<lTarget; j++) {
  6256. crossDot += target[j]*(*pp++);
  6257. }
  6258. if (stage==0) {
  6259. *ppe++ = energy[icount-1] + (*ppi)*(*ppi) -
  6260. (*ppo)*(*ppo);
  6261. ppo--;
  6262. ppi--;
  6263. if (energy[icount]>0.0) {
  6264. invenergy[icount] =
  6265. (float)1.0/(energy[icount]+EPS);
  6266. } else {
  6267. invenergy[icount] = (float) 0.0;
  6268. }
  6269. Andersen, et al. Experimental [Page 156]
  6270. RFC 3951 Internet Low Bit Rate Codec December 2004
  6271. measure=(float)-10000000.0;
  6272. if (crossDot > 0.0) {
  6273. measure = crossDot*crossDot*invenergy[icount];
  6274. }
  6275. }
  6276. else {
  6277. measure = crossDot*crossDot*invenergy[icount];
  6278. }
  6279. /* check if measure is better */
  6280. ftmp = crossDot*invenergy[icount];
  6281. if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
  6282. best_index = icount;
  6283. max_measure = measure;
  6284. gain = ftmp;
  6285. }
  6286. }
  6287. /* Loop over augmented part in the first codebook
  6288. * section, full search.
  6289. * The vectors are interpolated.
  6290. */
  6291. if (lTarget==SUBL) {
  6292. /* Search for best possible cb vector and
  6293. compute the CB-vectors' energy. */
  6294. searchAugmentedCB(20, 39, stage, base_size-lTarget/2,
  6295. target, buf+LPC_FILTERORDER+lMem,
  6296. &max_measure, &best_index, &gain, energy,
  6297. invenergy);
  6298. }
  6299. /* set search range for following codebook sections */
  6300. base_index=best_index;
  6301. /* unrestricted search */
  6302. if (CB_RESRANGE == -1) {
  6303. sInd=0;
  6304. eInd=range-1;
  6305. sIndAug=20;
  6306. eIndAug=39;
  6307. }
  6308. Andersen, et al. Experimental [Page 157]
  6309. RFC 3951 Internet Low Bit Rate Codec December 2004
  6310. /* restricted search around best index from first
  6311. codebook section */
  6312. else {
  6313. /* Initialize search indices */
  6314. sIndAug=0;
  6315. eIndAug=0;
  6316. sInd=base_index-CB_RESRANGE/2;
  6317. eInd=sInd+CB_RESRANGE;
  6318. if (lTarget==SUBL) {
  6319. if (sInd<0) {
  6320. sIndAug = 40 + sInd;
  6321. eIndAug = 39;
  6322. sInd=0;
  6323. } else if ( base_index < (base_size-20) ) {
  6324. if (eInd > range) {
  6325. sInd -= (eInd-range);
  6326. eInd = range;
  6327. }
  6328. } else { /* base_index >= (base_size-20) */
  6329. if (sInd < (base_size-20)) {
  6330. sIndAug = 20;
  6331. sInd = 0;
  6332. eInd = 0;
  6333. eIndAug = 19 + CB_RESRANGE;
  6334. if(eIndAug > 39) {
  6335. eInd = eIndAug-39;
  6336. eIndAug = 39;
  6337. }
  6338. } else {
  6339. sIndAug = 20 + sInd - (base_size-20);
  6340. eIndAug = 39;
  6341. sInd = 0;
  6342. eInd = CB_RESRANGE - (eIndAug-sIndAug+1);
  6343. }
  6344. }
  6345. } else { /* lTarget = 22 or 23 */
  6346. if (sInd < 0) {
  6347. eInd -= sInd;
  6348. Andersen, et al. Experimental [Page 158]
  6349. RFC 3951 Internet Low Bit Rate Codec December 2004
  6350. sInd = 0;
  6351. }
  6352. if(eInd > range) {
  6353. sInd -= (eInd - range);
  6354. eInd = range;
  6355. }
  6356. }
  6357. }
  6358. /* search of higher codebook section */
  6359. /* index search range */
  6360. counter = sInd;
  6361. sInd += base_size;
  6362. eInd += base_size;
  6363. if (stage==0) {
  6364. ppe = energy+base_size;
  6365. *ppe=0.0;
  6366. pp=cbvectors+lMem-lTarget;
  6367. for (j=0; j<lTarget; j++) {
  6368. *ppe+=(*pp)*(*pp++);
  6369. }
  6370. ppi = cbvectors + lMem - 1 - lTarget;
  6371. ppo = cbvectors + lMem - 1;
  6372. for (j=0; j<(range-1); j++) {
  6373. *(ppe+1) = *ppe + (*ppi)*(*ppi) - (*ppo)*(*ppo);
  6374. ppo--;
  6375. ppi--;
  6376. ppe++;
  6377. }
  6378. }
  6379. /* loop over search range */
  6380. for (icount=sInd; icount<eInd; icount++) {
  6381. /* calculate measure */
  6382. crossDot=0.0;
  6383. pp=cbvectors + lMem - (counter++) - lTarget;
  6384. for (j=0;j<lTarget;j++) {
  6385. Andersen, et al. Experimental [Page 159]
  6386. RFC 3951 Internet Low Bit Rate Codec December 2004
  6387. crossDot += target[j]*(*pp++);
  6388. }
  6389. if (energy[icount]>0.0) {
  6390. invenergy[icount] =(float)1.0/(energy[icount]+EPS);
  6391. } else {
  6392. invenergy[icount] =(float)0.0;
  6393. }
  6394. if (stage==0) {
  6395. measure=(float)-10000000.0;
  6396. if (crossDot > 0.0) {
  6397. measure = crossDot*crossDot*
  6398. invenergy[icount];
  6399. }
  6400. }
  6401. else {
  6402. measure = crossDot*crossDot*invenergy[icount];
  6403. }
  6404. /* check if measure is better */
  6405. ftmp = crossDot*invenergy[icount];
  6406. if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
  6407. best_index = icount;
  6408. max_measure = measure;
  6409. gain = ftmp;
  6410. }
  6411. }
  6412. /* Search the augmented CB inside the limited range. */
  6413. if ((lTarget==SUBL)&&(sIndAug!=0)) {
  6414. searchAugmentedCB(sIndAug, eIndAug, stage,
  6415. 2*base_size-20, target, cbvectors+lMem,
  6416. &max_measure, &best_index, &gain, energy,
  6417. invenergy);
  6418. }
  6419. /* record best index */
  6420. index[stage] = best_index;
  6421. /* gain quantization */
  6422. if (stage==0){
  6423. Andersen, et al. Experimental [Page 160]
  6424. RFC 3951 Internet Low Bit Rate Codec December 2004
  6425. if (gain<0.0){
  6426. gain = 0.0;
  6427. }
  6428. if (gain>CB_MAXGAIN) {
  6429. gain = (float)CB_MAXGAIN;
  6430. }
  6431. gain = gainquant(gain, 1.0, 32, &gain_index[stage]);
  6432. }
  6433. else {
  6434. if (stage==1) {
  6435. gain = gainquant(gain, (float)fabs(gains[stage-1]),
  6436. 16, &gain_index[stage]);
  6437. } else {
  6438. gain = gainquant(gain, (float)fabs(gains[stage-1]),
  6439. 8, &gain_index[stage]);
  6440. }
  6441. }
  6442. /* Extract the best (according to measure)
  6443. codebook vector */
  6444. if (lTarget==(STATE_LEN-iLBCenc_inst->state_short_len)) {
  6445. if (index[stage]<base_size) {
  6446. pp=buf+LPC_FILTERORDER+lMem-lTarget-index[stage];
  6447. } else {
  6448. pp=cbvectors+lMem-lTarget-
  6449. index[stage]+base_size;
  6450. }
  6451. } else {
  6452. if (index[stage]<base_size) {
  6453. if (index[stage]<(base_size-20)) {
  6454. pp=buf+LPC_FILTERORDER+lMem-
  6455. lTarget-index[stage];
  6456. } else {
  6457. createAugmentedVec(index[stage]-base_size+40,
  6458. buf+LPC_FILTERORDER+lMem,aug_vec);
  6459. pp=aug_vec;
  6460. }
  6461. } else {
  6462. int filterno, position;
  6463. filterno=index[stage]/base_size;
  6464. position=index[stage]-filterno*base_size;
  6465. Andersen, et al. Experimental [Page 161]
  6466. RFC 3951 Internet Low Bit Rate Codec December 2004
  6467. if (position<(base_size-20)) {
  6468. pp=cbvectors+filterno*lMem-lTarget-
  6469. index[stage]+filterno*base_size;
  6470. } else {
  6471. createAugmentedVec(
  6472. index[stage]-(filterno+1)*base_size+40,
  6473. cbvectors+filterno*lMem,aug_vec);
  6474. pp=aug_vec;
  6475. }
  6476. }
  6477. }
  6478. /* Subtract the best codebook vector, according
  6479. to measure, from the target vector */
  6480. for (j=0;j<lTarget;j++) {
  6481. cvec[j] += gain*(*pp);
  6482. target[j] -= gain*(*pp++);
  6483. }
  6484. /* record quantized gain */
  6485. gains[stage]=gain;
  6486. }/* end of Main Loop. for (stage=0;... */
  6487. /* Gain adjustment for energy matching */
  6488. cene=0.0;
  6489. for (i=0; i<lTarget; i++) {
  6490. cene+=cvec[i]*cvec[i];
  6491. }
  6492. j=gain_index[0];
  6493. for (i=gain_index[0]; i<32; i++) {
  6494. ftmp=cene*gain_sq5Tbl[i]*gain_sq5Tbl[i];
  6495. if ((ftmp<(tene*gains[0]*gains[0])) &&
  6496. (gain_sq5Tbl[j]<(2.0*gains[0]))) {
  6497. j=i;
  6498. }
  6499. }
  6500. gain_index[0]=j;
  6501. }
  6502. Andersen, et al. Experimental [Page 162]
  6503. RFC 3951 Internet Low Bit Rate Codec December 2004
  6504. A.35. LPCdecode.h
  6505. /******************************************************************
  6506. iLBC Speech Coder ANSI-C Source Code
  6507. LPC_decode.h
  6508. Copyright (C) The Internet Society (2004).
  6509. All Rights Reserved.
  6510. ******************************************************************/
  6511. #ifndef __iLBC_LPC_DECODE_H
  6512. #define __iLBC_LPC_DECODE_H
  6513. void LSFinterpolate2a_dec(
  6514. float *a, /* (o) lpc coefficients for a sub-frame */
  6515. float *lsf1, /* (i) first lsf coefficient vector */
  6516. float *lsf2, /* (i) second lsf coefficient vector */
  6517. float coef, /* (i) interpolation weight */
  6518. int length /* (i) length of lsf vectors */
  6519. );
  6520. void SimplelsfDEQ(
  6521. float *lsfdeq, /* (o) dequantized lsf coefficients */
  6522. int *index, /* (i) quantization index */
  6523. int lpc_n /* (i) number of LPCs */
  6524. );
  6525. void DecoderInterpolateLSF(
  6526. float *syntdenum, /* (o) synthesis filter coefficients */
  6527. float *weightdenum, /* (o) weighting denumerator
  6528. coefficients */
  6529. float *lsfdeq, /* (i) dequantized lsf coefficients */
  6530. int length, /* (i) length of lsf coefficient vector */
  6531. iLBC_Dec_Inst_t *iLBCdec_inst
  6532. /* (i) the decoder state structure */
  6533. );
  6534. #endif
  6535. Andersen, et al. Experimental [Page 163]
  6536. RFC 3951 Internet Low Bit Rate Codec December 2004
  6537. A.36. LPCdecode.c
  6538. /******************************************************************
  6539. iLBC Speech Coder ANSI-C Source Code
  6540. LPC_decode.c
  6541. Copyright (C) The Internet Society (2004).
  6542. All Rights Reserved.
  6543. ******************************************************************/
  6544. #include <math.h>
  6545. #include <string.h>
  6546. #include "helpfun.h"
  6547. #include "lsf.h"
  6548. #include "iLBC_define.h"
  6549. #include "constants.h"
  6550. /*---------------------------------------------------------------*
  6551. * interpolation of lsf coefficients for the decoder
  6552. *--------------------------------------------------------------*/
  6553. void LSFinterpolate2a_dec(
  6554. float *a, /* (o) lpc coefficients for a sub-frame */
  6555. float *lsf1, /* (i) first lsf coefficient vector */
  6556. float *lsf2, /* (i) second lsf coefficient vector */
  6557. float coef, /* (i) interpolation weight */
  6558. int length /* (i) length of lsf vectors */
  6559. ){
  6560. float lsftmp[LPC_FILTERORDER];
  6561. interpolate(lsftmp, lsf1, lsf2, coef, length);
  6562. lsf2a(a, lsftmp);
  6563. }
  6564. /*---------------------------------------------------------------*
  6565. * obtain dequantized lsf coefficients from quantization index
  6566. *--------------------------------------------------------------*/
  6567. void SimplelsfDEQ(
  6568. float *lsfdeq, /* (o) dequantized lsf coefficients */
  6569. int *index, /* (i) quantization index */
  6570. int lpc_n /* (i) number of LPCs */
  6571. ){
  6572. int i, j, pos, cb_pos;
  6573. Andersen, et al. Experimental [Page 164]
  6574. RFC 3951 Internet Low Bit Rate Codec December 2004
  6575. /* decode first LSF */
  6576. pos = 0;
  6577. cb_pos = 0;
  6578. for (i = 0; i < LSF_NSPLIT; i++) {
  6579. for (j = 0; j < dim_lsfCbTbl[i]; j++) {
  6580. lsfdeq[pos + j] = lsfCbTbl[cb_pos +
  6581. (long)(index[i])*dim_lsfCbTbl[i] + j];
  6582. }
  6583. pos += dim_lsfCbTbl[i];
  6584. cb_pos += size_lsfCbTbl[i]*dim_lsfCbTbl[i];
  6585. }
  6586. if (lpc_n>1) {
  6587. /* decode last LSF */
  6588. pos = 0;
  6589. cb_pos = 0;
  6590. for (i = 0; i < LSF_NSPLIT; i++) {
  6591. for (j = 0; j < dim_lsfCbTbl[i]; j++) {
  6592. lsfdeq[LPC_FILTERORDER + pos + j] =
  6593. lsfCbTbl[cb_pos +
  6594. (long)(index[LSF_NSPLIT + i])*
  6595. dim_lsfCbTbl[i] + j];
  6596. }
  6597. pos += dim_lsfCbTbl[i];
  6598. cb_pos += size_lsfCbTbl[i]*dim_lsfCbTbl[i];
  6599. }
  6600. }
  6601. }
  6602. /*----------------------------------------------------------------*
  6603. * obtain synthesis and weighting filters form lsf coefficients
  6604. *---------------------------------------------------------------*/
  6605. void DecoderInterpolateLSF(
  6606. float *syntdenum, /* (o) synthesis filter coefficients */
  6607. float *weightdenum, /* (o) weighting denumerator
  6608. coefficients */
  6609. float *lsfdeq, /* (i) dequantized lsf coefficients */
  6610. int length, /* (i) length of lsf coefficient vector */
  6611. iLBC_Dec_Inst_t *iLBCdec_inst
  6612. /* (i) the decoder state structure */
  6613. ){
  6614. int i, pos, lp_length;
  6615. float lp[LPC_FILTERORDER + 1], *lsfdeq2;
  6616. Andersen, et al. Experimental [Page 165]
  6617. RFC 3951 Internet Low Bit Rate Codec December 2004
  6618. lsfdeq2 = lsfdeq + length;
  6619. lp_length = length + 1;
  6620. if (iLBCdec_inst->mode==30) {
  6621. /* sub-frame 1: Interpolation between old and first */
  6622. LSFinterpolate2a_dec(lp, iLBCdec_inst->lsfdeqold, lsfdeq,
  6623. lsf_weightTbl_30ms[0], length);
  6624. memcpy(syntdenum,lp,lp_length*sizeof(float));
  6625. bwexpand(weightdenum, lp, LPC_CHIRP_WEIGHTDENUM,
  6626. lp_length);
  6627. /* sub-frames 2 to 6: interpolation between first
  6628. and last LSF */
  6629. pos = lp_length;
  6630. for (i = 1; i < 6; i++) {
  6631. LSFinterpolate2a_dec(lp, lsfdeq, lsfdeq2,
  6632. lsf_weightTbl_30ms[i], length);
  6633. memcpy(syntdenum + pos,lp,lp_length*sizeof(float));
  6634. bwexpand(weightdenum + pos, lp,
  6635. LPC_CHIRP_WEIGHTDENUM, lp_length);
  6636. pos += lp_length;
  6637. }
  6638. }
  6639. else {
  6640. pos = 0;
  6641. for (i = 0; i < iLBCdec_inst->nsub; i++) {
  6642. LSFinterpolate2a_dec(lp, iLBCdec_inst->lsfdeqold,
  6643. lsfdeq, lsf_weightTbl_20ms[i], length);
  6644. memcpy(syntdenum+pos,lp,lp_length*sizeof(float));
  6645. bwexpand(weightdenum+pos, lp, LPC_CHIRP_WEIGHTDENUM,
  6646. lp_length);
  6647. pos += lp_length;
  6648. }
  6649. }
  6650. /* update memory */
  6651. if (iLBCdec_inst->mode==30)
  6652. memcpy(iLBCdec_inst->lsfdeqold, lsfdeq2,
  6653. length*sizeof(float));
  6654. else
  6655. memcpy(iLBCdec_inst->lsfdeqold, lsfdeq,
  6656. length*sizeof(float));
  6657. }
  6658. Andersen, et al. Experimental [Page 166]
  6659. RFC 3951 Internet Low Bit Rate Codec December 2004
  6660. A.37. LPCencode.h
  6661. /******************************************************************
  6662. iLBC Speech Coder ANSI-C Source Code
  6663. LPCencode.h
  6664. Copyright (C) The Internet Society (2004).
  6665. All Rights Reserved.
  6666. ******************************************************************/
  6667. #ifndef __iLBC_LPCENCOD_H
  6668. #define __iLBC_LPCENCOD_H
  6669. void LPCencode(
  6670. float *syntdenum, /* (i/o) synthesis filter coefficients
  6671. before/after encoding */
  6672. float *weightdenum, /* (i/o) weighting denumerator coefficients
  6673. before/after encoding */
  6674. int *lsf_index, /* (o) lsf quantization index */
  6675. float *data, /* (i) lsf coefficients to quantize */
  6676. iLBC_Enc_Inst_t *iLBCenc_inst
  6677. /* (i/o) the encoder state structure */
  6678. );
  6679. #endif
  6680. A.38. LPCencode.c
  6681. /******************************************************************
  6682. iLBC Speech Coder ANSI-C Source Code
  6683. LPCencode.c
  6684. Copyright (C) The Internet Society (2004).
  6685. All Rights Reserved.
  6686. ******************************************************************/
  6687. #include <string.h>
  6688. #include "iLBC_define.h"
  6689. #include "helpfun.h"
  6690. #include "lsf.h"
  6691. #include "constants.h"
  6692. Andersen, et al. Experimental [Page 167]
  6693. RFC 3951 Internet Low Bit Rate Codec December 2004
  6694. /*----------------------------------------------------------------*
  6695. * lpc analysis (subrutine to LPCencode)
  6696. *---------------------------------------------------------------*/
  6697. void SimpleAnalysis(
  6698. float *lsf, /* (o) lsf coefficients */
  6699. float *data, /* (i) new data vector */
  6700. iLBC_Enc_Inst_t *iLBCenc_inst
  6701. /* (i/o) the encoder state structure */
  6702. ){
  6703. int k, is;
  6704. float temp[BLOCKL_MAX], lp[LPC_FILTERORDER + 1];
  6705. float lp2[LPC_FILTERORDER + 1];
  6706. float r[LPC_FILTERORDER + 1];
  6707. is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
  6708. memcpy(iLBCenc_inst->lpc_buffer+is,data,
  6709. iLBCenc_inst->blockl*sizeof(float));
  6710. /* No lookahead, last window is asymmetric */
  6711. for (k = 0; k < iLBCenc_inst->lpc_n; k++) {
  6712. is = LPC_LOOKBACK;
  6713. if (k < (iLBCenc_inst->lpc_n - 1)) {
  6714. window(temp, lpc_winTbl,
  6715. iLBCenc_inst->lpc_buffer, BLOCKL_MAX);
  6716. } else {
  6717. window(temp, lpc_asymwinTbl,
  6718. iLBCenc_inst->lpc_buffer + is, BLOCKL_MAX);
  6719. }
  6720. autocorr(r, temp, BLOCKL_MAX, LPC_FILTERORDER);
  6721. window(r, r, lpc_lagwinTbl, LPC_FILTERORDER + 1);
  6722. levdurb(lp, temp, r, LPC_FILTERORDER);
  6723. bwexpand(lp2, lp, LPC_CHIRP_SYNTDENUM, LPC_FILTERORDER+1);
  6724. a2lsf(lsf + k*LPC_FILTERORDER, lp2);
  6725. }
  6726. is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
  6727. memmove(iLBCenc_inst->lpc_buffer,
  6728. iLBCenc_inst->lpc_buffer+LPC_LOOKBACK+BLOCKL_MAX-is,
  6729. is*sizeof(float));
  6730. }
  6731. /*----------------------------------------------------------------*
  6732. Andersen, et al. Experimental [Page 168]
  6733. RFC 3951 Internet Low Bit Rate Codec December 2004
  6734. * lsf interpolator and conversion from lsf to a coefficients
  6735. * (subrutine to SimpleInterpolateLSF)
  6736. *---------------------------------------------------------------*/
  6737. void LSFinterpolate2a_enc(
  6738. float *a, /* (o) lpc coefficients */
  6739. float *lsf1,/* (i) first set of lsf coefficients */
  6740. float *lsf2,/* (i) second set of lsf coefficients */
  6741. float coef, /* (i) weighting coefficient to use between
  6742. lsf1 and lsf2 */
  6743. long length /* (i) length of coefficient vectors */
  6744. ){
  6745. float lsftmp[LPC_FILTERORDER];
  6746. interpolate(lsftmp, lsf1, lsf2, coef, length);
  6747. lsf2a(a, lsftmp);
  6748. }
  6749. /*----------------------------------------------------------------*
  6750. * lsf interpolator (subrutine to LPCencode)
  6751. *---------------------------------------------------------------*/
  6752. void SimpleInterpolateLSF(
  6753. float *syntdenum, /* (o) the synthesis filter denominator
  6754. resulting from the quantized
  6755. interpolated lsf */
  6756. float *weightdenum, /* (o) the weighting filter denominator
  6757. resulting from the unquantized
  6758. interpolated lsf */
  6759. float *lsf, /* (i) the unquantized lsf coefficients */
  6760. float *lsfdeq, /* (i) the dequantized lsf coefficients */
  6761. float *lsfold, /* (i) the unquantized lsf coefficients of
  6762. the previous signal frame */
  6763. float *lsfdeqold, /* (i) the dequantized lsf coefficients of
  6764. the previous signal frame */
  6765. int length, /* (i) should equate LPC_FILTERORDER */
  6766. iLBC_Enc_Inst_t *iLBCenc_inst
  6767. /* (i/o) the encoder state structure */
  6768. ){
  6769. int i, pos, lp_length;
  6770. float lp[LPC_FILTERORDER + 1], *lsf2, *lsfdeq2;
  6771. lsf2 = lsf + length;
  6772. lsfdeq2 = lsfdeq + length;
  6773. lp_length = length + 1;
  6774. if (iLBCenc_inst->mode==30) {
  6775. /* sub-frame 1: Interpolation between old and first
  6776. Andersen, et al. Experimental [Page 169]
  6777. RFC 3951 Internet Low Bit Rate Codec December 2004
  6778. set of lsf coefficients */
  6779. LSFinterpolate2a_enc(lp, lsfdeqold, lsfdeq,
  6780. lsf_weightTbl_30ms[0], length);
  6781. memcpy(syntdenum,lp,lp_length*sizeof(float));
  6782. LSFinterpolate2a_enc(lp, lsfold, lsf,
  6783. lsf_weightTbl_30ms[0], length);
  6784. bwexpand(weightdenum, lp, LPC_CHIRP_WEIGHTDENUM, lp_length);
  6785. /* sub-frame 2 to 6: Interpolation between first
  6786. and second set of lsf coefficients */
  6787. pos = lp_length;
  6788. for (i = 1; i < iLBCenc_inst->nsub; i++) {
  6789. LSFinterpolate2a_enc(lp, lsfdeq, lsfdeq2,
  6790. lsf_weightTbl_30ms[i], length);
  6791. memcpy(syntdenum + pos,lp,lp_length*sizeof(float));
  6792. LSFinterpolate2a_enc(lp, lsf, lsf2,
  6793. lsf_weightTbl_30ms[i], length);
  6794. bwexpand(weightdenum + pos, lp,
  6795. LPC_CHIRP_WEIGHTDENUM, lp_length);
  6796. pos += lp_length;
  6797. }
  6798. }
  6799. else {
  6800. pos = 0;
  6801. for (i = 0; i < iLBCenc_inst->nsub; i++) {
  6802. LSFinterpolate2a_enc(lp, lsfdeqold, lsfdeq,
  6803. lsf_weightTbl_20ms[i], length);
  6804. memcpy(syntdenum+pos,lp,lp_length*sizeof(float));
  6805. LSFinterpolate2a_enc(lp, lsfold, lsf,
  6806. lsf_weightTbl_20ms[i], length);
  6807. bwexpand(weightdenum+pos, lp,
  6808. LPC_CHIRP_WEIGHTDENUM, lp_length);
  6809. pos += lp_length;
  6810. }
  6811. }
  6812. /* update memory */
  6813. if (iLBCenc_inst->mode==30) {
  6814. memcpy(lsfold, lsf2, length*sizeof(float));
  6815. memcpy(lsfdeqold, lsfdeq2, length*sizeof(float));
  6816. }
  6817. else {
  6818. memcpy(lsfold, lsf, length*sizeof(float));
  6819. memcpy(lsfdeqold, lsfdeq, length*sizeof(float));
  6820. Andersen, et al. Experimental [Page 170]
  6821. RFC 3951 Internet Low Bit Rate Codec December 2004
  6822. }
  6823. }
  6824. /*----------------------------------------------------------------*
  6825. * lsf quantizer (subrutine to LPCencode)
  6826. *---------------------------------------------------------------*/
  6827. void SimplelsfQ(
  6828. float *lsfdeq, /* (o) dequantized lsf coefficients
  6829. (dimension FILTERORDER) */
  6830. int *index, /* (o) quantization index */
  6831. float *lsf, /* (i) the lsf coefficient vector to be
  6832. quantized (dimension FILTERORDER ) */
  6833. int lpc_n /* (i) number of lsf sets to quantize */
  6834. ){
  6835. /* Quantize first LSF with memoryless split VQ */
  6836. SplitVQ(lsfdeq, index, lsf, lsfCbTbl, LSF_NSPLIT,
  6837. dim_lsfCbTbl, size_lsfCbTbl);
  6838. if (lpc_n==2) {
  6839. /* Quantize second LSF with memoryless split VQ */
  6840. SplitVQ(lsfdeq + LPC_FILTERORDER, index + LSF_NSPLIT,
  6841. lsf + LPC_FILTERORDER, lsfCbTbl, LSF_NSPLIT,
  6842. dim_lsfCbTbl, size_lsfCbTbl);
  6843. }
  6844. }
  6845. /*----------------------------------------------------------------*
  6846. * lpc encoder
  6847. *---------------------------------------------------------------*/
  6848. void LPCencode(
  6849. float *syntdenum, /* (i/o) synthesis filter coefficients
  6850. before/after encoding */
  6851. float *weightdenum, /* (i/o) weighting denumerator
  6852. coefficients before/after
  6853. encoding */
  6854. int *lsf_index, /* (o) lsf quantization index */
  6855. float *data, /* (i) lsf coefficients to quantize */
  6856. iLBC_Enc_Inst_t *iLBCenc_inst
  6857. /* (i/o) the encoder state structure */
  6858. ){
  6859. float lsf[LPC_FILTERORDER * LPC_N_MAX];
  6860. float lsfdeq[LPC_FILTERORDER * LPC_N_MAX];
  6861. int change=0;
  6862. SimpleAnalysis(lsf, data, iLBCenc_inst);
  6863. SimplelsfQ(lsfdeq, lsf_index, lsf, iLBCenc_inst->lpc_n);
  6864. Andersen, et al. Experimental [Page 171]
  6865. RFC 3951 Internet Low Bit Rate Codec December 2004
  6866. change=LSF_check(lsfdeq, LPC_FILTERORDER, iLBCenc_inst->lpc_n);
  6867. SimpleInterpolateLSF(syntdenum, weightdenum,
  6868. lsf, lsfdeq, iLBCenc_inst->lsfold,
  6869. iLBCenc_inst->lsfdeqold, LPC_FILTERORDER, iLBCenc_inst);
  6870. }
  6871. A.39. lsf.h
  6872. /******************************************************************
  6873. iLBC Speech Coder ANSI-C Source Code
  6874. lsf.h
  6875. Copyright (C) The Internet Society (2004).
  6876. All Rights Reserved.
  6877. ******************************************************************/
  6878. #ifndef __iLBC_LSF_H
  6879. #define __iLBC_LSF_H
  6880. void a2lsf(
  6881. float *freq,/* (o) lsf coefficients */
  6882. float *a /* (i) lpc coefficients */
  6883. );
  6884. void lsf2a(
  6885. float *a_coef, /* (o) lpc coefficients */
  6886. float *freq /* (i) lsf coefficients */
  6887. );
  6888. #endif
  6889. A.40. lsf.c
  6890. /******************************************************************
  6891. iLBC Speech Coder ANSI-C Source Code
  6892. lsf.c
  6893. Copyright (C) The Internet Society (2004).
  6894. All Rights Reserved.
  6895. ******************************************************************/
  6896. #include <string.h>
  6897. Andersen, et al. Experimental [Page 172]
  6898. RFC 3951 Internet Low Bit Rate Codec December 2004
  6899. #include <math.h>
  6900. #include "iLBC_define.h"
  6901. /*----------------------------------------------------------------*
  6902. * conversion from lpc coefficients to lsf coefficients
  6903. *---------------------------------------------------------------*/
  6904. void a2lsf(
  6905. float *freq,/* (o) lsf coefficients */
  6906. float *a /* (i) lpc coefficients */
  6907. ){
  6908. float steps[LSF_NUMBER_OF_STEPS] =
  6909. {(float)0.00635, (float)0.003175, (float)0.0015875,
  6910. (float)0.00079375};
  6911. float step;
  6912. int step_idx;
  6913. int lsp_index;
  6914. float p[LPC_HALFORDER];
  6915. float q[LPC_HALFORDER];
  6916. float p_pre[LPC_HALFORDER];
  6917. float q_pre[LPC_HALFORDER];
  6918. float old_p, old_q, *old;
  6919. float *pq_coef;
  6920. float omega, old_omega;
  6921. int i;
  6922. float hlp, hlp1, hlp2, hlp3, hlp4, hlp5;
  6923. for (i=0; i<LPC_HALFORDER; i++) {
  6924. p[i] = (float)-1.0 * (a[i + 1] + a[LPC_FILTERORDER - i]);
  6925. q[i] = a[LPC_FILTERORDER - i] - a[i + 1];
  6926. }
  6927. p_pre[0] = (float)-1.0 - p[0];
  6928. p_pre[1] = - p_pre[0] - p[1];
  6929. p_pre[2] = - p_pre[1] - p[2];
  6930. p_pre[3] = - p_pre[2] - p[3];
  6931. p_pre[4] = - p_pre[3] - p[4];
  6932. p_pre[4] = p_pre[4] / 2;
  6933. q_pre[0] = (float)1.0 - q[0];
  6934. q_pre[1] = q_pre[0] - q[1];
  6935. q_pre[2] = q_pre[1] - q[2];
  6936. q_pre[3] = q_pre[2] - q[3];
  6937. q_pre[4] = q_pre[3] - q[4];
  6938. q_pre[4] = q_pre[4] / 2;
  6939. omega = 0.0;
  6940. Andersen, et al. Experimental [Page 173]
  6941. RFC 3951 Internet Low Bit Rate Codec December 2004
  6942. old_omega = 0.0;
  6943. old_p = FLOAT_MAX;
  6944. old_q = FLOAT_MAX;
  6945. /* Here we loop through lsp_index to find all the
  6946. LPC_FILTERORDER roots for omega. */
  6947. for (lsp_index = 0; lsp_index<LPC_FILTERORDER; lsp_index++) {
  6948. /* Depending on lsp_index being even or odd, we
  6949. alternatively solve the roots for the two LSP equations. */
  6950. if ((lsp_index & 0x1) == 0) {
  6951. pq_coef = p_pre;
  6952. old = &old_p;
  6953. } else {
  6954. pq_coef = q_pre;
  6955. old = &old_q;
  6956. }
  6957. /* Start with low resolution grid */
  6958. for (step_idx = 0, step = steps[step_idx];
  6959. step_idx < LSF_NUMBER_OF_STEPS;){
  6960. /* cos(10piw) + pq(0)cos(8piw) + pq(1)cos(6piw) +
  6961. pq(2)cos(4piw) + pq(3)cod(2piw) + pq(4) */
  6962. hlp = (float)cos(omega * TWO_PI);
  6963. hlp1 = (float)2.0 * hlp + pq_coef[0];
  6964. hlp2 = (float)2.0 * hlp * hlp1 - (float)1.0 +
  6965. pq_coef[1];
  6966. hlp3 = (float)2.0 * hlp * hlp2 - hlp1 + pq_coef[2];
  6967. hlp4 = (float)2.0 * hlp * hlp3 - hlp2 + pq_coef[3];
  6968. hlp5 = hlp * hlp4 - hlp3 + pq_coef[4];
  6969. if (((hlp5 * (*old)) <= 0.0) || (omega >= 0.5)){
  6970. if (step_idx == (LSF_NUMBER_OF_STEPS - 1)){
  6971. if (fabs(hlp5) >= fabs(*old)) {
  6972. freq[lsp_index] = omega - step;
  6973. } else {
  6974. freq[lsp_index] = omega;
  6975. }
  6976. Andersen, et al. Experimental [Page 174]
  6977. RFC 3951 Internet Low Bit Rate Codec December 2004
  6978. if ((*old) >= 0.0){
  6979. *old = (float)-1.0 * FLOAT_MAX;
  6980. } else {
  6981. *old = FLOAT_MAX;
  6982. }
  6983. omega = old_omega;
  6984. step_idx = 0;
  6985. step_idx = LSF_NUMBER_OF_STEPS;
  6986. } else {
  6987. if (step_idx == 0) {
  6988. old_omega = omega;
  6989. }
  6990. step_idx++;
  6991. omega -= steps[step_idx];
  6992. /* Go back one grid step */
  6993. step = steps[step_idx];
  6994. }
  6995. } else {
  6996. /* increment omega until they are of different sign,
  6997. and we know there is at least one root between omega
  6998. and old_omega */
  6999. *old = hlp5;
  7000. omega += step;
  7001. }
  7002. }
  7003. }
  7004. for (i = 0; i<LPC_FILTERORDER; i++) {
  7005. freq[i] = freq[i] * TWO_PI;
  7006. }
  7007. }
  7008. /*----------------------------------------------------------------*
  7009. * conversion from lsf coefficients to lpc coefficients
  7010. *---------------------------------------------------------------*/
  7011. void lsf2a(
  7012. float *a_coef, /* (o) lpc coefficients */
  7013. float *freq /* (i) lsf coefficients */
  7014. Andersen, et al. Experimental [Page 175]
  7015. RFC 3951 Internet Low Bit Rate Codec December 2004
  7016. ){
  7017. int i, j;
  7018. float hlp;
  7019. float p[LPC_HALFORDER], q[LPC_HALFORDER];
  7020. float a[LPC_HALFORDER + 1], a1[LPC_HALFORDER],
  7021. a2[LPC_HALFORDER];
  7022. float b[LPC_HALFORDER + 1], b1[LPC_HALFORDER],
  7023. b2[LPC_HALFORDER];
  7024. for (i=0; i<LPC_FILTERORDER; i++) {
  7025. freq[i] = freq[i] * PI2;
  7026. }
  7027. /* Check input for ill-conditioned cases. This part is not
  7028. found in the TIA standard. It involves the following 2 IF
  7029. blocks. If "freq" is judged ill-conditioned, then we first
  7030. modify freq[0] and freq[LPC_HALFORDER-1] (normally
  7031. LPC_HALFORDER = 10 for LPC applications), then we adjust
  7032. the other "freq" values slightly */
  7033. if ((freq[0] <= 0.0) || (freq[LPC_FILTERORDER - 1] >= 0.5)){
  7034. if (freq[0] <= 0.0) {
  7035. freq[0] = (float)0.022;
  7036. }
  7037. if (freq[LPC_FILTERORDER - 1] >= 0.5) {
  7038. freq[LPC_FILTERORDER - 1] = (float)0.499;
  7039. }
  7040. hlp = (freq[LPC_FILTERORDER - 1] - freq[0]) /
  7041. (float) (LPC_FILTERORDER - 1);
  7042. for (i=1; i<LPC_FILTERORDER; i++) {
  7043. freq[i] = freq[i - 1] + hlp;
  7044. }
  7045. }
  7046. memset(a1, 0, LPC_HALFORDER*sizeof(float));
  7047. memset(a2, 0, LPC_HALFORDER*sizeof(float));
  7048. memset(b1, 0, LPC_HALFORDER*sizeof(float));
  7049. memset(b2, 0, LPC_HALFORDER*sizeof(float));
  7050. memset(a, 0, (LPC_HALFORDER+1)*sizeof(float));
  7051. memset(b, 0, (LPC_HALFORDER+1)*sizeof(float));
  7052. Andersen, et al. Experimental [Page 176]
  7053. RFC 3951 Internet Low Bit Rate Codec December 2004
  7054. /* p[i] and q[i] compute cos(2*pi*omega_{2j}) and
  7055. cos(2*pi*omega_{2j-1} in eqs. 4.2.2.2-1 and 4.2.2.2-2.
  7056. Note that for this code p[i] specifies the coefficients
  7057. used in .Q_A(z) while q[i] specifies the coefficients used
  7058. in .P_A(z) */
  7059. for (i=0; i<LPC_HALFORDER; i++) {
  7060. p[i] = (float)cos(TWO_PI * freq[2 * i]);
  7061. q[i] = (float)cos(TWO_PI * freq[2 * i + 1]);
  7062. }
  7063. a[0] = 0.25;
  7064. b[0] = 0.25;
  7065. for (i= 0; i<LPC_HALFORDER; i++) {
  7066. a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i];
  7067. b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i];
  7068. a2[i] = a1[i];
  7069. a1[i] = a[i];
  7070. b2[i] = b1[i];
  7071. b1[i] = b[i];
  7072. }
  7073. for (j=0; j<LPC_FILTERORDER; j++) {
  7074. if (j == 0) {
  7075. a[0] = 0.25;
  7076. b[0] = -0.25;
  7077. } else {
  7078. a[0] = b[0] = 0.0;
  7079. }
  7080. for (i=0; i<LPC_HALFORDER; i++) {
  7081. a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i];
  7082. b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i];
  7083. a2[i] = a1[i];
  7084. a1[i] = a[i];
  7085. b2[i] = b1[i];
  7086. b1[i] = b[i];
  7087. }
  7088. a_coef[j + 1] = 2 * (a[LPC_HALFORDER] + b[LPC_HALFORDER]);
  7089. }
  7090. a_coef[0] = 1.0;
  7091. }
  7092. Andersen, et al. Experimental [Page 177]
  7093. RFC 3951 Internet Low Bit Rate Codec December 2004
  7094. A.41. packing.h
  7095. /******************************************************************
  7096. iLBC Speech Coder ANSI-C Source Code
  7097. packing.h
  7098. Copyright (C) The Internet Society (2004).
  7099. All Rights Reserved.
  7100. ******************************************************************/
  7101. #ifndef __PACKING_H
  7102. #define __PACKING_H
  7103. void packsplit(
  7104. int *index, /* (i) the value to split */
  7105. int *firstpart, /* (o) the value specified by most
  7106. significant bits */
  7107. int *rest, /* (o) the value specified by least
  7108. significant bits */
  7109. int bitno_firstpart, /* (i) number of bits in most
  7110. significant part */
  7111. int bitno_total /* (i) number of bits in full range
  7112. of value */
  7113. );
  7114. void packcombine(
  7115. int *index, /* (i/o) the msb value in the
  7116. combined value out */
  7117. int rest, /* (i) the lsb value */
  7118. int bitno_rest /* (i) the number of bits in the
  7119. lsb part */
  7120. );
  7121. void dopack(
  7122. unsigned char **bitstream, /* (i/o) on entrance pointer to
  7123. place in bitstream to pack
  7124. new data, on exit pointer
  7125. to place in bitstream to
  7126. pack future data */
  7127. int index, /* (i) the value to pack */
  7128. int bitno, /* (i) the number of bits that the
  7129. value will fit within */
  7130. int *pos /* (i/o) write position in the
  7131. current byte */
  7132. );
  7133. Andersen, et al. Experimental [Page 178]
  7134. RFC 3951 Internet Low Bit Rate Codec December 2004
  7135. void unpack(
  7136. unsigned char **bitstream, /* (i/o) on entrance pointer to
  7137. place in bitstream to
  7138. unpack new data from, on
  7139. exit pointer to place in
  7140. bitstream to unpack future
  7141. data from */
  7142. int *index, /* (o) resulting value */
  7143. int bitno, /* (i) number of bits used to
  7144. represent the value */
  7145. int *pos /* (i/o) read position in the
  7146. current byte */
  7147. );
  7148. #endif
  7149. A.42. packing.c
  7150. /******************************************************************
  7151. iLBC Speech Coder ANSI-C Source Code
  7152. packing.c
  7153. Copyright (C) The Internet Society (2004).
  7154. All Rights Reserved.
  7155. ******************************************************************/
  7156. #include <math.h>
  7157. #include <stdlib.h>
  7158. #include "iLBC_define.h"
  7159. #include "constants.h"
  7160. #include "helpfun.h"
  7161. #include "string.h"
  7162. /*----------------------------------------------------------------*
  7163. * splitting an integer into first most significant bits and
  7164. * remaining least significant bits
  7165. *---------------------------------------------------------------*/
  7166. void packsplit(
  7167. int *index, /* (i) the value to split */
  7168. int *firstpart, /* (o) the value specified by most
  7169. significant bits */
  7170. int *rest, /* (o) the value specified by least
  7171. significant bits */
  7172. Andersen, et al. Experimental [Page 179]
  7173. RFC 3951 Internet Low Bit Rate Codec December 2004
  7174. int bitno_firstpart, /* (i) number of bits in most
  7175. significant part */
  7176. int bitno_total /* (i) number of bits in full range
  7177. of value */
  7178. ){
  7179. int bitno_rest = bitno_total-bitno_firstpart;
  7180. *firstpart = *index>>(bitno_rest);
  7181. *rest = *index-(*firstpart<<(bitno_rest));
  7182. }
  7183. /*----------------------------------------------------------------*
  7184. * combining a value corresponding to msb's with a value
  7185. * corresponding to lsb's
  7186. *---------------------------------------------------------------*/
  7187. void packcombine(
  7188. int *index, /* (i/o) the msb value in the
  7189. combined value out */
  7190. int rest, /* (i) the lsb value */
  7191. int bitno_rest /* (i) the number of bits in the
  7192. lsb part */
  7193. ){
  7194. *index = *index<<bitno_rest;
  7195. *index += rest;
  7196. }
  7197. /*----------------------------------------------------------------*
  7198. * packing of bits into bitstream, i.e., vector of bytes
  7199. *---------------------------------------------------------------*/
  7200. void dopack(
  7201. unsigned char **bitstream, /* (i/o) on entrance pointer to
  7202. place in bitstream to pack
  7203. new data, on exit pointer
  7204. to place in bitstream to
  7205. pack future data */
  7206. int index, /* (i) the value to pack */
  7207. int bitno, /* (i) the number of bits that the
  7208. value will fit within */
  7209. int *pos /* (i/o) write position in the
  7210. current byte */
  7211. ){
  7212. int posLeft;
  7213. /* Clear the bits before starting in a new byte */
  7214. if ((*pos)==0) {
  7215. Andersen, et al. Experimental [Page 180]
  7216. RFC 3951 Internet Low Bit Rate Codec December 2004
  7217. **bitstream=0;
  7218. }
  7219. while (bitno>0) {
  7220. /* Jump to the next byte if end of this byte is reached*/
  7221. if (*pos==8) {
  7222. *pos=0;
  7223. (*bitstream)++;
  7224. **bitstream=0;
  7225. }
  7226. posLeft=8-(*pos);
  7227. /* Insert index into the bitstream */
  7228. if (bitno <= posLeft) {
  7229. **bitstream |= (unsigned char)(index<<(posLeft-bitno));
  7230. *pos+=bitno;
  7231. bitno=0;
  7232. } else {
  7233. **bitstream |= (unsigned char)(index>>(bitno-posLeft));
  7234. *pos=8;
  7235. index-=((index>>(bitno-posLeft))<<(bitno-posLeft));
  7236. bitno-=posLeft;
  7237. }
  7238. }
  7239. }
  7240. /*----------------------------------------------------------------*
  7241. * unpacking of bits from bitstream, i.e., vector of bytes
  7242. *---------------------------------------------------------------*/
  7243. void unpack(
  7244. unsigned char **bitstream, /* (i/o) on entrance pointer to
  7245. place in bitstream to
  7246. unpack new data from, on
  7247. exit pointer to place in
  7248. bitstream to unpack future
  7249. data from */
  7250. int *index, /* (o) resulting value */
  7251. int bitno, /* (i) number of bits used to
  7252. represent the value */
  7253. int *pos /* (i/o) read position in the
  7254. current byte */
  7255. Andersen, et al. Experimental [Page 181]
  7256. RFC 3951 Internet Low Bit Rate Codec December 2004
  7257. ){
  7258. int BitsLeft;
  7259. *index=0;
  7260. while (bitno>0) {
  7261. /* move forward in bitstream when the end of the
  7262. byte is reached */
  7263. if (*pos==8) {
  7264. *pos=0;
  7265. (*bitstream)++;
  7266. }
  7267. BitsLeft=8-(*pos);
  7268. /* Extract bits to index */
  7269. if (BitsLeft>=bitno) {
  7270. *index+=((((**bitstream)<<(*pos)) & 0xFF)>>(8-bitno));
  7271. *pos+=bitno;
  7272. bitno=0;
  7273. } else {
  7274. if ((8-bitno)>0) {
  7275. *index+=((((**bitstream)<<(*pos)) & 0xFF)>>
  7276. (8-bitno));
  7277. *pos=8;
  7278. } else {
  7279. *index+=(((int)(((**bitstream)<<(*pos)) & 0xFF))<<
  7280. (bitno-8));
  7281. *pos=8;
  7282. }
  7283. bitno-=BitsLeft;
  7284. }
  7285. }
  7286. }
  7287. A.43. StateConstructW.h
  7288. /******************************************************************
  7289. iLBC Speech Coder ANSI-C Source Code
  7290. StateConstructW.h
  7291. Andersen, et al. Experimental [Page 182]
  7292. RFC 3951 Internet Low Bit Rate Codec December 2004
  7293. Copyright (C) The Internet Society (2004).
  7294. All Rights Reserved.
  7295. ******************************************************************/
  7296. #ifndef __iLBC_STATECONSTRUCTW_H
  7297. #define __iLBC_STATECONSTRUCTW_H
  7298. void StateConstructW(
  7299. int idxForMax, /* (i) 6-bit index for the quantization of
  7300. max amplitude */
  7301. int *idxVec, /* (i) vector of quantization indexes */
  7302. float *syntDenum, /* (i) synthesis filter denumerator */
  7303. float *out, /* (o) the decoded state vector */
  7304. int len /* (i) length of a state vector */
  7305. );
  7306. #endif
  7307. A.44. StateConstructW.c
  7308. /******************************************************************
  7309. iLBC Speech Coder ANSI-C Source Code
  7310. StateConstructW.c
  7311. Copyright (C) The Internet Society (2004).
  7312. All Rights Reserved.
  7313. ******************************************************************/
  7314. #include <math.h>
  7315. #include <string.h>
  7316. #include "iLBC_define.h"
  7317. #include "constants.h"
  7318. #include "filter.h"
  7319. /*----------------------------------------------------------------*
  7320. * decoding of the start state
  7321. *---------------------------------------------------------------*/
  7322. void StateConstructW(
  7323. int idxForMax, /* (i) 6-bit index for the quantization of
  7324. max amplitude */
  7325. int *idxVec, /* (i) vector of quantization indexes */
  7326. float *syntDenum, /* (i) synthesis filter denumerator */
  7327. Andersen, et al. Experimental [Page 183]
  7328. RFC 3951 Internet Low Bit Rate Codec December 2004
  7329. float *out, /* (o) the decoded state vector */
  7330. int len /* (i) length of a state vector */
  7331. ){
  7332. float maxVal, tmpbuf[LPC_FILTERORDER+2*STATE_LEN], *tmp,
  7333. numerator[LPC_FILTERORDER+1];
  7334. float foutbuf[LPC_FILTERORDER+2*STATE_LEN], *fout;
  7335. int k,tmpi;
  7336. /* decoding of the maximum value */
  7337. maxVal = state_frgqTbl[idxForMax];
  7338. maxVal = (float)pow(10,maxVal)/(float)4.5;
  7339. /* initialization of buffers and coefficients */
  7340. memset(tmpbuf, 0, LPC_FILTERORDER*sizeof(float));
  7341. memset(foutbuf, 0, LPC_FILTERORDER*sizeof(float));
  7342. for (k=0; k<LPC_FILTERORDER; k++) {
  7343. numerator[k]=syntDenum[LPC_FILTERORDER-k];
  7344. }
  7345. numerator[LPC_FILTERORDER]=syntDenum[0];
  7346. tmp = &tmpbuf[LPC_FILTERORDER];
  7347. fout = &foutbuf[LPC_FILTERORDER];
  7348. /* decoding of the sample values */
  7349. for (k=0; k<len; k++) {
  7350. tmpi = len-1-k;
  7351. /* maxVal = 1/scal */
  7352. tmp[k] = maxVal*state_sq3Tbl[idxVec[tmpi]];
  7353. }
  7354. /* circular convolution with all-pass filter */
  7355. memset(tmp+len, 0, len*sizeof(float));
  7356. ZeroPoleFilter(tmp, numerator, syntDenum, 2*len,
  7357. LPC_FILTERORDER, fout);
  7358. for (k=0;k<len;k++) {
  7359. out[k] = fout[len-1-k]+fout[2*len-1-k];
  7360. }
  7361. }
  7362. Andersen, et al. Experimental [Page 184]
  7363. RFC 3951 Internet Low Bit Rate Codec December 2004
  7364. A.45. StateSearchW.h
  7365. /******************************************************************
  7366. iLBC Speech Coder ANSI-C Source Code
  7367. StateSearchW.h
  7368. Copyright (C) The Internet Society (2004).
  7369. All Rights Reserved.
  7370. ******************************************************************/
  7371. #ifndef __iLBC_STATESEARCHW_H
  7372. #define __iLBC_STATESEARCHW_H
  7373. void AbsQuantW(
  7374. iLBC_Enc_Inst_t *iLBCenc_inst,
  7375. /* (i) Encoder instance */
  7376. float *in, /* (i) vector to encode */
  7377. float *syntDenum, /* (i) denominator of synthesis filter */
  7378. float *weightDenum, /* (i) denominator of weighting filter */
  7379. int *out, /* (o) vector of quantizer indexes */
  7380. int len, /* (i) length of vector to encode and
  7381. vector of quantizer indexes */
  7382. int state_first /* (i) position of start state in the
  7383. 80 vec */
  7384. );
  7385. void StateSearchW(
  7386. iLBC_Enc_Inst_t *iLBCenc_inst,
  7387. /* (i) Encoder instance */
  7388. float *residual,/* (i) target residual vector */
  7389. float *syntDenum, /* (i) lpc synthesis filter */
  7390. float *weightDenum, /* (i) weighting filter denuminator */
  7391. int *idxForMax, /* (o) quantizer index for maximum
  7392. amplitude */
  7393. int *idxVec, /* (o) vector of quantization indexes */
  7394. int len, /* (i) length of all vectors */
  7395. int state_first /* (i) position of start state in the
  7396. 80 vec */
  7397. );
  7398. #endif
  7399. Andersen, et al. Experimental [Page 185]
  7400. RFC 3951 Internet Low Bit Rate Codec December 2004
  7401. A.46. StateSearchW.c
  7402. /******************************************************************
  7403. iLBC Speech Coder ANSI-C Source Code
  7404. StateSearchW.c
  7405. Copyright (C) The Internet Society (2004).
  7406. All Rights Reserved.
  7407. ******************************************************************/
  7408. #include <math.h>
  7409. #include <string.h>
  7410. #include "iLBC_define.h"
  7411. #include "constants.h"
  7412. #include "filter.h"
  7413. #include "helpfun.h"
  7414. /*----------------------------------------------------------------*
  7415. * predictive noise shaping encoding of scaled start state
  7416. * (subrutine for StateSearchW)
  7417. *---------------------------------------------------------------*/
  7418. void AbsQuantW(
  7419. iLBC_Enc_Inst_t *iLBCenc_inst,
  7420. /* (i) Encoder instance */
  7421. float *in, /* (i) vector to encode */
  7422. float *syntDenum, /* (i) denominator of synthesis filter */
  7423. float *weightDenum, /* (i) denominator of weighting filter */
  7424. int *out, /* (o) vector of quantizer indexes */
  7425. int len, /* (i) length of vector to encode and
  7426. vector of quantizer indexes */
  7427. int state_first /* (i) position of start state in the
  7428. 80 vec */
  7429. ){
  7430. float *syntOut;
  7431. float syntOutBuf[LPC_FILTERORDER+STATE_SHORT_LEN_30MS];
  7432. float toQ, xq;
  7433. int n;
  7434. int index;
  7435. /* initialization of buffer for filtering */
  7436. memset(syntOutBuf, 0, LPC_FILTERORDER*sizeof(float));
  7437. Andersen, et al. Experimental [Page 186]
  7438. RFC 3951 Internet Low Bit Rate Codec December 2004
  7439. /* initialization of pointer for filtering */
  7440. syntOut = &syntOutBuf[LPC_FILTERORDER];
  7441. /* synthesis and weighting filters on input */
  7442. if (state_first) {
  7443. AllPoleFilter (in, weightDenum, SUBL, LPC_FILTERORDER);
  7444. } else {
  7445. AllPoleFilter (in, weightDenum,
  7446. iLBCenc_inst->state_short_len-SUBL,
  7447. LPC_FILTERORDER);
  7448. }
  7449. /* encoding loop */
  7450. for (n=0; n<len; n++) {
  7451. /* time update of filter coefficients */
  7452. if ((state_first)&&(n==SUBL)){
  7453. syntDenum += (LPC_FILTERORDER+1);
  7454. weightDenum += (LPC_FILTERORDER+1);
  7455. /* synthesis and weighting filters on input */
  7456. AllPoleFilter (&in[n], weightDenum, len-n,
  7457. LPC_FILTERORDER);
  7458. } else if ((state_first==0)&&
  7459. (n==(iLBCenc_inst->state_short_len-SUBL))) {
  7460. syntDenum += (LPC_FILTERORDER+1);
  7461. weightDenum += (LPC_FILTERORDER+1);
  7462. /* synthesis and weighting filters on input */
  7463. AllPoleFilter (&in[n], weightDenum, len-n,
  7464. LPC_FILTERORDER);
  7465. }
  7466. /* prediction of synthesized and weighted input */
  7467. syntOut[n] = 0.0;
  7468. AllPoleFilter (&syntOut[n], weightDenum, 1,
  7469. LPC_FILTERORDER);
  7470. /* quantization */
  7471. toQ = in[n]-syntOut[n];
  7472. Andersen, et al. Experimental [Page 187]
  7473. RFC 3951 Internet Low Bit Rate Codec December 2004
  7474. sort_sq(&xq, &index, toQ, state_sq3Tbl, 8);
  7475. out[n]=index;
  7476. syntOut[n] = state_sq3Tbl[out[n]];
  7477. /* update of the prediction filter */
  7478. AllPoleFilter(&syntOut[n], weightDenum, 1,
  7479. LPC_FILTERORDER);
  7480. }
  7481. }
  7482. /*----------------------------------------------------------------*
  7483. * encoding of start state
  7484. *---------------------------------------------------------------*/
  7485. void StateSearchW(
  7486. iLBC_Enc_Inst_t *iLBCenc_inst,
  7487. /* (i) Encoder instance */
  7488. float *residual,/* (i) target residual vector */
  7489. float *syntDenum, /* (i) lpc synthesis filter */
  7490. float *weightDenum, /* (i) weighting filter denuminator */
  7491. int *idxForMax, /* (o) quantizer index for maximum
  7492. amplitude */
  7493. int *idxVec, /* (o) vector of quantization indexes */
  7494. int len, /* (i) length of all vectors */
  7495. int state_first /* (i) position of start state in the
  7496. 80 vec */
  7497. ){
  7498. float dtmp, maxVal;
  7499. float tmpbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN_30MS];
  7500. float *tmp, numerator[1+LPC_FILTERORDER];
  7501. float foutbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN_30MS], *fout;
  7502. int k;
  7503. float qmax, scal;
  7504. /* initialization of buffers and filter coefficients */
  7505. memset(tmpbuf, 0, LPC_FILTERORDER*sizeof(float));
  7506. memset(foutbuf, 0, LPC_FILTERORDER*sizeof(float));
  7507. for (k=0; k<LPC_FILTERORDER; k++) {
  7508. numerator[k]=syntDenum[LPC_FILTERORDER-k];
  7509. }
  7510. numerator[LPC_FILTERORDER]=syntDenum[0];
  7511. tmp = &tmpbuf[LPC_FILTERORDER];
  7512. fout = &foutbuf[LPC_FILTERORDER];
  7513. /* circular convolution with the all-pass filter */
  7514. Andersen, et al. Experimental [Page 188]
  7515. RFC 3951 Internet Low Bit Rate Codec December 2004
  7516. memcpy(tmp, residual, len*sizeof(float));
  7517. memset(tmp+len, 0, len*sizeof(float));
  7518. ZeroPoleFilter(tmp, numerator, syntDenum, 2*len,
  7519. LPC_FILTERORDER, fout);
  7520. for (k=0; k<len; k++) {
  7521. fout[k] += fout[k+len];
  7522. }
  7523. /* identification of the maximum amplitude value */
  7524. maxVal = fout[0];
  7525. for (k=1; k<len; k++) {
  7526. if (fout[k]*fout[k] > maxVal*maxVal){
  7527. maxVal = fout[k];
  7528. }
  7529. }
  7530. maxVal=(float)fabs(maxVal);
  7531. /* encoding of the maximum amplitude value */
  7532. if (maxVal < 10.0) {
  7533. maxVal = 10.0;
  7534. }
  7535. maxVal = (float)log10(maxVal);
  7536. sort_sq(&dtmp, idxForMax, maxVal, state_frgqTbl, 64);
  7537. /* decoding of the maximum amplitude representation value,
  7538. and corresponding scaling of start state */
  7539. maxVal=state_frgqTbl[*idxForMax];
  7540. qmax = (float)pow(10,maxVal);
  7541. scal = (float)(4.5)/qmax;
  7542. for (k=0; k<len; k++){
  7543. fout[k] *= scal;
  7544. }
  7545. /* predictive noise shaping encoding of scaled start state */
  7546. AbsQuantW(iLBCenc_inst, fout,syntDenum,
  7547. weightDenum,idxVec, len, state_first);
  7548. }
  7549. Andersen, et al. Experimental [Page 189]
  7550. RFC 3951 Internet Low Bit Rate Codec December 2004
  7551. A.47. syntFilter.h
  7552. /******************************************************************
  7553. iLBC Speech Coder ANSI-C Source Code
  7554. syntFilter.h
  7555. Copyright (C) The Internet Society (2004).
  7556. All Rights Reserved.
  7557. ******************************************************************/
  7558. #ifndef __iLBC_SYNTFILTER_H
  7559. #define __iLBC_SYNTFILTER_H
  7560. void syntFilter(
  7561. float *Out, /* (i/o) Signal to be filtered */
  7562. float *a, /* (i) LP parameters */
  7563. int len, /* (i) Length of signal */
  7564. float *mem /* (i/o) Filter state */
  7565. );
  7566. #endif
  7567. A.48. syntFilter.c
  7568. /******************************************************************
  7569. iLBC Speech Coder ANSI-C Source Code
  7570. syntFilter.c
  7571. Copyright (C) The Internet Society (2004).
  7572. All Rights Reserved.
  7573. ******************************************************************/
  7574. #include "iLBC_define.h"
  7575. /*----------------------------------------------------------------*
  7576. * LP synthesis filter.
  7577. *---------------------------------------------------------------*/
  7578. void syntFilter(
  7579. float *Out, /* (i/o) Signal to be filtered */
  7580. float *a, /* (i) LP parameters */
  7581. int len, /* (i) Length of signal */
  7582. Andersen, et al. Experimental [Page 190]
  7583. RFC 3951 Internet Low Bit Rate Codec December 2004
  7584. float *mem /* (i/o) Filter state */
  7585. ){
  7586. int i, j;
  7587. float *po, *pi, *pa, *pm;
  7588. po=Out;
  7589. /* Filter first part using memory from past */
  7590. for (i=0; i<LPC_FILTERORDER; i++) {
  7591. pi=&Out[i-1];
  7592. pa=&a[1];
  7593. pm=&mem[LPC_FILTERORDER-1];
  7594. for (j=1; j<=i; j++) {
  7595. *po-=(*pa++)*(*pi--);
  7596. }
  7597. for (j=i+1; j<LPC_FILTERORDER+1; j++) {
  7598. *po-=(*pa++)*(*pm--);
  7599. }
  7600. po++;
  7601. }
  7602. /* Filter last part where the state is entirely in
  7603. the output vector */
  7604. for (i=LPC_FILTERORDER; i<len; i++) {
  7605. pi=&Out[i-1];
  7606. pa=&a[1];
  7607. for (j=1; j<LPC_FILTERORDER+1; j++) {
  7608. *po-=(*pa++)*(*pi--);
  7609. }
  7610. po++;
  7611. }
  7612. /* Update state vector */
  7613. memcpy(mem, &Out[len-LPC_FILTERORDER],
  7614. LPC_FILTERORDER*sizeof(float));
  7615. }
  7616. Andersen, et al. Experimental [Page 191]
  7617. RFC 3951 Internet Low Bit Rate Codec December 2004
  7618. Authors' Addresses
  7619. Soren Vang Andersen
  7620. Department of Communication Technology
  7621. Aalborg University
  7622. Fredrik Bajers Vej 7A
  7623. 9200 Aalborg
  7624. Denmark
  7625. Phone: ++45 9 6358627
  7626. EMail: sva@kom.auc.dk
  7627. Alan Duric
  7628. Telio AS
  7629. Stoperigt. 2
  7630. Oslo, N-0250
  7631. Norway
  7632. Phone: +47 21673555
  7633. EMail: alan.duric@telio.no
  7634. Henrik Astrom
  7635. Global IP Sound AB
  7636. Olandsgatan 42
  7637. Stockholm, S-11663
  7638. Sweden
  7639. Phone: +46 8 54553040
  7640. EMail: henrik.astrom@globalipsound.com
  7641. Roar Hagen
  7642. Global IP Sound AB
  7643. Olandsgatan 42
  7644. Stockholm, S-11663
  7645. Sweden
  7646. Phone: +46 8 54553040
  7647. EMail: roar.hagen@globalipsound.com
  7648. Andersen, et al. Experimental [Page 192]
  7649. RFC 3951 Internet Low Bit Rate Codec December 2004
  7650. W. Bastiaan Kleijn
  7651. Global IP Sound AB
  7652. Olandsgatan 42
  7653. Stockholm, S-11663
  7654. Sweden
  7655. Phone: +46 8 54553040
  7656. EMail: bastiaan.kleijn@globalipsound.com
  7657. Jan Linden
  7658. Global IP Sound Inc.
  7659. 900 Kearny Street, suite 500
  7660. San Francisco, CA-94133
  7661. USA
  7662. Phone: +1 415 397 2555
  7663. EMail: jan.linden@globalipsound.com
  7664. Andersen, et al. Experimental [Page 193]
  7665. RFC 3951 Internet Low Bit Rate Codec December 2004
  7666. Full Copyright Statement
  7667. Copyright (C) The Internet Society (2004).
  7668. This document is subject to the rights, licenses and restrictions
  7669. contained in BCP 78, and except as set forth therein, the authors
  7670. retain all their rights.
  7671. This document and the information contained herein are provided on an
  7672. "AS IS" basis and THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS
  7673. OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY AND THE INTERNET
  7674. ENGINEERING TASK FORCE DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED,
  7675. INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE
  7676. INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED
  7677. WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  7678. Intellectual Property
  7679. The IETF takes no position regarding the validity or scope of any
  7680. Intellectual Property Rights or other rights that might be claimed to
  7681. pertain to the implementation or use of the technology described in
  7682. this document or the extent to which any license under such rights
  7683. might or might not be available; nor does it represent that it has
  7684. made any independent effort to identify any such rights. Information
  7685. on the IETF's procedures with respect to rights in IETF Documents can
  7686. be found in BCP 78 and BCP 79.
  7687. Copies of IPR disclosures made to the IETF Secretariat and any
  7688. assurances of licenses to be made available, or the result of an
  7689. attempt made to obtain a general license or permission for the use of
  7690. such proprietary rights by implementers or users of this
  7691. specification can be obtained from the IETF on-line IPR repository at
  7692. http://www.ietf.org/ipr.
  7693. The IETF invites any interested party to bring to its attention any
  7694. copyrights, patents or patent applications, or other proprietary
  7695. rights that may cover technology that may be required to implement
  7696. this standard. Please address the information to the IETF at ietf-
  7697. ipr@ietf.org.
  7698. Acknowledgement
  7699. Funding for the RFC Editor function is currently provided by the
  7700. Internet Society.
  7701. Andersen, et al. Experimental [Page 194]