spec.tex 320 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171
  1. \documentclass[9pt,letterpaper]{book}
  2. \usepackage{latexsym}
  3. \usepackage{amssymb}
  4. \usepackage{amsmath}
  5. \usepackage{bm}
  6. \usepackage{textcomp}
  7. \usepackage{graphicx}
  8. \usepackage{booktabs}
  9. \usepackage{tabularx}
  10. \usepackage{longtable}
  11. \usepackage{ltablex}
  12. \usepackage{wrapfig}
  13. \usepackage[pdfpagemode=None,pdfstartview=FitH,pdfview=FitH,colorlinks=true]%
  14. {hyperref}
  15. \newtheorem{theorem}{Theorem}[section]
  16. \newcommand{\idx}[1]{{\ensuremath{\mathit{#1}}}}
  17. \newcommand{\qti}{\idx{qti}}
  18. \newcommand{\qtj}{\idx{qtj}}
  19. \newcommand{\pli}{\idx{pli}}
  20. \newcommand{\plj}{\idx{plj}}
  21. \newcommand{\qi}{\idx{qi}}
  22. \newcommand{\ci}{\idx{ci}}
  23. \newcommand{\bmi}{\idx{bmi}}
  24. \newcommand{\bmj}{\idx{bmj}}
  25. \newcommand{\qri}{\idx{qri}}
  26. \newcommand{\qrj}{\idx{qrj}}
  27. \newcommand{\hti}{\idx{hti}}
  28. \newcommand{\sbi}{\idx{sbi}}
  29. \newcommand{\bi}{\idx{bi}}
  30. \newcommand{\bj}{\idx{bj}}
  31. \newcommand{\mbi}{\idx{mbi}}
  32. \newcommand{\mbj}{\idx{mbj}}
  33. \newcommand{\mi}{\idx{mi}}
  34. \newcommand{\cbi}{\idx{cbi}}
  35. \newcommand{\qii}{\idx{qii}}
  36. \newcommand{\ti}{\idx{ti}}
  37. \newcommand{\tj}{\idx{tj}}
  38. \newcommand{\rfi}{\idx{rfi}}
  39. \newcommand{\zzi}{\idx{zzi}}
  40. \newcommand{\ri}{\idx{ri}}
  41. %This somewhat odd construct ensures that \bitvar{\qi}, etc., will set the
  42. % qi in bold face, even though it is in a \mathit font, yet \bitvar{VAR} will
  43. % set VAR in a bold, roman font.
  44. \newcommand{\bitvar}[1]{\ensuremath{\mathbf{\bm{#1}}}}
  45. \newcommand{\locvar}[1]{\ensuremath{\mathrm{#1}}}
  46. \newcommand{\term}[1]{{\em #1}}
  47. \newcommand{\bin}[1]{\ensuremath{\mathtt{b#1}}}
  48. \newcommand{\hex}[1]{\ensuremath{\mathtt{0x#1}}}
  49. \newcommand{\ilog}{\ensuremath{\mathop{\mathrm{ilog}}\nolimits}}
  50. \newcommand{\round}{\ensuremath{\mathop{\mathrm{round}}\nolimits}}
  51. \newcommand{\sign}{\ensuremath{\mathop{\mathrm{sign}}\nolimits}}
  52. \newcommand{\lflim}{\ensuremath{\mathop{\mathrm{lflim}}\nolimits}}
  53. %Section-based table, figure, and equation numbering.
  54. \numberwithin{equation}{chapter}
  55. \numberwithin{figure}{chapter}
  56. \numberwithin{table}{chapter}
  57. \keepXColumns
  58. \pagestyle{headings}
  59. \bibliographystyle{alpha}
  60. \title{Theora Specification}
  61. \author{Xiph.org Foundation}
  62. \date{\today}
  63. \begin{document}
  64. \frontmatter
  65. \begin{titlepage}
  66. \maketitle
  67. \end{titlepage}
  68. \thispagestyle{empty}
  69. \cleardoublepage
  70. \pagenumbering{roman}
  71. \thispagestyle{plain}
  72. \tableofcontents
  73. \cleardoublepage
  74. \thispagestyle{plain}
  75. \listoffigures
  76. \cleardoublepage
  77. \thispagestyle{plain}
  78. \listoftables
  79. \cleardoublepage
  80. \thispagestyle{plain}
  81. \markboth{{\sc Notation and Conventions}}{{\sc Notation and Conventions}}
  82. \chapter*{Notation and Conventions}
  83. All parameters either passed in or out of a decoding procedure are given in
  84. \bitvar{bold\ face}.
  85. The prefix \bin{} indicates that the following value is to be interpreted as a
  86. binary number (base 2).
  87. \begin{verse}
  88. {\bf Example:} The value \bin{1110100} is equal to the decimal value 116.
  89. \end{verse}
  90. The prefix \hex{} indicates the the following value is to be interpreted as a
  91. hexadecimal number (base 16).
  92. \begin{verse}
  93. {\bf Example:} The value \hex{74} is equal to the decimal value 116.
  94. \end{verse}
  95. All arithmetic defined by this specification is exact.
  96. However, any real numbers that do arise will always be converted back to
  97. integers again in short order.
  98. The entire specification can be implemented using only normal integer
  99. operations.
  100. All operations are to be implemented with sufficiently large integers so that
  101. overflow cannot occur.
  102. Where the result of a computation is to be truncated to a fixed-sized binary
  103. representation, this will be explicitly noted.
  104. The size given for all variables is the maximum number of bits needed to store
  105. any value in that variable.
  106. Intermediate computations involving that variable may require more bits.
  107. The following operators are defined:
  108. \begin{description}
  109. \item[$|a|$]
  110. The absolute value of a number $a$.
  111. \begin{align*}
  112. |a| & = \left\{\begin{array}{ll}
  113. -a, & a < 0 \\
  114. a, & a \ge 0
  115. \end{array}\right.
  116. \end{align*}
  117. \item[$a*b$]
  118. Multiplication of a number $a$ by a number $b$.
  119. \item[$\frac{a}{b}$]
  120. Exact division of a number $a$ by a number $b$, producing a potentially
  121. non-integer result.
  122. \item[$\left\lfloor a\right\rfloor$]
  123. The largest integer less than or equal to a real number $a$.
  124. \item[$\left\lceil a\right\rceil$]
  125. The smallest integer greater than or equal to a real number $a$.
  126. \item[$a//b$]
  127. Integer division of $a$ by $b$.
  128. \begin{align*}
  129. a//b & = \left\{\begin{array}{ll}
  130. \left\lceil\frac{a}{b}\right\rceil, & a < 0 \\
  131. \left\lfloor\frac{a}{b}\right\rfloor, & a \ge 0
  132. \end{array}\right.
  133. \end{align*}
  134. \item[$a\%b$]
  135. The remainder from the integer division of $a$ by $b$.
  136. \begin{align*}
  137. a\%b & = a-|b|*\left\lfloor\frac{a}{|b|}\right\rfloor
  138. \end{align*}
  139. Note that with this definition, the result is always non-negative and less than
  140. $|b|$.
  141. \item[$a<<b$]
  142. The value obtained by left-shifting the two's complement integer $a$ by $b$
  143. bits.
  144. For purposes of this specification, overflow is ignored, and so this is
  145. equivalent to integer multiplication of $a$ by $2^b$.
  146. \item[$a>>b$]
  147. The value obtained by right-shifting the two's complement integer $a$ by $b$
  148. bits, filling in the leftmost bits of the new value with $0$ if $a$ is
  149. non-negative and $1$ if $a$ is negative.
  150. This is {\em not} equivalent to integer division of $a$ by $2^b$.
  151. Instead,
  152. \begin{align*}
  153. a>>b & = \left\lfloor\frac{a}{2^b}\right\rfloor.
  154. \end{align*}
  155. \item[$\round(a)$]
  156. Rounds a number $a$ to the nearest integer, with ties rounded away from $0$.
  157. \begin{align*}
  158. \round(a) = \left\{\begin{array}{ll}
  159. \lceil a-\frac{1}{2}\rceil & a \le 0 \\
  160. \lfloor a+\frac{1}{2}\rfloor & a > 0
  161. \end{array}\right.
  162. \end{align*}
  163. \item[$\sign(a)$]
  164. Returns the sign of a given number.
  165. \begin{align*}
  166. \sign(a) = \left\{\begin{array}{ll}
  167. -1 & a < 0 \\
  168. 0 & a = 0 \\
  169. 1 & a > 0
  170. \end{array}\right.
  171. \end{align*}
  172. \item[$\ilog(a)$]
  173. The minimum number of bits required to store a positive integer $a$ in
  174. two's complement notation, or $0$ for a non-positive integer $a$.
  175. \begin{align*}
  176. \ilog(a) = \left\{\begin{array}{ll}
  177. 0, & a \le 0 \\
  178. \left\lfloor\log_2{a}\right\rfloor+1, & a > 0
  179. \end{array}\right.
  180. \end{align*}
  181. \begin{verse}
  182. {\bf Examples:}
  183. \begin{itemize}
  184. \item $\ilog(-1)=0$
  185. \item $\ilog(0)=0$
  186. \item $\ilog(1)=1$
  187. \item $\ilog(2)=2$
  188. \item $\ilog(3)=2$
  189. \item $\ilog(4)=3$
  190. \item $\ilog(7)=3$
  191. \end{itemize}
  192. \end{verse}
  193. \item[$\min(a,b)$]
  194. The minimum of two numbers $a$ and $b$.
  195. \item[$\max(a,b)$]
  196. The maximum of two numbers $a$ and $b$.
  197. \end{description}
  198. \cleardoublepage
  199. \thispagestyle{plain}
  200. \markboth{{\sc Key words}}{{\sc Key words}}
  201. \chapter*{Key words}
  202. %We can't rewrite this, because this is text required by RFC 2119, so we use
  203. % some emergency stretching to get it typeset properly.
  204. \setlength{\emergencystretch}{2em}
  205. The key words ``MUST'', ``MUST NOT'', ``REQUIRED'', ``SHALL'', ``SHALL NOT'',
  206. ``SHOULD'', ``SHOULD NOT'', ``RECOMMENDED'', ``MAY'', and ``OPTIONAL'' in this
  207. document are to be intrepreted as described in RFC 2119 \cite{rfc2119}.\par
  208. \setlength{\emergencystretch}{0em}
  209. Where such assertions are placed on the contents of a Theora bitstream itself,
  210. implementations should be prepared to encounter bitstreams that do not follow
  211. these requirements.
  212. An application's behavior in the presecence of such non-conforming bitstreams
  213. is not defined by this specification, but any reasonable method of handling
  214. them MAY be used.
  215. By way of example, applications MAY discard the current frame, retain the
  216. current output thus far, or attempt to continue on by assuming some default
  217. values for the erroneous bits.
  218. When such an error occurs in the bitstream headers, an application MAY refuse
  219. to decode the entire stream.
  220. An application SHOULD NOT allow such non-conformant bitstreams to overflow
  221. buffers and potentially execute arbitrary code, as this represents a serious
  222. security risk.
  223. An application MUST, however, ensure any bits marked as reserved have the value
  224. zero, and refuse to decode the stream if they do not.
  225. These are used as place holders for future bitstream features with which the
  226. current bitstream is forward-compatible.
  227. Such features may not increment the bitstream version number, and can only be
  228. recognized by checking the value of these reserved bits.
  229. \cleardoublepage
  230. \mainmatter
  231. \pagenumbering{arabic}
  232. \setcounter{page}{1}
  233. \chapter{Introduction}
  234. Theora is a general purpose, lossy video codec.
  235. It is based on the VP3 video codec produced by On2 Technologies
  236. (\url{http://www.on2.com/}).
  237. On2 donated the VP3.1 source code to the Xiph.org Foundation and released it
  238. under a BSD-like license.
  239. On2 also made an irrevocable, royalty-free license grant for any patent claims
  240. it might have over the software and any derivatives.
  241. No formal specification exists for the VP3 format beyond this source code,
  242. however Mike Melanson maintains a detailed description \cite{Mel04}.
  243. Portions of this specification were adopted from that text with permission.
  244. \section{VP3 and Theora}
  245. Theora contains a superset of the features that were available in the original
  246. VP3 codec.
  247. Content encoded with VP3.1 can be losslessly transcoded into the Theora format.
  248. Theora content cannot, in general, be losslessly transcoded into the VP3
  249. format.
  250. If a feature is not available in the original VP3 format, this is mentioned
  251. when that feature is defined.
  252. A complete list of these features appears in Appendix~\ref{app:vp3-compat}.
  253. %TODO: VP3 - theora comparison in appendix
  254. \section{Video Formats}
  255. Theora currently supports progressive video data of arbitrary dimensions at a
  256. constant frame rate in one of several $Y'C_bC_r$ color spaces.
  257. The precise definition the supported color spaces appears in
  258. Section~\ref{sec:colorspaces}.
  259. Three different chroma subsampling formats are supported: 4:2:0, 4:2:2,
  260. and 4:4:4.
  261. The precise details of each of these formats and their sampling locations are
  262. described in Section~\ref{sec:pixfmts}.
  263. The Theora format does not support interlaced material, variable frame rates,
  264. bit-depths larger than 8 bits per component, nor alternate color spaces such
  265. as RGB or arbitrary multi-channel spaces.
  266. Black and white content can be efficiently encoded, however, because the
  267. uniform chroma planes compress well.
  268. Support for interlaced material is planned for a future version.
  269. \begin{verse}
  270. {\bf Note:} Infrequently changing frame rates---as when film and video
  271. sequences are cut together---can be supported in the Ogg container format by
  272. chaining several Theora streams together.
  273. \end{verse}
  274. Support for increased bit depths or additional color spaces is not planned.
  275. \section{Classification}
  276. Theora is a block-based lossy transform codec that utilizes an
  277. $8\times 8$ Type-II Discrete Cosine Transform and block-based motion
  278. compensation.
  279. This places it in the same class of codecs as MPEG-1, -2, -4, and H.263.
  280. The details of how individual blocks are organized and how DCT coefficients are
  281. stored in the bitstream differ substantially from these codecs, however.
  282. Theora supports only intra frames (I frames in MPEG) and inter frames (P frames
  283. in MPEG).
  284. There is no equivalent to the bi-predictive frames (B frames) found in MPEG
  285. codecs.
  286. \section{Assumptions}
  287. The Theora codec design assumes a complex, psychovisually-aware encoder and a
  288. simple, low-complexity decoder.
  289. %TODO: Talk more about implementation complexity.
  290. Theora provides none of its own framing, synchronization, or protection against
  291. transmission errors.
  292. An encoder is solely a method of accepting input video frames and
  293. compressing these frames into raw, unformatted `packets'.
  294. The decoder then accepts these raw packets in sequence, decodes them, and
  295. synthesizes a fascimile of the original video frames.
  296. Theora is a free-form variable bit rate (VBR) codec, and packets have no
  297. minimum size, maximum size, or fixed/expected size.
  298. Theora packets are thus intended to be used with a transport mechanism that
  299. provides free-form framing, synchronization, positioning, and error correction
  300. in accordance with these design assumptions, such as Ogg (for file transport)
  301. or RTP (for network multicast).
  302. For the purposes of a few examples in this document, we will assume that Theora
  303. is embedded in an Ogg stream specifically, although this is by no means a
  304. requirement or fundamental assumption in the Theora design.
  305. The specification for embedding Theora into an Ogg transport stream is given in
  306. Appendix~\ref{app:oggencapsulation}.
  307. \section{Codec Setup and Probability Model}
  308. Theora's heritage is the proprietary commerical codec VP3, and it retains a
  309. fair amount of inflexibility when compared to Vorbis \cite{vorbis}, the first
  310. Xiph.org codec, which began as a research codec.
  311. However, to provide additional scope for encoder improvement, Theora adopts
  312. some of the configurable aspects of decoder setup that are present in Vorbis.
  313. This configuration data is not available in VP3, which uses hardcoded values
  314. instead.
  315. Theora makes the same controversial design decision that Vorbis made to include
  316. the entire probability model for the DCT coefficients and all the quantization
  317. parameters in the bitstream headers.
  318. This is often several hundred fields.
  319. It is therefore impossible to decode any frame in the stream without
  320. having previously fetched the codec info and codec setup headers.
  321. \begin{verse}
  322. {\bf Note:} Theora {\em can} initiate decode at an arbitrary intra-frame packet
  323. within a bitstream so long as the codec has been initialized with the setup
  324. headers.
  325. \end{verse}
  326. Thus, Theora headers are both required for decode to begin and relatively large
  327. as bitstream headers go.
  328. The header size is unbounded, although as a rule-of-thumb less than 16kB is
  329. recommended, and Xiph.org's reference encoder follows this suggestion.
  330. %TODO: Is 8kB enough? My setup header is 7.4kB, that doesn't leave much room
  331. % for comments.
  332. %RG: the lesson from vorbis is that as small as possible is really
  333. % important in some applications. Practically, what's acceptable
  334. % depends a great deal on the target bitrate. I'd leave 16 kB in the
  335. % spec for now. fwiw more than 1k of comments is quite unusual.
  336. Our own design work indicates that the primary liability of the required header
  337. is in mindshare; it is an unusual design and thus causes some amount of
  338. complaint among engineers as this runs against current design trends and
  339. points out limitations in some existing software/interface designs.
  340. However, we find that it does not fundamentally limit Theora's suitable
  341. application space.
  342. %silvia: renamed
  343. %\subsection{Format Specification}
  344. \section{Format Conformance}
  345. The Theora format is well-defined by its decode specification; any encoder that
  346. produces packets that are correctly decoded by an implementation following
  347. this specification may be considered a proper Theora encoder.
  348. A decoder must faithfully and completely implement the specification defined
  349. herein %, except where noted,
  350. to be considered a conformant Theora decoder.
  351. A decoder need not be implemented strictly as described, but the
  352. actual decoder process MUST be {\em entirely mathematically equivalent}
  353. to the described process.
  354. Where appropriate, a non-normative description of encoder processes is
  355. included.
  356. These sections will be marked as such, and a proper Theora encoder is not
  357. bound to follow them.
  358. %TODO: \subsection{Hardware Profile}
  359. \chapter{Coded Video Structure}
  360. Theora's encoding and decoding process is based on $8\times 8$ blocks of
  361. pixels.
  362. This sections describes how a video frame is laid out, divided into
  363. blocks, and how those blocks are organized.
  364. \section{Frame Layout}
  365. A video frame in Theora is a two-dimensional array of pixels.
  366. Theora, like VP3, uses a right-handed coordinate system, with the origin in the
  367. lower-left corner of the frame.
  368. This is contrary to many video formats which use a left-handed coordinate
  369. system with the origin in the upper-left corner of the frame.
  370. %INT: This means that for interlaced material, the definition of `even fields'
  371. %INT: and `odd fields' may be reversed between Theora and other video codecs.
  372. %INT: This document will always refer to them as `top fields' and `bottom
  373. %INT: fields'.
  374. Theora divides the pixel array up into three separate \term{color planes}, one
  375. for each of the $Y'$, $C_b$, and $C_r$ components of the pixel.
  376. The $Y'$ plane is also called the \term{luma plane}, and the $C_b$ and $C_r$
  377. planes are also called the \term{chroma planes}.
  378. Each plane is assigned a numerical value, as shown in
  379. Table~\ref{tab:color-planes}.
  380. \begin{table}[htbp]
  381. \begin{center}
  382. \begin{tabular}{cl}\toprule
  383. Index & Color Plane \\\midrule
  384. $0$ & $Y'$ \\
  385. $1$ & $C_b$ \\
  386. $2$ & $C_r$ \\
  387. \bottomrule\end{tabular}
  388. \end{center}
  389. \caption{Color Plane Indices}
  390. \label{tab:color-planes}
  391. \end{table}
  392. In some pixel formats, the chroma planes are subsampled by a factor of two
  393. in one or both directions.
  394. This means that the width or height of the chroma planes may be half that of
  395. the total frame width and height.
  396. The luma plane is never subsampled.
  397. \section{Picture Region}
  398. An encoded video frame in Theora is required to have a width and height that
  399. are multiples of sixteen, making an integral number of blocks even when the
  400. chroma planes are subsampled.
  401. However, inside a frame a smaller \term{picture region} may be defined
  402. to present material whose dimensions are not a multiple of sixteen pixels, as
  403. shown in Figure~\ref{fig:pic-frame}.
  404. The picture region can be offset from the lower-left corner of the frame by up
  405. to 255 pixels in each direction, and may have an arbitrary width and height,
  406. provided that it is contained entirely within the coded frame.
  407. It is this picture region that contains the actual video data.
  408. The portions of the frame which lie outside the picture region may contain
  409. arbitrary image data, so the frame must be cropped to the picture region
  410. before display.
  411. The picture region plays no other role in the decode process, which operates on
  412. the entire video frame.
  413. \begin{figure}[htbp]
  414. \begin{center}
  415. \includegraphics{pic-frame}
  416. \end{center}
  417. \caption{Location of frame and picture regions}
  418. \label{fig:pic-frame}
  419. \end{figure}
  420. \section{Blocks and Super Blocks}
  421. \label{sec:blocks-and-sbs}
  422. Each color plane is subdivided into \term{blocks} of $8\times 8$ pixels.
  423. Blocks are grouped into $4\times 4$ arrays called \term{super blocks} as
  424. shown in Figure~\ref{fig:superblock}.
  425. Each color plane has its own set of blocks and super blocks.
  426. If the chroma planes are subsampled, they are still divided into $8\times 8$
  427. blocks of pixels; there are just fewer blocks than in the luma plane.
  428. The boundaries of blocks and super blocks in the luma plane do not necessarily
  429. coincide with those of the chroma planes, if the chroma planes have been
  430. subsampled.
  431. \begin{figure}[htbp]
  432. \begin{center}
  433. \includegraphics{superblock}
  434. \end{center}
  435. \caption{Subdivision of a frame into blocks and super blocks}
  436. \label{fig:superblock}
  437. \end{figure}
  438. Blocks are accessed in two different orders in the various decoder processes.
  439. The first is \term{raster order}, illustrated in Figure~\ref{fig:raster-block}.
  440. This accesses each block in row-major order, starting in the lower left of the
  441. frame and continuing along the bottom row of the entire frame, followed by the
  442. next row up, starting on the left edge of the frame, etc.
  443. \begin{figure}[htbp]
  444. \begin{center}
  445. \includegraphics{raster-block}
  446. \end{center}
  447. \caption{Raster ordering of $n\times m$ blocks}
  448. \label{fig:raster-block}
  449. \end{figure}
  450. The second is \term{coded order}.
  451. In coded order, blocks are accessed by super block.
  452. Within each frame, super blocks are traversed in raster order,
  453. similar to raster order for blocks.
  454. Within each super block, however, blocks are accessed in a Hilbert curve
  455. pattern, illustrated in Figure~\ref{fig:hilbert-block}.
  456. If a color plane does not contain a complete super block on the top or right
  457. sides, the same ordering is still used, simply with any blocks outside the
  458. frame boundary ommitted.
  459. \begin{figure}[htbp]
  460. \begin{center}
  461. \includegraphics{hilbert-block}
  462. \end{center}
  463. \caption{Hilbert curve ordering of blocks within a super block}
  464. \label{fig:hilbert-block}
  465. \end{figure}
  466. To illustrate this ordering, consider a frame that is 240 pixels wide and
  467. 48 pixels high.
  468. Each row of the luma plane has 30 blocks and 8 super blocks, and there are 6
  469. rows of blocks and two rows of super blocks.
  470. %When accessed in raster order, each block in the luma plane is assigned the
  471. % following indices:
  472. %\vspace{\baselineskip}
  473. %\begin{center}
  474. %\begin{tabular}{|ccccccc|}\hline
  475. %150 & 151 & 152 & 153 & $\ldots$ & 178 & 179 \\
  476. %120 & 121 & 122 & 123 & $\ldots$ & 148 & 149 \\\hline
  477. % 90 & 91 & 92 & 93 & $\ldots$ & 118 & 119 \\
  478. % 60 & 61 & 62 & 63 & $\ldots$ & 88 & 89 \\
  479. % 30 & 31 & 32 & 33 & $\ldots$ & 58 & 59 \\
  480. % 0 & 1 & 2 & 3 & $\ldots$ & 28 & 29 \\\hline
  481. %\end{tabular}
  482. %\end{center}
  483. %\vspace{\baselineskip}
  484. When accessed in coded order, each block in the luma plane is assigned the
  485. following indices:
  486. \vspace{\baselineskip}
  487. \begin{center}
  488. \begin{tabular}{|cccc|c|cc|}\hline
  489. 123 & 122 & 125 & 124 & $\ldots$ & 179 & 178 \\
  490. 120 & 121 & 126 & 127 & $\ldots$ & 176 & 177 \\\hline
  491. 5 & 6 & 9 & 10 & $\ldots$ & 117 & 118 \\
  492. 4 & 7 & 8 & 11 & $\ldots$ & 116 & 119 \\
  493. 3 & 2 & 13 & 12 & $\ldots$ & 115 & 114 \\
  494. 0 & 1 & 14 & 15 & $\ldots$ & 112 & 113 \\\hline
  495. \end{tabular}
  496. \end{center}
  497. \vspace{\baselineskip}
  498. Here the index values specify the order in which the blocks would be accessed.
  499. The indices of the blocks are numbered continuously from one color plane to the
  500. next.
  501. They do not reset to zero at the start of each plane.
  502. Instead, the numbering increases continuously from the $Y'$ plane to the $C_b$
  503. plane to the $C_r$ plane.
  504. The implication is that the blocks from all planes are treated as a unit during
  505. the various processing steps.
  506. Although blocks are sometimes accessed in raster order, in this document the
  507. index associated with a block is {\em always} its index in coded order.
  508. \section{Macro Blocks}
  509. \label{sec:mbs}
  510. A macro block contains a $2\times 2$ array of blocks in the luma plane
  511. {\em and} the co-located blocks in the chroma planes, as shown in
  512. Figure~\ref{fig:macroblock}.
  513. Thus macro blocks can represent anywhere from six to twelve blocks, depending
  514. on how the chroma planes are subsampled.
  515. This is in contrast to super blocks, which only contain blocks from a single
  516. color plane.
  517. % the whole super vs. macro blocks thing is a little confusing, and it can be
  518. % hard to remember which is what initially. A figure would/will help here,
  519. % but I tried to add some text emphasizing the difference in terms of
  520. % functionality.
  521. %TBT: At this point we haven't described any functionality yet.
  522. %TBT: As far as the reader knows, the only purpose of the blocks, macro blocks
  523. %TBT: and super blocks is for data organization---and for blocks and super
  524. %TBT: blocks, this is essentially true.
  525. %TBT: So lets restrict the differences we emphasize to those of data
  526. %TBT: organization, which the sentence I just added above does.
  527. Macro blocks contain information about coding mode and motion vectors for the
  528. corresponding blocks in all color planes.
  529. \begin{figure}[htbp]
  530. \begin{center}
  531. \includegraphics{macroblock}
  532. \end{center}
  533. \caption{Subdivision of a frame into macro blocks}
  534. \label{fig:macroblock}
  535. \end{figure}
  536. Macro blocks are also accessed in a \term{coded order}.
  537. This coded order proceeds by examining each super block in the luma plane in
  538. raster order, and traversing the four macro blocks inside using a smaller
  539. Hilbert curve, as shown in Figure~\ref{fig:hilbert-mb}.
  540. %r: I rearranged the wording to make a more formal idiom here
  541. If the luma plane does not contain a complete super block on the top or right
  542. sides, the same ordering is still used, with any macro blocks outside
  543. the frame boundary simply omitted.
  544. Because the frame size is constrained to be a multiple of 16, there are never
  545. any partial macro blocks.
  546. Unlike blocks, macro blocks need never be accessed in a pure raster order.
  547. \begin{figure}[htbp]
  548. \begin{center}
  549. \includegraphics{hilbert-mb}
  550. \end{center}
  551. \caption{Hilbert curve ordering of macro blocks within a super block}
  552. \label{fig:hilbert-mb}
  553. \end{figure}
  554. Using the same frame size as the example above, there are 15 macro blocks in
  555. each row and 3 rows of macro blocks.
  556. The macro blocks are assigned the following indices:
  557. \vspace{\baselineskip}
  558. \begin{center}
  559. \begin{tabular}{|cc|cc|c|cc|c|}\hline
  560. 30 & 31 & 32 & 33 & $\cdots$ & 42 & 43 & 44 \\\hline
  561. 1 & 2 & 5 & 6 & $\cdots$ & 25 & 26 & 29 \\
  562. 0 & 3 & 4 & 7 & $\cdots$ & 24 & 27 & 28 \\\hline
  563. \end{tabular}
  564. \end{center}
  565. \vspace{\baselineskip}
  566. \section{Coding Modes and Prediction}
  567. Each block is coded using one of a small, fixed set of \term{coding modes} that
  568. define how the block is predicted from previous frames.
  569. A block is predicted using one of two \term{reference frames}, selected
  570. according to the coding mode.
  571. A reference frame is the fully decoded version of a previous frame in the
  572. stream.
  573. The first available reference frame is the previous intra frame, called the
  574. \term{golden frame}.
  575. The second available reference frame is the previous frame, whether it was an
  576. intra frame or an inter frame.
  577. If the previous frame was an intra frame, then both reference frames are the
  578. same.
  579. See Figure~\ref{fig:reference-frames} for an illustration of the reference
  580. frames used for an intra frame that does not follow an intra frame.
  581. \begin{figure}[htbp]
  582. \begin{center}
  583. \includegraphics{reference-frames}
  584. \end{center}
  585. \caption{Example of reference frames for an inter frame}
  586. \label{fig:reference-frames}
  587. \end{figure}
  588. Two coding modes in particular are worth mentioning here.
  589. The INTRA mode is used for blocks that are not predicted from either reference
  590. frame.
  591. This is the only coding mode allowed in intra frames.
  592. The INTER\_NOMV coding mode uses the co-located contents of the block in the
  593. previous frame as the predictor.
  594. This is the default coding mode.
  595. \section{DCT Coefficients}
  596. \label{sec:dct-coeffs}
  597. A \term{residual} is added to the predicted contents of a block to form the
  598. final reconstruction.
  599. The residual is stored as a set of quantized coefficients from an integer
  600. approximation of a two-dimensional Type II Discrete Cosine Transform.
  601. The DCT takes an $8\times 8$ array of pixel values as input and returns an
  602. $8\times 8$ array of coefficient values.
  603. The \term{natural ordering} of these coefficients is defined to be row-major
  604. order, from lowest to highest frequency.
  605. They are also often indexed in \term{zig-zag order}, as shown in
  606. Figure~\ref{tab:zig-zag}.
  607. \begin{figure}[htbp]
  608. \begin{center}
  609. \begin{tabular}[c]{rr|c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c}
  610. &\multicolumn{1}{r}{} & && &&&&&$c$&&& && && \\
  611. &\multicolumn{1}{r}{} &0&&1&&2&&3&&4&&5&&6&&7 \\\cline{3-17}
  612. &0 & 0 &$\rightarrow$& 1 && 5 &$\rightarrow$& 6 && 14 &$\rightarrow$& 15 && 27 &$\rightarrow$& 28 \\[-0.5\defaultaddspace]
  613. & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
  614. &1 & 2 & & 4 && 7 & & 13 && 16 & & 26 && 29 & & 42 \\[-0.5\defaultaddspace]
  615. & &$\downarrow$&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&$\downarrow$ \\
  616. &2 & 3 & & 8 && 12 & & 17 && 25 & & 30 && 41 & & 43 \\[-0.5\defaultaddspace]
  617. & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
  618. &3 & 9 & & 11 && 18 & & 24 && 31 & & 40 && 44 & & 53 \\[-0.5\defaultaddspace]
  619. $r$&&$\downarrow$&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&$\downarrow$ \\
  620. &4 & 10 & & 19 && 23 & & 32 && 39 & & 45 && 52 & & 54 \\[-0.5\defaultaddspace]
  621. & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
  622. &5 & 20 & & 22 && 33 & & 38 && 46 & & 51 && 55 & & 60 \\[-0.5\defaultaddspace]
  623. & &$\downarrow$&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&$\downarrow$ \\
  624. &6 & 21 & & 34 && 37 & & 47 && 50 & & 56 && 59 & & 61 \\[-0.5\defaultaddspace]
  625. & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
  626. &7 & 35 &$\rightarrow$& 36 && 48 &$\rightarrow$& 49 && 57 &$\rightarrow$& 58 && 62 &$\rightarrow$& 63
  627. \end{tabular}
  628. \end{center}
  629. \caption{Zig-zag order}
  630. \label{tab:zig-zag}
  631. \end{figure}
  632. \begin{verse}
  633. {\bf Note:} the row and column indices refer to {\em frequency number} and not
  634. pixel locations.
  635. The frequency numbers are defined independently of the memory organization of
  636. the pixels.
  637. They have been written from top to bottom here to follow conventional notation,
  638. despite the right-handed coordinate system Theora uses for pixel locations.
  639. %RG: I'd rather we were internally consistent and put dc at the lower left.
  640. Many implementations of the DCT operate `in-place'.
  641. That is, they return DCT coefficients in the same memory buffer that the
  642. initial pixel values were stored in.
  643. Due to the right-handed coordinate system used for pixel locations in Theora,
  644. one must note carefully how both pixel values and DCT coefficients are
  645. organized in memory in such a system.
  646. \end{verse}
  647. DCT coefficient $(0,0)$ is called the \term{DC coefficient}.
  648. All the other coefficients are called \term{AC coefficients}.
  649. \chapter{Decoding Overview}
  650. This section provides a high level description of the Theora codec's
  651. construction.
  652. A bit-by-bit specification appears beginning in Section~\ref{sec:bitpacking}.
  653. The later sections assume a high-level understanding of the Theora decode
  654. process, which is provided below.
  655. \section{Decoder Configuration}
  656. Decoder setup consists of configuration of the quantization matrices and the
  657. Huffman codebooks for the DCT coefficients, and a table of limit values for
  658. the deblocking filter.
  659. The remainder of the decoding pipeline is not configurable.
  660. \subsection{Global Configuration}
  661. The global codec configuration consists of a few video related fields, such as
  662. frame rate, frame size, picture size and offset, aspect ratio, color space,
  663. pixel format, and a version number.
  664. The version number is divided into a major version, a minor version, amd a
  665. minor revision number.
  666. %r: afaik the released vp3 codec called itself 3.1 and is compatible w/ theora
  667. %r: even though we received the in-progress 3.2 codebase
  668. For the format defined in this specification, these are `3', `2', and
  669. `1', respectively, in reference to Theora's origin as a successor to
  670. the VP3.1 format.
  671. \subsection{Quantization Matrices}
  672. Theora allows up to 384 different quantization matrices to be defined, one for
  673. each \term{quantization type}, \term{color plane} ($Y'$, $C_b$, or $C_r$), and
  674. \term{quantization index}, \qi, which ranges from zero to 63, inclusive.
  675. There are currently two quantization types defined, which depend on the coding
  676. mode of the block being dequantized, as shown in Table~\ref{tab:quant-types}.
  677. \begin{table}[htbp]
  678. \begin{center}
  679. \begin{tabular}{cl}\toprule
  680. Quantization Type & Usage \\\midrule
  681. $0$ & INTRA-mode blocks \\
  682. $1$ & Blocks in any other mode. \\
  683. \bottomrule\end{tabular}
  684. \end{center}
  685. \caption{Quantization Type Indices}
  686. \label{tab:quant-types}
  687. \end{table}
  688. %r: I think 'nominally' is more specific than 'generally' here
  689. The quantization index, on the other hand, nominally represents a progressive
  690. range of quality levels, from low quality near zero to high quality near 63.
  691. However, the interpretation is arbitrary, and it is possible, for example, to
  692. partition the scale into two completely separate ranges with 32 levels each
  693. that are meant to represent different classes of source material, or any
  694. other arrangement that suits the encoder's requirements.
  695. Each quantization matrix is an $8\times 8$ matrix of 16-bit values, which is
  696. used to quantize the output of the $8\times 8$ DCT\@.
  697. Quantization matrices are specified using three components: a
  698. \term{base matrix} and two \term{scale values}.
  699. The first scale value is the \term{DC scale}, which is applied to the DC
  700. component of the base matrix.
  701. The second scale value is the \term{AC scale}, which is applied to all the
  702. other components of the base matrix.
  703. There are 64 DC scale values and 64 AC scale values, one for each \qi\ value.
  704. There are 64 elements in each base matrix, one for each DCT coefficient.
  705. They are stored in natural order (cf. Section~\ref{sec:dct-coeffs}).
  706. There is a separate set of base matrices for each quantization type and each
  707. color plane, with up to 64 possible base matrices in each set, one for each
  708. \qi\ value.
  709. %r: we will mention that the given matricies must bound the \qi range
  710. %r: in the detailed section. it's not important at this level.
  711. Typically the bitstream contains matrices for only a sparse subset of the
  712. possible \qi\ values.
  713. The base matrices for the remainder of the \qi\ values are computed using
  714. linear interpolation.
  715. This configuration allows the encoder to adjust the quantization matrices to
  716. approximate the complex, non-linear response of the human visual system to
  717. different quantization errors.
  718. Finally, because the in-loop deblocking filter strength depends on the strength
  719. of the quantization matrices defined in this header, a table of 64 \term{loop
  720. filter limit values} is defined, one for each \qi\ value.
  721. The precise specification of how all of this information is decoded appears in
  722. Section~\ref{sub:loop-filter-limits} and Section~\ref{sub:quant-params}.
  723. \subsection{Huffman Codebooks}
  724. Theora uses 80 configurable binary Huffman codes to represent the 32 tokens
  725. used to encode DCT coefficients.
  726. Each of the 32 token values has a different semantic meaning and is used to
  727. represent single coefficient values, zero runs, combinations of the two, and
  728. \term{End-Of-Block markers}.
  729. The 80 codes are divided up into five groups of 16, with each group
  730. corresponding to a set of DCT coefficient indices.
  731. The first group corresponds to the DC coefficient, while the remaining four
  732. groups correspond to different subsets of the AC coefficients.
  733. Within each frame, two pairs of 4-bit codebook indices are stored.
  734. The first pair selects which codebooks to use from the DC coefficient group for
  735. the $Y'$ coefficients and the $C_b$ and $C_r$ coefficients.
  736. The second pair selects which codebooks to use from {\em all four} of the AC
  737. coefficient groups for the $Y'$ coefficients and the $C_b$ and $C_r$
  738. coefficients.
  739. The precise specification of how the codebooks are decoded appears in
  740. Section~\ref{sub:huffman-tables}.
  741. \section{High-Level Decode Process}
  742. \subsection{Decoder Setup}
  743. Before decoding can begin, a decoder MUST be initialized using the bitstream
  744. headers corresponding to the stream to be decoded.
  745. Theora uses three header packets; all are required, in order, by this
  746. specification.
  747. Once set up, decode may begin at any intra-frame packet---or even inter-frame
  748. packets, provided the appropriate decoded reference frames have already been
  749. decoded and cached---belonging to the Theora stream.
  750. In Theora I, all packets after the three initial headers are intra-frame or
  751. inter-frame packets.
  752. The header packets are, in order, the identification header, the comment
  753. header, and the setup header.
  754. \paragraph{Identification Header}
  755. The identification header identifies the stream as Theora, provides a version
  756. number, and defines the characteristics of the video stream such as frame
  757. size.
  758. A complete description of the identification header appears in
  759. Section~\ref{sec:idheader}.
  760. \paragraph{Comment Header}
  761. The comment header includes user text comments (`tags') and a vendor string
  762. for the application/library that produced the stream.
  763. The format of the comment header is the same as that used in the Vorbis I and
  764. Speex codecs, with slight modifications due to the use of a different bit
  765. packing mechanism.
  766. A complete description of how the comment header is coded appears in
  767. Section~\ref{sec:commentheader}, along with a suggested set of tags.
  768. \paragraph{Setup Header}
  769. The setup header includes extensive codec setup information, including the
  770. complete set of quantization matrices and Huffman codebooks needed to decode
  771. the DCT coefficients.
  772. A complete description of the setup header appears in
  773. Section~\ref{sec:setupheader}.
  774. \subsection{Decode Procedure}
  775. The decoding and synthesis procedure for all video packets is fundamentally the
  776. same, with some steps omitted for intra frames.
  777. \begin{itemize}
  778. \item
  779. Decode packet type flag.
  780. \item
  781. Decode frame header.
  782. \item
  783. Decode coded block information (inter frames only).
  784. \item
  785. Decode macro block mode information (inter frames only).
  786. \item
  787. Decode motion vectors (inter frames only).
  788. \item
  789. Decode block-level \qi\ information.
  790. \item
  791. Decode DC coefficient for each coded block.
  792. \item
  793. Decode 1st AC coefficient for each coded block.
  794. \item
  795. Decode 2nd AC coefficient for each coded block.
  796. \item
  797. $\ldots$
  798. \item
  799. Decode 63rd AC coefficient for each coded block.
  800. \item Perform DC coefficient prediction.
  801. \item Reconstruct coded blocks.
  802. \item Copy uncoded bocks.
  803. \item Perform loop filtering.
  804. \end{itemize}
  805. \begin{verse}
  806. {\bf Note:} clever rearrangement of the steps in this process is possible.
  807. As an example, in a memory-constrained environment, one can make multiple
  808. passes through the DCT coefficients to avoid buffering them all in memory.
  809. On the first pass, the starting location of each coefficient is identified, and
  810. then 64 separate get pointers are used to read in the 64 DCT coefficients
  811. required to reconstruct each coded block in sequence.
  812. This operation produces entirely equivalent output and is naturally perfectly
  813. legal.
  814. It may even be a benefit in non-memory-constrained environments due to a
  815. reduced cache footprint.
  816. \end{verse}
  817. Theora makes equivalence easy to check by defining all decoding operations in
  818. terms of exact integer operations.
  819. No floating-point math is required, and in particular, the implementation of
  820. the iDCT transform MUST be followed precisely.
  821. This prevents the decoder mismatch problem commonly associated with codecs that
  822. provide a less rigorous transform specification.
  823. Such a mismatch problem would be devastating to Theora, since a single rounding
  824. error in one frame could propagate throughout the entire succeeding frame due
  825. to DC prediction.
  826. \paragraph{Packet Type Decode}
  827. Theora uses four packet types.
  828. The first three packet types mark each of the three Theora headers described
  829. above.
  830. The fourth packet type marks a video packet.
  831. All other packet types are reserved; packets marked with a reserved type should
  832. be ignored.
  833. Additionally, zero-length packets are treated as if they were an inter
  834. frame with no blocks coded. That is, as a duplicate frame.
  835. \paragraph{Frame Header Decode}
  836. The frame header contains some global information about the current frame.
  837. The first is the frame type field, which specifies if this is an intra frame or
  838. an inter frame.
  839. Inter frames predict their contents from previously decoded reference frames.
  840. Intra frames can be independently decoded with no established reference frames.
  841. The next piece of information in the frame header is the list of \qi\ values
  842. allowed in the frame.
  843. Theora allows from one to three different \qi\ values to be used in a single
  844. frame, each of which selects a set of six quantization matrices, one for each
  845. quantization type (inter or intra), and one for each color plane.
  846. The first \qi\ value is {\em always} used when dequantizing DC coefficients.
  847. The \qi\ value used when dequantizing AC coefficients, however, can vary from
  848. block to block.
  849. VP3, in contrast, only allows a single \qi\ value per frame for both the DC and
  850. AC coefficients.
  851. \paragraph{Coded Block Information}
  852. This stage determines which blocks in the frame are coded and which are
  853. uncoded.
  854. A \term{coded block list} is constructed which lists all the coded blocks in
  855. coded order.
  856. For intra frames, every block is coded, and so no data needs to be read from
  857. the packet.
  858. \paragraph{Macro Block Mode Information}
  859. For intra frames, every block is coded in INTRA mode, and this stage is
  860. skipped.
  861. In inter frames a \term{coded macro block list} is constructed from the coded
  862. block list.
  863. Any macro block which has at least one of its luma blocks coded is considered
  864. coded; all other macro blocks are uncoded, even if they contain coded chroma
  865. blocks.
  866. A coding mode is decoded for each coded macro block, and assigned to all its
  867. constituent coded blocks.
  868. All coded chroma blocks in uncoded macro blocks are assigned the INTER\_NOMV
  869. coding mode.
  870. \paragraph{Motion Vectors}
  871. Intra frames are coded entirely in INTRA mode, and so this stage is skipped.
  872. Some inter coding modes, however, require one or more motion vectors to be
  873. specified for each macro block.
  874. These are decoded in this stage, and an appropriate motion vector is assigned
  875. to each coded block in the macro block.
  876. \paragraph{Block-Level \qi\ Information}
  877. If a frame allows multiple \qi\ values, the \qi\ value assigned to each block
  878. is decoded here.
  879. Frames that use only a single \qi\ value have nothing to decode.
  880. \paragraph{DCT Coefficients}
  881. Finally, the quantized DCT coefficients are decoded.
  882. A list of DCT coefficients in zig-zag order for a single block is represented
  883. by a list of tokens.
  884. A token can take on one of 32 different values, each with a different semantic
  885. meaning.
  886. A single token can represent a single DCT coefficient, a run of zero
  887. coefficients within a single block, a combination of a run of zero
  888. coefficients followed by a single non-zero coefficient, an
  889. \term{End-Of-Block marker}, or a run of EOB markers.
  890. EOB markers signify that the remainder of the block is one long zero run.
  891. Unlike JPEG and MPEG, there is no requirement for each block to end with
  892. a special marker.
  893. If non-EOB tokens yield values for all 64 of the coefficients in a block, then
  894. no EOB marker occurs.
  895. Each token is associated with a specific \term{token index} in a block.
  896. For single-coefficient tokens, this index is the zig-zag index of the token in
  897. the block.
  898. For zero-run tokens, this index is the zig-zag index of the {\em first}
  899. coefficient in the run.
  900. For combination tokens, the index is again the zig-zag index of the first
  901. coefficient in the zero run.
  902. For EOB markers, which signify that the remainder of the block is one long zero
  903. run, the index is the zig-zag index of the first zero coefficient in that run.
  904. For EOB runs, the token index is that of the first EOB marker in the run.
  905. Due to zero runs and EOB markers, a block does not have to have a token for
  906. every zig-zag index.
  907. Tokens are grouped in the stream by token index, not by the block they
  908. originate from.
  909. This means that for each zig-zag index in turn, the tokens with that index from
  910. {\em all} the coded blocks are coded in coded block order.
  911. When decoding, a current token index is maintained for each coded block.
  912. This index is advanced by the number of coefficients that are added to the
  913. block as each token is decoded.
  914. After fully decoding all the tokens with token index \ti, the current token
  915. index of every coded block will be \ti\ or greater.
  916. If an EOB run of $n$ blocks is decoded at token index \ti, then it ends the
  917. next $n$ blocks in coded block order whose current token index is equal to
  918. \ti, but not greater.
  919. If there are fewer than $n$ blocks with a current token index of \ti, then the
  920. decoder goes through the coded block list again from the start, ending blocks
  921. with a current token index of $\ti+1$, and so on, until $n$ blocks have been
  922. ended.
  923. Tokens are read by parsing a Huffman code that depends on \ti\ and the color
  924. plane of the next coded block whose current token index is equal to \ti, but
  925. not greater.
  926. The Huffman codebooks are selected on a per-frame basis from the 80 codebooks
  927. defined in the setup header.
  928. Many tokens have a fixed number of \term{extra bits} associated with them.
  929. These bits are read from the packet immediately after the token is decoded.
  930. These are used to define things such as coefficient magnitude, sign, and the
  931. length of runs.
  932. \paragraph{DC Prediction}
  933. After the coefficients for each block are decoded, the quantized DC value of
  934. each block is adjusted based on the DC values of its neighbors.
  935. This adjustment is performed by scanning the blocks in raster order, not coded
  936. block order.
  937. \paragraph{Reconstruction}
  938. Finally, using the coding mode, motion vector (if applicable), quantized
  939. coefficient list, and \qi\ value defined for each block, all the coded blocks
  940. are reconstructed.
  941. The DCT coefficients are dequantized, an inverse DCT transform is applied, and
  942. the predictor is formed from the coding mode and motion vector and added to
  943. the result.
  944. \paragraph{Loop Filtering}
  945. To complete the reconstructed frame, an ``in-loop'' deblocking filter is
  946. applied to the edges of all coded blocks.
  947. \chapter{Video Formats}
  948. This section gives a precise description of the video formats that Theora is
  949. capable of storing.
  950. The Theora bitstream is capable of handling video at any arbitrary resolution
  951. up to $1048560\times 1048560$.
  952. Such video would require almost three terabytes of storage per frame for
  953. uncompressed data, so compliant decoders MAY refuse to decode images with
  954. sizes beyond their capabilities.
  955. %TODO: What MUST a "compliant" decoder accept?
  956. %TODO: What SHOULD a decoder use for an upper bound? (derive from total amount
  957. %TODO: of memory and memory bandwidth)
  958. %TODO: Any lower limits?
  959. %TODO: We really need hardware device profiles, but such things should be
  960. %TODO: developed with input from the hardware community.
  961. %TODO: And even then sometimes they're useless
  962. The remainder of this section talks about two specific aspects of the video
  963. format: the color space and the pixel format.
  964. The first describes how color is represented and how to transform that color
  965. representation into a device independent color space such as CIE $XYZ$ (1931).
  966. The second describes the various schemes for sampling the color values in time
  967. and space.
  968. \section{Color Space Conventions}
  969. There are a large number of different color standards used in digital video.
  970. Since Theora is a lossy codec, it restricts itself to only a few of them to
  971. simplify playback.
  972. Unlike the alternate method of describing all the parameters of the color
  973. model, this allows a few dedicated routines for color conversion to be written
  974. and heavily optimized in a decoder.
  975. More flexible conversion functions should instead be specified in an encoder,
  976. where additional computational complexity is more easily tolerated.
  977. The color spaces were selected to give a fair representation of color standards
  978. in use around the world today.
  979. Most of the standards that do not exactly match one of these can be converted
  980. to one fairly easily.
  981. All Theora color spaces are $Y'C_bC_r$ color spaces with one luma channel and
  982. two chroma channels.
  983. Each channel contains 8-bit discrete values in the range $0\ldots255$, which
  984. represent non-linear gamma pre-corrected signals.
  985. The Theora identification header contains an 8-bit value that describes the
  986. color space.
  987. This merely selects one of the color spaces available from an enumerated list.
  988. Currently, only two color spaces are defined, with a third possibility that
  989. indicates the color space is ``unknown".
  990. \section{Color Space Conversions and Parameters}
  991. \label{sec:color-xforms}
  992. The parameters which describe the conversions between each color space are
  993. listed below.
  994. These are the parameters needed to map colors from the encoded $Y'C_bC_r$
  995. representation to the device-independent color space CIE $XYZ$ (1931).
  996. These parameters define abstract mathematical conversion functions which are
  997. infinitely precise.
  998. The accuracy and precision with which the conversions are performed in a real
  999. system is determined by the quality of output desired and the available
  1000. processing power.
  1001. Exact decoder output is defined by this specification only in the original
  1002. $Y'C_bC_r$ space.
  1003. \begin{description}
  1004. \item[$Y'C_bC_r$ to $Y'P_bP_r$:]
  1005. \vspace{\baselineskip}\hfill
  1006. This conversion takes 8-bit discrete values in the range $[0\ldots255]$ and
  1007. maps them to real values in the range $[0\ldots1]$ for Y and
  1008. $[-\frac{1}{2}\ldots\frac{1}{2}]$ for $P_b$ and $P_r$.
  1009. Because some values may fall outside the offset and excursion defined for each
  1010. channel in the $Y'C_bC_r$ space, the results may fall outside these ranges in
  1011. $Y'P_bP_r$ space.
  1012. No clamping should be done at this stage.
  1013. \begin{align}
  1014. Y'_\mathrm{out} & =
  1015. \frac{Y'_\mathrm{in}-\mathrm{Offset}_Y}{\mathrm{Excursion}_Y} \\
  1016. P_b & =
  1017. \frac{C_b-\mathrm{Offset}_{C_b}}{\mathrm{Excursion}_{C_b}} \\
  1018. P_r & =
  1019. \frac{C_r-\mathrm{Offset}_{C_r}}{\mathrm{Excursion}_{C_r}}
  1020. \end{align}
  1021. Parameters: $\mathrm{Offset}_{Y,C_b,C_r}$, $\mathrm{Excursion}_{Y,C_b,C_r}$.
  1022. \item[$Y'P_bP_r$ to $R'G'B'$:]
  1023. \vspace{\baselineskip}\hfill
  1024. This conversion takes the one luma and two chroma channel representation and
  1025. maps it to the non-linear $R'G'B'$ space used to drive actual output devices.
  1026. Values should be clamped into the range $[0\ldots1]$ after this stage.
  1027. \begin{align}
  1028. R' & = Y'+2(1-K_r)P_r \\
  1029. G' & = Y'-2\frac{(1-K_b)K_b}{1-K_b-K_r}P_b-2\frac{(1-K_r)K_r}{1-K_b-K_r}P_r\\
  1030. B' & = Y'+2(1-K_b)P_b
  1031. \end{align}
  1032. Parameters: $K_b,K_r$.
  1033. \item[$R'G'B'$ to $RGB$ (Output device gamma correction):]
  1034. \vspace{\baselineskip}\hfill
  1035. This conversion takes the non-linear $R'G'B'$ voltage levels and maps them to
  1036. linear light levels produced by the actual output device.
  1037. Note that this conversion is only that of the output device, and its inverse is
  1038. {\em not} that used by the input device.
  1039. Because a dim viewing environment is assumed in most television standards, the
  1040. overall gamma between the input and output devices is usually around $1.1$ to
  1041. $1.2$, and not a strict $1.0$.
  1042. For calibration with actual output devices, the model
  1043. \begin{align}
  1044. L & =(E'+\Delta)^\gamma
  1045. \end{align}
  1046. should be used, with $\Delta$ the free parameter and $\gamma$ held fixed to
  1047. the value specified in this document.
  1048. The conversion function presented here is an idealized version with $\Delta=0$.
  1049. \begin{align}
  1050. R & = R'^\gamma \\
  1051. G & = G'^\gamma \\
  1052. B & = B'^\gamma
  1053. \end{align}
  1054. Parameters: $\gamma$.
  1055. \item[$RGB$ to $R'G'B'$ (Input device gamma correction):]
  1056. \vspace{\baselineskip}\hfill
  1057. %TODO: Tag section as non-normative
  1058. This conversion takes linear light levels and maps them to the non-linear
  1059. voltage levels produced in the actual input device.
  1060. This information is merely informative.
  1061. It is not required for building a decoder or for converting between the various
  1062. formats and the actual output capabilities of a particular device.
  1063. A linear segment is introduced on the low end to reduce noise in dark areas of
  1064. the image.
  1065. The rest of the scale is adjusted so that the power segment of the curve
  1066. intersects the linear segment with the proper slope, and so that it still maps
  1067. 0 to 0 and 1 to 1.
  1068. \begin{align}
  1069. R' & = \left\{
  1070. \begin{array}{ll}
  1071. \alpha R, & 0\le R<\delta \\
  1072. (1+\epsilon)R^\beta-\epsilon, & \delta\le R\le1
  1073. \end{array}\right. \\
  1074. G' & = \left\{
  1075. \begin{array}{ll}
  1076. \alpha G, & 0\le G<\delta \\
  1077. (1+\epsilon)G^\beta-\epsilon, & \delta\le G\le1
  1078. \end{array}\right. \\
  1079. B' & = \left\{
  1080. \begin{array}{ll}
  1081. \alpha B, & 0\le B<\delta \\
  1082. (1+\epsilon)B^\beta-\epsilon, & \delta\le B\le1
  1083. \end{array}\right.
  1084. \end{align}
  1085. Parameters: $\beta$, $\alpha$, $\delta$, $\epsilon$.
  1086. \item[$RGB$ to CIE $XYZ$ (1931):]
  1087. \vspace{\baselineskip}\hfill
  1088. This conversion maps a device-dependent linear RGB space to the
  1089. device-independent linear CIE $XYZ$ space.
  1090. The parameters are the CIE chromaticity coordinates of the three
  1091. primaries---red, green, and blue---as well as the chromaticity coordinates
  1092. of the white point of the device.
  1093. This is how hardware manufacturers and standards typically describe a
  1094. particular $RGB$ space.
  1095. The math required to convert these parameters into a useful transformation
  1096. matrix is reproduced below.
  1097. \begin{align}
  1098. F & =
  1099. \left[\begin{array}{ccc}
  1100. \frac{x_r}{y_r} & \frac{x_g}{y_g} & \frac{x_b}{y_b} \\
  1101. 1 & 1 & 1 \\
  1102. \frac{1-x_r-y_r}{y_r} & \frac{1-x_g-y_g}{y_g} & \frac{1-x_b-y_b}{y_b}
  1103. \end{array}\right] \\
  1104. \left[\begin{array}{c}
  1105. s_r \\
  1106. s_g \\
  1107. s_b
  1108. \end{array}\right] & =
  1109. F^{-1}\left[\begin{array}{c}
  1110. \frac{x_w}{y_w} \\
  1111. 1 \\
  1112. \frac{1-x_w-y_w}{y_w}
  1113. \end{array}\right] \\
  1114. \left[\begin{array}{c}
  1115. X \\
  1116. Y \\
  1117. Z
  1118. \end{array}\right] & =
  1119. F\left[\begin{array}{c}
  1120. s_rR \\
  1121. s_gG \\
  1122. s_bB
  1123. \end{array}\right]
  1124. \end{align}
  1125. Parameters: $x_r,x_g,x_b,x_w, y_r,y_g,y_b,y_w$.
  1126. \end{description}
  1127. \section{Available Color Spaces}
  1128. \label{sec:colorspaces}
  1129. These are the color spaces currently defined for use by Theora video.
  1130. Each one has a short name, with which it is referred to in this document, and
  1131. a more detailed specification of the standards from which its parameters are
  1132. derived.
  1133. Some standards do not specify all the parameters necessary.
  1134. For these unspecified parameters, this document serves as the definition of
  1135. what should be used when encoding or decoding Theora video.
  1136. \subsection{Rec.~470M (Rec.~ITU-R~BT.470-6 System M/NTSC with
  1137. Rec.~ITU-R~BT.601-5)}
  1138. \label{sec:470m}
  1139. This color space is used by broadcast television and DVDs in much of the
  1140. Americas, Japan, Korea, and the Union of Myanmar \cite{rec470}.
  1141. This color space may also be used for System M/PAL (Brazil), with an
  1142. appropriate conversion supplied by the encoder to compensate for the
  1143. different gamma value.
  1144. See Section~\ref{sec:470bg} for an appropriate gamma value to assume for M/PAL
  1145. input.
  1146. In the US, studio monitors are adjusted to a D65 white point
  1147. ($x_w,y_w=0.313,0.329$).
  1148. In Japan, studio monitors are adjusted to a D white of 9300K
  1149. ($x_w,y_w=0.285,0.293$).
  1150. Rec.~470 does not specify a digital encoding of the color signals.
  1151. For Theora, Rec.~ITU-R~BT.601-5 \cite{rec601} is used, starting from the
  1152. $R'G'B'$ signals specified by Rec.~470.
  1153. Rec.~470 does not specify an input gamma function.
  1154. For Theora, the Rec.~709 \cite{rec709} input function is assumed.
  1155. This is the same as that specified by SMPTE 170M \cite{smpte170m}, which claims
  1156. to reflect modern practice in the creation of NTSC signals circa 1994.
  1157. The parameters for all the color transformations defined in
  1158. Section~\ref{sec:color-xforms} are given in Table~\ref{tab:470m}.
  1159. \begin{table}[htb]
  1160. \begin{align*}
  1161. \mathrm{Offset}_{Y,C_b,C_r} & = (16, 128, 128) \\
  1162. \mathrm{Excursion}_{Y,C_b,C_r} & = (219, 224, 224) \\
  1163. K_r & = 0.299 \\
  1164. K_b & = 0.114 \\
  1165. \gamma & = 2.2 \\
  1166. \beta & = 0.45 \\
  1167. \alpha & = 4.5 \\
  1168. \delta & = 0.018 \\
  1169. \epsilon & = 0.099 \\
  1170. x_r,y_r & = 0.67, 0.33 \\
  1171. x_g,y_g & = 0.21, 0.71 \\
  1172. x_b,y_b & = 0.14, 0.08 \\
  1173. \text{(Illuminant C) } x_w,y_w & = 0.310, 0.316 \\
  1174. \end{align*}
  1175. \caption{Rec.~470M Parameters}
  1176. \label{tab:470m}
  1177. \end{table}
  1178. \subsection{Rec.~470BG (Rec.~ITU-R~BT.470-6 Systems B and G with
  1179. Rec.~ITU-R~BT.601-5)}
  1180. \label{sec:470bg}
  1181. This color space is used by the PAL and SECAM systems in much of the rest of
  1182. the world \cite{rec470}
  1183. This can be used directly by systems (B, B1, D, D1, G, H, I, K, N)/PAL and (B,
  1184. D, G, H, K, K1, L)/SECAM\@.
  1185. \begin{verse}
  1186. {\bf Note:} the Rec.~470BG chromaticity values are different from those
  1187. specified in Rec.~470M\@.
  1188. When PAL and SECAM systems were first designed, they were based upon the same
  1189. primaries as NTSC\@.
  1190. However, as methods of making color picture tubes have changed, the primaries
  1191. used have changed as well.
  1192. The U.S. recommends using correction circuitry to approximate the existing,
  1193. standard NTSC primaries.
  1194. Current PAL and SECAM systems have standardized on primaries in accord with
  1195. more recent technology.
  1196. \end{verse}
  1197. Rec.~470 provisionally permits the use of the NTSC chromaticity values (given
  1198. in Section~\ref{sec:470m}) with legacy PAL and SECAM equipment.
  1199. In Theora, material must be decoded assuming the new PAL and SECAM primaries.
  1200. Material intended for display on old legacy devices should be converted by the
  1201. decoder.
  1202. The official Rec.~470BG specifies a gamma value of $\gamma=2.8$.
  1203. However, in practice this value is unrealistically high \cite{Poyn97}.
  1204. Rec.~470BG states that the overall system gamma should be approximately
  1205. $\gamma\beta=1.2$.
  1206. Since most cameras pre-correct with a gamma value of $\beta=0.45$,
  1207. this suggests an output device gamma of approximately $\gamma=2.67$.
  1208. This is the value recommended for use with PAL systems in Theora.
  1209. Rec.~470 does not specify a digital encoding of the color signals.
  1210. For Theora, Rec.~ITU-R~BT.601-5 \cite{rec601} is used, starting from the
  1211. $R'G'B'$ signals specified by Rec.~470.
  1212. Rec.~470 does not specify an input gamma function.
  1213. For Theora, the Rec 709 \cite{rec709} input function is assumed.
  1214. The parameters for all the color transformations defined in
  1215. Section~\ref{sec:color-xforms} are given in Table~\ref{tab:470bg}.
  1216. \begin{table}[htb]
  1217. \begin{align*}
  1218. \mathrm{Offset}_{Y,C_b,C_r} & = (16, 128, 128) \\
  1219. \mathrm{Excursion}_{Y,C_b,C_r} & = (219, 224, 224) \\
  1220. K_r & = 0.299 \\
  1221. K_b & = 0.114 \\
  1222. \gamma & = 2.67 \\
  1223. \beta & = 0.45 \\
  1224. \alpha & = 4.5 \\
  1225. \delta & = 0.018 \\
  1226. \epsilon & = 0.099 \\
  1227. x_r,y_r & = 0.64, 0.33 \\
  1228. x_g,y_g & = 0.29, 0.60 \\
  1229. x_b,y_b & = 0.15, 0.06 \\
  1230. \text{(D65) } x_w,y_w & = 0.313, 0.329 \\
  1231. \end{align*}
  1232. \caption{Rec.~470BG Parameters}
  1233. \label{tab:470bg}
  1234. \end{table}
  1235. \section{Pixel Formats}
  1236. \label{sec:pixfmts}
  1237. Theora supports several different pixel formats, each of which uses different
  1238. subsampling for the chroma planes relative to the luma plane.
  1239. A decoder may need to recover a full resolution chroma plane with samples
  1240. co-sited with the luma plane in order to convert to RGB for display or perform
  1241. other processing.
  1242. Decoders can assume that the chroma signal satisfies the Nyquist-Shannon
  1243. sampling theorem.
  1244. The ideal low-pass reconstruction filter this implies is not practical, but any
  1245. suitable approximation can be used, depending on the available computing
  1246. power.
  1247. Decoders MAY simply use a box filter, assigning to each luma sample the chroma
  1248. sample closest to it.
  1249. Encoders would not go wrong in assuming that this will be the most common
  1250. approach.
  1251. \subsection{4:4:4 Subsampling}
  1252. \label{sec:444}
  1253. All three color planes are stored at full resolution---each pixel has a $Y'$,
  1254. a $C_b$ and a $C_r$ value (see Figure~\ref{fig:pixel444}).
  1255. The samples in the different planes are all at co-located sites.
  1256. \begin{figure}[htbp]
  1257. \begin{center}
  1258. \includegraphics{pixel444}
  1259. \end{center}
  1260. \caption{Pixels encoded 4:4:4}
  1261. \label{fig:pixel444}
  1262. \end{figure}
  1263. % Figure.
  1264. %YRB YRB
  1265. %
  1266. %
  1267. %
  1268. %YRB YRB
  1269. %
  1270. %
  1271. %
  1272. \subsection{4:2:2 Subsampling}
  1273. \label{sec:422}
  1274. The $C_b$ and $C_r$ planes are stored with half the horizontal resolution of
  1275. the $Y'$ plane.
  1276. Thus, each of these planes has half the number of horizontal blocks as the luma
  1277. plane (see Figure~\ref{fig:pixel422}).
  1278. Similarly, they have half the number of horizontal super blocks, rounded up.
  1279. Macro blocks are defined across color planes, and so their number does not
  1280. change, but each macro block contains half as many chroma blocks.
  1281. The chroma samples are vertically aligned with the luma samples, but
  1282. horizontally centered between two luma samples.
  1283. Thus, each luma sample has a unique closest chroma sample.
  1284. A horizontal phase shift may be required to produce signals which use different
  1285. horizontal chroma sampling locations for compatibility with different systems.
  1286. \begin{figure}[htbp]
  1287. \begin{center}
  1288. \includegraphics{pixel422}
  1289. \end{center}
  1290. \caption{Pixels encoded 4:2:2}
  1291. \label{fig:pixel422}
  1292. \end{figure}
  1293. % Figure.
  1294. %Y RB Y Y RB Y
  1295. %
  1296. %
  1297. %
  1298. %Y RB Y Y RB Y
  1299. %
  1300. %
  1301. %
  1302. \subsection{4:2:0 Subsampling}
  1303. \label{sec:420}
  1304. The $C_b$ and $C_r$ planes are stored with half the horizontal and half the
  1305. vertical resolution of the $Y'$ plane.
  1306. Thus, each of these planes has half the number of horizontal blocks and half
  1307. the number of vertical blocks as the luma plane, for a total of one quarter
  1308. the number of blocks (see Figure~\ref{fig:pixel420}).
  1309. Similarly, they have half the number of horizontal super blocks and half the
  1310. number of vertical super blocks, rounded up.
  1311. Macro blocks are defined across color planes, and so their number does not
  1312. change, but each macro block contains within it one quarter as many
  1313. chroma blocks.
  1314. The chroma samples are vertically and horizontally centered between four luma
  1315. samples.
  1316. Thus, each luma sample has a unique closest chroma sample.
  1317. This is the same sub-sampling pattern used with JPEG, MJPEG, and MPEG-1, and
  1318. was inherited from VP3.
  1319. A horizontal or vertical phase shift may be required to produce signals which
  1320. use different chroma sampling locations for compatibility with different
  1321. systems.
  1322. \begin{figure}[htbp]
  1323. \begin{center}
  1324. \includegraphics{pixel420}
  1325. \end{center}
  1326. \caption{Pixels encoded 4:2:0}
  1327. \label{fig:pixel420}
  1328. \end{figure}
  1329. % Figure.
  1330. %Y Y Y Y
  1331. %
  1332. % RB RB
  1333. %
  1334. %Y Y Y Y
  1335. %
  1336. %
  1337. %
  1338. %Y Y Y Y
  1339. %
  1340. % RB RB
  1341. %
  1342. %Y Y Y Y
  1343. %
  1344. %
  1345. %
  1346. \subsection{Subsampling and the Picture Region}
  1347. Although the frame size must be an integral number of macro blocks, and thus
  1348. both the number of pixels and the number of blocks in each direction must be
  1349. even, no such requirement is made of the picture region.
  1350. Thus, when using subsampled pixel formats, careful attention must be paid to
  1351. which chroma samples correspond to which luma samples.
  1352. As mentioned above, for each pixel format, there is a unique chroma sample that
  1353. is the closest to each luma sample.
  1354. When cropping the chroma planes to the picture region, all the chroma samples
  1355. corresponding to a luma sample in the cropped picture region must be included.
  1356. Thus, when dividing the width or height of the picture region by two to obtain
  1357. the size of the subsampled chroma planes, they must be rounded up.
  1358. Furthermore, the sampling locations are defined relative to the frame,
  1359. {\em not} the picture region.
  1360. When using the 4:2:2 and 4:2:0 formats, the locations of chroma samples
  1361. relative to the luma samples depends on whether or not the X offset of the
  1362. picture region is odd.
  1363. If the offset is even, each column of chroma samples corresponds to two columns
  1364. of luma samples (see Figure~\ref{fig:pic_even} for an example).
  1365. The only exception is if the width is odd, in which case the last column
  1366. corresponds to only one column of luma samples (see Figure~\ref{fig:pic_even_odd}).
  1367. If the offset is odd, then the first column of chroma samples corresponds to
  1368. only one column of luma samples, while the remaining columns each correspond
  1369. to two (see Figure~\ref{fig:pic_odd}).
  1370. In this case, if the width is even, the last column again corresponds to only
  1371. one column of luma samples (see Figure~\ref{fig:pic_odd_even}).
  1372. A similar process is followed with the rows of a picture region of odd height
  1373. encoded in the 4:2:0 format.
  1374. If the Y offset is even, each row of chroma samples corresponds to two rows of
  1375. luma samples (see Figure~\ref{fig:pic_even}), except with an odd height, where
  1376. the last row corresponds to one row of chroma luna samples only (see
  1377. Figure~\ref{fig:pic_even_odd}).
  1378. If the offset is odd, then it is the first row of chroma samples which
  1379. corresponds to only one row of luma samples, while the remaining rows each
  1380. correspond to two (Figure~\ref{fig:pic_odd}), except with an even height,
  1381. where the last row also corresponds to one (Figure~\ref{fig:pic_odd_even}).
  1382. Encoders should be aware of these differences in the subsampling when using an
  1383. even or odd offset.
  1384. In the typical case, with an even width and height, where one expects two rows
  1385. or columns of luma samples for every row or column of chroma samples, the
  1386. encoder must take care to ensure that the offsets used are both even.
  1387. \begin{figure}[htbp]
  1388. \begin{center}
  1389. \includegraphics[width=\textwidth]{pic_even}
  1390. \end{center}
  1391. \caption{Pixel correspondence between color planes with even picture
  1392. offset and even picture size}
  1393. \label{fig:pic_even}
  1394. \end{figure}
  1395. \begin{figure}[htbp]
  1396. \begin{center}
  1397. \includegraphics[width=\textwidth]{pic_even_odd}
  1398. \end{center}
  1399. \caption{Pixel correspondence with even picture offset and
  1400. odd picture size}
  1401. \label{fig:pic_even_odd}
  1402. \end{figure}
  1403. \begin{figure}[htbp]
  1404. \begin{center}
  1405. \includegraphics[width=\textwidth]{pic_odd}
  1406. \end{center}
  1407. \caption{Pixel correspondence with odd picture offset and
  1408. odd picture size}
  1409. \label{fig:pic_odd}
  1410. \end{figure}
  1411. \begin{figure}[htbp]
  1412. \begin{center}
  1413. \includegraphics[width=\textwidth]{pic_odd_even}
  1414. \end{center}
  1415. \caption{Pixel correspondence with odd picture offset and
  1416. even picture size}
  1417. \label{fig:pic_odd_even}
  1418. \end{figure}
  1419. \chapter{Bitpacking Convention}
  1420. \label{sec:bitpacking}
  1421. \section{Overview}
  1422. The Theora codec uses relatively unstructured raw packets containing
  1423. binary integer fields of arbitrary width.
  1424. Logically, each packet is a bitstream in which bits are written one-by-one by
  1425. the encoder and then read one-by-one in the same order by the decoder.
  1426. Most current binary storage arrangements group bits into a native storage unit
  1427. of eight bits (octets), sixteen bits, thirty-two bits, or less commonly other
  1428. fixed sizes.
  1429. The Theora bitpacking convention specifies the correct mapping of the logical
  1430. packet bitstream into an actual representation in fixed-width units.
  1431. \subsection{Octets and Bytes}
  1432. In most contemporary architectures, a `byte' is synonymous with an `octect',
  1433. that is, eight bits.
  1434. For purposes of the bitpacking convention, a byte implies the smallest native
  1435. integer storage representation offered by a platform.
  1436. Modern file systems invariably offer bytes as the fundamental atom of storage.
  1437. The most ubiquitous architectures today consider a `byte' to be an octet.
  1438. Note, however, that the Theora bitpacking convention is still well defined for
  1439. any native byte size; an implementation can use the native bit-width of a
  1440. given storage system.
  1441. This document assumes that a byte is one octet for purposes of example only.
  1442. \subsection{Words and Byte Order}
  1443. A `word' is an integer size that is a grouped multiple of the byte size.
  1444. Most architectures consider a word to be a group of two, four, or eight bytes.
  1445. Each byte in the word can be ranked by order of `significance', e.g.\ the
  1446. significance of the bits in each byte when storing a binary integer in the
  1447. word.
  1448. Several byte orderings are possible in a word.
  1449. The common ones are
  1450. \begin{itemize}
  1451. \item{Big-endian:}
  1452. in which the most significant byte comes first, e.g.\ 3-2-1-0,
  1453. \item{Little-endian:}
  1454. in which the least significant byte comes first, e.g.\ 0-1-2-3, and
  1455. \item{Mixed-endian:}
  1456. one of the less-common orderings that cannot be put into the above two
  1457. categories, e.g.\ 3-1-2-0 or 0-2-1-3.
  1458. \end{itemize}
  1459. The Theora bitpacking convention specifies storage and bitstream manipulation
  1460. at the byte, not word, level.
  1461. Thus host word ordering is of a concern only during optimization, when writing
  1462. code that operates on a word of storage at a time rather than a byte.
  1463. Logically, bytes are always encoded and decoded in order from byte zero through
  1464. byte $n$.
  1465. \subsection{Bit Order}
  1466. A byte has a well-defined `least significant' bit (LSb), which is the only bit
  1467. set when the byte is storing the two's complement integer value $+1$.
  1468. A byte's `most significant' bit (MSb) is at the opposite end.
  1469. Bits in a byte are numbered from zero at the LSb to $n$ for the MSb, where
  1470. $n=7$ in an octet.
  1471. \section{Coding Bits into Bytes}
  1472. The Theora codec needs to encode arbitrary bit-width integers from zero to 32
  1473. bits wide into packets.
  1474. These integer fields are not aligned to the boundaries of the byte
  1475. representation; the next field is read at the bit position immediately
  1476. after the end of the previous field.
  1477. The decoder logically unpacks integers by first reading the MSb of a binary
  1478. integer from the logical bitstream, followed by the next most significant
  1479. bit, etc., until the required number of bits have been read.
  1480. When unpacking the bytes into bits, the decoder begins by reading the MSb of
  1481. the integer to be read from the most significant unread bit position of the
  1482. source byte, followed by the next-most significant bit position of the
  1483. destination integer, and so on up to the requested number of bits.
  1484. Note that this differs from the Vorbis I codec, which
  1485. begins decoding with the LSb of the source integer, reading it from the
  1486. LSb of the source byte.
  1487. When all the bits of the current source byte are read, decoding continues with
  1488. the MSb of the next byte.
  1489. Any unfilled bits in the last byte of the packet MUST be cleared to zero by the
  1490. encoder.
  1491. \subsection{Signedness}
  1492. The binary integers decoded by the above process may be either signed or
  1493. unsigned.
  1494. This varies from integer to integer, and this specification
  1495. indicates how each value should be interpreted as it is read.
  1496. That is, depending on context, the three bit binary pattern \bin{111} can be
  1497. taken to represent either `$7$' as an unsigned integer or `$-1$' as a signed,
  1498. two's complement integer.
  1499. \subsection{Encoding Example}
  1500. The following example shows the state of an (8-bit) byte stream after several
  1501. binary integers are encoded, including the location of the put pointer for the
  1502. next bit to write to and the total length of the stream in bytes.
  1503. Encode the 4 bit unsigned integer value `12' (\bin{1100}) into an empty byte
  1504. stream.
  1505. \begin{tabular}{r|ccccccccl}
  1506. \multicolumn{1}{r}{}& &&&&$\downarrow$&&&& \\
  1507. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1508. byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
  1509. 0 & 0 & 0 & 0 & $\leftarrow$ \\
  1510. byte 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1511. byte 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1512. byte 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1513. \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
  1514. byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
  1515. byte stream length: 1 byte
  1516. \end{tabular}
  1517. \vspace{\baselineskip}
  1518. Continue by encoding the 3 bit signed integer value `-1' (\bin{111}).
  1519. \begin{tabular}{r|ccccccccl}
  1520. \multicolumn{1}{r}{} &&&&&&&&$\downarrow$& \\
  1521. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1522. byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
  1523. \textbf{1} & \textbf{1} & \textbf{1} & 0 & $\leftarrow$ \\
  1524. byte 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1525. byte 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1526. byte 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1527. \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
  1528. byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
  1529. byte stream length: 1 byte
  1530. \end{tabular}
  1531. \vspace{\baselineskip}
  1532. Continue by encoding the 7 bit integer value `17' (\bin{0010001}).
  1533. \begin{tabular}{r|ccccccccl}
  1534. \multicolumn{1}{r}{} &&&&&&&$\downarrow$&& \\
  1535. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1536. byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
  1537. \textbf{1} & \textbf{1} & \textbf{1} & \textbf{0} & \\
  1538. byte 1 & \textbf{0} & \textbf{1} & \textbf{0} & \textbf{0} &
  1539. \textbf{0} & \textbf{1} & 0 & 0 & $\leftarrow$ \\
  1540. byte 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1541. byte 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1542. \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
  1543. byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
  1544. byte stream length: 2 bytes
  1545. \end{tabular}
  1546. \vspace{\baselineskip}
  1547. Continue by encoding the 13 bit integer value `6969' (\bin{11011\ 00111001}).
  1548. \begin{tabular}{r|ccccccccl}
  1549. \multicolumn{1}{r}{} &&&&$\downarrow$&&&&& \\
  1550. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1551. byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
  1552. \textbf{1} & \textbf{1} & \textbf{1} & \textbf{0} & \\
  1553. byte 1 & \textbf{0} & \textbf{1} & \textbf{0} & \textbf{0} &
  1554. \textbf{0} & \textbf{1} & \textbf{1} & \textbf{1} & \\
  1555. byte 2 & \textbf{0} & \textbf{1} & \textbf{1} & \textbf{0} &
  1556. \textbf{0} & \textbf{1} & \textbf{1} & \textbf{1} & \\
  1557. byte 3 & \textbf{0} & \textbf{0} & \textbf{1} &
  1558. 0 & 0 & 0 & 0 & 0 & $\leftarrow$ \\
  1559. \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
  1560. byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
  1561. byte stream length: 4 bytes
  1562. \end{tabular}
  1563. \vspace{\baselineskip}
  1564. \subsection{Decoding Example}
  1565. The following example shows the state of the (8-bit) byte stream encoded in the
  1566. previous example after several binary integers are decoded, including the
  1567. location of the get pointer for the next bit to read.
  1568. Read a two bit unsigned integer from the example encoded above.
  1569. \begin{tabular}{r|ccccccccl}
  1570. \multicolumn{1}{r}{} &&&$\downarrow$&&&&&& \\
  1571. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1572. byte 0 & \textbf{1} & \textbf{1} & 0 & 0 & 1 & 1 & 1 & 0 & $\leftarrow$ \\
  1573. byte 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 & \\
  1574. byte 2 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & \\
  1575. byte 3 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 &
  1576. byte stream length: 4 bytes
  1577. \end{tabular}
  1578. \vspace{\baselineskip}
  1579. Value read: 3 (\bin{11}).
  1580. Read another two bit unsigned integer from the example encoded above.
  1581. \begin{tabular}{r|ccccccccl}
  1582. \multicolumn{1}{r}{} &&&&&$\downarrow$&&&& \\
  1583. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1584. byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
  1585. 1 & 1 & 1 & 0 & $\leftarrow$ \\
  1586. byte 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 & \\
  1587. byte 2 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & \\
  1588. byte 3 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 &
  1589. byte stream length: 4 bytes
  1590. \end{tabular}
  1591. \vspace{\baselineskip}
  1592. Value read: 0 (\bin{00}).
  1593. Two things are worth noting here.
  1594. \begin{itemize}
  1595. \item
  1596. Although these four bits were originally written as a single four-bit integer,
  1597. reading some other combination of bit-widths from the bitstream is well
  1598. defined.
  1599. No artificial alignment boundaries are maintained in the bitstream.
  1600. \item
  1601. The first value is the integer `$3$' only because the context stated we were
  1602. reading an unsigned integer.
  1603. Had the context stated we were reading a signed integer, the returned value
  1604. would have been the integer `$-1$'.
  1605. \end{itemize}
  1606. \subsection{End-of-Packet Alignment}
  1607. The typical use of bitpacking is to produce many independent byte-aligned
  1608. packets which are embedded into a larger byte-aligned container structure,
  1609. such as an Ogg transport bitstream.
  1610. Externally, each bitstream encoded as a byte stream MUST begin and end on a
  1611. byte boundary.
  1612. Often, the encoded packet bitstream is not an integer number of bytes, and so
  1613. there is unused space in the last byte of a packet.
  1614. %r: I think the generality here is necessary to be consistent with our assertions
  1615. %r: elsewhere about being independent of transport and byte width
  1616. When a Theora encoder produces packets for embedding in a byte-aligned
  1617. container, unused space in the last byte of a packet is always zeroed during
  1618. the encoding process.
  1619. Thus, should this unused space be read, it will return binary zeroes.
  1620. There is no marker pattern or stuffing bits that will allow the decoder to
  1621. obtain the exact size, in bits, of the original bitstream.
  1622. This knowledge is not required for decoding.
  1623. Attempting to read past the end of an encoded packet results in an
  1624. `end-of-packet' condition.
  1625. Any further read operations after an `end-of-packet' condition shall also
  1626. return `end-of-packet'.
  1627. Unlike Vorbis, Theora does not use truncated packets as a normal mode of
  1628. operation.
  1629. Therefore if a decoder encounters the `end-of-packet' condition during normal
  1630. decoding, it may attempt to use the bits that were read to recover as much of
  1631. encoded data as possible, signal a warning or error, or both.
  1632. \subsection{Reading Zero Bit Integers}
  1633. Reading a zero bit integer returns the value `$0$' and does not increment
  1634. the stream pointer.
  1635. Reading to the end of the packet, but not past the end, so that an
  1636. `end-of-packet' condition is not triggered, and then reading a zero bit
  1637. integer shall succeed, returning `$0$', and not trigger an `end-of-packet'
  1638. condition.
  1639. Reading a zero bit integer after a previous read sets the `end-of-packet'
  1640. condition shall fail, also returning `end-of-packet'.
  1641. \chapter{Bitstream Headers}
  1642. \label{sec:headers}
  1643. A Theora bitstream begins with three header packets.
  1644. The header packets are, in order, the identification header, the comment
  1645. header, and the setup header.
  1646. All are required for decode compliance.
  1647. An end-of-packet condition encountered while decoding the identification or
  1648. setup header packets renders the stream undecodable.
  1649. An end-of-packet condition encountered while decode the comment header is a
  1650. non-fatal error condition, and MAY be ignored by a decoder.
  1651. \paragraph{VP3 Compatibility}
  1652. VP3 relies on the headers provided by its container, usually either AVI or
  1653. Quicktime.
  1654. As such, several parameters available in these headers are not available to VP3
  1655. streams.
  1656. These are indicated as they appear in the sections below.
  1657. \section{Common Header Decode}
  1658. \label{sub:common-header}
  1659. \begin{figure}[Htbp]
  1660. \begin{center}
  1661. \begin{verbatim}
  1662. 0 1 2 3
  1663. 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
  1664. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1665. | header type | `t' | `h' | `e' |
  1666. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1667. | `o' | `r' | `a' | data... |
  1668. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1669. | ... header-specific data ... |
  1670. | ... |
  1671. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1672. \end{verbatim}
  1673. \end{center}
  1674. \caption{Common Header Packet Layout}
  1675. \label{fig:commonheader}
  1676. \end{figure}
  1677. \paragraph{Input parameters:} None.
  1678. \paragraph{Output parameters:}\hfill\\*
  1679. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  1680. \multicolumn{1}{c}{Name} &
  1681. \multicolumn{1}{c}{Type} &
  1682. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  1683. \multicolumn{1}{c}{Signed?} &
  1684. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  1685. \bitvar{HEADERTYPE} & Integer & 8 & No & The type of the header being
  1686. decoded. \\
  1687. \bottomrule\end{tabularx}
  1688. \paragraph{Variables used:} None.
  1689. \medskip
  1690. Each header packet begins with the same header fields, which are decoded as
  1691. follows:
  1692. \begin{enumerate}
  1693. \item
  1694. Read an 8-bit unsigned integer as \bitvar{HEADERTYPE}.
  1695. If the most significant bit of this integer is not set, then stop.
  1696. This is not a header packet.
  1697. \item
  1698. Read 6 8-bit unsigned integers.
  1699. If these do not have the values \hex{74}, \hex{68}, \hex{65}, \hex{6F},
  1700. \hex{72}, and \hex{61}, respectively, then stop.
  1701. This stream is not decodable by this specification.
  1702. These values correspond to the ASCII values of the characters `t', `h', `e',
  1703. `o', `r', and `a'.
  1704. \end{enumerate}
  1705. Decode continues according to \bitvar{HEADERTYPE}.
  1706. The identification header is type \hex{80}, the comment header is type
  1707. \hex{81}, and the setup header is type \hex{82}.
  1708. These packets must occur in the order: identification, comment, setup.
  1709. %r: I clarified the initial-bit scheme here
  1710. %TBT: Dashes let the reader know they'll have to pick up the rest of the
  1711. %TBT: sentence after the explanatory phrase.
  1712. %TBT: Otherwise it just sounds like the bit must exist.
  1713. All header packets have the most significant bit of the type
  1714. field---which is the initial bit in the packet---set.
  1715. This distinguishes them from video data packets in which the first bit
  1716. is unset.
  1717. % extra header packets are a feature Dan argued for way back when for
  1718. % backward-compatible extensions (and icc colourspace for example)
  1719. % I think it's reasonable
  1720. %TBT: You can always just stick more stuff in the setup header.
  1721. Packets with other header types (\hex{83}--\hex{FF}) are reserved and MUST be
  1722. ignored.
  1723. \section{Identification Header Decode}
  1724. \label{sec:idheader}
  1725. \begin{figure}[Htbp]
  1726. \begin{center}
  1727. \begin{verbatim}
  1728. 0 1 2 3
  1729. 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
  1730. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1731. | 0x80 | `t' | `h' | `e' |
  1732. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1733. | `o' | `r' | `a' | VMAJ |
  1734. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1735. | VMIN | VREV | FMBW |
  1736. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1737. | FMBH | PICW... |
  1738. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1739. | ...PICW | PICH |
  1740. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1741. | PICX | PICY | FRN... |
  1742. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1743. | ...FRN | FRD... |
  1744. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1745. | ...FRD | PARN... |
  1746. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1747. | ...PARN | PARD |
  1748. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1749. | CS | NOMBR |
  1750. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1751. | QUAL | KFGSHIFT| PF| Res |
  1752. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1753. \end{verbatim}
  1754. \end{center}
  1755. \caption{Identification Header Packet}
  1756. \label{fig:idheader}
  1757. \end{figure}
  1758. \paragraph{Input parameters:} None.
  1759. \paragraph{Output parameters:}\hfill\\*
  1760. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  1761. \multicolumn{1}{c}{Name} &
  1762. \multicolumn{1}{c}{Type} &
  1763. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  1764. \multicolumn{1}{c}{Signed?} &
  1765. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  1766. \bitvar{VMAJ} & Integer & 8 & No & The major version number. \\
  1767. \bitvar{VMIN} & Integer & 8 & No & The minor version number. \\
  1768. \bitvar{VREV} & Integer & 8 & No & The version revision number. \\
  1769. \bitvar{FMBW} & Integer & 16 & No & The width of the frame in macro
  1770. blocks. \\
  1771. \bitvar{FMBH} & Integer & 16 & No & The height of the frame in macro
  1772. blocks. \\
  1773. \bitvar{NSBS} & Integer & 32 & No & The total number of super blocks in a
  1774. frame. \\
  1775. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  1776. frame. \\
  1777. \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
  1778. frame. \\
  1779. \bitvar{PICW} & Integer & 20 & No & The width of the picture region in
  1780. pixels. \\
  1781. \bitvar{PICH} & Integer & 20 & No & The height of the picture region in
  1782. pixels. \\
  1783. \bitvar{PICX} & Integer & 8 & No & The X offset of the picture region in
  1784. pixels. \\
  1785. \bitvar{PICY} & Integer & 8 & No & The Y offset of the picture region in
  1786. pixels. \\
  1787. \bitvar{FRN} & Integer & 32 & No & The frame-rate numerator. \\
  1788. \bitvar{FRD} & Integer & 32 & No & The frame-rate denominator. \\
  1789. \bitvar{PARN} & Integer & 24 & No & The pixel aspect-ratio numerator. \\
  1790. \bitvar{PARD} & Integer & 24 & No & The pixel aspect-ratio denominator. \\
  1791. \bitvar{CS} & Integer & 8 & No & The color space. \\
  1792. \bitvar{PF} & Integer & 2 & No & The pixel format. \\
  1793. \bitvar{NOMBR} & Integer & 24 & No & The nominal bitrate of the stream, in
  1794. bits per second. \\
  1795. \bitvar{QUAL} & Integer & 6 & No & The quality hint. \\
  1796. \bitvar{KFGSHIFT} & Integer & 5 & No & The amount to shift the key frame
  1797. number by in the granule position. \\
  1798. \bottomrule\end{tabularx}
  1799. \paragraph{Variables used:} None.
  1800. \medskip
  1801. The identification header is a short header with only a few fields used to
  1802. declare the stream definitively as Theora and provide detailed information
  1803. about the format of the fully decoded video data.
  1804. The identification header is decoded as follows:
  1805. \begin{enumerate}
  1806. \item
  1807. Decode the common header fields according to the procedure described in
  1808. Section~\ref{sub:common-header}.
  1809. If \bitvar{HEADERTYPE} returned by this procedure is not \hex{80}, then stop.
  1810. This packet is not the identification header.
  1811. \item
  1812. Read an 8-bit unsigned integer as \bitvar{VMAJ}.
  1813. If \bitvar{VMAJ} is not $3$, then stop.
  1814. This stream is not decodable according to this specification.
  1815. \item
  1816. Read an 8-bit unsigned integer as \bitvar{VMIN}.
  1817. If \bitvar{VMIN} is not $2$, then stop.
  1818. This stream is not decodable according to this specification.
  1819. \item
  1820. Read an 8-bit unsigned integer as \bitvar{VREV}.
  1821. If \bitvar{VREV} is greater than $1$, then this stream
  1822. may contain optional features or interpretational changes
  1823. documented in a future version of this specification.
  1824. Regardless of the value of \bitvar{VREV}, the stream is decodable
  1825. according to this specification.
  1826. \item
  1827. Read a 16-bit unsigned integer as \bitvar{FMBW}.
  1828. This MUST be greater than zero.
  1829. This specifies the width of the coded frame in macro blocks.
  1830. The actual width of the frame in pixels is $\bitvar{FMBW}*16$.
  1831. \item
  1832. Read a 16-bit unsigned integer as \bitvar{FMBH}.
  1833. This MUST be greater than zero.
  1834. This specifies the height of the coded frame in macro blocks.
  1835. The actual height of the frame in pixels is $\bitvar{FMBH}*16$.
  1836. \item
  1837. Read a 24-bit unsigned integer as \bitvar{PICW}.
  1838. This MUST be no greater than $(\bitvar{FMBW}*16)$.
  1839. Note that 24 bits are read, even though only 20 bits are sufficient to specify
  1840. any value of the picture width.
  1841. This is done to preserve octet alignment in this header, to allow for a
  1842. simplified parser implementation.
  1843. \item
  1844. Read a 24-bit unsigned integer as \bitvar{PICH}.
  1845. This MUST be no greater than $(\bitvar{FMBH}*16)$.
  1846. Together with \bitvar{PICW}, this specifies the size of the displayable picture
  1847. region within the coded frame.
  1848. See Figure~\ref{fig:pic-frame}.
  1849. Again, 24 bits are read instead of 20.
  1850. \item
  1851. Read an 8-bit unsigned integer as \bitvar{PICX}.
  1852. This MUST be no greater than $(\bitvar{FMBW}*16-\bitvar{PICX})$.
  1853. \item
  1854. Read an 8-bit unsigned integer as \bitvar{PICY}.
  1855. This MUST be no greater than $(\bitvar{FMBH}*16-\bitvar{PICY})$.
  1856. Together with \bitvar{PICX}, this specifies the location of the lower-left
  1857. corner of the displayable picture region.
  1858. See Figure~\ref{fig:pic-frame}.
  1859. \item
  1860. Read a 32-bit unsigned integer as \bitvar{FRN}.
  1861. This MUST be greater than zero.
  1862. \item
  1863. Read a 32-bit unsigned integer as \bitvar{FRD}.
  1864. This MUST be greater than zero.
  1865. Theora is a fixed-frame rate video codec.
  1866. Frames are sampled at the constant rate of $\frac{\bitvar{FRN}}{\bitvar{FRD}}$
  1867. frames per second.
  1868. The presentation time of the first frame is at zero seconds.
  1869. No mechanism is provided to specify a non-zero offset for the initial
  1870. frame.
  1871. \item
  1872. Read a 24-bit unsigned integer as \bitvar{PARN}.
  1873. \item
  1874. Read a 24-bit unsigned integer as \bitvar{PARD}.
  1875. Together with \bitvar{PARN}, these specify the aspect ratio of the pixels
  1876. within a frame, defined as the ratio of the physical width of a pixel to its
  1877. physical height.
  1878. This is given by the ratio $\bitvar{PARN}:\bitvar{PARD}$.
  1879. If either of these fields are zero, this indicates that pixel aspect ratio
  1880. information was not available to the encoder.
  1881. In this case it MAY be specified by the application via an external means, or
  1882. a default value of $1:1$ MAY be used.
  1883. \item
  1884. Read an 8-bit unsigned integer as \bitvar{CS}.
  1885. This is a value from an enumerated list of the available color spaces, given in
  1886. Table~\ref{tab:colorspaces}.
  1887. The `Undefined' value indicates that color space information was not available
  1888. to the encoder.
  1889. It MAY be specified by the application via an external means.
  1890. If a reserved value is given, a decoder MAY refuse to decode the stream.
  1891. \begin{table}[htbp]
  1892. \begin{center}
  1893. \begin{tabular*}{215pt}{cl@{\extracolsep{\fill}}c}\toprule
  1894. Value & Color Space \\\midrule
  1895. $0$ & Undefined. \\
  1896. $1$ & Rec.~470M (see Section~\ref{sec:470m}). \\
  1897. $2$ & Rec.~470BG (see Section~\ref{sec:470bg}). \\
  1898. $3$ & Reserved. \\
  1899. $\vdots$ & \\
  1900. $255$ & \\
  1901. \bottomrule\end{tabular*}
  1902. \end{center}
  1903. \caption{Enumerated List of Color Spaces}
  1904. \label{tab:colorspaces}
  1905. \end{table}
  1906. \item
  1907. Read a 24-bit unsigned integer as \bitvar{NOMBR} signifying a rate in bits
  1908. per second. Rates equal to or greater than $2^{24}-1$ bits per second are
  1909. represented as $2^{24}-1$.
  1910. The \bitvar{NOMBR} field is used only as a hint.
  1911. For pure VBR streams, this value may be considerably off.
  1912. The field MAY be set to zero to indicate that the encoder did not care to
  1913. speculate.
  1914. \item
  1915. Read a 6-bit unsigned integer as \bitvar{QUAL}.
  1916. This value is used to provide a hint as to the relative quality of the stream
  1917. when compared to others produced by the same encoder.
  1918. Larger values indicate higher quality.
  1919. This can be used, for example, to select among several streams containing the
  1920. same material encoded with different settings.
  1921. \item
  1922. Read a 5-bit unsigned integer as \bitvar{KFGSHIFT}.
  1923. The \bitvar{KFGSHIFT} is used to partition the granule position associated with
  1924. each packet into two different parts.
  1925. The frame number of the last key frame, starting from zero, is stored in the
  1926. upper $64-\bitvar{KFGSHIFT}$ bits, while the lower \bitvar{KFGSHIFT} bits
  1927. contain the number of frames since the last keyframe.
  1928. Complete details on the granule position mapping are specified in Section~REF.
  1929. \item
  1930. Read a 2-bit unsigned integer as \bitvar{PF}.
  1931. The \bitvar{PF} field contains a value from an enumerated list of the available
  1932. pixel formats, given in Table~\ref{tab:pixel-formats}.
  1933. If the reserved value $1$ is given, stop.
  1934. This stream is not decodable according to this specification.
  1935. \begin{table}[htbp]
  1936. \begin{center}
  1937. \begin{tabular*}{215pt}{cl@{\extracolsep{\fill}}c}\toprule
  1938. Value & Pixel Format \\\midrule
  1939. $0$ & 4:2:0 (see Section~\ref{sec:420}). \\
  1940. $1$ & Reserved. \\
  1941. $2$ & 4:2:2 (see Section~\ref{sec:422}). \\
  1942. $3$ & 4:4:4 (see Section~\ref{sec:444}). \\
  1943. \bottomrule\end{tabular*}
  1944. \end{center}
  1945. \caption{Enumerated List of Pixel Formats}
  1946. \label{tab:pixel-formats}
  1947. \end{table}
  1948. \item
  1949. Read a 3-bit unsigned integer.
  1950. These bits are reserved.
  1951. If this value is not zero, then stop.
  1952. This stream is not decodable according to this specification.
  1953. \item
  1954. Assign \bitvar{NSBS} a value according to \bitvar{PF}, as given by
  1955. Table~\ref{tab:nsbs-for-pf}.
  1956. \begin{table}[bt]
  1957. \begin{center}
  1958. \begin{tabular}{cc}\toprule
  1959. \bitvar{PF} & \bitvar{NSBS} \\\midrule
  1960. $0$ & $\begin{aligned}
  1961. &((\bitvar{FMBW}+1)//2)*((\bitvar{FMBH}+1)//2)\\
  1962. & +2*((\bitvar{FMBW}+3)//4)*((\bitvar{FMBH}+3)//4)
  1963. \end{aligned}$ \\\midrule
  1964. $2$ & $\begin{aligned}
  1965. &((\bitvar{FMBW}+1)//2)*((\bitvar{FMBH}+1)//2)\\
  1966. & +2*((\bitvar{FMBW}+3)//4)*((\bitvar{FMBH}+1)//2)
  1967. \end{aligned}$ \\\midrule
  1968. $3$ & $3*((\bitvar{FMBW}+1)//2)*((\bitvar{FMBH}+1)//2)$ \\
  1969. \bottomrule\end{tabular}
  1970. \end{center}
  1971. \caption{Number of Super Blocks for each Pixel Format}
  1972. \label{tab:nsbs-for-pf}
  1973. \end{table}
  1974. \item
  1975. Assign \bitvar{NBS} a value according to \bitvar{PF}, as given by
  1976. Table~\ref{tab:nbs-for-pf}.
  1977. \begin{table}[tb]
  1978. \begin{center}
  1979. \begin{tabular}{cc}\toprule
  1980. \bitvar{PF} & \bitvar{NBS} \\\midrule
  1981. $0$ & $6*\bitvar{FMBW}*\bitvar{FMBH}$ \\\midrule
  1982. $2$ & $8*\bitvar{FMBW}*\bitvar{FMBH}$ \\\midrule
  1983. $3$ & $12*\bitvar{FMBW}*\bitvar{FMBH}$ \\
  1984. \bottomrule\end{tabular}
  1985. \end{center}
  1986. \caption{Number of Blocks for each Pixel Format}
  1987. \label{tab:nbs-for-pf}
  1988. \end{table}
  1989. \item
  1990. Assign \bitvar{NMBS} the value $(\bitvar{FMBW}*\bitvar{FMBH})$.
  1991. \end{enumerate}
  1992. \paragraph{VP3 Compatibility}
  1993. VP3 does not correctly handle frame sizes that are not a multiple of 16.
  1994. Thus, \bitvar{PICW} and \bitvar{PICH} should be set to the frame width and
  1995. height in pixels, respectively, and \bitvar{PICX} and \bitvar{PICY} should be
  1996. set to zero.
  1997. VP3 headers do not specify a color space.
  1998. VP3 only supports the 4:2:0 pixel format.
  1999. \section{Comment Header}
  2000. \label{sec:commentheader}
  2001. The Theora comment header is the second of three header packets that begin a
  2002. Theora stream.
  2003. It is meant for short text comments, not aribtrary metadata; arbitrary metadata
  2004. belongs in a separate logical stream that provides greater structure and
  2005. machine parseability.
  2006. %r: I tried to morph this a little more in the direction of our
  2007. % application space
  2008. The comment field is meant to be used much like someone jotting a quick note on
  2009. the label of a video.
  2010. It should be a little information to remember the disc or tape by and explain it to
  2011. others; a short, to-the-point text note that can be more than a couple words,
  2012. but isn't going to be more than a short paragraph.
  2013. The essentials, in other words, whatever they turn out to be, e.g.:
  2014. %TODO: Example
  2015. The comment header is stored as a logical list of eight-bit clean vectors; the
  2016. number of vectors is bounded at $2^{32}-1$ and the length of each vector is
  2017. limited to $2^{32}-1$ bytes.
  2018. The vector length is encoded; the vector contents themselves are not null
  2019. terminated.
  2020. In addition to the vector list, there is a single vector for a vendor name,
  2021. also eight-bit clean with a length encoded in 32 bits.
  2022. %TODO: The 1.0 release of libtheora sets the vendor string to ...
  2023. \subsection{Comment Length Decode}
  2024. \label{sub:comment-len}
  2025. \begin{figure}
  2026. \begin{center}
  2027. \begin{tabular}{ | c | c | }
  2028. \hline
  2029. 4 byte length &
  2030. UTF-8 encoded string ...\\
  2031. \hline
  2032. \end{tabular}
  2033. \end{center}
  2034. \caption{Length encoded string layout}
  2035. \label{fig:comment-len}
  2036. \end{figure}
  2037. \paragraph{Input parameters:} None.
  2038. \paragraph{Output parameters:}\hfill\\*
  2039. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2040. \multicolumn{1}{c}{Name} &
  2041. \multicolumn{1}{c}{Type} &
  2042. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2043. \multicolumn{1}{c}{Signed?} &
  2044. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2045. \bitvar{LEN} & Integer & 32 & No & A single 32-bit length value. \\
  2046. \bottomrule\end{tabularx}
  2047. \paragraph{Variables used:}\hfill\\*
  2048. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2049. \multicolumn{1}{c}{Name} &
  2050. \multicolumn{1}{c}{Type} &
  2051. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2052. \multicolumn{1}{c}{Signed?} &
  2053. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2054. \locvar{LEN0} & Integer & 8 & No & The first octet of the string length. \\
  2055. \locvar{LEN1} & Integer & 8 & No & The second octet of the string length. \\
  2056. \locvar{LEN2} & Integer & 8 & No & The third octet of the string length. \\
  2057. \locvar{LEN3} & Integer & 8 & No & The fourth octet of the string
  2058. length. \\
  2059. \bottomrule\end{tabularx}
  2060. \medskip
  2061. A single comment vector is decoded as follows:
  2062. \begin{enumerate}
  2063. \item
  2064. Read an 8-bit unsigned integer as \locvar{LEN0}.
  2065. \item
  2066. Read an 8-bit unsigned integer as \locvar{LEN1}.
  2067. \item
  2068. Read an 8-bit unsigned integer as \locvar{LEN2}.
  2069. \item
  2070. Read an 8-bit unsigned integer as \locvar{LEN3}.
  2071. \item
  2072. Assign \bitvar{LEN} the value $(\locvar{LEN0}+(\locvar{LEN1}<<8)+
  2073. (\locvar{LEN2}<<16)+(\locvar{LEN3}<<24))$.
  2074. This construction is used so that on platforms with 8-bit bytes, the memory
  2075. organization of the comment header is identical with that of Vorbis I,
  2076. allowing for common parsing code despite the different bit packing
  2077. conventions.
  2078. \end{enumerate}
  2079. \subsection{Comment Header Decode}
  2080. \begin{figure}
  2081. \begin{center}
  2082. \begin{tabular}{ | c | }
  2083. \hline
  2084. vendor string \\ \hline
  2085. number of comments \\ \hline
  2086. comment string \\ \hline
  2087. comment string \\ \hline
  2088. ... \\
  2089. \hline
  2090. \end{tabular}
  2091. \end{center}
  2092. \caption{Comment Header Layout}
  2093. \label{fig:commentheader}
  2094. \end{figure}
  2095. \paragraph{Input parameters:} None.
  2096. \paragraph{Output parameters:}\hfill\\*
  2097. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2098. \multicolumn{1}{c}{Name} &
  2099. \multicolumn{1}{c}{Type} &
  2100. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2101. \multicolumn{1}{c}{Signed?} &
  2102. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2103. \bitvar{VENDOR} & \multicolumn{3}{l}{String} & The vendor string. \\
  2104. \bitvar{NCOMMENTS} & Integer & 32 & No & The number of user
  2105. comments. \\
  2106. \bitvar{COMMENTS} & \multicolumn{3}{l}{String Array} & A list of
  2107. \bitvar{NCOMMENTS} user comment values. \\
  2108. \bottomrule\end{tabularx}
  2109. \paragraph{Variables used:}\hfill\\*
  2110. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2111. \multicolumn{1}{c}{Name} &
  2112. \multicolumn{1}{c}{Type} &
  2113. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2114. \multicolumn{1}{c}{Signed?} &
  2115. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2116. \locvar{\ci} & Integer & 32 & No & The index of the current user
  2117. comment. \\
  2118. \bottomrule\end{tabularx}
  2119. \medskip
  2120. The complete comment header is decoded as follows:
  2121. \begin{enumerate}
  2122. \item
  2123. Decode the common header fields according to the procedure described in
  2124. Section~\ref{sub:common-header}.
  2125. If \bitvar{HEADERTYPE} returned by this procedure is not \hex{81}, then stop.
  2126. This packet is not the comment header.
  2127. \item
  2128. Decode the length of the vendor string using the procedure given in
  2129. Section~\ref{sub:comment-len} into \bitvar{LEN}.
  2130. \item
  2131. Read \bitvar{LEN} 8-bit unsigned integers.
  2132. \item
  2133. Set the string \bitvar{VENDOR} to the contents of these octets.
  2134. \item
  2135. Decode the number of user comments using the procedure given in
  2136. Section~\ref{sub:comment-len} into \bitvar{LEN}.
  2137. \item
  2138. Assign \bitvar{NCOMMENTS} the value stored in \bitvar{LEN}.
  2139. \item
  2140. For each consecutive value of \locvar{\ci} from $0$ to
  2141. $(\bitvar{NCOMMENTS}-1)$, inclusive:
  2142. \begin{enumerate}
  2143. \item
  2144. Decode the length of the current user comment using the procedure given in
  2145. Section~\ref{sub:comment-len} into \bitvar{LEN}.
  2146. \item
  2147. Read \bitvar{LEN} 8-bit unsigned integers.
  2148. \item
  2149. Set the string $\bitvar{COMMENTS}[\locvar{\ci}]$ to the contents of these
  2150. octets.
  2151. \end{enumerate}
  2152. \end{enumerate}
  2153. The comment header comprises the entirety of the second header packet.
  2154. Unlike the first header packet, it is not generally the only packet on the
  2155. second page and may span multiple pages.
  2156. The length of the comment header packet is (practically) unbounded.
  2157. The comment header packet is not optional; it must be present in the stream
  2158. even if it is logically empty.
  2159. %TODO: \paragraph{VP3 Compatibility}
  2160. \subsection{User Comment Format}
  2161. The user comment vectors are structured similarly to a UNIX environment
  2162. variable.
  2163. That is, comment fields consist of a field name and a corresponding value and
  2164. look like:
  2165. \begin{center}
  2166. \begin{tabular}{rcl}
  2167. $\bitvar{COMMENTS}[0]$ & = & ``TITLE=the look of Theora" \\
  2168. $\bitvar{COMMENTS}[1]$ & = & ``DIRECTOR=me"
  2169. \end{tabular}
  2170. \end{center}
  2171. The field name is case-insensitive and MUST consist of ASCII characters
  2172. \hex{20} through \hex{7D}, \hex{3D} (`=') excluded.
  2173. ASCII \hex{41} through \hex{5A} inclusive (characters `A'--`Z') are to be
  2174. considered equivalent to ASCII \hex{61} through \hex{7A} inclusive
  2175. (characters `a'--`z').
  2176. An entirely empty field name---one that is zero characters long---is not
  2177. disallowed.
  2178. The field name is immediately followed by ASCII \hex{3D} (`='); this equals
  2179. sign is used to terminate the field name.
  2180. The data immediately after \hex{3D} until the end of the vector is the eight-bit
  2181. clean value of the field contents encoded as a UTF-8 string~\cite{rfc2044}.
  2182. Field names MUST NOT be `internationalized'; this is a concession to
  2183. simplicity, not an attempt to exclude the majority of the world that doesn't
  2184. speak English.
  2185. Applications MAY wish to present internationalized versions of the standard
  2186. field names listed below to the user, but they are not to be stored in the
  2187. bitstream.
  2188. Field {\em contents}, however, use the UTF-8 character encoding to allow easy
  2189. representation of any language.
  2190. Individual `vendors' MAY use non-standard field names within reason.
  2191. The proper use of comment fields as human-readable notes has already been
  2192. explained.
  2193. Abuse will be discouraged.
  2194. There is no vendor-specific prefix to `non-standard' field names.
  2195. Vendors SHOULD make some effort to avoid arbitrarily polluting the common
  2196. namespace.
  2197. %"and other bodies"?
  2198. %If you're going to be that vague, you might as well not say anything at all.
  2199. Xiph.org and other bodies will generally collect and rationalize the more
  2200. useful tags to help with standardization.
  2201. Field names are not restricted to occur only once within a comment header.
  2202. %TODO: Example
  2203. \paragraph{Field Names}
  2204. %r should this be an appendix?
  2205. Below is a proposed, minimal list of standard field names with a description of
  2206. their intended use.
  2207. No field names are mandatory; a comment header may contain one or more, all, or
  2208. none of the names in this list.
  2209. \begin{description}
  2210. \item{TITLE:} Video name.
  2211. \item{ARTIST:} Filmmaker or other creator name.
  2212. \item{VERSION:} Subtitle, remix info, or other text distinguishing
  2213. versions of a video.
  2214. \item{DATE:} Date associated with the video. Implementations SHOULD attempt
  2215. to parse this field as an ISO 8601 date for machine interpretation and
  2216. conversion.
  2217. \item{LOCATION:} Location associated with the video. This is usually the
  2218. filming location for non-fiction works.
  2219. \item{COPYRIGHT:} Copyright statement.
  2220. \item{LICENSE:} Copyright and other licensing information.
  2221. Implementations wishing to do automatic parsing of e.g
  2222. of distribution terms SHOULD look here for a URL uniquely defining
  2223. the license. If no instance of this field is present, or if no
  2224. instance contains a parseable URL, and implementation MAY look
  2225. in the COPYRIGHT field for such a URL.
  2226. \item{ORGANIZATION:} Studio name, Publisher, or other organization
  2227. involved in the creation of the video.
  2228. \item{DIRECTOR:} Director or Filmmaker credit, similar to ARTIST.
  2229. \item{PRODUCER:} Producer credit for the video.
  2230. \item{COMPOSER:} Music credit for the video.
  2231. \item{ACTOR:} Acting credit for the video.
  2232. \item{TAG:} subject or category tag, keyword, or other content
  2233. classification labels. The value of each instance of this
  2234. field SHOULD be treated as a single label, with multiple
  2235. instances of the field for multiple tags. The value of
  2236. a single field SHOULD NOT be parsed into multiple tags
  2237. based on some internal delimeter.
  2238. \item{DESCRIPTION:} General description, summary, or blurb.
  2239. \end{description}
  2240. \section{Setup Header}
  2241. \label{sec:setupheader}
  2242. The Theora setup header contains the limit values used to drive the loop
  2243. filter, the base matrices and scale values used to build the dequantization
  2244. tables, and the Huffman tables used to unpack the DCT tokens.
  2245. Because the contents of this header are specific to Theora, no concessions have
  2246. been made to keep the fields octet-aligned for easy parsing.
  2247. \begin{figure}
  2248. \begin{center}
  2249. \begin{tabular}{ | c | }
  2250. \hline
  2251. common header block \\ \hline
  2252. loop filter table resolution \\ \hline
  2253. loop filter table \\ \hline
  2254. scale table resolution \\ \hline
  2255. AC scale table \\ \hline
  2256. DC scale table \\ \hline
  2257. number of base matricies \\ \hline
  2258. base quatization matricies \\ \hline
  2259. ... \\ \hline
  2260. quant range interpolation table \\ \hline
  2261. DCT token Huffman tables \\
  2262. \hline
  2263. \end{tabular}
  2264. \end{center}
  2265. \caption{Setup Header structure}
  2266. \label{fig:setupheader}
  2267. \end{figure}
  2268. \subsection{Loop Filter Limit Table Decode}
  2269. \label{sub:loop-filter-limits}
  2270. \paragraph{Input parameters:} None.
  2271. \paragraph{Output parameters:}\hfill\\*
  2272. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2273. \multicolumn{1}{c}{Name} &
  2274. \multicolumn{1}{c}{Type} &
  2275. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2276. \multicolumn{1}{c}{Signed?} &
  2277. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2278. \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
  2279. 7 & No & A 64-element array of loop filter limit
  2280. values. \\
  2281. \bottomrule\end{tabularx}
  2282. \paragraph{Variables used:}\hfill\\*
  2283. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2284. \multicolumn{1}{c}{Name} &
  2285. \multicolumn{1}{c}{Type} &
  2286. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2287. \multicolumn{1}{c}{Signed?} &
  2288. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2289. \locvar{\qi} & Integer & 6 & No & The quantization index. \\
  2290. \locvar{NBITS} & Integer & 3 & No & The size of values being read in the
  2291. current table. \\
  2292. \bottomrule\end{tabularx}
  2293. \medskip
  2294. This procedure decodes the table of loop filter limit values used to drive the
  2295. loop filter, which is described in Section~\ref{sub:loop-filter-limits}.
  2296. It is decoded as follows:
  2297. \begin{enumerate}
  2298. \item
  2299. Read a 3-bit unsigned integer as \locvar{NBITS}.
  2300. \item
  2301. For each consecutive value of \locvar{\qi} from $0$ to $63$, inclusive:
  2302. \begin{enumerate}
  2303. \item
  2304. Read an \locvar{NBITS}-bit unsigned integer as $\bitvar{LFLIMS}[\locvar{\qi}]$.
  2305. \end{enumerate}
  2306. \end{enumerate}
  2307. \paragraph{VP3 Compatibility}
  2308. The loop filter limit values are hardcoded in VP3.
  2309. The values used are given in Appendix~\ref{app:vp3-loop-filter-limits}.
  2310. \subsection{Quantization Parameters Decode}
  2311. \label{sub:quant-params}
  2312. \paragraph{Input parameters:} None.
  2313. \paragraph{Output parameters:}\hfill\\*
  2314. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2315. \multicolumn{1}{c}{Name} &
  2316. \multicolumn{1}{c}{Type} &
  2317. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2318. \multicolumn{1}{c}{Signed?} &
  2319. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2320. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2321. 16 & No & A 64-element array of scale values for
  2322. AC coefficients for each \qi\ value. \\
  2323. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2324. 16 & No & A 64-element array of scale values for
  2325. the DC coefficient for each \qi\ value. \\
  2326. \bitvar{NBMS} & Integer & 10 & No & The number of base matrices. \\
  2327. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2328. 8 & No & A $\bitvar{NBMS}\times 64$ array
  2329. containing the base matrices. \\
  2330. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2331. 6 & No & A $2\times 3$ array containing the
  2332. number of quant ranges for a given \qti\ and \pli, respectively.
  2333. This is at most $63$. \\
  2334. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2335. 6 & No & A $2\times 3\times 63$ array of the
  2336. sizes of each quant range for a given \qti\ and \pli, respectively.
  2337. Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
  2338. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2339. 9 & No & A $2\times 3\times 64$ array of the
  2340. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  2341. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
  2342. \bottomrule\end{tabularx}
  2343. \paragraph{Variables used:}\hfill\\*
  2344. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2345. \multicolumn{1}{c}{Name} &
  2346. \multicolumn{1}{c}{Type} &
  2347. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2348. \multicolumn{1}{c}{Signed?} &
  2349. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2350. \locvar{\qti} & Integer & 1 & No & A quantization type index.
  2351. See Table~\ref{tab:quant-types}.\\
  2352. \locvar{\qtj} & Integer & 1 & No & A quantization type index. \\
  2353. \locvar{\pli} & Integer & 2 & No & A color plane index.
  2354. See Table~\ref{tab:color-planes}.\\
  2355. \locvar{\plj} & Integer & 2 & No & A color plane index. \\
  2356. \locvar{\qi} & Integer & 6 & No & The quantization index. \\
  2357. \locvar{\ci} & Integer & 6 & No & The DCT coefficient index. \\
  2358. \locvar{\bmi} & Integer & 9 & No & The base matrix index. \\
  2359. \locvar{\qri} & Integer & 6 & No & The quant range index. \\
  2360. \locvar{NBITS} & Integer & 5 & No & The size of fields to read. \\
  2361. \locvar{NEWQR} & Integer & 1 & No & Flag that indicates a new set of quant
  2362. ranges will be defined. \\
  2363. \locvar{RPQR} & Integer & 1 & No & Flag that indicates the quant ranges to
  2364. copy will come from the same color plane. \\
  2365. \bottomrule\end{tabularx}
  2366. \medskip
  2367. The AC scale and DC scale values are defined in two simple tables with 64
  2368. values each, one for each \qi\ value.
  2369. The same scale values are used for every quantization type and color plane.
  2370. The base matrices for all quantization types and color planes are stored in a
  2371. single table.
  2372. These are then referenced by index in several sets of \term{quant ranges}.
  2373. The purpose of the quant ranges is to specify which base matrices are used for
  2374. which \qi\ values.
  2375. A set of quant ranges is defined for each quantization type and color plane.
  2376. To save space in the header, bit flags allow a set of quant ranges to be copied
  2377. from a previously defined set instead of being specified explicitly.
  2378. Every set except the first one can be copied from the immediately preceding
  2379. set.
  2380. Similarly, if the quantization type is not $0$, the set can be copied from the
  2381. set defined for the same color plane for the preceding quantization type.
  2382. This formulation allows compact representation of, for example, the same
  2383. set of quant ranges in both chroma channels, as is done in the original VP3,
  2384. or the same set of quant ranges in INTRA and INTER modes.
  2385. Each quant range is defined by a size and two base matrix indices, one for each
  2386. end of the range.
  2387. The base matrix for the end of one range is used as the start of the next
  2388. range, so that for $n$ ranges, $n+1$ base matrices are specified.
  2389. The base matrices for the \qi\ values between the two endpoints of the range
  2390. are generated by linear interpolation.
  2391. %TODO: figure
  2392. The location of the endpoints of each range is encoded by their size.
  2393. The \qi\ value for the left end-point is the sum of the sizes of all preceding
  2394. ranges, and the \qi\ value for the right end-point adds the size of the
  2395. current range.
  2396. Thus the sum of the sizes of all the ranges MUST be 63, so that the last range
  2397. falls on the last possible \qi\ value.
  2398. The complete set of quantization parameters are decoded as follows:
  2399. \begin{enumerate}
  2400. \item
  2401. Read a 4-bit unsigned integer.
  2402. Assign \locvar{NBITS} the value read, plus one.
  2403. \item
  2404. For each consecutive value of \locvar{\qi} from $0$ to $63$, inclusive:
  2405. \begin{enumerate}
  2406. \item
  2407. Read an \locvar{NBITS}-bit unsigned integer as
  2408. $\bitvar{ACSCALE}[\locvar{\qi}]$.
  2409. \end{enumerate}
  2410. \item
  2411. Read a 4-bit unsigned integer.
  2412. Assign \locvar{NBITS} the value read, plus one.
  2413. \item
  2414. For each consecutive value of \locvar{\qi} from $0$ to $63$, inclusive:
  2415. \begin{enumerate}
  2416. \item
  2417. Read an \locvar{NBITS}-bit unsigned integer as
  2418. $\bitvar{DCSCALE}[\locvar{\qi}]$.
  2419. \end{enumerate}
  2420. \item
  2421. Read a 9-bit unsigned integer.
  2422. Assign \bitvar{NBMS} the value decoded, plus one.
  2423. \bitvar{NBMS} MUST be no greater than 384.
  2424. \item
  2425. For each consecutive value of \locvar{\bmi} from $0$ to $(\bitvar{NBMS}-1)$,
  2426. inclusive:
  2427. \begin{enumerate}
  2428. \item
  2429. For each consecutive value of \locvar{\ci} from $0$ to $63$, inclusive:
  2430. \begin{enumerate}
  2431. \item
  2432. Read an 8-bit unsigned integer as $\bitvar{BMS}[\locvar{\bmi}][\locvar{\ci}]$.
  2433. \end{enumerate}
  2434. \end{enumerate}
  2435. \item
  2436. For each consecutive value of \locvar{\qti} from $0$ to $1$, inclusive:
  2437. \begin{enumerate}
  2438. \item
  2439. For each consecutive value of \locvar{\pli} from $0$ to $2$, inclusive:
  2440. \begin{enumerate}
  2441. \item
  2442. If $\locvar{\qti}>0$ or $\locvar{\pli}>0$, read a 1-bit unsigned integer as
  2443. \locvar{NEWQR}.
  2444. \item
  2445. Else, assign \locvar{NEWQR} the value one.
  2446. \item
  2447. If \locvar{NEWQR} is zero, then we are copying a previously defined set of
  2448. quant ranges.
  2449. In that case:
  2450. \begin{enumerate}
  2451. \item
  2452. If $\locvar{\qti}>0$, read a 1-bit unsigned integer as \locvar{RPQR}.
  2453. \item
  2454. Else, assign \locvar{RPQR} the value zero.
  2455. \item
  2456. If \locvar{RPQR} is one, assign \locvar{\qtj} the value $(\locvar{\qti}-1)$
  2457. and assign \locvar{\plj} the value \locvar{\pli}.
  2458. This selects the set of quant ranges defined for the same color plane as this
  2459. one, but for the previous quantization type.
  2460. \item
  2461. Else assign \locvar{\qtj} the value $(3*\locvar{\qti}+\locvar{\pli}-1)//3$ and
  2462. assign \locvar{\plj} the value $(\locvar{\pli}+2)\%3$.
  2463. This selects the most recent set of quant ranges defined.
  2464. \item
  2465. Assign $\bitvar{NQRS}[\locvar{\qti}][\locvar{\pli}]$ the value
  2466. $\bitvar{NQRS}[\locvar{\qtj}][\locvar{\plj}]$.
  2467. \item
  2468. Assign $\bitvar{QRSIZES}[\locvar{\qti}][\locvar{\pli}]$ the values in
  2469. $\bitvar{QRSIZES}[\locvar{\qtj}][\locvar{\plj}]$.
  2470. \item
  2471. Assign $\bitvar{QRBMIS}[\locvar{\qti}][\locvar{\pli}]$ the values in
  2472. $\bitvar{QRBMIS}[\locvar{\qtj}][\locvar{\plj}]$.
  2473. \end{enumerate}
  2474. \item
  2475. Else, \locvar{NEWQR} is one, which indicates that we are defining a new set of
  2476. quant ranges.
  2477. In that case:
  2478. \begin{enumerate}
  2479. \item
  2480. Assign $\locvar{\qri}$ the value zero.
  2481. \item
  2482. Assign $\locvar{\qi}$ the value zero.
  2483. \item
  2484. Read an $\ilog(\bitvar{NBMS}-1)$-bit unsigned integer as\\
  2485. $\bitvar{QRBMIS}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$.
  2486. If this is greater than or equal to \bitvar{NBMS}, stop.
  2487. The stream is undecodable.
  2488. \item
  2489. \label{step:qr-loop}
  2490. Read an $\ilog(62-\locvar{\qi})$-bit unsigned integer.
  2491. Assign\\ $\bitvar{QRSIZES}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$ the value
  2492. read, plus one.
  2493. \item
  2494. Assign \locvar{\qi} the value $\locvar{\qi}+
  2495. \bitvar{QRSIZES}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$.
  2496. \item
  2497. Assign \locvar{\qri} the value $\locvar{\qri}+1$.
  2498. \item
  2499. Read an $\ilog(\bitvar{NBMS}-1)$-bit unsigned integer as\\
  2500. $\bitvar{QRBMIS}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$.
  2501. \item
  2502. If \locvar{\qi} is less than 63, go back to step~\ref{step:qr-loop}.
  2503. \item
  2504. If \locvar{\qi} is greater than 63, stop.
  2505. The stream is undecodable.
  2506. \item
  2507. Assign $\bitvar{NQRS}[\locvar{\qti}][\locvar{\pli}]$ the value \locvar{\qri}.
  2508. \end{enumerate}
  2509. \end{enumerate}
  2510. \end{enumerate}
  2511. \end{enumerate}
  2512. \paragraph{VP3 Compatibility}
  2513. The quantization parameters are hardcoded in VP3.
  2514. The values used are given in Appendix~\ref{app:vp3-quant-params}.
  2515. \subsection{Computing a Quantization Matrix}
  2516. \label{sub:quant-mat}
  2517. \paragraph{Input parameters:}\hfill\\*
  2518. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2519. \multicolumn{1}{c}{Name} &
  2520. \multicolumn{1}{c}{Type} &
  2521. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2522. \multicolumn{1}{c}{Signed?} &
  2523. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2524. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2525. 16 & No & A 64-element array of scale values for
  2526. AC coefficients for each \qi\ value. \\
  2527. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2528. 16 & No & A 64-element array of scale values for
  2529. the DC coefficient for each \qi\ value. \\
  2530. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2531. 8 & No & A $\bitvar{NBMS}\times 64$ array
  2532. containing the base matrices. \\
  2533. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2534. 6 & No & A $2\times 3$ array containing the
  2535. number of quant ranges for a given \qti\ and \pli, respectively.
  2536. This is at most $63$. \\
  2537. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2538. 6 & No & A $2\times 3\times 63$ array of the
  2539. sizes of each quant range for a given \qti\ and \pli, respectively.
  2540. Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
  2541. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2542. 9 & No & A $2\times 3\times 64$ array of the
  2543. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  2544. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
  2545. \bitvar{\qti} & Integer & 1 & No & A quantization type index.
  2546. See Table~\ref{tab:quant-types}.\\
  2547. \bitvar{\pli} & Integer & 2 & No & A color plane index.
  2548. See Table~\ref{tab:color-planes}.\\
  2549. \bitvar{\qi} & Integer & 6 & No & The quantization index. \\
  2550. \bottomrule\end{tabularx}
  2551. \paragraph{Output parameters:}\hfill\\*
  2552. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2553. \multicolumn{1}{c}{Name} &
  2554. \multicolumn{1}{c}{Type} &
  2555. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2556. \multicolumn{1}{c}{Signed?} &
  2557. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2558. \bitvar{QMAT} & \multicolumn{1}{p{40pt}}{Integer array} &
  2559. 16 & No & A 64-element array of quantization
  2560. values for each DCT coefficient in natural order. \\
  2561. \bottomrule\end{tabularx}
  2562. \paragraph{Variables used:}\hfill\\*
  2563. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2564. \multicolumn{1}{c}{Name} &
  2565. \multicolumn{1}{c}{Type} &
  2566. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2567. \multicolumn{1}{c}{Signed?} &
  2568. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2569. \locvar{\ci} & Integer & 6 & No & The DCT coefficient index. \\
  2570. \locvar{\bmi} & Integer & 9 & No & The base matrix index. \\
  2571. \locvar{\bmj} & Integer & 9 & No & The base matrix index. \\
  2572. \locvar{\qri} & Integer & 6 & No & The quant range index. \\
  2573. \locvar{QISTART} & Integer & 6 & No & The left end-point of the \qi\ range. \\
  2574. \locvar{QIEND } & Integer & 6 & No & The right end-point of the \qi\ range. \\
  2575. \locvar{BM} & \multicolumn{1}{p{40pt}}{Integer array} &
  2576. 8 & No & A 64-element array containing the
  2577. interpolated base matrix. \\
  2578. \locvar{QMIN} & Integer & 16 & No & The minimum quantization value allowed
  2579. for the current coefficient. \\
  2580. \locvar{QSCALE} & Integer & 16 & No & The current scale value. \\
  2581. \bottomrule\end{tabularx}
  2582. \medskip
  2583. The following procedure can be used to generate a single quantization matrix
  2584. for a given quantization type, color plane, and \qi\ value, given the
  2585. quantization parameters decoded in Section~\ref{sub:quant-params}.
  2586. Note that the product of the scale value and the base matrix value is in units
  2587. of $100$ths of a pixel value, and thus is divided by $100$ to return it to
  2588. units of a single pixel value.
  2589. This value is then scaled by four, to match the scaling of the DCT output,
  2590. which is also a factor of four larger than the orthonormal version of the
  2591. transform.
  2592. \begin{enumerate}
  2593. \item
  2594. Assign \locvar{\qri} the index of a quant range such that
  2595. \begin{displaymath}
  2596. \bitvar{\qi} \ge \sum_{\qrj=0}^{\locvar{\qri}-1}
  2597. \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj],
  2598. \end{displaymath}
  2599. and
  2600. \begin{displaymath}
  2601. \bitvar{\qi} \le \sum_{\qrj=0}^{\locvar{\qri}}
  2602. \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj],
  2603. \end{displaymath}
  2604. where summation from $0$ to $-1$ is defined to be zero.
  2605. If there is more than one such value of $\locvar{\qri}$, i.e., if \bitvar{\qi}
  2606. lies on the boundary between two quant ranges, then the output will be the
  2607. same regardless of which one is chosen.
  2608. \item
  2609. Assign \locvar{QISTART} the value
  2610. \begin{displaymath}
  2611. \sum_{\qrj=0}^{\qri-1} \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj].
  2612. \end{displaymath}
  2613. \item
  2614. Assign \locvar{QIEND} the value
  2615. \begin{displaymath}
  2616. \sum_{\qrj=0}^{\qri} \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj].
  2617. \end{displaymath}
  2618. \item
  2619. Assign \locvar{\bmi} the value
  2620. $\bitvar{QRBMIS}[\bitvar{\qti}][\bitvar{\pli}][\qri]$.
  2621. \item
  2622. Assign \locvar{\bmj} the value
  2623. $\bitvar{QRBMIS}[\bitvar{\qti}][\bitvar{\pli}][\qri+1]$.
  2624. \item
  2625. For each consecutive value of \locvar{\ci} from $0$ to $63$, inclusive:
  2626. \begin{enumerate}
  2627. \item
  2628. Assign $\locvar{BM}[\locvar{\ci}]$ the value
  2629. \begin{displaymath}
  2630. \begin{split}
  2631. (&2*(\locvar{QIEND}-\bitvar{\qi})*\bitvar{BMS}[\locvar{\bmi}][\locvar{\ci}]\\
  2632. &+2*(\bitvar{\qi}-
  2633. \locvar{QISTART})*\bitvar{BMS}[\locvar{\bmj}][\locvar{\ci}]\\
  2634. &+\bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\locvar{\qri}])//
  2635. (2*\bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\locvar{\qri}])
  2636. \end{split}
  2637. \end{displaymath}
  2638. \item
  2639. Assign \locvar{QMIN} the value given by Table~\ref{tab:qmin} according to
  2640. \bitvar{\qti} and \locvar{\ci}.
  2641. \begin{table}[htbp]
  2642. \begin{center}
  2643. \begin{tabular}{clr}\toprule
  2644. Coefficient & \multicolumn{1}{c}{\bitvar{\qti}}
  2645. & \locvar{QMIN} \\\midrule
  2646. $\locvar{\ci}=0$ & $0$ (Intra) & $16$ \\
  2647. $\locvar{\ci}>0$ & $0$ (Intra) & $8$ \\
  2648. $\locvar{\ci}=0$ & $1$ (Inter) & $32$ \\
  2649. $\locvar{\ci}>0$ & $1$ (Inter) & $16$ \\
  2650. \bottomrule\end{tabular}
  2651. \end{center}
  2652. \caption{Minimum Quantization Values}
  2653. \label{tab:qmin}
  2654. \end{table}
  2655. \item
  2656. If \locvar{\ci} equals zero, assign $\locvar{QSCALE}$ the value
  2657. $\bitvar{DCSCALE}[\bitvar{\qi}]$.
  2658. \item
  2659. Else, assign $\locvar{QSCALE}$ the value
  2660. $\bitvar{ACSCALE}[\bitvar{\qi}]$.
  2661. \item
  2662. Assign $\bitvar{QMAT}[\locvar{\ci}]$ the value
  2663. \begin{displaymath}
  2664. \max(\locvar{QMIN},
  2665. \min((\locvar{QSCALE}*\locvar{BM}[\locvar{\ci}]//100)*4,4096)).
  2666. \end{displaymath}
  2667. \end{enumerate}
  2668. \end{enumerate}
  2669. \subsection{DCT Token Huffman Tables}
  2670. \label{sub:huffman-tables}
  2671. \paragraph{Input parameters:} None.
  2672. \paragraph{Output parameters:}\hfill\\*
  2673. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2674. \multicolumn{1}{c}{Name} &
  2675. \multicolumn{1}{c}{Type} &
  2676. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2677. \multicolumn{1}{c}{Signed?} &
  2678. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2679. \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
  2680. & An 80-element array of Huffman tables
  2681. with up to 32 entries each. \\
  2682. \bottomrule\end{tabularx}
  2683. \paragraph{Variables used:}\hfill\\*
  2684. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2685. \multicolumn{1}{c}{Name} &
  2686. \multicolumn{1}{c}{Type} &
  2687. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2688. \multicolumn{1}{c}{Signed?} &
  2689. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2690. \locvar{HBITS} & Bit string & 32 & No & A string of up to 32 bits. \\
  2691. \locvar{TOKEN} & Integer & 5 & No & A single DCT token value. \\
  2692. \locvar{ISLEAF} & Integer & 1 & No & Flag that indicates if the current
  2693. node of the tree being decoded is a leaf node. \\
  2694. \bottomrule\end{tabularx}
  2695. \medskip
  2696. The Huffman tables used to decode DCT tokens are stored in the setup header in
  2697. the form of a binary tree.
  2698. This enforces the requirements that the code be full---so that any sequence of
  2699. bits will produce a valid sequence of tokens---and that the code be
  2700. prefix-free so that there is no ambiguity when decoding.
  2701. One more restriction is placed on the tables that is not explicitly enforced by
  2702. the bitstream syntax, but nevertheless must be obeyed by compliant encoders.
  2703. There must be no more than 32 entries in a single table.
  2704. Note that this restriction along with the fullness requirement limit the
  2705. maximum size of a single Huffman code to 32 bits.
  2706. It is probably a good idea to enforce this latter consequence explicitly when
  2707. implementing the decoding procedure as a recursive algorithm, so as to prevent
  2708. a possible stack overflow given an invalid bitstream.
  2709. Although there are 32 different DCT tokens, and thus a normal table will have
  2710. exactly 32 entries, this is not explicitly required.
  2711. It is allowable to use a Huffman code that omits some---but not all---of the
  2712. possible token values.
  2713. It is also allowable, if not particularly useful, to specify multiple codes for
  2714. the same token value in a single table.
  2715. Note also that token values may appear in the tree in any order.
  2716. In particular, it is not safe to assume that token value zero (which ends a
  2717. single block), has a Huffman code of all zeros.
  2718. The tree is decoded as follows:
  2719. \begin{enumerate}
  2720. \item
  2721. For each consecutive value of \locvar{\hti} from $0$ to $79$, inclusive:
  2722. \begin{enumerate}
  2723. \item
  2724. Set \locvar{HBITS} to the empty string.
  2725. \item
  2726. \label{step:huff-tree-loop}
  2727. If \locvar{HBITS} is longer than 32 bits in length, stop.
  2728. The stream is undecodable.
  2729. \item
  2730. Read a 1-bit unsigned integer as \locvar{ISLEAF}.
  2731. \item
  2732. If \locvar{ISLEAF} is one:
  2733. \begin{enumerate}
  2734. \item
  2735. If the number of entries in table $\bitvar{HTS}[\locvar{\hti}]$ is already 32,
  2736. stop.
  2737. The stream is undecodable.
  2738. \item
  2739. Read a 5-bit unsigned integer as \locvar{TOKEN}.
  2740. \item
  2741. Add the pair $(\locvar{HBITS},\locvar{TOKEN})$ to Huffman table
  2742. $\bitvar{HTS}[\locvar{\hti}]$.
  2743. \end{enumerate}
  2744. \item
  2745. Otherwise:
  2746. \begin{enumerate}
  2747. \item
  2748. Add a `0' to the end of \locvar{HBITS}.
  2749. \item
  2750. Decode the `0' sub-tree using this procedure, starting from
  2751. step~\ref{step:huff-tree-loop}.
  2752. \item
  2753. Remove the `0' from the end of \locvar{HBITS} and add a `1' to the end of
  2754. \locvar{HBITS}.
  2755. \item
  2756. Decode the `1' sub-tree using this procedure, starting from
  2757. step~\ref{step:huff-tree-loop}.
  2758. \item
  2759. Remove the `1' from the end of \locvar{HBITS}.
  2760. \end{enumerate}
  2761. \end{enumerate}
  2762. \end{enumerate}
  2763. \paragraph{VP3 Compatibility}
  2764. The DCT token Huffman tables are hardcoded in VP3.
  2765. The values used are given in Appendix~\ref{app:vp3-huffman-tables}.
  2766. \subsection{Setup Header Decode}
  2767. \paragraph{Input parameters:} None.
  2768. \paragraph{Output parameters:}\hfill\\*
  2769. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2770. \multicolumn{1}{c}{Name} &
  2771. \multicolumn{1}{c}{Type} &
  2772. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2773. \multicolumn{1}{c}{Signed?} &
  2774. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2775. \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
  2776. 7 & No & A 64-element array of loop filter limit
  2777. values. \\
  2778. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2779. 16 & No & A 64-element array of scale values for
  2780. AC coefficients for each \qi\ value. \\
  2781. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2782. 16 & No & A 64-element array of scale values for
  2783. the DC coefficient for each \qi\ value. \\
  2784. \bitvar{NBMS} & Integer & 10 & No & The number of base matrices. \\
  2785. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2786. 8 & No & A $\bitvar{NBMS}\times 64$ array
  2787. containing the base matrices. \\
  2788. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2789. 6 & No & A $2\times 3$ array containing the
  2790. number of quant ranges for a given \qti\ and \pli, respectively.
  2791. This is at most $63$. \\
  2792. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2793. 6 & No & A $2\times 3\times 63$ array of the
  2794. sizes of each quant range for a given \qti\ and \pli, respectively.
  2795. Only the first $\bitvar{NQRS}[\qti][\pli]$ values will be used. \\
  2796. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2797. 9 & No & A $2\times 3\times 64$ array of the
  2798. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  2799. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values will be used. \\
  2800. \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
  2801. & An 80-element array of Huffman tables
  2802. with up to 32 entries each. \\
  2803. \bottomrule\end{tabularx}
  2804. \paragraph{Variables used:} None.
  2805. \medskip
  2806. The complete setup header is decoded as follows:
  2807. \begin{enumerate}
  2808. \item
  2809. Decode the common header fields according to the procedure described in
  2810. Section~\ref{sub:common-header}.
  2811. If \bitvar{HEADERTYPE} returned by this procedure is not \hex{82}, then stop.
  2812. This packet is not the setup header.
  2813. \item
  2814. Decode the loop filter limit value table using the procedure given in
  2815. Section~\ref{sub:loop-filter-limits} into \bitvar{LFLIMS}.
  2816. \item
  2817. Decode the quantization parameters using the procedure given in
  2818. Section~\ref{sub:quant-params}.
  2819. The results are stored in \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{NBMS},
  2820. \bitvar{BMS}, \bitvar{NQRS}, \bitvar{QRSIZES}, and \bitvar{QRBMIS}.
  2821. \item
  2822. Decode the DCT token Huffman tables using the procedure given in
  2823. Section~\ref{sub:huffman-tables} into \bitvar{HTS}.
  2824. \end{enumerate}
  2825. \chapter{Frame Decode}
  2826. This section describes the complete procedure necessary to decode a single
  2827. frame.
  2828. This begins with the frame header, followed by coded block flags, macro block
  2829. modes, motion vectors, block-level \qi\ values, and finally the DCT residual
  2830. tokens, which are used to reconstruct the frame.
  2831. \section{Frame Header Decode}
  2832. \label{sub:frame-header}
  2833. \paragraph{Input parameters:} None.
  2834. \paragraph{Output parameters:}\hfill\\*
  2835. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2836. \multicolumn{1}{c}{Name} &
  2837. \multicolumn{1}{c}{Type} &
  2838. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2839. \multicolumn{1}{c}{Signed?} &
  2840. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2841. \bitvar{FTYPE} & Integer & 1 & No & The frame type. \\
  2842. \bitvar{NQIS} & Integer & 2 & No & The number of \qi\ values. \\
  2843. \bitvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
  2844. 6 & No & An \bitvar{NQIS}-element array of
  2845. \qi\ values. \\
  2846. \bottomrule\end{tabularx}
  2847. \paragraph{Variables used:}\hfill\\*
  2848. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2849. \multicolumn{1}{c}{Name} &
  2850. \multicolumn{1}{c}{Type} &
  2851. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2852. \multicolumn{1}{c}{Signed?} &
  2853. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2854. \locvar{MOREQIS} & Integer & 1 & No & A flag indicating there are more
  2855. \qi\ values to be decoded. \\
  2856. \bottomrule\end{tabularx}
  2857. \medskip
  2858. The frame header selects which type of frame is being decoded, intra or inter,
  2859. and contains the list of \qi\ values that will be used in this frame.
  2860. The first \qi\ value will be used for {\em all} DC coefficients in all blocks.
  2861. This is done to ensure that DC prediction, which is done in the quantized
  2862. domain, works as expected.
  2863. The AC coefficients, however, can be dequantized using any \qi\ value on the
  2864. list, selected on a block-by-block basis.
  2865. \begin{enumerate}
  2866. \item
  2867. Read a 1-bit unsigned integer.
  2868. If the value read is not zero, stop.
  2869. This is not a data packet.
  2870. \item
  2871. Read a 1-bit unsigned integer as \bitvar{FTYPE}.
  2872. This is the type of frame being decoded, as given in
  2873. Table~\ref{tab:frame-type}.
  2874. If this is the first frame being decoded, this MUST be zero.
  2875. \begin{table}[htbp]
  2876. \begin{center}
  2877. \begin{tabular}{cl}\toprule
  2878. \bitvar{FTYPE} & Frame Type \\\midrule
  2879. $0$ & Intra frame \\
  2880. $1$ & Inter frame \\
  2881. \bottomrule\end{tabular}
  2882. \end{center}
  2883. \caption{Frame Type Values}
  2884. \label{tab:frame-type}
  2885. \end{table}
  2886. \item
  2887. Read in a 6-bit unsigned integer as $\bitvar{QIS}[0]$.
  2888. \item
  2889. Read a 1-bit unsigned integer as \locvar{MOREQIS}.
  2890. \item
  2891. If \locvar{MOREQIS} is zero, set \bitvar{NQIS} to 1.
  2892. \item
  2893. Otherwise:
  2894. \begin{enumerate}
  2895. \item
  2896. Read in a 6-bit unsigned integer as $\bitvar{QIS}[1]$.
  2897. \item
  2898. Read a 1-bit unsigned integer as \locvar{MOREQIS}.
  2899. \item
  2900. If \locvar{MOREQIS} is zero, set \bitvar{NQIS} to 2.
  2901. \item
  2902. Otherwise:
  2903. \begin{enumerate}
  2904. \item
  2905. Read in a 6-bit unsigned integer as $\bitvar{QIS}[2]$.
  2906. \item
  2907. Set \bitvar{NQIS} to 3.
  2908. \end{enumerate}
  2909. \end{enumerate}
  2910. \item
  2911. If \bitvar{FTYPE} is 0, read a 3-bit unsigned integer.
  2912. These bits are reserved.
  2913. If this value is not zero, stop.
  2914. This frame is not decodable according to this specification.
  2915. \end{enumerate}
  2916. \paragraph{VP3 Compatibility}
  2917. The precise format of the frame header is substantially different in Theora
  2918. than in VP3.
  2919. The original VP3 format includes a larger number of unused, reserved bits that
  2920. are required to be zero.
  2921. The original VP3 frame header also can contain only a single \qi\ value,
  2922. because VP3 does not support block-level \qi\ values and uses the same
  2923. \qi\ value for all the coefficients in a frame.
  2924. \section{Run-Length Encoded Bit Strings}
  2925. Two variations of run-length encoding are used to store sequences of bits for
  2926. the block coded flags and the block-level \qi\ values.
  2927. The procedures to decode these bit sequences are specified in the following two
  2928. sections.
  2929. \subsection{Long-Run Bit String Decode}
  2930. \label{sub:long-run}
  2931. \paragraph{Input parameters:}\hfill\\*
  2932. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2933. \multicolumn{1}{c}{Name} &
  2934. \multicolumn{1}{c}{Type} &
  2935. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2936. \multicolumn{1}{c}{Signed?} &
  2937. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2938. \bitvar{NBITS} & Integer & 36 & No & The number of bits to decode. \\
  2939. \bottomrule\end{tabularx}
  2940. \paragraph{Output parameters:}\hfill\\*
  2941. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2942. \multicolumn{1}{c}{Name} &
  2943. \multicolumn{1}{c}{Type} &
  2944. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2945. \multicolumn{1}{c}{Signed?} &
  2946. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2947. \bitvar{BITS} & Bit string & & & The decoded bits. \\
  2948. \bottomrule\end{tabularx}
  2949. \paragraph{Variables used:}\hfill\\*
  2950. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2951. \multicolumn{1}{c}{Name} &
  2952. \multicolumn{1}{c}{Type} &
  2953. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2954. \multicolumn{1}{c}{Signed?} &
  2955. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2956. \locvar{LEN} & Integer & 36 & No & The number of bits decoded so far. \\
  2957. \locvar{BIT} & Integer & 1 & No & The value associated with the current
  2958. run. \\
  2959. \locvar{RLEN} & Integer & 13 & No & The length of the current run. \\
  2960. \locvar{RBITS} & Integer & 4 & No & The number of extra bits needed to
  2961. decode the run length. \\
  2962. \locvar{RSTART} & Integer & 6 & No & The start of the possible run-length
  2963. values for a given Huffman code. \\
  2964. \locvar{ROFFS} & Integer & 12 & No & The offset from \locvar{RSTART} of the
  2965. run-length. \\
  2966. \bottomrule\end{tabularx}
  2967. \medskip
  2968. There is no practical limit to the number of consecutive 0's and 1's that can
  2969. be decoded with this procedure.
  2970. In reality, the run length is limited by the number of blocks in a single
  2971. frame, because more will never be requested.
  2972. A separate procedure described in Section~\ref{sub:short-run} is used when
  2973. there is a known limit on the maximum size of the runs.
  2974. For the first run, a single bit value is read, and then a Huffman-coded
  2975. representation of a run length is decoded, and that many copies of the bit
  2976. value are appended to the bit string.
  2977. For each consecutive run, the value of the bit is toggled instead of being read
  2978. from the bitstream.
  2979. The only exception is if the length of the previous run was 4129, the maximum
  2980. possible length encodable by the Huffman-coded representation.
  2981. In this case another bit value is read from the stream, to allow for
  2982. consecutive runs of 0's or 1's longer than this maximum.
  2983. Note that in both cases---for the first run and after a run of length 4129---if
  2984. no more bits are needed, then no bit value is read.
  2985. The complete decoding procedure is as follows:
  2986. \begin{enumerate}
  2987. \item
  2988. Assign \locvar{LEN} the value 0.
  2989. \item
  2990. Assign \bitvar{BITS} the empty string.
  2991. \item
  2992. If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
  2993. \bitvar{BITS}.
  2994. \item
  2995. Read a 1-bit unsigned integer as \locvar{BIT}.
  2996. \item
  2997. \label{step:long-run-loop}
  2998. Read a bit at a time until one of the Huffman codes given in
  2999. Table~\ref{tab:long-run} is recognized.
  3000. \begin{table}[htbp]
  3001. \begin{center}
  3002. \begin{tabular}{lrrl}\toprule
  3003. Huffman Code & \locvar{RSTART} & \locvar{RBITS} & Run Lengths \\\midrule
  3004. \bin{0} & $1$ & $0$ & $1$ \\
  3005. \bin{10} & $2$ & $1$ & $2\ldots 3$ \\
  3006. \bin{110} & $4$ & $1$ & $4\ldots 5$ \\
  3007. \bin{1110} & $6$ & $2$ & $6\ldots 9$ \\
  3008. \bin{11110} & $10$ & $3$ & $10\ldots 17$ \\
  3009. \bin{111110} & $18$ & $4$ & $18\ldots 33$ \\
  3010. \bin{111111} & $34$ & $12$ & $34\ldots 4129$ \\
  3011. \bottomrule\end{tabular}
  3012. \end{center}
  3013. \caption{Huffman Codes for Long Run Lengths}
  3014. \label{tab:long-run}
  3015. \end{table}
  3016. \item
  3017. Assign \locvar{RSTART} and \locvar{RBITS} the values given in
  3018. Table~\ref{tab:long-run} according to the Huffman code read.
  3019. \item
  3020. Read an \locvar{RBITS}-bit unsigned integer as \locvar{ROFFS}.
  3021. \item
  3022. Assign \locvar{RLEN} the value $(\locvar{RSTART}+\locvar{ROFFS})$.
  3023. \item
  3024. Append \locvar{RLEN} copies of \locvar{BIT} to \bitvar{BITS}.
  3025. \item
  3026. Add \locvar{RLEN} to the value \locvar{LEN}.
  3027. \locvar{LEN} MUST be less than or equal to \bitvar{NBITS}.
  3028. \item
  3029. If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
  3030. \bitvar{BITS}.
  3031. \item
  3032. If \locvar{RLEN} equals 4129, read a 1-bit unsigned integer as \locvar{BIT}.
  3033. \item
  3034. Otherwise, assign \locvar{BIT} the value $(1-\locvar{BIT})$.
  3035. \item
  3036. Continue decoding runs from step~\ref{step:long-run-loop}.
  3037. \end{enumerate}
  3038. \paragraph{VP3 Compatibility}
  3039. VP3 does not read a new bit value after decoding a run length of 4129.
  3040. This limits the maximum number of consecutive 0's or 1's to 4129 in
  3041. VP3-compatible streams.
  3042. For reasonable video sizes of $1920\times 1080$ or less in 4:2:0 format---the
  3043. only pixel format VP3 supports---this does not pose any problems because runs
  3044. longer than 4129 are not needed.
  3045. \subsection{Short-Run Bit String Decode}
  3046. \label{sub:short-run}
  3047. \paragraph{Input parameters:}\hfill\\*
  3048. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3049. \multicolumn{1}{c}{Name} &
  3050. \multicolumn{1}{c}{Type} &
  3051. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3052. \multicolumn{1}{c}{Signed?} &
  3053. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3054. \bitvar{NBITS} & Integer & 36 & No & The number of bits to decode. \\
  3055. \bottomrule\end{tabularx}
  3056. \paragraph{Output parameters:}\hfill\\*
  3057. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3058. \multicolumn{1}{c}{Name} &
  3059. \multicolumn{1}{c}{Type} &
  3060. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3061. \multicolumn{1}{c}{Signed?} &
  3062. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3063. \bitvar{BITS} & Bit string & & & The decoded bits. \\
  3064. \bottomrule\end{tabularx}
  3065. \paragraph{Variables used:}\hfill\\*
  3066. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3067. \multicolumn{1}{c}{Name} &
  3068. \multicolumn{1}{c}{Type} &
  3069. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3070. \multicolumn{1}{c}{Signed?} &
  3071. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3072. \locvar{LEN} & Integer & 36 & No & The number of bits decoded so far. \\
  3073. \locvar{BIT} & Integer & 1 & No & The value associated with the current
  3074. run. \\
  3075. \locvar{RLEN} & Integer & 13 & No & The length of the current run. \\
  3076. \locvar{RBITS} & Integer & 4 & No & The number of extra bits needed to
  3077. decode the run length. \\
  3078. \locvar{RSTART} & Integer & 6 & No & The start of the possible run-length
  3079. values for a given Huffman code. \\
  3080. \locvar{ROFFS} & Integer & 12 & No & The offset from \locvar{RSTART} of the
  3081. run-length. \\
  3082. \bottomrule\end{tabularx}
  3083. \medskip
  3084. This procedure is similar to the procedure outlined in
  3085. Section~\ref{sub:long-run}, except that the maximum number of consecutive 0's
  3086. or 1's is limited to 30.
  3087. This is the maximum run length needed when encoding a bit for each of the 16
  3088. blocks in a super block when it is known that not all the bits in a super
  3089. block are the same.
  3090. The complete decoding procedure is as follows:
  3091. \begin{enumerate}
  3092. \item
  3093. Assign \locvar{LEN} the value 0.
  3094. \item
  3095. Assign \bitvar{BITS} the empty string.
  3096. \item
  3097. If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
  3098. \bitvar{BITS}.
  3099. \item
  3100. Read a 1-bit unsigned integer as \locvar{BIT}.
  3101. \item
  3102. \label{step:short-run-loop}
  3103. Read a bit at a time until one of the Huffman codes given in
  3104. Table~\ref{tab:short-run} is recognized.
  3105. \begin{table}[htbp]
  3106. \begin{center}
  3107. \begin{tabular}{lrrl}\toprule
  3108. Huffman Code & \locvar{RSTART} & \locvar{RBITS} & Run Lengths \\\midrule
  3109. \bin{0} & $1$ & $1$ & $1\ldots 2$ \\
  3110. \bin{10} & $3$ & $1$ & $3\ldots 4$ \\
  3111. \bin{110} & $5$ & $1$ & $5\ldots 6$ \\
  3112. \bin{1110} & $7$ & $2$ & $7\ldots 10$ \\
  3113. \bin{11110} & $11$ & $2$ & $11\ldots 14$ \\
  3114. \bin{11111} & $15$ & $4$ & $15\ldots 30$ \\
  3115. \bottomrule\end{tabular}
  3116. \end{center}
  3117. \caption{Huffman Codes for Short Run Lengths}
  3118. \label{tab:short-run}
  3119. \end{table}
  3120. \item
  3121. Assign \locvar{RSTART} and \locvar{RBITS} the values given in
  3122. Table~\ref{tab:short-run} according to the Huffman code read.
  3123. \item
  3124. Read an \locvar{RBITS}-bit unsigned integer as \locvar{ROFFS}.
  3125. \item
  3126. Assign \locvar{RLEN} the value $(\locvar{RSTART}+\locvar{ROFFS})$.
  3127. \item
  3128. Append \locvar{RLEN} copies of \locvar{BIT} to \bitvar{BITS}.
  3129. \item
  3130. Add \locvar{RLEN} to the value \locvar{LEN}.
  3131. \locvar{LEN} MUST be less than or equal to \bitvar{NBITS}.
  3132. \item
  3133. If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
  3134. \bitvar{BITS}.
  3135. \item
  3136. Assign \locvar{BIT} the value $(1-\locvar{BIT})$.
  3137. \item
  3138. Continue decoding runs from step~\ref{step:short-run-loop}.
  3139. \end{enumerate}
  3140. \section{Coded Block Flags Decode}
  3141. \label{sub:coded-blocks}
  3142. \paragraph{Input parameters:}\hfill\\*
  3143. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3144. \multicolumn{1}{c}{Name} &
  3145. \multicolumn{1}{c}{Type} &
  3146. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3147. \multicolumn{1}{c}{Signed?} &
  3148. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3149. \bitvar{FTYPE} & Integer & 1 & No & The frame type. \\
  3150. \bitvar{NSBS} & Integer & 32 & No & The total number of super blocks in a
  3151. frame. \\
  3152. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  3153. frame. \\
  3154. \bottomrule\end{tabularx}
  3155. \paragraph{Output parameters:}\hfill\\*
  3156. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3157. \multicolumn{1}{c}{Name} &
  3158. \multicolumn{1}{c}{Type} &
  3159. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3160. \multicolumn{1}{c}{Signed?} &
  3161. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3162. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3163. 1 & No & An \bitvar{NBS}-element array of flags
  3164. indicating which blocks are coded. \\
  3165. \bottomrule\end{tabularx}
  3166. \paragraph{Variables used:}\hfill\\*
  3167. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3168. \multicolumn{1}{c}{Name} &
  3169. \multicolumn{1}{c}{Type} &
  3170. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3171. \multicolumn{1}{c}{Signed?} &
  3172. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3173. \locvar{NBITS} & Integer & 36 & No & The length of a bit string to decode. \\
  3174. \locvar{BITS} & Bit string & & & A decoded set of flags. \\
  3175. \locvar{SBPCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3176. 1 & No & An \bitvar{NSBS}-element array of flags
  3177. indicating whether or not each super block is partially coded. \\
  3178. \locvar{SBFCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3179. 1 & No & An \bitvar{NSBS}-element array of flags
  3180. indicating whether or not each non-partially coded super block is fully
  3181. coded. \\
  3182. \locvar{\sbi} & Integer & 32 & No & The index of the current super
  3183. block. \\
  3184. \locvar{\bi} & Integer & 36 & No & The index of the current block in coded
  3185. order. \\
  3186. \bottomrule\end{tabularx}
  3187. \medskip
  3188. This procedure determines which blocks are coded in a given frame.
  3189. In an intra frame, it marks all blocks coded.
  3190. In an inter frame, however, any or all of the blocks may remain uncoded.
  3191. The output is a list of bit flags, one for each block, marking it coded or not
  3192. coded.
  3193. It is important to note that flags are still decoded for any blocks which lie
  3194. entirely outside the picture region, even though they are not displayed.
  3195. Encoders MAY choose to code such blocks.
  3196. Decoders MUST faithfully reconstruct such blocks, because their contents can be
  3197. used for predictors in future frames.
  3198. Flags are \textit{not} decoded for portions of a super block which lie outside
  3199. the full frame, as there are no blocks in those regions.
  3200. The complete procedure is as follows:
  3201. \begin{enumerate}
  3202. \item
  3203. If \bitvar{FTYPE} is zero (intra frame):
  3204. \begin{enumerate}
  3205. \item
  3206. For each consecutive value of \locvar{\bi} from 0 to $(\locvar{NBS}-1)$, assign
  3207. $\bitvar{BCODED}[\locvar{\bi}]$ the value one.
  3208. \end{enumerate}
  3209. \item
  3210. Otherwise (inter frame):
  3211. \begin{enumerate}
  3212. \item
  3213. Assign \locvar{NBITS} the value \bitvar{NSBS}.
  3214. \item
  3215. Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
  3216. described in Section~\ref{sub:long-run}.
  3217. This represents the list of partially coded super blocks.
  3218. \item
  3219. For each consecutive value of \locvar{\sbi} from 0 to $(\locvar{NSBS}-1)$,
  3220. remove the bit at the head of the string \locvar{BITS} and assign it to
  3221. $\locvar{SBPCODED}[\locvar{\sbi}]$.
  3222. \item
  3223. Assign \locvar{NBITS} the total number of super blocks such that \\
  3224. $\locvar{SBPCODED}[\locvar{\sbi}]$ equals zero.
  3225. \item
  3226. Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
  3227. described in Section~\ref{sub:long-run}.
  3228. This represents the list of fully coded super blocks.
  3229. \item
  3230. For each consecutive value of \locvar{\sbi} from 0 to $(\locvar{NSBS}-1)$ such
  3231. that $\locvar{SBPCODED}[\locvar{\sbi}]$ equals zero, remove the bit at the
  3232. head of the string \locvar{BITS} and assign it to
  3233. $\locvar{SBFCODED}[\locvar{\sbi}]$.
  3234. \item
  3235. Assign \locvar{NBITS} the number of blocks contained in super blocks where
  3236. $\locvar{SBPCODED}[\locvar{\sbi}]$ equals one.
  3237. Note that this might {\em not} be equal to 16 times the number of partially
  3238. coded super blocks, since super blocks which overlap the edge of the frame
  3239. will have fewer than 16 blocks in them.
  3240. \item
  3241. Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
  3242. described in Section~\ref{sub:short-run}.
  3243. \item
  3244. For each block in coded order---indexed by \locvar{\bi}:
  3245. \begin{enumerate}
  3246. \item
  3247. Assign \locvar{\sbi} the index of the super block containing block
  3248. \locvar{\bi}.
  3249. \item
  3250. If $\locvar{SBPCODED}[\locvar{\sbi}]$ is zero, assign
  3251. $\bitvar{BCODED}[\locvar{\bi}]$ the value $\locvar{SBFCODED}[\locvar{\sbi}]$.
  3252. \item
  3253. Otherwise, remove the bit at the head of the string \locvar{BITS} and assign it
  3254. to $\bitvar{BCODED}[\locvar{\bi}]$.
  3255. \end{enumerate}
  3256. \end{enumerate}
  3257. \end{enumerate}
  3258. \section{Macro Block Coding Modes}
  3259. \label{sub:mb-modes}
  3260. \paragraph{Input parameters:}\hfill\\*
  3261. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3262. \multicolumn{1}{c}{Name} &
  3263. \multicolumn{1}{c}{Type} &
  3264. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3265. \multicolumn{1}{c}{Signed?} &
  3266. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3267. \bitvar{FTYPE} & Integer & 1 & No & The frame type. \\
  3268. \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
  3269. frame. \\
  3270. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  3271. frame. \\
  3272. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3273. 1 & No & An \bitvar{NBS}-element array of flags
  3274. indicating which blocks are coded. \\
  3275. \bottomrule\end{tabularx}
  3276. \paragraph{Output parameters:}\hfill\\*
  3277. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3278. \multicolumn{1}{c}{Name} &
  3279. \multicolumn{1}{c}{Type} &
  3280. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3281. \multicolumn{1}{c}{Signed?} &
  3282. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3283. \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3284. 3 & No & An \bitvar{NMBS}-element array of coding
  3285. modes for each macro block. \\
  3286. \bottomrule\end{tabularx}
  3287. \paragraph{Variables used:}\hfill\\*
  3288. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3289. \multicolumn{1}{c}{Name} &
  3290. \multicolumn{1}{c}{Type} &
  3291. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3292. \multicolumn{1}{c}{Signed?} &
  3293. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3294. \locvar{MSCHEME} & Integer & 3 & No & The mode coding scheme. \\
  3295. \locvar{MALPHABET} & \multicolumn{1}{p{40pt}}{Integer array}
  3296. & 3 & No & The list of modes corresponding to each
  3297. Huffman code. \\
  3298. \locvar{\mbi} & Integer & 32 & No & The index of the current macro
  3299. block. \\
  3300. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  3301. coded order. \\
  3302. \locvar{\mi} & Integer & 3 & No & The index of a Huffman code from
  3303. Table~\ref{tab:mode-codes}, starting from $0$. \\
  3304. \bottomrule\end{tabularx}
  3305. \medskip
  3306. In an intra frame, every macro block marked as coded in INTRA mode.
  3307. In an inter frame, however, a macro block can be coded in one of eight coding
  3308. modes, given in Table~\ref{tab:coding-modes}.
  3309. All of the blocks in all color planes contained in a macro block will be
  3310. assigned the coding mode of that macro block.
  3311. \begin{table}[htbp]
  3312. \begin{center}
  3313. \begin{tabular}{cl}\toprule
  3314. Index & Coding Mode \\\midrule
  3315. $0$ & INTER\_NOMV \\
  3316. $1$ & INTRA \\
  3317. $2$ & INTER\_MV \\
  3318. $3$ & INTER\_MV\_LAST \\
  3319. $4$ & INTER\_MV\_LAST2 \\
  3320. $5$ & INTER\_GOLDEN\_NOMV \\
  3321. $6$ & INTER\_GOLDEN\_MV \\
  3322. $7$ & INTER\_MV\_FOUR \\
  3323. \bottomrule\end{tabular}
  3324. \end{center}
  3325. \caption{Macro Block Coding Modes}
  3326. \label{tab:coding-modes}
  3327. \end{table}
  3328. An important thing to note is that a coding mode is only stored in the
  3329. bitstream for a macro block if it has at least one {\em luma} block coded.
  3330. A macro block that contains coded blocks in the chroma planes, but not in the
  3331. luma plane, MUST be coded in INTER\_NOMV mode.
  3332. Thus, no coding mode needs to be decoded for such a macro block.
  3333. Coding modes are encoded using one of eight different schemes.
  3334. Schemes 0 through 6 use the same simple Huffman code to represent the mode
  3335. numbers, as given in Table~\ref{tab:mode-codes}.
  3336. The difference in the schemes is the mode number assigned to each code.
  3337. Scheme 0 uses an assignment specified in the bitstream, while schemes 1--6 use
  3338. a fixed assignment, also given in Table~\ref{tab:mode-codes}.
  3339. Scheme 7 simply codes each mode directly in the bitstream using three bits.
  3340. \begin{table}[htbp]
  3341. \begin{center}
  3342. \begin{tabular}{lccccccc}\toprule
  3343. Scheme & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & $7$ \\\cmidrule{2-7}
  3344. Huffman Code & \multicolumn{6}{c}{Coding Mode} & \locvar{\mi} \\\midrule
  3345. \bin{0} & $3$ & $3$ & $3$ & $3$ & $0$ & $0$ & $0$ \\
  3346. \bin{10} & $4$ & $4$ & $2$ & $2$ & $3$ & $5$ & $1$ \\
  3347. \bin{110} & $2$ & $0$ & $4$ & $0$ & $4$ & $3$ & $2$ \\
  3348. \bin{1110} & $0$ & $2$ & $0$ & $4$ & $2$ & $4$ & $3$ \\
  3349. \bin{11110} & $1$ & $1$ & $1$ & $1$ & $1$ & $2$ & $4$ \\
  3350. \bin{111110} & $5$ & $5$ & $5$ & $5$ & $5$ & $1$ & $5$ \\
  3351. \bin{1111110} & $6$ & $6$ & $6$ & $6$ & $6$ & $6$ & $6$ \\
  3352. \bin{1111111} & $7$ & $7$ & $7$ & $7$ & $7$ & $7$ & $7$ \\
  3353. \bottomrule\end{tabular}
  3354. \end{center}
  3355. \caption{Macro Block Mode Schemes}
  3356. \label{tab:mode-codes}
  3357. \end{table}
  3358. \begin{enumerate}
  3359. \item
  3360. If \bitvar{FTYPE} is 0 (intra frame):
  3361. \begin{enumerate}
  3362. \item
  3363. For each consecutive value of \locvar{\mbi} from 0 to $(\bitvar{NMBS}-1)$,
  3364. inclusive, assign $\bitvar{MBMODES}[\mbi]$ the value 1 (INTRA).
  3365. \end{enumerate}
  3366. \item
  3367. Otherwise (inter frame):
  3368. \begin{enumerate}
  3369. \item
  3370. Read a 3-bit unsigned integer as \locvar{MSCHEME}.
  3371. \item
  3372. If \locvar{MSCHEME} is 0:
  3373. \begin{enumerate}
  3374. \item
  3375. For each consecutive value of \locvar{MODE} from 0 to 7, inclusive:
  3376. \begin{enumerate}
  3377. \item
  3378. Read a 3-bit unsigned integer as \locvar{\mi}.
  3379. \item
  3380. Assign $\locvar{MALPHABET}[\mi]$ the value \locvar{MODE}.
  3381. \end{enumerate}
  3382. \end{enumerate}
  3383. \item
  3384. Otherwise, if \locvar{MSCHEME} is not 7, assign the entries of
  3385. \locvar{MALPHABET} the values in the corresponding column of
  3386. Table~\ref{tab:mode-codes}.
  3387. \item
  3388. For each consecutive macro block in coded order (cf.
  3389. Section~\ref{sec:mbs})---indexed by \locvar{\mbi}:
  3390. \begin{enumerate}
  3391. \item
  3392. If a block \locvar{\bi} in the luma plane of macro block \locvar{\mbi} exists
  3393. such that $\bitvar{BCODED}[\locvar{\bi}]$ is 1:
  3394. \begin{enumerate}
  3395. \item
  3396. If \locvar{MSCHEME} is not 7, read one bit at a time until one of the Huffman
  3397. codes in Table~\ref{tab:mode-codes} is recognized, and assign
  3398. $\bitvar{MBMODES}[\locvar{\mbi}]$ the value
  3399. $\locvar{MALPHABET}[\locvar{\mi}]$, where \locvar{\mi} is the index of the
  3400. Huffman code decoded.
  3401. \item
  3402. Otherwise, read a 3-bit unsigned integer as $\bitvar{MBMODES}[\locvar{\mbi}]$.
  3403. \end{enumerate}
  3404. \item
  3405. Otherwise, if no luma-plane blocks in the macro block are coded, assign
  3406. $\bitvar{MBMODES}[\locvar{\mbi}]$ the value 0 (INTER\_NOMV).
  3407. \end{enumerate}
  3408. \end{enumerate}
  3409. \end{enumerate}
  3410. \section{Motion Vectors}
  3411. In an intra frame, no motion vectors are used, and so motion vector decoding is
  3412. skipped.
  3413. In an inter frame, however, many of the inter coding modes require a motion
  3414. vector in order to specify an offset into the reference frame from which to
  3415. predict a block.
  3416. These procedures assigns such a motion vector to every block.
  3417. \subsection{Motion Vector Decode}
  3418. \label{sub:mv-decode}
  3419. \paragraph{Input parameters:}\hfill\\*
  3420. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3421. \multicolumn{1}{c}{Name} &
  3422. \multicolumn{1}{c}{Type} &
  3423. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3424. \multicolumn{1}{c}{Signed?} &
  3425. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3426. \bitvar{MVMODE} & Integer & 1 & No & The motion vector decoding method. \\
  3427. \bottomrule\end{tabularx}
  3428. \paragraph{Output parameters:}\hfill\\*
  3429. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3430. \multicolumn{1}{c}{Name} &
  3431. \multicolumn{1}{c}{Type} &
  3432. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3433. \multicolumn{1}{c}{Signed?} &
  3434. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3435. \bitvar{MVX} & Integer & 6 & Yes & The X component of the motion
  3436. vector. \\
  3437. \bitvar{MVY} & Integer & 6 & Yes & The Y component of the motion
  3438. vector. \\
  3439. \bottomrule\end{tabularx}
  3440. \paragraph{Variables used:}\hfill\\*
  3441. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3442. \multicolumn{1}{c}{Name} &
  3443. \multicolumn{1}{c}{Type} &
  3444. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3445. \multicolumn{1}{c}{Signed?} &
  3446. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3447. \locvar{MVSIGN} & Integer & 1 & No & The sign of the motion vector component
  3448. just decoded. \\
  3449. \bottomrule\end{tabularx}
  3450. \medskip
  3451. The individual components of a motion vector can be coded using one of two
  3452. methods.
  3453. The first uses a variable length Huffman code, given in
  3454. Table~\ref{tab:mv-huff-codes}.
  3455. The second encodes the magnitude of the component directly in 5 bits, and the
  3456. sign in one bit.
  3457. Note that in this case there are two representations for the value zero.
  3458. For compatibility with VP3, a sign bit is read even if the magnitude read is
  3459. zero.
  3460. One scheme is chosen and used for the entire frame.
  3461. Each component can take on integer values from $-31\ldots 31$, inclusive, at
  3462. half-pixel resolution, i.e. $-15.5\ldots 15.5$ pixels in the luma plane.
  3463. For each subsampled axis in the chroma planes, the corresponding motion vector
  3464. component is interpreted as being at quarter-pixel resolution, i.e.
  3465. $-7.75\ldots 7.75$ pixels.
  3466. The precise details of how these vectors are used to compute predictors for
  3467. each block are described in Section~\ref{sec:predictors}.
  3468. \begin{table}[ht]
  3469. \begin{center}
  3470. \begin{tabular}{lrlr}\toprule
  3471. Huffman Code & Value & Huffman Code & Value \\\midrule
  3472. \bin{000} & $0$ \\
  3473. \bin{001} & $1$ & \bin{010} & $-1$ \\
  3474. \bin{0110} & $2$ & \bin{0111} & $-2$ \\
  3475. \bin{1000} & $3$ & \bin{1001} & $-3$ \\
  3476. \bin{101000} & $4$ & \bin{101001} & $-4$ \\
  3477. \bin{101010} & $5$ & \bin{101011} & $-5$ \\
  3478. \bin{101100} & $6$ & \bin{101101} & $-6$ \\
  3479. \bin{101110} & $7$ & \bin{101111} & $-7$ \\
  3480. \bin{1100000} & $8$ & \bin{1100001} & $-8$ \\
  3481. \bin{1100010} & $9$ & \bin{1100011} & $-9$ \\
  3482. \bin{1100100} & $10$ & \bin{1100101} & $-10$ \\
  3483. \bin{1100110} & $11$ & \bin{1100111} & $-11$ \\
  3484. \bin{1101000} & $12$ & \bin{1101001} & $-12$ \\
  3485. \bin{1101010} & $13$ & \bin{1101011} & $-13$ \\
  3486. \bin{1101100} & $14$ & \bin{1101101} & $-14$ \\
  3487. \bin{1101110} & $15$ & \bin{1101111} & $-15$ \\
  3488. \bin{11100000} & $16$ & \bin{11100001} & $-16$ \\
  3489. \bin{11100010} & $17$ & \bin{11100011} & $-17$ \\
  3490. \bin{11100100} & $18$ & \bin{11100101} & $-18$ \\
  3491. \bin{11100110} & $19$ & \bin{11100111} & $-19$ \\
  3492. \bin{11101000} & $20$ & \bin{11101001} & $-20$ \\
  3493. \bin{11101010} & $21$ & \bin{11101011} & $-21$ \\
  3494. \bin{11101100} & $22$ & \bin{11101101} & $-22$ \\
  3495. \bin{11101110} & $23$ & \bin{11101111} & $-23$ \\
  3496. \bin{11110000} & $24$ & \bin{11110001} & $-24$ \\
  3497. \bin{11110010} & $25$ & \bin{11110011} & $-25$ \\
  3498. \bin{11110100} & $26$ & \bin{11110101} & $-26$ \\
  3499. \bin{11110110} & $27$ & \bin{11110111} & $-27$ \\
  3500. \bin{11111000} & $28$ & \bin{11111001} & $-28$ \\
  3501. \bin{11111010} & $29$ & \bin{11111011} & $-29$ \\
  3502. \bin{11111100} & $30$ & \bin{11111101} & $-30$ \\
  3503. \bin{11111110} & $31$ & \bin{11111111} & $-31$ \\
  3504. \bottomrule\end{tabular}
  3505. \end{center}
  3506. \caption{Huffman Codes for Motion Vector Components}
  3507. \label{tab:mv-huff-codes}
  3508. \end{table}
  3509. A single motion vector is decoded is follows:
  3510. \begin{enumerate}
  3511. \item
  3512. If \bitvar{MVMODE} is 0:
  3513. \begin{enumerate}
  3514. \item
  3515. Read 1 bit at a time until one of the Huffman codes in
  3516. Table~\ref{tab:mv-huff-codes} is recognized, and assign the value to
  3517. \locvar{MVX}.
  3518. \item
  3519. Read 1 bit at a time until one of the Huffman codes in
  3520. Table~\ref{tab:mv-huff-codes} is recognized, and assign the value to
  3521. \locvar{MVY}.
  3522. \end{enumerate}
  3523. \item
  3524. Otherwise:
  3525. \begin{enumerate}
  3526. \item
  3527. Read a 5-bit unsigned integer as \bitvar{MVX}.
  3528. \item
  3529. Read a 1-bit unsigned integer as \locvar{MVSIGN}.
  3530. \item
  3531. If \locvar{MVSIGN} is 1, assign \bitvar{MVX} the value $-\bitvar{MVX}$.
  3532. \item
  3533. Read a 5-bit unsigned integer as \bitvar{MVY}.
  3534. \item
  3535. Read a 1-bit unsigned integer as \locvar{MVSIGN}.
  3536. \item
  3537. If \locvar{MVSIGN} is 1, assign \bitvar{MVY} the value $-\bitvar{MVY}$.
  3538. \end{enumerate}
  3539. \end{enumerate}
  3540. \subsection{Macro Block Motion Vector Decode}
  3541. \label{sub:mb-mv-decode}
  3542. \paragraph{Input parameters:}\hfill\\*
  3543. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3544. \multicolumn{1}{c}{Name} &
  3545. \multicolumn{1}{c}{Type} &
  3546. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3547. \multicolumn{1}{c}{Signed?} &
  3548. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3549. \bitvar{PF} & Integer & 2 & No & The pixel format. \\
  3550. \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
  3551. frame. \\
  3552. \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3553. 3 & No & An \bitvar{NMBS}-element array of coding
  3554. modes for each macro block. \\
  3555. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  3556. frame. \\
  3557. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3558. 1 & No & An \bitvar{NBS}-element array of flags
  3559. indicating which blocks are coded. \\
  3560. \bottomrule\end{tabularx}
  3561. \paragraph{Output parameters:}\hfill\\*
  3562. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3563. \multicolumn{1}{c}{Name} &
  3564. \multicolumn{1}{c}{Type} &
  3565. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3566. \multicolumn{1}{c}{Signed?} &
  3567. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3568. \bitvar{MVECTS} & \multicolumn{1}{p{50pt}}{Array of 2D Integer Vectors} &
  3569. 6 & Yes & An \bitvar{NBS}-element array of
  3570. motion vectors for each block. \\
  3571. \bottomrule\end{tabularx}
  3572. \paragraph{Variables used:}\hfill\\*
  3573. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3574. \multicolumn{1}{c}{Name} &
  3575. \multicolumn{1}{c}{Type} &
  3576. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3577. \multicolumn{1}{c}{Signed?} &
  3578. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3579. \locvar{LAST1} & \multicolumn{1}{p{50pt}}{2D Integer Vector} &
  3580. 6 & Yes & The last motion vector. \\
  3581. \locvar{LAST2} & \multicolumn{1}{p{50pt}}{2D Integer Vector} &
  3582. 6 & Yes & The second to last motion vector. \\
  3583. \locvar{MVX} & Integer & 6 & Yes & The X component of a motion vector. \\
  3584. \locvar{MVY} & Integer & 6 & Yes & The Y component of a motion vector. \\
  3585. \locvar{\mbi} & Integer & 32 & No & The index of the current macro
  3586. block. \\
  3587. \locvar{A} & Integer & 36 & No & The index of the lower-left luma block
  3588. in the macro block. \\
  3589. \locvar{B} & Integer & 36 & No & The index of the lower-right luma
  3590. block in the macro block. \\
  3591. \locvar{C} & Integer & 36 & No & The index of the upper-left luma block
  3592. in the macro block. \\
  3593. \locvar{D} & Integer & 36 & No & The index of the upper-right luma
  3594. block in the macro block. \\
  3595. \locvar{E} & Integer & 36 & No & The index of a chroma block in the
  3596. macro block, depending on the pixel format. \\
  3597. \locvar{F} & Integer & 36 & No & The index of a chroma block in the
  3598. macro block, depending on the pixel format. \\
  3599. \locvar{G} & Integer & 36 & No & The index of a chroma block in the
  3600. macro block, depending on the pixel format. \\
  3601. \locvar{H} & Integer & 36 & No & The index of a chroma block in the
  3602. macro block, depending on the pixel format. \\
  3603. \locvar{I} & Integer & 36 & No & The index of a chroma block in the
  3604. macro block, depending on the pixel format. \\
  3605. \locvar{J} & Integer & 36 & No & The index of a chroma block in the
  3606. macro block, depending on the pixel format. \\
  3607. \locvar{K} & Integer & 36 & No & The index of a chroma block in the
  3608. macro block, depending on the pixel format. \\
  3609. \locvar{L} & Integer & 36 & No & The index of a chroma block in the
  3610. macro block, depending on the pixel format. \\
  3611. \bottomrule\end{tabularx}
  3612. \medskip
  3613. Motion vectors are stored for each macro block.
  3614. In every mode except for INTER\_MV\_FOUR, every block in all the color planes
  3615. are assigned the same motion vector.
  3616. In INTER\_MV\_FOUR mode, all four blocks in the luma plane are assigned their
  3617. own motion vector, and motion vectors for blocks in the chroma planes are
  3618. computed from these, using averaging appropriate to the pixel format.
  3619. For INTER\_MV and INTER\_GOLDEN\_MV modes, a single motion vector is decoded
  3620. and applied to each block.
  3621. For INTER\_MV\_FOUR macro blocks, a motion vector is decoded for each coded
  3622. luma block.
  3623. Uncoded luma blocks receive the default $(0,0)$ vector for the purposes of
  3624. computing the chroma motion vectors.
  3625. None of the remaining macro block coding modes require decoding motion vectors
  3626. from the stream.
  3627. INTRA mode does not use a motion-compensated predictor, and so requires no
  3628. motion vector, and INTER\_NOMV and INTER\_GOLDEN\_NOMV modes use the default
  3629. vector $(0,0)$ for each block.
  3630. This also includes all macro blocks with no coded luma blocks, as they are
  3631. coded in INTER\_NOMV mode by definition.
  3632. The modes INTER\_MV\_LAST and INTER\_MV\_LAST2 use the motion vector from the
  3633. last macro block (in coded order) and the second to last macro block,
  3634. respectively, that contained a motion vector pointing to the previous frame.
  3635. Thus no explicit motion vector needs to be decoded for these modes.
  3636. Macro blocks coded in INTRA mode or one of the GOLDEN modes are not considered
  3637. in this process.
  3638. If an insufficient number of macro blocks have been coded in one of the INTER
  3639. modes, then the $(0,0)$ vector is used instead.
  3640. For macro blocks coded in INTER\_MV\_FOUR mode, the vector from the upper-right
  3641. luma block is used, even if the upper-right block is not coded.
  3642. The motion vectors are decoded from the stream as follows:
  3643. \begin{enumerate}
  3644. \item
  3645. Assign \locvar{LAST1} and \locvar{LAST2} both the value $(0,0)$.
  3646. \item
  3647. Read a 1-bit unsigned integer as \locvar{MVMODE}.
  3648. Note that this value is read even if no macro blocks require a motion vector to
  3649. be decoded.
  3650. \item
  3651. For each consecutive value of \locvar{\mbi} from 0 to $(\bitvar{NMBS}-1)$:
  3652. \begin{enumerate}
  3653. \item
  3654. If $\bitvar{MBMODES}[\locvar{\mbi}]$ is 7 (INTER\_MV\_FOUR):
  3655. \begin{enumerate}
  3656. \item
  3657. Let \locvar{A}, \locvar{B}, \locvar{C}, and \locvar{D} be the indices in coded
  3658. order \locvar{\bi} of the luma blocks in macro block \locvar{\mbi}, arranged
  3659. into raster order.
  3660. Thus, \locvar{A} is the index in coded order of the block in the lower left,
  3661. \locvar{B} the lower right, \locvar{C} the upper left, and \locvar{D} the
  3662. upper right. % TODO: as shown in Figure~REF.
  3663. \item If $\bitvar{BCODED}[\locvar{A}]$ is non-zero:
  3664. \begin{enumerate}
  3665. \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
  3666. the procedure described in Section~\ref{sub:mv-decode}.
  3667. \item Assign $\bitvar{MVECTS}[\locvar{A}]$ the value
  3668. $(\locvar{MVX},\locvar{MVY})$.
  3669. \end{enumerate}
  3670. \item Otherwise, assign $\bitvar{MVECTS}[\locvar{A}]$ the value $(0,0)$.
  3671. \item If $\bitvar{BCODED}[\locvar{B}]$ is non-zero:
  3672. \begin{enumerate}
  3673. \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
  3674. the procedure described in Section~\ref{sub:mv-decode}.
  3675. \item Assign $\bitvar{MVECTS}[\locvar{B}]$ the value
  3676. $(\locvar{MVX},\locvar{MVY})$.
  3677. \end{enumerate}
  3678. \item
  3679. Otherwise assign $\bitvar{MVECTS}[\locvar{B}]$ the value $(0,0)$.
  3680. \item If $\bitvar{BCODED}[\locvar{C}]$ is non-zero:
  3681. \begin{enumerate}
  3682. \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
  3683. the procedure described in Section~\ref{sub:mv-decode}.
  3684. \item Assign $\bitvar{MVECTS}[\locvar{C}]$ the value
  3685. $(\locvar{MVX},\locvar{MVY})$.
  3686. \end{enumerate}
  3687. \item Otherwise assign $\bitvar{MVECTS}[\locvar{C}]$ the value $(0,0)$.
  3688. \item If $\bitvar{BCODED}[\locvar{D}]$ is non-zero:
  3689. \begin{enumerate}
  3690. \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
  3691. the procedure described in Section~\ref{sub:mv-decode}.
  3692. \item Assign $\bitvar{MVECTS}[\locvar{D}]$ the value
  3693. $(\locvar{MVX},\locvar{MVY})$.
  3694. \end{enumerate}
  3695. \item
  3696. Otherwise, assign $\bitvar{MVECTS}[\locvar{D}]$ the value $(0,0)$.
  3697. \item
  3698. If \bitvar{PF} is 0 (4:2:0):
  3699. \begin{enumerate}
  3700. \item
  3701. Let \locvar{E} and \locvar{F} be the index in coded order of the one block in
  3702. the macro block from the $C_b$ and $C_r$ planes, respectively.
  3703. \item
  3704. Assign $\bitvar{MVECTS}[\locvar{E}]$ and $\bitvar{MVECTS}[\locvar{F}]$ the
  3705. value
  3706. \begin{multline*}
  3707. (\round\biggl(\frac{\begin{aligned}
  3708. \bitvar{MVECTS}[\locvar{A}]_x+\bitvar{MVECTS}[\locvar{B}]_x+\\
  3709. \bitvar{MVECTS}[\locvar{C}]_x+\bitvar{MVECTS}[\locvar{D}]_x
  3710. \end{aligned}}{4}\biggr), \\
  3711. \round\biggl(\frac{\begin{aligned}
  3712. \bitvar{MVECTS}[\locvar{A}]_y+\bitvar{MVECTS}[\locvar{B}]_y+\\
  3713. \bitvar{MVECTS}[\locvar{C}]_y+\bitvar{MVECTS}[\locvar{D}]_y
  3714. \end{aligned}}{4}\biggr))
  3715. \end{multline*}
  3716. \end{enumerate}
  3717. \item
  3718. If \bitvar{PF} is 2 (4:2:2):
  3719. \begin{enumerate}
  3720. \item
  3721. Let \locvar{E} and \locvar{F} be the indices in coded order of the bottom and
  3722. top blocks in the macro block from the $C_b$ plane, respectively, and
  3723. \locvar{G} and \locvar{H} be the indices in coded order of the bottom and top
  3724. blocks in the $C_r$ plane, respectively. %TODO: as shown in Figure~REF.
  3725. \item
  3726. Assign $\bitvar{MVECTS}[\locvar{E}]$ and $\bitvar{MVECTS}[\locvar{G}]$ the
  3727. value
  3728. \begin{multline*}
  3729. (\round\left(\frac{
  3730. \bitvar{MVECTS}[\locvar{A}]_x+\bitvar{MVECTS}[\locvar{B}]_x}{2}\right), \\
  3731. \round\left(\frac{
  3732. \bitvar{MVECTS}[\locvar{A}]_y+\bitvar{MVECTS}[\locvar{B}]_y}{2}\right))
  3733. \end{multline*}
  3734. \item
  3735. Assign $\bitvar{MVECTS}[\locvar{F}]$ and $\bitvar{MVECTS}[\locvar{H}]$ the
  3736. value
  3737. \begin{multline*}
  3738. (\round\left(\frac{
  3739. \bitvar{MVECTS}[\locvar{C}]_x+\bitvar{MVECTS}[\locvar{D}]_x}{2}\right), \\
  3740. \round\left(\frac{
  3741. \bitvar{MVECTS}[\locvar{C}]_y+\bitvar{MVECTS}[\locvar{D}]_y}{2}\right))
  3742. \end{multline*}
  3743. \end{enumerate}
  3744. \item
  3745. If \bitvar{PF} is 3 (4:4:4):
  3746. \begin{enumerate}
  3747. \item
  3748. Let \locvar{E}, \locvar{F}, \locvar{G}, and \locvar{H} be the indices
  3749. \locvar{\bi} in coded order of the $C_b$ plane blocks in macro block
  3750. \locvar{\mbi}, arranged into raster order, and \locvar{I}, \locvar{J},
  3751. \locvar{K}, and \locvar{L} be the indices \locvar{\bi} in coded order of the
  3752. $C_r$ plane blocks in macro block \locvar{\mbi}, arranged into raster order.
  3753. %TODO: as shown in Figure~REF.
  3754. \item
  3755. Assign $\bitvar{MVECTS}[\locvar{E}]$ and $\bitvar{MVECTS}[\locvar{I}]$ the
  3756. value \\ $\bitvar{MVECTS}[\locvar{A}]$.
  3757. \item
  3758. Assign $\bitvar{MVECTS}[\locvar{F}]$ and $\bitvar{MVECTS}[\locvar{J}]$ the
  3759. value \\ $\bitvar{MVECTS}[\locvar{B}]$.
  3760. \item
  3761. Assign $\bitvar{MVECTS}[\locvar{G}]$ and $\bitvar{MVECTS}[\locvar{K}]$ the
  3762. value \\ $\bitvar{MVECTS}[\locvar{C}]$.
  3763. \item
  3764. Assign $\bitvar{MVECTS}[\locvar{H}]$ and $\bitvar{MVECTS}[\locvar{L}]$ the
  3765. value \\ $\bitvar{MVECTS}[\locvar{D}]$.
  3766. \end{enumerate}
  3767. \item
  3768. Assign \locvar{LAST2} the value \locvar{LAST1}.
  3769. \item
  3770. Assign \locvar{LAST1} the value $(\locvar{MVX},\locvar{MVY})$.
  3771. This is the value of the motion vector decoded from the last coded luma block
  3772. in raster order.
  3773. There must always be at least one, since macro blocks with no coded luma blocks
  3774. must use mode 0:~INTER\_NOMV.
  3775. \end{enumerate}
  3776. \item
  3777. Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 6 (INTER\_GOLDEN\_MV),
  3778. decode a single motion vector into \locvar{MVX} and \locvar{MVY} using the
  3779. procedure described in Section~\ref{sub:mv-decode}.
  3780. \item
  3781. Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 4 (INTER\_MV\_LAST2):
  3782. \begin{enumerate}
  3783. \item
  3784. Assign $(\locvar{MVX},\locvar{MVY})$ the value \locvar{LAST2}.
  3785. \item
  3786. Assign \locvar{LAST2} the value \locvar{LAST1}.
  3787. \item
  3788. Assign \locvar{LAST1} the value $(\locvar{MVX},\locvar{MVY})$.
  3789. \end{enumerate}
  3790. \item
  3791. Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 3 (INTER\_MV\_LAST), assign
  3792. $(\locvar{MVX},\locvar{MVY})$ the value \locvar{LAST1}.
  3793. \item
  3794. Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 2 (INTER\_MV):
  3795. \begin{enumerate}
  3796. \item
  3797. Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using the
  3798. procedure described in Section~\ref{sub:mv-decode}.
  3799. \item
  3800. Assign \locvar{LAST2} the value \locvar{LAST1}.
  3801. \item
  3802. Assign \locvar{LAST1} the value $(\locvar{MVX},\locvar{MVY})$.
  3803. \end{enumerate}
  3804. \item
  3805. Otherwise ($\bitvar{MBMODES}[\locvar{\mbi}]$ is 5:~INTER\_GOLDEN\_NOMV,
  3806. 1:~INTRA, or 0:~INTER\_NOMV), assign \locvar{MVX} and \locvar{MVY} the value
  3807. zero.
  3808. \item
  3809. If $\bitvar{MBMODES}[\locvar{\mbi}]$ is not 7 (not INTER\_MV\_FOUR), then for
  3810. each coded block \locvar{\bi} in macro block \locvar{\mbi}:
  3811. \begin{enumerate}
  3812. \item
  3813. Assign $\bitvar{MVECTS}[\locvar{\bi}]$ the value $(\locvar{MVX},\locvar{MVY})$.
  3814. \end{enumerate}
  3815. \end{enumerate}
  3816. \end{enumerate}
  3817. \paragraph{VP3 Compatibility}
  3818. Unless all four luma blocks in the macro block are coded, the VP3 encoder does
  3819. not select mode INTER\_MV\_FOUR.
  3820. Theora removes this restriction by treating the motion vector for an uncoded
  3821. luma block as the default $(0,0)$ vector.
  3822. This is consistent with the premise that the block has not changed since the
  3823. previous frame and that chroma information can be largely ignored when
  3824. estimating motion.
  3825. No modification is required for INTER\_MV\_FOUR macro blocks in VP3 streams to
  3826. be decoded correctly by a Theora decoder.
  3827. However, regardless of how many of the luma blocks are actually coded, the VP3
  3828. decoder always reads four motion vectors from the stream for INTER\_MV\_FOUR
  3829. mode.
  3830. The motion vectors read are used to calculate the motion vectors for the chroma
  3831. blocks, but are otherwise ignored.
  3832. Thus, care should be taken when creating Theora streams meant to be backwards
  3833. compatible with VP3 to only use INTER\_MV\_FOUR mode when all four luma
  3834. blocks are coded.
  3835. \section{Block-Level \qi\ Decode}
  3836. \label{sub:block-qis}
  3837. \paragraph{Input parameters:}\hfill\\*
  3838. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3839. \multicolumn{1}{c}{Name} &
  3840. \multicolumn{1}{c}{Type} &
  3841. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3842. \multicolumn{1}{c}{Signed?} &
  3843. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3844. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  3845. frame. \\
  3846. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3847. 1 & No & An \bitvar{NBS}-element array of flags
  3848. indicating which blocks are coded. \\
  3849. \bitvar{NQIS} & Integer & 2 & No & The number of \qi\ values. \\
  3850. \bottomrule\end{tabularx}
  3851. \paragraph{Output parameters:}\hfill\\*
  3852. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3853. \multicolumn{1}{c}{Name} &
  3854. \multicolumn{1}{c}{Type} &
  3855. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3856. \multicolumn{1}{c}{Signed?} &
  3857. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3858. \bitvar{QIIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3859. 2 & No & An \bitvar{NBS}-element array of
  3860. \locvar{\qii} values for each block. \\
  3861. \bottomrule\end{tabularx}
  3862. \paragraph{Variables used:}\hfill\\*
  3863. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3864. \multicolumn{1}{c}{Name} &
  3865. \multicolumn{1}{c}{Type} &
  3866. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3867. \multicolumn{1}{c}{Signed?} &
  3868. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3869. \locvar{NBITS} & Integer & 36 & No & The length of a bit string to decode. \\
  3870. \locvar{BITS} & Bit string & & & A decoded set of flags. \\
  3871. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  3872. coded order. \\
  3873. \locvar{\qii} & Integer & 2 & No & The index of \qi\ value in the list of
  3874. \qi\ values defined for this frame. \\
  3875. \bottomrule\end{tabularx}
  3876. \medskip
  3877. This procedure selects the \qi\ value to be used for dequantizing the AC
  3878. coefficients of each block.
  3879. DC coefficients all use the same \qi\ value, so as to avoid interference with
  3880. the DC prediction mechanism, which occurs in the quantized domain.
  3881. The value is actually represented by an index \locvar{\qii} into the list of
  3882. \qi\ values defined for the frame.
  3883. The decoder makes multiple passes through the list of coded blocks, one for
  3884. each \qi\ value except the last one.
  3885. In each pass, an RLE-coded bitmask is decoded to divide the blocks into two
  3886. groups: those that use the current \qi\ value in the list, and those that use
  3887. a value from later in the list.
  3888. Each subsequent pass is restricted to the blocks in the second group.
  3889. \begin{enumerate}
  3890. \item
  3891. For each value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$, assign
  3892. $\bitvar{QIIS}[\locvar{\bi}]$ the value zero.
  3893. \item
  3894. For each consecutive value of \locvar{\qii} from 0 to $(\bitvar{NQIS}-2)$:
  3895. \begin{enumerate}
  3896. \item
  3897. Assign \locvar{NBITS} be the number of blocks \locvar{\bi} such that
  3898. $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero and $\bitvar{QIIS}[\locvar{\bi}]$
  3899. equals $\locvar{\qii}$.
  3900. \item
  3901. Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
  3902. described in Section~\ref{sub:long-run}.
  3903. This represents the list of blocks that use \qi\ value \locvar{\qii} or higher.
  3904. \item
  3905. For each consecutive value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$ such
  3906. that $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero and
  3907. $\bitvar{QIIS}[\locvar{\bi}]$ equals $\locvar{\qii}$:
  3908. \begin{enumerate}
  3909. \item
  3910. Remove the bit at the head of the string \locvar{BITS} and add its value to
  3911. $\bitvar{QIIS}[\locvar{\bi}]$.
  3912. \end{enumerate}
  3913. \end{enumerate}
  3914. \end{enumerate}
  3915. \paragraph{VP3 Compatibility}
  3916. For VP3 compatible streams, only one \qi\ value can be specified in the frame
  3917. header, so the main loop of the above procedure, which would iterate from $0$
  3918. to $-1$, is never executed.
  3919. Thus, no bits are read, and each block uses the one \qi\ value defined for the
  3920. frame.
  3921. \cleardoublepage
  3922. \section{DCT Coefficients}
  3923. \label{sec:dct-decode}
  3924. The quantized DCT coefficients are decoded by making 64 passes through the list
  3925. of coded blocks, one for each token index in zig-zag order.
  3926. For the DC tokens, two Huffman tables are chosen from among the first 16, one
  3927. for the luma plane and one for the chroma planes.
  3928. The AC tokens, however, are divided into four different groups.
  3929. Again, two 4-bit indices are decoded, one for the luma plane, and one for the
  3930. chroma planes, but these select the codebooks for {\em all four} groups.
  3931. AC coefficients in group one use codebooks $16\ldots 31$, while group two uses
  3932. $32\ldots 47$, etc.
  3933. Note that this second set of indices is decoded even if there are no non-zero
  3934. AC coefficients in the frame.
  3935. Tokens are divided into two major types: EOB tokens, which fill the remainder
  3936. of one or more blocks with zeros, and coefficient tokens, which fill in one or
  3937. more coefficients within a single block.
  3938. A decoding procedure for the first is given in Section~\ref{sub:eob-token}, and
  3939. for the second in Section~\ref{sub:coeff-token}.
  3940. The decoding procedure for the complete set of quantized coefficients is given
  3941. in Section~\ref{sub:dct-coeffs}.
  3942. \subsection{EOB Token Decode}
  3943. \label{sub:eob-token}
  3944. \paragraph{Input parameters:}\hfill\\*
  3945. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3946. \multicolumn{1}{c}{Name} &
  3947. \multicolumn{1}{c}{Type} &
  3948. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3949. \multicolumn{1}{c}{Signed?} &
  3950. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3951. \bitvar{TOKEN} & Integer & 5 & No & The token being decoded.
  3952. This must be in the range $0\ldots 6$. \\
  3953. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  3954. frame. \\
  3955. \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3956. 7 & No & An \bitvar{NBS}-element array of the
  3957. current token index for each block. \\
  3958. \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3959. 7 & No & An \bitvar{NBS}-element array of the
  3960. coefficient count for each block. \\
  3961. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  3962. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  3963. quantized DCT coefficient values for each block in zig-zag order. \\
  3964. \bitvar{\bi} & Integer & 36 & No & The index of the current block in
  3965. coded order. \\
  3966. \bitvar{\ti} & Integer & 6 & No & The current token index. \\
  3967. \bottomrule\end{tabularx}
  3968. \paragraph{Output parameters:}\hfill\\*
  3969. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3970. \multicolumn{1}{c}{Name} &
  3971. \multicolumn{1}{c}{Type} &
  3972. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3973. \multicolumn{1}{c}{Signed?} &
  3974. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3975. \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3976. 7 & No & An \bitvar{NBS}-element array of the
  3977. current token index for each block. \\
  3978. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  3979. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  3980. quantized DCT coefficient values for each block in zig-zag order. \\
  3981. \bitvar{EOBS} & Integer & 36 & No & The remaining length of the current
  3982. EOB run. \\
  3983. \bottomrule\end{tabularx}
  3984. \paragraph{Variables used:}\hfill\\*
  3985. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3986. \multicolumn{1}{c}{Name} &
  3987. \multicolumn{1}{c}{Type} &
  3988. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3989. \multicolumn{1}{c}{Signed?} &
  3990. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3991. \locvar{\bj} & Integer & 36 & No & Another index of a block in coded
  3992. order. \\
  3993. \locvar{\tj} & Integer & 6 & No & Another token index. \\
  3994. \bottomrule\end{tabularx}
  3995. \medskip
  3996. A summary of the EOB tokens is given in Table~\ref{tab:eob-tokens}.
  3997. An important thing to note is that token 6 does not add an offset to the
  3998. decoded run value, even though in general it should only be used for runs of
  3999. size 32 or longer.
  4000. If a value of zero is decoded for this run, it is treated as an EOB run the
  4001. size of the remaining coded blocks.
  4002. \begin{table}[htbp]
  4003. \begin{center}
  4004. \begin{tabular}{ccl}\toprule
  4005. Token Value & Extra Bits & EOB Run Lengths \\\midrule
  4006. $0$ & $0$ & $1$ \\
  4007. $1$ & $0$ & $2$ \\
  4008. $2$ & $0$ & $3$ \\
  4009. $3$ & $2$ & $4\ldots 7$ \\
  4010. $4$ & $3$ & $8\ldots 15$ \\
  4011. $5$ & $4$ & $16\ldots 31$ \\
  4012. $6$ & $12$ & $1\ldots 4095$, or all remaining blocks \\
  4013. \bottomrule\end{tabular}
  4014. \end{center}
  4015. \caption{EOB Token Summary}
  4016. \label{tab:eob-tokens}
  4017. \end{table}
  4018. There is no restriction that one EOB token cannot be immediately followed by
  4019. another, so no special cases are necessary to extend the range of the maximum
  4020. run length as were required in Section~\ref{sub:long-run}.
  4021. Indeed, depending on the lengths of the Huffman codes, it may even cheaper to
  4022. encode, by way of example, an EOB run of length 31 followed by an EOB run of
  4023. length 1 than to encode an EOB run of length 32 directly.
  4024. There is also no restriction that an EOB run stop at the end of a color plane
  4025. or a token index.
  4026. The run MUST, however, end at or before the end of the frame.
  4027. \begin{enumerate}
  4028. \item
  4029. If \bitvar{TOKEN} is 0, assign \bitvar{EOBS} the value 1.
  4030. \item
  4031. Otherwise, if \bitvar{TOKEN} is 1, assign \bitvar{EOBS} the value 2.
  4032. \item
  4033. Otherwise, if \bitvar{TOKEN} is 2, assign \bitvar{EOBS} the value 3.
  4034. \item
  4035. Otherwise, if \bitvar{TOKEN} is 3:
  4036. \begin{enumerate}
  4037. \item
  4038. Read a 2-bit unsigned integer as \bitvar{EOBS}.
  4039. \item
  4040. Assign \bitvar{EOBS} the value $(\bitvar{EOBS}+4)$.
  4041. \end{enumerate}
  4042. \item
  4043. Otherwise, if \bitvar{TOKEN} is 4:
  4044. \begin{enumerate}
  4045. \item
  4046. Read a 3-bit unsigned integer as \bitvar{EOBS}.
  4047. \item
  4048. Assign \bitvar{EOBS} the value $(\bitvar{EOBS}+8)$.
  4049. \end{enumerate}
  4050. \item
  4051. Otherwise, if \bitvar{TOKEN} is 5:
  4052. \begin{enumerate}
  4053. \item
  4054. Read a 4-bit unsigned integer as \bitvar{EOBS}.
  4055. \item
  4056. Assign \bitvar{EOBS} the value $(\bitvar{EOBS}+16)$.
  4057. \end{enumerate}
  4058. \item
  4059. Otherwise, \bitvar{TOKEN} is 6:
  4060. \begin{enumerate}
  4061. \item
  4062. Read a 12-bit unsigned integer as \bitvar{EOBS}.
  4063. \item
  4064. If \bitvar{EOBS} is zero, assign \bitvar{EOBS} to be the number of coded blocks
  4065. \locvar{\bj} such that $\bitvar{TIS}[\locvar{\bj}]$ is less than 64.
  4066. \end{enumerate}
  4067. \item
  4068. For each value of \locvar{\tj} from $\bitvar{\ti}$ to 63, assign
  4069. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4070. \item
  4071. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4072. \item
  4073. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value 64.
  4074. \item
  4075. Assign \bitvar{EOBS} the value $(\bitvar{EOBS}-1)$.
  4076. \end{enumerate}
  4077. \paragraph{VP3 Compatibility}
  4078. The VP3 encoder does not use the special interpretation of a zero-length EOB
  4079. run, though its decoder {\em does} support it.
  4080. That may be due more to a happy accident in the way the decoder was written
  4081. than intentional design, however, and other VP3 implementations might not
  4082. reproduce it faithfully.
  4083. For backwards compatibility, it may be wise to avoid it, especially as for most
  4084. frame sizes there are fewer than 4095 blocks, making it unnecessary.
  4085. \subsection{Coefficient Token Decode}
  4086. \label{sub:coeff-token}
  4087. \paragraph{Input parameters:}\hfill\\*
  4088. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4089. \multicolumn{1}{c}{Name} &
  4090. \multicolumn{1}{c}{Type} &
  4091. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4092. \multicolumn{1}{c}{Signed?} &
  4093. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4094. \bitvar{TOKEN} & Integer & 5 & No & The token being decoded.
  4095. This must be in the range $7\ldots 31$. \\
  4096. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  4097. frame. \\
  4098. \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4099. 7 & No & An \bitvar{NBS}-element array of the
  4100. current token index for each block. \\
  4101. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  4102. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  4103. quantized DCT coefficient values for each block in zig-zag order. \\
  4104. \bitvar{\bi} & Integer & 36 & No & The index of the current block in
  4105. coded order. \\
  4106. \bitvar{\ti} & Integer & 6 & No & The current token index. \\
  4107. \bottomrule\end{tabularx}
  4108. \paragraph{Output parameters:}\hfill\\*
  4109. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4110. \multicolumn{1}{c}{Name} &
  4111. \multicolumn{1}{c}{Type} &
  4112. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4113. \multicolumn{1}{c}{Signed?} &
  4114. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4115. \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4116. 7 & No & An \bitvar{NBS}-element array of the
  4117. current token index for each block. \\
  4118. \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4119. 7 & No & An \bitvar{NBS}-element array of the
  4120. coefficient count for each block. \\
  4121. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  4122. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  4123. quantized DCT coefficient values for each block in zig-zag order. \\
  4124. \bottomrule\end{tabularx}
  4125. \paragraph{Variables used:}\hfill\\*
  4126. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4127. \multicolumn{1}{c}{Name} &
  4128. \multicolumn{1}{c}{Type} &
  4129. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4130. \multicolumn{1}{c}{Signed?} &
  4131. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4132. \locvar{SIGN} & Integer & 1 & No & A flag indicating the sign of the
  4133. current coefficient. \\
  4134. \locvar{MAG} & Integer & 10 & No & The magnitude of the current
  4135. coefficient. \\
  4136. \locvar{RLEN} & Integer & 6 & No & The length of the current zero run. \\
  4137. \locvar{\tj} & Integer & 6 & No & Another token index. \\
  4138. \bottomrule\end{tabularx}
  4139. \medskip
  4140. Each of these tokens decodes one or more coefficients in the current block.
  4141. A summary of the meanings of the token values is presented in
  4142. Table~\ref{tab:coeff-tokens}.
  4143. There are often several different ways to tokenize a given coefficient list.
  4144. Which one is optimal depends on the exact lengths of the Huffman codes used to
  4145. represent each token.
  4146. Note that we do not update the coefficient count for the block if we decode a
  4147. pure zero run.
  4148. \begin{table}[htbp]
  4149. \begin{center}
  4150. \begin{tabularx}{\textwidth}{cclX}\toprule
  4151. Token Value & Extra Bits & \multicolumn{1}{p{55pt}}{Number of Coefficients}
  4152. & Description \\\midrule
  4153. $7$ & $3$ & $1\ldots 8$ & Short zero run. \\
  4154. $8$ & $6$ & $1\ldots 64$ & Zero run. \\
  4155. $9$ & $0$ & $1$ & $1$. \\
  4156. $10$ & $0$ & $1$ & $-1$. \\
  4157. $11$ & $0$ & $1$ & $2$. \\
  4158. $12$ & $0$ & $1$ & $-2$. \\
  4159. $13$ & $1$ & $1$ & $\pm 3$. \\
  4160. $14$ & $1$ & $1$ & $\pm 4$. \\
  4161. $15$ & $1$ & $1$ & $\pm 5$. \\
  4162. $16$ & $1$ & $1$ & $\pm 6$. \\
  4163. $17$ & $2$ & $1$ & $\pm 7\ldots 8$. \\
  4164. $18$ & $3$ & $1$ & $\pm 9\ldots 12$. \\
  4165. $19$ & $4$ & $1$ & $\pm 13\ldots 20$. \\
  4166. $20$ & $5$ & $1$ & $\pm 21\ldots 36$. \\
  4167. $21$ & $6$ & $1$ & $\pm 37\ldots 68$. \\
  4168. $22$ & $10$ & $1$ & $\pm 69\ldots 580$. \\
  4169. $23$ & $1$ & $2$ & One zero followed by $\pm 1$. \\
  4170. $24$ & $1$ & $3$ & Two zeros followed by $\pm 1$. \\
  4171. $25$ & $1$ & $4$ & Three zeros followed by
  4172. $\pm 1$. \\
  4173. $26$ & $1$ & $5$ & Four zeros followed by
  4174. $\pm 1$. \\
  4175. $27$ & $1$ & $6$ & Five zeros followed by
  4176. $\pm 1$. \\
  4177. $28$ & $3$ & $7\ldots 10$ & $6\ldots 9$ zeros followed by
  4178. $\pm 1$. \\
  4179. $29$ & $4$ & $11\ldots 18$ & $10\ldots 17$ zeros followed by
  4180. $\pm 1$.\\
  4181. $30$ & $2$ & $2$ & One zero followed by
  4182. $\pm 2\ldots 3$. \\
  4183. $31$ & $3$ & $3\ldots 4$ & $2\ldots 3$ zeros followed by
  4184. $\pm 2\ldots 3$. \\
  4185. \bottomrule\end{tabularx}
  4186. \end{center}
  4187. \caption{Coefficient Token Summary}
  4188. \label{tab:coeff-tokens}
  4189. \end{table}
  4190. For tokens which represent more than one coefficient, they MUST NOT bring the
  4191. total number of coefficients in the block to more than 64.
  4192. Care should be taken in a decoder to check for this, as otherwise it may permit
  4193. buffer overflows from invalidly formed packets.
  4194. \begin{verse}
  4195. {\bf Note:} One way to achieve this efficiently is to combine the inverse
  4196. zig-zag mapping (described later in Section~\ref{sub:dequant}) with
  4197. coefficient decode, and use a table look-up to map zig-zag indices greater
  4198. than 63 to a safe location.
  4199. \end{verse}
  4200. \begin{enumerate}
  4201. \item
  4202. If \bitvar{TOKEN} is 7:
  4203. \begin{enumerate}
  4204. \item
  4205. Read in a 3-bit unsigned integer as \locvar{RLEN}.
  4206. \item
  4207. Assign \locvar{RLEN} the value $(\locvar{RLEN}+1)$.
  4208. \item
  4209. For each value of \locvar{\tj} from \bitvar{\ti} to
  4210. $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
  4211. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4212. \item
  4213. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
  4214. $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}$.
  4215. \end{enumerate}
  4216. \item
  4217. Otherwise, if \bitvar{TOKEN} is 8:
  4218. \begin{enumerate}
  4219. \item
  4220. Read in a 6-bit unsigned integer as \locvar{RLEN}.
  4221. \item
  4222. Assign \locvar{RLEN} the value $(\locvar{RLEN}+1)$.
  4223. \item
  4224. For each value of \locvar{\tj} from \bitvar{\ti} to
  4225. $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
  4226. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4227. \item
  4228. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
  4229. $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}$.
  4230. \end{enumerate}
  4231. \item
  4232. Otherwise, if \bitvar{TOKEN} is 9:
  4233. \begin{enumerate}
  4234. \item
  4235. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $1$.
  4236. \item
  4237. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4238. \item
  4239. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4240. \end{enumerate}
  4241. \item
  4242. Otherwise, if \bitvar{TOKEN} is 10:
  4243. \begin{enumerate}
  4244. \item
  4245. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-1$.
  4246. \item
  4247. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4248. \item
  4249. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4250. \end{enumerate}
  4251. \item
  4252. Otherwise, if \bitvar{TOKEN} is 11:
  4253. \begin{enumerate}
  4254. \item
  4255. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $2$.
  4256. \item
  4257. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4258. \item
  4259. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4260. \end{enumerate}
  4261. \item
  4262. Otherwise, if \bitvar{TOKEN} is 12:
  4263. \begin{enumerate}
  4264. \item
  4265. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-2$.
  4266. \item
  4267. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4268. \item
  4269. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4270. \end{enumerate}
  4271. \item
  4272. Otherwise, if \bitvar{TOKEN} is 13:
  4273. \begin{enumerate}
  4274. \item
  4275. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4276. \item
  4277. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4278. the value $3$.
  4279. \item
  4280. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-3$.
  4281. \item
  4282. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4283. \item
  4284. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4285. \end{enumerate}
  4286. \item
  4287. Otherwise, if \bitvar{TOKEN} is 14:
  4288. \begin{enumerate}
  4289. \item
  4290. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4291. \item
  4292. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4293. the value $4$.
  4294. \item
  4295. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-4$.
  4296. \item
  4297. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4298. \item
  4299. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4300. \end{enumerate}
  4301. \item
  4302. Otherwise, if \bitvar{TOKEN} is 15:
  4303. \begin{enumerate}
  4304. \item
  4305. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4306. \item
  4307. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4308. the value $5$.
  4309. \item
  4310. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-5$.
  4311. \item
  4312. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4313. \item
  4314. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4315. \end{enumerate}
  4316. \item
  4317. Otherwise, if \bitvar{TOKEN} is 16:
  4318. \begin{enumerate}
  4319. \item
  4320. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4321. \item
  4322. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4323. the value $6$.
  4324. \item
  4325. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-6$.
  4326. \item
  4327. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4328. \item
  4329. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4330. \end{enumerate}
  4331. \item
  4332. Otherwise, if \bitvar{TOKEN} is 17:
  4333. \begin{enumerate}
  4334. \item
  4335. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4336. \item
  4337. Read a 1-bit unsigned integer as \locvar{MAG}.
  4338. \item
  4339. Assign \locvar{MAG} the value $(\locvar{MAG}+7)$.
  4340. \item
  4341. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4342. the value $\locvar{MAG}$.
  4343. \item
  4344. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4345. $-\locvar{MAG}$.
  4346. \item
  4347. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4348. \item
  4349. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4350. \end{enumerate}
  4351. \item
  4352. Otherwise, if \bitvar{TOKEN} is 18:
  4353. \begin{enumerate}
  4354. \item
  4355. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4356. \item
  4357. Read a 2-bit unsigned integer as \locvar{MAG}.
  4358. \item
  4359. Assign \locvar{MAG} the value $(\locvar{MAG}+9)$.
  4360. \item
  4361. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4362. the value $\locvar{MAG}$.
  4363. \item
  4364. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4365. $-\locvar{MAG}$.
  4366. \item
  4367. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4368. \item
  4369. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4370. \end{enumerate}
  4371. \item
  4372. Otherwise, if \bitvar{TOKEN} is 19:
  4373. \begin{enumerate}
  4374. \item
  4375. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4376. \item
  4377. Read a 3-bit unsigned integer as \locvar{MAG}.
  4378. \item
  4379. Assign \locvar{MAG} the value $(\locvar{MAG}+13)$.
  4380. \item
  4381. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4382. the value $\locvar{MAG}$.
  4383. \item
  4384. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4385. $-\locvar{MAG}$.
  4386. \item
  4387. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4388. \item
  4389. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4390. \end{enumerate}
  4391. \item
  4392. Otherwise, if \bitvar{TOKEN} is 20:
  4393. \begin{enumerate}
  4394. \item
  4395. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4396. \item
  4397. Read a 4-bit unsigned integer as \locvar{MAG}.
  4398. \item
  4399. Assign \locvar{MAG} the value $(\locvar{MAG}+21)$.
  4400. \item
  4401. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4402. the value $\locvar{MAG}$.
  4403. \item
  4404. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4405. $-\locvar{MAG}$.
  4406. \item
  4407. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4408. \item
  4409. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4410. \end{enumerate}
  4411. \item
  4412. Otherwise, if \bitvar{TOKEN} is 21:
  4413. \begin{enumerate}
  4414. \item
  4415. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4416. \item
  4417. Read a 5-bit unsigned integer as \locvar{MAG}.
  4418. \item
  4419. Assign \locvar{MAG} the value $(\locvar{MAG}+37)$.
  4420. \item
  4421. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4422. the value $\locvar{MAG}$.
  4423. \item
  4424. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4425. $-\locvar{MAG}$.
  4426. \item
  4427. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4428. \item
  4429. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4430. \end{enumerate}
  4431. \item
  4432. Otherwise, if \bitvar{TOKEN} is 22:
  4433. \begin{enumerate}
  4434. \item
  4435. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4436. \item
  4437. Read a 9-bit unsigned integer as \locvar{MAG}.
  4438. \item
  4439. Assign \locvar{MAG} the value $(\locvar{MAG}+69)$.
  4440. \item
  4441. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4442. the value $\locvar{MAG}$.
  4443. \item
  4444. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4445. $-\locvar{MAG}$.
  4446. \item
  4447. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4448. \item
  4449. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4450. \end{enumerate}
  4451. \item
  4452. Otherwise, if \bitvar{TOKEN} is 23:
  4453. \begin{enumerate}
  4454. \item
  4455. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value zero.
  4456. \item
  4457. Read a 1-bit unsigned integer as SIGN.
  4458. \item
  4459. If \locvar{SIGN} is zero, assign
  4460. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value $1$.
  4461. \item
  4462. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value
  4463. $-1$.
  4464. \item
  4465. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+2$.
  4466. \item
  4467. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4468. \end{enumerate}
  4469. \item
  4470. Otherwise, if \bitvar{TOKEN} is 24:
  4471. \begin{enumerate}
  4472. \item
  4473. For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+1)$, assign
  4474. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4475. \item
  4476. Read a 1-bit unsigned integer as SIGN.
  4477. \item
  4478. If \locvar{SIGN} is zero, assign
  4479. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+2]$ the value $1$.
  4480. \item
  4481. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+2]$ the value
  4482. $-1$.
  4483. \item
  4484. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+3$.
  4485. \item
  4486. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4487. \end{enumerate}
  4488. \item
  4489. Otherwise, if \bitvar{TOKEN} is 25:
  4490. \begin{enumerate}
  4491. \item
  4492. For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+2)$, assign
  4493. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4494. \item
  4495. Read a 1-bit unsigned integer as SIGN.
  4496. \item
  4497. If \locvar{SIGN} is zero, assign
  4498. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+3]$ the value $1$.
  4499. \item
  4500. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+3]$ the value
  4501. $-1$.
  4502. \item
  4503. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+4$.
  4504. \item
  4505. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4506. \end{enumerate}
  4507. \item
  4508. Otherwise, if \bitvar{TOKEN} is 26:
  4509. \begin{enumerate}
  4510. \item
  4511. For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+3)$, assign
  4512. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4513. \item
  4514. Read a 1-bit unsigned integer as SIGN.
  4515. \item
  4516. If \locvar{SIGN} is zero, assign
  4517. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+4]$ the value $1$.
  4518. \item
  4519. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+4]$ the value
  4520. $-1$.
  4521. \item
  4522. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+5$.
  4523. \item
  4524. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4525. \end{enumerate}
  4526. \item
  4527. Otherwise, if \bitvar{TOKEN} is 27:
  4528. \begin{enumerate}
  4529. \item
  4530. For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+4)$, assign
  4531. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4532. \item
  4533. Read a 1-bit unsigned integer as SIGN.
  4534. \item
  4535. If \locvar{SIGN} is zero, assign
  4536. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+5]$ the value $1$.
  4537. \item
  4538. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+5]$ the value
  4539. $-1$.
  4540. \item
  4541. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+6$.
  4542. \item
  4543. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4544. \end{enumerate}
  4545. \item
  4546. Otherwise, if \bitvar{TOKEN} is 28:
  4547. \begin{enumerate}
  4548. \item
  4549. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4550. \item
  4551. Read a 2-bit unsigned integer as \locvar{RLEN}.
  4552. \item
  4553. Assign \locvar{RLEN} the value $(\locvar{RLEN}+6)$.
  4554. \item
  4555. For each value of \locvar{\tj} from \bitvar{\ti} to
  4556. $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
  4557. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4558. \item
  4559. If \locvar{SIGN} is zero, assign
  4560. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$ the value $1$.
  4561. \item
  4562. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$
  4563. the value $-1$.
  4564. \item
  4565. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
  4566. $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}+1$.
  4567. \item
  4568. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4569. \end{enumerate}
  4570. \item
  4571. Otherwise, if \bitvar{TOKEN} is 29:
  4572. \begin{enumerate}
  4573. \item
  4574. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4575. \item
  4576. Read a 3-bit unsigned integer as \locvar{RLEN}.
  4577. \item
  4578. Assign \locvar{RLEN} the value $(\locvar{RLEN}+10)$.
  4579. \item
  4580. For each value of \locvar{\tj} from \bitvar{\ti} to
  4581. $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
  4582. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4583. \item
  4584. If \locvar{SIGN} is zero, assign
  4585. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$ the value $1$.
  4586. \item
  4587. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$
  4588. the value $-1$.
  4589. \item
  4590. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
  4591. $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}+1$.
  4592. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4593. \end{enumerate}
  4594. \item
  4595. Otherwise, if \bitvar{TOKEN} is 30:
  4596. \begin{enumerate}
  4597. \item
  4598. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\ti}]$ the value zero.
  4599. \item
  4600. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4601. \item
  4602. Read a 1-bit unsigned integer as \locvar{MAG}.
  4603. \item
  4604. Assign \locvar{MAG} the value $(\locvar{MAG}+2)$.
  4605. \item
  4606. If \locvar{SIGN} is zero, assign
  4607. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value $\locvar{MAG}$.
  4608. \item
  4609. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value
  4610. $-\locvar{MAG}$.
  4611. \item
  4612. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+2$.
  4613. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4614. \end{enumerate}
  4615. \item
  4616. Otherwise, if \bitvar{TOKEN} is 31:
  4617. \begin{enumerate}
  4618. \item
  4619. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4620. \item
  4621. Read a 1-bit unsigned integer as \locvar{MAG}.
  4622. \item
  4623. Assign \locvar{MAG} the value $(\locvar{MAG}+2)$.
  4624. \item
  4625. Read a 1-bit unsigned integer as \locvar{RLEN}.
  4626. \item
  4627. Assign \locvar{RLEN} the value $(\locvar{RLEN}+2)$.
  4628. \item
  4629. For each value of \locvar{\tj} from \bitvar{\ti} to
  4630. $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
  4631. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4632. \item
  4633. If \locvar{SIGN} is zero, assign
  4634. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$ the value
  4635. $\locvar{MAG}$.
  4636. \item
  4637. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$
  4638. the value $-\locvar{MAG}$.
  4639. \item
  4640. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
  4641. $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}+1$.
  4642. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4643. \end{enumerate}
  4644. \end{enumerate}
  4645. \subsection{DCT Coefficient Decode}
  4646. \label{sub:dct-coeffs}
  4647. \paragraph{Input parameters:}\hfill\\*
  4648. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4649. \multicolumn{1}{c}{Name} &
  4650. \multicolumn{1}{c}{Type} &
  4651. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4652. \multicolumn{1}{c}{Signed?} &
  4653. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4654. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  4655. frame. \\
  4656. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4657. 1 & No & An \bitvar{NBS}-element array of flags
  4658. indicating which blocks are coded. \\
  4659. \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
  4660. frame. \\
  4661. \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
  4662. & An 80-element array of Huffman tables
  4663. with up to 32 entries each. \\
  4664. \bottomrule\end{tabularx}
  4665. \paragraph{Output parameters:}\hfill\\*
  4666. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4667. \multicolumn{1}{c}{Name} &
  4668. \multicolumn{1}{c}{Type} &
  4669. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4670. \multicolumn{1}{c}{Signed?} &
  4671. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4672. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  4673. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  4674. quantized DCT coefficient values for each block in zig-zag order. \\
  4675. \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4676. 7 & No & An \bitvar{NBS}-element array of the
  4677. coefficient count for each block. \\
  4678. \bottomrule\end{tabularx}
  4679. \paragraph{Variables used:}\hfill\\*
  4680. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4681. \multicolumn{1}{c}{Name} &
  4682. \multicolumn{1}{c}{Type} &
  4683. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4684. \multicolumn{1}{c}{Signed?} &
  4685. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4686. \locvar{NLBS} & Integer & 34 & No & The number of blocks in the luma
  4687. plane. \\
  4688. \locvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4689. 7 & No & An \bitvar{NBS}-element array of the
  4690. current token index for each block. \\
  4691. \locvar{EOBS} & Integer & 36 & No & The remaining length of the current
  4692. EOB run. \\
  4693. \locvar{TOKEN} & Integer & 5 & No & The current token being decoded. \\
  4694. \locvar{HG} & Integer & 3 & No & The current Huffman table group. \\
  4695. \locvar{\cbi} & Integer & 36 & No & The index of the current block in the
  4696. coded block list. \\
  4697. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  4698. coded order. \\
  4699. \locvar{\bj} & Integer & 36 & No & Another index of a block in coded
  4700. order. \\
  4701. \locvar{\ti} & Integer & 6 & No & The current token index. \\
  4702. \locvar{\tj} & Integer & 6 & No & Another token index. \\
  4703. \locvar{\hti_L} & Integer & 4 & No & The index of the current Huffman table
  4704. to use for the luma plane within a group. \\
  4705. \locvar{\hti_C} & Integer & 4 & No & The index of the current Huffman table
  4706. to use for the chroma planes within a group. \\
  4707. \locvar{\hti} & Integer & 7 & No & The index of the current Huffman table
  4708. to use. \\
  4709. \bottomrule\end{tabularx}
  4710. \medskip
  4711. This procedure puts the above two procedures to work to decode the entire set
  4712. of DCT coefficients for the frame.
  4713. At the end of this procedure, \locvar{EOBS} MUST be zero, and
  4714. $\locvar{TIS}[\locvar{\bi}]$ MUST be 64 for every coded \locvar{\bi}.
  4715. Note that we update the coefficient count of every block before continuing an
  4716. EOB run or decoding a token, despite the fact that it is already up to date
  4717. unless the previous token was a pure zero run.
  4718. This is done intentionally to mimic the VP3 accounting rules.
  4719. Thus the only time the coefficient count does not include the coefficients in a
  4720. pure zero run is when when that run reaches all the way to coefficient 63.
  4721. Note, however, that regardless of the coefficient count, any additional
  4722. coefficients are still set to zero.
  4723. The only use of the count is in determining if a special case of the inverse
  4724. DCT can be used in Section~\ref{sub:2d-idct}.
  4725. \begin{enumerate}
  4726. \item
  4727. Assign \locvar{NLBS} the value $(\bitvar{NMBS}*4)$.
  4728. \item
  4729. For each consecutive value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$,
  4730. assign $\locvar{TIS}[\locvar{\bi}]$ the value zero.
  4731. \item
  4732. Assign \locvar{EOBS} the value 0.
  4733. \item
  4734. For each consecutive value of \locvar{\ti} from 0 to 63:
  4735. \begin{enumerate}
  4736. \item
  4737. If \locvar{\ti} is $0$ or $1$:
  4738. \begin{enumerate}
  4739. \item
  4740. Read a 4-bit unsigned integer as \locvar{\hti_L}.
  4741. \item
  4742. Read a 4-bit unsigned integer as \locvar{\hti_C}.
  4743. \end{enumerate}
  4744. \item
  4745. For each consecutive value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$ for
  4746. which $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero and
  4747. $\locvar{TIS}[\locvar{\bi}]$ equals \locvar{\ti}:
  4748. \begin{enumerate}
  4749. \item
  4750. Assign $\bitvar{NCOEFFS}[\locvar{\bi}]$ the value \locvar{\ti}.
  4751. \item
  4752. If \locvar{EOBS} is greater than zero:
  4753. \begin{enumerate}
  4754. \item
  4755. For each value of \locvar{\tj} from $\locvar{\ti}$ to 63, assign
  4756. $\bitvar{COEFFS}[\locvar{\bi}][\locvar{\tj}]$ the value zero.
  4757. \item
  4758. Assign $\locvar{TIS}[\locvar{\bi}]$ the value 64.
  4759. \item
  4760. Assign \locvar{EOBS} the value $(\locvar{EOBS}-1)$.
  4761. \end{enumerate}
  4762. \item
  4763. Otherwise:
  4764. \begin{enumerate}
  4765. \item
  4766. Assign \locvar{HG} a value based on \locvar{\ti} from
  4767. Table~\ref{tab:huff-groups}.
  4768. \begin{table}[htbp]
  4769. \begin{center}
  4770. \begin{tabular}{lc}\toprule
  4771. \locvar{\ti} & \locvar{HG} \\\midrule
  4772. $0$ & $0$ \\
  4773. $1\ldots 5$ & $1$ \\
  4774. $6\ldots 14$ & $2$ \\
  4775. $15\ldots 27$ & $3$ \\
  4776. $28\ldots 63$ & $4$ \\
  4777. \bottomrule\end{tabular}
  4778. \end{center}
  4779. \caption{Huffman Table Groups}
  4780. \label{tab:huff-groups}
  4781. \end{table}
  4782. \item
  4783. If \locvar{\bi} is less than \locvar{NLBS}, assign \locvar{\hti} the value
  4784. $(16*\locvar{HG}+\locvar{\hti_L})$.
  4785. \item
  4786. Otherwise, assign \locvar{\hti} the value
  4787. $(16*\locvar{HG}+\locvar{\hti_C})$.
  4788. \item
  4789. Read one bit at a time until one of the codes in $\bitvar{HTS}[\locvar{\hti}]$
  4790. is recognized, and assign the value to \locvar{TOKEN}.
  4791. \item
  4792. If \locvar{TOKEN} is less than 7, expand an EOB token using the procedure given
  4793. in Section~\ref{sub:eob-token} to update $\locvar{TIS}[\locvar{\bi}]$,
  4794. $\bitvar{COEFFS}[\locvar{\bi}]$, and \locvar{EOBS}.
  4795. \item
  4796. Otherwise, expand a coefficient token using the procedure given in
  4797. Section~\ref{sub:coeff-token} to update $\locvar{TIS}[\locvar{\bi}]$,
  4798. $\bitvar{COEFFS}[\locvar{\bi}]$, and $\bitvar{NCOEFFS}[\locvar{\bi}]$.
  4799. \end{enumerate}
  4800. \end{enumerate}
  4801. \end{enumerate}
  4802. \end{enumerate}
  4803. \section{Undoing DC Prediction}
  4804. The actual value of a DC coefficient decoded by Section~\ref{sec:dct-decode} is
  4805. the residual from a predicted value computed by the encoder.
  4806. This prediction is only applied to DC coefficients.
  4807. Quantized AC coefficients are encoded directly.
  4808. This section describes how to undo this prediction to recover the original
  4809. DC coefficients.
  4810. The predicted DC value for a block is computed from the DC values of its
  4811. immediate neighbors which precede the block in raster order.
  4812. Thus, reversing this prediction must procede in raster order, instead of coded
  4813. order.
  4814. Note that this step comes before dequantizing the coefficients.
  4815. For this reason, DC coefficients are all quantized with the same \qi\ value,
  4816. regardless of the block-level \qi\ values decoded in
  4817. Section~\ref{sub:block-qis}.
  4818. Those \qi\ values are applied only to the AC coefficients.
  4819. \subsection{Computing the DC Predictor}
  4820. \label{sub:dc-pred}
  4821. \paragraph{Input parameters:}\hfill\\*
  4822. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4823. \multicolumn{1}{c}{Name} &
  4824. \multicolumn{1}{c}{Type} &
  4825. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4826. \multicolumn{1}{c}{Signed?} &
  4827. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4828. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4829. 1 & No & An \bitvar{NBS}-element array of flags
  4830. indicating which blocks are coded. \\
  4831. \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4832. 3 & No & An \bitvar{NMBS}-element array of
  4833. coding modes for each macro block. \\
  4834. \bitvar{LASTDC} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4835. 16 & Yes & A 3-element array containing the
  4836. most recently decoded DC value, one for inter mode and for each reference
  4837. frame. \\
  4838. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  4839. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  4840. quantized DCT coefficient values for each block in zig-zag order. \\
  4841. \bitvar{\bi} & Integer & 36 & No & The index of the current block in
  4842. coded order. \\
  4843. \bottomrule\end{tabularx}
  4844. \paragraph{Output parameters:}\hfill\\*
  4845. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4846. \multicolumn{1}{c}{Name} &
  4847. \multicolumn{1}{c}{Type} &
  4848. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4849. \multicolumn{1}{c}{Signed?} &
  4850. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4851. \bitvar{DCPRED} & Integer & 16 & Yes & The predicted DC value for the current
  4852. block. \\
  4853. \bottomrule\end{tabularx}
  4854. \paragraph{Variables used:}\hfill\\*
  4855. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4856. \multicolumn{1}{c}{Name} &
  4857. \multicolumn{1}{c}{Type} &
  4858. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4859. \multicolumn{1}{c}{Signed?} &
  4860. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4861. \locvar{P} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4862. 1 & No & A 4-element array indicating which
  4863. neighbors can be used for DC prediction. \\
  4864. \locvar{PBI} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4865. 36 & No & A 4-element array containing the
  4866. coded-order block index of the current block's neighbors. \\
  4867. \locvar{W} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4868. 7 & Yes & A 4-element array of the weights to
  4869. apply to each neighboring DC value. \\
  4870. \locvar{PDIV} & Integer & 8 & No & The valud to divide the weighted sum
  4871. by. \\
  4872. \locvar{\bj} & Integer & 36 & No & The index of a neighboring block in
  4873. coded order. \\
  4874. \locvar{\mbi} & Integer & 32 & No & The index of the macro block
  4875. containing block \locvar{\bi}. \\
  4876. \locvar{\mbi} & Integer & 32 & No & The index of the macro block
  4877. containing block \locvar{\bj}. \\
  4878. \locvar{\rfi} & Integer & 2 & No & The index of the reference frame
  4879. indicated by the coding mode for macro block \locvar{\mbi}. \\
  4880. \bottomrule\end{tabularx}
  4881. \medskip
  4882. This procedure outlines how a predictor is formed for a single block.
  4883. The predictor is computed as a weighted sum of the neighboring DC values from
  4884. coded blocks which use the same reference frame.
  4885. This latter condition is determined only by checking the coding mode for the
  4886. block.
  4887. Even if the golden frame and the previous frame are in fact the same, e.g. for
  4888. the first inter frame after an intra frame, they are still treated as being
  4889. different for the purposes of DC prediction.
  4890. The weighted sum is divided by a power of two, with truncation towards zero,
  4891. and the result is checked for outranging if necessary.
  4892. If there are no neighboring coded blocks which use the same reference frame as
  4893. the current block, then the most recent DC value of any block that used that
  4894. reference frame is used instead.
  4895. If no such block exists, then the predictor is set to zero.
  4896. \begin{enumerate}
  4897. \item
  4898. Assign \locvar{\mbi} the index of the macro block containing block
  4899. \bitvar{\bi}.
  4900. \item
  4901. Assign \locvar{\rfi} the value of the Reference Frame Index column of
  4902. Table~\ref{tab:cm-refs} corresponding to $\bitvar{MBMODES}[\locvar{\mbi}]$.
  4903. \begin{table}[htpb]
  4904. \begin{center}
  4905. \begin{tabular}{ll}\toprule
  4906. Coding Mode & Reference Frame Index \\\midrule
  4907. $0$ (INTER\_NOMV) & $1$ (Previous) \\
  4908. $1$ (INTRA) & $0$ (None) \\
  4909. $2$ (INTER\_MV) & $1$ (Previous) \\
  4910. $3$ (INTER\_MV\_LAST) & $1$ (Previous) \\
  4911. $4$ (INTER\_MV\_LAST2) & $1$ (Previous) \\
  4912. $5$ (INTER\_GOLDEN\_NOMV) & $2$ (Golden) \\
  4913. $6$ (INTER\_GOLDEN\_MV) & $2$ (Golden) \\
  4914. $7$ (INTER\_MV\_FOUR) & $1$ (Previous) \\
  4915. \bottomrule\end{tabular}
  4916. \end{center}
  4917. \caption{Reference Frames for Each Coding Mode}
  4918. \label{tab:cm-refs}
  4919. \end{table}
  4920. \item
  4921. If block \locvar{\bi} is not along the left edge of the coded frame:
  4922. \begin{enumerate}
  4923. \item
  4924. Assign \locvar{\bj} the coded-order index of block \locvar{\bi}'s left
  4925. neighbor, i.e., in the same row but one column to the left.
  4926. \item
  4927. If $\bitvar{BCODED}[\bj]$ is not zero:
  4928. \begin{enumerate}
  4929. \item
  4930. Assign \locvar{\mbj} the index of the macro block containing block
  4931. \locvar{\bj}.
  4932. \item
  4933. If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
  4934. corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
  4935. \begin{enumerate}
  4936. \item
  4937. Assign $\locvar{P}[0]$ the value $1$.
  4938. \item
  4939. Assign $\locvar{PBI}[0]$ the value \locvar{\bj}.
  4940. \end{enumerate}
  4941. \item
  4942. Otherwise, assign $\locvar{P}[0]$ the value zero.
  4943. \end{enumerate}
  4944. \item
  4945. Otherwise, assign $\locvar{P}[0]$ the value zero.
  4946. \end{enumerate}
  4947. \item
  4948. Otherwise, assign $\locvar{P}[0]$ the value zero.
  4949. \item
  4950. If block \locvar{\bi} is not along the left edge nor the bottom edge of the
  4951. coded frame:
  4952. \begin{enumerate}
  4953. \item
  4954. Assign \locvar{\bj} the coded-order index of block \locvar{\bi}'s lower-left
  4955. neighbor, i.e., one row down and one column to the left.
  4956. \item
  4957. If $\bitvar{BCODED}[\bj]$ is not zero:
  4958. \begin{enumerate}
  4959. \item
  4960. Assign \locvar{\mbj} the index of the macro block containing block
  4961. \locvar{\bj}.
  4962. \item
  4963. If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
  4964. corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
  4965. \begin{enumerate}
  4966. \item
  4967. Assign $\locvar{P}[1]$ the value $1$.
  4968. \item
  4969. Assign $\locvar{PBI}[1]$ the value \locvar{\bj}.
  4970. \end{enumerate}
  4971. \item
  4972. Otherwise, assign $\locvar{P}[1]$ the value zero.
  4973. \end{enumerate}
  4974. \item
  4975. Otherwise, assign $\locvar{P}[1]$ the value zero.
  4976. \end{enumerate}
  4977. \item
  4978. Otherwise, assign $\locvar{P}[1]$ the value zero.
  4979. \item
  4980. If block \locvar{\bi} is not along the the bottom edge of the coded frame:
  4981. \begin{enumerate}
  4982. \item
  4983. Assign \locvar{\bj} the coded-order index of block \locvar{\bi}'s lower
  4984. neighbor, i.e., in the same column but one row down.
  4985. \item
  4986. If $\bitvar{BCODED}[\bj]$ is not zero:
  4987. \begin{enumerate}
  4988. \item
  4989. Assign \locvar{\mbj} the index of the macro block containing block
  4990. \locvar{\bj}.
  4991. \item
  4992. If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
  4993. corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
  4994. \begin{enumerate}
  4995. \item
  4996. Assign $\locvar{P}[2]$ the value $1$.
  4997. \item
  4998. Assign $\locvar{PBI}[2]$ the value \locvar{\bj}.
  4999. \end{enumerate}
  5000. \item
  5001. Otherwise, assign $\locvar{P}[2]$ the value zero.
  5002. \end{enumerate}
  5003. \item
  5004. Otherwise, assign $\locvar{P}[2]$ the value zero.
  5005. \end{enumerate}
  5006. \item
  5007. Otherwise, assign $\locvar{P}[2]$ the value zero.
  5008. \item
  5009. If block \locvar{\bi} is not along the right edge nor the bottom edge of the
  5010. coded frame:
  5011. \begin{enumerate}
  5012. \item
  5013. Assign \locvar{\bj} the coded-order index of block \locvar{\bi}'s lower-right
  5014. neighbor, i.e., one row down and one column to the right.
  5015. \item
  5016. If $\bitvar{BCODED}[\bj]$ is not zero:
  5017. \begin{enumerate}
  5018. \item
  5019. Assign \locvar{\mbj} the index of the macro block containing block
  5020. \locvar{\bj}.
  5021. \item
  5022. If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
  5023. corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
  5024. \begin{enumerate}
  5025. \item
  5026. Assign $\locvar{P}[3]$ the value $1$.
  5027. \item
  5028. Assign $\locvar{PBI}[3]$ the value \locvar{\bj}.
  5029. \end{enumerate}
  5030. \item
  5031. Otherwise, assign $\locvar{P}[3]$ the value zero.
  5032. \end{enumerate}
  5033. \item
  5034. Otherwise, assign $\locvar{P}[3]$ the value zero.
  5035. \end{enumerate}
  5036. \item
  5037. Otherwise, assign $\locvar{P}[3]$ the value zero.
  5038. \item
  5039. If none of the values $\locvar{P}[0]$, $\locvar{P}[1]$, $\locvar{P}[2]$, nor
  5040. $\locvar{P}[3]$ are non-zero, then assign \bitvar{DCPRED} the value
  5041. $\bitvar{LASTDC}[\locvar{\rfi}]$.
  5042. \item
  5043. Otherwise:
  5044. \begin{enumerate}
  5045. \item
  5046. Assign the array \locvar{W} and the variable \locvar{PDIV} the values from the
  5047. row of Table~\ref{tab:dc-weights} corresonding to the values of each
  5048. $\locvar{P}[\idx{i}]$.
  5049. \begin{table}[htb]
  5050. \begin{center}
  5051. \begin{tabular}{ccccrrrrr}\toprule
  5052. \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[0]$ (L)} &
  5053. \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[1]$ (DL)} &
  5054. \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[2]$ (D)} &
  5055. \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[3]$ (DR)} &
  5056. \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[3]$ (L)} &
  5057. \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[1]$ (DL)} &
  5058. \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[2]$ (D)} &
  5059. \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[3]$ (DR)} &
  5060. \locvar{PDIV} \\\midrule
  5061. $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ \\
  5062. $0$ & $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $0$ & $1$ \\
  5063. $1$ & $1$ & $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ \\
  5064. $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $1$ \\
  5065. $1$ & $0$ & $1$ & $0$ & $1$ & $0$ & $1$ & $0$ & $2$ \\
  5066. $0$ & $1$ & $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $1$ \\
  5067. $1$ & $1$ & $1$ & $0$ & $29$ & $-26$ & $29$ & $0$ & $32$ \\
  5068. $0$ & $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ & $1$ \\
  5069. $1$ & $0$ & $0$ & $1$ & $75$ & $0$ & $0$ & $53$ & $128$ \\
  5070. $0$ & $1$ & $0$ & $1$ & $0$ & $1$ & $0$ & $1$ & $2$ \\
  5071. $1$ & $1$ & $0$ & $1$ & $75$ & $0$ & $0$ & $53$ & $128$ \\
  5072. $0$ & $0$ & $1$ & $1$ & $0$ & $0$ & $1$ & $0$ & $1$ \\
  5073. $1$ & $0$ & $1$ & $1$ & $75$ & $0$ & $0$ & $53$ & $128$ \\
  5074. $0$ & $1$ & $1$ & $1$ & $0$ & $3$ & $10$ & $3$ & $16$ \\
  5075. $1$ & $1$ & $1$ & $1$ & $29$ & $-26$ & $29$ & $0$ & $32$ \\
  5076. \bottomrule\end{tabular}
  5077. \end{center}
  5078. \caption{Weights and Divisors for Each Set of Available DC Predictors}
  5079. \label{tab:dc-weights}
  5080. \end{table}
  5081. \item
  5082. Assign \bitvar{DCPRED} the value zero.
  5083. \item
  5084. If $\locvar{P}[0]$ is non-zero, assign \bitvar{DCPRED} the value
  5085. $(\bitvar{DCPRED}+\locvar{W}[0]*\bitvar{COEFFS}[\locvar{PBI}[0]][0])$.
  5086. \item
  5087. If $\locvar{P}[1]$ is non-zero, assign \bitvar{DCPRED} the value
  5088. $(\bitvar{DCPRED}+\locvar{W}[1]*\bitvar{COEFFS}[\locvar{PBI}[1]][0])$.
  5089. \item
  5090. If $\locvar{P}[2]$ is non-zero, assign \bitvar{DCPRED} the value
  5091. $(\bitvar{DCPRED}+\locvar{W}[2]*\bitvar{COEFFS}[\locvar{PBI}[2]][0])$.
  5092. \item
  5093. If $\locvar{P}[3]$ is non-zero, assign \bitvar{DCPRED} the value
  5094. $(\bitvar{DCPRED}+\locvar{W}[3]*\bitvar{COEFFS}[\locvar{PBI}[3]][0])$.
  5095. \item
  5096. Assign \bitvar{DCPRED} the value $(\bitvar{DCPRED}//\locvar{PDIV})$.
  5097. \item
  5098. If $\locvar{P}[0]$, $\locvar{P}[1]$, and $\locvar{P}[2]$ are all non-zero:
  5099. \begin{enumerate}
  5100. \item
  5101. If $|\bitvar{DCPRED}-\bitvar{COEFFS}[\locvar{PBI}[2]][0]|$ is greater than
  5102. $128$, assign \bitvar{DCPRED} the value $\bitvar{COEFFS}[\locvar{PBI}[2]][0]$.
  5103. \item
  5104. Otherwise, if $|\bitvar{DCPRED}-\bitvar{COEFFS}[\locvar{PBI}[0]][0]|$ is
  5105. greater than $128$, assign \bitvar{DCPRED} the value
  5106. $\bitvar{COEFFS}[\locvar{PBI}[0]][0]$.
  5107. \item
  5108. Otherwise, if $|\bitvar{DCPRED}-\bitvar{COEFFS}[\locvar{PBI}[1]][0]|$ is
  5109. greater than $128$, assign \bitvar{DCPRED} the value
  5110. $\bitvar{COEFFS}[\locvar{PBI}[1]][0]$.
  5111. \end{enumerate}
  5112. \end{enumerate}
  5113. \end{enumerate}
  5114. \subsection{Inverting the DC Prediction Process}
  5115. \label{sub:dc-pred-undo}
  5116. \paragraph{Input parameters:}\hfill\\*
  5117. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5118. \multicolumn{1}{c}{Name} &
  5119. \multicolumn{1}{c}{Type} &
  5120. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5121. \multicolumn{1}{c}{Signed?} &
  5122. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5123. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5124. 1 & No & An \bitvar{NBS}-element array of flags
  5125. indicating which blocks are coded. \\
  5126. \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5127. 3 & No & An \bitvar{NMBS}-element array of
  5128. coding modes for each macro block. \\
  5129. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5130. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  5131. quantized DCT coefficient values for each block in zig-zag order. \\
  5132. \bottomrule\end{tabularx}
  5133. \paragraph{Output parameters:}\hfill\\*
  5134. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5135. \multicolumn{1}{c}{Name} &
  5136. \multicolumn{1}{c}{Type} &
  5137. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5138. \multicolumn{1}{c}{Signed?} &
  5139. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5140. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5141. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  5142. quantized DCT coefficient values for each block in zig-zag order. The DC
  5143. value of each block will be updated. \\
  5144. \bottomrule\end{tabularx}
  5145. \paragraph{Variables used:}\hfill\\*
  5146. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5147. \multicolumn{1}{c}{Name} &
  5148. \multicolumn{1}{c}{Type} &
  5149. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5150. \multicolumn{1}{c}{Signed?} &
  5151. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5152. \locvar{LASTDC} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5153. 16 & Yes & A 3-element array containing the
  5154. most recently decoded DC value, one for inter mode and for each reference
  5155. frame. \\
  5156. \locvar{DCPRED} & Integer & 11 & Yes & The predicted DC value for the current
  5157. block. \\
  5158. \locvar{DC} & Integer & 17 & Yes & The actual DC value for the current
  5159. block. \\
  5160. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  5161. coded order. \\
  5162. \locvar{\mbi} & Integer & 32 & No & The index of the macro block
  5163. containing block \locvar{\bi}. \\
  5164. \locvar{\rfi} & Integer & 2 & No & The index of the reference frame
  5165. indicated by the coding mode for macro block \locvar{\mbi}. \\
  5166. \locvar{\pli} & Integer & 2 & No & A color plane index. \\
  5167. \bottomrule\end{tabularx}
  5168. \medskip
  5169. This procedure describes the complete process of undoing the DC prediction to
  5170. recover the original DC values.
  5171. Because it is possible to add a value as large as $580$ to the predicted DC
  5172. coefficient value at every block, which will then be used to increase the
  5173. predictor for the next block, the reconstructed DC value could overflow a
  5174. 16-bit integer.
  5175. This is handled by truncating the result to a 16-bit signed representation,
  5176. simply throwing away any higher bits in the two's complement representation of
  5177. the number.
  5178. \begin{enumerate}
  5179. \item
  5180. For each consecutive value of \locvar{\pli} from $0$ to $2$:
  5181. \begin{enumerate}
  5182. \item
  5183. Assign $\locvar{LASTDC}[0]$ the value zero.
  5184. \item
  5185. Assign $\locvar{LASTDC}[1]$ the value zero.
  5186. \item
  5187. Assign $\locvar{LASTDC}[2]$ the value zero.
  5188. \item
  5189. For each block of color plane \locvar{\pli} in {\em raster} order, with
  5190. coded-order index \locvar{\bi}:
  5191. \begin{enumerate}
  5192. \item
  5193. If $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero:
  5194. \begin{enumerate}
  5195. \item
  5196. Compute the value \locvar{DCPRED} using the procedure outlined in
  5197. Section~\ref{sub:dc-pred}.
  5198. \item
  5199. Assign \locvar{DC} the value
  5200. $(\bitvar{COEFFS}[\locvar{\bi}][0]+\locvar{DCPRED})$.
  5201. \item
  5202. Truncate \locvar{DC} to a 16-bit representation by dropping any higher-order
  5203. bits.
  5204. \item
  5205. Assign $\bitvar{COEFFS}[\locvar{\bi}][0]$ the value \locvar{DC}.
  5206. \item
  5207. Assign \locvar{\mbi} the index of the macro block containing block
  5208. \locvar{\bi}.
  5209. \item
  5210. Assign \locvar{\rfi} the value of the Reference Frame Index column of
  5211. Table~\ref{tab:cm-refs} corresponding to $\bitvar{MBMODES}[\locvar{\mbi}]$.
  5212. \item
  5213. Assign $\locvar{LASTDC}[\rfi]$ the value $\locvar{DC}$.
  5214. \end{enumerate}
  5215. \end{enumerate}
  5216. \end{enumerate}
  5217. \end{enumerate}
  5218. \section{Reconstruction}
  5219. At this stage, the complete contents of the data packet have been decoded.
  5220. All that remains is to reconstruct the contents of the new frame.
  5221. This is applied on a block by block basis, and as each block is independent,
  5222. the order they are processed in does not matter.
  5223. \subsection{Predictors}
  5224. \label{sec:predictors}
  5225. For each block, a predictor is formed based on its coding mode and motion
  5226. vector.
  5227. There are three basic types of predictors: the intra predictor, the whole-pixel
  5228. predictor, and the half-pixel predictor.
  5229. The former is used for all blocks coded in INTRA mode, while all other blocks
  5230. use one of the latter two.
  5231. The whole-pixel predictor is used if the fractional part of both motion vector
  5232. components is zero, otherwise the half-pixel predictor is used.
  5233. \subsubsection{The Intra Predictor}
  5234. \label{sub:predintra}
  5235. \paragraph{Input parameters:} None.
  5236. \paragraph{Output parameters:}\hfill\\*
  5237. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5238. \multicolumn{1}{c}{Name} &
  5239. \multicolumn{1}{c}{Type} &
  5240. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5241. \multicolumn{1}{c}{Signed?} &
  5242. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5243. \bitvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5244. 8 & No & An $8\times 8$ array of predictor
  5245. values to use for INTRA coded blocks. \\
  5246. \bottomrule\end{tabularx}
  5247. \paragraph{Variables used:}\hfill\\*
  5248. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5249. \multicolumn{1}{c}{Name} &
  5250. \multicolumn{1}{c}{Type} &
  5251. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5252. \multicolumn{1}{c}{Signed?} &
  5253. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5254. \locvar{\idx{bx}} & Integer & 3 & No & The horizontal pixel index in the
  5255. block. \\
  5256. \locvar{\idx{by}} & Integer & 3 & No & The vertical pixel index in the
  5257. block. \\
  5258. \bottomrule\end{tabularx}
  5259. \medskip
  5260. The intra predictor is nothing more than the constant value $128$.
  5261. This is applied for the sole purpose of centering the range of possible DC
  5262. values for INTRA blocks around zero.
  5263. \begin{enumerate}
  5264. \item
  5265. For each value of \locvar{\idx{by}} from $0$ to $7$, inclusive:
  5266. \begin{enumerate}
  5267. \item
  5268. For each value of \locvar{\idx{bx}} from $0$ to $7$, inclusive:
  5269. \begin{enumerate}
  5270. \item
  5271. Assign $\bitvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value $128$.
  5272. \end{enumerate}
  5273. \end{enumerate}
  5274. \end{enumerate}
  5275. \subsubsection{The Whole-Pixel Predictor}
  5276. \label{sub:predfullpel}
  5277. \paragraph{Input parameters:}\hfill\\*
  5278. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5279. \multicolumn{1}{c}{Name} &
  5280. \multicolumn{1}{c}{Type} &
  5281. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5282. \multicolumn{1}{c}{Signed?} &
  5283. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5284. \bitvar{RPW} & Integer & 20 & No & The width of the current plane of the
  5285. reference frame in pixels. \\
  5286. \bitvar{RPH} & Integer & 20 & No & The height of the current plane of the
  5287. reference frame in pixels. \\
  5288. \bitvar{REFP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5289. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  5290. array containing the contents of the current plane of the reference frame. \\
  5291. \bitvar{BX} & Integer & 20 & No & The horizontal pixel index of the
  5292. lower-left corner of the current block. \\
  5293. \bitvar{BY} & Integer & 20 & No & The vertical pixel index of the
  5294. lower-left corner of the current block. \\
  5295. \bitvar{MVX} & Integer & 5 & No & The horizontal component of the block
  5296. motion vector.
  5297. This is always a whole-pixel value. \\
  5298. \bitvar{MVY} & Integer & 5 & No & The vertical component of the block
  5299. motion vector.
  5300. This is always a whole-pixel value. \\
  5301. \bottomrule\end{tabularx}
  5302. \paragraph{Output parameters:}\hfill\\*
  5303. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5304. \multicolumn{1}{c}{Name} &
  5305. \multicolumn{1}{c}{Type} &
  5306. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5307. \multicolumn{1}{c}{Signed?} &
  5308. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5309. \bitvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5310. 8 & No & An $8\times 8$ array of predictor
  5311. values to use for INTER coded blocks. \\
  5312. \bottomrule\end{tabularx}
  5313. \paragraph{Variables used:}\hfill\\*
  5314. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5315. \multicolumn{1}{c}{Name} &
  5316. \multicolumn{1}{c}{Type} &
  5317. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5318. \multicolumn{1}{c}{Signed?} &
  5319. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5320. \locvar{\idx{bx}} & Integer & 3 & Yes & The horizontal pixel index in the
  5321. block. \\
  5322. \locvar{\idx{by}} & Integer & 3 & Yes & The vertical pixel index in the
  5323. block. \\
  5324. \locvar{\idx{rx}} & Integer & 20 & No & The horizontal pixel index in the
  5325. reference frame. \\
  5326. \locvar{\idx{ry}} & Integer & 20 & No & The vertical pixel index in the
  5327. reference frame. \\
  5328. \bottomrule\end{tabularx}
  5329. \medskip
  5330. The whole pixel predictor simply copies verbatim the contents of the reference
  5331. frame pointed to by the block's motion vector.
  5332. If the vector points outside the reference frame, then the closest value on the
  5333. edge of the reference frame is used instead.
  5334. In practice, this is usually implemented by expanding the size of the reference
  5335. frame by $8$ or $16$ pixels on each side---depending on whether or not the
  5336. corresponding axis is subsampled in the current plane---and copying the border
  5337. pixels into this region.
  5338. \begin{enumerate}
  5339. \item
  5340. For each value of \locvar{\idx{by}} from $0$ to $7$, inclusive:
  5341. \begin{enumerate}
  5342. \item
  5343. Assign \locvar{\idx{ry}} the value
  5344. $(\bitvar{BY}+\bitvar{MVY}+\locvar{\idx{by}})$.
  5345. \item
  5346. If \locvar{\idx{ry}} is greater than $(\bitvar{RPH}-1)$, assign
  5347. \locvar{\idx{ry}} the value $(\bitvar{RPH}-1)$.
  5348. \item
  5349. If \locvar{\idx{ry}} is less than zero, assign \locvar{\idx{ry}} the value
  5350. zero.
  5351. \item
  5352. For each value of \locvar{\idx{bx}} from $0$ to $7$, inclusive:
  5353. \begin{enumerate}
  5354. \item
  5355. Assign \locvar{\idx{rx}} the value
  5356. $(\bitvar{BX}+\bitvar{MVX}+\locvar{\idx{bx}})$.
  5357. \item
  5358. If \locvar{\idx{rx}} is greater than $(\bitvar{RPW}-1)$, assign
  5359. \locvar{\idx{rx}} the value $(\bitvar{RPW}-1)$.
  5360. \item
  5361. If \locvar{\idx{rx}} is less than zero, assign \locvar{\idx{rx}} the value
  5362. zero.
  5363. \item
  5364. Assign $\bitvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value
  5365. $\bitvar{REFP}[\locvar{\idx{ry}}][\locvar{\idx{rx}}]$.
  5366. \end{enumerate}
  5367. \end{enumerate}
  5368. \end{enumerate}
  5369. \subsubsection{The Half-Pixel Predictor}
  5370. \label{sub:predhalfpel}
  5371. \paragraph{Input parameters:}\hfill\\*
  5372. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5373. \multicolumn{1}{c}{Name} &
  5374. \multicolumn{1}{c}{Type} &
  5375. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5376. \multicolumn{1}{c}{Signed?} &
  5377. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5378. \bitvar{RPW} & Integer & 20 & No & The width of the current plane of the
  5379. reference frame in pixels. \\
  5380. \bitvar{RPH} & Integer & 20 & No & The height of the current plane of the
  5381. reference frame in pixels. \\
  5382. \bitvar{REFP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5383. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  5384. array containing the contents of the current plane of the reference frame. \\
  5385. \bitvar{BX} & Integer & 20 & No & The horizontal pixel index of the
  5386. lower-left corner of the current block. \\
  5387. \bitvar{BY} & Integer & 20 & No & The vertical pixel index of the
  5388. lower-left corner of the current block. \\
  5389. \bitvar{MVX} & Integer & 5 & No & The horizontal component of the first
  5390. whole-pixel motion vector. \\
  5391. \bitvar{MVY} & Integer & 5 & No & The vertical component of the first
  5392. whole-pixel motion vector. \\
  5393. \bitvar{MVX2} & Integer & 5 & No & The horizontal component of the second
  5394. whole-pixel motion vector. \\
  5395. \bitvar{MVY2} & Integer & 5 & No & The vertical component of the second
  5396. whole-pixel motion vector. \\
  5397. \bottomrule\end{tabularx}
  5398. \paragraph{Output parameters:}\hfill\\*
  5399. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5400. \multicolumn{1}{c}{Name} &
  5401. \multicolumn{1}{c}{Type} &
  5402. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5403. \multicolumn{1}{c}{Signed?} &
  5404. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5405. \bitvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5406. 8 & No & An $8\times 8$ array of predictor
  5407. values to use for INTER coded blocks. \\
  5408. \bottomrule\end{tabularx}
  5409. \paragraph{Variables used:}\hfill\\*
  5410. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5411. \multicolumn{1}{c}{Name} &
  5412. \multicolumn{1}{c}{Type} &
  5413. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5414. \multicolumn{1}{c}{Signed?} &
  5415. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5416. \locvar{\idx{bx}} & Integer & 3 & Yes & The horizontal pixel index in the
  5417. block. \\
  5418. \locvar{\idx{by}} & Integer & 3 & Yes & The vertical pixel index in the
  5419. block. \\
  5420. \locvar{\idx{rx1}} & Integer & 20 & No & The first horizontal pixel index in
  5421. the reference frame. \\
  5422. \locvar{\idx{ry1}} & Integer & 20 & No & The first vertical pixel index in the
  5423. reference frame. \\
  5424. \locvar{\idx{rx2}} & Integer & 20 & No & The second horizontal pixel index in
  5425. the reference frame. \\
  5426. \locvar{\idx{ry2}} & Integer & 20 & No & The second vertical pixel index in
  5427. the reference frame. \\
  5428. \bottomrule\end{tabularx}
  5429. \medskip
  5430. If one or both of the components of the block motion vector is not a
  5431. whole-pixel value, then the half-pixel predictor is used.
  5432. The half-pixel predictor converts the fractional motion vector into two
  5433. whole-pixel motion vectors.
  5434. The first is formed by truncating the values of each component towards zero,
  5435. and the second is formed by truncating them away from zero.
  5436. The contributions from the reference frame at the locations pointed to by each
  5437. vector are averaged, truncating towards negative infinity.
  5438. Only two samples from the reference frame contribute to each predictor value,
  5439. even if both components of the motion vector have non-zero fractional
  5440. components.
  5441. Motion vector components with quarter-pixel accuracy in the chroma planes are
  5442. treated exactly the same as those with half-pixel accuracy.
  5443. Any non-zero fractional part gets rounded one way in the first vector, and the
  5444. other way in the second.
  5445. \begin{enumerate}
  5446. \item
  5447. For each value of \locvar{\idx{by}} from $0$ to $7$, inclusive:
  5448. \begin{enumerate}
  5449. \item
  5450. Assign \locvar{\idx{ry1}} the value
  5451. $(\bitvar{BY}+\bitvar{MVY1}+\locvar{\idx{by}})$.
  5452. \item
  5453. If \locvar{\idx{ry1}} is greater than $(\bitvar{RPH}-1)$, assign
  5454. \locvar{\idx{ry1}} the value $(\bitvar{RPH}-1)$.
  5455. \item
  5456. If \locvar{\idx{ry1}} is less than zero, assign \locvar{\idx{ry1}} the value
  5457. zero.
  5458. \item
  5459. Assign \locvar{\idx{ry2}} the value
  5460. $(\bitvar{BY}+\bitvar{MVY2}+\locvar{\idx{by}})$.
  5461. \item
  5462. If \locvar{\idx{ry2}} is greater than $(\bitvar{RPH}-1)$, assign
  5463. \locvar{\idx{ry2}} the value $(\bitvar{RPH}-1)$.
  5464. \item
  5465. If \locvar{\idx{ry2}} is less than zero, assign \locvar{\idx{ry2}} the value
  5466. zero.
  5467. \item
  5468. For each value of \locvar{\idx{bx}} from $0$ to $7$, inclusive:
  5469. \begin{enumerate}
  5470. \item
  5471. Assign \locvar{\idx{rx1}} the value
  5472. $(\bitvar{BX}+\bitvar{MVX1}+\locvar{\idx{bx}})$.
  5473. \item
  5474. If \locvar{\idx{rx1}} is greater than $(\bitvar{RPW}-1)$, assign
  5475. \locvar{\idx{rx1}} the value $(\bitvar{RPW}-1)$.
  5476. \item
  5477. If \locvar{\idx{rx1}} is less than zero, assign \locvar{\idx{rx1}} the value
  5478. zero.
  5479. \item
  5480. Assign \locvar{\idx{rx2}} the value
  5481. $(\bitvar{BX}+\bitvar{MVX2}+\locvar{\idx{bx}})$.
  5482. \item
  5483. If \locvar{\idx{rx2}} is greater than $(\bitvar{RPW}-1)$, assign
  5484. \locvar{\idx{rx2}} the value $(\bitvar{RPW}-1)$.
  5485. \item
  5486. If \locvar{\idx{rx2}} is less than zero, assign \locvar{\idx{rx2}} the value
  5487. zero.
  5488. \item
  5489. Assign $\bitvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value
  5490. \begin{equation*}
  5491. (\bitvar{REFP}[\locvar{\idx{ry1}}][\locvar{\idx{rx1}}]+
  5492. \bitvar{REFP}[\locvar{\idx{ry2}}][\locvar{\idx{rx2}}])>>1.
  5493. \end{equation*}
  5494. \end{enumerate}
  5495. \end{enumerate}
  5496. \end{enumerate}
  5497. \subsection{Dequantization}
  5498. \label{sub:dequant}
  5499. \paragraph{Input parameters:}\hfill\\*
  5500. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5501. \multicolumn{1}{c}{Name} &
  5502. \multicolumn{1}{c}{Type} &
  5503. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5504. \multicolumn{1}{c}{Signed?} &
  5505. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5506. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5507. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  5508. quantized DCT coefficient values for each block in zig-zag order. \\
  5509. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  5510. 16 & No & A 64-element array of scale values for
  5511. AC coefficients for each \qi\ value. \\
  5512. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  5513. 16 & No & A 64-element array of scale values for
  5514. the DC coefficient for each \qi\ value. \\
  5515. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  5516. 8 & No & A $\bitvar{NBMS}\times 64$ array
  5517. containing the base matrices. \\
  5518. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  5519. 6 & No & A $2\times 3$ array containing the
  5520. number of quant ranges for a given \qti\ and \pli, respectively.
  5521. This is at most $63$. \\
  5522. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  5523. 6 & No & A $2\times 3\times 63$ array of the
  5524. sizes of each quant range for a given \qti\ and \pli, respectively.
  5525. Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
  5526. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  5527. 9 & No & A $2\times 3\times 64$ array of the
  5528. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  5529. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
  5530. \bitvar{\qti} & Integer & 1 & No & A quantization type index.
  5531. See Table~\ref{tab:quant-types}.\\
  5532. \bitvar{\pli} & Integer & 2 & No & A color plane index.
  5533. See Table~\ref{tab:color-planes}.\\
  5534. \bitvar{\idx{qi0}} & Integer & 6 & No & The quantization index of the DC
  5535. coefficient. \\
  5536. \bitvar{\qi} & Integer & 6 & No & The quantization index of the AC
  5537. coefficients. \\
  5538. \bitvar{\bi} & Integer & 36 & No & The index of the current block in
  5539. coded order. \\
  5540. \bottomrule\end{tabularx}
  5541. \paragraph{Output parameters:}\hfill\\*
  5542. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5543. \multicolumn{1}{c}{Name} &
  5544. \multicolumn{1}{c}{Type} &
  5545. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5546. \multicolumn{1}{c}{Signed?} &
  5547. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5548. \bitvar{DQC} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5549. 14 & Yes & A $64$-element array of dequantized
  5550. DCT coefficients in natural order (cf. Section~\ref{sec:dct-coeffs}). \\
  5551. \bottomrule\end{tabularx}
  5552. \paragraph{Variables used:}\hfill\\*
  5553. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5554. \multicolumn{1}{c}{Name} &
  5555. \multicolumn{1}{c}{Type} &
  5556. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5557. \multicolumn{1}{c}{Signed?} &
  5558. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5559. \locvar{QMAT} & \multicolumn{1}{p{40pt}}{Integer array} &
  5560. 16 & No & A 64-element array of quantization
  5561. values for each DCT coefficient in natural order. \\
  5562. \locvar{\ci} & Integer & 6 & No & The DCT coefficient index in natural
  5563. order. \\
  5564. \locvar{\zzi} & Integer & 6 & No & The DCT coefficient index in zig-zag
  5565. order. \\
  5566. \locvar{C} & Integer & 29 & Yes & A single dequantized coefficient. \\
  5567. \bottomrule\end{tabularx}
  5568. \medskip
  5569. This procedure takes the quantized DCT coefficient values in zig-zag order for
  5570. a single block---after DC prediction has been undone---and returns the
  5571. dequantized values in natural order.
  5572. If large coefficient values are decoded for coarsely quantized coefficients,
  5573. the resulting dequantized value can be significantly larger than 16 bits.
  5574. Such a coefficient is truncated to a signed 16-bit representation by discarding
  5575. the higher-order bits of its twos-complement representation.
  5576. Although this procedure recomputes the quantization matrices from the
  5577. parameters in the setup header for each block, there are at most six different
  5578. ones used for each color plane.
  5579. An efficient implementation could compute them once in advance.
  5580. \begin{enumerate}
  5581. \item
  5582. Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS},
  5583. \bitvar{QRSIZES}, \bitvar{QRBMIS}, \bitvar{\qti}, \bitvar{\pli}, and
  5584. \bitvar{\idx{qi0}}, use the procedure given in Section~\ref{sub:quant-mat} to
  5585. compute the DC quantization matrix \locvar{QMAT}.
  5586. \item
  5587. Assign \locvar{C} the value
  5588. $\bitvar{COEFFS}[\bitvar{\bi}][0]*\locvar{QMAT}[0]$.
  5589. \item
  5590. Truncate \locvar{C} to a 16-bit representation by dropping any higher-order
  5591. bits.
  5592. \item
  5593. Assign $\bitvar{DQC}[0]$ the value \locvar{C}.
  5594. \item
  5595. Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS},
  5596. \bitvar{QRSIZES}, \bitvar{QRBMIS}, \bitvar{\qti}, \bitvar{\pli}, and
  5597. \bitvar{\qi}, use the procedure given in Section~\ref{sub:quant-mat} to
  5598. compute the AC quantization matrix \locvar{QMAT}.
  5599. \item
  5600. For each value of \locvar{\ci} from 1 to 63, inclusive:
  5601. \begin{enumerate}
  5602. \item
  5603. Assign \locvar{\zzi} the index in zig-zag order corresponding to \locvar{\ci}.
  5604. E.g., the value at row $(\locvar{\ci}//8)$ and column $(\locvar{\ci}\%8)$ in
  5605. Figure~\ref{tab:zig-zag}
  5606. \item
  5607. Assign \locvar{C} the value
  5608. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\zzi}]*\locvar{QMAT}[\locvar{\ci}]$.
  5609. \item
  5610. Truncate \locvar{C} to a 16-bit representation by dropping any higher-order
  5611. bits.
  5612. \item
  5613. Assign $\bitvar{DQC}[\locvar{\ci}]$ the value \locvar{C}.
  5614. \end{enumerate}
  5615. \end{enumerate}
  5616. \subsection{The Inverse DCT}
  5617. The 2D inverse DCT is separated into two applications of the 1D inverse DCT.
  5618. The transform is first applied to each row, and then applied to each column of
  5619. the result.
  5620. Each application of the 1D inverse DCT scales the values by a factor of two
  5621. relative to the orthonormal version of the transform, for a total scale factor
  5622. of four for the 2D transform.
  5623. It is assumed that a similar scale factor is applied during the forward DCT
  5624. used in the encoder, so that a division by 16 is required after the transform
  5625. has been applied in both directions.
  5626. The inclusion of this scale factor allows the integerized transform to operate
  5627. with increased precision.
  5628. All divisions throughout the transform are implemented with right shifts.
  5629. Only the final division by $16$ is rounded, with ties rounded towards positive
  5630. infinity.
  5631. All intermediate values are truncated to a 32-bit signed representation by
  5632. discarding any higher-order bits in their two's complement representation.
  5633. The final output of each 1D transform is truncated to a 16-bit signed value in
  5634. the same manner.
  5635. In practice, if the high word of a $16\times 16$ bit multiplication can be
  5636. obtained directly, 16 bits is sufficient for every calculation except scaling
  5637. by $C4$.
  5638. Thus we truncate to 16 bits before that multiplication to allow an
  5639. implementation entirely in 16-bit registers.
  5640. Implementations using larger registers must sign-extend the 16-bit value to
  5641. maintain compatibility.
  5642. Note that if 16-bit register are used, overflow in the additions and
  5643. subtractions should be handled using \textit{unsaturated} arithmetic.
  5644. That is, the high-order bits should be discarded and the low-order bits
  5645. retained, instead of clamping the result to the maximum or minimum value.
  5646. This allows the maximum flexibility in re-ordering these instructions without
  5647. deviating from this specification.
  5648. The 1D transform can only overflow if input coefficients larger than $\pm 6201$
  5649. are present.
  5650. However, the result of applying the 2D forward transform on pixel values in the
  5651. range $-255\ldots 255$ can be as large as $\pm 8157$ due to the scale factor
  5652. of four that is applied, and quantization errors could make this even larger.
  5653. Therefore, the coefficients cannot simply be clamped into a valid range before
  5654. the transform.
  5655. \subsubsection{The 1D Inverse DCT}
  5656. \label{sub:1d-idct}
  5657. \paragraph{Input parameters:}\hfill\\*
  5658. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5659. \multicolumn{1}{c}{Name} &
  5660. \multicolumn{1}{c}{Type} &
  5661. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5662. \multicolumn{1}{c}{Signed?} &
  5663. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5664. \bitvar{Y} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5665. 16 & Yes & An 8-element array of DCT
  5666. coefficients. \\
  5667. \bottomrule\end{tabularx}
  5668. \paragraph{Output parameters:}\hfill\\*
  5669. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5670. \multicolumn{1}{c}{Name} &
  5671. \multicolumn{1}{c}{Type} &
  5672. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5673. \multicolumn{1}{c}{Signed?} &
  5674. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5675. \bitvar{X} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5676. 16 & Yes & An 8-element array of output values. \\
  5677. \bottomrule\end{tabularx}
  5678. \paragraph{Variables used:}\hfill\\*
  5679. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5680. \multicolumn{1}{c}{Name} &
  5681. \multicolumn{1}{c}{Type} &
  5682. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5683. \multicolumn{1}{c}{Signed?} &
  5684. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5685. \locvar{T} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5686. 32 & Yes & An 8-element array containing the
  5687. current value of each signal line. \\
  5688. \locvar{R} & Integer & 32 & Yes & A temporary value. \\
  5689. \bottomrule\end{tabularx}
  5690. \medskip
  5691. A compliant decoder MUST use the exact implementation of the inverse DCT
  5692. defined in this specification.
  5693. Some operations may be re-ordered, but the result must be precisely equivalent.
  5694. This is a design decision that limits some avenues of decoder optimization, but
  5695. prevents any drift in the prediction loop.
  5696. Theora uses a 16-bit integerized approximation of of the 8-point 1D inverse DCT
  5697. based on the Chen factorization \cite{CSF77}.
  5698. It requires 16 multiplications and 26 additions and subtractions.
  5699. \begin{figure}[htbp]
  5700. \begin{center}
  5701. \includegraphics[width=\textwidth]{idct}
  5702. \end{center}
  5703. \caption{Signal Flow Graph for the 1D Inverse DCT}
  5704. \label{fig:idct}
  5705. \end{figure}
  5706. A signal flow graph of the transformation is presented in
  5707. Figure~\ref{fig:idct}.
  5708. This graph provides a good visualization of which parts of the transform are
  5709. parallelizable.
  5710. Time increases from left to right.
  5711. Each signal line is involved in an operation where the line is marked with a
  5712. dot $\cdot$ or a circled plus sign $\oplus$.
  5713. The constants $\locvar{C}i$ and $\locvar{S}j$ are the 16-bit integer
  5714. approximations of $\cos(\frac{i\pi}{16})$ and $\sin(\frac{j\pi}{16})$ listed
  5715. in Table~\ref{tab:dct-consts}.
  5716. When they appear next to a signal line, the value on that line is scaled by the
  5717. given constant.
  5718. A circled minus sign $\ominus$ next to a signal line indicates that the value
  5719. on that line is negated.
  5720. Operations on a single signal path through the graph cannot be reordered, but
  5721. operations on different paths may be, or may be executed in parallel.
  5722. Different graphs may be obtainable using the associative, commutative, and
  5723. distributive properties of unsaturated arithmetic.
  5724. The column of numbers on the left represents an initial permutation of the
  5725. input DCT coefficients.
  5726. The column on the right represents the unpermuted output.
  5727. One can be obtained by bit-reversing the 3-bit binary representation of the
  5728. other.
  5729. \begin{table}[htbp]
  5730. \begin{center}
  5731. \begin{tabular}{llr}\toprule
  5732. $\locvar{C}i$ & $\locvar{S}j$ & Value \\\midrule
  5733. $\locvar{C1}$ & $\locvar{S7}$ & $64277$ \\
  5734. $\locvar{C2}$ & $\locvar{S6}$ & $60547$ \\
  5735. $\locvar{C3}$ & $\locvar{S5}$ & $54491$ \\
  5736. $\locvar{C4}$ & $\locvar{S4}$ & $46341$ \\
  5737. $\locvar{C5}$ & $\locvar{S3}$ & $36410$ \\
  5738. $\locvar{C6}$ & $\locvar{S2}$ & $25080$ \\
  5739. $\locvar{C7}$ & $\locvar{S1}$ & $12785$ \\
  5740. \bottomrule\end{tabular}
  5741. \end{center}
  5742. \caption{16-bit Approximations of Sines and Cosines}
  5743. \label{tab:dct-consts}
  5744. \end{table}
  5745. \begin{enumerate}
  5746. \item
  5747. Assign $\locvar{T}[0]$ the value $\bitvar{Y}[0]+\bitvar{Y}[4]$.
  5748. \item
  5749. Truncate $\locvar{T}[0]$ to a 16-bit signed representation by dropping any
  5750. higher-order bits.
  5751. \item
  5752. Assign $\locvar{T}[0]$ the value
  5753. $\locvar{C4}*\locvar{T}[0]>>16$.
  5754. \item
  5755. Assign $\locvar{T}[1]$ the value $\bitvar{Y}[0]-\bitvar{Y}[4]$.
  5756. \item
  5757. Truncate $\locvar{T}[1]$ to a 16-bit signed representation by dropping any
  5758. higher-order bits.
  5759. \item
  5760. Assign $\locvar{T}[1]$ the value $\locvar{C4}*\locvar{T}[1]>>16$.
  5761. \item
  5762. Assign $\locvar{T}[2]$ the value $(\locvar{C6}*\bitvar{Y}[2]>>16)-
  5763. (\locvar{S6}*\bitvar{Y}[6]>>16)$.
  5764. \item
  5765. Assign $\locvar{T}[3]$ the value $(\locvar{S6}*\bitvar{Y}[2]>>16)+
  5766. (\locvar{C6}*\bitvar{Y}[6]>>16)$.
  5767. \item
  5768. Assign $\locvar{T}[4]$ the value $(\locvar{C7}*\bitvar{Y}[1]>>16)-
  5769. (\locvar{S7}*\bitvar{Y}[7]>>16)$.
  5770. \item
  5771. Assign $\locvar{T}[5]$ the value $(\locvar{C3}*\bitvar{Y}[5]>>16)-
  5772. (\locvar{S3}*\bitvar{Y}[3]>>16)$.
  5773. \item
  5774. Assign $\locvar{T}[6]$ the value $(\locvar{S3}*\bitvar{Y}[5]>>16)+
  5775. (\locvar{C3}*\bitvar{Y}[3]>>16)$.
  5776. \item
  5777. Assign $\locvar{T}[7]$ the value $(\locvar{S7}*\bitvar{Y}[1]>>16)+
  5778. (\locvar{C7}*\bitvar{Y}[7]>>16)$.
  5779. \item
  5780. Assign \locvar{R} the value $\locvar{T}[4]+\locvar{T}[5]$.
  5781. \item
  5782. Assign $\locvar{T}[5]$ the value $\locvar{T}[4]-\locvar{T}[5]$.
  5783. \item
  5784. Truncate $\locvar{T}[5]$ to a 16-bit signed representation by dropping any
  5785. higher-order bits.
  5786. \item
  5787. Assign $\locvar{T}[5]$ the value $\locvar{C4}*\locvar{T}[5]>>16$.
  5788. \item
  5789. Assign $\locvar{T}[4]$ the value $\locvar{R}$.
  5790. \item
  5791. Assign \locvar{R} the value $\locvar{T}[7]+\locvar{T}[6]$.
  5792. \item
  5793. Assign $\locvar{T}[6]$ the value $\locvar{T}[7]-\locvar{T}[6]$.
  5794. \item
  5795. Truncate $\locvar{T}[6]$ to a 16-bit signed representation by dropping any
  5796. higher-order bits.
  5797. \item
  5798. Assign $\locvar{T}[6]$ the value $\locvar{C4}*\locvar{T}[6]>>16$.
  5799. \item
  5800. Assign $\locvar{T}[7]$ the value $\locvar{R}$.
  5801. \item
  5802. Assign \locvar{R} the value $\locvar{T}[0]+\locvar{T}[3]$.
  5803. \item
  5804. Assign $\locvar{T}[3]$ the value $\locvar{T}[0]-\locvar{T}[3]$.
  5805. \item
  5806. Assign $\locvar{T}[0]$ the value \locvar{R}.
  5807. \item
  5808. Assign \locvar{R} the value $\locvar{T}[1]+\locvar{T}[2]$
  5809. \item
  5810. Assign $\locvar{T}[2]$ the value $\locvar{T}[1]-\locvar{T}[2]$
  5811. \item
  5812. Assign $\locvar{T}[1]$ the value \locvar{R}.
  5813. \item
  5814. Assign \locvar{R} the value $\locvar{T}[6]+\locvar{T}[5]$.
  5815. \item
  5816. Assign $\locvar{T}[5]$ the value $\locvar{T}[6]-\locvar{T}[5]$.
  5817. \item
  5818. Assign $\locvar{T}[6]$ the value \locvar{R}.
  5819. \item
  5820. Assign \locvar{R} the value $\locvar{T}[0]+\locvar{T}[7]$.
  5821. \item
  5822. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5823. higher-order bits.
  5824. \item
  5825. Assign $\bitvar{X}[0]$ the value \locvar{R}.
  5826. \item
  5827. Assign \locvar{R} the value $\locvar{T}[1]+\locvar{T}[6]$.
  5828. \item
  5829. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5830. higher-order bits.
  5831. \item
  5832. Assign $\bitvar{X}[1]$ the value \locvar{R}.
  5833. \item
  5834. Assign \locvar{R} the value $\locvar{T}[2]+\locvar{T}[5]$.
  5835. \item
  5836. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5837. higher-order bits.
  5838. \item
  5839. Assign $\bitvar{X}[2]$ the value \locvar{R}.
  5840. \item
  5841. Assign \locvar{R} the value $\locvar{T}[3]+\locvar{T}[4]$.
  5842. \item
  5843. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5844. higher-order bits.
  5845. \item
  5846. Assign $\bitvar{X}[3]$ the value \locvar{R}.
  5847. \item
  5848. Assign \locvar{R} the value $\locvar{T}[3]-\locvar{T}[4]$.
  5849. \item
  5850. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5851. higher-order bits.
  5852. \item
  5853. Assign $\bitvar{X}[4]$ the value \locvar{R}.
  5854. \item
  5855. Assign \locvar{R} the value $\locvar{T}[2]-\locvar{T}[5]$.
  5856. \item
  5857. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5858. higher-order bits.
  5859. \item
  5860. Assign $\bitvar{X}[5]$ the value \locvar{R}.
  5861. \item
  5862. Assign \locvar{R} the value $\locvar{T}[1]-\locvar{T}[6]$.
  5863. \item
  5864. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5865. higher-order bits.
  5866. \item
  5867. Assign $\bitvar{X}[6]$ the value \locvar{R}.
  5868. \item
  5869. Assign \locvar{R} the value $\locvar{T}[0]-\locvar{T}[7]$.
  5870. \item
  5871. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5872. higher-order bits.
  5873. \item
  5874. Assign $\bitvar{X}[7]$ the value \locvar{R}.
  5875. \end{enumerate}
  5876. \subsubsection{The 2D Inverse DCT}
  5877. \label{sub:2d-idct}
  5878. \paragraph{Input parameters:}\hfill\\*
  5879. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5880. \multicolumn{1}{c}{Name} &
  5881. \multicolumn{1}{c}{Type} &
  5882. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5883. \multicolumn{1}{c}{Signed?} &
  5884. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5885. \bitvar{DQC} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5886. 14 & Yes & A $64$-element array of dequantized
  5887. DCT coefficients in natural order (cf. Section~\ref{sec:dct-coeffs}). \\
  5888. \bottomrule\end{tabularx}
  5889. \paragraph{Output parameters:}\hfill\\*
  5890. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5891. \multicolumn{1}{c}{Name} &
  5892. \multicolumn{1}{c}{Type} &
  5893. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5894. \multicolumn{1}{c}{Signed?} &
  5895. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5896. \bitvar{RES} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5897. 16 & Yes & An $8\times 8$ array containing the
  5898. decoded residual for the current block. \\
  5899. \bottomrule\end{tabularx}
  5900. \paragraph{Variables used:}\hfill\\*
  5901. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5902. \multicolumn{1}{c}{Name} &
  5903. \multicolumn{1}{c}{Type} &
  5904. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5905. \multicolumn{1}{c}{Signed?} &
  5906. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5907. \locvar{\ci} & Integer & 3 & No & The column index. \\
  5908. \locvar{\ri} & Integer & 3 & No & The row index. \\
  5909. \locvar{Y} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5910. 16 & Yes & An 8-element array of 1D iDCT input
  5911. values. \\
  5912. \locvar{X} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5913. 16 & Yes & An 8-element array of 1D iDCT output
  5914. values. \\
  5915. \bottomrule\end{tabularx}
  5916. \medskip
  5917. This procedure applies the 1D inverse DCT transform 16 times to a block of
  5918. dequantized coefficients: once for each of the 8 rows, and once for each of
  5919. the 8 columns of the result.
  5920. Note that the coordinate system used for the columns is the same right-handed
  5921. coordinate system used by the rest of Theora.
  5922. Thus, the column is indexed from bottom to top, not top to bottom.
  5923. The final values are divided by sixteen, rounding with ties rounded towards
  5924. postive infinity.
  5925. \begin{enumerate}
  5926. \item
  5927. For each value of \locvar{\ri} from 0 to 7:
  5928. \begin{enumerate}
  5929. \item
  5930. For each value of \locvar{\ci} from 0 to 7:
  5931. \begin{enumerate}
  5932. \item
  5933. Assign $\locvar{Y}[\locvar{\ci}]$ the value
  5934. $\bitvar{DQC}[\locvar{\ri}*8+\locvar{\ci}]$.
  5935. \end{enumerate}
  5936. \item
  5937. Compute \locvar{X}, the 1D inverse DCT of \locvar{Y} using the procedure
  5938. described in Section~\ref{sub:1d-idct}.
  5939. \item
  5940. For each value of $\locvar{\ci}$ from 0 to 7:
  5941. \begin{enumerate}
  5942. \item
  5943. Assign $\bitvar{RES}[\locvar{\ri}][\locvar{\ci}]$ the value
  5944. $\locvar{X}[\locvar{\ci}]$.
  5945. \end{enumerate}
  5946. \end{enumerate}
  5947. \item
  5948. For each value of \locvar{\ci} from 0 to 7:
  5949. \begin{enumerate}
  5950. \item
  5951. For each value of \locvar{\ri} from 0 to 7:
  5952. \begin{enumerate}
  5953. \item
  5954. Assign $\locvar{Y}[\locvar{\ri}]$ the value
  5955. $\bitvar{RES}[\locvar{\ri}][\locvar{\ci}]$.
  5956. \end{enumerate}
  5957. \item
  5958. Compute \locvar{X}, the 1D inverse DCT of \locvar{Y} using the procedure
  5959. described in Section~\ref{sub:1d-idct}.
  5960. \item
  5961. For each value of \locvar{\ri} from 0 to 7:
  5962. \begin{enumerate}
  5963. \item
  5964. Assign $\bitvar{RES}[\locvar{\ri}][\locvar{\ci}]$ the value
  5965. $(\locvar{X}[\locvar{\ri}]+8)>>4$.
  5966. \end{enumerate}
  5967. \end{enumerate}
  5968. \end{enumerate}
  5969. \subsubsection{The 1D Forward DCT (Non-Normative)}
  5970. \paragraph{Input parameters:}\hfill\\*
  5971. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5972. \multicolumn{1}{c}{Name} &
  5973. \multicolumn{1}{c}{Type} &
  5974. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5975. \multicolumn{1}{c}{Signed?} &
  5976. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5977. \bitvar{X} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5978. 14 & Yes & An 8-element array of input values. \\
  5979. \bottomrule\end{tabularx}
  5980. \paragraph{Output parameters:}\hfill\\*
  5981. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5982. \multicolumn{1}{c}{Name} &
  5983. \multicolumn{1}{c}{Type} &
  5984. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5985. \multicolumn{1}{c}{Signed?} &
  5986. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5987. \bitvar{Y} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5988. 16 & Yes & An 8-element array of DCT
  5989. coefficients. \\
  5990. \bottomrule\end{tabularx}
  5991. \paragraph{Variables used:}\hfill\\*
  5992. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5993. \multicolumn{1}{c}{Name} &
  5994. \multicolumn{1}{c}{Type} &
  5995. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5996. \multicolumn{1}{c}{Signed?} &
  5997. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5998. \locvar{T} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5999. 16 & Yes & An 8-element array containing the
  6000. current value of each signal line. \\
  6001. \locvar{R} & Integer & 16 & Yes & A temporary value. \\
  6002. \bottomrule\end{tabularx}
  6003. \medskip
  6004. The forward transform used in the encoder is not mandated by this standard as
  6005. the inverse one is.
  6006. Precise equivalence in the inverse transform alone is all that is required to
  6007. guarantee that there is no mismatch in the prediction loop between encoder and
  6008. any compliant decoder implementation.
  6009. However, a forward transform is provided here as a convenience for implementing
  6010. an encoder.
  6011. This is the version of the transform used by Xiph.org's Theora encoder, which
  6012. is the same as that used by VP3.
  6013. Like the inverse DCT, it is first applied to each row, and then applied to each
  6014. column of the result.
  6015. \begin{figure}[htbp]
  6016. \begin{center}
  6017. \includegraphics[width=\textwidth]{fdct}
  6018. \end{center}
  6019. \caption{Signal Flow Graph for the 1D Forward DCT}
  6020. \label{fig:fdct}
  6021. \end{figure}
  6022. The signal flow graph for the forward transform is given in
  6023. Figure~\ref{fig:fdct}.
  6024. It is largely the reverse of the flow graph given for the inverse DCT.
  6025. It is important to note that the signs on the constants in the rotations have
  6026. changed, and the \locvar{C4} scale factors on one of the lower butterflies now
  6027. appear on the opposite side.
  6028. The column of numbers on the left represents the unpermuted input, and the
  6029. column on the right the permuted output DCT coefficients.
  6030. A proper division by $2^{16}$ is done after the multiplications instead of a
  6031. shift in the forward transform.
  6032. This can be implemented quickly by adding an offset of $\hex{FFFF}$ if the
  6033. number is negative, and then shifting as before.
  6034. This slightly increases the computational complexity of the transform.
  6035. Unlike the inverse DCT, 16-bit registers and a $16\times16\rightarrow32$ bit
  6036. multiply are sufficient to avoid any overflow, so long as the input is in the
  6037. range $-6270\ldots 6270$, which is larger than required.
  6038. \begin{enumerate}
  6039. \item
  6040. Assign $\locvar{T}[0]$ the value $\bitvar{X}[0]+\bitvar{X}[7]$.
  6041. \item
  6042. Assign $\locvar{T}[1]$ the value $\bitvar{X}[1]+\bitvar{X}[6]$.
  6043. \item
  6044. Assign $\locvar{T}[2]$ the value $\bitvar{X}[2]+\bitvar{X}[5]$.
  6045. \item
  6046. Assign $\locvar{T}[3]$ the value $\bitvar{X}[3]+\bitvar{X}[4]$.
  6047. \item
  6048. Assign $\locvar{T}[4]$ the value $\bitvar{X}[3]-\bitvar{X}[4]$.
  6049. \item
  6050. Assign $\locvar{T}[5]$ the value $\bitvar{X}[2]-\bitvar{X}[5]$.
  6051. \item
  6052. Assign $\locvar{T}[6]$ the value $\bitvar{X}[1]-\bitvar{X}[6]$.
  6053. \item
  6054. Assign $\locvar{T}[7]$ the value $\bitvar{X}[0]-\bitvar{X}[7]$.
  6055. \item
  6056. Assign \locvar{R} the value $\locvar{T}[0]+\locvar{T}[3]$.
  6057. \item
  6058. Assign $\locvar{T}[3]$ the value $\locvar{T}[0]-\locvar{T}[3]$.
  6059. \item
  6060. Assign $\locvar{T}[0]$ the value \locvar{R}.
  6061. \item
  6062. Assign \locvar{R} the value $\locvar{T}[1]+\locvar{T}[2]$.
  6063. \item
  6064. Assign $\locvar{T}[2]$ the value $\locvar{T}[1]-\locvar{T}[2]$.
  6065. \item
  6066. Assign $\locvar{T}[1]$ the value \locvar{R}.
  6067. \item
  6068. Assign \locvar{R} the value $\locvar{T}[6]-\locvar{T}[5]$.
  6069. \item
  6070. Assign $\locvar{T}[6]$ the value
  6071. $(\locvar{C4}*(\locvar{T}[6]+\locvar{T}[5]))//16$.
  6072. \item
  6073. Assign $\locvar{T}[5]$ the value $(\locvar{C4}*\locvar{R})//16$.
  6074. \item
  6075. Assign \locvar{R} the value $\locvar{T}[4]+\locvar{T}[5]$.
  6076. \item
  6077. Assign $\locvar{T}[5]$ the value $\locvar{T}[4]-\locvar{T}[5]$.
  6078. \item
  6079. Assign $\locvar{T}[4]$ the value \locvar{R}.
  6080. \item
  6081. Assign \locvar{R} the value $\locvar{T}[7]+\locvar{T}[6]$.
  6082. \item
  6083. Assign $\locvar{T}[6]$ the value $\locvar{T}[7]-\locvar{T}[6]$.
  6084. \item
  6085. Assign $\locvar{T}[7]$ the value \locvar{R}.
  6086. \item
  6087. Assign $\bitvar{Y}[0]$ the value
  6088. $(\locvar{C4}*(\locvar{T}[0]+\locvar{T}[1]))//16$.
  6089. \item
  6090. Assign $\bitvar{Y}[4]$ the value
  6091. $(\locvar{C4}*(\locvar{T}[0]-\locvar{T}[1]))//16$.
  6092. \item
  6093. Assign $\bitvar{Y}[2]$ the value
  6094. $((\locvar{S6}*\locvar{T}[3])//16)+
  6095. ((\locvar{C6}*\locvar{T}[2])//16)$.
  6096. \item
  6097. Assign $\bitvar{Y}[6]$ the value
  6098. $((\locvar{C6}*\locvar{T}[3])//16)-
  6099. ((\locvar{S6}*\locvar{T}[2])//16)$.
  6100. \item
  6101. Assign $\bitvar{Y}[1]$ the value
  6102. $((\locvar{S7}*\locvar{T}[7])//16)+
  6103. ((\locvar{C7}*\locvar{T}[4])//16)$.
  6104. \item
  6105. Assign $\bitvar{Y}[5]$ the value
  6106. $((\locvar{S3}*\locvar{T}[6])//16)+
  6107. ((\locvar{C3}*\locvar{T}[5])//16)$.
  6108. \item
  6109. Assign $\bitvar{Y}[3]$ the value
  6110. $((\locvar{C3}*\locvar{T}[6])//16)-
  6111. ((\locvar{S3}*\locvar{T}[5])//16)$.
  6112. \item
  6113. Assign $\bitvar{Y}[7]$ the value
  6114. $((\locvar{C7}*\locvar{T}[7])//16)-
  6115. ((\locvar{S7}*\locvar{T}[4])//16)$.
  6116. \end{enumerate}
  6117. \subsection{The Complete Reconstruction Algorithm}
  6118. \label{sub:recon}
  6119. \paragraph{Input parameters:}\hfill\\*
  6120. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6121. \multicolumn{1}{c}{Name} &
  6122. \multicolumn{1}{c}{Type} &
  6123. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6124. \multicolumn{1}{c}{Signed?} &
  6125. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6126. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  6127. 16 & No & A 64-element array of scale values
  6128. for AC coefficients for each \qi\ value. \\
  6129. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  6130. 16 & No & A 64-element array of scale values
  6131. for the DC coefficient for each \qi\ value. \\
  6132. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  6133. 8 & No & A $\bitvar{NBMS}\times 64$ array
  6134. containing the base matrices. \\
  6135. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  6136. 6 & No & A $2\times 3$ array containing the
  6137. number of quant ranges for a given \qti\ and \pli, respectively.
  6138. This is at most $63$. \\
  6139. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  6140. 6 & No & A $2\times 3\times 63$ array of the
  6141. sizes of each quant range for a given \qti\ and \pli, respectively.
  6142. Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
  6143. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  6144. 9 & No & A $2\times 3\times 64$ array of the
  6145. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  6146. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
  6147. \bitvar{RPYW} & Integer & 20 & No & The width of the $Y'$ plane of the
  6148. reference frames in pixels. \\
  6149. \bitvar{RPYH} & Integer & 20 & No & The height of the $Y'$ plane of the
  6150. reference frames in pixels. \\
  6151. \bitvar{RPCW} & Integer & 20 & No & The width of the $C_b$ and $C_r$
  6152. planes of the reference frames in pixels. \\
  6153. \bitvar{RPCH} & Integer & 20 & No & The height of the $C_b$ and $C_r$
  6154. planes of the reference frames in pixels. \\
  6155. \bitvar{GOLDREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6156. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6157. array containing the contents of the $Y'$ plane of the golden reference
  6158. frame. \\
  6159. \bitvar{GOLDREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6160. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6161. array containing the contents of the $C_b$ plane of the golden reference
  6162. frame. \\
  6163. \bitvar{GOLDREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6164. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6165. array containing the contents of the $C_r$ plane of the golden reference
  6166. frame. \\
  6167. \bitvar{PREVREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6168. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6169. array containing the contents of the $Y'$ plane of the previous reference
  6170. frame. \\
  6171. \bitvar{PREVREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6172. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6173. array containing the contents of the $C_b$ plane of the previous reference
  6174. frame. \\
  6175. \bitvar{PREVREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6176. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6177. array containing the contents of the $C_r$ plane of the previous reference
  6178. frame. \\
  6179. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  6180. frame. \\
  6181. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6182. 1 & No & An \bitvar{NBS}-element array of
  6183. flags indicating which blocks are coded. \\
  6184. \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6185. 3 & No & An \bitvar{NMBS}-element array of
  6186. coding modes for each macro block. \\
  6187. \bitvar{MVECTS} & \multicolumn{1}{p{50pt}}{Array of 2D Integer Vectors} &
  6188. 6 & Yes & An \bitvar{NBS}-element array of
  6189. motion vectors for each block. \\
  6190. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6191. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  6192. quantized DCT coefficient values for each block in zig-zag order. \\
  6193. \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6194. 7 & No & An \bitvar{NBS}-element array of the
  6195. coefficient count for each block. \\
  6196. \bitvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
  6197. 6 & No & An \bitvar{NQIS}-element array of
  6198. \qi\ values. \\
  6199. \bitvar{QIIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6200. 2 & No & An \bitvar{NBS}-element array of
  6201. \locvar{\qii} values for each block. \\
  6202. \bottomrule\end{tabularx}
  6203. \paragraph{Output parameters:}\hfill\\*
  6204. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6205. \multicolumn{1}{c}{Name} &
  6206. \multicolumn{1}{c}{Type} &
  6207. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6208. \multicolumn{1}{c}{Signed?} &
  6209. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6210. \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6211. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6212. array containing the contents of the $Y'$ plane of the reconstructed frame. \\
  6213. \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6214. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6215. array containing the contents of the $C_b$ plane of the reconstructed frame. \\
  6216. \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6217. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6218. array containing the contents of the $C_r$ plane of the reconstructed frame. \\
  6219. \bottomrule\end{tabularx}
  6220. \paragraph{Variables used:}\hfill\\*
  6221. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6222. \multicolumn{1}{c}{Name} &
  6223. \multicolumn{1}{c}{Type} &
  6224. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6225. \multicolumn{1}{c}{Signed?} &
  6226. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6227. \locvar{RPW} & Integer & 20 & No & The width of the current plane of the
  6228. current reference frame in pixels. \\
  6229. \locvar{RPH} & Integer & 20 & No & The height of the current plane of
  6230. the current reference frame in pixels. \\
  6231. \locvar{REFP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6232. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6233. array containing the contents of the current plane of the current reference
  6234. frame. \\
  6235. \locvar{BX} & Integer & 20 & No & The horizontal pixel index of the
  6236. lower-left corner of the current block. \\
  6237. \locvar{BY} & Integer & 20 & No & The vertical pixel index of the
  6238. lower-left corner of the current block. \\
  6239. \locvar{MVX} & Integer & 5 & No & The horizontal component of the first
  6240. whole-pixel motion vector. \\
  6241. \locvar{MVY} & Integer & 5 & No & The vertical component of the first
  6242. whole-pixel motion vector. \\
  6243. \locvar{MVX2} & Integer & 5 & No & The horizontal component of the second
  6244. whole-pixel motion vector. \\
  6245. \locvar{MVY2} & Integer & 5 & No & The vertical component of the second
  6246. whole-pixel motion vector. \\
  6247. \locvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6248. 8 & No & An $8\times 8$ array of predictor
  6249. values to use for the current block. \\
  6250. \locvar{RES} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6251. 16 & Yes & An $8\times 8$ array containing the
  6252. decoded residual for the current block. \\
  6253. \locvar{QMAT} & \multicolumn{1}{p{40pt}}{Integer array} &
  6254. 16 & No & A 64-element array of quantization
  6255. values for each DCT coefficient in natural order. \\
  6256. \locvar{DC} & Integer & 29 & Yes & The dequantized DC coefficient of a
  6257. block. \\
  6258. \locvar{P} & Integer & 17 & Yes & A reconstructed pixel value. \\
  6259. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  6260. coded order. \\
  6261. \locvar{\mbi} & Integer & 32 & No & The index of the macro block
  6262. containing block \locvar{\bi}. \\
  6263. \locvar{\pli} & Integer & 2 & No & The color plane index of the current
  6264. block. \\
  6265. \locvar{\rfi} & Integer & 2 & No & The index of the reference frame
  6266. indicated by the coding mode for macro block \locvar{\mbi}. \\
  6267. \locvar{\idx{bx}} & Integer & 3 & No & The horizontal pixel index in the
  6268. block. \\
  6269. \locvar{\idx{by}} & Integer & 3 & No & The vertical pixel index in the
  6270. block. \\
  6271. \locvar{\qti} & Integer & 1 & No & A quantization type index.
  6272. See Table~\ref{tab:quant-types}.\\
  6273. \locvar{\idx{qi0}} & Integer & 6 & No & The quantization index of the DC
  6274. coefficient. \\
  6275. \locvar{\qi} & Integer & 6 & No & The quantization index of the AC
  6276. coefficients. \\
  6277. \bottomrule\end{tabularx}
  6278. \medskip
  6279. This section takes the decoded packet data and uses the previously defined
  6280. procedures to reconstruct each block of the current frame.
  6281. For coded blocks, a predictor is formed using the coding mode and, if
  6282. applicable, the motion vector, and then the residual is computed from the
  6283. quantized DCT coefficients.
  6284. For uncoded blocks, the contents of the co-located block are copied from the
  6285. previous frame and the residual is cleared to zero.
  6286. Then the predictor and residual are added, and the result clamped to the range
  6287. $0\ldots 255$ and stored in the current frame.
  6288. In the special case that a block contains only a DC coefficient, the
  6289. dequantization and inverse DCT transform is skipped.
  6290. Instead the constant pixel value for the entire block is computed in one step.
  6291. Note that the truncation of intermediate operations is omitted and the final
  6292. rounding is slightly different in this case.
  6293. The check for whether or not the block contains only a DC coefficient is based
  6294. on the coefficient count returned from the token decode procedure of
  6295. Section~\ref{sec:dct-decode}, and not by checking to see if the remaining
  6296. coefficient values are zero.
  6297. Also note that even when the coefficient count indicates the block contains
  6298. zero coefficients, the DC coefficient is still processed, as undoing DC
  6299. prediction might have made it non-zero.
  6300. After this procedure, the frame is completely reconstructed, but before it can
  6301. be used as a reference frame, a loop filter must be run over it to help reduce
  6302. blocking artifacts.
  6303. This is detailed in Section~\ref{sec:loopfilter}.
  6304. \begin{enumerate}
  6305. \item
  6306. Assign \locvar{\idx{qi0}} the value $\bitvar{QIS}[0]$.
  6307. \item
  6308. For each value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$:
  6309. \begin{enumerate}
  6310. \item
  6311. Assign \locvar{\pli} the index of the color plane block \locvar{\bi} belongs
  6312. to.
  6313. \item
  6314. Assign \locvar{BX} the horizontal pixel index of the lower-left corner of block
  6315. \locvar{\bi}.
  6316. \item
  6317. Assign \locvar{BY} the vertical pixel index of the lower-left corner of block
  6318. \locvar{\bi}.
  6319. \item
  6320. If $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero:
  6321. \begin{enumerate}
  6322. \item
  6323. Assign \locvar{\mbi} the index of the macro block containing block
  6324. \locvar{\bi}.
  6325. \item
  6326. If $\bitvar{MBMODES}[\locvar{\mbi}]$ is 1 (INTRA), assign \locvar{\qti} the
  6327. value $0$.
  6328. \item
  6329. Otherwise, assign \locvar{\qti} the value $1$.
  6330. \item
  6331. Assign \locvar{\rfi} the value of the Reference Frame Index column of
  6332. Table~\ref{tab:cm-refs} corresponding to $\bitvar{MBMODES}[\locvar{\mbi}]$.
  6333. \item
  6334. If \locvar{\rfi} is zero, compute \locvar{PRED} using the procedure given in
  6335. Section~\ref{sub:predintra}.
  6336. \item
  6337. Otherwise:
  6338. \begin{enumerate}
  6339. \item
  6340. Assign \locvar{REFP}, \locvar{RPW}, and \locvar{RPH} the values given in
  6341. Table~\ref{tab:refp} corresponding to current value of \locvar{\rfi} and
  6342. \locvar{\pli}.
  6343. \begin{table}[htbp]
  6344. \begin{center}
  6345. \begin{tabular}{cclll}\toprule
  6346. \locvar{\rfi} & \locvar{\pli} &
  6347. \locvar{REFP} & \locvar{RPW} & \locvar{RPH} \\\midrule
  6348. $1$ & $0$ & \bitvar{PREVREFY} & \bitvar{RPYW} & \bitvar{RPYH} \\
  6349. $1$ & $1$ & \bitvar{PREVREFCB} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6350. $1$ & $2$ & \bitvar{PREVREFCR} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6351. $2$ & $0$ & \bitvar{GOLDREFY} & \bitvar{RPYW} & \bitvar{RPYH} \\
  6352. $2$ & $1$ & \bitvar{GOLDREFCB} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6353. $2$ & $2$ & \bitvar{GOLDREFCR} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6354. \bottomrule\end{tabular}
  6355. \end{center}
  6356. \caption{Reference Planes and Sizes for Each \locvar{\rfi} and \locvar{\pli}}
  6357. \label{tab:refp}
  6358. \end{table}
  6359. \item
  6360. Assign \locvar{MVX} the value
  6361. \begin{equation*}
  6362. \left\lfloor\lvert\bitvar{MVECTS}[\locvar{\bi}]_x\rvert\right\rfloor*
  6363. \sign(\bitvar{MVECTS}[\locvar{\bi}]_x).
  6364. \end{equation*}
  6365. \item
  6366. Assign \locvar{MVY} the value
  6367. \begin{equation*}
  6368. \left\lfloor\lvert\bitvar{MVECTS}[\locvar{\bi}]_y\rvert\right\rfloor*
  6369. \sign(\bitvar{MVECTS}[\locvar{\bi}]_y).
  6370. \end{equation*}
  6371. \item
  6372. Assign \locvar{MVX2} the value
  6373. \begin{equation*}
  6374. \left\lceil\lvert\bitvar{MVECTS}[\locvar{\bi}]_x\rvert\right\rceil*
  6375. \sign(\bitvar{MVECTS}[\locvar{\bi}]_x).
  6376. \end{equation*}
  6377. \item
  6378. Assign \locvar{MVY2} the value
  6379. \begin{equation*}
  6380. \left\lceil\lvert\bitvar{MVECTS}[\locvar{\bi}]_y\rvert\right\rceil*
  6381. \sign(\bitvar{MVECTS}[\locvar{\bi}]_y).
  6382. \end{equation*}
  6383. \item
  6384. If \locvar{MVX} equals \locvar{MVX2} and \locvar{MVY} equals \locvar{MVY2},
  6385. use the values \locvar{REFP}, \locvar{RPW}, \locvar{RPH}, \locvar{BX},
  6386. \locvar{BY}, \locvar{MVX}, and \locvar{MVY}, compute \locvar{PRED} using the
  6387. procedure given in Section~\ref{sub:predfullpel}.
  6388. \item
  6389. Otherwise, use the values \locvar{REFP}, \locvar{RPW}, \locvar{RPH},
  6390. \locvar{BX}, \locvar{BY}, \locvar{MVX}, \locvar{MVY}, \locvar{MVX2}, and
  6391. \locvar{MVY2} to compute \locvar{PRED} using the procedure given in
  6392. Section~\ref{sub:predhalfpel}.
  6393. \end{enumerate}
  6394. \item
  6395. If $\bitvar{NCOEFFS}[\locvar{\bi}]$ is less than 2:
  6396. \begin{enumerate}
  6397. \item
  6398. Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS}, \\
  6399. \bitvar{QRSIZES}, \bitvar{QRBMIS}, \locvar{\qti}, \locvar{\pli}, and
  6400. \locvar{\idx{qi0}}, use the procedure given in Section~\ref{sub:quant-mat} to
  6401. compute the DC quantization matrix \locvar{QMAT}.
  6402. \item
  6403. Assign \locvar{DC} the value
  6404. \begin{equation*}
  6405. (\bitvar{COEFFS}[\bitvar{\bi}][0]*\locvar{QMAT}[0]+15)>>5.
  6406. \end{equation*}
  6407. \item
  6408. Truncate \locvar{DC} to a 16-bit signed representation by dropping any
  6409. higher-order bits.
  6410. \item
  6411. For each value of \locvar{\idx{by}} from 0 to 7, and each value of
  6412. \locvar{\idx{bx}} from 0 to 7, assign
  6413. $\locvar{RES}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value \locvar{DC}.
  6414. \end{enumerate}
  6415. \item
  6416. Otherwise:
  6417. \begin{enumerate}
  6418. \item
  6419. Assign \locvar{\qi} the value $\bitvar{QIS}[\bitvar{QIIS}[\locvar{\bi}]]$.
  6420. \item
  6421. Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS}, \\
  6422. \bitvar{QRSIZES}, \bitvar{QRBMIS}, \locvar{\qti}, \locvar{\pli},
  6423. \locvar{\idx{qi0}}, and \locvar{\qi}, compute \locvar{DQC} using the procedure
  6424. given in Section~\ref{sub:dequant}.
  6425. \item
  6426. Using \locvar{DQC}, compute \locvar{RES} using the procedure given in
  6427. Section~\ref{sub:2d-idct}.
  6428. \end{enumerate}
  6429. \end{enumerate}
  6430. \item
  6431. Otherwise:
  6432. \begin{enumerate}
  6433. \item
  6434. Assign \locvar{\rfi} the value 1.
  6435. \item
  6436. Assign \locvar{REFP}, \locvar{RPW}, and \locvar{RPH} the values given in
  6437. Table~\ref{tab:refp} corresponding to current value of \locvar{\rfi} and
  6438. \locvar{\pli}.
  6439. \item
  6440. Assign \locvar{MVX} the value 0.
  6441. \item
  6442. Assign \locvar{MVY} the value 0.
  6443. \item
  6444. Using the values \locvar{REFP}, \locvar{RPW}, \locvar{RPH}, \locvar{BX},
  6445. \locvar{BY}, \locvar{MVX}, and \locvar{MVY}, compute \locvar{PRED} using the
  6446. procedure given in Section~\ref{sub:predfullpel}.
  6447. This is simply a copy of the co-located block in the previous reference frame.
  6448. \item
  6449. For each value of \locvar{\idx{by}} from 0 to 7, and each value of
  6450. \locvar{\idx{bx}} from 0 to 7, assign
  6451. $\locvar{RES}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value 0.
  6452. \end{enumerate}
  6453. \item
  6454. For each value of \locvar{\idx{by}} from 0 to 7, and each value of
  6455. \locvar{\idx{bx}} from 0 to 7:
  6456. \begin{enumerate}
  6457. \item
  6458. Assign \locvar{P} the value
  6459. $(\locvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]+
  6460. \locvar{RES}[\locvar{\idx{by}}][\locvar{\idx{bx}}])$.
  6461. \item
  6462. If \locvar{P} is greater than $255$, assign \locvar{P} the value $255$.
  6463. \item
  6464. If \locvar{P} is less than $0$, assign \locvar{P} the value $0$.
  6465. \item
  6466. If \locvar{\pli} equals 0, assign
  6467. $\bitvar{RECY}[\locvar{BY}+\locvar{\idx{by}}][\locvar{BX}+\locvar{\idx{bx}}]$
  6468. the value \locvar{P}.
  6469. \item
  6470. Otherwise, if \locvar{\pli} equals 1, assign
  6471. $\bitvar{RECB}[\locvar{BY}+\locvar{\idx{by}}][\locvar{BX}+\locvar{\idx{bx}}]$
  6472. the value \locvar{P}.
  6473. \item
  6474. Otherwise, \locvar{\pli} equals 2, so assign
  6475. $\bitvar{RECR}[\locvar{BY}+\locvar{\idx{by}}][\locvar{BX}+\locvar{\idx{bx}}]$
  6476. the value \locvar{P}.
  6477. \end{enumerate}
  6478. \end{enumerate}
  6479. \end{enumerate}
  6480. \section{Loop Filtering}
  6481. \label{sec:loopfilter}
  6482. \begin{figure}[htbp]
  6483. \begin{center}
  6484. \includegraphics{lflim}
  6485. \end{center}
  6486. \caption{The loop filter response function.}
  6487. \label{fig:lflim}
  6488. \end{figure}
  6489. The loop filter is a simple deblocking filter that is based on running a small
  6490. edge detecting filter over the coded block edges and adjusting the pixel
  6491. values by a tapered response.
  6492. The filter response is modulated by the following non-linear function:
  6493. \begin{align*}
  6494. \lflim(\locvar{R},\bitvar{L})&=\left\{\begin{array}{ll}
  6495. 0, & \locvar{R}\le-2*\bitvar{L} \\
  6496. -\locvar{R}-2*\bitvar{L}, & -2*\bitvar{L}<\locvar{R}\le-\bitvar{L} \\
  6497. \locvar{R}, & -\bitvar{L}<\locvar{R}<\bitvar{L} \\
  6498. -\locvar{R}+2*\bitvar{L}, & \bitvar{L}\le\locvar{R}<2*\bitvar{L} \\
  6499. 0, & 2*\bitvar{L}\le\locvar{R}
  6500. \end{array}\right.
  6501. \end{align*}
  6502. Here \bitvar{L} is a limiting value equal to $\bitvar{LFLIMS}[\idx{qi0}]$.
  6503. It defines the peaks of the function, illustrated in Figure~\ref{fig:lflim}.
  6504. \bitvar{LFLIMS} is an array of values specified in the setup header and is
  6505. indexed by \idx{qi0}, the first quantization index for the frame, the one used
  6506. for all the DC coefficients.
  6507. Larger values of \bitvar{L} indicate a stronger filter.
  6508. \subsection{Horizontal Filter}
  6509. \label{sub:filth}
  6510. \paragraph{Input parameters:}\hfill\\*
  6511. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6512. \multicolumn{1}{c}{Name} &
  6513. \multicolumn{1}{c}{Type} &
  6514. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6515. \multicolumn{1}{c}{Signed?} &
  6516. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6517. \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6518. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6519. array containing the contents of a plane of the reconstructed frame. \\
  6520. \bitvar{FX} & Integer & 20 & No & The horizontal pixel index of the
  6521. lower-left corner of the area to be filtered. \\
  6522. \bitvar{FY} & Integer & 20 & No & The vertical pixel index of the
  6523. lower-left corner of the area to be filtered. \\
  6524. \bitvar{L} & Integer & 7 & No & The loop filter limit value. \\
  6525. \bottomrule\end{tabularx}
  6526. \paragraph{Output parameters:}\hfill\\*
  6527. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6528. \multicolumn{1}{c}{Name} &
  6529. \multicolumn{1}{c}{Type} &
  6530. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6531. \multicolumn{1}{c}{Signed?} &
  6532. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6533. \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6534. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6535. array containing the contents of a plane of the reconstructed frame. \\
  6536. \bottomrule\end{tabularx}
  6537. \paragraph{Variables used:}\hfill\\*
  6538. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6539. \multicolumn{1}{c}{Name} &
  6540. \multicolumn{1}{c}{Type} &
  6541. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6542. \multicolumn{1}{c}{Signed?} &
  6543. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6544. \locvar{R} & Integer & 9 & Yes & The edge detector response. \\
  6545. \locvar{P} & Integer & 9 & Yes & A filtered pixel value. \\
  6546. \locvar{\idx{by}} & Integer & 20 & No & The vertical pixel index in the
  6547. block. \\
  6548. \bottomrule\end{tabularx}
  6549. \medskip
  6550. This procedure applies a $4$-tap horizontal filter to each row of a vertical
  6551. block edge.
  6552. \begin{enumerate}
  6553. \item
  6554. For each value of \locvar{\idx{by}} from $0$ to $7$:
  6555. \begin{enumerate}
  6556. \item
  6557. Assign \locvar{R} the value
  6558. \begin{multline*}
  6559. (\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}]-
  6560. 3*\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]+\\
  6561. 3*\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]-
  6562. \bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+3]+4)>>3
  6563. \end{multline*}
  6564. \item
  6565. Assign \locvar{P} the value
  6566. $(\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]+
  6567. \lflim(\locvar{R},\bitvar{L}))$.
  6568. \item
  6569. If \locvar{P} is less than zero, assign
  6570. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]$ the value zero.
  6571. \item
  6572. Otherwise, if \locvar{P} is greater than $255$, assign
  6573. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]$ the value $255$.
  6574. \item
  6575. Otherwise, assign
  6576. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]$ the value
  6577. \locvar{P}.
  6578. \item
  6579. Assign \locvar{P} the value
  6580. $(\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]-
  6581. \lflim(\locvar{R},\bitvar{L}))$.
  6582. \item
  6583. If \locvar{P} is less than zero, assign
  6584. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]$ the value zero.
  6585. \item
  6586. Otherwise, if \locvar{P} is greater than $255$, assign
  6587. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]$ the value $255$.
  6588. \item
  6589. Otherwise, assign
  6590. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]$ the value
  6591. \locvar{P}.
  6592. \end{enumerate}
  6593. \end{enumerate}
  6594. \subsection{Vertical Filter}
  6595. \label{sub:filtv}
  6596. \paragraph{Input parameters:}\hfill\\*
  6597. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6598. \multicolumn{1}{c}{Name} &
  6599. \multicolumn{1}{c}{Type} &
  6600. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6601. \multicolumn{1}{c}{Signed?} &
  6602. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6603. \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6604. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6605. array containing the contents of a plane of the reconstructed frame. \\
  6606. \bitvar{FX} & Integer & 20 & No & The horizontal pixel index of the
  6607. lower-left corner of the area to be filtered. \\
  6608. \bitvar{FY} & Integer & 20 & No & The vertical pixel index of the
  6609. lower-left corner of the area to be filtered. \\
  6610. \bitvar{L} & Integer & 7 & No & The loop filter limit value. \\
  6611. \bottomrule\end{tabularx}
  6612. \paragraph{Output parameters:}\hfill\\*
  6613. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6614. \multicolumn{1}{c}{Name} &
  6615. \multicolumn{1}{c}{Type} &
  6616. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6617. \multicolumn{1}{c}{Signed?} &
  6618. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6619. \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6620. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6621. array containing the contents of a plane of the reconstructed frame. \\
  6622. \bottomrule\end{tabularx}
  6623. \paragraph{Variables used:}\hfill\\*
  6624. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6625. \multicolumn{1}{c}{Name} &
  6626. \multicolumn{1}{c}{Type} &
  6627. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6628. \multicolumn{1}{c}{Signed?} &
  6629. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6630. \locvar{R} & Integer & 9 & Yes & The edge detector response. \\
  6631. \locvar{P} & Integer & 9 & Yes & A filtered pixel value. \\
  6632. \locvar{\idx{bx}} & Integer & 20 & No & The horizontal pixel index in the
  6633. block. \\
  6634. \bottomrule\end{tabularx}
  6635. \medskip
  6636. This procedure applies a $4$-tap vertical filter to each column of a horizontal
  6637. block edge.
  6638. \begin{enumerate}
  6639. \item
  6640. For each value of \locvar{\idx{bx}} from $0$ to $7$:
  6641. \begin{enumerate}
  6642. \item
  6643. Assign \locvar{R} the value
  6644. \begin{multline*}
  6645. (\bitvar{RECP}[\bitvar{FY}][\bitvar{FX}+\locvar{\idx{bx}}]-
  6646. 3*\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]+\\
  6647. 3*\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]-
  6648. \bitvar{RECP}[\bitvar{FY}+3][\bitvar{FX}+\locvar{\idx{bx}}]+4)>>3
  6649. \end{multline*}
  6650. \item
  6651. Assign \locvar{P} the value
  6652. $(\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]+
  6653. \lflim(\locvar{R},\bitvar{L}))$.
  6654. \item
  6655. If \locvar{P} is less than zero, assign
  6656. $\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]$ the value zero.
  6657. \item
  6658. Otherwise, if \locvar{P} is greater than $255$, assign
  6659. $\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]$ the value $255$.
  6660. \item
  6661. Otherwise, assign
  6662. $\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]$ the value
  6663. \locvar{P}.
  6664. \item
  6665. Assign \locvar{P} the value
  6666. $(\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]-
  6667. \lflim(\locvar{R},\bitvar{L}))$.
  6668. \item
  6669. If \locvar{P} is less than zero, assign
  6670. $\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]$ the value zero.
  6671. \item
  6672. Otherwise, if \locvar{P} is greater than $255$, assign
  6673. $\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]$ the value $255$.
  6674. \item
  6675. Otherwise, assign
  6676. $\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]$ the value
  6677. \locvar{P}.
  6678. \end{enumerate}
  6679. \end{enumerate}
  6680. \subsection{Complete Loop Filter}
  6681. \label{sub:loop-filt}
  6682. \paragraph{Input parameters:}\hfill\\*
  6683. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6684. \multicolumn{1}{c}{Name} &
  6685. \multicolumn{1}{c}{Type} &
  6686. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6687. \multicolumn{1}{c}{Signed?} &
  6688. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6689. \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
  6690. 7 & No & A 64-element array of loop filter limit
  6691. values. \\
  6692. \bitvar{RPYW} & Integer & 20 & No & The width of the $Y'$ plane of the
  6693. reconstruced frame in pixels. \\
  6694. \bitvar{RPYH} & Integer & 20 & No & The height of the $Y'$ plane of the
  6695. reconstruced frame in pixels. \\
  6696. \bitvar{RPCW} & Integer & 20 & No & The width of the $C_b$ and $C_r$
  6697. planes of the reconstruced frame in pixels. \\
  6698. \bitvar{RPCH} & Integer & 20 & No & The height of the $C_b$ and $C_r$
  6699. planes of the reconstruced frame in pixels. \\
  6700. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  6701. frame. \\
  6702. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6703. 1 & No & An \bitvar{NBS}-element array of
  6704. flags indicating which blocks are coded. \\
  6705. \bitvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
  6706. 6 & No & An \bitvar{NQIS}-element array of
  6707. \qi\ values. \\
  6708. \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6709. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6710. array containing the contents of the $Y'$ plane of the reconstructed frame. \\
  6711. \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6712. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6713. array containing the contents of the $C_b$ plane of the reconstructed frame. \\
  6714. \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6715. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6716. array containing the contents of the $C_r$ plane of the reconstructed frame. \\
  6717. \bottomrule\end{tabularx}
  6718. \paragraph{Output parameters:}\hfill\\*
  6719. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6720. \multicolumn{1}{c}{Name} &
  6721. \multicolumn{1}{c}{Type} &
  6722. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6723. \multicolumn{1}{c}{Signed?} &
  6724. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6725. \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6726. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6727. array containing the contents of the $Y'$ plane of the reconstructed frame. \\
  6728. \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6729. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6730. array containing the contents of the $C_b$ plane of the reconstructed frame. \\
  6731. \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6732. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6733. array containing the contents of the $C_r$ plane of the reconstructed frame. \\
  6734. \bottomrule\end{tabularx}
  6735. \paragraph{Variables used:}\hfill\\*
  6736. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6737. \multicolumn{1}{c}{Name} &
  6738. \multicolumn{1}{c}{Type} &
  6739. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6740. \multicolumn{1}{c}{Signed?} &
  6741. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6742. \locvar{RPW} & Integer & 20 & No & The width of the current plane of the
  6743. reconstructed frame in pixels. \\
  6744. \locvar{RPH} & Integer & 20 & No & The height of the current plane of
  6745. the reconstructed frame in pixels. \\
  6746. \locvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6747. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6748. array containing the contents of the current plane of the reconstruced
  6749. frame. \\
  6750. \locvar{BX} & Integer & 20 & No & The horizontal pixel index of the
  6751. lower-left corner of the current block. \\
  6752. \locvar{BY} & Integer & 20 & No & The vertical pixel index of the
  6753. lower-left corner of the current block. \\
  6754. \locvar{FX} & Integer & 20 & No & The horizontal pixel index of the
  6755. lower-left corner of the area to be filtered. \\
  6756. \locvar{FY} & Integer & 20 & No & The vertical pixel index of the
  6757. lower-left corner of the area to be filtered. \\
  6758. \locvar{L} & Integer & 7 & No & The loop filter limit value. \\
  6759. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  6760. coded order. \\
  6761. \locvar{\bj} & Integer & 36 & No & The index of a neighboring block in
  6762. coded order. \\
  6763. \locvar{\pli} & Integer & 2 & No & The color plane index of the current
  6764. block. \\
  6765. \bottomrule\end{tabularx}
  6766. \medskip
  6767. This procedure defines the order that the various block edges are filtered.
  6768. Because each application of one of the two filters above destructively modifies
  6769. the contents of the reconstructed image, the precise output obtained differs
  6770. depending on the order that horizontal and vertical filters are applied to the
  6771. edges of a single block.
  6772. The order defined here conforms to that used by VP3.
  6773. \begin{enumerate}
  6774. \item
  6775. Assign \locvar{L} the value $\bitvar{LFLIMS}[\bitvar{QIS}[0]]$.
  6776. \item
  6777. For each block in {\em raster} order, with coded-order index \locvar{\bi}:
  6778. \begin{enumerate}
  6779. \item
  6780. If $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero:
  6781. \begin{enumerate}
  6782. \item
  6783. Assign \locvar{\pli} the index of the color plane block \locvar{\bi} belongs
  6784. to.
  6785. \item
  6786. Assign \locvar{RECP}, \locvar{RPW}, and \locvar{RPH} the values given in
  6787. Table~\ref{tab:recp} corresponding to the value of \locvar{\pli}.
  6788. \begin{table}[htbp]
  6789. \begin{center}
  6790. \begin{tabular}{clll}\toprule
  6791. \locvar{\pli} & \locvar{RECP} & \locvar{RPW} & \locvar{RPH} \\\midrule
  6792. $0$ & \bitvar{RECY} & \bitvar{RPYW} & \bitvar{RPYH} \\
  6793. $1$ & \bitvar{RECCB} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6794. $2$ & \bitvar{RECCR} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6795. \bottomrule\end{tabular}
  6796. \end{center}
  6797. \caption{Reconstructed Planes and Sizes for Each \locvar{\pli}}
  6798. \label{tab:recp}
  6799. \end{table}
  6800. \item
  6801. Assign \locvar{BX} the horizontal pixel index of the lower-left corner of the
  6802. block \locvar{\bi}.
  6803. \item
  6804. Assign \locvar{BY} the vertical pixel index of the lower-left corner of the
  6805. block \locvar{\bi}.
  6806. \item
  6807. If \locvar{BX} is greater than zero:
  6808. \begin{enumerate}
  6809. \item
  6810. Assign \locvar{FX} the value $(\locvar{BX}-2)$.
  6811. \item
  6812. Assign \locvar{FY} the value \locvar{BY}.
  6813. \item
  6814. Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
  6815. horizontal block filter to the left edge of block \locvar{\bi} with the
  6816. procedure described in Section~\ref{sub:filth}.
  6817. \end{enumerate}
  6818. \item
  6819. If \locvar{BY} is greater than zero:
  6820. \begin{enumerate}
  6821. \item
  6822. Assign \locvar{FX} the value \locvar{BX}.
  6823. \item
  6824. Assign \locvar{FY} the value $(\locvar{BY}-2)$
  6825. \item
  6826. Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
  6827. vertical block filter to the bottom edge of block \locvar{\bi} with the
  6828. procedure described in Section~\ref{sub:filtv}.
  6829. \end{enumerate}
  6830. \item
  6831. If $(\locvar{BX}+8)$ is less than \locvar{RPW} and
  6832. $\bitvar{BCODED}[\locvar{\bj}]$ is zero, where \locvar{\bj} is the coded-order
  6833. index of the block adjacent to \locvar{\bi} on the right:
  6834. \begin{enumerate}
  6835. \item
  6836. Assign \locvar{FX} the value $(\locvar{BX}+6)$.
  6837. \item
  6838. Assign \locvar{FY} the value \locvar{BY}.
  6839. \item
  6840. Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
  6841. horizontal block filter to the right edge of block \locvar{\bi} with the
  6842. procedure described in Section~\ref{sub:filth}.
  6843. \end{enumerate}
  6844. \item
  6845. If $(\locvar{BY}+8)$ is less than \locvar{RPH} and
  6846. $\bitvar{BCODED}[\locvar{\bj}]$ is zero, where \locvar{\bj} is the coded-order
  6847. index of the block adjacent to \locvar{\bi} above:
  6848. \begin{enumerate}
  6849. \item
  6850. Assign \locvar{FX} the value \locvar{BX}.
  6851. \item
  6852. Assign \locvar{FY} the value $(\locvar{BY}+6)$
  6853. \item
  6854. Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
  6855. vertical block filter to the top edge of block \locvar{\bi} with the
  6856. procedure described in Section~\ref{sub:filtv}.
  6857. \end{enumerate}
  6858. \end{enumerate}
  6859. \end{enumerate}
  6860. \end{enumerate}
  6861. \paragraph{VP3 Compatibility}
  6862. The original VP3 decoder implemented unrestricted motion vectors by enlarging
  6863. the reconstructed frame buffers and repeating the pixels on its edges into the
  6864. padding region.
  6865. However, for the previous reference frame this padding ocurred before the loop
  6866. filter was applied, but for the golden reference frame it occurred afterwards.
  6867. This means that for the previous reference frame, the padding values were
  6868. required to be stored separately from the main image values.
  6869. Furthermore, even if the previous and golden reference frames were in fact the
  6870. same frame, they could have different padding values.
  6871. Finally, the encoder did not apply the loop filter at all, which resulted in
  6872. artifacts, particularly in near-static scenes, due to prediction-loop
  6873. mismatch.
  6874. This last can only be considered a bug in the VP3 encoder.
  6875. Given all these things, Theora now uniformly applies the loop filter before
  6876. the reference frames are padded.
  6877. This means it is possible to use the same buffer for the previous and golden
  6878. reference frames when they do indeed refer to the same frame.
  6879. It also means that on architectures where memory bandwidth is limited, it is
  6880. possible to avoid storing padding values, and simply clamp the motion vectors
  6881. applied to each pixel as described in Sections~\ref{sub:predfullpel}
  6882. and~\ref{sub:predhalfpel}.
  6883. This means that the predicted pixel values along the edges of the frame might
  6884. differ slightly between VP3 and Theora, but since the VP3 encoder did not
  6885. apply the loop filter in the first place, this is not likely to impose any
  6886. serious compatibility issues.
  6887. \section{Complete Frame Decode}
  6888. \paragraph{Input parameters:}\hfill\\*
  6889. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6890. \multicolumn{1}{c}{Name} &
  6891. \multicolumn{1}{c}{Type} &
  6892. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6893. \multicolumn{1}{c}{Signed?} &
  6894. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6895. \bitvar{FMBW} & Integer & 16 & No & The width of the frame in macro
  6896. blocks. \\
  6897. \bitvar{FMBH} & Integer & 16 & No & The height of the frame in macro
  6898. blocks. \\
  6899. \bitvar{NSBS} & Integer & 32 & No & The total number of super blocks in a
  6900. frame. \\
  6901. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  6902. frame. \\
  6903. \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
  6904. frame. \\
  6905. \bitvar{FRN} & Integer & 32 & No & The frame-rate numerator. \\
  6906. \bitvar{FRD} & Integer & 32 & No & The frame-rate denominator. \\
  6907. \bitvar{PARN} & Integer & 24 & No & The pixel aspect-ratio numerator. \\
  6908. \bitvar{PARD} & Integer & 24 & No & The pixel aspect-ratio
  6909. denominator. \\
  6910. \bitvar{CS} & Integer & 8 & No & The color space. \\
  6911. \bitvar{PF} & Integer & 2 & No & The pixel format. \\
  6912. \bitvar{NOMBR} & Integer & 24 & No & The nominal bitrate of the stream, in
  6913. bits per second. \\
  6914. \bitvar{QUAL} & Integer & 6 & No & The quality hint. \\
  6915. \bitvar{KFGSHIFT} & Integer & 5 & No & The amount to shift the key frame
  6916. number by in the granule position. \\
  6917. \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
  6918. 7 & No & A 64-element array of loop filter
  6919. limit values. \\
  6920. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  6921. 16 & No & A 64-element array of scale values
  6922. for AC coefficients for each \qi\ value. \\
  6923. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  6924. 16 & No & A 64-element array of scale values
  6925. for the DC coefficient for each \qi\ value. \\
  6926. \bitvar{NBMS} & Integer & 10 & No & The number of base matrices. \\
  6927. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  6928. 8 & No & A $\bitvar{NBMS}\times 64$ array
  6929. containing the base matrices. \\
  6930. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  6931. 6 & No & A $2\times 3$ array containing the
  6932. number of quant ranges for a given \qti\ and \pli, respectively.
  6933. This is at most $63$. \\
  6934. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  6935. 6 & No & A $2\times 3\times 63$ array of the
  6936. sizes of each quant range for a given \qti\ and \pli, respectively.
  6937. Only the first $\bitvar{NQRS}[\qti][\pli]$ values will be used. \\
  6938. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  6939. 9 & No & A $2\times 3\times 64$ array of the
  6940. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  6941. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values will be used. \\
  6942. \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
  6943. & An 80-element array of Huffman tables
  6944. with up to 32 entries each. \\
  6945. \bitvar{GOLDREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6946. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6947. array containing the contents of the $Y'$ plane of the golden reference
  6948. frame. \\
  6949. \bitvar{GOLDREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6950. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6951. array containing the contents of the $C_b$ plane of the golden reference
  6952. frame. \\
  6953. \bitvar{GOLDREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6954. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6955. array containing the contents of the $C_r$ plane of the golden reference
  6956. frame. \\
  6957. \bitvar{PREVREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6958. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6959. array containing the contents of the $Y'$ plane of the previous reference
  6960. frame. \\
  6961. \bitvar{PREVREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6962. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6963. array containing the contents of the $C_b$ plane of the previous reference
  6964. frame. \\
  6965. \bitvar{PREVREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6966. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6967. array containing the contents of the $C_r$ plane of the previous reference
  6968. frame. \\
  6969. \bottomrule\end{tabularx}
  6970. \paragraph{Output parameters:}\hfill\\*
  6971. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6972. \multicolumn{1}{c}{Name} &
  6973. \multicolumn{1}{c}{Type} &
  6974. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6975. \multicolumn{1}{c}{Signed?} &
  6976. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6977. \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6978. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6979. array containing the contents of the $Y'$ plane of the reconstructed frame. \\
  6980. \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6981. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6982. array containing the contents of the $C_b$ plane of the reconstructed
  6983. frame. \\
  6984. \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6985. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6986. array containing the contents of the $C_r$ plane of the reconstructed
  6987. frame. \\
  6988. \bitvar{GOLDREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6989. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6990. array containing the contents of the $Y'$ plane of the golden reference
  6991. frame. \\
  6992. \bitvar{GOLDREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6993. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6994. array containing the contents of the $C_b$ plane of the golden reference
  6995. frame. \\
  6996. \bitvar{GOLDREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6997. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6998. array containing the contents of the $C_r$ plane of the golden reference
  6999. frame. \\
  7000. \bitvar{PREVREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7001. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  7002. array containing the contents of the $Y'$ plane of the previous reference
  7003. frame. \\
  7004. \bitvar{PREVREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7005. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  7006. array containing the contents of the $C_b$ plane of the previous reference
  7007. frame. \\
  7008. \bitvar{PREVREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7009. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  7010. array containing the contents of the $C_r$ plane of the previous reference
  7011. frame. \\
  7012. \bottomrule\end{tabularx}
  7013. \paragraph{Variables used:}\hfill\\*
  7014. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  7015. \multicolumn{1}{c}{Name} &
  7016. \multicolumn{1}{c}{Type} &
  7017. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  7018. \multicolumn{1}{c}{Signed?} &
  7019. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  7020. \locvar{FTYPE} & Integer & 1 & No & The frame type. \\
  7021. \locvar{NQIS} & Integer & 2 & No & The number of \qi\ values. \\
  7022. \locvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
  7023. 6 & No & An \locvar{NQIS}-element array of
  7024. \qi\ values. \\
  7025. \locvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  7026. 1 & No & An \bitvar{NBS}-element array of flags
  7027. indicating which blocks are coded. \\
  7028. \locvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  7029. 3 & No & An \bitvar{NMBS}-element array of
  7030. coding modes for each macro block. \\
  7031. \locvar{MVECTS} & \multicolumn{1}{p{50pt}}{Array of 2D Integer Vectors} &
  7032. 6 & Yes & An \bitvar{NBS}-element array of motion
  7033. vectors for each block. \\
  7034. \locvar{QIIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  7035. 2 & No & An \bitvar{NBS}-element array of
  7036. \locvar{\qii} values for each block. \\
  7037. \locvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7038. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  7039. quantized DCT coefficient values for each block in zig-zag order. \\
  7040. \locvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  7041. 7 & No & An \bitvar{NBS}-element array of the
  7042. coefficient count for each block. \\
  7043. \bitvar{RPYW} & Integer & 20 & No & The width of the $Y'$ plane of the
  7044. reference frames in pixels. \\
  7045. \bitvar{RPYH} & Integer & 20 & No & The height of the $Y'$ plane of the
  7046. reference frames in pixels. \\
  7047. \bitvar{RPCW} & Integer & 20 & No & The width of the $C_b$ and $C_r$
  7048. planes of the reference frames in pixels. \\
  7049. \bitvar{RPCH} & Integer & 20 & No & The height of the $C_b$ and $C_r$
  7050. planes of the reference frames in pixels. \\
  7051. \locvar{\bi} & Integer & 36 & No & The index of the current block in coded
  7052. order. \\
  7053. \bottomrule\end{tabularx}
  7054. \medskip
  7055. This procedure uses all the procedures defined in the previous section of this
  7056. chapter to decode and reconstruct a complete frame.
  7057. It takes as input values decoded from the headers, as well as the current
  7058. reference frames.
  7059. As output, it gives the uncropped, reconstructed frame.
  7060. This should be cropped to picture region before display.
  7061. As a special case, a 0-byte packet is treated exactly like an inter frame with
  7062. no coded blocks.
  7063. \begin{enumerate}
  7064. \item
  7065. If the size of the data packet is non-zero:
  7066. \begin{enumerate}
  7067. \item
  7068. Decode the frame header values \locvar{FTYPE}, \locvar{NQIS}, and \locvar{QIS}
  7069. using the procedure given in Section~\ref{sub:frame-header}.
  7070. \item
  7071. Using \locvar{FTYPE}, \bitvar{NSBS}, and \bitvar{NBS}, decode the list of coded
  7072. block flags into \locvar{BCODED} using the procedure given in
  7073. Section~\ref{sub:coded-blocks}.
  7074. \item
  7075. Using \locvar{FTYPE}, \bitvar{NMBS}, \bitvar{NBS}, and \bitvar{BCODED}, decode
  7076. the macro block coding modes into \locvar{MBMODES} using the procedure given
  7077. in Section~\ref{sub:mb-modes}.
  7078. \item
  7079. If \locvar{FTYPE} is non-zero (inter frame), using \bitvar{PF}, \bitvar{NMBS},
  7080. \locvar{MBMODES}, \bitvar{NBS}, and \locvar{BCODED}, decode the motion vectors
  7081. into \locvar{MVECTS} using the procedure given in Section~\ref{sub:mv-decode}.
  7082. \item
  7083. Using \bitvar{NBS}, \locvar{BCODED}, and \locvar{NQIS}, decode the block-level
  7084. \qi\ values into \locvar{QIIS} using the procedure given in
  7085. Section~\ref{sub:block-qis}.
  7086. \item
  7087. Using \bitvar{NBS}, \bitvar{NMBS}, \locvar{BCODED}, and \bitvar{HTS}, decode
  7088. the DCT coefficients into \locvar{NCOEFFS} and \locvar{NCOEFFS} using the
  7089. procedure given in Section~\ref{sub:dct-coeffs}.
  7090. \item
  7091. Using \locvar{BCODED} and \locvar{MBMODES}, undo the DC prediction on the DC
  7092. coefficients stored in \locvar{COEFFS} using the procedure given in
  7093. Section~\ref{sub:dc-pred-undo}.
  7094. \end{enumerate}
  7095. \item
  7096. Otherwise:
  7097. \begin{enumerate}
  7098. \item
  7099. Assign \locvar{FTYPE} the value 1 (inter frame).
  7100. \item
  7101. Assign \locvar{NQIS} the value 1.
  7102. \item
  7103. Assign $\locvar{QIS}[0]$ the value 63.
  7104. \item
  7105. For each value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$, assign
  7106. $\locvar{BCODED}[\locvar{\bi}]$ the value zero.
  7107. \end{enumerate}
  7108. \item
  7109. Assign \locvar{RPYW} and \locvar{RPYH} the values $(16*\bitvar{FMBW})$ and
  7110. $(16*\bitvar{FMBH})$, respectively.
  7111. \item
  7112. Assign \locvar{RPCW} and \locvar{RPCH} the values from the row of
  7113. Table~\ref{tab:rpcwh-for-pf} corresponding to \bitvar{PF}.
  7114. \begin{table}[tb]
  7115. \begin{center}
  7116. \begin{tabular}{crr}\toprule
  7117. \bitvar{PF} & \multicolumn{1}{c}{\locvar{RPCW}}
  7118. & \multicolumn{1}{c}{\locvar{RPCH}} \\\midrule
  7119. $0$ & $8*\bitvar{FMBW}$ & $8*\bitvar{FMBH}$ \\
  7120. $2$ & $8*\bitvar{FMBW}$ & $16*\bitvar{FMBH}$ \\
  7121. $3$ & $16*\bitvar{FMBW}$ & $16*\bitvar{FMBH}$ \\
  7122. \bottomrule\end{tabular}
  7123. \end{center}
  7124. \caption{Width and Height of Chroma Planes for each Pixel Format}
  7125. \label{tab:rpcwh-for-pf}
  7126. \end{table}
  7127. \item
  7128. Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS},
  7129. \bitvar{QRSIZES}, \bitvar{QRBMIS}, \bitvar{NBS}, \locvar{BCODED},
  7130. \locvar{MBMODES}, \locvar{MVECTS}, \locvar{COEFFS}, \locvar{NCOEFFS},
  7131. \locvar{QIS}, \locvar{QIIS}, \locvar{RPYW}, \locvar{RPYH}, \locvar{RPCW},
  7132. \locvar{RPCH}, \bitvar{GOLDREFY}, \bitvar{GOLDREFCB}, \bitvar{GOLDREFCR},
  7133. \bitvar{PREVREFY}, \bitvar{PREVREFCB}, and \bitvar{PREVREFCR}, reconstruct the
  7134. complete frame into \bitvar{RECY}, \bitvar{RECCB}, and \bitvar{RECCR} using
  7135. the procedure given in Section~\ref{sub:recon}.
  7136. \item
  7137. Using \bitvar{LFLIMS}, \locvar{RPYW}, \locvar{RPYH}, \locvar{RPCW},
  7138. \locvar{RPCH}, \bitvar{NBS}, \locvar{BCODED}, and \locvar{QIS}, apply the loop
  7139. filter to the reconstructed frame in \bitvar{RECY}, \bitvar{RECCB}, and
  7140. \bitvar{RECCR} using the procedure given in Section~\ref{sub:loop-filt}.
  7141. \item
  7142. If \locvar{FTYPE} is zero (intra frame), assign \bitvar{GOLDREFY},
  7143. \bitvar{GOLDREFCB}, and \bitvar{GOLDREFCR} the values \bitvar{RECY},
  7144. \bitvar{RECCB}, and \bitvar{RECCR}, respectively.
  7145. \item
  7146. Assign \bitvar{PREVREFY}, \bitvar{PREVREFCB}, and \bitvar{PREVREFCR} the values
  7147. \bitvar{RECY}, \bitvar{RECCB}, and \bitvar{RECCR}, respectively.
  7148. \end{enumerate}
  7149. %\backmatter
  7150. \appendix
  7151. \chapter{Ogg Bitstream Encapsulation}
  7152. \label{app:oggencapsulation}
  7153. \section{Overview}
  7154. This document specifies the embedding or encapsulation of Theora packets
  7155. in an Ogg transport stream.
  7156. Ogg is a stream oriented wrapper for coded, linear time-based data.
  7157. It provides syncronization, multiplexing, framing, error detection and
  7158. seeking landmarks for the decoder and complements the raw packet format
  7159. used by the Theora codec.
  7160. This document assumes familiarity with the details of the Ogg standard.
  7161. The Xiph.org documentation provides an overview of the Ogg transport stream
  7162. format at \url{http://www.xiph.org/ogg/doc/oggstream.html} and a detailed
  7163. description at \url{http://www.xiph.org/ogg/doc/framing.html}.
  7164. The format is also defined in RFC~3533 \cite{rfc3533}.
  7165. While Theora packets can be embedded in a wide variety of media
  7166. containers and streaming mechanisms, the Xiph.org Foundation
  7167. recommends Ogg as the native format for Theora video in file-oriented
  7168. storage and transmission contexts.
  7169. \subsection{MIME type}
  7170. The generic MIME type of any Ogg file is {\tt application/ogg}.
  7171. The specific MIME type for the Ogg Theora profile documented here
  7172. is {\tt video/ogg}. This is the MIME type recommended for files
  7173. conforming to this appendix. The recommended filename extension
  7174. is {\tt .ogv}.
  7175. Outside of an encapsulation, the mime type {\tt video/theora} may
  7176. be used to refer specifically to the Theora compressed video stream.
  7177. \section{Embedding in a logical bitstream}
  7178. Ogg separates the concept of a {\em logical bitstream} consisting of the
  7179. framing of a particular sequence of packets and complete within itself
  7180. from the {\em physical bitstream} which may consist either of a single
  7181. logical bitstream or a number of logical bitstreams multiplexed
  7182. together.
  7183. This section specifies the embedding of Theora packets in a logical Ogg
  7184. bitstream.
  7185. The mapping of Ogg Theora logical bitstreams into a multiplexed physical Ogg
  7186. stream is described in the next section.
  7187. \subsection{Headers}
  7188. The initial identification header packet appears by itself in a
  7189. single Ogg page.
  7190. This page defines the start of the logical stream and MUST have
  7191. the `beginning of stream' flag set.
  7192. The second and third header packets (comment metadata and decoder
  7193. setup data) can together span one or more Ogg pages.
  7194. If there are additional non-normative header packets, they MUST be
  7195. included in this sequence of pages as well.
  7196. The comment header packet MUST begin the second Ogg page in the logical
  7197. bitstream, and there MUST be a page break between the last header
  7198. packet and the first frame data packet.
  7199. These two page break requirements facilitate stream identification and
  7200. simplify header acquisition for seeking and live streaming applications.
  7201. All header pages MUST have their granule position field set to zero.
  7202. \subsection{Frame data}
  7203. The first frame data packet in a logical bitstream MUST begin a new Ogg
  7204. page.
  7205. All other data packets are placed one at a time into Ogg pages
  7206. until the end of the stream.
  7207. Packets can span pages and multiple packets can be placed within any
  7208. one page.
  7209. The last page in the logical bitstream SHOULD have its
  7210. 'end of stream' flag set to indicate complete transmission
  7211. of the available video.
  7212. Frame data pages MUST be marked with a granule position corresponding to
  7213. the end of the display interval of the last frame/packet that finishes
  7214. in that page. See the next section for details.
  7215. \subsection{Granule position}
  7216. Data packets are marked by a granulepos derived from the count of decodable
  7217. frames after that packet is processed. The field itself is divided into two
  7218. sections, the width of the less significant section being given by the KFGSHIFT
  7219. parameter decoded from the identification header
  7220. (Section~\ref{sec:idheader}).
  7221. The more significant portion of the field gives the count of coded
  7222. frames after the coding of the last keyframe in stream, and the less
  7223. significant portion gives the count of frames since the last keyframe.
  7224. Thus a stream would begin with a split granulepos of $1|0$ (a keyframe),
  7225. followed by $1|1$, $1|2$, $1|3$, etc. Around a keyframe in the
  7226. middle of the stream the granulepos sequence might be $1234|35$,
  7227. $1234|36$, $1234|37$, $1271|0$ (for the keyframe), $1271|1$, and so
  7228. on. In this way the granulepos field increased monotonically as required
  7229. by the Ogg format, but contains information necessary to efficiently
  7230. find the previous keyframe to continue decoding after a seek.
  7231. Prior to bitstream version 3.2.1, data packets were marked by a
  7232. granulepos derived from the index of the frame being decoded,
  7233. rather than the count. That is they marked the beginning of the
  7234. display interval of a frame rather than the end. Such streams
  7235. have the VREV field of the identification header set to `0'
  7236. instead of `1'. They can be interpreted according to the description
  7237. above by adding 1 to the more signification field of the split
  7238. granulepos when VREV is less than 1.
  7239. \section{Multiplexed stream mapping}
  7240. Applications supporting Ogg Theora must support Theora bitstreams
  7241. multiplexed with compressed audio data in the Vorbis I and Speex
  7242. formats, and should support Ogg-encapsulated MNG graphics for overlays.
  7243. Multiple audio and video bitstreams may be multiplexed together.
  7244. How playback of multiple/alternate streams is handled is up to the
  7245. application.
  7246. Some conventions based on included metadata aide interoperability
  7247. in this respect.
  7248. %TODO: describe multiple vs. alternate streams, language mapping
  7249. % and reference metadata descriptions.
  7250. \subsection{Chained streams}
  7251. Ogg Theora decoders and playback applications MUST support both grouped
  7252. streams (multiplexed concurrent logical streams) and chained streams
  7253. (sequential concatenation of independent physical bitstreams).
  7254. The number and codec data types of multiplexed streams and the decoder
  7255. parameters for those stream types that re-occur can all change at a
  7256. chaining boundary.
  7257. A playback application MUST be prepared to handle such changes and
  7258. SHOULD do so smoothly with the minimum possible visible disruption.
  7259. The specification of grouped streams below applies independently to each
  7260. segment of a chained bitstream.
  7261. \subsection{Grouped streams}
  7262. At the beginning of a multiplexed stream, the `beginning of stream'
  7263. pages for each logical bitstream will be grouped together.
  7264. Within these, the first page to occur MUST be the Theora page.
  7265. This facilitates identification of Ogg Theora files among other
  7266. Ogg-encapsulated content.
  7267. A playback application must nevertheless handle streams where this
  7268. arrangement is not correct.
  7269. %TBT: Then what's the point of requiring it in the spec?
  7270. If there is more than one Theora logical stream, the first page should
  7271. be from the primary stream.
  7272. That is, the best choice for the stream a generic player should begin
  7273. displaying without special user direction.
  7274. If there is more than one audio stream, or of any other stream
  7275. type, the identification page of the primary stream of that type
  7276. should be placed before the others.
  7277. %TBT: That's all pretty vague.
  7278. After the `beginning of stream' pages, the header pages of each of
  7279. the logical streams MUST be grouped together before any data pages
  7280. occur.
  7281. After all the header pages have been placed,
  7282. the data pages are multiplexed together.
  7283. They should be placed in the stream in increasing order by the
  7284. time equivalents of their granule position fields.
  7285. This facilitates seeking while limiting the buffering requirements of the
  7286. playback demultiplexer.
  7287. %TODO: A lot of this language is encoder-oriented.
  7288. %TODO: We define a decoder-oriented specification.
  7289. %TODO: The language should be changed to match.
  7290. \cleardoublepage
  7291. \chapter{VP3}
  7292. \section{VP3 Compatibility}
  7293. \label{app:vp3-compat}
  7294. This section lists all of the encoder and decoder issues that may affect VP3
  7295. compatibly.
  7296. Each is described in more detail in the text itself.
  7297. This list is provided merely for reference.
  7298. \begin{itemize}
  7299. \item
  7300. Bitstream headers (Section~\ref{sec:headers}).
  7301. \begin{itemize}
  7302. \item
  7303. Identification header (Section~\ref{sec:idheader}).
  7304. \begin{itemize}
  7305. \item
  7306. Non-multiple of 16 picture sizes.
  7307. \item
  7308. Standardized color spaces.
  7309. \item
  7310. Support for $4:4:4$ and $4:2:2$ pixel formats.
  7311. \end{itemize}
  7312. \item
  7313. Setup header
  7314. \begin{itemize}
  7315. \item
  7316. Loop filter limit values (Section~\ref{sub:loop-filter-limits}).
  7317. \item
  7318. Quantization parameters (Section~\ref{sub:quant-params}).
  7319. \item
  7320. Huffman tables (Section~\ref{sub:huffman-tables}).
  7321. \end{itemize}
  7322. \end{itemize}
  7323. \item
  7324. Frame header format (Section~\ref{sub:frame-header}).
  7325. \item
  7326. Extended long-run bit strings (Section~\ref{sub:long-run}).
  7327. \item
  7328. INTER\_MV\_FOUR handling of uncoded blocks (Section~\ref{sub:mb-mv-decode}).
  7329. \item
  7330. Block-level \qi\ values (Section~\ref{sub:block-qis}).
  7331. \item
  7332. Zero-length EOB runs (Section~\ref{sub:eob-token}).
  7333. \item
  7334. Unrestricted motion vector padding and the loop filter
  7335. (Section~\ref{sub:loop-filt}).
  7336. \end{itemize}
  7337. \section{Loop Filter Limit Values}
  7338. \label{app:vp3-loop-filter-limits}
  7339. The hard-coded loop filter limit values used in VP3 are defined as follows:
  7340. \begin{align*}
  7341. \bitvar{LFLIMS} = & \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7342. \{ & 30, & 25, & 20, & 20, & 15, & 15, & 14, & 14, & \\
  7343. & 13, & 13, & 12, & 12, & 11, & 11, & 10, & 10, & \\
  7344. & 9, & 9, & 8, & 8, & 7, & 7, & 7, & 7, & \\
  7345. & 6, & 6, & 6, & 6, & 5, & 5, & 5, & 5, & \\
  7346. & 4, & 4, & 4, & 4, & 3, & 3, & 3, & 3, & \\
  7347. & 2, & 2, & 2, & 2, & 2, & 2, & 2, & 2, & \\
  7348. & 0, & 0, & 0, & 0, & 0, & 0, & 0, & 0, & \\
  7349. & 0, & 0, & 0, & 0, & 0, & 0, & 0, & 0\;\ & \!\} \\
  7350. \end{array}
  7351. \end{align*}
  7352. \section{Quantization Parameters}
  7353. \label{app:vp3-quant-params}
  7354. The hard-coded quantization parameters used by VP3 are defined as follows:
  7355. \begin{align*}
  7356. \bitvar{ACSCALE} = & \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7357. \{ & 500, & 450, & 400, & 370, & 340, & 310, & 285, & 265, & \\
  7358. & 245, & 225, & 210, & 195, & 185, & 180, & 170, & 160, & \\
  7359. & 150, & 145, & 135, & 130, & 125, & 115, & 110, & 107, & \\
  7360. & 100, & 96, & 93, & 89, & 85, & 82, & 75, & 74, & \\
  7361. & 70, & 68, & 64, & 60, & 57, & 56, & 52, & 50, & \\
  7362. & 49, & 45, & 44, & 43, & 40, & 38, & 37, & 35, & \\
  7363. & 33, & 32, & 30, & 29, & 28, & 25, & 24, & 22, & \\
  7364. & 21, & 19, & 18, & 17, & 15, & 13, & 12, & 10\;\ & \!\} \\
  7365. \end{array} \\
  7366. \bitvar{DCSCALE} = & \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7367. \{ & 220, & 200, & 190, & 180, & 170, & 170, & 160, & 160, & \\
  7368. & 150, & 150, & 140, & 140, & 130, & 130, & 120, & 120, & \\
  7369. & 110, & 110, & 100, & 100, & 90, & 90, & 90, & 80, & \\
  7370. & 80, & 80, & 70, & 70, & 70, & 60, & 60, & 60, & \\
  7371. & 60, & 50, & 50, & 50, & 50, & 40, & 40, & 40, & \\
  7372. & 40, & 40, & 30, & 30, & 30, & 30, & 30, & 30, & \\
  7373. & 30, & 20, & 20, & 20, & 20, & 20, & 20, & 20, & \\
  7374. & 20, & 10, & 10, & 10, & 10, & 10, & 10, & 10\;\ & \!\} \\
  7375. \end{array}
  7376. \end{align*}
  7377. VP3 defines only a single quantization range for each quantization type and
  7378. color plane, and the base matrix used is constant throughout the range.
  7379. There are three base matrices defined.
  7380. The first is used for the $Y'$ channel of INTRA mode blocks, and the second for
  7381. both the $C_b$ and $C_r$ channels of INTRA mode blocks.
  7382. The last is used for INTER mode blocks of all channels.
  7383. \begin{align*}
  7384. \bitvar{BMS} = \{ & \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7385. \{ & 16, & 11, & 10, & 16, & 24, & 40, & 51, & 61, & \\
  7386. & 12, & 12, & 14, & 19, & 26, & 58, & 60, & 55, & \\
  7387. & 14, & 13, & 16, & 24, & 40, & 57, & 69, & 56, & \\
  7388. & 14, & 17, & 22, & 29, & 51, & 87, & 80, & 62, & \\
  7389. & 18, & 22, & 37, & 58, & 68, & 109, & 103, & 77, & \\
  7390. & 24, & 35, & 55, & 64, & 81, & 104, & 113, & 92, & \\
  7391. & 49, & 64, & 78, & 87, & 103, & 121, & 120, & 101, & \\
  7392. & 72, & 92, & 95, & 98, & 112, & 100, & 103, & 99\;\ & \!\}, \\
  7393. %\end{array} \\
  7394. %& \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7395. \{ & 17, & 18, & 24, & 47, & 99, & 99, & 99, & 99, & \\
  7396. & 18, & 21, & 26, & 66, & 99, & 99, & 99, & 99, & \\
  7397. & 24, & 26, & 56, & 99, & 99, & 99, & 99, & 99, & \\
  7398. & 47, & 66, & 99, & 99, & 99, & 99, & 99, & 99, & \\
  7399. & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99, & \\
  7400. & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99, & \\
  7401. & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99, & \\
  7402. & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99\;\ & \!\}, \\
  7403. %\end{array} \\
  7404. %& \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7405. \{ & 16, & 16, & 16, & 20, & 24, & 28, & 32, & 40, & \\
  7406. & 16, & 16, & 20, & 24, & 28, & 32, & 40, & 48, & \\
  7407. & 16, & 20, & 24, & 28, & 32, & 40, & 48, & 64, & \\
  7408. & 20, & 24, & 28, & 32, & 40, & 48, & 64, & 64, & \\
  7409. & 24, & 28, & 32, & 40, & 48, & 64, & 64, & 64, & \\
  7410. & 28, & 32, & 40, & 48, & 64, & 64, & 64, & 96, & \\
  7411. & 32, & 40, & 48, & 64, & 64, & 64, & 96, & 128, & \\
  7412. & 40, & 48, & 64, & 64, & 64, & 96, & 128, & 128\;\ & \!\}\;\;\} \\
  7413. \end{array}
  7414. \end{align*}
  7415. The remaining parameters simply assign these matrices to the proper quant
  7416. ranges.
  7417. \begin{align*}
  7418. \bitvar{NQRS} = & \{ \{1, 1, 1\}, \{1, 1, 1\} \} \\
  7419. \bitvar{QRSIZES} = &
  7420. \{ \{ \{1\}, \{1\}, \{1\} \}, \{ \{1\}, \{1\}, \{1\} \} \} \\
  7421. \bitvar{QRBMIS} = &
  7422. \{ \{ \{0, 0\}, \{1, 1\}, \{1, 1\} \}, \{ \{2, 2\}, \{2, 2\}, \{2, 2\} \} \} \\
  7423. \end{align*}
  7424. \section{Huffman Tables}
  7425. \label{app:vp3-huffman-tables}
  7426. The following tables contain the hard-coded Huffman codes used by VP3.
  7427. There are 80 tables in all, each with a Huffman code for all 32 token values.
  7428. The tokens are sorted by the most significant bits of their Huffman code.
  7429. This is the same order in which they will be decoded from the setup header.
  7430. \include{vp3huff}
  7431. \cleardoublepage
  7432. \chapter{Colophon}
  7433. Ogg is a \href{http://www.xiph.org}{Xiph.org Foundation} effort to protect
  7434. essential tenets of Internet multimedia from corporate hostage-taking; Open
  7435. Source is the net's greatest tool to keep everyone honest.
  7436. See \href{http://www.xiph.org/about.html}{About the Xiph.org Foundation} for
  7437. details.
  7438. Ogg Theora is the first Ogg video codec.
  7439. Anyone may freely use and distribute the Ogg and Theora specifications, whether
  7440. in private, public, or corporate capacity.
  7441. However, the Xiph.org Foundation and the Ogg project reserve the right to set
  7442. the Ogg Theora specification and certify specification compliance.
  7443. Xiph.org's Theora software codec implementation is distributed under a BSD-like
  7444. license.
  7445. This does not restrict third parties from distributing independent
  7446. implementations of Theora software under other licenses.
  7447. \begin{wrapfigure}{l}{0pt}
  7448. \includegraphics[width=2.5cm]{xifish}
  7449. \end{wrapfigure}
  7450. These pages are Copyright \textcopyright{} 2004-2007 Xiph.org Foundation.
  7451. All rights reserved.
  7452. Ogg, Theora, Vorbis, Xiph.org Foundation and their logos are trademarks
  7453. (\texttrademark) of the \href{http://www.xiph.org}{Xiph.org Foundation}.
  7454. This document is set in \LaTeX.
  7455. \cleardoublepage
  7456. \bibliography{spec}
  7457. \end{document}