draft-ietf-codec-opus.xml 277 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE rfc SYSTEM 'rfc2629.dtd'>
  3. <?rfc toc="yes" symrefs="yes" ?>
  4. <rfc ipr="trust200902" category="std" docName="draft-ietf-codec-opus-08">
  5. <front>
  6. <title abbrev="Interactive Audio Codec">Definition of the Opus Audio Codec</title>
  7. <author initials="JM" surname="Valin" fullname="Jean-Marc Valin">
  8. <organization>Mozilla</organization>
  9. <address>
  10. <postal>
  11. <street>650 Castro Street</street>
  12. <city>Mountain View</city>
  13. <region>CA</region>
  14. <code>94041</code>
  15. <country>USA</country>
  16. </postal>
  17. <phone>+1 650 903-0800</phone>
  18. <email>jmvalin@jmvalin.ca</email>
  19. </address>
  20. </author>
  21. <author initials="K." surname="Vos" fullname="Koen Vos">
  22. <organization>Skype Technologies S.A.</organization>
  23. <address>
  24. <postal>
  25. <street>Stadsgarden 6</street>
  26. <city>Stockholm</city>
  27. <region></region>
  28. <code>11645</code>
  29. <country>SE</country>
  30. </postal>
  31. <phone>+46 855 921 989</phone>
  32. <email>koen.vos@skype.net</email>
  33. </address>
  34. </author>
  35. <author initials="T." surname="Terriberry" fullname="Timothy Terriberry">
  36. <organization>Mozilla Corporation</organization>
  37. <address>
  38. <postal>
  39. <street>650 Castro Street</street>
  40. <city>Mountain View</city>
  41. <region>CA</region>
  42. <code>94041</code>
  43. <country>USA</country>
  44. </postal>
  45. <phone>+1 650 903-0800</phone>
  46. <email>tterriberry@mozilla.com</email>
  47. </address>
  48. </author>
  49. <date day="16" month="August" year="2011" />
  50. <area>General</area>
  51. <workgroup></workgroup>
  52. <abstract>
  53. <t>
  54. This document defines the Opus codec, designed for interactive speech and audio
  55. transmission over the Internet.
  56. </t>
  57. </abstract>
  58. </front>
  59. <middle>
  60. <section anchor="introduction" title="Introduction">
  61. <t>
  62. The Opus codec is a real-time interactive audio codec composed of a linear
  63. prediction (LP)-based layer and a Modified Discrete Cosine Transform
  64. (MDCT)-based layer.
  65. The main idea behind using two layers is that in speech, linear prediction
  66. techniques (such as CELP) code low frequencies more efficiently than transform
  67. (e.g., MDCT) domain techniques, while the situation is reversed for music and
  68. higher speech frequencies.
  69. Thus a codec with both layers available can operate over a wider range than
  70. either one alone and, by combining them, achieve better quality than either
  71. one individually.
  72. </t>
  73. <t>
  74. The primary normative part of this specification is provided by the source code
  75. in <xref target="ref-implementation"></xref>.
  76. In general, only the decoder portion of this software is normative, though a
  77. significant amount of code is shared by both the encoder and decoder.
  78. <!--TODO: Forward reference conformance test-->
  79. The decoder contains significant amounts of integer and fixed-point arithmetic
  80. which must be performed exactly, including all rounding considerations, so any
  81. useful specification must make extensive use of domain-specific symbolic
  82. language to adequately define these operations.
  83. Additionally, any
  84. conflict between the symbolic representation and the included reference
  85. implementation must be resolved. For the practical reasons of compatibility and
  86. testability it would be advantageous to give the reference implementation
  87. priority in any disagreement. The C language is also one of the most
  88. widely understood human-readable symbolic representations for machine
  89. behavior.
  90. For these reasons this RFC uses the reference implementation as the sole
  91. symbolic representation of the codec.
  92. </t>
  93. <!--TODO: C is not unambiguous; many parts are implementation-defined-->
  94. <t>While the symbolic representation is unambiguous and complete it is not
  95. always the easiest way to understand the codec's operation. For this reason
  96. this document also describes significant parts of the codec in English and
  97. takes the opportunity to explain the rationale behind many of the more
  98. surprising elements of the design. These descriptions are intended to be
  99. accurate and informative, but the limitations of common English sometimes
  100. result in ambiguity, so it is expected that the reader will always read
  101. them alongside the symbolic representation. Numerous references to the
  102. implementation are provided for this purpose. The descriptions sometimes
  103. differ from the reference in ordering or through mathematical simplification
  104. wherever such deviation makes an explanation easier to understand.
  105. For example, the right shift and left shift operations in the reference
  106. implementation are often described using division and multiplication in the text.
  107. In general, the text is focused on the "what" and "why" while the symbolic
  108. representation most clearly provides the "how".
  109. </t>
  110. <section anchor="notation" title="Notation and Conventions">
  111. <t>
  112. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
  113. "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
  114. interpreted as described in RFC 2119.
  115. </t>
  116. <t>
  117. Even when using floating-point, various operations in the codec require
  118. bit-exact fixed-point behavior.
  119. The notation "Q&lt;n&gt;", where n is an integer, denotes the number of binary
  120. digits to the right of the decimal point in a fixed-point number.
  121. For example, a signed Q14 value in a 16-bit word can represent values from
  122. -2.0 to 1.99993896484375, inclusive.
  123. This notation is for informational purposes only.
  124. Arithmetic, when described, always operates on the underlying integer.
  125. E.g., the text will explicitly indicate any shifts required after a
  126. multiplication.
  127. </t>
  128. <t>
  129. Expressions, where included in the text, follow C operator rules and
  130. precedence, with the exception that syntax like "2**n" is used to indicate 2
  131. raised to the power n.
  132. The text also makes use of the following functions:
  133. </t>
  134. <section anchor="min" title="min(x,y)">
  135. <t>
  136. The smallest of two values x and y.
  137. </t>
  138. </section>
  139. <section anchor="max" title="max(x,y)">
  140. <t>
  141. The largest of two values x and y.
  142. </t>
  143. </section>
  144. <section anchor="clamp" title="clamp(lo,x,hi)">
  145. <figure align="center">
  146. <artwork align="center"><![CDATA[
  147. clamp(lo,x,hi) = max(lo,min(x,hi))
  148. ]]></artwork>
  149. </figure>
  150. <t>
  151. With this definition, if lo&gt;hi, the lower bound is the one that is enforced.
  152. </t>
  153. </section>
  154. <section anchor="sign" title="sign(x)">
  155. <t>
  156. The sign of x, i.e.,
  157. <figure align="center">
  158. <artwork align="center"><![CDATA[
  159. ( -1, x < 0 ,
  160. sign(x) = < 0, x == 0 ,
  161. ( 1, x > 0 .
  162. ]]></artwork>
  163. </figure>
  164. </t>
  165. </section>
  166. <section anchor="log2" title="log2(f)">
  167. <t>
  168. The base-two logarithm of f.
  169. </t>
  170. </section>
  171. <section anchor="ilog" title="ilog(n)">
  172. <t>
  173. The minimum number of bits required to store a positive integer n in two's
  174. complement notation, or 0 for a non-positive integer n.
  175. <figure align="center">
  176. <artwork align="center"><![CDATA[
  177. ( 0, n <= 0,
  178. ilog(n) = <
  179. ( floor(log2(n))+1, n > 0
  180. ]]></artwork>
  181. </figure>
  182. Examples:
  183. <list style="symbols">
  184. <t>ilog(-1) = 0</t>
  185. <t>ilog(0) = 0</t>
  186. <t>ilog(1) = 1</t>
  187. <t>ilog(2) = 2</t>
  188. <t>ilog(3) = 2</t>
  189. <t>ilog(4) = 3</t>
  190. <t>ilog(7) = 3</t>
  191. </list>
  192. </t>
  193. </section>
  194. </section>
  195. </section>
  196. <section anchor="overview" title="Opus Codec Overview">
  197. <t>
  198. The Opus codec scales from 6&nbsp;kb/s narrowband mono speech to 510&nbsp;kb/s
  199. fullband stereo music, with algorithmic delays ranging from 5&nbsp;ms to
  200. 65.2&nbsp;ms.
  201. At any given time, either the LP layer, the MDCT layer, or both, may be active.
  202. It can seamlessly switch between all of its various operating modes, giving it
  203. a great deal of flexibility to adapt to varying content and network
  204. conditions without renegotiating the current session.
  205. Internally, the codec always operates at a 48&nbsp;kHz sampling rate, though it
  206. allows input and output of various bandwidths, defined as follows:
  207. </t>
  208. <texttable>
  209. <ttcol>Abbreviation</ttcol>
  210. <ttcol align="right">Audio Bandwidth</ttcol>
  211. <ttcol align="right">Sampling Rate (Effective)</ttcol>
  212. <c>NB (narrowband)</c> <c>4&nbsp;kHz</c> <c>8&nbsp;kHz</c>
  213. <c>MB (medium-band)</c> <c>6&nbsp;kHz</c> <c>12&nbsp;kHz</c>
  214. <c>WB (wideband)</c> <c>8&nbsp;kHz</c> <c>16&nbsp;kHz</c>
  215. <c>SWB (super-wideband)</c> <c>12&nbsp;kHz</c> <c>24&nbsp;kHz</c>
  216. <c>FB (fullband)</c> <c>20&nbsp;kHz</c> <c>48&nbsp;kHz</c>
  217. </texttable>
  218. <t>
  219. These can be chosen independently on the encoder and decoder side, e.g., a
  220. fullband signal can be decoded as wideband, or vice versa.
  221. This approach ensures a sender and receiver can always interoperate, regardless
  222. of the capabilities of their actual audio hardware.
  223. </t>
  224. <t>
  225. Opus defines super-wideband (SWB) mode to have an effective sampling rate of
  226. 24&nbsp;kHz, unlike some other audio coding standards that use 32&nbsp;kHz.
  227. This was chosen for a number of reasons.
  228. The band layout in the MDCT layer naturally allows skipping coefficients for
  229. frequencies over 12&nbsp;kHz, but does not allow cleanly dropping frequencies
  230. over 16&nbsp;kHz.
  231. The choice of 24&nbsp;kHz also makes resampling in the MDCT layer easier, as 24
  232. evenly divides 48, and when 24&nbsp;kHz is sufficient, it can save computation
  233. in other processing, such as Acoustic Echo Cancellation (AEC).
  234. Experimental changes to the band layout to allow a 16&nbsp;kHz cutoff showed
  235. potential quality degredations, and at typical bitrates the number of bits
  236. saved by using such a cutoff instead of coding in fullband (FB) mode is very
  237. small.
  238. Therefore, if an application wishes to process a signal sampled at 32&nbsp;kHz,
  239. it should just use FB mode.
  240. </t>
  241. <t>
  242. The LP layer is based on the
  243. <eref target='http://developer.skype.com/silk'>SILK</eref> codec
  244. <xref target="SILK"></xref>.
  245. It supports NB, MB, or WB audio and frame sizes from 10&nbsp;ms to 60&nbsp;ms,
  246. and requires an additional 5.2&nbsp;ms look-ahead for noise shaping estimation
  247. (5&nbsp;ms) and internal resampling (0.2&nbsp;ms).
  248. Like Vorbis and many other modern codecs, SILK is inherently designed for
  249. variable-bitrate (VBR) coding, though an encoder can with sufficient effort
  250. produce constant-bitrate (CBR) or near-CBR streams.
  251. </t>
  252. <t>
  253. The MDCT layer is based on the
  254. <eref target='http://www.celt-codec.org/'>CELT</eref> codec
  255. <xref target="CELT"></xref>.
  256. It supports sampling NB, WB, SWB, or FB audio and frame sizes from 2.5&nbsp;ms
  257. to 20&nbsp;ms, and requires an additional 2.5&nbsp;ms look-ahead due to the
  258. overlapping MDCT windows.
  259. The CELT codec is inherently designed for CBR coding, but unlike many CBR
  260. codecs it is not limited to a set of predetermined rates.
  261. It internally allocates bits to exactly fill any given target budget, and an
  262. encoder can produce a VBR stream by varying the target on a per-frame basis.
  263. The MDCT layer is not used for speech when the audio bandwidth is WB or less,
  264. as it is not useful there.
  265. On the other hand, non-speech signals are not always adequately coded using
  266. linear prediction, so for music only the MDCT layer should be used.
  267. </t>
  268. <t>
  269. A hybrid mode allows the use of both layers simultaneously with a frame size of
  270. 10 or 20&nbsp;ms and a SWB or FB audio bandwidth.
  271. Each frame is split into a low frequency signal and a high frequency signal,
  272. with a cutoff of 8&nbsp;kHz.
  273. The LP layer then codes the low frequency signal, followed by the MDCT layer
  274. coding the high frequency signal.
  275. In the MDCT layer, all bands below 8&nbsp;kHz are discarded, so there is no
  276. coding redundancy between the two layers.
  277. </t>
  278. <t>
  279. At the decoder, the two decoder outputs are simply added together.
  280. To compensate for the different look-aheads required by each layer, the CELT
  281. encoder input is delayed by an additional 2.7&nbsp;ms.
  282. This ensures that low frequencies and high frequencies arrive at the same time.
  283. This extra delay MAY be reduced by an encoder by using less look-ahead for noise
  284. shaping or using a simpler resampler in the LP layer, but this will reduce
  285. quality.
  286. However, the base 2.5&nbsp;ms look-ahead in the CELT layer cannot be reduced in
  287. the encoder because it is needed for the MDCT overlap, whose size is fixed by
  288. the decoder.
  289. </t>
  290. <t>
  291. Both layers use the same entropy coder, avoiding any waste from "padding bits"
  292. between them.
  293. The hybrid approach makes it easy to support both CBR and VBR coding.
  294. Although the LP layer is VBR, the bit allocation of the MDCT layer can produce
  295. a final stream that is CBR by using all the bits left unused by the LP layer.
  296. </t>
  297. </section>
  298. <section anchor="modes" title="Codec Modes">
  299. <t>
  300. As described, the two layers can be combined in three possible operating modes:
  301. <list style="numbers">
  302. <t>A LP-only mode for use in low bitrate connections with an audio bandwidth of
  303. WB or less,</t>
  304. <t>A hybrid (LP+MDCT) mode for SWB or FB speech at medium bitrates, and</t>
  305. <t>An MDCT-only mode for very low delay speech transmission as well as music
  306. transmission.</t>
  307. </list>
  308. </t>
  309. <t>
  310. A single packet may contain multiple audio frames.
  311. However, they must share a common set of parameters, including the operating
  312. mode, audio bandwidth, frame size, and channel count.
  313. This section describes the possible combinations of these parameters and the
  314. internal framing used to pack multiple frames into a single packet.
  315. This framing is not self-delimiting.
  316. Instead, it assumes that a higher layer (such as UDP or RTP or Ogg or Matroska)
  317. will communicate the length, in bytes, of the packet, and it uses this
  318. information to reduce the framing overhead in the packet itself.
  319. A decoder implementation MUST support the framing described in this section.
  320. An alternative, self-delimiting variant of the framing is described in
  321. <xref target="self-delimiting-framing"/>.
  322. Support for that variant is OPTIONAL.
  323. </t>
  324. <section anchor="toc_byte" title="The TOC Byte">
  325. <t>
  326. An Opus packet begins with a single-byte table-of-contents (TOC) header that
  327. signals which of the various modes and configurations a given packet uses.
  328. It is composed of a frame count code, "c", a stereo flag, "s", and a
  329. configuration number, "config", arranged as illustrated in
  330. <xref target="toc_byte_fig"/>.
  331. A description of each of these fields follows.
  332. </t>
  333. <figure anchor="toc_byte_fig" title="The TOC byte">
  334. <artwork align="center"><![CDATA[
  335. 0
  336. 0 1 2 3 4 5 6 7
  337. +-+-+-+-+-+-+-+-+
  338. | c |s| config |
  339. +-+-+-+-+-+-+-+-+
  340. ]]></artwork>
  341. </figure>
  342. <t>
  343. The top five bits of the TOC byte, labeled "config", encode one of 32 possible
  344. configurations of operating mode, audio bandwidth, and frame size.
  345. <xref target="config_bits"/> lists the parameters for each configuration.
  346. </t>
  347. <texttable anchor="config_bits" title="TOC Byte Configuration Parameters">
  348. <ttcol>Configuration Number(s)</ttcol>
  349. <ttcol>Mode</ttcol>
  350. <ttcol>Bandwidth</ttcol>
  351. <ttcol>Frame Size(s)</ttcol>
  352. <c>0...3</c> <c>LP-only</c> <c>NB</c> <c>10, 20, 40, 60&nbsp;ms</c>
  353. <c>4...7</c> <c>LP-only</c> <c>MB</c> <c>10, 20, 40, 60&nbsp;ms</c>
  354. <c>8...11</c> <c>LP-only</c> <c>WB</c> <c>10, 20, 40, 60&nbsp;ms</c>
  355. <c>12...13</c> <c>Hybrid</c> <c>SWB</c> <c>10, 20&nbsp;ms</c>
  356. <c>14...15</c> <c>Hybrid</c> <c>FB</c> <c>10, 20&nbsp;ms</c>
  357. <c>16...19</c> <c>MDCT-only</c> <c>NB</c> <c>2.5, 5, 10, 20&nbsp;ms</c>
  358. <c>20...23</c> <c>MDCT-only</c> <c>WB</c> <c>2.5, 5, 10, 20&nbsp;ms</c>
  359. <c>24...27</c> <c>MDCT-only</c> <c>SWB</c> <c>2.5, 5, 10, 20&nbsp;ms</c>
  360. <c>28...31</c> <c>MDCT-only</c> <c>FB</c> <c>2.5, 5, 10, 20&nbsp;ms</c>
  361. </texttable>
  362. <t>
  363. One additional bit, labeled "s", is used to signal mono vs. stereo, with 0
  364. indicating mono and 1 indicating stereo.
  365. </t>
  366. <t>
  367. The remaining two bits of the TOC byte, labeled "c", code the number of frames
  368. per packet (codes 0 to 3) as follows:
  369. <list style="symbols">
  370. <t>0: 1 frame in the packet</t>
  371. <t>1: 2 frames in the packet, each with equal compressed size</t>
  372. <t>2: 2 frames in the packet, with different compressed sizes</t>
  373. <t>3: an arbitrary number of frames in the packet</t>
  374. </list>
  375. This draft refers to a packet as a code 0 packet, code 1 packet, etc., based on
  376. the value of "c".
  377. </t>
  378. <t>
  379. A well-formed Opus packet MUST contain at least one byte with the TOC
  380. information, though the frame(s) within a packet MAY be zero bytes long.
  381. </t>
  382. </section>
  383. <section title="Frame Packing">
  384. <t>
  385. This section describes how frames are packed according to each possible value
  386. of "c" in the TOC byte.
  387. </t>
  388. <section anchor="frame-length-coding" title="Frame Length Coding">
  389. <t>
  390. When a packet contains multiple VBR frames, the compressed length of one or
  391. more of these frames is indicated with a one or two byte sequence, with the
  392. meaning of the first byte as follows:
  393. <list style="symbols">
  394. <t>0: No frame (discontinuous transmission (DTX) or lost packet)</t>
  395. <!--TODO: Would be nice to be clearer about the distinction between "frame
  396. size" (in samples or ms) and "the compressed size of the frame" (in bytes).
  397. "the compressed length of the frame" is maybe a little better, but not when we
  398. jump back and forth to talking about sizes.-->
  399. <t>1...251: Size of the frame in bytes</t>
  400. <t>252...255: A second byte is needed. The total size is (size[1]*4)+size[0]</t>
  401. </list>
  402. </t>
  403. <t>
  404. The maximum representable size is 255*4+255=1275&nbsp;bytes. This limit MUST NOT
  405. be exceeded, even when no length field is used.
  406. For 20&nbsp;ms frames, this represents a bitrate of 510&nbsp;kb/s, which is
  407. approximately the highest useful rate for lossily compressed fullband stereo
  408. music.
  409. Beyond this point, lossless codecs are more appropriate.
  410. It is also roughly the maximum useful rate of the MDCT layer, as shortly
  411. thereafter quality no longer improves with additional bits due to limitations
  412. on the codebook sizes.
  413. </t>
  414. <t>
  415. No length is transmitted for the last frame in a VBR packet, or for any of the
  416. frames in a CBR packet, as it can be inferred from the total size of the
  417. packet and the size of all other data in the packet.
  418. However, the length of any individual frame MUST NOT exceed 1275&nbsp;bytes, to
  419. allow for repacketization by gateways, conference bridges, or other software.
  420. </t>
  421. </section>
  422. <section title="One Frame in the Packet (Code&nbsp;0)">
  423. <t>
  424. For code&nbsp;0 packets, the TOC byte is immediately followed by N-1&nbsp;bytes
  425. of compressed data for a single frame (where N is the size of the packet),
  426. as illustrated in <xref target="code0_packet"/>.
  427. </t>
  428. <figure anchor="code0_packet" title="A Code 0 Packet" align="center">
  429. <artwork align="center"><![CDATA[
  430. 0 1 2 3
  431. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  432. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  433. |0|0|s| config | |
  434. +-+-+-+-+-+-+-+-+ |
  435. | Compressed frame 1 (N-1 bytes)... :
  436. : |
  437. | |
  438. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  439. ]]></artwork>
  440. </figure>
  441. </section>
  442. <section title="Two Frames in the Packet, Each with Equal Compressed Size (Code&nbsp;1)">
  443. <t>
  444. For code 1 packets, the TOC byte is immediately followed by the
  445. (N-1)/2&nbsp;bytes of compressed data for the first frame, followed by
  446. (N-1)/2&nbsp;bytes of compressed data for the second frame, as illustrated in
  447. <xref target="code1_packet"/>.
  448. The number of payload bytes available for compressed data, N-1, MUST be even
  449. for all code 1 packets.
  450. </t>
  451. <figure anchor="code1_packet" title="A Code 1 Packet" align="center">
  452. <artwork align="center"><![CDATA[
  453. 0 1 2 3
  454. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  455. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  456. |1|0|s| config | |
  457. +-+-+-+-+-+-+-+-+ :
  458. | Compressed frame 1 ((N-1)/2 bytes)... |
  459. : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  460. | | |
  461. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
  462. | Compressed frame 2 ((N-1)/2 bytes)... |
  463. : +-+-+-+-+-+-+-+-+
  464. | |
  465. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  466. ]]></artwork>
  467. </figure>
  468. </section>
  469. <section title="Two Frames in the Packet, with Different Compressed Sizes (Code&nbsp;2)">
  470. <t>
  471. For code 2 packets, the TOC byte is followed by a one or two byte sequence
  472. indicating the length of the first frame (marked N1 in the figure below),
  473. followed by N1 bytes of compressed data for the first frame.
  474. The remaining N-N1-2 or N-N1-3&nbsp;bytes are the compressed data for the
  475. second frame.
  476. This is illustrated in <xref target="code2_packet"/>.
  477. The length of the first frame, N1, MUST be no larger than the size of the
  478. payload remaining after decoding that length for all code 2 packets.
  479. </t>
  480. <figure anchor="code2_packet" title="A Code 2 Packet" align="center">
  481. <artwork align="center"><![CDATA[
  482. 0 1 2 3
  483. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  484. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  485. |0|1|s| config | N1 (1-2 bytes): |
  486. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
  487. | Compressed frame 1 (N1 bytes)... |
  488. : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  489. | | |
  490. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  491. | Compressed frame 2... :
  492. : |
  493. | |
  494. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  495. ]]></artwork>
  496. </figure>
  497. </section>
  498. <section title="An Arbitrary Number of Frames in the Packet (Code&nbsp;3)">
  499. <t>
  500. Code 3 packets may encode an arbitrary number of frames, as well as additional
  501. padding, called "Opus padding" to indicate that this padding is added at the
  502. Opus layer, rather than at the transport layer.
  503. For code 3 packets, the TOC byte is followed by a byte encoding the number of
  504. frames in the packet in bits 0 to 5 (marked "M" in the figure below), with bit
  505. 6 indicating whether or not Opus padding is inserted (marked "p" in the figure
  506. below), and bit 7 indicating VBR (marked "v" in the figure below).
  507. M MUST NOT be zero, and the audio duration contained within a packet MUST NOT
  508. exceed 120&nbsp;ms.
  509. This limits the maximum frame count for any frame size to 48 (for 2.5&nbsp;ms
  510. frames), with lower limits for longer frame sizes.
  511. <xref target="frame_count_byte"/> illustrates the layout of the frame count
  512. byte.
  513. </t>
  514. <figure anchor="frame_count_byte" title="The frame count byte">
  515. <artwork align="center"><![CDATA[
  516. 0
  517. 0 1 2 3 4 5 6 7
  518. +-+-+-+-+-+-+-+-+
  519. | M |p|v|
  520. +-+-+-+-+-+-+-+-+
  521. ]]></artwork>
  522. </figure>
  523. <t>
  524. When Opus padding is used, the number of bytes of padding is encoded in the
  525. bytes following the frame count byte.
  526. Values from 0...254 indicate that 0...254&nbsp;bytes of padding are included,
  527. in addition to the byte(s) used to indicate the size of the padding.
  528. If the value is 255, then the size of the additional padding is 254&nbsp;bytes,
  529. plus the padding value encoded in the next byte.
  530. The additional padding bytes appear at the end of the packet, and SHOULD be set
  531. to zero by the encoder.
  532. The decoder MUST accept any value for the padding bytes, however.
  533. By using code 255 multiple times, it is possible to create a packet of any
  534. specific, desired size.
  535. Let P be the total amount of padding, including both the trailing padding bytes
  536. themselves and the header bytes used to indicate how many there are.
  537. Then P MUST be no more than N-2 for CBR packets, or N-M-1 for VBR packets.
  538. </t>
  539. <t>
  540. In the CBR case, the compressed length of each frame in bytes is equal to the
  541. number of remaining bytes in the packet after subtracting the (optional)
  542. padding, (N-2-P), divided by M.
  543. This number MUST be an integer multiple of M.
  544. The compressed data for all M frames then follows, each of size
  545. (N-2-P)/M&nbsp;bytes, as illustrated in <xref target="code3cbr_packet"/>.
  546. </t>
  547. <figure anchor="code3cbr_packet" title="A CBR Code 3 Packet" align="center">
  548. <artwork align="center"><![CDATA[
  549. 0 1 2 3
  550. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  551. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  552. |1|1|s| config | M |p|0| Padding length (Optional) :
  553. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  554. | |
  555. : Compressed frame 1 ((N-2-P)/M bytes)... :
  556. | |
  557. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  558. | |
  559. : Compressed frame 2 ((N-2-P)/M bytes)... :
  560. | |
  561. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  562. | |
  563. : ... :
  564. | |
  565. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  566. | |
  567. : Compressed frame M ((N-2-P)/M bytes)... :
  568. | |
  569. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  570. : Opus Padding (Optional)... |
  571. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  572. ]]></artwork>
  573. </figure>
  574. <t>
  575. In the VBR case, the (optional) padding length is followed by M-1 frame
  576. lengths (indicated by "N1" to "N[M-1]" in the figure below), each encoded in a
  577. one or two byte sequence as described above.
  578. The packet MUST contain enough data for the M-1 lengths after the (optional)
  579. padding, and the sum of these lengths MUST be no larger than the number of
  580. bytes remaining in the packet after decoding them.
  581. The compressed data for all M frames follows, each frame consisting of the
  582. indicated number of bytes, with the final frame consuming any remaining bytes
  583. before the final padding, as illustrated in <xref target="code3cbr_packet"/>.
  584. The number of header bytes (TOC byte, frame count byte, padding length bytes,
  585. and frame length bytes), plus the length of the first M-1 frames themselves,
  586. plus the length of the padding MUST be no larger than N, the total size of the
  587. packet.
  588. </t>
  589. <figure anchor="code3vbr_packet" title="A VBR Code 3 Packet" align="center">
  590. <artwork align="center"><![CDATA[
  591. 0 1 2 3
  592. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  593. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  594. |1|1|s| config | M |p|1| Padding length (Optional) :
  595. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  596. : N1 (1-2 bytes): N2 (1-2 bytes): ... : N[M-1] |
  597. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  598. | |
  599. : Compressed frame 1 (N1 bytes)... :
  600. | |
  601. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  602. | |
  603. : Compressed frame 2 (N2 bytes)... :
  604. | |
  605. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  606. | |
  607. : ... :
  608. | |
  609. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  610. | |
  611. : Compressed frame M... :
  612. | |
  613. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  614. : Opus Padding (Optional)... |
  615. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  616. ]]></artwork>
  617. </figure>
  618. </section>
  619. </section>
  620. <section anchor="examples" title="Examples">
  621. <t>
  622. Simplest case, one NB mono 20&nbsp;ms SILK frame:
  623. </t>
  624. <figure>
  625. <artwork><![CDATA[
  626. 0 1 2 3
  627. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  628. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  629. |0|0|0| 1 | compressed data... :
  630. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  631. ]]></artwork>
  632. </figure>
  633. <t>
  634. Two FB mono 5&nbsp;ms CELT frames of the same compressed size:
  635. </t>
  636. <figure>
  637. <artwork><![CDATA[
  638. 0 1 2 3
  639. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  640. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  641. |1|0|0| 29 | compressed data... :
  642. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  643. ]]></artwork>
  644. </figure>
  645. <t>
  646. Two FB mono 20&nbsp;ms hybrid frames of different compressed size:
  647. </t>
  648. <figure>
  649. <artwork><![CDATA[
  650. 0 1 2 3
  651. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  652. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  653. |1|1|0| 15 | 2 |0|1| N1 | |
  654. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  655. | compressed data... :
  656. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  657. ]]></artwork>
  658. </figure>
  659. <t>
  660. Four FB stereo 20&nbsp;ms CELT frames of the same compressed size:
  661. </t>
  662. <figure>
  663. <artwork><![CDATA[
  664. 0 1 2 3
  665. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  666. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  667. |1|1|1| 31 | 4 |0|0| compressed data... :
  668. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  669. ]]></artwork>
  670. </figure>
  671. </section>
  672. <section title="Extending Opus">
  673. <t>
  674. A receiver MUST NOT process packets which violate the rules above as normal
  675. Opus packets.
  676. They are reserved for future applications, such as in-band headers (containing
  677. metadata, etc.) or multichannel support.
  678. </t>
  679. </section>
  680. </section>
  681. <section title="Opus Decoder">
  682. <t>
  683. The Opus decoder consists of two main blocks: the SILK decoder and the CELT decoder.
  684. The output of the Opus decode is the sum of the outputs from the SILK and CELT decoders
  685. with proper sample rate conversion and delay compensation as illustrated in the
  686. block diagram below. At any given time, one or both of the SILK and CELT decoders
  687. may be active.
  688. </t>
  689. <figure>
  690. <artwork>
  691. <![CDATA[
  692. +-------+ +----------+
  693. | SILK | | sample |
  694. +->|decoder|--->| rate |----+
  695. bit- +-------+ | | | |conversion| v
  696. stream | Range |---+ +-------+ +----------+ /---\ audio
  697. ------->|decoder| | + |------>
  698. | |---+ +-------+ \---/
  699. +-------+ | | CELT | ^
  700. +-------------->|decoder|-------+
  701. | |
  702. +-------+
  703. ]]>
  704. </artwork>
  705. </figure>
  706. <section anchor="range-decoder" title="Range Decoder">
  707. <t>
  708. Opus uses an entropy coder based on <xref target="range-coding"></xref>,
  709. which is itself a rediscovery of the FIFO arithmetic code introduced by <xref target="coding-thesis"></xref>.
  710. It is very similar to arithmetic encoding, except that encoding is done with
  711. digits in any base instead of with bits,
  712. so it is faster when using larger bases (i.e., an octet). All of the
  713. calculations in the range coder must use bit-exact integer arithmetic.
  714. </t>
  715. <t>
  716. Symbols may also be coded as "raw bits" packed directly into the bitstream,
  717. bypassing the range coder.
  718. These are packed backwards starting at the end of the frame, as illustrated in
  719. <xref target="rawbits-example"/>.
  720. This reduces complexity and makes the stream more resilient to bit errors, as
  721. corruption in the raw bits will not desynchronize the decoding process, unlike
  722. corruption in the input to the range decoder.
  723. Raw bits are only used in the CELT layer.
  724. </t>
  725. <figure anchor="rawbits-example" title="Illustrative example of packing range
  726. coder and raw bits data">
  727. <artwork align="center"><![CDATA[
  728. 0 1 2 3
  729. 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
  730. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  731. | Range coder data (packed MSb to LSb) -> :
  732. + +
  733. : :
  734. + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  735. : | <- Boundary occurs at an arbitrary bit position :
  736. +-+-+-+ +
  737. : <- Raw bits data (packed LSb to MSb) |
  738. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  739. ]]></artwork>
  740. </figure>
  741. <t>
  742. Each symbol coded by the range coder is drawn from a finite alphabet and coded
  743. in a separate "context", which describes the size of the alphabet and the
  744. relative frequency of each symbol in that alphabet.
  745. </t>
  746. <t>
  747. Suppose there is a context with n symbols, identified with an index that ranges
  748. from 0 to n-1.
  749. The parameters needed to encode or decode a symbol in this context are
  750. represented by a three-tuple (fl[k],&nbsp;fh[k],&nbsp;ft), with
  751. 0&nbsp;&lt;=&nbsp;fl[k]&nbsp;&lt;&nbsp;fh[k]&nbsp;&lt;=&nbsp;ft&nbsp;&lt;=&nbsp;65535.
  752. The values of this tuple are derived from the probability model for the
  753. symbol, represented by traditional "frequency counts". Because Opus
  754. uses static contexts these are not updated as symbols are decoded.
  755. Let f[i] be the frequency of symbol i.
  756. Then the three-tuple corresponding to symbol k is given by
  757. </t>
  758. <figure align="center">
  759. <artwork align="center"><![CDATA[
  760. k-1 n-1
  761. __ __
  762. fl[k] = \ f[i], fh[k] = fl[k] + f[k], ft[k] = \ f[i]
  763. /_ /_
  764. i=0 i=0
  765. ]]></artwork>
  766. </figure>
  767. <t>
  768. The range decoder extracts the symbols and integers encoded using the range
  769. encoder in <xref target="range-encoder"/>.
  770. The range decoder maintains an internal state vector composed of the two-tuple
  771. (val,rng), representing the difference between the high end of the current
  772. range and the actual coded value, minus one, and the size of the current
  773. range, respectively.
  774. Both val and rng are 32-bit unsigned integer values.
  775. The decoder initializes rng to 128 and initializes val to 127 minus the top 7
  776. bits of the first input octet.
  777. The remaining bit is saved for use in the renormalization procedure described
  778. in <xref target="range-decoder-renorm"/>, which the decoder invokes
  779. immediately after initialization to read additional bits and establish the
  780. invariant that rng&nbsp;&gt;&nbsp;2**23.
  781. </t>
  782. <section anchor="decoding-symbols" title="Decoding Symbols">
  783. <t>
  784. Decoding a symbol is a two-step process.
  785. The first step determines a 16-bit unsigned value fs, which lies within the
  786. range of some symbol in the current context.
  787. The second step updates the range decoder state with the three-tuple
  788. (fl[k],&nbsp;fh[k],&nbsp;ft) corresponding to that symbol.
  789. </t>
  790. <t>
  791. The first step is implemented by ec_decode() (entdec.c), which computes
  792. <figure align="center">
  793. <artwork align="center"><![CDATA[
  794. fs = ft - min(val/(rng/ft)+1, ft) .
  795. ]]></artwork>
  796. </figure>
  797. The divisions here are exact integer division.
  798. </t>
  799. <t>
  800. The decoder then identifies the symbol in the current context corresponding to
  801. fs; i.e., the value of k whose three-tuple (fl[k],&nbsp;fh[k],&nbsp;ft)
  802. satisfies fl[k]&nbsp;&lt;=&nbsp;fs&nbsp;&lt;&nbsp;fh[k].
  803. It uses this tuple to update val according to
  804. <figure align="center">
  805. <artwork align="center"><![CDATA[
  806. val = val - (rng/ft)*(ft-fh[k]) .
  807. ]]></artwork>
  808. </figure>
  809. If fl[k] is greater than zero, then the decoder updates rng using
  810. <figure align="center">
  811. <artwork align="center"><![CDATA[
  812. rng = (rng/ft)*(fh[k]-fl[k]) .
  813. ]]></artwork>
  814. </figure>
  815. Otherwise, it updates rng using
  816. <figure align="center">
  817. <artwork align="center"><![CDATA[
  818. rng = rng - (rng/ft)*(ft-fh[k]).
  819. ]]></artwork>
  820. </figure>
  821. </t>
  822. <t>
  823. Using a special case for the first symbol (rather than the last symbol, as is
  824. commonly done in other arithmetic coders) ensures that all the truncation
  825. error from the finite precision arithmetic accumulates in symbol 0.
  826. This makes the cost of coding a 0 slightly smaller, on average, than its
  827. estimated probability indicates and makes the cost of coding any other symbol
  828. slightly larger.
  829. When contexts are designed so that 0 is the most probable symbol, which is
  830. often the case, this strategy minimizes the inefficiency introduced by the
  831. finite precision.
  832. It also makes some of the special-case decoding routines in
  833. <xref target="decoding-alternate"/> particularly simple.
  834. </t>
  835. <t>
  836. After the updates, implemented by ec_dec_update() (entdec.c), the decoder
  837. normalizes the range using the procedure in the next section, and returns the
  838. index k.
  839. </t>
  840. <section anchor="range-decoder-renorm" title="Renormalization">
  841. <t>
  842. To normalize the range, the decoder repeats the following process, implemented
  843. by ec_dec_normalize() (entdec.c), until rng&nbsp;&gt;&nbsp;2**23.
  844. If rng is already greater than 2**23, the entire process is skipped.
  845. First, it sets rng to (rng&lt;&lt;8).
  846. Then it reads the next octet of the payload and combines it with the left-over
  847. bit buffered from the previous octet to form the 8-bit value sym.
  848. It takes the left-over bit as the high bit (bit 7) of sym, and the top 7 bits
  849. of the octet it just read as the other 7 bits of sym.
  850. The remaining bit in the octet just read is buffered for use in the next
  851. iteration.
  852. If no more input octets remain, it uses zero bits instead.
  853. Then, it sets
  854. <figure align="center">
  855. <artwork align="center"><![CDATA[
  856. val = ((val<<8) + (255-sym)) & 0x7FFFFFFF .
  857. ]]></artwork>
  858. </figure>
  859. </t>
  860. <t>
  861. It is normal and expected that the range decoder will read several bytes
  862. into the raw bits data (if any) at the end of the packet by the time the frame
  863. is completely decoded, as illustrated in <xref target="finalize-example"/>.
  864. This same data MUST also be returned as raw bits when requested.
  865. The encoder is expected to terminate the stream in such a way that the decoder
  866. will decode the intended values regardless of the data contained in the raw
  867. bits.
  868. <xref target="encoder-finalizing"/> describes a procedure for doing this.
  869. If the range decoder consumes all of the bytes belonging to the current frame,
  870. it MUST continue to use zero when any further input bytes are required, even
  871. if there is additional data in the current packet from padding or other
  872. frames.
  873. </t>
  874. <figure anchor="finalize-example" title="Illustrative example of raw bits
  875. overlapping range coder data">
  876. <artwork align="center"><![CDATA[
  877. n n+1 n+2 n+3
  878. 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
  879. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  880. : | <----------- Overlap region ------------> | :
  881. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  882. ^ ^
  883. | End of data buffered by the range coder |
  884. ...-----------------------------------------------+
  885. |
  886. | End of data consumed by raw bits
  887. +-------------------------------------------------------...
  888. ]]></artwork>
  889. </figure>
  890. </section>
  891. </section>
  892. <section anchor="decoding-alternate" title="Alternate Decoding Methods">
  893. <t>
  894. The reference implementation uses three additional decoding methods that are
  895. exactly equivalent to the above, but make assumptions and simplifications that
  896. allow for a more efficient implementation.
  897. </t>
  898. <section title="ec_decode_bin()">
  899. <t>
  900. The first is ec_decode_bin() (entdec.c), defined using the parameter ftb
  901. instead of ft.
  902. It is mathematically equivalent to calling ec_decode() with
  903. ft = (1&lt;&lt;ftb), but avoids one of the divisions.
  904. </t>
  905. </section>
  906. <section title="ec_dec_bit_logp()">
  907. <t>
  908. The next is ec_dec_bit_logp() (entdec.c), which decodes a single binary symbol,
  909. replacing both the ec_decode() and ec_dec_update() steps.
  910. The context is described by a single parameter, logp, which is the absolute
  911. value of the base-2 logarithm of the probability of a "1".
  912. It is mathematically equivalent to calling ec_decode() with
  913. ft&nbsp;=&nbsp;(1&lt;&lt;logp), followed by ec_dec_update() with
  914. the 3-tuple (fl[k]&nbsp;=&nbsp;0, fh[k]&nbsp;=&nbsp;(1&lt;&lt;logp)-1,
  915. ft&nbsp;=&nbsp;(1&lt;&lt;logp)) if the returned value
  916. of fs is less than (1&lt;&lt;logp)-1 (a "0" was decoded), and with
  917. (fl[k]&nbsp;=&nbsp;(1&lt;&lt;logp)-1,
  918. fh[k]&nbsp;=&nbsp;ft&nbsp;=&nbsp;(1&lt;&lt;logp)) otherwise (a "1" was
  919. decoded).
  920. The implementation requires no multiplications or divisions.
  921. </t>
  922. </section>
  923. <section title="ec_dec_icdf()">
  924. <t>
  925. The last is ec_dec_icdf() (entdec.c), which decodes a single symbol with a
  926. table-based context of up to 8 bits, also replacing both the ec_decode() and
  927. ec_dec_update() steps, as well as the search for the decoded symbol in between.
  928. The context is described by two parameters, an icdf
  929. ("inverse" cumulative distribution function) table and ftb.
  930. As with ec_decode_bin(), (1&lt;&lt;ftb) is equivalent to ft.
  931. idcf[k], on the other hand, stores (1&lt;&lt;ftb)-fh[k], which is equal to
  932. (1&lt;&lt;ftb)-fl[k+1].
  933. fl[0] is assumed to be 0, and the table is terminated by a value of 0 (where
  934. fh[k]&nbsp;==&nbsp;ft).
  935. </t>
  936. <t>
  937. The function is mathematically equivalent to calling ec_decode() with
  938. ft&nbsp;=&nbsp;(1&lt;&lt;ftb), using the returned value fs to search the table
  939. for the first entry where fs&nbsp;&lt;&nbsp;(1&lt;&lt;ftb)-icdf[k], and
  940. calling ec_dec_update() with fl[k]&nbsp;=&nbsp;(1&lt;&lt;ftb)-icdf[k-1] (or 0
  941. if k&nbsp;==&nbsp;0), fh[k]&nbsp;=&nbsp;(1&lt;&lt;ftb)-idcf[k], and
  942. ft&nbsp;=&nbsp;(1&lt;&lt;ftb).
  943. Combining the search with the update allows the division to be replaced by a
  944. series of multiplications (which are usually much cheaper), and using an
  945. inverse CDF allows the use of an ftb as large as 8 in an 8-bit table without
  946. any special cases.
  947. This is the primary interface with the range decoder in the SILK layer, though
  948. it is used in a few places in the CELT layer as well.
  949. </t>
  950. <t>
  951. Although icdf[k] is more convenient for the code, the frequency counts, f[k],
  952. are a more natural representation of the probability distribution function
  953. (PDF) for a given symbol.
  954. Therefore this draft lists the latter, not the former, when describing the
  955. context in which a symbol is coded as a list, e.g., {4, 4, 4, 4}/16 for a
  956. uniform context with four possible values and ft=16.
  957. The value of ft after the slash is always the sum of the entries in the PDF,
  958. but is included for convenience.
  959. Contexts with identical probabilities, f[k]/ft, but different values of ft
  960. (or equivalently, ftb) are not the same, and cannot, in general, be used in
  961. place of one another.
  962. An icdf table is also not capable of representing a PDF where the first symbol
  963. has 0 probability.
  964. In such contexts, ec_dec_icdf() can decode the symbol by using a table that
  965. drops the entries for any initial zero-probability values and adding the
  966. constant offset of the first value with a non-zero probability to its return
  967. value.
  968. </t>
  969. </section>
  970. </section>
  971. <section anchor="decoding-bits" title="Decoding Raw Bits">
  972. <t>
  973. The raw bits used by the CELT layer are packed at the end of the packet, with
  974. the least significant bit of the first value packed in the least significant
  975. bit of the last byte, filling up to the most significant bit in the last byte,
  976. continuing on to the least significant bit of the penultimate byte, and so on.
  977. The reference implementation reads them using ec_dec_bits() (entdec.c).
  978. Because the range decoder must read several bytes ahead in the stream, as
  979. described in <xref target="range-decoder-renorm"/>, the input consumed by the
  980. raw bits MAY overlap with the input consumed by the range coder, and a decoder
  981. MUST allow this.
  982. The format should render it impossible to attempt to read more raw bits than
  983. there are actual bits in the frame, though a decoder MAY wish to check for
  984. this and report an error.
  985. </t>
  986. </section>
  987. <section anchor="decoding-ints" title="Decoding Uniformly Distributed Integers">
  988. <t>
  989. The ec_dec_uint() (entdec.c) function decodes one of ft equiprobable values in
  990. the range 0 to ft-1, inclusive, each with a frequency of 1, where ft may be as
  991. large as 2**32-1.
  992. Because ec_decode() is limited to a total frequency of 2**16-1, this is split
  993. up into a range coded symbol representing up to 8 of the high bits of the
  994. value, and, if necessary, raw bits representing the remaining bits.
  995. The limit of 8 bits in the range coded symbol is a trade-off between
  996. implementation complexity, modeling error (since the symbols no longer truly
  997. have equal coding cost), and rounding error introduced by the range coder
  998. itself (which gets larger as more bits are included).
  999. Using raw bits reduces the maximum number of divisions required in the worst
  1000. case, but means that it may be possible to decode a value outside the range
  1001. 0 to ft-1, inclusive.
  1002. </t>
  1003. <t>
  1004. ec_dec_uint() takes a single, positive parameter, ft, which is not necessarily
  1005. a power of two, and returns an integer, t, whose value lies between 0 and
  1006. ft-1, inclusive.
  1007. Let ftb = ilog(ft-1), i.e., the number of bits required to store ft-1 in two's
  1008. complement notation.
  1009. If ftb is 8 or less, then t is decoded with t = ec_decode(ft), and the range
  1010. coder state is updated using the three-tuple (t,t+1,ft).
  1011. </t>
  1012. <t>
  1013. If ftb is greater than 8, then the top 8 bits of t are decoded using
  1014. t = ec_decode((ft-1&gt;&gt;ftb-8)+1),
  1015. the decoder state is updated using the three-tuple
  1016. (t,t+1,(ft-1&gt;&gt;ftb-8)+1), and the remaining bits are decoded as raw bits,
  1017. setting t = t&lt;&lt;ftb-8|ec_dec_bits(ftb-8).
  1018. If, at this point, t >= ft, then the current frame is corrupt.
  1019. In that case, the decoder should assume there has been an error in the coding,
  1020. decoding, or transmission and SHOULD take measures to conceal the
  1021. error and/or report to the application that a problem has occurred.
  1022. </t>
  1023. </section>
  1024. <section anchor="decoder-tell" title="Current Bit Usage">
  1025. <t>
  1026. The bit allocation routines in the CELT decoder need a conservative upper bound
  1027. on the number of bits that have been used from the current frame thus far,
  1028. including both range coder bits and raw bits.
  1029. This drives allocation decisions that must match those made in the encoder.
  1030. The upper bound is computed in the reference implementation to whole-bit
  1031. precision by the function ec_tell() (entcode.h) and to fractional 1/8th bit
  1032. precision by the function ec_tell_frac() (entcode.c).
  1033. Like all operations in the range coder, it must be implemented in a bit-exact
  1034. manner, and must produce exactly the same value returned by the same functions
  1035. in the encoder after encoding the same symbols.
  1036. </t>
  1037. <t>
  1038. ec_tell() is guaranteed to return ceil(ec_tell_frac()/8.0).
  1039. In various places the codec will check to ensure there is enough room to
  1040. contain a symbol before attempting to decode it.
  1041. In practice, although the number of bits used so far is an upper bound,
  1042. decoding a symbol whose probability model suggests it has a worst-case cost of
  1043. p 1/8th bits may actually advance the return value of ec_tell_frac() by
  1044. p-1, p, or p+1 1/8th bits, due to approximation error in that upper bound,
  1045. truncation error in the range coder, and for large values of ft, modeling
  1046. error in ec_dec_uint().
  1047. </t>
  1048. <t>
  1049. However, this error is bounded, and periodic calls to ec_tell() or
  1050. ec_tell_frac() at precisely defined points in the decoding process prevent it
  1051. from accumulating.
  1052. For a range coder symbol that requires a whole number of bits (i.e.,
  1053. ft/(fh[k]-fl[k]) is a power of two), where there are at least p 1/8th bits
  1054. available, decoding the symbol will never advance the decoder past the end of
  1055. the frame ("bust the budget").
  1056. In this case the return value of ec_tell_frac() will only advance by more than
  1057. p 1/8th bits if there was an additional, fractional number of bits remaining,
  1058. and it will never advance beyond the next whole-bit boundary, which is safe,
  1059. since frames always contain a whole number of bits.
  1060. However, when p is not a whole number of bits, an extra 1/8th bit is required
  1061. to ensure that decoding the symbol will not bust the budget.
  1062. </t>
  1063. <t>
  1064. The reference implementation keeps track of the total number of whole bits that
  1065. have been processed by the decoder so far in the variable nbits_total,
  1066. including the (possibly fractional) number of bits that are currently
  1067. buffered, but not consumed, inside the range coder.
  1068. nbits_total is initialized to 33 just after the initial range renormalization
  1069. process completes (or equivalently, it can be initialized to 9 before the
  1070. first renormalization).
  1071. The extra two bits over the actual amount buffered by the range coder
  1072. guarantees that it is an upper bound and that there is enough room for the
  1073. encoder to terminate the stream.
  1074. Each iteration through the range coder's renormalization loop increases
  1075. nbits_total by 8.
  1076. Reading raw bits increases nbits_total by the number of raw bits read.
  1077. </t>
  1078. <section anchor="ec_tell" title="ec_tell()">
  1079. <t>
  1080. The whole number of bits buffered in rng may be estimated via l = ilog(rng).
  1081. ec_tell() then becomes a simple matter of removing these bits from the total.
  1082. It returns (nbits_total - l).
  1083. </t>
  1084. <t>
  1085. In a newly initialized decoder, before any symbols have been read, this reports
  1086. that 1 bit has been used.
  1087. This is the bit reserved for termination of the encoder.
  1088. </t>
  1089. </section>
  1090. <section anchor="ec_tell_frac" title="ec_tell_frac()">
  1091. <t>
  1092. ec_tell_frac() estimates the number of bits buffered in rng to fractional
  1093. precision.
  1094. Since rng must be greater than 2**23 after renormalization, l must be at least
  1095. 24.
  1096. Let r = rng&gt;&gt;(l-16), so that 32768 &lt;= r &lt; 65536, an unsigned Q15
  1097. value representing the fractional part of rng.
  1098. Then the following procedure can be used to add one bit of precision to l.
  1099. First, update r = r*r&gt;&gt;15.
  1100. Then add the 16th bit of r to l via l = 2*l + (r&gt;&gt;16).
  1101. Finally, if this bit was a 1, reduce r by a factor of two via r = r&gt;&gt;1,
  1102. so that it once again lies in the range 32768 &lt;= r &lt; 65536.
  1103. </t>
  1104. <t>
  1105. This procedure is repeated three times to extend l to 1/8th bit precision.
  1106. ec_tell_frac() then returns (nbits_total*8 - l).
  1107. </t>
  1108. </section>
  1109. </section>
  1110. </section>
  1111. <section anchor='outline_decoder' title='SILK Decoder'>
  1112. <t>
  1113. The decoder's LP layer uses a modified version of the SILK codec (herein simply
  1114. called "SILK"), which runs a decoded excitation signal through adaptive
  1115. long-term and short-term prediction synthesis filters.
  1116. It runs in NB, MB, and WB modes internally.
  1117. When used in a hybrid frame in SWB or FB mode, the LP layer itself still only
  1118. runs in WB mode.
  1119. </t>
  1120. <t>
  1121. Internally, the LP layer of a single Opus frame is composed of either a single
  1122. 10&nbsp;ms regular SILK frame or between one and three 20&nbsp;ms regular SILK
  1123. frames.
  1124. A stereo Opus frame may double the number of regular SILK frames (up to a total
  1125. of six), since it includes separate frames for a mid channel and, optionally,
  1126. a side channel.
  1127. Optional Low Bit-Rate Redundancy (LBRR) frames, which are reduced-bitrate
  1128. encodings of previous SILK frames, may be included to aid in recovery from
  1129. packet loss.
  1130. If present, these appear before the regular SILK frames.
  1131. They are in most respects identical to regular, active SILK frames, except that
  1132. they are usually encoded with a lower bitrate.
  1133. This draft uses "SILK frame" to refer to either one and "regular SILK frame" if
  1134. it needs to draw a distinction between the two.
  1135. </t>
  1136. <t>
  1137. Each SILK frame is in turn composed of either two or four 5&nbsp;ms subframes.
  1138. Various parameters, such as the quantization gain of the excitation and the
  1139. pitch lag and filter coefficients can vary on a subframe-by-subframe basis.
  1140. </t>
  1141. <t>
  1142. All of these frames and subframes are decoded from the same range coder, with
  1143. no padding between them.
  1144. Thus packing multiple SILK frames in a single Opus frame saves, on average,
  1145. half a byte per SILK frame.
  1146. It also allows some parameters to be predicted from prior SILK frames in the
  1147. same Opus frame, since this does not degrade packet loss robustness (beyond
  1148. any penalty for merely using fewer, larger packets to store multiple frames).
  1149. </t>
  1150. <t>
  1151. Stereo support in SILK uses a variant of mid-side coding, allowing a mono
  1152. decoder to simply decode the mid channel.
  1153. However, the data for the two channels is interleaved, so a mono decoder must
  1154. still unpack the data for the side channel.
  1155. It would be required to do so anyway for hybrid Opus frames, or to support
  1156. decoding individual 20&nbsp;ms frames.
  1157. </t>
  1158. <texttable anchor="silk_symbols">
  1159. <ttcol align="center">Symbol(s)</ttcol>
  1160. <ttcol align="center">PDF(s)</ttcol>
  1161. <ttcol align="center">Condition</ttcol>
  1162. <c>VAD flags</c>
  1163. <c>{1, 1}/2</c>
  1164. <c/>
  1165. <c>LBRR flag</c>
  1166. <c>{1, 1}/2</c>
  1167. <c/>
  1168. <c>Per-frame LBRR flags</c>
  1169. <c><xref target="silk_lbrr_flag_pdfs"/></c>
  1170. <c><xref target="silk_lbrr_flags"/></c>
  1171. <c>LBRR Frame(s)</c>
  1172. <c><xref target="silk_frame"/></c>
  1173. <c><xref target="silk_lbrr_flags"/></c>
  1174. <c>Regular SILK Frame(s)</c>
  1175. <c><xref target="silk_frame"/></c>
  1176. <c/>
  1177. <postamble>
  1178. Organization of the SILK layer of an Opus frame.
  1179. </postamble>
  1180. </texttable>
  1181. <section title="Decoder Modules">
  1182. <t>
  1183. An overview of the decoder is given in <xref target="decoder_figure"/>.
  1184. </t>
  1185. <figure align="center" anchor="decoder_figure">
  1186. <artwork align="center">
  1187. <![CDATA[
  1188. +---------+ +------------+
  1189. -->| Range |--->| Decode |---------------------------+
  1190. 1 | Decoder | 2 | Parameters |----------+ 5 |
  1191. +---------+ +------------+ 4 | |
  1192. 3 | | |
  1193. \/ \/ \/
  1194. +------------+ +------------+ +------------+
  1195. | Generate |-->| LTP |-->| LPC |-->
  1196. | Excitation | | Synthesis | | Synthesis | 6
  1197. +------------+ +------------+ +------------+
  1198. 1: Range encoded bitstream
  1199. 2: Coded parameters
  1200. 3: Pulses and gains
  1201. 4: Pitch lags and LTP coefficients
  1202. 5: LPC coefficients
  1203. 6: Decoded signal
  1204. ]]>
  1205. </artwork>
  1206. <postamble>Decoder block diagram.</postamble>
  1207. </figure>
  1208. <section title='Range Decoder'>
  1209. <t>
  1210. The range decoder decodes the encoded parameters from the received bitstream. Output from this function includes the pulses and gains for generating the excitation signal, as well as LTP and LSF codebook indices, which are needed for decoding LTP and LPC coefficients needed for LTP and LPC synthesis filtering the excitation signal, respectively.
  1211. </t>
  1212. </section>
  1213. <section title='Decode Parameters'>
  1214. <t>
  1215. Pulses and gains are decoded from the parameters that were decoded by the range decoder.
  1216. </t>
  1217. <t>
  1218. When a voiced frame is decoded and LTP codebook selection and indices are received, LTP coefficients are decoded using the selected codebook by choosing the vector that corresponds to the given codebook index in that codebook. This is done for each of the four subframes.
  1219. The LPC coefficients are decoded from the LSF codebook by first adding the chosen LSF vector and the decoded LSF residual signal. The resulting LSF vector is stabilized using the same method that was used in the encoder; see
  1220. <xref target='lsf_stabilizer_overview_section' />. The LSF coefficients are then converted to LPC coefficients, and passed on to the LPC synthesis filter.
  1221. </t>
  1222. </section>
  1223. <section title='Generate Excitation'>
  1224. <t>
  1225. The pulses signal is multiplied with the quantization gain to create the excitation signal.
  1226. </t>
  1227. </section>
  1228. <section title='LTP Synthesis'>
  1229. <t>
  1230. For voiced speech, the excitation signal e(n) is input to an LTP synthesis filter that recreates the long-term correlation removed in the LTP analysis filter and generates an LPC excitation signal e_LPC(n), according to
  1231. <figure align="center">
  1232. <artwork align="center">
  1233. <![CDATA[
  1234. d
  1235. __
  1236. e_LPC(n) = e(n) + \ e_LPC(n - L - i) * b_i,
  1237. /_
  1238. i=-d
  1239. ]]>
  1240. </artwork>
  1241. </figure>
  1242. using the pitch lag L, and the decoded LTP coefficients b_i.
  1243. The number of LTP coefficients is 5, and thus d&nbsp;=&nbsp;2.
  1244. For unvoiced speech, the output signal is simply a copy of the excitation signal, i.e., e_LPC(n) = e(n).
  1245. </t>
  1246. </section>
  1247. <section title='LPC Synthesis'>
  1248. <t>
  1249. In a similar manner, the short-term correlation that was removed in the LPC analysis filter is recreated in the LPC synthesis filter. The LPC excitation signal e_LPC(n) is filtered using the LTP coefficients a_i, according to
  1250. <figure align="center">
  1251. <artwork align="center">
  1252. <![CDATA[
  1253. d_LPC
  1254. __
  1255. y(n) = e_LPC(n) + \ y(n - i) * a_i,
  1256. /_
  1257. i=1
  1258. ]]>
  1259. </artwork>
  1260. </figure>
  1261. where d_LPC is the LPC synthesis filter order, and y(n) is the decoded output signal.
  1262. </t>
  1263. </section>
  1264. </section>
  1265. <!--TODO: Document mandated decoder resets-->
  1266. <section title="Header Bits">
  1267. <t>
  1268. The LP layer begins with two to eight header bits, decoded in silk_Decode()
  1269. (silk_dec_API.c).
  1270. These consist of one Voice Activity Detection (VAD) bit per frame (up to 3),
  1271. followed by a single flag indicating the presence of LBRR frames.
  1272. For a stereo packet, these flags correspond to the mid channel, and a second
  1273. set of flags is included for the side channel.
  1274. </t>
  1275. <t>
  1276. Because these are the first symbols decoded by the range coder, they can be
  1277. extracted directly from the upper bits of the first byte of compressed data.
  1278. Thus, a receiver can determine if an Opus frame contains any active SILK frames
  1279. without the overhead of using the range decoder.
  1280. </t>
  1281. </section>
  1282. <section anchor="silk_lbrr_flags" title="LBRR Flags">
  1283. <t>
  1284. For Opus frames longer than 20&nbsp;ms, a set of per-frame LBRR flags is
  1285. decoded for each channel that has its LBRR flag set.
  1286. For 40&nbsp;ms Opus frames the 2-frame LBRR flag PDF from
  1287. <xref target="silk_lbrr_flag_pdfs"/> is used, and for 60&nbsp;ms Opus frames
  1288. the 3-frame LBRR flag PDF is used.
  1289. For each channel, the resulting 2- or 3-bit integer contains the corresponding
  1290. LBRR flag for each frame, packed in order from the LSb to the MSb.
  1291. </t>
  1292. <texttable anchor="silk_lbrr_flag_pdfs" title="LBRR Flag PDFs">
  1293. <ttcol>Frame Size</ttcol>
  1294. <ttcol>PDF</ttcol>
  1295. <c>40&nbsp;ms</c> <c>{0, 53, 53, 150}/256</c>
  1296. <c>60&nbsp;ms</c> <c>{0, 41, 20, 29, 41, 15, 28, 82}/256</c>
  1297. </texttable>
  1298. </section>
  1299. <section anchor="silk_lbrr_frames" title="LBRR Frames">
  1300. <t>
  1301. The LBRR frames, if present, immediately follow, one per set LBRR flag, and
  1302. prior to any regular SILK frames.
  1303. <xref target="silk_frame"/> describes their exact contents.
  1304. LBRR frames do not include their own separate VAD flags.
  1305. LBRR frames are only meant to be transmitted for active speech, thus all LBRR
  1306. frames are treated as active.
  1307. </t>
  1308. <t>
  1309. In a stereo Opus frame longer than 20&nbsp;ms, although all the per-frame LBRR
  1310. flags for the mid channel are coded before the per-frame LBRR flags for the
  1311. side channel, the LBRR frames themselves are interleaved.
  1312. The LBRR frame for the mid channel of a given 20&nbsp;ms interval (if present)
  1313. is immediately followed by the corresponding LBRR frame for the side channel
  1314. (if present).
  1315. </t>
  1316. </section>
  1317. <section anchor="silk_regular_frames" title="Regular SILK Frames">
  1318. <t>
  1319. The regular SILK frame(s) follow the LBRR frames (if any).
  1320. <xref target="silk_frame"/> describes their contents, as well.
  1321. Unlike the LBRR frames, a regular SILK frame is always coded for each time
  1322. interval in an Opus frame, even if the corresponding VAD flag is unset.
  1323. Like the LBRR frames, in stereo Opus frames longer than 20&nbsp;ms, the mid and
  1324. side frames are interleaved for each 20&nbsp;ms interval.
  1325. The side frame may be skipped by coding an appropriate flag, as detailed in
  1326. <xref target="silk_mid_only_flag"/>.
  1327. </t>
  1328. </section>
  1329. <section anchor="silk_frame" title="SILK Frame Contents">
  1330. <t>
  1331. Each SILK frame includes a set of side information that encodes the frame type,
  1332. quantization type and gains, short-term prediction filter coefficients, an LSF
  1333. interpolation weight, long-term prediction filter lags and gains, and a
  1334. linear congruential generator (LCG) seed.
  1335. The quantized excitation signal follows these at the end of the frame.
  1336. <xref target="silk_frame_symbols"/> details the overall organization of a
  1337. SILK frame.
  1338. </t>
  1339. <texttable anchor="silk_frame_symbols">
  1340. <ttcol align="center">Symbol(s)</ttcol>
  1341. <ttcol align="center">PDF(s)</ttcol>
  1342. <ttcol align="center">Condition</ttcol>
  1343. <c>Stereo Prediction Weights</c>
  1344. <c><xref target="silk_stereo_pred_pdfs"/></c>
  1345. <c><xref target="silk_stereo_pred"/></c>
  1346. <c>Mid-Only Flag</c>
  1347. <c><xref target="silk_mid_only_pdf"/></c>
  1348. <c><xref target="silk_mid_only_flag"/></c>
  1349. <c>Frame Type</c>
  1350. <c><xref target="silk_frame_type"/></c>
  1351. <c/>
  1352. <c>Subframe Gains</c>
  1353. <c><xref target="silk_gains"/></c>
  1354. <c/>
  1355. <c>Normalized LSF Stage 1 Index</c>
  1356. <c><xref target="silk_nlsf_stage1_pdfs"/></c>
  1357. <c/>
  1358. <c>Normalized LSF Stage 2 Residual</c>
  1359. <c><xref target="silk_nlsf_stage2"/></c>
  1360. <c/>
  1361. <c>Normalized LSF Interpolation Weight</c>
  1362. <c><xref target="silk_nlsf_interp_pdf"/></c>
  1363. <c><xref target="silk_nlsf_interpolation"/></c>
  1364. <c>Primary Pitch Lag</c>
  1365. <c><xref target="silk_ltp_lags"/></c>
  1366. <c>Voiced frame</c>
  1367. <c>Subframe Pitch Contour</c>
  1368. <c><xref target="silk_pitch_contour_pdfs"/></c>
  1369. <c>Voiced frame</c>
  1370. <c>Periodicity Index</c>
  1371. <c><xref target="silk_perindex_pdf"/></c>
  1372. <c>Voiced frame</c>
  1373. <c>LTP Filter</c>
  1374. <c><xref target="silk_ltp_filter_pdfs"/></c>
  1375. <c>Voiced frame</c>
  1376. <c>LTP Scaling</c>
  1377. <c><xref target="silk_ltp_scaling_pdf"/></c>
  1378. <c><xref target="silk_ltp_scaling"/></c>
  1379. <c>LCG Seed</c>
  1380. <c><xref target="silk_seed_pdf"/></c>
  1381. <c/>
  1382. <c>Excitation Rate Level</c>
  1383. <c><xref target="silk_rate_level_pdfs"/></c>
  1384. <c/>
  1385. <c>Excitation Pulse Counts</c>
  1386. <c><xref target="silk_pulse_count_pdfs"/></c>
  1387. <c/>
  1388. <c>Excitation Pulse Locations</c>
  1389. <c><xref target="silk_pulse_locations"/></c>
  1390. <c>Non-zero pulse count</c>
  1391. <c>Excitation LSb's</c>
  1392. <c><xref target="silk_shell_lsb_pdf"/></c>
  1393. <c><xref target="silk_pulse_counts"/></c>
  1394. <c>Excitation Signs</c>
  1395. <c><xref target="silk_sign_pdfs"/></c>
  1396. <c><xref target="silk_signs"/></c>
  1397. <postamble>
  1398. Order of the symbols in an individual SILK frame.
  1399. </postamble>
  1400. </texttable>
  1401. <section anchor="silk_stereo_pred" title="Stereo Prediction Weights">
  1402. <t>
  1403. A SILK frame corresponding to the mid channel of a stereo Opus frame begins
  1404. with a pair of side channel prediction weights, designed such that zeros
  1405. indicate normal mid-side coupling.
  1406. Since these weights can change on every frame, the first portion of each frame
  1407. linearly interpolates between the previous weights and the current ones, using
  1408. zeros for the previous weights if none are available.
  1409. These prediction weights are never included in a mono Opus frame, and the
  1410. previous weights are reset to zeros on any transition from a mono to stereo.
  1411. They are also not included in an LBRR frame for the side channel, even if the
  1412. LBRR flags indicate the corresponding mid channel was not coded.
  1413. In that case, the previous weights are used, again substituting in zeros if no
  1414. previous weights are available since the last decoder reset.
  1415. </t>
  1416. <t>
  1417. The prediction weights are coded in three separate pieces, which are decoded
  1418. by silk_stereo_decode_pred() (silk_decode_stereo_pred.c).
  1419. The first piece jointly codes the high-order part of a table index for both
  1420. weights.
  1421. The second piece codes the low-order part of each table index.
  1422. The third piece codes an offset used to linearly interpolate between table
  1423. indices.
  1424. The details are as follows.
  1425. </t>
  1426. <t>
  1427. Let n be an index decoded with the 25-element stage-1 PDF in
  1428. <xref target="silk_stereo_pred_pdfs"/>.
  1429. Then let i0 and i1 be indices decoded with the stage-2 and stage-3 PDFs in
  1430. <xref target="silk_stereo_pred_pdfs"/>, respectively, and let i2 and i3
  1431. be two more indices decoded with the stage-2 and stage-3 PDFs, all in that
  1432. order.
  1433. </t>
  1434. <texttable anchor="silk_stereo_pred_pdfs" title="Stereo Weight PDFs">
  1435. <ttcol align="left">Stage</ttcol>
  1436. <ttcol align="left">PDF</ttcol>
  1437. <c>Stage 1</c>
  1438. <c>{7, 2, 1, 1, 1,
  1439. 10, 24, 8, 1, 1,
  1440. 3, 23, 92, 23, 3,
  1441. 1, 1, 8, 24, 10,
  1442. 1, 1, 1, 2, 7}/256</c>
  1443. <c>Stage 2</c>
  1444. <c>{85, 86, 85}/256</c>
  1445. <c>Stage 3</c>
  1446. <c>{51, 51, 52, 51, 51}/256</c>
  1447. </texttable>
  1448. <t>
  1449. Then use n, i0, and i2 to form two table indices, wi0 and wi1, according to
  1450. <figure align="center">
  1451. <artwork align="center"><![CDATA[
  1452. wi0 = i0 + 3*(n/5)
  1453. wi1 = i2 + 3*(n%5)
  1454. ]]></artwork>
  1455. </figure>
  1456. where the division is exact integer division.
  1457. The range of these indices is 0 to 14, inclusive.
  1458. Let w[i] be the i'th weight from <xref target="silk_stereo_weights_table"/>.
  1459. Then the two prediction weights, w0_Q13 and w1_Q13, are
  1460. <figure align="center">
  1461. <artwork align="center"><![CDATA[
  1462. w1_Q13 = w_Q13[wi1]
  1463. + ((w_Q13[wi1+1] - w_Q13[wi1])*6554) >> 16)*(2*i3 + 1)
  1464. w0_Q13 = w_Q13[wi0]
  1465. + ((w_Q13[wi0+1] - w_Q13[wi0])*6554) >> 16)*(2*i1 + 1)
  1466. - w1_Q13
  1467. ]]></artwork>
  1468. </figure>
  1469. </t>
  1470. <texttable anchor="silk_stereo_weights_table"
  1471. title="Stereo Weight Table">
  1472. <ttcol align="left">Index</ttcol>
  1473. <ttcol align="right">Weight (Q13)</ttcol>
  1474. <c>0</c> <c>-13732</c>
  1475. <c>1</c> <c>-10050</c>
  1476. <c>2</c> <c>-8266</c>
  1477. <c>3</c> <c>-7526</c>
  1478. <c>4</c> <c>-6500</c>
  1479. <c>5</c> <c>-5000</c>
  1480. <c>6</c> <c>-2950</c>
  1481. <c>7</c> <c>-820</c>
  1482. <c>8</c> <c>820</c>
  1483. <c>9</c> <c>2950</c>
  1484. <c>10</c> <c>5000</c>
  1485. <c>11</c> <c>6500</c>
  1486. <c>12</c> <c>7526</c>
  1487. <c>13</c> <c>8266</c>
  1488. <c>14</c> <c>10050</c>
  1489. <c>15</c> <c>13732</c>
  1490. </texttable>
  1491. </section>
  1492. <section anchor="silk_mid_only_flag" title="Mid-Only Flag">
  1493. <t>
  1494. A flag appears after the stereo prediction weights that indicates if only the
  1495. mid channel is coded for this time interval.
  1496. It is omitted when there are no stereo weights, i.e., unless the SILK frame
  1497. corresponds to the mid channel of a stereo Opus frame, and it is also omitted
  1498. for an LBRR frame when the corresponding LBRR flags indicate the side channel
  1499. is present.
  1500. When present, the decoder reads a single value using the PDF in
  1501. <xref target="silk_mid_only_pdf"/>, as implemented in
  1502. silk_stereo_decode_mid_only() (silk_decode_stereo_pred.c).
  1503. If the flag is set, then there is no corresponding SILK frame for the side
  1504. channel, the entire decoding process for the side channel is skipped, and
  1505. zeros are used during the stereo unmixing process<!--TODO: ref-->.
  1506. As stated above, LBRR frames still include this flag when the LBRR flag
  1507. indicates that the side channel is not coded.
  1508. In that case, if this flag is zero (indicating that there should be a side
  1509. channel), then Packet Loss Concealment (PLC, see
  1510. <xref target="Packet Loss Concealment"/>) SHOULD be invoked to recover a
  1511. side channel signal.
  1512. </t>
  1513. <texttable anchor="silk_mid_only_pdf" title="Mid-Only Flag PDF">
  1514. <ttcol align="left">PDF</ttcol>
  1515. <c>{192, 64}/256</c>
  1516. </texttable>
  1517. </section>
  1518. <section anchor="silk_frame_type" title="Frame Type">
  1519. <t>
  1520. Each SILK frame contains a single "frame type" symbol that jointly codes the
  1521. signal type and quantization offset type of the corresponding frame.
  1522. If the current frame is a regular SILK frame whose VAD bit was not set (an
  1523. "inactive" frame), then the frame type symbol takes on a value of either 0 or
  1524. 1 and is decoded using the first PDF in <xref target="silk_frame_type_pdfs"/>.
  1525. If the frame is an LBRR frame or a regular SILK frame whose VAD flag was set
  1526. (an "active" frame), then the value of the symbol may range from 2 to 5,
  1527. inclusive, and is decoded using the second PDF in
  1528. <xref target="silk_frame_type_pdfs"/>.
  1529. <xref target="silk_frame_type_table"/> translates between the value of the
  1530. frame type symbol and the corresponding signal type and quantization offset
  1531. type.
  1532. </t>
  1533. <texttable anchor="silk_frame_type_pdfs" title="Frame Type PDFs">
  1534. <ttcol>VAD Flag</ttcol>
  1535. <ttcol>PDF</ttcol>
  1536. <c>Inactive</c> <c>{26, 230, 0, 0, 0, 0}/256</c>
  1537. <c>Active</c> <c>{0, 0, 24, 74, 148, 10}/256</c>
  1538. </texttable>
  1539. <texttable anchor="silk_frame_type_table"
  1540. title="Signal Type and Quantization Offset Type from Frame Type">
  1541. <ttcol>Frame Type</ttcol>
  1542. <ttcol>Signal Type</ttcol>
  1543. <ttcol align="right">Quantization Offset Type</ttcol>
  1544. <c>0</c> <c>Inactive</c> <c>Low</c>
  1545. <c>1</c> <c>Inactive</c> <c>High</c>
  1546. <c>2</c> <c>Unvoiced</c> <c>Low</c>
  1547. <c>3</c> <c>Unvoiced</c> <c>High</c>
  1548. <c>4</c> <c>Voiced</c> <c>Low</c>
  1549. <c>5</c> <c>Voiced</c> <c>High</c>
  1550. </texttable>
  1551. </section>
  1552. <section anchor="silk_gains" title="Subframe Gains">
  1553. <t>
  1554. A separate quantization gain is coded for each 5&nbsp;ms subframe.
  1555. These gains control the step size between quantization levels of the excitation
  1556. signal and, therefore, the quality of the reconstruction.
  1557. They are independent of the pitch gains coded for voiced frames.
  1558. The quantization gains are themselves uniformly quantized to 6&nbsp;bits on a
  1559. log scale, giving them a resolution of approximately 1.369&nbsp;dB and a range
  1560. of approximately 1.94&nbsp;dB to 88.21&nbsp;dB.
  1561. </t>
  1562. <t>
  1563. For the first LBRR frame, an LBRR frame where the previous LBRR frame in the
  1564. same channel is not coded, or the first regular SILK frame in the current
  1565. channel of an Opus frame, the first subframe uses an independent coding
  1566. method.
  1567. In a stereo Opus frame, the mid-only flag (from
  1568. <xref target="silk_mid_only_flag"/>) may cause the first regular SILK frame in
  1569. the side channel to occur in a later time interval than the first regular SILK
  1570. frame in the mid channel.
  1571. The 3 most significant bits of the quantization gain are decoded using a PDF
  1572. selected from <xref target="silk_independent_gain_msb_pdfs"/> based on the
  1573. decoded signal type.
  1574. </t>
  1575. <texttable anchor="silk_independent_gain_msb_pdfs"
  1576. title="PDFs for Independent Quantization Gain MSb Coding">
  1577. <ttcol align="left">Signal Type</ttcol>
  1578. <ttcol align="left">PDF</ttcol>
  1579. <c>Inactive</c> <c>{32, 112, 68, 29, 12, 1, 1, 1}/256</c>
  1580. <c>Unvoiced</c> <c>{2, 17, 45, 60, 62, 47, 19, 4}/256</c>
  1581. <c>Voiced</c> <c>{1, 3, 26, 71, 94, 50, 9, 2}/256</c>
  1582. </texttable>
  1583. <t>
  1584. The 3 least significant bits are decoded using a uniform PDF:
  1585. </t>
  1586. <texttable anchor="silk_independent_gain_lsb_pdf"
  1587. title="PDF for Independent Quantization Gain LSb Coding">
  1588. <ttcol align="left">PDF</ttcol>
  1589. <c>{32, 32, 32, 32, 32, 32, 32, 32}/256</c>
  1590. </texttable>
  1591. <t>
  1592. For all other subframes (including the first subframe of frames not listed as
  1593. using independent coding above), the quantization gain is coded relative to
  1594. the gain from the previous subframe (in the same channel).
  1595. In particular, unlike an LBRR frame where the previous frame is not coded, in a
  1596. 60&nbsp;ms stereo Opus frame, if the first and third regular SILK frames
  1597. in the side channel are coded, but the second is not, the first subframe of
  1598. the third frame is still coded relative to the last subframe in the first
  1599. frame.
  1600. The PDF in <xref target="silk_delta_gain_pdf"/> yields a delta gain index
  1601. between 0 and 40, inclusive.
  1602. </t>
  1603. <texttable anchor="silk_delta_gain_pdf"
  1604. title="PDF for Delta Quantization Gain Coding">
  1605. <ttcol align="left">PDF</ttcol>
  1606. <c>{6, 5, 11, 31, 132, 21, 8, 4,
  1607. 3, 2, 2, 2, 1, 1, 1, 1,
  1608. 1, 1, 1, 1, 1, 1, 1, 1,
  1609. 1, 1, 1, 1, 1, 1, 1, 1,
  1610. 1, 1, 1, 1, 1, 1, 1, 1, 1}/256</c>
  1611. </texttable>
  1612. <t>
  1613. The following formula translates this index into a quantization gain for the
  1614. current subframe using the gain from the previous subframe:
  1615. </t>
  1616. <figure align="center">
  1617. <artwork align="center"><![CDATA[
  1618. log_gain = min(max(2*gain_index - 16,
  1619. previous_log_gain + gain_index - 4), 63)
  1620. ]]></artwork>
  1621. </figure>
  1622. <t>
  1623. silk_gains_dequant() (silk_gain_quant.c) dequantizes the gain for the
  1624. k'th subframe and converts it into a linear Q16 scale factor via
  1625. <figure align="center">
  1626. <artwork align="center"><![CDATA[
  1627. gain_Q16[k] = silk_log2lin((0x1D1C71*log_gain>>16) + 2090)
  1628. ]]></artwork>
  1629. </figure>
  1630. </t>
  1631. <t>
  1632. The function silk_log2lin() (silk_log2lin.c) computes an approximation of
  1633. of 2**(inLog_Q7/128.0), where inLog_Q7 is its Q7 input.
  1634. Let i = inLog_Q7&gt;&gt;7 be the integer part of inLogQ7 and
  1635. f = inLog_Q7&amp;127 be the fractional part.
  1636. Then, if i &lt; 16, then
  1637. <figure align="center">
  1638. <artwork align="center"><![CDATA[
  1639. (1<<i) + (((-174*f*(128-f)>>16)+f)>>7)*(1<<i)
  1640. ]]></artwork>
  1641. </figure>
  1642. yields the approximate exponential.
  1643. Otherwise, silk_log2lin uses
  1644. <figure align="center">
  1645. <artwork align="center"><![CDATA[
  1646. (1<<i) + ((-174*f*(128-f)>>16)+f)*((1<<i)>>7) .
  1647. ]]></artwork>
  1648. </figure>
  1649. </t>
  1650. </section>
  1651. <section anchor="silk_nlsfs" title="Normalized Line Spectral Frequency (LSF)
  1652. and Linear Predictive Coding (LPC) Coefficients">
  1653. <t>
  1654. Normalized Line Spectral Frequency (LSF) coefficients follow the quantization
  1655. gains in the bitstream, and represent the Linear Predictive Coding (LPC)
  1656. coefficients for the current SILK frame.
  1657. Once decoded, the normalized LSFs form an increasing list of Q15 values between
  1658. 0 and 1.
  1659. These represent the interleaved zeros on the unit circle between 0 and pi
  1660. (hence "normalized") in the standard decomposition of the LPC filter into a
  1661. symmetric part and an anti-symmetric part (P and Q in
  1662. <xref target="silk_nlsf2lpc"/>).
  1663. Because of non-linear effects in the decoding process, an implementation SHOULD
  1664. match the fixed-point arithmetic described in this section exactly.
  1665. An encoder SHOULD also use the same process.
  1666. </t>
  1667. <t>
  1668. The normalized LSFs are coded using a two-stage vector quantizer (VQ)
  1669. (<xref target="silk_nlsf_stage1"/> and <xref target="silk_nlsf_stage2"/>).
  1670. NB and MB frames use an order-10 predictor, while WB frames use an order-16
  1671. predictor, and thus have different sets of tables.
  1672. After reconstructing the normalized LSFs
  1673. (<xref target="silk_nlsf_reconstruction"/>), the decoder runs them through a
  1674. stabilization process (<xref target="silk_nlsf_stabilization"/>), interpolates
  1675. them between frames (<xref target="silk_nlsf_interpolation"/>), converts them
  1676. back into LPC coefficients (<xref target="silk_nlsf2lpc"/>), and then runs
  1677. them through further processes to limit the range of the coefficients
  1678. (<xref target="silk_lpc_range_limit"/>) and the gain of the filter
  1679. (<xref target="silk_lpc_gain_limit"/>).
  1680. All of this is necessary to ensure the reconstruction process is stable.
  1681. </t>
  1682. <section anchor="silk_nlsf_stage1" title="Stage 1 Normalized LSF Decoding">
  1683. <t>
  1684. The first VQ stage uses a 32-element codebook, coded with one of the PDFs in
  1685. <xref target="silk_nlsf_stage1_pdfs"/>, depending on the audio bandwidth and
  1686. the signal type of the current SILK frame.
  1687. This yields a single index, I1, for the entire frame.
  1688. This indexes an element in a coarse codebook, selects the PDFs for the
  1689. second stage of the VQ, and selects the prediction weights used to remove
  1690. intra-frame redundancy from the second stage.
  1691. The actual codebook elements are listed in
  1692. <xref target="silk_nlsf_nbmb_codebook"/> and
  1693. <xref target="silk_nlsf_wb_codebook"/>, but they are not needed until the last
  1694. stages of reconstructing the LSF coefficients.
  1695. </t>
  1696. <texttable anchor="silk_nlsf_stage1_pdfs"
  1697. title="PDFs for Normalized LSF Index Stage-1 Decoding">
  1698. <ttcol align="left">Audio Bandwidth</ttcol>
  1699. <ttcol align="left">Signal Type</ttcol>
  1700. <ttcol align="left">PDF</ttcol>
  1701. <c>NB or MB</c> <c>Inactive or unvoiced</c>
  1702. <c>
  1703. {44, 34, 30, 19, 21, 12, 11, 3,
  1704. 3, 2, 16, 2, 2, 1, 5, 2,
  1705. 1, 3, 3, 1, 1, 2, 2, 2,
  1706. 3, 1, 9, 9, 2, 7, 2, 1}/256
  1707. </c>
  1708. <c>NB or MB</c> <c>Voiced</c>
  1709. <c>
  1710. {1, 10, 1, 8, 3, 8, 8, 14,
  1711. 13, 14, 1, 14, 12, 13, 11, 11,
  1712. 12, 11, 10, 10, 11, 8, 9, 8,
  1713. 7, 8, 1, 1, 6, 1, 6, 5}/256
  1714. </c>
  1715. <c>WB</c> <c>Inactive or unvoiced</c>
  1716. <c>
  1717. {31, 21, 3, 17, 1, 8, 17, 4,
  1718. 1, 18, 16, 4, 2, 3, 1, 10,
  1719. 1, 3, 16, 11, 16, 2, 2, 3,
  1720. 2, 11, 1, 4, 9, 8, 7, 3}/256
  1721. </c>
  1722. <c>WB</c> <c>Voiced</c>
  1723. <c>
  1724. {1, 4, 16, 5, 18, 11, 5, 14,
  1725. 15, 1, 3, 12, 13, 14, 14, 6,
  1726. 14, 12, 2, 6, 1, 12, 12, 11,
  1727. 10, 3, 10, 5, 1, 1, 1, 3}/256
  1728. </c>
  1729. </texttable>
  1730. </section>
  1731. <section anchor="silk_nlsf_stage2" title="Stage 2 Normalized LSF Decoding">
  1732. <t>
  1733. A total of 16 PDFs are available for the LSF residual in the second stage: the
  1734. 8 (a...h) for NB and MB frames given in
  1735. <xref target="silk_nlsf_stage2_nbmb_pdfs"/>, and the 8 (i...p) for WB frames
  1736. given in <xref target="silk_nlsf_stage2_wb_pdfs"/>.
  1737. Which PDF is used for which coefficient is driven by the index, I1,
  1738. decoded in the first stage.
  1739. <xref target="silk_nlsf_nbmb_stage2_cb_sel"/> lists the letter of the
  1740. corresponding PDF for each normalized LSF coefficient for NB and MB, and
  1741. <xref target="silk_nlsf_wb_stage2_cb_sel"/> lists the same information for WB.
  1742. </t>
  1743. <texttable anchor="silk_nlsf_stage2_nbmb_pdfs"
  1744. title="PDFs for NB/MB Normalized LSF Index Stage-2 Decoding">
  1745. <ttcol align="left">Codebook</ttcol>
  1746. <ttcol align="left">PDF</ttcol>
  1747. <c>a</c> <c>{1, 1, 1, 15, 224, 11, 1, 1, 1}/256</c>
  1748. <c>b</c> <c>{1, 1, 2, 34, 183, 32, 1, 1, 1}/256</c>
  1749. <c>c</c> <c>{1, 1, 4, 42, 149, 55, 2, 1, 1}/256</c>
  1750. <c>d</c> <c>{1, 1, 8, 52, 123, 61, 8, 1, 1}/256</c>
  1751. <c>e</c> <c>{1, 3, 16, 53, 101, 74, 6, 1, 1}/256</c>
  1752. <c>f</c> <c>{1, 3, 17, 55, 90, 73, 15, 1, 1}/256</c>
  1753. <c>g</c> <c>{1, 7, 24, 53, 74, 67, 26, 3, 1}/256</c>
  1754. <c>h</c> <c>{1, 1, 18, 63, 78, 58, 30, 6, 1}/256</c>
  1755. </texttable>
  1756. <texttable anchor="silk_nlsf_stage2_wb_pdfs"
  1757. title="PDFs for WB Normalized LSF Index Stage-2 Decoding">
  1758. <ttcol align="left">Codebook</ttcol>
  1759. <ttcol align="left">PDF</ttcol>
  1760. <c>i</c> <c>{1, 1, 1, 9, 232, 9, 1, 1, 1}/256</c>
  1761. <c>j</c> <c>{1, 1, 2, 28, 186, 35, 1, 1, 1}/256</c>
  1762. <c>k</c> <c>{1, 1, 3, 42, 152, 53, 2, 1, 1}/256</c>
  1763. <c>l</c> <c>{1, 1, 10, 49, 126, 65, 2, 1, 1}/256</c>
  1764. <c>m</c> <c>{1, 4, 19, 48, 100, 77, 5, 1, 1}/256</c>
  1765. <c>n</c> <c>{1, 1, 14, 54, 100, 72, 12, 1, 1}/256</c>
  1766. <c>o</c> <c>{1, 1, 15, 61, 87, 61, 25, 4, 1}/256</c>
  1767. <c>p</c> <c>{1, 7, 21, 50, 77, 81, 17, 1, 1}/256</c>
  1768. </texttable>
  1769. <texttable anchor="silk_nlsf_nbmb_stage2_cb_sel"
  1770. title="Codebook Selection for NB/MB Normalized LSF Index Stage 2 Decoding">
  1771. <ttcol>I1</ttcol>
  1772. <ttcol>Coefficient</ttcol>
  1773. <c/>
  1774. <c><spanx style="vbare">0&nbsp;1&nbsp;2&nbsp;3&nbsp;4&nbsp;5&nbsp;6&nbsp;7&nbsp;8&nbsp;9</spanx></c>
  1775. <c> 0</c>
  1776. <c><spanx style="vbare">a&nbsp;a&nbsp;a&nbsp;a&nbsp;a&nbsp;a&nbsp;a&nbsp;a&nbsp;a&nbsp;a</spanx></c>
  1777. <c> 1</c>
  1778. <c><spanx style="vbare">b&nbsp;d&nbsp;b&nbsp;c&nbsp;c&nbsp;b&nbsp;c&nbsp;b&nbsp;b&nbsp;b</spanx></c>
  1779. <c> 2</c>
  1780. <c><spanx style="vbare">c&nbsp;b&nbsp;b&nbsp;b&nbsp;b&nbsp;b&nbsp;b&nbsp;b&nbsp;b&nbsp;b</spanx></c>
  1781. <c> 3</c>
  1782. <c><spanx style="vbare">b&nbsp;c&nbsp;c&nbsp;c&nbsp;c&nbsp;b&nbsp;c&nbsp;b&nbsp;b&nbsp;b</spanx></c>
  1783. <c> 4</c>
  1784. <c><spanx style="vbare">c&nbsp;d&nbsp;d&nbsp;d&nbsp;d&nbsp;c&nbsp;c&nbsp;c&nbsp;c&nbsp;c</spanx></c>
  1785. <c> 5</c>
  1786. <c><spanx style="vbare">a&nbsp;f&nbsp;d&nbsp;d&nbsp;c&nbsp;c&nbsp;c&nbsp;c&nbsp;b&nbsp;b</spanx></c>
  1787. <c> g</c>
  1788. <c><spanx style="vbare">a&nbsp;c&nbsp;c&nbsp;c&nbsp;c&nbsp;c&nbsp;c&nbsp;c&nbsp;c&nbsp;b</spanx></c>
  1789. <c> 7</c>
  1790. <c><spanx style="vbare">c&nbsp;d&nbsp;g&nbsp;e&nbsp;e&nbsp;e&nbsp;f&nbsp;e&nbsp;f&nbsp;f</spanx></c>
  1791. <c> 8</c>
  1792. <c><spanx style="vbare">c&nbsp;e&nbsp;f&nbsp;f&nbsp;e&nbsp;f&nbsp;e&nbsp;g&nbsp;e&nbsp;e</spanx></c>
  1793. <c> 9</c>
  1794. <c><spanx style="vbare">c&nbsp;e&nbsp;e&nbsp;h&nbsp;e&nbsp;f&nbsp;e&nbsp;f&nbsp;f&nbsp;e</spanx></c>
  1795. <c>10</c>
  1796. <c><spanx style="vbare">e&nbsp;d&nbsp;d&nbsp;d&nbsp;c&nbsp;d&nbsp;c&nbsp;c&nbsp;c&nbsp;c</spanx></c>
  1797. <c>11</c>
  1798. <c><spanx style="vbare">b&nbsp;f&nbsp;f&nbsp;g&nbsp;e&nbsp;f&nbsp;e&nbsp;f&nbsp;f&nbsp;f</spanx></c>
  1799. <c>12</c>
  1800. <c><spanx style="vbare">c&nbsp;h&nbsp;e&nbsp;g&nbsp;f&nbsp;f&nbsp;f&nbsp;f&nbsp;f&nbsp;f</spanx></c>
  1801. <c>13</c>
  1802. <c><spanx style="vbare">c&nbsp;h&nbsp;f&nbsp;f&nbsp;f&nbsp;f&nbsp;f&nbsp;g&nbsp;f&nbsp;e</spanx></c>
  1803. <c>14</c>
  1804. <c><spanx style="vbare">d&nbsp;d&nbsp;f&nbsp;e&nbsp;e&nbsp;f&nbsp;e&nbsp;f&nbsp;e&nbsp;e</spanx></c>
  1805. <c>15</c>
  1806. <c><spanx style="vbare">c&nbsp;d&nbsp;d&nbsp;f&nbsp;f&nbsp;e&nbsp;e&nbsp;e&nbsp;e&nbsp;e</spanx></c>
  1807. <c>16</c>
  1808. <c><spanx style="vbare">c&nbsp;e&nbsp;e&nbsp;g&nbsp;e&nbsp;f&nbsp;e&nbsp;f&nbsp;f&nbsp;f</spanx></c>
  1809. <c>17</c>
  1810. <c><spanx style="vbare">c&nbsp;f&nbsp;e&nbsp;g&nbsp;f&nbsp;f&nbsp;f&nbsp;e&nbsp;f&nbsp;e</spanx></c>
  1811. <c>18</c>
  1812. <c><spanx style="vbare">c&nbsp;h&nbsp;e&nbsp;f&nbsp;e&nbsp;f&nbsp;e&nbsp;f&nbsp;f&nbsp;f</spanx></c>
  1813. <c>19</c>
  1814. <c><spanx style="vbare">c&nbsp;f&nbsp;e&nbsp;g&nbsp;h&nbsp;g&nbsp;f&nbsp;g&nbsp;f&nbsp;e</spanx></c>
  1815. <c>20</c>
  1816. <c><spanx style="vbare">d&nbsp;g&nbsp;h&nbsp;e&nbsp;g&nbsp;f&nbsp;f&nbsp;g&nbsp;e&nbsp;f</spanx></c>
  1817. <c>21</c>
  1818. <c><spanx style="vbare">c&nbsp;h&nbsp;g&nbsp;e&nbsp;e&nbsp;e&nbsp;f&nbsp;e&nbsp;f&nbsp;f</spanx></c>
  1819. <c>22</c>
  1820. <c><spanx style="vbare">e&nbsp;f&nbsp;f&nbsp;e&nbsp;g&nbsp;g&nbsp;f&nbsp;g&nbsp;f&nbsp;e</spanx></c>
  1821. <c>23</c>
  1822. <c><spanx style="vbare">c&nbsp;f&nbsp;f&nbsp;g&nbsp;f&nbsp;g&nbsp;e&nbsp;g&nbsp;e&nbsp;e</spanx></c>
  1823. <c>24</c>
  1824. <c><spanx style="vbare">e&nbsp;f&nbsp;f&nbsp;f&nbsp;d&nbsp;h&nbsp;e&nbsp;f&nbsp;f&nbsp;e</spanx></c>
  1825. <c>25</c>
  1826. <c><spanx style="vbare">c&nbsp;d&nbsp;e&nbsp;f&nbsp;f&nbsp;g&nbsp;e&nbsp;f&nbsp;f&nbsp;e</spanx></c>
  1827. <c>26</c>
  1828. <c><spanx style="vbare">c&nbsp;d&nbsp;c&nbsp;d&nbsp;d&nbsp;e&nbsp;c&nbsp;d&nbsp;d&nbsp;d</spanx></c>
  1829. <c>27</c>
  1830. <c><spanx style="vbare">b&nbsp;b&nbsp;c&nbsp;c&nbsp;c&nbsp;c&nbsp;c&nbsp;d&nbsp;c&nbsp;c</spanx></c>
  1831. <c>28</c>
  1832. <c><spanx style="vbare">e&nbsp;f&nbsp;f&nbsp;g&nbsp;g&nbsp;g&nbsp;f&nbsp;g&nbsp;e&nbsp;f</spanx></c>
  1833. <c>29</c>
  1834. <c><spanx style="vbare">d&nbsp;f&nbsp;f&nbsp;e&nbsp;e&nbsp;e&nbsp;e&nbsp;d&nbsp;d&nbsp;c</spanx></c>
  1835. <c>30</c>
  1836. <c><spanx style="vbare">c&nbsp;f&nbsp;d&nbsp;h&nbsp;f&nbsp;f&nbsp;e&nbsp;e&nbsp;f&nbsp;e</spanx></c>
  1837. <c>31</c>
  1838. <c><spanx style="vbare">e&nbsp;e&nbsp;f&nbsp;e&nbsp;f&nbsp;g&nbsp;f&nbsp;g&nbsp;f&nbsp;e</spanx></c>
  1839. </texttable>
  1840. <texttable anchor="silk_nlsf_wb_stage2_cb_sel"
  1841. title="Codebook Selection for WB Normalized LSF Index Stage 2 Decoding">
  1842. <ttcol>I1</ttcol>
  1843. <ttcol>Coefficient</ttcol>
  1844. <c/>
  1845. <c><spanx style="vbare">0&nbsp;&nbsp;1&nbsp;&nbsp;2&nbsp;&nbsp;3&nbsp;&nbsp;4&nbsp;&nbsp;5&nbsp;&nbsp;6&nbsp;&nbsp;7&nbsp;&nbsp;8&nbsp;&nbsp;9&nbsp;10&nbsp;11&nbsp;12&nbsp;13&nbsp;14&nbsp;15</spanx></c>
  1846. <c> 0</c>
  1847. <c><spanx style="vbare">i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i</spanx></c>
  1848. <c> 1</c>
  1849. <c><spanx style="vbare">k&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;l</spanx></c>
  1850. <c> 2</c>
  1851. <c><spanx style="vbare">k&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;p&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;k&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;l</spanx></c>
  1852. <c> 3</c>
  1853. <c><spanx style="vbare">i&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;j</spanx></c>
  1854. <c> 4</c>
  1855. <c><spanx style="vbare">i&nbsp;&nbsp;o&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;o&nbsp;&nbsp;m&nbsp;&nbsp;p&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;l</spanx></c>
  1856. <c> 5</c>
  1857. <c><spanx style="vbare">i&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;m</spanx></c>
  1858. <c> 6</c>
  1859. <c><spanx style="vbare">i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i</spanx></c>
  1860. <c> 7</c>
  1861. <c><spanx style="vbare">i&nbsp;&nbsp;k&nbsp;&nbsp;o&nbsp;&nbsp;l&nbsp;&nbsp;p&nbsp;&nbsp;k&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;l</spanx></c>
  1862. <c> 8</c>
  1863. <c><spanx style="vbare">i&nbsp;&nbsp;o&nbsp;&nbsp;k&nbsp;&nbsp;o&nbsp;&nbsp;o&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;o&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l</spanx></c>
  1864. <c> 9</c>
  1865. <c><spanx style="vbare">k&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i</spanx></c>
  1866. <c>j0</c>
  1867. <c><spanx style="vbare">i&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;j</spanx></c>
  1868. <c>11</c>
  1869. <c><spanx style="vbare">k&nbsp;&nbsp;k&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;l</spanx></c>
  1870. <c>12</c>
  1871. <c><spanx style="vbare">k&nbsp;&nbsp;k&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;l</spanx></c>
  1872. <c>13</c>
  1873. <c><spanx style="vbare">l&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;o&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;m</spanx></c>
  1874. <c>14</c>
  1875. <c><spanx style="vbare">i&nbsp;&nbsp;o&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;p&nbsp;&nbsp;n&nbsp;&nbsp;k&nbsp;&nbsp;o&nbsp;&nbsp;n&nbsp;&nbsp;p&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;l</spanx></c>
  1876. <c>15</c>
  1877. <c><spanx style="vbare">i&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;j&nbsp;&nbsp;i</spanx></c>
  1878. <c>16</c>
  1879. <c><spanx style="vbare">j&nbsp;&nbsp;o&nbsp;&nbsp;n&nbsp;&nbsp;p&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;m</spanx></c>
  1880. <c>17</c>
  1881. <c><spanx style="vbare">j&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;k&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;m</spanx></c>
  1882. <c>18</c>
  1883. <c><spanx style="vbare">k&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;m</spanx></c>
  1884. <c>19</c>
  1885. <c><spanx style="vbare">i&nbsp;&nbsp;k&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i</spanx></c>
  1886. <c>20</c>
  1887. <c><spanx style="vbare">l&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;m</spanx></c>
  1888. <c>21</c>
  1889. <c><spanx style="vbare">k&nbsp;&nbsp;o&nbsp;&nbsp;l&nbsp;&nbsp;p&nbsp;&nbsp;p&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;l&nbsp;&nbsp;l</spanx></c>
  1890. <c>22</c>
  1891. <c><spanx style="vbare">k&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;o&nbsp;&nbsp;o&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;m</spanx></c>
  1892. <c>23</c>
  1893. <c><spanx style="vbare">j&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;j</spanx></c>
  1894. <c>24</c>
  1895. <c><spanx style="vbare">k&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;o&nbsp;&nbsp;o&nbsp;&nbsp;m&nbsp;&nbsp;p&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l</spanx></c>
  1896. <c>25</c>
  1897. <c><spanx style="vbare">i&nbsp;&nbsp;o&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i</spanx></c>
  1898. <c>26</c>
  1899. <c><spanx style="vbare">i&nbsp;&nbsp;o&nbsp;&nbsp;o&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;k&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;p&nbsp;&nbsp;p&nbsp;&nbsp;m&nbsp;&nbsp;m&nbsp;&nbsp;m</spanx></c>
  1900. <c>27</c>
  1901. <c><spanx style="vbare">l&nbsp;&nbsp;l&nbsp;&nbsp;p&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;l</spanx></c>
  1902. <c>28</c>
  1903. <c><spanx style="vbare">i&nbsp;&nbsp;i&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;j&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;j</spanx></c>
  1904. <c>29</c>
  1905. <c><spanx style="vbare">i&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;j</spanx></c>
  1906. <c>30</c>
  1907. <c><spanx style="vbare">l&nbsp;&nbsp;n&nbsp;&nbsp;n&nbsp;&nbsp;m&nbsp;&nbsp;p&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;i&nbsp;&nbsp;j&nbsp;&nbsp;i</spanx></c>
  1908. <c>31</c>
  1909. <c><spanx style="vbare">k&nbsp;&nbsp;l&nbsp;&nbsp;n&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;l&nbsp;&nbsp;k&nbsp;&nbsp;j&nbsp;&nbsp;k&nbsp;&nbsp;o&nbsp;&nbsp;m&nbsp;&nbsp;i&nbsp;&nbsp;i&nbsp;&nbsp;i</spanx></c>
  1910. </texttable>
  1911. <t>
  1912. Decoding the second stage residual proceeds as follows.
  1913. For each coefficient, the decoder reads a symbol using the PDF corresponding to
  1914. I1 from either <xref target="silk_nlsf_nbmb_stage2_cb_sel"/> or
  1915. <xref target="silk_nlsf_wb_stage2_cb_sel"/>, and subtracts 4 from the result
  1916. to give an index in the range -4 to 4, inclusive.
  1917. If the index is either -4 or 4, it reads a second symbol using the PDF in
  1918. <xref target="silk_nlsf_ext_pdf"/>, and adds the value of this second symbol
  1919. to the index, using the same sign.
  1920. This gives the index, I2[k], a total range of -10 to 10, inclusive.
  1921. </t>
  1922. <texttable anchor="silk_nlsf_ext_pdf"
  1923. title="PDF for Normalized LSF Index Extension Decoding">
  1924. <ttcol align="left">PDF</ttcol>
  1925. <c>{156, 60, 24, 9, 4, 2, 1}/256</c>
  1926. </texttable>
  1927. <t>
  1928. The decoded indices from both stages are translated back into normalized LSF
  1929. coefficients in silk_NLSF_decode() (silk_NLSF_decode.c).
  1930. The stage-2 indices represent residuals after both the first stage of the VQ
  1931. and a separate backwards-prediction step.
  1932. The backwards prediction process in the encoder subtracts a prediction from
  1933. each residual formed by a multiple of the coefficient that follows it.
  1934. The decoder must undo this process.
  1935. <xref target="silk_nlsf_pred_weights"/> contains lists of prediction weights
  1936. for each coefficient.
  1937. There are two lists for NB and MB, and another two lists for WB, giving two
  1938. possible prediction weights for each coefficient.
  1939. </t>
  1940. <texttable anchor="silk_nlsf_pred_weights"
  1941. title="Prediction Weights for Normalized LSF Decoding">
  1942. <ttcol align="left">Coefficient</ttcol>
  1943. <ttcol align="right">A</ttcol>
  1944. <ttcol align="right">B</ttcol>
  1945. <ttcol align="right">C</ttcol>
  1946. <ttcol align="right">D</ttcol>
  1947. <c>0</c> <c>179</c> <c>116</c> <c>175</c> <c>68</c>
  1948. <c>1</c> <c>138</c> <c>67</c> <c>148</c> <c>62</c>
  1949. <c>2</c> <c>140</c> <c>82</c> <c>160</c> <c>66</c>
  1950. <c>3</c> <c>148</c> <c>59</c> <c>176</c> <c>60</c>
  1951. <c>4</c> <c>151</c> <c>92</c> <c>178</c> <c>72</c>
  1952. <c>5</c> <c>149</c> <c>72</c> <c>173</c> <c>117</c>
  1953. <c>6</c> <c>153</c> <c>100</c> <c>174</c> <c>85</c>
  1954. <c>7</c> <c>151</c> <c>89</c> <c>164</c> <c>90</c>
  1955. <c>8</c> <c>163</c> <c>92</c> <c>177</c> <c>118</c>
  1956. <c>9</c> <c/> <c/> <c>174</c> <c>136</c>
  1957. <c>10</c> <c/> <c/> <c>196</c> <c>151</c>
  1958. <c>11</c> <c/> <c/> <c>182</c> <c>142</c>
  1959. <c>12</c> <c/> <c/> <c>198</c> <c>160</c>
  1960. <c>13</c> <c/> <c/> <c>192</c> <c>142</c>
  1961. <c>14</c> <c/> <c/> <c>182</c> <c>155</c>
  1962. </texttable>
  1963. <t>
  1964. The prediction is undone using the procedure implemented in
  1965. silk_NLSF_residual_dequant() (silk_NLSF_decode.c), which is as follows.
  1966. Each coefficient selects its prediction weight from one of the two lists based
  1967. on the stage-1 index, I1.
  1968. <xref target="silk_nlsf_nbmb_weight_sel"/> gives the selections for each
  1969. coefficient for NB and MB, and <xref target="silk_nlsf_wb_weight_sel"/> gives
  1970. the selections for WB.
  1971. Let d_LPC be the order of the codebook, i.e., 10 for NB and MB, and 16 for WB,
  1972. and let pred_Q8[k] be the weight for the k'th coefficient selected by this
  1973. process for 0&nbsp;&lt;=&nbsp;k&nbsp;&lt;&nbsp;d_LPC-1.
  1974. Then, the stage-2 residual for each coefficient is computed via
  1975. <figure align="center">
  1976. <artwork align="center"><![CDATA[
  1977. res_Q10[k] = (k+1 < d_LPC ? (res_Q10[k+1]*pred_Q8[k])>>8 : 0)
  1978. + ((((I2[k]<<10) + sign(I2[k])*102)*qstep)>>16) ,
  1979. ]]></artwork>
  1980. </figure>
  1981. where qstep is the Q16 quantization step size, which is 11796 for NB and MB
  1982. and 9830 for WB (representing step sizes of approximately 0.18 and 0.15,
  1983. respectively).
  1984. </t>
  1985. <texttable anchor="silk_nlsf_nbmb_weight_sel"
  1986. title="Prediction Weight Selection for NB/MB Normalized LSF Decoding">
  1987. <ttcol>I1</ttcol>
  1988. <ttcol>Coefficient</ttcol>
  1989. <c/>
  1990. <c><spanx style="vbare">0&nbsp;1&nbsp;2&nbsp;3&nbsp;4&nbsp;5&nbsp;6&nbsp;7&nbsp;8</spanx></c>
  1991. <c> 0</c>
  1992. <c><spanx style="vbare">A&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  1993. <c> 1</c>
  1994. <c><spanx style="vbare">B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  1995. <c> 2</c>
  1996. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  1997. <c> 3</c>
  1998. <c><spanx style="vbare">B&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  1999. <c> 4</c>
  2000. <c><spanx style="vbare">A&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  2001. <c> 5</c>
  2002. <c><spanx style="vbare">A&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  2003. <c> 6</c>
  2004. <c><spanx style="vbare">B&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2005. <c> 7</c>
  2006. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;A</spanx></c>
  2007. <c> 8</c>
  2008. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A&nbsp;B&nbsp;B</spanx></c>
  2009. <c> 9</c>
  2010. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2011. <c>10</c>
  2012. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  2013. <c>11</c>
  2014. <c><spanx style="vbare">A&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2015. <c>12</c>
  2016. <c><spanx style="vbare">A&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2017. <c>13</c>
  2018. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2019. <c>14</c>
  2020. <c><spanx style="vbare">B&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2021. <c>15</c>
  2022. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2023. <c>16</c>
  2024. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2025. <c>17</c>
  2026. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2027. <c>18</c>
  2028. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2029. <c>19</c>
  2030. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2031. <c>20</c>
  2032. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;B&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2033. <c>21</c>
  2034. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2035. <c>22</c>
  2036. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2037. <c>23</c>
  2038. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;B&nbsp;B</spanx></c>
  2039. <c>24</c>
  2040. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2041. <c>25</c>
  2042. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2043. <c>26</c>
  2044. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  2045. <c>27</c>
  2046. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  2047. <c>28</c>
  2048. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2049. <c>29</c>
  2050. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  2051. <c>30</c>
  2052. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A&nbsp;B</spanx></c>
  2053. <c>31</c>
  2054. <c><spanx style="vbare">B&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2055. </texttable>
  2056. <texttable anchor="silk_nlsf_wb_weight_sel"
  2057. title="Prediction Weight Selection for WB Normalized LSF Decoding">
  2058. <ttcol>I1</ttcol>
  2059. <ttcol>Coefficient</ttcol>
  2060. <c/>
  2061. <c><spanx style="vbare">0&nbsp;&nbsp;1&nbsp;&nbsp;2&nbsp;&nbsp;3&nbsp;&nbsp;4&nbsp;&nbsp;5&nbsp;&nbsp;6&nbsp;&nbsp;7&nbsp;&nbsp;8&nbsp;&nbsp;9&nbsp;10&nbsp;11&nbsp;12&nbsp;13&nbsp;14</spanx></c>
  2062. <c> 0</c>
  2063. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D</spanx></c>
  2064. <c> 1</c>
  2065. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2066. <c> 2</c>
  2067. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2068. <c> 3</c>
  2069. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2070. <c> 4</c>
  2071. <c><spanx style="vbare">C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C</spanx></c>
  2072. <c> 5</c>
  2073. <c><spanx style="vbare">C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2074. <c> 6</c>
  2075. <c><spanx style="vbare">D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C</spanx></c>
  2076. <c> 7</c>
  2077. <c><spanx style="vbare">C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D</spanx></c>
  2078. <c> 8</c>
  2079. <c><spanx style="vbare">C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D</spanx></c>
  2080. <c> 9</c>
  2081. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D</spanx></c>
  2082. <c>10</c>
  2083. <c><spanx style="vbare">C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2084. <c>11</c>
  2085. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2086. <c>12</c>
  2087. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2088. <c>13</c>
  2089. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2090. <c>14</c>
  2091. <c><spanx style="vbare">C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D</spanx></c>
  2092. <c>15</c>
  2093. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C</spanx></c>
  2094. <c>16</c>
  2095. <c><spanx style="vbare">C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2096. <c>17</c>
  2097. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2098. <c>18</c>
  2099. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D</spanx></c>
  2100. <c>19</c>
  2101. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2102. <c>20</c>
  2103. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2104. <c>21</c>
  2105. <c><spanx style="vbare">C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C</spanx></c>
  2106. <c>22</c>
  2107. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2108. <c>23</c>
  2109. <c><spanx style="vbare">C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C</spanx></c>
  2110. <c>24</c>
  2111. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D</spanx></c>
  2112. <c>25</c>
  2113. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D</spanx></c>
  2114. <c>26</c>
  2115. <c><spanx style="vbare">C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D</spanx></c>
  2116. <c>27</c>
  2117. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D</spanx></c>
  2118. <c>28</c>
  2119. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D</spanx></c>
  2120. <c>29</c>
  2121. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D</spanx></c>
  2122. <c>30</c>
  2123. <c><spanx style="vbare">D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;C</spanx></c>
  2124. <c>31</c>
  2125. <c><spanx style="vbare">C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C&nbsp;&nbsp;C&nbsp;&nbsp;D&nbsp;&nbsp;C</spanx></c>
  2126. </texttable>
  2127. </section>
  2128. <section anchor="silk_nlsf_reconstruction"
  2129. title="Reconstructing the Normalized LSF Coefficients">
  2130. <t>
  2131. Once the stage-1 index I1 and the stage-2 residual res_Q10[] have been decoded,
  2132. the final normalized LSF coefficients can be reconstructed.
  2133. </t>
  2134. <t>
  2135. The spectral distortion introduced by the quantization of each LSF coefficient
  2136. varies, so the stage-2 residual is weighted accordingly, using the
  2137. low-complexity Inverse Harmonic Mean Weighting (IHMW) function proposed in
  2138. <xref target="laroia-icassp"/>.
  2139. The weights are derived directly from the stage-1 codebook vector.
  2140. Let cb1_Q8[k] be the k'th entry of the stage-1 codebook vector from
  2141. <xref target="silk_nlsf_nbmb_codebook"/> or
  2142. <xref target="silk_nlsf_wb_codebook"/>.
  2143. Then for 0&nbsp;&lt;=&nbsp;k&nbsp;&lt;&nbsp;d_LPC the following expression
  2144. computes the square of the weight as a Q18 value:
  2145. <figure align="center">
  2146. <artwork align="center">
  2147. <![CDATA[
  2148. w2_Q18[k] = (1024/(cb1_Q8[k] - cb1_Q8[k-1])
  2149. + 1024/(cb1_Q8[k+1] - cb1_Q8[k])) << 16 ,
  2150. ]]>
  2151. </artwork>
  2152. </figure>
  2153. where cb1_Q8[-1]&nbsp;=&nbsp;0 and cb1_Q8[d_LPC]&nbsp;=&nbsp;256, and the
  2154. division is exact integer division.
  2155. This is reduced to an unsquared, Q9 value using the following square-root
  2156. approximation:
  2157. <figure align="center">
  2158. <artwork align="center"><![CDATA[
  2159. i = ilog(w2_Q18[k])
  2160. f = (w2_Q18[k]>>(i-8)) & 127
  2161. y = ((i&1) ? 32768 : 46214) >> ((32-i)>>1)
  2162. w_Q9[k] = y + ((213*f*y)>>16)
  2163. ]]></artwork>
  2164. </figure>
  2165. The cb1_Q8[] vector completely determines these weights, and they may be
  2166. tabulated and stored as 13-bit unsigned values (with a range of 1819 to 5227,
  2167. inclusive) to avoid computing them when decoding.
  2168. The reference implementation already requires code to compute these weights on
  2169. unquantized coefficients in the encoder, in silk_NLSF_VQ_weights_laroia()
  2170. (silk_NLSF_VQ_weights_laroia.c) and its callers, so it reuses that code in the
  2171. decoder instead of using a pre-computed table to reduce the amount of ROM
  2172. required.
  2173. </t>
  2174. <texttable anchor="silk_nlsf_nbmb_codebook"
  2175. title="Codebook Vectors for NB/MB Normalized LSF Stage 1 Decoding">
  2176. <ttcol>I1</ttcol>
  2177. <ttcol>Codebook (Q8)</ttcol>
  2178. <c/>
  2179. <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;9</spanx></c>
  2180. <c>0</c>
  2181. <c><spanx style="vbare">12&nbsp;&nbsp;35&nbsp;&nbsp;60&nbsp;&nbsp;83&nbsp;108&nbsp;132&nbsp;157&nbsp;180&nbsp;206&nbsp;228</spanx></c>
  2182. <c>1</c>
  2183. <c><spanx style="vbare">15&nbsp;&nbsp;32&nbsp;&nbsp;55&nbsp;&nbsp;77&nbsp;101&nbsp;125&nbsp;151&nbsp;175&nbsp;201&nbsp;225</spanx></c>
  2184. <c>2</c>
  2185. <c><spanx style="vbare">19&nbsp;&nbsp;42&nbsp;&nbsp;66&nbsp;&nbsp;89&nbsp;114&nbsp;137&nbsp;162&nbsp;184&nbsp;209&nbsp;230</spanx></c>
  2186. <c>3</c>
  2187. <c><spanx style="vbare">12&nbsp;&nbsp;25&nbsp;&nbsp;50&nbsp;&nbsp;72&nbsp;&nbsp;97&nbsp;120&nbsp;147&nbsp;172&nbsp;200&nbsp;223</spanx></c>
  2188. <c>4</c>
  2189. <c><spanx style="vbare">26&nbsp;&nbsp;44&nbsp;&nbsp;69&nbsp;&nbsp;90&nbsp;114&nbsp;135&nbsp;159&nbsp;180&nbsp;205&nbsp;225</spanx></c>
  2190. <c>5</c>
  2191. <c><spanx style="vbare">13&nbsp;&nbsp;22&nbsp;&nbsp;53&nbsp;&nbsp;80&nbsp;106&nbsp;130&nbsp;156&nbsp;180&nbsp;205&nbsp;228</spanx></c>
  2192. <c>6</c>
  2193. <c><spanx style="vbare">15&nbsp;&nbsp;25&nbsp;&nbsp;44&nbsp;&nbsp;64&nbsp;&nbsp;90&nbsp;115&nbsp;142&nbsp;168&nbsp;196&nbsp;222</spanx></c>
  2194. <c>7</c>
  2195. <c><spanx style="vbare">19&nbsp;&nbsp;24&nbsp;&nbsp;62&nbsp;&nbsp;82&nbsp;100&nbsp;120&nbsp;145&nbsp;168&nbsp;190&nbsp;214</spanx></c>
  2196. <c>8</c>
  2197. <c><spanx style="vbare">22&nbsp;&nbsp;31&nbsp;&nbsp;50&nbsp;&nbsp;79&nbsp;103&nbsp;120&nbsp;151&nbsp;170&nbsp;203&nbsp;227</spanx></c>
  2198. <c>9</c>
  2199. <c><spanx style="vbare">21&nbsp;&nbsp;29&nbsp;&nbsp;45&nbsp;&nbsp;65&nbsp;106&nbsp;124&nbsp;150&nbsp;171&nbsp;196&nbsp;224</spanx></c>
  2200. <c>10</c>
  2201. <c><spanx style="vbare">30&nbsp;&nbsp;49&nbsp;&nbsp;75&nbsp;&nbsp;97&nbsp;121&nbsp;142&nbsp;165&nbsp;186&nbsp;209&nbsp;229</spanx></c>
  2202. <c>11</c>
  2203. <c><spanx style="vbare">19&nbsp;&nbsp;25&nbsp;&nbsp;52&nbsp;&nbsp;70&nbsp;&nbsp;93&nbsp;116&nbsp;143&nbsp;166&nbsp;192&nbsp;219</spanx></c>
  2204. <c>12</c>
  2205. <c><spanx style="vbare">26&nbsp;&nbsp;34&nbsp;&nbsp;62&nbsp;&nbsp;75&nbsp;&nbsp;97&nbsp;118&nbsp;145&nbsp;167&nbsp;194&nbsp;217</spanx></c>
  2206. <c>13</c>
  2207. <c><spanx style="vbare">25&nbsp;&nbsp;33&nbsp;&nbsp;56&nbsp;&nbsp;70&nbsp;&nbsp;91&nbsp;113&nbsp;143&nbsp;165&nbsp;196&nbsp;223</spanx></c>
  2208. <c>14</c>
  2209. <c><spanx style="vbare">21&nbsp;&nbsp;34&nbsp;&nbsp;51&nbsp;&nbsp;72&nbsp;&nbsp;97&nbsp;117&nbsp;145&nbsp;171&nbsp;196&nbsp;222</spanx></c>
  2210. <c>15</c>
  2211. <c><spanx style="vbare">20&nbsp;&nbsp;29&nbsp;&nbsp;50&nbsp;&nbsp;67&nbsp;&nbsp;90&nbsp;117&nbsp;144&nbsp;168&nbsp;197&nbsp;221</spanx></c>
  2212. <c>16</c>
  2213. <c><spanx style="vbare">22&nbsp;&nbsp;31&nbsp;&nbsp;48&nbsp;&nbsp;66&nbsp;&nbsp;95&nbsp;117&nbsp;146&nbsp;168&nbsp;196&nbsp;222</spanx></c>
  2214. <c>17</c>
  2215. <c><spanx style="vbare">24&nbsp;&nbsp;33&nbsp;&nbsp;51&nbsp;&nbsp;77&nbsp;116&nbsp;134&nbsp;158&nbsp;180&nbsp;200&nbsp;224</spanx></c>
  2216. <c>18</c>
  2217. <c><spanx style="vbare">21&nbsp;&nbsp;28&nbsp;&nbsp;70&nbsp;&nbsp;87&nbsp;106&nbsp;124&nbsp;149&nbsp;170&nbsp;194&nbsp;217</spanx></c>
  2218. <c>19</c>
  2219. <c><spanx style="vbare">26&nbsp;&nbsp;33&nbsp;&nbsp;53&nbsp;&nbsp;64&nbsp;&nbsp;83&nbsp;117&nbsp;152&nbsp;173&nbsp;204&nbsp;225</spanx></c>
  2220. <c>20</c>
  2221. <c><spanx style="vbare">27&nbsp;&nbsp;34&nbsp;&nbsp;65&nbsp;&nbsp;95&nbsp;108&nbsp;129&nbsp;155&nbsp;174&nbsp;210&nbsp;225</spanx></c>
  2222. <c>21</c>
  2223. <c><spanx style="vbare">20&nbsp;&nbsp;26&nbsp;&nbsp;72&nbsp;&nbsp;99&nbsp;113&nbsp;131&nbsp;154&nbsp;176&nbsp;200&nbsp;219</spanx></c>
  2224. <c>22</c>
  2225. <c><spanx style="vbare">34&nbsp;&nbsp;43&nbsp;&nbsp;61&nbsp;&nbsp;78&nbsp;&nbsp;93&nbsp;114&nbsp;155&nbsp;177&nbsp;205&nbsp;229</spanx></c>
  2226. <c>23</c>
  2227. <c><spanx style="vbare">23&nbsp;&nbsp;29&nbsp;&nbsp;54&nbsp;&nbsp;97&nbsp;124&nbsp;138&nbsp;163&nbsp;179&nbsp;209&nbsp;229</spanx></c>
  2228. <c>24</c>
  2229. <c><spanx style="vbare">30&nbsp;&nbsp;38&nbsp;&nbsp;56&nbsp;&nbsp;89&nbsp;118&nbsp;129&nbsp;158&nbsp;178&nbsp;200&nbsp;231</spanx></c>
  2230. <c>25</c>
  2231. <c><spanx style="vbare">21&nbsp;&nbsp;29&nbsp;&nbsp;49&nbsp;&nbsp;63&nbsp;&nbsp;85&nbsp;111&nbsp;142&nbsp;163&nbsp;193&nbsp;222</spanx></c>
  2232. <c>26</c>
  2233. <c><spanx style="vbare">27&nbsp;&nbsp;48&nbsp;&nbsp;77&nbsp;103&nbsp;133&nbsp;158&nbsp;179&nbsp;196&nbsp;215&nbsp;232</spanx></c>
  2234. <c>27</c>
  2235. <c><spanx style="vbare">29&nbsp;&nbsp;47&nbsp;&nbsp;74&nbsp;&nbsp;99&nbsp;124&nbsp;151&nbsp;176&nbsp;198&nbsp;220&nbsp;237</spanx></c>
  2236. <c>28</c>
  2237. <c><spanx style="vbare">33&nbsp;&nbsp;42&nbsp;&nbsp;61&nbsp;&nbsp;76&nbsp;&nbsp;93&nbsp;121&nbsp;155&nbsp;174&nbsp;207&nbsp;225</spanx></c>
  2238. <c>29</c>
  2239. <c><spanx style="vbare">29&nbsp;&nbsp;53&nbsp;&nbsp;87&nbsp;112&nbsp;136&nbsp;154&nbsp;170&nbsp;188&nbsp;208&nbsp;227</spanx></c>
  2240. <c>30</c>
  2241. <c><spanx style="vbare">24&nbsp;&nbsp;30&nbsp;&nbsp;52&nbsp;&nbsp;84&nbsp;131&nbsp;150&nbsp;166&nbsp;186&nbsp;203&nbsp;229</spanx></c>
  2242. <c>31</c>
  2243. <c><spanx style="vbare">37&nbsp;&nbsp;48&nbsp;&nbsp;64&nbsp;&nbsp;84&nbsp;104&nbsp;118&nbsp;156&nbsp;177&nbsp;201&nbsp;230</spanx></c>
  2244. </texttable>
  2245. <texttable anchor="silk_nlsf_wb_codebook"
  2246. title="Codebook Vectors for WB Normalized LSF Stage 1 Decoding">
  2247. <ttcol>I1</ttcol>
  2248. <ttcol>Codebook (Q8)</ttcol>
  2249. <c/>
  2250. <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;2&nbsp;&nbsp;3&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;10&nbsp;&nbsp;11&nbsp;&nbsp;12&nbsp;&nbsp;13&nbsp;&nbsp;14&nbsp;&nbsp;15</spanx></c>
  2251. <c>0</c>
  2252. <c><spanx style="vbare">&nbsp;7&nbsp;23&nbsp;38&nbsp;54&nbsp;69&nbsp;&nbsp;85&nbsp;100&nbsp;116&nbsp;131&nbsp;147&nbsp;162&nbsp;178&nbsp;193&nbsp;208&nbsp;223&nbsp;239</spanx></c>
  2253. <c>1</c>
  2254. <c><spanx style="vbare">13&nbsp;25&nbsp;41&nbsp;55&nbsp;69&nbsp;&nbsp;83&nbsp;&nbsp;98&nbsp;112&nbsp;127&nbsp;142&nbsp;157&nbsp;171&nbsp;187&nbsp;203&nbsp;220&nbsp;236</spanx></c>
  2255. <c>2</c>
  2256. <c><spanx style="vbare">15&nbsp;21&nbsp;34&nbsp;51&nbsp;61&nbsp;&nbsp;78&nbsp;&nbsp;92&nbsp;106&nbsp;126&nbsp;136&nbsp;152&nbsp;167&nbsp;185&nbsp;205&nbsp;225&nbsp;240</spanx></c>
  2257. <c>3</c>
  2258. <c><spanx style="vbare">10&nbsp;21&nbsp;36&nbsp;50&nbsp;63&nbsp;&nbsp;79&nbsp;&nbsp;95&nbsp;110&nbsp;126&nbsp;141&nbsp;157&nbsp;173&nbsp;189&nbsp;205&nbsp;221&nbsp;237</spanx></c>
  2259. <c>4</c>
  2260. <c><spanx style="vbare">17&nbsp;20&nbsp;37&nbsp;51&nbsp;59&nbsp;&nbsp;78&nbsp;&nbsp;89&nbsp;107&nbsp;123&nbsp;134&nbsp;150&nbsp;164&nbsp;184&nbsp;205&nbsp;224&nbsp;240</spanx></c>
  2261. <c>5</c>
  2262. <c><spanx style="vbare">10&nbsp;15&nbsp;32&nbsp;51&nbsp;67&nbsp;&nbsp;81&nbsp;&nbsp;96&nbsp;112&nbsp;129&nbsp;142&nbsp;158&nbsp;173&nbsp;189&nbsp;204&nbsp;220&nbsp;236</spanx></c>
  2263. <c>6</c>
  2264. <c><spanx style="vbare">&nbsp;8&nbsp;21&nbsp;37&nbsp;51&nbsp;65&nbsp;&nbsp;79&nbsp;&nbsp;98&nbsp;113&nbsp;126&nbsp;138&nbsp;155&nbsp;168&nbsp;179&nbsp;192&nbsp;209&nbsp;218</spanx></c>
  2265. <c>7</c>
  2266. <c><spanx style="vbare">12&nbsp;15&nbsp;34&nbsp;55&nbsp;63&nbsp;&nbsp;78&nbsp;&nbsp;87&nbsp;108&nbsp;118&nbsp;131&nbsp;148&nbsp;167&nbsp;185&nbsp;203&nbsp;219&nbsp;236</spanx></c>
  2267. <c>8</c>
  2268. <c><spanx style="vbare">16&nbsp;19&nbsp;32&nbsp;36&nbsp;56&nbsp;&nbsp;79&nbsp;&nbsp;91&nbsp;108&nbsp;118&nbsp;136&nbsp;154&nbsp;171&nbsp;186&nbsp;204&nbsp;220&nbsp;237</spanx></c>
  2269. <c>9</c>
  2270. <c><spanx style="vbare">11&nbsp;28&nbsp;43&nbsp;58&nbsp;74&nbsp;&nbsp;89&nbsp;105&nbsp;120&nbsp;135&nbsp;150&nbsp;165&nbsp;180&nbsp;196&nbsp;211&nbsp;226&nbsp;241</spanx></c>
  2271. <c>10</c>
  2272. <c><spanx style="vbare">&nbsp;6&nbsp;16&nbsp;33&nbsp;46&nbsp;60&nbsp;&nbsp;75&nbsp;&nbsp;92&nbsp;107&nbsp;123&nbsp;137&nbsp;156&nbsp;169&nbsp;185&nbsp;199&nbsp;214&nbsp;225</spanx></c>
  2273. <c>11</c>
  2274. <c><spanx style="vbare">11&nbsp;19&nbsp;30&nbsp;44&nbsp;57&nbsp;&nbsp;74&nbsp;&nbsp;89&nbsp;105&nbsp;121&nbsp;135&nbsp;152&nbsp;169&nbsp;186&nbsp;202&nbsp;218&nbsp;234</spanx></c>
  2275. <c>12</c>
  2276. <c><spanx style="vbare">12&nbsp;19&nbsp;29&nbsp;46&nbsp;57&nbsp;&nbsp;71&nbsp;&nbsp;88&nbsp;100&nbsp;120&nbsp;132&nbsp;148&nbsp;165&nbsp;182&nbsp;199&nbsp;216&nbsp;233</spanx></c>
  2277. <c>13</c>
  2278. <c><spanx style="vbare">17&nbsp;23&nbsp;35&nbsp;46&nbsp;56&nbsp;&nbsp;77&nbsp;&nbsp;92&nbsp;106&nbsp;123&nbsp;134&nbsp;152&nbsp;167&nbsp;185&nbsp;204&nbsp;222&nbsp;237</spanx></c>
  2279. <c>14</c>
  2280. <c><spanx style="vbare">14&nbsp;17&nbsp;45&nbsp;53&nbsp;63&nbsp;&nbsp;75&nbsp;&nbsp;89&nbsp;107&nbsp;115&nbsp;132&nbsp;151&nbsp;171&nbsp;188&nbsp;206&nbsp;221&nbsp;240</spanx></c>
  2281. <c>15</c>
  2282. <c><spanx style="vbare">&nbsp;9&nbsp;16&nbsp;29&nbsp;40&nbsp;56&nbsp;&nbsp;71&nbsp;&nbsp;88&nbsp;103&nbsp;119&nbsp;137&nbsp;154&nbsp;171&nbsp;189&nbsp;205&nbsp;222&nbsp;237</spanx></c>
  2283. <c>16</c>
  2284. <c><spanx style="vbare">16&nbsp;19&nbsp;36&nbsp;48&nbsp;57&nbsp;&nbsp;76&nbsp;&nbsp;87&nbsp;105&nbsp;118&nbsp;132&nbsp;150&nbsp;167&nbsp;185&nbsp;202&nbsp;218&nbsp;236</spanx></c>
  2285. <c>17</c>
  2286. <c><spanx style="vbare">12&nbsp;17&nbsp;29&nbsp;54&nbsp;71&nbsp;&nbsp;81&nbsp;&nbsp;94&nbsp;104&nbsp;126&nbsp;136&nbsp;149&nbsp;164&nbsp;182&nbsp;201&nbsp;221&nbsp;237</spanx></c>
  2287. <c>18</c>
  2288. <c><spanx style="vbare">15&nbsp;28&nbsp;47&nbsp;62&nbsp;79&nbsp;&nbsp;97&nbsp;115&nbsp;129&nbsp;142&nbsp;155&nbsp;168&nbsp;180&nbsp;194&nbsp;208&nbsp;223&nbsp;238</spanx></c>
  2289. <c>19</c>
  2290. <c><spanx style="vbare">&nbsp;8&nbsp;14&nbsp;30&nbsp;45&nbsp;62&nbsp;&nbsp;78&nbsp;&nbsp;94&nbsp;111&nbsp;127&nbsp;143&nbsp;159&nbsp;175&nbsp;192&nbsp;207&nbsp;223&nbsp;239</spanx></c>
  2291. <c>20</c>
  2292. <c><spanx style="vbare">17&nbsp;30&nbsp;49&nbsp;62&nbsp;79&nbsp;&nbsp;92&nbsp;107&nbsp;119&nbsp;132&nbsp;145&nbsp;160&nbsp;174&nbsp;190&nbsp;204&nbsp;220&nbsp;235</spanx></c>
  2293. <c>21</c>
  2294. <c><spanx style="vbare">14&nbsp;19&nbsp;36&nbsp;45&nbsp;61&nbsp;&nbsp;76&nbsp;&nbsp;91&nbsp;108&nbsp;121&nbsp;138&nbsp;154&nbsp;172&nbsp;189&nbsp;205&nbsp;222&nbsp;238</spanx></c>
  2295. <c>22</c>
  2296. <c><spanx style="vbare">12&nbsp;18&nbsp;31&nbsp;45&nbsp;60&nbsp;&nbsp;76&nbsp;&nbsp;91&nbsp;107&nbsp;123&nbsp;138&nbsp;154&nbsp;171&nbsp;187&nbsp;204&nbsp;221&nbsp;236</spanx></c>
  2297. <c>23</c>
  2298. <c><spanx style="vbare">13&nbsp;17&nbsp;31&nbsp;43&nbsp;53&nbsp;&nbsp;70&nbsp;&nbsp;83&nbsp;103&nbsp;114&nbsp;131&nbsp;149&nbsp;167&nbsp;185&nbsp;203&nbsp;220&nbsp;237</spanx></c>
  2299. <c>24</c>
  2300. <c><spanx style="vbare">17&nbsp;22&nbsp;35&nbsp;42&nbsp;58&nbsp;&nbsp;78&nbsp;&nbsp;93&nbsp;110&nbsp;125&nbsp;139&nbsp;155&nbsp;170&nbsp;188&nbsp;206&nbsp;224&nbsp;240</spanx></c>
  2301. <c>25</c>
  2302. <c><spanx style="vbare">&nbsp;8&nbsp;15&nbsp;34&nbsp;50&nbsp;67&nbsp;&nbsp;83&nbsp;&nbsp;99&nbsp;115&nbsp;131&nbsp;146&nbsp;162&nbsp;178&nbsp;193&nbsp;209&nbsp;224&nbsp;239</spanx></c>
  2303. <c>26</c>
  2304. <c><spanx style="vbare">13&nbsp;16&nbsp;41&nbsp;66&nbsp;73&nbsp;&nbsp;86&nbsp;&nbsp;95&nbsp;111&nbsp;128&nbsp;137&nbsp;150&nbsp;163&nbsp;183&nbsp;206&nbsp;225&nbsp;241</spanx></c>
  2305. <c>27</c>
  2306. <c><spanx style="vbare">17&nbsp;25&nbsp;37&nbsp;52&nbsp;63&nbsp;&nbsp;75&nbsp;&nbsp;92&nbsp;102&nbsp;119&nbsp;132&nbsp;144&nbsp;160&nbsp;175&nbsp;191&nbsp;212&nbsp;231</spanx></c>
  2307. <c>28</c>
  2308. <c><spanx style="vbare">19&nbsp;31&nbsp;49&nbsp;65&nbsp;83&nbsp;100&nbsp;117&nbsp;133&nbsp;147&nbsp;161&nbsp;174&nbsp;187&nbsp;200&nbsp;213&nbsp;227&nbsp;242</spanx></c>
  2309. <c>29</c>
  2310. <c><spanx style="vbare">18&nbsp;31&nbsp;52&nbsp;68&nbsp;88&nbsp;103&nbsp;117&nbsp;126&nbsp;138&nbsp;149&nbsp;163&nbsp;177&nbsp;192&nbsp;207&nbsp;223&nbsp;239</spanx></c>
  2311. <c>30</c>
  2312. <c><spanx style="vbare">16&nbsp;29&nbsp;47&nbsp;61&nbsp;76&nbsp;&nbsp;90&nbsp;106&nbsp;119&nbsp;133&nbsp;147&nbsp;161&nbsp;176&nbsp;193&nbsp;209&nbsp;224&nbsp;240</spanx></c>
  2313. <c>31</c>
  2314. <c><spanx style="vbare">15&nbsp;21&nbsp;35&nbsp;50&nbsp;61&nbsp;&nbsp;73&nbsp;&nbsp;86&nbsp;&nbsp;97&nbsp;110&nbsp;119&nbsp;129&nbsp;141&nbsp;175&nbsp;198&nbsp;218&nbsp;237</spanx></c>
  2315. </texttable>
  2316. <t>
  2317. Given the stage-1 codebook entry cb1_Q8[], the stage-2 residual res_Q10[], and
  2318. their corresponding weights, w_Q9[], the reconstructed normalized LSF
  2319. coefficients are
  2320. <figure align="center">
  2321. <artwork align="center"><![CDATA[
  2322. NLSF_Q15[k] = (cb1_Q8[k]<<7) + (res_Q10[k]<<14)/w_Q9[k] ,
  2323. ]]></artwork>
  2324. </figure>
  2325. where the division is exact integer division.
  2326. However, nothing in either the reconstruction process or the
  2327. quantization process in the encoder thus far guarantees that the coefficients
  2328. are monotonically increasing and separated well enough to ensure a stable
  2329. filter.
  2330. When using the reference encoder, roughly 2% of frames violate this constraint.
  2331. The next section describes a stabilization procedure used to make these
  2332. guarantees.
  2333. </t>
  2334. </section>
  2335. <section anchor="silk_nlsf_stabilization" title="Normalized LSF Stabilization">
  2336. <t>
  2337. The normalized LSF stabilization procedure is implemented in
  2338. silk_NLSF_stabilize() (silk_NLSF_stabilize.c).
  2339. This process ensures that consecutive values of the normalized LSF
  2340. coefficients, NLSF_Q15[], are spaced some minimum distance apart
  2341. (predetermined to be the 0.01 percentile of a large training set).
  2342. <xref target="silk_nlsf_min_spacing"/> gives the minimum spacings for NB and MB
  2343. and those for WB, where row k is the minimum allowed value of
  2344. NLSF_Q[k]-NLSF_Q[k-1].
  2345. For the purposes of computing this spacing for the first and last coefficient,
  2346. NLSF_Q15[-1] is taken to be 0, and NLSF_Q15[d_LPC] is taken to be 32768.
  2347. </t>
  2348. <texttable anchor="silk_nlsf_min_spacing"
  2349. title="Minimum Spacing for Normalized LSF Coefficients">
  2350. <ttcol>Coefficient</ttcol>
  2351. <ttcol align="right">NB and MB</ttcol>
  2352. <ttcol align="right">WB</ttcol>
  2353. <c>0</c> <c>250</c> <c>100</c>
  2354. <c>1</c> <c>3</c> <c>3</c>
  2355. <c>2</c> <c>6</c> <c>40</c>
  2356. <c>3</c> <c>3</c> <c>3</c>
  2357. <c>4</c> <c>3</c> <c>3</c>
  2358. <c>5</c> <c>3</c> <c>3</c>
  2359. <c>6</c> <c>4</c> <c>5</c>
  2360. <c>7</c> <c>3</c> <c>14</c>
  2361. <c>8</c> <c>3</c> <c>14</c>
  2362. <c>9</c> <c>3</c> <c>10</c>
  2363. <c>10</c> <c>461</c> <c>11</c>
  2364. <c>11</c> <c/> <c>3</c>
  2365. <c>12</c> <c/> <c>8</c>
  2366. <c>13</c> <c/> <c>9</c>
  2367. <c>14</c> <c/> <c>7</c>
  2368. <c>15</c> <c/> <c>3</c>
  2369. <c>16</c> <c/> <c>347</c>
  2370. </texttable>
  2371. <t>
  2372. The procedure starts off by trying to make small adjustments which attempt to
  2373. minimize the amount of distortion introduced.
  2374. After 20 such adjustments, it falls back to a more direct method which
  2375. guarantees the constraints are enforced but may require large adjustments.
  2376. </t>
  2377. <t>
  2378. Let NDeltaMin_Q15[k] be the minimum required spacing for the current audio
  2379. bandwidth from <xref target="silk_nlsf_min_spacing"/>.
  2380. First, the procedure finds the index i where
  2381. NLSF_Q15[i]&nbsp;-&nbsp;NLSF_Q15[i-1]&nbsp;-&nbsp;NDeltaMin_Q15[i] is the
  2382. smallest, breaking ties by using the lower value of i.
  2383. If this value is non-negative, then the stabilization stops; the coefficients
  2384. satisfy all the constraints.
  2385. Otherwise, if i&nbsp;==&nbsp;0, it sets NLSF_Q15[0] to NDeltaMin_Q15[0], and if
  2386. i&nbsp;==&nbsp;d_LPC, it sets NLSF_Q15[d_LPC-1] to
  2387. (32768&nbsp;-&nbsp;NDeltaMin_Q15[d_LPC]).
  2388. For all other values of i, both NLSF_Q15[i-1] and NLSF_Q15[i] are updated as
  2389. follows:
  2390. <figure align="center">
  2391. <artwork align="center"><![CDATA[
  2392. i-1
  2393. __
  2394. min_center_Q15 = (NDeltaMin[i]>>1) + \ NDeltaMin[k]
  2395. /_
  2396. k=0
  2397. d_LPC
  2398. __
  2399. max_center_Q15 = 32768 - (NDeltaMin[i]>>1) - \ NDeltaMin[k]
  2400. /_
  2401. k=i+1
  2402. center_freq_Q15 = clamp(min_center_Q15[i],
  2403. (NLSF_Q15[i-1] + NLSF_Q15[i] + 1)>>1,
  2404. max_center_Q15[i])
  2405. NLSF_Q15[i-1] = center_freq_Q15 - (NDeltaMin_Q15[i]>>1)
  2406. NLSF_Q15[i] = NLSF_Q15[i-1] + NDeltaMin_Q15[i] .
  2407. ]]></artwork>
  2408. </figure>
  2409. Then the procedure repeats again, until it has either executed 20 times or
  2410. has stopped because the coefficients satisfy all the constraints.
  2411. </t>
  2412. <t>
  2413. After the 20th repetition of the above procedure, the following fallback
  2414. procedure executes once.
  2415. First, the values of NLSF_Q15[k] for 0&nbsp;&lt;=&nbsp;k&nbsp;&lt;&nbsp;d_LPC
  2416. are sorted in ascending order.
  2417. Then for each value of k from 0 to d_LPC-1, NLSF_Q15[k] is set to
  2418. <figure align="center">
  2419. <artwork align="center"><![CDATA[
  2420. max(NLSF_Q15[k], NLSF_Q15[k-1] + NDeltaMin_Q15[k]) .
  2421. ]]></artwork>
  2422. </figure>
  2423. Next, for each value of k from d_LPC-1 down to 0, NLSF_Q15[k] is set to
  2424. <figure align="center">
  2425. <artwork align="center"><![CDATA[
  2426. min(NLSF_Q15[k], NLSF_Q15[k+1] - NDeltaMin_Q15[k+1]) .
  2427. ]]></artwork>
  2428. </figure>
  2429. </t>
  2430. </section>
  2431. <section anchor="silk_nlsf_interpolation" title="Normalized LSF Interpolation">
  2432. <t>
  2433. For 20&nbsp;ms SILK frames, the first half of the frame (i.e., the first two
  2434. subframes) may use normalized LSF coefficients that are interpolated between
  2435. the decoded LSFs for the most recent coded frame (in the same channel) and the
  2436. current frame.
  2437. A Q2 interpolation factor follows the LSF coefficient indices in the bitstream,
  2438. which is decoded using the PDF in <xref target="silk_nlsf_interp_pdf"/>.
  2439. This happens in silk_decode_indices() (silk_decode_indices.c).
  2440. For the first frame after a decoder reset, when no prior LSF coefficients are
  2441. available, the decoder still decodes this factor, but ignores its value and
  2442. always uses 4 instead.
  2443. For 10&nbsp;ms SILK frames, this factor is not stored at all.
  2444. </t>
  2445. <texttable anchor="silk_nlsf_interp_pdf"
  2446. title="PDF for Normalized LSF Interpolation Index">
  2447. <ttcol>PDF</ttcol>
  2448. <c>{13, 22, 29, 11, 181}/256</c>
  2449. </texttable>
  2450. <t>
  2451. Let n2_Q15[k] be the normalized LSF coefficients decoded by the procedure in
  2452. <xref target="silk_nlsfs"/>, n0_Q15[k] be the LSF coefficients
  2453. decoded for the prior frame, and w_Q2 be the interpolation factor.
  2454. Then the normalized LSF coefficients used for the first half of a 20&nbsp;ms
  2455. frame, n1_Q15[k], are
  2456. <figure align="center">
  2457. <artwork align="center"><![CDATA[
  2458. n1_Q15[k] = n0_Q15[k] + (w_Q2*(n2_Q15[k] - n0_Q15[k]) >> 2) .
  2459. ]]></artwork>
  2460. </figure>
  2461. This interpolation is performed in silk_decode_parameters()
  2462. (silk_decode_parameters.c).
  2463. </t>
  2464. </section>
  2465. <section anchor="silk_nlsf2lpc"
  2466. title="Converting Normalized LSFs to LPC Coefficients">
  2467. <t>
  2468. Any LPC filter A(z) can be split into a symmetric part P(z) and an
  2469. anti-symmetric part Q(z) such that
  2470. <figure align="center">
  2471. <artwork align="center"><![CDATA[
  2472. d_LPC
  2473. __ -k 1
  2474. A(z) = 1 - \ a[k] * z = - * (P(z) + Q(z))
  2475. /_ 2
  2476. k=1
  2477. ]]></artwork>
  2478. </figure>
  2479. with
  2480. <figure align="center">
  2481. <artwork align="center"><![CDATA[
  2482. -d_LPC-1 -1
  2483. P(z) = A(z) + z * A(z )
  2484. -d_LPC-1 -1
  2485. Q(z) = A(z) - z * A(z ) .
  2486. ]]></artwork>
  2487. </figure>
  2488. The even normalized LSF coefficients correspond to a pair of conjugate roots of
  2489. P(z), while the odd coefficients correspond to a pair of conjugate roots of
  2490. Q(z), all of which lie on the unit circle.
  2491. In addition, P(z) has a root at pi and Q(z) has a root at 0.
  2492. Thus, they may be reconstructed mathematically from a set of normalized LSF
  2493. coefficients, n[k], as
  2494. <figure align="center">
  2495. <artwork align="center"><![CDATA[
  2496. d_LPC/2-1
  2497. -1 ___ -1 -2
  2498. P(z) = (1 + z ) * | | (1 - 2*cos(pi*n[2*k])*z + z )
  2499. k=0
  2500. d_LPC/2-1
  2501. -1 ___ -1 -2
  2502. Q(z) = (1 - z ) * | | (1 - 2*cos(pi*n[2*k+1])*z + z )
  2503. k=0
  2504. ]]></artwork>
  2505. </figure>
  2506. </t>
  2507. <t>
  2508. However, SILK performs this reconstruction using a fixed-point approximation so
  2509. that all decoders can reproduce it in a bit-exact manner to avoid prediction
  2510. drift.
  2511. The function silk_NLSF2A() (silk_NLSF2A.c) implements this procedure.
  2512. </t>
  2513. <t>
  2514. To start, it approximates cos(pi*n[k]) using a table lookup with linear
  2515. interpolation.
  2516. The encoder SHOULD use the inverse of this piecewise linear approximation,
  2517. rather than the true inverse of the cosine function, when deriving the
  2518. normalized LSF coefficients.
  2519. </t>
  2520. <t>
  2521. The top 7 bits of each normalized LSF coefficient index a value in the table,
  2522. and the next 8 bits interpolate between it and the next value.
  2523. Let i&nbsp;=&nbsp;n[k]&gt;&gt;8 be the integer index and
  2524. f&nbsp;=&nbsp;n[k]&amp;255 be the fractional part of a given coefficient.
  2525. Then the approximated cosine, c_Q17[k], is
  2526. <figure align="center">
  2527. <artwork align="center"><![CDATA[
  2528. c_Q17[k] = (cos_Q13[i]*256 + (cos_Q13[i+1]-cos_Q13[i])*f + 8) >> 4 ,
  2529. ]]></artwork>
  2530. </figure>
  2531. where cos_Q13[i] is the corresponding entry of
  2532. <xref target="silk_cos_table"/>.
  2533. </t>
  2534. <texttable anchor="silk_cos_table"
  2535. title="Q13 Cosine Table for LSF Conversion">
  2536. <ttcol align="right"></ttcol>
  2537. <ttcol align="right">0</ttcol>
  2538. <ttcol align="right">1</ttcol>
  2539. <ttcol align="right">2</ttcol>
  2540. <ttcol align="right">3</ttcol>
  2541. <c>0</c>
  2542. <c>8192</c> <c>8190</c> <c>8182</c> <c>8170</c>
  2543. <c>4</c>
  2544. <c>8152</c> <c>8130</c> <c>8104</c> <c>8072</c>
  2545. <c>8</c>
  2546. <c>8034</c> <c>7994</c> <c>7946</c> <c>7896</c>
  2547. <c>12</c>
  2548. <c>7840</c> <c>7778</c> <c>7714</c> <c>7644</c>
  2549. <c>16</c>
  2550. <c>7568</c> <c>7490</c> <c>7406</c> <c>7318</c>
  2551. <c>20</c>
  2552. <c>7226</c> <c>7128</c> <c>7026</c> <c>6922</c>
  2553. <c>24</c>
  2554. <c>6812</c> <c>6698</c> <c>6580</c> <c>6458</c>
  2555. <c>28</c>
  2556. <c>6332</c> <c>6204</c> <c>6070</c> <c>5934</c>
  2557. <c>32</c>
  2558. <c>5792</c> <c>5648</c> <c>5502</c> <c>5352</c>
  2559. <c>36</c>
  2560. <c>5198</c> <c>5040</c> <c>4880</c> <c>4718</c>
  2561. <c>40</c>
  2562. <c>4552</c> <c>4382</c> <c>4212</c> <c>4038</c>
  2563. <c>44</c>
  2564. <c>3862</c> <c>3684</c> <c>3502</c> <c>3320</c>
  2565. <c>48</c>
  2566. <c>3136</c> <c>2948</c> <c>2760</c> <c>2570</c>
  2567. <c>52</c>
  2568. <c>2378</c> <c>2186</c> <c>1990</c> <c>1794</c>
  2569. <c>56</c>
  2570. <c>1598</c> <c>1400</c> <c>1202</c> <c>1002</c>
  2571. <c>60</c>
  2572. <c>802</c> <c>602</c> <c>402</c> <c>202</c>
  2573. <c>64</c>
  2574. <c>0</c> <c>-202</c> <c>-402</c> <c>-602</c>
  2575. <c>68</c>
  2576. <c>-802</c><c>-1002</c><c>-1202</c><c>-1400</c>
  2577. <c>72</c>
  2578. <c>-1598</c><c>-1794</c><c>-1990</c><c>-2186</c>
  2579. <c>76</c>
  2580. <c>-2378</c><c>-2570</c><c>-2760</c><c>-2948</c>
  2581. <c>80</c>
  2582. <c>-3136</c><c>-3320</c><c>-3502</c><c>-3684</c>
  2583. <c>84</c>
  2584. <c>-3862</c><c>-4038</c><c>-4212</c><c>-4382</c>
  2585. <c>88</c>
  2586. <c>-4552</c><c>-4718</c><c>-4880</c><c>-5040</c>
  2587. <c>92</c>
  2588. <c>-5198</c><c>-5352</c><c>-5502</c><c>-5648</c>
  2589. <c>96</c>
  2590. <c>-5792</c><c>-5934</c><c>-6070</c><c>-6204</c>
  2591. <c>100</c>
  2592. <c>-6332</c><c>-6458</c><c>-6580</c><c>-6698</c>
  2593. <c>104</c>
  2594. <c>-6812</c><c>-6922</c><c>-7026</c><c>-7128</c>
  2595. <c>108</c>
  2596. <c>-7226</c><c>-7318</c><c>-7406</c><c>-7490</c>
  2597. <c>112</c>
  2598. <c>-7568</c><c>-7644</c><c>-7714</c><c>-7778</c>
  2599. <c>116</c>
  2600. <c>-7840</c><c>-7896</c><c>-7946</c><c>-7994</c>
  2601. <c>120</c>
  2602. <c>-8034</c><c>-8072</c><c>-8104</c><c>-8130</c>
  2603. <c>124</c>
  2604. <c>-8152</c><c>-8170</c><c>-8182</c><c>-8190</c>
  2605. <c>128</c>
  2606. <c>-8192</c> <c/> <c/> <c/>
  2607. </texttable>
  2608. <t>
  2609. Given the list of cosine values, silk_NLSF2A_find_poly() (silk_NLSF2A.c)
  2610. computes the coefficients of P and Q, described here via a simple recurrence.
  2611. Let p_Q16[k][j] and q_Q16[k][j] be the coefficients of the products of the
  2612. first (k+1) root pairs for P and Q, with j indexing the coefficient number.
  2613. Only the first (k+2) coefficients are needed, as the products are symmetric.
  2614. Let p_Q16[0][0]&nbsp;=&nbsp;q_Q16[0][0]&nbsp;=&nbsp;1&lt;&lt;16,
  2615. p_Q16[0][1]&nbsp;=&nbsp;-c_Q17[0], q_Q16[0][1]&nbsp;=&nbsp;-c_Q17[1], and
  2616. d2&nbsp;=&nbsp;d_LPC/2.
  2617. As boundary conditions, assume
  2618. p_Q16[k][j]&nbsp;=&nbsp;q_Q16[k][j]&nbsp;=&nbsp;0 for all
  2619. j&nbsp;&lt;&nbsp;0.
  2620. Also, assume p_Q16[k][k+2]&nbsp;=&nbsp;p_Q16[k][k] and
  2621. q_Q16[k][k+2]&nbsp;=&nbsp;q_Q16[k][k] (because of the symmetry).
  2622. Then, for 0&nbsp;&lt;k&nbsp;&lt;&nbsp;d2 and 0&nbsp;&lt;=&nbsp;j&nbsp;&lt;=&nbsp;k+1,
  2623. <figure align="center">
  2624. <artwork align="center"><![CDATA[
  2625. p_Q16[k][j] = p_Q16[k-1][j] + p_Q16[k-1][j-2]
  2626. - ((c_Q17[2*k]*p_Q16[k-1][j-1] + 32768)>>16) ,
  2627. q_Q16[k][j] = q_Q16[k-1][j] + q_Q16[k-1][j-2]
  2628. - ((c_Q17[2*k+1]*q_Q16[k-1][j-1] + 32768)>>16) .
  2629. ]]></artwork>
  2630. </figure>
  2631. The use of Q17 values for the cosine terms in an otherwise Q16 expression
  2632. implicitly scales them by a factor of 2.
  2633. The multiplications in this recurrence may require up to 48 bits of precision
  2634. in the result to avoid overflow.
  2635. In practice, each row of the recurrence only depends on the previous row, so an
  2636. implementation does not need to store all of them.
  2637. </t>
  2638. <t>
  2639. silk_NLSF2A() uses the values from the last row of this recurrence to
  2640. reconstruct a 32-bit version of the LPC filter (without the leading 1.0
  2641. coefficient), a32_Q17[k], 0&nbsp;&lt;=&nbsp;k&nbsp;&lt;&nbsp;d2:
  2642. <figure align="center">
  2643. <artwork align="center"><![CDATA[
  2644. a32_Q17[k] = -(q_Q16[d2-1][k+1] - q_Q16[d2-1][k])
  2645. - (p_Q16[d2-1][k+1] + p_Q16[d2-1][k])) ,
  2646. a32_Q17[d_LPC-k-1] = (q_Q16[d2-1][k+1] - q_Q16[d2-1][k])
  2647. - (p_Q16[d2-1][k+1] + p_Q16[d2-1][k])) .
  2648. ]]></artwork>
  2649. </figure>
  2650. The sum and difference of two terms from each of the p_Q16 and q_Q16
  2651. coefficient lists reflect the (1&nbsp;+&nbsp;z**-1) and
  2652. (1&nbsp;-&nbsp;z**-1) factors of P and Q, respectively.
  2653. The promotion of the expression from Q16 to Q17 implicitly scales the result
  2654. by 1/2.
  2655. </t>
  2656. </section>
  2657. <section anchor="silk_lpc_range_limit"
  2658. title="Limiting the Range of the LPC Coefficients">
  2659. <t>
  2660. The a32_Q17[] coefficients are too large to fit in a 16-bit value, which
  2661. significantly increases the cost of applying this filter in fixed-point
  2662. decoders.
  2663. Reducing them to Q12 precision doesn't incur any significant quality loss,
  2664. but still does not guarantee they will fit.
  2665. silk_NLSF2A() applies up to 10 rounds of bandwidth expansion to limit
  2666. the dynamic range of these coefficients.
  2667. Even floating-point decoders SHOULD perform these steps, to avoid mismatch.
  2668. </t>
  2669. <t>
  2670. For each round, the process first finds the index k such that abs(a32_Q17[k])
  2671. is largest, breaking ties by choosing the lowest value of k.
  2672. Then, it computes the corresponding Q12 precision value, maxabs_Q12, subject to
  2673. an upper bound to avoid overflow in subsequent computations:
  2674. <figure align="center">
  2675. <artwork align="center"><![CDATA[
  2676. maxabs_Q12 = min((maxabs_Q17 + 16) >> 5, 163838) .
  2677. ]]></artwork>
  2678. </figure>
  2679. If this is larger than 32767, the procedure derives the chirp factor,
  2680. sc_Q16[0], to use in the bandwidth expansion as
  2681. <figure align="center">
  2682. <artwork align="center"><![CDATA[
  2683. (maxabs_Q12 - 32767) << 14
  2684. sc_Q16[0] = 65470 - -------------------------- ,
  2685. (maxabs_Q12 * (k+1)) >> 2
  2686. ]]></artwork>
  2687. </figure>
  2688. where the division here is exact integer division.
  2689. This is an approximation of the chirp factor needed to reduce the target
  2690. coefficient to 32767, though it is both less than 0.999 and, for
  2691. k&nbsp;&gt;&nbsp;0 when maxabs_Q12 is much greater than 32767, still slightly
  2692. too large.
  2693. </t>
  2694. <t>
  2695. silk_bwexpander_32() (silk_bwexpander_32.c) performs the bandwidth expansion
  2696. (again, only when maxabs_Q12 is greater than 32767) using the following
  2697. recurrence:
  2698. <figure align="center">
  2699. <artwork align="center"><![CDATA[
  2700. a32_Q17[k] = (a32_Q17[k]*sc_Q16[k]) >> 16
  2701. sc_Q16[k+1] = (sc_Q16[0]*sc_Q16[k] + 32768) >> 16
  2702. ]]></artwork>
  2703. </figure>
  2704. The first multiply may require up to 48 bits of precision in the result to
  2705. avoid overflow.
  2706. The second multiply must be unsigned to avoid overflow with only 32 bits of
  2707. precision.
  2708. The reference implementation uses a slightly more complex formulation that
  2709. avoids the 32-bit overflow using signed multiplication, but is otherwise
  2710. equivalent.
  2711. </t>
  2712. <t>
  2713. After 10 rounds of bandwidth expansion are performed, they are simply saturated
  2714. to 16 bits:
  2715. <figure align="center">
  2716. <artwork align="center"><![CDATA[
  2717. a32_Q17[k] = clamp(-32768, (a32_Q17[k]+16) >> 5, 32767) << 5 .
  2718. ]]></artwork>
  2719. </figure>
  2720. Because this performs the actual saturation in the Q12 domain, but converts the
  2721. coefficients back to the Q17 domain for the purposes of prediction gain
  2722. limiting, this step must be performed after the 10th round of bandwidth
  2723. expansion, regardless of whether or not the Q12 version of any coefficient
  2724. still overflows a 16-bit integer.
  2725. This saturation is not performed if maxabs_Q12 drops to 32767 or less prior to
  2726. the 10th round.
  2727. </t>
  2728. </section>
  2729. <section anchor="silk_lpc_gain_limit"
  2730. title="Limiting the Prediction Gain of the LPC Filter">
  2731. <t>
  2732. Even if the Q12 coefficients would fit, the resulting filter may still have a
  2733. significant gain (especially for voiced sounds), making the filter unstable.
  2734. silk_NLSF2A() applies up to 18 additional rounds of bandwidth expansion to
  2735. limit the prediction gain.
  2736. Instead of controlling the amount of bandwidth expansion using the prediction
  2737. gain itself (which may diverge to infinity for an unstable filter),
  2738. silk_NLSF2A() uses LPC_inverse_pred_gain_QA() (silk_LPC_inv_pred_gain.c)
  2739. to compute the reflection coefficients associated with the filter.
  2740. The filter is stable if and only if the magnitude of these coefficients is
  2741. sufficiently less than one.
  2742. The reflection coefficients, rc[k], can be computed using a simple Levinson
  2743. recurrence, initialized with the LPC coefficients
  2744. a[d_LPC-1][n]&nbsp;=&nbsp;a[n], and then updated via
  2745. <figure align="center">
  2746. <artwork align="center"><![CDATA[
  2747. rc[k] = -a[k][k] ,
  2748. a[k][n] - a[k][k-n-1]*rc[k]
  2749. a[k-1][n] = --------------------------- .
  2750. 2
  2751. 1 - rc[k]
  2752. ]]></artwork>
  2753. </figure>
  2754. </t>
  2755. <t>
  2756. However, LPC_inverse_pred_gain_QA() approximates this using fixed-point
  2757. arithmetic to guarantee reproducible results across platforms and
  2758. implementations.
  2759. It is important to run on the real Q12 coefficients that will be used during
  2760. reconstruction, because small changes in the coefficients can make a stable
  2761. filter unstable, but increasing the precision back to Q16 allows more accurate
  2762. computation of the reflection coefficients.
  2763. Thus, let
  2764. <figure align="center">
  2765. <artwork align="center"><![CDATA[
  2766. a32_Q16[d_LPC-1][n] = ((a32_Q17[n] + 16) >> 5) << 4
  2767. ]]></artwork>
  2768. </figure>
  2769. be the Q16 representation of the Q12 version of the LPC coefficients that will
  2770. eventually be used.
  2771. Then for each k from d_LPC-1 down to 0, if
  2772. abs(a32_Q16[k][k])&nbsp;&gt;&nbsp;65520, the filter is unstable and the
  2773. recurrence stops.
  2774. Otherwise, the row k-1 of a32_Q16 is computed from row k as
  2775. <figure align="center">
  2776. <artwork align="center"><![CDATA[
  2777. rc_Q31[k] = -a32_Q16[k][k] << 15 ,
  2778. div_Q30[k] = (1<<30) - 1 - (rc_Q31[k]*rc_Q31[k] >> 32) ,
  2779. b1[k] = ilog(div_Q30[k]) - 16 ,
  2780. (1<<29) - 1
  2781. inv_Qb1[k] = ----------------------- ,
  2782. div_Q30[k] >> (b1[k]+1)
  2783. err_Q29[k] = (1<<29)
  2784. - ((div_Q30[k]<<(15-b1[k]))*inv_Qb1[k] >> 16) ,
  2785. mul_Q16[k] = ((inv_Qb1[k] << 16)
  2786. + (err_Q29[k]*inv_Qb1[k] >> 13)) >> b1[k] ,
  2787. b2[k] = ilog(mul_Q16[k]) - 15 ,
  2788. t_Q16[k-1][n] = a32_Q16[k][n]
  2789. - ((a32_Q16[k][k-n-1]*rc_Q31[k] >> 32) << 1) ,
  2790. a32_Q16[k-1][n] = ((t_Q16[k-1][n] *
  2791. (mul_Q16[k] << (16-b2[k]))) >> 32) << b2[k] .
  2792. ]]></artwork>
  2793. </figure>
  2794. Here, rc_Q30[k] are the reflection coefficients.
  2795. div_Q30[k] is the denominator for each iteration, and mul_Q16[k] is its
  2796. multiplicative inverse.
  2797. inv_Qb1[k], which ranges from 16384 to 32767, is a low-precision version of
  2798. that inverse (with b1[k] fractional bits, where b1[k] ranges from 3 to 14).
  2799. err_Q29[k] is the residual error, ranging from -32392 to 32763, which is used
  2800. to improve the accuracy.
  2801. t_Q16[k-1][n], 0&nbsp;&lt;=&nbsp;n&nbsp;&lt;&nbsp;k, are the numerators for the
  2802. next row of coefficients in the recursion, and a32_Q16[k-1][n] is the final
  2803. version of that row.
  2804. Every multiply in this procedure except the one used to compute mul_Q16[k]
  2805. requires more than 32 bits of precision, but otherwise all intermediate
  2806. results fit in 32 bits or less.
  2807. In practice, because each row only depends on the next one, an implementation
  2808. does not need to store them all.
  2809. If abs(a32_Q16[k][k])&nbsp;&lt;=&nbsp;65520 for
  2810. 0&nbsp;&lt;=&nbsp;k&nbsp;&lt;&nbsp;d_LPC, then the filter is considered stable.
  2811. </t>
  2812. <t>
  2813. On round i, 1&nbsp;&lt;=&nbsp;i&nbsp;&lt;=&nbsp;18, if the filter passes this
  2814. stability check, then this procedure stops, and the final LPC coefficients to
  2815. use for reconstruction<!--TODO: In section...--> are
  2816. <figure align="center">
  2817. <artwork align="center"><![CDATA[
  2818. a_Q12[k] = (a32_Q17[k] + 16) >> 5 .
  2819. ]]></artwork>
  2820. </figure>
  2821. Otherwise, a round of bandwidth expansion is applied using the same procedure
  2822. as in <xref target="silk_lpc_range_limit"/>, with
  2823. <figure align="center">
  2824. <artwork align="center"><![CDATA[
  2825. sc_Q16[0] = 65536 - i*(i+9) .
  2826. ]]></artwork>
  2827. </figure>
  2828. If, after the 18th round, the filter still fails the stability check, then
  2829. a_Q12[k] is set to 0 for all k.
  2830. </t>
  2831. </section>
  2832. </section>
  2833. <section anchor="silk_ltp_params"
  2834. title="Long-Term Prediction (LTP) Parameters">
  2835. <t>
  2836. After the normalized LSF indices and, for 20&nbsp;ms frames, the LSF
  2837. interpolation index, voiced frames (see <xref target="silk_frame_type"/>)
  2838. include additional Long-Term Prediction (LTP) parameters.
  2839. There is one primary lag index for each SILK frame, but this is refined to
  2840. produce a separate lag index per subframe using a vector quantizer.
  2841. Each subframe also gets its own prediction gain coefficient.
  2842. </t>
  2843. <section anchor="silk_ltp_lags" title="Pitch Lags">
  2844. <t>
  2845. The primary lag index is coded either relative to the primary lag of the prior
  2846. frame or as an absolute index.
  2847. Like the quantization gains, the first LBRR frame, an LBRR frame where the
  2848. previous LBRR frame was not coded, and the first regular SILK frame in each
  2849. channel of an Opus frame all code the pitch lag as an absolute index.
  2850. When the most recent coded frame in the current channel was not voiced, this
  2851. also forces absolute coding.
  2852. In particular, unlike an LBRR frame where the previous frame is not coded, in a
  2853. 60&nbsp;ms stereo Opus frame, if the first and third regular SILK frames
  2854. in the side channel are coded, voiced frames, but the second is not coded, the
  2855. third still uses relative coding.
  2856. </t>
  2857. <t>
  2858. With absolute coding, the primary pitch lag may range from 2&nbsp;ms
  2859. (inclusive) up to 18&nbsp;ms (exclusive), corresponding to pitches from
  2860. 500&nbsp;Hz down to 55.6&nbsp;Hz, respectively.
  2861. It is comprised of a high part and a low part, where the decoder reads the high
  2862. part using the 32-entry codebook in <xref target="silk_abs_pitch_high_pdf"/>
  2863. and the low part using the codebook corresponding to the current audio
  2864. bandwidth from <xref target="silk_abs_pitch_low_pdf"/>.
  2865. The final primary pitch lag is then
  2866. <figure align="center">
  2867. <artwork align="center"><![CDATA[
  2868. lag = lag_high*lag_scale + lag_low + lag_min
  2869. ]]></artwork>
  2870. </figure>
  2871. where lag_high is the high part, lag_low is the low part, and lag_scale
  2872. and lag_min are the values from the "Scale" and "Minimum Lag" columns of
  2873. <xref target="silk_abs_pitch_low_pdf"/>, respectively.
  2874. </t>
  2875. <texttable anchor="silk_abs_pitch_high_pdf"
  2876. title="PDF for High Part of Primary Pitch Lag">
  2877. <ttcol align="left">PDF</ttcol>
  2878. <c>{3, 3, 6, 11, 21, 30, 32, 19,
  2879. 11, 10, 12, 13, 13, 12, 11, 9,
  2880. 8, 7, 6, 4, 2, 2, 2, 1,
  2881. 1, 1, 1, 1, 1, 1, 1, 1}/256</c>
  2882. </texttable>
  2883. <texttable anchor="silk_abs_pitch_low_pdf"
  2884. title="PDF for Low Part of Primary Pitch Lag">
  2885. <ttcol>Audio Bandwidth</ttcol>
  2886. <ttcol>PDF</ttcol>
  2887. <ttcol>Scale</ttcol>
  2888. <ttcol>Minimum Lag</ttcol>
  2889. <ttcol>Maximum Lag</ttcol>
  2890. <c>NB</c> <c>{64, 64, 64, 64}/256</c> <c>4</c> <c>16</c> <c>144</c>
  2891. <c>MB</c> <c>{43, 42, 43, 43, 42, 43}/256</c> <c>6</c> <c>24</c> <c>216</c>
  2892. <c>WB</c> <c>{32, 32, 32, 32, 32, 32, 32, 32}/256</c> <c>8</c> <c>32</c> <c>288</c>
  2893. </texttable>
  2894. <t>
  2895. All frames that do not use absolute coding for the primary lag index use
  2896. relative coding instead.
  2897. The decoder reads a single delta value using the 21-entry PDF in
  2898. <xref target="silk_rel_pitch_pdf"/>.
  2899. If the resulting value is zero, it falls back to the absolute coding procedure
  2900. from the prior paragraph.
  2901. Otherwise, the final primary pitch lag is then
  2902. <figure align="center">
  2903. <artwork align="center"><![CDATA[
  2904. lag = lag_prev + (delta_lag_index - 9)
  2905. ]]></artwork>
  2906. </figure>
  2907. where lag_prev is the primary pitch lag from the most recent frame in the same
  2908. channel and delta_lag_index is the value just decoded.
  2909. This allows a per-frame change in the pitch lag of -8 to +11 samples.
  2910. The decoder does no clamping at this point, so this value can fall outside the
  2911. range of 2&nbsp;ms to 18&nbsp;ms, and the decoder must use this unclamped
  2912. value when using relative coding in the next SILK frame (if any).
  2913. However, because an Opus frame can use relative coding for at most two
  2914. consecutive SILK frames, integer overflow should not be an issue.
  2915. </t>
  2916. <texttable anchor="silk_rel_pitch_pdf"
  2917. title="PDF for Primary Pitch Lag Change">
  2918. <ttcol align="left">PDF</ttcol>
  2919. <c>{46, 2, 2, 3, 4, 6, 10, 15,
  2920. 26, 38, 30, 22, 15, 10, 7, 6,
  2921. 4, 4, 2, 2, 2}/256</c>
  2922. </texttable>
  2923. <t>
  2924. After the primary pitch lag, a "pitch contour", stored as a single entry from
  2925. one of four small VQ codebooks, gives lag offsets for each subframe in the
  2926. current SILK frame.
  2927. The codebook index is decoded using one of the PDFs in
  2928. <xref target="silk_pitch_contour_pdfs"/> depending on the current frame size
  2929. and audio bandwidth.
  2930. <xref target="silk_pitch_contour_cb_nb10ms"/> through
  2931. <xref target="silk_pitch_contour_cb_mbwb20ms"/> give the corresponding offsets
  2932. to apply to the primary pitch lag for each subframe given the decoded codebook
  2933. index.
  2934. </t>
  2935. <texttable anchor="silk_pitch_contour_pdfs"
  2936. title="PDFs for Subframe Pitch Contour">
  2937. <ttcol>Audio Bandwidth</ttcol>
  2938. <ttcol>SILK Frame Size</ttcol>
  2939. <ttcol align="right">Codebook Size</ttcol>
  2940. <ttcol>PDF</ttcol>
  2941. <c>NB</c> <c>10&nbsp;ms</c> <c>3</c>
  2942. <c>{143, 50, 63}/256</c>
  2943. <c>NB</c> <c>20&nbsp;ms</c> <c>11</c>
  2944. <c>{68, 12, 21, 17, 19, 22, 30, 24,
  2945. 17, 16, 10}/256</c>
  2946. <c>MB or WB</c> <c>10&nbsp;ms</c> <c>12</c>
  2947. <c>{91, 46, 39, 19, 14, 12, 8, 7,
  2948. 6, 5, 5, 4}/256</c>
  2949. <c>MB or WB</c> <c>20&nbsp;ms</c> <c>34</c>
  2950. <c>{33, 22, 18, 16, 15, 14, 14, 13,
  2951. 13, 10, 9, 9, 8, 6, 6, 6,
  2952. 5, 4, 4, 4, 3, 3, 3, 2,
  2953. 2, 2, 2, 2, 2, 2, 1, 1,
  2954. 1, 1}/256</c>
  2955. </texttable>
  2956. <texttable anchor="silk_pitch_contour_cb_nb10ms"
  2957. title="Codebook Vectors for Subframe Pitch Contour: NB, 10&nbsp;ms Frames">
  2958. <ttcol>Index</ttcol>
  2959. <ttcol align="right">Subframe Offsets</ttcol>
  2960. <c>0</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2961. <c>1</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0</spanx></c>
  2962. <c>2</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;1</spanx></c>
  2963. </texttable>
  2964. <texttable anchor="silk_pitch_contour_cb_nb20ms"
  2965. title="Codebook Vectors for Subframe Pitch Contour: NB, 20&nbsp;ms Frames">
  2966. <ttcol>Index</ttcol>
  2967. <ttcol align="right">Subframe Offsets</ttcol>
  2968. <c>0</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2969. <c>1</c> <c><spanx style="vbare">&nbsp;2&nbsp;&nbsp;1&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  2970. <c>2</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;2</spanx></c>
  2971. <c>3</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1</spanx></c>
  2972. <c>4</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2973. <c>5</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1</spanx></c>
  2974. <c>6</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;1</spanx></c>
  2975. <c>7</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2976. <c>8</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2977. <c>9</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  2978. <c>10</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  2979. </texttable>
  2980. <texttable anchor="silk_pitch_contour_cb_mbwb10ms"
  2981. title="Codebook Vectors for Subframe Pitch Contour: MB or WB, 10&nbsp;ms Frames">
  2982. <ttcol>Index</ttcol>
  2983. <ttcol align="right">Subframe Offsets</ttcol>
  2984. <c>0</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2985. <c>1</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;1</spanx></c>
  2986. <c>2</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0</spanx></c>
  2987. <c>3</c> <c><spanx style="vbare">-1&nbsp;&nbsp;1</spanx></c>
  2988. <c>4</c> <c><spanx style="vbare">&nbsp;1&nbsp;-1</spanx></c>
  2989. <c>5</c> <c><spanx style="vbare">-1&nbsp;&nbsp;2</spanx></c>
  2990. <c>6</c> <c><spanx style="vbare">&nbsp;2&nbsp;-1</spanx></c>
  2991. <c>7</c> <c><spanx style="vbare">-2&nbsp;&nbsp;2</spanx></c>
  2992. <c>8</c> <c><spanx style="vbare">&nbsp;2&nbsp;-2</spanx></c>
  2993. <c>9</c> <c><spanx style="vbare">-2&nbsp;&nbsp;3</spanx></c>
  2994. <c>10</c> <c><spanx style="vbare">&nbsp;3&nbsp;-2</spanx></c>
  2995. <c>11</c> <c><spanx style="vbare">-3&nbsp;&nbsp;3</spanx></c>
  2996. </texttable>
  2997. <texttable anchor="silk_pitch_contour_cb_mbwb20ms"
  2998. title="Codebook Vectors for Subframe Pitch Contour: MB or WB, 20&nbsp;ms Frames">
  2999. <ttcol>Index</ttcol>
  3000. <ttcol align="right">Subframe Offsets</ttcol>
  3001. <c>0</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  3002. <c>1</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;1</spanx></c>
  3003. <c>2</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  3004. <c>3</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  3005. <c>4</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1</spanx></c>
  3006. <c>5</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  3007. <c>6</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1</spanx></c>
  3008. <c>7</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  3009. <c>8</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;2</spanx></c>
  3010. <c>9</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  3011. <c>10</c> <c><spanx style="vbare">-2&nbsp;-1&nbsp;&nbsp;1&nbsp;&nbsp;2</spanx></c>
  3012. <c>11</c> <c><spanx style="vbare">&nbsp;2&nbsp;&nbsp;1&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  3013. <c>12</c> <c><spanx style="vbare">-2&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;2</spanx></c>
  3014. <c>13</c> <c><spanx style="vbare">-2&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;3</spanx></c>
  3015. <c>14</c> <c><spanx style="vbare">&nbsp;2&nbsp;&nbsp;1&nbsp;-1&nbsp;-2</spanx></c>
  3016. <c>15</c> <c><spanx style="vbare">-3&nbsp;-1&nbsp;&nbsp;1&nbsp;&nbsp;3</spanx></c>
  3017. <c>16</c> <c><spanx style="vbare">&nbsp;2&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;-2</spanx></c>
  3018. <c>17</c> <c><spanx style="vbare">&nbsp;3&nbsp;&nbsp;1&nbsp;&nbsp;0&nbsp;-2</spanx></c>
  3019. <c>18</c> <c><spanx style="vbare">-3&nbsp;-1&nbsp;&nbsp;2&nbsp;&nbsp;4</spanx></c>
  3020. <c>19</c> <c><spanx style="vbare">-4&nbsp;-1&nbsp;&nbsp;1&nbsp;&nbsp;4</spanx></c>
  3021. <c>20</c> <c><spanx style="vbare">&nbsp;3&nbsp;&nbsp;1&nbsp;-1&nbsp;-3</spanx></c>
  3022. <c>21</c> <c><spanx style="vbare">-4&nbsp;-1&nbsp;&nbsp;2&nbsp;&nbsp;5</spanx></c>
  3023. <c>22</c> <c><spanx style="vbare">&nbsp;4&nbsp;&nbsp;2&nbsp;-1&nbsp;-3</spanx></c>
  3024. <c>23</c> <c><spanx style="vbare">&nbsp;4&nbsp;&nbsp;1&nbsp;-1&nbsp;-4</spanx></c>
  3025. <c>24</c> <c><spanx style="vbare">-5&nbsp;-1&nbsp;&nbsp;2&nbsp;&nbsp;6</spanx></c>
  3026. <c>25</c> <c><spanx style="vbare">&nbsp;5&nbsp;&nbsp;2&nbsp;-1&nbsp;-4</spanx></c>
  3027. <c>26</c> <c><spanx style="vbare">-6&nbsp;-2&nbsp;&nbsp;2&nbsp;&nbsp;6</spanx></c>
  3028. <c>27</c> <c><spanx style="vbare">-5&nbsp;-2&nbsp;&nbsp;2&nbsp;&nbsp;5</spanx></c>
  3029. <c>28</c> <c><spanx style="vbare">&nbsp;6&nbsp;&nbsp;2&nbsp;-1&nbsp;-5</spanx></c>
  3030. <c>29</c> <c><spanx style="vbare">-7&nbsp;-2&nbsp;&nbsp;3&nbsp;&nbsp;8</spanx></c>
  3031. <c>30</c> <c><spanx style="vbare">&nbsp;6&nbsp;&nbsp;2&nbsp;-2&nbsp;-6</spanx></c>
  3032. <c>31</c> <c><spanx style="vbare">&nbsp;5&nbsp;&nbsp;2&nbsp;-2&nbsp;-5</spanx></c>
  3033. <c>32</c> <c><spanx style="vbare">&nbsp;8&nbsp;&nbsp;3&nbsp;-2&nbsp;-7</spanx></c>
  3034. <c>33</c> <c><spanx style="vbare">-9&nbsp;-3&nbsp;&nbsp;3&nbsp;&nbsp;9</spanx></c>
  3035. </texttable>
  3036. <t>
  3037. The final pitch lag for each subframe is assembled in silk_decode_pitch()
  3038. (silk_decode_pitch.c).
  3039. Let lag be the primary pitch lag for the current SILK frame, contour_index be
  3040. index of the VQ codebook, and lag_cb[contour_index][k] be the corresponding
  3041. entry of the codebook from the appropriate table given above for the k'th
  3042. subframe.
  3043. Then the final pitch lag for that subframe is
  3044. <figure align="center">
  3045. <artwork align="center"><![CDATA[
  3046. pitch_lags[k] = clamp(lag_min, lag + lag_cb[contour_index][k],
  3047. lag_max)
  3048. ]]></artwork>
  3049. </figure>
  3050. where lag_min and lag_max are the values from the "Minimum Lag" and
  3051. "Maximum Lag" columns of <xref target="silk_abs_pitch_low_pdf"/>,
  3052. respectively.
  3053. </t>
  3054. </section>
  3055. <section anchor="silk_ltp_coeffs" title="LTP Filter Coefficients">
  3056. <t>
  3057. SILK can use a separate 5-tap pitch filter for each subframe.
  3058. It selects the filter to use from one of three codebooks.
  3059. The three codebooks each represent different rate-distortion trade-offs, with
  3060. average rates of 1.61&nbsp;bits/subframe, 3.68&nbsp;bits/subframe, and
  3061. 4.85&nbsp;bits/subframe, respectively.
  3062. </t>
  3063. <t>
  3064. The importance of the filter coefficients generally depends on two factors: the
  3065. periodicity of the signal and relative energy between the current subframe and
  3066. the signal from one period earlier.
  3067. Greater periodicity and decaying energy both lead to more important filter
  3068. coefficients, and thus should be coded with lower distortion and higher rate.
  3069. These properties are relatively stable over the duration of a single SILK
  3070. frame, hence all of the subframes in a SILK frame must choose their filter
  3071. from the same codebook.
  3072. This is signaled with an explicitly-coded "periodicity index".
  3073. This immediately follows the subframe pitch lags, and is coded using the
  3074. 3-entry PDF from <xref target="silk_perindex_pdf"/>.
  3075. </t>
  3076. <texttable anchor="silk_perindex_pdf" title="Periodicity Index PDF">
  3077. <ttcol>PDF</ttcol>
  3078. <c>{77, 80, 99}/256</c>
  3079. </texttable>
  3080. <t>
  3081. The index of the filter to use for each subframe follows.
  3082. They are all coded using the PDF from <xref target="silk_ltp_filter_pdfs"/>
  3083. corresponding to the periodicity index.
  3084. <xref target="silk_ltp_filter_coeffs0"/> through
  3085. <xref target="silk_ltp_filter_coeffs2"/> contain the corresponding filter taps
  3086. as signed Q7 integers.
  3087. </t>
  3088. <texttable anchor="silk_ltp_filter_pdfs" title="LTP Filter PDFs">
  3089. <ttcol>Periodicity Index</ttcol>
  3090. <ttcol align="right">Codebook Size</ttcol>
  3091. <ttcol>PDF</ttcol>
  3092. <c>0</c> <c>8</c> <c>{185, 15, 13, 13, 9, 9, 6, 6}/256</c>
  3093. <c>1</c> <c>16</c> <c>{57, 34, 21, 20, 15, 13, 12, 13,
  3094. 10, 10, 9, 10, 9, 8, 7, 8}/256</c>
  3095. <c>2</c> <c>32</c> <c>{15, 16, 14, 12, 12, 12, 11, 11,
  3096. 11, 10, 9, 9, 9, 9, 8, 8,
  3097. 8, 8, 7, 7, 6, 6, 5, 4,
  3098. 5, 4, 4, 4, 3, 4, 3, 2}/256</c>
  3099. </texttable>
  3100. <texttable anchor="silk_ltp_filter_coeffs0"
  3101. title="Codebook Vectors for LTP Filter, Periodicity Index 0">
  3102. <ttcol>Index</ttcol>
  3103. <ttcol align="right">Filter Taps (Q7)</ttcol>
  3104. <c>0</c>
  3105. <c><spanx style="vbare">&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;24&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;5</spanx></c>
  3106. <c>1</c>
  3107. <c><spanx style="vbare">&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;0</spanx></c>
  3108. <c>2</c>
  3109. <c><spanx style="vbare">&nbsp;12&nbsp;&nbsp;28&nbsp;&nbsp;41&nbsp;&nbsp;13&nbsp;&nbsp;-4</spanx></c>
  3110. <c>3</c>
  3111. <c><spanx style="vbare">&nbsp;-9&nbsp;&nbsp;15&nbsp;&nbsp;42&nbsp;&nbsp;25&nbsp;&nbsp;14</spanx></c>
  3112. <c>4</c>
  3113. <c><spanx style="vbare">&nbsp;&nbsp;1&nbsp;&nbsp;-2&nbsp;&nbsp;62&nbsp;&nbsp;41&nbsp;&nbsp;-9</spanx></c>
  3114. <c>5</c>
  3115. <c><spanx style="vbare">-10&nbsp;&nbsp;37&nbsp;&nbsp;65&nbsp;&nbsp;-4&nbsp;&nbsp;&nbsp;3</spanx></c>
  3116. <c>6</c>
  3117. <c><spanx style="vbare">&nbsp;-6&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;66&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;-8</spanx></c>
  3118. <c>7</c>
  3119. <c><spanx style="vbare">&nbsp;16&nbsp;&nbsp;14&nbsp;&nbsp;38&nbsp;&nbsp;-3&nbsp;&nbsp;33</spanx></c>
  3120. </texttable>
  3121. <texttable anchor="silk_ltp_filter_coeffs1"
  3122. title="Codebook Vectors for LTP Filter, Periodicity Index 1">
  3123. <ttcol>Index</ttcol>
  3124. <ttcol align="right">Filter Taps (Q7)</ttcol>
  3125. <c>0</c>
  3126. <c><spanx style="vbare">&nbsp;13&nbsp;&nbsp;22&nbsp;&nbsp;39&nbsp;&nbsp;23&nbsp;&nbsp;12</spanx></c>
  3127. <c>1</c>
  3128. <c><spanx style="vbare">&nbsp;-1&nbsp;&nbsp;36&nbsp;&nbsp;64&nbsp;&nbsp;27&nbsp;&nbsp;-6</spanx></c>
  3129. <c>2</c>
  3130. <c><spanx style="vbare">&nbsp;-7&nbsp;&nbsp;10&nbsp;&nbsp;55&nbsp;&nbsp;43&nbsp;&nbsp;17</spanx></c>
  3131. <c>3</c>
  3132. <c><spanx style="vbare">&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;1</spanx></c>
  3133. <c>4</c>
  3134. <c><spanx style="vbare">&nbsp;&nbsp;6&nbsp;-11&nbsp;&nbsp;74&nbsp;&nbsp;53&nbsp;&nbsp;-9</spanx></c>
  3135. <c>5</c>
  3136. <c><spanx style="vbare">-12&nbsp;&nbsp;55&nbsp;&nbsp;76&nbsp;-12&nbsp;&nbsp;&nbsp;8</spanx></c>
  3137. <c>6</c>
  3138. <c><spanx style="vbare">&nbsp;-3&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;93&nbsp;&nbsp;27&nbsp;&nbsp;-4</spanx></c>
  3139. <c>7</c>
  3140. <c><spanx style="vbare">&nbsp;26&nbsp;&nbsp;39&nbsp;&nbsp;59&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;-8</spanx></c>
  3141. <c>8</c>
  3142. <c><spanx style="vbare">&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;77&nbsp;&nbsp;11&nbsp;&nbsp;&nbsp;9</spanx></c>
  3143. <c>9</c>
  3144. <c><spanx style="vbare">&nbsp;-8&nbsp;&nbsp;22&nbsp;&nbsp;44&nbsp;&nbsp;-6&nbsp;&nbsp;&nbsp;7</spanx></c>
  3145. <c>10</c>
  3146. <c><spanx style="vbare">&nbsp;40&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;26&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;9</spanx></c>
  3147. <c>11</c>
  3148. <c><spanx style="vbare">&nbsp;-7&nbsp;&nbsp;20&nbsp;101&nbsp;&nbsp;-7&nbsp;&nbsp;&nbsp;4</spanx></c>
  3149. <c>12</c>
  3150. <c><spanx style="vbare">&nbsp;&nbsp;3&nbsp;&nbsp;-8&nbsp;&nbsp;42&nbsp;&nbsp;26&nbsp;&nbsp;&nbsp;0</spanx></c>
  3151. <c>13</c>
  3152. <c><spanx style="vbare">-15&nbsp;&nbsp;33&nbsp;&nbsp;68&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;23</spanx></c>
  3153. <c>14</c>
  3154. <c><spanx style="vbare">&nbsp;-2&nbsp;&nbsp;55&nbsp;&nbsp;46&nbsp;&nbsp;-2&nbsp;&nbsp;15</spanx></c>
  3155. <c>15</c>
  3156. <c><spanx style="vbare">&nbsp;&nbsp;3&nbsp;&nbsp;-1&nbsp;&nbsp;21&nbsp;&nbsp;16&nbsp;&nbsp;41</spanx></c>
  3157. </texttable>
  3158. <texttable anchor="silk_ltp_filter_coeffs2"
  3159. title="Codebook Vectors for LTP Filter, Periodicity Index 2">
  3160. <ttcol>Index</ttcol>
  3161. <ttcol align="right">Filter Taps (Q7)</ttcol>
  3162. <c>0</c>
  3163. <c><spanx style="vbare">&nbsp;-6&nbsp;&nbsp;27&nbsp;&nbsp;61&nbsp;&nbsp;39&nbsp;&nbsp;&nbsp;5</spanx></c>
  3164. <c>1</c>
  3165. <c><spanx style="vbare">-11&nbsp;&nbsp;42&nbsp;&nbsp;88&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;1</spanx></c>
  3166. <c>2</c>
  3167. <c><spanx style="vbare">&nbsp;-2&nbsp;&nbsp;60&nbsp;&nbsp;65&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;-4</spanx></c>
  3168. <c>3</c>
  3169. <c><spanx style="vbare">&nbsp;-1&nbsp;&nbsp;-5&nbsp;&nbsp;73&nbsp;&nbsp;56&nbsp;&nbsp;&nbsp;1</spanx></c>
  3170. <c>4</c>
  3171. <c><spanx style="vbare">&nbsp;-9&nbsp;&nbsp;19&nbsp;&nbsp;94&nbsp;&nbsp;29&nbsp;&nbsp;-9</spanx></c>
  3172. <c>5</c>
  3173. <c><spanx style="vbare">&nbsp;&nbsp;0&nbsp;&nbsp;12&nbsp;&nbsp;99&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;4</spanx></c>
  3174. <c>6</c>
  3175. <c><spanx style="vbare">&nbsp;&nbsp;8&nbsp;-19&nbsp;102&nbsp;&nbsp;46&nbsp;-13</spanx></c>
  3176. <c>7</c>
  3177. <c><spanx style="vbare">&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;13&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;2</spanx></c>
  3178. <c>8</c>
  3179. <c><spanx style="vbare">&nbsp;&nbsp;9&nbsp;-21&nbsp;&nbsp;84&nbsp;&nbsp;72&nbsp;-18</spanx></c>
  3180. <c>9</c>
  3181. <c><spanx style="vbare">-11&nbsp;&nbsp;46&nbsp;104&nbsp;-22&nbsp;&nbsp;&nbsp;8</spanx></c>
  3182. <c>10</c>
  3183. <c><spanx style="vbare">&nbsp;18&nbsp;&nbsp;38&nbsp;&nbsp;48&nbsp;&nbsp;23&nbsp;&nbsp;&nbsp;0</spanx></c>
  3184. <c>11</c>
  3185. <c><spanx style="vbare">-16&nbsp;&nbsp;70&nbsp;&nbsp;83&nbsp;-21&nbsp;&nbsp;11</spanx></c>
  3186. <c>12</c>
  3187. <c><spanx style="vbare">&nbsp;&nbsp;5&nbsp;-11&nbsp;117&nbsp;&nbsp;22&nbsp;&nbsp;-8</spanx></c>
  3188. <c>13</c>
  3189. <c><spanx style="vbare">&nbsp;-6&nbsp;&nbsp;23&nbsp;117&nbsp;-12&nbsp;&nbsp;&nbsp;3</spanx></c>
  3190. <c>14</c>
  3191. <c><spanx style="vbare">&nbsp;&nbsp;3&nbsp;&nbsp;-8&nbsp;&nbsp;95&nbsp;&nbsp;28&nbsp;&nbsp;&nbsp;4</spanx></c>
  3192. <c>15</c>
  3193. <c><spanx style="vbare">-10&nbsp;&nbsp;15&nbsp;&nbsp;77&nbsp;&nbsp;60&nbsp;-15</spanx></c>
  3194. <c>16</c>
  3195. <c><spanx style="vbare">&nbsp;-1&nbsp;&nbsp;&nbsp;4&nbsp;124&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;-4</spanx></c>
  3196. <c>17</c>
  3197. <c><spanx style="vbare">&nbsp;&nbsp;3&nbsp;&nbsp;38&nbsp;&nbsp;84&nbsp;&nbsp;24&nbsp;-25</spanx></c>
  3198. <c>18</c>
  3199. <c><spanx style="vbare">&nbsp;&nbsp;2&nbsp;&nbsp;13&nbsp;&nbsp;42&nbsp;&nbsp;13&nbsp;&nbsp;31</spanx></c>
  3200. <c>19</c>
  3201. <c><spanx style="vbare">&nbsp;21&nbsp;&nbsp;-4&nbsp;&nbsp;56&nbsp;&nbsp;46&nbsp;&nbsp;-1</spanx></c>
  3202. <c>20</c>
  3203. <c><spanx style="vbare">&nbsp;-1&nbsp;&nbsp;35&nbsp;&nbsp;79&nbsp;-13&nbsp;&nbsp;19</spanx></c>
  3204. <c>21</c>
  3205. <c><spanx style="vbare">&nbsp;-7&nbsp;&nbsp;65&nbsp;&nbsp;88&nbsp;&nbsp;-9&nbsp;-14</spanx></c>
  3206. <c>22</c>
  3207. <c><spanx style="vbare">&nbsp;20&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;81&nbsp;&nbsp;49&nbsp;-29</spanx></c>
  3208. <c>23</c>
  3209. <c><spanx style="vbare">&nbsp;20&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;75&nbsp;&nbsp;&nbsp;3&nbsp;-17</spanx></c>
  3210. <c>24</c>
  3211. <c><spanx style="vbare">&nbsp;&nbsp;5&nbsp;&nbsp;-9&nbsp;&nbsp;44&nbsp;&nbsp;92&nbsp;&nbsp;-8</spanx></c>
  3212. <c>25</c>
  3213. <c><spanx style="vbare">&nbsp;&nbsp;1&nbsp;&nbsp;-3&nbsp;&nbsp;22&nbsp;&nbsp;69&nbsp;&nbsp;31</spanx></c>
  3214. <c>26</c>
  3215. <c><spanx style="vbare">&nbsp;-6&nbsp;&nbsp;95&nbsp;&nbsp;41&nbsp;-12&nbsp;&nbsp;&nbsp;5</spanx></c>
  3216. <c>27</c>
  3217. <c><spanx style="vbare">&nbsp;39&nbsp;&nbsp;67&nbsp;&nbsp;16&nbsp;&nbsp;-4&nbsp;&nbsp;&nbsp;1</spanx></c>
  3218. <c>28</c>
  3219. <c><spanx style="vbare">&nbsp;&nbsp;0&nbsp;&nbsp;-6&nbsp;120&nbsp;&nbsp;55&nbsp;-36</spanx></c>
  3220. <c>29</c>
  3221. <c><spanx style="vbare">-13&nbsp;&nbsp;44&nbsp;122&nbsp;&nbsp;&nbsp;4&nbsp;-24</spanx></c>
  3222. <c>30</c>
  3223. <c><spanx style="vbare">&nbsp;81&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;11&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;7</spanx></c>
  3224. <c>31</c>
  3225. <c><spanx style="vbare">&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;10&nbsp;&nbsp;88</spanx></c>
  3226. </texttable>
  3227. </section>
  3228. <section anchor="silk_ltp_scaling" title="LTP Scaling Parameter">
  3229. <t>
  3230. In some circumstances an LTP scaling parameter appears after the LTP filter
  3231. coefficients.
  3232. This allows the encoder to trade off the prediction gain between
  3233. packets against the recovery time after packet loss.
  3234. Like the quantization gains, only the first LBRR frame in an Opus frame,
  3235. an LBRR frame where the prior LBRR frame was not coded, and the first regular
  3236. SILK frame in each channel of an Opus frame include this field, and, like all
  3237. of the other LTP parameters, only for frames that are also voiced.
  3238. Unlike absolute-coding for pitch lags, a regular SILK frame other than the
  3239. first one in a channel will not include this field even if the prior frame was
  3240. not voiced.
  3241. </t>
  3242. <t>
  3243. If present, the value is coded using the 3-entry PDF in
  3244. <xref target="silk_ltp_scaling_pdf"/>.
  3245. The three possible values represent Q14 scale factors of 15565, 12288, and
  3246. 8192, respectively (corresponding to approximately 0.95, 0.75, and 0.5).
  3247. Frames that do not code the scaling parameter use the default factor of 15565
  3248. (approximately 0.95).
  3249. </t>
  3250. <texttable anchor="silk_ltp_scaling_pdf"
  3251. title="PDF for LTP Scaling Parameter">
  3252. <ttcol align="left">PDF</ttcol>
  3253. <c>{128, 64, 64}/256</c>
  3254. </texttable>
  3255. </section>
  3256. </section>
  3257. <section anchor="silk_seed" title="Linear Congruential Generator (LCG) Seed">
  3258. <t>
  3259. SILK uses a linear congruential generator (LCG) to inject pseudorandom noise
  3260. into the quantized excitation.
  3261. To ensure synchronization of this process between the encoder and decoder, each
  3262. SILK frame stores a 2-bit seed after the LTP parameters (if any).
  3263. The encoder may consider the choice of this seed during quantization, meaning
  3264. the flexibility to choose the LCG seed can reduce distortion.
  3265. The seed is decoded with the uniform 4-entry PDF in
  3266. <xref target="silk_seed_pdf"/>, yielding a value between 0 and 3, inclusive.
  3267. </t>
  3268. <texttable anchor="silk_seed_pdf"
  3269. title="PDF for LCG Seed">
  3270. <ttcol align="left">PDF</ttcol>
  3271. <c>{64, 64, 64, 64}/256</c>
  3272. </texttable>
  3273. </section>
  3274. <section anchor="silk_excitation" title="Excitation">
  3275. <t>
  3276. SILK codes the excitation using a modified version of the Pyramid Vector
  3277. Quantization (PVQ) codebook <xref target="PVQ"/>.
  3278. The PVQ codebook is designed for Laplace-distributed values and consists of all
  3279. sums of K signed, unit pulses in a vector of dimension N, where two pulses at
  3280. the same position are required to have the same sign.
  3281. Thus the codebook includes all integer codevectors y of dimension N that
  3282. satisfy
  3283. <figure align="center">
  3284. <artwork align="center"><![CDATA[
  3285. N-1
  3286. __
  3287. \ abs(y[j]) = K .
  3288. /_
  3289. j=0
  3290. ]]></artwork>
  3291. </figure>
  3292. Unlike regular PVQ, SILK uses a variable-length, rather than fixed-length,
  3293. encoding.
  3294. This encoding is better suited to the more Gaussian-like distribution of the
  3295. coefficient magnitudes and the non-uniform distribution of their signs (caused
  3296. by the quantization offset described below).
  3297. SILK also handles large codebooks by coding the least significant bits (LSb's)
  3298. of each coefficient directly.
  3299. This adds a small coding efficiency loss, but greatly reduces the computation
  3300. time and ROM size required for decoding, as implemented in
  3301. silk_decode_pulses() (silk_decode_pulses.c).
  3302. </t>
  3303. <t>
  3304. SILK fixes the dimension of the codebook to N&nbsp;=&nbsp;16.
  3305. The excitation is made up of a number of "shell blocks", each 16 samples in
  3306. size.
  3307. <xref target="silk_shell_block_table"/> lists the number of shell blocks
  3308. required for a SILK frame for each possible audio bandwidth and frame size.
  3309. 10&nbsp;ms MB frames nominally contain 120&nbsp;samples (10&nbsp;ms at
  3310. 12&nbsp;kHz), which is not a multiple of 16.
  3311. This is handled by coding 8 shell blocks (128 samples) and discarding the final
  3312. 8 samples of the last block.
  3313. The decoder contains no special case that prevents an encoder from placing
  3314. pulses in these samples, and they must be correctly parsed from the bitstream
  3315. if present, but they are otherwise ignored.
  3316. </t>
  3317. <texttable anchor="silk_shell_block_table"
  3318. title="Number of Shell Blocks Per SILK Frame">
  3319. <ttcol>Audio Bandwidth</ttcol>
  3320. <ttcol>Frame Size</ttcol>
  3321. <ttcol align="right">Number of Shell Blocks</ttcol>
  3322. <c>NB</c> <c>10&nbsp;ms</c> <c>5</c>
  3323. <c>MB</c> <c>10&nbsp;ms</c> <c>8</c>
  3324. <c>WB</c> <c>10&nbsp;ms</c> <c>10</c>
  3325. <c>NB</c> <c>20&nbsp;ms</c> <c>10</c>
  3326. <c>MB</c> <c>20&nbsp;ms</c> <c>15</c>
  3327. <c>WB</c> <c>20&nbsp;ms</c> <c>20</c>
  3328. </texttable>
  3329. <section anchor="silk_rate_level" title="Rate Level">
  3330. <t>
  3331. The first symbol in the excitation is a "rate level", which is an index from 0
  3332. to 8, inclusive, coded using the PDF in <xref target="silk_rate_level_pdfs"/>
  3333. corresponding to the signal type of the current frame (from
  3334. <xref target="silk_frame_type"/>).
  3335. The rate level selects the PDF used to decode the number of pulses in
  3336. the individual shell blocks.
  3337. It does not directly convey any information about the bitrate or the number of
  3338. pulses itself, but merely changes the probability of the symbols in
  3339. <xref target="silk_pulse_counts"/>.
  3340. Level&nbsp;0 provides a more efficient encoding at low rates generally, and
  3341. level&nbsp;8 provides a more efficient encoding at high rates generally,
  3342. though the most efficient level for a particular SILK frame may depend on the
  3343. exact distribution of the coded symbols.
  3344. An encoder should, but is not required to, use the most efficient rate level.
  3345. </t>
  3346. <texttable anchor="silk_rate_level_pdfs"
  3347. title="PDFs for the Rate Level">
  3348. <ttcol>Signal Type</ttcol>
  3349. <ttcol>PDF</ttcol>
  3350. <c>Inactive or Unvoiced</c>
  3351. <c>{15, 51, 12, 46, 45, 13, 33, 27, 14}/256</c>
  3352. <c>Voiced</c>
  3353. <c>{33, 30, 36, 17, 34, 49, 18, 21, 18}/256</c>
  3354. </texttable>
  3355. </section>
  3356. <section anchor="silk_pulse_counts" title="Pulses Per Shell Block">
  3357. <t>
  3358. The total number of pulses in each of the shell blocks follows the rate level.
  3359. The pulse counts for all of the shell blocks are coded consecutively, before
  3360. the content of any of the blocks.
  3361. Each block may have anywhere from 0 to 16 pulses, inclusive, coded using the
  3362. 18-entry PDF in <xref target="silk_pulse_count_pdfs"/> corresponding to the
  3363. rate level from <xref target="silk_rate_level"/>.
  3364. The special value 17 indicates that this block has one or more additional
  3365. LSb's to decode for each coefficient.
  3366. If the decoder encounters this value, it decodes another value for the actual
  3367. pulse count of the block, but uses the PDF corresponding to the special rate
  3368. level&nbsp;9 instead of the normal rate level.
  3369. This process repeats until the decoder reads a value less than 17, and it then
  3370. sets the number of extra LSb's used to the number of 17's decoded for that
  3371. block.
  3372. If it reads the value 17 ten times, then the next iteration uses the special
  3373. rate level&nbsp;10 instead of 9.
  3374. The probability of decoding a 17 when using the PDF for rate level&nbsp;10 is
  3375. zero, ensuring that the number of LSb's for a block will not exceed 10.
  3376. The cumulative distribution for rate level&nbsp;10 is just a shifted version of
  3377. that for 9 and thus does not require any additional storage.
  3378. </t>
  3379. <texttable anchor="silk_pulse_count_pdfs"
  3380. title="PDFs for the Pulse Count">
  3381. <ttcol>Rate Level</ttcol>
  3382. <ttcol>PDF</ttcol>
  3383. <c>0</c>
  3384. <c>{131, 74, 25, 8, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}/256</c>
  3385. <c>1</c>
  3386. <c>{58, 93, 60, 23, 7, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}/256</c>
  3387. <c>2</c>
  3388. <c>{43, 51, 46, 33, 24, 16, 11, 8, 6, 3, 3, 3, 2, 1, 1, 2, 1, 2}/256</c>
  3389. <c>3</c>
  3390. <c>{17, 52, 71, 57, 31, 12, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}/256</c>
  3391. <c>4</c>
  3392. <c>{6, 21, 41, 53, 49, 35, 21, 11, 6, 3, 2, 2, 1, 1, 1, 1, 1, 1}/256</c>
  3393. <c>5</c>
  3394. <c>{7, 14, 22, 28, 29, 28, 25, 20, 17, 13, 11, 9, 7, 5, 4, 4, 3, 10}/256</c>
  3395. <c>6</c>
  3396. <c>{2, 5, 14, 29, 42, 46, 41, 31, 19, 11, 6, 3, 2, 1, 1, 1, 1, 1}/256</c>
  3397. <c>7</c>
  3398. <c>{1, 2, 4, 10, 19, 29, 35, 37, 34, 28, 20, 14, 8, 5, 4, 2, 2, 2}/256</c>
  3399. <c>8</c>
  3400. <c>{1, 2, 2, 5, 9, 14, 20, 24, 27, 28, 26, 23, 20, 15, 11, 8, 6, 15}/256</c>
  3401. <c>9</c>
  3402. <c>{1, 1, 1, 6, 27, 58, 56, 39, 25, 14, 10, 6, 3, 3, 2, 1, 1, 2}/256</c>
  3403. <c>10</c>
  3404. <c>{2, 1, 6, 27, 58, 56, 39, 25, 14, 10, 6, 3, 3, 2, 1, 1, 2, 0}/256</c>
  3405. </texttable>
  3406. </section>
  3407. <section anchor="silk_pulse_locations" title="Pulse Location Decoding">
  3408. <t>
  3409. The locations of the pulses in each shell block follows the pulse counts,
  3410. as decoded by silk_shell_decoder() (silk_shell_coder.c).
  3411. As with the pulse counts, these locations are coded for all the shell blocks
  3412. before any of the remaining information for each block.
  3413. Unlike many other codecs, SILK places no restriction on the distribution of
  3414. pulses within a shell block.
  3415. All of the pulses may be placed in a single location, or each one in a unique
  3416. location, or anything in between.
  3417. </t>
  3418. <t>
  3419. The location of pulses is coded by recursively partitioning each block into
  3420. halves, and coding how many pulses fall on the left side of the split.
  3421. All remaining pulses must fall on the right side of the split.
  3422. The process then recurses into the left half, and after that returns, the
  3423. right half (preorder traversal).
  3424. The PDF to use is chosen by the size of the current partition (16, 8, 4, or 2)
  3425. and the number of pulses in the partition (1 to 16, inclusive).
  3426. <xref target="silk_shell_code3_pdfs"/> through
  3427. <xref target="silk_shell_code0_pdfs"/> list the PDFs used for each partition
  3428. size and pulse count.
  3429. This process skips partitions without any pulses, i.e., where the initial pulse
  3430. count from <xref target="silk_pulse_counts"/> was zero, or where the split in
  3431. the prior level indicated that all of the pulses fell on the other side.
  3432. These partitions have nothing to code, so they require no PDF.
  3433. </t>
  3434. <texttable anchor="silk_shell_code3_pdfs"
  3435. title="PDFs for Pulse Count Split, 16 Sample Partitions">
  3436. <ttcol>Pulse Count</ttcol>
  3437. <ttcol>PDF</ttcol>
  3438. <c>1</c> <c>{126, 130}/256</c>
  3439. <c>2</c> <c>{56, 142, 58}/256</c>
  3440. <c>3</c> <c>{25, 101, 104, 26}/256</c>
  3441. <c>4</c> <c>{12, 60, 108, 64, 12}/256</c>
  3442. <c>5</c> <c>{7, 35, 84, 87, 37, 6}/256</c>
  3443. <c>6</c> <c>{4, 20, 59, 86, 63, 21, 3}/256</c>
  3444. <c>7</c> <c>{3, 12, 38, 72, 75, 42, 12, 2}/256</c>
  3445. <c>8</c> <c>{2, 8, 25, 54, 73, 59, 27, 7, 1}/256</c>
  3446. <c>9</c> <c>{2, 5, 17, 39, 63, 65, 42, 18, 4, 1}/256</c>
  3447. <c>10</c> <c>{1, 4, 12, 28, 49, 63, 54, 30, 11, 3, 1}/256</c>
  3448. <c>11</c> <c>{1, 4, 8, 20, 37, 55, 57, 41, 22, 8, 2, 1}/256</c>
  3449. <c>12</c> <c>{1, 3, 7, 15, 28, 44, 53, 48, 33, 16, 6, 1, 1}/256</c>
  3450. <c>13</c> <c>{1, 2, 6, 12, 21, 35, 47, 48, 40, 25, 12, 5, 1, 1}/256</c>
  3451. <c>14</c> <c>{1, 1, 4, 10, 17, 27, 37, 47, 43, 33, 21, 9, 4, 1, 1}/256</c>
  3452. <c>15</c> <c>{1, 1, 1, 8, 14, 22, 33, 40, 43, 38, 28, 16, 8, 1, 1, 1}/256</c>
  3453. <c>16</c> <c>{1, 1, 1, 1, 13, 18, 27, 36, 41, 41, 34, 24, 14, 1, 1, 1, 1}/256</c>
  3454. </texttable>
  3455. <texttable anchor="silk_shell_code2_pdfs"
  3456. title="PDFs for Pulse Count Split, 8 Sample Partitions">
  3457. <ttcol>Pulse Count</ttcol>
  3458. <ttcol>PDF</ttcol>
  3459. <c>1</c> <c>{127, 129}/256</c>
  3460. <c>2</c> <c>{53, 149, 54}/256</c>
  3461. <c>3</c> <c>{22, 105, 106, 23}/256</c>
  3462. <c>4</c> <c>{11, 61, 111, 63, 10}/256</c>
  3463. <c>5</c> <c>{6, 35, 86, 88, 36, 5}/256</c>
  3464. <c>6</c> <c>{4, 20, 59, 87, 62, 21, 3}/256</c>
  3465. <c>7</c> <c>{3, 13, 40, 71, 73, 41, 13, 2}/256</c>
  3466. <c>8</c> <c>{3, 9, 27, 53, 70, 56, 28, 9, 1}/256</c>
  3467. <c>9</c> <c>{3, 8, 19, 37, 57, 61, 44, 20, 6, 1}/256</c>
  3468. <c>10</c> <c>{3, 7, 15, 28, 44, 54, 49, 33, 17, 5, 1}/256</c>
  3469. <c>11</c> <c>{1, 7, 13, 22, 34, 46, 48, 38, 28, 14, 4, 1}/256</c>
  3470. <c>12</c> <c>{1, 1, 11, 22, 27, 35, 42, 47, 33, 25, 10, 1, 1}/256</c>
  3471. <c>13</c> <c>{1, 1, 6, 14, 26, 37, 43, 43, 37, 26, 14, 6, 1, 1}/256</c>
  3472. <c>14</c> <c>{1, 1, 4, 10, 20, 31, 40, 42, 40, 31, 20, 10, 4, 1, 1}/256</c>
  3473. <c>15</c> <c>{1, 1, 3, 8, 16, 26, 35, 38, 38, 35, 26, 16, 8, 3, 1, 1}/256</c>
  3474. <c>16</c> <c>{1, 1, 2, 6, 12, 21, 30, 36, 38, 36, 30, 21, 12, 6, 2, 1, 1}/256</c>
  3475. </texttable>
  3476. <texttable anchor="silk_shell_code1_pdfs"
  3477. title="PDFs for Pulse Count Split, 4 Sample Partitions">
  3478. <ttcol>Pulse Count</ttcol>
  3479. <ttcol>PDF</ttcol>
  3480. <c>1</c> <c>{127, 129}/256</c>
  3481. <c>2</c> <c>{49, 157, 50}/256</c>
  3482. <c>3</c> <c>{20, 107, 109, 20}/256</c>
  3483. <c>4</c> <c>{11, 60, 113, 62, 10}/256</c>
  3484. <c>5</c> <c>{7, 36, 84, 87, 36, 6}/256</c>
  3485. <c>6</c> <c>{6, 24, 57, 82, 60, 23, 4}/256</c>
  3486. <c>7</c> <c>{5, 18, 39, 64, 68, 42, 16, 4}/256</c>
  3487. <c>8</c> <c>{6, 14, 29, 47, 61, 52, 30, 14, 3}/256</c>
  3488. <c>9</c> <c>{1, 15, 23, 35, 51, 50, 40, 30, 10, 1}/256</c>
  3489. <c>10</c> <c>{1, 1, 21, 32, 42, 52, 46, 41, 18, 1, 1}/256</c>
  3490. <c>11</c> <c>{1, 6, 16, 27, 36, 42, 42, 36, 27, 16, 6, 1}/256</c>
  3491. <c>12</c> <c>{1, 5, 12, 21, 31, 38, 40, 38, 31, 21, 12, 5, 1}/256</c>
  3492. <c>13</c> <c>{1, 3, 9, 17, 26, 34, 38, 38, 34, 26, 17, 9, 3, 1}/256</c>
  3493. <c>14</c> <c>{1, 3, 7, 14, 22, 29, 34, 36, 34, 29, 22, 14, 7, 3, 1}/256</c>
  3494. <c>15</c> <c>{1, 2, 5, 11, 18, 25, 31, 35, 35, 31, 25, 18, 11, 5, 2, 1}/256</c>
  3495. <c>16</c> <c>{1, 1, 4, 9, 15, 21, 28, 32, 34, 32, 28, 21, 15, 9, 4, 1, 1}/256</c>
  3496. </texttable>
  3497. <texttable anchor="silk_shell_code0_pdfs"
  3498. title="PDFs for Pulse Count Split, 2 Sample Partitions">
  3499. <ttcol>Pulse Count</ttcol>
  3500. <ttcol>PDF</ttcol>
  3501. <c>1</c> <c>{128, 128}/256</c>
  3502. <c>2</c> <c>{42, 172, 42}/256</c>
  3503. <c>3</c> <c>{21, 107, 107, 21}/256</c>
  3504. <c>4</c> <c>{12, 60, 112, 61, 11}/256</c>
  3505. <c>5</c> <c>{8, 34, 86, 86, 35, 7}/256</c>
  3506. <c>6</c> <c>{8, 23, 55, 90, 55, 20, 5}/256</c>
  3507. <c>7</c> <c>{5, 15, 38, 72, 72, 36, 15, 3}/256</c>
  3508. <c>8</c> <c>{6, 12, 27, 52, 77, 47, 20, 10, 5}/256</c>
  3509. <c>9</c> <c>{6, 19, 28, 35, 40, 40, 35, 28, 19, 6}/256</c>
  3510. <c>10</c> <c>{4, 14, 22, 31, 37, 40, 37, 31, 22, 14, 4}/256</c>
  3511. <c>11</c> <c>{3, 10, 18, 26, 33, 38, 38, 33, 26, 18, 10, 3}/256</c>
  3512. <c>12</c> <c>{2, 8, 13, 21, 29, 36, 38, 36, 29, 21, 13, 8, 2}/256</c>
  3513. <c>13</c> <c>{1, 5, 10, 17, 25, 32, 38, 38, 32, 25, 17, 10, 5, 1}/256</c>
  3514. <c>14</c> <c>{1, 4, 7, 13, 21, 29, 35, 36, 35, 29, 21, 13, 7, 4, 1}/256</c>
  3515. <c>15</c> <c>{1, 2, 5, 10, 17, 25, 32, 36, 36, 32, 25, 17, 10, 5, 2, 1}/256</c>
  3516. <c>16</c> <c>{1, 2, 4, 7, 13, 21, 28, 34, 36, 34, 28, 21, 13, 7, 4, 2, 1}/256</c>
  3517. </texttable>
  3518. </section>
  3519. <section anchor="silk_shell_lsb" title="LSb Decoding">
  3520. <t>
  3521. After the decoder reads the pulse locations for all blocks, it reads the LSb's
  3522. (if any) for each block in turn.
  3523. Inside each block, it reads all the LSb's for each coefficient in turn, even
  3524. those where no pulses were allocated, before proceeding to the next one.
  3525. They are coded from most significant to least significant, and they all use the
  3526. PDF in <xref target="silk_shell_lsb_pdf"/>.
  3527. </t>
  3528. <texttable anchor="silk_shell_lsb_pdf" title="PDF for Excitation LSb's">
  3529. <ttcol>PDF</ttcol>
  3530. <c>{136, 120}/256</c>
  3531. </texttable>
  3532. <t>
  3533. The number of LSb's read for each coefficient in a block is determined in
  3534. <xref target="silk_pulse_counts"/>.
  3535. The magnitude of the coefficient is initially equal to the number of pulses
  3536. placed at that location in <xref target="silk_pulse_locations"/>.
  3537. As each LSb is decoded, the magnitude is doubled, and then the value of the LSb
  3538. added to it, to obtain an updated magnitude.
  3539. </t>
  3540. </section>
  3541. <section anchor="silk_signs" title="Sign Decoding">
  3542. <t>
  3543. After decoding the pulse locations and the LSb's, the decoder knows the
  3544. magnitude of each coefficient in the excitation.
  3545. It then decodes a sign for all coefficients with a non-zero magnitude, using
  3546. one of the PDFs from <xref target="silk_sign_pdfs"/>.
  3547. If the value decoded is 0, then the coefficient magnitude is negated.
  3548. Otherwise, it remains positive.
  3549. </t>
  3550. <t>
  3551. The decoder chooses the PDF for the sign based on the signal type and
  3552. quantization offset type (from <xref target="silk_frame_type"/>) and the
  3553. number of pulses in the block (from <xref target="silk_pulse_counts"/>).
  3554. The number of pulses in the block does not take into account any LSb's.
  3555. If a block has no pulses, even if it has some LSb's (and thus may have some
  3556. non-zero coefficients), then no signs are decoded.
  3557. In that case, any non-zero coefficients use a positive sign.
  3558. </t>
  3559. <texttable anchor="silk_sign_pdfs"
  3560. title="PDFs for Excitation Signs">
  3561. <ttcol>Signal Type</ttcol>
  3562. <ttcol>Quantization Offset Type</ttcol>
  3563. <ttcol>Pulse Count</ttcol>
  3564. <ttcol>PDF</ttcol>
  3565. <c>Inactive</c> <c>Low</c> <c>1</c> <c>{207, 49}/256</c>
  3566. <c>Inactive</c> <c>Low</c> <c>2</c> <c>{189, 67}/256</c>
  3567. <c>Inactive</c> <c>Low</c> <c>3</c> <c>{179, 77}/256</c>
  3568. <c>Inactive</c> <c>Low</c> <c>4</c> <c>{174, 82}/256</c>
  3569. <c>Inactive</c> <c>Low</c> <c>5</c> <c>{163, 93}/256</c>
  3570. <c>Inactive</c> <c>Low</c> <c>6 or more</c> <c>{157, 99}/256</c>
  3571. <c>Inactive</c> <c>High</c> <c>1</c> <c>{245, 11}/256</c>
  3572. <c>Inactive</c> <c>High</c> <c>2</c> <c>{238, 18}/256</c>
  3573. <c>Inactive</c> <c>High</c> <c>3</c> <c>{232, 24}/256</c>
  3574. <c>Inactive</c> <c>High</c> <c>4</c> <c>{225, 31}/256</c>
  3575. <c>Inactive</c> <c>High</c> <c>5</c> <c>{220, 36}/256</c>
  3576. <c>Inactive</c> <c>High</c> <c>6 or more</c> <c>{211, 45}/256</c>
  3577. <c>Unvoiced</c> <c>Low</c> <c>1</c> <c>{210, 46}/256</c>
  3578. <c>Unvoiced</c> <c>Low</c> <c>2</c> <c>{190, 66}/256</c>
  3579. <c>Unvoiced</c> <c>Low</c> <c>3</c> <c>{178, 78}/256</c>
  3580. <c>Unvoiced</c> <c>Low</c> <c>4</c> <c>{169, 87}/256</c>
  3581. <c>Unvoiced</c> <c>Low</c> <c>5</c> <c>{162, 94}/256</c>
  3582. <c>Unvoiced</c> <c>Low</c> <c>6 or more</c> <c>{152, 104}/256</c>
  3583. <c>Unvoiced</c> <c>High</c> <c>1</c> <c>{242, 14}/256</c>
  3584. <c>Unvoiced</c> <c>High</c> <c>2</c> <c>{235, 21}/256</c>
  3585. <c>Unvoiced</c> <c>High</c> <c>3</c> <c>{224, 32}/256</c>
  3586. <c>Unvoiced</c> <c>High</c> <c>4</c> <c>{214, 42}/256</c>
  3587. <c>Unvoiced</c> <c>High</c> <c>5</c> <c>{205, 51}/256</c>
  3588. <c>Unvoiced</c> <c>High</c> <c>6 or more</c> <c>{190, 66}/256</c>
  3589. <c>Voiced</c> <c>Low</c> <c>1</c> <c>{162, 94}/256</c>
  3590. <c>Voiced</c> <c>Low</c> <c>2</c> <c>{152, 104}/256</c>
  3591. <c>Voiced</c> <c>Low</c> <c>3</c> <c>{147, 109}/256</c>
  3592. <c>Voiced</c> <c>Low</c> <c>4</c> <c>{144, 112}/256</c>
  3593. <c>Voiced</c> <c>Low</c> <c>5</c> <c>{141, 115}/256</c>
  3594. <c>Voiced</c> <c>Low</c> <c>6 or more</c> <c>{138, 118}/256</c>
  3595. <c>Voiced</c> <c>High</c> <c>1</c> <c>{203, 53}/256</c>
  3596. <c>Voiced</c> <c>High</c> <c>2</c> <c>{187, 69}/256</c>
  3597. <c>Voiced</c> <c>High</c> <c>3</c> <c>{176, 80}/256</c>
  3598. <c>Voiced</c> <c>High</c> <c>4</c> <c>{168, 88}/256</c>
  3599. <c>Voiced</c> <c>High</c> <c>5</c> <c>{161, 95}/256</c>
  3600. <c>Voiced</c> <c>High</c> <c>6 or more</c> <c>{154, 102}/256</c>
  3601. </texttable>
  3602. </section>
  3603. </section>
  3604. </section>
  3605. </section>
  3606. <section title="CELT Decoder">
  3607. <t>
  3608. The CELT layer is decoded based on the following symbols and sets of symbols:
  3609. </t>
  3610. <texttable anchor='table_example'>
  3611. <ttcol align='center'>Symbol(s)</ttcol>
  3612. <ttcol align='center'>PDF</ttcol>
  3613. <ttcol align='center'>Condition</ttcol>
  3614. <c>silence</c> <c>{32767, 1}/32768</c> <c></c>
  3615. <c>post-filter</c> <c>{1, 1}/2</c> <c></c>
  3616. <c>octave</c> <c>uniform (6)</c><c>post-filter</c>
  3617. <c>period</c> <c>raw bits (4+octave)</c><c>post-filter</c>
  3618. <c>gain</c> <c>raw bits (3)</c><c>post-filter</c>
  3619. <c>tapset</c> <c>{2, 1, 1}/4</c><c>post-filter</c>
  3620. <c>transient</c> <c>{7, 1}/8</c><c></c>
  3621. <c>intra</c> <c>{7, 1}/8</c><c></c>
  3622. <c>coarse energy</c><c><xref target="energy-decoding"/></c><c></c>
  3623. <c>tf_change</c> <c><xref target="transient-decoding"/></c><c></c>
  3624. <c>tf_select</c> <c>{1, 1}/2</c><c><xref target="transient-decoding"/></c>
  3625. <c>spread</c> <c>{7, 2, 21, 2}/32</c><c></c>
  3626. <c>dyn. alloc.</c> <c><xref target="allocation"/></c><c></c>
  3627. <c>alloc. trim</c> <c>{2, 2, 5, 10, 22, 46, 22, 10, 5, 2, 2}/128</c><c></c>
  3628. <c>skip</c> <c>{1, 1}/2</c><c><xref target="allocation"/></c>
  3629. <c>intensity</c> <c>uniform</c><c><xref target="allocation"/></c>
  3630. <c>dual</c> <c>{1, 1}/2</c><c></c>
  3631. <c>fine energy</c> <c><xref target="energy-decoding"/></c><c></c>
  3632. <c>residual</c> <c><xref target="PVQ-decoder"/></c><c></c>
  3633. <c>anti-collapse</c><c>{1, 1}/2</c><c><xref target="anti-collapse"/></c>
  3634. <c>finalize</c> <c><xref target="energy-decoding"/></c><c></c>
  3635. <postamble>Order of the symbols in the CELT section of the bitstream.</postamble>
  3636. </texttable>
  3637. <t>
  3638. The decoder extracts information from the range-coded bitstream in the order
  3639. described in the figure above. In some circumstances, it is
  3640. possible for a decoded value to be out of range due to a very small amount of redundancy
  3641. in the encoding of large integers by the range coder.
  3642. In that case, the decoder should assume there has been an error in the coding,
  3643. decoding, or transmission and SHOULD take measures to conceal the error and/or report
  3644. to the application that a problem has occurred.
  3645. </t>
  3646. <section anchor="transient-decoding" title="Transient Decoding">
  3647. <t>
  3648. The "transient" flag encoded in the bitstream has a probability of 1/8.
  3649. When it is set, then the MDCT coefficients represent multiple
  3650. short MDCTs in the frame. When not set, the coefficients represent a single
  3651. long MDCT for the frame. In addition to the global transient flag is a per-band
  3652. binary flag to change the time-frequency (tf) resolution independently in each band. The
  3653. change in tf resolution is defined in tf_select_table[][] in celt.c and depends
  3654. on the frame size, whether the transient flag is set, and the value of tf_select.
  3655. The tf_select flag uses a 1/2 probability, but is only decoded
  3656. if it can have an impact on the result knowing the value of all per-band
  3657. tf_change flags.
  3658. </t>
  3659. </section>
  3660. <section anchor="energy-decoding" title="Energy Envelope Decoding">
  3661. <t>
  3662. It is important to quantize the energy with sufficient resolution because
  3663. any energy quantization error cannot be compensated for at a later
  3664. stage. Regardless of the resolution used for encoding the shape of a band,
  3665. it is perceptually important to preserve the energy in each band. CELT uses a
  3666. three-step coarse-fine-fine strategy for encoding the energy in the base-2 log
  3667. domain, as implemented in quant_bands.c</t>
  3668. <section anchor="coarse-energy-decoding" title="Coarse energy decoding">
  3669. <t>
  3670. Coarse quantization of the energy uses a fixed resolution of 6 dB
  3671. (integer part of base-2 log). To minimize the bitrate, prediction is applied
  3672. both in time (using the previous frame) and in frequency (using the previous
  3673. bands). The part of the prediction that is based on the
  3674. previous frame can be disabled, creating an "intra" frame where the energy
  3675. is coded without reference to prior frames. The decoder first reads the intra flag
  3676. to determine what prediction is used.
  3677. The 2-D z-transform of
  3678. the prediction filter is:
  3679. <figure align="center">
  3680. <artwork align="center"><![CDATA[
  3681. -1 -1
  3682. (1 - alpha*z_l )*(1 - z_b )
  3683. A(z_l, z_b) = -----------------------------
  3684. -1
  3685. 1 - beta*z_b
  3686. ]]></artwork>
  3687. </figure>
  3688. where b is the band index and l is the frame index. The prediction coefficients
  3689. applied depend on the frame size in use when not using intra energy and are alpha=0, beta=4915/32768
  3690. when using intra energy.
  3691. The time-domain prediction is based on the final fine quantization of the previous
  3692. frame, while the frequency domain (within the current frame) prediction is based
  3693. on coarse quantization only (because the fine quantization has not been computed
  3694. yet). The prediction is clamped internally so that fixed point implementations with
  3695. limited dynamic range do not suffer desynchronization.
  3696. We approximate the ideal
  3697. probability distribution of the prediction error using a Laplace distribution
  3698. with separate parameters for each frame size in intra- and inter-frame modes. The
  3699. coarse energy quantization is performed by unquant_coarse_energy() and
  3700. unquant_coarse_energy_impl() (quant_bands.c). The encoding of the Laplace-distributed values is
  3701. implemented in ec_laplace_decode() (laplace.c).
  3702. </t>
  3703. </section>
  3704. <section anchor="fine-energy-decoding" title="Fine energy quantization">
  3705. <t>
  3706. The number of bits assigned to fine energy quantization in each band is determined
  3707. by the bit allocation computation described in <xref target="allocation"></xref>.
  3708. Let B_i be the number of fine energy bits
  3709. for band i; the refinement is an integer f in the range [0,2**B_i-1]. The mapping between f
  3710. and the correction applied to the coarse energy is equal to (f+1/2)/2**B_i - 1/2. Fine
  3711. energy quantization is implemented in quant_fine_energy() (quant_bands.c).
  3712. </t>
  3713. <t>
  3714. When some bits are left "unused" after all other flags have been decoded, these bits
  3715. are assigned to a "final" step of fine allocation. In effect, these bits are used
  3716. to add one extra fine energy bit per band per channel. The allocation process
  3717. determines two "priorities" for the final fine bits.
  3718. Any remaining bits are first assigned only to bands of priority 0, starting
  3719. from band 0 and going up. If all bands of priority 0 have received one bit per
  3720. channel, then bands of priority 1 are assigned an extra bit per channel,
  3721. starting from band 0. If any bits are left after this, they are left unused.
  3722. This is implemented in unquant_energy_finalise() (quant_bands.c).
  3723. </t>
  3724. </section> <!-- fine energy -->
  3725. </section> <!-- Energy decode -->
  3726. <section anchor="allocation" title="Bit allocation">
  3727. <t>Many codecs transmit significant amounts of side information for
  3728. the purpose of controlling bit allocation within a frame. Often this
  3729. side information controls bit usage indirectly and must be carefully
  3730. selected to achieve the desired rate constraints.</t>
  3731. <t>The band-energy normalized structure of Opus MDCT mode ensures that a
  3732. constant bit allocation for the shape content of a band will result in a
  3733. roughly constant tone to noise ratio, which provides for fairly consistent
  3734. perceptual performance. The effectiveness of this approach is the result of
  3735. two factors: that the band energy, which is understood to be perceptually
  3736. important on its own, is always preserved regardless of the shape precision, and because
  3737. the constant tone-to-noise ratio implies a constant intra-band noise to masking ratio.
  3738. Intra-band masking is the strongest of the perceptual masking effects. This structure
  3739. means that the ideal allocation is more consistent from frame to frame than
  3740. it is for other codecs without an equivalent structure.</t>
  3741. <t>Because the bit allocation is used to drive the decoding of the range-coder
  3742. stream, it MUST be recovered exactly so that identical coding decisions are
  3743. made in the encoder and decoder. Any deviation from the reference's resulting
  3744. bit allocation will result in corrupted output, though implementers are
  3745. free to implement the procedure in any way which produces identical results.</t>
  3746. <t>Because all of the information required to decode a frame must be derived
  3747. from that frame alone in order to retain robustness to packet loss, the
  3748. overhead of explicitly signaling the allocation would be considerable,
  3749. especially for low-latency (small frame size) applications,
  3750. even though the allocation is relatively static.</t>
  3751. <t>For this reason, in the MDCT mode Opus uses a primarily implicit bit
  3752. allocation. The available bitstream capacity is known in advance to both
  3753. the encoder and decoder without additional signaling, ultimately from the
  3754. packet sizes expressed by a higher-level protocol. Using this information
  3755. the codec interpolates an allocation from a hard-coded table.</t>
  3756. <t>While the band-energy structure effectively models intra-band masking,
  3757. it ignores the weaker inter-band masking, band-temporal masking, and
  3758. other less significant perceptual effects. While these effects can
  3759. often be ignored, they can become significant for particular samples. One
  3760. mechanism available to encoders would be to simply increase the overall
  3761. rate for these frames, but this is not possible in a constant rate mode
  3762. and can be fairly inefficient. As a result three explicitly signaled
  3763. mechanisms are provided to alter the implicit allocation:</t>
  3764. <t>
  3765. <list style="symbols">
  3766. <t>Band boost</t>
  3767. <t>Allocation trim</t>
  3768. <t>Band skipping</t>
  3769. </list>
  3770. </t>
  3771. <t>The first of these mechanisms, band boost, allows an encoder to boost
  3772. the allocation in specific bands. The second, allocation trim, works by
  3773. biasing the overall allocation towards higher or lower frequency bands. The third, band
  3774. skipping, selects which low-precision high frequency bands
  3775. will be allocated no shape bits at all.</t>
  3776. <t>In stereo mode there are two additional parameters
  3777. potentially coded as part of the allocation procedure: a parameter to allow the
  3778. selective elimination of allocation for the 'side' in jointly coded bands,
  3779. and a flag to deactivate joint coding. These values are not signaled if
  3780. they would be meaningless in the overall context of the allocation.</t>
  3781. <t>Because every signaled adjustment increases overhead and implementation
  3782. complexity, none were included speculatively: the reference encoder makes use
  3783. of all of these mechanisms. While the decision logic in the reference was
  3784. found to be effective enough to justify the overhead and complexity, further
  3785. analysis techniques may be discovered which increase the effectiveness of these
  3786. parameters. As with other signaled parameters, an encoder is free to choose the
  3787. values in any manner, but unless a technique is known to deliver superior
  3788. perceptual results the methods used by the reference implementation should be
  3789. used.</t>
  3790. <t>The allocation process consists of the following steps: determining the per-band
  3791. maximum allocation vector, decoding the boosts, decoding the tilt, determining
  3792. the remaining capacity of the frame, searching the mode table for the
  3793. entry nearest but not exceeding the available space (subject to the tilt, boosts, band
  3794. maximums, and band minimums), linear interpolation, reallocation of
  3795. unused bits with concurrent skip decoding, determination of the
  3796. fine-energy vs. shape split, and final reallocation. This process results
  3797. in a per-band shape allocation (in 1/8th bit units), a per-band fine-energy
  3798. allocation (in 1 bit per channel units), a set of band priorities for
  3799. controlling the use of remaining bits at the end of the frame, and a
  3800. remaining balance of unallocated space, which is usually zero except
  3801. at very high rates.</t>
  3802. <t>The maximum allocation vector is an approximation of the maximum space
  3803. that can be used by each band for a given mode. The value is
  3804. approximate because the shape encoding is variable rate (due
  3805. to entropy coding of splitting parameters). Setting the maximum too low reduces the
  3806. maximum achievable quality in a band while setting it too high
  3807. may result in waste: bitstream capacity available at the end
  3808. of the frame which can not be put to any use. The maximums
  3809. specified by the codec reflect the average maximum. In the reference
  3810. the maximums are provided in partially computed form, in order to fit in less
  3811. memory as a static table (XXX cache.caps). Implementations are expected
  3812. to simply use the same table data, but the procedure for generating
  3813. this table is included in rate.c as part of compute_pulse_cache().</t>
  3814. <t>To convert the values in cache.caps into the actual maximums: first
  3815. set nbBands to the maximum number of bands for this mode, and stereo to
  3816. zero if stereo is not in use and one otherwise. For each band set N
  3817. to the number of MDCT bins covered by the band (for one channel), set LM
  3818. to the shift value for the frame size (e.g. 0 for 120, 1 for 240, 3 for 480),
  3819. then set i to nbBands*(2*LM+stereo). Then set the maximum for the band to
  3820. the i-th index of cache.caps + 64 and multiply by the number of channels
  3821. in the current frame (one or two) and by N, then divide the result by 4
  3822. using truncating integer division. The resulting vector will be called
  3823. cap[]. The elements fit in signed 16-bit integers but do not fit in 8 bits.
  3824. This procedure is implemented in the reference in the function init_caps() in celt.c.
  3825. </t>
  3826. <t>The band boosts are represented by a series of binary symbols which
  3827. are coded with very low probability. Each band can potentially be boosted
  3828. multiple times, subject to the frame actually having enough room to obey
  3829. the boost and having enough room to code the boost symbol. The default
  3830. coding cost for a boost starts out at six bits, but subsequent boosts
  3831. in a band cost only a single bit and every time a band is boosted the
  3832. initial cost is reduced (down to a minimum of two). Since the initial
  3833. cost of coding a boost is 6 bits, the coding cost of the boost symbols when
  3834. completely unused is 0.48 bits/frame for a 21 band mode (21*-log2(1-1/2**6)).</t>
  3835. <t>To decode the band boosts: First set 'dynalloc_logp' to 6, the initial
  3836. amount of storage required to signal a boost in bits, 'total_bits' to the
  3837. size of the frame in 8th bits, 'total_boost' to zero, and 'tell' to the total number
  3838. of 8th bits decoded
  3839. so far. For each band from the coding start (0 normally, but 17 in hybrid mode)
  3840. to the coding end (which changes depending on the signaled bandwidth): set 'width'
  3841. to the number of MDCT bins in this band for all channels. Take the larger of width
  3842. and 64, then the minimum of that value and the width times eight and set 'quanta'
  3843. to the result. This represents a boost step size of six bits subject to limits
  3844. of 1/bit/sample and 1/8th bit/sample. Set 'boost' to zero and 'dynalloc_loop_logp'
  3845. to dynalloc_logp. While dynalloc_loop_log (the current worst case symbol cost) in
  3846. 8th bits plus tell is less than total_bits plus total_boost and boost is less than cap[] for this
  3847. band: Decode a bit from the bitstream with a with dynalloc_loop_logp as the cost
  3848. of a one, update tell to reflect the current used capacity, if the decoded value
  3849. is zero break the loop otherwise add quanta to boost and total_boost, subtract quanta from
  3850. total_bits, and set dynalloc_loop_log to 1. When the while loop finishes
  3851. boost contains the boost for this band. If boost is non-zero and dynalloc_logp
  3852. is greater than 2, decrease dynalloc_logp. Once this process has been
  3853. executed on all bands, the band boosts have been decoded. This procedure
  3854. is implemented around line 2352 of celt.c.</t>
  3855. <t>At very low rates it is possible that there won't be enough available
  3856. space to execute the inner loop even once. In these cases band boost
  3857. is not possible but its overhead is completely eliminated. Because of the
  3858. high cost of band boost when activated, a reasonable encoder should not be
  3859. using it at very low rates. The reference implements its dynalloc decision
  3860. logic around line 1269 of celt.c.</t>
  3861. <t>The allocation trim is a integer value from 0-10. The default value of
  3862. 5 indicates no trim. The trim parameter is entropy coded in order to
  3863. lower the coding cost of less extreme adjustments. Values lower than
  3864. 5 bias the allocation towards lower frequencies and values above 5
  3865. bias it towards higher frequencies. Like other signaled parameters, signaling
  3866. of the trim is gated so that it is not included if there is insufficient space
  3867. available in the bitstream. To decode the trim, first set
  3868. the trim value to 5, then iff the count of decoded 8th bits so far (ec_tell_frac)
  3869. plus 48 (6 bits) is less than or equal to the total frame size in 8th
  3870. bits minus total_boost (a product of the above band boost procedure),
  3871. decode the trim value using the inverse CDF {127, 126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0}.</t>
  3872. <t>For 10 ms and 20 ms frames using short blocks and that have at least LM+2 bits left prior to
  3873. the allocation process, then one anti-collapse bit is reserved in the allocation process so it can
  3874. be decoded later. Following the the anti-collapse reservation, one bit is reserved for skip if available.</t>
  3875. <t>For stereo frames, bits are reserved for intensity stereo and for dual stereo. Intensity stereo
  3876. requires ilog2(end-start) bits. Those bits are reserved if there is enough bits left. Following this, one
  3877. bit is reserved for dual stereo if available.</t>
  3878. <t>The allocation computation begins by setting up some initial conditions.
  3879. 'total' is set to the remaining available 8th bits, computed by taking the
  3880. size of the coded frame times 8 and subtracting ec_tell_frac(). From this value, one (8th bit)
  3881. is subtracted to ensure that the resulting allocation will be conservative. 'anti_collapse_rsv'
  3882. is set to 8 (8th bits) iff the frame is a transient, LM is greater than 1, and total is
  3883. greater than or equal to (LM+2) * 8. Total is then decremented by anti_collapse_rsv and clamped
  3884. to be equal to or greater than zero. 'skip_rsv' is set to 8 (8th bits) if total is greater than
  3885. 8, otherwise it is zero. Total is then decremented by skip_rsv. This reserves space for the
  3886. final skipping flag.</t>
  3887. <t>If the current frame is stereo, intensity_rsv is set to the conservative log2 in 8th bits
  3888. of the number of coded bands for this frame (given by the table LOG2_FRAC_TABLE). If
  3889. intensity_rsv is greater than total then intensity_rsv is set to zero. Otherwise total is
  3890. decremented by intensity_rsv, and if total is still greater than 8, dual_stereo_rsv is
  3891. set to 8 and total is decremented by dual_stereo_rsv.</t>
  3892. <t>The allocation process then computes a vector representing the hard minimum amounts allocation
  3893. any band will receive for shape. This minimum is higher than the technical limit of the PVQ
  3894. process, but very low rate allocations produce an excessively sparse spectrum and these bands
  3895. are better served by having no allocation at all. For each coded band, set thresh[band] to
  3896. twenty-four times the number of MDCT bins in the band and divide by 16. If 8 times the number
  3897. of channels is greater, use that instead. This sets the minimum allocation to one bit per channel
  3898. or 48 128th bits per MDCT bin, whichever is greater. The band-size dependent part of this
  3899. value is not scaled by the channel count, because at the very low rates where this limit is
  3900. applicable there will usually be no bits allocated to the side.</t>
  3901. <t>The previously decoded allocation trim is used to derive a vector of per-band adjustments,
  3902. 'trim_offsets[]'. For each coded band take the alloc_trim and subtract 5 and LM. Then multiply
  3903. the result by the number of channels, the number of MDCT bins in the shortest frame size for this mode,
  3904. the number of remaining bands, 2**LM, and 8. Then divide this value by 64. Finally, if the
  3905. number of MDCT bins in the band per channel is only one, 8 times the number of channels is subtracted
  3906. in order to diminish the allocation by one bit, because width 1 bands receive greater benefit
  3907. from the coarse energy coding.</t>
  3908. </section>
  3909. <section anchor="PVQ-decoder" title="Shape Decoder">
  3910. <t>
  3911. In each band, the normalized "shape" is encoded
  3912. using a vector quantization scheme called a "pyramid vector quantizer".
  3913. </t>
  3914. <t>In
  3915. the simplest case, the number of bits allocated in
  3916. <xref target="allocation"></xref> is converted to a number of pulses as described
  3917. by <xref target="bits-pulses"></xref>. Knowing the number of pulses and the
  3918. number of samples in the band, the decoder calculates the size of the codebook
  3919. as detailed in <xref target="cwrs-decoder"></xref>. The size is used to decode
  3920. an unsigned integer (uniform probability model), which is the codeword index.
  3921. This index is converted into the corresponding vector as explained in
  3922. <xref target="cwrs-decoder"></xref>. This vector is then scaled to unit norm.
  3923. </t>
  3924. <section anchor="bits-pulses" title="Bits to Pulses">
  3925. <t>
  3926. Although the allocation is performed in 1/8th bit units, the quantization requires
  3927. an integer number of pulses K. To do this, the encoder searches for the value
  3928. of K that produces the number of bits nearest to the allocated value
  3929. (rounding down if exactly halfway between two values), not to exceed
  3930. the total number of bits available. For efficiency reasons, the search is performed against a
  3931. precomputed allocation table which only permits some K values for each N. The number of
  3932. codebook entries can be computed as explained in <xref target="cwrs-decoder"></xref>. The difference
  3933. between the number of bits allocated and the number of bits used is accumulated to a
  3934. "balance" (initialized to zero) that helps adjust the
  3935. allocation for the next bands. One third of the balance is applied to the
  3936. bit allocation of each band to help achieve the target allocation. The only
  3937. exceptions are the band before the last and the last band, for which half the balance
  3938. and the whole balance are applied, respectively.
  3939. </t>
  3940. </section>
  3941. <section anchor="cwrs-decoder" title="Index Decoding">
  3942. <t>
  3943. The codeword is decoded as a uniformly-distributed integer value
  3944. by decode_pulses() (cwrs.c).
  3945. The codeword is converted from a unique index in the same way specified in
  3946. <xref target="PVQ"></xref>. The indexing is based on the calculation of V(N,K)
  3947. (denoted N(L,K) in <xref target="PVQ"></xref>), which is the number of possible
  3948. combinations of K pulses
  3949. in N samples. The number of combinations can be computed recursively as
  3950. V(N,K) = V(N-1,K) + V(N,K-1) + V(N-1,K-1), with V(N,0) = 1 and V(0,K) = 0, K != 0.
  3951. There are many different ways to compute V(N,K), including precomputed tables and direct
  3952. use of the recursive formulation. The reference implementation applies the recursive
  3953. formulation one line (or column) at a time to save on memory use,
  3954. along with an alternate,
  3955. univariate recurrence to initialize an arbitrary line, and direct
  3956. polynomial solutions for small N. All of these methods are
  3957. equivalent, and have different trade-offs in speed, memory usage, and
  3958. code size. Implementations MAY use any methods they like, as long as
  3959. they are equivalent to the mathematical definition.
  3960. </t>
  3961. <t>
  3962. The decoding of the codeword from the index is performed as specified in
  3963. <xref target="PVQ"></xref>, as implemented in function
  3964. decode_pulses() (cwrs.c). The decoded codeword is then normalised such that it's
  3965. L2-norm equals one.
  3966. </t>
  3967. </section>
  3968. <section anchor="spreading" title="Spreading">
  3969. <t>
  3970. The normalised vector decoded in <xref target="cwrs-decoder"/> is then rotated
  3971. for the purpose of avoiding tonal artefacts. The rotation gain is equal to
  3972. <figure align="center">
  3973. <artwork align="center"><![CDATA[
  3974. g_r = N / (N + f_r*K)
  3975. ]]></artwork>
  3976. </figure>
  3977. where N is the number of dimensions, K is the number of pulses, and f_r depends on
  3978. the value of the "spread" parameter in the bit-stream.
  3979. </t>
  3980. <texttable anchor="spread values" title="Spreading values">
  3981. <ttcol>Spread value</ttcol>
  3982. <ttcol>f_r</ttcol>
  3983. <c>0</c> <c>infinite (no rotation)</c>
  3984. <c>1</c> <c>15</c>
  3985. <c>2</c> <c>10</c>
  3986. <c>3</c> <c>5</c>
  3987. </texttable>
  3988. <t>
  3989. The rotation angle is then calculated as
  3990. <figure align="center">
  3991. <artwork align="center"><![CDATA[
  3992. 2
  3993. pi * g_r
  3994. theta = ----------
  3995. 4
  3996. ]]></artwork>
  3997. </figure>
  3998. A 2-D rotation R(i,j) between points x_i and x_j is defined as:
  3999. <figure align="center">
  4000. <artwork align="center"><![CDATA[
  4001. x_i' = cos(theta)*x_i + sin(theta)*x_j
  4002. x_j' = -sin(theta)*x_i + cos(theta)*x_j
  4003. ]]></artwork>
  4004. </figure>
  4005. An N-D rotation is then achieved by applying a series of 2-D rotations back and forth, in the
  4006. following order: R(x_1, x_2), R(x_2, x_3), ..., R(x_N-2, X_N-1), R(x_N-1, X_N),
  4007. R(x_N-2, X_N-1), ..., R(x_1, x_2).
  4008. </t>
  4009. <t>
  4010. If the decoded vector represents more
  4011. than one time block, then the following process is applied separately on each time block.
  4012. </t>
  4013. </section>
  4014. <section anchor="split" title="Split decoding">
  4015. <t>
  4016. To avoid the need for multi-precision calculations when decoding PVQ codevectors,
  4017. the maximum size allowed for codebooks is 32 bits. When larger codebooks are
  4018. needed, the vector is instead split in two sub-vectors of size N/2.
  4019. A quantized gain parameter with precision
  4020. derived from the current allocation is entropy coded to represent the relative
  4021. gains of each side of the split, and the entire decoding process is recursively
  4022. applied. Multiple levels of splitting may be applied up to a frame size
  4023. dependent limit. The same recursive mechanism is applied for the joint coding
  4024. of stereo audio.
  4025. </t>
  4026. </section>
  4027. <section anchor="tf-change" title="Time-Frequency change">
  4028. <t>
  4029. The time-frequency (TF) parameters are used to control the time-frequency resolution tradeoff
  4030. in each coded band. For each band, there are two possible TF choices. For the first
  4031. band coded, the PDF is {3, 1}/4 for frames marked as transient and {15, 1}/16 for
  4032. the other frames. For subsequent bands, the TF choice is coded relative to the
  4033. previous TF choice with probability {15, 1}/15 for transient frames and {31, 1}/32
  4034. otherwise. The mapping between the decoded TF choices and the adjustment in TF
  4035. resolution is shown in the tables below.
  4036. </t>
  4037. <texttable anchor='tf_00'>
  4038. <ttcol align='center'>Frame size (ms)</ttcol>
  4039. <ttcol align='center'>0</ttcol>
  4040. <ttcol align='center'>1</ttcol>
  4041. <c>2.5</c> <c>0</c> <c>-1</c>
  4042. <c>5</c> <c>0</c> <c>-1</c>
  4043. <c>10</c> <c>0</c> <c>-2</c>
  4044. <c>20</c> <c>0</c> <c>-2</c>
  4045. <postamble>TF adjustments for non-transient frames and tf_select=0</postamble>
  4046. </texttable>
  4047. <texttable anchor='tf_01'>
  4048. <ttcol align='center'>Frame size (ms)</ttcol>
  4049. <ttcol align='center'>0</ttcol>
  4050. <ttcol align='center'>1</ttcol>
  4051. <c>2.5</c> <c>0</c> <c>-1</c>
  4052. <c>5</c> <c>0</c> <c>-2</c>
  4053. <c>10</c> <c>0</c> <c>-3</c>
  4054. <c>20</c> <c>0</c> <c>-3</c>
  4055. <postamble>TF adjustments for non-transient frames and tf_select=1</postamble>
  4056. </texttable>
  4057. <texttable anchor='tf_10'>
  4058. <ttcol align='center'>Frame size (ms)</ttcol>
  4059. <ttcol align='center'>0</ttcol>
  4060. <ttcol align='center'>1</ttcol>
  4061. <c>2.5</c> <c>0</c> <c>-1</c>
  4062. <c>5</c> <c>1</c> <c>0</c>
  4063. <c>10</c> <c>2</c> <c>0</c>
  4064. <c>20</c> <c>3</c> <c>0</c>
  4065. <postamble>TF adjustments for transient frames and tf_select=0</postamble>
  4066. </texttable>
  4067. <texttable anchor='tf_11'>
  4068. <ttcol align='center'>Frame size (ms)</ttcol>
  4069. <ttcol align='center'>0</ttcol>
  4070. <ttcol align='center'>1</ttcol>
  4071. <c>2.5</c> <c>0</c> <c>-1</c>
  4072. <c>5</c> <c>1</c> <c>-1</c>
  4073. <c>10</c> <c>1</c> <c>-1</c>
  4074. <c>20</c> <c>1</c> <c>-1</c>
  4075. <postamble>TF adjustments for transient frames and tf_select=1</postamble>
  4076. </texttable>
  4077. <t>
  4078. A negative TF adjustment means that the temporal resolution is increased,
  4079. while a positive TF adjustment means that the frequency resolution is increased.
  4080. Changes in TF resolution are implemented using the Hadamard transform. To increase
  4081. the time resolution by N, N "levels" of the Hadamard transform are applied to the
  4082. decoded vector for each interleaved MDCT vector. To increase the frequency resolution
  4083. (assumes a transient frame), then N levels of the Hadamard transform are applied
  4084. <spanx style="emph">across</spanx> the interleaved MDCT vector. In the case of increased
  4085. time resolution the decoder uses the "sequency order" because the input vector
  4086. is sorted in time.
  4087. </t>
  4088. </section>
  4089. </section>
  4090. <section anchor="anti-collapse" title="Anti-collapse processing">
  4091. <t>
  4092. When the frame has the transient bit set, an anti-collapse bit is decoded.
  4093. When anti-collapse is set, the energy in each small MDCT is prevented
  4094. from collapsing to zero. For each band of each MDCT where a collapse is
  4095. detected, a pseudo-random signal is inserted with an energy corresponding
  4096. to the min energy over the two previous frames. A renormalization step is
  4097. then required to ensure that the anti-collapse step did not alter the
  4098. energy preservation property.
  4099. </t>
  4100. </section>
  4101. <section anchor="denormalization" title="Denormalization">
  4102. <t>
  4103. Just like each band was normalized in the encoder, the last step of the decoder before
  4104. the inverse MDCT is to denormalize the bands. Each decoded normalized band is
  4105. multiplied by the square root of the decoded energy. This is done by denormalise_bands()
  4106. (bands.c).
  4107. </t>
  4108. </section>
  4109. <section anchor="inverse-mdct" title="Inverse MDCT">
  4110. <t>The inverse MDCT implementation has no special characteristics. The
  4111. input is N frequency-domain samples and the output is 2*N time-domain
  4112. samples, while scaling by 1/2. A "low-overlap" window is used to reduce the algorithmic delay.
  4113. It is derived from a basic (full overlap) 240-sample version of the window used by the Vorbis codec:
  4114. <figure align="center">
  4115. <artwork align="center"><![CDATA[
  4116. 2
  4117. / /pi /pi n + 1/2\ \ \
  4118. W(n) = |sin|-- * sin|-- * -------| | | .
  4119. \ \2 \2 L / / /
  4120. ]]></artwork>
  4121. </figure>
  4122. The low-overlap window is created by zero-padding the basic window and inserting ones in the
  4123. middle, such that the resulting window still satisfies power complementarity. The IMDCT and
  4124. windowing are performed by mdct_backward (mdct.c).
  4125. </t>
  4126. <section anchor="post-filter" title="Post-filter">
  4127. <t>
  4128. The output of the inverse MDCT (after weighted overlap-add) is sent to the
  4129. post-filter. Although the post-filter is applied at the end, the post-filter
  4130. parameters are encoded at the beginning, just after the silence flag.
  4131. The post-filter can be switched on or off using one bit (logp=1).
  4132. If the post-filter is enabled, then the octave is decoded as an integer value
  4133. between 0 and 6 of uniform probability. Once the octave is known, the fine pitch
  4134. within the octave is decoded using 4+octave raw bits. The final pitch period
  4135. is equal to (16&lt;&lt;octave)+fine_pitch-1 so it is bounded between 15 and 1022,
  4136. inclusively. Next, the gain is decoded as three raw bits and is equal to
  4137. G=3*(int_gain+1)/32. The set of post-filter taps is decoded last, using
  4138. a pdf equal to {2, 1, 1}/4. Tapset zero corresponds to the filter coefficients
  4139. g0 = 0.3066406250, g1 = 0.2170410156, g2 = 0.1296386719. Tapset one
  4140. corresponds to the filter coefficients g0 = 0.4638671875, g1 = 0.2680664062,
  4141. g2 = 0, and tapset two uses filter coefficients g0 = 0.7998046875,
  4142. g1 = 0.1000976562, g2 = 0.
  4143. </t>
  4144. <t>
  4145. The post-filter response is thus computed as:
  4146. <figure align="center">
  4147. <artwork align="center">
  4148. <![CDATA[
  4149. y(n) = x(n) + G*(g0*y(n-T) + g1*(y(n-T+1)+y(n-T+1))
  4150. + g2*(y(n-T+2)+y(n-T+2)))
  4151. ]]>
  4152. </artwork>
  4153. </figure>
  4154. During a transition between different gains, a smooth transition is calculated
  4155. using the square of the MDCT window. It is important that values of y(n) be
  4156. interpolated one at a time such that the past value of y(n) used is interpolated.
  4157. </t>
  4158. </section>
  4159. <section anchor="deemphasis" title="De-emphasis">
  4160. <t>
  4161. After the post-filter,
  4162. the signal is de-emphasized using the inverse of the pre-emphasis filter
  4163. used in the encoder:
  4164. <figure align="center">
  4165. <artwork align="center"><![CDATA[
  4166. 1 1
  4167. ---- = --------------- ,
  4168. A(z) -1
  4169. 1 - alpha_p*z
  4170. ]]></artwork>
  4171. </figure>
  4172. where alpha_p=0.8500061035.
  4173. </t>
  4174. </section>
  4175. </section>
  4176. <section anchor="Packet Loss Concealment" title="Packet Loss Concealment (PLC)">
  4177. <t>
  4178. Packet loss concealment (PLC) is an optional decoder-side feature which
  4179. SHOULD be included when transmitting over an unreliable channel. Because
  4180. PLC is not part of the bitstream, there are several possible ways to
  4181. implement PLC with different complexity/quality trade-offs. The PLC in
  4182. the reference implementation finds a periodicity in the decoded
  4183. signal and repeats the windowed waveform using the pitch offset. The windowed
  4184. waveform is overlapped in such a way as to preserve the time-domain aliasing
  4185. cancellation with the previous frame and the next frame. This is implemented
  4186. in celt_decode_lost() (mdct.c).
  4187. </t>
  4188. </section>
  4189. </section>
  4190. <section anchor="switching" title="Mode Switching">
  4191. <t>
  4192. Switching between the Opus coding modes requires careful consideration. More
  4193. specifically, the transitions that cannot be easily handled are the ones where
  4194. the lower frequencies have to switch between the SILK LP-based model and the CELT
  4195. transform model. If nothing is done, a glitch will occur for these transitions.
  4196. On the other hand, switching between the SILK-only modes and the hybrid mode
  4197. does not require any special treatment.
  4198. </t>
  4199. <t>
  4200. There are two ways to avoid or reduce glitches during the problematic mode
  4201. transitions: with side information or without it. Only transitions with side
  4202. information are normatively specified. For transitions with no side
  4203. information, it is RECOMMENDED for the decoder to use a concealment technique
  4204. (e.g. make use of the PLC algorithm) to "fill in"
  4205. the gap or discontinuity caused by the mode transition. Note that this
  4206. concealment MUST NOT be applied when switching between the SILK mode and the
  4207. hybrid mode or vice versa. Similarly, it MUST NOT be applied when merely
  4208. changing the bandwidth within the same mode.
  4209. </t>
  4210. <section anchor="side-info" title="Switching Side Information">
  4211. <t>
  4212. Switching with side information involves transmitting in-band a 5-ms
  4213. "redundant" CELT frame within the Opus frame.
  4214. This frame is designed to fill in the gap or discontinuity without requiring
  4215. the decoder to conceal it. For transitions from a CELT-only frame to a
  4216. SILK-only or hybrid frame, the redundant frame is inserted in the frame
  4217. following the transition (i.e. the SILK-only/hybrid frame). For transitions
  4218. from a SILK-only/hybrid frame to a CELT-only frame, the redundant frame is
  4219. inserted in the first frame. For all SILK-only and hybrid frames (not only
  4220. those involved in a mode transition), a binary symbol of probability 2^-12
  4221. needs to be decoded just after the SILK part of the bitstream. When the
  4222. symbol value is 1, the frame then includes an embedded redundant frame. The
  4223. redundant frame always starts and ends on a byte boundary. For SILK-only
  4224. frames, the number of bytes is simply the number of whole remaining bytes.
  4225. For hybrid frames, the number of bytes is equal to 2, plus a decoded unsigned
  4226. integer (ec_dec_uint()) between 0 and 255. For hybrid frames, the redundant
  4227. frame is placed at the end of the frame, after the CELT layer of the
  4228. hybrid frame. The redundant frame is decoded like any other CELT-only frame,
  4229. with the exception that it does not contain a TOC byte. The bandwidth
  4230. is instead set to the same bandwidth of the current frame (for MB
  4231. frames, the redundant frame is set to WB).
  4232. </t>
  4233. <t>
  4234. For CELT-only to SILK-only/hybrid transitions, the first
  4235. 2.5 ms of the redundant frame is used as-is for the reconstructed
  4236. output. The remaining 2.5 ms is overlapped and added (cross-faded using
  4237. the square of the MDCT power-complementary window) to the decoded SILK/hybrid
  4238. signal, ensuring a smooth transition. For SILK-only/hyrid to CELT-only
  4239. transitions, only the second half of the 5-ms decoded redundant frame is used.
  4240. In that case, only a 2.5-ms cross-fade is applied, still using the
  4241. power-complementary window.
  4242. </t>
  4243. </section>
  4244. </section>
  4245. </section>
  4246. <!-- ******************************************************************* -->
  4247. <!-- ************************** OPUS ENCODER *********************** -->
  4248. <!-- ******************************************************************* -->
  4249. <section title="Codec Encoder">
  4250. <t>
  4251. Opus encoder block diagram.
  4252. <figure>
  4253. <artwork>
  4254. <![CDATA[
  4255. +----------+ +-------+
  4256. | sample | | SILK |
  4257. +->| rate |--->|encoder|--+
  4258. | |conversion| | | |
  4259. audio | +----------+ +-------+ | +-------+
  4260. ------+ +--->| Range |
  4261. | +------------+ +-------+ |encoder|---->
  4262. | | Delay | | CELT | +--->| | bitstream
  4263. +->|Compensation|->|encoder|--+ +-------+
  4264. | | | |
  4265. +------------+ +-------+
  4266. ]]>
  4267. </artwork>
  4268. </figure>
  4269. </t>
  4270. <section anchor="range-encoder" title="Range Coder">
  4271. <t>
  4272. The range coder also acts as the bit-packer for Opus. It is
  4273. used in three different ways, to encode:
  4274. <list style="symbols">
  4275. <t>entropy-coded symbols with a fixed probability model using ec_encode(), (entenc.c)</t>
  4276. <t>integers from 0 to 2**M-1 using ec_enc_uint() or ec_enc_bits(), (entenc.c)</t>
  4277. <t>integers from 0 to N-1 (where N is not a power of two) using ec_enc_uint(). (entenc.c)</t>
  4278. </list>
  4279. </t>
  4280. <t>
  4281. The range encoder maintains an internal state vector composed of the
  4282. four-tuple (low,rng,rem,ext) representing the low end of the current
  4283. range, the size of the current range, a single buffered output octet,
  4284. and a count of additional carry-propagating output octets. Both rng
  4285. and low are 32-bit unsigned integer values, rem is an octet value or
  4286. the special value -1, and ext is an integer with at least 16 bits.
  4287. This state vector is initialized at the start of each each frame to
  4288. the value (0,2**31,-1,0). The reference implementation re-uses the
  4289. 'val' field of the entropy coder structure to hold low, in order to
  4290. allow the same structure to be used for encoding and decoding, but
  4291. we maintain the distinction here for clarity.
  4292. </t>
  4293. <section anchor="encoding-symbols" title="Encoding Symbols">
  4294. <t>
  4295. The main encoding function is ec_encode() (entenc.c),
  4296. which takes as an argument a three-tuple (fl,fh,ft)
  4297. describing the range of the symbol to be encoded in the current
  4298. context, with 0 &lt;= fl &lt; fh &lt;= ft &lt;= 65535. The values of this tuple
  4299. are derived from the probability model for the symbol. Let f(i) be
  4300. the frequency of the i'th symbol in the current context. Then the
  4301. three-tuple corresponding to the k'th symbol is given by
  4302. <![CDATA[
  4303. fl=sum(f(i),i<k), fh=fl+f(i), and ft=sum(f(i)).
  4304. ]]>
  4305. </t>
  4306. <t>
  4307. ec_encode() updates the state of the encoder as follows. If fl is
  4308. greater than zero, then low = low + rng - (rng/ft)*(ft-fl) and
  4309. rng = (rng/ft)*(fh-fl). Otherwise, low is unchanged and
  4310. rng = rng - (rng/ft)*(fh-fl). The divisions here are exact integer
  4311. division. After this update, the range is normalized.
  4312. </t>
  4313. <t>
  4314. To normalize the range, the following process is repeated until
  4315. rng &gt; 2**23. First, the top 9 bits of low, (low&gt;&gt;23), are placed into
  4316. a carry buffer. Then, low is set to <![CDATA[(low << 8 & 0x7FFFFFFF) and rng
  4317. is set to (rng<<8)]]>. This process is carried out by
  4318. ec_enc_normalize() (entenc.c).
  4319. </t>
  4320. <t>
  4321. The 9 bits produced in each iteration of the normalization loop
  4322. consist of 8 data bits and a carry flag. The final value of the
  4323. output bits is not determined until carry propagation is accounted
  4324. for. Therefore the reference implementation buffers a single
  4325. (non-propagating) output octet and keeps a count of additional
  4326. propagating (0xFF) output octets. An implementation MAY choose to use
  4327. any mathematically equivalent scheme to perform carry propagation.
  4328. </t>
  4329. <t>
  4330. The function ec_enc_carry_out() (entenc.c) performs
  4331. this buffering. It takes a 9-bit input value, c, from the normalization:
  4332. 8 bits of output and a carry bit. If c is 0xFF, then ext is incremented
  4333. and no octets are output. Otherwise, if rem is not the special value
  4334. -1, then the octet (rem+(c>>8)) is output. Then ext octets are output
  4335. with the value 0 if the carry bit is set, or 0xFF if it is not, and
  4336. rem is set to the lower 8 bits of c. After this, ext is set to zero.
  4337. </t>
  4338. <t>
  4339. In the reference implementation, a special version of ec_encode()
  4340. called ec_encode_bin() (entenc.c) is defined to
  4341. take a two-tuple (fl,ftb), where <![CDATA[0 <= fl < 2**ftb and ftb < 16. It is
  4342. mathematically equivalent to calling ec_encode() with the three-tuple
  4343. (fl,fl+1,1<<ftb)]]>, but avoids using division.
  4344. </t>
  4345. </section>
  4346. <section anchor="encoding-bits" title="Encoding Raw Bits">
  4347. <t>
  4348. The CELT layer also allows directly encoding a series of raw bits, outside
  4349. of the range coder, implemented in ec_enc_bits() (entenc.c).
  4350. The raw bits are packed at the end of the packet, starting by storing the
  4351. least significant bit of the value to be packed in the least significant bit
  4352. of the last byte, filling up to the most significant bit in
  4353. the last byte, and then continuing in the least significant bit of the
  4354. penultimate byte, and so on.
  4355. This packing may continue into the last byte output by the range coder,
  4356. though the format should render it impossible to overwrite any set bit
  4357. produced by the range coder when the procedure in
  4358. <xref target='encoder-finalizing'/> is followed to finalize the stream.
  4359. </t>
  4360. </section>
  4361. <section anchor="encoding-ints" title="Encoding Uniformly Distributed Integers">
  4362. <t>
  4363. The function ec_enc_uint() is based on ec_encode() and encodes one of N
  4364. equiprobable symbols, each with a frequency of 1, where N may be as large as
  4365. 2**32-1. Because ec_encode() is limited to a total frequency of 2**16-1, this
  4366. is done by encoding a series of symbols in smaller contexts.
  4367. </t>
  4368. <t>
  4369. ec_enc_uint() (entenc.c) takes a two-tuple (fl,ft),
  4370. where ft is not necessarily a power of two. Let ftb be the location
  4371. of the highest 1 bit in the two's-complement representation of
  4372. (ft-1), or -1 if no bits are set. If ftb>8, then the top 8 bits of fl
  4373. are encoded using ec_encode() with the three-tuple
  4374. (fl>>ftb-8,(fl>>ftb-8)+1,(ft-1>>ftb-8)+1), and the remaining bits
  4375. are encoded as raw bits. Otherwise, fl is encoded with ec_encode() directly
  4376. using the three-tuple (fl,fl+1,ft).
  4377. </t>
  4378. </section>
  4379. <section anchor="encoder-finalizing" title="Finalizing the Stream">
  4380. <t>
  4381. After all symbols are encoded, the stream must be finalized by
  4382. outputting a value inside the current range. Let end be the integer
  4383. in the interval [low,low+rng) with the largest number of trailing
  4384. zero bits, b, such that end+(1&lt;&lt;b)-1 is also in the interval
  4385. [low,low+rng). Then while end is not zero, the top 9 bits of end, e.g.,
  4386. <![CDATA[(end>>23), are sent to the carry buffer, and end is replaced by
  4387. (end<<8&0x7FFFFFFF). Finally, if the value in carry buffer, rem, is]]>
  4388. neither zero nor the special value -1, or the carry count, ext, is
  4389. greater than zero, then 9 zero bits are sent to the carry buffer.
  4390. After the carry buffer is finished outputting octets, the rest of the
  4391. output buffer (if any) is padded with zero bits, until it reaches the raw
  4392. bits. Finally, rem is set to the
  4393. special value -1. This process is implemented by ec_enc_done()
  4394. (entenc.c).
  4395. </t>
  4396. </section>
  4397. <section anchor="encoder-tell" title="Current Bit Usage">
  4398. <t>
  4399. The bit allocation routines in Opus need to be able to determine a
  4400. conservative upper bound on the number of bits that have been used
  4401. to encode the current frame thus far. This drives allocation
  4402. decisions and ensures that the range coder and raw bits will not
  4403. overflow the output buffer. This is computed in the
  4404. reference implementation to whole-bit precision by
  4405. the function ec_tell() (entcode.h) and to fractional 1/8th bit
  4406. precision by the function ec_tell_frac() (entcode.c).
  4407. Like all operations in the range coder, it must be implemented in a
  4408. bit-exact manner, and must produce exactly the same value returned by
  4409. the same functions in the decoder after decoding the same symbols.
  4410. </t>
  4411. </section>
  4412. </section>
  4413. <section title='SILK Encoder'>
  4414. <t>
  4415. In the following, we focus on the core encoder and describe its components. For simplicity, we will refer to the core encoder simply as the encoder in the remainder of this document. An overview of the encoder is given in <xref target="encoder_figure" />.
  4416. </t>
  4417. <figure align="center" anchor="encoder_figure">
  4418. <artwork align="center">
  4419. <![CDATA[
  4420. +---+
  4421. +----------------------------->| |
  4422. +---------+ | +---------+ | |
  4423. |Voice | | |LTP | | |
  4424. +----->|Activity |-----+ +---->|Scaling |---------+--->| |
  4425. | |Detector | 3 | | |Control |<+ 12 | | |
  4426. | +---------+ | | +---------+ | | | |
  4427. | | | +---------+ | | | |
  4428. | | | |Gains | | 11 | | |
  4429. | | | +->|Processor|-|---+---|--->| R |
  4430. | | | | | | | | | | a |
  4431. | \/ | | +---------+ | | | | n |
  4432. | +---------+ | | +---------+ | | | | g |
  4433. | |Pitch | | | |LSF | | | | | e |
  4434. | +->|Analysis |-+ | |Quantizer|-|---|---|--->| |
  4435. | | | |4| | | | | 8 | | | E |->
  4436. | | +---------+ | | +---------+ | | | | n |14
  4437. | | | | 9/\ 10| | | | | c |
  4438. | | | | | \/ | | | | o |
  4439. | | +---------+ | | +----------+| | | | d |
  4440. | | |Noise | +--|->|Prediction|+---|---|--->| e |
  4441. | +->|Shaping |-|--+ |Analysis || 7 | | | r |
  4442. | | |Analysis |5| | | || | | | |
  4443. | | +---------+ | | +----------+| | | | |
  4444. | | | | /\ | | | | |
  4445. | | +---------|--|-------+ | | | | |
  4446. | | | \/ \/ \/ \/ \/ | |
  4447. | +---------+ | | +---------+ +------------+ | |
  4448. | |High-Pass| | | | | |Noise | | |
  4449. -+->|Filter |-+----+----->|Prefilter|------>|Shaping |->| |
  4450. 1 | | 2 | | 6 |Quantization|13| |
  4451. +---------+ +---------+ +------------+ +---+
  4452. 1: Input speech signal
  4453. 2: High passed input signal
  4454. 3: Voice activity estimate
  4455. 4: Pitch lags (per 5 ms) and voicing decision (per 20 ms)
  4456. 5: Noise shaping quantization coefficients
  4457. - Short term synthesis and analysis
  4458. noise shaping coefficients (per 5 ms)
  4459. - Long term synthesis and analysis noise
  4460. shaping coefficients (per 5 ms and for voiced speech only)
  4461. - Noise shaping tilt (per 5 ms)
  4462. - Quantizer gain/step size (per 5 ms)
  4463. 6: Input signal filtered with analysis noise shaping filters
  4464. 7: Short and long term prediction coefficients
  4465. LTP (per 5 ms) and LPC (per 20 ms)
  4466. 8: LSF quantization indices
  4467. 9: LSF coefficients
  4468. 10: Quantized LSF coefficients
  4469. 11: Processed gains, and synthesis noise shape coefficients
  4470. 12: LTP state scaling coefficient. Controlling error propagation
  4471. / prediction gain trade-off
  4472. 13: Quantized signal
  4473. 14: Range encoded bitstream
  4474. ]]>
  4475. </artwork>
  4476. <postamble>Encoder block diagram.</postamble>
  4477. </figure>
  4478. <section title='Voice Activity Detection'>
  4479. <t>
  4480. The input signal is processed by a Voice Activity Detector (VAD) to produce a measure of voice activity, spectral tilt, and signal-to-noise estimates for each frame. The VAD uses a sequence of half-band filterbanks to split the signal into four subbands: 0 - Fs/16, Fs/16 - Fs/8, Fs/8 - Fs/4, and Fs/4 - Fs/2, where Fs is the sampling frequency (8, 12, 16, or 24&nbsp;kHz). The lowest subband, from 0 - Fs/16, is high-pass filtered with a first-order moving average (MA) filter (with transfer function H(z) = 1-z**(-1)) to reduce the energy at the lowest frequencies. For each frame, the signal energy per subband is computed. In each subband, a noise level estimator tracks the background noise level and a Signal-to-Noise Ratio (SNR) value is computed as the logarithm of the ratio of energy to noise level. Using these intermediate variables, the following parameters are calculated for use in other SILK modules:
  4481. <list style="symbols">
  4482. <t>
  4483. Average SNR. The average of the subband SNR values.
  4484. </t>
  4485. <t>
  4486. Smoothed subband SNRs. Temporally smoothed subband SNR values.
  4487. </t>
  4488. <t>
  4489. Speech activity level. Based on the average SNR and a weighted average of the subband energies.
  4490. </t>
  4491. <t>
  4492. Spectral tilt. A weighted average of the subband SNRs, with positive weights for the low subbands and negative weights for the high subbands.
  4493. </t>
  4494. </list>
  4495. </t>
  4496. </section>
  4497. <section title='High-Pass Filter'>
  4498. <t>
  4499. The input signal is filtered by a high-pass filter to remove the lowest part of the spectrum that contains little speech energy and may contain background noise. This is a second order Auto Regressive Moving Average (ARMA) filter with a cut-off frequency around 70&nbsp;Hz.
  4500. </t>
  4501. <t>
  4502. In the future, a music detector may also be used to lower the cut-off frequency when the input signal is detected to be music rather than speech.
  4503. </t>
  4504. </section>
  4505. <section title='Pitch Analysis' anchor='pitch_estimator_overview_section'>
  4506. <t>
  4507. The high-passed input signal is processed by the open loop pitch estimator shown in <xref target='pitch_estimator_figure' />.
  4508. <figure align="center" anchor="pitch_estimator_figure">
  4509. <artwork align="center">
  4510. <![CDATA[
  4511. +--------+ +----------+
  4512. |2 x Down| |Time- |
  4513. +->|sampling|->|Correlator| |
  4514. | | | | | |4
  4515. | +--------+ +----------+ \/
  4516. | | 2 +-------+
  4517. | | +-->|Speech |5
  4518. +---------+ +--------+ | \/ | |Type |->
  4519. |LPC | |Down | | +----------+ | |
  4520. +->|Analysis | +->|sample |-+------------->|Time- | +-------+
  4521. | | | | |to 8 kHz| |Correlator|----------->
  4522. | +---------+ | +--------+ |__________| 6
  4523. | | | |3
  4524. | \/ | \/
  4525. | +---------+ | +----------+
  4526. | |Whitening| | |Time- |
  4527. -+->|Filter |-+--------------------------->|Correlator|----------->
  4528. 1 | | | | 7
  4529. +---------+ +----------+
  4530. 1: Input signal
  4531. 2: Lag candidates from stage 1
  4532. 3: Lag candidates from stage 2
  4533. 4: Correlation threshold
  4534. 5: Voiced/unvoiced flag
  4535. 6: Pitch correlation
  4536. 7: Pitch lags
  4537. ]]>
  4538. </artwork>
  4539. <postamble>Block diagram of the pitch estimator.</postamble>
  4540. </figure>
  4541. The pitch analysis finds a binary voiced/unvoiced classification, and, for frames classified as voiced, four pitch lags per frame - one for each 5&nbsp;ms subframe - and a pitch correlation indicating the periodicity of the signal. The input is first whitened using a Linear Prediction (LP) whitening filter, where the coefficients are computed through standard Linear Prediction Coding (LPC) analysis. The order of the whitening filter is 16 for best results, but is reduced to 12 for medium complexity and 8 for low complexity modes. The whitened signal is analyzed to find pitch lags for which the time correlation is high. The analysis consists of three stages for reducing the complexity:
  4542. <list style="symbols">
  4543. <t>In the first stage, the whitened signal is downsampled to 4&nbsp;kHz (from 8&nbsp;kHz) and the current frame is correlated to a signal delayed by a range of lags, starting from a shortest lag corresponding to 500&nbsp;Hz, to a longest lag corresponding to 56&nbsp;Hz.</t>
  4544. <t>
  4545. The second stage operates on an 8&nbsp;kHz signal (downsampled from 12, 16, or 24&nbsp;kHz) and measures time correlations only near the lags corresponding to those that had sufficiently high correlations in the first stage. The resulting correlations are adjusted for a small bias towards short lags to avoid ending up with a multiple of the true pitch lag. The highest adjusted correlation is compared to a threshold depending on:
  4546. <list style="symbols">
  4547. <t>
  4548. Whether the previous frame was classified as voiced
  4549. </t>
  4550. <t>
  4551. The speech activity level
  4552. </t>
  4553. <t>
  4554. The spectral tilt.
  4555. </t>
  4556. </list>
  4557. If the threshold is exceeded, the current frame is classified as voiced and the lag with the highest adjusted correlation is stored for a final pitch analysis of the highest precision in the third stage.
  4558. </t>
  4559. <t>
  4560. The last stage operates directly on the whitened input signal to compute time correlations for each of the four subframes independently in a narrow range around the lag with highest correlation from the second stage.
  4561. </t>
  4562. </list>
  4563. </t>
  4564. </section>
  4565. <section title='Noise Shaping Analysis' anchor='noise_shaping_analysis_overview_section'>
  4566. <t>
  4567. The noise shaping analysis finds gains and filter coefficients used in the prefilter and noise shaping quantizer. These parameters are chosen such that they will fulfill several requirements:
  4568. <list style="symbols">
  4569. <t>Balancing quantization noise and bitrate. The quantization gains determine the step size between reconstruction levels of the excitation signal. Therefore, increasing the quantization gain amplifies quantization noise, but also reduces the bitrate by lowering the entropy of the quantization indices.</t>
  4570. <t>Spectral shaping of the quantization noise; the noise shaping quantizer is capable of reducing quantization noise in some parts of the spectrum at the cost of increased noise in other parts without substantially changing the bitrate. By shaping the noise such that it follows the signal spectrum, it becomes less audible. In practice, best results are obtained by making the shape of the noise spectrum slightly flatter than the signal spectrum.</t>
  4571. <t>De-emphasizing spectral valleys; by using different coefficients in the analysis and synthesis part of the prefilter and noise shaping quantizer, the levels of the spectral valleys can be decreased relative to the levels of the spectral peaks such as speech formants and harmonics. This reduces the entropy of the signal, which is the difference between the coded signal and the quantization noise, thus lowering the bitrate.</t>
  4572. <t>Matching the levels of the decoded speech formants to the levels of the original speech formants; an adjustment gain and a first order tilt coefficient are computed to compensate for the effect of the noise shaping quantization on the level and spectral tilt.</t>
  4573. </list>
  4574. </t>
  4575. <t>
  4576. <figure align="center" anchor="noise_shape_analysis_spectra_figure">
  4577. <artwork align="center">
  4578. <![CDATA[
  4579. / \ ___
  4580. | // \\
  4581. | // \\ ____
  4582. |_// \\___// \\ ____
  4583. | / ___ \ / \\ // \\
  4584. P |/ / \ \_/ \\_____// \\
  4585. o | / \ ____ \ / \\
  4586. w | / \___/ \ \___/ ____ \\___ 1
  4587. e |/ \ / \ \
  4588. r | \_____/ \ \__ 2
  4589. | \
  4590. | \___ 3
  4591. |
  4592. +---------------------------------------->
  4593. Frequency
  4594. 1: Input signal spectrum
  4595. 2: De-emphasized and level matched spectrum
  4596. 3: Quantization noise spectrum
  4597. ]]>
  4598. </artwork>
  4599. <postamble>Noise shaping and spectral de-emphasis illustration.</postamble>
  4600. </figure>
  4601. <xref target='noise_shape_analysis_spectra_figure' /> shows an example of an input signal spectrum (1). After de-emphasis and level matching, the spectrum has deeper valleys (2). The quantization noise spectrum (3) more or less follows the input signal spectrum, while having slightly less pronounced peaks. The entropy, which provides a lower bound on the bitrate for encoding the excitation signal, is proportional to the area between the de-emphasized spectrum (2) and the quantization noise spectrum (3). Without de-emphasis, the entropy is proportional to the area between input spectrum (1) and quantization noise (3) - clearly higher.
  4602. </t>
  4603. <t>
  4604. The transformation from input signal to de-emphasized signal can be described as a filtering operation with a filter
  4605. <figure align="center">
  4606. <artwork align="center">
  4607. <![CDATA[
  4608. -1 Wana(z)
  4609. H(z) = G * ( 1 - c_tilt * z ) * -------
  4610. Wsyn(z),
  4611. ]]>
  4612. </artwork>
  4613. </figure>
  4614. having an adjustment gain G, a first order tilt adjustment filter with
  4615. tilt coefficient c_tilt, and where
  4616. <figure align="center">
  4617. <artwork align="center">
  4618. <![CDATA[
  4619. 16 d
  4620. __ -k -L __ -k
  4621. Wana(z) = (1 - \ (a_ana(k) * z )*(1 - z * \ b_ana(k) * z ),
  4622. /_ /_
  4623. k=1 k=-d
  4624. ]]>
  4625. </artwork>
  4626. </figure>
  4627. is the analysis part of the de-emphasis filter, consisting of the short-term shaping filter with coefficients a_ana(k), and the long-term shaping filter with coefficients b_ana(k) and pitch lag L. The parameter d determines the number of long-term shaping filter taps.
  4628. </t>
  4629. <t>
  4630. Similarly, but without the tilt adjustment, the synthesis part can be written as
  4631. <figure align="center">
  4632. <artwork align="center">
  4633. <![CDATA[
  4634. 16 d
  4635. __ -k -L __ -k
  4636. Wsyn(z) = (1 - \ (a_syn(k) * z )*(1 - z * \ b_syn(k) * z ).
  4637. /_ /_
  4638. k=1 k=-d
  4639. ]]>
  4640. </artwork>
  4641. </figure>
  4642. </t>
  4643. <t>
  4644. All noise shaping parameters are computed and applied per subframe of 5&nbsp;ms. First, an LPC analysis is performed on a windowed signal block of 15&nbsp;ms. The signal block has a look-ahead of 5&nbsp;ms relative to the current subframe, and the window is an asymmetric sine window. The LPC analysis is done with the autocorrelation method, with an order of 16 for best quality or 12 in low complexity operation. The quantization gain is found by taking the square root of the residual energy from the LPC analysis and multiplying it by a value inversely proportional to the coding quality control parameter and the pitch correlation.
  4645. </t>
  4646. <t>
  4647. Next we find the two sets of short-term noise shaping coefficients a_ana(k) and a_syn(k), by applying different amounts of bandwidth expansion to the coefficients found in the LPC analysis. This bandwidth expansion moves the roots of the LPC polynomial towards the origin, using the formulas
  4648. <figure align="center">
  4649. <artwork align="center">
  4650. <![CDATA[
  4651. k
  4652. a_ana(k) = a(k)*g_ana , and
  4653. k
  4654. a_syn(k) = a(k)*g_syn ,
  4655. ]]>
  4656. </artwork>
  4657. </figure>
  4658. where a(k) is the k'th LPC coefficient, and the bandwidth expansion factors g_ana and g_syn are calculated as
  4659. <figure align="center">
  4660. <artwork align="center">
  4661. <![CDATA[
  4662. g_ana = 0.94 - 0.02*C, and
  4663. g_syn = 0.94 + 0.02*C,
  4664. ]]>
  4665. </artwork>
  4666. </figure>
  4667. where C is the coding quality control parameter between 0 and 1. Applying more bandwidth expansion to the analysis part than to the synthesis part gives the desired de-emphasis of spectral valleys in between formants.
  4668. </t>
  4669. <t>
  4670. The long-term shaping is applied only during voiced frames. It uses three filter taps, described by
  4671. <figure align="center">
  4672. <artwork align="center">
  4673. <![CDATA[
  4674. b_ana = F_ana * [0.25, 0.5, 0.25], and
  4675. b_syn = F_syn * [0.25, 0.5, 0.25].
  4676. ]]>
  4677. </artwork>
  4678. </figure>
  4679. For unvoiced frames these coefficients are set to 0. The multiplication factors F_ana and F_syn are chosen between 0 and 1, depending on the coding quality control parameter, as well as the calculated pitch correlation and smoothed subband SNR of the lowest subband. By having F_ana less than F_syn, the pitch harmonics are emphasized relative to the valleys in between the harmonics.
  4680. </t>
  4681. <t>
  4682. The tilt coefficient c_tilt is for unvoiced frames chosen as
  4683. <figure align="center">
  4684. <artwork align="center">
  4685. <![CDATA[
  4686. c_tilt = 0.4, and as
  4687. c_tilt = 0.04 + 0.06 * C
  4688. ]]>
  4689. </artwork>
  4690. </figure>
  4691. for voiced frames, where C again is the coding quality control parameter and is between 0 and 1.
  4692. </t>
  4693. <t>
  4694. The adjustment gain G serves to correct any level mismatch between the original and decoded signals that might arise from the noise shaping and de-emphasis. This gain is computed as the ratio of the prediction gain of the short-term analysis and synthesis filter coefficients. The prediction gain of an LPC synthesis filter is the square root of the output energy when the filter is excited by a unit-energy impulse on the input. An efficient way to compute the prediction gain is by first computing the reflection coefficients from the LPC coefficients through the step-down algorithm, and extracting the prediction gain from the reflection coefficients as
  4695. <figure align="center">
  4696. <artwork align="center">
  4697. <![CDATA[
  4698. K
  4699. ___ 2 -0.5
  4700. predGain = ( | | 1 - (r_k) ) ,
  4701. k=1
  4702. ]]>
  4703. </artwork>
  4704. </figure>
  4705. where r_k is the k'th reflection coefficient.
  4706. </t>
  4707. <t>
  4708. Initial values for the quantization gains are computed as the square-root of the residual energy of the LPC analysis, adjusted by the coding quality control parameter. These quantization gains are later adjusted based on the results of the prediction analysis.
  4709. </t>
  4710. </section>
  4711. <section title='Prefilter'>
  4712. <t>
  4713. In the prefilter the input signal is filtered using the spectral valley de-emphasis filter coefficients from the noise shaping analysis (see <xref target='noise_shaping_analysis_overview_section'/>). By applying only the noise shaping analysis filter to the input signal, it provides the input to the noise shaping quantizer.
  4714. </t>
  4715. </section>
  4716. <section title='Prediction Analysis' anchor='pred_ana_overview_section'>
  4717. <t>
  4718. The prediction analysis is performed in one of two ways depending on how the pitch estimator classified the frame. The processing for voiced and unvoiced speech is described in <xref target='pred_ana_voiced_overview_section' /> and <xref target='pred_ana_unvoiced_overview_section' />, respectively. Inputs to this function include the pre-whitened signal from the pitch estimator (see <xref target='pitch_estimator_overview_section'/>).
  4719. </t>
  4720. <section title='Voiced Speech' anchor='pred_ana_voiced_overview_section'>
  4721. <t>
  4722. For a frame of voiced speech the pitch pulses will remain dominant in the pre-whitened input signal. Further whitening is desirable as it leads to higher quality at the same available bitrate. To achieve this, a Long-Term Prediction (LTP) analysis is carried out to estimate the coefficients of a fifth-order LTP filter for each of four subframes. The LTP coefficients are used to find an LTP residual signal with the simulated output signal as input to obtain better modeling of the output signal. This LTP residual signal is the input to an LPC analysis where the LPCs are estimated using Burg's method, such that the residual energy is minimized. The estimated LPCs are converted to a Line Spectral Frequency (LSF) vector and quantized as described in <xref target='lsf_quantizer_overview_section' />. After quantization, the quantized LSF vector is converted back to LPC coefficients using the full procedure in <xref target="silk_nlsfs"/>. By using LPC coefficients derived from the quantized LSF coefficients, the encoder remains fully synchronized with the decoder. The LTP coefficients are quantized using a method described in <xref target='ltp_quantizer_overview_section' />. The quantized LPC and LTP coefficients are then used to filter the high-pass filtered input signal and measure residual energy for each of the four subframes.
  4723. </t>
  4724. </section>
  4725. <section title='Unvoiced Speech' anchor='pred_ana_unvoiced_overview_section'>
  4726. <t>
  4727. For a speech signal that has been classified as unvoiced, there is no need for LTP filtering, as it has already been determined that the pre-whitened input signal is not periodic enough within the allowed pitch period range for LTP analysis to be worth the cost in terms of complexity and rate. The pre-whitened input signal is therefore discarded, and instead the high-pass filtered input signal is used for LPC analysis using Burg's method. The resulting LPC coefficients are converted to an LSF vector and quantized as described in the following section. They are then transformed back to obtain quantized LPC coefficients, which are then used to filter the high-pass filtered input signal and measure residual energy for each of the four subframes.
  4728. </t>
  4729. </section>
  4730. </section>
  4731. <section title='LSF Quantization' anchor='lsf_quantizer_overview_section'>
  4732. <t>In general, the purpose of quantization is to significantly lower the bitrate at the cost of introducing some distortion. A higher rate should always result in lower distortion, and lowering the rate will generally lead to higher distortion. A commonly used but generally suboptimal approach is to use a quantization method with a constant rate, where only the error is minimized when quantizing.</t>
  4733. <section title='Rate-Distortion Optimization'>
  4734. <t>Instead, we minimize an objective function that consists of a weighted sum of rate and distortion, and use a codebook with an associated non-uniform rate table. Thus, we take into account that the probability mass function for selecting the codebook entries is by no means guaranteed to be uniform in our scenario. This approach has several advantages. It ensures that rarely used codebook vector centroids, which are modeling statistical outliers in the training set, are quantized with low error at the expense of a high rate. At the same time, it allows modeling frequently used centroids with low error and a relatively low rate. This approach leads to equal or lower distortion than the fixed-rate codebook at any given average rate, provided that the data is similar to that used for training the codebook.</t>
  4735. </section>
  4736. <section title='Error Mapping' anchor='lsf_error_mapping_overview_section'>
  4737. <t>
  4738. Instead of minimizing the error in the LSF domain, we map the errors to better approximate spectral distortion by applying an individual weight to each element in the error vector. The weight vectors are calculated for each input vector using the Inverse Harmonic Mean Weighting (IHMW) function proposed by Laroia et al. (see <xref target="laroia-icassp" />).
  4739. Consequently, we solve the following minimization problem, i.e.,
  4740. <figure align="center">
  4741. <artwork align="center">
  4742. <![CDATA[
  4743. LSF_q = argmin { (LSF - c)' * W * (LSF - c) + mu * rate },
  4744. c in C
  4745. ]]>
  4746. </artwork>
  4747. </figure>
  4748. where LSF_q is the quantized vector, LSF is the input vector to be quantized, and c is the quantized LSF vector candidate taken from the set C of all possible outcomes of the codebook.
  4749. </t>
  4750. </section>
  4751. <section title='Survivor Based Codebook Search'>
  4752. <t>
  4753. This number of possible combinations is far too high to carry out a full search for each frame, so for all stages but the last (i.e., s smaller than S), only the best min(L, Ms) centroids are carried over to stage s+1. In each stage, the objective function (i.e., the weighted sum of accumulated bitrate and distortion) is evaluated for each codebook vector entry and the results are sorted. Only the best paths and their corresponding quantization errors are considered in the next stage. In the last stage, S, the single best path through the multistage codebook is determined. By varying the maximum number of survivors from each stage to the next, L, the complexity can be adjusted in real time, at the cost of a potential increase when evaluating the objective function for the resulting quantized vector. This approach scales all the way between the two extremes, L=1 being a greedy search, and the desirable but infeasible full search, L=T/MS. Performance almost as good as that of the infeasible full search can be obtained at substantially lower complexity by using this approach (see, e.g., <xref target='leblanc-tsap'/>).
  4754. </t>
  4755. </section>
  4756. <section title='LSF Stabilization' anchor='lsf_stabilizer_overview_section'>
  4757. <t>If the input is stable, finding the best candidate usually results in a quantized vector that is also stable. Due to the multi-stage approach, however, it is theoretically possible that the best quantization candidate is unstable. Because of this, it is necessary to explicitly ensure that the quantized vectors are stable. Therefore we apply an LSF stabilization method which ensures that the LSF parameters are within valid range, increasingly sorted, and have minimum distances between each other and the border values that have been predetermined as the 0.01 percentile distance values from a large training set.</t>
  4758. </section>
  4759. <section title='Off-Line Codebook Training'>
  4760. <t>
  4761. The vectors and rate tables for the multi-stage codebook have been trained by minimizing the average of the objective function for LSF vectors from a large training set.
  4762. </t>
  4763. </section>
  4764. </section>
  4765. <section title='LTP Quantization' anchor='ltp_quantizer_overview_section'>
  4766. <t>
  4767. For voiced frames, the prediction analysis described in <xref target='pred_ana_voiced_overview_section' /> resulted in four sets (one set per subframe) of five LTP coefficients, plus four weighting matrices. The LTP coefficients for each subframe are quantized using entropy constrained vector quantization. A total of three vector codebooks are available for quantization, with different rate-distortion trade-offs. The three codebooks have 10, 20, and 40 vectors and average rates of about 3, 4, and 5 bits per vector, respectively. Consequently, the first codebook has larger average quantization distortion at a lower rate, whereas the last codebook has smaller average quantization distortion at a higher rate. Given the weighting matrix W_ltp and LTP vector b, the weighted rate-distortion measure for a codebook vector cb_i with rate r_i is give by
  4768. <figure align="center">
  4769. <artwork align="center">
  4770. <![CDATA[
  4771. RD = u * (b - cb_i)' * W_ltp * (b - cb_i) + r_i,
  4772. ]]>
  4773. </artwork>
  4774. </figure>
  4775. where u is a fixed, heuristically-determined parameter balancing the distortion and rate. Which codebook gives the best performance for a given LTP vector depends on the weighting matrix for that LTP vector. For example, for a low valued W_ltp, it is advantageous to use the codebook with 10 vectors as it has a lower average rate. For a large W_ltp, on the other hand, it is often better to use the codebook with 40 vectors, as it is more likely to contain the best codebook vector.
  4776. The weighting matrix W_ltp depends mostly on two aspects of the input signal. The first is the periodicity of the signal; the more periodic, the larger W_ltp. The second is the change in signal energy in the current subframe, relative to the signal one pitch lag earlier. A decaying energy leads to a larger W_ltp than an increasing energy. Both aspects fluctuate relatively slowly, which causes the W_ltp matrices for different subframes of one frame often to be similar. Because of this, one of the three codebooks typically gives good performance for all subframes, and therefore the codebook search for the subframe LTP vectors is constrained to only allow codebook vectors to be chosen from the same codebook, resulting in a rate reduction.
  4777. </t>
  4778. <t>
  4779. To find the best codebook, each of the three vector codebooks is used to quantize all subframe LTP vectors and produce a combined weighted rate-distortion measure for each vector codebook. The vector codebook with the lowest combined rate-distortion over all subframes is chosen. The quantized LTP vectors are used in the noise shaping quantizer, and the index of the codebook plus the four indices for the four subframe codebook vectors are passed on to the range encoder.
  4780. </t>
  4781. </section>
  4782. <section title='Noise Shaping Quantizer'>
  4783. <t>
  4784. The noise shaping quantizer independently shapes the signal and coding noise spectra to obtain a perceptually higher quality at the same bitrate.
  4785. </t>
  4786. <t>
  4787. The prefilter output signal is multiplied with a compensation gain G computed in the noise shaping analysis. Then the output of a synthesis shaping filter is added, and the output of a prediction filter is subtracted to create a residual signal. The residual signal is multiplied by the inverse quantized quantization gain from the noise shaping analysis, and input to a scalar quantizer. The quantization indices of the scalar quantizer represent a signal of pulses that is input to the pyramid range encoder. The scalar quantizer also outputs a quantization signal, which is multiplied by the quantized quantization gain from the noise shaping analysis to create an excitation signal. The output of the prediction filter is added to the excitation signal to form the quantized output signal y(n). The quantized output signal y(n) is input to the synthesis shaping and prediction filters.
  4788. </t>
  4789. </section>
  4790. </section>
  4791. <section title="CELT Encoder">
  4792. <t>
  4793. Most of the aspects of the CELT encoder can be directly derived from the description
  4794. of the decoder. For example, the filters and rotations in the encoder are simply the
  4795. inverse of the operation performed by the decoder. Similarly, the quantizers generally
  4796. optimize for the mean square error (because noise shaping is part of the bit-stream itself),
  4797. so no special search is required. For this reason, only the less straightforward aspects of the
  4798. encoder are described here.
  4799. </t>
  4800. <section anchor="pitch-prefilter" title="Pitch prefilter">
  4801. <t>The pitch prefilter is applied after the pre-emphasis and before the de-emphasis. It's applied
  4802. in such a way as to be the inverse of the decoder's post-filter. The main non-obvious aspect of the
  4803. prefilter is the selection of the pitch period. The pitch search should be optimised for the
  4804. following criteria:
  4805. <list style="symbols">
  4806. <t>continuity: it is important that the pitch period
  4807. does not change abruptly between frames; and</t>
  4808. <t>avoidance of pitch multiples: when the period used is a multiple of the real period
  4809. (lower frequency fundamental), the post-filter loses most of its ability to reduce noise</t>
  4810. </list>
  4811. </t>
  4812. </section>
  4813. <section anchor="normalization" title="Bands and Normalization">
  4814. <t>
  4815. The MDCT output is divided into bands that are designed to match the ear's critical
  4816. bands for the smallest (2.5&nbsp;ms) frame size. The larger frame sizes use integer
  4817. multiples of the 2.5&nbsp;ms layout. For each band, the encoder
  4818. computes the energy that will later be encoded. Each band is then normalized by the
  4819. square root of the <spanx style="strong">unquantized</spanx> energy, such that each band now forms a unit vector X.
  4820. The energy and the normalization are computed by compute_band_energies()
  4821. and normalise_bands() (bands.c), respectively.
  4822. </t>
  4823. </section>
  4824. <section anchor="energy-quantization" title="Energy Envelope Quantization">
  4825. <t>
  4826. Energy quantization (both coarse and fine) can be easily understood from the decoding process.
  4827. The quantizer simply minimizes the log energy error for each band, with the exception that at
  4828. very low rate, larger errors are allowed in the coarse energy to minimize the bit-rate. When the
  4829. avaialble CPU requirements allow it, it is best to try encoding the coarse energy both with and without
  4830. inter-frame prediction such that the best prediction mode can be selected. The optimal mode depends on
  4831. the coding rate, the available bit-rate, and the current rate of packet loss.
  4832. </t>
  4833. </section> <!-- Energy quant -->
  4834. <section anchor="pvq" title="Spherical Vector Quantization">
  4835. <t>CELT uses a Pyramid Vector Quantization (PVQ) <xref target="PVQ"></xref>
  4836. codebook for quantizing the details of the spectrum in each band that have not
  4837. been predicted by the pitch predictor. The PVQ codebook consists of all sums
  4838. of K signed pulses in a vector of N samples, where two pulses at the same position
  4839. are required to have the same sign. Thus the codebook includes
  4840. all integer codevectors y of N dimensions that satisfy sum(abs(y(j))) = K.
  4841. </t>
  4842. <t>
  4843. In bands where there are sufficient bits allocated the PVQ is used to encode
  4844. the unit vector that results from the normalization in
  4845. <xref target="normalization"></xref> directly. Given a PVQ codevector y,
  4846. the unit vector X is obtained as X = y/||y||, where ||.|| denotes the
  4847. L2 norm.
  4848. </t>
  4849. <section anchor="pvq-search" title="PVQ Search">
  4850. <t>
  4851. The search for the best codevector y is performed by alg_quant()
  4852. (vq.c). There are several possible approaches to the
  4853. search, with a trade-off between quality and complexity. The method used in the reference
  4854. implementation computes an initial codeword y1 by projecting the residual signal
  4855. R = X - p' onto the codebook pyramid of K-1 pulses:
  4856. </t>
  4857. <t>
  4858. y0 = round_towards_zero( (K-1) * R / sum(abs(R)))
  4859. </t>
  4860. <t>
  4861. Depending on N, K and the input data, the initial codeword y0 may contain from
  4862. 0 to K-1 non-zero values. All the remaining pulses, with the exception of the last one,
  4863. are found iteratively with a greedy search that minimizes the normalized correlation
  4864. between y and R:
  4865. <figure align="center">
  4866. <artwork align="center"><![CDATA[
  4867. T
  4868. J = -R * y / ||y||
  4869. ]]></artwork>
  4870. </figure>
  4871. </t>
  4872. <t>
  4873. The search described above is considered to be a good trade-off between quality
  4874. and computational cost. However, there are other possible ways to search the PVQ
  4875. codebook and the implementers MAY use any other search methods.
  4876. </t>
  4877. </section>
  4878. </section>
  4879. </section>
  4880. </section>
  4881. <section title="Conformance">
  4882. <t>
  4883. It is the intention to allow the greatest possible choice of freedom in
  4884. implementing the specification. For this reason, outside of a few exceptions
  4885. noted in this section, conformance is defined through the reference
  4886. implementation of the decoder provided in <xref target="ref-implementation"/>.
  4887. Although this document includes an English description of the codec, should
  4888. the description contradict the source code of the reference implementation,
  4889. the latter shall take precedence.
  4890. </t>
  4891. <t>
  4892. Compliance with this specification means that a decoder's output MUST be
  4893. within the thresholds specified by the opus_compare.c tool (included
  4894. with the code) when compared to the reference implementation. Either the floating-point
  4895. implementation or the fixed-point implementation can be used as a reference and being
  4896. within the threshold for one of the two is sufficient.
  4897. </t>
  4898. <t>
  4899. To complement the Opus specification, the "Opus Custom" codec is defined to
  4900. handle special sampling rates and frame rates that are not supported by the
  4901. main Opus specification. Use of Opus Custom is discouraged for all but very
  4902. special applications for which a frame size different from 2.5, 5, 10, or 20&nbsp;ms is
  4903. needed (for either complexity or latency reasons). Such applications will not
  4904. be compatible with the "main" Opus codec. In Opus Custom operation,
  4905. only the CELT layer is available, which is available using the celt_* function
  4906. calls in celt.h.
  4907. </t>
  4908. </section>
  4909. <section anchor="security" title="Security Considerations">
  4910. <t>
  4911. Implementations of the Opus codec need to take appropriate security considerations
  4912. into account, as outlined in <xref target="DOS"/> and <xref target="SECGUIDE"/>.
  4913. It is extremely important for the decoder to be robust against malicious
  4914. payloads.
  4915. Malicious payloads must not cause the decoder to overrun its allocated memory
  4916. or to take an excessive amount of resources to decode.
  4917. Although problems
  4918. in encoders are typically rarer, the same applies to the encoder. Malicious
  4919. audio streams must not cause the encoder to misbehave because this would
  4920. allow an attacker to attack transcoding gateways.
  4921. </t>
  4922. <t>
  4923. The reference implementation contains no known buffer overflow or cases where
  4924. a specially crafted packet or audio segment could cause a significant increase
  4925. in CPU load.
  4926. However, on certain CPU architectures where denormalized floating-point
  4927. operations are much slower than normal floating-point operations, it is
  4928. possible for some audio content (e.g., silence or near-silence) to cause a certain
  4929. an increase in CPU load.
  4930. Denormals can be introduced by reordering operations in the compiler and depend
  4931. on the target architecture, so it is difficult to guarantee that an implementation
  4932. avoids them.
  4933. For architectures on which denormals are problematic, it is RECOMMENDED to
  4934. add very small floating-point offsets to the affected signals
  4935. to prevent significant numbers of denormalized
  4936. operations. Alternatively, it is often possible to configure the hardware to treat
  4937. denormals as zero (DAZ).
  4938. No such issue exists for the fixed-point reference implementation.
  4939. </t>
  4940. <t>The reference implementation was validated in the following conditions:
  4941. <list style="numbers">
  4942. <t>Sending the decoder valid packets generated by the reference encoder and
  4943. verifying that the decoder's final range coder state matches that of the encoder.</t>
  4944. <t>Sending the decoder packets generated by the reference encoder, after random corruption.</t>
  4945. <t>Sending the decoder random packets to the decoder.</t>
  4946. <t>Altering the encoder to make random coding decisions (internal fuzzing), including
  4947. mode switching and verifying that the range coder final states match.</t>
  4948. </list>
  4949. In all of the conditions above, both the encoder and the decoder were run inside
  4950. the Valgrind memory debugger, which tracks reads and writes to invalid memory
  4951. regions, as well as use of uninitialized memory. There were no error reported
  4952. on any of the tested conditions.
  4953. </t>
  4954. </section>
  4955. <section title="IANA Considerations ">
  4956. <t>
  4957. This document has no actions for IANA.
  4958. </t>
  4959. </section>
  4960. <section anchor="Acknowledgments" title="Acknowledgments">
  4961. <t>
  4962. Thanks to all other developers, including Raymond Chen, Soeren Skak Jensen, Gregory Maxwell,
  4963. Christopher Montgomery, and Karsten Vandborg Soerensen. We would also
  4964. like to thank Igor Dyakonov and Jan Skoglund for their help with subjective testing of the
  4965. Opus codec. Thanks to John Ridges, Keith Yan, and many others on the Opus and CELT mailing lists
  4966. for their bug reports and feedback, as well as Ralph Giles, Christian Hoene, and
  4967. Kat Walsh, for their feedback on the draft.
  4968. </t>
  4969. </section>
  4970. </middle>
  4971. <back>
  4972. <references title="Informative References">
  4973. <reference anchor='SILK'>
  4974. <front>
  4975. <title>SILK Speech Codec</title>
  4976. <author initials='K.' surname='Vos' fullname='K. Vos'>
  4977. <organization /></author>
  4978. <author initials='S.' surname='Jensen' fullname='S. Jensen'>
  4979. <organization /></author>
  4980. <author initials='K.' surname='Soerensen' fullname='K. Soerensen'>
  4981. <organization /></author>
  4982. <date year='2010' month='March' />
  4983. <abstract>
  4984. <t></t>
  4985. </abstract></front>
  4986. <seriesInfo name='Internet-Draft' value='draft-vos-silk-01' />
  4987. <format type='TXT' target='http://tools.ietf.org/html/draft-vos-silk-01' />
  4988. </reference>
  4989. <reference anchor="laroia-icassp">
  4990. <front>
  4991. <title abbrev="Robust and Efficient Quantization of Speech LSP">
  4992. Robust and Efficient Quantization of Speech LSP Parameters Using Structured Vector Quantization
  4993. </title>
  4994. <author initials="R.L." surname="Laroia" fullname="R.">
  4995. <organization/>
  4996. </author>
  4997. <author initials="N.P." surname="Phamdo" fullname="N.">
  4998. <organization/>
  4999. </author>
  5000. <author initials="N.F." surname="Farvardin" fullname="N.">
  5001. <organization/>
  5002. </author>
  5003. </front>
  5004. <seriesInfo name="ICASSP-1991, Proc. IEEE Int. Conf. Acoust., Speech, Signal Processing, pp. 641-644, October" value="1991"/>
  5005. </reference>
  5006. <reference anchor="sinervo-norsig">
  5007. <front>
  5008. <title abbrev="SVQ versus MSVQ">Evaluation of Split and Multistage Techniques in LSF Quantization</title>
  5009. <author initials="U.S." surname="Sinervo" fullname="Ulpu Sinervo">
  5010. <organization/>
  5011. </author>
  5012. <author initials="J.N." surname="Nurminen" fullname="Jani Nurminen">
  5013. <organization/>
  5014. </author>
  5015. <author initials="A.H." surname="Heikkinen" fullname="Ari Heikkinen">
  5016. <organization/>
  5017. </author>
  5018. <author initials="J.S." surname="Saarinen" fullname="Jukka Saarinen">
  5019. <organization/>
  5020. </author>
  5021. </front>
  5022. <seriesInfo name="NORSIG-2001, Norsk symposium i signalbehandling, Trondheim, Norge, October" value="2001"/>
  5023. </reference>
  5024. <reference anchor="leblanc-tsap">
  5025. <front>
  5026. <title>Efficient Search and Design Procedures for Robust Multi-Stage VQ of LPC Parameters for 4&nbsp;kb/s Speech Coding</title>
  5027. <author initials="W.P." surname="LeBlanc" fullname="">
  5028. <organization/>
  5029. </author>
  5030. <author initials="B." surname="Bhattacharya" fullname="">
  5031. <organization/>
  5032. </author>
  5033. <author initials="S.A." surname="Mahmoud" fullname="">
  5034. <organization/>
  5035. </author>
  5036. <author initials="V." surname="Cuperman" fullname="">
  5037. <organization/>
  5038. </author>
  5039. </front>
  5040. <seriesInfo name="IEEE Transactions on Speech and Audio Processing, Vol. 1, No. 4, October" value="1993" />
  5041. </reference>
  5042. <reference anchor='CELT'>
  5043. <front>
  5044. <title>Constrained-Energy Lapped Transform (CELT) Codec</title>
  5045. <author initials='J-M.' surname='Valin' fullname='J-M. Valin'>
  5046. <organization /></author>
  5047. <author initials='T.' surname='Terriberry' fullname='T. Terriberry'>
  5048. <organization /></author>
  5049. <author initials='G.' surname='Maxwell' fullname='G. Maxwell'>
  5050. <organization /></author>
  5051. <author initials='C.' surname='Montgomery' fullname='C. Montgomery'>
  5052. <organization /></author>
  5053. <date year='2010' month='July' />
  5054. <abstract>
  5055. <t></t>
  5056. </abstract></front>
  5057. <seriesInfo name='Internet-Draft' value='draft-valin-celt-codec-02' />
  5058. <format type='TXT' target='http://tools.ietf.org/html/draft-valin-celt-codec-02' />
  5059. </reference>
  5060. <reference anchor='DOS'>
  5061. <front>
  5062. <title>Internet Denial-of-Service Considerations</title>
  5063. <author initials='M.' surname='Handley' fullname='M. Handley'>
  5064. <organization /></author>
  5065. <author initials='E.' surname='Rescorla' fullname='E. Rescorla'>
  5066. <organization /></author>
  5067. <author>
  5068. <organization>IAB</organization></author>
  5069. <date year='2006' month='December' />
  5070. <abstract>
  5071. <t>This document provides an overview of possible avenues for denial-of-service (DoS) attack on Internet systems. The aim is to encourage protocol designers and network engineers towards designs that are more robust. We discuss partial solutions that reduce the effectiveness of attacks, and how some solutions might inadvertently open up alternative vulnerabilities. This memo provides information for the Internet community.</t></abstract></front>
  5072. <seriesInfo name='RFC' value='4732' />
  5073. <format type='TXT' octets='91844' target='ftp://ftp.isi.edu/in-notes/rfc4732.txt' />
  5074. </reference>
  5075. <reference anchor='SECGUIDE'>
  5076. <front>
  5077. <title>Guidelines for Writing RFC Text on Security Considerations</title>
  5078. <author initials='E.' surname='Rescorla' fullname='E. Rescorla'>
  5079. <organization /></author>
  5080. <author initials='B.' surname='Korver' fullname='B. Korver'>
  5081. <organization /></author>
  5082. <date year='2003' month='July' />
  5083. <abstract>
  5084. <t>All RFCs are required to have a Security Considerations section. Historically, such sections have been relatively weak. This document provides guidelines to RFC authors on how to write a good Security Considerations section. This document specifies an Internet Best Current Practices for the Internet Community, and requests discussion and suggestions for improvements.</t></abstract></front>
  5085. <seriesInfo name='BCP' value='72' />
  5086. <seriesInfo name='RFC' value='3552' />
  5087. <format type='TXT' octets='110393' target='ftp://ftp.isi.edu/in-notes/rfc3552.txt' />
  5088. </reference>
  5089. <reference anchor="range-coding">
  5090. <front>
  5091. <title>Range encoding: An algorithm for removing redundancy from a digitised message</title>
  5092. <author initials="G." surname="Nigel" fullname=""><organization/></author>
  5093. <author initials="N." surname="Martin" fullname=""><organization/></author>
  5094. <date year="1979" />
  5095. </front>
  5096. <seriesInfo name="Proc. Institution of Electronic and Radio Engineers International Conference on Video and Data Recording" value="" />
  5097. </reference>
  5098. <reference anchor="coding-thesis">
  5099. <front>
  5100. <title>Source coding algorithms for fast data compression</title>
  5101. <author initials="R." surname="Pasco" fullname=""><organization/></author>
  5102. <date month="May" year="1976" />
  5103. </front>
  5104. <seriesInfo name="Ph.D. thesis" value="Dept. of Electrical Engineering, Stanford University" />
  5105. </reference>
  5106. <reference anchor="PVQ">
  5107. <front>
  5108. <title>A Pyramid Vector Quantizer</title>
  5109. <author initials="T." surname="Fischer" fullname=""><organization/></author>
  5110. <date month="July" year="1986" />
  5111. </front>
  5112. <seriesInfo name="IEEE Trans. on Information Theory, Vol. 32" value="pp. 568-583" />
  5113. </reference>
  5114. </references>
  5115. <section anchor="ref-implementation" title="Reference Implementation">
  5116. <t>This appendix contains the complete source code for the
  5117. reference implementation of the Opus codec written in C. This
  5118. implementation can be compiled for
  5119. either floating-point or fixed-point architectures.
  5120. </t>
  5121. <t>The implementation can be compiled with either a C89 or a C99
  5122. compiler. It is reasonably optimized for most platforms such that
  5123. only architecture-specific optimizations are likely to be useful.
  5124. The FFT used is a slightly modified version of the KISS-FFT package,
  5125. but it is easy to substitute any other FFT library.
  5126. </t>
  5127. <section title="Extracting the source">
  5128. <t>
  5129. The complete source code can be extracted from this draft, by running the
  5130. following command line:
  5131. <list style="symbols">
  5132. <t><![CDATA[
  5133. cat draft-ietf-codec-opus.txt | grep '^\ \ \ ###' | sed 's/\s\s\s###//' | base64 -d > opus_source.tar.gz
  5134. ]]></t>
  5135. <t>
  5136. tar xzvf opus_source.tar.gz
  5137. </t>
  5138. <t>cd opus_source</t>
  5139. <t>make</t>
  5140. </list>
  5141. </t>
  5142. </section>
  5143. <section title="Development Versions">
  5144. <t>
  5145. The current development version of the source code is available in a
  5146. <eref target='git://git.opus-codec.org/opus.git'>Git repository</eref>.
  5147. Development snapshots are provided at
  5148. <eref target='http://opus-codec.org/'/>.
  5149. </t>
  5150. </section>
  5151. <section title="Base64-encoded source code">
  5152. <t>
  5153. <?rfc include="opus_source.base64"?>
  5154. </t>
  5155. </section>
  5156. </section>
  5157. <!--
  5158. <section anchor="opus-compare" title="opus_compare.c">
  5159. <t>
  5160. <?rfc include="opus_compare_escaped.c"?>
  5161. </t>
  5162. </section>
  5163. -->
  5164. <section anchor="self-delimiting-framing" title="Self-Delimiting Framing">
  5165. <t>
  5166. To use the internal framing described in <xref target="modes"/>, the decoder
  5167. must know the total length of the Opus packet, in bytes.
  5168. This section describes a simple variation of that framing which can be used
  5169. when the total length of the packet is not known.
  5170. Nothing in the encoding of the packet itself allows a decoder to distinguish
  5171. between the regular, undelimited framing and the self-delimiting framing
  5172. described in this appendix.
  5173. Which one is used and where must be established by context at the transport
  5174. layer.
  5175. It is RECOMMENDED that a transport layer choose exactly one framing scheme,
  5176. rather than allowing an encoder to signal which one it wants to use.
  5177. </t>
  5178. <t>
  5179. For example, although a regular Opus stream does not support more than two
  5180. channels, a multi-channel Opus stream may be formed from several one- and
  5181. two-channel streams.
  5182. To pack an Opus packet from each of these streams together in a single packet
  5183. at the transport layer, one could use the self-delimiting framing for all but
  5184. the last stream, and then the regular, undelimited framing for the last one.
  5185. Reverting to the undelimited framing for the last stream saves overhead
  5186. (because the total size of the transport-layer packet will still be known),
  5187. and ensures that a "multi-channel" stream which only has a single Opus stream
  5188. uses the same framing as a regular Opus stream does.
  5189. This avoids the need for signaling to distinguish these two cases.
  5190. </t>
  5191. <t>
  5192. The self-delimiting framing is identical to the regular, undelimited framing
  5193. from <xref target="modes"/>, except that each Opus packet contains one extra
  5194. length field, encoded using the same one- or two-byte scheme from
  5195. <xref target="frame-length-coding"/>.
  5196. This extra length immediately precedes the compressed data of the first Opus
  5197. frame in the packet, and is interpreted in the various modes as follows:
  5198. <list style="symbols">
  5199. <t>
  5200. Code&nbsp;0 packets: It is the length of the single Opus frame (see
  5201. <xref target="sd_code0_packet"/>).
  5202. </t>
  5203. <t>
  5204. Code&nbsp;1 packets: It is the length used for both of the Opus frames (see
  5205. <xref target="sd_code1_packet"/>).
  5206. </t>
  5207. <t>
  5208. Code&nbsp;2 packets: It is the length of the second Opus frame (see
  5209. <xref target="sd_code2_packet"/>).</t>
  5210. <t>
  5211. CBR Code&nbsp;3 packets: It is the length used for all of the Opus frames (see
  5212. <xref target="sd_code3cbr_packet"/>).
  5213. </t>
  5214. <t>VBR Code&nbsp;3 packets: It is the length of the last Opus frame (see
  5215. <xref target="sd_code3vbr_packet"/>).
  5216. </t>
  5217. </list>
  5218. </t>
  5219. <figure anchor="sd_code0_packet" title="A Self-Delimited Code 0 Packet"
  5220. align="center">
  5221. <artwork align="center"><![CDATA[
  5222. 0 1 2 3
  5223. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  5224. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5225. |0|0|s| config | N1 (1-2 bytes): |
  5226. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  5227. | Compressed frame 1 (N1 bytes)... :
  5228. : |
  5229. | |
  5230. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5231. ]]></artwork>
  5232. </figure>
  5233. <figure anchor="sd_code1_packet" title="A Self-Delimited Code 1 Packet"
  5234. align="center">
  5235. <artwork align="center"><![CDATA[
  5236. 0 1 2 3
  5237. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  5238. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5239. |1|0|s| config | N1 (1-2 bytes): |
  5240. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
  5241. | Compressed frame 1 (N1 bytes)... |
  5242. : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5243. | | |
  5244. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
  5245. | Compressed frame 2 (N1 bytes)... |
  5246. : +-+-+-+-+-+-+-+-+
  5247. | |
  5248. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5249. ]]></artwork>
  5250. </figure>
  5251. <figure anchor="sd_code2_packet" title="A Self-Delimited Code 2 Packet"
  5252. align="center">
  5253. <artwork align="center"><![CDATA[
  5254. 0 1 2 3
  5255. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  5256. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5257. |0|1|s| config | N1 (1-2 bytes): N2 (1-2 bytes : |
  5258. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
  5259. | Compressed frame 1 (N1 bytes)... |
  5260. : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5261. | | |
  5262. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  5263. | Compressed frame 2 (N2 bytes)... :
  5264. : |
  5265. | |
  5266. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5267. ]]></artwork>
  5268. </figure>
  5269. <figure anchor="sd_code3cbr_packet" title="A Self-Delimited CBR Code 3 Packet"
  5270. align="center">
  5271. <artwork align="center"><![CDATA[
  5272. 0 1 2 3
  5273. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  5274. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5275. |1|1|s| config | M |p|0| Pad len (Opt) : N1 (1-2 bytes):
  5276. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5277. | |
  5278. : Compressed frame 1 (N1 bytes)... :
  5279. | |
  5280. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5281. | |
  5282. : Compressed frame 2 (N1 bytes)... :
  5283. | |
  5284. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5285. | |
  5286. : ... :
  5287. | |
  5288. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5289. | |
  5290. : Compressed frame M (N1 bytes)... :
  5291. | |
  5292. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5293. : Opus Padding (Optional)... |
  5294. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5295. ]]></artwork>
  5296. </figure>
  5297. <figure anchor="sd_code3vbr_packet" title="A Self-Delimited VBR Code 3 Packet"
  5298. align="center">
  5299. <artwork align="center"><![CDATA[
  5300. 0 1 2 3
  5301. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  5302. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5303. |1|1|s| config | M |p|1| Padding length (Optional) :
  5304. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5305. : N1 (1-2 bytes): ... : N[M-1] | N[M] :
  5306. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5307. | |
  5308. : Compressed frame 1 (N1 bytes)... :
  5309. | |
  5310. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5311. | |
  5312. : Compressed frame 2 (N2 bytes)... :
  5313. | |
  5314. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5315. | |
  5316. : ... :
  5317. | |
  5318. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5319. | |
  5320. : Compressed frame M (N[M] bytes)... :
  5321. | |
  5322. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5323. : Opus Padding (Optional)... |
  5324. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5325. ]]></artwork>
  5326. </figure>
  5327. </section>
  5328. </back>
  5329. </rfc>