spec.tex 329 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194
  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. %Provide section numbering for \paragraph.
  58. \makeatletter
  59. \renewcommand{\paragraph}{\@startsection{paragraph}{4}{0ex}%
  60. {-3.25ex plus -1ex minus -0.2ex}%
  61. {1.5ex plus 0.2ex}%
  62. {\normalfont\normalsize\bfseries}}
  63. \makeatother
  64. \stepcounter{secnumdepth}
  65. \stepcounter{tocdepth}
  66. \keepXColumns
  67. \pagestyle{headings}
  68. \bibliographystyle{alpha}
  69. \title{Theora Specification}
  70. \author{Xiph.Org Foundation}
  71. \date{\today}
  72. \begin{document}
  73. \frontmatter
  74. \begin{titlepage}
  75. \maketitle
  76. \end{titlepage}
  77. \thispagestyle{empty}
  78. \cleardoublepage
  79. \pagenumbering{roman}
  80. \thispagestyle{plain}
  81. \tableofcontents
  82. \cleardoublepage
  83. \thispagestyle{plain}
  84. \listoffigures
  85. \cleardoublepage
  86. \thispagestyle{plain}
  87. \listoftables
  88. \cleardoublepage
  89. \thispagestyle{plain}
  90. \markboth{{\sc Notation and Conventions}}{{\sc Notation and Conventions}}
  91. \chapter*{Notation and Conventions}
  92. All parameters either passed in or out of a decoding procedure are given in
  93. \bitvar{bold\ face}.
  94. The prefix \bin{} indicates that the following value is to be interpreted as a
  95. binary number (base 2).
  96. \begin{verse}
  97. {\bf Example:} The value \bin{1110100} is equal to the decimal value 116.
  98. \end{verse}
  99. The prefix \hex{} indicates the following value is to be interpreted as a
  100. hexadecimal number (base 16).
  101. \begin{verse}
  102. {\bf Example:} The value \hex{74} is equal to the decimal value 116.
  103. \end{verse}
  104. All arithmetic defined by this specification is exact.
  105. However, any real numbers that do arise will always be converted back to
  106. integers again in short order.
  107. The entire specification can be implemented using only normal integer
  108. operations.
  109. All operations are to be implemented with sufficiently large integers so that
  110. overflow cannot occur.
  111. Where the result of a computation is to be truncated to a fixed-sized binary
  112. representation, this will be explicitly noted.
  113. The size given for all variables is the maximum number of bits needed to store
  114. any value in that variable.
  115. Intermediate computations involving that variable may require more bits.
  116. The following operators are defined:
  117. \begin{description}
  118. \item[$|a|$]
  119. The absolute value of a number $a$.
  120. \begin{align*}
  121. |a| & = \left\{\begin{array}{ll}
  122. -a, & a < 0 \\
  123. a, & a \ge 0
  124. \end{array}\right.
  125. \end{align*}
  126. \item[$a*b$]
  127. Multiplication of a number $a$ by a number $b$.
  128. \item[$\frac{a}{b}$]
  129. Exact division of a number $a$ by a number $b$, producing a potentially
  130. non-integer result.
  131. \item[$\left\lfloor a\right\rfloor$]
  132. The largest integer less than or equal to a real number $a$.
  133. \item[$\left\lceil a\right\rceil$]
  134. The smallest integer greater than or equal to a real number $a$.
  135. \item[$a//b$]
  136. Integer division of $a$ by $b$.
  137. \begin{align*}
  138. a//b & = \left\{\begin{array}{ll}
  139. \left\lceil\frac{a}{b}\right\rceil, & a < 0 \\
  140. \left\lfloor\frac{a}{b}\right\rfloor, & a \ge 0
  141. \end{array}\right.
  142. \end{align*}
  143. \item[$a\%b$]
  144. The remainder from the integer division of $a$ by $b$.
  145. \begin{align*}
  146. a\%b & = a-|b|*\left\lfloor\frac{a}{|b|}\right\rfloor
  147. \end{align*}
  148. Note that with this definition, the result is always non-negative and less than
  149. $|b|$.
  150. \item[$a<<b$]
  151. The value obtained by left-shifting the two's complement integer $a$ by $b$
  152. bits.
  153. For purposes of this specification, overflow is ignored, and so this is
  154. equivalent to integer multiplication of $a$ by $2^b$.
  155. \item[$a>>b$]
  156. The value obtained by right-shifting the two's complement integer $a$ by $b$
  157. bits, filling in the leftmost bits of the new value with $0$ if $a$ is
  158. non-negative and $1$ if $a$ is negative.
  159. This is {\em not} equivalent to integer division of $a$ by $2^b$.
  160. Instead,
  161. \begin{align*}
  162. a>>b & = \left\lfloor\frac{a}{2^b}\right\rfloor.
  163. \end{align*}
  164. \item[$\round(a)$]
  165. Rounds a number $a$ to the nearest integer, with ties rounded away from $0$.
  166. \begin{align*}
  167. \round(a) = \left\{\begin{array}{ll}
  168. \lceil a-\frac{1}{2}\rceil & a \le 0 \\
  169. \lfloor a+\frac{1}{2}\rfloor & a > 0
  170. \end{array}\right.
  171. \end{align*}
  172. \item[$\sign(a)$]
  173. Returns the sign of a given number.
  174. \begin{align*}
  175. \sign(a) = \left\{\begin{array}{ll}
  176. -1 & a < 0 \\
  177. 0 & a = 0 \\
  178. 1 & a > 0
  179. \end{array}\right.
  180. \end{align*}
  181. \item[$\ilog(a)$]
  182. The minimum number of bits required to store a positive integer $a$ in
  183. two's complement notation, or $0$ for a non-positive integer $a$.
  184. \begin{align*}
  185. \ilog(a) = \left\{\begin{array}{ll}
  186. 0, & a \le 0 \\
  187. \left\lfloor\log_2{a}\right\rfloor+1, & a > 0
  188. \end{array}\right.
  189. \end{align*}
  190. \begin{verse}
  191. {\bf Examples:}
  192. \begin{itemize}
  193. \item $\ilog(-1)=0$
  194. \item $\ilog(0)=0$
  195. \item $\ilog(1)=1$
  196. \item $\ilog(2)=2$
  197. \item $\ilog(3)=2$
  198. \item $\ilog(4)=3$
  199. \item $\ilog(7)=3$
  200. \end{itemize}
  201. \end{verse}
  202. \item[$\min(a,b)$]
  203. The minimum of two numbers $a$ and $b$.
  204. \item[$\max(a,b)$]
  205. The maximum of two numbers $a$ and $b$.
  206. \end{description}
  207. \cleardoublepage
  208. \thispagestyle{plain}
  209. \markboth{{\sc Key words}}{{\sc Key words}}
  210. \chapter*{Key words}
  211. %We can't rewrite this, because this is text required by RFC 2119, so we use
  212. % some emergency stretching to get it typeset properly.
  213. \setlength{\emergencystretch}{2em}
  214. The key words ``MUST'', ``MUST NOT'', ``REQUIRED'', ``SHALL'', ``SHALL NOT'',
  215. ``SHOULD'', ``SHOULD NOT'', ``RECOMMENDED'', ``MAY'', and ``OPTIONAL'' in this
  216. document are to be intrepreted as described in RFC 2119 \cite{rfc2119}.\par
  217. \setlength{\emergencystretch}{0em}
  218. Where such assertions are placed on the contents of a Theora bitstream itself,
  219. implementations should be prepared to encounter bitstreams that do not follow
  220. these requirements.
  221. An application's behavior in the presecence of such non-conforming bitstreams
  222. is not defined by this specification, but any reasonable method of handling
  223. them MAY be used.
  224. By way of example, applications MAY discard the current frame, retain the
  225. current output thus far, or attempt to continue on by assuming some default
  226. values for the erroneous bits.
  227. When such an error occurs in the bitstream headers, an application MAY refuse
  228. to decode the entire stream.
  229. An application SHOULD NOT allow such non-conformant bitstreams to overflow
  230. buffers and potentially execute arbitrary code, as this represents a serious
  231. security risk.
  232. An application MUST, however, ensure any bits marked as reserved have the value
  233. zero, and refuse to decode the stream if they do not.
  234. These are used as place holders for future bitstream features with which the
  235. current bitstream is forward-compatible.
  236. Such features may not increment the bitstream version number, and can only be
  237. recognized by checking the value of these reserved bits.
  238. \cleardoublepage
  239. \mainmatter
  240. \pagenumbering{arabic}
  241. \setcounter{page}{1}
  242. \chapter{Introduction}
  243. Theora is a general purpose, lossy video codec.
  244. It is based on the VP3 video codec produced by On2 Technologies
  245. (\url{http://www.on2.com/}).
  246. On2 donated the VP3.1 source code to the Xiph.Org Foundation and released it
  247. under a BSD-like license.
  248. On2 also made an irrevocable, royalty-free license grant for any patent claims
  249. it might have over the software and any derivatives.
  250. No formal specification exists for the VP3 format beyond this source code,
  251. however Mike Melanson maintains a detailed description \cite{Mel04}.
  252. Portions of this specification were adopted from that text with permission.
  253. \section{VP3 and Theora}
  254. Theora contains a superset of the features that were available in the original
  255. VP3 codec.
  256. Content encoded with VP3.1 can be losslessly transcoded into the Theora format.
  257. Theora content cannot, in general, be losslessly transcoded into the VP3
  258. format.
  259. If a feature is not available in the original VP3 format, this is mentioned
  260. when that feature is defined.
  261. A complete list of these features appears in Appendix~\ref{app:vp3-compat}.
  262. %TODO: VP3 - theora comparison in appendix
  263. \section{Video Formats}
  264. Theora currently supports progressive video data of arbitrary dimensions at a
  265. constant frame rate in one of several $Y'C_bC_r$ color spaces.
  266. The precise definition the supported color spaces appears in
  267. Section~\ref{sec:colorspaces}.
  268. Three different chroma subsampling formats are supported: 4:2:0, 4:2:2,
  269. and 4:4:4.
  270. The precise details of each of these formats and their sampling locations are
  271. described in Section~\ref{sec:pixfmts}.
  272. The Theora format does not support interlaced material, variable frame rates,
  273. bit-depths larger than 8 bits per component, nor alternate color spaces such
  274. as RGB or arbitrary multi-channel spaces.
  275. Black and white content can be efficiently encoded, however, because the
  276. uniform chroma planes compress well.
  277. Support for interlaced material is planned for a future version.
  278. \begin{verse}
  279. {\bf Note:} Infrequently changing frame rates---as when film and video
  280. sequences are cut together---can be supported in the Ogg container format by
  281. chaining several Theora streams together.
  282. \end{verse}
  283. Support for increased bit depths or additional color spaces is not planned.
  284. \section{Classification}
  285. Theora is a block-based lossy transform codec that utilizes an
  286. $8\times 8$ Type-II Discrete Cosine Transform and block-based motion
  287. compensation.
  288. This places it in the same class of codecs as MPEG-1, -2, -4, and H.263.
  289. The details of how individual blocks are organized and how DCT coefficients are
  290. stored in the bitstream differ substantially from these codecs, however.
  291. Theora supports only intra frames (I frames in MPEG) and inter frames (P frames
  292. in MPEG).
  293. There is no equivalent to the bi-predictive frames (B frames) found in MPEG
  294. codecs.
  295. \section{Assumptions}
  296. The Theora codec design assumes a complex, psychovisually-aware encoder and a
  297. simple, low-complexity decoder.
  298. %TODO: Talk more about implementation complexity.
  299. Theora provides none of its own framing, synchronization, or protection against
  300. transmission errors.
  301. An encoder is solely a method of accepting input video frames and
  302. compressing these frames into raw, unformatted `packets'.
  303. The decoder then accepts these raw packets in sequence, decodes them, and
  304. synthesizes a fascimile of the original video frames.
  305. Theora is a free-form variable bit rate (VBR) codec, and packets have no
  306. minimum size, maximum size, or fixed/expected size.
  307. Theora packets are thus intended to be used with a transport mechanism that
  308. provides free-form framing, synchronization, positioning, and error correction
  309. in accordance with these design assumptions, such as Ogg (for file transport)
  310. or RTP (for network multicast).
  311. For the purposes of a few examples in this document, we will assume that Theora
  312. is embedded in an Ogg stream specifically, although this is by no means a
  313. requirement or fundamental assumption in the Theora design.
  314. The specification for embedding Theora into an Ogg transport stream is given in
  315. Appendix~\ref{app:oggencapsulation}.
  316. \section{Codec Setup and Probability Model}
  317. Theora's heritage is the proprietary commerical codec VP3, and it retains a
  318. fair amount of inflexibility when compared to Vorbis \cite{vorbis}, the first
  319. Xiph.Org codec, which began as a research codec.
  320. However, to provide additional scope for encoder improvement, Theora adopts
  321. some of the configurable aspects of decoder setup that are present in Vorbis.
  322. This configuration data is not available in VP3, which uses hardcoded values
  323. instead.
  324. Theora makes the same controversial design decision that Vorbis made to include
  325. the entire probability model for the DCT coefficients and all the quantization
  326. parameters in the bitstream headers.
  327. This is often several hundred fields.
  328. It is therefore impossible to decode any frame in the stream without
  329. having previously fetched the codec info and codec setup headers.
  330. \begin{verse}
  331. {\bf Note:} Theora {\em can} initiate decode at an arbitrary intra-frame packet
  332. within a bitstream so long as the codec has been initialized with the setup
  333. headers.
  334. \end{verse}
  335. Thus, Theora headers are both required for decode to begin and relatively large
  336. as bitstream headers go.
  337. The header size is unbounded, although as a rule-of-thumb less than 16kB is
  338. recommended, and Xiph.Org's reference encoder follows this suggestion.
  339. %TODO: Is 8kB enough? My setup header is 7.4kB, that doesn't leave much room
  340. % for comments.
  341. %RG: the lesson from vorbis is that as small as possible is really
  342. % important in some applications. Practically, what's acceptable
  343. % depends a great deal on the target bitrate. I'd leave 16 kB in the
  344. % spec for now. fwiw more than 1k of comments is quite unusual.
  345. Our own design work indicates that the primary liability of the required header
  346. is in mindshare; it is an unusual design and thus causes some amount of
  347. complaint among engineers as this runs against current design trends and
  348. points out limitations in some existing software/interface designs.
  349. However, we find that it does not fundamentally limit Theora's suitable
  350. application space.
  351. %silvia: renamed
  352. %\subsection{Format Specification}
  353. \section{Format Conformance}
  354. The Theora format is well-defined by its decode specification; any encoder that
  355. produces packets that are correctly decoded by an implementation following
  356. this specification may be considered a proper Theora encoder.
  357. A decoder must faithfully and completely implement the specification defined
  358. herein %, except where noted,
  359. to be considered a conformant Theora decoder.
  360. A decoder need not be implemented strictly as described, but the
  361. actual decoder process MUST be {\em entirely mathematically equivalent}
  362. to the described process.
  363. Where appropriate, a non-normative description of encoder processes is
  364. included.
  365. These sections will be marked as such, and a proper Theora encoder is not
  366. bound to follow them.
  367. %TODO: \subsection{Hardware Profile}
  368. \chapter{Coded Video Structure}
  369. Theora's encoding and decoding process is based on $8\times 8$ blocks of
  370. pixels.
  371. This sections describes how a video frame is laid out, divided into
  372. blocks, and how those blocks are organized.
  373. \section{Frame Layout}
  374. A video frame in Theora is a two-dimensional array of pixels.
  375. Theora, like VP3, uses a right-handed coordinate system, with the origin in the
  376. lower-left corner of the frame.
  377. This is contrary to many video formats which use a left-handed coordinate
  378. system with the origin in the upper-left corner of the frame.
  379. %INT: This means that for interlaced material, the definition of `even fields'
  380. %INT: and `odd fields' may be reversed between Theora and other video codecs.
  381. %INT: This document will always refer to them as `top fields' and `bottom
  382. %INT: fields'.
  383. Theora divides the pixel array up into three separate \term{color planes}, one
  384. for each of the $Y'$, $C_b$, and $C_r$ components of the pixel.
  385. The $Y'$ plane is also called the \term{luma plane}, and the $C_b$ and $C_r$
  386. planes are also called the \term{chroma planes}.
  387. Each plane is assigned a numerical value, as shown in
  388. Table~\ref{tab:color-planes}.
  389. \begin{table}[htbp]
  390. \begin{center}
  391. \begin{tabular}{cl}\toprule
  392. Index & Color Plane \\\midrule
  393. $0$ & $Y'$ \\
  394. $1$ & $C_b$ \\
  395. $2$ & $C_r$ \\
  396. \bottomrule\end{tabular}
  397. \end{center}
  398. \caption{Color Plane Indices}
  399. \label{tab:color-planes}
  400. \end{table}
  401. In some pixel formats, the chroma planes are subsampled by a factor of two
  402. in one or both directions.
  403. This means that the width or height of the chroma planes may be half that of
  404. the total frame width and height.
  405. The luma plane is never subsampled.
  406. \section{Picture Region}
  407. An encoded video frame in Theora is required to have a width and height that
  408. are multiples of sixteen, making an integral number of blocks even when the
  409. chroma planes are subsampled.
  410. However, inside a frame a smaller \term{picture region} may be defined
  411. to present material whose dimensions are not a multiple of sixteen pixels, as
  412. shown in Figure~\ref{fig:pic-frame}.
  413. The picture region can be offset from the lower-left corner of the frame by up
  414. to 255 pixels in each direction, and may have an arbitrary width and height,
  415. provided that it is contained entirely within the coded frame.
  416. It is this picture region that contains the actual video data.
  417. The portions of the frame which lie outside the picture region may contain
  418. arbitrary image data, so the frame must be cropped to the picture region
  419. before display.
  420. The picture region plays no other role in the decode process, which operates on
  421. the entire video frame.
  422. \begin{figure}[htbp]
  423. \begin{center}
  424. \includegraphics{pic-frame}
  425. \end{center}
  426. \caption{Location of frame and picture regions}
  427. \label{fig:pic-frame}
  428. \end{figure}
  429. \section{Blocks and Super Blocks}
  430. \label{sec:blocks-and-sbs}
  431. Each color plane is subdivided into \term{blocks} of $8\times 8$ pixels.
  432. Blocks are grouped into $4\times 4$ arrays called \term{super blocks} as
  433. shown in Figure~\ref{fig:superblock}.
  434. Each color plane has its own set of blocks and super blocks.
  435. If the chroma planes are subsampled, they are still divided into $8\times 8$
  436. blocks of pixels; there are just fewer blocks than in the luma plane.
  437. The boundaries of blocks and super blocks in the luma plane do not necessarily
  438. coincide with those of the chroma planes, if the chroma planes have been
  439. subsampled.
  440. \begin{figure}[htbp]
  441. \begin{center}
  442. \includegraphics{superblock}
  443. \end{center}
  444. \caption{Subdivision of a frame into blocks and super blocks}
  445. \label{fig:superblock}
  446. \end{figure}
  447. Blocks are accessed in two different orders in the various decoder processes.
  448. The first is \term{raster order}, illustrated in Figure~\ref{fig:raster-block}.
  449. This accesses each block in row-major order, starting in the lower left of the
  450. frame and continuing along the bottom row of the entire frame, followed by the
  451. next row up, starting on the left edge of the frame, etc.
  452. \begin{figure}[htbp]
  453. \begin{center}
  454. \includegraphics{raster-block}
  455. \end{center}
  456. \caption{Raster ordering of $n\times m$ blocks}
  457. \label{fig:raster-block}
  458. \end{figure}
  459. The second is \term{coded order}.
  460. In coded order, blocks are accessed by super block.
  461. Within each frame, super blocks are traversed in raster order,
  462. similar to raster order for blocks.
  463. Within each super block, however, blocks are accessed in a Hilbert curve
  464. pattern, illustrated in Figure~\ref{fig:hilbert-block}.
  465. If a color plane does not contain a complete super block on the top or right
  466. sides, the same ordering is still used, simply with any blocks outside the
  467. frame boundary ommitted.
  468. \begin{figure}[htbp]
  469. \begin{center}
  470. \includegraphics{hilbert-block}
  471. \end{center}
  472. \caption{Hilbert curve ordering of blocks within a super block}
  473. \label{fig:hilbert-block}
  474. \end{figure}
  475. To illustrate this ordering, consider a frame that is 240 pixels wide and
  476. 48 pixels high.
  477. Each row of the luma plane has 30 blocks and 8 super blocks, and there are 6
  478. rows of blocks and two rows of super blocks.
  479. %When accessed in raster order, each block in the luma plane is assigned the
  480. % following indices:
  481. %\vspace{\baselineskip}
  482. %\begin{center}
  483. %\begin{tabular}{|ccccccc|}\hline
  484. %150 & 151 & 152 & 153 & $\ldots$ & 178 & 179 \\
  485. %120 & 121 & 122 & 123 & $\ldots$ & 148 & 149 \\\hline
  486. % 90 & 91 & 92 & 93 & $\ldots$ & 118 & 119 \\
  487. % 60 & 61 & 62 & 63 & $\ldots$ & 88 & 89 \\
  488. % 30 & 31 & 32 & 33 & $\ldots$ & 58 & 59 \\
  489. % 0 & 1 & 2 & 3 & $\ldots$ & 28 & 29 \\\hline
  490. %\end{tabular}
  491. %\end{center}
  492. %\vspace{\baselineskip}
  493. When accessed in coded order, each block in the luma plane is assigned the
  494. following indices:
  495. \vspace{\baselineskip}
  496. \begin{center}
  497. \begin{tabular}{|cccc|c|cc|}\hline
  498. 123 & 122 & 125 & 124 & $\ldots$ & 179 & 178 \\
  499. 120 & 121 & 126 & 127 & $\ldots$ & 176 & 177 \\\hline
  500. 5 & 6 & 9 & 10 & $\ldots$ & 117 & 118 \\
  501. 4 & 7 & 8 & 11 & $\ldots$ & 116 & 119 \\
  502. 3 & 2 & 13 & 12 & $\ldots$ & 115 & 114 \\
  503. 0 & 1 & 14 & 15 & $\ldots$ & 112 & 113 \\\hline
  504. \end{tabular}
  505. \end{center}
  506. \vspace{\baselineskip}
  507. Here the index values specify the order in which the blocks would be accessed.
  508. The indices of the blocks are numbered continuously from one color plane to the
  509. next.
  510. They do not reset to zero at the start of each plane.
  511. Instead, the numbering increases continuously from the $Y'$ plane to the $C_b$
  512. plane to the $C_r$ plane.
  513. The implication is that the blocks from all planes are treated as a unit during
  514. the various processing steps.
  515. Although blocks are sometimes accessed in raster order, in this document the
  516. index associated with a block is {\em always} its index in coded order.
  517. \section{Macro Blocks}
  518. \label{sec:mbs}
  519. A macro block contains a $2\times 2$ array of blocks in the luma plane
  520. {\em and} the co-located blocks in the chroma planes, as shown in
  521. Figure~\ref{fig:macroblock}.
  522. Thus macro blocks can represent anywhere from six to twelve blocks, depending
  523. on how the chroma planes are subsampled.
  524. This is in contrast to super blocks, which only contain blocks from a single
  525. color plane.
  526. % the whole super vs. macro blocks thing is a little confusing, and it can be
  527. % hard to remember which is what initially. A figure would/will help here,
  528. % but I tried to add some text emphasizing the difference in terms of
  529. % functionality.
  530. %TBT: At this point we haven't described any functionality yet.
  531. %TBT: As far as the reader knows, the only purpose of the blocks, macro blocks
  532. %TBT: and super blocks is for data organization---and for blocks and super
  533. %TBT: blocks, this is essentially true.
  534. %TBT: So lets restrict the differences we emphasize to those of data
  535. %TBT: organization, which the sentence I just added above does.
  536. Macro blocks contain information about coding mode and motion vectors for the
  537. corresponding blocks in all color planes.
  538. \begin{figure}[htbp]
  539. \begin{center}
  540. \includegraphics{macroblock}
  541. \end{center}
  542. \caption{Subdivision of a frame into macro blocks}
  543. \label{fig:macroblock}
  544. \end{figure}
  545. Macro blocks are also accessed in a \term{coded order}.
  546. This coded order proceeds by examining each super block in the luma plane in
  547. raster order, and traversing the four macro blocks inside using a smaller
  548. Hilbert curve, as shown in Figure~\ref{fig:hilbert-mb}.
  549. %r: I rearranged the wording to make a more formal idiom here
  550. If the luma plane does not contain a complete super block on the top or right
  551. sides, the same ordering is still used, with any macro blocks outside
  552. the frame boundary simply omitted.
  553. Because the frame size is constrained to be a multiple of 16, there are never
  554. any partial macro blocks.
  555. Unlike blocks, macro blocks need never be accessed in a pure raster order.
  556. \begin{figure}[htbp]
  557. \begin{center}
  558. \includegraphics{hilbert-mb}
  559. \end{center}
  560. \caption{Hilbert curve ordering of macro blocks within a super block}
  561. \label{fig:hilbert-mb}
  562. \end{figure}
  563. Using the same frame size as the example above, there are 15 macro blocks in
  564. each row and 3 rows of macro blocks.
  565. The macro blocks are assigned the following indices:
  566. \vspace{\baselineskip}
  567. \begin{center}
  568. \begin{tabular}{|cc|cc|c|cc|c|}\hline
  569. 30 & 31 & 32 & 33 & $\cdots$ & 42 & 43 & 44 \\\hline
  570. 1 & 2 & 5 & 6 & $\cdots$ & 25 & 26 & 29 \\
  571. 0 & 3 & 4 & 7 & $\cdots$ & 24 & 27 & 28 \\\hline
  572. \end{tabular}
  573. \end{center}
  574. \vspace{\baselineskip}
  575. \section{Coding Modes and Prediction}
  576. Each block is coded using one of a small, fixed set of \term{coding modes} that
  577. define how the block is predicted from previous frames.
  578. A block is predicted using one of two \term{reference frames}, selected
  579. according to the coding mode.
  580. A reference frame is the fully decoded version of a previous frame in the
  581. stream.
  582. The first available reference frame is the previous intra frame, called the
  583. \term{golden frame}.
  584. The second available reference frame is the previous frame, whether it was an
  585. intra frame or an inter frame.
  586. If the previous frame was an intra frame, then both reference frames are the
  587. same.
  588. See Figure~\ref{fig:reference-frames} for an illustration of the reference
  589. frames used for an intra frame that does not follow an intra frame.
  590. \begin{figure}[htbp]
  591. \begin{center}
  592. \includegraphics{reference-frames}
  593. \end{center}
  594. \caption{Example of reference frames for an inter frame}
  595. \label{fig:reference-frames}
  596. \end{figure}
  597. Two coding modes in particular are worth mentioning here.
  598. The INTRA mode is used for blocks that are not predicted from either reference
  599. frame.
  600. This is the only coding mode allowed in intra frames.
  601. The INTER\_NOMV coding mode uses the co-located contents of the block in the
  602. previous frame as the predictor.
  603. This is the default coding mode.
  604. \section{DCT Coefficients}
  605. \label{sec:dct-coeffs}
  606. A \term{residual} is added to the predicted contents of a block to form the
  607. final reconstruction.
  608. The residual is stored as a set of quantized coefficients from an integer
  609. approximation of a two-dimensional Type II Discrete Cosine Transform.
  610. The DCT takes an $8\times 8$ array of pixel values as input and returns an
  611. $8\times 8$ array of coefficient values.
  612. The \term{natural ordering} of these coefficients is defined to be row-major
  613. order, from lowest to highest frequency.
  614. They are also often indexed in \term{zig-zag order}, as shown in
  615. Figure~\ref{tab:zig-zag}.
  616. \begin{figure}[htbp]
  617. \begin{center}
  618. \begin{tabular}[c]{rr|c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c}
  619. &\multicolumn{1}{r}{} & && &&&&&$c$&&& && && \\
  620. &\multicolumn{1}{r}{} &0&&1&&2&&3&&4&&5&&6&&7 \\\cline{3-17}
  621. &0 & 0 &$\rightarrow$& 1 && 5 &$\rightarrow$& 6 && 14 &$\rightarrow$& 15 && 27 &$\rightarrow$& 28 \\[-0.5\defaultaddspace]
  622. & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
  623. &1 & 2 & & 4 && 7 & & 13 && 16 & & 26 && 29 & & 42 \\[-0.5\defaultaddspace]
  624. & &$\downarrow$&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&$\downarrow$ \\
  625. &2 & 3 & & 8 && 12 & & 17 && 25 & & 30 && 41 & & 43 \\[-0.5\defaultaddspace]
  626. & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
  627. &3 & 9 & & 11 && 18 & & 24 && 31 & & 40 && 44 & & 53 \\[-0.5\defaultaddspace]
  628. $r$&&$\downarrow$&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&$\downarrow$ \\
  629. &4 & 10 & & 19 && 23 & & 32 && 39 & & 45 && 52 & & 54 \\[-0.5\defaultaddspace]
  630. & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
  631. &5 & 20 & & 22 && 33 & & 38 && 46 & & 51 && 55 & & 60 \\[-0.5\defaultaddspace]
  632. & &$\downarrow$&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&$\downarrow$ \\
  633. &6 & 21 & & 34 && 37 & & 47 && 50 & & 56 && 59 & & 61 \\[-0.5\defaultaddspace]
  634. & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
  635. &7 & 35 &$\rightarrow$& 36 && 48 &$\rightarrow$& 49 && 57 &$\rightarrow$& 58 && 62 &$\rightarrow$& 63
  636. \end{tabular}
  637. \end{center}
  638. \caption{Zig-zag order}
  639. \label{tab:zig-zag}
  640. \end{figure}
  641. \begin{verse}
  642. {\bf Note:} the row and column indices refer to {\em frequency number} and not
  643. pixel locations.
  644. The frequency numbers are defined independently of the memory organization of
  645. the pixels.
  646. They have been written from top to bottom here to follow conventional notation,
  647. despite the right-handed coordinate system Theora uses for pixel locations.
  648. %RG: I'd rather we were internally consistent and put dc at the lower left.
  649. Many implementations of the DCT operate `in-place'.
  650. That is, they return DCT coefficients in the same memory buffer that the
  651. initial pixel values were stored in.
  652. Due to the right-handed coordinate system used for pixel locations in Theora,
  653. one must note carefully how both pixel values and DCT coefficients are
  654. organized in memory in such a system.
  655. \end{verse}
  656. DCT coefficient $(0,0)$ is called the \term{DC coefficient}.
  657. All the other coefficients are called \term{AC coefficients}.
  658. \chapter{Decoding Overview}
  659. This section provides a high level description of the Theora codec's
  660. construction.
  661. A bit-by-bit specification appears beginning in Section~\ref{sec:bitpacking}.
  662. The later sections assume a high-level understanding of the Theora decode
  663. process, which is provided below.
  664. \section{Decoder Configuration}
  665. Decoder setup consists of configuration of the quantization matrices and the
  666. Huffman codebooks for the DCT coefficients, and a table of limit values for
  667. the deblocking filter.
  668. The remainder of the decoding pipeline is not configurable.
  669. \subsection{Global Configuration}
  670. The global codec configuration consists of a few video related fields, such as
  671. frame rate, frame size, picture size and offset, aspect ratio, color space,
  672. pixel format, and a version number.
  673. The version number is divided into a major version, a minor version, amd a
  674. minor revision number.
  675. %r: afaik the released vp3 codec called itself 3.1 and is compatible w/ theora
  676. %r: even though we received the in-progress 3.2 codebase
  677. For the format defined in this specification, these are `3', `2', and
  678. `1', respectively, in reference to Theora's origin as a successor to
  679. the VP3.1 format.
  680. \subsection{Quantization Matrices}
  681. Theora allows up to 384 different quantization matrices to be defined, one for
  682. each \term{quantization type}, \term{color plane} ($Y'$, $C_b$, or $C_r$), and
  683. \term{quantization index}, \qi, which ranges from zero to 63, inclusive.
  684. There are currently two quantization types defined, which depend on the coding
  685. mode of the block being dequantized, as shown in Table~\ref{tab:quant-types}.
  686. \begin{table}[htbp]
  687. \begin{center}
  688. \begin{tabular}{cl}\toprule
  689. Quantization Type & Usage \\\midrule
  690. $0$ & INTRA-mode blocks \\
  691. $1$ & Blocks in any other mode. \\
  692. \bottomrule\end{tabular}
  693. \end{center}
  694. \caption{Quantization Type Indices}
  695. \label{tab:quant-types}
  696. \end{table}
  697. %r: I think 'nominally' is more specific than 'generally' here
  698. The quantization index, on the other hand, nominally represents a progressive
  699. range of quality levels, from low quality near zero to high quality near 63.
  700. However, the interpretation is arbitrary, and it is possible, for example, to
  701. partition the scale into two completely separate ranges with 32 levels each
  702. that are meant to represent different classes of source material, or any
  703. other arrangement that suits the encoder's requirements.
  704. Each quantization matrix is an $8\times 8$ matrix of 16-bit values, which is
  705. used to quantize the output of the $8\times 8$ DCT\@.
  706. Quantization matrices are specified using three components: a
  707. \term{base matrix} and two \term{scale values}.
  708. The first scale value is the \term{DC scale}, which is applied to the DC
  709. component of the base matrix.
  710. The second scale value is the \term{AC scale}, which is applied to all the
  711. other components of the base matrix.
  712. There are 64 DC scale values and 64 AC scale values, one for each \qi\ value.
  713. There are 64 elements in each base matrix, one for each DCT coefficient.
  714. They are stored in natural order (cf. Section~\ref{sec:dct-coeffs}).
  715. There is a separate set of base matrices for each quantization type and each
  716. color plane, with up to 64 possible base matrices in each set, one for each
  717. \qi\ value.
  718. %r: we will mention that the given matricies must bound the \qi range
  719. %r: in the detailed section. it's not important at this level.
  720. Typically the bitstream contains matrices for only a sparse subset of the
  721. possible \qi\ values.
  722. The base matrices for the remainder of the \qi\ values are computed using
  723. linear interpolation.
  724. This configuration allows the encoder to adjust the quantization matrices to
  725. approximate the complex, non-linear response of the human visual system to
  726. different quantization errors.
  727. Finally, because the in-loop deblocking filter strength depends on the strength
  728. of the quantization matrices defined in this header, a table of 64 \term{loop
  729. filter limit values} is defined, one for each \qi\ value.
  730. The precise specification of how all of this information is decoded appears in
  731. Section~\ref{sub:loop-filter-limits} and Section~\ref{sub:quant-params}.
  732. \subsection{Huffman Codebooks}
  733. Theora uses 80 configurable binary Huffman codes to represent the 32 tokens
  734. used to encode DCT coefficients.
  735. Each of the 32 token values has a different semantic meaning and is used to
  736. represent single coefficient values, zero runs, combinations of the two, and
  737. \term{End-Of-Block markers}.
  738. The 80 codes are divided up into five groups of 16, with each group
  739. corresponding to a set of DCT coefficient indices.
  740. The first group corresponds to the DC coefficient, while the remaining four
  741. groups correspond to different subsets of the AC coefficients.
  742. Within each frame, two pairs of 4-bit codebook indices are stored.
  743. The first pair selects which codebooks to use from the DC coefficient group for
  744. the $Y'$ coefficients and the $C_b$ and $C_r$ coefficients.
  745. The second pair selects which codebooks to use from {\em all four} of the AC
  746. coefficient groups for the $Y'$ coefficients and the $C_b$ and $C_r$
  747. coefficients.
  748. The precise specification of how the codebooks are decoded appears in
  749. Section~\ref{sub:huffman-tables}.
  750. \section{High-Level Decode Process}
  751. \subsection{Decoder Setup}
  752. Before decoding can begin, a decoder MUST be initialized using the bitstream
  753. headers corresponding to the stream to be decoded.
  754. Theora uses three header packets; all are required, in order, by this
  755. specification.
  756. Once set up, decode may begin at any intra-frame packet---or even inter-frame
  757. packets, provided the appropriate decoded reference frames have already been
  758. decoded and cached---belonging to the Theora stream.
  759. In Theora I, all packets after the three initial headers are intra-frame or
  760. inter-frame packets.
  761. The header packets are, in order, the identification header, the comment
  762. header, and the setup header.
  763. \paragraph{Identification Header}
  764. The identification header identifies the stream as Theora, provides a version
  765. number, and defines the characteristics of the video stream such as frame
  766. size.
  767. A complete description of the identification header appears in
  768. Section~\ref{sec:idheader}.
  769. \paragraph{Comment Header}
  770. The comment header includes user text comments (`tags') and a vendor string
  771. for the application/library that produced the stream.
  772. The format of the comment header is the same as that used in the Vorbis I and
  773. Speex codecs, with slight modifications due to the use of a different bit
  774. packing mechanism.
  775. A complete description of how the comment header is coded appears in
  776. Section~\ref{sec:commentheader}, along with a suggested set of tags.
  777. \paragraph{Setup Header}
  778. The setup header includes extensive codec setup information, including the
  779. complete set of quantization matrices and Huffman codebooks needed to decode
  780. the DCT coefficients.
  781. A complete description of the setup header appears in
  782. Section~\ref{sec:setupheader}.
  783. \subsection{Decode Procedure}
  784. The decoding and synthesis procedure for all video packets is fundamentally the
  785. same, with some steps omitted for intra frames.
  786. \begin{itemize}
  787. \item
  788. Decode packet type flag.
  789. \item
  790. Decode frame header.
  791. \item
  792. Decode coded block information (inter frames only).
  793. \item
  794. Decode macro block mode information (inter frames only).
  795. \item
  796. Decode motion vectors (inter frames only).
  797. \item
  798. Decode block-level \qi\ information.
  799. \item
  800. Decode DC coefficient for each coded block.
  801. \item
  802. Decode 1st AC coefficient for each coded block.
  803. \item
  804. Decode 2nd AC coefficient for each coded block.
  805. \item
  806. $\ldots$
  807. \item
  808. Decode 63rd AC coefficient for each coded block.
  809. \item Perform DC coefficient prediction.
  810. \item Reconstruct coded blocks.
  811. \item Copy uncoded bocks.
  812. \item Perform loop filtering.
  813. \end{itemize}
  814. \begin{verse}
  815. {\bf Note:} clever rearrangement of the steps in this process is possible.
  816. As an example, in a memory-constrained environment, one can make multiple
  817. passes through the DCT coefficients to avoid buffering them all in memory.
  818. On the first pass, the starting location of each coefficient is identified, and
  819. then 64 separate get pointers are used to read in the 64 DCT coefficients
  820. required to reconstruct each coded block in sequence.
  821. This operation produces entirely equivalent output and is naturally perfectly
  822. legal.
  823. It may even be a benefit in non-memory-constrained environments due to a
  824. reduced cache footprint.
  825. \end{verse}
  826. Theora makes equivalence easy to check by defining all decoding operations in
  827. terms of exact integer operations.
  828. No floating-point math is required, and in particular, the implementation of
  829. the iDCT transform MUST be followed precisely.
  830. This prevents the decoder mismatch problem commonly associated with codecs that
  831. provide a less rigorous transform specification.
  832. Such a mismatch problem would be devastating to Theora, since a single rounding
  833. error in one frame could propagate throughout the entire succeeding frame due
  834. to DC prediction.
  835. \paragraph{Packet Type Decode}
  836. Theora uses four packet types.
  837. The first three packet types mark each of the three Theora headers described
  838. above.
  839. The fourth packet type marks a video packet.
  840. All other packet types are reserved; packets marked with a reserved type should
  841. be ignored.
  842. Additionally, zero-length packets are treated as if they were an inter
  843. frame with no blocks coded. That is, as a duplicate frame.
  844. \paragraph{Frame Header Decode}
  845. The frame header contains some global information about the current frame.
  846. The first is the frame type field, which specifies if this is an intra frame or
  847. an inter frame.
  848. Inter frames predict their contents from previously decoded reference frames.
  849. Intra frames can be independently decoded with no established reference frames.
  850. The next piece of information in the frame header is the list of \qi\ values
  851. allowed in the frame.
  852. Theora allows from one to three different \qi\ values to be used in a single
  853. frame, each of which selects a set of six quantization matrices, one for each
  854. quantization type (inter or intra), and one for each color plane.
  855. The first \qi\ value is {\em always} used when dequantizing DC coefficients.
  856. The \qi\ value used when dequantizing AC coefficients, however, can vary from
  857. block to block.
  858. VP3, in contrast, only allows a single \qi\ value per frame for both the DC and
  859. AC coefficients.
  860. \paragraph{Coded Block Information}
  861. This stage determines which blocks in the frame are coded and which are
  862. uncoded.
  863. A \term{coded block list} is constructed which lists all the coded blocks in
  864. coded order.
  865. For intra frames, every block is coded, and so no data needs to be read from
  866. the packet.
  867. \paragraph{Macro Block Mode Information}
  868. For intra frames, every block is coded in INTRA mode, and this stage is
  869. skipped.
  870. In inter frames a \term{coded macro block list} is constructed from the coded
  871. block list.
  872. Any macro block which has at least one of its luma blocks coded is considered
  873. coded; all other macro blocks are uncoded, even if they contain coded chroma
  874. blocks.
  875. A coding mode is decoded for each coded macro block, and assigned to all its
  876. constituent coded blocks.
  877. All coded chroma blocks in uncoded macro blocks are assigned the INTER\_NOMV
  878. coding mode.
  879. \paragraph{Motion Vectors}
  880. Intra frames are coded entirely in INTRA mode, and so this stage is skipped.
  881. Some inter coding modes, however, require one or more motion vectors to be
  882. specified for each macro block.
  883. These are decoded in this stage, and an appropriate motion vector is assigned
  884. to each coded block in the macro block.
  885. \paragraph{Block-Level \qi\ Information}
  886. If a frame allows multiple \qi\ values, the \qi\ value assigned to each block
  887. is decoded here.
  888. Frames that use only a single \qi\ value have nothing to decode.
  889. \paragraph{DCT Coefficients}
  890. Finally, the quantized DCT coefficients are decoded.
  891. A list of DCT coefficients in zig-zag order for a single block is represented
  892. by a list of tokens.
  893. A token can take on one of 32 different values, each with a different semantic
  894. meaning.
  895. A single token can represent a single DCT coefficient, a run of zero
  896. coefficients within a single block, a combination of a run of zero
  897. coefficients followed by a single non-zero coefficient, an
  898. \term{End-Of-Block marker}, or a run of EOB markers.
  899. EOB markers signify that the remainder of the block is one long zero run.
  900. Unlike JPEG and MPEG, there is no requirement for each block to end with
  901. a special marker.
  902. If non-EOB tokens yield values for all 64 of the coefficients in a block, then
  903. no EOB marker occurs.
  904. Each token is associated with a specific \term{token index} in a block.
  905. For single-coefficient tokens, this index is the zig-zag index of the token in
  906. the block.
  907. For zero-run tokens, this index is the zig-zag index of the {\em first}
  908. coefficient in the run.
  909. For combination tokens, the index is again the zig-zag index of the first
  910. coefficient in the zero run.
  911. For EOB markers, which signify that the remainder of the block is one long zero
  912. run, the index is the zig-zag index of the first zero coefficient in that run.
  913. For EOB runs, the token index is that of the first EOB marker in the run.
  914. Due to zero runs and EOB markers, a block does not have to have a token for
  915. every zig-zag index.
  916. Tokens are grouped in the stream by token index, not by the block they
  917. originate from.
  918. This means that for each zig-zag index in turn, the tokens with that index from
  919. {\em all} the coded blocks are coded in coded block order.
  920. When decoding, a current token index is maintained for each coded block.
  921. This index is advanced by the number of coefficients that are added to the
  922. block as each token is decoded.
  923. After fully decoding all the tokens with token index \ti, the current token
  924. index of every coded block will be \ti\ or greater.
  925. If an EOB run of $n$ blocks is decoded at token index \ti, then it ends the
  926. next $n$ blocks in coded block order whose current token index is equal to
  927. \ti, but not greater.
  928. If there are fewer than $n$ blocks with a current token index of \ti, then the
  929. decoder goes through the coded block list again from the start, ending blocks
  930. with a current token index of $\ti+1$, and so on, until $n$ blocks have been
  931. ended.
  932. Tokens are read by parsing a Huffman code that depends on \ti\ and the color
  933. plane of the next coded block whose current token index is equal to \ti, but
  934. not greater.
  935. The Huffman codebooks are selected on a per-frame basis from the 80 codebooks
  936. defined in the setup header.
  937. Many tokens have a fixed number of \term{extra bits} associated with them.
  938. These bits are read from the packet immediately after the token is decoded.
  939. These are used to define things such as coefficient magnitude, sign, and the
  940. length of runs.
  941. \paragraph{DC Prediction}
  942. After the coefficients for each block are decoded, the quantized DC value of
  943. each block is adjusted based on the DC values of its neighbors.
  944. This adjustment is performed by scanning the blocks in raster order, not coded
  945. block order.
  946. \paragraph{Reconstruction}
  947. Finally, using the coding mode, motion vector (if applicable), quantized
  948. coefficient list, and \qi\ value defined for each block, all the coded blocks
  949. are reconstructed.
  950. The DCT coefficients are dequantized, an inverse DCT transform is applied, and
  951. the predictor is formed from the coding mode and motion vector and added to
  952. the result.
  953. \paragraph{Loop Filtering}
  954. To complete the reconstructed frame, an ``in-loop'' deblocking filter is
  955. applied to the edges of all coded blocks.
  956. \chapter{Video Formats}
  957. This section gives a precise description of the video formats that Theora is
  958. capable of storing.
  959. The Theora bitstream is capable of handling video at any arbitrary resolution
  960. up to $1048560\times 1048560$.
  961. Such video would require almost three terabytes of storage per frame for
  962. uncompressed data, so compliant decoders MAY refuse to decode images with
  963. sizes beyond their capabilities.
  964. %TODO: What MUST a "compliant" decoder accept?
  965. %TODO: What SHOULD a decoder use for an upper bound? (derive from total amount
  966. %TODO: of memory and memory bandwidth)
  967. %TODO: Any lower limits?
  968. %TODO: We really need hardware device profiles, but such things should be
  969. %TODO: developed with input from the hardware community.
  970. %TODO: And even then sometimes they're useless
  971. The remainder of this section talks about two specific aspects of the video
  972. format: the color space and the pixel format.
  973. The first describes how color is represented and how to transform that color
  974. representation into a device independent color space such as CIE $XYZ$ (1931).
  975. The second describes the various schemes for sampling the color values in time
  976. and space.
  977. \section{Color Space Conventions}
  978. There are a large number of different color standards used in digital video.
  979. Since Theora is a lossy codec, it restricts itself to only a few of them to
  980. simplify playback.
  981. Unlike the alternate method of describing all the parameters of the color
  982. model, this allows a few dedicated routines for color conversion to be written
  983. and heavily optimized in a decoder.
  984. More flexible conversion functions should instead be specified in an encoder,
  985. where additional computational complexity is more easily tolerated.
  986. The color spaces were selected to give a fair representation of color standards
  987. in use around the world today.
  988. Most of the standards that do not exactly match one of these can be converted
  989. to one fairly easily.
  990. All Theora color spaces are $Y'C_bC_r$ color spaces with one luma channel and
  991. two chroma channels.
  992. Each channel contains 8-bit discrete values in the range $0\ldots255$, which
  993. represent non-linear gamma pre-corrected signals.
  994. The Theora identification header contains an 8-bit value that describes the
  995. color space.
  996. This merely selects one of the color spaces available from an enumerated list.
  997. Currently, only two color spaces are defined, with a third possibility that
  998. indicates the color space is ``unknown".
  999. \section{Color Space Conversions and Parameters}
  1000. \label{sec:color-xforms}
  1001. The parameters which describe the conversions between each color space are
  1002. listed below.
  1003. These are the parameters needed to map colors from the encoded $Y'C_bC_r$
  1004. representation to the device-independent color space CIE $XYZ$ (1931).
  1005. These parameters define abstract mathematical conversion functions which are
  1006. infinitely precise.
  1007. The accuracy and precision with which the conversions are performed in a real
  1008. system is determined by the quality of output desired and the available
  1009. processing power.
  1010. Exact decoder output is defined by this specification only in the original
  1011. $Y'C_bC_r$ space.
  1012. \begin{description}
  1013. \item[$Y'C_bC_r$ to $Y'P_bP_r$:]
  1014. \vspace{\baselineskip}\hfill
  1015. This conversion takes 8-bit discrete values in the range $[0\ldots255]$ and
  1016. maps them to real values in the range $[0\ldots1]$ for Y and
  1017. $[-\frac{1}{2}\ldots\frac{1}{2}]$ for $P_b$ and $P_r$.
  1018. Because some values may fall outside the offset and excursion defined for each
  1019. channel in the $Y'C_bC_r$ space, the results may fall outside these ranges in
  1020. $Y'P_bP_r$ space.
  1021. No clamping should be done at this stage.
  1022. \begin{align}
  1023. Y'_\mathrm{out} & =
  1024. \frac{Y'_\mathrm{in}-\mathrm{Offset}_Y}{\mathrm{Excursion}_Y} \\
  1025. P_b & =
  1026. \frac{C_b-\mathrm{Offset}_{C_b}}{\mathrm{Excursion}_{C_b}} \\
  1027. P_r & =
  1028. \frac{C_r-\mathrm{Offset}_{C_r}}{\mathrm{Excursion}_{C_r}}
  1029. \end{align}
  1030. Parameters: $\mathrm{Offset}_{Y,C_b,C_r}$, $\mathrm{Excursion}_{Y,C_b,C_r}$.
  1031. \item[$Y'P_bP_r$ to $R'G'B'$:]
  1032. \vspace{\baselineskip}\hfill
  1033. This conversion takes the one luma and two chroma channel representation and
  1034. maps it to the non-linear $R'G'B'$ space used to drive actual output devices.
  1035. Values should be clamped into the range $[0\ldots1]$ after this stage.
  1036. \begin{align}
  1037. R' & = Y'+2(1-K_r)P_r \\
  1038. 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\\
  1039. B' & = Y'+2(1-K_b)P_b
  1040. \end{align}
  1041. Parameters: $K_b,K_r$.
  1042. \item[$R'G'B'$ to $RGB$ (Output device gamma correction):]
  1043. \vspace{\baselineskip}\hfill
  1044. This conversion takes the non-linear $R'G'B'$ voltage levels and maps them to
  1045. linear light levels produced by the actual output device.
  1046. Note that this conversion is only that of the output device, and its inverse is
  1047. {\em not} that used by the input device.
  1048. Because a dim viewing environment is assumed in most television standards, the
  1049. overall gamma between the input and output devices is usually around $1.1$ to
  1050. $1.2$, and not a strict $1.0$.
  1051. For calibration with actual output devices, the model
  1052. \begin{align}
  1053. L & =(E'+\Delta)^\gamma
  1054. \end{align}
  1055. should be used, with $\Delta$ the free parameter and $\gamma$ held fixed to
  1056. the value specified in this document.
  1057. The conversion function presented here is an idealized version with $\Delta=0$.
  1058. \begin{align}
  1059. R & = R'^\gamma \\
  1060. G & = G'^\gamma \\
  1061. B & = B'^\gamma
  1062. \end{align}
  1063. Parameters: $\gamma$.
  1064. \item[$RGB$ to $R'G'B'$ (Input device gamma correction):]
  1065. \vspace{\baselineskip}\hfill
  1066. %TODO: Tag section as non-normative
  1067. This conversion takes linear light levels and maps them to the non-linear
  1068. voltage levels produced in the actual input device.
  1069. This information is merely informative.
  1070. It is not required for building a decoder or for converting between the various
  1071. formats and the actual output capabilities of a particular device.
  1072. A linear segment is introduced on the low end to reduce noise in dark areas of
  1073. the image.
  1074. The rest of the scale is adjusted so that the power segment of the curve
  1075. intersects the linear segment with the proper slope, and so that it still maps
  1076. 0 to 0 and 1 to 1.
  1077. \begin{align}
  1078. R' & = \left\{
  1079. \begin{array}{ll}
  1080. \alpha R, & 0\le R<\delta \\
  1081. (1+\epsilon)R^\beta-\epsilon, & \delta\le R\le1
  1082. \end{array}\right. \\
  1083. G' & = \left\{
  1084. \begin{array}{ll}
  1085. \alpha G, & 0\le G<\delta \\
  1086. (1+\epsilon)G^\beta-\epsilon, & \delta\le G\le1
  1087. \end{array}\right. \\
  1088. B' & = \left\{
  1089. \begin{array}{ll}
  1090. \alpha B, & 0\le B<\delta \\
  1091. (1+\epsilon)B^\beta-\epsilon, & \delta\le B\le1
  1092. \end{array}\right.
  1093. \end{align}
  1094. Parameters: $\beta$, $\alpha$, $\delta$, $\epsilon$.
  1095. \item[$RGB$ to CIE $XYZ$ (1931):]
  1096. \vspace{\baselineskip}\hfill
  1097. This conversion maps a device-dependent linear RGB space to the
  1098. device-independent linear CIE $XYZ$ space.
  1099. The parameters are the CIE chromaticity coordinates of the three
  1100. primaries---red, green, and blue---as well as the chromaticity coordinates
  1101. of the white point of the device.
  1102. This is how hardware manufacturers and standards typically describe a
  1103. particular $RGB$ space.
  1104. The math required to convert these parameters into a useful transformation
  1105. matrix is reproduced below.
  1106. \begin{align}
  1107. F & =
  1108. \left[\begin{array}{ccc}
  1109. \frac{x_r}{y_r} & \frac{x_g}{y_g} & \frac{x_b}{y_b} \\
  1110. 1 & 1 & 1 \\
  1111. \frac{1-x_r-y_r}{y_r} & \frac{1-x_g-y_g}{y_g} & \frac{1-x_b-y_b}{y_b}
  1112. \end{array}\right] \\
  1113. \left[\begin{array}{c}
  1114. s_r \\
  1115. s_g \\
  1116. s_b
  1117. \end{array}\right] & =
  1118. F^{-1}\left[\begin{array}{c}
  1119. \frac{x_w}{y_w} \\
  1120. 1 \\
  1121. \frac{1-x_w-y_w}{y_w}
  1122. \end{array}\right] \\
  1123. \left[\begin{array}{c}
  1124. X \\
  1125. Y \\
  1126. Z
  1127. \end{array}\right] & =
  1128. F\left[\begin{array}{c}
  1129. s_rR \\
  1130. s_gG \\
  1131. s_bB
  1132. \end{array}\right]
  1133. \end{align}
  1134. Parameters: $x_r,x_g,x_b,x_w, y_r,y_g,y_b,y_w$.
  1135. \end{description}
  1136. \section{Available Color Spaces}
  1137. \label{sec:colorspaces}
  1138. These are the color spaces currently defined for use by Theora video.
  1139. Each one has a short name, with which it is referred to in this document, and
  1140. a more detailed specification of the standards from which its parameters are
  1141. derived.
  1142. Some standards do not specify all the parameters necessary.
  1143. For these unspecified parameters, this document serves as the definition of
  1144. what should be used when encoding or decoding Theora video.
  1145. \subsection{Rec.~470M (Rec.~ITU-R~BT.470-6 System M/NTSC with
  1146. Rec.~ITU-R~BT.601-5)}
  1147. \label{sec:470m}
  1148. This color space is used by broadcast television and DVDs in much of the
  1149. Americas, Japan, Korea, and the Union of Myanmar \cite{rec470}.
  1150. This color space may also be used for System M/PAL (Brazil), with an
  1151. appropriate conversion supplied by the encoder to compensate for the
  1152. different gamma value.
  1153. See Section~\ref{sec:470bg} for an appropriate gamma value to assume for M/PAL
  1154. input.
  1155. In the US, studio monitors are adjusted to a D65 white point
  1156. ($x_w,y_w=0.313,0.329$).
  1157. In Japan, studio monitors are adjusted to a D white of 9300K
  1158. ($x_w,y_w=0.285,0.293$).
  1159. Rec.~470 does not specify a digital encoding of the color signals.
  1160. For Theora, Rec.~ITU-R~BT.601-5 \cite{rec601} is used, starting from the
  1161. $R'G'B'$ signals specified by Rec.~470.
  1162. Rec.~470 does not specify an input gamma function.
  1163. For Theora, the Rec.~709 \cite{rec709} input function is assumed.
  1164. This is the same as that specified by SMPTE 170M \cite{smpte170m}, which claims
  1165. to reflect modern practice in the creation of NTSC signals circa 1994.
  1166. The parameters for all the color transformations defined in
  1167. Section~\ref{sec:color-xforms} are given in Table~\ref{tab:470m}.
  1168. \begin{table}[htb]
  1169. \begin{align*}
  1170. \mathrm{Offset}_{Y,C_b,C_r} & = (16, 128, 128) \\
  1171. \mathrm{Excursion}_{Y,C_b,C_r} & = (219, 224, 224) \\
  1172. K_r & = 0.299 \\
  1173. K_b & = 0.114 \\
  1174. \gamma & = 2.2 \\
  1175. \beta & = 0.45 \\
  1176. \alpha & = 4.5 \\
  1177. \delta & = 0.018 \\
  1178. \epsilon & = 0.099 \\
  1179. x_r,y_r & = 0.67, 0.33 \\
  1180. x_g,y_g & = 0.21, 0.71 \\
  1181. x_b,y_b & = 0.14, 0.08 \\
  1182. \text{(Illuminant C) } x_w,y_w & = 0.310, 0.316 \\
  1183. \end{align*}
  1184. \caption{Rec.~470M Parameters}
  1185. \label{tab:470m}
  1186. \end{table}
  1187. \subsection{Rec.~470BG (Rec.~ITU-R~BT.470-6 Systems B and G with
  1188. Rec.~ITU-R~BT.601-5)}
  1189. \label{sec:470bg}
  1190. This color space is used by the PAL and SECAM systems in much of the rest of
  1191. the world \cite{rec470}
  1192. This can be used directly by systems (B, B1, D, D1, G, H, I, K, N)/PAL and (B,
  1193. D, G, H, K, K1, L)/SECAM\@.
  1194. \begin{verse}
  1195. {\bf Note:} the Rec.~470BG chromaticity values are different from those
  1196. specified in Rec.~470M\@.
  1197. When PAL and SECAM systems were first designed, they were based upon the same
  1198. primaries as NTSC\@.
  1199. However, as methods of making color picture tubes have changed, the primaries
  1200. used have changed as well.
  1201. The U.S. recommends using correction circuitry to approximate the existing,
  1202. standard NTSC primaries.
  1203. Current PAL and SECAM systems have standardized on primaries in accord with
  1204. more recent technology.
  1205. \end{verse}
  1206. Rec.~470 provisionally permits the use of the NTSC chromaticity values (given
  1207. in Section~\ref{sec:470m}) with legacy PAL and SECAM equipment.
  1208. In Theora, material must be decoded assuming the new PAL and SECAM primaries.
  1209. Material intended for display on old legacy devices should be converted by the
  1210. decoder.
  1211. The official Rec.~470BG specifies a gamma value of $\gamma=2.8$.
  1212. However, in practice this value is unrealistically high \cite{Poyn97}.
  1213. Rec.~470BG states that the overall system gamma should be approximately
  1214. $\gamma\beta=1.2$.
  1215. Since most cameras pre-correct with a gamma value of $\beta=0.45$,
  1216. this suggests an output device gamma of approximately $\gamma=2.67$.
  1217. This is the value recommended for use with PAL systems in Theora.
  1218. Rec.~470 does not specify a digital encoding of the color signals.
  1219. For Theora, Rec.~ITU-R~BT.601-5 \cite{rec601} is used, starting from the
  1220. $R'G'B'$ signals specified by Rec.~470.
  1221. Rec.~470 does not specify an input gamma function.
  1222. For Theora, the Rec 709 \cite{rec709} input function is assumed.
  1223. The parameters for all the color transformations defined in
  1224. Section~\ref{sec:color-xforms} are given in Table~\ref{tab:470bg}.
  1225. \begin{table}[htb]
  1226. \begin{align*}
  1227. \mathrm{Offset}_{Y,C_b,C_r} & = (16, 128, 128) \\
  1228. \mathrm{Excursion}_{Y,C_b,C_r} & = (219, 224, 224) \\
  1229. K_r & = 0.299 \\
  1230. K_b & = 0.114 \\
  1231. \gamma & = 2.67 \\
  1232. \beta & = 0.45 \\
  1233. \alpha & = 4.5 \\
  1234. \delta & = 0.018 \\
  1235. \epsilon & = 0.099 \\
  1236. x_r,y_r & = 0.64, 0.33 \\
  1237. x_g,y_g & = 0.29, 0.60 \\
  1238. x_b,y_b & = 0.15, 0.06 \\
  1239. \text{(D65) } x_w,y_w & = 0.313, 0.329 \\
  1240. \end{align*}
  1241. \caption{Rec.~470BG Parameters}
  1242. \label{tab:470bg}
  1243. \end{table}
  1244. \section{Pixel Formats}
  1245. \label{sec:pixfmts}
  1246. Theora supports several different pixel formats, each of which uses different
  1247. subsampling for the chroma planes relative to the luma plane.
  1248. A decoder may need to recover a full resolution chroma plane with samples
  1249. co-sited with the luma plane in order to convert to RGB for display or perform
  1250. other processing.
  1251. Decoders can assume that the chroma signal satisfies the Nyquist-Shannon
  1252. sampling theorem.
  1253. The ideal low-pass reconstruction filter this implies is not practical, but any
  1254. suitable approximation can be used, depending on the available computing
  1255. power.
  1256. Decoders MAY simply use a box filter, assigning to each luma sample the chroma
  1257. sample closest to it.
  1258. Encoders would not go wrong in assuming that this will be the most common
  1259. approach.
  1260. \subsection{4:4:4 Subsampling}
  1261. \label{sec:444}
  1262. All three color planes are stored at full resolution---each pixel has a $Y'$,
  1263. a $C_b$ and a $C_r$ value (see Figure~\ref{fig:pixel444}).
  1264. The samples in the different planes are all at co-located sites.
  1265. \begin{figure}[htbp]
  1266. \begin{center}
  1267. \includegraphics{pixel444}
  1268. \end{center}
  1269. \caption{Pixels encoded 4:4:4}
  1270. \label{fig:pixel444}
  1271. \end{figure}
  1272. % Figure.
  1273. %YRB YRB
  1274. %
  1275. %
  1276. %
  1277. %YRB YRB
  1278. %
  1279. %
  1280. %
  1281. \subsection{4:2:2 Subsampling}
  1282. \label{sec:422}
  1283. The $C_b$ and $C_r$ planes are stored with half the horizontal resolution of
  1284. the $Y'$ plane.
  1285. Thus, each of these planes has half the number of horizontal blocks as the luma
  1286. plane (see Figure~\ref{fig:pixel422}).
  1287. Similarly, they have half the number of horizontal super blocks, rounded up.
  1288. Macro blocks are defined across color planes, and so their number does not
  1289. change, but each macro block contains half as many chroma blocks.
  1290. The chroma samples are vertically aligned with the luma samples, but
  1291. horizontally centered between two luma samples.
  1292. Thus, each luma sample has a unique closest chroma sample.
  1293. A horizontal phase shift may be required to produce signals which use different
  1294. horizontal chroma sampling locations for compatibility with different systems.
  1295. \begin{figure}[htbp]
  1296. \begin{center}
  1297. \includegraphics{pixel422}
  1298. \end{center}
  1299. \caption{Pixels encoded 4:2:2}
  1300. \label{fig:pixel422}
  1301. \end{figure}
  1302. % Figure.
  1303. %Y RB Y Y RB Y
  1304. %
  1305. %
  1306. %
  1307. %Y RB Y Y RB Y
  1308. %
  1309. %
  1310. %
  1311. \subsection{4:2:0 Subsampling}
  1312. \label{sec:420}
  1313. The $C_b$ and $C_r$ planes are stored with half the horizontal and half the
  1314. vertical resolution of the $Y'$ plane.
  1315. Thus, each of these planes has half the number of horizontal blocks and half
  1316. the number of vertical blocks as the luma plane, for a total of one quarter
  1317. the number of blocks (see Figure~\ref{fig:pixel420}).
  1318. Similarly, they have half the number of horizontal super blocks and half the
  1319. number of vertical super blocks, rounded up.
  1320. Macro blocks are defined across color planes, and so their number does not
  1321. change, but each macro block contains within it one quarter as many
  1322. chroma blocks.
  1323. The chroma samples are vertically and horizontally centered between four luma
  1324. samples.
  1325. Thus, each luma sample has a unique closest chroma sample.
  1326. This is the same sub-sampling pattern used with JPEG, MJPEG, and MPEG-1, and
  1327. was inherited from VP3.
  1328. A horizontal or vertical phase shift may be required to produce signals which
  1329. use different chroma sampling locations for compatibility with different
  1330. systems.
  1331. \begin{figure}[htbp]
  1332. \begin{center}
  1333. \includegraphics{pixel420}
  1334. \end{center}
  1335. \caption{Pixels encoded 4:2:0}
  1336. \label{fig:pixel420}
  1337. \end{figure}
  1338. % Figure.
  1339. %Y Y Y Y
  1340. %
  1341. % RB RB
  1342. %
  1343. %Y Y Y Y
  1344. %
  1345. %
  1346. %
  1347. %Y Y Y Y
  1348. %
  1349. % RB RB
  1350. %
  1351. %Y Y Y Y
  1352. %
  1353. %
  1354. %
  1355. \subsection{Subsampling and the Picture Region}
  1356. Although the frame size must be an integral number of macro blocks, and thus
  1357. both the number of pixels and the number of blocks in each direction must be
  1358. even, no such requirement is made of the picture region.
  1359. Thus, when using subsampled pixel formats, careful attention must be paid to
  1360. which chroma samples correspond to which luma samples.
  1361. As mentioned above, for each pixel format, there is a unique chroma sample that
  1362. is the closest to each luma sample.
  1363. When cropping the chroma planes to the picture region, all the chroma samples
  1364. corresponding to a luma sample in the cropped picture region must be included.
  1365. Thus, when dividing the width or height of the picture region by two to obtain
  1366. the size of the subsampled chroma planes, they must be rounded up.
  1367. Furthermore, the sampling locations are defined relative to the frame,
  1368. {\em not} the picture region.
  1369. When using the 4:2:2 and 4:2:0 formats, the locations of chroma samples
  1370. relative to the luma samples depends on whether or not the X offset of the
  1371. picture region is odd.
  1372. If the offset is even, each column of chroma samples corresponds to two columns
  1373. of luma samples (see Figure~\ref{fig:pic_even} for an example).
  1374. The only exception is if the width is odd, in which case the last column
  1375. corresponds to only one column of luma samples (see Figure~\ref{fig:pic_even_odd}).
  1376. If the offset is odd, then the first column of chroma samples corresponds to
  1377. only one column of luma samples, while the remaining columns each correspond
  1378. to two (see Figure~\ref{fig:pic_odd}).
  1379. In this case, if the width is even, the last column again corresponds to only
  1380. one column of luma samples (see Figure~\ref{fig:pic_odd_even}).
  1381. A similar process is followed with the rows of a picture region of odd height
  1382. encoded in the 4:2:0 format.
  1383. If the Y offset is even, each row of chroma samples corresponds to two rows of
  1384. luma samples (see Figure~\ref{fig:pic_even}), except with an odd height, where
  1385. the last row corresponds to one row of chroma luna samples only (see
  1386. Figure~\ref{fig:pic_even_odd}).
  1387. If the offset is odd, then it is the first row of chroma samples which
  1388. corresponds to only one row of luma samples, while the remaining rows each
  1389. correspond to two (Figure~\ref{fig:pic_odd}), except with an even height,
  1390. where the last row also corresponds to one (Figure~\ref{fig:pic_odd_even}).
  1391. Encoders should be aware of these differences in the subsampling when using an
  1392. even or odd offset.
  1393. In the typical case, with an even width and height, where one expects two rows
  1394. or columns of luma samples for every row or column of chroma samples, the
  1395. encoder must take care to ensure that the offsets used are both even.
  1396. \begin{figure}[htbp]
  1397. \begin{center}
  1398. \includegraphics[width=\textwidth]{pic_even}
  1399. \end{center}
  1400. \caption{Pixel correspondence between color planes with even picture
  1401. offset and even picture size}
  1402. \label{fig:pic_even}
  1403. \end{figure}
  1404. \begin{figure}[htbp]
  1405. \begin{center}
  1406. \includegraphics[width=\textwidth]{pic_even_odd}
  1407. \end{center}
  1408. \caption{Pixel correspondence with even picture offset and
  1409. odd picture size}
  1410. \label{fig:pic_even_odd}
  1411. \end{figure}
  1412. \begin{figure}[htbp]
  1413. \begin{center}
  1414. \includegraphics[width=\textwidth]{pic_odd}
  1415. \end{center}
  1416. \caption{Pixel correspondence with odd picture offset and
  1417. odd picture size}
  1418. \label{fig:pic_odd}
  1419. \end{figure}
  1420. \begin{figure}[htbp]
  1421. \begin{center}
  1422. \includegraphics[width=\textwidth]{pic_odd_even}
  1423. \end{center}
  1424. \caption{Pixel correspondence with odd picture offset and
  1425. even picture size}
  1426. \label{fig:pic_odd_even}
  1427. \end{figure}
  1428. \chapter{Bitpacking Convention}
  1429. \label{sec:bitpacking}
  1430. \section{Overview}
  1431. The Theora codec uses relatively unstructured raw packets containing
  1432. binary integer fields of arbitrary width.
  1433. Logically, each packet is a bitstream in which bits are written one-by-one by
  1434. the encoder and then read one-by-one in the same order by the decoder.
  1435. Most current binary storage arrangements group bits into a native storage unit
  1436. of eight bits (octets), sixteen bits, thirty-two bits, or less commonly other
  1437. fixed sizes.
  1438. The Theora bitpacking convention specifies the correct mapping of the logical
  1439. packet bitstream into an actual representation in fixed-width units.
  1440. \subsection{Octets and Bytes}
  1441. In most contemporary architectures, a `byte' is synonymous with an `octect',
  1442. that is, eight bits.
  1443. For purposes of the bitpacking convention, a byte implies the smallest native
  1444. integer storage representation offered by a platform.
  1445. Modern file systems invariably offer bytes as the fundamental atom of storage.
  1446. The most ubiquitous architectures today consider a `byte' to be an octet.
  1447. Note, however, that the Theora bitpacking convention is still well defined for
  1448. any native byte size; an implementation can use the native bit-width of a
  1449. given storage system.
  1450. This document assumes that a byte is one octet for purposes of example only.
  1451. \subsection{Words and Byte Order}
  1452. A `word' is an integer size that is a grouped multiple of the byte size.
  1453. Most architectures consider a word to be a group of two, four, or eight bytes.
  1454. Each byte in the word can be ranked by order of `significance', e.g.\ the
  1455. significance of the bits in each byte when storing a binary integer in the
  1456. word.
  1457. Several byte orderings are possible in a word.
  1458. The common ones are
  1459. \begin{itemize}
  1460. \item{Big-endian:}
  1461. in which the most significant byte comes first, e.g.\ 3-2-1-0,
  1462. \item{Little-endian:}
  1463. in which the least significant byte comes first, e.g.\ 0-1-2-3, and
  1464. \item{Mixed-endian:}
  1465. one of the less-common orderings that cannot be put into the above two
  1466. categories, e.g.\ 3-1-2-0 or 0-2-1-3.
  1467. \end{itemize}
  1468. The Theora bitpacking convention specifies storage and bitstream manipulation
  1469. at the byte, not word, level.
  1470. Thus host word ordering is of a concern only during optimization, when writing
  1471. code that operates on a word of storage at a time rather than a byte.
  1472. Logically, bytes are always encoded and decoded in order from byte zero through
  1473. byte $n$.
  1474. \subsection{Bit Order}
  1475. A byte has a well-defined `least significant' bit (LSb), which is the only bit
  1476. set when the byte is storing the two's complement integer value $+1$.
  1477. A byte's `most significant' bit (MSb) is at the opposite end.
  1478. Bits in a byte are numbered from zero at the LSb to $n$ for the MSb, where
  1479. $n=7$ in an octet.
  1480. \section{Coding Bits into Bytes}
  1481. The Theora codec needs to encode arbitrary bit-width integers from zero to 32
  1482. bits wide into packets.
  1483. These integer fields are not aligned to the boundaries of the byte
  1484. representation; the next field is read at the bit position immediately
  1485. after the end of the previous field.
  1486. The decoder logically unpacks integers by first reading the MSb of a binary
  1487. integer from the logical bitstream, followed by the next most significant
  1488. bit, etc., until the required number of bits have been read.
  1489. When unpacking the bytes into bits, the decoder begins by reading the MSb of
  1490. the integer to be read from the most significant unread bit position of the
  1491. source byte, followed by the next-most significant bit position of the
  1492. destination integer, and so on up to the requested number of bits.
  1493. Note that this differs from the Vorbis I codec, which
  1494. begins decoding with the LSb of the source integer, reading it from the
  1495. LSb of the source byte.
  1496. When all the bits of the current source byte are read, decoding continues with
  1497. the MSb of the next byte.
  1498. Any unfilled bits in the last byte of the packet MUST be cleared to zero by the
  1499. encoder.
  1500. \subsection{Signedness}
  1501. The binary integers decoded by the above process may be either signed or
  1502. unsigned.
  1503. This varies from integer to integer, and this specification
  1504. indicates how each value should be interpreted as it is read.
  1505. That is, depending on context, the three bit binary pattern \bin{111} can be
  1506. taken to represent either `$7$' as an unsigned integer or `$-1$' as a signed,
  1507. two's complement integer.
  1508. \subsection{Encoding Example}
  1509. The following example shows the state of an (8-bit) byte stream after several
  1510. binary integers are encoded, including the location of the put pointer for the
  1511. next bit to write to and the total length of the stream in bytes.
  1512. Encode the 4 bit unsigned integer value `12' (\bin{1100}) into an empty byte
  1513. stream.
  1514. \begin{tabular}{r|ccccccccl}
  1515. \multicolumn{1}{r}{}& &&&&$\downarrow$&&&& \\
  1516. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1517. byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
  1518. 0 & 0 & 0 & 0 & $\leftarrow$ \\
  1519. byte 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1520. byte 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1521. byte 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1522. \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
  1523. byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
  1524. byte stream length: 1 byte
  1525. \end{tabular}
  1526. \vspace{\baselineskip}
  1527. Continue by encoding the 3 bit signed integer value `-1' (\bin{111}).
  1528. \begin{tabular}{r|ccccccccl}
  1529. \multicolumn{1}{r}{} &&&&&&&&$\downarrow$& \\
  1530. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1531. byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
  1532. \textbf{1} & \textbf{1} & \textbf{1} & 0 & $\leftarrow$ \\
  1533. byte 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1534. byte 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1535. byte 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1536. \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
  1537. byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
  1538. byte stream length: 1 byte
  1539. \end{tabular}
  1540. \vspace{\baselineskip}
  1541. Continue by encoding the 7 bit integer value `17' (\bin{0010001}).
  1542. \begin{tabular}{r|ccccccccl}
  1543. \multicolumn{1}{r}{} &&&&&&&$\downarrow$&& \\
  1544. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1545. byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
  1546. \textbf{1} & \textbf{1} & \textbf{1} & \textbf{0} & \\
  1547. byte 1 & \textbf{0} & \textbf{1} & \textbf{0} & \textbf{0} &
  1548. \textbf{0} & \textbf{1} & 0 & 0 & $\leftarrow$ \\
  1549. byte 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1550. byte 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
  1551. \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
  1552. byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
  1553. byte stream length: 2 bytes
  1554. \end{tabular}
  1555. \vspace{\baselineskip}
  1556. Continue by encoding the 13 bit integer value `6969' (\bin{11011\ 00111001}).
  1557. \begin{tabular}{r|ccccccccl}
  1558. \multicolumn{1}{r}{} &&&&$\downarrow$&&&&& \\
  1559. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1560. byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
  1561. \textbf{1} & \textbf{1} & \textbf{1} & \textbf{0} & \\
  1562. byte 1 & \textbf{0} & \textbf{1} & \textbf{0} & \textbf{0} &
  1563. \textbf{0} & \textbf{1} & \textbf{1} & \textbf{1} & \\
  1564. byte 2 & \textbf{0} & \textbf{1} & \textbf{1} & \textbf{0} &
  1565. \textbf{0} & \textbf{1} & \textbf{1} & \textbf{1} & \\
  1566. byte 3 & \textbf{0} & \textbf{0} & \textbf{1} &
  1567. 0 & 0 & 0 & 0 & 0 & $\leftarrow$ \\
  1568. \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
  1569. byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
  1570. byte stream length: 4 bytes
  1571. \end{tabular}
  1572. \vspace{\baselineskip}
  1573. \subsection{Decoding Example}
  1574. The following example shows the state of the (8-bit) byte stream encoded in the
  1575. previous example after several binary integers are decoded, including the
  1576. location of the get pointer for the next bit to read.
  1577. Read a two bit unsigned integer from the example encoded above.
  1578. \begin{tabular}{r|ccccccccl}
  1579. \multicolumn{1}{r}{} &&&$\downarrow$&&&&&& \\
  1580. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1581. byte 0 & \textbf{1} & \textbf{1} & 0 & 0 & 1 & 1 & 1 & 0 & $\leftarrow$ \\
  1582. byte 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 & \\
  1583. byte 2 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & \\
  1584. byte 3 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 &
  1585. byte stream length: 4 bytes
  1586. \end{tabular}
  1587. \vspace{\baselineskip}
  1588. Value read: 3 (\bin{11}).
  1589. Read another two bit unsigned integer from the example encoded above.
  1590. \begin{tabular}{r|ccccccccl}
  1591. \multicolumn{1}{r}{} &&&&&$\downarrow$&&&& \\
  1592. & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
  1593. byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
  1594. 1 & 1 & 1 & 0 & $\leftarrow$ \\
  1595. byte 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 & \\
  1596. byte 2 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & \\
  1597. byte 3 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 &
  1598. byte stream length: 4 bytes
  1599. \end{tabular}
  1600. \vspace{\baselineskip}
  1601. Value read: 0 (\bin{00}).
  1602. Two things are worth noting here.
  1603. \begin{itemize}
  1604. \item
  1605. Although these four bits were originally written as a single four-bit integer,
  1606. reading some other combination of bit-widths from the bitstream is well
  1607. defined.
  1608. No artificial alignment boundaries are maintained in the bitstream.
  1609. \item
  1610. The first value is the integer `$3$' only because the context stated we were
  1611. reading an unsigned integer.
  1612. Had the context stated we were reading a signed integer, the returned value
  1613. would have been the integer `$-1$'.
  1614. \end{itemize}
  1615. \subsection{End-of-Packet Alignment}
  1616. The typical use of bitpacking is to produce many independent byte-aligned
  1617. packets which are embedded into a larger byte-aligned container structure,
  1618. such as an Ogg transport bitstream.
  1619. Externally, each bitstream encoded as a byte stream MUST begin and end on a
  1620. byte boundary.
  1621. Often, the encoded packet bitstream is not an integer number of bytes, and so
  1622. there is unused space in the last byte of a packet.
  1623. %r: I think the generality here is necessary to be consistent with our assertions
  1624. %r: elsewhere about being independent of transport and byte width
  1625. When a Theora encoder produces packets for embedding in a byte-aligned
  1626. container, unused space in the last byte of a packet is always zeroed during
  1627. the encoding process.
  1628. Thus, should this unused space be read, it will return binary zeroes.
  1629. There is no marker pattern or stuffing bits that will allow the decoder to
  1630. obtain the exact size, in bits, of the original bitstream.
  1631. This knowledge is not required for decoding.
  1632. Attempting to read past the end of an encoded packet results in an
  1633. `end-of-packet' condition.
  1634. Any further read operations after an `end-of-packet' condition shall also
  1635. return `end-of-packet'.
  1636. Unlike Vorbis, Theora does not use truncated packets as a normal mode of
  1637. operation.
  1638. Therefore if a decoder encounters the `end-of-packet' condition during normal
  1639. decoding, it may attempt to use the bits that were read to recover as much of
  1640. encoded data as possible, signal a warning or error, or both.
  1641. \subsection{Reading Zero Bit Integers}
  1642. Reading a zero bit integer returns the value `$0$' and does not increment
  1643. the stream pointer.
  1644. Reading to the end of the packet, but not past the end, so that an
  1645. `end-of-packet' condition is not triggered, and then reading a zero bit
  1646. integer shall succeed, returning `$0$', and not trigger an `end-of-packet'
  1647. condition.
  1648. Reading a zero bit integer after a previous read sets the `end-of-packet'
  1649. condition shall fail, also returning `end-of-packet'.
  1650. \chapter{Bitstream Headers}
  1651. \label{sec:headers}
  1652. A Theora bitstream begins with three header packets.
  1653. The header packets are, in order, the identification header, the comment
  1654. header, and the setup header.
  1655. All are required for decode compliance.
  1656. An end-of-packet condition encountered while decoding the identification or
  1657. setup header packets renders the stream undecodable.
  1658. An end-of-packet condition encountered while decode the comment header is a
  1659. non-fatal error condition, and MAY be ignored by a decoder.
  1660. \paragraph{VP3 Compatibility}
  1661. VP3 relies on the headers provided by its container, usually either AVI or
  1662. Quicktime.
  1663. As such, several parameters available in these headers are not available to VP3
  1664. streams.
  1665. These are indicated as they appear in the sections below.
  1666. \section{Common Header Decode}
  1667. \label{sub:common-header}
  1668. \begin{figure}[Htbp]
  1669. \begin{center}
  1670. \begin{verbatim}
  1671. 0 1 2 3
  1672. 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
  1673. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1674. | header type | `t' | `h' | `e' |
  1675. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1676. | `o' | `r' | `a' | data... |
  1677. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1678. | ... header-specific data ... |
  1679. | ... |
  1680. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1681. \end{verbatim}
  1682. \end{center}
  1683. \caption{Common Header Packet Layout}
  1684. \label{fig:commonheader}
  1685. \end{figure}
  1686. \paragraph{Input parameters:} None.
  1687. \paragraph{Output parameters:}\hfill\\*
  1688. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  1689. \multicolumn{1}{c}{Name} &
  1690. \multicolumn{1}{c}{Type} &
  1691. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  1692. \multicolumn{1}{c}{Signed?} &
  1693. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  1694. \bitvar{HEADERTYPE} & Integer & 8 & No & The type of the header being
  1695. decoded. \\
  1696. \bottomrule\end{tabularx}
  1697. \paragraph{Variables used:} None.
  1698. \medskip
  1699. Each header packet begins with the same header fields, which are decoded as
  1700. follows:
  1701. \begin{enumerate}
  1702. \item
  1703. Read an 8-bit unsigned integer as \bitvar{HEADERTYPE}.
  1704. If the most significant bit of this integer is not set, then stop.
  1705. This is not a header packet.
  1706. \item
  1707. Read 6 8-bit unsigned integers.
  1708. If these do not have the values \hex{74}, \hex{68}, \hex{65}, \hex{6F},
  1709. \hex{72}, and \hex{61}, respectively, then stop.
  1710. This stream is not decodable by this specification.
  1711. These values correspond to the ASCII values of the characters `t', `h', `e',
  1712. `o', `r', and `a'.
  1713. \end{enumerate}
  1714. Decode continues according to \bitvar{HEADERTYPE}.
  1715. The identification header is type \hex{80}, the comment header is type
  1716. \hex{81}, and the setup header is type \hex{82}.
  1717. These packets must occur in the order: identification, comment, setup.
  1718. %r: I clarified the initial-bit scheme here
  1719. %TBT: Dashes let the reader know they'll have to pick up the rest of the
  1720. %TBT: sentence after the explanatory phrase.
  1721. %TBT: Otherwise it just sounds like the bit must exist.
  1722. All header packets have the most significant bit of the type
  1723. field---which is the initial bit in the packet---set.
  1724. This distinguishes them from video data packets in which the first bit
  1725. is unset.
  1726. % extra header packets are a feature Dan argued for way back when for
  1727. % backward-compatible extensions (and icc colourspace for example)
  1728. % I think it's reasonable
  1729. %TBT: You can always just stick more stuff in the setup header.
  1730. Packets with other header types (\hex{83}--\hex{FF}) are reserved and MUST be
  1731. ignored.
  1732. \section{Identification Header Decode}
  1733. \label{sec:idheader}
  1734. \begin{figure}[Htbp]
  1735. \begin{center}
  1736. \begin{verbatim}
  1737. 0 1 2 3
  1738. 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
  1739. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1740. | 0x80 | `t' | `h' | `e' |
  1741. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1742. | `o' | `r' | `a' | VMAJ |
  1743. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1744. | VMIN | VREV | FMBW |
  1745. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1746. | FMBH | PICW... |
  1747. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1748. | ...PICW | PICH |
  1749. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1750. | PICX | PICY | FRN... |
  1751. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1752. | ...FRN | FRD... |
  1753. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1754. | ...FRD | PARN... |
  1755. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1756. | ...PARN | PARD |
  1757. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1758. | CS | NOMBR |
  1759. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1760. | QUAL | KFGSHIFT| PF| Res |
  1761. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1762. \end{verbatim}
  1763. \end{center}
  1764. \caption{Identification Header Packet}
  1765. \label{fig:idheader}
  1766. \end{figure}
  1767. \paragraph{Input parameters:} None.
  1768. \paragraph{Output parameters:}\hfill\\*
  1769. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  1770. \multicolumn{1}{c}{Name} &
  1771. \multicolumn{1}{c}{Type} &
  1772. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  1773. \multicolumn{1}{c}{Signed?} &
  1774. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  1775. \bitvar{VMAJ} & Integer & 8 & No & The major version number. \\
  1776. \bitvar{VMIN} & Integer & 8 & No & The minor version number. \\
  1777. \bitvar{VREV} & Integer & 8 & No & The version revision number. \\
  1778. \bitvar{FMBW} & Integer & 16 & No & The width of the frame in macro
  1779. blocks. \\
  1780. \bitvar{FMBH} & Integer & 16 & No & The height of the frame in macro
  1781. blocks. \\
  1782. \bitvar{NSBS} & Integer & 32 & No & The total number of super blocks in a
  1783. frame. \\
  1784. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  1785. frame. \\
  1786. \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
  1787. frame. \\
  1788. \bitvar{PICW} & Integer & 20 & No & The width of the picture region in
  1789. pixels. \\
  1790. \bitvar{PICH} & Integer & 20 & No & The height of the picture region in
  1791. pixels. \\
  1792. \bitvar{PICX} & Integer & 8 & No & The X offset of the picture region in
  1793. pixels. \\
  1794. \bitvar{PICY} & Integer & 8 & No & The Y offset of the picture region in
  1795. pixels. \\
  1796. \bitvar{FRN} & Integer & 32 & No & The frame-rate numerator. \\
  1797. \bitvar{FRD} & Integer & 32 & No & The frame-rate denominator. \\
  1798. \bitvar{PARN} & Integer & 24 & No & The pixel aspect-ratio numerator. \\
  1799. \bitvar{PARD} & Integer & 24 & No & The pixel aspect-ratio denominator. \\
  1800. \bitvar{CS} & Integer & 8 & No & The color space. \\
  1801. \bitvar{PF} & Integer & 2 & No & The pixel format. \\
  1802. \bitvar{NOMBR} & Integer & 24 & No & The nominal bitrate of the stream, in
  1803. bits per second. \\
  1804. \bitvar{QUAL} & Integer & 6 & No & The quality hint. \\
  1805. \bitvar{KFGSHIFT} & Integer & 5 & No & The amount to shift the key frame
  1806. number by in the granule position. \\
  1807. \bottomrule\end{tabularx}
  1808. \paragraph{Variables used:} None.
  1809. \medskip
  1810. The identification header is a short header with only a few fields used to
  1811. declare the stream definitively as Theora and provide detailed information
  1812. about the format of the fully decoded video data.
  1813. The identification header is decoded as follows:
  1814. \begin{enumerate}
  1815. \item
  1816. Decode the common header fields according to the procedure described in
  1817. Section~\ref{sub:common-header}.
  1818. If \bitvar{HEADERTYPE} returned by this procedure is not \hex{80}, then stop.
  1819. This packet is not the identification header.
  1820. \item
  1821. Read an 8-bit unsigned integer as \bitvar{VMAJ}.
  1822. If \bitvar{VMAJ} is not $3$, then stop.
  1823. This stream is not decodable according to this specification.
  1824. \item
  1825. Read an 8-bit unsigned integer as \bitvar{VMIN}.
  1826. If \bitvar{VMIN} is not $2$, then stop.
  1827. This stream is not decodable according to this specification.
  1828. \item
  1829. Read an 8-bit unsigned integer as \bitvar{VREV}.
  1830. If \bitvar{VREV} is greater than $1$, then this stream
  1831. may contain optional features or interpretational changes
  1832. documented in a future version of this specification.
  1833. Regardless of the value of \bitvar{VREV}, the stream is decodable
  1834. according to this specification.
  1835. \item
  1836. Read a 16-bit unsigned integer as \bitvar{FMBW}.
  1837. This MUST be greater than zero.
  1838. This specifies the width of the coded frame in macro blocks.
  1839. The actual width of the frame in pixels is $\bitvar{FMBW}*16$.
  1840. \item
  1841. Read a 16-bit unsigned integer as \bitvar{FMBH}.
  1842. This MUST be greater than zero.
  1843. This specifies the height of the coded frame in macro blocks.
  1844. The actual height of the frame in pixels is $\bitvar{FMBH}*16$.
  1845. \item
  1846. Read a 24-bit unsigned integer as \bitvar{PICW}.
  1847. This MUST be no greater than $(\bitvar{FMBW}*16)$.
  1848. Note that 24 bits are read, even though only 20 bits are sufficient to specify
  1849. any value of the picture width.
  1850. This is done to preserve octet alignment in this header, to allow for a
  1851. simplified parser implementation.
  1852. \item
  1853. Read a 24-bit unsigned integer as \bitvar{PICH}.
  1854. This MUST be no greater than $(\bitvar{FMBH}*16)$.
  1855. Together with \bitvar{PICW}, this specifies the size of the displayable picture
  1856. region within the coded frame.
  1857. See Figure~\ref{fig:pic-frame}.
  1858. Again, 24 bits are read instead of 20.
  1859. \item
  1860. Read an 8-bit unsigned integer as \bitvar{PICX}.
  1861. This MUST be no greater than $(\bitvar{FMBW}*16-\bitvar{PICX})$.
  1862. \item
  1863. Read an 8-bit unsigned integer as \bitvar{PICY}.
  1864. This MUST be no greater than $(\bitvar{FMBH}*16-\bitvar{PICY})$.
  1865. Together with \bitvar{PICX}, this specifies the location of the lower-left
  1866. corner of the displayable picture region.
  1867. See Figure~\ref{fig:pic-frame}.
  1868. \item
  1869. Read a 32-bit unsigned integer as \bitvar{FRN}.
  1870. This MUST be greater than zero.
  1871. \item
  1872. Read a 32-bit unsigned integer as \bitvar{FRD}.
  1873. This MUST be greater than zero.
  1874. Theora is a fixed-frame rate video codec.
  1875. Frames are sampled at the constant rate of $\frac{\bitvar{FRN}}{\bitvar{FRD}}$
  1876. frames per second.
  1877. The presentation time of the first frame is at zero seconds.
  1878. No mechanism is provided to specify a non-zero offset for the initial
  1879. frame.
  1880. \item
  1881. Read a 24-bit unsigned integer as \bitvar{PARN}.
  1882. \item
  1883. Read a 24-bit unsigned integer as \bitvar{PARD}.
  1884. Together with \bitvar{PARN}, these specify the aspect ratio of the pixels
  1885. within a frame, defined as the ratio of the physical width of a pixel to its
  1886. physical height.
  1887. This is given by the ratio $\bitvar{PARN}:\bitvar{PARD}$.
  1888. If either of these fields are zero, this indicates that pixel aspect ratio
  1889. information was not available to the encoder.
  1890. In this case it MAY be specified by the application via an external means, or
  1891. a default value of $1:1$ MAY be used.
  1892. \item
  1893. Read an 8-bit unsigned integer as \bitvar{CS}.
  1894. This is a value from an enumerated list of the available color spaces, given in
  1895. Table~\ref{tab:colorspaces}.
  1896. The `Undefined' value indicates that color space information was not available
  1897. to the encoder.
  1898. It MAY be specified by the application via an external means.
  1899. If a reserved value is given, a decoder MAY refuse to decode the stream.
  1900. \begin{table}[htbp]
  1901. \begin{center}
  1902. \begin{tabular*}{215pt}{cl@{\extracolsep{\fill}}c}\toprule
  1903. Value & Color Space \\\midrule
  1904. $0$ & Undefined. \\
  1905. $1$ & Rec.~470M (see Section~\ref{sec:470m}). \\
  1906. $2$ & Rec.~470BG (see Section~\ref{sec:470bg}). \\
  1907. $3$ & Reserved. \\
  1908. $\vdots$ & \\
  1909. $255$ & \\
  1910. \bottomrule\end{tabular*}
  1911. \end{center}
  1912. \caption{Enumerated List of Color Spaces}
  1913. \label{tab:colorspaces}
  1914. \end{table}
  1915. \item
  1916. Read a 24-bit unsigned integer as \bitvar{NOMBR} signifying a rate in bits
  1917. per second. Rates equal to or greater than $2^{24}-1$ bits per second are
  1918. represented as $2^{24}-1$.
  1919. The \bitvar{NOMBR} field is used only as a hint.
  1920. For pure VBR streams, this value may be considerably off.
  1921. The field MAY be set to zero to indicate that the encoder did not care to
  1922. speculate.
  1923. \item
  1924. Read a 6-bit unsigned integer as \bitvar{QUAL}.
  1925. This value is used to provide a hint as to the relative quality of the stream
  1926. when compared to others produced by the same encoder.
  1927. Larger values indicate higher quality.
  1928. This can be used, for example, to select among several streams containing the
  1929. same material encoded with different settings.
  1930. \item
  1931. Read a 5-bit unsigned integer as \bitvar{KFGSHIFT}.
  1932. The \bitvar{KFGSHIFT} is used to partition the granule position associated with
  1933. each packet into two different parts.
  1934. The frame number of the last key frame, starting from zero, is stored in the
  1935. upper $64-\bitvar{KFGSHIFT}$ bits, while the lower \bitvar{KFGSHIFT} bits
  1936. contain the number of frames since the last keyframe.
  1937. Complete details on the granule position mapping are specified in Section~REF.
  1938. \item
  1939. Read a 2-bit unsigned integer as \bitvar{PF}.
  1940. The \bitvar{PF} field contains a value from an enumerated list of the available
  1941. pixel formats, given in Table~\ref{tab:pixel-formats}.
  1942. If the reserved value $1$ is given, stop.
  1943. This stream is not decodable according to this specification.
  1944. \begin{table}[htbp]
  1945. \begin{center}
  1946. \begin{tabular*}{215pt}{cl@{\extracolsep{\fill}}c}\toprule
  1947. Value & Pixel Format \\\midrule
  1948. $0$ & 4:2:0 (see Section~\ref{sec:420}). \\
  1949. $1$ & Reserved. \\
  1950. $2$ & 4:2:2 (see Section~\ref{sec:422}). \\
  1951. $3$ & 4:4:4 (see Section~\ref{sec:444}). \\
  1952. \bottomrule\end{tabular*}
  1953. \end{center}
  1954. \caption{Enumerated List of Pixel Formats}
  1955. \label{tab:pixel-formats}
  1956. \end{table}
  1957. \item
  1958. Read a 3-bit unsigned integer.
  1959. These bits are reserved.
  1960. If this value is not zero, then stop.
  1961. This stream is not decodable according to this specification.
  1962. \item
  1963. Assign \bitvar{NSBS} a value according to \bitvar{PF}, as given by
  1964. Table~\ref{tab:nsbs-for-pf}.
  1965. \begin{table}[bt]
  1966. \begin{center}
  1967. \begin{tabular}{cc}\toprule
  1968. \bitvar{PF} & \bitvar{NSBS} \\\midrule
  1969. $0$ & $\begin{aligned}
  1970. &((\bitvar{FMBW}+1)//2)*((\bitvar{FMBH}+1)//2)\\
  1971. & +2*((\bitvar{FMBW}+3)//4)*((\bitvar{FMBH}+3)//4)
  1972. \end{aligned}$ \\\midrule
  1973. $2$ & $\begin{aligned}
  1974. &((\bitvar{FMBW}+1)//2)*((\bitvar{FMBH}+1)//2)\\
  1975. & +2*((\bitvar{FMBW}+3)//4)*((\bitvar{FMBH}+1)//2)
  1976. \end{aligned}$ \\\midrule
  1977. $3$ & $3*((\bitvar{FMBW}+1)//2)*((\bitvar{FMBH}+1)//2)$ \\
  1978. \bottomrule\end{tabular}
  1979. \end{center}
  1980. \caption{Number of Super Blocks for each Pixel Format}
  1981. \label{tab:nsbs-for-pf}
  1982. \end{table}
  1983. \item
  1984. Assign \bitvar{NBS} a value according to \bitvar{PF}, as given by
  1985. Table~\ref{tab:nbs-for-pf}.
  1986. \begin{table}[tb]
  1987. \begin{center}
  1988. \begin{tabular}{cc}\toprule
  1989. \bitvar{PF} & \bitvar{NBS} \\\midrule
  1990. $0$ & $6*\bitvar{FMBW}*\bitvar{FMBH}$ \\\midrule
  1991. $2$ & $8*\bitvar{FMBW}*\bitvar{FMBH}$ \\\midrule
  1992. $3$ & $12*\bitvar{FMBW}*\bitvar{FMBH}$ \\
  1993. \bottomrule\end{tabular}
  1994. \end{center}
  1995. \caption{Number of Blocks for each Pixel Format}
  1996. \label{tab:nbs-for-pf}
  1997. \end{table}
  1998. \item
  1999. Assign \bitvar{NMBS} the value $(\bitvar{FMBW}*\bitvar{FMBH})$.
  2000. \end{enumerate}
  2001. \paragraph{VP3 Compatibility}
  2002. VP3 does not correctly handle frame sizes that are not a multiple of 16.
  2003. Thus, \bitvar{PICW} and \bitvar{PICH} should be set to the frame width and
  2004. height in pixels, respectively, and \bitvar{PICX} and \bitvar{PICY} should be
  2005. set to zero.
  2006. VP3 headers do not specify a color space.
  2007. VP3 only supports the 4:2:0 pixel format.
  2008. \section{Comment Header}
  2009. \label{sec:commentheader}
  2010. The Theora comment header is the second of three header packets that begin a
  2011. Theora stream.
  2012. It is meant for short text comments, not aribtrary metadata; arbitrary metadata
  2013. belongs in a separate logical stream that provides greater structure and
  2014. machine parseability.
  2015. %r: I tried to morph this a little more in the direction of our
  2016. % application space
  2017. The comment field is meant to be used much like someone jotting a quick note on
  2018. the label of a video.
  2019. It should be a little information to remember the disc or tape by and explain it to
  2020. others; a short, to-the-point text note that can be more than a couple words,
  2021. but isn't going to be more than a short paragraph.
  2022. The essentials, in other words, whatever they turn out to be, e.g.:
  2023. %TODO: Example
  2024. The comment header is stored as a logical list of eight-bit clean vectors; the
  2025. number of vectors is bounded at $2^{32}-1$ and the length of each vector is
  2026. limited to $2^{32}-1$ bytes.
  2027. The vector length is encoded; the vector contents themselves are not null
  2028. terminated.
  2029. In addition to the vector list, there is a single vector for a vendor name,
  2030. also eight-bit clean with a length encoded in 32 bits.
  2031. %TODO: The 1.0 release of libtheora sets the vendor string to ...
  2032. \subsection{Comment Length Decode}
  2033. \label{sub:comment-len}
  2034. \begin{figure}
  2035. \begin{center}
  2036. \begin{tabular}{ | c | c | }
  2037. \hline
  2038. 4 byte length &
  2039. UTF-8 encoded string ...\\
  2040. \hline
  2041. \end{tabular}
  2042. \end{center}
  2043. \caption{Length encoded string layout}
  2044. \label{fig:comment-len}
  2045. \end{figure}
  2046. \paragraph{Input parameters:} None.
  2047. \paragraph{Output parameters:}\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. \bitvar{LEN} & Integer & 32 & No & A single 32-bit length value. \\
  2055. \bottomrule\end{tabularx}
  2056. \paragraph{Variables used:}\hfill\\*
  2057. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2058. \multicolumn{1}{c}{Name} &
  2059. \multicolumn{1}{c}{Type} &
  2060. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2061. \multicolumn{1}{c}{Signed?} &
  2062. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2063. \locvar{LEN0} & Integer & 8 & No & The first octet of the string length. \\
  2064. \locvar{LEN1} & Integer & 8 & No & The second octet of the string length. \\
  2065. \locvar{LEN2} & Integer & 8 & No & The third octet of the string length. \\
  2066. \locvar{LEN3} & Integer & 8 & No & The fourth octet of the string
  2067. length. \\
  2068. \bottomrule\end{tabularx}
  2069. \medskip
  2070. A single comment vector is decoded as follows:
  2071. \begin{enumerate}
  2072. \item
  2073. Read an 8-bit unsigned integer as \locvar{LEN0}.
  2074. \item
  2075. Read an 8-bit unsigned integer as \locvar{LEN1}.
  2076. \item
  2077. Read an 8-bit unsigned integer as \locvar{LEN2}.
  2078. \item
  2079. Read an 8-bit unsigned integer as \locvar{LEN3}.
  2080. \item
  2081. Assign \bitvar{LEN} the value $(\locvar{LEN0}+(\locvar{LEN1}<<8)+
  2082. (\locvar{LEN2}<<16)+(\locvar{LEN3}<<24))$.
  2083. This construction is used so that on platforms with 8-bit bytes, the memory
  2084. organization of the comment header is identical with that of Vorbis I,
  2085. allowing for common parsing code despite the different bit packing
  2086. conventions.
  2087. \end{enumerate}
  2088. \subsection{Comment Header Decode}
  2089. \begin{figure}
  2090. \begin{center}
  2091. \begin{tabular}{ | c | }
  2092. \hline
  2093. vendor string \\ \hline
  2094. number of comments \\ \hline
  2095. comment string \\ \hline
  2096. comment string \\ \hline
  2097. ... \\
  2098. \hline
  2099. \end{tabular}
  2100. \end{center}
  2101. \caption{Comment Header Layout}
  2102. \label{fig:commentheader}
  2103. \end{figure}
  2104. \paragraph{Input parameters:} None.
  2105. \paragraph{Output parameters:}\hfill\\*
  2106. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2107. \multicolumn{1}{c}{Name} &
  2108. \multicolumn{1}{c}{Type} &
  2109. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2110. \multicolumn{1}{c}{Signed?} &
  2111. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2112. \bitvar{VENDOR} & \multicolumn{3}{l}{String} & The vendor string. \\
  2113. \bitvar{NCOMMENTS} & Integer & 32 & No & The number of user
  2114. comments. \\
  2115. \bitvar{COMMENTS} & \multicolumn{3}{l}{String Array} & A list of
  2116. \bitvar{NCOMMENTS} user comment values. \\
  2117. \bottomrule\end{tabularx}
  2118. \paragraph{Variables used:}\hfill\\*
  2119. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2120. \multicolumn{1}{c}{Name} &
  2121. \multicolumn{1}{c}{Type} &
  2122. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2123. \multicolumn{1}{c}{Signed?} &
  2124. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2125. \locvar{\ci} & Integer & 32 & No & The index of the current user
  2126. comment. \\
  2127. \bottomrule\end{tabularx}
  2128. \medskip
  2129. The complete comment header is decoded as follows:
  2130. \begin{enumerate}
  2131. \item
  2132. Decode the common header fields according to the procedure described in
  2133. Section~\ref{sub:common-header}.
  2134. If \bitvar{HEADERTYPE} returned by this procedure is not \hex{81}, then stop.
  2135. This packet is not the comment header.
  2136. \item
  2137. Decode the length of the vendor string using the procedure given in
  2138. Section~\ref{sub:comment-len} into \bitvar{LEN}.
  2139. \item
  2140. Read \bitvar{LEN} 8-bit unsigned integers.
  2141. \item
  2142. Set the string \bitvar{VENDOR} to the contents of these octets.
  2143. \item
  2144. Decode the number of user comments using the procedure given in
  2145. Section~\ref{sub:comment-len} into \bitvar{LEN}.
  2146. \item
  2147. Assign \bitvar{NCOMMENTS} the value stored in \bitvar{LEN}.
  2148. \item
  2149. For each consecutive value of \locvar{\ci} from $0$ to
  2150. $(\bitvar{NCOMMENTS}-1)$, inclusive:
  2151. \begin{enumerate}
  2152. \item
  2153. Decode the length of the current user comment using the procedure given in
  2154. Section~\ref{sub:comment-len} into \bitvar{LEN}.
  2155. \item
  2156. Read \bitvar{LEN} 8-bit unsigned integers.
  2157. \item
  2158. Set the string $\bitvar{COMMENTS}[\locvar{\ci}]$ to the contents of these
  2159. octets.
  2160. \end{enumerate}
  2161. \end{enumerate}
  2162. The comment header comprises the entirety of the second header packet.
  2163. Unlike the first header packet, it is not generally the only packet on the
  2164. second page and may span multiple pages.
  2165. The length of the comment header packet is (practically) unbounded.
  2166. The comment header packet is not optional; it must be present in the stream
  2167. even if it is logically empty.
  2168. %TODO: \paragraph{VP3 Compatibility}
  2169. \subsection{User Comment Format}
  2170. The user comment vectors are structured similarly to a UNIX environment
  2171. variable.
  2172. That is, comment fields consist of a field name and a corresponding value and
  2173. look like:
  2174. \begin{center}
  2175. \begin{tabular}{rcl}
  2176. $\bitvar{COMMENTS}[0]$ & = & ``TITLE=the look of Theora" \\
  2177. $\bitvar{COMMENTS}[1]$ & = & ``DIRECTOR=me"
  2178. \end{tabular}
  2179. \end{center}
  2180. The field name is case-insensitive and MUST consist of ASCII characters
  2181. \hex{20} through \hex{7D}, \hex{3D} (`=') excluded.
  2182. ASCII \hex{41} through \hex{5A} inclusive (characters `A'--`Z') are to be
  2183. considered equivalent to ASCII \hex{61} through \hex{7A} inclusive
  2184. (characters `a'--`z').
  2185. An entirely empty field name---one that is zero characters long---is not
  2186. disallowed.
  2187. The field name is immediately followed by ASCII \hex{3D} (`='); this equals
  2188. sign is used to terminate the field name.
  2189. The data immediately after \hex{3D} until the end of the vector is the eight-bit
  2190. clean value of the field contents encoded as a UTF-8 string~\cite{rfc2044}.
  2191. Field names MUST NOT be `internationalized'; this is a concession to
  2192. simplicity, not an attempt to exclude the majority of the world that doesn't
  2193. speak English.
  2194. Applications MAY wish to present internationalized versions of the standard
  2195. field names listed below to the user, but they are not to be stored in the
  2196. bitstream.
  2197. Field {\em contents}, however, use the UTF-8 character encoding to allow easy
  2198. representation of any language.
  2199. Individual `vendors' MAY use non-standard field names within reason.
  2200. The proper use of comment fields as human-readable notes has already been
  2201. explained.
  2202. Abuse will be discouraged.
  2203. There is no vendor-specific prefix to `non-standard' field names.
  2204. Vendors SHOULD make some effort to avoid arbitrarily polluting the common
  2205. namespace.
  2206. %"and other bodies"?
  2207. %If you're going to be that vague, you might as well not say anything at all.
  2208. Xiph.Org and other bodies will generally collect and rationalize the more
  2209. useful tags to help with standardization.
  2210. Field names are not restricted to occur only once within a comment header.
  2211. %TODO: Example
  2212. \paragraph{Field Names}
  2213. %r should this be an appendix?
  2214. Below is a proposed, minimal list of standard field names with a description of
  2215. their intended use.
  2216. No field names are mandatory; a comment header may contain one or more, all, or
  2217. none of the names in this list.
  2218. \begin{description}
  2219. \item{TITLE:} Video name.
  2220. \item{ARTIST:} Filmmaker or other creator name.
  2221. \item{VERSION:} Subtitle, remix info, or other text distinguishing
  2222. versions of a video.
  2223. \item{DATE:} Date associated with the video. Implementations SHOULD attempt
  2224. to parse this field as an ISO 8601 date for machine interpretation and
  2225. conversion.
  2226. \item{LOCATION:} Location associated with the video. This is usually the
  2227. filming location for non-fiction works.
  2228. \item{COPYRIGHT:} Copyright statement.
  2229. \item{LICENSE:} Copyright and other licensing information.
  2230. Implementations wishing to do automatic parsing of e.g
  2231. of distribution terms SHOULD look here for a URL uniquely defining
  2232. the license. If no instance of this field is present, or if no
  2233. instance contains a parseable URL, and implementation MAY look
  2234. in the COPYRIGHT field for such a URL.
  2235. \item{ORGANIZATION:} Studio name, Publisher, or other organization
  2236. involved in the creation of the video.
  2237. \item{DIRECTOR:} Director or Filmmaker credit, similar to ARTIST.
  2238. \item{PRODUCER:} Producer credit for the video.
  2239. \item{COMPOSER:} Music credit for the video.
  2240. \item{ACTOR:} Acting credit for the video.
  2241. \item{TAG:} subject or category tag, keyword, or other content
  2242. classification labels. The value of each instance of this
  2243. field SHOULD be treated as a single label, with multiple
  2244. instances of the field for multiple tags. The value of
  2245. a single field SHOULD NOT be parsed into multiple tags
  2246. based on some internal delimeter.
  2247. \item{DESCRIPTION:} General description, summary, or blurb.
  2248. \end{description}
  2249. \section{Setup Header}
  2250. \label{sec:setupheader}
  2251. The Theora setup header contains the limit values used to drive the loop
  2252. filter, the base matrices and scale values used to build the dequantization
  2253. tables, and the Huffman tables used to unpack the DCT tokens.
  2254. Because the contents of this header are specific to Theora, no concessions have
  2255. been made to keep the fields octet-aligned for easy parsing.
  2256. \begin{figure}
  2257. \begin{center}
  2258. \begin{tabular}{ | c | }
  2259. \hline
  2260. common header block \\ \hline
  2261. loop filter table resolution \\ \hline
  2262. loop filter table \\ \hline
  2263. scale table resolution \\ \hline
  2264. AC scale table \\ \hline
  2265. DC scale table \\ \hline
  2266. number of base matricies \\ \hline
  2267. base quatization matricies \\ \hline
  2268. ... \\ \hline
  2269. quant range interpolation table \\ \hline
  2270. DCT token Huffman tables \\
  2271. \hline
  2272. \end{tabular}
  2273. \end{center}
  2274. \caption{Setup Header structure}
  2275. \label{fig:setupheader}
  2276. \end{figure}
  2277. \subsection{Loop Filter Limit Table Decode}
  2278. \label{sub:loop-filter-limits}
  2279. \paragraph{Input parameters:} None.
  2280. \paragraph{Output parameters:}\hfill\\*
  2281. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2282. \multicolumn{1}{c}{Name} &
  2283. \multicolumn{1}{c}{Type} &
  2284. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2285. \multicolumn{1}{c}{Signed?} &
  2286. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2287. \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
  2288. 7 & No & A 64-element array of loop filter limit
  2289. values. \\
  2290. \bottomrule\end{tabularx}
  2291. \paragraph{Variables used:}\hfill\\*
  2292. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2293. \multicolumn{1}{c}{Name} &
  2294. \multicolumn{1}{c}{Type} &
  2295. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2296. \multicolumn{1}{c}{Signed?} &
  2297. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2298. \locvar{\qi} & Integer & 6 & No & The quantization index. \\
  2299. \locvar{NBITS} & Integer & 3 & No & The size of values being read in the
  2300. current table. \\
  2301. \bottomrule\end{tabularx}
  2302. \medskip
  2303. This procedure decodes the table of loop filter limit values used to drive the
  2304. loop filter, which is described in Section~\ref{sub:loop-filter-limits}.
  2305. It is decoded as follows:
  2306. \begin{enumerate}
  2307. \item
  2308. Read a 3-bit unsigned integer as \locvar{NBITS}.
  2309. \item
  2310. For each consecutive value of \locvar{\qi} from $0$ to $63$, inclusive:
  2311. \begin{enumerate}
  2312. \item
  2313. Read an \locvar{NBITS}-bit unsigned integer as $\bitvar{LFLIMS}[\locvar{\qi}]$.
  2314. \end{enumerate}
  2315. \end{enumerate}
  2316. \paragraph{VP3 Compatibility}
  2317. The loop filter limit values are hardcoded in VP3.
  2318. The values used are given in Appendix~\ref{app:vp3-loop-filter-limits}.
  2319. \subsection{Quantization Parameters Decode}
  2320. \label{sub:quant-params}
  2321. \paragraph{Input parameters:} None.
  2322. \paragraph{Output parameters:}\hfill\\*
  2323. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2324. \multicolumn{1}{c}{Name} &
  2325. \multicolumn{1}{c}{Type} &
  2326. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2327. \multicolumn{1}{c}{Signed?} &
  2328. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2329. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2330. 16 & No & A 64-element array of scale values for
  2331. AC coefficients for each \qi\ value. \\
  2332. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2333. 16 & No & A 64-element array of scale values for
  2334. the DC coefficient for each \qi\ value. \\
  2335. \bitvar{NBMS} & Integer & 10 & No & The number of base matrices. \\
  2336. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2337. 8 & No & A $\bitvar{NBMS}\times 64$ array
  2338. containing the base matrices. \\
  2339. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2340. 6 & No & A $2\times 3$ array containing the
  2341. number of quant ranges for a given \qti\ and \pli, respectively.
  2342. This is at most $63$. \\
  2343. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2344. 6 & No & A $2\times 3\times 63$ array of the
  2345. sizes of each quant range for a given \qti\ and \pli, respectively.
  2346. Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
  2347. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2348. 9 & No & A $2\times 3\times 64$ array of the
  2349. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  2350. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
  2351. \bottomrule\end{tabularx}
  2352. \paragraph{Variables used:}\hfill\\*
  2353. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2354. \multicolumn{1}{c}{Name} &
  2355. \multicolumn{1}{c}{Type} &
  2356. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2357. \multicolumn{1}{c}{Signed?} &
  2358. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2359. \locvar{\qti} & Integer & 1 & No & A quantization type index.
  2360. See Table~\ref{tab:quant-types}.\\
  2361. \locvar{\qtj} & Integer & 1 & No & A quantization type index. \\
  2362. \locvar{\pli} & Integer & 2 & No & A color plane index.
  2363. See Table~\ref{tab:color-planes}.\\
  2364. \locvar{\plj} & Integer & 2 & No & A color plane index. \\
  2365. \locvar{\qi} & Integer & 6 & No & The quantization index. \\
  2366. \locvar{\ci} & Integer & 6 & No & The DCT coefficient index. \\
  2367. \locvar{\bmi} & Integer & 9 & No & The base matrix index. \\
  2368. \locvar{\qri} & Integer & 6 & No & The quant range index. \\
  2369. \locvar{NBITS} & Integer & 5 & No & The size of fields to read. \\
  2370. \locvar{NEWQR} & Integer & 1 & No & Flag that indicates a new set of quant
  2371. ranges will be defined. \\
  2372. \locvar{RPQR} & Integer & 1 & No & Flag that indicates the quant ranges to
  2373. copy will come from the same color plane. \\
  2374. \bottomrule\end{tabularx}
  2375. \medskip
  2376. The AC scale and DC scale values are defined in two simple tables with 64
  2377. values each, one for each \qi\ value.
  2378. The same scale values are used for every quantization type and color plane.
  2379. The base matrices for all quantization types and color planes are stored in a
  2380. single table.
  2381. These are then referenced by index in several sets of \term{quant ranges}.
  2382. The purpose of the quant ranges is to specify which base matrices are used for
  2383. which \qi\ values.
  2384. A set of quant ranges is defined for each quantization type and color plane.
  2385. To save space in the header, bit flags allow a set of quant ranges to be copied
  2386. from a previously defined set instead of being specified explicitly.
  2387. Every set except the first one can be copied from the immediately preceding
  2388. set.
  2389. Similarly, if the quantization type is not $0$, the set can be copied from the
  2390. set defined for the same color plane for the preceding quantization type.
  2391. This formulation allows compact representation of, for example, the same
  2392. set of quant ranges in both chroma channels, as is done in the original VP3,
  2393. or the same set of quant ranges in INTRA and INTER modes.
  2394. Each quant range is defined by a size and two base matrix indices, one for each
  2395. end of the range.
  2396. The base matrix for the end of one range is used as the start of the next
  2397. range, so that for $n$ ranges, $n+1$ base matrices are specified.
  2398. The base matrices for the \qi\ values between the two endpoints of the range
  2399. are generated by linear interpolation.
  2400. %TODO: figure
  2401. The location of the endpoints of each range is encoded by their size.
  2402. The \qi\ value for the left end-point is the sum of the sizes of all preceding
  2403. ranges, and the \qi\ value for the right end-point adds the size of the
  2404. current range.
  2405. Thus the sum of the sizes of all the ranges MUST be 63, so that the last range
  2406. falls on the last possible \qi\ value.
  2407. The complete set of quantization parameters are decoded as follows:
  2408. \begin{enumerate}
  2409. \item
  2410. Read a 4-bit unsigned integer.
  2411. Assign \locvar{NBITS} the value read, plus one.
  2412. \item
  2413. For each consecutive value of \locvar{\qi} from $0$ to $63$, inclusive:
  2414. \begin{enumerate}
  2415. \item
  2416. Read an \locvar{NBITS}-bit unsigned integer as
  2417. $\bitvar{ACSCALE}[\locvar{\qi}]$.
  2418. \end{enumerate}
  2419. \item
  2420. Read a 4-bit unsigned integer.
  2421. Assign \locvar{NBITS} the value read, plus one.
  2422. \item
  2423. For each consecutive value of \locvar{\qi} from $0$ to $63$, inclusive:
  2424. \begin{enumerate}
  2425. \item
  2426. Read an \locvar{NBITS}-bit unsigned integer as
  2427. $\bitvar{DCSCALE}[\locvar{\qi}]$.
  2428. \end{enumerate}
  2429. \item
  2430. Read a 9-bit unsigned integer.
  2431. Assign \bitvar{NBMS} the value decoded, plus one.
  2432. \bitvar{NBMS} MUST be no greater than 384.
  2433. \item
  2434. For each consecutive value of \locvar{\bmi} from $0$ to $(\bitvar{NBMS}-1)$,
  2435. inclusive:
  2436. \begin{enumerate}
  2437. \item
  2438. For each consecutive value of \locvar{\ci} from $0$ to $63$, inclusive:
  2439. \begin{enumerate}
  2440. \item
  2441. Read an 8-bit unsigned integer as $\bitvar{BMS}[\locvar{\bmi}][\locvar{\ci}]$.
  2442. \end{enumerate}
  2443. \end{enumerate}
  2444. \item
  2445. For each consecutive value of \locvar{\qti} from $0$ to $1$, inclusive:
  2446. \begin{enumerate}
  2447. \item
  2448. For each consecutive value of \locvar{\pli} from $0$ to $2$, inclusive:
  2449. \begin{enumerate}
  2450. \item
  2451. If $\locvar{\qti}>0$ or $\locvar{\pli}>0$, read a 1-bit unsigned integer as
  2452. \locvar{NEWQR}.
  2453. \item
  2454. Else, assign \locvar{NEWQR} the value one.
  2455. \item
  2456. If \locvar{NEWQR} is zero, then we are copying a previously defined set of
  2457. quant ranges.
  2458. In that case:
  2459. \begin{enumerate}
  2460. \item
  2461. If $\locvar{\qti}>0$, read a 1-bit unsigned integer as \locvar{RPQR}.
  2462. \item
  2463. Else, assign \locvar{RPQR} the value zero.
  2464. \item
  2465. If \locvar{RPQR} is one, assign \locvar{\qtj} the value $(\locvar{\qti}-1)$
  2466. and assign \locvar{\plj} the value \locvar{\pli}.
  2467. This selects the set of quant ranges defined for the same color plane as this
  2468. one, but for the previous quantization type.
  2469. \item
  2470. Else assign \locvar{\qtj} the value $(3*\locvar{\qti}+\locvar{\pli}-1)//3$ and
  2471. assign \locvar{\plj} the value $(\locvar{\pli}+2)\%3$.
  2472. This selects the most recent set of quant ranges defined.
  2473. \item
  2474. Assign $\bitvar{NQRS}[\locvar{\qti}][\locvar{\pli}]$ the value
  2475. $\bitvar{NQRS}[\locvar{\qtj}][\locvar{\plj}]$.
  2476. \item
  2477. Assign $\bitvar{QRSIZES}[\locvar{\qti}][\locvar{\pli}]$ the values in
  2478. $\bitvar{QRSIZES}[\locvar{\qtj}][\locvar{\plj}]$.
  2479. \item
  2480. Assign $\bitvar{QRBMIS}[\locvar{\qti}][\locvar{\pli}]$ the values in
  2481. $\bitvar{QRBMIS}[\locvar{\qtj}][\locvar{\plj}]$.
  2482. \end{enumerate}
  2483. \item
  2484. Else, \locvar{NEWQR} is one, which indicates that we are defining a new set of
  2485. quant ranges.
  2486. In that case:
  2487. \begin{enumerate}
  2488. \item
  2489. Assign $\locvar{\qri}$ the value zero.
  2490. \item
  2491. Assign $\locvar{\qi}$ the value zero.
  2492. \item
  2493. Read an $\ilog(\bitvar{NBMS}-1)$-bit unsigned integer as\\
  2494. $\bitvar{QRBMIS}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$.
  2495. If this is greater than or equal to \bitvar{NBMS}, stop.
  2496. The stream is undecodable.
  2497. \item
  2498. \label{step:qr-loop}
  2499. Read an $\ilog(62-\locvar{\qi})$-bit unsigned integer.
  2500. Assign\\ $\bitvar{QRSIZES}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$ the value
  2501. read, plus one.
  2502. \item
  2503. Assign \locvar{\qi} the value $\locvar{\qi}+
  2504. \bitvar{QRSIZES}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$.
  2505. \item
  2506. Assign \locvar{\qri} the value $\locvar{\qri}+1$.
  2507. \item
  2508. Read an $\ilog(\bitvar{NBMS}-1)$-bit unsigned integer as\\
  2509. $\bitvar{QRBMIS}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$.
  2510. \item
  2511. If \locvar{\qi} is less than 63, go back to step~\ref{step:qr-loop}.
  2512. \item
  2513. If \locvar{\qi} is greater than 63, stop.
  2514. The stream is undecodable.
  2515. \item
  2516. Assign $\bitvar{NQRS}[\locvar{\qti}][\locvar{\pli}]$ the value \locvar{\qri}.
  2517. \end{enumerate}
  2518. \end{enumerate}
  2519. \end{enumerate}
  2520. \end{enumerate}
  2521. \paragraph{VP3 Compatibility}
  2522. The quantization parameters are hardcoded in VP3.
  2523. The values used are given in Appendix~\ref{app:vp3-quant-params}.
  2524. \subsection{Computing a Quantization Matrix}
  2525. \label{sub:quant-mat}
  2526. \paragraph{Input parameters:}\hfill\\*
  2527. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2528. \multicolumn{1}{c}{Name} &
  2529. \multicolumn{1}{c}{Type} &
  2530. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2531. \multicolumn{1}{c}{Signed?} &
  2532. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2533. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2534. 16 & No & A 64-element array of scale values for
  2535. AC coefficients for each \qi\ value. \\
  2536. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2537. 16 & No & A 64-element array of scale values for
  2538. the DC coefficient for each \qi\ value. \\
  2539. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2540. 8 & No & A $\bitvar{NBMS}\times 64$ array
  2541. containing the base matrices. \\
  2542. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2543. 6 & No & A $2\times 3$ array containing the
  2544. number of quant ranges for a given \qti\ and \pli, respectively.
  2545. This is at most $63$. \\
  2546. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2547. 6 & No & A $2\times 3\times 63$ array of the
  2548. sizes of each quant range for a given \qti\ and \pli, respectively.
  2549. Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
  2550. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2551. 9 & No & A $2\times 3\times 64$ array of the
  2552. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  2553. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
  2554. \bitvar{\qti} & Integer & 1 & No & A quantization type index.
  2555. See Table~\ref{tab:quant-types}.\\
  2556. \bitvar{\pli} & Integer & 2 & No & A color plane index.
  2557. See Table~\ref{tab:color-planes}.\\
  2558. \bitvar{\qi} & Integer & 6 & No & The quantization index. \\
  2559. \bottomrule\end{tabularx}
  2560. \paragraph{Output parameters:}\hfill\\*
  2561. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2562. \multicolumn{1}{c}{Name} &
  2563. \multicolumn{1}{c}{Type} &
  2564. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2565. \multicolumn{1}{c}{Signed?} &
  2566. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2567. \bitvar{QMAT} & \multicolumn{1}{p{40pt}}{Integer array} &
  2568. 16 & No & A 64-element array of quantization
  2569. values for each DCT coefficient in natural order. \\
  2570. \bottomrule\end{tabularx}
  2571. \paragraph{Variables used:}\hfill\\*
  2572. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2573. \multicolumn{1}{c}{Name} &
  2574. \multicolumn{1}{c}{Type} &
  2575. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2576. \multicolumn{1}{c}{Signed?} &
  2577. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2578. \locvar{\ci} & Integer & 6 & No & The DCT coefficient index. \\
  2579. \locvar{\bmi} & Integer & 9 & No & The base matrix index. \\
  2580. \locvar{\bmj} & Integer & 9 & No & The base matrix index. \\
  2581. \locvar{\qri} & Integer & 6 & No & The quant range index. \\
  2582. \locvar{QISTART} & Integer & 6 & No & The left end-point of the \qi\ range. \\
  2583. \locvar{QIEND } & Integer & 6 & No & The right end-point of the \qi\ range. \\
  2584. \locvar{BM} & \multicolumn{1}{p{40pt}}{Integer array} &
  2585. 8 & No & A 64-element array containing the
  2586. interpolated base matrix. \\
  2587. \locvar{QMIN} & Integer & 16 & No & The minimum quantization value allowed
  2588. for the current coefficient. \\
  2589. \locvar{QSCALE} & Integer & 16 & No & The current scale value. \\
  2590. \bottomrule\end{tabularx}
  2591. \medskip
  2592. The following procedure can be used to generate a single quantization matrix
  2593. for a given quantization type, color plane, and \qi\ value, given the
  2594. quantization parameters decoded in Section~\ref{sub:quant-params}.
  2595. Note that the product of the scale value and the base matrix value is in units
  2596. of $100$ths of a pixel value, and thus is divided by $100$ to return it to
  2597. units of a single pixel value.
  2598. This value is then scaled by four, to match the scaling of the DCT output,
  2599. which is also a factor of four larger than the orthonormal version of the
  2600. transform.
  2601. \begin{enumerate}
  2602. \item
  2603. Assign \locvar{\qri} the index of a quant range such that
  2604. \begin{displaymath}
  2605. \bitvar{\qi} \ge \sum_{\qrj=0}^{\locvar{\qri}-1}
  2606. \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj],
  2607. \end{displaymath}
  2608. and
  2609. \begin{displaymath}
  2610. \bitvar{\qi} \le \sum_{\qrj=0}^{\locvar{\qri}}
  2611. \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj],
  2612. \end{displaymath}
  2613. where summation from $0$ to $-1$ is defined to be zero.
  2614. If there is more than one such value of $\locvar{\qri}$, i.e., if \bitvar{\qi}
  2615. lies on the boundary between two quant ranges, then the output will be the
  2616. same regardless of which one is chosen.
  2617. \item
  2618. Assign \locvar{QISTART} the value
  2619. \begin{displaymath}
  2620. \sum_{\qrj=0}^{\qri-1} \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj].
  2621. \end{displaymath}
  2622. \item
  2623. Assign \locvar{QIEND} the value
  2624. \begin{displaymath}
  2625. \sum_{\qrj=0}^{\qri} \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj].
  2626. \end{displaymath}
  2627. \item
  2628. Assign \locvar{\bmi} the value
  2629. $\bitvar{QRBMIS}[\bitvar{\qti}][\bitvar{\pli}][\qri]$.
  2630. \item
  2631. Assign \locvar{\bmj} the value
  2632. $\bitvar{QRBMIS}[\bitvar{\qti}][\bitvar{\pli}][\qri+1]$.
  2633. \item
  2634. For each consecutive value of \locvar{\ci} from $0$ to $63$, inclusive:
  2635. \begin{enumerate}
  2636. \item
  2637. Assign $\locvar{BM}[\locvar{\ci}]$ the value
  2638. \begin{displaymath}
  2639. \begin{split}
  2640. (&2*(\locvar{QIEND}-\bitvar{\qi})*\bitvar{BMS}[\locvar{\bmi}][\locvar{\ci}]\\
  2641. &+2*(\bitvar{\qi}-
  2642. \locvar{QISTART})*\bitvar{BMS}[\locvar{\bmj}][\locvar{\ci}]\\
  2643. &+\bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\locvar{\qri}])//
  2644. (2*\bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\locvar{\qri}])
  2645. \end{split}
  2646. \end{displaymath}
  2647. \item
  2648. Assign \locvar{QMIN} the value given by Table~\ref{tab:qmin} according to
  2649. \bitvar{\qti} and \locvar{\ci}.
  2650. \begin{table}[htbp]
  2651. \begin{center}
  2652. \begin{tabular}{clr}\toprule
  2653. Coefficient & \multicolumn{1}{c}{\bitvar{\qti}}
  2654. & \locvar{QMIN} \\\midrule
  2655. $\locvar{\ci}=0$ & $0$ (Intra) & $16$ \\
  2656. $\locvar{\ci}>0$ & $0$ (Intra) & $8$ \\
  2657. $\locvar{\ci}=0$ & $1$ (Inter) & $32$ \\
  2658. $\locvar{\ci}>0$ & $1$ (Inter) & $16$ \\
  2659. \bottomrule\end{tabular}
  2660. \end{center}
  2661. \caption{Minimum Quantization Values}
  2662. \label{tab:qmin}
  2663. \end{table}
  2664. \item
  2665. If \locvar{\ci} equals zero, assign $\locvar{QSCALE}$ the value
  2666. $\bitvar{DCSCALE}[\bitvar{\qi}]$.
  2667. \item
  2668. Else, assign $\locvar{QSCALE}$ the value
  2669. $\bitvar{ACSCALE}[\bitvar{\qi}]$.
  2670. \item
  2671. Assign $\bitvar{QMAT}[\locvar{\ci}]$ the value
  2672. \begin{displaymath}
  2673. \max(\locvar{QMIN},
  2674. \min((\locvar{QSCALE}*\locvar{BM}[\locvar{\ci}]//100)*4,4096)).
  2675. \end{displaymath}
  2676. \end{enumerate}
  2677. \end{enumerate}
  2678. \subsection{DCT Token Huffman Tables}
  2679. \label{sub:huffman-tables}
  2680. \paragraph{Input parameters:} None.
  2681. \paragraph{Output parameters:}\hfill\\*
  2682. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2683. \multicolumn{1}{c}{Name} &
  2684. \multicolumn{1}{c}{Type} &
  2685. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2686. \multicolumn{1}{c}{Signed?} &
  2687. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2688. \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
  2689. & An 80-element array of Huffman tables
  2690. with up to 32 entries each. \\
  2691. \bottomrule\end{tabularx}
  2692. \paragraph{Variables used:}\hfill\\*
  2693. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2694. \multicolumn{1}{c}{Name} &
  2695. \multicolumn{1}{c}{Type} &
  2696. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2697. \multicolumn{1}{c}{Signed?} &
  2698. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2699. \locvar{HBITS} & Bit string & 32 & No & A string of up to 32 bits. \\
  2700. \locvar{TOKEN} & Integer & 5 & No & A single DCT token value. \\
  2701. \locvar{ISLEAF} & Integer & 1 & No & Flag that indicates if the current
  2702. node of the tree being decoded is a leaf node. \\
  2703. \bottomrule\end{tabularx}
  2704. \medskip
  2705. The Huffman tables used to decode DCT tokens are stored in the setup header in
  2706. the form of a binary tree.
  2707. This enforces the requirements that the code be full---so that any sequence of
  2708. bits will produce a valid sequence of tokens---and that the code be
  2709. prefix-free so that there is no ambiguity when decoding.
  2710. One more restriction is placed on the tables that is not explicitly enforced by
  2711. the bitstream syntax, but nevertheless must be obeyed by compliant encoders.
  2712. There must be no more than 32 entries in a single table.
  2713. Note that this restriction along with the fullness requirement limit the
  2714. maximum size of a single Huffman code to 32 bits.
  2715. It is probably a good idea to enforce this latter consequence explicitly when
  2716. implementing the decoding procedure as a recursive algorithm, so as to prevent
  2717. a possible stack overflow given an invalid bitstream.
  2718. Although there are 32 different DCT tokens, and thus a normal table will have
  2719. exactly 32 entries, this is not explicitly required.
  2720. It is allowable to use a Huffman code that omits some---but not all---of the
  2721. possible token values.
  2722. It is also allowable, if not particularly useful, to specify multiple codes for
  2723. the same token value in a single table.
  2724. Note also that token values may appear in the tree in any order.
  2725. In particular, it is not safe to assume that token value zero (which ends a
  2726. single block), has a Huffman code of all zeros.
  2727. The tree is decoded as follows:
  2728. \begin{enumerate}
  2729. \item
  2730. For each consecutive value of \locvar{\hti} from $0$ to $79$, inclusive:
  2731. \begin{enumerate}
  2732. \item
  2733. Set \locvar{HBITS} to the empty string.
  2734. \item
  2735. \label{step:huff-tree-loop}
  2736. If \locvar{HBITS} is longer than 32 bits in length, stop.
  2737. The stream is undecodable.
  2738. \item
  2739. Read a 1-bit unsigned integer as \locvar{ISLEAF}.
  2740. \item
  2741. If \locvar{ISLEAF} is one:
  2742. \begin{enumerate}
  2743. \item
  2744. If the number of entries in table $\bitvar{HTS}[\locvar{\hti}]$ is already 32,
  2745. stop.
  2746. The stream is undecodable.
  2747. \item
  2748. Read a 5-bit unsigned integer as \locvar{TOKEN}.
  2749. \item
  2750. Add the pair $(\locvar{HBITS},\locvar{TOKEN})$ to Huffman table
  2751. $\bitvar{HTS}[\locvar{\hti}]$.
  2752. \end{enumerate}
  2753. \item
  2754. Otherwise:
  2755. \begin{enumerate}
  2756. \item
  2757. Add a `0' to the end of \locvar{HBITS}.
  2758. \item
  2759. Decode the `0' sub-tree using this procedure, starting from
  2760. step~\ref{step:huff-tree-loop}.
  2761. \item
  2762. Remove the `0' from the end of \locvar{HBITS} and add a `1' to the end of
  2763. \locvar{HBITS}.
  2764. \item
  2765. Decode the `1' sub-tree using this procedure, starting from
  2766. step~\ref{step:huff-tree-loop}.
  2767. \item
  2768. Remove the `1' from the end of \locvar{HBITS}.
  2769. \end{enumerate}
  2770. \end{enumerate}
  2771. \end{enumerate}
  2772. \paragraph{VP3 Compatibility}
  2773. The DCT token Huffman tables are hardcoded in VP3.
  2774. The values used are given in Appendix~\ref{app:vp3-huffman-tables}.
  2775. \subsection{Setup Header Decode}
  2776. \paragraph{Input parameters:} None.
  2777. \paragraph{Output parameters:}\hfill\\*
  2778. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2779. \multicolumn{1}{c}{Name} &
  2780. \multicolumn{1}{c}{Type} &
  2781. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2782. \multicolumn{1}{c}{Signed?} &
  2783. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2784. \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
  2785. 7 & No & A 64-element array of loop filter limit
  2786. values. \\
  2787. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2788. 16 & No & A 64-element array of scale values for
  2789. AC coefficients for each \qi\ value. \\
  2790. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  2791. 16 & No & A 64-element array of scale values for
  2792. the DC coefficient for each \qi\ value. \\
  2793. \bitvar{NBMS} & Integer & 10 & No & The number of base matrices. \\
  2794. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2795. 8 & No & A $\bitvar{NBMS}\times 64$ array
  2796. containing the base matrices. \\
  2797. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  2798. 6 & No & A $2\times 3$ array containing the
  2799. number of quant ranges for a given \qti\ and \pli, respectively.
  2800. This is at most $63$. \\
  2801. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2802. 6 & No & A $2\times 3\times 63$ array of the
  2803. sizes of each quant range for a given \qti\ and \pli, respectively.
  2804. Only the first $\bitvar{NQRS}[\qti][\pli]$ values will be used. \\
  2805. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  2806. 9 & No & A $2\times 3\times 64$ array of the
  2807. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  2808. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values will be used. \\
  2809. \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
  2810. & An 80-element array of Huffman tables
  2811. with up to 32 entries each. \\
  2812. \bottomrule\end{tabularx}
  2813. \paragraph{Variables used:} None.
  2814. \medskip
  2815. The complete setup header is decoded as follows:
  2816. \begin{enumerate}
  2817. \item
  2818. Decode the common header fields according to the procedure described in
  2819. Section~\ref{sub:common-header}.
  2820. If \bitvar{HEADERTYPE} returned by this procedure is not \hex{82}, then stop.
  2821. This packet is not the setup header.
  2822. \item
  2823. Decode the loop filter limit value table using the procedure given in
  2824. Section~\ref{sub:loop-filter-limits} into \bitvar{LFLIMS}.
  2825. \item
  2826. Decode the quantization parameters using the procedure given in
  2827. Section~\ref{sub:quant-params}.
  2828. The results are stored in \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{NBMS},
  2829. \bitvar{BMS}, \bitvar{NQRS}, \bitvar{QRSIZES}, and \bitvar{QRBMIS}.
  2830. \item
  2831. Decode the DCT token Huffman tables using the procedure given in
  2832. Section~\ref{sub:huffman-tables} into \bitvar{HTS}.
  2833. \end{enumerate}
  2834. \chapter{Frame Decode}
  2835. This section describes the complete procedure necessary to decode a single
  2836. frame.
  2837. This begins with the frame header, followed by coded block flags, macro block
  2838. modes, motion vectors, block-level \qi\ values, and finally the DCT residual
  2839. tokens, which are used to reconstruct the frame.
  2840. \section{Frame Header Decode}
  2841. \label{sub:frame-header}
  2842. \paragraph{Input parameters:} None.
  2843. \paragraph{Output parameters:}\hfill\\*
  2844. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2845. \multicolumn{1}{c}{Name} &
  2846. \multicolumn{1}{c}{Type} &
  2847. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2848. \multicolumn{1}{c}{Signed?} &
  2849. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2850. \bitvar{FTYPE} & Integer & 1 & No & The frame type. \\
  2851. \bitvar{NQIS} & Integer & 2 & No & The number of \qi\ values. \\
  2852. \bitvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
  2853. 6 & No & An \bitvar{NQIS}-element array of
  2854. \qi\ values. \\
  2855. \bottomrule\end{tabularx}
  2856. \paragraph{Variables used:}\hfill\\*
  2857. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2858. \multicolumn{1}{c}{Name} &
  2859. \multicolumn{1}{c}{Type} &
  2860. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2861. \multicolumn{1}{c}{Signed?} &
  2862. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2863. \locvar{MOREQIS} & Integer & 1 & No & A flag indicating there are more
  2864. \qi\ values to be decoded. \\
  2865. \bottomrule\end{tabularx}
  2866. \medskip
  2867. The frame header selects which type of frame is being decoded, intra or inter,
  2868. and contains the list of \qi\ values that will be used in this frame.
  2869. The first \qi\ value will be used for {\em all} DC coefficients in all blocks.
  2870. This is done to ensure that DC prediction, which is done in the quantized
  2871. domain, works as expected.
  2872. The AC coefficients, however, can be dequantized using any \qi\ value on the
  2873. list, selected on a block-by-block basis.
  2874. \begin{enumerate}
  2875. \item
  2876. Read a 1-bit unsigned integer.
  2877. If the value read is not zero, stop.
  2878. This is not a data packet.
  2879. \item
  2880. Read a 1-bit unsigned integer as \bitvar{FTYPE}.
  2881. This is the type of frame being decoded, as given in
  2882. Table~\ref{tab:frame-type}.
  2883. If this is the first frame being decoded, this MUST be zero.
  2884. \begin{table}[htbp]
  2885. \begin{center}
  2886. \begin{tabular}{cl}\toprule
  2887. \bitvar{FTYPE} & Frame Type \\\midrule
  2888. $0$ & Intra frame \\
  2889. $1$ & Inter frame \\
  2890. \bottomrule\end{tabular}
  2891. \end{center}
  2892. \caption{Frame Type Values}
  2893. \label{tab:frame-type}
  2894. \end{table}
  2895. \item
  2896. Read in a 6-bit unsigned integer as $\bitvar{QIS}[0]$.
  2897. \item
  2898. Read a 1-bit unsigned integer as \locvar{MOREQIS}.
  2899. \item
  2900. If \locvar{MOREQIS} is zero, set \bitvar{NQIS} to 1.
  2901. \item
  2902. Otherwise:
  2903. \begin{enumerate}
  2904. \item
  2905. Read in a 6-bit unsigned integer as $\bitvar{QIS}[1]$.
  2906. \item
  2907. Read a 1-bit unsigned integer as \locvar{MOREQIS}.
  2908. \item
  2909. If \locvar{MOREQIS} is zero, set \bitvar{NQIS} to 2.
  2910. \item
  2911. Otherwise:
  2912. \begin{enumerate}
  2913. \item
  2914. Read in a 6-bit unsigned integer as $\bitvar{QIS}[2]$.
  2915. \item
  2916. Set \bitvar{NQIS} to 3.
  2917. \end{enumerate}
  2918. \end{enumerate}
  2919. \item
  2920. If \bitvar{FTYPE} is 0, read a 3-bit unsigned integer.
  2921. These bits are reserved.
  2922. If this value is not zero, stop.
  2923. This frame is not decodable according to this specification.
  2924. \end{enumerate}
  2925. \paragraph{VP3 Compatibility}
  2926. The precise format of the frame header is substantially different in Theora
  2927. than in VP3.
  2928. The original VP3 format includes a larger number of unused, reserved bits that
  2929. are required to be zero.
  2930. The original VP3 frame header also can contain only a single \qi\ value,
  2931. because VP3 does not support block-level \qi\ values and uses the same
  2932. \qi\ value for all the coefficients in a frame.
  2933. \section{Run-Length Encoded Bit Strings}
  2934. Two variations of run-length encoding are used to store sequences of bits for
  2935. the block coded flags and the block-level \qi\ values.
  2936. The procedures to decode these bit sequences are specified in the following two
  2937. sections.
  2938. \subsection{Long-Run Bit String Decode}
  2939. \label{sub:long-run}
  2940. \paragraph{Input 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{NBITS} & Integer & 36 & No & The number of bits to decode. \\
  2948. \bottomrule\end{tabularx}
  2949. \paragraph{Output parameters:}\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. \bitvar{BITS} & Bit string & & & The decoded bits. \\
  2957. \bottomrule\end{tabularx}
  2958. \paragraph{Variables used:}\hfill\\*
  2959. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  2960. \multicolumn{1}{c}{Name} &
  2961. \multicolumn{1}{c}{Type} &
  2962. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  2963. \multicolumn{1}{c}{Signed?} &
  2964. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  2965. \locvar{LEN} & Integer & 36 & No & The number of bits decoded so far. \\
  2966. \locvar{BIT} & Integer & 1 & No & The value associated with the current
  2967. run. \\
  2968. \locvar{RLEN} & Integer & 13 & No & The length of the current run. \\
  2969. \locvar{RBITS} & Integer & 4 & No & The number of extra bits needed to
  2970. decode the run length. \\
  2971. \locvar{RSTART} & Integer & 6 & No & The start of the possible run-length
  2972. values for a given Huffman code. \\
  2973. \locvar{ROFFS} & Integer & 12 & No & The offset from \locvar{RSTART} of the
  2974. run-length. \\
  2975. \bottomrule\end{tabularx}
  2976. \medskip
  2977. There is no practical limit to the number of consecutive 0's and 1's that can
  2978. be decoded with this procedure.
  2979. In reality, the run length is limited by the number of blocks in a single
  2980. frame, because more will never be requested.
  2981. A separate procedure described in Section~\ref{sub:short-run} is used when
  2982. there is a known limit on the maximum size of the runs.
  2983. For the first run, a single bit value is read, and then a Huffman-coded
  2984. representation of a run length is decoded, and that many copies of the bit
  2985. value are appended to the bit string.
  2986. For each consecutive run, the value of the bit is toggled instead of being read
  2987. from the bitstream.
  2988. The only exception is if the length of the previous run was 4129, the maximum
  2989. possible length encodable by the Huffman-coded representation.
  2990. In this case another bit value is read from the stream, to allow for
  2991. consecutive runs of 0's or 1's longer than this maximum.
  2992. Note that in both cases---for the first run and after a run of length 4129---if
  2993. no more bits are needed, then no bit value is read.
  2994. The complete decoding procedure is as follows:
  2995. \begin{enumerate}
  2996. \item
  2997. Assign \locvar{LEN} the value 0.
  2998. \item
  2999. Assign \bitvar{BITS} the empty string.
  3000. \item
  3001. If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
  3002. \bitvar{BITS}.
  3003. \item
  3004. Read a 1-bit unsigned integer as \locvar{BIT}.
  3005. \item
  3006. \label{step:long-run-loop}
  3007. Read a bit at a time until one of the Huffman codes given in
  3008. Table~\ref{tab:long-run} is recognized.
  3009. \begin{table}[htbp]
  3010. \begin{center}
  3011. \begin{tabular}{lrrl}\toprule
  3012. Huffman Code & \locvar{RSTART} & \locvar{RBITS} & Run Lengths \\\midrule
  3013. \bin{0} & $1$ & $0$ & $1$ \\
  3014. \bin{10} & $2$ & $1$ & $2\ldots 3$ \\
  3015. \bin{110} & $4$ & $1$ & $4\ldots 5$ \\
  3016. \bin{1110} & $6$ & $2$ & $6\ldots 9$ \\
  3017. \bin{11110} & $10$ & $3$ & $10\ldots 17$ \\
  3018. \bin{111110} & $18$ & $4$ & $18\ldots 33$ \\
  3019. \bin{111111} & $34$ & $12$ & $34\ldots 4129$ \\
  3020. \bottomrule\end{tabular}
  3021. \end{center}
  3022. \caption{Huffman Codes for Long Run Lengths}
  3023. \label{tab:long-run}
  3024. \end{table}
  3025. \item
  3026. Assign \locvar{RSTART} and \locvar{RBITS} the values given in
  3027. Table~\ref{tab:long-run} according to the Huffman code read.
  3028. \item
  3029. Read an \locvar{RBITS}-bit unsigned integer as \locvar{ROFFS}.
  3030. \item
  3031. Assign \locvar{RLEN} the value $(\locvar{RSTART}+\locvar{ROFFS})$.
  3032. \item
  3033. Append \locvar{RLEN} copies of \locvar{BIT} to \bitvar{BITS}.
  3034. \item
  3035. Add \locvar{RLEN} to the value \locvar{LEN}.
  3036. \locvar{LEN} MUST be less than or equal to \bitvar{NBITS}.
  3037. \item
  3038. If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
  3039. \bitvar{BITS}.
  3040. \item
  3041. If \locvar{RLEN} equals 4129, read a 1-bit unsigned integer as \locvar{BIT}.
  3042. \item
  3043. Otherwise, assign \locvar{BIT} the value $(1-\locvar{BIT})$.
  3044. \item
  3045. Continue decoding runs from step~\ref{step:long-run-loop}.
  3046. \end{enumerate}
  3047. \paragraph{VP3 Compatibility}
  3048. VP3 does not read a new bit value after decoding a run length of 4129.
  3049. This limits the maximum number of consecutive 0's or 1's to 4129 in
  3050. VP3-compatible streams.
  3051. For reasonable video sizes of $1920\times 1080$ or less in 4:2:0 format---the
  3052. only pixel format VP3 supports---this does not pose any problems because runs
  3053. longer than 4129 are not needed.
  3054. \subsection{Short-Run Bit String Decode}
  3055. \label{sub:short-run}
  3056. \paragraph{Input 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{NBITS} & Integer & 36 & No & The number of bits to decode. \\
  3064. \bottomrule\end{tabularx}
  3065. \paragraph{Output parameters:}\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. \bitvar{BITS} & Bit string & & & The decoded bits. \\
  3073. \bottomrule\end{tabularx}
  3074. \paragraph{Variables used:}\hfill\\*
  3075. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3076. \multicolumn{1}{c}{Name} &
  3077. \multicolumn{1}{c}{Type} &
  3078. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3079. \multicolumn{1}{c}{Signed?} &
  3080. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3081. \locvar{LEN} & Integer & 36 & No & The number of bits decoded so far. \\
  3082. \locvar{BIT} & Integer & 1 & No & The value associated with the current
  3083. run. \\
  3084. \locvar{RLEN} & Integer & 13 & No & The length of the current run. \\
  3085. \locvar{RBITS} & Integer & 4 & No & The number of extra bits needed to
  3086. decode the run length. \\
  3087. \locvar{RSTART} & Integer & 6 & No & The start of the possible run-length
  3088. values for a given Huffman code. \\
  3089. \locvar{ROFFS} & Integer & 12 & No & The offset from \locvar{RSTART} of the
  3090. run-length. \\
  3091. \bottomrule\end{tabularx}
  3092. \medskip
  3093. This procedure is similar to the procedure outlined in
  3094. Section~\ref{sub:long-run}, except that the maximum number of consecutive 0's
  3095. or 1's is limited to 30.
  3096. This is the maximum run length needed when encoding a bit for each of the 16
  3097. blocks in a super block when it is known that not all the bits in a super
  3098. block are the same.
  3099. The complete decoding procedure is as follows:
  3100. \begin{enumerate}
  3101. \item
  3102. Assign \locvar{LEN} the value 0.
  3103. \item
  3104. Assign \bitvar{BITS} the empty string.
  3105. \item
  3106. If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
  3107. \bitvar{BITS}.
  3108. \item
  3109. Read a 1-bit unsigned integer as \locvar{BIT}.
  3110. \item
  3111. \label{step:short-run-loop}
  3112. Read a bit at a time until one of the Huffman codes given in
  3113. Table~\ref{tab:short-run} is recognized.
  3114. \begin{table}[htbp]
  3115. \begin{center}
  3116. \begin{tabular}{lrrl}\toprule
  3117. Huffman Code & \locvar{RSTART} & \locvar{RBITS} & Run Lengths \\\midrule
  3118. \bin{0} & $1$ & $1$ & $1\ldots 2$ \\
  3119. \bin{10} & $3$ & $1$ & $3\ldots 4$ \\
  3120. \bin{110} & $5$ & $1$ & $5\ldots 6$ \\
  3121. \bin{1110} & $7$ & $2$ & $7\ldots 10$ \\
  3122. \bin{11110} & $11$ & $2$ & $11\ldots 14$ \\
  3123. \bin{11111} & $15$ & $4$ & $15\ldots 30$ \\
  3124. \bottomrule\end{tabular}
  3125. \end{center}
  3126. \caption{Huffman Codes for Short Run Lengths}
  3127. \label{tab:short-run}
  3128. \end{table}
  3129. \item
  3130. Assign \locvar{RSTART} and \locvar{RBITS} the values given in
  3131. Table~\ref{tab:short-run} according to the Huffman code read.
  3132. \item
  3133. Read an \locvar{RBITS}-bit unsigned integer as \locvar{ROFFS}.
  3134. \item
  3135. Assign \locvar{RLEN} the value $(\locvar{RSTART}+\locvar{ROFFS})$.
  3136. \item
  3137. Append \locvar{RLEN} copies of \locvar{BIT} to \bitvar{BITS}.
  3138. \item
  3139. Add \locvar{RLEN} to the value \locvar{LEN}.
  3140. \locvar{LEN} MUST be less than or equal to \bitvar{NBITS}.
  3141. \item
  3142. If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
  3143. \bitvar{BITS}.
  3144. \item
  3145. Assign \locvar{BIT} the value $(1-\locvar{BIT})$.
  3146. \item
  3147. Continue decoding runs from step~\ref{step:short-run-loop}.
  3148. \end{enumerate}
  3149. \section{Coded Block Flags Decode}
  3150. \label{sub:coded-blocks}
  3151. \paragraph{Input parameters:}\hfill\\*
  3152. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3153. \multicolumn{1}{c}{Name} &
  3154. \multicolumn{1}{c}{Type} &
  3155. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3156. \multicolumn{1}{c}{Signed?} &
  3157. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3158. \bitvar{FTYPE} & Integer & 1 & No & The frame type. \\
  3159. \bitvar{NSBS} & Integer & 32 & No & The total number of super blocks in a
  3160. frame. \\
  3161. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  3162. frame. \\
  3163. \bottomrule\end{tabularx}
  3164. \paragraph{Output parameters:}\hfill\\*
  3165. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3166. \multicolumn{1}{c}{Name} &
  3167. \multicolumn{1}{c}{Type} &
  3168. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3169. \multicolumn{1}{c}{Signed?} &
  3170. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3171. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3172. 1 & No & An \bitvar{NBS}-element array of flags
  3173. indicating which blocks are coded. \\
  3174. \bottomrule\end{tabularx}
  3175. \paragraph{Variables used:}\hfill\\*
  3176. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3177. \multicolumn{1}{c}{Name} &
  3178. \multicolumn{1}{c}{Type} &
  3179. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3180. \multicolumn{1}{c}{Signed?} &
  3181. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3182. \locvar{NBITS} & Integer & 36 & No & The length of a bit string to decode. \\
  3183. \locvar{BITS} & Bit string & & & A decoded set of flags. \\
  3184. \locvar{SBPCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3185. 1 & No & An \bitvar{NSBS}-element array of flags
  3186. indicating whether or not each super block is partially coded. \\
  3187. \locvar{SBFCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3188. 1 & No & An \bitvar{NSBS}-element array of flags
  3189. indicating whether or not each non-partially coded super block is fully
  3190. coded. \\
  3191. \locvar{\sbi} & Integer & 32 & No & The index of the current super
  3192. block. \\
  3193. \locvar{\bi} & Integer & 36 & No & The index of the current block in coded
  3194. order. \\
  3195. \bottomrule\end{tabularx}
  3196. \medskip
  3197. This procedure determines which blocks are coded in a given frame.
  3198. In an intra frame, it marks all blocks coded.
  3199. In an inter frame, however, any or all of the blocks may remain uncoded.
  3200. The output is a list of bit flags, one for each block, marking it coded or not
  3201. coded.
  3202. It is important to note that flags are still decoded for any blocks which lie
  3203. entirely outside the picture region, even though they are not displayed.
  3204. Encoders MAY choose to code such blocks.
  3205. Decoders MUST faithfully reconstruct such blocks, because their contents can be
  3206. used for predictors in future frames.
  3207. Flags are \textit{not} decoded for portions of a super block which lie outside
  3208. the full frame, as there are no blocks in those regions.
  3209. The complete procedure is as follows:
  3210. \begin{enumerate}
  3211. \item
  3212. If \bitvar{FTYPE} is zero (intra frame):
  3213. \begin{enumerate}
  3214. \item
  3215. For each consecutive value of \locvar{\bi} from 0 to $(\locvar{NBS}-1)$, assign
  3216. $\bitvar{BCODED}[\locvar{\bi}]$ the value one.
  3217. \end{enumerate}
  3218. \item
  3219. Otherwise (inter frame):
  3220. \begin{enumerate}
  3221. \item
  3222. Assign \locvar{NBITS} the value \bitvar{NSBS}.
  3223. \item
  3224. Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
  3225. described in Section~\ref{sub:long-run}.
  3226. This represents the list of partially coded super blocks.
  3227. \item
  3228. For each consecutive value of \locvar{\sbi} from 0 to $(\locvar{NSBS}-1)$,
  3229. remove the bit at the head of the string \locvar{BITS} and assign it to
  3230. $\locvar{SBPCODED}[\locvar{\sbi}]$.
  3231. \item
  3232. Assign \locvar{NBITS} the total number of super blocks such that \\
  3233. $\locvar{SBPCODED}[\locvar{\sbi}]$ equals zero.
  3234. \item
  3235. Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
  3236. described in Section~\ref{sub:long-run}.
  3237. This represents the list of fully coded super blocks.
  3238. \item
  3239. For each consecutive value of \locvar{\sbi} from 0 to $(\locvar{NSBS}-1)$ such
  3240. that $\locvar{SBPCODED}[\locvar{\sbi}]$ equals zero, remove the bit at the
  3241. head of the string \locvar{BITS} and assign it to
  3242. $\locvar{SBFCODED}[\locvar{\sbi}]$.
  3243. \item
  3244. Assign \locvar{NBITS} the number of blocks contained in super blocks where
  3245. $\locvar{SBPCODED}[\locvar{\sbi}]$ equals one.
  3246. Note that this might {\em not} be equal to 16 times the number of partially
  3247. coded super blocks, since super blocks which overlap the edge of the frame
  3248. will have fewer than 16 blocks in them.
  3249. \item
  3250. Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
  3251. described in Section~\ref{sub:short-run}.
  3252. \item
  3253. For each block in coded order---indexed by \locvar{\bi}:
  3254. \begin{enumerate}
  3255. \item
  3256. Assign \locvar{\sbi} the index of the super block containing block
  3257. \locvar{\bi}.
  3258. \item
  3259. If $\locvar{SBPCODED}[\locvar{\sbi}]$ is zero, assign
  3260. $\bitvar{BCODED}[\locvar{\bi}]$ the value $\locvar{SBFCODED}[\locvar{\sbi}]$.
  3261. \item
  3262. Otherwise, remove the bit at the head of the string \locvar{BITS} and assign it
  3263. to $\bitvar{BCODED}[\locvar{\bi}]$.
  3264. \end{enumerate}
  3265. \end{enumerate}
  3266. \end{enumerate}
  3267. \section{Macro Block Coding Modes}
  3268. \label{sub:mb-modes}
  3269. \paragraph{Input parameters:}\hfill\\*
  3270. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3271. \multicolumn{1}{c}{Name} &
  3272. \multicolumn{1}{c}{Type} &
  3273. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3274. \multicolumn{1}{c}{Signed?} &
  3275. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3276. \bitvar{FTYPE} & Integer & 1 & No & The frame type. \\
  3277. \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
  3278. frame. \\
  3279. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  3280. frame. \\
  3281. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3282. 1 & No & An \bitvar{NBS}-element array of flags
  3283. indicating which blocks are coded. \\
  3284. \bottomrule\end{tabularx}
  3285. \paragraph{Output parameters:}\hfill\\*
  3286. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3287. \multicolumn{1}{c}{Name} &
  3288. \multicolumn{1}{c}{Type} &
  3289. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3290. \multicolumn{1}{c}{Signed?} &
  3291. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3292. \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3293. 3 & No & An \bitvar{NMBS}-element array of coding
  3294. modes for each macro block. \\
  3295. \bottomrule\end{tabularx}
  3296. \paragraph{Variables used:}\hfill\\*
  3297. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3298. \multicolumn{1}{c}{Name} &
  3299. \multicolumn{1}{c}{Type} &
  3300. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3301. \multicolumn{1}{c}{Signed?} &
  3302. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3303. \locvar{MSCHEME} & Integer & 3 & No & The mode coding scheme. \\
  3304. \locvar{MALPHABET} & \multicolumn{1}{p{40pt}}{Integer array}
  3305. & 3 & No & The list of modes corresponding to each
  3306. Huffman code. \\
  3307. \locvar{\mbi} & Integer & 32 & No & The index of the current macro
  3308. block. \\
  3309. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  3310. coded order. \\
  3311. \locvar{\mi} & Integer & 3 & No & The index of a Huffman code from
  3312. Table~\ref{tab:mode-codes}, starting from $0$. \\
  3313. \bottomrule\end{tabularx}
  3314. \medskip
  3315. In an intra frame, every macro block marked as coded in INTRA mode.
  3316. In an inter frame, however, a macro block can be coded in one of eight coding
  3317. modes, given in Table~\ref{tab:coding-modes}.
  3318. All of the blocks in all color planes contained in a macro block will be
  3319. assigned the coding mode of that macro block.
  3320. \begin{table}[htbp]
  3321. \begin{center}
  3322. \begin{tabular}{cl}\toprule
  3323. Index & Coding Mode \\\midrule
  3324. $0$ & INTER\_NOMV \\
  3325. $1$ & INTRA \\
  3326. $2$ & INTER\_MV \\
  3327. $3$ & INTER\_MV\_LAST \\
  3328. $4$ & INTER\_MV\_LAST2 \\
  3329. $5$ & INTER\_GOLDEN\_NOMV \\
  3330. $6$ & INTER\_GOLDEN\_MV \\
  3331. $7$ & INTER\_MV\_FOUR \\
  3332. \bottomrule\end{tabular}
  3333. \end{center}
  3334. \caption{Macro Block Coding Modes}
  3335. \label{tab:coding-modes}
  3336. \end{table}
  3337. An important thing to note is that a coding mode is only stored in the
  3338. bitstream for a macro block if it has at least one {\em luma} block coded.
  3339. A macro block that contains coded blocks in the chroma planes, but not in the
  3340. luma plane, MUST be coded in INTER\_NOMV mode.
  3341. Thus, no coding mode needs to be decoded for such a macro block.
  3342. Coding modes are encoded using one of eight different schemes.
  3343. Schemes 0 through 6 use the same simple Huffman code to represent the mode
  3344. numbers, as given in Table~\ref{tab:mode-codes}.
  3345. The difference in the schemes is the mode number assigned to each code.
  3346. Scheme 0 uses an assignment specified in the bitstream, while schemes 1--6 use
  3347. a fixed assignment, also given in Table~\ref{tab:mode-codes}.
  3348. Scheme 7 simply codes each mode directly in the bitstream using three bits.
  3349. \begin{table}[htbp]
  3350. \begin{center}
  3351. \begin{tabular}{lccccccc}\toprule
  3352. Scheme & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & $7$ \\\cmidrule{2-7}
  3353. Huffman Code & \multicolumn{6}{c}{Coding Mode} & \locvar{\mi} \\\midrule
  3354. \bin{0} & $3$ & $3$ & $3$ & $3$ & $0$ & $0$ & $0$ \\
  3355. \bin{10} & $4$ & $4$ & $2$ & $2$ & $3$ & $5$ & $1$ \\
  3356. \bin{110} & $2$ & $0$ & $4$ & $0$ & $4$ & $3$ & $2$ \\
  3357. \bin{1110} & $0$ & $2$ & $0$ & $4$ & $2$ & $4$ & $3$ \\
  3358. \bin{11110} & $1$ & $1$ & $1$ & $1$ & $1$ & $2$ & $4$ \\
  3359. \bin{111110} & $5$ & $5$ & $5$ & $5$ & $5$ & $1$ & $5$ \\
  3360. \bin{1111110} & $6$ & $6$ & $6$ & $6$ & $6$ & $6$ & $6$ \\
  3361. \bin{1111111} & $7$ & $7$ & $7$ & $7$ & $7$ & $7$ & $7$ \\
  3362. \bottomrule\end{tabular}
  3363. \end{center}
  3364. \caption{Macro Block Mode Schemes}
  3365. \label{tab:mode-codes}
  3366. \end{table}
  3367. \begin{enumerate}
  3368. \item
  3369. If \bitvar{FTYPE} is 0 (intra frame):
  3370. \begin{enumerate}
  3371. \item
  3372. For each consecutive value of \locvar{\mbi} from 0 to $(\bitvar{NMBS}-1)$,
  3373. inclusive, assign $\bitvar{MBMODES}[\mbi]$ the value 1 (INTRA).
  3374. \end{enumerate}
  3375. \item
  3376. Otherwise (inter frame):
  3377. \begin{enumerate}
  3378. \item
  3379. Read a 3-bit unsigned integer as \locvar{MSCHEME}.
  3380. \item
  3381. If \locvar{MSCHEME} is 0:
  3382. \begin{enumerate}
  3383. \item
  3384. For each consecutive value of \locvar{MODE} from 0 to 7, inclusive:
  3385. \begin{enumerate}
  3386. \item
  3387. Read a 3-bit unsigned integer as \locvar{\mi}.
  3388. \item
  3389. Assign $\locvar{MALPHABET}[\mi]$ the value \locvar{MODE}.
  3390. \end{enumerate}
  3391. \end{enumerate}
  3392. \item
  3393. Otherwise, if \locvar{MSCHEME} is not 7, assign the entries of
  3394. \locvar{MALPHABET} the values in the corresponding column of
  3395. Table~\ref{tab:mode-codes}.
  3396. \item
  3397. For each consecutive macro block in coded order (cf.
  3398. Section~\ref{sec:mbs})---indexed by \locvar{\mbi}:
  3399. \begin{enumerate}
  3400. \item
  3401. If a block \locvar{\bi} in the luma plane of macro block \locvar{\mbi} exists
  3402. such that $\bitvar{BCODED}[\locvar{\bi}]$ is 1:
  3403. \begin{enumerate}
  3404. \item
  3405. If \locvar{MSCHEME} is not 7, read one bit at a time until one of the Huffman
  3406. codes in Table~\ref{tab:mode-codes} is recognized, and assign
  3407. $\bitvar{MBMODES}[\locvar{\mbi}]$ the value
  3408. $\locvar{MALPHABET}[\locvar{\mi}]$, where \locvar{\mi} is the index of the
  3409. Huffman code decoded.
  3410. \item
  3411. Otherwise, read a 3-bit unsigned integer as $\bitvar{MBMODES}[\locvar{\mbi}]$.
  3412. \end{enumerate}
  3413. \item
  3414. Otherwise, if no luma-plane blocks in the macro block are coded, assign
  3415. $\bitvar{MBMODES}[\locvar{\mbi}]$ the value 0 (INTER\_NOMV).
  3416. \end{enumerate}
  3417. \end{enumerate}
  3418. \end{enumerate}
  3419. \section{Motion Vectors}
  3420. In an intra frame, no motion vectors are used, and so motion vector decoding is
  3421. skipped.
  3422. In an inter frame, however, many of the inter coding modes require a motion
  3423. vector in order to specify an offset into the reference frame from which to
  3424. predict a block.
  3425. These procedures assigns such a motion vector to every block.
  3426. \subsection{Motion Vector Decode}
  3427. \label{sub:mv-decode}
  3428. \paragraph{Input 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{MVMODE} & Integer & 1 & No & The motion vector decoding method. \\
  3436. \bottomrule\end{tabularx}
  3437. \paragraph{Output parameters:}\hfill\\*
  3438. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3439. \multicolumn{1}{c}{Name} &
  3440. \multicolumn{1}{c}{Type} &
  3441. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3442. \multicolumn{1}{c}{Signed?} &
  3443. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3444. \bitvar{MVX} & Integer & 6 & Yes & The X component of the motion
  3445. vector. \\
  3446. \bitvar{MVY} & Integer & 6 & Yes & The Y component of the motion
  3447. vector. \\
  3448. \bottomrule\end{tabularx}
  3449. \paragraph{Variables used:}\hfill\\*
  3450. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3451. \multicolumn{1}{c}{Name} &
  3452. \multicolumn{1}{c}{Type} &
  3453. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3454. \multicolumn{1}{c}{Signed?} &
  3455. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3456. \locvar{MVSIGN} & Integer & 1 & No & The sign of the motion vector component
  3457. just decoded. \\
  3458. \bottomrule\end{tabularx}
  3459. \medskip
  3460. The individual components of a motion vector can be coded using one of two
  3461. methods.
  3462. The first uses a variable length Huffman code, given in
  3463. Table~\ref{tab:mv-huff-codes}.
  3464. The second encodes the magnitude of the component directly in 5 bits, and the
  3465. sign in one bit.
  3466. Note that in this case there are two representations for the value zero.
  3467. For compatibility with VP3, a sign bit is read even if the magnitude read is
  3468. zero.
  3469. One scheme is chosen and used for the entire frame.
  3470. Each component can take on integer values from $-31\ldots 31$, inclusive, at
  3471. half-pixel resolution, i.e. $-15.5\ldots 15.5$ pixels in the luma plane.
  3472. For each subsampled axis in the chroma planes, the corresponding motion vector
  3473. component is interpreted as being at quarter-pixel resolution, i.e.
  3474. $-7.75\ldots 7.75$ pixels.
  3475. The precise details of how these vectors are used to compute predictors for
  3476. each block are described in Section~\ref{sec:predictors}.
  3477. \begin{table}[ht]
  3478. \begin{center}
  3479. \begin{tabular}{lrlr}\toprule
  3480. Huffman Code & Value & Huffman Code & Value \\\midrule
  3481. \bin{000} & $0$ \\
  3482. \bin{001} & $1$ & \bin{010} & $-1$ \\
  3483. \bin{0110} & $2$ & \bin{0111} & $-2$ \\
  3484. \bin{1000} & $3$ & \bin{1001} & $-3$ \\
  3485. \bin{101000} & $4$ & \bin{101001} & $-4$ \\
  3486. \bin{101010} & $5$ & \bin{101011} & $-5$ \\
  3487. \bin{101100} & $6$ & \bin{101101} & $-6$ \\
  3488. \bin{101110} & $7$ & \bin{101111} & $-7$ \\
  3489. \bin{1100000} & $8$ & \bin{1100001} & $-8$ \\
  3490. \bin{1100010} & $9$ & \bin{1100011} & $-9$ \\
  3491. \bin{1100100} & $10$ & \bin{1100101} & $-10$ \\
  3492. \bin{1100110} & $11$ & \bin{1100111} & $-11$ \\
  3493. \bin{1101000} & $12$ & \bin{1101001} & $-12$ \\
  3494. \bin{1101010} & $13$ & \bin{1101011} & $-13$ \\
  3495. \bin{1101100} & $14$ & \bin{1101101} & $-14$ \\
  3496. \bin{1101110} & $15$ & \bin{1101111} & $-15$ \\
  3497. \bin{11100000} & $16$ & \bin{11100001} & $-16$ \\
  3498. \bin{11100010} & $17$ & \bin{11100011} & $-17$ \\
  3499. \bin{11100100} & $18$ & \bin{11100101} & $-18$ \\
  3500. \bin{11100110} & $19$ & \bin{11100111} & $-19$ \\
  3501. \bin{11101000} & $20$ & \bin{11101001} & $-20$ \\
  3502. \bin{11101010} & $21$ & \bin{11101011} & $-21$ \\
  3503. \bin{11101100} & $22$ & \bin{11101101} & $-22$ \\
  3504. \bin{11101110} & $23$ & \bin{11101111} & $-23$ \\
  3505. \bin{11110000} & $24$ & \bin{11110001} & $-24$ \\
  3506. \bin{11110010} & $25$ & \bin{11110011} & $-25$ \\
  3507. \bin{11110100} & $26$ & \bin{11110101} & $-26$ \\
  3508. \bin{11110110} & $27$ & \bin{11110111} & $-27$ \\
  3509. \bin{11111000} & $28$ & \bin{11111001} & $-28$ \\
  3510. \bin{11111010} & $29$ & \bin{11111011} & $-29$ \\
  3511. \bin{11111100} & $30$ & \bin{11111101} & $-30$ \\
  3512. \bin{11111110} & $31$ & \bin{11111111} & $-31$ \\
  3513. \bottomrule\end{tabular}
  3514. \end{center}
  3515. \caption{Huffman Codes for Motion Vector Components}
  3516. \label{tab:mv-huff-codes}
  3517. \end{table}
  3518. A single motion vector is decoded is follows:
  3519. \begin{enumerate}
  3520. \item
  3521. If \bitvar{MVMODE} is 0:
  3522. \begin{enumerate}
  3523. \item
  3524. Read 1 bit at a time until one of the Huffman codes in
  3525. Table~\ref{tab:mv-huff-codes} is recognized, and assign the value to
  3526. \locvar{MVX}.
  3527. \item
  3528. Read 1 bit at a time until one of the Huffman codes in
  3529. Table~\ref{tab:mv-huff-codes} is recognized, and assign the value to
  3530. \locvar{MVY}.
  3531. \end{enumerate}
  3532. \item
  3533. Otherwise:
  3534. \begin{enumerate}
  3535. \item
  3536. Read a 5-bit unsigned integer as \bitvar{MVX}.
  3537. \item
  3538. Read a 1-bit unsigned integer as \locvar{MVSIGN}.
  3539. \item
  3540. If \locvar{MVSIGN} is 1, assign \bitvar{MVX} the value $-\bitvar{MVX}$.
  3541. \item
  3542. Read a 5-bit unsigned integer as \bitvar{MVY}.
  3543. \item
  3544. Read a 1-bit unsigned integer as \locvar{MVSIGN}.
  3545. \item
  3546. If \locvar{MVSIGN} is 1, assign \bitvar{MVY} the value $-\bitvar{MVY}$.
  3547. \end{enumerate}
  3548. \end{enumerate}
  3549. \subsection{Macro Block Motion Vector Decode}
  3550. \label{sub:mb-mv-decode}
  3551. \paragraph{Input parameters:}\hfill\\*
  3552. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3553. \multicolumn{1}{c}{Name} &
  3554. \multicolumn{1}{c}{Type} &
  3555. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3556. \multicolumn{1}{c}{Signed?} &
  3557. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3558. \bitvar{PF} & Integer & 2 & No & The pixel format. \\
  3559. \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
  3560. frame. \\
  3561. \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3562. 3 & No & An \bitvar{NMBS}-element array of coding
  3563. modes for each macro block. \\
  3564. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  3565. frame. \\
  3566. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3567. 1 & No & An \bitvar{NBS}-element array of flags
  3568. indicating which blocks are coded. \\
  3569. \bottomrule\end{tabularx}
  3570. \paragraph{Output parameters:}\hfill\\*
  3571. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3572. \multicolumn{1}{c}{Name} &
  3573. \multicolumn{1}{c}{Type} &
  3574. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3575. \multicolumn{1}{c}{Signed?} &
  3576. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3577. \bitvar{MVECTS} & \multicolumn{1}{p{50pt}}{Array of 2D Integer Vectors} &
  3578. 6 & Yes & An \bitvar{NBS}-element array of
  3579. motion vectors for each block. \\
  3580. \bottomrule\end{tabularx}
  3581. \paragraph{Variables used:}\hfill\\*
  3582. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3583. \multicolumn{1}{c}{Name} &
  3584. \multicolumn{1}{c}{Type} &
  3585. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3586. \multicolumn{1}{c}{Signed?} &
  3587. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3588. \locvar{LAST1} & \multicolumn{1}{p{50pt}}{2D Integer Vector} &
  3589. 6 & Yes & The last motion vector. \\
  3590. \locvar{LAST2} & \multicolumn{1}{p{50pt}}{2D Integer Vector} &
  3591. 6 & Yes & The second to last motion vector. \\
  3592. \locvar{MVX} & Integer & 6 & Yes & The X component of a motion vector. \\
  3593. \locvar{MVY} & Integer & 6 & Yes & The Y component of a motion vector. \\
  3594. \locvar{\mbi} & Integer & 32 & No & The index of the current macro
  3595. block. \\
  3596. \locvar{A} & Integer & 36 & No & The index of the lower-left luma block
  3597. in the macro block. \\
  3598. \locvar{B} & Integer & 36 & No & The index of the lower-right luma
  3599. block in the macro block. \\
  3600. \locvar{C} & Integer & 36 & No & The index of the upper-left luma block
  3601. in the macro block. \\
  3602. \locvar{D} & Integer & 36 & No & The index of the upper-right luma
  3603. block in the macro block. \\
  3604. \locvar{E} & Integer & 36 & No & The index of a chroma block in the
  3605. macro block, depending on the pixel format. \\
  3606. \locvar{F} & Integer & 36 & No & The index of a chroma block in the
  3607. macro block, depending on the pixel format. \\
  3608. \locvar{G} & Integer & 36 & No & The index of a chroma block in the
  3609. macro block, depending on the pixel format. \\
  3610. \locvar{H} & Integer & 36 & No & The index of a chroma block in the
  3611. macro block, depending on the pixel format. \\
  3612. \locvar{I} & Integer & 36 & No & The index of a chroma block in the
  3613. macro block, depending on the pixel format. \\
  3614. \locvar{J} & Integer & 36 & No & The index of a chroma block in the
  3615. macro block, depending on the pixel format. \\
  3616. \locvar{K} & Integer & 36 & No & The index of a chroma block in the
  3617. macro block, depending on the pixel format. \\
  3618. \locvar{L} & Integer & 36 & No & The index of a chroma block in the
  3619. macro block, depending on the pixel format. \\
  3620. \bottomrule\end{tabularx}
  3621. \medskip
  3622. Motion vectors are stored for each macro block.
  3623. In every mode except for INTER\_MV\_FOUR, every block in all the color planes
  3624. are assigned the same motion vector.
  3625. In INTER\_MV\_FOUR mode, all four blocks in the luma plane are assigned their
  3626. own motion vector, and motion vectors for blocks in the chroma planes are
  3627. computed from these, using averaging appropriate to the pixel format.
  3628. For INTER\_MV and INTER\_GOLDEN\_MV modes, a single motion vector is decoded
  3629. and applied to each block.
  3630. For INTER\_MV\_FOUR macro blocks, a motion vector is decoded for each coded
  3631. luma block, in raster order, not coded order.
  3632. Uncoded luma blocks receive the default $(0,0)$ vector for the purposes of
  3633. computing the chroma motion vectors.
  3634. None of the remaining macro block coding modes require decoding motion vectors
  3635. from the stream.
  3636. INTRA mode does not use a motion-compensated predictor, and thus does not
  3637. require a motion vector.
  3638. Both the INTER\_NOMV and the INTER\_GOLDEN\_NOMV modes use the default
  3639. vector $(0,0)$ for each block.
  3640. This also includes all macro blocks with no coded luma blocks, as they are
  3641. coded in INTER\_NOMV mode by definition.
  3642. The modes INTER\_MV\_LAST and INTER\_MV\_LAST2 use the motion vector from the
  3643. last macro block (in coded order) and the second to last macro block,
  3644. respectively, that contained a motion vector pointing to the previous frame.
  3645. Thus no explicit motion vector needs to be decoded for these modes.
  3646. Macro blocks coded in INTRA mode or one of the GOLDEN modes are not considered
  3647. in this process.
  3648. For macro blocks coded in INTER\_MV\_FOUR mode, this process uses the vector
  3649. from the last coded luma block in the macro block, again in raster order, not
  3650. coded order.
  3651. This is usually the upper-right block.
  3652. If an insufficient number of macro blocks have been coded in one of the INTER
  3653. modes, then the $(0,0)$ vector is used instead.
  3654. The motion vectors are decoded from the stream as follows:
  3655. \begin{enumerate}
  3656. \item
  3657. Assign \locvar{LAST1} and \locvar{LAST2} both the value $(0,0)$.
  3658. \item
  3659. Read a 1-bit unsigned integer as \locvar{MVMODE}.
  3660. Note that this value is read even if no macro blocks require a motion vector to
  3661. be decoded.
  3662. \item
  3663. For each consecutive value of \locvar{\mbi} from 0 to $(\bitvar{NMBS}-1)$:
  3664. \begin{enumerate}
  3665. \item
  3666. If $\bitvar{MBMODES}[\locvar{\mbi}]$ is 7 (INTER\_MV\_FOUR):
  3667. \begin{enumerate}
  3668. \item
  3669. Let \locvar{A}, \locvar{B}, \locvar{C}, and \locvar{D} be the indices in coded
  3670. order \locvar{\bi} of the luma blocks in macro block \locvar{\mbi}, arranged
  3671. into raster order.
  3672. Thus, \locvar{A} is the index in coded order of the block in the lower left,
  3673. \locvar{B} the lower right, \locvar{C} the upper left, and \locvar{D} the
  3674. upper right. % TODO: as shown in Figure~REF.
  3675. \item If $\bitvar{BCODED}[\locvar{A}]$ is non-zero:
  3676. \begin{enumerate}
  3677. \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
  3678. the procedure described in Section~\ref{sub:mv-decode}.
  3679. \item Assign $\bitvar{MVECTS}[\locvar{A}]$ the value
  3680. $(\locvar{MVX},\locvar{MVY})$.
  3681. \end{enumerate}
  3682. \item Otherwise, assign $\bitvar{MVECTS}[\locvar{A}]$ the value $(0,0)$.
  3683. \item If $\bitvar{BCODED}[\locvar{B}]$ is non-zero:
  3684. \begin{enumerate}
  3685. \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
  3686. the procedure described in Section~\ref{sub:mv-decode}.
  3687. \item Assign $\bitvar{MVECTS}[\locvar{B}]$ the value
  3688. $(\locvar{MVX},\locvar{MVY})$.
  3689. \end{enumerate}
  3690. \item
  3691. Otherwise assign $\bitvar{MVECTS}[\locvar{B}]$ the value $(0,0)$.
  3692. \item If $\bitvar{BCODED}[\locvar{C}]$ is non-zero:
  3693. \begin{enumerate}
  3694. \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
  3695. the procedure described in Section~\ref{sub:mv-decode}.
  3696. \item Assign $\bitvar{MVECTS}[\locvar{C}]$ the value
  3697. $(\locvar{MVX},\locvar{MVY})$.
  3698. \end{enumerate}
  3699. \item Otherwise assign $\bitvar{MVECTS}[\locvar{C}]$ the value $(0,0)$.
  3700. \item If $\bitvar{BCODED}[\locvar{D}]$ is non-zero:
  3701. \begin{enumerate}
  3702. \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
  3703. the procedure described in Section~\ref{sub:mv-decode}.
  3704. \item Assign $\bitvar{MVECTS}[\locvar{D}]$ the value
  3705. $(\locvar{MVX},\locvar{MVY})$.
  3706. \end{enumerate}
  3707. \item
  3708. Otherwise, assign $\bitvar{MVECTS}[\locvar{D}]$ the value $(0,0)$.
  3709. \item
  3710. If \bitvar{PF} is 0 (4:2:0):
  3711. \begin{enumerate}
  3712. \item
  3713. Let \locvar{E} and \locvar{F} be the index in coded order of the one block in
  3714. the macro block from the $C_b$ and $C_r$ planes, respectively.
  3715. \item
  3716. Assign $\bitvar{MVECTS}[\locvar{E}]$ and $\bitvar{MVECTS}[\locvar{F}]$ the
  3717. value
  3718. \begin{multline*}
  3719. (\round\biggl(\frac{\begin{aligned}
  3720. \bitvar{MVECTS}[\locvar{A}]_x+\bitvar{MVECTS}[\locvar{B}]_x+\\
  3721. \bitvar{MVECTS}[\locvar{C}]_x+\bitvar{MVECTS}[\locvar{D}]_x
  3722. \end{aligned}}{4}\biggr), \\
  3723. \round\biggl(\frac{\begin{aligned}
  3724. \bitvar{MVECTS}[\locvar{A}]_y+\bitvar{MVECTS}[\locvar{B}]_y+\\
  3725. \bitvar{MVECTS}[\locvar{C}]_y+\bitvar{MVECTS}[\locvar{D}]_y
  3726. \end{aligned}}{4}\biggr))
  3727. \end{multline*}
  3728. \end{enumerate}
  3729. \item
  3730. If \bitvar{PF} is 2 (4:2:2):
  3731. \begin{enumerate}
  3732. \item
  3733. Let \locvar{E} and \locvar{F} be the indices in coded order of the bottom and
  3734. top blocks in the macro block from the $C_b$ plane, respectively, and
  3735. \locvar{G} and \locvar{H} be the indices in coded order of the bottom and top
  3736. blocks in the $C_r$ plane, respectively. %TODO: as shown in Figure~REF.
  3737. \item
  3738. Assign $\bitvar{MVECTS}[\locvar{E}]$ and $\bitvar{MVECTS}[\locvar{G}]$ the
  3739. value
  3740. \begin{multline*}
  3741. (\round\left(\frac{
  3742. \bitvar{MVECTS}[\locvar{A}]_x+\bitvar{MVECTS}[\locvar{B}]_x}{2}\right), \\
  3743. \round\left(\frac{
  3744. \bitvar{MVECTS}[\locvar{A}]_y+\bitvar{MVECTS}[\locvar{B}]_y}{2}\right))
  3745. \end{multline*}
  3746. \item
  3747. Assign $\bitvar{MVECTS}[\locvar{F}]$ and $\bitvar{MVECTS}[\locvar{H}]$ the
  3748. value
  3749. \begin{multline*}
  3750. (\round\left(\frac{
  3751. \bitvar{MVECTS}[\locvar{C}]_x+\bitvar{MVECTS}[\locvar{D}]_x}{2}\right), \\
  3752. \round\left(\frac{
  3753. \bitvar{MVECTS}[\locvar{C}]_y+\bitvar{MVECTS}[\locvar{D}]_y}{2}\right))
  3754. \end{multline*}
  3755. \end{enumerate}
  3756. \item
  3757. If \bitvar{PF} is 3 (4:4:4):
  3758. \begin{enumerate}
  3759. \item
  3760. Let \locvar{E}, \locvar{F}, \locvar{G}, and \locvar{H} be the indices
  3761. \locvar{\bi} in coded order of the $C_b$ plane blocks in macro block
  3762. \locvar{\mbi}, arranged into raster order, and \locvar{I}, \locvar{J},
  3763. \locvar{K}, and \locvar{L} be the indices \locvar{\bi} in coded order of the
  3764. $C_r$ plane blocks in macro block \locvar{\mbi}, arranged into raster order.
  3765. %TODO: as shown in Figure~REF.
  3766. \item
  3767. Assign $\bitvar{MVECTS}[\locvar{E}]$ and $\bitvar{MVECTS}[\locvar{I}]$ the
  3768. value \\ $\bitvar{MVECTS}[\locvar{A}]$.
  3769. \item
  3770. Assign $\bitvar{MVECTS}[\locvar{F}]$ and $\bitvar{MVECTS}[\locvar{J}]$ the
  3771. value \\ $\bitvar{MVECTS}[\locvar{B}]$.
  3772. \item
  3773. Assign $\bitvar{MVECTS}[\locvar{G}]$ and $\bitvar{MVECTS}[\locvar{K}]$ the
  3774. value \\ $\bitvar{MVECTS}[\locvar{C}]$.
  3775. \item
  3776. Assign $\bitvar{MVECTS}[\locvar{H}]$ and $\bitvar{MVECTS}[\locvar{L}]$ the
  3777. value \\ $\bitvar{MVECTS}[\locvar{D}]$.
  3778. \end{enumerate}
  3779. \item
  3780. Assign \locvar{LAST2} the value \locvar{LAST1}.
  3781. \item
  3782. Assign \locvar{LAST1} the value $(\locvar{MVX},\locvar{MVY})$.
  3783. This is the value of the motion vector decoded from the last coded luma block
  3784. in raster order.
  3785. There must always be at least one, since macro blocks with no coded luma blocks
  3786. must use mode 0:~INTER\_NOMV.
  3787. \end{enumerate}
  3788. \item
  3789. Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 6 (INTER\_GOLDEN\_MV),
  3790. decode a single motion vector into \locvar{MVX} and \locvar{MVY} using the
  3791. procedure described in Section~\ref{sub:mv-decode}.
  3792. \item
  3793. Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 4 (INTER\_MV\_LAST2):
  3794. \begin{enumerate}
  3795. \item
  3796. Assign $(\locvar{MVX},\locvar{MVY})$ the value \locvar{LAST2}.
  3797. \item
  3798. Assign \locvar{LAST2} the value \locvar{LAST1}.
  3799. \item
  3800. Assign \locvar{LAST1} the value $(\locvar{MVX},\locvar{MVY})$.
  3801. \end{enumerate}
  3802. \item
  3803. Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 3 (INTER\_MV\_LAST), assign
  3804. $(\locvar{MVX},\locvar{MVY})$ the value \locvar{LAST1}.
  3805. \item
  3806. Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 2 (INTER\_MV):
  3807. \begin{enumerate}
  3808. \item
  3809. Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using the
  3810. procedure described in Section~\ref{sub:mv-decode}.
  3811. \item
  3812. Assign \locvar{LAST2} the value \locvar{LAST1}.
  3813. \item
  3814. Assign \locvar{LAST1} the value $(\locvar{MVX},\locvar{MVY})$.
  3815. \end{enumerate}
  3816. \item
  3817. Otherwise ($\bitvar{MBMODES}[\locvar{\mbi}]$ is 5:~INTER\_GOLDEN\_NOMV,
  3818. 1:~INTRA, or 0:~INTER\_NOMV), assign \locvar{MVX} and \locvar{MVY} the value
  3819. zero.
  3820. \item
  3821. If $\bitvar{MBMODES}[\locvar{\mbi}]$ is not 7 (not INTER\_MV\_FOUR), then for
  3822. each coded block \locvar{\bi} in macro block \locvar{\mbi}:
  3823. \begin{enumerate}
  3824. \item
  3825. Assign $\bitvar{MVECTS}[\locvar{\bi}]$ the value $(\locvar{MVX},\locvar{MVY})$.
  3826. \end{enumerate}
  3827. \end{enumerate}
  3828. \end{enumerate}
  3829. \paragraph{VP3 Compatibility}
  3830. Unless all four luma blocks in the macro block are coded, the VP3 encoder does
  3831. not select mode INTER\_MV\_FOUR.
  3832. Theora removes this restriction by treating the motion vector for an uncoded
  3833. luma block as the default $(0,0)$ vector.
  3834. This is consistent with the premise that the block has not changed since the
  3835. previous frame and that chroma information can be largely ignored when
  3836. estimating motion.
  3837. No modification is required for INTER\_MV\_FOUR macro blocks in VP3 streams to
  3838. be decoded correctly by a Theora decoder.
  3839. However, regardless of how many of the luma blocks are actually coded, the VP3
  3840. decoder always reads four motion vectors from the stream for INTER\_MV\_FOUR
  3841. mode.
  3842. The motion vectors read are used to calculate the motion vectors for the chroma
  3843. blocks, but are otherwise ignored.
  3844. Thus, care should be taken when creating Theora streams meant to be backwards
  3845. compatible with VP3 to only use INTER\_MV\_FOUR mode when all four luma
  3846. blocks are coded.
  3847. \section{Block-Level \qi\ Decode}
  3848. \label{sub:block-qis}
  3849. \paragraph{Input parameters:}\hfill\\*
  3850. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3851. \multicolumn{1}{c}{Name} &
  3852. \multicolumn{1}{c}{Type} &
  3853. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3854. \multicolumn{1}{c}{Signed?} &
  3855. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3856. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  3857. frame. \\
  3858. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3859. 1 & No & An \bitvar{NBS}-element array of flags
  3860. indicating which blocks are coded. \\
  3861. \bitvar{NQIS} & Integer & 2 & No & The number of \qi\ values. \\
  3862. \bottomrule\end{tabularx}
  3863. \paragraph{Output parameters:}\hfill\\*
  3864. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3865. \multicolumn{1}{c}{Name} &
  3866. \multicolumn{1}{c}{Type} &
  3867. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3868. \multicolumn{1}{c}{Signed?} &
  3869. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3870. \bitvar{QIIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3871. 2 & No & An \bitvar{NBS}-element array of
  3872. \locvar{\qii} values for each block. \\
  3873. \bottomrule\end{tabularx}
  3874. \paragraph{Variables used:}\hfill\\*
  3875. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3876. \multicolumn{1}{c}{Name} &
  3877. \multicolumn{1}{c}{Type} &
  3878. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3879. \multicolumn{1}{c}{Signed?} &
  3880. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3881. \locvar{NBITS} & Integer & 36 & No & The length of a bit string to decode. \\
  3882. \locvar{BITS} & Bit string & & & A decoded set of flags. \\
  3883. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  3884. coded order. \\
  3885. \locvar{\qii} & Integer & 2 & No & The index of \qi\ value in the list of
  3886. \qi\ values defined for this frame. \\
  3887. \bottomrule\end{tabularx}
  3888. \medskip
  3889. This procedure selects the \qi\ value to be used for dequantizing the AC
  3890. coefficients of each block.
  3891. DC coefficients all use the same \qi\ value, so as to avoid interference with
  3892. the DC prediction mechanism, which occurs in the quantized domain.
  3893. The value is actually represented by an index \locvar{\qii} into the list of
  3894. \qi\ values defined for the frame.
  3895. The decoder makes multiple passes through the list of coded blocks, one for
  3896. each \qi\ value except the last one.
  3897. In each pass, an RLE-coded bitmask is decoded to divide the blocks into two
  3898. groups: those that use the current \qi\ value in the list, and those that use
  3899. a value from later in the list.
  3900. Each subsequent pass is restricted to the blocks in the second group.
  3901. \begin{enumerate}
  3902. \item
  3903. For each value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$, assign
  3904. $\bitvar{QIIS}[\locvar{\bi}]$ the value zero.
  3905. \item
  3906. For each consecutive value of \locvar{\qii} from 0 to $(\bitvar{NQIS}-2)$:
  3907. \begin{enumerate}
  3908. \item
  3909. Assign \locvar{NBITS} be the number of blocks \locvar{\bi} such that
  3910. $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero and $\bitvar{QIIS}[\locvar{\bi}]$
  3911. equals $\locvar{\qii}$.
  3912. \item
  3913. Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
  3914. described in Section~\ref{sub:long-run}.
  3915. This represents the list of blocks that use \qi\ value \locvar{\qii} or higher.
  3916. \item
  3917. For each consecutive value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$ such
  3918. that $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero and
  3919. $\bitvar{QIIS}[\locvar{\bi}]$ equals $\locvar{\qii}$:
  3920. \begin{enumerate}
  3921. \item
  3922. Remove the bit at the head of the string \locvar{BITS} and add its value to
  3923. $\bitvar{QIIS}[\locvar{\bi}]$.
  3924. \end{enumerate}
  3925. \end{enumerate}
  3926. \end{enumerate}
  3927. \paragraph{VP3 Compatibility}
  3928. For VP3 compatible streams, only one \qi\ value can be specified in the frame
  3929. header, so the main loop of the above procedure, which would iterate from $0$
  3930. to $-1$, is never executed.
  3931. Thus, no bits are read, and each block uses the one \qi\ value defined for the
  3932. frame.
  3933. \cleardoublepage
  3934. \section{DCT Coefficients}
  3935. \label{sec:dct-decode}
  3936. The quantized DCT coefficients are decoded by making 64 passes through the list
  3937. of coded blocks, one for each token index in zig-zag order.
  3938. For the DC tokens, two Huffman tables are chosen from among the first 16, one
  3939. for the luma plane and one for the chroma planes.
  3940. The AC tokens, however, are divided into four different groups.
  3941. Again, two 4-bit indices are decoded, one for the luma plane, and one for the
  3942. chroma planes, but these select the codebooks for {\em all four} groups.
  3943. AC coefficients in group one use codebooks $16\ldots 31$, while group two uses
  3944. $32\ldots 47$, etc.
  3945. Note that this second set of indices is decoded even if there are no non-zero
  3946. AC coefficients in the frame.
  3947. Tokens are divided into two major types: EOB tokens, which fill the remainder
  3948. of one or more blocks with zeros, and coefficient tokens, which fill in one or
  3949. more coefficients within a single block.
  3950. A decoding procedure for the first is given in Section~\ref{sub:eob-token}, and
  3951. for the second in Section~\ref{sub:coeff-token}.
  3952. The decoding procedure for the complete set of quantized coefficients is given
  3953. in Section~\ref{sub:dct-coeffs}.
  3954. \subsection{EOB Token Decode}
  3955. \label{sub:eob-token}
  3956. \paragraph{Input parameters:}\hfill\\*
  3957. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3958. \multicolumn{1}{c}{Name} &
  3959. \multicolumn{1}{c}{Type} &
  3960. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3961. \multicolumn{1}{c}{Signed?} &
  3962. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3963. \bitvar{TOKEN} & Integer & 5 & No & The token being decoded.
  3964. This must be in the range $0\ldots 6$. \\
  3965. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  3966. frame. \\
  3967. \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3968. 7 & No & An \bitvar{NBS}-element array of the
  3969. current token index for each block. \\
  3970. \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3971. 7 & No & An \bitvar{NBS}-element array of the
  3972. coefficient count for each block. \\
  3973. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  3974. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  3975. quantized DCT coefficient values for each block in zig-zag order. \\
  3976. \bitvar{\bi} & Integer & 36 & No & The index of the current block in
  3977. coded order. \\
  3978. \bitvar{\ti} & Integer & 6 & No & The current token index. \\
  3979. \bottomrule\end{tabularx}
  3980. \paragraph{Output parameters:}\hfill\\*
  3981. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3982. \multicolumn{1}{c}{Name} &
  3983. \multicolumn{1}{c}{Type} &
  3984. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  3985. \multicolumn{1}{c}{Signed?} &
  3986. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  3987. \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  3988. 7 & No & An \bitvar{NBS}-element array of the
  3989. current token index for each block. \\
  3990. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  3991. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  3992. quantized DCT coefficient values for each block in zig-zag order. \\
  3993. \bitvar{EOBS} & Integer & 36 & No & The remaining length of the current
  3994. EOB run. \\
  3995. \bottomrule\end{tabularx}
  3996. \paragraph{Variables used:}\hfill\\*
  3997. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  3998. \multicolumn{1}{c}{Name} &
  3999. \multicolumn{1}{c}{Type} &
  4000. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4001. \multicolumn{1}{c}{Signed?} &
  4002. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4003. \locvar{\bj} & Integer & 36 & No & Another index of a block in coded
  4004. order. \\
  4005. \locvar{\tj} & Integer & 6 & No & Another token index. \\
  4006. \bottomrule\end{tabularx}
  4007. \medskip
  4008. A summary of the EOB tokens is given in Table~\ref{tab:eob-tokens}.
  4009. An important thing to note is that token 6 does not add an offset to the
  4010. decoded run value, even though in general it should only be used for runs of
  4011. size 32 or longer.
  4012. If a value of zero is decoded for this run, it is treated as an EOB run the
  4013. size of the remaining coded blocks.
  4014. \begin{table}[htbp]
  4015. \begin{center}
  4016. \begin{tabular}{ccl}\toprule
  4017. Token Value & Extra Bits & EOB Run Lengths \\\midrule
  4018. $0$ & $0$ & $1$ \\
  4019. $1$ & $0$ & $2$ \\
  4020. $2$ & $0$ & $3$ \\
  4021. $3$ & $2$ & $4\ldots 7$ \\
  4022. $4$ & $3$ & $8\ldots 15$ \\
  4023. $5$ & $4$ & $16\ldots 31$ \\
  4024. $6$ & $12$ & $1\ldots 4095$, or all remaining blocks \\
  4025. \bottomrule\end{tabular}
  4026. \end{center}
  4027. \caption{EOB Token Summary}
  4028. \label{tab:eob-tokens}
  4029. \end{table}
  4030. There is no restriction that one EOB token cannot be immediately followed by
  4031. another, so no special cases are necessary to extend the range of the maximum
  4032. run length as were required in Section~\ref{sub:long-run}.
  4033. Indeed, depending on the lengths of the Huffman codes, it may even cheaper to
  4034. encode, by way of example, an EOB run of length 31 followed by an EOB run of
  4035. length 1 than to encode an EOB run of length 32 directly.
  4036. There is also no restriction that an EOB run stop at the end of a color plane
  4037. or a token index.
  4038. The run MUST, however, end at or before the end of the frame.
  4039. \begin{enumerate}
  4040. \item
  4041. If \bitvar{TOKEN} is 0, assign \bitvar{EOBS} the value 1.
  4042. \item
  4043. Otherwise, if \bitvar{TOKEN} is 1, assign \bitvar{EOBS} the value 2.
  4044. \item
  4045. Otherwise, if \bitvar{TOKEN} is 2, assign \bitvar{EOBS} the value 3.
  4046. \item
  4047. Otherwise, if \bitvar{TOKEN} is 3:
  4048. \begin{enumerate}
  4049. \item
  4050. Read a 2-bit unsigned integer as \bitvar{EOBS}.
  4051. \item
  4052. Assign \bitvar{EOBS} the value $(\bitvar{EOBS}+4)$.
  4053. \end{enumerate}
  4054. \item
  4055. Otherwise, if \bitvar{TOKEN} is 4:
  4056. \begin{enumerate}
  4057. \item
  4058. Read a 3-bit unsigned integer as \bitvar{EOBS}.
  4059. \item
  4060. Assign \bitvar{EOBS} the value $(\bitvar{EOBS}+8)$.
  4061. \end{enumerate}
  4062. \item
  4063. Otherwise, if \bitvar{TOKEN} is 5:
  4064. \begin{enumerate}
  4065. \item
  4066. Read a 4-bit unsigned integer as \bitvar{EOBS}.
  4067. \item
  4068. Assign \bitvar{EOBS} the value $(\bitvar{EOBS}+16)$.
  4069. \end{enumerate}
  4070. \item
  4071. Otherwise, \bitvar{TOKEN} is 6:
  4072. \begin{enumerate}
  4073. \item
  4074. Read a 12-bit unsigned integer as \bitvar{EOBS}.
  4075. \item
  4076. If \bitvar{EOBS} is zero, assign \bitvar{EOBS} to be the number of coded blocks
  4077. \locvar{\bj} such that $\bitvar{TIS}[\locvar{\bj}]$ is less than 64.
  4078. \end{enumerate}
  4079. \item
  4080. For each value of \locvar{\tj} from $\bitvar{\ti}$ to 63, assign
  4081. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4082. \item
  4083. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4084. \item
  4085. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value 64.
  4086. \item
  4087. Assign \bitvar{EOBS} the value $(\bitvar{EOBS}-1)$.
  4088. \end{enumerate}
  4089. \paragraph{VP3 Compatibility}
  4090. The VP3 encoder does not use the special interpretation of a zero-length EOB
  4091. run, though its decoder {\em does} support it.
  4092. That may be due more to a happy accident in the way the decoder was written
  4093. than intentional design, however, and other VP3 implementations might not
  4094. reproduce it faithfully.
  4095. For backwards compatibility, it may be wise to avoid it, especially as for most
  4096. frame sizes there are fewer than 4095 blocks, making it unnecessary.
  4097. \subsection{Coefficient Token Decode}
  4098. \label{sub:coeff-token}
  4099. \paragraph{Input parameters:}\hfill\\*
  4100. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4101. \multicolumn{1}{c}{Name} &
  4102. \multicolumn{1}{c}{Type} &
  4103. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4104. \multicolumn{1}{c}{Signed?} &
  4105. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4106. \bitvar{TOKEN} & Integer & 5 & No & The token being decoded.
  4107. This must be in the range $7\ldots 31$. \\
  4108. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  4109. frame. \\
  4110. \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4111. 7 & No & An \bitvar{NBS}-element array of the
  4112. current token index for each block. \\
  4113. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  4114. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  4115. quantized DCT coefficient values for each block in zig-zag order. \\
  4116. \bitvar{\bi} & Integer & 36 & No & The index of the current block in
  4117. coded order. \\
  4118. \bitvar{\ti} & Integer & 6 & No & The current token index. \\
  4119. \bottomrule\end{tabularx}
  4120. \paragraph{Output parameters:}\hfill\\*
  4121. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4122. \multicolumn{1}{c}{Name} &
  4123. \multicolumn{1}{c}{Type} &
  4124. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4125. \multicolumn{1}{c}{Signed?} &
  4126. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4127. \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4128. 7 & No & An \bitvar{NBS}-element array of the
  4129. current token index for each block. \\
  4130. \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4131. 7 & No & An \bitvar{NBS}-element array of the
  4132. coefficient count for each block. \\
  4133. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  4134. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  4135. quantized DCT coefficient values for each block in zig-zag order. \\
  4136. \bottomrule\end{tabularx}
  4137. \paragraph{Variables used:}\hfill\\*
  4138. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4139. \multicolumn{1}{c}{Name} &
  4140. \multicolumn{1}{c}{Type} &
  4141. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4142. \multicolumn{1}{c}{Signed?} &
  4143. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4144. \locvar{SIGN} & Integer & 1 & No & A flag indicating the sign of the
  4145. current coefficient. \\
  4146. \locvar{MAG} & Integer & 10 & No & The magnitude of the current
  4147. coefficient. \\
  4148. \locvar{RLEN} & Integer & 6 & No & The length of the current zero run. \\
  4149. \locvar{\tj} & Integer & 6 & No & Another token index. \\
  4150. \bottomrule\end{tabularx}
  4151. \medskip
  4152. Each of these tokens decodes one or more coefficients in the current block.
  4153. A summary of the meanings of the token values is presented in
  4154. Table~\ref{tab:coeff-tokens}.
  4155. There are often several different ways to tokenize a given coefficient list.
  4156. Which one is optimal depends on the exact lengths of the Huffman codes used to
  4157. represent each token.
  4158. Note that we do not update the coefficient count for the block if we decode a
  4159. pure zero run.
  4160. \begin{table}[htbp]
  4161. \begin{center}
  4162. \begin{tabularx}{\textwidth}{cclX}\toprule
  4163. Token Value & Extra Bits & \multicolumn{1}{p{55pt}}{Number of Coefficients}
  4164. & Description \\\midrule
  4165. $7$ & $3$ & $1\ldots 8$ & Short zero run. \\
  4166. $8$ & $6$ & $1\ldots 64$ & Zero run. \\
  4167. $9$ & $0$ & $1$ & $1$. \\
  4168. $10$ & $0$ & $1$ & $-1$. \\
  4169. $11$ & $0$ & $1$ & $2$. \\
  4170. $12$ & $0$ & $1$ & $-2$. \\
  4171. $13$ & $1$ & $1$ & $\pm 3$. \\
  4172. $14$ & $1$ & $1$ & $\pm 4$. \\
  4173. $15$ & $1$ & $1$ & $\pm 5$. \\
  4174. $16$ & $1$ & $1$ & $\pm 6$. \\
  4175. $17$ & $2$ & $1$ & $\pm 7\ldots 8$. \\
  4176. $18$ & $3$ & $1$ & $\pm 9\ldots 12$. \\
  4177. $19$ & $4$ & $1$ & $\pm 13\ldots 20$. \\
  4178. $20$ & $5$ & $1$ & $\pm 21\ldots 36$. \\
  4179. $21$ & $6$ & $1$ & $\pm 37\ldots 68$. \\
  4180. $22$ & $10$ & $1$ & $\pm 69\ldots 580$. \\
  4181. $23$ & $1$ & $2$ & One zero followed by $\pm 1$. \\
  4182. $24$ & $1$ & $3$ & Two zeros followed by $\pm 1$. \\
  4183. $25$ & $1$ & $4$ & Three zeros followed by
  4184. $\pm 1$. \\
  4185. $26$ & $1$ & $5$ & Four zeros followed by
  4186. $\pm 1$. \\
  4187. $27$ & $1$ & $6$ & Five zeros followed by
  4188. $\pm 1$. \\
  4189. $28$ & $3$ & $7\ldots 10$ & $6\ldots 9$ zeros followed by
  4190. $\pm 1$. \\
  4191. $29$ & $4$ & $11\ldots 18$ & $10\ldots 17$ zeros followed by
  4192. $\pm 1$.\\
  4193. $30$ & $2$ & $2$ & One zero followed by
  4194. $\pm 2\ldots 3$. \\
  4195. $31$ & $3$ & $3\ldots 4$ & $2\ldots 3$ zeros followed by
  4196. $\pm 2\ldots 3$. \\
  4197. \bottomrule\end{tabularx}
  4198. \end{center}
  4199. \caption{Coefficient Token Summary}
  4200. \label{tab:coeff-tokens}
  4201. \end{table}
  4202. For tokens which represent more than one coefficient, they MUST NOT bring the
  4203. total number of coefficients in the block to more than 64.
  4204. Care should be taken in a decoder to check for this, as otherwise it may permit
  4205. buffer overflows from invalidly formed packets.
  4206. \begin{verse}
  4207. {\bf Note:} One way to achieve this efficiently is to combine the inverse
  4208. zig-zag mapping (described later in Section~\ref{sub:dequant}) with
  4209. coefficient decode, and use a table look-up to map zig-zag indices greater
  4210. than 63 to a safe location.
  4211. \end{verse}
  4212. \begin{enumerate}
  4213. \item
  4214. If \bitvar{TOKEN} is 7:
  4215. \begin{enumerate}
  4216. \item
  4217. Read in a 3-bit unsigned integer as \locvar{RLEN}.
  4218. \item
  4219. Assign \locvar{RLEN} the value $(\locvar{RLEN}+1)$.
  4220. \item
  4221. For each value of \locvar{\tj} from \bitvar{\ti} to
  4222. $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
  4223. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4224. \item
  4225. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
  4226. $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}$.
  4227. \end{enumerate}
  4228. \item
  4229. Otherwise, if \bitvar{TOKEN} is 8:
  4230. \begin{enumerate}
  4231. \item
  4232. Read in a 6-bit unsigned integer as \locvar{RLEN}.
  4233. \item
  4234. Assign \locvar{RLEN} the value $(\locvar{RLEN}+1)$.
  4235. \item
  4236. For each value of \locvar{\tj} from \bitvar{\ti} to
  4237. $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
  4238. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4239. \item
  4240. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
  4241. $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}$.
  4242. \end{enumerate}
  4243. \item
  4244. Otherwise, if \bitvar{TOKEN} is 9:
  4245. \begin{enumerate}
  4246. \item
  4247. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $1$.
  4248. \item
  4249. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4250. \item
  4251. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4252. \end{enumerate}
  4253. \item
  4254. Otherwise, if \bitvar{TOKEN} is 10:
  4255. \begin{enumerate}
  4256. \item
  4257. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-1$.
  4258. \item
  4259. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4260. \item
  4261. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4262. \end{enumerate}
  4263. \item
  4264. Otherwise, if \bitvar{TOKEN} is 11:
  4265. \begin{enumerate}
  4266. \item
  4267. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $2$.
  4268. \item
  4269. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4270. \item
  4271. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4272. \end{enumerate}
  4273. \item
  4274. Otherwise, if \bitvar{TOKEN} is 12:
  4275. \begin{enumerate}
  4276. \item
  4277. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-2$.
  4278. \item
  4279. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4280. \item
  4281. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4282. \end{enumerate}
  4283. \item
  4284. Otherwise, if \bitvar{TOKEN} is 13:
  4285. \begin{enumerate}
  4286. \item
  4287. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4288. \item
  4289. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4290. the value $3$.
  4291. \item
  4292. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-3$.
  4293. \item
  4294. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4295. \item
  4296. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4297. \end{enumerate}
  4298. \item
  4299. Otherwise, if \bitvar{TOKEN} is 14:
  4300. \begin{enumerate}
  4301. \item
  4302. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4303. \item
  4304. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4305. the value $4$.
  4306. \item
  4307. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-4$.
  4308. \item
  4309. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4310. \item
  4311. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4312. \end{enumerate}
  4313. \item
  4314. Otherwise, if \bitvar{TOKEN} is 15:
  4315. \begin{enumerate}
  4316. \item
  4317. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4318. \item
  4319. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4320. the value $5$.
  4321. \item
  4322. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-5$.
  4323. \item
  4324. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4325. \item
  4326. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4327. \end{enumerate}
  4328. \item
  4329. Otherwise, if \bitvar{TOKEN} is 16:
  4330. \begin{enumerate}
  4331. \item
  4332. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4333. \item
  4334. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4335. the value $6$.
  4336. \item
  4337. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-6$.
  4338. \item
  4339. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4340. \item
  4341. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4342. \end{enumerate}
  4343. \item
  4344. Otherwise, if \bitvar{TOKEN} is 17:
  4345. \begin{enumerate}
  4346. \item
  4347. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4348. \item
  4349. Read a 1-bit unsigned integer as \locvar{MAG}.
  4350. \item
  4351. Assign \locvar{MAG} the value $(\locvar{MAG}+7)$.
  4352. \item
  4353. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4354. the value $\locvar{MAG}$.
  4355. \item
  4356. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4357. $-\locvar{MAG}$.
  4358. \item
  4359. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4360. \item
  4361. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4362. \end{enumerate}
  4363. \item
  4364. Otherwise, if \bitvar{TOKEN} is 18:
  4365. \begin{enumerate}
  4366. \item
  4367. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4368. \item
  4369. Read a 2-bit unsigned integer as \locvar{MAG}.
  4370. \item
  4371. Assign \locvar{MAG} the value $(\locvar{MAG}+9)$.
  4372. \item
  4373. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4374. the value $\locvar{MAG}$.
  4375. \item
  4376. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4377. $-\locvar{MAG}$.
  4378. \item
  4379. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4380. \item
  4381. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4382. \end{enumerate}
  4383. \item
  4384. Otherwise, if \bitvar{TOKEN} is 19:
  4385. \begin{enumerate}
  4386. \item
  4387. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4388. \item
  4389. Read a 3-bit unsigned integer as \locvar{MAG}.
  4390. \item
  4391. Assign \locvar{MAG} the value $(\locvar{MAG}+13)$.
  4392. \item
  4393. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4394. the value $\locvar{MAG}$.
  4395. \item
  4396. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4397. $-\locvar{MAG}$.
  4398. \item
  4399. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4400. \item
  4401. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4402. \end{enumerate}
  4403. \item
  4404. Otherwise, if \bitvar{TOKEN} is 20:
  4405. \begin{enumerate}
  4406. \item
  4407. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4408. \item
  4409. Read a 4-bit unsigned integer as \locvar{MAG}.
  4410. \item
  4411. Assign \locvar{MAG} the value $(\locvar{MAG}+21)$.
  4412. \item
  4413. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4414. the value $\locvar{MAG}$.
  4415. \item
  4416. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4417. $-\locvar{MAG}$.
  4418. \item
  4419. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4420. \item
  4421. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4422. \end{enumerate}
  4423. \item
  4424. Otherwise, if \bitvar{TOKEN} is 21:
  4425. \begin{enumerate}
  4426. \item
  4427. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4428. \item
  4429. Read a 5-bit unsigned integer as \locvar{MAG}.
  4430. \item
  4431. Assign \locvar{MAG} the value $(\locvar{MAG}+37)$.
  4432. \item
  4433. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4434. the value $\locvar{MAG}$.
  4435. \item
  4436. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4437. $-\locvar{MAG}$.
  4438. \item
  4439. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4440. \item
  4441. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4442. \end{enumerate}
  4443. \item
  4444. Otherwise, if \bitvar{TOKEN} is 22:
  4445. \begin{enumerate}
  4446. \item
  4447. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4448. \item
  4449. Read a 9-bit unsigned integer as \locvar{MAG}.
  4450. \item
  4451. Assign \locvar{MAG} the value $(\locvar{MAG}+69)$.
  4452. \item
  4453. If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
  4454. the value $\locvar{MAG}$.
  4455. \item
  4456. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
  4457. $-\locvar{MAG}$.
  4458. \item
  4459. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
  4460. \item
  4461. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4462. \end{enumerate}
  4463. \item
  4464. Otherwise, if \bitvar{TOKEN} is 23:
  4465. \begin{enumerate}
  4466. \item
  4467. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value zero.
  4468. \item
  4469. Read a 1-bit unsigned integer as SIGN.
  4470. \item
  4471. If \locvar{SIGN} is zero, assign
  4472. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value $1$.
  4473. \item
  4474. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value
  4475. $-1$.
  4476. \item
  4477. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+2$.
  4478. \item
  4479. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4480. \end{enumerate}
  4481. \item
  4482. Otherwise, if \bitvar{TOKEN} is 24:
  4483. \begin{enumerate}
  4484. \item
  4485. For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+1)$, assign
  4486. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4487. \item
  4488. Read a 1-bit unsigned integer as SIGN.
  4489. \item
  4490. If \locvar{SIGN} is zero, assign
  4491. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+2]$ the value $1$.
  4492. \item
  4493. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+2]$ the value
  4494. $-1$.
  4495. \item
  4496. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+3$.
  4497. \item
  4498. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4499. \end{enumerate}
  4500. \item
  4501. Otherwise, if \bitvar{TOKEN} is 25:
  4502. \begin{enumerate}
  4503. \item
  4504. For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+2)$, assign
  4505. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4506. \item
  4507. Read a 1-bit unsigned integer as SIGN.
  4508. \item
  4509. If \locvar{SIGN} is zero, assign
  4510. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+3]$ the value $1$.
  4511. \item
  4512. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+3]$ the value
  4513. $-1$.
  4514. \item
  4515. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+4$.
  4516. \item
  4517. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4518. \end{enumerate}
  4519. \item
  4520. Otherwise, if \bitvar{TOKEN} is 26:
  4521. \begin{enumerate}
  4522. \item
  4523. For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+3)$, assign
  4524. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4525. \item
  4526. Read a 1-bit unsigned integer as SIGN.
  4527. \item
  4528. If \locvar{SIGN} is zero, assign
  4529. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+4]$ the value $1$.
  4530. \item
  4531. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+4]$ the value
  4532. $-1$.
  4533. \item
  4534. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+5$.
  4535. \item
  4536. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4537. \end{enumerate}
  4538. \item
  4539. Otherwise, if \bitvar{TOKEN} is 27:
  4540. \begin{enumerate}
  4541. \item
  4542. For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+4)$, assign
  4543. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4544. \item
  4545. Read a 1-bit unsigned integer as SIGN.
  4546. \item
  4547. If \locvar{SIGN} is zero, assign
  4548. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+5]$ the value $1$.
  4549. \item
  4550. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+5]$ the value
  4551. $-1$.
  4552. \item
  4553. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+6$.
  4554. \item
  4555. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4556. \end{enumerate}
  4557. \item
  4558. Otherwise, if \bitvar{TOKEN} is 28:
  4559. \begin{enumerate}
  4560. \item
  4561. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4562. \item
  4563. Read a 2-bit unsigned integer as \locvar{RLEN}.
  4564. \item
  4565. Assign \locvar{RLEN} the value $(\locvar{RLEN}+6)$.
  4566. \item
  4567. For each value of \locvar{\tj} from \bitvar{\ti} to
  4568. $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
  4569. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4570. \item
  4571. If \locvar{SIGN} is zero, assign
  4572. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$ the value $1$.
  4573. \item
  4574. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$
  4575. the value $-1$.
  4576. \item
  4577. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
  4578. $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}+1$.
  4579. \item
  4580. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4581. \end{enumerate}
  4582. \item
  4583. Otherwise, if \bitvar{TOKEN} is 29:
  4584. \begin{enumerate}
  4585. \item
  4586. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4587. \item
  4588. Read a 3-bit unsigned integer as \locvar{RLEN}.
  4589. \item
  4590. Assign \locvar{RLEN} the value $(\locvar{RLEN}+10)$.
  4591. \item
  4592. For each value of \locvar{\tj} from \bitvar{\ti} to
  4593. $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
  4594. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4595. \item
  4596. If \locvar{SIGN} is zero, assign
  4597. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$ the value $1$.
  4598. \item
  4599. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$
  4600. the value $-1$.
  4601. \item
  4602. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
  4603. $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}+1$.
  4604. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4605. \end{enumerate}
  4606. \item
  4607. Otherwise, if \bitvar{TOKEN} is 30:
  4608. \begin{enumerate}
  4609. \item
  4610. Assign $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\ti}]$ the value zero.
  4611. \item
  4612. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4613. \item
  4614. Read a 1-bit unsigned integer as \locvar{MAG}.
  4615. \item
  4616. Assign \locvar{MAG} the value $(\locvar{MAG}+2)$.
  4617. \item
  4618. If \locvar{SIGN} is zero, assign
  4619. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value $\locvar{MAG}$.
  4620. \item
  4621. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value
  4622. $-\locvar{MAG}$.
  4623. \item
  4624. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+2$.
  4625. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4626. \end{enumerate}
  4627. \item
  4628. Otherwise, if \bitvar{TOKEN} is 31:
  4629. \begin{enumerate}
  4630. \item
  4631. Read a 1-bit unsigned integer as \locvar{SIGN}.
  4632. \item
  4633. Read a 1-bit unsigned integer as \locvar{MAG}.
  4634. \item
  4635. Assign \locvar{MAG} the value $(\locvar{MAG}+2)$.
  4636. \item
  4637. Read a 1-bit unsigned integer as \locvar{RLEN}.
  4638. \item
  4639. Assign \locvar{RLEN} the value $(\locvar{RLEN}+2)$.
  4640. \item
  4641. For each value of \locvar{\tj} from \bitvar{\ti} to
  4642. $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
  4643. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
  4644. \item
  4645. If \locvar{SIGN} is zero, assign
  4646. $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$ the value
  4647. $\locvar{MAG}$.
  4648. \item
  4649. Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$
  4650. the value $-\locvar{MAG}$.
  4651. \item
  4652. Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
  4653. $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}+1$.
  4654. Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
  4655. \end{enumerate}
  4656. \end{enumerate}
  4657. \subsection{DCT Coefficient Decode}
  4658. \label{sub:dct-coeffs}
  4659. \paragraph{Input parameters:}\hfill\\*
  4660. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4661. \multicolumn{1}{c}{Name} &
  4662. \multicolumn{1}{c}{Type} &
  4663. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4664. \multicolumn{1}{c}{Signed?} &
  4665. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4666. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  4667. frame. \\
  4668. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4669. 1 & No & An \bitvar{NBS}-element array of flags
  4670. indicating which blocks are coded. \\
  4671. \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
  4672. frame. \\
  4673. \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
  4674. & An 80-element array of Huffman tables
  4675. with up to 32 entries each. \\
  4676. \bottomrule\end{tabularx}
  4677. \paragraph{Output parameters:}\hfill\\*
  4678. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4679. \multicolumn{1}{c}{Name} &
  4680. \multicolumn{1}{c}{Type} &
  4681. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4682. \multicolumn{1}{c}{Signed?} &
  4683. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4684. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  4685. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  4686. quantized DCT coefficient values for each block in zig-zag order. \\
  4687. \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4688. 7 & No & An \bitvar{NBS}-element array of the
  4689. coefficient count for each block. \\
  4690. \bottomrule\end{tabularx}
  4691. \paragraph{Variables used:}\hfill\\*
  4692. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4693. \multicolumn{1}{c}{Name} &
  4694. \multicolumn{1}{c}{Type} &
  4695. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4696. \multicolumn{1}{c}{Signed?} &
  4697. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4698. \locvar{NLBS} & Integer & 34 & No & The number of blocks in the luma
  4699. plane. \\
  4700. \locvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4701. 7 & No & An \bitvar{NBS}-element array of the
  4702. current token index for each block. \\
  4703. \locvar{EOBS} & Integer & 36 & No & The remaining length of the current
  4704. EOB run. \\
  4705. \locvar{TOKEN} & Integer & 5 & No & The current token being decoded. \\
  4706. \locvar{HG} & Integer & 3 & No & The current Huffman table group. \\
  4707. \locvar{\cbi} & Integer & 36 & No & The index of the current block in the
  4708. coded block list. \\
  4709. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  4710. coded order. \\
  4711. \locvar{\bj} & Integer & 36 & No & Another index of a block in coded
  4712. order. \\
  4713. \locvar{\ti} & Integer & 6 & No & The current token index. \\
  4714. \locvar{\tj} & Integer & 6 & No & Another token index. \\
  4715. \locvar{\hti_L} & Integer & 4 & No & The index of the current Huffman table
  4716. to use for the luma plane within a group. \\
  4717. \locvar{\hti_C} & Integer & 4 & No & The index of the current Huffman table
  4718. to use for the chroma planes within a group. \\
  4719. \locvar{\hti} & Integer & 7 & No & The index of the current Huffman table
  4720. to use. \\
  4721. \bottomrule\end{tabularx}
  4722. \medskip
  4723. This procedure puts the above two procedures to work to decode the entire set
  4724. of DCT coefficients for the frame.
  4725. At the end of this procedure, \locvar{EOBS} MUST be zero, and
  4726. $\locvar{TIS}[\locvar{\bi}]$ MUST be 64 for every coded \locvar{\bi}.
  4727. Note that we update the coefficient count of every block before continuing an
  4728. EOB run or decoding a token, despite the fact that it is already up to date
  4729. unless the previous token was a pure zero run.
  4730. This is done intentionally to mimic the VP3 accounting rules.
  4731. Thus the only time the coefficient count does not include the coefficients in a
  4732. pure zero run is when when that run reaches all the way to coefficient 63.
  4733. Note, however, that regardless of the coefficient count, any additional
  4734. coefficients are still set to zero.
  4735. The only use of the count is in determining if a special case of the inverse
  4736. DCT can be used in Section~\ref{sub:2d-idct}.
  4737. \begin{enumerate}
  4738. \item
  4739. Assign \locvar{NLBS} the value $(\bitvar{NMBS}*4)$.
  4740. \item
  4741. For each consecutive value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$,
  4742. assign $\locvar{TIS}[\locvar{\bi}]$ the value zero.
  4743. \item
  4744. Assign \locvar{EOBS} the value 0.
  4745. \item
  4746. For each consecutive value of \locvar{\ti} from 0 to 63:
  4747. \begin{enumerate}
  4748. \item
  4749. If \locvar{\ti} is $0$ or $1$:
  4750. \begin{enumerate}
  4751. \item
  4752. Read a 4-bit unsigned integer as \locvar{\hti_L}.
  4753. \item
  4754. Read a 4-bit unsigned integer as \locvar{\hti_C}.
  4755. \end{enumerate}
  4756. \item
  4757. For each consecutive value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$ for
  4758. which $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero and
  4759. $\locvar{TIS}[\locvar{\bi}]$ equals \locvar{\ti}:
  4760. \begin{enumerate}
  4761. \item
  4762. Assign $\bitvar{NCOEFFS}[\locvar{\bi}]$ the value \locvar{\ti}.
  4763. \item
  4764. If \locvar{EOBS} is greater than zero:
  4765. \begin{enumerate}
  4766. \item
  4767. For each value of \locvar{\tj} from $\locvar{\ti}$ to 63, assign
  4768. $\bitvar{COEFFS}[\locvar{\bi}][\locvar{\tj}]$ the value zero.
  4769. \item
  4770. Assign $\locvar{TIS}[\locvar{\bi}]$ the value 64.
  4771. \item
  4772. Assign \locvar{EOBS} the value $(\locvar{EOBS}-1)$.
  4773. \end{enumerate}
  4774. \item
  4775. Otherwise:
  4776. \begin{enumerate}
  4777. \item
  4778. Assign \locvar{HG} a value based on \locvar{\ti} from
  4779. Table~\ref{tab:huff-groups}.
  4780. \begin{table}[htbp]
  4781. \begin{center}
  4782. \begin{tabular}{lc}\toprule
  4783. \locvar{\ti} & \locvar{HG} \\\midrule
  4784. $0$ & $0$ \\
  4785. $1\ldots 5$ & $1$ \\
  4786. $6\ldots 14$ & $2$ \\
  4787. $15\ldots 27$ & $3$ \\
  4788. $28\ldots 63$ & $4$ \\
  4789. \bottomrule\end{tabular}
  4790. \end{center}
  4791. \caption{Huffman Table Groups}
  4792. \label{tab:huff-groups}
  4793. \end{table}
  4794. \item
  4795. If \locvar{\bi} is less than \locvar{NLBS}, assign \locvar{\hti} the value
  4796. $(16*\locvar{HG}+\locvar{\hti_L})$.
  4797. \item
  4798. Otherwise, assign \locvar{\hti} the value
  4799. $(16*\locvar{HG}+\locvar{\hti_C})$.
  4800. \item
  4801. Read one bit at a time until one of the codes in $\bitvar{HTS}[\locvar{\hti}]$
  4802. is recognized, and assign the value to \locvar{TOKEN}.
  4803. \item
  4804. If \locvar{TOKEN} is less than 7, expand an EOB token using the procedure given
  4805. in Section~\ref{sub:eob-token} to update $\locvar{TIS}[\locvar{\bi}]$,
  4806. $\bitvar{COEFFS}[\locvar{\bi}]$, and \locvar{EOBS}.
  4807. \item
  4808. Otherwise, expand a coefficient token using the procedure given in
  4809. Section~\ref{sub:coeff-token} to update $\locvar{TIS}[\locvar{\bi}]$,
  4810. $\bitvar{COEFFS}[\locvar{\bi}]$, and $\bitvar{NCOEFFS}[\locvar{\bi}]$.
  4811. \end{enumerate}
  4812. \end{enumerate}
  4813. \end{enumerate}
  4814. \end{enumerate}
  4815. \section{Undoing DC Prediction}
  4816. The actual value of a DC coefficient decoded by Section~\ref{sec:dct-decode} is
  4817. the residual from a predicted value computed by the encoder.
  4818. This prediction is only applied to DC coefficients.
  4819. Quantized AC coefficients are encoded directly.
  4820. This section describes how to undo this prediction to recover the original
  4821. DC coefficients.
  4822. The predicted DC value for a block is computed from the DC values of its
  4823. immediate neighbors which precede the block in raster order.
  4824. Thus, reversing this prediction must procede in raster order, instead of coded
  4825. order.
  4826. Note that this step comes before dequantizing the coefficients.
  4827. For this reason, DC coefficients are all quantized with the same \qi\ value,
  4828. regardless of the block-level \qi\ values decoded in
  4829. Section~\ref{sub:block-qis}.
  4830. Those \qi\ values are applied only to the AC coefficients.
  4831. \subsection{Computing the DC Predictor}
  4832. \label{sub:dc-pred}
  4833. \paragraph{Input parameters:}\hfill\\*
  4834. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4835. \multicolumn{1}{c}{Name} &
  4836. \multicolumn{1}{c}{Type} &
  4837. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4838. \multicolumn{1}{c}{Signed?} &
  4839. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4840. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4841. 1 & No & An \bitvar{NBS}-element array of flags
  4842. indicating which blocks are coded. \\
  4843. \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4844. 3 & No & An \bitvar{NMBS}-element array of
  4845. coding modes for each macro block. \\
  4846. \bitvar{LASTDC} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4847. 16 & Yes & A 3-element array containing the
  4848. most recently decoded DC value, one for inter mode and for each reference
  4849. frame. \\
  4850. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  4851. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  4852. quantized DCT coefficient values for each block in zig-zag order. \\
  4853. \bitvar{\bi} & Integer & 36 & No & The index of the current block in
  4854. coded order. \\
  4855. \bottomrule\end{tabularx}
  4856. \paragraph{Output parameters:}\hfill\\*
  4857. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4858. \multicolumn{1}{c}{Name} &
  4859. \multicolumn{1}{c}{Type} &
  4860. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4861. \multicolumn{1}{c}{Signed?} &
  4862. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4863. \bitvar{DCPRED} & Integer & 16 & Yes & The predicted DC value for the current
  4864. block. \\
  4865. \bottomrule\end{tabularx}
  4866. \paragraph{Variables used:}\hfill\\*
  4867. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  4868. \multicolumn{1}{c}{Name} &
  4869. \multicolumn{1}{c}{Type} &
  4870. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  4871. \multicolumn{1}{c}{Signed?} &
  4872. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  4873. \locvar{P} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4874. 1 & No & A 4-element array indicating which
  4875. neighbors can be used for DC prediction. \\
  4876. \locvar{PBI} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4877. 36 & No & A 4-element array containing the
  4878. coded-order block index of the current block's neighbors. \\
  4879. \locvar{W} & \multicolumn{1}{p{40pt}}{Integer Array} &
  4880. 7 & Yes & A 4-element array of the weights to
  4881. apply to each neighboring DC value. \\
  4882. \locvar{PDIV} & Integer & 8 & No & The value to divide the weighted sum
  4883. by. \\
  4884. \locvar{\bj} & Integer & 36 & No & The index of a neighboring block in
  4885. coded order. \\
  4886. \locvar{\mbi} & Integer & 32 & No & The index of the macro block
  4887. containing block \bitvar{\bi}. \\
  4888. \locvar{\mbj} & Integer & 32 & No & The index of the macro block
  4889. containing block \locvar{\bj}. \\
  4890. \locvar{\rfi} & Integer & 2 & No & The index of the reference frame
  4891. indicated by the coding mode for macro block \locvar{\mbi}. \\
  4892. \bottomrule\end{tabularx}
  4893. \medskip
  4894. This procedure outlines how a predictor is formed for a single block.
  4895. The predictor is computed as a weighted sum of the neighboring DC values from
  4896. coded blocks which use the same reference frame.
  4897. This latter condition is determined only by checking the coding mode for the
  4898. block.
  4899. Even if the golden frame and the previous frame are in fact the same, e.g. for
  4900. the first inter frame after an intra frame, they are still treated as being
  4901. different for the purposes of DC prediction.
  4902. The weighted sum is divided by a power of two, with truncation towards zero,
  4903. and the result is checked for outranging if necessary.
  4904. If there are no neighboring coded blocks which use the same reference frame as
  4905. the current block, then the most recent DC value of any block that used that
  4906. reference frame is used instead.
  4907. If no such block exists, then the predictor is set to zero.
  4908. \begin{enumerate}
  4909. \item
  4910. Assign \locvar{\mbi} the index of the macro block containing block
  4911. \bitvar{\bi}.
  4912. \item
  4913. Assign \locvar{\rfi} the value of the Reference Frame Index column of
  4914. Table~\ref{tab:cm-refs} corresponding to $\bitvar{MBMODES}[\locvar{\mbi}]$.
  4915. \begin{table}[htpb]
  4916. \begin{center}
  4917. \begin{tabular}{ll}\toprule
  4918. Coding Mode & Reference Frame Index \\\midrule
  4919. $0$ (INTER\_NOMV) & $1$ (Previous) \\
  4920. $1$ (INTRA) & $0$ (None) \\
  4921. $2$ (INTER\_MV) & $1$ (Previous) \\
  4922. $3$ (INTER\_MV\_LAST) & $1$ (Previous) \\
  4923. $4$ (INTER\_MV\_LAST2) & $1$ (Previous) \\
  4924. $5$ (INTER\_GOLDEN\_NOMV) & $2$ (Golden) \\
  4925. $6$ (INTER\_GOLDEN\_MV) & $2$ (Golden) \\
  4926. $7$ (INTER\_MV\_FOUR) & $1$ (Previous) \\
  4927. \bottomrule\end{tabular}
  4928. \end{center}
  4929. \caption{Reference Frames for Each Coding Mode}
  4930. \label{tab:cm-refs}
  4931. \end{table}
  4932. \item
  4933. If block \bitvar{\bi} is not along the left edge of the coded frame:
  4934. \begin{enumerate}
  4935. \item
  4936. Assign \locvar{\bj} the coded-order index of block \bitvar{\bi}'s left
  4937. neighbor, i.e., in the same row but one column to the left.
  4938. \item
  4939. If $\bitvar{BCODED}[\bj]$ is not zero:
  4940. \begin{enumerate}
  4941. \item
  4942. Assign \locvar{\mbj} the index of the macro block containing block
  4943. \locvar{\bj}.
  4944. \item
  4945. If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
  4946. corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
  4947. \begin{enumerate}
  4948. \item
  4949. Assign $\locvar{P}[0]$ the value $1$.
  4950. \item
  4951. Assign $\locvar{PBI}[0]$ the value \locvar{\bj}.
  4952. \end{enumerate}
  4953. \item
  4954. Otherwise, assign $\locvar{P}[0]$ the value zero.
  4955. \end{enumerate}
  4956. \item
  4957. Otherwise, assign $\locvar{P}[0]$ the value zero.
  4958. \end{enumerate}
  4959. \item
  4960. Otherwise, assign $\locvar{P}[0]$ the value zero.
  4961. \item
  4962. If block \bitvar{\bi} is not along the left edge nor the bottom edge of the
  4963. coded frame:
  4964. \begin{enumerate}
  4965. \item
  4966. Assign \locvar{\bj} the coded-order index of block \bitvar{\bi}'s lower-left
  4967. neighbor, i.e., one row down and one column to the left.
  4968. \item
  4969. If $\bitvar{BCODED}[\bj]$ is not zero:
  4970. \begin{enumerate}
  4971. \item
  4972. Assign \locvar{\mbj} the index of the macro block containing block
  4973. \locvar{\bj}.
  4974. \item
  4975. If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
  4976. corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
  4977. \begin{enumerate}
  4978. \item
  4979. Assign $\locvar{P}[1]$ the value $1$.
  4980. \item
  4981. Assign $\locvar{PBI}[1]$ the value \locvar{\bj}.
  4982. \end{enumerate}
  4983. \item
  4984. Otherwise, assign $\locvar{P}[1]$ the value zero.
  4985. \end{enumerate}
  4986. \item
  4987. Otherwise, assign $\locvar{P}[1]$ the value zero.
  4988. \end{enumerate}
  4989. \item
  4990. Otherwise, assign $\locvar{P}[1]$ the value zero.
  4991. \item
  4992. If block \bitvar{\bi} is not along the bottom edge of the coded frame:
  4993. \begin{enumerate}
  4994. \item
  4995. Assign \locvar{\bj} the coded-order index of block \bitvar{\bi}'s lower
  4996. neighbor, i.e., in the same column but one row down.
  4997. \item
  4998. If $\bitvar{BCODED}[\bj]$ is not zero:
  4999. \begin{enumerate}
  5000. \item
  5001. Assign \locvar{\mbj} the index of the macro block containing block
  5002. \locvar{\bj}.
  5003. \item
  5004. If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
  5005. corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
  5006. \begin{enumerate}
  5007. \item
  5008. Assign $\locvar{P}[2]$ the value $1$.
  5009. \item
  5010. Assign $\locvar{PBI}[2]$ the value \locvar{\bj}.
  5011. \end{enumerate}
  5012. \item
  5013. Otherwise, assign $\locvar{P}[2]$ the value zero.
  5014. \end{enumerate}
  5015. \item
  5016. Otherwise, assign $\locvar{P}[2]$ the value zero.
  5017. \end{enumerate}
  5018. \item
  5019. Otherwise, assign $\locvar{P}[2]$ the value zero.
  5020. \item
  5021. If block \bitvar{\bi} is not along the right edge nor the bottom edge of the
  5022. coded frame:
  5023. \begin{enumerate}
  5024. \item
  5025. Assign \locvar{\bj} the coded-order index of block \bitvar{\bi}'s lower-right
  5026. neighbor, i.e., one row down and one column to the right.
  5027. \item
  5028. If $\bitvar{BCODED}[\bj]$ is not zero:
  5029. \begin{enumerate}
  5030. \item
  5031. Assign \locvar{\mbj} the index of the macro block containing block
  5032. \locvar{\bj}.
  5033. \item
  5034. If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
  5035. corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
  5036. \begin{enumerate}
  5037. \item
  5038. Assign $\locvar{P}[3]$ the value $1$.
  5039. \item
  5040. Assign $\locvar{PBI}[3]$ the value \locvar{\bj}.
  5041. \end{enumerate}
  5042. \item
  5043. Otherwise, assign $\locvar{P}[3]$ the value zero.
  5044. \end{enumerate}
  5045. \item
  5046. Otherwise, assign $\locvar{P}[3]$ the value zero.
  5047. \end{enumerate}
  5048. \item
  5049. Otherwise, assign $\locvar{P}[3]$ the value zero.
  5050. \item
  5051. If none of the values $\locvar{P}[0]$, $\locvar{P}[1]$, $\locvar{P}[2]$, nor
  5052. $\locvar{P}[3]$ are non-zero, then assign \bitvar{DCPRED} the value
  5053. $\bitvar{LASTDC}[\locvar{\rfi}]$.
  5054. \item
  5055. Otherwise:
  5056. \begin{enumerate}
  5057. \item
  5058. Assign the array \locvar{W} and the variable \locvar{PDIV} the values from the
  5059. row of Table~\ref{tab:dc-weights} corresonding to the values of each
  5060. $\locvar{P}[\idx{i}]$.
  5061. \begin{table}[htb]
  5062. \begin{center}
  5063. \begin{tabular}{ccccrrrrr}\toprule
  5064. \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[0]$ (L)} &
  5065. \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[1]$ (DL)} &
  5066. \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[2]$ (D)} &
  5067. \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[3]$ (DR)} &
  5068. \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[0]$ (L)} &
  5069. \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[1]$ (DL)} &
  5070. \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[2]$ (D)} &
  5071. \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[3]$ (DR)} &
  5072. \locvar{PDIV} \\\midrule
  5073. $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ \\
  5074. $0$ & $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $0$ & $1$ \\
  5075. $1$ & $1$ & $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ \\
  5076. $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $1$ \\
  5077. $1$ & $0$ & $1$ & $0$ & $1$ & $0$ & $1$ & $0$ & $2$ \\
  5078. $0$ & $1$ & $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $1$ \\
  5079. $1$ & $1$ & $1$ & $0$ & $29$ & $-26$ & $29$ & $0$ & $32$ \\
  5080. $0$ & $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ & $1$ \\
  5081. $1$ & $0$ & $0$ & $1$ & $75$ & $0$ & $0$ & $53$ & $128$ \\
  5082. $0$ & $1$ & $0$ & $1$ & $0$ & $1$ & $0$ & $1$ & $2$ \\
  5083. $1$ & $1$ & $0$ & $1$ & $75$ & $0$ & $0$ & $53$ & $128$ \\
  5084. $0$ & $0$ & $1$ & $1$ & $0$ & $0$ & $1$ & $0$ & $1$ \\
  5085. $1$ & $0$ & $1$ & $1$ & $75$ & $0$ & $0$ & $53$ & $128$ \\
  5086. $0$ & $1$ & $1$ & $1$ & $0$ & $3$ & $10$ & $3$ & $16$ \\
  5087. $1$ & $1$ & $1$ & $1$ & $29$ & $-26$ & $29$ & $0$ & $32$ \\
  5088. \bottomrule\end{tabular}
  5089. \end{center}
  5090. \caption{Weights and Divisors for Each Set of Available DC Predictors}
  5091. \label{tab:dc-weights}
  5092. \end{table}
  5093. \item
  5094. Assign \bitvar{DCPRED} the value zero.
  5095. \item
  5096. If $\locvar{P}[0]$ is non-zero, assign \bitvar{DCPRED} the value
  5097. $(\bitvar{DCPRED}+\locvar{W}[0]*\bitvar{COEFFS}[\locvar{PBI}[0]][0])$.
  5098. \item
  5099. If $\locvar{P}[1]$ is non-zero, assign \bitvar{DCPRED} the value
  5100. $(\bitvar{DCPRED}+\locvar{W}[1]*\bitvar{COEFFS}[\locvar{PBI}[1]][0])$.
  5101. \item
  5102. If $\locvar{P}[2]$ is non-zero, assign \bitvar{DCPRED} the value
  5103. $(\bitvar{DCPRED}+\locvar{W}[2]*\bitvar{COEFFS}[\locvar{PBI}[2]][0])$.
  5104. \item
  5105. If $\locvar{P}[3]$ is non-zero, assign \bitvar{DCPRED} the value
  5106. $(\bitvar{DCPRED}+\locvar{W}[3]*\bitvar{COEFFS}[\locvar{PBI}[3]][0])$.
  5107. \item
  5108. Assign \bitvar{DCPRED} the value $(\bitvar{DCPRED}//\locvar{PDIV})$.
  5109. \item
  5110. If $\locvar{P}[0]$, $\locvar{P}[1]$, and $\locvar{P}[2]$ are all non-zero:
  5111. \begin{enumerate}
  5112. \item
  5113. If $|\bitvar{DCPRED}-\bitvar{COEFFS}[\locvar{PBI}[2]][0]|$ is greater than
  5114. $128$, assign \bitvar{DCPRED} the value $\bitvar{COEFFS}[\locvar{PBI}[2]][0]$.
  5115. \item
  5116. Otherwise, if $|\bitvar{DCPRED}-\bitvar{COEFFS}[\locvar{PBI}[0]][0]|$ is
  5117. greater than $128$, assign \bitvar{DCPRED} the value
  5118. $\bitvar{COEFFS}[\locvar{PBI}[0]][0]$.
  5119. \item
  5120. Otherwise, if $|\bitvar{DCPRED}-\bitvar{COEFFS}[\locvar{PBI}[1]][0]|$ is
  5121. greater than $128$, assign \bitvar{DCPRED} the value
  5122. $\bitvar{COEFFS}[\locvar{PBI}[1]][0]$.
  5123. \end{enumerate}
  5124. \end{enumerate}
  5125. \end{enumerate}
  5126. \subsection{Inverting the DC Prediction Process}
  5127. \label{sub:dc-pred-undo}
  5128. \paragraph{Input parameters:}\hfill\\*
  5129. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5130. \multicolumn{1}{c}{Name} &
  5131. \multicolumn{1}{c}{Type} &
  5132. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5133. \multicolumn{1}{c}{Signed?} &
  5134. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5135. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5136. 1 & No & An \bitvar{NBS}-element array of flags
  5137. indicating which blocks are coded. \\
  5138. \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5139. 3 & No & An \bitvar{NMBS}-element array of
  5140. coding modes for each macro block. \\
  5141. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5142. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  5143. quantized DCT coefficient values for each block in zig-zag order. \\
  5144. \bottomrule\end{tabularx}
  5145. \paragraph{Output parameters:}\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. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5153. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  5154. quantized DCT coefficient values for each block in zig-zag order. The DC
  5155. value of each block will be updated. \\
  5156. \bottomrule\end{tabularx}
  5157. \paragraph{Variables used:}\hfill\\*
  5158. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5159. \multicolumn{1}{c}{Name} &
  5160. \multicolumn{1}{c}{Type} &
  5161. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5162. \multicolumn{1}{c}{Signed?} &
  5163. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5164. \locvar{LASTDC} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5165. 16 & Yes & A 3-element array containing the
  5166. most recently decoded DC value, one for inter mode and for each reference
  5167. frame. \\
  5168. \locvar{DCPRED} & Integer & 11 & Yes & The predicted DC value for the current
  5169. block. \\
  5170. \locvar{DC} & Integer & 17 & Yes & The actual DC value for the current
  5171. block. \\
  5172. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  5173. coded order. \\
  5174. \locvar{\mbi} & Integer & 32 & No & The index of the macro block
  5175. containing block \locvar{\bi}. \\
  5176. \locvar{\rfi} & Integer & 2 & No & The index of the reference frame
  5177. indicated by the coding mode for macro block \locvar{\mbi}. \\
  5178. \locvar{\pli} & Integer & 2 & No & A color plane index. \\
  5179. \bottomrule\end{tabularx}
  5180. \medskip
  5181. This procedure describes the complete process of undoing the DC prediction to
  5182. recover the original DC values.
  5183. Because it is possible to add a value as large as $580$ to the predicted DC
  5184. coefficient value at every block, which will then be used to increase the
  5185. predictor for the next block, the reconstructed DC value could overflow a
  5186. 16-bit integer.
  5187. This is handled by truncating the result to a 16-bit signed representation,
  5188. simply throwing away any higher bits in the two's complement representation of
  5189. the number.
  5190. \begin{enumerate}
  5191. \item
  5192. For each consecutive value of \locvar{\pli} from $0$ to $2$:
  5193. \begin{enumerate}
  5194. \item
  5195. Assign $\locvar{LASTDC}[0]$ the value zero.
  5196. \item
  5197. Assign $\locvar{LASTDC}[1]$ the value zero.
  5198. \item
  5199. Assign $\locvar{LASTDC}[2]$ the value zero.
  5200. \item
  5201. For each block of color plane \locvar{\pli} in {\em raster} order, with
  5202. coded-order index \locvar{\bi}:
  5203. \begin{enumerate}
  5204. \item
  5205. If $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero:
  5206. \begin{enumerate}
  5207. \item
  5208. Compute the value \locvar{DCPRED} using the procedure outlined in
  5209. Section~\ref{sub:dc-pred}.
  5210. \item
  5211. Assign \locvar{DC} the value
  5212. $(\bitvar{COEFFS}[\locvar{\bi}][0]+\locvar{DCPRED})$.
  5213. \item
  5214. Truncate \locvar{DC} to a 16-bit representation by dropping any higher-order
  5215. bits.
  5216. \item
  5217. Assign $\bitvar{COEFFS}[\locvar{\bi}][0]$ the value \locvar{DC}.
  5218. \item
  5219. Assign \locvar{\mbi} the index of the macro block containing block
  5220. \locvar{\bi}.
  5221. \item
  5222. Assign \locvar{\rfi} the value of the Reference Frame Index column of
  5223. Table~\ref{tab:cm-refs} corresponding to $\bitvar{MBMODES}[\locvar{\mbi}]$.
  5224. \item
  5225. Assign $\locvar{LASTDC}[\rfi]$ the value $\locvar{DC}$.
  5226. \end{enumerate}
  5227. \end{enumerate}
  5228. \end{enumerate}
  5229. \end{enumerate}
  5230. \section{Reconstruction}
  5231. At this stage, the complete contents of the data packet have been decoded.
  5232. All that remains is to reconstruct the contents of the new frame.
  5233. This is applied on a block by block basis, and as each block is independent,
  5234. the order they are processed in does not matter.
  5235. \subsection{Predictors}
  5236. \label{sec:predictors}
  5237. For each block, a predictor is formed based on its coding mode and motion
  5238. vector.
  5239. There are three basic types of predictors: the intra predictor, the whole-pixel
  5240. predictor, and the half-pixel predictor.
  5241. The former is used for all blocks coded in INTRA mode, while all other blocks
  5242. use one of the latter two.
  5243. The whole-pixel predictor is used if the fractional part of both motion vector
  5244. components is zero, otherwise the half-pixel predictor is used.
  5245. \subsubsection{The Intra Predictor}
  5246. \label{sub:predintra}
  5247. \paragraph{Input parameters:} None.
  5248. \paragraph{Output parameters:}\hfill\\*
  5249. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5250. \multicolumn{1}{c}{Name} &
  5251. \multicolumn{1}{c}{Type} &
  5252. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5253. \multicolumn{1}{c}{Signed?} &
  5254. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5255. \bitvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5256. 8 & No & An $8\times 8$ array of predictor
  5257. values to use for INTRA coded blocks. \\
  5258. \bottomrule\end{tabularx}
  5259. \paragraph{Variables used:}\hfill\\*
  5260. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5261. \multicolumn{1}{c}{Name} &
  5262. \multicolumn{1}{c}{Type} &
  5263. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5264. \multicolumn{1}{c}{Signed?} &
  5265. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5266. \locvar{\idx{bx}} & Integer & 3 & No & The horizontal pixel index in the
  5267. block. \\
  5268. \locvar{\idx{by}} & Integer & 3 & No & The vertical pixel index in the
  5269. block. \\
  5270. \bottomrule\end{tabularx}
  5271. \medskip
  5272. The intra predictor is nothing more than the constant value $128$.
  5273. This is applied for the sole purpose of centering the range of possible DC
  5274. values for INTRA blocks around zero.
  5275. \begin{enumerate}
  5276. \item
  5277. For each value of \locvar{\idx{by}} from $0$ to $7$, inclusive:
  5278. \begin{enumerate}
  5279. \item
  5280. For each value of \locvar{\idx{bx}} from $0$ to $7$, inclusive:
  5281. \begin{enumerate}
  5282. \item
  5283. Assign $\bitvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value $128$.
  5284. \end{enumerate}
  5285. \end{enumerate}
  5286. \end{enumerate}
  5287. \subsubsection{The Whole-Pixel Predictor}
  5288. \label{sub:predfullpel}
  5289. \paragraph{Input parameters:}\hfill\\*
  5290. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5291. \multicolumn{1}{c}{Name} &
  5292. \multicolumn{1}{c}{Type} &
  5293. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5294. \multicolumn{1}{c}{Signed?} &
  5295. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5296. \bitvar{RPW} & Integer & 20 & No & The width of the current plane of the
  5297. reference frame in pixels. \\
  5298. \bitvar{RPH} & Integer & 20 & No & The height of the current plane of the
  5299. reference frame in pixels. \\
  5300. \bitvar{REFP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5301. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  5302. array containing the contents of the current plane of the reference frame. \\
  5303. \bitvar{BX} & Integer & 20 & No & The horizontal pixel index of the
  5304. lower-left corner of the current block. \\
  5305. \bitvar{BY} & Integer & 20 & No & The vertical pixel index of the
  5306. lower-left corner of the current block. \\
  5307. \bitvar{MVX} & Integer & 5 & No & The horizontal component of the block
  5308. motion vector.
  5309. This is always a whole-pixel value. \\
  5310. \bitvar{MVY} & Integer & 5 & No & The vertical component of the block
  5311. motion vector.
  5312. This is always a whole-pixel value. \\
  5313. \bottomrule\end{tabularx}
  5314. \paragraph{Output parameters:}\hfill\\*
  5315. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5316. \multicolumn{1}{c}{Name} &
  5317. \multicolumn{1}{c}{Type} &
  5318. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5319. \multicolumn{1}{c}{Signed?} &
  5320. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5321. \bitvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5322. 8 & No & An $8\times 8$ array of predictor
  5323. values to use for INTER coded blocks. \\
  5324. \bottomrule\end{tabularx}
  5325. \paragraph{Variables used:}\hfill\\*
  5326. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5327. \multicolumn{1}{c}{Name} &
  5328. \multicolumn{1}{c}{Type} &
  5329. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5330. \multicolumn{1}{c}{Signed?} &
  5331. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5332. \locvar{\idx{bx}} & Integer & 3 & Yes & The horizontal pixel index in the
  5333. block. \\
  5334. \locvar{\idx{by}} & Integer & 3 & Yes & The vertical pixel index in the
  5335. block. \\
  5336. \locvar{\idx{rx}} & Integer & 20 & No & The horizontal pixel index in the
  5337. reference frame. \\
  5338. \locvar{\idx{ry}} & Integer & 20 & No & The vertical pixel index in the
  5339. reference frame. \\
  5340. \bottomrule\end{tabularx}
  5341. \medskip
  5342. The whole pixel predictor simply copies verbatim the contents of the reference
  5343. frame pointed to by the block's motion vector.
  5344. If the vector points outside the reference frame, then the closest value on the
  5345. edge of the reference frame is used instead.
  5346. In practice, this is usually implemented by expanding the size of the reference
  5347. frame by $8$ or $16$ pixels on each side---depending on whether or not the
  5348. corresponding axis is subsampled in the current plane---and copying the border
  5349. pixels into this region.
  5350. \begin{enumerate}
  5351. \item
  5352. For each value of \locvar{\idx{by}} from $0$ to $7$, inclusive:
  5353. \begin{enumerate}
  5354. \item
  5355. Assign \locvar{\idx{ry}} the value
  5356. $(\bitvar{BY}+\bitvar{MVY}+\locvar{\idx{by}})$.
  5357. \item
  5358. If \locvar{\idx{ry}} is greater than $(\bitvar{RPH}-1)$, assign
  5359. \locvar{\idx{ry}} the value $(\bitvar{RPH}-1)$.
  5360. \item
  5361. If \locvar{\idx{ry}} is less than zero, assign \locvar{\idx{ry}} the value
  5362. zero.
  5363. \item
  5364. For each value of \locvar{\idx{bx}} from $0$ to $7$, inclusive:
  5365. \begin{enumerate}
  5366. \item
  5367. Assign \locvar{\idx{rx}} the value
  5368. $(\bitvar{BX}+\bitvar{MVX}+\locvar{\idx{bx}})$.
  5369. \item
  5370. If \locvar{\idx{rx}} is greater than $(\bitvar{RPW}-1)$, assign
  5371. \locvar{\idx{rx}} the value $(\bitvar{RPW}-1)$.
  5372. \item
  5373. If \locvar{\idx{rx}} is less than zero, assign \locvar{\idx{rx}} the value
  5374. zero.
  5375. \item
  5376. Assign $\bitvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value
  5377. $\bitvar{REFP}[\locvar{\idx{ry}}][\locvar{\idx{rx}}]$.
  5378. \end{enumerate}
  5379. \end{enumerate}
  5380. \end{enumerate}
  5381. \subsubsection{The Half-Pixel Predictor}
  5382. \label{sub:predhalfpel}
  5383. \paragraph{Input parameters:}\hfill\\*
  5384. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5385. \multicolumn{1}{c}{Name} &
  5386. \multicolumn{1}{c}{Type} &
  5387. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5388. \multicolumn{1}{c}{Signed?} &
  5389. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5390. \bitvar{RPW} & Integer & 20 & No & The width of the current plane of the
  5391. reference frame in pixels. \\
  5392. \bitvar{RPH} & Integer & 20 & No & The height of the current plane of the
  5393. reference frame in pixels. \\
  5394. \bitvar{REFP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5395. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  5396. array containing the contents of the current plane of the reference frame. \\
  5397. \bitvar{BX} & Integer & 20 & No & The horizontal pixel index of the
  5398. lower-left corner of the current block. \\
  5399. \bitvar{BY} & Integer & 20 & No & The vertical pixel index of the
  5400. lower-left corner of the current block. \\
  5401. \bitvar{MVX} & Integer & 5 & No & The horizontal component of the first
  5402. whole-pixel motion vector. \\
  5403. \bitvar{MVY} & Integer & 5 & No & The vertical component of the first
  5404. whole-pixel motion vector. \\
  5405. \bitvar{MVX2} & Integer & 5 & No & The horizontal component of the second
  5406. whole-pixel motion vector. \\
  5407. \bitvar{MVY2} & Integer & 5 & No & The vertical component of the second
  5408. whole-pixel motion vector. \\
  5409. \bottomrule\end{tabularx}
  5410. \paragraph{Output parameters:}\hfill\\*
  5411. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5412. \multicolumn{1}{c}{Name} &
  5413. \multicolumn{1}{c}{Type} &
  5414. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5415. \multicolumn{1}{c}{Signed?} &
  5416. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5417. \bitvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5418. 8 & No & An $8\times 8$ array of predictor
  5419. values to use for INTER coded blocks. \\
  5420. \bottomrule\end{tabularx}
  5421. \paragraph{Variables used:}\hfill\\*
  5422. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5423. \multicolumn{1}{c}{Name} &
  5424. \multicolumn{1}{c}{Type} &
  5425. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5426. \multicolumn{1}{c}{Signed?} &
  5427. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5428. \locvar{\idx{bx}} & Integer & 3 & Yes & The horizontal pixel index in the
  5429. block. \\
  5430. \locvar{\idx{by}} & Integer & 3 & Yes & The vertical pixel index in the
  5431. block. \\
  5432. \locvar{\idx{rx1}} & Integer & 20 & No & The first horizontal pixel index in
  5433. the reference frame. \\
  5434. \locvar{\idx{ry1}} & Integer & 20 & No & The first vertical pixel index in the
  5435. reference frame. \\
  5436. \locvar{\idx{rx2}} & Integer & 20 & No & The second horizontal pixel index in
  5437. the reference frame. \\
  5438. \locvar{\idx{ry2}} & Integer & 20 & No & The second vertical pixel index in
  5439. the reference frame. \\
  5440. \bottomrule\end{tabularx}
  5441. \medskip
  5442. If one or both of the components of the block motion vector is not a
  5443. whole-pixel value, then the half-pixel predictor is used.
  5444. The half-pixel predictor converts the fractional motion vector into two
  5445. whole-pixel motion vectors.
  5446. The first is formed by truncating the values of each component towards zero,
  5447. and the second is formed by truncating them away from zero.
  5448. The contributions from the reference frame at the locations pointed to by each
  5449. vector are averaged, truncating towards negative infinity.
  5450. Only two samples from the reference frame contribute to each predictor value,
  5451. even if both components of the motion vector have non-zero fractional
  5452. components.
  5453. Motion vector components with quarter-pixel accuracy in the chroma planes are
  5454. treated exactly the same as those with half-pixel accuracy.
  5455. Any non-zero fractional part gets rounded one way in the first vector, and the
  5456. other way in the second.
  5457. \begin{enumerate}
  5458. \item
  5459. For each value of \locvar{\idx{by}} from $0$ to $7$, inclusive:
  5460. \begin{enumerate}
  5461. \item
  5462. Assign \locvar{\idx{ry1}} the value
  5463. $(\bitvar{BY}+\bitvar{MVY1}+\locvar{\idx{by}})$.
  5464. \item
  5465. If \locvar{\idx{ry1}} is greater than $(\bitvar{RPH}-1)$, assign
  5466. \locvar{\idx{ry1}} the value $(\bitvar{RPH}-1)$.
  5467. \item
  5468. If \locvar{\idx{ry1}} is less than zero, assign \locvar{\idx{ry1}} the value
  5469. zero.
  5470. \item
  5471. Assign \locvar{\idx{ry2}} the value
  5472. $(\bitvar{BY}+\bitvar{MVY2}+\locvar{\idx{by}})$.
  5473. \item
  5474. If \locvar{\idx{ry2}} is greater than $(\bitvar{RPH}-1)$, assign
  5475. \locvar{\idx{ry2}} the value $(\bitvar{RPH}-1)$.
  5476. \item
  5477. If \locvar{\idx{ry2}} is less than zero, assign \locvar{\idx{ry2}} the value
  5478. zero.
  5479. \item
  5480. For each value of \locvar{\idx{bx}} from $0$ to $7$, inclusive:
  5481. \begin{enumerate}
  5482. \item
  5483. Assign \locvar{\idx{rx1}} the value
  5484. $(\bitvar{BX}+\bitvar{MVX1}+\locvar{\idx{bx}})$.
  5485. \item
  5486. If \locvar{\idx{rx1}} is greater than $(\bitvar{RPW}-1)$, assign
  5487. \locvar{\idx{rx1}} the value $(\bitvar{RPW}-1)$.
  5488. \item
  5489. If \locvar{\idx{rx1}} is less than zero, assign \locvar{\idx{rx1}} the value
  5490. zero.
  5491. \item
  5492. Assign \locvar{\idx{rx2}} the value
  5493. $(\bitvar{BX}+\bitvar{MVX2}+\locvar{\idx{bx}})$.
  5494. \item
  5495. If \locvar{\idx{rx2}} is greater than $(\bitvar{RPW}-1)$, assign
  5496. \locvar{\idx{rx2}} the value $(\bitvar{RPW}-1)$.
  5497. \item
  5498. If \locvar{\idx{rx2}} is less than zero, assign \locvar{\idx{rx2}} the value
  5499. zero.
  5500. \item
  5501. Assign $\bitvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value
  5502. \begin{equation*}
  5503. (\bitvar{REFP}[\locvar{\idx{ry1}}][\locvar{\idx{rx1}}]+
  5504. \bitvar{REFP}[\locvar{\idx{ry2}}][\locvar{\idx{rx2}}])>>1.
  5505. \end{equation*}
  5506. \end{enumerate}
  5507. \end{enumerate}
  5508. \end{enumerate}
  5509. \subsection{Dequantization}
  5510. \label{sub:dequant}
  5511. \paragraph{Input parameters:}\hfill\\*
  5512. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5513. \multicolumn{1}{c}{Name} &
  5514. \multicolumn{1}{c}{Type} &
  5515. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5516. \multicolumn{1}{c}{Signed?} &
  5517. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5518. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5519. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  5520. quantized DCT coefficient values for each block in zig-zag order. \\
  5521. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  5522. 16 & No & A 64-element array of scale values for
  5523. AC coefficients for each \qi\ value. \\
  5524. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  5525. 16 & No & A 64-element array of scale values for
  5526. the DC coefficient for each \qi\ value. \\
  5527. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  5528. 8 & No & A $\bitvar{NBMS}\times 64$ array
  5529. containing the base matrices. \\
  5530. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  5531. 6 & No & A $2\times 3$ array containing the
  5532. number of quant ranges for a given \qti\ and \pli, respectively.
  5533. This is at most $63$. \\
  5534. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  5535. 6 & No & A $2\times 3\times 63$ array of the
  5536. sizes of each quant range for a given \qti\ and \pli, respectively.
  5537. Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
  5538. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  5539. 9 & No & A $2\times 3\times 64$ array of the
  5540. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  5541. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
  5542. \bitvar{\qti} & Integer & 1 & No & A quantization type index.
  5543. See Table~\ref{tab:quant-types}.\\
  5544. \bitvar{\pli} & Integer & 2 & No & A color plane index.
  5545. See Table~\ref{tab:color-planes}.\\
  5546. \bitvar{\idx{qi0}} & Integer & 6 & No & The quantization index of the DC
  5547. coefficient. \\
  5548. \bitvar{\qi} & Integer & 6 & No & The quantization index of the AC
  5549. coefficients. \\
  5550. \bitvar{\bi} & Integer & 36 & No & The index of the current block in
  5551. coded order. \\
  5552. \bottomrule\end{tabularx}
  5553. \paragraph{Output parameters:}\hfill\\*
  5554. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5555. \multicolumn{1}{c}{Name} &
  5556. \multicolumn{1}{c}{Type} &
  5557. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5558. \multicolumn{1}{c}{Signed?} &
  5559. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5560. \bitvar{DQC} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5561. 14 & Yes & A $64$-element array of dequantized
  5562. DCT coefficients in natural order (cf. Section~\ref{sec:dct-coeffs}). \\
  5563. \bottomrule\end{tabularx}
  5564. \paragraph{Variables used:}\hfill\\*
  5565. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5566. \multicolumn{1}{c}{Name} &
  5567. \multicolumn{1}{c}{Type} &
  5568. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5569. \multicolumn{1}{c}{Signed?} &
  5570. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5571. \locvar{QMAT} & \multicolumn{1}{p{40pt}}{Integer array} &
  5572. 16 & No & A 64-element array of quantization
  5573. values for each DCT coefficient in natural order. \\
  5574. \locvar{\ci} & Integer & 6 & No & The DCT coefficient index in natural
  5575. order. \\
  5576. \locvar{\zzi} & Integer & 6 & No & The DCT coefficient index in zig-zag
  5577. order. \\
  5578. \locvar{C} & Integer & 29 & Yes & A single dequantized coefficient. \\
  5579. \bottomrule\end{tabularx}
  5580. \medskip
  5581. This procedure takes the quantized DCT coefficient values in zig-zag order for
  5582. a single block---after DC prediction has been undone---and returns the
  5583. dequantized values in natural order.
  5584. If large coefficient values are decoded for coarsely quantized coefficients,
  5585. the resulting dequantized value can be significantly larger than 16 bits.
  5586. Such a coefficient is truncated to a signed 16-bit representation by discarding
  5587. the higher-order bits of its twos-complement representation.
  5588. Although this procedure recomputes the quantization matrices from the
  5589. parameters in the setup header for each block, there are at most six different
  5590. ones used for each color plane.
  5591. An efficient implementation could compute them once in advance.
  5592. \begin{enumerate}
  5593. \item
  5594. Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS},
  5595. \bitvar{QRSIZES}, \bitvar{QRBMIS}, \bitvar{\qti}, \bitvar{\pli}, and
  5596. \bitvar{\idx{qi0}}, use the procedure given in Section~\ref{sub:quant-mat} to
  5597. compute the DC quantization matrix \locvar{QMAT}.
  5598. \item
  5599. Assign \locvar{C} the value
  5600. $\bitvar{COEFFS}[\bitvar{\bi}][0]*\locvar{QMAT}[0]$.
  5601. \item
  5602. Truncate \locvar{C} to a 16-bit representation by dropping any higher-order
  5603. bits.
  5604. \item
  5605. Assign $\bitvar{DQC}[0]$ the value \locvar{C}.
  5606. \item
  5607. Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS},
  5608. \bitvar{QRSIZES}, \bitvar{QRBMIS}, \bitvar{\qti}, \bitvar{\pli}, and
  5609. \bitvar{\qi}, use the procedure given in Section~\ref{sub:quant-mat} to
  5610. compute the AC quantization matrix \locvar{QMAT}.
  5611. \item
  5612. For each value of \locvar{\ci} from 1 to 63, inclusive:
  5613. \begin{enumerate}
  5614. \item
  5615. Assign \locvar{\zzi} the index in zig-zag order corresponding to \locvar{\ci}.
  5616. E.g., the value at row $(\locvar{\ci}//8)$ and column $(\locvar{\ci}\%8)$ in
  5617. Figure~\ref{tab:zig-zag}
  5618. \item
  5619. Assign \locvar{C} the value
  5620. $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\zzi}]*\locvar{QMAT}[\locvar{\ci}]$.
  5621. \item
  5622. Truncate \locvar{C} to a 16-bit representation by dropping any higher-order
  5623. bits.
  5624. \item
  5625. Assign $\bitvar{DQC}[\locvar{\ci}]$ the value \locvar{C}.
  5626. \end{enumerate}
  5627. \end{enumerate}
  5628. \subsection{The Inverse DCT}
  5629. The 2D inverse DCT is separated into two applications of the 1D inverse DCT.
  5630. The transform is first applied to each row, and then applied to each column of
  5631. the result.
  5632. Each application of the 1D inverse DCT scales the values by a factor of two
  5633. relative to the orthonormal version of the transform, for a total scale factor
  5634. of four for the 2D transform.
  5635. It is assumed that a similar scale factor is applied during the forward DCT
  5636. used in the encoder, so that a division by 16 is required after the transform
  5637. has been applied in both directions.
  5638. The inclusion of this scale factor allows the integerized transform to operate
  5639. with increased precision.
  5640. All divisions throughout the transform are implemented with right shifts.
  5641. Only the final division by $16$ is rounded, with ties rounded towards positive
  5642. infinity.
  5643. All intermediate values are truncated to a 32-bit signed representation by
  5644. discarding any higher-order bits in their two's complement representation.
  5645. The final output of each 1D transform is truncated to a 16-bit signed value in
  5646. the same manner.
  5647. In practice, if the high word of a $16\times 16$ bit multiplication can be
  5648. obtained directly, 16 bits is sufficient for every calculation except scaling
  5649. by $C4$.
  5650. Thus we truncate to 16 bits before that multiplication to allow an
  5651. implementation entirely in 16-bit registers.
  5652. Implementations using larger registers must sign-extend the 16-bit value to
  5653. maintain compatibility.
  5654. Note that if 16-bit register are used, overflow in the additions and
  5655. subtractions should be handled using \textit{unsaturated} arithmetic.
  5656. That is, the high-order bits should be discarded and the low-order bits
  5657. retained, instead of clamping the result to the maximum or minimum value.
  5658. This allows the maximum flexibility in re-ordering these instructions without
  5659. deviating from this specification.
  5660. The 1D transform can only overflow if input coefficients larger than $\pm 6201$
  5661. are present.
  5662. However, the result of applying the 2D forward transform on pixel values in the
  5663. range $-255\ldots 255$ can be as large as $\pm 8157$ due to the scale factor
  5664. of four that is applied, and quantization errors could make this even larger.
  5665. Therefore, the coefficients cannot simply be clamped into a valid range before
  5666. the transform.
  5667. \subsubsection{The 1D Inverse DCT}
  5668. \label{sub:1d-idct}
  5669. \paragraph{Input parameters:}\hfill\\*
  5670. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5671. \multicolumn{1}{c}{Name} &
  5672. \multicolumn{1}{c}{Type} &
  5673. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5674. \multicolumn{1}{c}{Signed?} &
  5675. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5676. \bitvar{Y} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5677. 16 & Yes & An 8-element array of DCT
  5678. coefficients. \\
  5679. \bottomrule\end{tabularx}
  5680. \paragraph{Output parameters:}\hfill\\*
  5681. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5682. \multicolumn{1}{c}{Name} &
  5683. \multicolumn{1}{c}{Type} &
  5684. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5685. \multicolumn{1}{c}{Signed?} &
  5686. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5687. \bitvar{X} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5688. 16 & Yes & An 8-element array of output values. \\
  5689. \bottomrule\end{tabularx}
  5690. \paragraph{Variables used:}\hfill\\*
  5691. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5692. \multicolumn{1}{c}{Name} &
  5693. \multicolumn{1}{c}{Type} &
  5694. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5695. \multicolumn{1}{c}{Signed?} &
  5696. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5697. \locvar{T} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5698. 32 & Yes & An 8-element array containing the
  5699. current value of each signal line. \\
  5700. \locvar{R} & Integer & 32 & Yes & A temporary value. \\
  5701. \bottomrule\end{tabularx}
  5702. \medskip
  5703. A compliant decoder MUST use the exact implementation of the inverse DCT
  5704. defined in this specification.
  5705. Some operations may be re-ordered, but the result must be precisely equivalent.
  5706. This is a design decision that limits some avenues of decoder optimization, but
  5707. prevents any drift in the prediction loop.
  5708. The full inverse DCT described in this section is only used when there is at
  5709. least one non-zero AC coefficient.
  5710. Otherwise, a special DC-only case is used, which is described below in
  5711. step~\ref{step:dc-only-idct} of Section~\ref{sub:recon}.
  5712. The DC-only special case is not exactly equivalent to applying the full inverse
  5713. transform, because it avoids the intermediate multiplications and truncation,
  5714. and it MUST be used in place of the full transform to ensure bit-exact decode.
  5715. Theora uses a 16-bit integerized approximation of of the 8-point 1D inverse DCT
  5716. based on the Chen factorization \cite{CSF77}.
  5717. It requires 16 multiplications and 26 additions and subtractions.
  5718. \begin{figure}[htbp]
  5719. \begin{center}
  5720. \includegraphics[width=\textwidth]{idct}
  5721. \end{center}
  5722. \caption{Signal Flow Graph for the 1D Inverse DCT}
  5723. \label{fig:idct}
  5724. \end{figure}
  5725. A signal flow graph of the transformation is presented in
  5726. Figure~\ref{fig:idct}.
  5727. This graph provides a good visualization of which parts of the transform are
  5728. parallelizable.
  5729. Time increases from left to right.
  5730. Each signal line is involved in an operation where the line is marked with a
  5731. dot $\cdot$ or a circled plus sign $\oplus$.
  5732. The constants $\locvar{C}i$ and $\locvar{S}j$ are the 16-bit integer
  5733. approximations of $\cos(\frac{i\pi}{16})$ and $\sin(\frac{j\pi}{16})$ listed
  5734. in Table~\ref{tab:dct-consts}.
  5735. When they appear next to a signal line, the value on that line is scaled by the
  5736. given constant.
  5737. A circled minus sign $\ominus$ next to a signal line indicates that the value
  5738. on that line is negated.
  5739. Operations on a single signal path through the graph cannot be reordered, but
  5740. operations on different paths may be, or may be executed in parallel.
  5741. Different graphs may be obtainable using the associative, commutative, and
  5742. distributive properties of unsaturated arithmetic.
  5743. The column of numbers on the left represents an initial permutation of the
  5744. input DCT coefficients.
  5745. The column on the right represents the unpermuted output.
  5746. One can be obtained by bit-reversing the 3-bit binary representation of the
  5747. other.
  5748. \begin{table}[htbp]
  5749. \begin{center}
  5750. \begin{tabular}{llr}\toprule
  5751. $\locvar{C}i$ & $\locvar{S}j$ & Value \\\midrule
  5752. $\locvar{C1}$ & $\locvar{S7}$ & $64277$ \\
  5753. $\locvar{C2}$ & $\locvar{S6}$ & $60547$ \\
  5754. $\locvar{C3}$ & $\locvar{S5}$ & $54491$ \\
  5755. $\locvar{C4}$ & $\locvar{S4}$ & $46341$ \\
  5756. $\locvar{C5}$ & $\locvar{S3}$ & $36410$ \\
  5757. $\locvar{C6}$ & $\locvar{S2}$ & $25080$ \\
  5758. $\locvar{C7}$ & $\locvar{S1}$ & $12785$ \\
  5759. \bottomrule\end{tabular}
  5760. \end{center}
  5761. \caption{16-bit Approximations of Sines and Cosines}
  5762. \label{tab:dct-consts}
  5763. \end{table}
  5764. \begin{enumerate}
  5765. \item
  5766. Assign $\locvar{T}[0]$ the value $\bitvar{Y}[0]+\bitvar{Y}[4]$.
  5767. \item
  5768. Truncate $\locvar{T}[0]$ to a 16-bit signed representation by dropping any
  5769. higher-order bits.
  5770. \item
  5771. Assign $\locvar{T}[0]$ the value
  5772. $\locvar{C4}*\locvar{T}[0]>>16$.
  5773. \item
  5774. Assign $\locvar{T}[1]$ the value $\bitvar{Y}[0]-\bitvar{Y}[4]$.
  5775. \item
  5776. Truncate $\locvar{T}[1]$ to a 16-bit signed representation by dropping any
  5777. higher-order bits.
  5778. \item
  5779. Assign $\locvar{T}[1]$ the value $\locvar{C4}*\locvar{T}[1]>>16$.
  5780. \item
  5781. Assign $\locvar{T}[2]$ the value $(\locvar{C6}*\bitvar{Y}[2]>>16)-
  5782. (\locvar{S6}*\bitvar{Y}[6]>>16)$.
  5783. \item
  5784. Assign $\locvar{T}[3]$ the value $(\locvar{S6}*\bitvar{Y}[2]>>16)+
  5785. (\locvar{C6}*\bitvar{Y}[6]>>16)$.
  5786. \item
  5787. Assign $\locvar{T}[4]$ the value $(\locvar{C7}*\bitvar{Y}[1]>>16)-
  5788. (\locvar{S7}*\bitvar{Y}[7]>>16)$.
  5789. \item
  5790. Assign $\locvar{T}[5]$ the value $(\locvar{C3}*\bitvar{Y}[5]>>16)-
  5791. (\locvar{S3}*\bitvar{Y}[3]>>16)$.
  5792. \item
  5793. Assign $\locvar{T}[6]$ the value $(\locvar{S3}*\bitvar{Y}[5]>>16)+
  5794. (\locvar{C3}*\bitvar{Y}[3]>>16)$.
  5795. \item
  5796. Assign $\locvar{T}[7]$ the value $(\locvar{S7}*\bitvar{Y}[1]>>16)+
  5797. (\locvar{C7}*\bitvar{Y}[7]>>16)$.
  5798. \item
  5799. Assign \locvar{R} the value $\locvar{T}[4]+\locvar{T}[5]$.
  5800. \item
  5801. Assign $\locvar{T}[5]$ the value $\locvar{T}[4]-\locvar{T}[5]$.
  5802. \item
  5803. Truncate $\locvar{T}[5]$ to a 16-bit signed representation by dropping any
  5804. higher-order bits.
  5805. \item
  5806. Assign $\locvar{T}[5]$ the value $\locvar{C4}*\locvar{T}[5]>>16$.
  5807. \item
  5808. Assign $\locvar{T}[4]$ the value $\locvar{R}$.
  5809. \item
  5810. Assign \locvar{R} the value $\locvar{T}[7]+\locvar{T}[6]$.
  5811. \item
  5812. Assign $\locvar{T}[6]$ the value $\locvar{T}[7]-\locvar{T}[6]$.
  5813. \item
  5814. Truncate $\locvar{T}[6]$ to a 16-bit signed representation by dropping any
  5815. higher-order bits.
  5816. \item
  5817. Assign $\locvar{T}[6]$ the value $\locvar{C4}*\locvar{T}[6]>>16$.
  5818. \item
  5819. Assign $\locvar{T}[7]$ the value $\locvar{R}$.
  5820. \item
  5821. Assign \locvar{R} the value $\locvar{T}[0]+\locvar{T}[3]$.
  5822. \item
  5823. Assign $\locvar{T}[3]$ the value $\locvar{T}[0]-\locvar{T}[3]$.
  5824. \item
  5825. Assign $\locvar{T}[0]$ the value \locvar{R}.
  5826. \item
  5827. Assign \locvar{R} the value $\locvar{T}[1]+\locvar{T}[2]$
  5828. \item
  5829. Assign $\locvar{T}[2]$ the value $\locvar{T}[1]-\locvar{T}[2]$
  5830. \item
  5831. Assign $\locvar{T}[1]$ the value \locvar{R}.
  5832. \item
  5833. Assign \locvar{R} the value $\locvar{T}[6]+\locvar{T}[5]$.
  5834. \item
  5835. Assign $\locvar{T}[5]$ the value $\locvar{T}[6]-\locvar{T}[5]$.
  5836. \item
  5837. Assign $\locvar{T}[6]$ the value \locvar{R}.
  5838. \item
  5839. Assign \locvar{R} the value $\locvar{T}[0]+\locvar{T}[7]$.
  5840. \item
  5841. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5842. higher-order bits.
  5843. \item
  5844. Assign $\bitvar{X}[0]$ the value \locvar{R}.
  5845. \item
  5846. Assign \locvar{R} the value $\locvar{T}[1]+\locvar{T}[6]$.
  5847. \item
  5848. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5849. higher-order bits.
  5850. \item
  5851. Assign $\bitvar{X}[1]$ the value \locvar{R}.
  5852. \item
  5853. Assign \locvar{R} the value $\locvar{T}[2]+\locvar{T}[5]$.
  5854. \item
  5855. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5856. higher-order bits.
  5857. \item
  5858. Assign $\bitvar{X}[2]$ the value \locvar{R}.
  5859. \item
  5860. Assign \locvar{R} the value $\locvar{T}[3]+\locvar{T}[4]$.
  5861. \item
  5862. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5863. higher-order bits.
  5864. \item
  5865. Assign $\bitvar{X}[3]$ the value \locvar{R}.
  5866. \item
  5867. Assign \locvar{R} the value $\locvar{T}[3]-\locvar{T}[4]$.
  5868. \item
  5869. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5870. higher-order bits.
  5871. \item
  5872. Assign $\bitvar{X}[4]$ the value \locvar{R}.
  5873. \item
  5874. Assign \locvar{R} the value $\locvar{T}[2]-\locvar{T}[5]$.
  5875. \item
  5876. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5877. higher-order bits.
  5878. \item
  5879. Assign $\bitvar{X}[5]$ the value \locvar{R}.
  5880. \item
  5881. Assign \locvar{R} the value $\locvar{T}[1]-\locvar{T}[6]$.
  5882. \item
  5883. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5884. higher-order bits.
  5885. \item
  5886. Assign $\bitvar{X}[6]$ the value \locvar{R}.
  5887. \item
  5888. Assign \locvar{R} the value $\locvar{T}[0]-\locvar{T}[7]$.
  5889. \item
  5890. Truncate \locvar{R} to a 16-bit signed representation by dropping any
  5891. higher-order bits.
  5892. \item
  5893. Assign $\bitvar{X}[7]$ the value \locvar{R}.
  5894. \end{enumerate}
  5895. \subsubsection{The 2D Inverse DCT}
  5896. \label{sub:2d-idct}
  5897. \paragraph{Input parameters:}\hfill\\*
  5898. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5899. \multicolumn{1}{c}{Name} &
  5900. \multicolumn{1}{c}{Type} &
  5901. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5902. \multicolumn{1}{c}{Signed?} &
  5903. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5904. \bitvar{DQC} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5905. 14 & Yes & A $64$-element array of dequantized
  5906. DCT coefficients in natural order (cf. Section~\ref{sec:dct-coeffs}). \\
  5907. \bottomrule\end{tabularx}
  5908. \paragraph{Output parameters:}\hfill\\*
  5909. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5910. \multicolumn{1}{c}{Name} &
  5911. \multicolumn{1}{c}{Type} &
  5912. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5913. \multicolumn{1}{c}{Signed?} &
  5914. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5915. \bitvar{RES} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  5916. 16 & Yes & An $8\times 8$ array containing the
  5917. decoded residual for the current block. \\
  5918. \bottomrule\end{tabularx}
  5919. \paragraph{Variables used:}\hfill\\*
  5920. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5921. \multicolumn{1}{c}{Name} &
  5922. \multicolumn{1}{c}{Type} &
  5923. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5924. \multicolumn{1}{c}{Signed?} &
  5925. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5926. \locvar{\ci} & Integer & 3 & No & The column index. \\
  5927. \locvar{\ri} & Integer & 3 & No & The row index. \\
  5928. \locvar{Y} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5929. 16 & Yes & An 8-element array of 1D iDCT input
  5930. values. \\
  5931. \locvar{X} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5932. 16 & Yes & An 8-element array of 1D iDCT output
  5933. values. \\
  5934. \bottomrule\end{tabularx}
  5935. \medskip
  5936. This procedure applies the 1D inverse DCT transform 16 times to a block of
  5937. dequantized coefficients: once for each of the 8 rows, and once for each of
  5938. the 8 columns of the result.
  5939. Note that the coordinate system used for the columns is the same right-handed
  5940. coordinate system used by the rest of Theora.
  5941. Thus, the column is indexed from bottom to top, not top to bottom.
  5942. The final values are divided by sixteen, rounding with ties rounded towards
  5943. postive infinity.
  5944. \begin{enumerate}
  5945. \item
  5946. For each value of \locvar{\ri} from 0 to 7:
  5947. \begin{enumerate}
  5948. \item
  5949. For each value of \locvar{\ci} from 0 to 7:
  5950. \begin{enumerate}
  5951. \item
  5952. Assign $\locvar{Y}[\locvar{\ci}]$ the value
  5953. $\bitvar{DQC}[\locvar{\ri}*8+\locvar{\ci}]$.
  5954. \end{enumerate}
  5955. \item
  5956. Compute \locvar{X}, the 1D inverse DCT of \locvar{Y} using the procedure
  5957. described in Section~\ref{sub:1d-idct}.
  5958. \item
  5959. For each value of $\locvar{\ci}$ from 0 to 7:
  5960. \begin{enumerate}
  5961. \item
  5962. Assign $\bitvar{RES}[\locvar{\ri}][\locvar{\ci}]$ the value
  5963. $\locvar{X}[\locvar{\ci}]$.
  5964. \end{enumerate}
  5965. \end{enumerate}
  5966. \item
  5967. For each value of \locvar{\ci} from 0 to 7:
  5968. \begin{enumerate}
  5969. \item
  5970. For each value of \locvar{\ri} from 0 to 7:
  5971. \begin{enumerate}
  5972. \item
  5973. Assign $\locvar{Y}[\locvar{\ri}]$ the value
  5974. $\bitvar{RES}[\locvar{\ri}][\locvar{\ci}]$.
  5975. \end{enumerate}
  5976. \item
  5977. Compute \locvar{X}, the 1D inverse DCT of \locvar{Y} using the procedure
  5978. described in Section~\ref{sub:1d-idct}.
  5979. \item
  5980. For each value of \locvar{\ri} from 0 to 7:
  5981. \begin{enumerate}
  5982. \item
  5983. Assign $\bitvar{RES}[\locvar{\ri}][\locvar{\ci}]$ the value
  5984. $(\locvar{X}[\locvar{\ri}]+8)>>4$.
  5985. \end{enumerate}
  5986. \end{enumerate}
  5987. \end{enumerate}
  5988. \subsubsection{The 1D Forward DCT (Non-Normative)}
  5989. \paragraph{Input parameters:}\hfill\\*
  5990. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  5991. \multicolumn{1}{c}{Name} &
  5992. \multicolumn{1}{c}{Type} &
  5993. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  5994. \multicolumn{1}{c}{Signed?} &
  5995. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  5996. \bitvar{X} & \multicolumn{1}{p{40pt}}{Integer Array} &
  5997. 14 & Yes & An 8-element array of input values. \\
  5998. \bottomrule\end{tabularx}
  5999. \paragraph{Output parameters:}\hfill\\*
  6000. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6001. \multicolumn{1}{c}{Name} &
  6002. \multicolumn{1}{c}{Type} &
  6003. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6004. \multicolumn{1}{c}{Signed?} &
  6005. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6006. \bitvar{Y} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6007. 16 & Yes & An 8-element array of DCT
  6008. coefficients. \\
  6009. \bottomrule\end{tabularx}
  6010. \paragraph{Variables used:}\hfill\\*
  6011. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6012. \multicolumn{1}{c}{Name} &
  6013. \multicolumn{1}{c}{Type} &
  6014. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6015. \multicolumn{1}{c}{Signed?} &
  6016. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6017. \locvar{T} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6018. 16 & Yes & An 8-element array containing the
  6019. current value of each signal line. \\
  6020. \locvar{R} & Integer & 16 & Yes & A temporary value. \\
  6021. \bottomrule\end{tabularx}
  6022. \medskip
  6023. The forward transform used in the encoder is not mandated by this standard as
  6024. the inverse one is.
  6025. Precise equivalence in the inverse transform alone is all that is required to
  6026. guarantee that there is no mismatch in the prediction loop between encoder and
  6027. any compliant decoder implementation.
  6028. However, a forward transform is provided here as a convenience for implementing
  6029. an encoder.
  6030. This is the version of the transform used by Xiph.Org's Theora encoder, which
  6031. is the same as that used by VP3.
  6032. Like the inverse DCT, it is first applied to each row, and then applied to each
  6033. column of the result.
  6034. \begin{figure}[htbp]
  6035. \begin{center}
  6036. \includegraphics[width=\textwidth]{fdct}
  6037. \end{center}
  6038. \caption{Signal Flow Graph for the 1D Forward DCT}
  6039. \label{fig:fdct}
  6040. \end{figure}
  6041. The signal flow graph for the forward transform is given in
  6042. Figure~\ref{fig:fdct}.
  6043. It is largely the reverse of the flow graph given for the inverse DCT.
  6044. It is important to note that the signs on the constants in the rotations have
  6045. changed, and the \locvar{C4} scale factors on one of the lower butterflies now
  6046. appear on the opposite side.
  6047. The column of numbers on the left represents the unpermuted input, and the
  6048. column on the right the permuted output DCT coefficients.
  6049. A proper division by $2^{16}$ is done after the multiplications instead of a
  6050. shift in the forward transform.
  6051. This can be implemented quickly by adding an offset of $\hex{FFFF}$ if the
  6052. number is negative, and then shifting as before.
  6053. This slightly increases the computational complexity of the transform.
  6054. Unlike the inverse DCT, 16-bit registers and a $16\times16\rightarrow32$ bit
  6055. multiply are sufficient to avoid any overflow, so long as the input is in the
  6056. range $-6270\ldots 6270$, which is larger than required.
  6057. \begin{enumerate}
  6058. \item
  6059. Assign $\locvar{T}[0]$ the value $\bitvar{X}[0]+\bitvar{X}[7]$.
  6060. \item
  6061. Assign $\locvar{T}[1]$ the value $\bitvar{X}[1]+\bitvar{X}[6]$.
  6062. \item
  6063. Assign $\locvar{T}[2]$ the value $\bitvar{X}[2]+\bitvar{X}[5]$.
  6064. \item
  6065. Assign $\locvar{T}[3]$ the value $\bitvar{X}[3]+\bitvar{X}[4]$.
  6066. \item
  6067. Assign $\locvar{T}[4]$ the value $\bitvar{X}[3]-\bitvar{X}[4]$.
  6068. \item
  6069. Assign $\locvar{T}[5]$ the value $\bitvar{X}[2]-\bitvar{X}[5]$.
  6070. \item
  6071. Assign $\locvar{T}[6]$ the value $\bitvar{X}[1]-\bitvar{X}[6]$.
  6072. \item
  6073. Assign $\locvar{T}[7]$ the value $\bitvar{X}[0]-\bitvar{X}[7]$.
  6074. \item
  6075. Assign \locvar{R} the value $\locvar{T}[0]+\locvar{T}[3]$.
  6076. \item
  6077. Assign $\locvar{T}[3]$ the value $\locvar{T}[0]-\locvar{T}[3]$.
  6078. \item
  6079. Assign $\locvar{T}[0]$ the value \locvar{R}.
  6080. \item
  6081. Assign \locvar{R} the value $\locvar{T}[1]+\locvar{T}[2]$.
  6082. \item
  6083. Assign $\locvar{T}[2]$ the value $\locvar{T}[1]-\locvar{T}[2]$.
  6084. \item
  6085. Assign $\locvar{T}[1]$ the value \locvar{R}.
  6086. \item
  6087. Assign \locvar{R} the value $\locvar{T}[6]-\locvar{T}[5]$.
  6088. \item
  6089. Assign $\locvar{T}[6]$ the value
  6090. $(\locvar{C4}*(\locvar{T}[6]+\locvar{T}[5]))//16$.
  6091. \item
  6092. Assign $\locvar{T}[5]$ the value $(\locvar{C4}*\locvar{R})//16$.
  6093. \item
  6094. Assign \locvar{R} the value $\locvar{T}[4]+\locvar{T}[5]$.
  6095. \item
  6096. Assign $\locvar{T}[5]$ the value $\locvar{T}[4]-\locvar{T}[5]$.
  6097. \item
  6098. Assign $\locvar{T}[4]$ the value \locvar{R}.
  6099. \item
  6100. Assign \locvar{R} the value $\locvar{T}[7]+\locvar{T}[6]$.
  6101. \item
  6102. Assign $\locvar{T}[6]$ the value $\locvar{T}[7]-\locvar{T}[6]$.
  6103. \item
  6104. Assign $\locvar{T}[7]$ the value \locvar{R}.
  6105. \item
  6106. Assign $\bitvar{Y}[0]$ the value
  6107. $(\locvar{C4}*(\locvar{T}[0]+\locvar{T}[1]))//16$.
  6108. \item
  6109. Assign $\bitvar{Y}[4]$ the value
  6110. $(\locvar{C4}*(\locvar{T}[0]-\locvar{T}[1]))//16$.
  6111. \item
  6112. Assign $\bitvar{Y}[2]$ the value
  6113. $((\locvar{S6}*\locvar{T}[3])//16)+
  6114. ((\locvar{C6}*\locvar{T}[2])//16)$.
  6115. \item
  6116. Assign $\bitvar{Y}[6]$ the value
  6117. $((\locvar{C6}*\locvar{T}[3])//16)-
  6118. ((\locvar{S6}*\locvar{T}[2])//16)$.
  6119. \item
  6120. Assign $\bitvar{Y}[1]$ the value
  6121. $((\locvar{S7}*\locvar{T}[7])//16)+
  6122. ((\locvar{C7}*\locvar{T}[4])//16)$.
  6123. \item
  6124. Assign $\bitvar{Y}[5]$ the value
  6125. $((\locvar{S3}*\locvar{T}[6])//16)+
  6126. ((\locvar{C3}*\locvar{T}[5])//16)$.
  6127. \item
  6128. Assign $\bitvar{Y}[3]$ the value
  6129. $((\locvar{C3}*\locvar{T}[6])//16)-
  6130. ((\locvar{S3}*\locvar{T}[5])//16)$.
  6131. \item
  6132. Assign $\bitvar{Y}[7]$ the value
  6133. $((\locvar{C7}*\locvar{T}[7])//16)-
  6134. ((\locvar{S7}*\locvar{T}[4])//16)$.
  6135. \end{enumerate}
  6136. \subsection{The Complete Reconstruction Algorithm}
  6137. \label{sub:recon}
  6138. \paragraph{Input parameters:}\hfill\\*
  6139. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6140. \multicolumn{1}{c}{Name} &
  6141. \multicolumn{1}{c}{Type} &
  6142. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6143. \multicolumn{1}{c}{Signed?} &
  6144. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6145. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  6146. 16 & No & A 64-element array of scale values
  6147. for AC coefficients for each \qi\ value. \\
  6148. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  6149. 16 & No & A 64-element array of scale values
  6150. for the DC coefficient for each \qi\ value. \\
  6151. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  6152. 8 & No & A $\bitvar{NBMS}\times 64$ array
  6153. containing the base matrices. \\
  6154. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  6155. 6 & No & A $2\times 3$ array containing the
  6156. number of quant ranges for a given \qti\ and \pli, respectively.
  6157. This is at most $63$. \\
  6158. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  6159. 6 & No & A $2\times 3\times 63$ array of the
  6160. sizes of each quant range for a given \qti\ and \pli, respectively.
  6161. Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
  6162. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  6163. 9 & No & A $2\times 3\times 64$ array of the
  6164. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  6165. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
  6166. \bitvar{RPYW} & Integer & 20 & No & The width of the $Y'$ plane of the
  6167. reference frames in pixels. \\
  6168. \bitvar{RPYH} & Integer & 20 & No & The height of the $Y'$ plane of the
  6169. reference frames in pixels. \\
  6170. \bitvar{RPCW} & Integer & 20 & No & The width of the $C_b$ and $C_r$
  6171. planes of the reference frames in pixels. \\
  6172. \bitvar{RPCH} & Integer & 20 & No & The height of the $C_b$ and $C_r$
  6173. planes of the reference frames in pixels. \\
  6174. \bitvar{GOLDREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6175. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6176. array containing the contents of the $Y'$ plane of the golden reference
  6177. frame. \\
  6178. \bitvar{GOLDREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6179. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6180. array containing the contents of the $C_b$ plane of the golden reference
  6181. frame. \\
  6182. \bitvar{GOLDREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6183. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6184. array containing the contents of the $C_r$ plane of the golden reference
  6185. frame. \\
  6186. \bitvar{PREVREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6187. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6188. array containing the contents of the $Y'$ plane of the previous reference
  6189. frame. \\
  6190. \bitvar{PREVREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6191. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6192. array containing the contents of the $C_b$ plane of the previous reference
  6193. frame. \\
  6194. \bitvar{PREVREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6195. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6196. array containing the contents of the $C_r$ plane of the previous reference
  6197. frame. \\
  6198. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  6199. frame. \\
  6200. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6201. 1 & No & An \bitvar{NBS}-element array of
  6202. flags indicating which blocks are coded. \\
  6203. \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6204. 3 & No & An \bitvar{NMBS}-element array of
  6205. coding modes for each macro block. \\
  6206. \bitvar{MVECTS} & \multicolumn{1}{p{50pt}}{Array of 2D Integer Vectors} &
  6207. 6 & Yes & An \bitvar{NBS}-element array of
  6208. motion vectors for each block. \\
  6209. \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6210. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  6211. quantized DCT coefficient values for each block in zig-zag order. \\
  6212. \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6213. 7 & No & An \bitvar{NBS}-element array of the
  6214. coefficient count for each block. \\
  6215. \bitvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
  6216. 6 & No & An \bitvar{NQIS}-element array of
  6217. \qi\ values. \\
  6218. \bitvar{QIIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6219. 2 & No & An \bitvar{NBS}-element array of
  6220. \locvar{\qii} values for each block. \\
  6221. \bottomrule\end{tabularx}
  6222. \paragraph{Output parameters:}\hfill\\*
  6223. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6224. \multicolumn{1}{c}{Name} &
  6225. \multicolumn{1}{c}{Type} &
  6226. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6227. \multicolumn{1}{c}{Signed?} &
  6228. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6229. \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6230. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6231. array containing the contents of the $Y'$ plane of the reconstructed frame. \\
  6232. \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6233. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6234. array containing the contents of the $C_b$ plane of the reconstructed frame. \\
  6235. \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6236. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6237. array containing the contents of the $C_r$ plane of the reconstructed frame. \\
  6238. \bottomrule\end{tabularx}
  6239. \paragraph{Variables used:}\hfill\\*
  6240. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6241. \multicolumn{1}{c}{Name} &
  6242. \multicolumn{1}{c}{Type} &
  6243. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6244. \multicolumn{1}{c}{Signed?} &
  6245. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6246. \locvar{RPW} & Integer & 20 & No & The width of the current plane of the
  6247. current reference frame in pixels. \\
  6248. \locvar{RPH} & Integer & 20 & No & The height of the current plane of
  6249. the current reference frame in pixels. \\
  6250. \locvar{REFP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6251. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6252. array containing the contents of the current plane of the current reference
  6253. frame. \\
  6254. \locvar{BX} & Integer & 20 & No & The horizontal pixel index of the
  6255. lower-left corner of the current block. \\
  6256. \locvar{BY} & Integer & 20 & No & The vertical pixel index of the
  6257. lower-left corner of the current block. \\
  6258. \locvar{MVX} & Integer & 5 & No & The horizontal component of the first
  6259. whole-pixel motion vector. \\
  6260. \locvar{MVY} & Integer & 5 & No & The vertical component of the first
  6261. whole-pixel motion vector. \\
  6262. \locvar{MVX2} & Integer & 5 & No & The horizontal component of the second
  6263. whole-pixel motion vector. \\
  6264. \locvar{MVY2} & Integer & 5 & No & The vertical component of the second
  6265. whole-pixel motion vector. \\
  6266. \locvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6267. 8 & No & An $8\times 8$ array of predictor
  6268. values to use for the current block. \\
  6269. \locvar{RES} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6270. 16 & Yes & An $8\times 8$ array containing the
  6271. decoded residual for the current block. \\
  6272. \locvar{QMAT} & \multicolumn{1}{p{40pt}}{Integer array} &
  6273. 16 & No & A 64-element array of quantization
  6274. values for each DCT coefficient in natural order. \\
  6275. \locvar{DC} & Integer & 29 & Yes & The dequantized DC coefficient of a
  6276. block. \\
  6277. \locvar{P} & Integer & 17 & Yes & A reconstructed pixel value. \\
  6278. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  6279. coded order. \\
  6280. \locvar{\mbi} & Integer & 32 & No & The index of the macro block
  6281. containing block \locvar{\bi}. \\
  6282. \locvar{\pli} & Integer & 2 & No & The color plane index of the current
  6283. block. \\
  6284. \locvar{\rfi} & Integer & 2 & No & The index of the reference frame
  6285. indicated by the coding mode for macro block \locvar{\mbi}. \\
  6286. \locvar{\idx{bx}} & Integer & 3 & No & The horizontal pixel index in the
  6287. block. \\
  6288. \locvar{\idx{by}} & Integer & 3 & No & The vertical pixel index in the
  6289. block. \\
  6290. \locvar{\qti} & Integer & 1 & No & A quantization type index.
  6291. See Table~\ref{tab:quant-types}.\\
  6292. \locvar{\idx{qi0}} & Integer & 6 & No & The quantization index of the DC
  6293. coefficient. \\
  6294. \locvar{\qi} & Integer & 6 & No & The quantization index of the AC
  6295. coefficients. \\
  6296. \bottomrule\end{tabularx}
  6297. \medskip
  6298. This section takes the decoded packet data and uses the previously defined
  6299. procedures to reconstruct each block of the current frame.
  6300. For coded blocks, a predictor is formed using the coding mode and, if
  6301. applicable, the motion vector, and then the residual is computed from the
  6302. quantized DCT coefficients.
  6303. For uncoded blocks, the contents of the co-located block are copied from the
  6304. previous frame and the residual is cleared to zero.
  6305. Then the predictor and residual are added, and the result clamped to the range
  6306. $0\ldots 255$ and stored in the current frame.
  6307. In the special case that a block contains only a DC coefficient, the
  6308. dequantization and inverse DCT transform is skipped.
  6309. Instead the constant pixel value for the entire block is computed in one step.
  6310. Note that the truncation of intermediate operations is omitted and the final
  6311. rounding is slightly different in this case.
  6312. The check for whether or not the block contains only a DC coefficient is based
  6313. on the coefficient count returned from the token decode procedure of
  6314. Section~\ref{sec:dct-decode}, and not by checking to see if the remaining
  6315. coefficient values are zero.
  6316. Also note that even when the coefficient count indicates the block contains
  6317. zero coefficients, the DC coefficient is still processed, as undoing DC
  6318. prediction might have made it non-zero.
  6319. After this procedure, the frame is completely reconstructed, but before it can
  6320. be used as a reference frame, a loop filter must be run over it to help reduce
  6321. blocking artifacts.
  6322. This is detailed in Section~\ref{sec:loopfilter}.
  6323. \begin{enumerate}
  6324. \item
  6325. Assign \locvar{\idx{qi0}} the value $\bitvar{QIS}[0]$.
  6326. \item
  6327. For each value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$:
  6328. \begin{enumerate}
  6329. \item
  6330. Assign \locvar{\pli} the index of the color plane block \locvar{\bi} belongs
  6331. to.
  6332. \item
  6333. Assign \locvar{BX} the horizontal pixel index of the lower-left corner of block
  6334. \locvar{\bi}.
  6335. \item
  6336. Assign \locvar{BY} the vertical pixel index of the lower-left corner of block
  6337. \locvar{\bi}.
  6338. \item
  6339. If $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero:
  6340. \begin{enumerate}
  6341. \item
  6342. Assign \locvar{\mbi} the index of the macro block containing block
  6343. \locvar{\bi}.
  6344. \item
  6345. If $\bitvar{MBMODES}[\locvar{\mbi}]$ is 1 (INTRA), assign \locvar{\qti} the
  6346. value $0$.
  6347. \item
  6348. Otherwise, assign \locvar{\qti} the value $1$.
  6349. \item
  6350. Assign \locvar{\rfi} the value of the Reference Frame Index column of
  6351. Table~\ref{tab:cm-refs} corresponding to $\bitvar{MBMODES}[\locvar{\mbi}]$.
  6352. \item
  6353. If \locvar{\rfi} is zero, compute \locvar{PRED} using the procedure given in
  6354. Section~\ref{sub:predintra}.
  6355. \item
  6356. Otherwise:
  6357. \begin{enumerate}
  6358. \item
  6359. Assign \locvar{REFP}, \locvar{RPW}, and \locvar{RPH} the values given in
  6360. Table~\ref{tab:refp} corresponding to current value of \locvar{\rfi} and
  6361. \locvar{\pli}.
  6362. \begin{table}[htbp]
  6363. \begin{center}
  6364. \begin{tabular}{cclll}\toprule
  6365. \locvar{\rfi} & \locvar{\pli} &
  6366. \locvar{REFP} & \locvar{RPW} & \locvar{RPH} \\\midrule
  6367. $1$ & $0$ & \bitvar{PREVREFY} & \bitvar{RPYW} & \bitvar{RPYH} \\
  6368. $1$ & $1$ & \bitvar{PREVREFCB} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6369. $1$ & $2$ & \bitvar{PREVREFCR} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6370. $2$ & $0$ & \bitvar{GOLDREFY} & \bitvar{RPYW} & \bitvar{RPYH} \\
  6371. $2$ & $1$ & \bitvar{GOLDREFCB} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6372. $2$ & $2$ & \bitvar{GOLDREFCR} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6373. \bottomrule\end{tabular}
  6374. \end{center}
  6375. \caption{Reference Planes and Sizes for Each \locvar{\rfi} and \locvar{\pli}}
  6376. \label{tab:refp}
  6377. \end{table}
  6378. \item
  6379. Assign \locvar{MVX} the value
  6380. \begin{equation*}
  6381. \left\lfloor\lvert\bitvar{MVECTS}[\locvar{\bi}]_x\rvert\right\rfloor*
  6382. \sign(\bitvar{MVECTS}[\locvar{\bi}]_x).
  6383. \end{equation*}
  6384. \item
  6385. Assign \locvar{MVY} the value
  6386. \begin{equation*}
  6387. \left\lfloor\lvert\bitvar{MVECTS}[\locvar{\bi}]_y\rvert\right\rfloor*
  6388. \sign(\bitvar{MVECTS}[\locvar{\bi}]_y).
  6389. \end{equation*}
  6390. \item
  6391. Assign \locvar{MVX2} the value
  6392. \begin{equation*}
  6393. \left\lceil\lvert\bitvar{MVECTS}[\locvar{\bi}]_x\rvert\right\rceil*
  6394. \sign(\bitvar{MVECTS}[\locvar{\bi}]_x).
  6395. \end{equation*}
  6396. \item
  6397. Assign \locvar{MVY2} the value
  6398. \begin{equation*}
  6399. \left\lceil\lvert\bitvar{MVECTS}[\locvar{\bi}]_y\rvert\right\rceil*
  6400. \sign(\bitvar{MVECTS}[\locvar{\bi}]_y).
  6401. \end{equation*}
  6402. \item
  6403. If \locvar{MVX} equals \locvar{MVX2} and \locvar{MVY} equals \locvar{MVY2},
  6404. use the values \locvar{REFP}, \locvar{RPW}, \locvar{RPH}, \locvar{BX},
  6405. \locvar{BY}, \locvar{MVX}, and \locvar{MVY}, compute \locvar{PRED} using the
  6406. procedure given in Section~\ref{sub:predfullpel}.
  6407. \item
  6408. Otherwise, use the values \locvar{REFP}, \locvar{RPW}, \locvar{RPH},
  6409. \locvar{BX}, \locvar{BY}, \locvar{MVX}, \locvar{MVY}, \locvar{MVX2}, and
  6410. \locvar{MVY2} to compute \locvar{PRED} using the procedure given in
  6411. Section~\ref{sub:predhalfpel}.
  6412. \end{enumerate}
  6413. \item
  6414. \label{step:dc-only-idct}
  6415. If $\bitvar{NCOEFFS}[\locvar{\bi}]$ is less than 2:
  6416. \begin{enumerate}
  6417. \item
  6418. Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS}, \\
  6419. \bitvar{QRSIZES}, \bitvar{QRBMIS}, \locvar{\qti}, \locvar{\pli}, and
  6420. \locvar{\idx{qi0}}, use the procedure given in Section~\ref{sub:quant-mat} to
  6421. compute the DC quantization matrix \locvar{QMAT}.
  6422. \item
  6423. Assign \locvar{DC} the value
  6424. \begin{equation*}
  6425. (\bitvar{COEFFS}[\bitvar{\bi}][0]*\locvar{QMAT}[0]+15)>>5.
  6426. \end{equation*}
  6427. \item
  6428. Truncate \locvar{DC} to a 16-bit signed representation by dropping any
  6429. higher-order bits.
  6430. \item
  6431. For each value of \locvar{\idx{by}} from 0 to 7, and each value of
  6432. \locvar{\idx{bx}} from 0 to 7, assign
  6433. $\locvar{RES}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value \locvar{DC}.
  6434. \end{enumerate}
  6435. \item
  6436. Otherwise:
  6437. \begin{enumerate}
  6438. \item
  6439. Assign \locvar{\qi} the value $\bitvar{QIS}[\bitvar{QIIS}[\locvar{\bi}]]$.
  6440. \item
  6441. Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS}, \\
  6442. \bitvar{QRSIZES}, \bitvar{QRBMIS}, \locvar{\qti}, \locvar{\pli},
  6443. \locvar{\idx{qi0}}, and \locvar{\qi}, compute \locvar{DQC} using the procedure
  6444. given in Section~\ref{sub:dequant}.
  6445. \item
  6446. Using \locvar{DQC}, compute \locvar{RES} using the procedure given in
  6447. Section~\ref{sub:2d-idct}.
  6448. \end{enumerate}
  6449. \end{enumerate}
  6450. \item
  6451. Otherwise:
  6452. \begin{enumerate}
  6453. \item
  6454. Assign \locvar{\rfi} the value 1.
  6455. \item
  6456. Assign \locvar{REFP}, \locvar{RPW}, and \locvar{RPH} the values given in
  6457. Table~\ref{tab:refp} corresponding to current value of \locvar{\rfi} and
  6458. \locvar{\pli}.
  6459. \item
  6460. Assign \locvar{MVX} the value 0.
  6461. \item
  6462. Assign \locvar{MVY} the value 0.
  6463. \item
  6464. Using the values \locvar{REFP}, \locvar{RPW}, \locvar{RPH}, \locvar{BX},
  6465. \locvar{BY}, \locvar{MVX}, and \locvar{MVY}, compute \locvar{PRED} using the
  6466. procedure given in Section~\ref{sub:predfullpel}.
  6467. This is simply a copy of the co-located block in the previous reference frame.
  6468. \item
  6469. For each value of \locvar{\idx{by}} from 0 to 7, and each value of
  6470. \locvar{\idx{bx}} from 0 to 7, assign
  6471. $\locvar{RES}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value 0.
  6472. \end{enumerate}
  6473. \item
  6474. For each value of \locvar{\idx{by}} from 0 to 7, and each value of
  6475. \locvar{\idx{bx}} from 0 to 7:
  6476. \begin{enumerate}
  6477. \item
  6478. Assign \locvar{P} the value
  6479. $(\locvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]+
  6480. \locvar{RES}[\locvar{\idx{by}}][\locvar{\idx{bx}}])$.
  6481. \item
  6482. If \locvar{P} is greater than $255$, assign \locvar{P} the value $255$.
  6483. \item
  6484. If \locvar{P} is less than $0$, assign \locvar{P} the value $0$.
  6485. \item
  6486. If \locvar{\pli} equals 0, assign
  6487. $\bitvar{RECY}[\locvar{BY}+\locvar{\idx{by}}][\locvar{BX}+\locvar{\idx{bx}}]$
  6488. the value \locvar{P}.
  6489. \item
  6490. Otherwise, if \locvar{\pli} equals 1, assign
  6491. $\bitvar{RECB}[\locvar{BY}+\locvar{\idx{by}}][\locvar{BX}+\locvar{\idx{bx}}]$
  6492. the value \locvar{P}.
  6493. \item
  6494. Otherwise, \locvar{\pli} equals 2, so assign
  6495. $\bitvar{RECR}[\locvar{BY}+\locvar{\idx{by}}][\locvar{BX}+\locvar{\idx{bx}}]$
  6496. the value \locvar{P}.
  6497. \end{enumerate}
  6498. \end{enumerate}
  6499. \end{enumerate}
  6500. \section{Loop Filtering}
  6501. \label{sec:loopfilter}
  6502. \begin{figure}[htbp]
  6503. \begin{center}
  6504. \includegraphics{lflim}
  6505. \end{center}
  6506. \caption{The loop filter response function.}
  6507. \label{fig:lflim}
  6508. \end{figure}
  6509. The loop filter is a simple deblocking filter that is based on running a small
  6510. edge detecting filter over the coded block edges and adjusting the pixel
  6511. values by a tapered response.
  6512. The filter response is modulated by the following non-linear function:
  6513. \begin{align*}
  6514. \lflim(\locvar{R},\bitvar{L})&=\left\{\begin{array}{ll}
  6515. 0, & \locvar{R}\le-2*\bitvar{L} \\
  6516. -\locvar{R}-2*\bitvar{L}, & -2*\bitvar{L}<\locvar{R}\le-\bitvar{L} \\
  6517. \locvar{R}, & -\bitvar{L}<\locvar{R}<\bitvar{L} \\
  6518. -\locvar{R}+2*\bitvar{L}, & \bitvar{L}\le\locvar{R}<2*\bitvar{L} \\
  6519. 0, & 2*\bitvar{L}\le\locvar{R}
  6520. \end{array}\right.
  6521. \end{align*}
  6522. Here \bitvar{L} is a limiting value equal to $\bitvar{LFLIMS}[\idx{qi0}]$.
  6523. It defines the peaks of the function, illustrated in Figure~\ref{fig:lflim}.
  6524. \bitvar{LFLIMS} is an array of values specified in the setup header and is
  6525. indexed by \idx{qi0}, the first quantization index for the frame, the one used
  6526. for all the DC coefficients.
  6527. Larger values of \bitvar{L} indicate a stronger filter.
  6528. \subsection{Horizontal Filter}
  6529. \label{sub:filth}
  6530. \paragraph{Input parameters:}\hfill\\*
  6531. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6532. \multicolumn{1}{c}{Name} &
  6533. \multicolumn{1}{c}{Type} &
  6534. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6535. \multicolumn{1}{c}{Signed?} &
  6536. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6537. \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6538. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6539. array containing the contents of a plane of the reconstructed frame. \\
  6540. \bitvar{FX} & Integer & 20 & No & The horizontal pixel index of the
  6541. lower-left corner of the area to be filtered. \\
  6542. \bitvar{FY} & Integer & 20 & No & The vertical pixel index of the
  6543. lower-left corner of the area to be filtered. \\
  6544. \bitvar{L} & Integer & 7 & No & The loop filter limit value. \\
  6545. \bottomrule\end{tabularx}
  6546. \paragraph{Output parameters:}\hfill\\*
  6547. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6548. \multicolumn{1}{c}{Name} &
  6549. \multicolumn{1}{c}{Type} &
  6550. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6551. \multicolumn{1}{c}{Signed?} &
  6552. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6553. \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6554. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6555. array containing the contents of a plane of the reconstructed frame. \\
  6556. \bottomrule\end{tabularx}
  6557. \paragraph{Variables used:}\hfill\\*
  6558. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6559. \multicolumn{1}{c}{Name} &
  6560. \multicolumn{1}{c}{Type} &
  6561. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6562. \multicolumn{1}{c}{Signed?} &
  6563. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6564. \locvar{R} & Integer & 9 & Yes & The edge detector response. \\
  6565. \locvar{P} & Integer & 9 & Yes & A filtered pixel value. \\
  6566. \locvar{\idx{by}} & Integer & 20 & No & The vertical pixel index in the
  6567. block. \\
  6568. \bottomrule\end{tabularx}
  6569. \medskip
  6570. This procedure applies a $4$-tap horizontal filter to each row of a vertical
  6571. block edge.
  6572. \begin{enumerate}
  6573. \item
  6574. For each value of \locvar{\idx{by}} from $0$ to $7$:
  6575. \begin{enumerate}
  6576. \item
  6577. Assign \locvar{R} the value
  6578. \begin{multline*}
  6579. (\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}]-
  6580. 3*\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]+\\
  6581. 3*\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]-
  6582. \bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+3]+4)>>3
  6583. \end{multline*}
  6584. \item
  6585. Assign \locvar{P} the value
  6586. $(\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]+
  6587. \lflim(\locvar{R},\bitvar{L}))$.
  6588. \item
  6589. If \locvar{P} is less than zero, assign
  6590. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]$ the value zero.
  6591. \item
  6592. Otherwise, if \locvar{P} is greater than $255$, assign
  6593. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]$ the value $255$.
  6594. \item
  6595. Otherwise, assign
  6596. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]$ the value
  6597. \locvar{P}.
  6598. \item
  6599. Assign \locvar{P} the value
  6600. $(\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]-
  6601. \lflim(\locvar{R},\bitvar{L}))$.
  6602. \item
  6603. If \locvar{P} is less than zero, assign
  6604. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]$ the value zero.
  6605. \item
  6606. Otherwise, if \locvar{P} is greater than $255$, assign
  6607. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]$ the value $255$.
  6608. \item
  6609. Otherwise, assign
  6610. $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]$ the value
  6611. \locvar{P}.
  6612. \end{enumerate}
  6613. \end{enumerate}
  6614. \subsection{Vertical Filter}
  6615. \label{sub:filtv}
  6616. \paragraph{Input parameters:}\hfill\\*
  6617. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6618. \multicolumn{1}{c}{Name} &
  6619. \multicolumn{1}{c}{Type} &
  6620. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6621. \multicolumn{1}{c}{Signed?} &
  6622. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6623. \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6624. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6625. array containing the contents of a plane of the reconstructed frame. \\
  6626. \bitvar{FX} & Integer & 20 & No & The horizontal pixel index of the
  6627. lower-left corner of the area to be filtered. \\
  6628. \bitvar{FY} & Integer & 20 & No & The vertical pixel index of the
  6629. lower-left corner of the area to be filtered. \\
  6630. \bitvar{L} & Integer & 7 & No & The loop filter limit value. \\
  6631. \bottomrule\end{tabularx}
  6632. \paragraph{Output parameters:}\hfill\\*
  6633. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6634. \multicolumn{1}{c}{Name} &
  6635. \multicolumn{1}{c}{Type} &
  6636. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6637. \multicolumn{1}{c}{Signed?} &
  6638. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6639. \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6640. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6641. array containing the contents of a plane of the reconstructed frame. \\
  6642. \bottomrule\end{tabularx}
  6643. \paragraph{Variables used:}\hfill\\*
  6644. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6645. \multicolumn{1}{c}{Name} &
  6646. \multicolumn{1}{c}{Type} &
  6647. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6648. \multicolumn{1}{c}{Signed?} &
  6649. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6650. \locvar{R} & Integer & 9 & Yes & The edge detector response. \\
  6651. \locvar{P} & Integer & 9 & Yes & A filtered pixel value. \\
  6652. \locvar{\idx{bx}} & Integer & 20 & No & The horizontal pixel index in the
  6653. block. \\
  6654. \bottomrule\end{tabularx}
  6655. \medskip
  6656. This procedure applies a $4$-tap vertical filter to each column of a horizontal
  6657. block edge.
  6658. \begin{enumerate}
  6659. \item
  6660. For each value of \locvar{\idx{bx}} from $0$ to $7$:
  6661. \begin{enumerate}
  6662. \item
  6663. Assign \locvar{R} the value
  6664. \begin{multline*}
  6665. (\bitvar{RECP}[\bitvar{FY}][\bitvar{FX}+\locvar{\idx{bx}}]-
  6666. 3*\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]+\\
  6667. 3*\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]-
  6668. \bitvar{RECP}[\bitvar{FY}+3][\bitvar{FX}+\locvar{\idx{bx}}]+4)>>3
  6669. \end{multline*}
  6670. \item
  6671. Assign \locvar{P} the value
  6672. $(\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]+
  6673. \lflim(\locvar{R},\bitvar{L}))$.
  6674. \item
  6675. If \locvar{P} is less than zero, assign
  6676. $\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]$ the value zero.
  6677. \item
  6678. Otherwise, if \locvar{P} is greater than $255$, assign
  6679. $\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]$ the value $255$.
  6680. \item
  6681. Otherwise, assign
  6682. $\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]$ the value
  6683. \locvar{P}.
  6684. \item
  6685. Assign \locvar{P} the value
  6686. $(\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]-
  6687. \lflim(\locvar{R},\bitvar{L}))$.
  6688. \item
  6689. If \locvar{P} is less than zero, assign
  6690. $\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]$ the value zero.
  6691. \item
  6692. Otherwise, if \locvar{P} is greater than $255$, assign
  6693. $\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]$ the value $255$.
  6694. \item
  6695. Otherwise, assign
  6696. $\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]$ the value
  6697. \locvar{P}.
  6698. \end{enumerate}
  6699. \end{enumerate}
  6700. \subsection{Complete Loop Filter}
  6701. \label{sub:loop-filt}
  6702. \paragraph{Input parameters:}\hfill\\*
  6703. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6704. \multicolumn{1}{c}{Name} &
  6705. \multicolumn{1}{c}{Type} &
  6706. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6707. \multicolumn{1}{c}{Signed?} &
  6708. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6709. \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
  6710. 7 & No & A 64-element array of loop filter limit
  6711. values. \\
  6712. \bitvar{RPYW} & Integer & 20 & No & The width of the $Y'$ plane of the
  6713. reconstruced frame in pixels. \\
  6714. \bitvar{RPYH} & Integer & 20 & No & The height of the $Y'$ plane of the
  6715. reconstruced frame in pixels. \\
  6716. \bitvar{RPCW} & Integer & 20 & No & The width of the $C_b$ and $C_r$
  6717. planes of the reconstruced frame in pixels. \\
  6718. \bitvar{RPCH} & Integer & 20 & No & The height of the $C_b$ and $C_r$
  6719. planes of the reconstruced frame in pixels. \\
  6720. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  6721. frame. \\
  6722. \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  6723. 1 & No & An \bitvar{NBS}-element array of
  6724. flags indicating which blocks are coded. \\
  6725. \bitvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
  6726. 6 & No & An \bitvar{NQIS}-element array of
  6727. \qi\ values. \\
  6728. \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6729. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6730. array containing the contents of the $Y'$ plane of the reconstructed frame. \\
  6731. \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6732. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6733. array containing the contents of the $C_b$ plane of the reconstructed frame. \\
  6734. \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6735. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6736. array containing the contents of the $C_r$ plane of the reconstructed frame. \\
  6737. \bottomrule\end{tabularx}
  6738. \paragraph{Output parameters:}\hfill\\*
  6739. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6740. \multicolumn{1}{c}{Name} &
  6741. \multicolumn{1}{c}{Type} &
  6742. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6743. \multicolumn{1}{c}{Signed?} &
  6744. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6745. \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6746. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6747. array containing the contents of the $Y'$ plane of the reconstructed frame. \\
  6748. \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6749. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6750. array containing the contents of the $C_b$ plane of the reconstructed frame. \\
  6751. \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6752. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6753. array containing the contents of the $C_r$ plane of the reconstructed frame. \\
  6754. \bottomrule\end{tabularx}
  6755. \paragraph{Variables used:}\hfill\\*
  6756. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6757. \multicolumn{1}{c}{Name} &
  6758. \multicolumn{1}{c}{Type} &
  6759. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6760. \multicolumn{1}{c}{Signed?} &
  6761. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6762. \locvar{RPW} & Integer & 20 & No & The width of the current plane of the
  6763. reconstructed frame in pixels. \\
  6764. \locvar{RPH} & Integer & 20 & No & The height of the current plane of
  6765. the reconstructed frame in pixels. \\
  6766. \locvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6767. 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
  6768. array containing the contents of the current plane of the reconstruced
  6769. frame. \\
  6770. \locvar{BX} & Integer & 20 & No & The horizontal pixel index of the
  6771. lower-left corner of the current block. \\
  6772. \locvar{BY} & Integer & 20 & No & The vertical pixel index of the
  6773. lower-left corner of the current block. \\
  6774. \locvar{FX} & Integer & 20 & No & The horizontal pixel index of the
  6775. lower-left corner of the area to be filtered. \\
  6776. \locvar{FY} & Integer & 20 & No & The vertical pixel index of the
  6777. lower-left corner of the area to be filtered. \\
  6778. \locvar{L} & Integer & 7 & No & The loop filter limit value. \\
  6779. \locvar{\bi} & Integer & 36 & No & The index of the current block in
  6780. coded order. \\
  6781. \locvar{\bj} & Integer & 36 & No & The index of a neighboring block in
  6782. coded order. \\
  6783. \locvar{\pli} & Integer & 2 & No & The color plane index of the current
  6784. block. \\
  6785. \bottomrule\end{tabularx}
  6786. \medskip
  6787. This procedure defines the order that the various block edges are filtered.
  6788. Because each application of one of the two filters above destructively modifies
  6789. the contents of the reconstructed image, the precise output obtained differs
  6790. depending on the order that horizontal and vertical filters are applied to the
  6791. edges of a single block.
  6792. The order defined here conforms to that used by VP3.
  6793. \begin{enumerate}
  6794. \item
  6795. Assign \locvar{L} the value $\bitvar{LFLIMS}[\bitvar{QIS}[0]]$.
  6796. \item
  6797. For each block in {\em raster} order, with coded-order index \locvar{\bi}:
  6798. \begin{enumerate}
  6799. \item
  6800. If $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero:
  6801. \begin{enumerate}
  6802. \item
  6803. Assign \locvar{\pli} the index of the color plane block \locvar{\bi} belongs
  6804. to.
  6805. \item
  6806. Assign \locvar{RECP}, \locvar{RPW}, and \locvar{RPH} the values given in
  6807. Table~\ref{tab:recp} corresponding to the value of \locvar{\pli}.
  6808. \begin{table}[htbp]
  6809. \begin{center}
  6810. \begin{tabular}{clll}\toprule
  6811. \locvar{\pli} & \locvar{RECP} & \locvar{RPW} & \locvar{RPH} \\\midrule
  6812. $0$ & \bitvar{RECY} & \bitvar{RPYW} & \bitvar{RPYH} \\
  6813. $1$ & \bitvar{RECCB} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6814. $2$ & \bitvar{RECCR} & \bitvar{RPCW} & \bitvar{RPCH} \\
  6815. \bottomrule\end{tabular}
  6816. \end{center}
  6817. \caption{Reconstructed Planes and Sizes for Each \locvar{\pli}}
  6818. \label{tab:recp}
  6819. \end{table}
  6820. \item
  6821. Assign \locvar{BX} the horizontal pixel index of the lower-left corner of the
  6822. block \locvar{\bi}.
  6823. \item
  6824. Assign \locvar{BY} the vertical pixel index of the lower-left corner of the
  6825. block \locvar{\bi}.
  6826. \item
  6827. If \locvar{BX} is greater than zero:
  6828. \begin{enumerate}
  6829. \item
  6830. Assign \locvar{FX} the value $(\locvar{BX}-2)$.
  6831. \item
  6832. Assign \locvar{FY} the value \locvar{BY}.
  6833. \item
  6834. Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
  6835. horizontal block filter to the left edge of block \locvar{\bi} with the
  6836. procedure described in Section~\ref{sub:filth}.
  6837. \end{enumerate}
  6838. \item
  6839. If \locvar{BY} is greater than zero:
  6840. \begin{enumerate}
  6841. \item
  6842. Assign \locvar{FX} the value \locvar{BX}.
  6843. \item
  6844. Assign \locvar{FY} the value $(\locvar{BY}-2)$
  6845. \item
  6846. Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
  6847. vertical block filter to the bottom edge of block \locvar{\bi} with the
  6848. procedure described in Section~\ref{sub:filtv}.
  6849. \end{enumerate}
  6850. \item
  6851. If $(\locvar{BX}+8)$ is less than \locvar{RPW} and
  6852. $\bitvar{BCODED}[\locvar{\bj}]$ is zero, where \locvar{\bj} is the coded-order
  6853. index of the block adjacent to \locvar{\bi} on the right:
  6854. \begin{enumerate}
  6855. \item
  6856. Assign \locvar{FX} the value $(\locvar{BX}+6)$.
  6857. \item
  6858. Assign \locvar{FY} the value \locvar{BY}.
  6859. \item
  6860. Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
  6861. horizontal block filter to the right edge of block \locvar{\bi} with the
  6862. procedure described in Section~\ref{sub:filth}.
  6863. \end{enumerate}
  6864. \item
  6865. If $(\locvar{BY}+8)$ is less than \locvar{RPH} and
  6866. $\bitvar{BCODED}[\locvar{\bj}]$ is zero, where \locvar{\bj} is the coded-order
  6867. index of the block adjacent to \locvar{\bi} above:
  6868. \begin{enumerate}
  6869. \item
  6870. Assign \locvar{FX} the value \locvar{BX}.
  6871. \item
  6872. Assign \locvar{FY} the value $(\locvar{BY}+6)$
  6873. \item
  6874. Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
  6875. vertical block filter to the top edge of block \locvar{\bi} with the
  6876. procedure described in Section~\ref{sub:filtv}.
  6877. \end{enumerate}
  6878. \end{enumerate}
  6879. \end{enumerate}
  6880. \end{enumerate}
  6881. \paragraph{VP3 Compatibility}
  6882. The original VP3 decoder implemented unrestricted motion vectors by enlarging
  6883. the reconstructed frame buffers and repeating the pixels on its edges into the
  6884. padding region.
  6885. However, for the previous reference frame this padding ocurred before the loop
  6886. filter was applied, but for the golden reference frame it occurred afterwards.
  6887. This means that for the previous reference frame, the padding values were
  6888. required to be stored separately from the main image values.
  6889. Furthermore, even if the previous and golden reference frames were in fact the
  6890. same frame, they could have different padding values.
  6891. Finally, the encoder did not apply the loop filter at all, which resulted in
  6892. artifacts, particularly in near-static scenes, due to prediction-loop
  6893. mismatch.
  6894. This last can only be considered a bug in the VP3 encoder.
  6895. Given all these things, Theora now uniformly applies the loop filter before
  6896. the reference frames are padded.
  6897. This means it is possible to use the same buffer for the previous and golden
  6898. reference frames when they do indeed refer to the same frame.
  6899. It also means that on architectures where memory bandwidth is limited, it is
  6900. possible to avoid storing padding values, and simply clamp the motion vectors
  6901. applied to each pixel as described in Sections~\ref{sub:predfullpel}
  6902. and~\ref{sub:predhalfpel}.
  6903. This means that the predicted pixel values along the edges of the frame might
  6904. differ slightly between VP3 and Theora, but since the VP3 encoder did not
  6905. apply the loop filter in the first place, this is not likely to impose any
  6906. serious compatibility issues.
  6907. \section{Complete Frame Decode}
  6908. \paragraph{Input parameters:}\hfill\\*
  6909. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6910. \multicolumn{1}{c}{Name} &
  6911. \multicolumn{1}{c}{Type} &
  6912. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6913. \multicolumn{1}{c}{Signed?} &
  6914. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6915. \bitvar{FMBW} & Integer & 16 & No & The width of the frame in macro
  6916. blocks. \\
  6917. \bitvar{FMBH} & Integer & 16 & No & The height of the frame in macro
  6918. blocks. \\
  6919. \bitvar{NSBS} & Integer & 32 & No & The total number of super blocks in a
  6920. frame. \\
  6921. \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
  6922. frame. \\
  6923. \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
  6924. frame. \\
  6925. \bitvar{FRN} & Integer & 32 & No & The frame-rate numerator. \\
  6926. \bitvar{FRD} & Integer & 32 & No & The frame-rate denominator. \\
  6927. \bitvar{PARN} & Integer & 24 & No & The pixel aspect-ratio numerator. \\
  6928. \bitvar{PARD} & Integer & 24 & No & The pixel aspect-ratio
  6929. denominator. \\
  6930. \bitvar{CS} & Integer & 8 & No & The color space. \\
  6931. \bitvar{PF} & Integer & 2 & No & The pixel format. \\
  6932. \bitvar{NOMBR} & Integer & 24 & No & The nominal bitrate of the stream, in
  6933. bits per second. \\
  6934. \bitvar{QUAL} & Integer & 6 & No & The quality hint. \\
  6935. \bitvar{KFGSHIFT} & Integer & 5 & No & The amount to shift the key frame
  6936. number by in the granule position. \\
  6937. \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
  6938. 7 & No & A 64-element array of loop filter
  6939. limit values. \\
  6940. \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  6941. 16 & No & A 64-element array of scale values
  6942. for AC coefficients for each \qi\ value. \\
  6943. \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
  6944. 16 & No & A 64-element array of scale values
  6945. for the DC coefficient for each \qi\ value. \\
  6946. \bitvar{NBMS} & Integer & 10 & No & The number of base matrices. \\
  6947. \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  6948. 8 & No & A $\bitvar{NBMS}\times 64$ array
  6949. containing the base matrices. \\
  6950. \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
  6951. 6 & No & A $2\times 3$ array containing the
  6952. number of quant ranges for a given \qti\ and \pli, respectively.
  6953. This is at most $63$. \\
  6954. \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  6955. 6 & No & A $2\times 3\times 63$ array of the
  6956. sizes of each quant range for a given \qti\ and \pli, respectively.
  6957. Only the first $\bitvar{NQRS}[\qti][\pli]$ values will be used. \\
  6958. \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
  6959. 9 & No & A $2\times 3\times 64$ array of the
  6960. \bmi's used for each quant range for a given \qti\ and \pli, respectively.
  6961. Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values will be used. \\
  6962. \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
  6963. & An 80-element array of Huffman tables
  6964. with up to 32 entries each. \\
  6965. \bitvar{GOLDREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6966. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6967. array containing the contents of the $Y'$ plane of the golden reference
  6968. frame. \\
  6969. \bitvar{GOLDREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6970. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6971. array containing the contents of the $C_b$ plane of the golden reference
  6972. frame. \\
  6973. \bitvar{GOLDREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6974. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6975. array containing the contents of the $C_r$ plane of the golden reference
  6976. frame. \\
  6977. \bitvar{PREVREFY} & \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 previous reference
  6980. frame. \\
  6981. \bitvar{PREVREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6982. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6983. array containing the contents of the $C_b$ plane of the previous reference
  6984. frame. \\
  6985. \bitvar{PREVREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6986. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  6987. array containing the contents of the $C_r$ plane of the previous reference
  6988. frame. \\
  6989. \bottomrule\end{tabularx}
  6990. \paragraph{Output parameters:}\hfill\\*
  6991. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  6992. \multicolumn{1}{c}{Name} &
  6993. \multicolumn{1}{c}{Type} &
  6994. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  6995. \multicolumn{1}{c}{Signed?} &
  6996. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  6997. \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  6998. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  6999. array containing the contents of the $Y'$ plane of the reconstructed frame. \\
  7000. \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7001. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  7002. array containing the contents of the $C_b$ plane of the reconstructed
  7003. frame. \\
  7004. \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7005. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  7006. array containing the contents of the $C_r$ plane of the reconstructed
  7007. frame. \\
  7008. \bitvar{GOLDREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7009. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  7010. array containing the contents of the $Y'$ plane of the golden reference
  7011. frame. \\
  7012. \bitvar{GOLDREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7013. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  7014. array containing the contents of the $C_b$ plane of the golden reference
  7015. frame. \\
  7016. \bitvar{GOLDREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7017. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  7018. array containing the contents of the $C_r$ plane of the golden reference
  7019. frame. \\
  7020. \bitvar{PREVREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7021. 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
  7022. array containing the contents of the $Y'$ plane of the previous reference
  7023. frame. \\
  7024. \bitvar{PREVREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7025. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  7026. array containing the contents of the $C_b$ plane of the previous reference
  7027. frame. \\
  7028. \bitvar{PREVREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7029. 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
  7030. array containing the contents of the $C_r$ plane of the previous reference
  7031. frame. \\
  7032. \bottomrule\end{tabularx}
  7033. \paragraph{Variables used:}\hfill\\*
  7034. \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
  7035. \multicolumn{1}{c}{Name} &
  7036. \multicolumn{1}{c}{Type} &
  7037. \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
  7038. \multicolumn{1}{c}{Signed?} &
  7039. \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
  7040. \locvar{FTYPE} & Integer & 1 & No & The frame type. \\
  7041. \locvar{NQIS} & Integer & 2 & No & The number of \qi\ values. \\
  7042. \locvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
  7043. 6 & No & An \locvar{NQIS}-element array of
  7044. \qi\ values. \\
  7045. \locvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
  7046. 1 & No & An \bitvar{NBS}-element array of flags
  7047. indicating which blocks are coded. \\
  7048. \locvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
  7049. 3 & No & An \bitvar{NMBS}-element array of
  7050. coding modes for each macro block. \\
  7051. \locvar{MVECTS} & \multicolumn{1}{p{50pt}}{Array of 2D Integer Vectors} &
  7052. 6 & Yes & An \bitvar{NBS}-element array of motion
  7053. vectors for each block. \\
  7054. \locvar{QIIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  7055. 2 & No & An \bitvar{NBS}-element array of
  7056. \locvar{\qii} values for each block. \\
  7057. \locvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
  7058. 16 & Yes & An $\bitvar{NBS}\times 64$ array of
  7059. quantized DCT coefficient values for each block in zig-zag order. \\
  7060. \locvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
  7061. 7 & No & An \bitvar{NBS}-element array of the
  7062. coefficient count for each block. \\
  7063. \bitvar{RPYW} & Integer & 20 & No & The width of the $Y'$ plane of the
  7064. reference frames in pixels. \\
  7065. \bitvar{RPYH} & Integer & 20 & No & The height of the $Y'$ plane of the
  7066. reference frames in pixels. \\
  7067. \bitvar{RPCW} & Integer & 20 & No & The width of the $C_b$ and $C_r$
  7068. planes of the reference frames in pixels. \\
  7069. \bitvar{RPCH} & Integer & 20 & No & The height of the $C_b$ and $C_r$
  7070. planes of the reference frames in pixels. \\
  7071. \locvar{\bi} & Integer & 36 & No & The index of the current block in coded
  7072. order. \\
  7073. \bottomrule\end{tabularx}
  7074. \medskip
  7075. This procedure uses all the procedures defined in the previous section of this
  7076. chapter to decode and reconstruct a complete frame.
  7077. It takes as input values decoded from the headers, as well as the current
  7078. reference frames.
  7079. As output, it gives the uncropped, reconstructed frame.
  7080. This should be cropped to picture region before display.
  7081. As a special case, a 0-byte packet is treated exactly like an inter frame with
  7082. no coded blocks.
  7083. \begin{enumerate}
  7084. \item
  7085. If the size of the data packet is non-zero:
  7086. \begin{enumerate}
  7087. \item
  7088. Decode the frame header values \locvar{FTYPE}, \locvar{NQIS}, and \locvar{QIS}
  7089. using the procedure given in Section~\ref{sub:frame-header}.
  7090. \item
  7091. Using \locvar{FTYPE}, \bitvar{NSBS}, and \bitvar{NBS}, decode the list of coded
  7092. block flags into \locvar{BCODED} using the procedure given in
  7093. Section~\ref{sub:coded-blocks}.
  7094. \item
  7095. Using \locvar{FTYPE}, \bitvar{NMBS}, \bitvar{NBS}, and \bitvar{BCODED}, decode
  7096. the macro block coding modes into \locvar{MBMODES} using the procedure given
  7097. in Section~\ref{sub:mb-modes}.
  7098. \item
  7099. If \locvar{FTYPE} is non-zero (inter frame), using \bitvar{PF}, \bitvar{NMBS},
  7100. \locvar{MBMODES}, \bitvar{NBS}, and \locvar{BCODED}, decode the motion vectors
  7101. into \locvar{MVECTS} using the procedure given in
  7102. Section~\ref{sub:mb-mv-decode}.
  7103. \item
  7104. Using \bitvar{NBS}, \locvar{BCODED}, and \locvar{NQIS}, decode the block-level
  7105. \qi\ values into \locvar{QIIS} using the procedure given in
  7106. Section~\ref{sub:block-qis}.
  7107. \item
  7108. Using \bitvar{NBS}, \bitvar{NMBS}, \locvar{BCODED}, and \bitvar{HTS}, decode
  7109. the DCT coefficients into \locvar{NCOEFFS} and \locvar{NCOEFFS} using the
  7110. procedure given in Section~\ref{sub:dct-coeffs}.
  7111. \item
  7112. Using \locvar{BCODED} and \locvar{MBMODES}, undo the DC prediction on the DC
  7113. coefficients stored in \locvar{COEFFS} using the procedure given in
  7114. Section~\ref{sub:dc-pred-undo}.
  7115. \end{enumerate}
  7116. \item
  7117. Otherwise:
  7118. \begin{enumerate}
  7119. \item
  7120. Assign \locvar{FTYPE} the value 1 (inter frame).
  7121. \item
  7122. Assign \locvar{NQIS} the value 1.
  7123. \item
  7124. Assign $\locvar{QIS}[0]$ the value 63.
  7125. \item
  7126. For each value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$, assign
  7127. $\locvar{BCODED}[\locvar{\bi}]$ the value zero.
  7128. \end{enumerate}
  7129. \item
  7130. Assign \locvar{RPYW} and \locvar{RPYH} the values $(16*\bitvar{FMBW})$ and
  7131. $(16*\bitvar{FMBH})$, respectively.
  7132. \item
  7133. Assign \locvar{RPCW} and \locvar{RPCH} the values from the row of
  7134. Table~\ref{tab:rpcwh-for-pf} corresponding to \bitvar{PF}.
  7135. \begin{table}[tb]
  7136. \begin{center}
  7137. \begin{tabular}{crr}\toprule
  7138. \bitvar{PF} & \multicolumn{1}{c}{\locvar{RPCW}}
  7139. & \multicolumn{1}{c}{\locvar{RPCH}} \\\midrule
  7140. $0$ & $8*\bitvar{FMBW}$ & $8*\bitvar{FMBH}$ \\
  7141. $2$ & $8*\bitvar{FMBW}$ & $16*\bitvar{FMBH}$ \\
  7142. $3$ & $16*\bitvar{FMBW}$ & $16*\bitvar{FMBH}$ \\
  7143. \bottomrule\end{tabular}
  7144. \end{center}
  7145. \caption{Width and Height of Chroma Planes for each Pixel Format}
  7146. \label{tab:rpcwh-for-pf}
  7147. \end{table}
  7148. \item
  7149. Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS},
  7150. \bitvar{QRSIZES}, \bitvar{QRBMIS}, \bitvar{NBS}, \locvar{BCODED},
  7151. \locvar{MBMODES}, \locvar{MVECTS}, \locvar{COEFFS}, \locvar{NCOEFFS},
  7152. \locvar{QIS}, \locvar{QIIS}, \locvar{RPYW}, \locvar{RPYH}, \locvar{RPCW},
  7153. \locvar{RPCH}, \bitvar{GOLDREFY}, \bitvar{GOLDREFCB}, \bitvar{GOLDREFCR},
  7154. \bitvar{PREVREFY}, \bitvar{PREVREFCB}, and \bitvar{PREVREFCR}, reconstruct the
  7155. complete frame into \bitvar{RECY}, \bitvar{RECCB}, and \bitvar{RECCR} using
  7156. the procedure given in Section~\ref{sub:recon}.
  7157. \item
  7158. Using \bitvar{LFLIMS}, \locvar{RPYW}, \locvar{RPYH}, \locvar{RPCW},
  7159. \locvar{RPCH}, \bitvar{NBS}, \locvar{BCODED}, and \locvar{QIS}, apply the loop
  7160. filter to the reconstructed frame in \bitvar{RECY}, \bitvar{RECCB}, and
  7161. \bitvar{RECCR} using the procedure given in Section~\ref{sub:loop-filt}.
  7162. \item
  7163. If \locvar{FTYPE} is zero (intra frame), assign \bitvar{GOLDREFY},
  7164. \bitvar{GOLDREFCB}, and \bitvar{GOLDREFCR} the values \bitvar{RECY},
  7165. \bitvar{RECCB}, and \bitvar{RECCR}, respectively.
  7166. \item
  7167. Assign \bitvar{PREVREFY}, \bitvar{PREVREFCB}, and \bitvar{PREVREFCR} the values
  7168. \bitvar{RECY}, \bitvar{RECCB}, and \bitvar{RECCR}, respectively.
  7169. \end{enumerate}
  7170. %\backmatter
  7171. \appendix
  7172. \chapter{Ogg Bitstream Encapsulation}
  7173. \label{app:oggencapsulation}
  7174. \section{Overview}
  7175. This document specifies the embedding or encapsulation of Theora packets
  7176. in an Ogg transport stream.
  7177. Ogg is a stream oriented wrapper for coded, linear time-based data.
  7178. It provides syncronization, multiplexing, framing, error detection and
  7179. seeking landmarks for the decoder and complements the raw packet format
  7180. used by the Theora codec.
  7181. This document assumes familiarity with the details of the Ogg standard.
  7182. The Xiph.Org documentation provides an overview of the Ogg transport stream
  7183. format at \url{http://www.xiph.org/ogg/doc/oggstream.html} and a detailed
  7184. description at \url{http://www.xiph.org/ogg/doc/framing.html}.
  7185. The format is also defined in RFC~3533 \cite{rfc3533}.
  7186. While Theora packets can be embedded in a wide variety of media
  7187. containers and streaming mechanisms, the Xiph.Org Foundation
  7188. recommends Ogg as the native format for Theora video in file-oriented
  7189. storage and transmission contexts.
  7190. \subsection{MIME type}
  7191. The generic MIME type of any Ogg file is {\tt application/ogg}.
  7192. The specific MIME type for the Ogg Theora profile documented here
  7193. is {\tt video/ogg}. This is the MIME type recommended for files
  7194. conforming to this appendix. The recommended filename extension
  7195. is {\tt .ogv}.
  7196. Outside of an encapsulation, the mime type {\tt video/theora} may
  7197. be used to refer specifically to the Theora compressed video stream.
  7198. \section{Embedding in a logical bitstream}
  7199. Ogg separates the concept of a {\em logical bitstream} consisting of the
  7200. framing of a particular sequence of packets and complete within itself
  7201. from the {\em physical bitstream} which may consist either of a single
  7202. logical bitstream or a number of logical bitstreams multiplexed
  7203. together.
  7204. This section specifies the embedding of Theora packets in a logical Ogg
  7205. bitstream.
  7206. The mapping of Ogg Theora logical bitstreams into a multiplexed physical Ogg
  7207. stream is described in the next section.
  7208. \subsection{Headers}
  7209. The initial identification header packet appears by itself in a
  7210. single Ogg page.
  7211. This page defines the start of the logical stream and MUST have
  7212. the `beginning of stream' flag set.
  7213. The second and third header packets (comment metadata and decoder
  7214. setup data) can together span one or more Ogg pages.
  7215. If there are additional non-normative header packets, they MUST be
  7216. included in this sequence of pages as well.
  7217. The comment header packet MUST begin the second Ogg page in the logical
  7218. bitstream, and there MUST be a page break between the last header
  7219. packet and the first frame data packet.
  7220. These two page break requirements facilitate stream identification and
  7221. simplify header acquisition for seeking and live streaming applications.
  7222. All header pages MUST have their granule position field set to zero.
  7223. \subsection{Frame data}
  7224. The first frame data packet in a logical bitstream MUST begin a new Ogg
  7225. page.
  7226. All other data packets are placed one at a time into Ogg pages
  7227. until the end of the stream.
  7228. Packets can span pages and multiple packets can be placed within any
  7229. one page.
  7230. The last page in the logical bitstream SHOULD have its
  7231. 'end of stream' flag set to indicate complete transmission
  7232. of the available video.
  7233. Frame data pages MUST be marked with a granule position corresponding to
  7234. the end of the display interval of the last frame/packet that finishes
  7235. in that page. See the next section for details.
  7236. \subsection{Granule position}
  7237. Data packets are marked by a granulepos derived from the count of decodable
  7238. frames after that packet is processed. The field itself is divided into two
  7239. sections, the width of the less significant section being given by the KFGSHIFT
  7240. parameter decoded from the identification header
  7241. (Section~\ref{sec:idheader}).
  7242. The more significant portion of the field gives the count of coded
  7243. frames after the coding of the last keyframe in stream, and the less
  7244. significant portion gives the count of frames since the last keyframe.
  7245. Thus a stream would begin with a split granulepos of $1|0$ (a keyframe),
  7246. followed by $1|1$, $1|2$, $1|3$, etc. Around a keyframe in the
  7247. middle of the stream the granulepos sequence might be $1234|35$,
  7248. $1234|36$, $1234|37$, $1271|0$ (for the keyframe), $1271|1$, and so
  7249. on. In this way the granulepos field increased monotonically as required
  7250. by the Ogg format, but contains information necessary to efficiently
  7251. find the previous keyframe to continue decoding after a seek.
  7252. Prior to bitstream version 3.2.1, data packets were marked by a
  7253. granulepos derived from the index of the frame being decoded,
  7254. rather than the count. That is they marked the beginning of the
  7255. display interval of a frame rather than the end. Such streams
  7256. have the VREV field of the identification header set to `0'
  7257. instead of `1'. They can be interpreted according to the description
  7258. above by adding 1 to the more signification field of the split
  7259. granulepos when VREV is less than 1.
  7260. \section{Multiplexed stream mapping}
  7261. Applications supporting Ogg Theora must support Theora bitstreams
  7262. multiplexed with compressed audio data in the Vorbis I and Speex
  7263. formats, and should support Ogg-encapsulated MNG graphics for overlays.
  7264. Multiple audio and video bitstreams may be multiplexed together.
  7265. How playback of multiple/alternate streams is handled is up to the
  7266. application.
  7267. Some conventions based on included metadata aide interoperability
  7268. in this respect.
  7269. %TODO: describe multiple vs. alternate streams, language mapping
  7270. % and reference metadata descriptions.
  7271. \subsection{Chained streams}
  7272. Ogg Theora decoders and playback applications MUST support both grouped
  7273. streams (multiplexed concurrent logical streams) and chained streams
  7274. (sequential concatenation of independent physical bitstreams).
  7275. The number and codec data types of multiplexed streams and the decoder
  7276. parameters for those stream types that re-occur can all change at a
  7277. chaining boundary.
  7278. A playback application MUST be prepared to handle such changes and
  7279. SHOULD do so smoothly with the minimum possible visible disruption.
  7280. The specification of grouped streams below applies independently to each
  7281. segment of a chained bitstream.
  7282. \subsection{Grouped streams}
  7283. At the beginning of a multiplexed stream, the `beginning of stream'
  7284. pages for each logical bitstream will be grouped together.
  7285. Within these, the first page to occur MUST be the Theora page.
  7286. This facilitates identification of Ogg Theora files among other
  7287. Ogg-encapsulated content.
  7288. A playback application must nevertheless handle streams where this
  7289. arrangement is not correct.
  7290. %TBT: Then what's the point of requiring it in the spec?
  7291. If there is more than one Theora logical stream, the first page should
  7292. be from the primary stream.
  7293. That is, the best choice for the stream a generic player should begin
  7294. displaying without special user direction.
  7295. If there is more than one audio stream, or of any other stream
  7296. type, the identification page of the primary stream of that type
  7297. should be placed before the others.
  7298. %TBT: That's all pretty vague.
  7299. After the `beginning of stream' pages, the header pages of each of
  7300. the logical streams MUST be grouped together before any data pages
  7301. occur.
  7302. After all the header pages have been placed,
  7303. the data pages are multiplexed together.
  7304. They should be placed in the stream in increasing order by the
  7305. time equivalents of their granule position fields.
  7306. This facilitates seeking while limiting the buffering requirements of the
  7307. playback demultiplexer.
  7308. %TODO: A lot of this language is encoder-oriented.
  7309. %TODO: We define a decoder-oriented specification.
  7310. %TODO: The language should be changed to match.
  7311. \cleardoublepage
  7312. \chapter{VP3}
  7313. \section{VP3 Compatibility}
  7314. \label{app:vp3-compat}
  7315. This section lists all of the encoder and decoder issues that may affect VP3
  7316. compatibly.
  7317. Each is described in more detail in the text itself.
  7318. This list is provided merely for reference.
  7319. \begin{itemize}
  7320. \item
  7321. Bitstream headers (Section~\ref{sec:headers}).
  7322. \begin{itemize}
  7323. \item
  7324. Identification header (Section~\ref{sec:idheader}).
  7325. \begin{itemize}
  7326. \item
  7327. Non-multiple of 16 picture sizes.
  7328. \item
  7329. Standardized color spaces.
  7330. \item
  7331. Support for $4:4:4$ and $4:2:2$ pixel formats.
  7332. \end{itemize}
  7333. \item
  7334. Setup header
  7335. \begin{itemize}
  7336. \item
  7337. Loop filter limit values (Section~\ref{sub:loop-filter-limits}).
  7338. \item
  7339. Quantization parameters (Section~\ref{sub:quant-params}).
  7340. \item
  7341. Huffman tables (Section~\ref{sub:huffman-tables}).
  7342. \end{itemize}
  7343. \end{itemize}
  7344. \item
  7345. Frame header format (Section~\ref{sub:frame-header}).
  7346. \item
  7347. Extended long-run bit strings (Section~\ref{sub:long-run}).
  7348. \item
  7349. INTER\_MV\_FOUR handling of uncoded blocks (Section~\ref{sub:mb-mv-decode}).
  7350. \item
  7351. Block-level \qi\ values (Section~\ref{sub:block-qis}).
  7352. \item
  7353. Zero-length EOB runs (Section~\ref{sub:eob-token}).
  7354. \item
  7355. Unrestricted motion vector padding and the loop filter
  7356. (Section~\ref{sub:loop-filt}).
  7357. \end{itemize}
  7358. \section{Loop Filter Limit Values}
  7359. \label{app:vp3-loop-filter-limits}
  7360. The hard-coded loop filter limit values used in VP3 are defined as follows:
  7361. \begin{align*}
  7362. \bitvar{LFLIMS} = & \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7363. \{ & 30, & 25, & 20, & 20, & 15, & 15, & 14, & 14, & \\
  7364. & 13, & 13, & 12, & 12, & 11, & 11, & 10, & 10, & \\
  7365. & 9, & 9, & 8, & 8, & 7, & 7, & 7, & 7, & \\
  7366. & 6, & 6, & 6, & 6, & 5, & 5, & 5, & 5, & \\
  7367. & 4, & 4, & 4, & 4, & 3, & 3, & 3, & 3, & \\
  7368. & 2, & 2, & 2, & 2, & 2, & 2, & 2, & 2, & \\
  7369. & 0, & 0, & 0, & 0, & 0, & 0, & 0, & 0, & \\
  7370. & 0, & 0, & 0, & 0, & 0, & 0, & 0, & 0\;\ & \!\} \\
  7371. \end{array}
  7372. \end{align*}
  7373. \section{Quantization Parameters}
  7374. \label{app:vp3-quant-params}
  7375. The hard-coded quantization parameters used by VP3 are defined as follows:
  7376. \begin{align*}
  7377. \bitvar{ACSCALE} = & \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7378. \{ & 500, & 450, & 400, & 370, & 340, & 310, & 285, & 265, & \\
  7379. & 245, & 225, & 210, & 195, & 185, & 180, & 170, & 160, & \\
  7380. & 150, & 145, & 135, & 130, & 125, & 115, & 110, & 107, & \\
  7381. & 100, & 96, & 93, & 89, & 85, & 82, & 75, & 74, & \\
  7382. & 70, & 68, & 64, & 60, & 57, & 56, & 52, & 50, & \\
  7383. & 49, & 45, & 44, & 43, & 40, & 38, & 37, & 35, & \\
  7384. & 33, & 32, & 30, & 29, & 28, & 25, & 24, & 22, & \\
  7385. & 21, & 19, & 18, & 17, & 15, & 13, & 12, & 10\;\ & \!\} \\
  7386. \end{array} \\
  7387. \bitvar{DCSCALE} = & \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7388. \{ & 220, & 200, & 190, & 180, & 170, & 170, & 160, & 160, & \\
  7389. & 150, & 150, & 140, & 140, & 130, & 130, & 120, & 120, & \\
  7390. & 110, & 110, & 100, & 100, & 90, & 90, & 90, & 80, & \\
  7391. & 80, & 80, & 70, & 70, & 70, & 60, & 60, & 60, & \\
  7392. & 60, & 50, & 50, & 50, & 50, & 40, & 40, & 40, & \\
  7393. & 40, & 40, & 30, & 30, & 30, & 30, & 30, & 30, & \\
  7394. & 30, & 20, & 20, & 20, & 20, & 20, & 20, & 20, & \\
  7395. & 20, & 10, & 10, & 10, & 10, & 10, & 10, & 10\;\ & \!\} \\
  7396. \end{array}
  7397. \end{align*}
  7398. VP3 defines only a single quantization range for each quantization type and
  7399. color plane, and the base matrix used is constant throughout the range.
  7400. There are three base matrices defined.
  7401. The first is used for the $Y'$ channel of INTRA mode blocks, and the second for
  7402. both the $C_b$ and $C_r$ channels of INTRA mode blocks.
  7403. The last is used for INTER mode blocks of all channels.
  7404. \begin{align*}
  7405. \bitvar{BMS} = \{ & \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7406. \{ & 16, & 11, & 10, & 16, & 24, & 40, & 51, & 61, & \\
  7407. & 12, & 12, & 14, & 19, & 26, & 58, & 60, & 55, & \\
  7408. & 14, & 13, & 16, & 24, & 40, & 57, & 69, & 56, & \\
  7409. & 14, & 17, & 22, & 29, & 51, & 87, & 80, & 62, & \\
  7410. & 18, & 22, & 37, & 58, & 68, & 109, & 103, & 77, & \\
  7411. & 24, & 35, & 55, & 64, & 81, & 104, & 113, & 92, & \\
  7412. & 49, & 64, & 78, & 87, & 103, & 121, & 120, & 101, & \\
  7413. & 72, & 92, & 95, & 98, & 112, & 100, & 103, & 99\;\ & \!\}, \\
  7414. %\end{array} \\
  7415. %& \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7416. \{ & 17, & 18, & 24, & 47, & 99, & 99, & 99, & 99, & \\
  7417. & 18, & 21, & 26, & 66, & 99, & 99, & 99, & 99, & \\
  7418. & 24, & 26, & 56, & 99, & 99, & 99, & 99, & 99, & \\
  7419. & 47, & 66, & 99, & 99, & 99, & 99, & 99, & 99, & \\
  7420. & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99, & \\
  7421. & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99, & \\
  7422. & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99, & \\
  7423. & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99\;\ & \!\}, \\
  7424. %\end{array} \\
  7425. %& \begin{array}[t]{r@{}rrrrrrrr@{}l}
  7426. \{ & 16, & 16, & 16, & 20, & 24, & 28, & 32, & 40, & \\
  7427. & 16, & 16, & 20, & 24, & 28, & 32, & 40, & 48, & \\
  7428. & 16, & 20, & 24, & 28, & 32, & 40, & 48, & 64, & \\
  7429. & 20, & 24, & 28, & 32, & 40, & 48, & 64, & 64, & \\
  7430. & 24, & 28, & 32, & 40, & 48, & 64, & 64, & 64, & \\
  7431. & 28, & 32, & 40, & 48, & 64, & 64, & 64, & 96, & \\
  7432. & 32, & 40, & 48, & 64, & 64, & 64, & 96, & 128, & \\
  7433. & 40, & 48, & 64, & 64, & 64, & 96, & 128, & 128\;\ & \!\}\;\;\} \\
  7434. \end{array}
  7435. \end{align*}
  7436. The remaining parameters simply assign these matrices to the proper quant
  7437. ranges.
  7438. \begin{align*}
  7439. \bitvar{NQRS} = & \{ \{1, 1, 1\}, \{1, 1, 1\} \} \\
  7440. \bitvar{QRSIZES} = &
  7441. \{ \{ \{63\}, \{63\}, \{63\} \}, \{ \{63\}, \{63\}, \{63\} \} \} \\
  7442. \bitvar{QRBMIS} = &
  7443. \{ \{ \{0, 0\}, \{1, 1\}, \{1, 1\} \}, \{ \{2, 2\}, \{2, 2\}, \{2, 2\} \} \} \\
  7444. \end{align*}
  7445. \section{Huffman Tables}
  7446. \label{app:vp3-huffman-tables}
  7447. The following tables contain the hard-coded Huffman codes used by VP3.
  7448. There are 80 tables in all, each with a Huffman code for all 32 token values.
  7449. The tokens are sorted by the most significant bits of their Huffman code.
  7450. This is the same order in which they will be decoded from the setup header.
  7451. \include{vp3huff}
  7452. \cleardoublepage
  7453. \chapter{Colophon}
  7454. Ogg is a \href{http://www.xiph.org}{Xiph.Org Foundation} effort to protect
  7455. essential tenets of Internet multimedia from corporate hostage-taking; Open
  7456. Source is the net's greatest tool to keep everyone honest.
  7457. See \href{http://www.xiph.org/about.html}{About the Xiph.Org Foundation} for
  7458. details.
  7459. Ogg Theora is the first Ogg video codec.
  7460. Anyone may freely use and distribute the Ogg and Theora specifications, whether
  7461. in private, public, or corporate capacity.
  7462. However, the Xiph.Org Foundation and the Ogg project reserve the right to set
  7463. the Ogg Theora specification and certify specification compliance.
  7464. Xiph.Org's Theora software codec implementation is distributed under a BSD-like
  7465. license.
  7466. This does not restrict third parties from distributing independent
  7467. implementations of Theora software under other licenses.
  7468. \begin{wrapfigure}{l}{0pt}
  7469. \includegraphics[width=2.5cm]{xifish}
  7470. \end{wrapfigure}
  7471. These pages are Copyright \textcopyright{} 2004-2007 Xiph.Org Foundation.
  7472. All rights reserved.
  7473. Ogg, Theora, Vorbis, Xiph.Org Foundation and their logos are trademarks
  7474. (\texttrademark) of the \href{http://www.xiph.org}{Xiph.Org Foundation}.
  7475. This document is set in \LaTeX.
  7476. \cleardoublepage
  7477. \bibliography{spec}
  7478. \end{document}