draft-ietf-codec-opus.xml 278 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770
  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>Octasic Inc.</organization>
  9. <address>
  10. <postal>
  11. <street>4101, Molson Street</street>
  12. <city>Montreal</city>
  13. <region>Quebec</region>
  14. <code></code>
  15. <country>Canada</country>
  16. </postal>
  17. <phone>+1 514 282-8858</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 (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 | | Delay | ^
  700. +->|decoder|----| compens- |----+
  701. | | | ation |
  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. Opus only uses static contexts.
  746. They are not adapted to the statistics of the data as it is coded.
  747. </t>
  748. <t>
  749. Suppose there is a context with n symbols, identified with an index that ranges
  750. from 0 to n-1.
  751. The parameters needed to encode or decode a symbol in this context are
  752. represented by a three-tuple (fl[k],&nbsp;fh[k],&nbsp;ft), with
  753. 0&nbsp;&lt;=&nbsp;fl[k]&nbsp;&lt;&nbsp;fh[k]&nbsp;&lt;=&nbsp;ft&nbsp;&lt;=&nbsp;65535.
  754. The values of this tuple are derived from the probability model for the
  755. symbol, represented by traditional "frequency counts" (although, since Opus
  756. uses static contexts, these are not updated as symbols are decoded).
  757. Let f[i] be the frequency of symbol i.
  758. Then the three-tuple corresponding to symbol k is given by
  759. </t>
  760. <figure align="center">
  761. <artwork align="center"><![CDATA[
  762. k-1 n-1
  763. __ __
  764. fl[k] = \ f[i], fh[k] = fl[k] + f[k], ft[k] = \ f[i]
  765. /_ /_
  766. i=0 i=0
  767. ]]></artwork>
  768. </figure>
  769. <t>
  770. The range decoder extracts the symbols and integers encoded using the range
  771. encoder in <xref target="range-encoder"/>.
  772. The range decoder maintains an internal state vector composed of the two-tuple
  773. (val,rng), representing the difference between the high end of the current
  774. range and the actual coded value, minus one, and the size of the current
  775. range, respectively.
  776. Both val and rng are 32-bit unsigned integer values.
  777. The decoder initializes rng to 128 and initializes val to 127 minus the top 7
  778. bits of the first input octet.
  779. The remaining bit is saved for use in the renormalization procedure described
  780. in <xref target="range-decoder-renorm"/>, which the decoder invokes
  781. immediately after initialization to read additional bits and establish the
  782. invariant that rng&nbsp;&gt;&nbsp;2**23.
  783. </t>
  784. <section anchor="decoding-symbols" title="Decoding Symbols">
  785. <t>
  786. Decoding a symbol is a two-step process.
  787. The first step determines a 16-bit unsigned value fs, which lies within the
  788. range of some symbol in the current context.
  789. The second step updates the range decoder state with the three-tuple
  790. (fl[k],&nbsp;fh[k],&nbsp;ft) corresponding to that symbol.
  791. </t>
  792. <t>
  793. The first step is implemented by ec_decode() (entdec.c), which computes
  794. <figure align="center">
  795. <artwork align="center"><![CDATA[
  796. fs = ft - min(val/(rng/ft)+1, ft) .
  797. ]]></artwork>
  798. </figure>
  799. The divisions here are exact integer division.
  800. </t>
  801. <t>
  802. The decoder then identifies the symbol in the current context corresponding to
  803. fs; i.e., the value of k whose three-tuple (fl[k],&nbsp;fh[k],&nbsp;ft)
  804. satisfies fl[k]&nbsp;&lt;=&nbsp;fs&nbsp;&lt;&nbsp;fh[k].
  805. It uses this tuple to update val according to
  806. <figure align="center">
  807. <artwork align="center"><![CDATA[
  808. val = val - (rng/ft)*(ft-fh[k]) .
  809. ]]></artwork>
  810. </figure>
  811. If fl[k] is greater than zero, then the decoder updates rng using
  812. <figure align="center">
  813. <artwork align="center"><![CDATA[
  814. rng = (rng/ft)*(fh[k]-fl[k]) .
  815. ]]></artwork>
  816. </figure>
  817. Otherwise, it updates rng using
  818. <figure align="center">
  819. <artwork align="center"><![CDATA[
  820. rng = rng - (rng/ft)*(ft-fh[k]).
  821. ]]></artwork>
  822. </figure>
  823. </t>
  824. <t>
  825. Using a special case for the first symbol (rather than the last symbol, as is
  826. commonly done in other arithmetic coders) ensures that all the truncation
  827. error from the finite precision arithmetic accumulates in symbol 0.
  828. This makes the cost of coding a 0 slightly smaller, on average, than its
  829. estimated probability indicates and makes the cost of coding any other symbol
  830. slightly larger.
  831. When contexts are designed so that 0 is the most probable symbol, which is
  832. often the case, this strategy minimizes the inefficiency introduced by the
  833. finite precision.
  834. It also makes some of the special-case decoding routines in
  835. <xref target="decoding-alternate"/> particularly simple.
  836. </t>
  837. <t>
  838. After the updates, implemented by ec_dec_update() (entdec.c), the decoder
  839. normalizes the range using the procedure in the next section, and returns the
  840. index k.
  841. </t>
  842. <section anchor="range-decoder-renorm" title="Renormalization">
  843. <t>
  844. To normalize the range, the decoder repeats the following process, implemented
  845. by ec_dec_normalize() (entdec.c), until rng&nbsp;&gt;&nbsp;2**23.
  846. If rng is already greater than 2**23, the entire process is skipped.
  847. First, it sets rng to (rng&lt;&lt;8).
  848. Then it reads the next octet of the payload and combines it with the left-over
  849. bit buffered from the previous octet to form the 8-bit value sym.
  850. It takes the left-over bit as the high bit (bit 7) of sym, and the top 7 bits
  851. of the octet it just read as the other 7 bits of sym.
  852. The remaining bit in the octet just read is buffered for use in the next
  853. iteration.
  854. If no more input octets remain, it uses zero bits instead.
  855. Then, it sets
  856. <figure align="center">
  857. <artwork align="center"><![CDATA[
  858. val = ((val<<8) + (255-sym)) & 0x7FFFFFFF .
  859. ]]></artwork>
  860. </figure>
  861. </t>
  862. <t>
  863. It is normal and expected that the range decoder will read several bytes
  864. into the raw bits data (if any) at the end of the packet by the time the frame
  865. is completely decoded, as illustrated in <xref target="finalize-example"/>.
  866. This same data MUST also be returned as raw bits when requested.
  867. The encoder is expected to terminate the stream in such a way that the decoder
  868. will decode the intended values regardless of the data contained in the raw
  869. bits.
  870. <xref target="encoder-finalizing"/> describes a procedure for doing this.
  871. If the range decoder consumes all of the bytes belonging to the current frame,
  872. it MUST continue to use zero when any further input bytes are required, even
  873. if there is additional data in the current packet from padding or other
  874. frames.
  875. </t>
  876. <figure anchor="finalize-example" title="Illustrative example of raw bits
  877. overlapping range coder data">
  878. <artwork align="center"><![CDATA[
  879. n n+1 n+2 n+3
  880. 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
  881. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  882. : | <----------- Overlap region ------------> | :
  883. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  884. ^ ^
  885. | End of data buffered by the range coder |
  886. ...-----------------------------------------------+
  887. |
  888. | End of data consumed by raw bits
  889. +-------------------------------------------------------...
  890. ]]></artwork>
  891. </figure>
  892. </section>
  893. </section>
  894. <section anchor="decoding-alternate" title="Alternate Decoding Methods">
  895. <t>
  896. The reference implementation uses three additional decoding methods that are
  897. exactly equivalent to the above, but make assumptions and simplifications that
  898. allow for a more efficient implementation.
  899. </t>
  900. <section title="ec_decode_bin()">
  901. <t>
  902. The first is ec_decode_bin() (entdec.c), defined using the parameter ftb
  903. instead of ft.
  904. It is mathematically equivalent to calling ec_decode() with
  905. ft = (1&lt;&lt;ftb), but avoids one of the divisions.
  906. </t>
  907. </section>
  908. <section title="ec_dec_bit_logp()">
  909. <t>
  910. The next is ec_dec_bit_logp() (entdec.c), which decodes a single binary symbol,
  911. replacing both the ec_decode() and ec_dec_update() steps.
  912. The context is described by a single parameter, logp, which is the absolute
  913. value of the base-2 logarithm of the probability of a "1".
  914. It is mathematically equivalent to calling ec_decode() with
  915. ft&nbsp;=&nbsp;(1&lt;&lt;logp), followed by ec_dec_update() with
  916. the 3-tuple (fl[k]&nbsp;=&nbsp;0, fh[k]&nbsp;=&nbsp;(1&lt;&lt;logp)-1,
  917. ft&nbsp;=&nbsp;(1&lt;&lt;logp)) if the returned value
  918. of fs is less than (1&lt;&lt;logp)-1 (a "0" was decoded), and with
  919. (fl[k]&nbsp;=&nbsp;(1&lt;&lt;logp)-1,
  920. fh[k]&nbsp;=&nbsp;ft&nbsp;=&nbsp;(1&lt;&lt;logp)) otherwise (a "1" was
  921. decoded).
  922. The implementation requires no multiplications or divisions.
  923. </t>
  924. </section>
  925. <section title="ec_dec_icdf()">
  926. <t>
  927. The last is ec_dec_icdf() (entdec.c), which decodes a single symbol with a
  928. table-based context of up to 8 bits, also replacing both the ec_decode() and
  929. ec_dec_update() steps, as well as the search for the decoded symbol in between.
  930. The context is described by two parameters, an icdf
  931. ("inverse" cumulative distribution function) table and ftb.
  932. As with ec_decode_bin(), (1&lt;&lt;ftb) is equivalent to ft.
  933. idcf[k], on the other hand, stores (1&lt;&lt;ftb)-fh[k], which is equal to
  934. (1&lt;&lt;ftb)-fl[k+1].
  935. fl[0] is assumed to be 0, and the table is terminated by a value of 0 (where
  936. fh[k]&nbsp;==&nbsp;ft).
  937. </t>
  938. <t>
  939. The function is mathematically equivalent to calling ec_decode() with
  940. ft&nbsp;=&nbsp;(1&lt;&lt;ftb), using the returned value fs to search the table
  941. for the first entry where fs&nbsp;&lt;&nbsp;(1&lt;&lt;ftb)-icdf[k], and
  942. calling ec_dec_update() with fl[k]&nbsp;=&nbsp;(1&lt;&lt;ftb)-icdf[k-1] (or 0
  943. if k&nbsp;==&nbsp;0), fh[k]&nbsp;=&nbsp;(1&lt;&lt;ftb)-idcf[k], and
  944. ft&nbsp;=&nbsp;(1&lt;&lt;ftb).
  945. Combining the search with the update allows the division to be replaced by a
  946. series of multiplications (which are usually much cheaper), and using an
  947. inverse CDF allows the use of an ftb as large as 8 in an 8-bit table without
  948. any special cases.
  949. This is the primary interface with the range decoder in the SILK layer, though
  950. it is used in a few places in the CELT layer as well.
  951. </t>
  952. <t>
  953. Although icdf[k] is more convenient for the code, the frequency counts, f[k],
  954. are a more natural representation of the probability distribution function
  955. (PDF) for a given symbol.
  956. Therefore this draft lists the latter, not the former, when describing the
  957. context in which a symbol is coded as a list, e.g., {4, 4, 4, 4}/16 for a
  958. uniform context with four possible values and ft=16.
  959. The value of ft after the slash is always the sum of the entries in the PDF,
  960. but is included for convenience.
  961. Contexts with identical probabilities, f[k]/ft, but different values of ft
  962. (or equivalently, ftb) are not the same, and cannot, in general, be used in
  963. place of one another.
  964. An icdf table is also not capable of representing a PDF where the first symbol
  965. has 0 probability.
  966. In such contexts, ec_dec_icdf() can decode the symbol by using a table that
  967. drops the entries for any initial zero-probability values and adding the
  968. constant offset of the first value with a non-zero probability to its return
  969. value.
  970. </t>
  971. </section>
  972. </section>
  973. <section anchor="decoding-bits" title="Decoding Raw Bits">
  974. <t>
  975. The raw bits used by the CELT layer are packed at the end of the packet, with
  976. the least significant bit of the first value packed in the least significant
  977. bit of the last byte, filling up to the most significant bit in the last byte,
  978. continuing on to the least significant bit of the penultimate byte, and so on.
  979. The reference implementation reads them using ec_dec_bits() (entdec.c).
  980. Because the range decoder must read several bytes ahead in the stream, as
  981. described in <xref target="range-decoder-renorm"/>, the input consumed by the
  982. raw bits MAY overlap with the input consumed by the range coder, and a decoder
  983. MUST allow this.
  984. The format should render it impossible to attempt to read more raw bits than
  985. there are actual bits in the frame, though a decoder MAY wish to check for
  986. this and report an error.
  987. </t>
  988. </section>
  989. <section anchor="decoding-ints" title="Decoding Uniformly Distributed Integers">
  990. <t>
  991. The ec_dec_uint() (entdec.c) function decodes one of ft equiprobable values in
  992. the range 0 to ft-1, inclusive, each with a frequency of 1, where ft may be as
  993. large as 2**32-1.
  994. Because ec_decode() is limited to a total frequency of 2**16-1, this is split
  995. up into a range coded symbol representing up to 8 of the high bits of the
  996. value, and, if necessary, raw bits representing the remaining bits.
  997. The limit of 8 bits in the range coded symbol is a trade-off between
  998. implementation complexity, modeling error (since the symbols no longer truly
  999. have equal coding cost), and rounding error introduced by the range coder
  1000. itself (which gets larger as more bits are included).
  1001. Using raw bits reduces the maximum number of divisions required in the worst
  1002. case, but means that it may be possible to decode a value outside the range
  1003. 0 to ft-1, inclusive.
  1004. </t>
  1005. <t>
  1006. ec_dec_uint() takes a single, positive parameter, ft, which is not necessarily
  1007. a power of two, and returns an integer, t, whose value lies between 0 and
  1008. ft-1, inclusive.
  1009. Let ftb = ilog(ft-1), i.e., the number of bits required to store ft-1 in two's
  1010. complement notation.
  1011. If ftb is 8 or less, then t is decoded with t = ec_decode(ft), and the range
  1012. coder state is updated using the three-tuple (t,t+1,ft).
  1013. </t>
  1014. <t>
  1015. If ftb is greater than 8, then the top 8 bits of t are decoded using
  1016. t = ec_decode((ft-1&gt;&gt;ftb-8)+1),
  1017. the decoder state is updated using the three-tuple
  1018. (t,t+1,(ft-1&gt;&gt;ftb-8)+1), and the remaining bits are decoded as raw bits,
  1019. setting t = t&lt;&lt;ftb-8|ec_dec_bits(ftb-8).
  1020. If, at this point, t >= ft, then the current frame is corrupt.
  1021. In that case, the decoder should assume there has been an error in the coding,
  1022. decoding, or transmission and SHOULD take measures to conceal the
  1023. error and/or report to the application that a problem has occurred.
  1024. </t>
  1025. </section>
  1026. <section anchor="decoder-tell" title="Current Bit Usage">
  1027. <t>
  1028. The bit allocation routines in the CELT decoder need a conservative upper bound
  1029. on the number of bits that have been used from the current frame thus far,
  1030. including both range coder bits and raw bits.
  1031. This drives allocation decisions that must match those made in the encoder.
  1032. The upper bound is computed in the reference implementation to whole-bit
  1033. precision by the function ec_tell() (entcode.h) and to fractional 1/8th bit
  1034. precision by the function ec_tell_frac() (entcode.c).
  1035. Like all operations in the range coder, it must be implemented in a bit-exact
  1036. manner, and must produce exactly the same value returned by the same functions
  1037. in the encoder after encoding the same symbols.
  1038. </t>
  1039. <t>
  1040. ec_tell() is guaranteed to return ceil(ec_tell_frac()/8.0).
  1041. In various places the codec will check to ensure there is enough room to
  1042. contain a symbol before attempting to decode it.
  1043. In practice, although the number of bits used so far is an upper bound,
  1044. decoding a symbol whose probability model suggests it has a worst-case cost of
  1045. p 1/8th bits may actually advance the return value of ec_tell_frac() by
  1046. p-1, p, or p+1 1/8th bits, due to approximation error in that upper bound,
  1047. truncation error in the range coder, and for large values of ft, modeling
  1048. error in ec_dec_uint().
  1049. </t>
  1050. <t>
  1051. However, this error is bounded, and periodic calls to ec_tell() or
  1052. ec_tell_frac() at precisely defined points in the decoding process prevent it
  1053. from accumulating.
  1054. For a range coder symbol that requires a whole number of bits (i.e.,
  1055. ft/(fh[k]-fl[k]) is a power of two), where there are at least p 1/8th bits
  1056. available, decoding the symbol will never advance the decoder past the end of
  1057. the frame ("bust the budget").
  1058. In this case the return value of ec_tell_frac() will only advance by more than
  1059. p 1/8th bits if there was an additional, fractional number of bits remaining,
  1060. and it will never advance beyond the next whole-bit boundary, which is safe,
  1061. since frames always contain a whole number of bits.
  1062. However, when p is not a whole number of bits, an extra 1/8th bit is required
  1063. to ensure that decoding the symbol will not bust the budget.
  1064. </t>
  1065. <t>
  1066. The reference implementation keeps track of the total number of whole bits that
  1067. have been processed by the decoder so far in the variable nbits_total,
  1068. including the (possibly fractional) number of bits that are currently
  1069. buffered, but not consumed, inside the range coder.
  1070. nbits_total is initialized to 33 just after the initial range renormalization
  1071. process completes (or equivalently, it can be initialized to 9 before the
  1072. first renormalization).
  1073. The extra two bits over the actual amount buffered by the range coder
  1074. guarantees that it is an upper bound and that there is enough room for the
  1075. encoder to terminate the stream.
  1076. Each iteration through the range coder's renormalization loop increases
  1077. nbits_total by 8.
  1078. Reading raw bits increases nbits_total by the number of raw bits read.
  1079. </t>
  1080. <section anchor="ec_tell" title="ec_tell()">
  1081. <t>
  1082. The whole number of bits buffered in rng may be estimated via l = ilog(rng).
  1083. ec_tell() then becomes a simple matter of removing these bits from the total.
  1084. It returns (nbits_total - l).
  1085. </t>
  1086. <t>
  1087. In a newly initialized decoder, before any symbols have been read, this reports
  1088. that 1 bit has been used.
  1089. This is the bit reserved for termination of the encoder.
  1090. </t>
  1091. </section>
  1092. <section anchor="ec_tell_frac" title="ec_tell_frac()">
  1093. <t>
  1094. ec_tell_frac() estimates the number of bits buffered in rng to fractional
  1095. precision.
  1096. Since rng must be greater than 2**23 after renormalization, l must be at least
  1097. 24.
  1098. Let r = rng&gt;&gt;(l-16), so that 32768 &lt;= r &lt; 65536, an unsigned Q15
  1099. value representing the fractional part of rng.
  1100. Then the following procedure can be used to add one bit of precision to l.
  1101. First, update r = r*r&gt;&gt;15.
  1102. Then add the 16th bit of r to l via l = 2*l + (r&gt;&gt;16).
  1103. Finally, if this bit was a 1, reduce r by a factor of two via r = r&gt;&gt;1,
  1104. so that it once again lies in the range 32768 &lt;= r &lt; 65536.
  1105. </t>
  1106. <t>
  1107. This procedure is repeated three times to extend l to 1/8th bit precision.
  1108. ec_tell_frac() then returns (nbits_total*8 - l).
  1109. </t>
  1110. </section>
  1111. </section>
  1112. </section>
  1113. <section anchor='outline_decoder' title='SILK Decoder'>
  1114. <t>
  1115. The decoder's LP layer uses a modified version of the SILK codec (herein simply
  1116. called "SILK"), which runs a decoded excitation signal through adaptive
  1117. long-term and short-term prediction synthesis filters.
  1118. It runs in NB, MB, and WB modes internally.
  1119. When used in a hybrid frame in SWB or FB mode, the LP layer itself still only
  1120. runs in WB mode.
  1121. </t>
  1122. <t>
  1123. Internally, the LP layer of a single Opus frame is composed of either a single
  1124. 10&nbsp;ms regular SILK frame or between one and three 20&nbsp;ms regular SILK
  1125. frames.
  1126. A stereo Opus frame may double the number of regular SILK frames (up to a total
  1127. of six), since it includes separate frames for a mid channel and, optionally,
  1128. a side channel.
  1129. Optional Low Bit-Rate Redundancy (LBRR) frames, which are reduced-bitrate
  1130. encodings of previous SILK frames, may be included to aid in recovery from
  1131. packet loss.
  1132. If present, these appear before the regular SILK frames.
  1133. They are in most respects identical to regular, active SILK frames, except that
  1134. they are usually encoded with a lower bitrate.
  1135. This draft uses "SILK frame" to refer to either one and "regular SILK frame" if
  1136. it needs to draw a distinction between the two.
  1137. </t>
  1138. <t>
  1139. Each SILK frame is in turn composed of either two or four 5&nbsp;ms subframes.
  1140. Various parameters, such as the quantization gain of the excitation and the
  1141. pitch lag and filter coefficients can vary on a subframe-by-subframe basis.
  1142. </t>
  1143. <t>
  1144. All of these frames and subframes are decoded from the same range coder, with
  1145. no padding between them.
  1146. Thus packing multiple SILK frames in a single Opus frame saves, on average,
  1147. half a byte per SILK frame.
  1148. It also allows some parameters to be predicted from prior SILK frames in the
  1149. same Opus frame, since this does not degrade packet loss robustness (beyond
  1150. any penalty for merely using fewer, larger packets to store multiple frames).
  1151. </t>
  1152. <t>
  1153. Stereo support in SILK uses a variant of mid-side coding, allowing a mono
  1154. decoder to simply decode the mid channel.
  1155. However, the data for the two channels is interleaved, so a mono decoder must
  1156. still unpack the data for the side channel.
  1157. It would be required to do so anyway for hybrid Opus frames, or to support
  1158. decoding individual 20&nbsp;ms frames.
  1159. </t>
  1160. <texttable anchor="silk_symbols">
  1161. <ttcol align="center">Symbol(s)</ttcol>
  1162. <ttcol align="center">PDF(s)</ttcol>
  1163. <ttcol align="center">Condition</ttcol>
  1164. <c>VAD flags</c>
  1165. <c>{1, 1}/2</c>
  1166. <c/>
  1167. <c>LBRR flag</c>
  1168. <c>{1, 1}/2</c>
  1169. <c/>
  1170. <c>Per-frame LBRR flags</c>
  1171. <c><xref target="silk_lbrr_flag_pdfs"/></c>
  1172. <c><xref target="silk_lbrr_flags"/></c>
  1173. <c>LBRR Frame(s)</c>
  1174. <c><xref target="silk_frame"/></c>
  1175. <c><xref target="silk_lbrr_flags"/></c>
  1176. <c>Regular SILK Frame(s)</c>
  1177. <c><xref target="silk_frame"/></c>
  1178. <c/>
  1179. <postamble>
  1180. Organization of the SILK layer of an Opus frame.
  1181. </postamble>
  1182. </texttable>
  1183. <section title="Decoder Modules">
  1184. <t>
  1185. An overview of the decoder is given in <xref target="decoder_figure"/>.
  1186. </t>
  1187. <figure align="center" anchor="decoder_figure">
  1188. <artwork align="center">
  1189. <![CDATA[
  1190. +---------+ +------------+
  1191. -->| Range |--->| Decode |---------------------------+
  1192. 1 | Decoder | 2 | Parameters |----------+ 5 |
  1193. +---------+ +------------+ 4 | |
  1194. 3 | | |
  1195. \/ \/ \/
  1196. +------------+ +------------+ +------------+
  1197. | Generate |-->| LTP |-->| LPC |-->
  1198. | Excitation | | Synthesis | | Synthesis | 6
  1199. +------------+ +------------+ +------------+
  1200. 1: Range encoded bitstream
  1201. 2: Coded parameters
  1202. 3: Pulses and gains
  1203. 4: Pitch lags and LTP coefficients
  1204. 5: LPC coefficients
  1205. 6: Decoded signal
  1206. ]]>
  1207. </artwork>
  1208. <postamble>Decoder block diagram.</postamble>
  1209. </figure>
  1210. <section title='Range Decoder'>
  1211. <t>
  1212. 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.
  1213. </t>
  1214. </section>
  1215. <section title='Decode Parameters'>
  1216. <t>
  1217. Pulses and gains are decoded from the parameters that were decoded by the range decoder.
  1218. </t>
  1219. <t>
  1220. 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.
  1221. 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
  1222. <xref target='lsf_stabilizer_overview_section' />. The LSF coefficients are then converted to LPC coefficients, and passed on to the LPC synthesis filter.
  1223. </t>
  1224. </section>
  1225. <section title='Generate Excitation'>
  1226. <t>
  1227. The pulses signal is multiplied with the quantization gain to create the excitation signal.
  1228. </t>
  1229. </section>
  1230. <section title='LTP Synthesis'>
  1231. <t>
  1232. 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
  1233. <figure align="center">
  1234. <artwork align="center">
  1235. <![CDATA[
  1236. d
  1237. __
  1238. e_LPC(n) = e(n) + \ e_LPC(n - L - i) * b_i,
  1239. /_
  1240. i=-d
  1241. ]]>
  1242. </artwork>
  1243. </figure>
  1244. using the pitch lag L, and the decoded LTP coefficients b_i.
  1245. The number of LTP coefficients is 5, and thus d&nbsp;=&nbsp;2.
  1246. For unvoiced speech, the output signal is simply a copy of the excitation signal, i.e., e_LPC(n) = e(n).
  1247. </t>
  1248. </section>
  1249. <section title='LPC Synthesis'>
  1250. <t>
  1251. 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
  1252. <figure align="center">
  1253. <artwork align="center">
  1254. <![CDATA[
  1255. d_LPC
  1256. __
  1257. y(n) = e_LPC(n) + \ y(n - i) * a_i,
  1258. /_
  1259. i=1
  1260. ]]>
  1261. </artwork>
  1262. </figure>
  1263. where d_LPC is the LPC synthesis filter order, and y(n) is the decoded output signal.
  1264. </t>
  1265. </section>
  1266. </section>
  1267. <!--TODO: Document mandated decoder resets-->
  1268. <section title="Header Bits">
  1269. <t>
  1270. The LP layer begins with two to eight header bits, decoded in silk_Decode()
  1271. (silk_dec_API.c).
  1272. These consist of one Voice Activity Detection (VAD) bit per frame (up to 3),
  1273. followed by a single flag indicating the presence of LBRR frames.
  1274. For a stereo packet, these flags correspond to the mid channel, and a second
  1275. set of flags is included for the side channel.
  1276. </t>
  1277. <t>
  1278. Because these are the first symbols decoded by the range coder, they can be
  1279. extracted directly from the upper bits of the first byte of compressed data.
  1280. Thus, a receiver can determine if an Opus frame contains any active SILK frames
  1281. without the overhead of using the range decoder.
  1282. </t>
  1283. </section>
  1284. <section anchor="silk_lbrr_flags" title="LBRR Flags">
  1285. <t>
  1286. For Opus frames longer than 20&nbsp;ms, a set of per-frame LBRR flags is
  1287. decoded for each channel that has its LBRR flag set.
  1288. For 40&nbsp;ms Opus frames the 2-frame LBRR flag PDF from
  1289. <xref target="silk_lbrr_flag_pdfs"/> is used, and for 60&nbsp;ms Opus frames
  1290. the 3-frame LBRR flag PDF is used.
  1291. For each channel, the resulting 2- or 3-bit integer contains the corresponding
  1292. LBRR flag for each frame, packed in order from the LSb to the MSb.
  1293. </t>
  1294. <texttable anchor="silk_lbrr_flag_pdfs" title="LBRR Flag PDFs">
  1295. <ttcol>Frame Size</ttcol>
  1296. <ttcol>PDF</ttcol>
  1297. <c>40&nbsp;ms</c> <c>{0, 53, 53, 150}/256</c>
  1298. <c>60&nbsp;ms</c> <c>{0, 41, 20, 29, 41, 15, 28, 82}/256</c>
  1299. </texttable>
  1300. </section>
  1301. <section anchor="silk_lbrr_frames" title="LBRR Frames">
  1302. <t>
  1303. The LBRR frames, if present, immediately follow, one per set LBRR flag, and
  1304. prior to any regular SILK frames.
  1305. <xref target="silk_frame"/> describes their exact contents.
  1306. LBRR frames do not include their own separate VAD flags.
  1307. LBRR frames are only meant to be transmitted for active speech, thus all LBRR
  1308. frames are treated as active.
  1309. </t>
  1310. <t>
  1311. In a stereo Opus frame longer than 20&nbsp;ms, although all the per-frame LBRR
  1312. flags for the mid channel are coded before the per-frame LBRR flags for the
  1313. side channel, the LBRR frames themselves are interleaved.
  1314. The LBRR frame for the mid channel of a given 20&nbsp;ms interval (if present)
  1315. is immediately followed by the corresponding LBRR frame for the side channel
  1316. (if present).
  1317. </t>
  1318. </section>
  1319. <section anchor="silk_regular_frames" title="Regular SILK Frames">
  1320. <t>
  1321. The regular SILK frame(s) follow the LBRR frames (if any).
  1322. <xref target="silk_frame"/> describes their contents, as well.
  1323. Unlike the LBRR frames, a regular SILK frame is always coded for each time
  1324. interval in an Opus frame, even if the corresponding VAD flag is unset.
  1325. Like the LBRR frames, in stereo Opus frames longer than 20&nbsp;ms, the mid and
  1326. side frames are interleaved for each 20&nbsp;ms interval.
  1327. The side frame may be skipped by coding an appropriate flag, as detailed in
  1328. <xref target="silk_mid_only_flag"/>.
  1329. </t>
  1330. </section>
  1331. <section anchor="silk_frame" title="SILK Frame Contents">
  1332. <t>
  1333. Each SILK frame includes a set of side information that encodes the frame type,
  1334. quantization type and gains, short-term prediction filter coefficients, an LSF
  1335. interpolation weight, long-term prediction filter lags and gains, and a
  1336. linear congruential generator (LCG) seed.
  1337. The quantized excitation signal follows these at the end of the frame.
  1338. <xref target="silk_frame_symbols"/> details the overall organization of a
  1339. SILK frame.
  1340. </t>
  1341. <texttable anchor="silk_frame_symbols">
  1342. <ttcol align="center">Symbol(s)</ttcol>
  1343. <ttcol align="center">PDF(s)</ttcol>
  1344. <ttcol align="center">Condition</ttcol>
  1345. <c>Stereo Prediction Weights</c>
  1346. <c><xref target="silk_stereo_pred_pdfs"/></c>
  1347. <c><xref target="silk_stereo_pred"/></c>
  1348. <c>Mid-Only Flag</c>
  1349. <c><xref target="silk_mid_only_pdf"/></c>
  1350. <c><xref target="silk_mid_only_flag"/></c>
  1351. <c>Frame Type</c>
  1352. <c><xref target="silk_frame_type"/></c>
  1353. <c/>
  1354. <c>Subframe Gains</c>
  1355. <c><xref target="silk_gains"/></c>
  1356. <c/>
  1357. <c>Normalized LSF Stage 1 Index</c>
  1358. <c><xref target="silk_nlsf_stage1_pdfs"/></c>
  1359. <c/>
  1360. <c>Normalized LSF Stage 2 Residual</c>
  1361. <c><xref target="silk_nlsfs"/></c>
  1362. <c/>
  1363. <c>Normalized LSF Interpolation Weight</c>
  1364. <c><xref target="silk_nlsf_interp_pdf"/></c>
  1365. <c><xref target="silk_nlsf_interpolation"/></c>
  1366. <c>Primary Pitch Lag</c>
  1367. <c><xref target="silk_ltp_lags"/></c>
  1368. <c>Voiced frame</c>
  1369. <c>Subframe Pitch Contour</c>
  1370. <c><xref target="silk_pitch_contour_pdfs"/></c>
  1371. <c>Voiced frame</c>
  1372. <c>Periodicity Index</c>
  1373. <c><xref target="silk_perindex_pdf"/></c>
  1374. <c>Voiced frame</c>
  1375. <c>LTP Filter</c>
  1376. <c><xref target="silk_ltp_filter_pdfs"/></c>
  1377. <c>Voiced frame</c>
  1378. <c>LTP Scaling</c>
  1379. <c><xref target="silk_ltp_scaling_pdf"/></c>
  1380. <c><xref target="silk_ltp_scaling"/></c>
  1381. <c>LCG Seed</c>
  1382. <c><xref target="silk_seed_pdf"/></c>
  1383. <c/>
  1384. <c>Excitation Rate Level</c>
  1385. <c><xref target="silk_rate_level_pdfs"/></c>
  1386. <c/>
  1387. <c>Excitation Pulse Counts</c>
  1388. <c><xref target="silk_pulse_count_pdfs"/></c>
  1389. <c/>
  1390. <c>Excitation Pulse Locations</c>
  1391. <c><xref target="silk_pulse_locations"/></c>
  1392. <c>Non-zero pulse count</c>
  1393. <c>Excitation LSb's</c>
  1394. <c><xref target="silk_shell_lsb_pdf"/></c>
  1395. <c><xref target="silk_pulse_counts"/></c>
  1396. <c>Excitation Signs</c>
  1397. <c><xref target="silk_sign_pdfs"/></c>
  1398. <c><xref target="silk_signs"/></c>
  1399. <postamble>
  1400. Order of the symbols in an individual SILK frame.
  1401. </postamble>
  1402. </texttable>
  1403. <section anchor="silk_stereo_pred" title="Stereo Prediction Weights">
  1404. <t>
  1405. A SILK frame corresponding to the mid channel of a stereo Opus frame begins
  1406. with a pair of side channel prediction weights, designed such that zeros
  1407. indicate normal mid-side coupling.
  1408. Since these weights can change on every frame, the first portion of each frame
  1409. linearly interpolates between the previous weights and the current ones, using
  1410. zeros for the previous weights if none are available.
  1411. These prediction weights are never included in a mono Opus frame, and the
  1412. previous weights are reset to zeros on any transition from a mono to stereo.
  1413. They are also not included in an LBRR frame for the side channel, even if the
  1414. LBRR flags indicate the corresponding mid channel was not coded.
  1415. In that case, the previous weights are used, again substituting in zeros if no
  1416. previous weights are available since the last decoder reset.
  1417. </t>
  1418. <t>
  1419. The prediction weights are coded in three separate pieces, which are decoded
  1420. by silk_stereo_decode_pred() (silk_decode_stereo_pred.c).
  1421. The first piece jointly codes the high-order part of a table index for both
  1422. weights.
  1423. The second piece codes the low-order part of each table index.
  1424. The third piece codes an offset used to linearly interpolate between table
  1425. indices.
  1426. The details are as follows.
  1427. </t>
  1428. <t>
  1429. Let n be an index decoded with the 25-element stage-1 PDF in
  1430. <xref target="silk_stereo_pred_pdfs"/>.
  1431. Then let i0 and i1 be indices decoded with the stage-2 and stage-3 PDFs in
  1432. <xref target="silk_stereo_pred_pdfs"/>, respectively, and let i2 and i3
  1433. be two more indices decoded with the stage-2 and stage-3 PDFs, all in that
  1434. order.
  1435. </t>
  1436. <texttable anchor="silk_stereo_pred_pdfs" title="Stereo Weight PDFs">
  1437. <ttcol align="left">Stage</ttcol>
  1438. <ttcol align="left">PDF</ttcol>
  1439. <c>Stage 1</c>
  1440. <c>{7, 2, 1, 1, 1,
  1441. 10, 24, 8, 1, 1,
  1442. 3, 23, 92, 23, 3,
  1443. 1, 1, 8, 24, 10,
  1444. 1, 1, 1, 2, 7}/256</c>
  1445. <c>Stage 2</c>
  1446. <c>{85, 86, 85}/256</c>
  1447. <c>Stage 3</c>
  1448. <c>{51, 51, 52, 51, 51}/256</c>
  1449. </texttable>
  1450. <t>
  1451. Then use n, i0, and i2 to form two table indices, wi0 and wi1, according to
  1452. <figure align="center">
  1453. <artwork align="center"><![CDATA[
  1454. wi0 = i0 + 3*(n/5)
  1455. wi1 = i2 + 3*(n%5)
  1456. ]]></artwork>
  1457. </figure>
  1458. where the division is exact integer division.
  1459. The range of these indices is 0 to 14, inclusive.
  1460. Let w[i] be the i'th weight from <xref target="silk_stereo_weights_table"/>.
  1461. Then the two prediction weights, w0_Q13 and w1_Q13, are
  1462. <figure align="center">
  1463. <artwork align="center"><![CDATA[
  1464. w1_Q13 = w_Q13[wi1]
  1465. + ((w_Q13[wi1+1] - w_Q13[wi1])*6554) >> 16)*(2*i3 + 1)
  1466. w0_Q13 = w_Q13[wi0]
  1467. + ((w_Q13[wi0+1] - w_Q13[wi0])*6554) >> 16)*(2*i1 + 1)
  1468. - w1_Q13
  1469. ]]></artwork>
  1470. </figure>
  1471. </t>
  1472. <texttable anchor="silk_stereo_weights_table"
  1473. title="Stereo Weight Table">
  1474. <ttcol align="left">Index</ttcol>
  1475. <ttcol align="right">Weight (Q13)</ttcol>
  1476. <c>0</c> <c>-13732</c>
  1477. <c>1</c> <c>-10050</c>
  1478. <c>2</c> <c>-8266</c>
  1479. <c>3</c> <c>-7526</c>
  1480. <c>4</c> <c>-6500</c>
  1481. <c>5</c> <c>-5000</c>
  1482. <c>6</c> <c>-2950</c>
  1483. <c>7</c> <c>-820</c>
  1484. <c>8</c> <c>820</c>
  1485. <c>9</c> <c>2950</c>
  1486. <c>10</c> <c>5000</c>
  1487. <c>11</c> <c>6500</c>
  1488. <c>12</c> <c>7526</c>
  1489. <c>13</c> <c>8266</c>
  1490. <c>14</c> <c>10050</c>
  1491. <c>15</c> <c>13732</c>
  1492. </texttable>
  1493. </section>
  1494. <section anchor="silk_mid_only_flag" title="Mid-Only Flag">
  1495. <t>
  1496. A flag appears after the stereo prediction weights that indicates if only the
  1497. mid channel is coded for this time interval.
  1498. It is omitted when there are no stereo weights, i.e., unless the SILK frame
  1499. corresponds to the mid channel of a stereo Opus frame, and it is also omitted
  1500. for an LBRR frame when the corresponding LBRR flags indicate the side channel
  1501. is present.
  1502. When present, the decoder reads a single value using the PDF in
  1503. <xref target="silk_mid_only_pdf"/>, as implemented in
  1504. silk_stereo_decode_mid_only() (silk_decode_stereo_pred.c).
  1505. If the flag is set, then there is no corresponding SILK frame for the side
  1506. channel, the entire decoding process for the side channel is skipped, and
  1507. zeros are used during the stereo unmixing process<!--TODO: ref-->.
  1508. As stated above, LBRR frames still include this flag when the LBRR flag
  1509. indicates that the side channel is not coded.
  1510. In that case, if this flag is zero (indicating that there should be a side
  1511. channel), then Packet Loss Concealment (PLC, see
  1512. <xref target="Packet Loss Concealment"/>) SHOULD be invoked to recover a
  1513. side channel signal.
  1514. </t>
  1515. <texttable anchor="silk_mid_only_pdf" title="Mid-Only Flag PDF">
  1516. <ttcol align="left">PDF</ttcol>
  1517. <c>{192, 64}/256</c>
  1518. </texttable>
  1519. </section>
  1520. <section anchor="silk_frame_type" title="Frame Type">
  1521. <t>
  1522. Each SILK frame contains a single "frame type" symbol that jointly codes the
  1523. signal type and quantization offset type of the corresponding frame.
  1524. If the current frame is a regular SILK frame whose VAD bit was not set (an
  1525. "inactive" frame), then the frame type symbol takes on a value of either 0 or
  1526. 1 and is decoded using the first PDF in <xref target="silk_frame_type_pdfs"/>.
  1527. If the frame is an LBRR frame or a regular SILK frame whose VAD flag was set
  1528. (an "active" frame), then the value of the symbol may range from 2 to 5,
  1529. inclusive, and is decoded using the second PDF in
  1530. <xref target="silk_frame_type_pdfs"/>.
  1531. <xref target="silk_frame_type_table"/> translates between the value of the
  1532. frame type symbol and the corresponding signal type and quantization offset
  1533. type.
  1534. </t>
  1535. <texttable anchor="silk_frame_type_pdfs" title="Frame Type PDFs">
  1536. <ttcol>VAD Flag</ttcol>
  1537. <ttcol>PDF</ttcol>
  1538. <c>Inactive</c> <c>{26, 230, 0, 0, 0, 0}/256</c>
  1539. <c>Active</c> <c>{0, 0, 24, 74, 148, 10}/256</c>
  1540. </texttable>
  1541. <texttable anchor="silk_frame_type_table"
  1542. title="Signal Type and Quantization Offset Type from Frame Type">
  1543. <ttcol>Frame Type</ttcol>
  1544. <ttcol>Signal Type</ttcol>
  1545. <ttcol align="right">Quantization Offset Type</ttcol>
  1546. <c>0</c> <c>Inactive</c> <c>0</c>
  1547. <c>1</c> <c>Inactive</c> <c>1</c>
  1548. <c>2</c> <c>Unvoiced</c> <c>0</c>
  1549. <c>3</c> <c>Unvoiced</c> <c>1</c>
  1550. <c>4</c> <c>Voiced</c> <c>0</c>
  1551. <c>5</c> <c>Voiced</c> <c>1</c>
  1552. </texttable>
  1553. </section>
  1554. <section anchor="silk_gains" title="Subframe Gains">
  1555. <t>
  1556. A separate quantization gain is coded for each 5&nbsp;ms subframe.
  1557. These gains control the step size between quantization levels of the excitation
  1558. signal and, therefore, the quality of the reconstruction.
  1559. They are independent of the pitch gains coded for voiced frames.
  1560. The quantization gains are themselves uniformly quantized to 6&nbsp;bits on a
  1561. log scale, giving them a resolution of approximately 1.369&nbsp;dB and a range
  1562. of approximately 1.94&nbsp;dB to 88.21&nbsp;dB.
  1563. </t>
  1564. <t>
  1565. For the first LBRR frame, an LBRR frame where the previous LBRR frame in the
  1566. same channel is not coded, or the first regular SILK frame in the current
  1567. channel of an Opus frame, the first subframe uses an independent coding
  1568. method.
  1569. In a stereo Opus frame, the mid-only flag (from
  1570. <xref target="silk_mid_only_flag"/>) may cause the first regular SILK frame in
  1571. the side channel to occur in a later time interval than the first regular SILK
  1572. frame in the mid channel.
  1573. The 3 most significant bits of the quantization gain are decoded using a PDF
  1574. selected from <xref target="silk_independent_gain_msb_pdfs"/> based on the
  1575. decoded signal type.
  1576. </t>
  1577. <texttable anchor="silk_independent_gain_msb_pdfs"
  1578. title="PDFs for Independent Quantization Gain MSb Coding">
  1579. <ttcol align="left">Signal Type</ttcol>
  1580. <ttcol align="left">PDF</ttcol>
  1581. <c>Inactive</c> <c>{32, 112, 68, 29, 12, 1, 1, 1}/256</c>
  1582. <c>Unvoiced</c> <c>{2, 17, 45, 60, 62, 47, 19, 4}/256</c>
  1583. <c>Voiced</c> <c>{1, 3, 26, 71, 94, 50, 9, 2}/256</c>
  1584. </texttable>
  1585. <t>
  1586. The 3 least significant bits are decoded using a uniform PDF:
  1587. </t>
  1588. <texttable anchor="silk_independent_gain_lsb_pdf"
  1589. title="PDF for Independent Quantization Gain LSb Coding">
  1590. <ttcol align="left">PDF</ttcol>
  1591. <c>{32, 32, 32, 32, 32, 32, 32, 32}/256</c>
  1592. </texttable>
  1593. <t>
  1594. For all other subframes (including the first subframe of frames not listed as
  1595. using independent coding above), the quantization gain is coded relative to
  1596. the gain from the previous subframe (in the same channel).
  1597. In particular, unlike an LBRR frame where the previous frame is not coded, in a
  1598. 60&nbsp;ms stereo Opus frame, if the first and third regular SILK frames
  1599. in the side channel are coded, but the second is not, the first subframe of
  1600. the third frame is still coded relative to the last subframe in the first
  1601. frame.
  1602. The PDF in <xref target="silk_delta_gain_pdf"/> yields a delta gain index
  1603. between 0 and 40, inclusive.
  1604. </t>
  1605. <texttable anchor="silk_delta_gain_pdf"
  1606. title="PDF for Delta Quantization Gain Coding">
  1607. <ttcol align="left">PDF</ttcol>
  1608. <c>{6, 5, 11, 31, 132, 21, 8, 4,
  1609. 3, 2, 2, 2, 1, 1, 1, 1,
  1610. 1, 1, 1, 1, 1, 1, 1, 1,
  1611. 1, 1, 1, 1, 1, 1, 1, 1,
  1612. 1, 1, 1, 1, 1, 1, 1, 1, 1}/256</c>
  1613. </texttable>
  1614. <t>
  1615. The following formula translates this index into a quantization gain for the
  1616. current subframe using the gain from the previous subframe:
  1617. </t>
  1618. <figure align="center">
  1619. <artwork align="center"><![CDATA[
  1620. log_gain = min(max(2*gain_index - 16,
  1621. previous_log_gain + gain_index - 4), 63)
  1622. ]]></artwork>
  1623. </figure>
  1624. <t>
  1625. silk_gains_dequant() (silk_gain_quant.c) dequantizes the gain for the
  1626. k'th subframe and converts it into a linear Q16 scale factor via
  1627. <figure align="center">
  1628. <artwork align="center"><![CDATA[
  1629. gain_Q16[k] = silk_log2lin((0x1D1C71*log_gain>>16) + 2090)
  1630. ]]></artwork>
  1631. </figure>
  1632. </t>
  1633. <t>
  1634. The function silk_log2lin() (silk_log2lin.c) computes an approximation of
  1635. of 2**(inLog_Q7/128.0), where inLog_Q7 is its Q7 input.
  1636. Let i = inLog_Q7&gt;&gt;7 be the integer part of inLogQ7 and
  1637. f = inLog_Q7&amp;127 be the fractional part.
  1638. Then, if i &lt; 16, then
  1639. <figure align="center">
  1640. <artwork align="center"><![CDATA[
  1641. (1<<i) + (((-174*f*(128-f)>>16)+f)>>7)*(1<<i)
  1642. ]]></artwork>
  1643. </figure>
  1644. yields the approximate exponential.
  1645. Otherwise, silk_log2lin uses
  1646. <figure align="center">
  1647. <artwork align="center"><![CDATA[
  1648. (1<<i) + ((-174*f*(128-f)>>16)+f)*((1<<i)>>7) .
  1649. ]]></artwork>
  1650. </figure>
  1651. </t>
  1652. </section>
  1653. <section anchor="silk_nlsfs" title="Normalized Line Spectral Frequencies">
  1654. <t>
  1655. Normalized Line Spectral Frequencies (LSFs) follow the quantization gains in
  1656. the bitstream, and represent the Linear Prediction Coefficients (LPCs) for the
  1657. current SILK frame.
  1658. Once decoded, they form an increasing list of Q15 values between 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. NB and MB frames use an order-10 predictor, while WB frames use an order-16
  1670. predictor, and thus have different sets of tables.
  1671. The first VQ stage uses a 32-element codebook, coded with one of the PDFs in
  1672. <xref target="silk_nlsf_stage1_pdfs"/>, depending on the audio bandwidth and
  1673. the signal type of the current SILK frame.
  1674. This yields a single index, I1, for the entire frame.
  1675. This indexes an element in a coarse codebook, selects the PDFs for the
  1676. second stage of the VQ, and selects the prediction weights used to remove
  1677. intra-frame redundancy from the second stage.
  1678. The actual codebook elements are listed in
  1679. <xref target="silk_nlsf_nbmb_codebook"/> and
  1680. <xref target="silk_nlsf_wb_codebook"/>, but they are not needed until the last
  1681. stages of reconstructing the LSF coefficients.
  1682. </t>
  1683. <texttable anchor="silk_nlsf_stage1_pdfs"
  1684. title="PDFs for Normalized LSF Index Stage-1 Decoding">
  1685. <ttcol align="left">Audio Bandwidth</ttcol>
  1686. <ttcol align="left">Signal Type</ttcol>
  1687. <ttcol align="left">PDF</ttcol>
  1688. <c>NB or MB</c> <c>Inactive or unvoiced</c>
  1689. <c>
  1690. {44, 34, 30, 19, 21, 12, 11, 3,
  1691. 3, 2, 16, 2, 2, 1, 5, 2,
  1692. 1, 3, 3, 1, 1, 2, 2, 2,
  1693. 3, 1, 9, 9, 2, 7, 2, 1}/256
  1694. </c>
  1695. <c>NB or MB</c> <c>Voiced</c>
  1696. <c>
  1697. {1, 10, 1, 8, 3, 8, 8, 14,
  1698. 13, 14, 1, 14, 12, 13, 11, 11,
  1699. 12, 11, 10, 10, 11, 8, 9, 8,
  1700. 7, 8, 1, 1, 6, 1, 6, 5}/256
  1701. </c>
  1702. <c>WB</c> <c>Inactive or unvoiced</c>
  1703. <c>
  1704. {31, 21, 3, 17, 1, 8, 17, 4,
  1705. 1, 18, 16, 4, 2, 3, 1, 10,
  1706. 1, 3, 16, 11, 16, 2, 2, 3,
  1707. 2, 11, 1, 4, 9, 8, 7, 3}/256
  1708. </c>
  1709. <c>WB</c> <c>Voiced</c>
  1710. <c>
  1711. {1, 4, 16, 5, 18, 11, 5, 14,
  1712. 15, 1, 3, 12, 13, 14, 14, 6,
  1713. 14, 12, 2, 6, 1, 12, 12, 11,
  1714. 10, 3, 10, 5, 1, 1, 1, 3}/256
  1715. </c>
  1716. </texttable>
  1717. <t>
  1718. A total of 16 PDFs are available for the LSF residual in the second stage: the
  1719. 8 (a...h) for NB and MB frames given in
  1720. <xref target="silk_nlsf_stage2_nbmb_pdfs"/>, and the 8 (i...p) for WB frames
  1721. given in <xref target="silk_nlsf_stage2_wb_pdfs"/>.
  1722. Which PDF is used for which coefficient is driven by the index, I1,
  1723. decoded in the first stage.
  1724. <xref target="silk_nlsf_nbmb_stage2_cb_sel"/> lists the letter of the
  1725. corresponding PDF for each normalized LSF coefficient for NB and MB, and
  1726. <xref target="silk_nlsf_wb_stage2_cb_sel"/> lists the same information for WB.
  1727. </t>
  1728. <texttable anchor="silk_nlsf_stage2_nbmb_pdfs"
  1729. title="PDFs for NB/MB Normalized LSF Index Stage-2 Decoding">
  1730. <ttcol align="left">Codebook</ttcol>
  1731. <ttcol align="left">PDF</ttcol>
  1732. <c>a</c> <c>{1, 1, 1, 15, 224, 11, 1, 1, 1}/256</c>
  1733. <c>b</c> <c>{1, 1, 2, 34, 183, 32, 1, 1, 1}/256</c>
  1734. <c>c</c> <c>{1, 1, 4, 42, 149, 55, 2, 1, 1}/256</c>
  1735. <c>d</c> <c>{1, 1, 8, 52, 123, 61, 8, 1, 1}/256</c>
  1736. <c>e</c> <c>{1, 3, 16, 53, 101, 74, 6, 1, 1}/256</c>
  1737. <c>f</c> <c>{1, 3, 17, 55, 90, 73, 15, 1, 1}/256</c>
  1738. <c>g</c> <c>{1, 7, 24, 53, 74, 67, 26, 3, 1}/256</c>
  1739. <c>h</c> <c>{1, 1, 18, 63, 78, 58, 30, 6, 1}/256</c>
  1740. </texttable>
  1741. <texttable anchor="silk_nlsf_stage2_wb_pdfs"
  1742. title="PDFs for WB Normalized LSF Index Stage-2 Decoding">
  1743. <ttcol align="left">Codebook</ttcol>
  1744. <ttcol align="left">PDF</ttcol>
  1745. <c>i</c> <c>{1, 1, 1, 9, 232, 9, 1, 1, 1}/256</c>
  1746. <c>j</c> <c>{1, 1, 2, 28, 186, 35, 1, 1, 1}/256</c>
  1747. <c>k</c> <c>{1, 1, 3, 42, 152, 53, 2, 1, 1}/256</c>
  1748. <c>l</c> <c>{1, 1, 10, 49, 126, 65, 2, 1, 1}/256</c>
  1749. <c>m</c> <c>{1, 4, 19, 48, 100, 77, 5, 1, 1}/256</c>
  1750. <c>n</c> <c>{1, 1, 14, 54, 100, 72, 12, 1, 1}/256</c>
  1751. <c>o</c> <c>{1, 1, 15, 61, 87, 61, 25, 4, 1}/256</c>
  1752. <c>p</c> <c>{1, 7, 21, 50, 77, 81, 17, 1, 1}/256</c>
  1753. </texttable>
  1754. <texttable anchor="silk_nlsf_nbmb_stage2_cb_sel"
  1755. title="Codebook Selection for NB/MB Normalized LSF Index Stage 2 Decoding">
  1756. <ttcol>I1</ttcol>
  1757. <ttcol>Coefficient</ttcol>
  1758. <c/>
  1759. <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>
  1760. <c> 0</c>
  1761. <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>
  1762. <c> 1</c>
  1763. <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>
  1764. <c> 2</c>
  1765. <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>
  1766. <c> 3</c>
  1767. <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>
  1768. <c> 4</c>
  1769. <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>
  1770. <c> 5</c>
  1771. <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>
  1772. <c> g</c>
  1773. <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>
  1774. <c> 7</c>
  1775. <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>
  1776. <c> 8</c>
  1777. <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>
  1778. <c> 9</c>
  1779. <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>
  1780. <c>10</c>
  1781. <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>
  1782. <c>11</c>
  1783. <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>
  1784. <c>12</c>
  1785. <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>
  1786. <c>13</c>
  1787. <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>
  1788. <c>14</c>
  1789. <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>
  1790. <c>15</c>
  1791. <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>
  1792. <c>16</c>
  1793. <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>
  1794. <c>17</c>
  1795. <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>
  1796. <c>18</c>
  1797. <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>
  1798. <c>19</c>
  1799. <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>
  1800. <c>20</c>
  1801. <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>
  1802. <c>21</c>
  1803. <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>
  1804. <c>22</c>
  1805. <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>
  1806. <c>23</c>
  1807. <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>
  1808. <c>24</c>
  1809. <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>
  1810. <c>25</c>
  1811. <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>
  1812. <c>26</c>
  1813. <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>
  1814. <c>27</c>
  1815. <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>
  1816. <c>28</c>
  1817. <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>
  1818. <c>29</c>
  1819. <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>
  1820. <c>30</c>
  1821. <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>
  1822. <c>31</c>
  1823. <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>
  1824. </texttable>
  1825. <texttable anchor="silk_nlsf_wb_stage2_cb_sel"
  1826. title="Codebook Selection for WB Normalized LSF Index Stage 2 Decoding">
  1827. <ttcol>I1</ttcol>
  1828. <ttcol>Coefficient</ttcol>
  1829. <c/>
  1830. <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>
  1831. <c> 0</c>
  1832. <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>
  1833. <c> 1</c>
  1834. <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>
  1835. <c> 2</c>
  1836. <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>
  1837. <c> 3</c>
  1838. <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>
  1839. <c> 4</c>
  1840. <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>
  1841. <c> 5</c>
  1842. <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>
  1843. <c> 6</c>
  1844. <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>
  1845. <c> 7</c>
  1846. <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>
  1847. <c> 8</c>
  1848. <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>
  1849. <c> 9</c>
  1850. <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>
  1851. <c>j0</c>
  1852. <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>
  1853. <c>11</c>
  1854. <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>
  1855. <c>12</c>
  1856. <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>
  1857. <c>13</c>
  1858. <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>
  1859. <c>14</c>
  1860. <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>
  1861. <c>15</c>
  1862. <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>
  1863. <c>16</c>
  1864. <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>
  1865. <c>17</c>
  1866. <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>
  1867. <c>18</c>
  1868. <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>
  1869. <c>19</c>
  1870. <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>
  1871. <c>20</c>
  1872. <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>
  1873. <c>21</c>
  1874. <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>
  1875. <c>22</c>
  1876. <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>
  1877. <c>23</c>
  1878. <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>
  1879. <c>24</c>
  1880. <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>
  1881. <c>25</c>
  1882. <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>
  1883. <c>26</c>
  1884. <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>
  1885. <c>27</c>
  1886. <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>
  1887. <c>28</c>
  1888. <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>
  1889. <c>29</c>
  1890. <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>
  1891. <c>30</c>
  1892. <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>
  1893. <c>31</c>
  1894. <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>
  1895. </texttable>
  1896. <t>
  1897. Decoding the second stage residual proceeds as follows.
  1898. For each coefficient, the decoder reads a symbol using the PDF corresponding to
  1899. I1 from either <xref target="silk_nlsf_nbmb_stage2_cb_sel"/> or
  1900. <xref target="silk_nlsf_wb_stage2_cb_sel"/>, and subtracts 4 from the result
  1901. to give an index in the range -4 to 4, inclusive.
  1902. If the index is either -4 or 4, it reads a second symbol using the PDF in
  1903. <xref target="silk_nlsf_ext_pdf"/>, and adds the value of this second symbol
  1904. to the index, using the same sign.
  1905. This gives the index, I2[k], a total range of -10 to 10, inclusive.
  1906. </t>
  1907. <texttable anchor="silk_nlsf_ext_pdf"
  1908. title="PDF for Normalized LSF Index Extension Decoding">
  1909. <ttcol align="left">PDF</ttcol>
  1910. <c>{156, 60, 24, 9, 4, 2, 1}/256</c>
  1911. </texttable>
  1912. <t>
  1913. The decoded indices from both stages are translated back into normalized LSF
  1914. coefficients in silk_NLSF_decode() (silk_NLSF_decode.c).
  1915. The stage-2 indices represent residuals after both the first stage of the VQ
  1916. and a separate backwards-prediction step.
  1917. The backwards prediction process in the encoder subtracts a prediction from
  1918. each residual formed by a multiple of the coefficient that follows it.
  1919. The decoder must undo this process.
  1920. <xref target="silk_nlsf_pred_weights"/> contains lists of prediction weights
  1921. for each coefficient.
  1922. There are two lists for NB and MB, and another two lists for WB, giving two
  1923. possible prediction weights for each coefficient.
  1924. </t>
  1925. <texttable anchor="silk_nlsf_pred_weights"
  1926. title="Prediction Weights for Normalized LSF Decoding">
  1927. <ttcol align="left">Coefficient</ttcol>
  1928. <ttcol align="right">A</ttcol>
  1929. <ttcol align="right">B</ttcol>
  1930. <ttcol align="right">C</ttcol>
  1931. <ttcol align="right">D</ttcol>
  1932. <c>0</c> <c>179</c> <c>116</c> <c>175</c> <c>68</c>
  1933. <c>1</c> <c>138</c> <c>67</c> <c>148</c> <c>62</c>
  1934. <c>2</c> <c>140</c> <c>82</c> <c>160</c> <c>66</c>
  1935. <c>3</c> <c>148</c> <c>59</c> <c>176</c> <c>60</c>
  1936. <c>4</c> <c>151</c> <c>92</c> <c>178</c> <c>72</c>
  1937. <c>5</c> <c>149</c> <c>72</c> <c>173</c> <c>117</c>
  1938. <c>6</c> <c>153</c> <c>100</c> <c>174</c> <c>85</c>
  1939. <c>7</c> <c>151</c> <c>89</c> <c>164</c> <c>90</c>
  1940. <c>8</c> <c>163</c> <c>92</c> <c>177</c> <c>118</c>
  1941. <c>9</c> <c/> <c/> <c>174</c> <c>136</c>
  1942. <c>10</c> <c/> <c/> <c>196</c> <c>151</c>
  1943. <c>11</c> <c/> <c/> <c>182</c> <c>142</c>
  1944. <c>12</c> <c/> <c/> <c>198</c> <c>160</c>
  1945. <c>13</c> <c/> <c/> <c>192</c> <c>142</c>
  1946. <c>14</c> <c/> <c/> <c>182</c> <c>155</c>
  1947. </texttable>
  1948. <t>
  1949. The prediction is undone using the procedure implemented in
  1950. silk_NLSF_residual_dequant() (silk_NLSF_decode.c), which is as follows.
  1951. Each coefficient selects its prediction weight from one of the two lists based
  1952. on the stage-1 index, I1.
  1953. <xref target="silk_nlsf_nbmb_weight_sel"/> gives the selections for each
  1954. coefficient for NB and MB, and <xref target="silk_nlsf_wb_weight_sel"/> gives
  1955. the selections for WB.
  1956. Let d_LPC be the order of the codebook, i.e., 10 for NB and MB, and 16 for WB,
  1957. and let pred_Q8[k] be the weight for the k'th coefficient selected by this
  1958. process for 0&nbsp;&lt;=&nbsp;k&nbsp;&lt;&nbsp;d_LPC-1.
  1959. Then, the stage-2 residual for each coefficient is computed via
  1960. <figure align="center">
  1961. <artwork align="center"><![CDATA[
  1962. res_Q10[k] = (k+1 < d_LPC ? (res_Q10[k+1]*pred_Q8[k])>>8 : 0)
  1963. + ((((I2[k]<<10) + sign(I2[k])*102)*qstep)>>16) ,
  1964. ]]></artwork>
  1965. </figure>
  1966. where qstep is the Q16 quantization step size, which is 11796 for NB and MB
  1967. and 9830 for WB (representing step sizes of approximately 0.18 and 0.15,
  1968. respectively).
  1969. </t>
  1970. <texttable anchor="silk_nlsf_nbmb_weight_sel"
  1971. title="Prediction Weight Selection for NB/MB Normalized LSF Decoding">
  1972. <ttcol>I1</ttcol>
  1973. <ttcol>Coefficient</ttcol>
  1974. <c/>
  1975. <c><spanx style="vbare">0&nbsp;1&nbsp;2&nbsp;3&nbsp;4&nbsp;5&nbsp;6&nbsp;7&nbsp;8</spanx></c>
  1976. <c> 0</c>
  1977. <c><spanx style="vbare">A&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  1978. <c> 1</c>
  1979. <c><spanx style="vbare">B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  1980. <c> 2</c>
  1981. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  1982. <c> 3</c>
  1983. <c><spanx style="vbare">B&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  1984. <c> 4</c>
  1985. <c><spanx style="vbare">A&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  1986. <c> 5</c>
  1987. <c><spanx style="vbare">A&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  1988. <c> 6</c>
  1989. <c><spanx style="vbare">B&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  1990. <c> 7</c>
  1991. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;A</spanx></c>
  1992. <c> 8</c>
  1993. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A&nbsp;B&nbsp;B</spanx></c>
  1994. <c> 9</c>
  1995. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  1996. <c>10</c>
  1997. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  1998. <c>11</c>
  1999. <c><spanx style="vbare">A&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2000. <c>12</c>
  2001. <c><spanx style="vbare">A&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2002. <c>13</c>
  2003. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2004. <c>14</c>
  2005. <c><spanx style="vbare">B&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2006. <c>15</c>
  2007. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2008. <c>16</c>
  2009. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2010. <c>17</c>
  2011. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2012. <c>18</c>
  2013. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2014. <c>19</c>
  2015. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2016. <c>20</c>
  2017. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;B&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2018. <c>21</c>
  2019. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2020. <c>22</c>
  2021. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2022. <c>23</c>
  2023. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;B&nbsp;B</spanx></c>
  2024. <c>24</c>
  2025. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2026. <c>25</c>
  2027. <c><spanx style="vbare">A&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;B&nbsp;A</spanx></c>
  2028. <c>26</c>
  2029. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  2030. <c>27</c>
  2031. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  2032. <c>28</c>
  2033. <c><spanx style="vbare">A&nbsp;A&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A</spanx></c>
  2034. <c>29</c>
  2035. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;B&nbsp;A&nbsp;A&nbsp;A&nbsp;A&nbsp;A</spanx></c>
  2036. <c>30</c>
  2037. <c><spanx style="vbare">A&nbsp;A&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;A&nbsp;B</spanx></c>
  2038. <c>31</c>
  2039. <c><spanx style="vbare">B&nbsp;A&nbsp;B&nbsp;B&nbsp;A&nbsp;B&nbsp;B&nbsp;B&nbsp;B</spanx></c>
  2040. </texttable>
  2041. <texttable anchor="silk_nlsf_wb_weight_sel"
  2042. title="Prediction Weight Selection for WB Normalized LSF Decoding">
  2043. <ttcol>I1</ttcol>
  2044. <ttcol>Coefficient</ttcol>
  2045. <c/>
  2046. <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>
  2047. <c> 0</c>
  2048. <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>
  2049. <c> 1</c>
  2050. <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>
  2051. <c> 2</c>
  2052. <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>
  2053. <c> 3</c>
  2054. <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>
  2055. <c> 4</c>
  2056. <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>
  2057. <c> 5</c>
  2058. <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>
  2059. <c> 6</c>
  2060. <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>
  2061. <c> 7</c>
  2062. <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>
  2063. <c> 8</c>
  2064. <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>
  2065. <c> 9</c>
  2066. <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>
  2067. <c>10</c>
  2068. <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>
  2069. <c>11</c>
  2070. <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>
  2071. <c>12</c>
  2072. <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>
  2073. <c>13</c>
  2074. <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>
  2075. <c>14</c>
  2076. <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>
  2077. <c>15</c>
  2078. <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>
  2079. <c>16</c>
  2080. <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>
  2081. <c>17</c>
  2082. <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>
  2083. <c>18</c>
  2084. <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>
  2085. <c>19</c>
  2086. <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>
  2087. <c>20</c>
  2088. <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>
  2089. <c>21</c>
  2090. <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>
  2091. <c>22</c>
  2092. <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>
  2093. <c>23</c>
  2094. <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>
  2095. <c>24</c>
  2096. <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>
  2097. <c>25</c>
  2098. <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>
  2099. <c>26</c>
  2100. <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>
  2101. <c>27</c>
  2102. <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>
  2103. <c>28</c>
  2104. <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>
  2105. <c>29</c>
  2106. <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>
  2107. <c>30</c>
  2108. <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>
  2109. <c>31</c>
  2110. <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>
  2111. </texttable>
  2112. <t>
  2113. The spectral distortion introduced by the quantization of each LSF coefficient
  2114. varies, so the stage-2 residual is weighted accordingly, using the
  2115. low-complexity weighting function proposed in <xref target="laroia-icassp"/>.
  2116. The weights are derived directly from the stage-1 codebook vector.
  2117. Let cb1_Q8[k] be the k'th entry of the stage-1 codebook vector from
  2118. <xref target="silk_nlsf_nbmb_codebook"/> or
  2119. <xref target="silk_nlsf_wb_codebook"/>.
  2120. Then for 0&nbsp;&lt;=&nbsp;k&nbsp;&lt;&nbsp;d_LPC the following expression
  2121. computes the square of the weight as a Q18 value:
  2122. <figure align="center">
  2123. <artwork align="center">
  2124. <![CDATA[
  2125. w2_Q18[k] = (1024/(cb1_Q8[k] - cb1_Q8[k-1])
  2126. + 1024/(cb1_Q8[k+1] - cb1_Q8[k])) << 16 ,
  2127. ]]>
  2128. </artwork>
  2129. </figure>
  2130. where cb1_Q8[-1]&nbsp;=&nbsp;0 and cb1_Q8[d_LPC]&nbsp;=&nbsp;256, and the
  2131. division is exact integer division.
  2132. This is reduced to an unsquared, Q9 value using the following square-root
  2133. approximation:
  2134. <figure align="center">
  2135. <artwork align="center"><![CDATA[
  2136. i = ilog(w2_Q18[k])
  2137. f = (w2_Q18[k]>>(i-8)) & 127
  2138. y = ((i&1) ? 32768 : 46214) >> ((32-i)>>1)
  2139. w_Q9[k] = y + ((213*f*y)>>16)
  2140. ]]></artwork>
  2141. </figure>
  2142. The cb1_Q8[] vector completely determines these weights, and they may be
  2143. tabulated and stored as 13-bit unsigned values (with a range of 1819 to 5227,
  2144. inclusive) to avoid computing them when decoding.
  2145. The reference implementation already requires code to compute these weights on
  2146. unquantized coefficients in the encoder, in silk_NLSF_VQ_weights_laroia()
  2147. (silk_NLSF_VQ_weights_laroia.c) and its callers, so it reuses that code in the
  2148. decoder instead of using a pre-computed table to reduce the amount of ROM
  2149. required.
  2150. </t>
  2151. <texttable anchor="silk_nlsf_nbmb_codebook"
  2152. title="Codebook Vectors for NB/MB Normalized LSF Stage 1 Decoding">
  2153. <ttcol>I1</ttcol>
  2154. <ttcol>Codebook</ttcol>
  2155. <c/>
  2156. <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>
  2157. <c>0</c>
  2158. <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>
  2159. <c>1</c>
  2160. <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>
  2161. <c>2</c>
  2162. <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>
  2163. <c>3</c>
  2164. <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>
  2165. <c>4</c>
  2166. <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>
  2167. <c>5</c>
  2168. <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>
  2169. <c>6</c>
  2170. <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>
  2171. <c>7</c>
  2172. <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>
  2173. <c>8</c>
  2174. <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>
  2175. <c>9</c>
  2176. <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>
  2177. <c>10</c>
  2178. <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>
  2179. <c>11</c>
  2180. <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>
  2181. <c>12</c>
  2182. <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>
  2183. <c>13</c>
  2184. <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>
  2185. <c>14</c>
  2186. <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>
  2187. <c>15</c>
  2188. <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>
  2189. <c>16</c>
  2190. <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>
  2191. <c>17</c>
  2192. <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>
  2193. <c>18</c>
  2194. <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>
  2195. <c>19</c>
  2196. <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>
  2197. <c>20</c>
  2198. <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>
  2199. <c>21</c>
  2200. <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>
  2201. <c>22</c>
  2202. <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>
  2203. <c>23</c>
  2204. <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>
  2205. <c>24</c>
  2206. <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>
  2207. <c>25</c>
  2208. <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>
  2209. <c>26</c>
  2210. <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>
  2211. <c>27</c>
  2212. <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>
  2213. <c>28</c>
  2214. <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>
  2215. <c>29</c>
  2216. <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>
  2217. <c>30</c>
  2218. <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>
  2219. <c>31</c>
  2220. <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>
  2221. </texttable>
  2222. <texttable anchor="silk_nlsf_wb_codebook"
  2223. title="Codebook Vectors for WB Normalized LSF Stage 1 Decoding">
  2224. <ttcol>I1</ttcol>
  2225. <ttcol>Codebook</ttcol>
  2226. <c/>
  2227. <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>
  2228. <c>0</c>
  2229. <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>
  2230. <c>1</c>
  2231. <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>
  2232. <c>2</c>
  2233. <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>
  2234. <c>3</c>
  2235. <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>
  2236. <c>4</c>
  2237. <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>
  2238. <c>5</c>
  2239. <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>
  2240. <c>6</c>
  2241. <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>
  2242. <c>7</c>
  2243. <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>
  2244. <c>8</c>
  2245. <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>
  2246. <c>9</c>
  2247. <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>
  2248. <c>10</c>
  2249. <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>
  2250. <c>11</c>
  2251. <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>
  2252. <c>12</c>
  2253. <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>
  2254. <c>13</c>
  2255. <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>
  2256. <c>14</c>
  2257. <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>
  2258. <c>15</c>
  2259. <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>
  2260. <c>16</c>
  2261. <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>
  2262. <c>17</c>
  2263. <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>
  2264. <c>18</c>
  2265. <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>
  2266. <c>19</c>
  2267. <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>
  2268. <c>20</c>
  2269. <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>
  2270. <c>21</c>
  2271. <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>
  2272. <c>22</c>
  2273. <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>
  2274. <c>23</c>
  2275. <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>
  2276. <c>24</c>
  2277. <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>
  2278. <c>25</c>
  2279. <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>
  2280. <c>26</c>
  2281. <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>
  2282. <c>27</c>
  2283. <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>
  2284. <c>28</c>
  2285. <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>
  2286. <c>29</c>
  2287. <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>
  2288. <c>30</c>
  2289. <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>
  2290. <c>31</c>
  2291. <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>
  2292. </texttable>
  2293. <t>
  2294. Given the stage-1 codebook entry cb1_Q8[], the stage-2 residual res_Q10[], and
  2295. their corresponding weights, w_Q9[], the reconstructed normalized LSF
  2296. coefficients are
  2297. <figure align="center">
  2298. <artwork align="center"><![CDATA[
  2299. NLSF_Q15[k] = (cb1_Q8[k]<<7) + (res_Q10[k]<<14)/w_Q9[k] ,
  2300. ]]></artwork>
  2301. </figure>
  2302. where the division is exact integer division.
  2303. However, nothing in either the reconstruction process or the
  2304. quantization process in the encoder thus far guarantees that the coefficients
  2305. are monotonically increasing and separated well enough to ensure a stable
  2306. filter.
  2307. When using the reference encoder, roughly 2% of frames violate this constraint.
  2308. The next section describes a stabilization procedure used to make these
  2309. guarantees.
  2310. </t>
  2311. <section anchor="silk_nlsf_stabilization" title="Normalized LSF Stabilization">
  2312. <t>
  2313. The normalized LSF stabilization procedure is implemented in
  2314. silk_NLSF_stabilize() (silk_NLSF_stabilize.c).
  2315. This process ensures that consecutive values of the normalized LSF
  2316. coefficients, NLSF_Q15[], are spaced some minimum distance apart
  2317. (predetermined to be the 0.01 percentile of a large training set).
  2318. <xref target="silk_nlsf_min_spacing"/> gives the minimum spacings for NB and MB
  2319. and those for WB, where row k is the minimum allowed value of
  2320. NLSF_Q[k]-NLSF_Q[k-1].
  2321. For the purposes of computing this spacing for the first and last coefficient,
  2322. NLSF_Q15[-1] is taken to be 0, and NLSF_Q15[d_LPC] is taken to be 32768.
  2323. </t>
  2324. <texttable anchor="silk_nlsf_min_spacing"
  2325. title="Minimum Spacing for Normalized LSF Coefficients">
  2326. <ttcol>Coefficient</ttcol>
  2327. <ttcol align="right">NB and MB</ttcol>
  2328. <ttcol align="right">WB</ttcol>
  2329. <c>0</c> <c>250</c> <c>100</c>
  2330. <c>1</c> <c>3</c> <c>3</c>
  2331. <c>2</c> <c>6</c> <c>40</c>
  2332. <c>3</c> <c>3</c> <c>3</c>
  2333. <c>4</c> <c>3</c> <c>3</c>
  2334. <c>5</c> <c>3</c> <c>3</c>
  2335. <c>6</c> <c>4</c> <c>5</c>
  2336. <c>7</c> <c>3</c> <c>14</c>
  2337. <c>8</c> <c>3</c> <c>14</c>
  2338. <c>9</c> <c>3</c> <c>10</c>
  2339. <c>10</c> <c>461</c> <c>11</c>
  2340. <c>11</c> <c/> <c>3</c>
  2341. <c>12</c> <c/> <c>8</c>
  2342. <c>13</c> <c/> <c>9</c>
  2343. <c>14</c> <c/> <c>7</c>
  2344. <c>15</c> <c/> <c>3</c>
  2345. <c>16</c> <c/> <c>347</c>
  2346. </texttable>
  2347. <t>
  2348. The procedure starts off by trying to make small adjustments which attempt to
  2349. minimize the amount of distortion introduced.
  2350. After 20 such adjustments, it falls back to a more direct method which
  2351. guarantees the constraints are enforced but may require large adjustments.
  2352. </t>
  2353. <t>
  2354. Let NDeltaMin_Q15[k] be the minimum required spacing for the current audio
  2355. bandwidth from <xref target="silk_nlsf_min_spacing"/>.
  2356. First, the procedure finds the index i where
  2357. NLSF_Q15[i]&nbsp;-&nbsp;NLSF_Q15[i-1]&nbsp;-&nbsp;NDeltaMin_Q15[i] is the
  2358. smallest, breaking ties by using the lower value of i.
  2359. If this value is non-negative, then the stabilization stops; the coefficients
  2360. satisfy all the constraints.
  2361. Otherwise, if i&nbsp;==&nbsp;0, it sets NLSF_Q15[0] to NDeltaMin_Q15[0], and if
  2362. i&nbsp;==&nbsp;d_LPC, it sets NLSF_Q15[d_LPC-1] to
  2363. (32768&nbsp;-&nbsp;NDeltaMin_Q15[d_LPC]).
  2364. For all other values of i, both NLSF_Q15[i-1] and NLSF_Q15[i] are updated as
  2365. follows:
  2366. <figure align="center">
  2367. <artwork align="center"><![CDATA[
  2368. i-1
  2369. __
  2370. min_center_Q15 = (NDeltaMin[i]>>1) + \ NDeltaMin[k]
  2371. /_
  2372. k=0
  2373. d_LPC
  2374. __
  2375. max_center_Q15 = 32768 - (NDeltaMin[i]>>1) - \ NDeltaMin[k]
  2376. /_
  2377. k=i+1
  2378. center_freq_Q15 = clamp(min_center_Q15[i],
  2379. (NLSF_Q15[i-1] + NLSF_Q15[i] + 1)>>1,
  2380. max_center_Q15[i])
  2381. NLSF_Q15[i-1] = center_freq_Q15 - (NDeltaMin_Q15[i]>>1)
  2382. NLSF_Q15[i] = NLSF_Q15[i-1] + NDeltaMin_Q15[i] .
  2383. ]]></artwork>
  2384. </figure>
  2385. Then the procedure repeats again, until it has either executed 20 times or
  2386. has stopped because the coefficients satisfy all the constraints.
  2387. </t>
  2388. <t>
  2389. After the 20th repetition of the above procedure, the following fallback
  2390. procedure executes once.
  2391. First, the values of NLSF_Q15[k] for 0&nbsp;&lt;=&nbsp;k&nbsp;&lt;&nbsp;d_LPC
  2392. are sorted in ascending order.
  2393. Then for each value of k from 0 to d_LPC-1, NLSF_Q15[k] is set to
  2394. <figure align="center">
  2395. <artwork align="center"><![CDATA[
  2396. max(NLSF_Q15[k], NLSF_Q15[k-1] + NDeltaMin_Q15[k]) .
  2397. ]]></artwork>
  2398. </figure>
  2399. Next, for each value of k from d_LPC-1 down to 0, NLSF_Q15[k] is set to
  2400. <figure align="center">
  2401. <artwork align="center"><![CDATA[
  2402. min(NLSF_Q15[k], NLSF_Q15[k+1] - NDeltaMin_Q15[k+1]) .
  2403. ]]></artwork>
  2404. </figure>
  2405. </t>
  2406. </section>
  2407. <section anchor="silk_nlsf_interpolation" title="Normalized LSF Interpolation">
  2408. <t>
  2409. For 20&nbsp;ms SILK frames, the first half of the frame (i.e., the first two
  2410. subframes) may use normalized LSF coefficients that are interpolated between
  2411. the decoded LSFs for the most recent coded frame (in the same channel) and the
  2412. current frame.
  2413. A Q2 interpolation factor follows the LSF coefficient indices in the bitstream,
  2414. which is decoded using the PDF in <xref target="silk_nlsf_interp_pdf"/>.
  2415. This happens in silk_decode_indices() (silk_decode_indices.c).
  2416. For the first frame after a decoder reset, when no prior LSF coefficients are
  2417. available, the decoder still decodes this factor, but ignores its value and
  2418. always uses 4 instead.
  2419. For 10&nbsp;ms SILK frames, this factor is not stored at all.
  2420. </t>
  2421. <texttable anchor="silk_nlsf_interp_pdf"
  2422. title="PDF for Normalized LSF Interpolation Index">
  2423. <ttcol>PDF</ttcol>
  2424. <c>{13, 22, 29, 11, 181}/256</c>
  2425. </texttable>
  2426. <t>
  2427. Let n2_Q15[k] be the normalized LSF coefficients decoded by the procedure in
  2428. <xref target="silk_nlsfs"/>, n0_Q15[k] be the LSF coefficients
  2429. decoded for the prior frame, and w_Q2 be the interpolation factor.
  2430. Then the normalized LSF coefficients used for the first half of a 20&nbsp;ms
  2431. frame, n1_Q15[k], are
  2432. <figure align="center">
  2433. <artwork align="center"><![CDATA[
  2434. n1_Q15[k] = n0_Q15[k] + (w_Q2*(n2_Q15[k] - n0_Q15[k]) >> 2) .
  2435. ]]></artwork>
  2436. </figure>
  2437. This interpolation is performed in silk_decode_parameters()
  2438. (silk_decode_parameters.c).
  2439. </t>
  2440. </section>
  2441. <section anchor="silk_nlsf2lpc"
  2442. title="Converting Normalized LSF Coefficients to LPCs">
  2443. <t>
  2444. Any LPC filter A(z) can be split into a symmetric part P(z) and an
  2445. anti-symmetric part Q(z) such that
  2446. <figure align="center">
  2447. <artwork align="center"><![CDATA[
  2448. d_LPC
  2449. __ -k 1
  2450. A(z) = 1 - \ a[k] * z = - * (P(z) + Q(z))
  2451. /_ 2
  2452. k=1
  2453. ]]></artwork>
  2454. </figure>
  2455. with
  2456. <figure align="center">
  2457. <artwork align="center"><![CDATA[
  2458. -d_LPC-1 -1
  2459. P(z) = A(z) + z * A(z )
  2460. -d_LPC-1 -1
  2461. Q(z) = A(z) - z * A(z ) .
  2462. ]]></artwork>
  2463. </figure>
  2464. The even normalized LSF coefficients correspond to a pair of conjugate roots of
  2465. P(z), while the odd coefficients correspond to a pair of conjugate roots of
  2466. Q(z), all of which lie on the unit circle.
  2467. In addition, P(z) has a root at pi and Q(z) has a root at 0.
  2468. Thus, they may be reconstructed mathematically from a set of normalized LSF
  2469. coefficients, n[k], as
  2470. <figure align="center">
  2471. <artwork align="center"><![CDATA[
  2472. d_LPC/2-1
  2473. -1 ___ -1 -2
  2474. P(z) = (1 + z ) * | | (1 - 2*cos(pi*n[2*k])*z + z )
  2475. k=0
  2476. d_LPC/2-1
  2477. -1 ___ -1 -2
  2478. Q(z) = (1 - z ) * | | (1 - 2*cos(pi*n[2*k+1])*z + z )
  2479. k=0
  2480. ]]></artwork>
  2481. </figure>
  2482. </t>
  2483. <t>
  2484. However, SILK performs this reconstruction using a fixed-point approximation so
  2485. that all decoders can reproduce it in a bit-exact manner to avoid prediction
  2486. drift.
  2487. The function silk_NLSF2A() (silk_NLSF2A.c) implements this procedure.
  2488. </t>
  2489. <t>
  2490. To start, it approximates cos(pi*n[k]) using a table lookup with linear
  2491. interpolation.
  2492. The encoder SHOULD use the inverse of this piecewise linear approximation,
  2493. rather than the true inverse of the cosine function, when deriving the
  2494. normalized LSF coefficients.
  2495. </t>
  2496. <t>
  2497. The top 7 bits of each normalized LSF coefficient index a value in the table,
  2498. and the next 8 bits interpolate between it and the next value.
  2499. Let i&nbsp;=&nbsp;n[k]&gt;&gt;8 be the integer index and
  2500. f&nbsp;=&nbsp;n[k]&amp;255 be the fractional part of a given coefficient.
  2501. Then the approximated cosine, c_Q17[k], is
  2502. <figure align="center">
  2503. <artwork align="center"><![CDATA[
  2504. c_Q17[k] = (cos_Q13[i]*256 + (cos_Q13[i+1]-cos_Q13[i])*f + 8) >> 4 ,
  2505. ]]></artwork>
  2506. </figure>
  2507. where cos_Q13[i] is the corresponding entry of
  2508. <xref target="silk_cos_table"/>.
  2509. </t>
  2510. <texttable anchor="silk_cos_table"
  2511. title="Q13 Cosine Table for LSF Conversion">
  2512. <ttcol align="right"></ttcol>
  2513. <ttcol align="right">0</ttcol>
  2514. <ttcol align="right">1</ttcol>
  2515. <ttcol align="right">2</ttcol>
  2516. <ttcol align="right">3</ttcol>
  2517. <c>0</c>
  2518. <c>8192</c> <c>8190</c> <c>8182</c> <c>8170</c>
  2519. <c>4</c>
  2520. <c>8152</c> <c>8130</c> <c>8104</c> <c>8072</c>
  2521. <c>8</c>
  2522. <c>8034</c> <c>7994</c> <c>7946</c> <c>7896</c>
  2523. <c>12</c>
  2524. <c>7840</c> <c>7778</c> <c>7714</c> <c>7644</c>
  2525. <c>16</c>
  2526. <c>7568</c> <c>7490</c> <c>7406</c> <c>7318</c>
  2527. <c>20</c>
  2528. <c>7226</c> <c>7128</c> <c>7026</c> <c>6922</c>
  2529. <c>24</c>
  2530. <c>6812</c> <c>6698</c> <c>6580</c> <c>6458</c>
  2531. <c>28</c>
  2532. <c>6332</c> <c>6204</c> <c>6070</c> <c>5934</c>
  2533. <c>32</c>
  2534. <c>5792</c> <c>5648</c> <c>5502</c> <c>5352</c>
  2535. <c>36</c>
  2536. <c>5198</c> <c>5040</c> <c>4880</c> <c>4718</c>
  2537. <c>40</c>
  2538. <c>4552</c> <c>4382</c> <c>4212</c> <c>4038</c>
  2539. <c>44</c>
  2540. <c>3862</c> <c>3684</c> <c>3502</c> <c>3320</c>
  2541. <c>48</c>
  2542. <c>3136</c> <c>2948</c> <c>2760</c> <c>2570</c>
  2543. <c>52</c>
  2544. <c>2378</c> <c>2186</c> <c>1990</c> <c>1794</c>
  2545. <c>56</c>
  2546. <c>1598</c> <c>1400</c> <c>1202</c> <c>1002</c>
  2547. <c>60</c>
  2548. <c>802</c> <c>602</c> <c>402</c> <c>202</c>
  2549. <c>64</c>
  2550. <c>0</c> <c>-202</c> <c>-402</c> <c>-602</c>
  2551. <c>68</c>
  2552. <c>-802</c><c>-1002</c><c>-1202</c><c>-1400</c>
  2553. <c>72</c>
  2554. <c>-1598</c><c>-1794</c><c>-1990</c><c>-2186</c>
  2555. <c>76</c>
  2556. <c>-2378</c><c>-2570</c><c>-2760</c><c>-2948</c>
  2557. <c>80</c>
  2558. <c>-3136</c><c>-3320</c><c>-3502</c><c>-3684</c>
  2559. <c>84</c>
  2560. <c>-3862</c><c>-4038</c><c>-4212</c><c>-4382</c>
  2561. <c>88</c>
  2562. <c>-4552</c><c>-4718</c><c>-4880</c><c>-5040</c>
  2563. <c>92</c>
  2564. <c>-5198</c><c>-5352</c><c>-5502</c><c>-5648</c>
  2565. <c>96</c>
  2566. <c>-5792</c><c>-5934</c><c>-6070</c><c>-6204</c>
  2567. <c>100</c>
  2568. <c>-6332</c><c>-6458</c><c>-6580</c><c>-6698</c>
  2569. <c>104</c>
  2570. <c>-6812</c><c>-6922</c><c>-7026</c><c>-7128</c>
  2571. <c>108</c>
  2572. <c>-7226</c><c>-7318</c><c>-7406</c><c>-7490</c>
  2573. <c>112</c>
  2574. <c>-7568</c><c>-7644</c><c>-7714</c><c>-7778</c>
  2575. <c>116</c>
  2576. <c>-7840</c><c>-7896</c><c>-7946</c><c>-7994</c>
  2577. <c>120</c>
  2578. <c>-8034</c><c>-8072</c><c>-8104</c><c>-8130</c>
  2579. <c>124</c>
  2580. <c>-8152</c><c>-8170</c><c>-8182</c><c>-8190</c>
  2581. <c>128</c>
  2582. <c>-8192</c> <c/> <c/> <c/>
  2583. </texttable>
  2584. <t>
  2585. Given the list of cosine values, silk_NLSF2A_find_poly() (silk_NLSF2A.c)
  2586. computes the coefficients of P and Q, described here via a simple recurrence.
  2587. Let p_Q16[k][j] and q_Q16[k][j] be the coefficients of the products of the
  2588. first (k+1) root pairs for P and Q, with j indexing the coefficient number.
  2589. Only the first (k+2) coefficients are needed, as the products are symmetric.
  2590. Let p_Q16[0][0]&nbsp;=&nbsp;q_Q16[0][0]&nbsp;=&nbsp;1&lt;&lt;16,
  2591. p_Q16[0][1]&nbsp;=&nbsp;-c_Q17[0], q_Q16[0][1]&nbsp;=&nbsp;-c_Q17[1], and
  2592. d2&nbsp;=&nbsp;d_LPC/2.
  2593. As boundary conditions, assume
  2594. p_Q16[k][j]&nbsp;=&nbsp;q_Q16[k][j]&nbsp;=&nbsp;0 for all
  2595. j&nbsp;&lt;&nbsp;0.
  2596. Also, assume p_Q16[k][k+2]&nbsp;=&nbsp;p_Q16[k][k] and
  2597. q_Q16[k][k+2]&nbsp;=&nbsp;q_Q16[k][k] (because of the symmetry).
  2598. Then, for 0&nbsp;&lt;k&nbsp;&lt;&nbsp;d2 and 0&nbsp;&lt;=&nbsp;j&nbsp;&lt;=&nbsp;k+1,
  2599. <figure align="center">
  2600. <artwork align="center"><![CDATA[
  2601. p_Q16[k][j] = p_Q16[k-1][j] + p_Q16[k-1][j-2]
  2602. - ((c_Q17[2*k]*p_Q16[k-1][j-1] + 32768)>>16) ,
  2603. q_Q16[k][j] = q_Q16[k-1][j] + q_Q16[k-1][j-2]
  2604. - ((c_Q17[2*k+1]*q_Q16[k-1][j-1] + 32768)>>16) .
  2605. ]]></artwork>
  2606. </figure>
  2607. The use of Q17 values for the cosine terms in an otherwise Q16 expression
  2608. implicitly scales them by a factor of 2.
  2609. The multiplications in this recurrence may require up to 48 bits of precision
  2610. in the result to avoid overflow.
  2611. In practice, each row of the recurrence only depends on the previous row, so an
  2612. implementation does not need to store all of them.
  2613. </t>
  2614. <t>
  2615. silk_NLSF2A() uses the values from the last row of this recurrence to
  2616. reconstruct a 32-bit version of the LPC filter (without the leading 1.0
  2617. coefficient), a32_Q17[k], 0&nbsp;&lt;=&nbsp;k&nbsp;&lt;&nbsp;d2:
  2618. <figure align="center">
  2619. <artwork align="center"><![CDATA[
  2620. a32_Q17[k] = -(q_Q16[d2-1][k+1] - q_Q16[d2-1][k])
  2621. - (p_Q16[d2-1][k+1] + p_Q16[d2-1][k])) ,
  2622. a32_Q17[d_LPC-k-1] = (q_Q16[d2-1][k+1] - q_Q16[d2-1][k])
  2623. - (p_Q16[d2-1][k+1] + p_Q16[d2-1][k])) .
  2624. ]]></artwork>
  2625. </figure>
  2626. The sum and difference of two terms from each of the p_Q16 and q_Q16
  2627. coefficient lists reflect the (1&nbsp;+&nbsp;z**-1) and
  2628. (1&nbsp;-&nbsp;z**-1) factors of P and Q, respectively.
  2629. The promotion of the expression from Q16 to Q17 implicitly scales the result
  2630. by 1/2.
  2631. </t>
  2632. </section>
  2633. <section anchor="silk_lpc_range"
  2634. title="Limiting the Range of the LPC Coefficients">
  2635. <t>
  2636. The a32_Q17[] coefficients are too large to fit in a 16-bit value, which
  2637. significantly increases the cost of applying this filter in fixed-point
  2638. decoders.
  2639. Reducing them to Q12 precision doesn't incur any significant quality loss,
  2640. but still does not guarantee they will fit.
  2641. silk_NLSF2A() applies up to 10 rounds of bandwidth expansion to limit
  2642. the dynamic range of these coefficients.
  2643. Even floating-point decoders SHOULD perform these steps, to avoid mismatch.
  2644. </t>
  2645. <t>
  2646. For each round, the process first finds the index k such that abs(a32_Q17[k])
  2647. is largest, breaking ties by choosing the lowest value of k.
  2648. Then, it computes the corresponding Q12 precision value, maxabs_Q12, subject to
  2649. an upper bound to avoid overflow in subsequent computations:
  2650. <figure align="center">
  2651. <artwork align="center"><![CDATA[
  2652. maxabs_Q12 = min((maxabs_Q17 + 16) >> 5, 163838) .
  2653. ]]></artwork>
  2654. </figure>
  2655. If this is larger than 32767, the procedure derives the chirp factor,
  2656. sc_Q16[0], to use in the bandwidth expansion as
  2657. <figure align="center">
  2658. <artwork align="center"><![CDATA[
  2659. (maxabs_Q12 - 32767) << 14
  2660. sc_Q16[0] = 65470 - -------------------------- ,
  2661. (maxabs_Q12 * (k+1)) >> 2
  2662. ]]></artwork>
  2663. </figure>
  2664. where the division here is exact integer division.
  2665. This is an approximation of the chirp factor needed to reduce the target
  2666. coefficient to 32767, though it is both less than 0.999 and, for
  2667. k&nbsp;&gt;&nbsp;0 when maxabs_Q12 is much greater than 32767, still slightly
  2668. too large.
  2669. </t>
  2670. <t>
  2671. silk_bwexpander_32() (silk_bwexpander_32.c) performs the bandwidth expansion
  2672. (again, only when maxabs_Q12 is greater than 32767) using the following
  2673. recurrence:
  2674. <figure align="center">
  2675. <artwork align="center"><![CDATA[
  2676. a32_Q17[k] = (a32_Q17[k]*sc_Q16[k]) >> 16
  2677. sc_Q16[k+1] = (sc_Q16[0]*sc_Q16[k] + 32768) >> 16
  2678. ]]></artwork>
  2679. </figure>
  2680. The first multiply may require up to 48 bits of precision in the result to
  2681. avoid overflow.
  2682. The second multiply must be unsigned to avoid overflow with only 32 bits of
  2683. precision.
  2684. The reference implementation uses a slightly more complex formulation that
  2685. avoids the 32-bit overflow using signed multiplication, but is otherwise
  2686. equivalent.
  2687. </t>
  2688. <t>
  2689. After 10 rounds of bandwidth expansion are performed, they are simply saturated
  2690. to 16 bits:
  2691. <figure align="center">
  2692. <artwork align="center"><![CDATA[
  2693. a32_Q17[k] = clamp(-32768, (a32_Q17[k]+16) >> 5, 32767) << 5 .
  2694. ]]></artwork>
  2695. </figure>
  2696. Because this performs the actual saturation in the Q12 domain, but converts the
  2697. coefficients back to the Q17 domain for the purposes of prediction gain
  2698. limiting, this step must be performed after the 10th round of bandwidth
  2699. expansion, regardless of whether or not the Q12 version of any coefficient
  2700. still overflows a 16-bit integer.
  2701. This saturation is not performed if maxabs_Q12 drops to 32767 or less prior to
  2702. the 10th round.
  2703. </t>
  2704. </section>
  2705. <section title="Limiting the Prediction Gain of the LPC Filter">
  2706. <t>
  2707. Even if the Q12 coefficients would fit, the resulting filter may still have a
  2708. significant gain (especially for voiced sounds), making the filter unstable.
  2709. silk_NLSF2A() applies up to 18 additional rounds of bandwidth expansion to
  2710. limit the prediction gain.
  2711. Instead of controlling the amount of bandwidth expansion using the prediction
  2712. gain itself (which may diverge to infinity for an unstable filter),
  2713. silk_NLSF2A() uses LPC_inverse_pred_gain_QA() (silk_LPC_inv_pred_gain.c)
  2714. to compute the reflection coefficients associated with the filter.
  2715. The filter is stable if and only if the magnitude of these coefficients is
  2716. sufficiently less than one.
  2717. The reflection coefficients, rc[k], can be computed using a simple Levinson
  2718. recurrence, initialized with the LPC coefficients
  2719. a[d_LPC-1][n]&nbsp;=&nbsp;a[n], and then updated via
  2720. <figure align="center">
  2721. <artwork align="center"><![CDATA[
  2722. rc[k] = -a[k][k] ,
  2723. a[k][n] - a[k][k-n-1]*rc[k]
  2724. a[k-1][n] = --------------------------- .
  2725. 2
  2726. 1 - rc[k]
  2727. ]]></artwork>
  2728. </figure>
  2729. </t>
  2730. <t>
  2731. However, LPC_inverse_pred_gain_QA() approximates this using fixed-point
  2732. arithmetic to guarantee reproducible results across platforms and
  2733. implementations.
  2734. It is important to run on the real Q12 coefficients that will be used during
  2735. reconstruction, because small changes in the coefficients can make a stable
  2736. filter unstable, but increasing the precision back to Q16 allows more accurate
  2737. computation of the reflection coefficients.
  2738. Thus, let
  2739. <figure align="center">
  2740. <artwork align="center"><![CDATA[
  2741. a32_Q16[d_LPC-1][n] = ((a32_Q17[n] + 16) >> 5) << 4
  2742. ]]></artwork>
  2743. </figure>
  2744. be the Q16 representation of the Q12 version of the LPC coefficients that will
  2745. eventually be used.
  2746. Then for each k from d_LPC-1 down to 0, if
  2747. abs(a32_Q16[k][k])&nbsp;&gt;&nbsp;65520, the filter is unstable and the
  2748. recurrence stops.
  2749. Otherwise, the row k-1 of a32_Q16 is computed from row k as
  2750. <figure align="center">
  2751. <artwork align="center"><![CDATA[
  2752. rc_Q31[k] = -a32_Q16[k][k] << 15 ,
  2753. div_Q30[k] = (1<<30) - 1 - (rc_Q31[k]*rc_Q31[k] >> 32) ,
  2754. b1[k] = ilog(div_Q30[k]) - 16 ,
  2755. (1<<29) - 1
  2756. inv_Qb1[k] = ----------------------- ,
  2757. div_Q30[k] >> (b1[k]+1)
  2758. err_Q29[k] = (1<<29)
  2759. - ((div_Q30[k]<<(15-b1[k]))*inv_Qb1[k] >> 16) ,
  2760. mul_Q16[k] = ((inv_Qb1[k] << 16)
  2761. + (err_Q29[k]*inv_Qb1[k] >> 13)) >> b1[k] ,
  2762. b2[k] = ilog(mul_Q16[k]) - 15 ,
  2763. t_Q16[k-1][n] = a32_Q16[k][n]
  2764. - ((a32_Q16[k][k-n-1]*rc_Q31[k] >> 32) << 1) ,
  2765. a32_Q16[k-1][n] = ((t_Q16[k-1][n] *
  2766. (mul_Q16[k] << (16-b2[k]))) >> 32) << b2[k] .
  2767. ]]></artwork>
  2768. </figure>
  2769. Here, rc_Q30[k] are the reflection coefficients.
  2770. div_Q30[k] is the denominator for each iteration, and mul_Q16[k] is its
  2771. multiplicative inverse.
  2772. inv_Qb1[k], which ranges from 16384 to 32767, is a low-precision version of
  2773. that inverse (with b1[k] fractional bits, where b1[k] ranges from 3 to 14).
  2774. err_Q29[k] is the residual error, ranging from -32392 to 32763, which is used
  2775. to improve the accuracy.
  2776. t_Q16[k-1][n], 0&nbsp;&lt;=&nbsp;n&nbsp;&lt;&nbsp;k, are the numerators for the
  2777. next row of coefficients in the recursion, and a32_Q16[k-1][n] is the final
  2778. version of that row.
  2779. Every multiply in this procedure except the one used to compute mul_Q16[k]
  2780. requires more than 32 bits of precision, but otherwise all intermediate
  2781. results fit in 32 bits or less.
  2782. In practice, because each row only depends on the next one, an implementation
  2783. does not need to store them all.
  2784. If abs(a32_Q16[k][k])&nbsp;&lt;=&nbsp;65520 for
  2785. 0&nbsp;&lt;=&nbsp;k&nbsp;&lt;&nbsp;d_LPC, then the filter is considered stable.
  2786. </t>
  2787. <t>
  2788. On round i, 1&nbsp;&lt;=&nbsp;i&nbsp;&lt;=&nbsp;18, if the filter passes this
  2789. stability check, then this procedure stops, and the final LPC coefficients to
  2790. use for reconstruction<!--TODO: In section...--> are
  2791. <figure align="center">
  2792. <artwork align="center"><![CDATA[
  2793. a_Q12[k] = (a32_Q17[k] + 16) >> 5 .
  2794. ]]></artwork>
  2795. </figure>
  2796. Otherwise, a round of bandwidth expansion is applied using the same procedure
  2797. as in <xref target="silk_lpc_range"/>, with
  2798. <figure align="center">
  2799. <artwork align="center"><![CDATA[
  2800. sc_Q16[0] = 65536 - i*(i+9) .
  2801. ]]></artwork>
  2802. </figure>
  2803. If, after the 18th round, the filter still fails the stability check, then
  2804. a_Q12[k] is set to 0 for all k.
  2805. </t>
  2806. </section>
  2807. </section>
  2808. <section anchor="silk_ltp_params"
  2809. title="Long-Term Prediction (LTP) Parameters">
  2810. <t>
  2811. After the normalized LSF indices and, for 20&nbsp;ms frames, the LSF
  2812. interpolation index, voiced frames (see <xref target="silk_frame_type"/>)
  2813. include additional Long-Term Prediction (LTP) parameters.
  2814. There is one primary lag index for each SILK frame, but this is refined to
  2815. produce a separate lag index per subframe using a vector quantizer.
  2816. Each subframe also gets its own prediction gain coefficient.
  2817. </t>
  2818. <section anchor="silk_ltp_lags" title="Pitch Lags">
  2819. <t>
  2820. The primary lag index is coded either relative to the primary lag of the prior
  2821. frame or as an absolute index.
  2822. Like the quantization gains, the first LBRR frame, an LBRR frame where the
  2823. previous LBRR frame was not coded, and the first regular SILK frame in each
  2824. channel of an Opus frame all code the pitch lag as an absolute index.
  2825. When the most recent coded frame in the current channel was not voiced, this
  2826. also forces absolute coding.
  2827. In particular, unlike an LBRR frame where the previous frame is not coded, in a
  2828. 60&nbsp;ms stereo Opus frame, if the first and third regular SILK frames
  2829. in the side channel are coded, voiced frames, but the second is not coded, the
  2830. third still uses relative coding.
  2831. </t>
  2832. <t>
  2833. With absolute coding, the primary pitch lag may range from 2&nbsp;ms
  2834. (inclusive) up to 18&nbsp;ms (exclusive), corresponding to pitches from
  2835. 500&nbsp;Hz down to 55.6&nbsp;Hz, respectively.
  2836. It is comprised of a high part and a low part, where the decoder reads the high
  2837. part using the 32-entry codebook in <xref target="silk_abs_pitch_high_pdf"/>
  2838. and the low part using the codebook corresponding to the current audio
  2839. bandwidth from <xref target="silk_abs_pitch_low_pdf"/>.
  2840. The final primary pitch lag is then
  2841. <figure align="center">
  2842. <artwork align="center"><![CDATA[
  2843. lag = lag_high*lag_scale + lag_low + lag_min
  2844. ]]></artwork>
  2845. </figure>
  2846. where lag_high is the high part, lag_low is the low part, and lag_scale
  2847. and lag_min are the values from the "Scale" and "Minimum Lag" columns of
  2848. <xref target="silk_abs_pitch_low_pdf"/>, respectively.
  2849. </t>
  2850. <texttable anchor="silk_abs_pitch_high_pdf"
  2851. title="PDF for High Part of Primary Pitch Lag">
  2852. <ttcol align="left">PDF</ttcol>
  2853. <c>{3, 3, 6, 11, 21, 30, 32, 19,
  2854. 11, 10, 12, 13, 13, 12, 11, 9,
  2855. 8, 7, 6, 4, 2, 2, 2, 1,
  2856. 1, 1, 1, 1, 1, 1, 1, 1}/256</c>
  2857. </texttable>
  2858. <texttable anchor="silk_abs_pitch_low_pdf"
  2859. title="PDF for Low Part of Primary Pitch Lag">
  2860. <ttcol>Audio Bandwidth</ttcol>
  2861. <ttcol>PDF</ttcol>
  2862. <ttcol>Scale</ttcol>
  2863. <ttcol>Minimum Lag</ttcol>
  2864. <ttcol>Maximum Lag</ttcol>
  2865. <c>NB</c> <c>{64, 64, 64, 64}/256</c> <c>4</c> <c>16</c> <c>144</c>
  2866. <c>MB</c> <c>{43, 42, 43, 43, 42, 43}/256</c> <c>6</c> <c>24</c> <c>216</c>
  2867. <c>WB</c> <c>{32, 32, 32, 32, 32, 32, 32, 32}/256</c> <c>8</c> <c>32</c> <c>288</c>
  2868. </texttable>
  2869. <t>
  2870. All frames that do not use absolute coding for the primary lag index use
  2871. relative coding instead.
  2872. The decoder reads a single delta value using the 21-entry PDF in
  2873. <xref target="silk_rel_pitch_pdf"/>.
  2874. If the resulting value is zero, it falls back to the absolute coding procedure
  2875. from the prior paragraph.
  2876. Otherwise, the final primary pitch lag is then
  2877. <figure align="center">
  2878. <artwork align="center"><![CDATA[
  2879. lag = lag_prev + (delta_lag_index - 9)
  2880. ]]></artwork>
  2881. </figure>
  2882. where lag_prev is the primary pitch lag from the most recent frame in the same
  2883. channel and delta_lag_index is the value just decoded.
  2884. This allows a per-frame change in the pitch lag of -8 to +11 samples.
  2885. The decoder does no clamping at this point, so this value can fall outside the
  2886. range of 2&nbsp;ms to 18&nbsp;ms, and the decoder must use this unclamped
  2887. value when using relative coding in the next SILK frame (if any).
  2888. However, because an Opus frame can use relative coding for at most two
  2889. consecutive SILK frames, integer overflow should not be an issue.
  2890. </t>
  2891. <texttable anchor="silk_rel_pitch_pdf"
  2892. title="PDF for Primary Pitch Lag Change">
  2893. <ttcol align="left">PDF</ttcol>
  2894. <c>{46, 2, 2, 3, 4, 6, 10, 15,
  2895. 26, 38, 30, 22, 15, 10, 7, 6,
  2896. 4, 4, 2, 2, 2}/256</c>
  2897. </texttable>
  2898. <t>
  2899. After the primary pitch lag, a "pitch contour", stored as a single entry from
  2900. one of four small VQ codebooks, gives lag offsets for each subframe in the
  2901. current SILK frame.
  2902. The codebook index is decoded using one of the PDFs in
  2903. <xref target="silk_pitch_contour_pdfs"/> depending on the current frame size
  2904. and audio bandwidth.
  2905. <xref target="silk_pitch_contour_cb_nb10ms"/> through
  2906. <xref target="silk_pitch_contour_cb_mbwb20ms"/> give the corresponding offsets
  2907. to apply to the primary pitch lag for each subframe given the decoded codebook
  2908. index.
  2909. </t>
  2910. <texttable anchor="silk_pitch_contour_pdfs"
  2911. title="PDFs for Subframe Pitch Contour">
  2912. <ttcol>Audio Bandwidth</ttcol>
  2913. <ttcol>SILK Frame Size</ttcol>
  2914. <ttcol align="right">Codebook Size</ttcol>
  2915. <ttcol>PDF</ttcol>
  2916. <c>NB</c> <c>10&nbsp;ms</c> <c>3</c>
  2917. <c>{143, 50, 63}/256</c>
  2918. <c>NB</c> <c>20&nbsp;ms</c> <c>11</c>
  2919. <c>{68, 12, 21, 17, 19, 22, 30, 24,
  2920. 17, 16, 10}/256</c>
  2921. <c>MB or WB</c> <c>10&nbsp;ms</c> <c>12</c>
  2922. <c>{91, 46, 39, 19, 14, 12, 8, 7,
  2923. 6, 5, 5, 4}/256</c>
  2924. <c>MB or WB</c> <c>20&nbsp;ms</c> <c>34</c>
  2925. <c>{33, 22, 18, 16, 15, 14, 14, 13,
  2926. 13, 10, 9, 9, 8, 6, 6, 6,
  2927. 5, 4, 4, 4, 3, 3, 3, 2,
  2928. 2, 2, 2, 2, 2, 2, 1, 1,
  2929. 1, 1}/256</c>
  2930. </texttable>
  2931. <texttable anchor="silk_pitch_contour_cb_nb10ms"
  2932. title="Codebook Vectors for Subframe Pitch Contour: NB, 10&nbsp;ms Frames">
  2933. <ttcol>Index</ttcol>
  2934. <ttcol align="right">Subframe Offsets</ttcol>
  2935. <c>0</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2936. <c>1</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0</spanx></c>
  2937. <c>2</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;1</spanx></c>
  2938. </texttable>
  2939. <texttable anchor="silk_pitch_contour_cb_nb20ms"
  2940. title="Codebook Vectors for Subframe Pitch Contour: NB, 20&nbsp;ms Frames">
  2941. <ttcol>Index</ttcol>
  2942. <ttcol align="right">Subframe Offsets</ttcol>
  2943. <c>0</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2944. <c>1</c> <c><spanx style="vbare">&nbsp;2&nbsp;&nbsp;1&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  2945. <c>2</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;2</spanx></c>
  2946. <c>3</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1</spanx></c>
  2947. <c>4</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2948. <c>5</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1</spanx></c>
  2949. <c>6</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;1</spanx></c>
  2950. <c>7</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2951. <c>8</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2952. <c>9</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  2953. <c>10</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  2954. </texttable>
  2955. <texttable anchor="silk_pitch_contour_cb_mbwb10ms"
  2956. title="Codebook Vectors for Subframe Pitch Contour: MB or WB, 10&nbsp;ms Frames">
  2957. <ttcol>Index</ttcol>
  2958. <ttcol align="right">Subframe Offsets</ttcol>
  2959. <c>0</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2960. <c>1</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;1</spanx></c>
  2961. <c>2</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0</spanx></c>
  2962. <c>3</c> <c><spanx style="vbare">-1&nbsp;&nbsp;1</spanx></c>
  2963. <c>4</c> <c><spanx style="vbare">&nbsp;1&nbsp;-1</spanx></c>
  2964. <c>5</c> <c><spanx style="vbare">-1&nbsp;&nbsp;2</spanx></c>
  2965. <c>6</c> <c><spanx style="vbare">&nbsp;2&nbsp;-1</spanx></c>
  2966. <c>7</c> <c><spanx style="vbare">-2&nbsp;&nbsp;2</spanx></c>
  2967. <c>8</c> <c><spanx style="vbare">&nbsp;2&nbsp;-2</spanx></c>
  2968. <c>9</c> <c><spanx style="vbare">-2&nbsp;&nbsp;3</spanx></c>
  2969. <c>10</c> <c><spanx style="vbare">&nbsp;3&nbsp;-2</spanx></c>
  2970. <c>11</c> <c><spanx style="vbare">-3&nbsp;&nbsp;3</spanx></c>
  2971. </texttable>
  2972. <texttable anchor="silk_pitch_contour_cb_mbwb20ms"
  2973. title="Codebook Vectors for Subframe Pitch Contour: MB or WB, 20&nbsp;ms Frames">
  2974. <ttcol>Index</ttcol>
  2975. <ttcol align="right">Subframe Offsets</ttcol>
  2976. <c>0</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2977. <c>1</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;1</spanx></c>
  2978. <c>2</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2979. <c>3</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2980. <c>4</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1</spanx></c>
  2981. <c>5</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0</spanx></c>
  2982. <c>6</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;1</spanx></c>
  2983. <c>7</c> <c><spanx style="vbare">&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  2984. <c>8</c> <c><spanx style="vbare">-1&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;2</spanx></c>
  2985. <c>9</c> <c><spanx style="vbare">&nbsp;1&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  2986. <c>10</c> <c><spanx style="vbare">-2&nbsp;-1&nbsp;&nbsp;1&nbsp;&nbsp;2</spanx></c>
  2987. <c>11</c> <c><spanx style="vbare">&nbsp;2&nbsp;&nbsp;1&nbsp;&nbsp;0&nbsp;-1</spanx></c>
  2988. <c>12</c> <c><spanx style="vbare">-2&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;&nbsp;2</spanx></c>
  2989. <c>13</c> <c><spanx style="vbare">-2&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;&nbsp;3</spanx></c>
  2990. <c>14</c> <c><spanx style="vbare">&nbsp;2&nbsp;&nbsp;1&nbsp;-1&nbsp;-2</spanx></c>
  2991. <c>15</c> <c><spanx style="vbare">-3&nbsp;-1&nbsp;&nbsp;1&nbsp;&nbsp;3</spanx></c>
  2992. <c>16</c> <c><spanx style="vbare">&nbsp;2&nbsp;&nbsp;0&nbsp;&nbsp;0&nbsp;-2</spanx></c>
  2993. <c>17</c> <c><spanx style="vbare">&nbsp;3&nbsp;&nbsp;1&nbsp;&nbsp;0&nbsp;-2</spanx></c>
  2994. <c>18</c> <c><spanx style="vbare">-3&nbsp;-1&nbsp;&nbsp;2&nbsp;&nbsp;4</spanx></c>
  2995. <c>19</c> <c><spanx style="vbare">-4&nbsp;-1&nbsp;&nbsp;1&nbsp;&nbsp;4</spanx></c>
  2996. <c>20</c> <c><spanx style="vbare">&nbsp;3&nbsp;&nbsp;1&nbsp;-1&nbsp;-3</spanx></c>
  2997. <c>21</c> <c><spanx style="vbare">-4&nbsp;-1&nbsp;&nbsp;2&nbsp;&nbsp;5</spanx></c>
  2998. <c>22</c> <c><spanx style="vbare">&nbsp;4&nbsp;&nbsp;2&nbsp;-1&nbsp;-3</spanx></c>
  2999. <c>23</c> <c><spanx style="vbare">&nbsp;4&nbsp;&nbsp;1&nbsp;-1&nbsp;-4</spanx></c>
  3000. <c>24</c> <c><spanx style="vbare">-5&nbsp;-1&nbsp;&nbsp;2&nbsp;&nbsp;6</spanx></c>
  3001. <c>25</c> <c><spanx style="vbare">&nbsp;5&nbsp;&nbsp;2&nbsp;-1&nbsp;-4</spanx></c>
  3002. <c>26</c> <c><spanx style="vbare">-6&nbsp;-2&nbsp;&nbsp;2&nbsp;&nbsp;6</spanx></c>
  3003. <c>27</c> <c><spanx style="vbare">-5&nbsp;-2&nbsp;&nbsp;2&nbsp;&nbsp;5</spanx></c>
  3004. <c>28</c> <c><spanx style="vbare">&nbsp;6&nbsp;&nbsp;2&nbsp;-1&nbsp;-5</spanx></c>
  3005. <c>29</c> <c><spanx style="vbare">-7&nbsp;-2&nbsp;&nbsp;3&nbsp;&nbsp;8</spanx></c>
  3006. <c>30</c> <c><spanx style="vbare">&nbsp;6&nbsp;&nbsp;2&nbsp;-2&nbsp;-6</spanx></c>
  3007. <c>31</c> <c><spanx style="vbare">&nbsp;5&nbsp;&nbsp;2&nbsp;-2&nbsp;-5</spanx></c>
  3008. <c>32</c> <c><spanx style="vbare">&nbsp;8&nbsp;&nbsp;3&nbsp;-2&nbsp;-7</spanx></c>
  3009. <c>33</c> <c><spanx style="vbare">-9&nbsp;-3&nbsp;&nbsp;3&nbsp;&nbsp;9</spanx></c>
  3010. </texttable>
  3011. <t>
  3012. The final pitch lag for each subframe is assembled in silk_decode_pitch()
  3013. (silk_decode_pitch.c).
  3014. Let lag be the primary pitch lag for the current SILK frame, contour_index be
  3015. index of the VQ codebook, and lag_cb[contour_index][k] be the corresponding
  3016. entry of the codebook from the appropriate table given above for the k'th
  3017. subframe.
  3018. Then the final pitch lag for that subframe is
  3019. <figure align="center">
  3020. <artwork align="center"><![CDATA[
  3021. pitch_lags[k] = clamp(lag_min, lag + lag_cb[contour_index][k],
  3022. lag_max)
  3023. ]]></artwork>
  3024. </figure>
  3025. where lag_min and lag_max are the values from the "Minimum Lag" and
  3026. "Maximum Lag" columns of <xref target="silk_abs_pitch_low_pdf"/>,
  3027. respectively.
  3028. </t>
  3029. </section>
  3030. <section anchor="silk_ltp_coeffs" title="LTP Filter Coefficients">
  3031. <t>
  3032. SILK can use a separate 5-tap pitch filter for each subframe.
  3033. It selects the filter to use from one of three codebooks.
  3034. All of the subframes in a SILK frame must choose their filter from the same
  3035. codebook, itself chosen via an explicitly-coded "periodicity index".
  3036. This immediately follows the subframe pitch lags, and is coded using the
  3037. 3-entry PDF from <xref target="silk_perindex_pdf"/>.
  3038. </t>
  3039. <texttable anchor="silk_perindex_pdf" title="Periodicity Index PDF">
  3040. <ttcol>PDF</ttcol>
  3041. <c>{77, 80, 99}/256</c>
  3042. </texttable>
  3043. <t>
  3044. The index of the filter for use for each subframe follows.
  3045. They are all coded using the PDF from <xref target="silk_ltp_filter_pdfs"/>
  3046. corresponding to the periodicity index.
  3047. <xref target="silk_ltp_filter_coeffs0"/> through
  3048. <xref target="silk_ltp_filter_coeffs2"/> contain the corresponding filter taps
  3049. as signed Q7 integers.
  3050. </t>
  3051. <texttable anchor="silk_ltp_filter_pdfs" title="LTP Filter PDFs">
  3052. <ttcol>Periodicity Index</ttcol>
  3053. <ttcol align="right">Codebook Size</ttcol>
  3054. <ttcol>PDF</ttcol>
  3055. <c>0</c> <c>8</c> <c>{185, 15, 13, 13, 9, 9, 6, 6}/256</c>
  3056. <c>1</c> <c>16</c> <c>{57, 34, 21, 20, 15, 13, 12, 13,
  3057. 10, 10, 9, 10, 9, 8, 7, 8}/256</c>
  3058. <c>2</c> <c>32</c> <c>{15, 16, 14, 12, 12, 12, 11, 11,
  3059. 11, 10, 9, 9, 9, 9, 8, 8,
  3060. 8, 8, 7, 7, 6, 6, 5, 4,
  3061. 5, 4, 4, 4, 3, 4, 3, 2}/256</c>
  3062. </texttable>
  3063. <texttable anchor="silk_ltp_filter_coeffs0"
  3064. title="Codebook Vectors for LTP Filter, Periodicity Index 0">
  3065. <ttcol>Index</ttcol>
  3066. <ttcol align="right">Filter Taps (Q7)</ttcol>
  3067. <c>0</c>
  3068. <c><spanx style="vbare">&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;24&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;5</spanx></c>
  3069. <c>1</c>
  3070. <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>
  3071. <c>2</c>
  3072. <c><spanx style="vbare">&nbsp;12&nbsp;&nbsp;28&nbsp;&nbsp;41&nbsp;&nbsp;13&nbsp;&nbsp;-4</spanx></c>
  3073. <c>3</c>
  3074. <c><spanx style="vbare">&nbsp;-9&nbsp;&nbsp;15&nbsp;&nbsp;42&nbsp;&nbsp;25&nbsp;&nbsp;14</spanx></c>
  3075. <c>4</c>
  3076. <c><spanx style="vbare">&nbsp;&nbsp;1&nbsp;&nbsp;-2&nbsp;&nbsp;62&nbsp;&nbsp;41&nbsp;&nbsp;-9</spanx></c>
  3077. <c>5</c>
  3078. <c><spanx style="vbare">-10&nbsp;&nbsp;37&nbsp;&nbsp;65&nbsp;&nbsp;-4&nbsp;&nbsp;&nbsp;3</spanx></c>
  3079. <c>6</c>
  3080. <c><spanx style="vbare">&nbsp;-6&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;66&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;-8</spanx></c>
  3081. <c>7</c>
  3082. <c><spanx style="vbare">&nbsp;16&nbsp;&nbsp;14&nbsp;&nbsp;38&nbsp;&nbsp;-3&nbsp;&nbsp;33</spanx></c>
  3083. </texttable>
  3084. <texttable anchor="silk_ltp_filter_coeffs1"
  3085. title="Codebook Vectors for LTP Filter, Periodicity Index 1">
  3086. <ttcol>Index</ttcol>
  3087. <ttcol align="right">Filter Taps (Q7)</ttcol>
  3088. <c>0</c>
  3089. <c><spanx style="vbare">&nbsp;13&nbsp;&nbsp;22&nbsp;&nbsp;39&nbsp;&nbsp;23&nbsp;&nbsp;12</spanx></c>
  3090. <c>1</c>
  3091. <c><spanx style="vbare">&nbsp;-1&nbsp;&nbsp;36&nbsp;&nbsp;64&nbsp;&nbsp;27&nbsp;&nbsp;-6</spanx></c>
  3092. <c>2</c>
  3093. <c><spanx style="vbare">&nbsp;-7&nbsp;&nbsp;10&nbsp;&nbsp;55&nbsp;&nbsp;43&nbsp;&nbsp;17</spanx></c>
  3094. <c>3</c>
  3095. <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>
  3096. <c>4</c>
  3097. <c><spanx style="vbare">&nbsp;&nbsp;6&nbsp;-11&nbsp;&nbsp;74&nbsp;&nbsp;53&nbsp;&nbsp;-9</spanx></c>
  3098. <c>5</c>
  3099. <c><spanx style="vbare">-12&nbsp;&nbsp;55&nbsp;&nbsp;76&nbsp;-12&nbsp;&nbsp;&nbsp;8</spanx></c>
  3100. <c>6</c>
  3101. <c><spanx style="vbare">&nbsp;-3&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;93&nbsp;&nbsp;27&nbsp;&nbsp;-4</spanx></c>
  3102. <c>7</c>
  3103. <c><spanx style="vbare">&nbsp;26&nbsp;&nbsp;39&nbsp;&nbsp;59&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;-8</spanx></c>
  3104. <c>8</c>
  3105. <c><spanx style="vbare">&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;77&nbsp;&nbsp;11&nbsp;&nbsp;&nbsp;9</spanx></c>
  3106. <c>9</c>
  3107. <c><spanx style="vbare">&nbsp;-8&nbsp;&nbsp;22&nbsp;&nbsp;44&nbsp;&nbsp;-6&nbsp;&nbsp;&nbsp;7</spanx></c>
  3108. <c>10</c>
  3109. <c><spanx style="vbare">&nbsp;40&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;26&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;9</spanx></c>
  3110. <c>11</c>
  3111. <c><spanx style="vbare">&nbsp;-7&nbsp;&nbsp;20&nbsp;101&nbsp;&nbsp;-7&nbsp;&nbsp;&nbsp;4</spanx></c>
  3112. <c>12</c>
  3113. <c><spanx style="vbare">&nbsp;&nbsp;3&nbsp;&nbsp;-8&nbsp;&nbsp;42&nbsp;&nbsp;26&nbsp;&nbsp;&nbsp;0</spanx></c>
  3114. <c>13</c>
  3115. <c><spanx style="vbare">-15&nbsp;&nbsp;33&nbsp;&nbsp;68&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;23</spanx></c>
  3116. <c>14</c>
  3117. <c><spanx style="vbare">&nbsp;-2&nbsp;&nbsp;55&nbsp;&nbsp;46&nbsp;&nbsp;-2&nbsp;&nbsp;15</spanx></c>
  3118. <c>15</c>
  3119. <c><spanx style="vbare">&nbsp;&nbsp;3&nbsp;&nbsp;-1&nbsp;&nbsp;21&nbsp;&nbsp;16&nbsp;&nbsp;41</spanx></c>
  3120. </texttable>
  3121. <texttable anchor="silk_ltp_filter_coeffs2"
  3122. title="Codebook Vectors for LTP Filter, Periodicity Index 2">
  3123. <ttcol>Index</ttcol>
  3124. <ttcol align="right">Filter Taps (Q7)</ttcol>
  3125. <c>0</c>
  3126. <c><spanx style="vbare">&nbsp;-6&nbsp;&nbsp;27&nbsp;&nbsp;61&nbsp;&nbsp;39&nbsp;&nbsp;&nbsp;5</spanx></c>
  3127. <c>1</c>
  3128. <c><spanx style="vbare">-11&nbsp;&nbsp;42&nbsp;&nbsp;88&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;1</spanx></c>
  3129. <c>2</c>
  3130. <c><spanx style="vbare">&nbsp;-2&nbsp;&nbsp;60&nbsp;&nbsp;65&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;-4</spanx></c>
  3131. <c>3</c>
  3132. <c><spanx style="vbare">&nbsp;-1&nbsp;&nbsp;-5&nbsp;&nbsp;73&nbsp;&nbsp;56&nbsp;&nbsp;&nbsp;1</spanx></c>
  3133. <c>4</c>
  3134. <c><spanx style="vbare">&nbsp;-9&nbsp;&nbsp;19&nbsp;&nbsp;94&nbsp;&nbsp;29&nbsp;&nbsp;-9</spanx></c>
  3135. <c>5</c>
  3136. <c><spanx style="vbare">&nbsp;&nbsp;0&nbsp;&nbsp;12&nbsp;&nbsp;99&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;4</spanx></c>
  3137. <c>6</c>
  3138. <c><spanx style="vbare">&nbsp;&nbsp;8&nbsp;-19&nbsp;102&nbsp;&nbsp;46&nbsp;-13</spanx></c>
  3139. <c>7</c>
  3140. <c><spanx style="vbare">&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;13&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;2</spanx></c>
  3141. <c>8</c>
  3142. <c><spanx style="vbare">&nbsp;&nbsp;9&nbsp;-21&nbsp;&nbsp;84&nbsp;&nbsp;72&nbsp;-18</spanx></c>
  3143. <c>9</c>
  3144. <c><spanx style="vbare">-11&nbsp;&nbsp;46&nbsp;104&nbsp;-22&nbsp;&nbsp;&nbsp;8</spanx></c>
  3145. <c>10</c>
  3146. <c><spanx style="vbare">&nbsp;18&nbsp;&nbsp;38&nbsp;&nbsp;48&nbsp;&nbsp;23&nbsp;&nbsp;&nbsp;0</spanx></c>
  3147. <c>11</c>
  3148. <c><spanx style="vbare">-16&nbsp;&nbsp;70&nbsp;&nbsp;83&nbsp;-21&nbsp;&nbsp;11</spanx></c>
  3149. <c>12</c>
  3150. <c><spanx style="vbare">&nbsp;&nbsp;5&nbsp;-11&nbsp;117&nbsp;&nbsp;22&nbsp;&nbsp;-8</spanx></c>
  3151. <c>13</c>
  3152. <c><spanx style="vbare">&nbsp;-6&nbsp;&nbsp;23&nbsp;117&nbsp;-12&nbsp;&nbsp;&nbsp;3</spanx></c>
  3153. <c>14</c>
  3154. <c><spanx style="vbare">&nbsp;&nbsp;3&nbsp;&nbsp;-8&nbsp;&nbsp;95&nbsp;&nbsp;28&nbsp;&nbsp;&nbsp;4</spanx></c>
  3155. <c>15</c>
  3156. <c><spanx style="vbare">-10&nbsp;&nbsp;15&nbsp;&nbsp;77&nbsp;&nbsp;60&nbsp;-15</spanx></c>
  3157. <c>16</c>
  3158. <c><spanx style="vbare">&nbsp;-1&nbsp;&nbsp;&nbsp;4&nbsp;124&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;-4</spanx></c>
  3159. <c>17</c>
  3160. <c><spanx style="vbare">&nbsp;&nbsp;3&nbsp;&nbsp;38&nbsp;&nbsp;84&nbsp;&nbsp;24&nbsp;-25</spanx></c>
  3161. <c>18</c>
  3162. <c><spanx style="vbare">&nbsp;&nbsp;2&nbsp;&nbsp;13&nbsp;&nbsp;42&nbsp;&nbsp;13&nbsp;&nbsp;31</spanx></c>
  3163. <c>19</c>
  3164. <c><spanx style="vbare">&nbsp;21&nbsp;&nbsp;-4&nbsp;&nbsp;56&nbsp;&nbsp;46&nbsp;&nbsp;-1</spanx></c>
  3165. <c>20</c>
  3166. <c><spanx style="vbare">&nbsp;-1&nbsp;&nbsp;35&nbsp;&nbsp;79&nbsp;-13&nbsp;&nbsp;19</spanx></c>
  3167. <c>21</c>
  3168. <c><spanx style="vbare">&nbsp;-7&nbsp;&nbsp;65&nbsp;&nbsp;88&nbsp;&nbsp;-9&nbsp;-14</spanx></c>
  3169. <c>22</c>
  3170. <c><spanx style="vbare">&nbsp;20&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;81&nbsp;&nbsp;49&nbsp;-29</spanx></c>
  3171. <c>23</c>
  3172. <c><spanx style="vbare">&nbsp;20&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;75&nbsp;&nbsp;&nbsp;3&nbsp;-17</spanx></c>
  3173. <c>24</c>
  3174. <c><spanx style="vbare">&nbsp;&nbsp;5&nbsp;&nbsp;-9&nbsp;&nbsp;44&nbsp;&nbsp;92&nbsp;&nbsp;-8</spanx></c>
  3175. <c>25</c>
  3176. <c><spanx style="vbare">&nbsp;&nbsp;1&nbsp;&nbsp;-3&nbsp;&nbsp;22&nbsp;&nbsp;69&nbsp;&nbsp;31</spanx></c>
  3177. <c>26</c>
  3178. <c><spanx style="vbare">&nbsp;-6&nbsp;&nbsp;95&nbsp;&nbsp;41&nbsp;-12&nbsp;&nbsp;&nbsp;5</spanx></c>
  3179. <c>27</c>
  3180. <c><spanx style="vbare">&nbsp;39&nbsp;&nbsp;67&nbsp;&nbsp;16&nbsp;&nbsp;-4&nbsp;&nbsp;&nbsp;1</spanx></c>
  3181. <c>28</c>
  3182. <c><spanx style="vbare">&nbsp;&nbsp;0&nbsp;&nbsp;-6&nbsp;120&nbsp;&nbsp;55&nbsp;-36</spanx></c>
  3183. <c>29</c>
  3184. <c><spanx style="vbare">-13&nbsp;&nbsp;44&nbsp;122&nbsp;&nbsp;&nbsp;4&nbsp;-24</spanx></c>
  3185. <c>30</c>
  3186. <c><spanx style="vbare">&nbsp;81&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;11&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;7</spanx></c>
  3187. <c>31</c>
  3188. <c><spanx style="vbare">&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;10&nbsp;&nbsp;88</spanx></c>
  3189. </texttable>
  3190. </section>
  3191. <section anchor="silk_ltp_scaling" title="LTP Scaling Parameter">
  3192. <t>
  3193. In some circumstances an LTP scaling parameter appears after the LTP filter
  3194. coefficients.
  3195. This allows the encoder to trade off the prediction gain between
  3196. packets against the recovery time after packet loss.
  3197. Like the quantization gains, only the first LBRR frame in an Opus frame,
  3198. an LBRR frame where the prior LBRR frame was not coded, and the first regular
  3199. SILK frame in each channel of an Opus frame include this field, and, like all
  3200. of the other LTP parameters, only for frames that are also voiced.
  3201. Unlike absolute-coding for pitch lags, a regular SILK frame other than the
  3202. first one in a channel will not include this field even if the prior frame was
  3203. not voiced.
  3204. </t>
  3205. <t>
  3206. If present, the value is coded using the 3-entry PDF in
  3207. <xref target="silk_ltp_scaling_pdf"/>.
  3208. The three possible values represent Q14 scale factors of 15565, 12288, and
  3209. 8192, respectively (corresponding to approximately 0.95, 0.75, and 0.5).
  3210. Frames that do not code the scaling parameter use the default factor of 15565
  3211. (approximately 0.95).
  3212. </t>
  3213. <texttable anchor="silk_ltp_scaling_pdf"
  3214. title="PDF for LTP Scaling Parameter">
  3215. <ttcol align="left">PDF</ttcol>
  3216. <c>{128, 64, 64}/256</c>
  3217. </texttable>
  3218. </section>
  3219. </section>
  3220. <section anchor="silk_seed" title="Linear Congruential Generator (LCG) Seed">
  3221. <t>
  3222. SILK uses a linear congruential generator (LCG) to inject pseudorandom noise
  3223. into the quantized excitation.
  3224. To ensure synchronization of this process between the encoder and decoder, each
  3225. SILK frame stores a 2-bit seed after the LTP parameters (if any).
  3226. The encoder may consider the choice of this seed during quantization, meaning
  3227. the flexibility to choose the LCG seed can reduce distortion.
  3228. The seed is decoded with the uniform 4-entry PDF in
  3229. <xref target="silk_seed_pdf"/>, yielding a value between 0 and 3, inclusive.
  3230. </t>
  3231. <texttable anchor="silk_seed_pdf"
  3232. title="PDF for LCG Seed">
  3233. <ttcol align="left">PDF</ttcol>
  3234. <c>{64, 64, 64, 64}/256</c>
  3235. </texttable>
  3236. </section>
  3237. <section anchor="silk_excitation" title="Excitation">
  3238. <t>
  3239. SILK codes the excitation using a modified version of the Pyramid Vector
  3240. Quantization (PVQ) codebook <xref target="PVQ"/>.
  3241. The PVQ codebook is designed for Laplace-distributed values and consists of all
  3242. sums of K signed, unit pulses in a vector of dimension N, where two pulses at
  3243. the same position are required to have the same sign.
  3244. Thus the codebook includes all integer codevectors y of dimension N that
  3245. satisfy
  3246. <figure align="center">
  3247. <artwork align="center"><![CDATA[
  3248. N-1
  3249. __
  3250. \ abs(y[j]) = K .
  3251. /_
  3252. j=0
  3253. ]]></artwork>
  3254. </figure>
  3255. Unlike regular PVQ, SILK uses a variable-length, rather than fixed-length,
  3256. encoding.
  3257. This encoding is better suited to the more Gaussian-like distribution of the
  3258. coefficient magnitudes and the non-uniform distribution of their signs (caused
  3259. by the quantization offset described below).
  3260. SILK also handles large codebooks by coding the least significant bits (LSb's)
  3261. of each coefficient directly.
  3262. This adds a small coding efficiency loss, but greatly reduces the computation
  3263. time and ROM size required for decoding, as implemented in
  3264. silk_decode_pulses() (silk_decode_pulses.c).
  3265. </t>
  3266. <t>
  3267. SILK fixes the dimension of the codebook to N&nbsp;=&nbsp;16.
  3268. The excitation is made up of a number of "shell blocks", each 16 samples in
  3269. size.
  3270. <xref target="silk_shell_block_table"/> lists the number of shell blocks
  3271. required for a SILK frame for each possible audio bandwidth and frame size.
  3272. 10&nbsp;ms MB frames nominally contain 120&nbsp;samples (10&nbsp;ms at
  3273. 12&nbsp;kHz), which is not a multiple of 16.
  3274. This is handled by coding 8 shell blocks (128 samples) and discarding the final
  3275. 8 samples of the last block.
  3276. The decoder contains no special case that prevents an encoder from placing
  3277. pulses in these samples, and they must be correctly parsed from the bitstream
  3278. if present, but they are otherwise ignored.
  3279. </t>
  3280. <texttable anchor="silk_shell_block_table"
  3281. title="Number of Shell Blocks Per SILK Frame">
  3282. <ttcol>Audio Bandwidth</ttcol>
  3283. <ttcol>Frame Size</ttcol>
  3284. <ttcol align="right">Number of Shell Blocks</ttcol>
  3285. <c>NB</c> <c>10&nbsp;ms</c> <c>5</c>
  3286. <c>MB</c> <c>10&nbsp;ms</c> <c>8</c>
  3287. <c>WB</c> <c>10&nbsp;ms</c> <c>10</c>
  3288. <c>NB</c> <c>20&nbsp;ms</c> <c>10</c>
  3289. <c>MB</c> <c>20&nbsp;ms</c> <c>15</c>
  3290. <c>WB</c> <c>20&nbsp;ms</c> <c>20</c>
  3291. </texttable>
  3292. <section anchor="silk_rate_level" title="Rate Level">
  3293. <t>
  3294. The first symbol in the excitation is a "rate level", which is an index from 0
  3295. to 8, inclusive, coded using the PDF in <xref target="silk_rate_level_pdfs"/>
  3296. corresponding to the signal type of the current frame (from
  3297. <xref target="silk_frame_type"/>).
  3298. The rate level selects the PDF used to decode the number of pulses in
  3299. the individual shell blocks.
  3300. It does not directly convey any information about the bitrate or the number of
  3301. pulses itself, but merely changes the probability of the symbols in
  3302. <xref target="silk_pulse_counts"/>.
  3303. Level&nbsp;0 provides a more efficient encoding at low rates generally, and
  3304. level&nbsp;8 provides a more efficient encoding at high rates generally,
  3305. though the most efficient level for a particular SILK frame may depend on the
  3306. exact distribution of the coded symbols.
  3307. An encoder should, but is not required to, use the most efficient rate level.
  3308. </t>
  3309. <texttable anchor="silk_rate_level_pdfs"
  3310. title="PDFs for the Rate Level">
  3311. <ttcol>Signal Type</ttcol>
  3312. <ttcol>PDF</ttcol>
  3313. <c>Inactive or Unvoiced</c>
  3314. <c>{15, 51, 12, 46, 45, 13, 33, 27, 14}/256</c>
  3315. <c>Voiced</c>
  3316. <c>{33, 30, 36, 17, 34, 49, 18, 21, 18}/256</c>
  3317. </texttable>
  3318. </section>
  3319. <section anchor="silk_pulse_counts" title="Pulses Per Shell Block">
  3320. <t>
  3321. The total number of pulses in each of the shell blocks follows the rate level.
  3322. The pulse counts for all of the shell blocks are coded consecutively, before
  3323. the content of any of the blocks.
  3324. Each block may have anywhere from 0 to 16 pulses, inclusive, coded using the
  3325. 18-entry PDF in <xref target="silk_pulse_count_pdfs"/> corresponding to the
  3326. rate level from <xref target="silk_rate_level"/>.
  3327. The special value 17 indicates that this block has one or more additional
  3328. LSb's to decode for each coefficient.
  3329. If the decoder encounters this value, it decodes another value for the actual
  3330. pulse count of the block, but uses the PDF corresponding to the special rate
  3331. level&nbsp;9 instead of the normal rate level.
  3332. This process repeats until the decoder reads a value less than 17, and it then
  3333. sets the number of extra LSb's used to the number of 17's decoded for that
  3334. block.
  3335. If it reads the value 17 ten times, then the next iteration uses the special
  3336. rate level&nbsp;10 instead of 9.
  3337. The probability of decoding a 17 when using the PDF for rate level&nbsp;10 is
  3338. zero, ensuring that the number of LSb's for a block will not exceed 10.
  3339. The cumulative distribution for rate level&nbsp;10 is just a shifted version of
  3340. that for 9 and thus does not require any additional storage.
  3341. </t>
  3342. <texttable anchor="silk_pulse_count_pdfs"
  3343. title="PDFs for the Pulse Count">
  3344. <ttcol>Rate Level</ttcol>
  3345. <ttcol>PDF</ttcol>
  3346. <c>0</c>
  3347. <c>{131, 74, 25, 8, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}/256</c>
  3348. <c>1</c>
  3349. <c>{58, 93, 60, 23, 7, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}/256</c>
  3350. <c>2</c>
  3351. <c>{43, 51, 46, 33, 24, 16, 11, 8, 6, 3, 3, 3, 2, 1, 1, 2, 1, 2}/256</c>
  3352. <c>3</c>
  3353. <c>{17, 52, 71, 57, 31, 12, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}/256</c>
  3354. <c>4</c>
  3355. <c>{6, 21, 41, 53, 49, 35, 21, 11, 6, 3, 2, 2, 1, 1, 1, 1, 1, 1}/256</c>
  3356. <c>5</c>
  3357. <c>{7, 14, 22, 28, 29, 28, 25, 20, 17, 13, 11, 9, 7, 5, 4, 4, 3, 10}/256</c>
  3358. <c>6</c>
  3359. <c>{2, 5, 14, 29, 42, 46, 41, 31, 19, 11, 6, 3, 2, 1, 1, 1, 1, 1}/256</c>
  3360. <c>7</c>
  3361. <c>{1, 2, 4, 10, 19, 29, 35, 37, 34, 28, 20, 14, 8, 5, 4, 2, 2, 2}/256</c>
  3362. <c>8</c>
  3363. <c>{1, 2, 2, 5, 9, 14, 20, 24, 27, 28, 26, 23, 20, 15, 11, 8, 6, 15}/256</c>
  3364. <c>9</c>
  3365. <c>{1, 1, 1, 6, 27, 58, 56, 39, 25, 14, 10, 6, 3, 3, 2, 1, 1, 2}/256</c>
  3366. <c>10</c>
  3367. <c>{2, 1, 6, 27, 58, 56, 39, 25, 14, 10, 6, 3, 3, 2, 1, 1, 2, 0}/256</c>
  3368. </texttable>
  3369. </section>
  3370. <section anchor="silk_pulse_locations" title="Pulse Location Decoding">
  3371. <t>
  3372. The locations of the pulses in each shell block follows the pulse counts,
  3373. as decoded by silk_shell_decoder() (silk_shell_coder.c).
  3374. As with the pulse counts, these locations are coded for all the shell blocks
  3375. before any of the remaining information for each block.
  3376. Unlike many other codecs, SILK places no restriction on the distribution of
  3377. pulses within a shell block.
  3378. All of the pulses may be placed in a single location, or each one in a unique
  3379. location, or anything in between.
  3380. </t>
  3381. <t>
  3382. The location of pulses is coded by recursively partitioning each block into
  3383. halves, and coding how many pulses fall on the left side of the split.
  3384. All remaining pulses must fall on the right side of the split.
  3385. The process then recurses into the left half, and after that returns, the
  3386. right half (preorder traversal).
  3387. The PDF to use is chosen by the size of the current partition (16, 8, 4, or 2)
  3388. and the number of pulses in the partition (1 to 16, inclusive).
  3389. <xref target="silk_shell_code3_pdfs"/> through
  3390. <xref target="silk_shell_code0_pdfs"/> list the PDFs used for each partition
  3391. size and pulse count.
  3392. This process skips partitions without any pulses, i.e., where the initial pulse
  3393. count from <xref target="silk_pulse_counts"/> was zero, or where the split in
  3394. the prior level indicated that all of the pulses fell on the other side.
  3395. These partitions have nothing to code, so they require no PDF.
  3396. </t>
  3397. <texttable anchor="silk_shell_code3_pdfs"
  3398. title="PDFs for Pulse Count Split, 16 Sample Partitions">
  3399. <ttcol>Pulse Count</ttcol>
  3400. <ttcol>PDF</ttcol>
  3401. <c>1</c> <c>{126, 130}/256</c>
  3402. <c>2</c> <c>{56, 142, 58}/256</c>
  3403. <c>3</c> <c>{25, 101, 104, 26}/256</c>
  3404. <c>4</c> <c>{12, 60, 108, 64, 12}/256</c>
  3405. <c>5</c> <c>{7, 35, 84, 87, 37, 6}/256</c>
  3406. <c>6</c> <c>{4, 20, 59, 86, 63, 21, 3}/256</c>
  3407. <c>7</c> <c>{3, 12, 38, 72, 75, 42, 12, 2}/256</c>
  3408. <c>8</c> <c>{2, 8, 25, 54, 73, 59, 27, 7, 1}/256</c>
  3409. <c>9</c> <c>{2, 5, 17, 39, 63, 65, 42, 18, 4, 1}/256</c>
  3410. <c>10</c> <c>{1, 4, 12, 28, 49, 63, 54, 30, 11, 3, 1}/256</c>
  3411. <c>11</c> <c>{1, 4, 8, 20, 37, 55, 57, 41, 22, 8, 2, 1}/256</c>
  3412. <c>12</c> <c>{1, 3, 7, 15, 28, 44, 53, 48, 33, 16, 6, 1, 1}/256</c>
  3413. <c>13</c> <c>{1, 2, 6, 12, 21, 35, 47, 48, 40, 25, 12, 5, 1, 1}/256</c>
  3414. <c>14</c> <c>{1, 1, 4, 10, 17, 27, 37, 47, 43, 33, 21, 9, 4, 1, 1}/256</c>
  3415. <c>15</c> <c>{1, 1, 1, 8, 14, 22, 33, 40, 43, 38, 28, 16, 8, 1, 1, 1}/256</c>
  3416. <c>16</c> <c>{1, 1, 1, 1, 13, 18, 27, 36, 41, 41, 34, 24, 14, 1, 1, 1, 1}/256</c>
  3417. </texttable>
  3418. <texttable anchor="silk_shell_code2_pdfs"
  3419. title="PDFs for Pulse Count Split, 8 Sample Partitions">
  3420. <ttcol>Pulse Count</ttcol>
  3421. <ttcol>PDF</ttcol>
  3422. <c>1</c> <c>{127, 129}/256</c>
  3423. <c>2</c> <c>{53, 149, 54}/256</c>
  3424. <c>3</c> <c>{22, 105, 106, 23}/256</c>
  3425. <c>4</c> <c>{11, 61, 111, 63, 10}/256</c>
  3426. <c>5</c> <c>{6, 35, 86, 88, 36, 5}/256</c>
  3427. <c>6</c> <c>{4, 20, 59, 87, 62, 21, 3}/256</c>
  3428. <c>7</c> <c>{3, 13, 40, 71, 73, 41, 13, 2}/256</c>
  3429. <c>8</c> <c>{3, 9, 27, 53, 70, 56, 28, 9, 1}/256</c>
  3430. <c>9</c> <c>{3, 8, 19, 37, 57, 61, 44, 20, 6, 1}/256</c>
  3431. <c>10</c> <c>{3, 7, 15, 28, 44, 54, 49, 33, 17, 5, 1}/256</c>
  3432. <c>11</c> <c>{1, 7, 13, 22, 34, 46, 48, 38, 28, 14, 4, 1}/256</c>
  3433. <c>12</c> <c>{1, 1, 11, 22, 27, 35, 42, 47, 33, 25, 10, 1, 1}/256</c>
  3434. <c>13</c> <c>{1, 1, 6, 14, 26, 37, 43, 43, 37, 26, 14, 6, 1, 1}/256</c>
  3435. <c>14</c> <c>{1, 1, 4, 10, 20, 31, 40, 42, 40, 31, 20, 10, 4, 1, 1}/256</c>
  3436. <c>15</c> <c>{1, 1, 3, 8, 16, 26, 35, 38, 38, 35, 26, 16, 8, 3, 1, 1}/256</c>
  3437. <c>16</c> <c>{1, 1, 2, 6, 12, 21, 30, 36, 38, 36, 30, 21, 12, 6, 2, 1, 1}/256</c>
  3438. </texttable>
  3439. <texttable anchor="silk_shell_code1_pdfs"
  3440. title="PDFs for Pulse Count Split, 4 Sample Partitions">
  3441. <ttcol>Pulse Count</ttcol>
  3442. <ttcol>PDF</ttcol>
  3443. <c>1</c> <c>{127, 129}/256</c>
  3444. <c>2</c> <c>{49, 157, 50}/256</c>
  3445. <c>3</c> <c>{20, 107, 109, 20}/256</c>
  3446. <c>4</c> <c>{11, 60, 113, 62, 10}/256</c>
  3447. <c>5</c> <c>{7, 36, 84, 87, 36, 6}/256</c>
  3448. <c>6</c> <c>{6, 24, 57, 82, 60, 23, 4}/256</c>
  3449. <c>7</c> <c>{5, 18, 39, 64, 68, 42, 16, 4}/256</c>
  3450. <c>8</c> <c>{6, 14, 29, 47, 61, 52, 30, 14, 3}/256</c>
  3451. <c>9</c> <c>{1, 15, 23, 35, 51, 50, 40, 30, 10, 1}/256</c>
  3452. <c>10</c> <c>{1, 1, 21, 32, 42, 52, 46, 41, 18, 1, 1}/256</c>
  3453. <c>11</c> <c>{1, 6, 16, 27, 36, 42, 42, 36, 27, 16, 6, 1}/256</c>
  3454. <c>12</c> <c>{1, 5, 12, 21, 31, 38, 40, 38, 31, 21, 12, 5, 1}/256</c>
  3455. <c>13</c> <c>{1, 3, 9, 17, 26, 34, 38, 38, 34, 26, 17, 9, 3, 1}/256</c>
  3456. <c>14</c> <c>{1, 3, 7, 14, 22, 29, 34, 36, 34, 29, 22, 14, 7, 3, 1}/256</c>
  3457. <c>15</c> <c>{1, 2, 5, 11, 18, 25, 31, 35, 35, 31, 25, 18, 11, 5, 2, 1}/256</c>
  3458. <c>16</c> <c>{1, 1, 4, 9, 15, 21, 28, 32, 34, 32, 28, 21, 15, 9, 4, 1, 1}/256</c>
  3459. </texttable>
  3460. <texttable anchor="silk_shell_code0_pdfs"
  3461. title="PDFs for Pulse Count Split, 2 Sample Partitions">
  3462. <ttcol>Pulse Count</ttcol>
  3463. <ttcol>PDF</ttcol>
  3464. <c>1</c> <c>{128, 128}/256</c>
  3465. <c>2</c> <c>{42, 172, 42}/256</c>
  3466. <c>3</c> <c>{21, 107, 107, 21}/256</c>
  3467. <c>4</c> <c>{12, 60, 112, 61, 11}/256</c>
  3468. <c>5</c> <c>{8, 34, 86, 86, 35, 7}/256</c>
  3469. <c>6</c> <c>{8, 23, 55, 90, 55, 20, 5}/256</c>
  3470. <c>7</c> <c>{5, 15, 38, 72, 72, 36, 15, 3}/256</c>
  3471. <c>8</c> <c>{6, 12, 27, 52, 77, 47, 20, 10, 5}/256</c>
  3472. <c>9</c> <c>{6, 19, 28, 35, 40, 40, 35, 28, 19, 6}/256</c>
  3473. <c>10</c> <c>{4, 14, 22, 31, 37, 40, 37, 31, 22, 14, 4}/256</c>
  3474. <c>11</c> <c>{3, 10, 18, 26, 33, 38, 38, 33, 26, 18, 10, 3}/256</c>
  3475. <c>12</c> <c>{2, 8, 13, 21, 29, 36, 38, 36, 29, 21, 13, 8, 2}/256</c>
  3476. <c>13</c> <c>{1, 5, 10, 17, 25, 32, 38, 38, 32, 25, 17, 10, 5, 1}/256</c>
  3477. <c>14</c> <c>{1, 4, 7, 13, 21, 29, 35, 36, 35, 29, 21, 13, 7, 4, 1}/256</c>
  3478. <c>15</c> <c>{1, 2, 5, 10, 17, 25, 32, 36, 36, 32, 25, 17, 10, 5, 2, 1}/256</c>
  3479. <c>16</c> <c>{1, 2, 4, 7, 13, 21, 28, 34, 36, 34, 28, 21, 13, 7, 4, 2, 1}/256</c>
  3480. </texttable>
  3481. </section>
  3482. <section anchor="silk_shell_lsb" title="LSb Decoding">
  3483. <t>
  3484. After the decoder reads the pulse locations for all blocks, it reads the LSb's
  3485. (if any) for each block in turn.
  3486. Inside each block, it reads all the LSb's for each coefficient in turn, even
  3487. those where no pulses were allocated, before proceeding to the next one.
  3488. They are coded from most significant to least significant, and they all use the
  3489. PDF in <xref target="silk_shell_lsb_pdf"/>.
  3490. </t>
  3491. <texttable anchor="silk_shell_lsb_pdf" title="PDF for Excitation LSb's">
  3492. <ttcol>PDF</ttcol>
  3493. <c>{136, 120}/256</c>
  3494. </texttable>
  3495. <t>
  3496. The number of LSb's read for each coefficient in a block is determined in
  3497. <xref target="silk_pulse_counts"/>.
  3498. The magnitude of the coefficient is initially equal to the number of pulses
  3499. placed at that location in <xref target="silk_pulse_locations"/>.
  3500. As each LSb is decoded, the magnitude is doubled, and then the value of the LSb
  3501. added to it, to obtain an updated magnitude.
  3502. </t>
  3503. </section>
  3504. <section anchor="silk_signs" title="Sign Decoding">
  3505. <t>
  3506. After decoding the pulse locations and the LSb's, the decoder knows the
  3507. magnitude of each coefficient in the excitation.
  3508. It then decodes a sign for all coefficients with a non-zero magnitude, using
  3509. one of the PDFs from <xref target="silk_sign_pdfs"/>.
  3510. If the value decoded is 0, then the coefficient magnitude is negated.
  3511. Otherwise, it remains positive.
  3512. </t>
  3513. <t>
  3514. The decoder chooses the PDF for the sign based on the signal type and
  3515. quantization offset type (from <xref target="silk_frame_type"/>) and the
  3516. number of pulses in the block (from <xref target="silk_pulse_counts"/>).
  3517. The number of pulses in the block does not take into account any LSb's.
  3518. If a block has no pulses, even if it has some LSb's (and thus may have some
  3519. non-zero coefficients), then no signs are decoded.
  3520. In that case, any non-zero coefficients use a positive sign.
  3521. </t>
  3522. <texttable anchor="silk_sign_pdfs"
  3523. title="PDFs for Excitation Signs">
  3524. <ttcol>Signal Type</ttcol>
  3525. <ttcol>Quantization Offset Type</ttcol>
  3526. <ttcol>Pulse Count</ttcol>
  3527. <ttcol>PDF</ttcol>
  3528. <c>Inactive</c> <c>0</c> <c>1</c> <c>{207, 49}/256</c>
  3529. <c>Inactive</c> <c>0</c> <c>2</c> <c>{189, 67}/256</c>
  3530. <c>Inactive</c> <c>0</c> <c>3</c> <c>{179, 77}/256</c>
  3531. <c>Inactive</c> <c>0</c> <c>4</c> <c>{174, 82}/256</c>
  3532. <c>Inactive</c> <c>0</c> <c>5</c> <c>{163, 93}/256</c>
  3533. <c>Inactive</c> <c>0</c> <c>6 or more</c> <c>{157, 99}/256</c>
  3534. <c>Inactive</c> <c>1</c> <c>1</c> <c>{245, 11}/256</c>
  3535. <c>Inactive</c> <c>1</c> <c>2</c> <c>{238, 18}/256</c>
  3536. <c>Inactive</c> <c>1</c> <c>3</c> <c>{232, 24}/256</c>
  3537. <c>Inactive</c> <c>1</c> <c>4</c> <c>{225, 31}/256</c>
  3538. <c>Inactive</c> <c>1</c> <c>5</c> <c>{220, 36}/256</c>
  3539. <c>Inactive</c> <c>1</c> <c>6 or more</c> <c>{211, 45}/256</c>
  3540. <c>Unvoiced</c> <c>0</c> <c>1</c> <c>{210, 46}/256</c>
  3541. <c>Unvoiced</c> <c>0</c> <c>2</c> <c>{190, 66}/256</c>
  3542. <c>Unvoiced</c> <c>0</c> <c>3</c> <c>{178, 78}/256</c>
  3543. <c>Unvoiced</c> <c>0</c> <c>4</c> <c>{169, 87}/256</c>
  3544. <c>Unvoiced</c> <c>0</c> <c>5</c> <c>{162, 94}/256</c>
  3545. <c>Unvoiced</c> <c>0</c> <c>6 or more</c> <c>{152, 104}/256</c>
  3546. <c>Unvoiced</c> <c>1</c> <c>1</c> <c>{242, 14}/256</c>
  3547. <c>Unvoiced</c> <c>1</c> <c>2</c> <c>{235, 21}/256</c>
  3548. <c>Unvoiced</c> <c>1</c> <c>3</c> <c>{224, 32}/256</c>
  3549. <c>Unvoiced</c> <c>1</c> <c>4</c> <c>{214, 42}/256</c>
  3550. <c>Unvoiced</c> <c>1</c> <c>5</c> <c>{205, 51}/256</c>
  3551. <c>Unvoiced</c> <c>1</c> <c>6 or more</c> <c>{190, 66}/256</c>
  3552. <c>Voiced</c> <c>0</c> <c>1</c> <c>{162, 94}/256</c>
  3553. <c>Voiced</c> <c>0</c> <c>2</c> <c>{152, 104}/256</c>
  3554. <c>Voiced</c> <c>0</c> <c>3</c> <c>{147, 109}/256</c>
  3555. <c>Voiced</c> <c>0</c> <c>4</c> <c>{144, 112}/256</c>
  3556. <c>Voiced</c> <c>0</c> <c>5</c> <c>{141, 115}/256</c>
  3557. <c>Voiced</c> <c>0</c> <c>6 or more</c> <c>{138, 118}/256</c>
  3558. <c>Voiced</c> <c>1</c> <c>1</c> <c>{203, 53}/256</c>
  3559. <c>Voiced</c> <c>1</c> <c>2</c> <c>{187, 69}/256</c>
  3560. <c>Voiced</c> <c>1</c> <c>3</c> <c>{176, 80}/256</c>
  3561. <c>Voiced</c> <c>1</c> <c>4</c> <c>{168, 88}/256</c>
  3562. <c>Voiced</c> <c>1</c> <c>5</c> <c>{161, 95}/256</c>
  3563. <c>Voiced</c> <c>1</c> <c>6 or more</c> <c>{154, 102}/256</c>
  3564. </texttable>
  3565. </section>
  3566. </section>
  3567. </section>
  3568. </section>
  3569. <section title="CELT Decoder">
  3570. <t>
  3571. The CELT layer is decoded based on the following symbols and sets of symbols:
  3572. </t>
  3573. <texttable anchor='table_example'>
  3574. <ttcol align='center'>Symbol(s)</ttcol>
  3575. <ttcol align='center'>PDF</ttcol>
  3576. <ttcol align='center'>Condition</ttcol>
  3577. <c>silence</c> <c>{32767, 1}/32768</c> <c></c>
  3578. <c>post-filter</c> <c>{1, 1}/2</c> <c></c>
  3579. <c>octave</c> <c>uniform (6)</c><c>post-filter</c>
  3580. <c>period</c> <c>raw bits (4+octave)</c><c>post-filter</c>
  3581. <c>gain</c> <c>raw bits (3)</c><c>post-filter</c>
  3582. <c>tapset</c> <c>{2, 1, 1}/4</c><c>post-filter</c>
  3583. <c>transient</c> <c>{7, 1}/8</c><c></c>
  3584. <c>intra</c> <c>{7, 1}/8</c><c></c>
  3585. <c>coarse energy</c><c><xref target="energy-decoding"/></c><c></c>
  3586. <c>tf_change</c> <c><xref target="transient-decoding"/></c><c></c>
  3587. <c>tf_select</c> <c>{1, 1}/2</c><c><xref target="transient-decoding"/></c>
  3588. <c>spread</c> <c>{7, 2, 21, 2}/32</c><c></c>
  3589. <c>dyn. alloc.</c> <c><xref target="allocation"/></c><c></c>
  3590. <c>alloc. trim</c> <c>{2, 2, 5, 10, 22, 46, 22, 10, 5, 2, 2}/128</c><c></c>
  3591. <c>skip</c> <c>{1, 1}/2</c><c><xref target="allocation"/></c>
  3592. <c>intensity</c> <c>uniform</c><c><xref target="allocation"/></c>
  3593. <c>dual</c> <c>{1, 1}/2</c><c></c>
  3594. <c>fine energy</c> <c><xref target="energy-decoding"/></c><c></c>
  3595. <c>residual</c> <c><xref target="PVQ-decoder"/></c><c></c>
  3596. <c>anti-collapse</c><c>{1, 1}/2</c><c><xref target="anti-collapse"/></c>
  3597. <c>finalize</c> <c><xref target="energy-decoding"/></c><c></c>
  3598. <postamble>Order of the symbols in the CELT section of the bitstream.</postamble>
  3599. </texttable>
  3600. <t>
  3601. The decoder extracts information from the range-coded bitstream in the order
  3602. described in the figure above. In some circumstances, it is
  3603. possible for a decoded value to be out of range due to a very small amount of redundancy
  3604. in the encoding of large integers by the range coder.
  3605. In that case, the decoder should assume there has been an error in the coding,
  3606. decoding, or transmission and SHOULD take measures to conceal the error and/or report
  3607. to the application that a problem has occurred.
  3608. </t>
  3609. <section anchor="transient-decoding" title="Transient Decoding">
  3610. <t>
  3611. The "transient" flag encoded in the bitstream has a probability of 1/8.
  3612. When it is set, then the MDCT coefficients represent multiple
  3613. short MDCTs in the frame. When not set, the coefficients represent a single
  3614. long MDCT for the frame. In addition to the global transient flag is a per-band
  3615. binary flag to change the time-frequency (tf) resolution independently in each band. The
  3616. change in tf resolution is defined in tf_select_table[][] in celt.c and depends
  3617. on the frame size, whether the transient flag is set, and the value of tf_select.
  3618. The tf_select flag uses a 1/2 probability, but is only decoded
  3619. if it can have an impact on the result knowing the value of all per-band
  3620. tf_change flags.
  3621. </t>
  3622. </section>
  3623. <section anchor="energy-decoding" title="Energy Envelope Decoding">
  3624. <t>
  3625. It is important to quantize the energy with sufficient resolution because
  3626. any energy quantization error cannot be compensated for at a later
  3627. stage. Regardless of the resolution used for encoding the shape of a band,
  3628. it is perceptually important to preserve the energy in each band. CELT uses a
  3629. three-step coarse-fine-fine strategy for encoding the energy in the base-2 log
  3630. domain, as implemented in quant_bands.c</t>
  3631. <section anchor="coarse-energy-decoding" title="Coarse energy decoding">
  3632. <t>
  3633. Coarse quantization of the energy uses a fixed resolution of 6 dB
  3634. (integer part of base-2 log). To minimize the bitrate, prediction is applied
  3635. both in time (using the previous frame) and in frequency (using the previous
  3636. bands). The part of the prediction that is based on the
  3637. previous frame can be disabled, creating an "intra" frame where the energy
  3638. is coded without reference to prior frames. The decoder first reads the intra flag
  3639. to determine what prediction is used.
  3640. The 2-D z-transform of
  3641. the prediction filter is:
  3642. <figure align="center">
  3643. <artwork align="center"><![CDATA[
  3644. -1 -1
  3645. (1 - alpha*z_l )*(1 - z_b )
  3646. A(z_l, z_b) = -----------------------------
  3647. -1
  3648. 1 - beta*z_b
  3649. ]]></artwork>
  3650. </figure>
  3651. where b is the band index and l is the frame index. The prediction coefficients
  3652. applied depend on the frame size in use when not using intra energy and are alpha=0, beta=4915/32768
  3653. when using intra energy.
  3654. The time-domain prediction is based on the final fine quantization of the previous
  3655. frame, while the frequency domain (within the current frame) prediction is based
  3656. on coarse quantization only (because the fine quantization has not been computed
  3657. yet). The prediction is clamped internally so that fixed point implementations with
  3658. limited dynamic range do not suffer desynchronization.
  3659. We approximate the ideal
  3660. probability distribution of the prediction error using a Laplace distribution
  3661. with separate parameters for each frame size in intra- and inter-frame modes. The
  3662. coarse energy quantization is performed by unquant_coarse_energy() and
  3663. unquant_coarse_energy_impl() (quant_bands.c). The encoding of the Laplace-distributed values is
  3664. implemented in ec_laplace_decode() (laplace.c).
  3665. </t>
  3666. </section>
  3667. <section anchor="fine-energy-decoding" title="Fine energy quantization">
  3668. <t>
  3669. The number of bits assigned to fine energy quantization in each band is determined
  3670. by the bit allocation computation described in <xref target="allocation"></xref>.
  3671. Let B_i be the number of fine energy bits
  3672. for band i; the refinement is an integer f in the range [0,2**B_i-1]. The mapping between f
  3673. and the correction applied to the coarse energy is equal to (f+1/2)/2**B_i - 1/2. Fine
  3674. energy quantization is implemented in quant_fine_energy() (quant_bands.c).
  3675. </t>
  3676. <t>
  3677. When some bits are left "unused" after all other flags have been decoded, these bits
  3678. are assigned to a "final" step of fine allocation. In effect, these bits are used
  3679. to add one extra fine energy bit per band per channel. The allocation process
  3680. determines two "priorities" for the final fine bits.
  3681. Any remaining bits are first assigned only to bands of priority 0, starting
  3682. from band 0 and going up. If all bands of priority 0 have received one bit per
  3683. channel, then bands of priority 1 are assigned an extra bit per channel,
  3684. starting from band 0. If any bits are left after this, they are left unused.
  3685. This is implemented in unquant_energy_finalise() (quant_bands.c).
  3686. </t>
  3687. </section> <!-- fine energy -->
  3688. </section> <!-- Energy decode -->
  3689. <section anchor="allocation" title="Bit allocation">
  3690. <t>Many codecs transmit significant amounts of side information for
  3691. the purpose of controlling bit allocation within a frame. Often this
  3692. side information controls bit usage indirectly and must be carefully
  3693. selected to achieve the desired rate constraints.</t>
  3694. <t>The band-energy normalized structure of Opus MDCT mode ensures that a
  3695. constant bit allocation for the shape content of a band will result in a
  3696. roughly constant tone to noise ratio, which provides for fairly consistent
  3697. perceptual performance. The effectiveness of this approach is the result of
  3698. two factors: that the band energy, which is understood to be perceptually
  3699. important on its own, is always preserved regardless of the shape precision, and because
  3700. the constant tone-to-noise ratio implies a constant intra-band noise to masking ratio.
  3701. Intra-band masking is the strongest of the perceptual masking effects. This structure
  3702. means that the ideal allocation is more consistent from frame to frame than
  3703. it is for other codecs without an equivalent structure.</t>
  3704. <t>Because the bit allocation is used to drive the decoding of the range-coder
  3705. stream, it MUST be recovered exactly so that identical coding decisions are
  3706. made in the encoder and decoder. Any deviation from the reference's resulting
  3707. bit allocation will result in corrupted output, though implementers are
  3708. free to implement the procedure in any way which produces identical results.</t>
  3709. <t>Because all of the information required to decode a frame must be derived
  3710. from that frame alone in order to retain robustness to packet loss, the
  3711. overhead of explicitly signaling the allocation would be considerable,
  3712. especially for low-latency (small frame size) applications,
  3713. even though the allocation is relatively static.</t>
  3714. <t>For this reason, in the MDCT mode Opus uses a primarily implicit bit
  3715. allocation. The available bitstream capacity is known in advance to both
  3716. the encoder and decoder without additional signaling, ultimately from the
  3717. packet sizes expressed by a higher-level protocol. Using this information
  3718. the codec interpolates an allocation from a hard-coded table.</t>
  3719. <t>While the band-energy structure effectively models intra-band masking,
  3720. it ignores the weaker inter-band masking, band-temporal masking, and
  3721. other less significant perceptual effects. While these effects can
  3722. often be ignored, they can become significant for particular samples. One
  3723. mechanism available to encoders would be to simply increase the overall
  3724. rate for these frames, but this is not possible in a constant rate mode
  3725. and can be fairly inefficient. As a result three explicitly signaled
  3726. mechanisms are provided to alter the implicit allocation:</t>
  3727. <t>
  3728. <list style="symbols">
  3729. <t>Band boost</t>
  3730. <t>Allocation trim</t>
  3731. <t>Band skipping</t>
  3732. </list>
  3733. </t>
  3734. <t>The first of these mechanisms, band boost, allows an encoder to boost
  3735. the allocation in specific bands. The second, allocation trim, works by
  3736. biasing the overall allocation towards higher or lower frequency bands. The third, band
  3737. skipping, selects which low-precision high frequency bands
  3738. will be allocated no shape bits at all.</t>
  3739. <t>In stereo mode there are two additional parameters
  3740. potentially coded as part of the allocation procedure: a parameter to allow the
  3741. selective elimination of allocation for the 'side' in jointly coded bands,
  3742. and a flag to deactivate joint coding. These values are not signaled if
  3743. they would be meaningless in the overall context of the allocation.</t>
  3744. <t>Because every signaled adjustment increases overhead and implementation
  3745. complexity, none were included speculatively: the reference encoder makes use
  3746. of all of these mechanisms. While the decision logic in the reference was
  3747. found to be effective enough to justify the overhead and complexity, further
  3748. analysis techniques may be discovered which increase the effectiveness of these
  3749. parameters. As with other signaled parameters, an encoder is free to choose the
  3750. values in any manner, but unless a technique is known to deliver superior
  3751. perceptual results the methods used by the reference implementation should be
  3752. used.</t>
  3753. <t>The allocation process consists of the following steps: determining the per-band
  3754. maximum allocation vector, decoding the boosts, decoding the tilt, determining
  3755. the remaining capacity of the frame, searching the mode table for the
  3756. entry nearest but not exceeding the available space (subject to the tilt, boosts, band
  3757. maximums, and band minimums), linear interpolation, reallocation of
  3758. unused bits with concurrent skip decoding, determination of the
  3759. fine-energy vs. shape split, and final reallocation. This process results
  3760. in a per-band shape allocation (in 1/8th bit units), a per-band fine-energy
  3761. allocation (in 1 bit per channel units), a set of band priorities for
  3762. controlling the use of remaining bits at the end of the frame, and a
  3763. remaining balance of unallocated space, which is usually zero except
  3764. at very high rates.</t>
  3765. <t>The maximum allocation vector is an approximation of the maximum space
  3766. that can be used by each band for a given mode. The value is
  3767. approximate because the shape encoding is variable rate (due
  3768. to entropy coding of splitting parameters). Setting the maximum too low reduces the
  3769. maximum achievable quality in a band while setting it too high
  3770. may result in waste: bitstream capacity available at the end
  3771. of the frame which can not be put to any use. The maximums
  3772. specified by the codec reflect the average maximum. In the reference
  3773. the maximums are provided in partially computed form, in order to fit in less
  3774. memory as a static table (XXX cache.caps). Implementations are expected
  3775. to simply use the same table data, but the procedure for generating
  3776. this table is included in rate.c as part of compute_pulse_cache().</t>
  3777. <t>To convert the values in cache.caps into the actual maximums: first
  3778. set nbBands to the maximum number of bands for this mode, and stereo to
  3779. zero if stereo is not in use and one otherwise. For each band set N
  3780. to the number of MDCT bins covered by the band (for one channel), set LM
  3781. to the shift value for the frame size (e.g. 0 for 120, 1 for 240, 3 for 480),
  3782. then set i to nbBands*(2*LM+stereo). Then set the maximum for the band to
  3783. the i-th index of cache.caps + 64 and multiply by the number of channels
  3784. in the current frame (one or two) and by N, then divide the result by 4
  3785. using truncating integer division. The resulting vector will be called
  3786. cap[]. The elements fit in signed 16-bit integers but do not fit in 8 bits.
  3787. This procedure is implemented in the reference in the function init_caps() in celt.c.
  3788. </t>
  3789. <t>The band boosts are represented by a series of binary symbols which
  3790. are coded with very low probability. Each band can potentially be boosted
  3791. multiple times, subject to the frame actually having enough room to obey
  3792. the boost and having enough room to code the boost symbol. The default
  3793. coding cost for a boost starts out at six bits, but subsequent boosts
  3794. in a band cost only a single bit and every time a band is boosted the
  3795. initial cost is reduced (down to a minimum of two). Since the initial
  3796. cost of coding a boost is 6 bits, the coding cost of the boost symbols when
  3797. completely unused is 0.48 bits/frame for a 21 band mode (21*-log2(1-1/2**6)).</t>
  3798. <t>To decode the band boosts: First set 'dynalloc_logp' to 6, the initial
  3799. amount of storage required to signal a boost in bits, 'total_bits' to the
  3800. size of the frame in 8th bits, 'total_boost' to zero, and 'tell' to the total number
  3801. of 8th bits decoded
  3802. so far. For each band from the coding start (0 normally, but 17 in hybrid mode)
  3803. to the coding end (which changes depending on the signaled bandwidth): set 'width'
  3804. to the number of MDCT bins in this band for all channels. Take the larger of width
  3805. and 64, then the minimum of that value and the width times eight and set 'quanta'
  3806. to the result. This represents a boost step size of six bits subject to limits
  3807. of 1/bit/sample and 1/8th bit/sample. Set 'boost' to zero and 'dynalloc_loop_logp'
  3808. to dynalloc_logp. While dynalloc_loop_log (the current worst case symbol cost) in
  3809. 8th bits plus tell is less than total_bits plus total_boost and boost is less than cap[] for this
  3810. band: Decode a bit from the bitstream with a with dynalloc_loop_logp as the cost
  3811. of a one, update tell to reflect the current used capacity, if the decoded value
  3812. is zero break the loop otherwise add quanta to boost and total_boost, subtract quanta from
  3813. total_bits, and set dynalloc_loop_log to 1. When the while loop finishes
  3814. boost contains the boost for this band. If boost is non-zero and dynalloc_logp
  3815. is greater than 2, decrease dynalloc_logp. Once this process has been
  3816. executed on all bands, the band boosts have been decoded. This procedure
  3817. is implemented around line 2352 of celt.c.</t>
  3818. <t>At very low rates it is possible that there won't be enough available
  3819. space to execute the inner loop even once. In these cases band boost
  3820. is not possible but its overhead is completely eliminated. Because of the
  3821. high cost of band boost when activated, a reasonable encoder should not be
  3822. using it at very low rates. The reference implements its dynalloc decision
  3823. logic around line 1269 of celt.c.</t>
  3824. <t>The allocation trim is a integer value from 0-10. The default value of
  3825. 5 indicates no trim. The trim parameter is entropy coded in order to
  3826. lower the coding cost of less extreme adjustments. Values lower than
  3827. 5 bias the allocation towards lower frequencies and values above 5
  3828. bias it towards higher frequencies. Like other signaled parameters, signaling
  3829. of the trim is gated so that it is not included if there is insufficient space
  3830. available in the bitstream. To decode the trim, first set
  3831. the trim value to 5, then iff the count of decoded 8th bits so far (ec_tell_frac)
  3832. plus 48 (6 bits) is less than or equal to the total frame size in 8th
  3833. bits minus total_boost (a product of the above band boost procedure),
  3834. decode the trim value using the inverse CDF {127, 126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0}.</t>
  3835. <t>Stereo parameters</t>
  3836. <t>Anti-collapse reservation</t>
  3837. <t>The allocation computation begins by setting up some initial conditions.
  3838. 'total' is set to the remaining available 8th bits, computed by taking the
  3839. size of the coded frame times 8 and subtracting ec_tell_frac(). From this value, one (8th bit)
  3840. is subtracted to ensure that the resulting allocation will be conservative. 'anti_collapse_rsv'
  3841. is set to 8 (8th bits) iff the frame is a transient, LM is greater than 1, and total is
  3842. greater than or equal to (LM+2) * 8. Total is then decremented by anti_collapse_rsv and clamped
  3843. to be equal to or greater than zero. 'skip_rsv' is set to 8 (8th bits) if total is greater than
  3844. 8, otherwise it is zero. Total is then decremented by skip_rsv. This reserves space for the
  3845. final skipping flag.</t>
  3846. <t>If the current frame is stereo, intensity_rsv is set to the conservative log2 in 8th bits
  3847. of the number of coded bands for this frame (given by the table LOG2_FRAC_TABLE). If
  3848. intensity_rsv is greater than total then intensity_rsv is set to zero. Otherwise total is
  3849. decremented by intensity_rsv, and if total is still greater than 8, dual_stereo_rsv is
  3850. set to 8 and total is decremented by dual_stereo_rsv.</t>
  3851. <t>The allocation process then computes a vector representing the hard minimum amounts allocation
  3852. any band will receive for shape. This minimum is higher than the technical limit of the PVQ
  3853. process, but very low rate allocations produce an excessively sparse spectrum and these bands
  3854. are better served by having no allocation at all. For each coded band, set thresh[band] to
  3855. twenty-four times the number of MDCT bins in the band and divide by 16. If 8 times the number
  3856. of channels is greater, use that instead. This sets the minimum allocation to one bit per channel
  3857. or 48 128th bits per MDCT bin, whichever is greater. The band-size dependent part of this
  3858. value is not scaled by the channel count, because at the very low rates where this limit is
  3859. applicable there will usually be no bits allocated to the side.</t>
  3860. <t>The previously decoded allocation trim is used to derive a vector of per-band adjustments,
  3861. 'trim_offsets[]'. For each coded band take the alloc_trim and subtract 5 and LM. Then multiply
  3862. the result by the number of channels, the number of MDCT bins in the shortest frame size for this mode,
  3863. the number of remaining bands, 2**LM, and 8. Then divide this value by 64. Finally, if the
  3864. number of MDCT bins in the band per channel is only one, 8 times the number of channels is subtracted
  3865. in order to diminish the allocation by one bit, because width 1 bands receive greater benefit
  3866. from the coarse energy coding.</t>
  3867. </section>
  3868. <section anchor="PVQ-decoder" title="Shape Decoder">
  3869. <t>
  3870. In each band, the normalized "shape" is encoded
  3871. using a vector quantization scheme called a "pyramid vector quantizer".
  3872. </t>
  3873. <t>In
  3874. the simplest case, the number of bits allocated in
  3875. <xref target="allocation"></xref> is converted to a number of pulses as described
  3876. by <xref target="bits-pulses"></xref>. Knowing the number of pulses and the
  3877. number of samples in the band, the decoder calculates the size of the codebook
  3878. as detailed in <xref target="cwrs-decoder"></xref>. The size is used to decode
  3879. an unsigned integer (uniform probability model), which is the codeword index.
  3880. This index is converted into the corresponding vector as explained in
  3881. <xref target="cwrs-decoder"></xref>. This vector is then scaled to unit norm.
  3882. </t>
  3883. <section anchor="bits-pulses" title="Bits to Pulses">
  3884. <t>
  3885. Although the allocation is performed in 1/8th bit units, the quantization requires
  3886. an integer number of pulses K. To do this, the encoder searches for the value
  3887. of K that produces the number of bits nearest to the allocated value
  3888. (rounding down if exactly halfway between two values), not to exceed
  3889. the total number of bits available. For efficiency reasons, the search is performed against a
  3890. precomputed allocation table which only permits some K values for each N. The number of
  3891. codebook entries can be computed as explained in <xref target="cwrs-encoding"></xref>. The difference
  3892. between the number of bits allocated and the number of bits used is accumulated to a
  3893. "balance" (initialized to zero) that helps adjust the
  3894. allocation for the next bands. One third of the balance is applied to the
  3895. bit allocation of each band to help achieve the target allocation. The only
  3896. exceptions are the band before the last and the last band, for which half the balance
  3897. and the whole balance are applied, respectively.
  3898. </t>
  3899. </section>
  3900. <section anchor="cwrs-decoder" title="Index Decoding">
  3901. <t>
  3902. The codeword is decoded as a uniformly-distributed integer value
  3903. by decode_pulses() (cwrs.c).
  3904. The codeword is converted from a unique index in the same way specified in
  3905. <xref target="PVQ"></xref>. The indexing is based on the calculation of V(N,K)
  3906. (denoted N(L,K) in <xref target="PVQ"></xref>), which is the number of possible
  3907. combinations of K pulses
  3908. in N samples. The number of combinations can be computed recursively as
  3909. 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.
  3910. There are many different ways to compute V(N,K), including precomputed tables and direct
  3911. use of the recursive formulation. The reference implementation applies the recursive
  3912. formulation one line (or column) at a time to save on memory use,
  3913. along with an alternate,
  3914. univariate recurrence to initialize an arbitrary line, and direct
  3915. polynomial solutions for small N. All of these methods are
  3916. equivalent, and have different trade-offs in speed, memory usage, and
  3917. code size. Implementations MAY use any methods they like, as long as
  3918. they are equivalent to the mathematical definition.
  3919. </t>
  3920. <t>
  3921. The decoding of the codeword from the index is performed as specified in
  3922. <xref target="PVQ"></xref>, as implemented in function
  3923. decode_pulses() (cwrs.c).
  3924. </t>
  3925. </section>
  3926. <section anchor="spreading" title="Spreading">
  3927. <t>
  3928. </t>
  3929. </section>
  3930. <section anchor="split" title="Split decoding">
  3931. <t>
  3932. To avoid the need for multi-precision calculations when decoding PVQ codevectors,
  3933. the maximum size allowed for codebooks is 32 bits. When larger codebooks are
  3934. needed, the vector is instead split in two sub-vectors of size N/2.
  3935. A quantized gain parameter with precision
  3936. derived from the current allocation is entropy coded to represent the relative
  3937. gains of each side of the split, and the entire decoding process is recursively
  3938. applied. Multiple levels of splitting may be applied up to a frame size
  3939. dependent limit. The same recursive mechanism is applied for the joint coding
  3940. of stereo audio.
  3941. </t>
  3942. </section>
  3943. <section anchor="tf-change" title="Time-Frequency change">
  3944. <t>
  3945. </t>
  3946. </section>
  3947. </section>
  3948. <section anchor="anti-collapse" title="Anti-collapse processing">
  3949. <t>
  3950. When the frame has the transient bit set, an anti-collapse bit is decoded.
  3951. When anti-collapse is set, the energy in each small MDCT is prevented
  3952. from collapsing to zero. For each band of each MDCT where a collapse is
  3953. detected, a pseudo-random signal is inserted with an energy corresponding
  3954. to the min energy over the two previous frames. A renormalization step is
  3955. then required to ensure that the anti-collapse step did not alter the
  3956. energy preservation property.
  3957. </t>
  3958. </section>
  3959. <section anchor="denormalization" title="Denormalization">
  3960. <t>
  3961. Just like each band was normalized in the encoder, the last step of the decoder before
  3962. the inverse MDCT is to denormalize the bands. Each decoded normalized band is
  3963. multiplied by the square root of the decoded energy. This is done by denormalise_bands()
  3964. (bands.c).
  3965. </t>
  3966. </section>
  3967. <section anchor="inverse-mdct" title="Inverse MDCT">
  3968. <t>The inverse MDCT implementation has no special characteristics. The
  3969. input is N frequency-domain samples and the output is 2*N time-domain
  3970. samples, while scaling by 1/2. The output is windowed using the same window
  3971. as the encoder. The IMDCT and windowing are performed by mdct_backward
  3972. (mdct.c). If a time-domain pre-emphasis
  3973. window was applied in the encoder, the (inverse) time-domain de-emphasis window
  3974. is applied on the IMDCT result.
  3975. </t>
  3976. <section anchor="post-filter" title="Post-filter">
  3977. <t>
  3978. The output of the inverse MDCT (after weighted overlap-add) is sent to the
  3979. post-filter. Although the post-filter is applied at the end, the post-filter
  3980. parameters are encoded at the beginning, just after the silence flag.
  3981. The post-filter can be switched on or off using one bit (logp=1).
  3982. If the post-filter is enabled, then the octave is decoded as an integer value
  3983. between 0 and 6 of uniform probability. Once the octave is known, the fine pitch
  3984. within the octave is decoded using 4+octave raw bits. The final pitch period
  3985. is equal to (16&lt;&lt;octave)+fine_pitch-1 so it is bounded between 15 and 1022,
  3986. inclusively. Next, the gain is decoded as three raw bits and is equal to
  3987. G=3*(int_gain+1)/32. The set of post-filter taps is decoded last, using
  3988. a pdf equal to {2, 1, 1}/4. Tapset zero corresponds to the filter coefficients
  3989. g0 = 0.3066406250, g1 = 0.2170410156, g2 = 0.1296386719. Tapset one
  3990. corresponds to the filter coefficients g0 = 0.4638671875, g1 = 0.2680664062,
  3991. g2 = 0, and tapset two uses filter coefficients g0 = 0.7998046875,
  3992. g1 = 0.1000976562, g2 = 0.
  3993. </t>
  3994. <t>
  3995. The post-filter response is thus computed as:
  3996. <figure align="center">
  3997. <artwork align="center">
  3998. <![CDATA[
  3999. y(n) = x(n) + G*(g0*y(n-T) + g1*(y(n-T+1)+y(n-T+1))
  4000. + g2*(y(n-T+2)+y(n-T+2)))
  4001. ]]>
  4002. </artwork>
  4003. </figure>
  4004. During a transition between different gains, a smooth transition is calculated
  4005. using the square of the MDCT window. It is important that values of y(n) be
  4006. interpolated one at a time such that the past value of y(n) used is interpolated.
  4007. </t>
  4008. </section>
  4009. <section anchor="deemphasis" title="De-emphasis">
  4010. <t>
  4011. After the post-filter,
  4012. the signal is de-emphasized using the inverse of the pre-emphasis filter
  4013. used in the encoder:
  4014. <figure align="center">
  4015. <artwork align="center"><![CDATA[
  4016. 1 1
  4017. ---- = --------------- ,
  4018. A(z) -1
  4019. 1 - alpha_p*z
  4020. ]]></artwork>
  4021. </figure>
  4022. where alpha_p=0.8500061035.
  4023. </t>
  4024. </section>
  4025. </section>
  4026. <section anchor="Packet Loss Concealment" title="Packet Loss Concealment (PLC)">
  4027. <t>
  4028. Packet loss concealment (PLC) is an optional decoder-side feature which
  4029. SHOULD be included when transmitting over an unreliable channel. Because
  4030. PLC is not part of the bitstream, there are several possible ways to
  4031. implement PLC with different complexity/quality trade-offs. The PLC in
  4032. the reference implementation finds a periodicity in the decoded
  4033. signal and repeats the windowed waveform using the pitch offset. The windowed
  4034. waveform is overlapped in such a way as to preserve the time-domain aliasing
  4035. cancellation with the previous frame and the next frame. This is implemented
  4036. in celt_decode_lost() (mdct.c).
  4037. </t>
  4038. </section>
  4039. </section>
  4040. <section anchor="switching" title="Mode Switching">
  4041. <t>
  4042. Switching between the Opus coding modes requires careful consideration. More
  4043. specifically, the transitions that cannot be easily handled are the ones where
  4044. the lower frequencies have to switch between the SILK LP-based model and the CELT
  4045. transform model. If nothing is done, a glitch will occur for these transitions.
  4046. On the other hand, switching between the SILK-only modes and the hybrid mode
  4047. does not require any special treatment.
  4048. </t>
  4049. <t>
  4050. There are two ways to avoid or reduce glitches during the problematic mode
  4051. transitions: with side information or without it. Only transitions with side
  4052. information are normatively specified. For transitions with no side
  4053. information, it is RECOMMENDED for the decoder to use a concealment technique
  4054. (e.g. make use of the PLC algorithm) to "fill in"
  4055. the gap or discontinuity caused by the mode transition. Note that this
  4056. concealment MUST NOT be applied when switching between the SILK mode and the
  4057. hybrid mode or vice versa. Similarly, it MUST NOT be applied when merely
  4058. changing the bandwidth within the same mode.
  4059. </t>
  4060. <section anchor="side-info" title="Switching Side Information">
  4061. <t>
  4062. Switching with side information involves transmitting in-band a 5-ms
  4063. "redundant" CELT frame within the Opus frame.
  4064. This frame is designed to fill in the gap or discontinuity without requiring
  4065. the decoder to conceal it. For transitions from a CELT-only frame to a
  4066. SILK-only or hybrid frame, the redundant frame is inserted in the frame
  4067. following the transition (i.e. the SILK-only/hybrid frame). For transitions
  4068. from a SILK-only/hybrid frame to a CELT-only frame, the redundant frame is
  4069. inserted in the first frame. For all SILK-only and hybrid frames (not only
  4070. those involved in a mode transition), a binary symbol of probability 2^-12
  4071. needs to be decoded just after the SILK part of the bitstream. When the
  4072. symbol value is 1, the frame then includes an embedded redundant frame. The
  4073. redundant frame always starts and ends on a byte boundary. For SILK-only
  4074. frames, the number of bytes is simply the number of whole remaining bytes.
  4075. For hybrid frames, the number of bytes is equal to 2, plus a decoded unsigned
  4076. integer (ec_dec_uint()) between 0 and 255. For hybrid frames, the redundant
  4077. frame is placed at the end of the frame, after the CELT layer of the
  4078. hybrid frame. The redundant frame is decoded like any other CELT-only frame,
  4079. with the exception that it does not contain a TOC byte. The bandwidth
  4080. is instead set to the same bandwidth of the current frame (for MB
  4081. frames, the redundant frame is set to WB).
  4082. </t>
  4083. <t>
  4084. For CELT-only to SILK-only/hybrid transitions, the first
  4085. 2.5 ms of the redundant frame is used as-is for the reconstructed
  4086. output. The remaining 2.5 ms is overlapped and added (cross-faded using
  4087. the square of the MDCT power-complementary window) to the decoded SILK/hybrid
  4088. signal, ensuring a smooth transition. For SILK-only/hyrid to CELT-only
  4089. transitions, only the second half of the 5-ms decoded redundant frame is used.
  4090. In that case, only a 2.5-ms cross-fade is applied, still using the
  4091. power-complementary window.
  4092. </t>
  4093. </section>
  4094. </section>
  4095. </section>
  4096. <!-- ******************************************************************* -->
  4097. <!-- ************************** OPUS ENCODER *********************** -->
  4098. <!-- ******************************************************************* -->
  4099. <section title="Codec Encoder">
  4100. <t>
  4101. Opus encoder block diagram.
  4102. <figure>
  4103. <artwork>
  4104. <![CDATA[
  4105. +----------+ +-------+
  4106. | sample | | SILK |
  4107. +->| rate |--->|encoder|--+
  4108. | |conversion| | | |
  4109. audio | +----------+ +-------+ | +-------+
  4110. ------+ +--->| Range |
  4111. | +-------+ |encoder|---->
  4112. | | CELT | +--->| | bitstream
  4113. +->|encoder|------------------+ +-------+
  4114. | |
  4115. +-------+
  4116. ]]>
  4117. </artwork>
  4118. </figure>
  4119. </t>
  4120. <section anchor="range-encoder" title="Range Coder">
  4121. <t>
  4122. The range coder also acts as the bit-packer for Opus. It is
  4123. used in three different ways, to encode:
  4124. <list style="symbols">
  4125. <t>entropy-coded symbols with a fixed probability model using ec_encode(), (entenc.c)</t>
  4126. <t>integers from 0 to 2**M-1 using ec_enc_uint() or ec_enc_bits(), (entenc.c)</t>
  4127. <t>integers from 0 to N-1 (where N is not a power of two) using ec_enc_uint(). (entenc.c)</t>
  4128. </list>
  4129. </t>
  4130. <t>
  4131. The range encoder maintains an internal state vector composed of the
  4132. four-tuple (low,rng,rem,ext) representing the low end of the current
  4133. range, the size of the current range, a single buffered output octet,
  4134. and a count of additional carry-propagating output octets. Both rng
  4135. and low are 32-bit unsigned integer values, rem is an octet value or
  4136. the special value -1, and ext is an integer with at least 16 bits.
  4137. This state vector is initialized at the start of each each frame to
  4138. the value (0,2**31,-1,0). The reference implementation re-uses the
  4139. 'val' field of the entropy coder structure to hold low, in order to
  4140. allow the same structure to be used for encoding and decoding, but
  4141. we maintain the distinction here for clarity.
  4142. </t>
  4143. <section anchor="encoding-symbols" title="Encoding Symbols">
  4144. <t>
  4145. The main encoding function is ec_encode() (entenc.c),
  4146. which takes as an argument a three-tuple (fl,fh,ft)
  4147. describing the range of the symbol to be encoded in the current
  4148. context, with 0 &lt;= fl &lt; fh &lt;= ft &lt;= 65535. The values of this tuple
  4149. are derived from the probability model for the symbol. Let f(i) be
  4150. the frequency of the i'th symbol in the current context. Then the
  4151. three-tuple corresponding to the k'th symbol is given by
  4152. <![CDATA[
  4153. fl=sum(f(i),i<k), fh=fl+f(i), and ft=sum(f(i)).
  4154. ]]>
  4155. </t>
  4156. <t>
  4157. ec_encode() updates the state of the encoder as follows. If fl is
  4158. greater than zero, then low = low + rng - (rng/ft)*(ft-fl) and
  4159. rng = (rng/ft)*(fh-fl). Otherwise, low is unchanged and
  4160. rng = rng - (rng/ft)*(fh-fl). The divisions here are exact integer
  4161. division. After this update, the range is normalized.
  4162. </t>
  4163. <t>
  4164. To normalize the range, the following process is repeated until
  4165. rng &gt; 2**23. First, the top 9 bits of low, (low&gt;&gt;23), are placed into
  4166. a carry buffer. Then, low is set to <![CDATA[(low << 8 & 0x7FFFFFFF) and rng
  4167. is set to (rng<<8)]]>. This process is carried out by
  4168. ec_enc_normalize() (entenc.c).
  4169. </t>
  4170. <t>
  4171. The 9 bits produced in each iteration of the normalization loop
  4172. consist of 8 data bits and a carry flag. The final value of the
  4173. output bits is not determined until carry propagation is accounted
  4174. for. Therefore the reference implementation buffers a single
  4175. (non-propagating) output octet and keeps a count of additional
  4176. propagating (0xFF) output octets. An implementation MAY choose to use
  4177. any mathematically equivalent scheme to perform carry propagation.
  4178. </t>
  4179. <t>
  4180. The function ec_enc_carry_out() (entenc.c) performs
  4181. this buffering. It takes a 9-bit input value, c, from the normalization:
  4182. 8 bits of output and a carry bit. If c is 0xFF, then ext is incremented
  4183. and no octets are output. Otherwise, if rem is not the special value
  4184. -1, then the octet (rem+(c>>8)) is output. Then ext octets are output
  4185. with the value 0 if the carry bit is set, or 0xFF if it is not, and
  4186. rem is set to the lower 8 bits of c. After this, ext is set to zero.
  4187. </t>
  4188. <t>
  4189. In the reference implementation, a special version of ec_encode()
  4190. called ec_encode_bin() (entenc.c) is defined to
  4191. take a two-tuple (fl,ftb), where <![CDATA[0 <= fl < 2**ftb and ftb < 16. It is
  4192. mathematically equivalent to calling ec_encode() with the three-tuple
  4193. (fl,fl+1,1<<ftb)]]>, but avoids using division.
  4194. </t>
  4195. </section>
  4196. <section anchor="encoding-bits" title="Encoding Raw Bits">
  4197. <t>
  4198. The CELT layer also allows directly encoding a series of raw bits, outside
  4199. of the range coder, implemented in ec_enc_bits() (entenc.c).
  4200. The raw bits are packed at the end of the packet, starting by storing the
  4201. least significant bit of the value to be packed in the least significant bit
  4202. of the last byte, filling up to the most significant bit in
  4203. the last byte, and then continuing in the least significant bit of the
  4204. penultimate byte, and so on.
  4205. This packing may continue into the last byte output by the range coder,
  4206. though the format should render it impossible to overwrite any set bit
  4207. produced by the range coder when the procedure in
  4208. <xref target='encoder-finalizing'/> is followed to finalize the stream.
  4209. </t>
  4210. </section>
  4211. <section anchor="encoding-ints" title="Encoding Uniformly Distributed Integers">
  4212. <t>
  4213. The function ec_enc_uint() is based on ec_encode() and encodes one of N
  4214. equiprobable symbols, each with a frequency of 1, where N may be as large as
  4215. 2**32-1. Because ec_encode() is limited to a total frequency of 2**16-1, this
  4216. is done by encoding a series of symbols in smaller contexts.
  4217. </t>
  4218. <t>
  4219. ec_enc_uint() (entenc.c) takes a two-tuple (fl,ft),
  4220. where ft is not necessarily a power of two. Let ftb be the location
  4221. of the highest 1 bit in the two's-complement representation of
  4222. (ft-1), or -1 if no bits are set. If ftb>8, then the top 8 bits of fl
  4223. are encoded using ec_encode() with the three-tuple
  4224. (fl>>ftb-8,(fl>>ftb-8)+1,(ft-1>>ftb-8)+1), and the remaining bits
  4225. are encoded as raw bits. Otherwise, fl is encoded with ec_encode() directly
  4226. using the three-tuple (fl,fl+1,ft).
  4227. </t>
  4228. </section>
  4229. <section anchor="encoder-finalizing" title="Finalizing the Stream">
  4230. <t>
  4231. After all symbols are encoded, the stream must be finalized by
  4232. outputting a value inside the current range. Let end be the integer
  4233. in the interval [low,low+rng) with the largest number of trailing
  4234. zero bits, b, such that end+(1&lt;&lt;b)-1 is also in the interval
  4235. [low,low+rng). Then while end is not zero, the top 9 bits of end, e.g.,
  4236. <![CDATA[(end>>23), are sent to the carry buffer, and end is replaced by
  4237. (end<<8&0x7FFFFFFF). Finally, if the value in carry buffer, rem, is]]>
  4238. neither zero nor the special value -1, or the carry count, ext, is
  4239. greater than zero, then 9 zero bits are sent to the carry buffer.
  4240. After the carry buffer is finished outputting octets, the rest of the
  4241. output buffer (if any) is padded with zero bits, until it reaches the raw
  4242. bits. Finally, rem is set to the
  4243. special value -1. This process is implemented by ec_enc_done()
  4244. (entenc.c).
  4245. </t>
  4246. </section>
  4247. <section anchor="encoder-tell" title="Current Bit Usage">
  4248. <t>
  4249. The bit allocation routines in Opus need to be able to determine a
  4250. conservative upper bound on the number of bits that have been used
  4251. to encode the current frame thus far. This drives allocation
  4252. decisions and ensures that the range coder and raw bits will not
  4253. overflow the output buffer. This is computed in the
  4254. reference implementation to whole-bit precision by
  4255. the function ec_tell() (entcode.h) and to fractional 1/8th bit
  4256. precision by the function ec_tell_frac() (entcode.c).
  4257. Like all operations in the range coder, it must be implemented in a
  4258. bit-exact manner, and must produce exactly the same value returned by
  4259. the same functions in the decoder after decoding the same symbols.
  4260. </t>
  4261. </section>
  4262. </section>
  4263. <section title='SILK Encoder'>
  4264. <t>
  4265. 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" />.
  4266. </t>
  4267. <figure align="center" anchor="encoder_figure">
  4268. <artwork align="center">
  4269. <![CDATA[
  4270. +---+
  4271. +----------------------------->| |
  4272. +---------+ | +---------+ | |
  4273. |Voice | | |LTP | | |
  4274. +----->|Activity |-----+ +---->|Scaling |---------+--->| |
  4275. | |Detector | 3 | | |Control |<+ 12 | | |
  4276. | +---------+ | | +---------+ | | | |
  4277. | | | +---------+ | | | |
  4278. | | | |Gains | | 11 | | |
  4279. | | | +->|Processor|-|---+---|--->| R |
  4280. | | | | | | | | | | a |
  4281. | \/ | | +---------+ | | | | n |
  4282. | +---------+ | | +---------+ | | | | g |
  4283. | |Pitch | | | |LSF | | | | | e |
  4284. | +->|Analysis |-+ | |Quantizer|-|---|---|--->| |
  4285. | | | |4| | | | | 8 | | | E |->
  4286. | | +---------+ | | +---------+ | | | | n |14
  4287. | | | | 9/\ 10| | | | | c |
  4288. | | | | | \/ | | | | o |
  4289. | | +---------+ | | +----------+| | | | d |
  4290. | | |Noise | +--|->|Prediction|+---|---|--->| e |
  4291. | +->|Shaping |-|--+ |Analysis || 7 | | | r |
  4292. | | |Analysis |5| | | || | | | |
  4293. | | +---------+ | | +----------+| | | | |
  4294. | | | | /\ | | | | |
  4295. | | +---------|--|-------+ | | | | |
  4296. | | | \/ \/ \/ \/ \/ | |
  4297. | +---------+ | | +---------+ +------------+ | |
  4298. | |High-Pass| | | | | |Noise | | |
  4299. -+->|Filter |-+----+----->|Prefilter|------>|Shaping |->| |
  4300. 1 | | 2 | | 6 |Quantization|13| |
  4301. +---------+ +---------+ +------------+ +---+
  4302. 1: Input speech signal
  4303. 2: High passed input signal
  4304. 3: Voice activity estimate
  4305. 4: Pitch lags (per 5 ms) and voicing decision (per 20 ms)
  4306. 5: Noise shaping quantization coefficients
  4307. - Short term synthesis and analysis
  4308. noise shaping coefficients (per 5 ms)
  4309. - Long term synthesis and analysis noise
  4310. shaping coefficients (per 5 ms and for voiced speech only)
  4311. - Noise shaping tilt (per 5 ms)
  4312. - Quantizer gain/step size (per 5 ms)
  4313. 6: Input signal filtered with analysis noise shaping filters
  4314. 7: Short and long term prediction coefficients
  4315. LTP (per 5 ms) and LPC (per 20 ms)
  4316. 8: LSF quantization indices
  4317. 9: LSF coefficients
  4318. 10: Quantized LSF coefficients
  4319. 11: Processed gains, and synthesis noise shape coefficients
  4320. 12: LTP state scaling coefficient. Controlling error propagation
  4321. / prediction gain trade-off
  4322. 13: Quantized signal
  4323. 14: Range encoded bitstream
  4324. ]]>
  4325. </artwork>
  4326. <postamble>Encoder block diagram.</postamble>
  4327. </figure>
  4328. <section title='Voice Activity Detection'>
  4329. <t>
  4330. The input signal is processed by a VAD (Voice Activity Detector) 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 MA (Moving Average) 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 an SNR (Signal-to-Noise Ratio) 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:
  4331. <list style="symbols">
  4332. <t>
  4333. Average SNR. The average of the subband SNR values.
  4334. </t>
  4335. <t>
  4336. Smoothed subband SNRs. Temporally smoothed subband SNR values.
  4337. </t>
  4338. <t>
  4339. Speech activity level. Based on the average SNR and a weighted average of the subband energies.
  4340. </t>
  4341. <t>
  4342. Spectral tilt. A weighted average of the subband SNRs, with positive weights for the low subbands and negative weights for the high subbands.
  4343. </t>
  4344. </list>
  4345. </t>
  4346. </section>
  4347. <section title='High-Pass Filter'>
  4348. <t>
  4349. 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 ARMA (Auto Regressive Moving Average) filter with a cut-off frequency around 70&nbsp;Hz.
  4350. </t>
  4351. <t>
  4352. 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.
  4353. </t>
  4354. </section>
  4355. <section title='Pitch Analysis' anchor='pitch_estimator_overview_section'>
  4356. <t>
  4357. The high-passed input signal is processed by the open loop pitch estimator shown in <xref target='pitch_estimator_figure' />.
  4358. <figure align="center" anchor="pitch_estimator_figure">
  4359. <artwork align="center">
  4360. <![CDATA[
  4361. +--------+ +----------+
  4362. |2 x Down| |Time- |
  4363. +->|sampling|->|Correlator| |
  4364. | | | | | |4
  4365. | +--------+ +----------+ \/
  4366. | | 2 +-------+
  4367. | | +-->|Speech |5
  4368. +---------+ +--------+ | \/ | |Type |->
  4369. |LPC | |Down | | +----------+ | |
  4370. +->|Analysis | +->|sample |-+------------->|Time- | +-------+
  4371. | | | | |to 8 kHz| |Correlator|----------->
  4372. | +---------+ | +--------+ |__________| 6
  4373. | | | |3
  4374. | \/ | \/
  4375. | +---------+ | +----------+
  4376. | |Whitening| | |Time- |
  4377. -+->|Filter |-+--------------------------->|Correlator|----------->
  4378. 1 | | | | 7
  4379. +---------+ +----------+
  4380. 1: Input signal
  4381. 2: Lag candidates from stage 1
  4382. 3: Lag candidates from stage 2
  4383. 4: Correlation threshold
  4384. 5: Voiced/unvoiced flag
  4385. 6: Pitch correlation
  4386. 7: Pitch lags
  4387. ]]>
  4388. </artwork>
  4389. <postamble>Block diagram of the pitch estimator.</postamble>
  4390. </figure>
  4391. 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:
  4392. <list style="symbols">
  4393. <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>
  4394. <t>
  4395. 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:
  4396. <list style="symbols">
  4397. <t>
  4398. Whether the previous frame was classified as voiced
  4399. </t>
  4400. <t>
  4401. The speech activity level
  4402. </t>
  4403. <t>
  4404. The spectral tilt.
  4405. </t>
  4406. </list>
  4407. 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.
  4408. </t>
  4409. <t>
  4410. 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.
  4411. </t>
  4412. </list>
  4413. </t>
  4414. </section>
  4415. <section title='Noise Shaping Analysis' anchor='noise_shaping_analysis_overview_section'>
  4416. <t>
  4417. 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:
  4418. <list style="symbols">
  4419. <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>
  4420. <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>
  4421. <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>
  4422. <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>
  4423. </list>
  4424. </t>
  4425. <t>
  4426. <figure align="center" anchor="noise_shape_analysis_spectra_figure">
  4427. <artwork align="center">
  4428. <![CDATA[
  4429. / \ ___
  4430. | // \\
  4431. | // \\ ____
  4432. |_// \\___// \\ ____
  4433. | / ___ \ / \\ // \\
  4434. P |/ / \ \_/ \\_____// \\
  4435. o | / \ ____ \ / \\
  4436. w | / \___/ \ \___/ ____ \\___ 1
  4437. e |/ \ / \ \
  4438. r | \_____/ \ \__ 2
  4439. | \
  4440. | \___ 3
  4441. |
  4442. +---------------------------------------->
  4443. Frequency
  4444. 1: Input signal spectrum
  4445. 2: De-emphasized and level matched spectrum
  4446. 3: Quantization noise spectrum
  4447. ]]>
  4448. </artwork>
  4449. <postamble>Noise shaping and spectral de-emphasis illustration.</postamble>
  4450. </figure>
  4451. <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.
  4452. </t>
  4453. <t>
  4454. The transformation from input signal to de-emphasized signal can be described as a filtering operation with a filter
  4455. <figure align="center">
  4456. <artwork align="center">
  4457. <![CDATA[
  4458. -1 Wana(z)
  4459. H(z) = G * ( 1 - c_tilt * z ) * -------
  4460. Wsyn(z),
  4461. ]]>
  4462. </artwork>
  4463. </figure>
  4464. having an adjustment gain G, a first order tilt adjustment filter with
  4465. tilt coefficient c_tilt, and where
  4466. <figure align="center">
  4467. <artwork align="center">
  4468. <![CDATA[
  4469. 16 d
  4470. __ -k -L __ -k
  4471. Wana(z) = (1 - \ (a_ana(k) * z )*(1 - z * \ b_ana(k) * z ),
  4472. /_ /_
  4473. k=1 k=-d
  4474. ]]>
  4475. </artwork>
  4476. </figure>
  4477. 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.
  4478. </t>
  4479. <t>
  4480. Similarly, but without the tilt adjustment, the synthesis part can be written as
  4481. <figure align="center">
  4482. <artwork align="center">
  4483. <![CDATA[
  4484. 16 d
  4485. __ -k -L __ -k
  4486. Wsyn(z) = (1 - \ (a_syn(k) * z )*(1 - z * \ b_syn(k) * z ).
  4487. /_ /_
  4488. k=1 k=-d
  4489. ]]>
  4490. </artwork>
  4491. </figure>
  4492. </t>
  4493. <t>
  4494. 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.
  4495. </t>
  4496. <t>
  4497. 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
  4498. <figure align="center">
  4499. <artwork align="center">
  4500. <![CDATA[
  4501. k
  4502. a_ana(k) = a(k)*g_ana , and
  4503. k
  4504. a_syn(k) = a(k)*g_syn ,
  4505. ]]>
  4506. </artwork>
  4507. </figure>
  4508. where a(k) is the k'th LPC coefficient, and the bandwidth expansion factors g_ana and g_syn are calculated as
  4509. <figure align="center">
  4510. <artwork align="center">
  4511. <![CDATA[
  4512. g_ana = 0.94 - 0.02*C, and
  4513. g_syn = 0.94 + 0.02*C,
  4514. ]]>
  4515. </artwork>
  4516. </figure>
  4517. 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.
  4518. </t>
  4519. <t>
  4520. The long-term shaping is applied only during voiced frames. It uses three filter taps, described by
  4521. <figure align="center">
  4522. <artwork align="center">
  4523. <![CDATA[
  4524. b_ana = F_ana * [0.25, 0.5, 0.25], and
  4525. b_syn = F_syn * [0.25, 0.5, 0.25].
  4526. ]]>
  4527. </artwork>
  4528. </figure>
  4529. 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.
  4530. </t>
  4531. <t>
  4532. The tilt coefficient c_tilt is for unvoiced frames chosen as
  4533. <figure align="center">
  4534. <artwork align="center">
  4535. <![CDATA[
  4536. c_tilt = 0.4, and as
  4537. c_tilt = 0.04 + 0.06 * C
  4538. ]]>
  4539. </artwork>
  4540. </figure>
  4541. for voiced frames, where C again is the coding quality control parameter and is between 0 and 1.
  4542. </t>
  4543. <t>
  4544. 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
  4545. <figure align="center">
  4546. <artwork align="center">
  4547. <![CDATA[
  4548. K
  4549. ___ 2 -0.5
  4550. predGain = ( | | 1 - (r_k) ) ,
  4551. k=1
  4552. ]]>
  4553. </artwork>
  4554. </figure>
  4555. where r_k is the k'th reflection coefficient.
  4556. </t>
  4557. <t>
  4558. 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.
  4559. </t>
  4560. </section>
  4561. <section title='Prefilter'>
  4562. <t>
  4563. 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.
  4564. </t>
  4565. </section>
  4566. <section title='Prediction Analysis' anchor='pred_ana_overview_section'>
  4567. <t>
  4568. 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'/>).
  4569. </t>
  4570. <section title='Voiced Speech' anchor='pred_ana_voiced_overview_section'>
  4571. <t>
  4572. 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 to LPC coefficients. By using these quantized 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.
  4573. </t>
  4574. </section>
  4575. <section title='Unvoiced Speech' anchor='pred_ana_unvoiced_overview_section'>
  4576. <t>
  4577. 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.
  4578. </t>
  4579. </section>
  4580. </section>
  4581. <section title='LSF Quantization' anchor='lsf_quantizer_overview_section'>
  4582. <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>
  4583. <section title='Rate-Distortion Optimization'>
  4584. <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>
  4585. </section>
  4586. <section title='Error Mapping' anchor='lsf_error_mapping_overview_section'>
  4587. <t>
  4588. 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" />).
  4589. Consequently, we solve the following minimization problem, i.e.,
  4590. <figure align="center">
  4591. <artwork align="center">
  4592. <![CDATA[
  4593. LSF_q = argmin { (LSF - c)' * W * (LSF - c) + mu * rate },
  4594. c in C
  4595. ]]>
  4596. </artwork>
  4597. </figure>
  4598. 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.
  4599. </t>
  4600. </section>
  4601. <section title='Multi-Stage Vector Codebook'>
  4602. <t>
  4603. We arrange the codebook in a multiple-stage structure to achieve a quantizer that is both memory efficient and highly scalable in terms of computational complexity (see, e.g., <xref target="sinervo-norsig"/>). In the first stage the input is the LSF vector to be quantized, and in any other stage s > 1, the input is the quantization error from the previous stage (see <xref target='lsf_quantizer_structure_overview_figure'/>).
  4604. </t>
  4605. <figure align="center" anchor="lsf_quantizer_structure_overview_figure">
  4606. <artwork align="center">
  4607. <![CDATA[
  4608. Stage 1: Stage 2: Stage S:
  4609. +----------+ +----------+ +----------+
  4610. | c_{1,1} | | c_{2,1} | | c_{S,1} |
  4611. LSF +----------+ res_1 +----------+ res_{S-1} +----------+
  4612. --->| c_{1,2} |------>| c_{2,2} |--> ... --->| c_{S,2} |--->
  4613. +----------+ +----------+ +----------+ res_S =
  4614. ... ... ... LSF-LSF_q
  4615. +----------+ +----------+ +----------+
  4616. |c_{1,M1-1}| |c_{2,M2-1}| |c_{S,MS-1}|
  4617. +----------+ +----------+ +----------+
  4618. | c_{1,M1} | | c_{2,M2} | | c_{S,MS} |
  4619. +----------+ +----------+ +----------+
  4620. ]]>
  4621. </artwork>
  4622. <postamble>Multi-Stage LSF Vector Codebook Structure.</postamble>
  4623. </figure>
  4624. <t>
  4625. By storing a total of M codebook vectors, i.e.,
  4626. <figure align="center">
  4627. <artwork align="center">
  4628. <![CDATA[
  4629. S
  4630. __
  4631. M = \ Ms,
  4632. /_
  4633. s=1
  4634. ]]>
  4635. </artwork>
  4636. </figure>
  4637. where M_s is the number of vectors in stage s, we obtain a total of
  4638. <figure align="center">
  4639. <artwork align="center">
  4640. <![CDATA[
  4641. S
  4642. ___
  4643. T = | | Ms
  4644. s=1
  4645. ]]>
  4646. </artwork>
  4647. </figure>
  4648. possible combinations for generating the quantized vector. It is, for example, possible to represent 2**36 uniquely combined vectors using only 216 vectors in memory, as is done in SILK for voiced speech at all sample frequencies above 8&nbsp;kHz.
  4649. </t>
  4650. </section>
  4651. <section title='Survivor Based Codebook Search'>
  4652. <t>
  4653. 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'/>).
  4654. </t>
  4655. </section>
  4656. <section title='LSF Stabilization' anchor='lsf_stabilizer_overview_section'>
  4657. <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>
  4658. </section>
  4659. <section title='Off-Line Codebook Training'>
  4660. <t>
  4661. 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.
  4662. </t>
  4663. </section>
  4664. </section>
  4665. <section title='LTP Quantization' anchor='ltp_quantizer_overview_section'>
  4666. <t>
  4667. 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
  4668. <figure align="center">
  4669. <artwork align="center">
  4670. <![CDATA[
  4671. RD = u * (b - cb_i)' * W_ltp * (b - cb_i) + r_i,
  4672. ]]>
  4673. </artwork>
  4674. </figure>
  4675. 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.
  4676. 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.
  4677. </t>
  4678. <t>
  4679. 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.
  4680. </t>
  4681. </section>
  4682. <section title='Noise Shaping Quantizer'>
  4683. <t>
  4684. The noise shaping quantizer independently shapes the signal and coding noise spectra to obtain a perceptually higher quality at the same bitrate.
  4685. </t>
  4686. <t>
  4687. 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.
  4688. </t>
  4689. </section>
  4690. <section title='Range Encoder'>
  4691. <t>
  4692. Range encoding is a well known method for entropy coding in which a bitstream sequence is continually updated with every new symbol, based on the probability for that symbol. It is similar to arithmetic coding, but rather than being restricted to generating binary output symbols, it can generate symbols in any chosen number base. In SILK all side information is range encoded. Each quantized parameter has its own cumulative density function based on histograms for the quantization indices obtained by running a training database.
  4693. </t>
  4694. <section title='Bitstream Encoding Details'>
  4695. <t>
  4696. TBD.
  4697. </t>
  4698. </section>
  4699. </section>
  4700. </section>
  4701. <section title="CELT Encoder">
  4702. <t>
  4703. Copy from CELT draft.
  4704. </t>
  4705. <section anchor="prefilter" title="Pre-filter">
  4706. <t>
  4707. Inverse of the post-filter
  4708. </t>
  4709. </section>
  4710. <section anchor="forward-mdct" title="Forward MDCT">
  4711. <t>The MDCT implementation has no special characteristics. The
  4712. input is a windowed signal (after pre-emphasis) of 2*N samples and the output is N
  4713. frequency-domain samples. A "low-overlap" window is used to reduce the algorithmic delay.
  4714. It is derived from a basic (full overlap) window that is the same as the one used in the Vorbis codec:
  4715. <figure align="center">
  4716. <artwork align="center"><![CDATA[
  4717. pi pi n + 1/2 2
  4718. W(n) = [sin(-- * sin(-- * -------))] .
  4719. 2 2 L
  4720. ]]></artwork>
  4721. </figure>
  4722. The low-overlap window is created by zero-padding the basic window and inserting ones in the middle, such that the resulting window still satisfies power complementarity. The MDCT is computed in mdct_forward() (mdct.c), which includes the windowing operation and a scaling of 2/N.
  4723. </t>
  4724. </section>
  4725. <section anchor="normalization" title="Bands and Normalization">
  4726. <t>
  4727. The MDCT output is divided into bands that are designed to match the ear's critical
  4728. bands for the smallest (2.5&nbsp;ms) frame size. The larger frame sizes use integer
  4729. multiples of the 2.5&nbsp;ms layout. For each band, the encoder
  4730. computes the energy that will later be encoded. Each band is then normalized by the
  4731. square root of the <spanx style="strong">unquantized</spanx> energy, such that each band now forms a unit vector X.
  4732. The energy and the normalization are computed by compute_band_energies()
  4733. and normalise_bands() (bands.c), respectively.
  4734. </t>
  4735. </section>
  4736. <section anchor="energy-quantization" title="Energy Envelope Quantization">
  4737. <t>
  4738. It is important to quantize the energy with sufficient resolution because
  4739. any energy quantization error cannot be compensated for at a later
  4740. stage. Regardless of the resolution used for encoding the shape of a band,
  4741. it is perceptually important to preserve the energy in each band. CELT uses a
  4742. coarse-fine strategy for encoding the energy in the base-2 log domain,
  4743. as implemented in quant_bands.c</t>
  4744. <section anchor="coarse-energy" title="Coarse energy quantization">
  4745. <t>
  4746. The coarse quantization of the energy uses a fixed resolution of 6 dB.
  4747. To minimize the bitrate, prediction is applied both in time (using the previous frame)
  4748. and in frequency (using the previous bands). The prediction using the
  4749. previous frame can be disabled, creating an "intra" frame where the energy
  4750. is coded without reference to prior frames. An encoder is able to choose the
  4751. mode used at will based on both loss robustness and efficiency
  4752. considerations.
  4753. The 2-D z-transform of
  4754. the prediction filter is:
  4755. <figure align="center">
  4756. <artwork align="center"><![CDATA[
  4757. -1 -1
  4758. (1 - alpha*z_l )*(1 - z_b )
  4759. A(z_l, z_b) = -----------------------------
  4760. -1
  4761. 1 - beta*z_b
  4762. ]]></artwork>
  4763. </figure>
  4764. where b is the band index and l is the frame index. The prediction coefficients
  4765. applied depend on the frame size in use when not using intra energy and are alpha=0, beta=4915/32768
  4766. when using intra energy.
  4767. The time-domain prediction is based on the final fine quantization of the previous
  4768. frame, while the frequency domain (within the current frame) prediction is based
  4769. on coarse quantization only (because the fine quantization has not been computed
  4770. yet). The prediction is clamped internally so that fixed point implementations with
  4771. limited dynamic range do not suffer desynchronization. Identical prediction
  4772. clamping must be implemented in all encoders and decoders.
  4773. We approximate the ideal
  4774. probability distribution of the prediction error using a Laplace distribution
  4775. with separate parameters for each frame size in intra- and inter-frame modes. The
  4776. coarse energy quantization is performed by quant_coarse_energy() and
  4777. quant_coarse_energy() (quant_bands.c). The encoding of the Laplace-distributed values is
  4778. implemented in ec_laplace_encode() (laplace.c).
  4779. </t>
  4780. <!-- FIXME: bit budget consideration -->
  4781. </section> <!-- coarse energy -->
  4782. <section anchor="fine-energy" title="Fine energy quantization">
  4783. <t>
  4784. After the coarse energy quantization and encoding, the bit allocation is computed
  4785. (<xref target="allocation"></xref>) and the number of bits to use for refining the
  4786. energy quantization is determined for each band. Let B_i be the number of fine energy bits
  4787. for band i; the refinement is an integer f in the range [0,2**B_i-1]. The mapping between f
  4788. and the correction applied to the coarse energy is equal to (f+1/2)/2**B_i - 1/2. Fine
  4789. energy quantization is implemented in quant_fine_energy()
  4790. (quant_bands.c).
  4791. </t>
  4792. <t>
  4793. If any bits are unused at the end of the encoding process, these bits are used to
  4794. increase the resolution of the fine energy encoding in some bands. Priority is given
  4795. to the bands for which the allocation (<xref target="allocation"></xref>) was rounded
  4796. down. At the same level of priority, lower bands are encoded first. Refinement bits
  4797. are added until there is no more room for fine energy or until each band
  4798. has gained an additional bit of precision or has the maximum fine
  4799. energy precision. This is implemented in quant_energy_finalise()
  4800. (quant_bands.c).
  4801. </t>
  4802. </section> <!-- fine energy -->
  4803. </section> <!-- Energy quant -->
  4804. <section anchor="pvq" title="Spherical Vector Quantization">
  4805. <t>CELT uses a Pyramid Vector Quantization (PVQ) <xref target="PVQ"></xref>
  4806. codebook for quantizing the details of the spectrum in each band that have not
  4807. been predicted by the pitch predictor. The PVQ codebook consists of all sums
  4808. of K signed pulses in a vector of N samples, where two pulses at the same position
  4809. are required to have the same sign. Thus the codebook includes
  4810. all integer codevectors y of N dimensions that satisfy sum(abs(y(j))) = K.
  4811. </t>
  4812. <t>
  4813. In bands where there are sufficient bits allocated the PVQ is used to encode
  4814. the unit vector that results from the normalization in
  4815. <xref target="normalization"></xref> directly. Given a PVQ codevector y,
  4816. the unit vector X is obtained as X = y/||y||, where ||.|| denotes the
  4817. L2 norm.
  4818. </t>
  4819. <section anchor="pvq-search" title="PVQ Search">
  4820. <t>
  4821. The search for the best codevector y is performed by alg_quant()
  4822. (vq.c). There are several possible approaches to the
  4823. search, with a trade-off between quality and complexity. The method used in the reference
  4824. implementation computes an initial codeword y1 by projecting the residual signal
  4825. R = X - p' onto the codebook pyramid of K-1 pulses:
  4826. </t>
  4827. <t>
  4828. y0 = round_towards_zero( (K-1) * R / sum(abs(R)))
  4829. </t>
  4830. <t>
  4831. Depending on N, K and the input data, the initial codeword y0 may contain from
  4832. 0 to K-1 non-zero values. All the remaining pulses, with the exception of the last one,
  4833. are found iteratively with a greedy search that minimizes the normalized correlation
  4834. between y and R:
  4835. <figure align="center">
  4836. <artwork align="center"><![CDATA[
  4837. T
  4838. J = -R * y / ||y||
  4839. ]]></artwork>
  4840. </figure>
  4841. </t>
  4842. <t>
  4843. The search described above is considered to be a good trade-off between quality
  4844. and computational cost. However, there are other possible ways to search the PVQ
  4845. codebook and the implementers MAY use any other search methods.
  4846. </t>
  4847. </section>
  4848. <section anchor="cwrs-encoding" title="Index Encoding">
  4849. <t>
  4850. The best PVQ codeword is encoded as a uniformly-distributed integer value
  4851. by encode_pulses() (cwrs.c).
  4852. The codeword is converted from a unique index in the same way as specified in
  4853. <xref target="PVQ"></xref>. The indexing is based on the calculation of V(N,K)
  4854. (denoted N(L,K) in <xref target="PVQ"></xref>), which is the number of possible
  4855. combinations of K pulses in N samples.
  4856. </t>
  4857. </section>
  4858. </section>
  4859. <section anchor="stereo" title="Stereo support">
  4860. <t>
  4861. When encoding a stereo stream, some parameters are shared across the left and right channels, while others are transmitted separately for each channel, or jointly encoded. Only one copy of the flags for the features, transients and pitch (pitch
  4862. period and filter parameters) are transmitted. The coarse and fine energy parameters are transmitted separately for each channel. Both the coarse energy and fine energy (including the remaining fine bits at the end of the stream) have the left and right bands interleaved in the stream, with the left band encoded first.
  4863. </t>
  4864. <t>
  4865. The main difference between mono and stereo coding is the PVQ coding of the normalized vectors. In stereo mode, a normalized mid-side (M-S) encoding is used. Let L and R be the normalized vector of a certain band for the left and right channels, respectively. The mid and side vectors are computed as M=L+R and S=L-R and no longer have unit norm.
  4866. </t>
  4867. <t>
  4868. From M and S, an angular parameter theta=2/pi*atan2(||S||, ||M||) is computed. The theta parameter is converted to a Q14 fixed-point parameter itheta, which is quantized on a scale from 0 to 1 with an interval of 2**(-qb), where qb is
  4869. based the number of bits allocated to the band. From here on, the value of itheta MUST be treated in a bit-exact manner since both the encoder and decoder rely on it to infer the bit allocation.
  4870. </t>
  4871. <t>
  4872. Let m=M/||M|| and s=S/||S||; m and s are separately encoded with the PVQ encoder described in <xref target="pvq"></xref>. The number of bits allocated to m and s depends on the value of itheta.
  4873. </t>
  4874. </section>
  4875. <section anchor="synthesis" title="Synthesis">
  4876. <t>
  4877. After all the quantization is completed, the quantized energy is used along with the
  4878. quantized normalized band data to resynthesize the MDCT spectrum. The inverse MDCT (<xref target="inverse-mdct"></xref>) and the weighted overlap-add are applied and the signal is stored in the "synthesis
  4879. buffer".
  4880. The encoder MAY omit this step of the processing if it does not need the decoded output.
  4881. </t>
  4882. </section>
  4883. <section anchor="vbr" title="Variable Bitrate (VBR)">
  4884. <t>
  4885. Each CELT frame can be encoded in a different number of octets, making it possible to vary the bitrate at will. This property can be used to implement source-controlled variable bitrate (VBR). Support for VBR is OPTIONAL for the encoder, but a decoder MUST be prepared to decode a stream that changes its bitrate dynamically. The method used to vary the bitrate in VBR mode is left to the implementer, as long as each frame can be decoded by the reference decoder.
  4886. </t>
  4887. </section>
  4888. </section>
  4889. </section>
  4890. <section title="Conformance">
  4891. <t>
  4892. It is the intention to allow the greatest possible choice of freedom in
  4893. implementing the specification. For this reason, outside of a few exceptions
  4894. noted in this section, conformance is defined through the reference
  4895. implementation of the decoder provided in <xref target="ref-implementation"/>.
  4896. Although this document includes an English description of the codec, should
  4897. the description contradict the source code of the reference implementation,
  4898. the latter shall take precedence.
  4899. </t>
  4900. <t>
  4901. Compliance with this specification means that a decoder's output MUST be
  4902. within the thresholds specified by the opus_compare.c tool in
  4903. <xref target="opus-compare"/> compared to the reference implementation.
  4904. </t>
  4905. <t>
  4906. To complement the Opus specification, the "Opus Custom" codec is defined to
  4907. handle special sampling rates and frame rates that are not supported by the
  4908. main Opus specification. Use of Opus Custom is discouraged for all but very
  4909. special applications for which a frame size different from 2.5, 5, 10, or 20&nbsp;ms is
  4910. needed (for either complexity or latency reasons). Such applications will not
  4911. be compatible with the "main" Opus codec. In Opus Custom operation,
  4912. only the CELT layer is available, which is available using the celt_* function
  4913. calls in celt.h.
  4914. </t>
  4915. </section>
  4916. <section anchor="security" title="Security Considerations">
  4917. <t>
  4918. The codec needs to take appropriate security considerations
  4919. into account, as outlined in <xref target="DOS"/> and <xref target="SECGUIDE"/>.
  4920. It is extremely important for the decoder to be robust against malicious
  4921. payloads.
  4922. Malicious payloads must not cause the decoder to overrun its allocated memory
  4923. or to take an excessive amount of resources to decode.
  4924. Although problems
  4925. in encoders are typically rarer, the same applies to the encoder. Malicious
  4926. audio streams must not cause the encoder to misbehave because this would
  4927. allow an attacker to attack transcoding gateways.
  4928. </t>
  4929. <t>
  4930. The reference implementation contains no known buffer overflow or cases where
  4931. a specially crafted packet or audio segment could cause a significant increase
  4932. in CPU load.
  4933. However, on certain CPU architectures where denormalized floating-point
  4934. operations are much slower than normal floating-point operations, it is
  4935. possible for some audio content (e.g., silence or near-silence) to cause such
  4936. an increase in CPU load.
  4937. Denormals can be introduced by reordering operations in the compiler and depend
  4938. on the target architecture, so it is difficult to guarantee that an implementation
  4939. avoids them.
  4940. For such architectures, it is RECOMMENDED that one add very small
  4941. floating-point offsets to prevent significant numbers of denormalized
  4942. operations or to configure the hardware to treat denormals as zero (DAZ).
  4943. <!--TODO: Add small offsets to what? We should be explicit-->
  4944. No such issue exists for the fixed-point reference implementation.
  4945. </t>
  4946. </section>
  4947. <section title="IANA Considerations ">
  4948. <t>
  4949. This document has no actions for IANA.
  4950. </t>
  4951. </section>
  4952. <section anchor="Acknowledgments" title="Acknowledgments">
  4953. <t>
  4954. Thanks to all other developers, including Raymond Chen, Soeren Skak Jensen, Gregory Maxwell,
  4955. Christopher Montgomery, and Karsten Vandborg Soerensen. We would also
  4956. like to thank Igor Dyakonov and Jan Skoglund for their help with subjective testing of the
  4957. Opus codec. Thanks to John Ridges, Keith Yan, and many others on the Opus and CELT mailing lists
  4958. for their bug reports and feedback, as well as Ralph Giles, Christian Hoene, and
  4959. Kat Walsh, for their feedback on the draft.
  4960. </t>
  4961. </section>
  4962. </middle>
  4963. <back>
  4964. <references title="Informative References">
  4965. <reference anchor='SILK'>
  4966. <front>
  4967. <title>SILK Speech Codec</title>
  4968. <author initials='K.' surname='Vos' fullname='K. Vos'>
  4969. <organization /></author>
  4970. <author initials='S.' surname='Jensen' fullname='S. Jensen'>
  4971. <organization /></author>
  4972. <author initials='K.' surname='Soerensen' fullname='K. Soerensen'>
  4973. <organization /></author>
  4974. <date year='2010' month='March' />
  4975. <abstract>
  4976. <t></t>
  4977. </abstract></front>
  4978. <seriesInfo name='Internet-Draft' value='draft-vos-silk-01' />
  4979. <format type='TXT' target='http://tools.ietf.org/html/draft-vos-silk-01' />
  4980. </reference>
  4981. <reference anchor="laroia-icassp">
  4982. <front>
  4983. <title abbrev="Robust and Efficient Quantization of Speech LSP">
  4984. Robust and Efficient Quantization of Speech LSP Parameters Using Structured Vector Quantization
  4985. </title>
  4986. <author initials="R.L." surname="Laroia" fullname="R.">
  4987. <organization/>
  4988. </author>
  4989. <author initials="N.P." surname="Phamdo" fullname="N.">
  4990. <organization/>
  4991. </author>
  4992. <author initials="N.F." surname="Farvardin" fullname="N.">
  4993. <organization/>
  4994. </author>
  4995. </front>
  4996. <seriesInfo name="ICASSP-1991, Proc. IEEE Int. Conf. Acoust., Speech, Signal Processing, pp. 641-644, October" value="1991"/>
  4997. </reference>
  4998. <reference anchor="sinervo-norsig">
  4999. <front>
  5000. <title abbrev="SVQ versus MSVQ">Evaluation of Split and Multistage Techniques in LSF Quantization</title>
  5001. <author initials="U.S." surname="Sinervo" fullname="Ulpu Sinervo">
  5002. <organization/>
  5003. </author>
  5004. <author initials="J.N." surname="Nurminen" fullname="Jani Nurminen">
  5005. <organization/>
  5006. </author>
  5007. <author initials="A.H." surname="Heikkinen" fullname="Ari Heikkinen">
  5008. <organization/>
  5009. </author>
  5010. <author initials="J.S." surname="Saarinen" fullname="Jukka Saarinen">
  5011. <organization/>
  5012. </author>
  5013. </front>
  5014. <seriesInfo name="NORSIG-2001, Norsk symposium i signalbehandling, Trondheim, Norge, October" value="2001"/>
  5015. </reference>
  5016. <reference anchor="leblanc-tsap">
  5017. <front>
  5018. <title>Efficient Search and Design Procedures for Robust Multi-Stage VQ of LPC Parameters for 4&nbsp;kb/s Speech Coding</title>
  5019. <author initials="W.P." surname="LeBlanc" fullname="">
  5020. <organization/>
  5021. </author>
  5022. <author initials="B." surname="Bhattacharya" fullname="">
  5023. <organization/>
  5024. </author>
  5025. <author initials="S.A." surname="Mahmoud" fullname="">
  5026. <organization/>
  5027. </author>
  5028. <author initials="V." surname="Cuperman" fullname="">
  5029. <organization/>
  5030. </author>
  5031. </front>
  5032. <seriesInfo name="IEEE Transactions on Speech and Audio Processing, Vol. 1, No. 4, October" value="1993" />
  5033. </reference>
  5034. <reference anchor='CELT'>
  5035. <front>
  5036. <title>Constrained-Energy Lapped Transform (CELT) Codec</title>
  5037. <author initials='J-M.' surname='Valin' fullname='J-M. Valin'>
  5038. <organization /></author>
  5039. <author initials='T.' surname='Terriberry' fullname='T. Terriberry'>
  5040. <organization /></author>
  5041. <author initials='G.' surname='Maxwell' fullname='G. Maxwell'>
  5042. <organization /></author>
  5043. <author initials='C.' surname='Montgomery' fullname='C. Montgomery'>
  5044. <organization /></author>
  5045. <date year='2010' month='July' />
  5046. <abstract>
  5047. <t></t>
  5048. </abstract></front>
  5049. <seriesInfo name='Internet-Draft' value='draft-valin-celt-codec-02' />
  5050. <format type='TXT' target='http://tools.ietf.org/html/draft-valin-celt-codec-02' />
  5051. </reference>
  5052. <reference anchor='DOS'>
  5053. <front>
  5054. <title>Internet Denial-of-Service Considerations</title>
  5055. <author initials='M.' surname='Handley' fullname='M. Handley'>
  5056. <organization /></author>
  5057. <author initials='E.' surname='Rescorla' fullname='E. Rescorla'>
  5058. <organization /></author>
  5059. <author>
  5060. <organization>IAB</organization></author>
  5061. <date year='2006' month='December' />
  5062. <abstract>
  5063. <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>
  5064. <seriesInfo name='RFC' value='4732' />
  5065. <format type='TXT' octets='91844' target='ftp://ftp.isi.edu/in-notes/rfc4732.txt' />
  5066. </reference>
  5067. <reference anchor='SECGUIDE'>
  5068. <front>
  5069. <title>Guidelines for Writing RFC Text on Security Considerations</title>
  5070. <author initials='E.' surname='Rescorla' fullname='E. Rescorla'>
  5071. <organization /></author>
  5072. <author initials='B.' surname='Korver' fullname='B. Korver'>
  5073. <organization /></author>
  5074. <date year='2003' month='July' />
  5075. <abstract>
  5076. <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>
  5077. <seriesInfo name='BCP' value='72' />
  5078. <seriesInfo name='RFC' value='3552' />
  5079. <format type='TXT' octets='110393' target='ftp://ftp.isi.edu/in-notes/rfc3552.txt' />
  5080. </reference>
  5081. <reference anchor="range-coding">
  5082. <front>
  5083. <title>Range encoding: An algorithm for removing redundancy from a digitised message</title>
  5084. <author initials="G." surname="Nigel" fullname=""><organization/></author>
  5085. <author initials="N." surname="Martin" fullname=""><organization/></author>
  5086. <date year="1979" />
  5087. </front>
  5088. <seriesInfo name="Proc. Institution of Electronic and Radio Engineers International Conference on Video and Data Recording" value="" />
  5089. </reference>
  5090. <reference anchor="coding-thesis">
  5091. <front>
  5092. <title>Source coding algorithms for fast data compression</title>
  5093. <author initials="R." surname="Pasco" fullname=""><organization/></author>
  5094. <date month="May" year="1976" />
  5095. </front>
  5096. <seriesInfo name="Ph.D. thesis" value="Dept. of Electrical Engineering, Stanford University" />
  5097. </reference>
  5098. <reference anchor="PVQ">
  5099. <front>
  5100. <title>A Pyramid Vector Quantizer</title>
  5101. <author initials="T." surname="Fischer" fullname=""><organization/></author>
  5102. <date month="July" year="1986" />
  5103. </front>
  5104. <seriesInfo name="IEEE Trans. on Information Theory, Vol. 32" value="pp. 568-583" />
  5105. </reference>
  5106. </references>
  5107. <section anchor="ref-implementation" title="Reference Implementation">
  5108. <t>This appendix contains the complete source code for the
  5109. reference implementation of the Opus codec written in C. This
  5110. implementation can be compiled for
  5111. either floating-point or fixed-point architectures.
  5112. </t>
  5113. <t>The implementation can be compiled with either a C89 or a C99
  5114. compiler. It is reasonably optimized for most platforms such that
  5115. only architecture-specific optimizations are likely to be useful.
  5116. The FFT used is a slightly modified version of the KISS-FFT package,
  5117. but it is easy to substitute any other FFT library.
  5118. </t>
  5119. <section title="Extracting the source">
  5120. <t>
  5121. The complete source code can be extracted from this draft, by running the
  5122. following command line:
  5123. <list style="symbols">
  5124. <t><![CDATA[
  5125. cat draft-ietf-codec-opus.txt | grep '^\ \ \ ###' | sed 's/\s\s\s###//' | base64 -d > opus_source.tar.gz
  5126. ]]></t>
  5127. <t>
  5128. tar xzvf opus_source.tar.gz
  5129. </t>
  5130. <t>cd opus_source</t>
  5131. <t>make</t>
  5132. </list>
  5133. </t>
  5134. </section>
  5135. <section title="Development Versions">
  5136. <t>
  5137. The current development version of the source code is available in a
  5138. <eref target='git://git.opus-codec.org/opus.git'>Git repository</eref>.
  5139. Development snapshots are provided at
  5140. <eref target='http://opus-codec.org/'/>.
  5141. </t>
  5142. </section>
  5143. <section title="Base64-encoded source code">
  5144. <t>
  5145. <?rfc include="opus_source.base64"?>
  5146. </t>
  5147. </section>
  5148. </section>
  5149. <section anchor="opus-compare" title="opus_compare.c">
  5150. <t>
  5151. <?rfc include="opus_compare_escaped.c"?>
  5152. </t>
  5153. </section>
  5154. <section anchor="self-delimiting-framing" title="Self-Delimiting Framing">
  5155. <t>
  5156. To use the internal framing described in <xref target="modes"/>, the decoder
  5157. must know the total length of the Opus packet, in bytes.
  5158. This section describes a simple variation of that framing which can be used
  5159. when the total length of the packet is not known.
  5160. Nothing in the encoding of the packet itself allows a decoder to distinguish
  5161. between the regular, undelimited framing and the self-delimiting framing
  5162. described in this appendix.
  5163. Which one is used and where must be established by context at the transport
  5164. layer.
  5165. It is RECOMMENDED that a transport layer choose exactly one framing scheme,
  5166. rather than allowing an encoder to signal which one it wants to use.
  5167. </t>
  5168. <t>
  5169. For example, although a regular Opus stream does not support more than two
  5170. channels, a multi-channel Opus stream may be formed from several one- and
  5171. two-channel streams.
  5172. To pack an Opus packet from each of these streams together in a single packet
  5173. at the transport layer, one could use the self-delimiting framing for all but
  5174. the last stream, and then the regular, undelimited framing for the last one.
  5175. Reverting to the undelimited framing for the last stream saves overhead
  5176. (because the total size of the transport-layer packet will still be known),
  5177. and ensures that a "multi-channel" stream which only has a single Opus stream
  5178. uses the same framing as a regular Opus stream does.
  5179. This avoids the need for signaling to distinguish these two cases.
  5180. </t>
  5181. <t>
  5182. The self-delimiting framing is identical to the regular, undelimited framing
  5183. from <xref target="modes"/>, except that each Opus packet contains one extra
  5184. length field, encoded using the same one- or two-byte scheme from
  5185. <xref target="frame-length-coding"/>.
  5186. This extra length immediately precedes the compressed data of the first Opus
  5187. frame in the packet, and is interpreted in the various modes as follows:
  5188. <list style="symbols">
  5189. <t>
  5190. Code&nbsp;0 packets: It is the length of the single Opus frame (see
  5191. <xref target="sd_code0_packet"/>).
  5192. </t>
  5193. <t>
  5194. Code&nbsp;1 packets: It is the length used for both of the Opus frames (see
  5195. <xref target="sd_code1_packet"/>).
  5196. </t>
  5197. <t>
  5198. Code&nbsp;2 packets: It is the length of the second Opus frame (see
  5199. <xref target="sd_code2_packet"/>).</t>
  5200. <t>
  5201. CBR Code&nbsp;3 packets: It is the length used for all of the Opus frames (see
  5202. <xref target="sd_code3cbr_packet"/>).
  5203. </t>
  5204. <t>VBR Code&nbsp;3 packets: It is the length of the last Opus frame (see
  5205. <xref target="sd_code3vbr_packet"/>).
  5206. </t>
  5207. </list>
  5208. </t>
  5209. <figure anchor="sd_code0_packet" title="A Self-Delimited Code 0 Packet"
  5210. align="center">
  5211. <artwork align="center"><![CDATA[
  5212. 0 1 2 3
  5213. 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
  5214. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5215. |0|0|s| config | N1 (1-2 bytes): |
  5216. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  5217. | Compressed frame 1 (N1 bytes)... :
  5218. : |
  5219. | |
  5220. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5221. ]]></artwork>
  5222. </figure>
  5223. <figure anchor="sd_code1_packet" title="A Self-Delimited Code 1 Packet"
  5224. align="center">
  5225. <artwork align="center"><![CDATA[
  5226. 0 1 2 3
  5227. 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
  5228. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5229. |1|0|s| config | N1 (1-2 bytes): |
  5230. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
  5231. | Compressed frame 1 (N1 bytes)... |
  5232. : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5233. | | |
  5234. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
  5235. | Compressed frame 2 (N1 bytes)... |
  5236. : +-+-+-+-+-+-+-+-+
  5237. | |
  5238. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5239. ]]></artwork>
  5240. </figure>
  5241. <figure anchor="sd_code2_packet" title="A Self-Delimited Code 2 Packet"
  5242. align="center">
  5243. <artwork align="center"><![CDATA[
  5244. 0 1 2 3
  5245. 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
  5246. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5247. |0|1|s| config | N1 (1-2 bytes): N2 (1-2 bytes : |
  5248. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
  5249. | Compressed frame 1 (N1 bytes)... |
  5250. : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5251. | | |
  5252. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  5253. | Compressed frame 2 (N2 bytes)... :
  5254. : |
  5255. | |
  5256. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5257. ]]></artwork>
  5258. </figure>
  5259. <figure anchor="sd_code3cbr_packet" title="A Self-Delimited CBR Code 3 Packet"
  5260. align="center">
  5261. <artwork align="center"><![CDATA[
  5262. 0 1 2 3
  5263. 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
  5264. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5265. |1|1|s| config | M |p|0| Pad len (Opt) : N1 (1-2 bytes):
  5266. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5267. | |
  5268. : Compressed frame 1 (N1 bytes)... :
  5269. | |
  5270. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5271. | |
  5272. : Compressed frame 2 (N1 bytes)... :
  5273. | |
  5274. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5275. | |
  5276. : ... :
  5277. | |
  5278. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5279. | |
  5280. : Compressed frame M (N1 bytes)... :
  5281. | |
  5282. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5283. : Opus Padding (Optional)... |
  5284. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5285. ]]></artwork>
  5286. </figure>
  5287. <figure anchor="sd_code3vbr_packet" title="A Self-Delimited VBR Code 3 Packet"
  5288. align="center">
  5289. <artwork align="center"><![CDATA[
  5290. 0 1 2 3
  5291. 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
  5292. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5293. |1|1|s| config | M |p|1| Padding length (Optional) :
  5294. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5295. : N1 (1-2 bytes): ... : N[M-1] | N[M] :
  5296. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5297. | |
  5298. : Compressed frame 1 (N1 bytes)... :
  5299. | |
  5300. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5301. | |
  5302. : Compressed frame 2 (N2 bytes)... :
  5303. | |
  5304. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5305. | |
  5306. : ... :
  5307. | |
  5308. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5309. | |
  5310. : Compressed frame M (N[M] bytes)... :
  5311. | |
  5312. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5313. : Opus Padding (Optional)... |
  5314. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5315. ]]></artwork>
  5316. </figure>
  5317. </section>
  5318. </back>
  5319. </rfc>