decode.c 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ogg/ogg.h>
  18. #include "decint.h"
  19. #if defined(OC_DUMP_IMAGES)
  20. # include <stdio.h>
  21. # include "png.h"
  22. #endif
  23. #if defined(HAVE_CAIRO)
  24. # include <cairo.h>
  25. #endif
  26. /*No post-processing.*/
  27. #define OC_PP_LEVEL_DISABLED (0)
  28. /*Keep track of DC qi for each block only.*/
  29. #define OC_PP_LEVEL_TRACKDCQI (1)
  30. /*Deblock the luma plane.*/
  31. #define OC_PP_LEVEL_DEBLOCKY (2)
  32. /*Dering the luma plane.*/
  33. #define OC_PP_LEVEL_DERINGY (3)
  34. /*Stronger luma plane deringing.*/
  35. #define OC_PP_LEVEL_SDERINGY (4)
  36. /*Deblock the chroma planes.*/
  37. #define OC_PP_LEVEL_DEBLOCKC (5)
  38. /*Dering the chroma planes.*/
  39. #define OC_PP_LEVEL_DERINGC (6)
  40. /*Stronger chroma plane deringing.*/
  41. #define OC_PP_LEVEL_SDERINGC (7)
  42. /*Maximum valid post-processing level.*/
  43. #define OC_PP_LEVEL_MAX (7)
  44. /*The mode alphabets for the various mode coding schemes.
  45. Scheme 0 uses a custom alphabet, which is not stored in this table.*/
  46. static const unsigned char OC_MODE_ALPHABETS[7][OC_NMODES]={
  47. /*Last MV dominates */
  48. {
  49. OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV_LAST2,OC_MODE_INTER_MV,
  50. OC_MODE_INTER_NOMV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
  51. OC_MODE_INTER_MV_FOUR
  52. },
  53. {
  54. OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV_LAST2,OC_MODE_INTER_NOMV,
  55. OC_MODE_INTER_MV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
  56. OC_MODE_INTER_MV_FOUR
  57. },
  58. {
  59. OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV,OC_MODE_INTER_MV_LAST2,
  60. OC_MODE_INTER_NOMV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
  61. OC_MODE_INTER_MV_FOUR
  62. },
  63. {
  64. OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV,OC_MODE_INTER_NOMV,
  65. OC_MODE_INTER_MV_LAST2,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,
  66. OC_MODE_GOLDEN_MV,OC_MODE_INTER_MV_FOUR
  67. },
  68. /*No MV dominates.*/
  69. {
  70. OC_MODE_INTER_NOMV,OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV_LAST2,
  71. OC_MODE_INTER_MV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
  72. OC_MODE_INTER_MV_FOUR
  73. },
  74. {
  75. OC_MODE_INTER_NOMV,OC_MODE_GOLDEN_NOMV,OC_MODE_INTER_MV_LAST,
  76. OC_MODE_INTER_MV_LAST2,OC_MODE_INTER_MV,OC_MODE_INTRA,OC_MODE_GOLDEN_MV,
  77. OC_MODE_INTER_MV_FOUR
  78. },
  79. /*Default ordering.*/
  80. {
  81. OC_MODE_INTER_NOMV,OC_MODE_INTRA,OC_MODE_INTER_MV,OC_MODE_INTER_MV_LAST,
  82. OC_MODE_INTER_MV_LAST2,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
  83. OC_MODE_INTER_MV_FOUR
  84. }
  85. };
  86. /*The original DCT tokens are extended and reordered during the construction of
  87. the Huffman tables.
  88. The extension means more bits can be read with fewer calls to the bitpacker
  89. during the Huffman decoding process (at the cost of larger Huffman tables),
  90. and fewer tokens require additional extra bits (reducing the average storage
  91. per decoded token).
  92. The revised ordering reveals essential information in the token value
  93. itself; specifically, whether or not there are additional extra bits to read
  94. and the parameter to which those extra bits are applied.
  95. The token is used to fetch a code word from the OC_DCT_CODE_WORD table below.
  96. The extra bits are added into code word at the bit position inferred from the
  97. token value, giving the final code word from which all required parameters
  98. are derived.
  99. The number of EOBs and the leading zero run length can be extracted directly.
  100. The coefficient magnitude is optionally negated before extraction, according
  101. to a 'flip' bit.*/
  102. /*The number of additional extra bits that are decoded with each of the
  103. internal DCT tokens.*/
  104. static const unsigned char OC_INTERNAL_DCT_TOKEN_EXTRA_BITS[15]={
  105. 12,4,3,3,4,4,5,5,8,8,8,8,3,3,6
  106. };
  107. /*Whether or not an internal token needs any additional extra bits.*/
  108. #define OC_DCT_TOKEN_NEEDS_MORE(token) \
  109. (token<(int)(sizeof(OC_INTERNAL_DCT_TOKEN_EXTRA_BITS)/ \
  110. sizeof(*OC_INTERNAL_DCT_TOKEN_EXTRA_BITS)))
  111. /*This token (OC_DCT_REPEAT_RUN3_TOKEN) requires more than 8 extra bits.*/
  112. #define OC_DCT_TOKEN_FAT_EOB (0)
  113. /*The number of EOBs to use for an end-of-frame token.
  114. Note: We want to set eobs to PTRDIFF_MAX here, but that requires C99, which
  115. is not yet available everywhere; this should be equivalent.*/
  116. #define OC_DCT_EOB_FINISH (~(size_t)0>>1)
  117. /*The location of the (6) run length bits in the code word.
  118. These are placed at index 0 and given 8 bits (even though 6 would suffice)
  119. because it may be faster to extract the lower byte on some platforms.*/
  120. #define OC_DCT_CW_RLEN_SHIFT (0)
  121. /*The location of the (12) EOB bits in the code word.*/
  122. #define OC_DCT_CW_EOB_SHIFT (8)
  123. /*The location of the (1) flip bit in the code word.
  124. This must be right under the magnitude bits.*/
  125. #define OC_DCT_CW_FLIP_BIT (20)
  126. /*The location of the (11) token magnitude bits in the code word.
  127. These must be last, and rely on a sign-extending right shift.*/
  128. #define OC_DCT_CW_MAG_SHIFT (21)
  129. /*Pack the given fields into a code word.*/
  130. #define OC_DCT_CW_PACK(_eobs,_rlen,_mag,_flip) \
  131. ((_eobs)<<OC_DCT_CW_EOB_SHIFT| \
  132. (_rlen)<<OC_DCT_CW_RLEN_SHIFT| \
  133. (_flip)<<OC_DCT_CW_FLIP_BIT| \
  134. (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
  135. /*A special code word value that signals the end of the frame (a long EOB run
  136. of zero).*/
  137. #define OC_DCT_CW_FINISH (0)
  138. /*The position at which to insert the extra bits in the code word.
  139. We use this formulation because Intel has no useful cmov.
  140. A real architecture would probably do better with two of those.
  141. This translates to 11 instructions(!), and is _still_ faster than either a
  142. table lookup (just barely) or the naive double-ternary implementation (which
  143. gcc translates to a jump and a cmov).
  144. This assumes OC_DCT_CW_RLEN_SHIFT is zero, but could easily be reworked if
  145. you want to make one of the other shifts zero.*/
  146. #define OC_DCT_TOKEN_EB_POS(_token) \
  147. ((OC_DCT_CW_EOB_SHIFT-OC_DCT_CW_MAG_SHIFT&-((_token)<2)) \
  148. +(OC_DCT_CW_MAG_SHIFT&-((_token)<12)))
  149. /*The code words for each internal token.
  150. See the notes at OC_DCT_TOKEN_MAP for the reasons why things are out of
  151. order.*/
  152. static const ogg_int32_t OC_DCT_CODE_WORD[92]={
  153. /*These tokens require additional extra bits for the EOB count.*/
  154. /*OC_DCT_REPEAT_RUN3_TOKEN (12 extra bits)*/
  155. OC_DCT_CW_FINISH,
  156. /*OC_DCT_REPEAT_RUN2_TOKEN (4 extra bits)*/
  157. OC_DCT_CW_PACK(16, 0, 0,0),
  158. /*These tokens require additional extra bits for the magnitude.*/
  159. /*OC_DCT_VAL_CAT5 (4 extra bits-1 already read)*/
  160. OC_DCT_CW_PACK( 0, 0, 13,0),
  161. OC_DCT_CW_PACK( 0, 0, 13,1),
  162. /*OC_DCT_VAL_CAT6 (5 extra bits-1 already read)*/
  163. OC_DCT_CW_PACK( 0, 0, 21,0),
  164. OC_DCT_CW_PACK( 0, 0, 21,1),
  165. /*OC_DCT_VAL_CAT7 (6 extra bits-1 already read)*/
  166. OC_DCT_CW_PACK( 0, 0, 37,0),
  167. OC_DCT_CW_PACK( 0, 0, 37,1),
  168. /*OC_DCT_VAL_CAT8 (10 extra bits-2 already read)*/
  169. OC_DCT_CW_PACK( 0, 0, 69,0),
  170. OC_DCT_CW_PACK( 0, 0,325,0),
  171. OC_DCT_CW_PACK( 0, 0, 69,1),
  172. OC_DCT_CW_PACK( 0, 0,325,1),
  173. /*These tokens require additional extra bits for the run length.*/
  174. /*OC_DCT_RUN_CAT1C (4 extra bits-1 already read)*/
  175. OC_DCT_CW_PACK( 0,10, +1,0),
  176. OC_DCT_CW_PACK( 0,10, -1,0),
  177. /*OC_DCT_ZRL_TOKEN (6 extra bits)
  178. Flip is set to distinguish this from OC_DCT_CW_FINISH.*/
  179. OC_DCT_CW_PACK( 0, 0, 0,1),
  180. /*The remaining tokens require no additional extra bits.*/
  181. /*OC_DCT_EOB1_TOKEN (0 extra bits)*/
  182. OC_DCT_CW_PACK( 1, 0, 0,0),
  183. /*OC_DCT_EOB2_TOKEN (0 extra bits)*/
  184. OC_DCT_CW_PACK( 2, 0, 0,0),
  185. /*OC_DCT_EOB3_TOKEN (0 extra bits)*/
  186. OC_DCT_CW_PACK( 3, 0, 0,0),
  187. /*OC_DCT_RUN_CAT1A (1 extra bit-1 already read)x5*/
  188. OC_DCT_CW_PACK( 0, 1, +1,0),
  189. OC_DCT_CW_PACK( 0, 1, -1,0),
  190. OC_DCT_CW_PACK( 0, 2, +1,0),
  191. OC_DCT_CW_PACK( 0, 2, -1,0),
  192. OC_DCT_CW_PACK( 0, 3, +1,0),
  193. OC_DCT_CW_PACK( 0, 3, -1,0),
  194. OC_DCT_CW_PACK( 0, 4, +1,0),
  195. OC_DCT_CW_PACK( 0, 4, -1,0),
  196. OC_DCT_CW_PACK( 0, 5, +1,0),
  197. OC_DCT_CW_PACK( 0, 5, -1,0),
  198. /*OC_DCT_RUN_CAT2A (2 extra bits-2 already read)*/
  199. OC_DCT_CW_PACK( 0, 1, +2,0),
  200. OC_DCT_CW_PACK( 0, 1, +3,0),
  201. OC_DCT_CW_PACK( 0, 1, -2,0),
  202. OC_DCT_CW_PACK( 0, 1, -3,0),
  203. /*OC_DCT_RUN_CAT1B (3 extra bits-3 already read)*/
  204. OC_DCT_CW_PACK( 0, 6, +1,0),
  205. OC_DCT_CW_PACK( 0, 7, +1,0),
  206. OC_DCT_CW_PACK( 0, 8, +1,0),
  207. OC_DCT_CW_PACK( 0, 9, +1,0),
  208. OC_DCT_CW_PACK( 0, 6, -1,0),
  209. OC_DCT_CW_PACK( 0, 7, -1,0),
  210. OC_DCT_CW_PACK( 0, 8, -1,0),
  211. OC_DCT_CW_PACK( 0, 9, -1,0),
  212. /*OC_DCT_RUN_CAT2B (3 extra bits-3 already read)*/
  213. OC_DCT_CW_PACK( 0, 2, +2,0),
  214. OC_DCT_CW_PACK( 0, 3, +2,0),
  215. OC_DCT_CW_PACK( 0, 2, +3,0),
  216. OC_DCT_CW_PACK( 0, 3, +3,0),
  217. OC_DCT_CW_PACK( 0, 2, -2,0),
  218. OC_DCT_CW_PACK( 0, 3, -2,0),
  219. OC_DCT_CW_PACK( 0, 2, -3,0),
  220. OC_DCT_CW_PACK( 0, 3, -3,0),
  221. /*OC_DCT_SHORT_ZRL_TOKEN (3 extra bits-3 already read)
  222. Flip is set on the first one to distinguish it from OC_DCT_CW_FINISH.*/
  223. OC_DCT_CW_PACK( 0, 0, 0,1),
  224. OC_DCT_CW_PACK( 0, 1, 0,0),
  225. OC_DCT_CW_PACK( 0, 2, 0,0),
  226. OC_DCT_CW_PACK( 0, 3, 0,0),
  227. OC_DCT_CW_PACK( 0, 4, 0,0),
  228. OC_DCT_CW_PACK( 0, 5, 0,0),
  229. OC_DCT_CW_PACK( 0, 6, 0,0),
  230. OC_DCT_CW_PACK( 0, 7, 0,0),
  231. /*OC_ONE_TOKEN (0 extra bits)*/
  232. OC_DCT_CW_PACK( 0, 0, +1,0),
  233. /*OC_MINUS_ONE_TOKEN (0 extra bits)*/
  234. OC_DCT_CW_PACK( 0, 0, -1,0),
  235. /*OC_TWO_TOKEN (0 extra bits)*/
  236. OC_DCT_CW_PACK( 0, 0, +2,0),
  237. /*OC_MINUS_TWO_TOKEN (0 extra bits)*/
  238. OC_DCT_CW_PACK( 0, 0, -2,0),
  239. /*OC_DCT_VAL_CAT2 (1 extra bit-1 already read)x4*/
  240. OC_DCT_CW_PACK( 0, 0, +3,0),
  241. OC_DCT_CW_PACK( 0, 0, -3,0),
  242. OC_DCT_CW_PACK( 0, 0, +4,0),
  243. OC_DCT_CW_PACK( 0, 0, -4,0),
  244. OC_DCT_CW_PACK( 0, 0, +5,0),
  245. OC_DCT_CW_PACK( 0, 0, -5,0),
  246. OC_DCT_CW_PACK( 0, 0, +6,0),
  247. OC_DCT_CW_PACK( 0, 0, -6,0),
  248. /*OC_DCT_VAL_CAT3 (2 extra bits-2 already read)*/
  249. OC_DCT_CW_PACK( 0, 0, +7,0),
  250. OC_DCT_CW_PACK( 0, 0, +8,0),
  251. OC_DCT_CW_PACK( 0, 0, -7,0),
  252. OC_DCT_CW_PACK( 0, 0, -8,0),
  253. /*OC_DCT_VAL_CAT4 (3 extra bits-3 already read)*/
  254. OC_DCT_CW_PACK( 0, 0, +9,0),
  255. OC_DCT_CW_PACK( 0, 0,+10,0),
  256. OC_DCT_CW_PACK( 0, 0,+11,0),
  257. OC_DCT_CW_PACK( 0, 0,+12,0),
  258. OC_DCT_CW_PACK( 0, 0, -9,0),
  259. OC_DCT_CW_PACK( 0, 0,-10,0),
  260. OC_DCT_CW_PACK( 0, 0,-11,0),
  261. OC_DCT_CW_PACK( 0, 0,-12,0),
  262. /*OC_DCT_REPEAT_RUN1_TOKEN (3 extra bits-3 already read)*/
  263. OC_DCT_CW_PACK( 8, 0, 0,0),
  264. OC_DCT_CW_PACK( 9, 0, 0,0),
  265. OC_DCT_CW_PACK(10, 0, 0,0),
  266. OC_DCT_CW_PACK(11, 0, 0,0),
  267. OC_DCT_CW_PACK(12, 0, 0,0),
  268. OC_DCT_CW_PACK(13, 0, 0,0),
  269. OC_DCT_CW_PACK(14, 0, 0,0),
  270. OC_DCT_CW_PACK(15, 0, 0,0),
  271. /*OC_DCT_REPEAT_RUN0_TOKEN (2 extra bits-2 already read)*/
  272. OC_DCT_CW_PACK( 4, 0, 0,0),
  273. OC_DCT_CW_PACK( 5, 0, 0,0),
  274. OC_DCT_CW_PACK( 6, 0, 0,0),
  275. OC_DCT_CW_PACK( 7, 0, 0,0),
  276. };
  277. static int oc_sb_run_unpack(oc_pack_buf *_opb){
  278. /*Coding scheme:
  279. Codeword Run Length
  280. 0 1
  281. 10x 2-3
  282. 110x 4-5
  283. 1110xx 6-9
  284. 11110xxx 10-17
  285. 111110xxxx 18-33
  286. 111111xxxxxxxxxxxx 34-4129*/
  287. static const ogg_int16_t OC_SB_RUN_TREE[22]={
  288. 4,
  289. -(1<<8|1),-(1<<8|1),-(1<<8|1),-(1<<8|1),
  290. -(1<<8|1),-(1<<8|1),-(1<<8|1),-(1<<8|1),
  291. -(3<<8|2),-(3<<8|2),-(3<<8|3),-(3<<8|3),
  292. -(4<<8|4),-(4<<8|5),-(4<<8|2<<4|6-6),17,
  293. 2,
  294. -(2<<8|2<<4|10-6),-(2<<8|2<<4|14-6),-(2<<8|4<<4|18-6),-(2<<8|12<<4|34-6)
  295. };
  296. int ret;
  297. ret=oc_huff_token_decode(_opb,OC_SB_RUN_TREE);
  298. if(ret>=0x10){
  299. int offs;
  300. offs=ret&0x1F;
  301. ret=6+offs+(int)oc_pack_read(_opb,ret-offs>>4);
  302. }
  303. return ret;
  304. }
  305. static int oc_block_run_unpack(oc_pack_buf *_opb){
  306. /*Coding scheme:
  307. Codeword Run Length
  308. 0x 1-2
  309. 10x 3-4
  310. 110x 5-6
  311. 1110xx 7-10
  312. 11110xx 11-14
  313. 11111xxxx 15-30*/
  314. static const ogg_int16_t OC_BLOCK_RUN_TREE[61]={
  315. 5,
  316. -(2<<8|1),-(2<<8|1),-(2<<8|1),-(2<<8|1),
  317. -(2<<8|1),-(2<<8|1),-(2<<8|1),-(2<<8|1),
  318. -(2<<8|2),-(2<<8|2),-(2<<8|2),-(2<<8|2),
  319. -(2<<8|2),-(2<<8|2),-(2<<8|2),-(2<<8|2),
  320. -(3<<8|3),-(3<<8|3),-(3<<8|3),-(3<<8|3),
  321. -(3<<8|4),-(3<<8|4),-(3<<8|4),-(3<<8|4),
  322. -(4<<8|5),-(4<<8|5),-(4<<8|6),-(4<<8|6),
  323. 33, 36, 39, 44,
  324. 1,-(1<<8|7),-(1<<8|8),
  325. 1,-(1<<8|9),-(1<<8|10),
  326. 2,-(2<<8|11),-(2<<8|12),-(2<<8|13),-(2<<8|14),
  327. 4,
  328. -(4<<8|15),-(4<<8|16),-(4<<8|17),-(4<<8|18),
  329. -(4<<8|19),-(4<<8|20),-(4<<8|21),-(4<<8|22),
  330. -(4<<8|23),-(4<<8|24),-(4<<8|25),-(4<<8|26),
  331. -(4<<8|27),-(4<<8|28),-(4<<8|29),-(4<<8|30)
  332. };
  333. return oc_huff_token_decode(_opb,OC_BLOCK_RUN_TREE);
  334. }
  335. void oc_dec_accel_init_c(oc_dec_ctx *_dec){
  336. # if defined(OC_DEC_USE_VTABLE)
  337. _dec->opt_vtable.dc_unpredict_mcu_plane=
  338. oc_dec_dc_unpredict_mcu_plane_c;
  339. # endif
  340. }
  341. static int oc_dec_init(oc_dec_ctx *_dec,const th_info *_info,
  342. const th_setup_info *_setup){
  343. int qti;
  344. int pli;
  345. int qi;
  346. int ret;
  347. ret=oc_state_init(&_dec->state,_info,3);
  348. if(ret<0)return ret;
  349. ret=oc_huff_trees_copy(_dec->huff_tables,
  350. (const ogg_int16_t *const *)_setup->huff_tables);
  351. if(ret<0){
  352. oc_state_clear(&_dec->state);
  353. return ret;
  354. }
  355. /*For each fragment, allocate one byte for every DCT coefficient token, plus
  356. one byte for extra-bits for each token, plus one more byte for the long
  357. EOB run, just in case it's the very last token and has a run length of
  358. one.*/
  359. _dec->dct_tokens=(unsigned char *)_ogg_malloc((64+64+1)*
  360. _dec->state.nfrags*sizeof(_dec->dct_tokens[0]));
  361. if(_dec->dct_tokens==NULL){
  362. oc_huff_trees_clear(_dec->huff_tables);
  363. oc_state_clear(&_dec->state);
  364. return TH_EFAULT;
  365. }
  366. for(qi=0;qi<64;qi++)for(pli=0;pli<3;pli++)for(qti=0;qti<2;qti++){
  367. _dec->state.dequant_tables[qi][pli][qti]=
  368. _dec->state.dequant_table_data[qi][pli][qti];
  369. }
  370. oc_dequant_tables_init(_dec->state.dequant_tables,_dec->pp_dc_scale,
  371. &_setup->qinfo);
  372. for(qi=0;qi<64;qi++){
  373. int qsum;
  374. qsum=0;
  375. for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
  376. qsum+=_dec->state.dequant_tables[qi][pli][qti][12]+
  377. _dec->state.dequant_tables[qi][pli][qti][17]+
  378. _dec->state.dequant_tables[qi][pli][qti][18]+
  379. _dec->state.dequant_tables[qi][pli][qti][24]<<(pli==0);
  380. }
  381. _dec->pp_sharp_mod[qi]=-(qsum>>11);
  382. }
  383. memcpy(_dec->state.loop_filter_limits,_setup->qinfo.loop_filter_limits,
  384. sizeof(_dec->state.loop_filter_limits));
  385. oc_dec_accel_init(_dec);
  386. _dec->pp_level=OC_PP_LEVEL_DISABLED;
  387. _dec->dc_qis=NULL;
  388. _dec->variances=NULL;
  389. _dec->pp_frame_data=NULL;
  390. _dec->stripe_cb.ctx=NULL;
  391. _dec->stripe_cb.stripe_decoded=NULL;
  392. #if defined(HAVE_CAIRO)
  393. _dec->telemetry_bits=0;
  394. _dec->telemetry_qi=0;
  395. _dec->telemetry_mbmode=0;
  396. _dec->telemetry_mv=0;
  397. _dec->telemetry_frame_data=NULL;
  398. #endif
  399. return 0;
  400. }
  401. static void oc_dec_clear(oc_dec_ctx *_dec){
  402. #if defined(HAVE_CAIRO)
  403. _ogg_free(_dec->telemetry_frame_data);
  404. #endif
  405. _ogg_free(_dec->pp_frame_data);
  406. _ogg_free(_dec->variances);
  407. _ogg_free(_dec->dc_qis);
  408. _ogg_free(_dec->dct_tokens);
  409. oc_huff_trees_clear(_dec->huff_tables);
  410. oc_state_clear(&_dec->state);
  411. }
  412. static int oc_dec_frame_header_unpack(oc_dec_ctx *_dec){
  413. long val;
  414. /*Check to make sure this is a data packet.*/
  415. val=oc_pack_read1(&_dec->opb);
  416. if(val!=0)return TH_EBADPACKET;
  417. /*Read in the frame type (I or P).*/
  418. val=oc_pack_read1(&_dec->opb);
  419. _dec->state.frame_type=(int)val;
  420. /*Read in the qi list.*/
  421. val=oc_pack_read(&_dec->opb,6);
  422. _dec->state.qis[0]=(unsigned char)val;
  423. val=oc_pack_read1(&_dec->opb);
  424. if(!val)_dec->state.nqis=1;
  425. else{
  426. val=oc_pack_read(&_dec->opb,6);
  427. _dec->state.qis[1]=(unsigned char)val;
  428. val=oc_pack_read1(&_dec->opb);
  429. if(!val)_dec->state.nqis=2;
  430. else{
  431. val=oc_pack_read(&_dec->opb,6);
  432. _dec->state.qis[2]=(unsigned char)val;
  433. _dec->state.nqis=3;
  434. }
  435. }
  436. if(_dec->state.frame_type==OC_INTRA_FRAME){
  437. /*Keyframes have 3 unused configuration bits, holdovers from VP3 days.
  438. Most of the other unused bits in the VP3 headers were eliminated.
  439. I don't know why these remain.*/
  440. /*I wanted to eliminate wasted bits, but not all config wiggle room
  441. --Monty.*/
  442. val=oc_pack_read(&_dec->opb,3);
  443. if(val!=0)return TH_EIMPL;
  444. }
  445. return 0;
  446. }
  447. /*Mark all fragments as coded and in OC_MODE_INTRA.
  448. This also builds up the coded fragment list (in coded order), and clears the
  449. uncoded fragment list.
  450. It does not update the coded macro block list nor the super block flags, as
  451. those are not used when decoding INTRA frames.*/
  452. static void oc_dec_mark_all_intra(oc_dec_ctx *_dec){
  453. const oc_sb_map *sb_maps;
  454. const oc_sb_flags *sb_flags;
  455. oc_fragment *frags;
  456. ptrdiff_t *coded_fragis;
  457. ptrdiff_t ncoded_fragis;
  458. ptrdiff_t prev_ncoded_fragis;
  459. unsigned nsbs;
  460. unsigned sbi;
  461. int pli;
  462. coded_fragis=_dec->state.coded_fragis;
  463. prev_ncoded_fragis=ncoded_fragis=0;
  464. sb_maps=(const oc_sb_map *)_dec->state.sb_maps;
  465. sb_flags=_dec->state.sb_flags;
  466. frags=_dec->state.frags;
  467. sbi=nsbs=0;
  468. for(pli=0;pli<3;pli++){
  469. nsbs+=_dec->state.fplanes[pli].nsbs;
  470. for(;sbi<nsbs;sbi++){
  471. int quadi;
  472. for(quadi=0;quadi<4;quadi++)if(sb_flags[sbi].quad_valid&1<<quadi){
  473. int bi;
  474. for(bi=0;bi<4;bi++){
  475. ptrdiff_t fragi;
  476. fragi=sb_maps[sbi][quadi][bi];
  477. if(fragi>=0){
  478. frags[fragi].coded=1;
  479. frags[fragi].refi=OC_FRAME_SELF;
  480. frags[fragi].mb_mode=OC_MODE_INTRA;
  481. coded_fragis[ncoded_fragis++]=fragi;
  482. }
  483. }
  484. }
  485. }
  486. _dec->state.ncoded_fragis[pli]=ncoded_fragis-prev_ncoded_fragis;
  487. prev_ncoded_fragis=ncoded_fragis;
  488. }
  489. _dec->state.ntotal_coded_fragis=ncoded_fragis;
  490. }
  491. /*Decodes the bit flags indicating whether each super block is partially coded
  492. or not.
  493. Return: The number of partially coded super blocks.*/
  494. static unsigned oc_dec_partial_sb_flags_unpack(oc_dec_ctx *_dec){
  495. oc_sb_flags *sb_flags;
  496. unsigned nsbs;
  497. unsigned sbi;
  498. unsigned npartial;
  499. unsigned run_count;
  500. long val;
  501. int flag;
  502. val=oc_pack_read1(&_dec->opb);
  503. flag=(int)val;
  504. sb_flags=_dec->state.sb_flags;
  505. nsbs=_dec->state.nsbs;
  506. sbi=npartial=0;
  507. while(sbi<nsbs){
  508. int full_run;
  509. run_count=oc_sb_run_unpack(&_dec->opb);
  510. full_run=run_count>=4129;
  511. do{
  512. sb_flags[sbi].coded_partially=flag;
  513. sb_flags[sbi].coded_fully=0;
  514. npartial+=flag;
  515. sbi++;
  516. }
  517. while(--run_count>0&&sbi<nsbs);
  518. if(full_run&&sbi<nsbs){
  519. val=oc_pack_read1(&_dec->opb);
  520. flag=(int)val;
  521. }
  522. else flag=!flag;
  523. }
  524. /*TODO: run_count should be 0 here.
  525. If it's not, we should issue a warning of some kind.*/
  526. return npartial;
  527. }
  528. /*Decodes the bit flags for whether or not each non-partially-coded super
  529. block is fully coded or not.
  530. This function should only be called if there is at least one
  531. non-partially-coded super block.
  532. Return: The number of partially coded super blocks.*/
  533. static void oc_dec_coded_sb_flags_unpack(oc_dec_ctx *_dec){
  534. oc_sb_flags *sb_flags;
  535. unsigned nsbs;
  536. unsigned sbi;
  537. unsigned run_count;
  538. long val;
  539. int flag;
  540. sb_flags=_dec->state.sb_flags;
  541. nsbs=_dec->state.nsbs;
  542. /*Skip partially coded super blocks.*/
  543. for(sbi=0;sb_flags[sbi].coded_partially;sbi++);
  544. val=oc_pack_read1(&_dec->opb);
  545. flag=(int)val;
  546. do{
  547. int full_run;
  548. run_count=oc_sb_run_unpack(&_dec->opb);
  549. full_run=run_count>=4129;
  550. for(;sbi<nsbs;sbi++){
  551. if(sb_flags[sbi].coded_partially)continue;
  552. if(run_count--<=0)break;
  553. sb_flags[sbi].coded_fully=flag;
  554. }
  555. if(full_run&&sbi<nsbs){
  556. val=oc_pack_read1(&_dec->opb);
  557. flag=(int)val;
  558. }
  559. else flag=!flag;
  560. }
  561. while(sbi<nsbs);
  562. /*TODO: run_count should be 0 here.
  563. If it's not, we should issue a warning of some kind.*/
  564. }
  565. static void oc_dec_coded_flags_unpack(oc_dec_ctx *_dec){
  566. const oc_sb_map *sb_maps;
  567. const oc_sb_flags *sb_flags;
  568. signed char *mb_modes;
  569. oc_fragment *frags;
  570. unsigned nsbs;
  571. unsigned sbi;
  572. unsigned npartial;
  573. long val;
  574. int pli;
  575. int flag;
  576. int run_count;
  577. ptrdiff_t *coded_fragis;
  578. ptrdiff_t *uncoded_fragis;
  579. ptrdiff_t ncoded_fragis;
  580. ptrdiff_t nuncoded_fragis;
  581. ptrdiff_t prev_ncoded_fragis;
  582. npartial=oc_dec_partial_sb_flags_unpack(_dec);
  583. if(npartial<_dec->state.nsbs)oc_dec_coded_sb_flags_unpack(_dec);
  584. if(npartial>0){
  585. val=oc_pack_read1(&_dec->opb);
  586. flag=!(int)val;
  587. }
  588. else flag=0;
  589. sb_maps=(const oc_sb_map *)_dec->state.sb_maps;
  590. sb_flags=_dec->state.sb_flags;
  591. mb_modes=_dec->state.mb_modes;
  592. frags=_dec->state.frags;
  593. sbi=nsbs=run_count=0;
  594. coded_fragis=_dec->state.coded_fragis;
  595. uncoded_fragis=coded_fragis+_dec->state.nfrags;
  596. prev_ncoded_fragis=ncoded_fragis=nuncoded_fragis=0;
  597. for(pli=0;pli<3;pli++){
  598. nsbs+=_dec->state.fplanes[pli].nsbs;
  599. for(;sbi<nsbs;sbi++){
  600. int quadi;
  601. for(quadi=0;quadi<4;quadi++)if(sb_flags[sbi].quad_valid&1<<quadi){
  602. int quad_coded;
  603. int bi;
  604. quad_coded=0;
  605. for(bi=0;bi<4;bi++){
  606. ptrdiff_t fragi;
  607. fragi=sb_maps[sbi][quadi][bi];
  608. if(fragi>=0){
  609. int coded;
  610. if(sb_flags[sbi].coded_fully)coded=1;
  611. else if(!sb_flags[sbi].coded_partially)coded=0;
  612. else{
  613. if(run_count<=0){
  614. run_count=oc_block_run_unpack(&_dec->opb);
  615. flag=!flag;
  616. }
  617. run_count--;
  618. coded=flag;
  619. }
  620. if(coded)coded_fragis[ncoded_fragis++]=fragi;
  621. else *(uncoded_fragis-++nuncoded_fragis)=fragi;
  622. quad_coded|=coded;
  623. frags[fragi].coded=coded;
  624. frags[fragi].refi=OC_FRAME_NONE;
  625. }
  626. }
  627. /*Remember if there's a coded luma block in this macro block.*/
  628. if(!pli)mb_modes[sbi<<2|quadi]=quad_coded;
  629. }
  630. }
  631. _dec->state.ncoded_fragis[pli]=ncoded_fragis-prev_ncoded_fragis;
  632. prev_ncoded_fragis=ncoded_fragis;
  633. }
  634. _dec->state.ntotal_coded_fragis=ncoded_fragis;
  635. /*TODO: run_count should be 0 here.
  636. If it's not, we should issue a warning of some kind.*/
  637. }
  638. /*Coding scheme:
  639. Codeword Mode Index
  640. 0 0
  641. 10 1
  642. 110 2
  643. 1110 3
  644. 11110 4
  645. 111110 5
  646. 1111110 6
  647. 1111111 7*/
  648. static const ogg_int16_t OC_VLC_MODE_TREE[26]={
  649. 4,
  650. -(1<<8|0),-(1<<8|0),-(1<<8|0),-(1<<8|0),
  651. -(1<<8|0),-(1<<8|0),-(1<<8|0),-(1<<8|0),
  652. -(2<<8|1),-(2<<8|1),-(2<<8|1),-(2<<8|1),
  653. -(3<<8|2),-(3<<8|2),-(4<<8|3),17,
  654. 3,
  655. -(1<<8|4),-(1<<8|4),-(1<<8|4),-(1<<8|4),
  656. -(2<<8|5),-(2<<8|5),-(3<<8|6),-(3<<8|7)
  657. };
  658. static const ogg_int16_t OC_CLC_MODE_TREE[9]={
  659. 3,
  660. -(3<<8|0),-(3<<8|1),-(3<<8|2),-(3<<8|3),
  661. -(3<<8|4),-(3<<8|5),-(3<<8|6),-(3<<8|7)
  662. };
  663. /*Unpacks the list of macro block modes for INTER frames.*/
  664. static void oc_dec_mb_modes_unpack(oc_dec_ctx *_dec){
  665. signed char *mb_modes;
  666. const unsigned char *alphabet;
  667. unsigned char scheme0_alphabet[8];
  668. const ogg_int16_t *mode_tree;
  669. size_t nmbs;
  670. size_t mbi;
  671. long val;
  672. int mode_scheme;
  673. val=oc_pack_read(&_dec->opb,3);
  674. mode_scheme=(int)val;
  675. if(mode_scheme==0){
  676. int mi;
  677. /*Just in case, initialize the modes to something.
  678. If the bitstream doesn't contain each index exactly once, it's likely
  679. corrupt and the rest of the packet is garbage anyway, but this way we
  680. won't crash, and we'll decode SOMETHING.*/
  681. /*LOOP VECTORIZES*/
  682. for(mi=0;mi<OC_NMODES;mi++)scheme0_alphabet[mi]=OC_MODE_INTER_NOMV;
  683. for(mi=0;mi<OC_NMODES;mi++){
  684. val=oc_pack_read(&_dec->opb,3);
  685. scheme0_alphabet[val]=OC_MODE_ALPHABETS[6][mi];
  686. }
  687. alphabet=scheme0_alphabet;
  688. }
  689. else alphabet=OC_MODE_ALPHABETS[mode_scheme-1];
  690. mode_tree=mode_scheme==7?OC_CLC_MODE_TREE:OC_VLC_MODE_TREE;
  691. mb_modes=_dec->state.mb_modes;
  692. nmbs=_dec->state.nmbs;
  693. for(mbi=0;mbi<nmbs;mbi++){
  694. if(mb_modes[mbi]>0){
  695. /*We have a coded luma block; decode a mode.*/
  696. mb_modes[mbi]=alphabet[oc_huff_token_decode(&_dec->opb,mode_tree)];
  697. }
  698. /*For other valid macro blocks, INTER_NOMV is forced, but we rely on the
  699. fact that OC_MODE_INTER_NOMV is already 0.*/
  700. }
  701. }
  702. static const ogg_int16_t OC_VLC_MV_COMP_TREE[101]={
  703. 5,
  704. -(3<<8|32+0),-(3<<8|32+0),-(3<<8|32+0),-(3<<8|32+0),
  705. -(3<<8|32+1),-(3<<8|32+1),-(3<<8|32+1),-(3<<8|32+1),
  706. -(3<<8|32-1),-(3<<8|32-1),-(3<<8|32-1),-(3<<8|32-1),
  707. -(4<<8|32+2),-(4<<8|32+2),-(4<<8|32-2),-(4<<8|32-2),
  708. -(4<<8|32+3),-(4<<8|32+3),-(4<<8|32-3),-(4<<8|32-3),
  709. 33, 36, 39, 42,
  710. 45, 50, 55, 60,
  711. 65, 74, 83, 92,
  712. 1,-(1<<8|32+4),-(1<<8|32-4),
  713. 1,-(1<<8|32+5),-(1<<8|32-5),
  714. 1,-(1<<8|32+6),-(1<<8|32-6),
  715. 1,-(1<<8|32+7),-(1<<8|32-7),
  716. 2,-(2<<8|32+8),-(2<<8|32-8),-(2<<8|32+9),-(2<<8|32-9),
  717. 2,-(2<<8|32+10),-(2<<8|32-10),-(2<<8|32+11),-(2<<8|32-11),
  718. 2,-(2<<8|32+12),-(2<<8|32-12),-(2<<8|32+13),-(2<<8|32-13),
  719. 2,-(2<<8|32+14),-(2<<8|32-14),-(2<<8|32+15),-(2<<8|32-15),
  720. 3,
  721. -(3<<8|32+16),-(3<<8|32-16),-(3<<8|32+17),-(3<<8|32-17),
  722. -(3<<8|32+18),-(3<<8|32-18),-(3<<8|32+19),-(3<<8|32-19),
  723. 3,
  724. -(3<<8|32+20),-(3<<8|32-20),-(3<<8|32+21),-(3<<8|32-21),
  725. -(3<<8|32+22),-(3<<8|32-22),-(3<<8|32+23),-(3<<8|32-23),
  726. 3,
  727. -(3<<8|32+24),-(3<<8|32-24),-(3<<8|32+25),-(3<<8|32-25),
  728. -(3<<8|32+26),-(3<<8|32-26),-(3<<8|32+27),-(3<<8|32-27),
  729. 3,
  730. -(3<<8|32+28),-(3<<8|32-28),-(3<<8|32+29),-(3<<8|32-29),
  731. -(3<<8|32+30),-(3<<8|32-30),-(3<<8|32+31),-(3<<8|32-31)
  732. };
  733. static const ogg_int16_t OC_CLC_MV_COMP_TREE[65]={
  734. 6,
  735. -(6<<8|32 +0),-(6<<8|32 -0),-(6<<8|32 +1),-(6<<8|32 -1),
  736. -(6<<8|32 +2),-(6<<8|32 -2),-(6<<8|32 +3),-(6<<8|32 -3),
  737. -(6<<8|32 +4),-(6<<8|32 -4),-(6<<8|32 +5),-(6<<8|32 -5),
  738. -(6<<8|32 +6),-(6<<8|32 -6),-(6<<8|32 +7),-(6<<8|32 -7),
  739. -(6<<8|32 +8),-(6<<8|32 -8),-(6<<8|32 +9),-(6<<8|32 -9),
  740. -(6<<8|32+10),-(6<<8|32-10),-(6<<8|32+11),-(6<<8|32-11),
  741. -(6<<8|32+12),-(6<<8|32-12),-(6<<8|32+13),-(6<<8|32-13),
  742. -(6<<8|32+14),-(6<<8|32-14),-(6<<8|32+15),-(6<<8|32-15),
  743. -(6<<8|32+16),-(6<<8|32-16),-(6<<8|32+17),-(6<<8|32-17),
  744. -(6<<8|32+18),-(6<<8|32-18),-(6<<8|32+19),-(6<<8|32-19),
  745. -(6<<8|32+20),-(6<<8|32-20),-(6<<8|32+21),-(6<<8|32-21),
  746. -(6<<8|32+22),-(6<<8|32-22),-(6<<8|32+23),-(6<<8|32-23),
  747. -(6<<8|32+24),-(6<<8|32-24),-(6<<8|32+25),-(6<<8|32-25),
  748. -(6<<8|32+26),-(6<<8|32-26),-(6<<8|32+27),-(6<<8|32-27),
  749. -(6<<8|32+28),-(6<<8|32-28),-(6<<8|32+29),-(6<<8|32-29),
  750. -(6<<8|32+30),-(6<<8|32-30),-(6<<8|32+31),-(6<<8|32-31)
  751. };
  752. static oc_mv oc_mv_unpack(oc_pack_buf *_opb,const ogg_int16_t *_tree){
  753. int dx;
  754. int dy;
  755. dx=oc_huff_token_decode(_opb,_tree)-32;
  756. dy=oc_huff_token_decode(_opb,_tree)-32;
  757. return OC_MV(dx,dy);
  758. }
  759. /*Unpacks the list of motion vectors for INTER frames, and propagtes the macro
  760. block modes and motion vectors to the individual fragments.*/
  761. static void oc_dec_mv_unpack_and_frag_modes_fill(oc_dec_ctx *_dec){
  762. const oc_mb_map *mb_maps;
  763. const signed char *mb_modes;
  764. oc_set_chroma_mvs_func set_chroma_mvs;
  765. const ogg_int16_t *mv_comp_tree;
  766. oc_fragment *frags;
  767. oc_mv *frag_mvs;
  768. const unsigned char *map_idxs;
  769. int map_nidxs;
  770. oc_mv last_mv;
  771. oc_mv prior_mv;
  772. oc_mv cbmvs[4];
  773. size_t nmbs;
  774. size_t mbi;
  775. long val;
  776. set_chroma_mvs=OC_SET_CHROMA_MVS_TABLE[_dec->state.info.pixel_fmt];
  777. val=oc_pack_read1(&_dec->opb);
  778. mv_comp_tree=val?OC_CLC_MV_COMP_TREE:OC_VLC_MV_COMP_TREE;
  779. map_idxs=OC_MB_MAP_IDXS[_dec->state.info.pixel_fmt];
  780. map_nidxs=OC_MB_MAP_NIDXS[_dec->state.info.pixel_fmt];
  781. prior_mv=last_mv=0;
  782. frags=_dec->state.frags;
  783. frag_mvs=_dec->state.frag_mvs;
  784. mb_maps=(const oc_mb_map *)_dec->state.mb_maps;
  785. mb_modes=_dec->state.mb_modes;
  786. nmbs=_dec->state.nmbs;
  787. for(mbi=0;mbi<nmbs;mbi++){
  788. int mb_mode;
  789. mb_mode=mb_modes[mbi];
  790. if(mb_mode!=OC_MODE_INVALID){
  791. oc_mv mbmv;
  792. ptrdiff_t fragi;
  793. int mapi;
  794. int mapii;
  795. int refi;
  796. if(mb_mode==OC_MODE_INTER_MV_FOUR){
  797. oc_mv lbmvs[4];
  798. int bi;
  799. prior_mv=last_mv;
  800. for(bi=0;bi<4;bi++){
  801. fragi=mb_maps[mbi][0][bi];
  802. if(frags[fragi].coded){
  803. frags[fragi].refi=OC_FRAME_PREV;
  804. frags[fragi].mb_mode=OC_MODE_INTER_MV_FOUR;
  805. lbmvs[bi]=last_mv=oc_mv_unpack(&_dec->opb,mv_comp_tree);
  806. frag_mvs[fragi]=lbmvs[bi];
  807. }
  808. else lbmvs[bi]=0;
  809. }
  810. (*set_chroma_mvs)(cbmvs,lbmvs);
  811. for(mapii=4;mapii<map_nidxs;mapii++){
  812. mapi=map_idxs[mapii];
  813. bi=mapi&3;
  814. fragi=mb_maps[mbi][mapi>>2][bi];
  815. if(frags[fragi].coded){
  816. frags[fragi].refi=OC_FRAME_PREV;
  817. frags[fragi].mb_mode=OC_MODE_INTER_MV_FOUR;
  818. frag_mvs[fragi]=cbmvs[bi];
  819. }
  820. }
  821. }
  822. else{
  823. switch(mb_mode){
  824. case OC_MODE_INTER_MV:{
  825. prior_mv=last_mv;
  826. last_mv=mbmv=oc_mv_unpack(&_dec->opb,mv_comp_tree);
  827. }break;
  828. case OC_MODE_INTER_MV_LAST:mbmv=last_mv;break;
  829. case OC_MODE_INTER_MV_LAST2:{
  830. mbmv=prior_mv;
  831. prior_mv=last_mv;
  832. last_mv=mbmv;
  833. }break;
  834. case OC_MODE_GOLDEN_MV:{
  835. mbmv=oc_mv_unpack(&_dec->opb,mv_comp_tree);
  836. }break;
  837. default:mbmv=0;break;
  838. }
  839. /*Fill in the MVs for the fragments.*/
  840. refi=OC_FRAME_FOR_MODE(mb_mode);
  841. mapii=0;
  842. do{
  843. mapi=map_idxs[mapii];
  844. fragi=mb_maps[mbi][mapi>>2][mapi&3];
  845. if(frags[fragi].coded){
  846. frags[fragi].refi=refi;
  847. frags[fragi].mb_mode=mb_mode;
  848. frag_mvs[fragi]=mbmv;
  849. }
  850. }
  851. while(++mapii<map_nidxs);
  852. }
  853. }
  854. }
  855. }
  856. static void oc_dec_block_qis_unpack(oc_dec_ctx *_dec){
  857. oc_fragment *frags;
  858. const ptrdiff_t *coded_fragis;
  859. ptrdiff_t ncoded_fragis;
  860. ptrdiff_t fragii;
  861. ptrdiff_t fragi;
  862. ncoded_fragis=_dec->state.ntotal_coded_fragis;
  863. if(ncoded_fragis<=0)return;
  864. frags=_dec->state.frags;
  865. coded_fragis=_dec->state.coded_fragis;
  866. if(_dec->state.nqis==1){
  867. /*If this frame has only a single qi value, then just use it for all coded
  868. fragments.*/
  869. for(fragii=0;fragii<ncoded_fragis;fragii++){
  870. frags[coded_fragis[fragii]].qii=0;
  871. }
  872. }
  873. else{
  874. long val;
  875. int flag;
  876. int nqi1;
  877. int run_count;
  878. /*Otherwise, we decode a qi index for each fragment, using two passes of
  879. the same binary RLE scheme used for super-block coded bits.
  880. The first pass marks each fragment as having a qii of 0 or greater than
  881. 0, and the second pass (if necessary), distinguishes between a qii of
  882. 1 and 2.
  883. At first we just store the qii in the fragment.
  884. After all the qii's are decoded, we make a final pass to replace them
  885. with the corresponding qi's for this frame.*/
  886. val=oc_pack_read1(&_dec->opb);
  887. flag=(int)val;
  888. nqi1=0;
  889. fragii=0;
  890. while(fragii<ncoded_fragis){
  891. int full_run;
  892. run_count=oc_sb_run_unpack(&_dec->opb);
  893. full_run=run_count>=4129;
  894. do{
  895. frags[coded_fragis[fragii++]].qii=flag;
  896. nqi1+=flag;
  897. }
  898. while(--run_count>0&&fragii<ncoded_fragis);
  899. if(full_run&&fragii<ncoded_fragis){
  900. val=oc_pack_read1(&_dec->opb);
  901. flag=(int)val;
  902. }
  903. else flag=!flag;
  904. }
  905. /*TODO: run_count should be 0 here.
  906. If it's not, we should issue a warning of some kind.*/
  907. /*If we have 3 different qi's for this frame, and there was at least one
  908. fragment with a non-zero qi, make the second pass.*/
  909. if(_dec->state.nqis==3&&nqi1>0){
  910. /*Skip qii==0 fragments.*/
  911. for(fragii=0;frags[coded_fragis[fragii]].qii==0;fragii++);
  912. val=oc_pack_read1(&_dec->opb);
  913. flag=(int)val;
  914. do{
  915. int full_run;
  916. run_count=oc_sb_run_unpack(&_dec->opb);
  917. full_run=run_count>=4129;
  918. for(;fragii<ncoded_fragis;fragii++){
  919. fragi=coded_fragis[fragii];
  920. if(frags[fragi].qii==0)continue;
  921. if(run_count--<=0)break;
  922. frags[fragi].qii+=flag;
  923. }
  924. if(full_run&&fragii<ncoded_fragis){
  925. val=oc_pack_read1(&_dec->opb);
  926. flag=(int)val;
  927. }
  928. else flag=!flag;
  929. }
  930. while(fragii<ncoded_fragis);
  931. /*TODO: run_count should be 0 here.
  932. If it's not, we should issue a warning of some kind.*/
  933. }
  934. }
  935. }
  936. /*Unpacks the DC coefficient tokens.
  937. Unlike when unpacking the AC coefficient tokens, we actually need to decode
  938. the DC coefficient values now so that we can do DC prediction.
  939. _huff_idx: The index of the Huffman table to use for each color plane.
  940. _ntoks_left: The number of tokens left to be decoded in each color plane for
  941. each coefficient.
  942. This is updated as EOB tokens and zero run tokens are decoded.
  943. Return: The length of any outstanding EOB run.*/
  944. static ptrdiff_t oc_dec_dc_coeff_unpack(oc_dec_ctx *_dec,int _huff_idxs[2],
  945. ptrdiff_t _ntoks_left[3][64]){
  946. unsigned char *dct_tokens;
  947. oc_fragment *frags;
  948. const ptrdiff_t *coded_fragis;
  949. ptrdiff_t ncoded_fragis;
  950. ptrdiff_t fragii;
  951. ptrdiff_t eobs;
  952. ptrdiff_t ti;
  953. int pli;
  954. dct_tokens=_dec->dct_tokens;
  955. frags=_dec->state.frags;
  956. coded_fragis=_dec->state.coded_fragis;
  957. ncoded_fragis=fragii=eobs=ti=0;
  958. for(pli=0;pli<3;pli++){
  959. ptrdiff_t run_counts[64];
  960. ptrdiff_t eob_count;
  961. ptrdiff_t eobi;
  962. int rli;
  963. ncoded_fragis+=_dec->state.ncoded_fragis[pli];
  964. memset(run_counts,0,sizeof(run_counts));
  965. _dec->eob_runs[pli][0]=eobs;
  966. _dec->ti0[pli][0]=ti;
  967. /*Continue any previous EOB run, if there was one.*/
  968. eobi=eobs;
  969. if(ncoded_fragis-fragii<eobi)eobi=ncoded_fragis-fragii;
  970. eob_count=eobi;
  971. eobs-=eobi;
  972. while(eobi-->0)frags[coded_fragis[fragii++]].dc=0;
  973. while(fragii<ncoded_fragis){
  974. int token;
  975. int cw;
  976. int eb;
  977. int skip;
  978. token=oc_huff_token_decode(&_dec->opb,
  979. _dec->huff_tables[_huff_idxs[pli+1>>1]]);
  980. dct_tokens[ti++]=(unsigned char)token;
  981. if(OC_DCT_TOKEN_NEEDS_MORE(token)){
  982. eb=(int)oc_pack_read(&_dec->opb,
  983. OC_INTERNAL_DCT_TOKEN_EXTRA_BITS[token]);
  984. dct_tokens[ti++]=(unsigned char)eb;
  985. if(token==OC_DCT_TOKEN_FAT_EOB)dct_tokens[ti++]=(unsigned char)(eb>>8);
  986. eb<<=OC_DCT_TOKEN_EB_POS(token);
  987. }
  988. else eb=0;
  989. cw=OC_DCT_CODE_WORD[token]+eb;
  990. eobs=cw>>OC_DCT_CW_EOB_SHIFT&0xFFF;
  991. if(cw==OC_DCT_CW_FINISH)eobs=OC_DCT_EOB_FINISH;
  992. if(eobs){
  993. eobi=OC_MINI(eobs,ncoded_fragis-fragii);
  994. eob_count+=eobi;
  995. eobs-=eobi;
  996. while(eobi-->0)frags[coded_fragis[fragii++]].dc=0;
  997. }
  998. else{
  999. int coeff;
  1000. skip=(unsigned char)(cw>>OC_DCT_CW_RLEN_SHIFT);
  1001. cw^=-(cw&1<<OC_DCT_CW_FLIP_BIT);
  1002. coeff=cw>>OC_DCT_CW_MAG_SHIFT;
  1003. if(skip)coeff=0;
  1004. run_counts[skip]++;
  1005. frags[coded_fragis[fragii++]].dc=coeff;
  1006. }
  1007. }
  1008. /*Add the total EOB count to the longest run length.*/
  1009. run_counts[63]+=eob_count;
  1010. /*And convert the run_counts array to a moment table.*/
  1011. for(rli=63;rli-->0;)run_counts[rli]+=run_counts[rli+1];
  1012. /*Finally, subtract off the number of coefficients that have been
  1013. accounted for by runs started in this coefficient.*/
  1014. for(rli=64;rli-->0;)_ntoks_left[pli][rli]-=run_counts[rli];
  1015. }
  1016. _dec->dct_tokens_count=ti;
  1017. return eobs;
  1018. }
  1019. /*Unpacks the AC coefficient tokens.
  1020. This can completely discard coefficient values while unpacking, and so is
  1021. somewhat simpler than unpacking the DC coefficient tokens.
  1022. _huff_idx: The index of the Huffman table to use for each color plane.
  1023. _ntoks_left: The number of tokens left to be decoded in each color plane for
  1024. each coefficient.
  1025. This is updated as EOB tokens and zero run tokens are decoded.
  1026. _eobs: The length of any outstanding EOB run from previous
  1027. coefficients.
  1028. Return: The length of any outstanding EOB run.*/
  1029. static int oc_dec_ac_coeff_unpack(oc_dec_ctx *_dec,int _zzi,int _huff_idxs[2],
  1030. ptrdiff_t _ntoks_left[3][64],ptrdiff_t _eobs){
  1031. unsigned char *dct_tokens;
  1032. ptrdiff_t ti;
  1033. int pli;
  1034. dct_tokens=_dec->dct_tokens;
  1035. ti=_dec->dct_tokens_count;
  1036. for(pli=0;pli<3;pli++){
  1037. ptrdiff_t run_counts[64];
  1038. ptrdiff_t eob_count;
  1039. size_t ntoks_left;
  1040. size_t ntoks;
  1041. int rli;
  1042. _dec->eob_runs[pli][_zzi]=_eobs;
  1043. _dec->ti0[pli][_zzi]=ti;
  1044. ntoks_left=_ntoks_left[pli][_zzi];
  1045. memset(run_counts,0,sizeof(run_counts));
  1046. eob_count=0;
  1047. ntoks=0;
  1048. while(ntoks+_eobs<ntoks_left){
  1049. int token;
  1050. int cw;
  1051. int eb;
  1052. int skip;
  1053. ntoks+=_eobs;
  1054. eob_count+=_eobs;
  1055. token=oc_huff_token_decode(&_dec->opb,
  1056. _dec->huff_tables[_huff_idxs[pli+1>>1]]);
  1057. dct_tokens[ti++]=(unsigned char)token;
  1058. if(OC_DCT_TOKEN_NEEDS_MORE(token)){
  1059. eb=(int)oc_pack_read(&_dec->opb,
  1060. OC_INTERNAL_DCT_TOKEN_EXTRA_BITS[token]);
  1061. dct_tokens[ti++]=(unsigned char)eb;
  1062. if(token==OC_DCT_TOKEN_FAT_EOB)dct_tokens[ti++]=(unsigned char)(eb>>8);
  1063. eb<<=OC_DCT_TOKEN_EB_POS(token);
  1064. }
  1065. else eb=0;
  1066. cw=OC_DCT_CODE_WORD[token]+eb;
  1067. skip=(unsigned char)(cw>>OC_DCT_CW_RLEN_SHIFT);
  1068. _eobs=cw>>OC_DCT_CW_EOB_SHIFT&0xFFF;
  1069. if(cw==OC_DCT_CW_FINISH)_eobs=OC_DCT_EOB_FINISH;
  1070. if(_eobs==0){
  1071. run_counts[skip]++;
  1072. ntoks++;
  1073. }
  1074. }
  1075. /*Add the portion of the last EOB run actually used by this coefficient.*/
  1076. eob_count+=ntoks_left-ntoks;
  1077. /*And remove it from the remaining EOB count.*/
  1078. _eobs-=ntoks_left-ntoks;
  1079. /*Add the total EOB count to the longest run length.*/
  1080. run_counts[63]+=eob_count;
  1081. /*And convert the run_counts array to a moment table.*/
  1082. for(rli=63;rli-->0;)run_counts[rli]+=run_counts[rli+1];
  1083. /*Finally, subtract off the number of coefficients that have been
  1084. accounted for by runs started in this coefficient.*/
  1085. for(rli=64-_zzi;rli-->0;)_ntoks_left[pli][_zzi+rli]-=run_counts[rli];
  1086. }
  1087. _dec->dct_tokens_count=ti;
  1088. return _eobs;
  1089. }
  1090. /*Tokens describing the DCT coefficients that belong to each fragment are
  1091. stored in the bitstream grouped by coefficient, not by fragment.
  1092. This means that we either decode all the tokens in order, building up a
  1093. separate coefficient list for each fragment as we go, and then go back and
  1094. do the iDCT on each fragment, or we have to create separate lists of tokens
  1095. for each coefficient, so that we can pull the next token required off the
  1096. head of the appropriate list when decoding a specific fragment.
  1097. The former was VP3's choice, and it meant 2*w*h extra storage for all the
  1098. decoded coefficient values.
  1099. We take the second option, which lets us store just one to three bytes per
  1100. token (generally far fewer than the number of coefficients, due to EOB
  1101. tokens and zero runs), and which requires us to only maintain a counter for
  1102. each of the 64 coefficients, instead of a counter for every fragment to
  1103. determine where the next token goes.
  1104. We actually use 3 counters per coefficient, one for each color plane, so we
  1105. can decode all color planes simultaneously.
  1106. This lets color conversion, etc., be done as soon as a full MCU (one or
  1107. two super block rows) is decoded, while the image data is still in cache.*/
  1108. static void oc_dec_residual_tokens_unpack(oc_dec_ctx *_dec){
  1109. static const unsigned char OC_HUFF_LIST_MAX[5]={1,6,15,28,64};
  1110. ptrdiff_t ntoks_left[3][64];
  1111. int huff_idxs[2];
  1112. ptrdiff_t eobs;
  1113. long val;
  1114. int pli;
  1115. int zzi;
  1116. int hgi;
  1117. for(pli=0;pli<3;pli++)for(zzi=0;zzi<64;zzi++){
  1118. ntoks_left[pli][zzi]=_dec->state.ncoded_fragis[pli];
  1119. }
  1120. val=oc_pack_read(&_dec->opb,4);
  1121. huff_idxs[0]=(int)val;
  1122. val=oc_pack_read(&_dec->opb,4);
  1123. huff_idxs[1]=(int)val;
  1124. _dec->eob_runs[0][0]=0;
  1125. eobs=oc_dec_dc_coeff_unpack(_dec,huff_idxs,ntoks_left);
  1126. #if defined(HAVE_CAIRO)
  1127. _dec->telemetry_dc_bytes=oc_pack_bytes_left(&_dec->opb);
  1128. #endif
  1129. val=oc_pack_read(&_dec->opb,4);
  1130. huff_idxs[0]=(int)val;
  1131. val=oc_pack_read(&_dec->opb,4);
  1132. huff_idxs[1]=(int)val;
  1133. zzi=1;
  1134. for(hgi=1;hgi<5;hgi++){
  1135. huff_idxs[0]+=16;
  1136. huff_idxs[1]+=16;
  1137. for(;zzi<OC_HUFF_LIST_MAX[hgi];zzi++){
  1138. eobs=oc_dec_ac_coeff_unpack(_dec,zzi,huff_idxs,ntoks_left,eobs);
  1139. }
  1140. }
  1141. /*TODO: eobs should be exactly zero, or 4096 or greater.
  1142. The second case occurs when an EOB run of size zero is encountered, which
  1143. gets treated as an infinite EOB run (where infinity is PTRDIFF_MAX).
  1144. If neither of these conditions holds, then a warning should be issued.*/
  1145. }
  1146. static int oc_dec_postprocess_init(oc_dec_ctx *_dec){
  1147. /*musl libc malloc()/realloc() calls might use floating point, so make sure
  1148. we've cleared the MMX state for them.*/
  1149. oc_restore_fpu(&_dec->state);
  1150. /*pp_level 0: disabled; free any memory used and return*/
  1151. if(_dec->pp_level<=OC_PP_LEVEL_DISABLED){
  1152. if(_dec->dc_qis!=NULL){
  1153. _ogg_free(_dec->dc_qis);
  1154. _dec->dc_qis=NULL;
  1155. _ogg_free(_dec->variances);
  1156. _dec->variances=NULL;
  1157. _ogg_free(_dec->pp_frame_data);
  1158. _dec->pp_frame_data=NULL;
  1159. }
  1160. return 1;
  1161. }
  1162. if(_dec->dc_qis==NULL){
  1163. /*If we haven't been tracking DC quantization indices, there's no point in
  1164. starting now.*/
  1165. if(_dec->state.frame_type!=OC_INTRA_FRAME)return 1;
  1166. _dec->dc_qis=(unsigned char *)_ogg_malloc(
  1167. _dec->state.nfrags*sizeof(_dec->dc_qis[0]));
  1168. if(_dec->dc_qis==NULL)return 1;
  1169. memset(_dec->dc_qis,_dec->state.qis[0],_dec->state.nfrags);
  1170. }
  1171. else{
  1172. unsigned char *dc_qis;
  1173. const ptrdiff_t *coded_fragis;
  1174. ptrdiff_t ncoded_fragis;
  1175. ptrdiff_t fragii;
  1176. unsigned char qi0;
  1177. /*Update the DC quantization index of each coded block.*/
  1178. dc_qis=_dec->dc_qis;
  1179. coded_fragis=_dec->state.coded_fragis;
  1180. ncoded_fragis=_dec->state.ncoded_fragis[0]+
  1181. _dec->state.ncoded_fragis[1]+_dec->state.ncoded_fragis[2];
  1182. qi0=(unsigned char)_dec->state.qis[0];
  1183. for(fragii=0;fragii<ncoded_fragis;fragii++){
  1184. dc_qis[coded_fragis[fragii]]=qi0;
  1185. }
  1186. }
  1187. /*pp_level 1: Stop after updating DC quantization indices.*/
  1188. if(_dec->pp_level<=OC_PP_LEVEL_TRACKDCQI){
  1189. if(_dec->variances!=NULL){
  1190. _ogg_free(_dec->variances);
  1191. _dec->variances=NULL;
  1192. _ogg_free(_dec->pp_frame_data);
  1193. _dec->pp_frame_data=NULL;
  1194. }
  1195. return 1;
  1196. }
  1197. if(_dec->variances==NULL){
  1198. size_t frame_sz;
  1199. size_t c_sz;
  1200. int c_w;
  1201. int c_h;
  1202. frame_sz=_dec->state.info.frame_width*(size_t)_dec->state.info.frame_height;
  1203. c_w=_dec->state.info.frame_width>>!(_dec->state.info.pixel_fmt&1);
  1204. c_h=_dec->state.info.frame_height>>!(_dec->state.info.pixel_fmt&2);
  1205. c_sz=c_w*(size_t)c_h;
  1206. /*Allocate space for the chroma planes, even if we're not going to use
  1207. them; this simplifies allocation state management, though it may waste
  1208. memory on the few systems that don't overcommit pages.*/
  1209. frame_sz+=c_sz<<1;
  1210. _dec->pp_frame_data=(unsigned char *)_ogg_malloc(
  1211. frame_sz*sizeof(_dec->pp_frame_data[0]));
  1212. _dec->variances=(int *)_ogg_malloc(
  1213. _dec->state.nfrags*sizeof(_dec->variances[0]));
  1214. if(_dec->variances==NULL||_dec->pp_frame_data==NULL){
  1215. _ogg_free(_dec->pp_frame_data);
  1216. _dec->pp_frame_data=NULL;
  1217. _ogg_free(_dec->variances);
  1218. _dec->variances=NULL;
  1219. return 1;
  1220. }
  1221. /*Force an update of the PP buffer pointers.*/
  1222. _dec->pp_frame_state=0;
  1223. }
  1224. /*Update the PP buffer pointers if necessary.*/
  1225. if(_dec->pp_frame_state!=1+(_dec->pp_level>=OC_PP_LEVEL_DEBLOCKC)){
  1226. if(_dec->pp_level<OC_PP_LEVEL_DEBLOCKC){
  1227. /*If chroma processing is disabled, just use the PP luma plane.*/
  1228. _dec->pp_frame_buf[0].width=_dec->state.info.frame_width;
  1229. _dec->pp_frame_buf[0].height=_dec->state.info.frame_height;
  1230. _dec->pp_frame_buf[0].stride=-_dec->pp_frame_buf[0].width;
  1231. _dec->pp_frame_buf[0].data=_dec->pp_frame_data+
  1232. (1-_dec->pp_frame_buf[0].height)*(ptrdiff_t)_dec->pp_frame_buf[0].stride;
  1233. }
  1234. else{
  1235. size_t y_sz;
  1236. size_t c_sz;
  1237. int c_w;
  1238. int c_h;
  1239. /*Otherwise, set up pointers to all three PP planes.*/
  1240. y_sz=_dec->state.info.frame_width*(size_t)_dec->state.info.frame_height;
  1241. c_w=_dec->state.info.frame_width>>!(_dec->state.info.pixel_fmt&1);
  1242. c_h=_dec->state.info.frame_height>>!(_dec->state.info.pixel_fmt&2);
  1243. c_sz=c_w*(size_t)c_h;
  1244. _dec->pp_frame_buf[0].width=_dec->state.info.frame_width;
  1245. _dec->pp_frame_buf[0].height=_dec->state.info.frame_height;
  1246. _dec->pp_frame_buf[0].stride=_dec->pp_frame_buf[0].width;
  1247. _dec->pp_frame_buf[0].data=_dec->pp_frame_data;
  1248. _dec->pp_frame_buf[1].width=c_w;
  1249. _dec->pp_frame_buf[1].height=c_h;
  1250. _dec->pp_frame_buf[1].stride=_dec->pp_frame_buf[1].width;
  1251. _dec->pp_frame_buf[1].data=_dec->pp_frame_buf[0].data+y_sz;
  1252. _dec->pp_frame_buf[2].width=c_w;
  1253. _dec->pp_frame_buf[2].height=c_h;
  1254. _dec->pp_frame_buf[2].stride=_dec->pp_frame_buf[2].width;
  1255. _dec->pp_frame_buf[2].data=_dec->pp_frame_buf[1].data+c_sz;
  1256. oc_ycbcr_buffer_flip(_dec->pp_frame_buf,_dec->pp_frame_buf);
  1257. }
  1258. _dec->pp_frame_state=1+(_dec->pp_level>=OC_PP_LEVEL_DEBLOCKC);
  1259. }
  1260. /*If we're not processing chroma, copy the reference frame's chroma planes.*/
  1261. if(_dec->pp_level<OC_PP_LEVEL_DEBLOCKC){
  1262. memcpy(_dec->pp_frame_buf+1,
  1263. _dec->state.ref_frame_bufs[_dec->state.ref_frame_idx[OC_FRAME_SELF]]+1,
  1264. sizeof(_dec->pp_frame_buf[1])*2);
  1265. }
  1266. return 0;
  1267. }
  1268. /*Initialize the main decoding pipeline.*/
  1269. static void oc_dec_pipeline_init(oc_dec_ctx *_dec,
  1270. oc_dec_pipeline_state *_pipe){
  1271. const ptrdiff_t *coded_fragis;
  1272. const ptrdiff_t *uncoded_fragis;
  1273. int flimit;
  1274. int pli;
  1275. int qii;
  1276. int qti;
  1277. int zzi;
  1278. /*If chroma is sub-sampled in the vertical direction, we have to decode two
  1279. super block rows of Y' for each super block row of Cb and Cr.*/
  1280. _pipe->mcu_nvfrags=4<<!(_dec->state.info.pixel_fmt&2);
  1281. /*Initialize the token and extra bits indices for each plane and
  1282. coefficient.*/
  1283. memcpy(_pipe->ti,_dec->ti0,sizeof(_pipe->ti));
  1284. /*Also copy over the initial the EOB run counts.*/
  1285. memcpy(_pipe->eob_runs,_dec->eob_runs,sizeof(_pipe->eob_runs));
  1286. /*Set up per-plane pointers to the coded and uncoded fragments lists.*/
  1287. coded_fragis=_dec->state.coded_fragis;
  1288. uncoded_fragis=coded_fragis+_dec->state.nfrags;
  1289. for(pli=0;pli<3;pli++){
  1290. ptrdiff_t ncoded_fragis;
  1291. _pipe->coded_fragis[pli]=coded_fragis;
  1292. _pipe->uncoded_fragis[pli]=uncoded_fragis;
  1293. ncoded_fragis=_dec->state.ncoded_fragis[pli];
  1294. coded_fragis+=ncoded_fragis;
  1295. uncoded_fragis+=ncoded_fragis-_dec->state.fplanes[pli].nfrags;
  1296. }
  1297. /*Set up condensed quantizer tables.*/
  1298. for(pli=0;pli<3;pli++){
  1299. for(qii=0;qii<_dec->state.nqis;qii++){
  1300. for(qti=0;qti<2;qti++){
  1301. _pipe->dequant[pli][qii][qti]=
  1302. _dec->state.dequant_tables[_dec->state.qis[qii]][pli][qti];
  1303. }
  1304. }
  1305. }
  1306. /*Set the previous DC predictor to 0 for all color planes and frame types.*/
  1307. memset(_pipe->pred_last,0,sizeof(_pipe->pred_last));
  1308. /*Initialize the bounding value array for the loop filter.*/
  1309. flimit=_dec->state.loop_filter_limits[_dec->state.qis[0]];
  1310. _pipe->loop_filter=flimit!=0;
  1311. if(flimit!=0)oc_loop_filter_init(&_dec->state,_pipe->bounding_values,flimit);
  1312. /*Initialize any buffers needed for post-processing.
  1313. We also save the current post-processing level, to guard against the user
  1314. changing it from a callback.*/
  1315. if(!oc_dec_postprocess_init(_dec))_pipe->pp_level=_dec->pp_level;
  1316. /*If we don't have enough information to post-process, disable it, regardless
  1317. of the user-requested level.*/
  1318. else{
  1319. _pipe->pp_level=OC_PP_LEVEL_DISABLED;
  1320. memcpy(_dec->pp_frame_buf,
  1321. _dec->state.ref_frame_bufs[_dec->state.ref_frame_idx[OC_FRAME_SELF]],
  1322. sizeof(_dec->pp_frame_buf[0])*3);
  1323. }
  1324. /*Clear down the DCT coefficient buffer for the first block.*/
  1325. for(zzi=0;zzi<64;zzi++)_pipe->dct_coeffs[zzi]=0;
  1326. }
  1327. /*Undo the DC prediction in a single plane of an MCU (one or two super block
  1328. rows).
  1329. As a side effect, the number of coded and uncoded fragments in this plane of
  1330. the MCU is also computed.*/
  1331. void oc_dec_dc_unpredict_mcu_plane_c(oc_dec_ctx *_dec,
  1332. oc_dec_pipeline_state *_pipe,int _pli){
  1333. const oc_fragment_plane *fplane;
  1334. oc_fragment *frags;
  1335. int *pred_last;
  1336. ptrdiff_t ncoded_fragis;
  1337. ptrdiff_t fragi;
  1338. int fragx;
  1339. int fragy;
  1340. int fragy0;
  1341. int fragy_end;
  1342. int nhfrags;
  1343. /*Compute the first and last fragment row of the current MCU for this
  1344. plane.*/
  1345. fplane=_dec->state.fplanes+_pli;
  1346. fragy0=_pipe->fragy0[_pli];
  1347. fragy_end=_pipe->fragy_end[_pli];
  1348. nhfrags=fplane->nhfrags;
  1349. pred_last=_pipe->pred_last[_pli];
  1350. frags=_dec->state.frags;
  1351. ncoded_fragis=0;
  1352. fragi=fplane->froffset+fragy0*(ptrdiff_t)nhfrags;
  1353. for(fragy=fragy0;fragy<fragy_end;fragy++){
  1354. if(fragy==0){
  1355. /*For the first row, all of the cases reduce to just using the previous
  1356. predictor for the same reference frame.*/
  1357. for(fragx=0;fragx<nhfrags;fragx++,fragi++){
  1358. if(frags[fragi].coded){
  1359. int refi;
  1360. refi=frags[fragi].refi;
  1361. pred_last[refi]=frags[fragi].dc+=pred_last[refi];
  1362. ncoded_fragis++;
  1363. }
  1364. }
  1365. }
  1366. else{
  1367. oc_fragment *u_frags;
  1368. int l_ref;
  1369. int ul_ref;
  1370. int u_ref;
  1371. u_frags=frags-nhfrags;
  1372. l_ref=-1;
  1373. ul_ref=-1;
  1374. u_ref=u_frags[fragi].refi;
  1375. for(fragx=0;fragx<nhfrags;fragx++,fragi++){
  1376. int ur_ref;
  1377. if(fragx+1>=nhfrags)ur_ref=-1;
  1378. else ur_ref=u_frags[fragi+1].refi;
  1379. if(frags[fragi].coded){
  1380. int pred;
  1381. int refi;
  1382. refi=frags[fragi].refi;
  1383. /*We break out a separate case based on which of our neighbors use
  1384. the same reference frames.
  1385. This is somewhat faster than trying to make a generic case which
  1386. handles all of them, since it reduces lots of poorly predicted
  1387. jumps to one switch statement, and also lets a number of the
  1388. multiplications be optimized out by strength reduction.*/
  1389. switch((l_ref==refi)|(ul_ref==refi)<<1|
  1390. (u_ref==refi)<<2|(ur_ref==refi)<<3){
  1391. default:pred=pred_last[refi];break;
  1392. case 1:
  1393. case 3:pred=frags[fragi-1].dc;break;
  1394. case 2:pred=u_frags[fragi-1].dc;break;
  1395. case 4:
  1396. case 6:
  1397. case 12:pred=u_frags[fragi].dc;break;
  1398. case 5:pred=(frags[fragi-1].dc+u_frags[fragi].dc)/2;break;
  1399. case 8:pred=u_frags[fragi+1].dc;break;
  1400. case 9:
  1401. case 11:
  1402. case 13:{
  1403. /*The TI compiler mis-compiles this line.*/
  1404. pred=(75*frags[fragi-1].dc+53*u_frags[fragi+1].dc)/128;
  1405. }break;
  1406. case 10:pred=(u_frags[fragi-1].dc+u_frags[fragi+1].dc)/2;break;
  1407. case 14:{
  1408. pred=(3*(u_frags[fragi-1].dc+u_frags[fragi+1].dc)
  1409. +10*u_frags[fragi].dc)/16;
  1410. }break;
  1411. case 7:
  1412. case 15:{
  1413. int p0;
  1414. int p1;
  1415. int p2;
  1416. p0=frags[fragi-1].dc;
  1417. p1=u_frags[fragi-1].dc;
  1418. p2=u_frags[fragi].dc;
  1419. pred=(29*(p0+p2)-26*p1)/32;
  1420. if(abs(pred-p2)>128)pred=p2;
  1421. else if(abs(pred-p0)>128)pred=p0;
  1422. else if(abs(pred-p1)>128)pred=p1;
  1423. }break;
  1424. }
  1425. pred_last[refi]=frags[fragi].dc+=pred;
  1426. ncoded_fragis++;
  1427. l_ref=refi;
  1428. }
  1429. else l_ref=-1;
  1430. ul_ref=u_ref;
  1431. u_ref=ur_ref;
  1432. }
  1433. }
  1434. }
  1435. _pipe->ncoded_fragis[_pli]=ncoded_fragis;
  1436. /*Also save the number of uncoded fragments so we know how many to copy.*/
  1437. _pipe->nuncoded_fragis[_pli]=
  1438. (fragy_end-fragy0)*(ptrdiff_t)nhfrags-ncoded_fragis;
  1439. }
  1440. /*Reconstructs all coded fragments in a single MCU (one or two super block
  1441. rows).
  1442. This requires that each coded fragment have a proper macro block mode and
  1443. motion vector (if not in INTRA mode), and have its DC value decoded, with
  1444. the DC prediction process reversed, and the number of coded and uncoded
  1445. fragments in this plane of the MCU be counted.
  1446. The token lists for each color plane and coefficient should also be filled
  1447. in, along with initial token offsets, extra bits offsets, and EOB run
  1448. counts.*/
  1449. static void oc_dec_frags_recon_mcu_plane(oc_dec_ctx *_dec,
  1450. oc_dec_pipeline_state *_pipe,int _pli){
  1451. unsigned char *dct_tokens;
  1452. const unsigned char *dct_fzig_zag;
  1453. ogg_uint16_t dc_quant[2];
  1454. const oc_fragment *frags;
  1455. const ptrdiff_t *coded_fragis;
  1456. ptrdiff_t ncoded_fragis;
  1457. ptrdiff_t fragii;
  1458. ptrdiff_t *ti;
  1459. ptrdiff_t *eob_runs;
  1460. int qti;
  1461. dct_tokens=_dec->dct_tokens;
  1462. dct_fzig_zag=_dec->state.opt_data.dct_fzig_zag;
  1463. frags=_dec->state.frags;
  1464. coded_fragis=_pipe->coded_fragis[_pli];
  1465. ncoded_fragis=_pipe->ncoded_fragis[_pli];
  1466. ti=_pipe->ti[_pli];
  1467. eob_runs=_pipe->eob_runs[_pli];
  1468. for(qti=0;qti<2;qti++)dc_quant[qti]=_pipe->dequant[_pli][0][qti][0];
  1469. for(fragii=0;fragii<ncoded_fragis;fragii++){
  1470. const ogg_uint16_t *ac_quant;
  1471. ptrdiff_t fragi;
  1472. int last_zzi;
  1473. int zzi;
  1474. fragi=coded_fragis[fragii];
  1475. qti=frags[fragi].mb_mode!=OC_MODE_INTRA;
  1476. ac_quant=_pipe->dequant[_pli][frags[fragi].qii][qti];
  1477. /*Decode the AC coefficients.*/
  1478. for(zzi=0;zzi<64;){
  1479. int token;
  1480. last_zzi=zzi;
  1481. if(eob_runs[zzi]){
  1482. eob_runs[zzi]--;
  1483. break;
  1484. }
  1485. else{
  1486. ptrdiff_t eob;
  1487. int cw;
  1488. int rlen;
  1489. int coeff;
  1490. int lti;
  1491. lti=ti[zzi];
  1492. token=dct_tokens[lti++];
  1493. cw=OC_DCT_CODE_WORD[token];
  1494. /*These parts could be done branchless, but the branches are fairly
  1495. predictable and the C code translates into more than a few
  1496. instructions, so it's worth it to avoid them.*/
  1497. if(OC_DCT_TOKEN_NEEDS_MORE(token)){
  1498. cw+=dct_tokens[lti++]<<OC_DCT_TOKEN_EB_POS(token);
  1499. }
  1500. eob=cw>>OC_DCT_CW_EOB_SHIFT&0xFFF;
  1501. if(token==OC_DCT_TOKEN_FAT_EOB){
  1502. eob+=dct_tokens[lti++]<<8;
  1503. if(eob==0)eob=OC_DCT_EOB_FINISH;
  1504. }
  1505. rlen=(unsigned char)(cw>>OC_DCT_CW_RLEN_SHIFT);
  1506. cw^=-(cw&1<<OC_DCT_CW_FLIP_BIT);
  1507. coeff=cw>>OC_DCT_CW_MAG_SHIFT;
  1508. eob_runs[zzi]=eob;
  1509. ti[zzi]=lti;
  1510. zzi+=rlen;
  1511. _pipe->dct_coeffs[dct_fzig_zag[zzi]]=
  1512. (ogg_int16_t)(coeff*(int)ac_quant[zzi]);
  1513. zzi+=!eob;
  1514. }
  1515. }
  1516. /*TODO: zzi should be exactly 64 here.
  1517. If it's not, we should report some kind of warning.*/
  1518. zzi=OC_MINI(zzi,64);
  1519. _pipe->dct_coeffs[0]=(ogg_int16_t)frags[fragi].dc;
  1520. /*last_zzi is always initialized.
  1521. If your compiler thinks otherwise, it is dumb.*/
  1522. oc_state_frag_recon(&_dec->state,fragi,_pli,
  1523. _pipe->dct_coeffs,last_zzi,dc_quant[qti]);
  1524. }
  1525. _pipe->coded_fragis[_pli]+=ncoded_fragis;
  1526. /*Right now the reconstructed MCU has only the coded blocks in it.*/
  1527. /*TODO: We make the decision here to always copy the uncoded blocks into it
  1528. from the reference frame.
  1529. We could also copy the coded blocks back over the reference frame, if we
  1530. wait for an additional MCU to be decoded, which might be faster if only a
  1531. small number of blocks are coded.
  1532. However, this introduces more latency, creating a larger cache footprint.
  1533. It's unknown which decision is better, but this one results in simpler
  1534. code, and the hard case (high bitrate, high resolution) is handled
  1535. correctly.*/
  1536. /*Copy the uncoded blocks from the previous reference frame.*/
  1537. if(_pipe->nuncoded_fragis[_pli]>0){
  1538. _pipe->uncoded_fragis[_pli]-=_pipe->nuncoded_fragis[_pli];
  1539. oc_frag_copy_list(&_dec->state,
  1540. _dec->state.ref_frame_data[OC_FRAME_SELF],
  1541. _dec->state.ref_frame_data[OC_FRAME_PREV],
  1542. _dec->state.ref_ystride[_pli],_pipe->uncoded_fragis[_pli],
  1543. _pipe->nuncoded_fragis[_pli],_dec->state.frag_buf_offs);
  1544. }
  1545. }
  1546. /*Filter a horizontal block edge.*/
  1547. static void oc_filter_hedge(unsigned char *_dst,int _dst_ystride,
  1548. const unsigned char *_src,int _src_ystride,int _qstep,int _flimit,
  1549. int *_variance0,int *_variance1){
  1550. unsigned char *rdst;
  1551. const unsigned char *rsrc;
  1552. unsigned char *cdst;
  1553. const unsigned char *csrc;
  1554. int r[10];
  1555. int sum0;
  1556. int sum1;
  1557. int bx;
  1558. int by;
  1559. rdst=_dst;
  1560. rsrc=_src;
  1561. for(bx=0;bx<8;bx++){
  1562. cdst=rdst;
  1563. csrc=rsrc;
  1564. for(by=0;by<10;by++){
  1565. r[by]=*csrc;
  1566. csrc+=_src_ystride;
  1567. }
  1568. sum0=sum1=0;
  1569. for(by=0;by<4;by++){
  1570. sum0+=abs(r[by+1]-r[by]);
  1571. sum1+=abs(r[by+5]-r[by+6]);
  1572. }
  1573. *_variance0+=OC_MINI(255,sum0);
  1574. *_variance1+=OC_MINI(255,sum1);
  1575. if(sum0<_flimit&&sum1<_flimit&&r[5]-r[4]<_qstep&&r[4]-r[5]<_qstep){
  1576. *cdst=(unsigned char)(r[0]*3+r[1]*2+r[2]+r[3]+r[4]+4>>3);
  1577. cdst+=_dst_ystride;
  1578. *cdst=(unsigned char)(r[0]*2+r[1]+r[2]*2+r[3]+r[4]+r[5]+4>>3);
  1579. cdst+=_dst_ystride;
  1580. for(by=0;by<4;by++){
  1581. *cdst=(unsigned char)(r[by]+r[by+1]+r[by+2]+r[by+3]*2+
  1582. r[by+4]+r[by+5]+r[by+6]+4>>3);
  1583. cdst+=_dst_ystride;
  1584. }
  1585. *cdst=(unsigned char)(r[4]+r[5]+r[6]+r[7]*2+r[8]+r[9]*2+4>>3);
  1586. cdst+=_dst_ystride;
  1587. *cdst=(unsigned char)(r[5]+r[6]+r[7]+r[8]*2+r[9]*3+4>>3);
  1588. }
  1589. else{
  1590. for(by=1;by<=8;by++){
  1591. *cdst=(unsigned char)r[by];
  1592. cdst+=_dst_ystride;
  1593. }
  1594. }
  1595. rdst++;
  1596. rsrc++;
  1597. }
  1598. }
  1599. /*Filter a vertical block edge.*/
  1600. static void oc_filter_vedge(unsigned char *_dst,int _dst_ystride,
  1601. int _qstep,int _flimit,int *_variances){
  1602. unsigned char *rdst;
  1603. const unsigned char *rsrc;
  1604. unsigned char *cdst;
  1605. int r[10];
  1606. int sum0;
  1607. int sum1;
  1608. int bx;
  1609. int by;
  1610. cdst=_dst;
  1611. for(by=0;by<8;by++){
  1612. rsrc=cdst-1;
  1613. rdst=cdst;
  1614. for(bx=0;bx<10;bx++)r[bx]=*rsrc++;
  1615. sum0=sum1=0;
  1616. for(bx=0;bx<4;bx++){
  1617. sum0+=abs(r[bx+1]-r[bx]);
  1618. sum1+=abs(r[bx+5]-r[bx+6]);
  1619. }
  1620. _variances[0]+=OC_MINI(255,sum0);
  1621. _variances[1]+=OC_MINI(255,sum1);
  1622. if(sum0<_flimit&&sum1<_flimit&&r[5]-r[4]<_qstep&&r[4]-r[5]<_qstep){
  1623. *rdst++=(unsigned char)(r[0]*3+r[1]*2+r[2]+r[3]+r[4]+4>>3);
  1624. *rdst++=(unsigned char)(r[0]*2+r[1]+r[2]*2+r[3]+r[4]+r[5]+4>>3);
  1625. for(bx=0;bx<4;bx++){
  1626. *rdst++=(unsigned char)(r[bx]+r[bx+1]+r[bx+2]+r[bx+3]*2+
  1627. r[bx+4]+r[bx+5]+r[bx+6]+4>>3);
  1628. }
  1629. *rdst++=(unsigned char)(r[4]+r[5]+r[6]+r[7]*2+r[8]+r[9]*2+4>>3);
  1630. *rdst=(unsigned char)(r[5]+r[6]+r[7]+r[8]*2+r[9]*3+4>>3);
  1631. }
  1632. cdst+=_dst_ystride;
  1633. }
  1634. }
  1635. static void oc_dec_deblock_frag_rows(oc_dec_ctx *_dec,
  1636. th_img_plane *_dst,th_img_plane *_src,int _pli,int _fragy0,
  1637. int _fragy_end){
  1638. oc_fragment_plane *fplane;
  1639. int *variance;
  1640. unsigned char *dc_qi;
  1641. unsigned char *dst;
  1642. const unsigned char *src;
  1643. ptrdiff_t froffset;
  1644. int dst_ystride;
  1645. int src_ystride;
  1646. int nhfrags;
  1647. int width;
  1648. int notstart;
  1649. int notdone;
  1650. int flimit;
  1651. int qstep;
  1652. int y_end;
  1653. int y;
  1654. int x;
  1655. _dst+=_pli;
  1656. _src+=_pli;
  1657. fplane=_dec->state.fplanes+_pli;
  1658. nhfrags=fplane->nhfrags;
  1659. froffset=fplane->froffset+_fragy0*(ptrdiff_t)nhfrags;
  1660. variance=_dec->variances+froffset;
  1661. dc_qi=_dec->dc_qis+froffset;
  1662. notstart=_fragy0>0;
  1663. notdone=_fragy_end<fplane->nvfrags;
  1664. /*We want to clear an extra row of variances, except at the end.*/
  1665. memset(variance+(nhfrags&-notstart),0,
  1666. (_fragy_end+notdone-_fragy0-notstart)*(nhfrags*sizeof(variance[0])));
  1667. /*Except for the first time, we want to point to the middle of the row.*/
  1668. y=(_fragy0<<3)+(notstart<<2);
  1669. dst_ystride=_dst->stride;
  1670. src_ystride=_src->stride;
  1671. dst=_dst->data+y*(ptrdiff_t)dst_ystride;
  1672. src=_src->data+y*(ptrdiff_t)src_ystride;
  1673. width=_dst->width;
  1674. for(;y<4;y++){
  1675. memcpy(dst,src,width*sizeof(dst[0]));
  1676. dst+=dst_ystride;
  1677. src+=src_ystride;
  1678. }
  1679. /*We also want to skip the last row in the frame for this loop.*/
  1680. y_end=_fragy_end-!notdone<<3;
  1681. for(;y<y_end;y+=8){
  1682. qstep=_dec->pp_dc_scale[*dc_qi];
  1683. flimit=(qstep*3)>>2;
  1684. oc_filter_hedge(dst,dst_ystride,src-src_ystride,src_ystride,
  1685. qstep,flimit,variance,variance+nhfrags);
  1686. variance++;
  1687. dc_qi++;
  1688. for(x=8;x<width;x+=8){
  1689. qstep=_dec->pp_dc_scale[*dc_qi];
  1690. flimit=(qstep*3)>>2;
  1691. oc_filter_hedge(dst+x,dst_ystride,src+x-src_ystride,src_ystride,
  1692. qstep,flimit,variance,variance+nhfrags);
  1693. oc_filter_vedge(dst+x-(dst_ystride<<2)-4,dst_ystride,
  1694. qstep,flimit,variance-1);
  1695. variance++;
  1696. dc_qi++;
  1697. }
  1698. dst+=dst_ystride<<3;
  1699. src+=src_ystride<<3;
  1700. }
  1701. /*And finally, handle the last row in the frame, if it's in the range.*/
  1702. if(!notdone){
  1703. int height;
  1704. height=_dst->height;
  1705. for(;y<height;y++){
  1706. memcpy(dst,src,width*sizeof(dst[0]));
  1707. dst+=dst_ystride;
  1708. src+=src_ystride;
  1709. }
  1710. /*Filter the last row of vertical block edges.*/
  1711. dc_qi++;
  1712. for(x=8;x<width;x+=8){
  1713. qstep=_dec->pp_dc_scale[*dc_qi++];
  1714. flimit=(qstep*3)>>2;
  1715. oc_filter_vedge(dst+x-(dst_ystride<<3)-4,dst_ystride,
  1716. qstep,flimit,variance++);
  1717. }
  1718. }
  1719. }
  1720. static void oc_dering_block(unsigned char *_idata,int _ystride,int _b,
  1721. int _dc_scale,int _sharp_mod,int _strong){
  1722. static const unsigned char OC_MOD_MAX[2]={24,32};
  1723. static const unsigned char OC_MOD_SHIFT[2]={1,0};
  1724. const unsigned char *psrc;
  1725. const unsigned char *src;
  1726. const unsigned char *nsrc;
  1727. unsigned char *dst;
  1728. int vmod[72];
  1729. int hmod[72];
  1730. int mod_hi;
  1731. int by;
  1732. int bx;
  1733. mod_hi=OC_MINI(3*_dc_scale,OC_MOD_MAX[_strong]);
  1734. dst=_idata;
  1735. src=dst;
  1736. psrc=src-(_ystride&-!(_b&4));
  1737. for(by=0;by<9;by++){
  1738. for(bx=0;bx<8;bx++){
  1739. int mod;
  1740. mod=32+_dc_scale-(abs(src[bx]-psrc[bx])<<OC_MOD_SHIFT[_strong]);
  1741. vmod[(by<<3)+bx]=mod<-64?_sharp_mod:OC_CLAMPI(0,mod,mod_hi);
  1742. }
  1743. psrc=src;
  1744. src+=_ystride&-(!(_b&8)|by<7);
  1745. }
  1746. nsrc=dst;
  1747. psrc=dst-!(_b&1);
  1748. for(bx=0;bx<9;bx++){
  1749. src=nsrc;
  1750. for(by=0;by<8;by++){
  1751. int mod;
  1752. mod=32+_dc_scale-(abs(*src-*psrc)<<OC_MOD_SHIFT[_strong]);
  1753. hmod[(bx<<3)+by]=mod<-64?_sharp_mod:OC_CLAMPI(0,mod,mod_hi);
  1754. psrc+=_ystride;
  1755. src+=_ystride;
  1756. }
  1757. psrc=nsrc;
  1758. nsrc+=!(_b&2)|bx<7;
  1759. }
  1760. src=dst;
  1761. psrc=src-(_ystride&-!(_b&4));
  1762. nsrc=src+_ystride;
  1763. for(by=0;by<8;by++){
  1764. int a;
  1765. int b;
  1766. int w;
  1767. a=128;
  1768. b=64;
  1769. w=hmod[by];
  1770. a-=w;
  1771. b+=w**(src-!(_b&1));
  1772. w=vmod[by<<3];
  1773. a-=w;
  1774. b+=w*psrc[0];
  1775. w=vmod[by+1<<3];
  1776. a-=w;
  1777. b+=w*nsrc[0];
  1778. w=hmod[(1<<3)+by];
  1779. a-=w;
  1780. b+=w*src[1];
  1781. dst[0]=OC_CLAMP255(a*src[0]+b>>7);
  1782. for(bx=1;bx<7;bx++){
  1783. a=128;
  1784. b=64;
  1785. w=hmod[(bx<<3)+by];
  1786. a-=w;
  1787. b+=w*src[bx-1];
  1788. w=vmod[(by<<3)+bx];
  1789. a-=w;
  1790. b+=w*psrc[bx];
  1791. w=vmod[(by+1<<3)+bx];
  1792. a-=w;
  1793. b+=w*nsrc[bx];
  1794. w=hmod[(bx+1<<3)+by];
  1795. a-=w;
  1796. b+=w*src[bx+1];
  1797. dst[bx]=OC_CLAMP255(a*src[bx]+b>>7);
  1798. }
  1799. a=128;
  1800. b=64;
  1801. w=hmod[(7<<3)+by];
  1802. a-=w;
  1803. b+=w*src[6];
  1804. w=vmod[(by<<3)+7];
  1805. a-=w;
  1806. b+=w*psrc[7];
  1807. w=vmod[(by+1<<3)+7];
  1808. a-=w;
  1809. b+=w*nsrc[7];
  1810. w=hmod[(8<<3)+by];
  1811. a-=w;
  1812. b+=w*src[7+!(_b&2)];
  1813. dst[7]=OC_CLAMP255(a*src[7]+b>>7);
  1814. dst+=_ystride;
  1815. psrc=src;
  1816. src=nsrc;
  1817. nsrc+=_ystride&-(!(_b&8)|by<6);
  1818. }
  1819. }
  1820. #define OC_DERING_THRESH1 (384)
  1821. #define OC_DERING_THRESH2 (4*OC_DERING_THRESH1)
  1822. #define OC_DERING_THRESH3 (5*OC_DERING_THRESH1)
  1823. #define OC_DERING_THRESH4 (10*OC_DERING_THRESH1)
  1824. static void oc_dec_dering_frag_rows(oc_dec_ctx *_dec,th_img_plane *_img,
  1825. int _pli,int _fragy0,int _fragy_end){
  1826. th_img_plane *iplane;
  1827. oc_fragment_plane *fplane;
  1828. oc_fragment *frag;
  1829. int *variance;
  1830. unsigned char *idata;
  1831. ptrdiff_t froffset;
  1832. int ystride;
  1833. int nhfrags;
  1834. int sthresh;
  1835. int strong;
  1836. int y_end;
  1837. int width;
  1838. int height;
  1839. int y;
  1840. int x;
  1841. iplane=_img+_pli;
  1842. fplane=_dec->state.fplanes+_pli;
  1843. nhfrags=fplane->nhfrags;
  1844. froffset=fplane->froffset+_fragy0*(ptrdiff_t)nhfrags;
  1845. variance=_dec->variances+froffset;
  1846. frag=_dec->state.frags+froffset;
  1847. strong=_dec->pp_level>=(_pli?OC_PP_LEVEL_SDERINGC:OC_PP_LEVEL_SDERINGY);
  1848. sthresh=_pli?OC_DERING_THRESH4:OC_DERING_THRESH3;
  1849. y=_fragy0<<3;
  1850. ystride=iplane->stride;
  1851. idata=iplane->data+y*(ptrdiff_t)ystride;
  1852. y_end=_fragy_end<<3;
  1853. width=iplane->width;
  1854. height=iplane->height;
  1855. for(;y<y_end;y+=8){
  1856. for(x=0;x<width;x+=8){
  1857. int b;
  1858. int qi;
  1859. int var;
  1860. qi=_dec->state.qis[frag->qii];
  1861. var=*variance;
  1862. b=(x<=0)|(x+8>=width)<<1|(y<=0)<<2|(y+8>=height)<<3;
  1863. if(strong&&var>sthresh){
  1864. oc_dering_block(idata+x,ystride,b,
  1865. _dec->pp_dc_scale[qi],_dec->pp_sharp_mod[qi],1);
  1866. if(_pli||!(b&1)&&*(variance-1)>OC_DERING_THRESH4||
  1867. !(b&2)&&variance[1]>OC_DERING_THRESH4||
  1868. !(b&4)&&*(variance-nhfrags)>OC_DERING_THRESH4||
  1869. !(b&8)&&variance[nhfrags]>OC_DERING_THRESH4){
  1870. oc_dering_block(idata+x,ystride,b,
  1871. _dec->pp_dc_scale[qi],_dec->pp_sharp_mod[qi],1);
  1872. oc_dering_block(idata+x,ystride,b,
  1873. _dec->pp_dc_scale[qi],_dec->pp_sharp_mod[qi],1);
  1874. }
  1875. }
  1876. else if(var>OC_DERING_THRESH2){
  1877. oc_dering_block(idata+x,ystride,b,
  1878. _dec->pp_dc_scale[qi],_dec->pp_sharp_mod[qi],1);
  1879. }
  1880. else if(var>OC_DERING_THRESH1){
  1881. oc_dering_block(idata+x,ystride,b,
  1882. _dec->pp_dc_scale[qi],_dec->pp_sharp_mod[qi],0);
  1883. }
  1884. frag++;
  1885. variance++;
  1886. }
  1887. idata+=ystride<<3;
  1888. }
  1889. }
  1890. th_dec_ctx *th_decode_alloc(const th_info *_info,const th_setup_info *_setup){
  1891. oc_dec_ctx *dec;
  1892. if(_info==NULL||_setup==NULL)return NULL;
  1893. dec=oc_aligned_malloc(sizeof(*dec),16);
  1894. if(dec==NULL||oc_dec_init(dec,_info,_setup)<0){
  1895. oc_aligned_free(dec);
  1896. return NULL;
  1897. }
  1898. dec->state.curframe_num=0;
  1899. return dec;
  1900. }
  1901. void th_decode_free(th_dec_ctx *_dec){
  1902. if(_dec!=NULL){
  1903. oc_dec_clear(_dec);
  1904. oc_aligned_free(_dec);
  1905. }
  1906. }
  1907. int th_decode_ctl(th_dec_ctx *_dec,int _req,void *_buf,
  1908. size_t _buf_sz){
  1909. switch(_req){
  1910. case TH_DECCTL_GET_PPLEVEL_MAX:{
  1911. if(_dec==NULL||_buf==NULL)return TH_EFAULT;
  1912. if(_buf_sz!=sizeof(int))return TH_EINVAL;
  1913. (*(int *)_buf)=OC_PP_LEVEL_MAX;
  1914. return 0;
  1915. }break;
  1916. case TH_DECCTL_SET_PPLEVEL:{
  1917. int pp_level;
  1918. if(_dec==NULL||_buf==NULL)return TH_EFAULT;
  1919. if(_buf_sz!=sizeof(int))return TH_EINVAL;
  1920. pp_level=*(int *)_buf;
  1921. if(pp_level<0||pp_level>OC_PP_LEVEL_MAX)return TH_EINVAL;
  1922. _dec->pp_level=pp_level;
  1923. return 0;
  1924. }break;
  1925. case TH_DECCTL_SET_GRANPOS:{
  1926. ogg_int64_t granpos;
  1927. if(_dec==NULL||_buf==NULL)return TH_EFAULT;
  1928. if(_buf_sz!=sizeof(ogg_int64_t))return TH_EINVAL;
  1929. granpos=*(ogg_int64_t *)_buf;
  1930. if(granpos<0)return TH_EINVAL;
  1931. _dec->state.granpos=granpos;
  1932. _dec->state.keyframe_num=(granpos>>_dec->state.info.keyframe_granule_shift)
  1933. -_dec->state.granpos_bias;
  1934. _dec->state.curframe_num=_dec->state.keyframe_num
  1935. +(granpos&(1<<_dec->state.info.keyframe_granule_shift)-1);
  1936. return 0;
  1937. }break;
  1938. case TH_DECCTL_SET_STRIPE_CB:{
  1939. th_stripe_callback *cb;
  1940. if(_dec==NULL||_buf==NULL)return TH_EFAULT;
  1941. if(_buf_sz!=sizeof(th_stripe_callback))return TH_EINVAL;
  1942. cb=(th_stripe_callback *)_buf;
  1943. _dec->stripe_cb.ctx=cb->ctx;
  1944. _dec->stripe_cb.stripe_decoded=cb->stripe_decoded;
  1945. return 0;
  1946. }break;
  1947. #ifdef HAVE_CAIRO
  1948. case TH_DECCTL_SET_TELEMETRY_MBMODE:{
  1949. if(_dec==NULL||_buf==NULL)return TH_EFAULT;
  1950. if(_buf_sz!=sizeof(int))return TH_EINVAL;
  1951. _dec->telemetry_mbmode=*(int *)_buf;
  1952. return 0;
  1953. }break;
  1954. case TH_DECCTL_SET_TELEMETRY_MV:{
  1955. if(_dec==NULL||_buf==NULL)return TH_EFAULT;
  1956. if(_buf_sz!=sizeof(int))return TH_EINVAL;
  1957. _dec->telemetry_mv=*(int *)_buf;
  1958. return 0;
  1959. }break;
  1960. case TH_DECCTL_SET_TELEMETRY_QI:{
  1961. if(_dec==NULL||_buf==NULL)return TH_EFAULT;
  1962. if(_buf_sz!=sizeof(int))return TH_EINVAL;
  1963. _dec->telemetry_qi=*(int *)_buf;
  1964. return 0;
  1965. }break;
  1966. case TH_DECCTL_SET_TELEMETRY_BITS:{
  1967. if(_dec==NULL||_buf==NULL)return TH_EFAULT;
  1968. if(_buf_sz!=sizeof(int))return TH_EINVAL;
  1969. _dec->telemetry_bits=*(int *)_buf;
  1970. return 0;
  1971. }break;
  1972. #endif
  1973. default:return TH_EIMPL;
  1974. }
  1975. }
  1976. /*We're decoding an INTER frame, but have no initialized reference
  1977. buffers (i.e., decoding did not start on a key frame).
  1978. We initialize them to a solid gray here.*/
  1979. static void oc_dec_init_dummy_frame(th_dec_ctx *_dec){
  1980. th_info *info;
  1981. size_t yplane_sz;
  1982. size_t cplane_sz;
  1983. ptrdiff_t yoffset;
  1984. int yhstride;
  1985. int yheight;
  1986. int chstride;
  1987. int cheight;
  1988. _dec->state.ref_frame_idx[OC_FRAME_GOLD]=0;
  1989. _dec->state.ref_frame_idx[OC_FRAME_PREV]=0;
  1990. _dec->state.ref_frame_idx[OC_FRAME_SELF]=0;
  1991. _dec->state.ref_frame_data[OC_FRAME_GOLD]=
  1992. _dec->state.ref_frame_data[OC_FRAME_PREV]=
  1993. _dec->state.ref_frame_data[OC_FRAME_SELF]=
  1994. _dec->state.ref_frame_bufs[0][0].data;
  1995. memcpy(_dec->pp_frame_buf,_dec->state.ref_frame_bufs[0],
  1996. sizeof(_dec->pp_frame_buf[0])*3);
  1997. info=&_dec->state.info;
  1998. yhstride=abs(_dec->state.ref_ystride[0]);
  1999. yheight=info->frame_height+2*OC_UMV_PADDING;
  2000. chstride=abs(_dec->state.ref_ystride[1]);
  2001. cheight=yheight>>!(info->pixel_fmt&2);
  2002. yplane_sz=yhstride*(size_t)yheight+16;
  2003. cplane_sz=chstride*(size_t)cheight;
  2004. yoffset=yhstride*(ptrdiff_t)(yheight-OC_UMV_PADDING-1)+OC_UMV_PADDING;
  2005. memset(_dec->state.ref_frame_data[0]-yoffset,0x80,yplane_sz+2*cplane_sz);
  2006. }
  2007. #if defined(HAVE_CAIRO)
  2008. static void oc_render_telemetry(th_dec_ctx *_dec,th_ycbcr_buffer _ycbcr,
  2009. int _telemetry){
  2010. /*Stuff the plane into cairo.*/
  2011. cairo_surface_t *cs;
  2012. unsigned char *data;
  2013. unsigned char *y_row;
  2014. unsigned char *u_row;
  2015. unsigned char *v_row;
  2016. unsigned char *rgb_row;
  2017. int cstride;
  2018. int w;
  2019. int h;
  2020. int x;
  2021. int y;
  2022. int hdec;
  2023. int vdec;
  2024. w=_ycbcr[0].width;
  2025. h=_ycbcr[0].height;
  2026. hdec=!(_dec->state.info.pixel_fmt&1);
  2027. vdec=!(_dec->state.info.pixel_fmt&2);
  2028. /*Lazy data buffer init.
  2029. We could try to re-use the post-processing buffer, which would save
  2030. memory, but complicate the allocation logic there.
  2031. I don't think anyone cares about memory usage when using telemetry; it is
  2032. not meant for embedded devices.*/
  2033. if(_dec->telemetry_frame_data==NULL){
  2034. _dec->telemetry_frame_data=_ogg_malloc(
  2035. (w*h+2*(w>>hdec)*(h>>vdec))*sizeof(*_dec->telemetry_frame_data));
  2036. if(_dec->telemetry_frame_data==NULL)return;
  2037. }
  2038. cs=cairo_image_surface_create(CAIRO_FORMAT_RGB24,w,h);
  2039. /*Sadly, no YUV support in Cairo (yet); convert into the RGB buffer.*/
  2040. data=cairo_image_surface_get_data(cs);
  2041. if(data==NULL){
  2042. cairo_surface_destroy(cs);
  2043. return;
  2044. }
  2045. cstride=cairo_image_surface_get_stride(cs);
  2046. y_row=_ycbcr[0].data;
  2047. u_row=_ycbcr[1].data;
  2048. v_row=_ycbcr[2].data;
  2049. rgb_row=data;
  2050. for(y=0;y<h;y++){
  2051. for(x=0;x<w;x++){
  2052. int r;
  2053. int g;
  2054. int b;
  2055. r=(1904000*y_row[x]+2609823*v_row[x>>hdec]-363703744)/1635200;
  2056. g=(3827562*y_row[x]-1287801*u_row[x>>hdec]
  2057. -2672387*v_row[x>>hdec]+447306710)/3287200;
  2058. b=(952000*y_row[x]+1649289*u_row[x>>hdec]-225932192)/817600;
  2059. rgb_row[4*x+0]=OC_CLAMP255(b);
  2060. rgb_row[4*x+1]=OC_CLAMP255(g);
  2061. rgb_row[4*x+2]=OC_CLAMP255(r);
  2062. }
  2063. y_row+=_ycbcr[0].stride;
  2064. u_row+=_ycbcr[1].stride&-((y&1)|!vdec);
  2065. v_row+=_ycbcr[2].stride&-((y&1)|!vdec);
  2066. rgb_row+=cstride;
  2067. }
  2068. /*Draw coded identifier for each macroblock (stored in Hilbert order).*/
  2069. {
  2070. cairo_t *c;
  2071. const oc_fragment *frags;
  2072. oc_mv *frag_mvs;
  2073. const signed char *mb_modes;
  2074. oc_mb_map *mb_maps;
  2075. size_t nmbs;
  2076. size_t mbi;
  2077. int row2;
  2078. int col2;
  2079. int qim[3]={0,0,0};
  2080. if(_dec->state.nqis==2){
  2081. int bqi;
  2082. bqi=_dec->state.qis[0];
  2083. if(_dec->state.qis[1]>bqi)qim[1]=1;
  2084. if(_dec->state.qis[1]<bqi)qim[1]=-1;
  2085. }
  2086. if(_dec->state.nqis==3){
  2087. int bqi;
  2088. int cqi;
  2089. int dqi;
  2090. bqi=_dec->state.qis[0];
  2091. cqi=_dec->state.qis[1];
  2092. dqi=_dec->state.qis[2];
  2093. if(cqi>bqi&&dqi>bqi){
  2094. if(dqi>cqi){
  2095. qim[1]=1;
  2096. qim[2]=2;
  2097. }
  2098. else{
  2099. qim[1]=2;
  2100. qim[2]=1;
  2101. }
  2102. }
  2103. else if(cqi<bqi&&dqi<bqi){
  2104. if(dqi<cqi){
  2105. qim[1]=-1;
  2106. qim[2]=-2;
  2107. }
  2108. else{
  2109. qim[1]=-2;
  2110. qim[2]=-1;
  2111. }
  2112. }
  2113. else{
  2114. if(cqi<bqi)qim[1]=-1;
  2115. else qim[1]=1;
  2116. if(dqi<bqi)qim[2]=-1;
  2117. else qim[2]=1;
  2118. }
  2119. }
  2120. c=cairo_create(cs);
  2121. frags=_dec->state.frags;
  2122. frag_mvs=_dec->state.frag_mvs;
  2123. mb_modes=_dec->state.mb_modes;
  2124. mb_maps=_dec->state.mb_maps;
  2125. nmbs=_dec->state.nmbs;
  2126. row2=0;
  2127. col2=0;
  2128. for(mbi=0;mbi<nmbs;mbi++){
  2129. float x;
  2130. float y;
  2131. int bi;
  2132. y=h-(row2+((col2+1>>1)&1))*16-16;
  2133. x=(col2>>1)*16;
  2134. cairo_set_line_width(c,1.);
  2135. /*Keyframe (all intra) red box.*/
  2136. if(_dec->state.frame_type==OC_INTRA_FRAME){
  2137. if(_dec->telemetry_mbmode&0x02){
  2138. cairo_set_source_rgba(c,1.,0,0,.5);
  2139. cairo_rectangle(c,x+2.5,y+2.5,11,11);
  2140. cairo_stroke_preserve(c);
  2141. cairo_set_source_rgba(c,1.,0,0,.25);
  2142. cairo_fill(c);
  2143. }
  2144. }
  2145. else{
  2146. ptrdiff_t fragi;
  2147. int frag_mvx;
  2148. int frag_mvy;
  2149. for(bi=0;bi<4;bi++){
  2150. fragi=mb_maps[mbi][0][bi];
  2151. if(fragi>=0&&frags[fragi].coded){
  2152. frag_mvx=OC_MV_X(frag_mvs[fragi]);
  2153. frag_mvy=OC_MV_Y(frag_mvs[fragi]);
  2154. break;
  2155. }
  2156. }
  2157. if(bi<4){
  2158. switch(mb_modes[mbi]){
  2159. case OC_MODE_INTRA:{
  2160. if(_dec->telemetry_mbmode&0x02){
  2161. cairo_set_source_rgba(c,1.,0,0,.5);
  2162. cairo_rectangle(c,x+2.5,y+2.5,11,11);
  2163. cairo_stroke_preserve(c);
  2164. cairo_set_source_rgba(c,1.,0,0,.25);
  2165. cairo_fill(c);
  2166. }
  2167. }break;
  2168. case OC_MODE_INTER_NOMV:{
  2169. if(_dec->telemetry_mbmode&0x01){
  2170. cairo_set_source_rgba(c,0,0,1.,.5);
  2171. cairo_rectangle(c,x+2.5,y+2.5,11,11);
  2172. cairo_stroke_preserve(c);
  2173. cairo_set_source_rgba(c,0,0,1.,.25);
  2174. cairo_fill(c);
  2175. }
  2176. }break;
  2177. case OC_MODE_INTER_MV:{
  2178. if(_dec->telemetry_mbmode&0x04){
  2179. cairo_rectangle(c,x+2.5,y+2.5,11,11);
  2180. cairo_set_source_rgba(c,0,1.,0,.5);
  2181. cairo_stroke(c);
  2182. }
  2183. if(_dec->telemetry_mv&0x04){
  2184. cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
  2185. cairo_set_source_rgba(c,1.,1.,1.,.9);
  2186. cairo_set_line_width(c,3.);
  2187. cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
  2188. cairo_stroke_preserve(c);
  2189. cairo_set_line_width(c,2.);
  2190. cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
  2191. cairo_stroke_preserve(c);
  2192. cairo_set_line_width(c,1.);
  2193. cairo_line_to(c,x+8,y+8);
  2194. cairo_stroke(c);
  2195. }
  2196. }break;
  2197. case OC_MODE_INTER_MV_LAST:{
  2198. if(_dec->telemetry_mbmode&0x08){
  2199. cairo_rectangle(c,x+2.5,y+2.5,11,11);
  2200. cairo_set_source_rgba(c,0,1.,0,.5);
  2201. cairo_move_to(c,x+13.5,y+2.5);
  2202. cairo_line_to(c,x+2.5,y+8);
  2203. cairo_line_to(c,x+13.5,y+13.5);
  2204. cairo_stroke(c);
  2205. }
  2206. if(_dec->telemetry_mv&0x08){
  2207. cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
  2208. cairo_set_source_rgba(c,1.,1.,1.,.9);
  2209. cairo_set_line_width(c,3.);
  2210. cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
  2211. cairo_stroke_preserve(c);
  2212. cairo_set_line_width(c,2.);
  2213. cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
  2214. cairo_stroke_preserve(c);
  2215. cairo_set_line_width(c,1.);
  2216. cairo_line_to(c,x+8,y+8);
  2217. cairo_stroke(c);
  2218. }
  2219. }break;
  2220. case OC_MODE_INTER_MV_LAST2:{
  2221. if(_dec->telemetry_mbmode&0x10){
  2222. cairo_rectangle(c,x+2.5,y+2.5,11,11);
  2223. cairo_set_source_rgba(c,0,1.,0,.5);
  2224. cairo_move_to(c,x+8,y+2.5);
  2225. cairo_line_to(c,x+2.5,y+8);
  2226. cairo_line_to(c,x+8,y+13.5);
  2227. cairo_move_to(c,x+13.5,y+2.5);
  2228. cairo_line_to(c,x+8,y+8);
  2229. cairo_line_to(c,x+13.5,y+13.5);
  2230. cairo_stroke(c);
  2231. }
  2232. if(_dec->telemetry_mv&0x10){
  2233. cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
  2234. cairo_set_source_rgba(c,1.,1.,1.,.9);
  2235. cairo_set_line_width(c,3.);
  2236. cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
  2237. cairo_stroke_preserve(c);
  2238. cairo_set_line_width(c,2.);
  2239. cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
  2240. cairo_stroke_preserve(c);
  2241. cairo_set_line_width(c,1.);
  2242. cairo_line_to(c,x+8,y+8);
  2243. cairo_stroke(c);
  2244. }
  2245. }break;
  2246. case OC_MODE_GOLDEN_NOMV:{
  2247. if(_dec->telemetry_mbmode&0x20){
  2248. cairo_set_source_rgba(c,1.,1.,0,.5);
  2249. cairo_rectangle(c,x+2.5,y+2.5,11,11);
  2250. cairo_stroke_preserve(c);
  2251. cairo_set_source_rgba(c,1.,1.,0,.25);
  2252. cairo_fill(c);
  2253. }
  2254. }break;
  2255. case OC_MODE_GOLDEN_MV:{
  2256. if(_dec->telemetry_mbmode&0x40){
  2257. cairo_rectangle(c,x+2.5,y+2.5,11,11);
  2258. cairo_set_source_rgba(c,1.,1.,0,.5);
  2259. cairo_stroke(c);
  2260. }
  2261. if(_dec->telemetry_mv&0x40){
  2262. cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
  2263. cairo_set_source_rgba(c,1.,1.,1.,.9);
  2264. cairo_set_line_width(c,3.);
  2265. cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
  2266. cairo_stroke_preserve(c);
  2267. cairo_set_line_width(c,2.);
  2268. cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
  2269. cairo_stroke_preserve(c);
  2270. cairo_set_line_width(c,1.);
  2271. cairo_line_to(c,x+8,y+8);
  2272. cairo_stroke(c);
  2273. }
  2274. }break;
  2275. case OC_MODE_INTER_MV_FOUR:{
  2276. if(_dec->telemetry_mbmode&0x80){
  2277. cairo_rectangle(c,x+2.5,y+2.5,4,4);
  2278. cairo_rectangle(c,x+9.5,y+2.5,4,4);
  2279. cairo_rectangle(c,x+2.5,y+9.5,4,4);
  2280. cairo_rectangle(c,x+9.5,y+9.5,4,4);
  2281. cairo_set_source_rgba(c,0,1.,0,.5);
  2282. cairo_stroke(c);
  2283. }
  2284. /*4mv is odd, coded in raster order.*/
  2285. fragi=mb_maps[mbi][0][0];
  2286. if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
  2287. frag_mvx=OC_MV_X(frag_mvs[fragi]);
  2288. frag_mvx=OC_MV_Y(frag_mvs[fragi]);
  2289. cairo_move_to(c,x+4+frag_mvx,y+12-frag_mvy);
  2290. cairo_set_source_rgba(c,1.,1.,1.,.9);
  2291. cairo_set_line_width(c,3.);
  2292. cairo_line_to(c,x+4+frag_mvx*.66,y+12-frag_mvy*.66);
  2293. cairo_stroke_preserve(c);
  2294. cairo_set_line_width(c,2.);
  2295. cairo_line_to(c,x+4+frag_mvx*.33,y+12-frag_mvy*.33);
  2296. cairo_stroke_preserve(c);
  2297. cairo_set_line_width(c,1.);
  2298. cairo_line_to(c,x+4,y+12);
  2299. cairo_stroke(c);
  2300. }
  2301. fragi=mb_maps[mbi][0][1];
  2302. if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
  2303. frag_mvx=OC_MV_X(frag_mvs[fragi]);
  2304. frag_mvx=OC_MV_Y(frag_mvs[fragi]);
  2305. cairo_move_to(c,x+12+frag_mvx,y+12-frag_mvy);
  2306. cairo_set_source_rgba(c,1.,1.,1.,.9);
  2307. cairo_set_line_width(c,3.);
  2308. cairo_line_to(c,x+12+frag_mvx*.66,y+12-frag_mvy*.66);
  2309. cairo_stroke_preserve(c);
  2310. cairo_set_line_width(c,2.);
  2311. cairo_line_to(c,x+12+frag_mvx*.33,y+12-frag_mvy*.33);
  2312. cairo_stroke_preserve(c);
  2313. cairo_set_line_width(c,1.);
  2314. cairo_line_to(c,x+12,y+12);
  2315. cairo_stroke(c);
  2316. }
  2317. fragi=mb_maps[mbi][0][2];
  2318. if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
  2319. frag_mvx=OC_MV_X(frag_mvs[fragi]);
  2320. frag_mvx=OC_MV_Y(frag_mvs[fragi]);
  2321. cairo_move_to(c,x+4+frag_mvx,y+4-frag_mvy);
  2322. cairo_set_source_rgba(c,1.,1.,1.,.9);
  2323. cairo_set_line_width(c,3.);
  2324. cairo_line_to(c,x+4+frag_mvx*.66,y+4-frag_mvy*.66);
  2325. cairo_stroke_preserve(c);
  2326. cairo_set_line_width(c,2.);
  2327. cairo_line_to(c,x+4+frag_mvx*.33,y+4-frag_mvy*.33);
  2328. cairo_stroke_preserve(c);
  2329. cairo_set_line_width(c,1.);
  2330. cairo_line_to(c,x+4,y+4);
  2331. cairo_stroke(c);
  2332. }
  2333. fragi=mb_maps[mbi][0][3];
  2334. if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
  2335. frag_mvx=OC_MV_X(frag_mvs[fragi]);
  2336. frag_mvx=OC_MV_Y(frag_mvs[fragi]);
  2337. cairo_move_to(c,x+12+frag_mvx,y+4-frag_mvy);
  2338. cairo_set_source_rgba(c,1.,1.,1.,.9);
  2339. cairo_set_line_width(c,3.);
  2340. cairo_line_to(c,x+12+frag_mvx*.66,y+4-frag_mvy*.66);
  2341. cairo_stroke_preserve(c);
  2342. cairo_set_line_width(c,2.);
  2343. cairo_line_to(c,x+12+frag_mvx*.33,y+4-frag_mvy*.33);
  2344. cairo_stroke_preserve(c);
  2345. cairo_set_line_width(c,1.);
  2346. cairo_line_to(c,x+12,y+4);
  2347. cairo_stroke(c);
  2348. }
  2349. }break;
  2350. }
  2351. }
  2352. }
  2353. /*qii illustration.*/
  2354. if(_dec->telemetry_qi&0x2){
  2355. cairo_set_line_cap(c,CAIRO_LINE_CAP_SQUARE);
  2356. for(bi=0;bi<4;bi++){
  2357. ptrdiff_t fragi;
  2358. int qiv;
  2359. int xp;
  2360. int yp;
  2361. xp=x+(bi&1)*8;
  2362. yp=y+8-(bi&2)*4;
  2363. fragi=mb_maps[mbi][0][bi];
  2364. if(fragi>=0&&frags[fragi].coded){
  2365. qiv=qim[frags[fragi].qii];
  2366. cairo_set_line_width(c,3.);
  2367. cairo_set_source_rgba(c,0.,0.,0.,.5);
  2368. switch(qiv){
  2369. /*Double plus:*/
  2370. case 2:{
  2371. if((bi&1)^((bi&2)>>1)){
  2372. cairo_move_to(c,xp+2.5,yp+1.5);
  2373. cairo_line_to(c,xp+2.5,yp+3.5);
  2374. cairo_move_to(c,xp+1.5,yp+2.5);
  2375. cairo_line_to(c,xp+3.5,yp+2.5);
  2376. cairo_move_to(c,xp+5.5,yp+4.5);
  2377. cairo_line_to(c,xp+5.5,yp+6.5);
  2378. cairo_move_to(c,xp+4.5,yp+5.5);
  2379. cairo_line_to(c,xp+6.5,yp+5.5);
  2380. cairo_stroke_preserve(c);
  2381. cairo_set_source_rgba(c,0.,1.,1.,1.);
  2382. }
  2383. else{
  2384. cairo_move_to(c,xp+5.5,yp+1.5);
  2385. cairo_line_to(c,xp+5.5,yp+3.5);
  2386. cairo_move_to(c,xp+4.5,yp+2.5);
  2387. cairo_line_to(c,xp+6.5,yp+2.5);
  2388. cairo_move_to(c,xp+2.5,yp+4.5);
  2389. cairo_line_to(c,xp+2.5,yp+6.5);
  2390. cairo_move_to(c,xp+1.5,yp+5.5);
  2391. cairo_line_to(c,xp+3.5,yp+5.5);
  2392. cairo_stroke_preserve(c);
  2393. cairo_set_source_rgba(c,0.,1.,1.,1.);
  2394. }
  2395. }break;
  2396. /*Double minus:*/
  2397. case -2:{
  2398. cairo_move_to(c,xp+2.5,yp+2.5);
  2399. cairo_line_to(c,xp+5.5,yp+2.5);
  2400. cairo_move_to(c,xp+2.5,yp+5.5);
  2401. cairo_line_to(c,xp+5.5,yp+5.5);
  2402. cairo_stroke_preserve(c);
  2403. cairo_set_source_rgba(c,1.,1.,1.,1.);
  2404. }break;
  2405. /*Plus:*/
  2406. case 1:{
  2407. if((bi&2)==0)yp-=2;
  2408. if((bi&1)==0)xp-=2;
  2409. cairo_move_to(c,xp+4.5,yp+2.5);
  2410. cairo_line_to(c,xp+4.5,yp+6.5);
  2411. cairo_move_to(c,xp+2.5,yp+4.5);
  2412. cairo_line_to(c,xp+6.5,yp+4.5);
  2413. cairo_stroke_preserve(c);
  2414. cairo_set_source_rgba(c,.1,1.,.3,1.);
  2415. break;
  2416. }
  2417. /*Fall through.*/
  2418. /*Minus:*/
  2419. case -1:{
  2420. cairo_move_to(c,xp+2.5,yp+4.5);
  2421. cairo_line_to(c,xp+6.5,yp+4.5);
  2422. cairo_stroke_preserve(c);
  2423. cairo_set_source_rgba(c,1.,.3,.1,1.);
  2424. }break;
  2425. default:continue;
  2426. }
  2427. cairo_set_line_width(c,1.);
  2428. cairo_stroke(c);
  2429. }
  2430. }
  2431. }
  2432. col2++;
  2433. if((col2>>1)>=_dec->state.nhmbs){
  2434. col2=0;
  2435. row2+=2;
  2436. }
  2437. }
  2438. /*Bit usage indicator[s]:*/
  2439. if(_dec->telemetry_bits){
  2440. int widths[6];
  2441. int fpsn;
  2442. int fpsd;
  2443. int mult;
  2444. int fullw;
  2445. int padw;
  2446. int i;
  2447. fpsn=_dec->state.info.fps_numerator;
  2448. fpsd=_dec->state.info.fps_denominator;
  2449. mult=(_dec->telemetry_bits>=0xFF?1:_dec->telemetry_bits);
  2450. fullw=250.f*h*fpsd*mult/fpsn;
  2451. padw=w-24;
  2452. /*Header and coded block bits.*/
  2453. if(_dec->telemetry_frame_bytes<0||
  2454. _dec->telemetry_frame_bytes==OC_LOTS_OF_BITS){
  2455. _dec->telemetry_frame_bytes=0;
  2456. }
  2457. if(_dec->telemetry_coding_bytes<0||
  2458. _dec->telemetry_coding_bytes>_dec->telemetry_frame_bytes){
  2459. _dec->telemetry_coding_bytes=0;
  2460. }
  2461. if(_dec->telemetry_mode_bytes<0||
  2462. _dec->telemetry_mode_bytes>_dec->telemetry_frame_bytes){
  2463. _dec->telemetry_mode_bytes=0;
  2464. }
  2465. if(_dec->telemetry_mv_bytes<0||
  2466. _dec->telemetry_mv_bytes>_dec->telemetry_frame_bytes){
  2467. _dec->telemetry_mv_bytes=0;
  2468. }
  2469. if(_dec->telemetry_qi_bytes<0||
  2470. _dec->telemetry_qi_bytes>_dec->telemetry_frame_bytes){
  2471. _dec->telemetry_qi_bytes=0;
  2472. }
  2473. if(_dec->telemetry_dc_bytes<0||
  2474. _dec->telemetry_dc_bytes>_dec->telemetry_frame_bytes){
  2475. _dec->telemetry_dc_bytes=0;
  2476. }
  2477. widths[0]=padw*
  2478. (_dec->telemetry_frame_bytes-_dec->telemetry_coding_bytes)/fullw;
  2479. widths[1]=padw*
  2480. (_dec->telemetry_coding_bytes-_dec->telemetry_mode_bytes)/fullw;
  2481. widths[2]=padw*
  2482. (_dec->telemetry_mode_bytes-_dec->telemetry_mv_bytes)/fullw;
  2483. widths[3]=padw*(_dec->telemetry_mv_bytes-_dec->telemetry_qi_bytes)/fullw;
  2484. widths[4]=padw*(_dec->telemetry_qi_bytes-_dec->telemetry_dc_bytes)/fullw;
  2485. widths[5]=padw*(_dec->telemetry_dc_bytes)/fullw;
  2486. for(i=0;i<6;i++)if(widths[i]>w)widths[i]=w;
  2487. cairo_set_source_rgba(c,.0,.0,.0,.6);
  2488. cairo_rectangle(c,10,h-33,widths[0]+1,5);
  2489. cairo_rectangle(c,10,h-29,widths[1]+1,5);
  2490. cairo_rectangle(c,10,h-25,widths[2]+1,5);
  2491. cairo_rectangle(c,10,h-21,widths[3]+1,5);
  2492. cairo_rectangle(c,10,h-17,widths[4]+1,5);
  2493. cairo_rectangle(c,10,h-13,widths[5]+1,5);
  2494. cairo_fill(c);
  2495. cairo_set_source_rgb(c,1,0,0);
  2496. cairo_rectangle(c,10.5,h-32.5,widths[0],4);
  2497. cairo_fill(c);
  2498. cairo_set_source_rgb(c,0,1,0);
  2499. cairo_rectangle(c,10.5,h-28.5,widths[1],4);
  2500. cairo_fill(c);
  2501. cairo_set_source_rgb(c,0,0,1);
  2502. cairo_rectangle(c,10.5,h-24.5,widths[2],4);
  2503. cairo_fill(c);
  2504. cairo_set_source_rgb(c,.6,.4,.0);
  2505. cairo_rectangle(c,10.5,h-20.5,widths[3],4);
  2506. cairo_fill(c);
  2507. cairo_set_source_rgb(c,.3,.3,.3);
  2508. cairo_rectangle(c,10.5,h-16.5,widths[4],4);
  2509. cairo_fill(c);
  2510. cairo_set_source_rgb(c,.5,.5,.8);
  2511. cairo_rectangle(c,10.5,h-12.5,widths[5],4);
  2512. cairo_fill(c);
  2513. }
  2514. /*Master qi indicator[s]:*/
  2515. if(_dec->telemetry_qi&0x1){
  2516. cairo_text_extents_t extents;
  2517. char buffer[10];
  2518. int p;
  2519. int y;
  2520. p=0;
  2521. y=h-7.5;
  2522. if(_dec->state.qis[0]>=10)buffer[p++]=48+_dec->state.qis[0]/10;
  2523. buffer[p++]=48+_dec->state.qis[0]%10;
  2524. if(_dec->state.nqis>=2){
  2525. buffer[p++]=' ';
  2526. if(_dec->state.qis[1]>=10)buffer[p++]=48+_dec->state.qis[1]/10;
  2527. buffer[p++]=48+_dec->state.qis[1]%10;
  2528. }
  2529. if(_dec->state.nqis==3){
  2530. buffer[p++]=' ';
  2531. if(_dec->state.qis[2]>=10)buffer[p++]=48+_dec->state.qis[2]/10;
  2532. buffer[p++]=48+_dec->state.qis[2]%10;
  2533. }
  2534. buffer[p++]='\0';
  2535. cairo_select_font_face(c,"sans",
  2536. CAIRO_FONT_SLANT_NORMAL,CAIRO_FONT_WEIGHT_BOLD);
  2537. cairo_set_font_size(c,18);
  2538. cairo_text_extents(c,buffer,&extents);
  2539. cairo_set_source_rgb(c,1,1,1);
  2540. cairo_move_to(c,w-extents.x_advance-10,y);
  2541. cairo_show_text(c,buffer);
  2542. cairo_set_source_rgb(c,0,0,0);
  2543. cairo_move_to(c,w-extents.x_advance-10,y);
  2544. cairo_text_path(c,buffer);
  2545. cairo_set_line_width(c,.8);
  2546. cairo_set_line_join(c,CAIRO_LINE_JOIN_ROUND);
  2547. cairo_stroke(c);
  2548. }
  2549. cairo_destroy(c);
  2550. }
  2551. /*Out of the Cairo plane into the telemetry YUV buffer.*/
  2552. _ycbcr[0].data=_dec->telemetry_frame_data;
  2553. _ycbcr[0].stride=_ycbcr[0].width;
  2554. _ycbcr[1].data=_ycbcr[0].data+h*_ycbcr[0].stride;
  2555. _ycbcr[1].stride=_ycbcr[1].width;
  2556. _ycbcr[2].data=_ycbcr[1].data+(h>>vdec)*_ycbcr[1].stride;
  2557. _ycbcr[2].stride=_ycbcr[2].width;
  2558. y_row=_ycbcr[0].data;
  2559. u_row=_ycbcr[1].data;
  2560. v_row=_ycbcr[2].data;
  2561. rgb_row=data;
  2562. /*This is one of the few places it's worth handling chroma on a
  2563. case-by-case basis.*/
  2564. switch(_dec->state.info.pixel_fmt){
  2565. case TH_PF_420:{
  2566. for(y=0;y<h;y+=2){
  2567. unsigned char *y_row2;
  2568. unsigned char *rgb_row2;
  2569. y_row2=y_row+_ycbcr[0].stride;
  2570. rgb_row2=rgb_row+cstride;
  2571. for(x=0;x<w;x+=2){
  2572. int y;
  2573. int u;
  2574. int v;
  2575. y=(65481*rgb_row[4*x+2]+128553*rgb_row[4*x+1]
  2576. +24966*rgb_row[4*x+0]+4207500)/255000;
  2577. y_row[x]=OC_CLAMP255(y);
  2578. y=(65481*rgb_row[4*x+6]+128553*rgb_row[4*x+5]
  2579. +24966*rgb_row[4*x+4]+4207500)/255000;
  2580. y_row[x+1]=OC_CLAMP255(y);
  2581. y=(65481*rgb_row2[4*x+2]+128553*rgb_row2[4*x+1]
  2582. +24966*rgb_row2[4*x+0]+4207500)/255000;
  2583. y_row2[x]=OC_CLAMP255(y);
  2584. y=(65481*rgb_row2[4*x+6]+128553*rgb_row2[4*x+5]
  2585. +24966*rgb_row2[4*x+4]+4207500)/255000;
  2586. y_row2[x+1]=OC_CLAMP255(y);
  2587. u=(-8372*(rgb_row[4*x+2]+rgb_row[4*x+6]
  2588. +rgb_row2[4*x+2]+rgb_row2[4*x+6])
  2589. -16436*(rgb_row[4*x+1]+rgb_row[4*x+5]
  2590. +rgb_row2[4*x+1]+rgb_row2[4*x+5])
  2591. +24808*(rgb_row[4*x+0]+rgb_row[4*x+4]
  2592. +rgb_row2[4*x+0]+rgb_row2[4*x+4])+29032005)/225930;
  2593. v=(39256*(rgb_row[4*x+2]+rgb_row[4*x+6]
  2594. +rgb_row2[4*x+2]+rgb_row2[4*x+6])
  2595. -32872*(rgb_row[4*x+1]+rgb_row[4*x+5]
  2596. +rgb_row2[4*x+1]+rgb_row2[4*x+5])
  2597. -6384*(rgb_row[4*x+0]+rgb_row[4*x+4]
  2598. +rgb_row2[4*x+0]+rgb_row2[4*x+4])+45940035)/357510;
  2599. u_row[x>>1]=OC_CLAMP255(u);
  2600. v_row[x>>1]=OC_CLAMP255(v);
  2601. }
  2602. y_row+=_ycbcr[0].stride<<1;
  2603. u_row+=_ycbcr[1].stride;
  2604. v_row+=_ycbcr[2].stride;
  2605. rgb_row+=cstride<<1;
  2606. }
  2607. }break;
  2608. case TH_PF_422:{
  2609. for(y=0;y<h;y++){
  2610. for(x=0;x<w;x+=2){
  2611. int y;
  2612. int u;
  2613. int v;
  2614. y=(65481*rgb_row[4*x+2]+128553*rgb_row[4*x+1]
  2615. +24966*rgb_row[4*x+0]+4207500)/255000;
  2616. y_row[x]=OC_CLAMP255(y);
  2617. y=(65481*rgb_row[4*x+6]+128553*rgb_row[4*x+5]
  2618. +24966*rgb_row[4*x+4]+4207500)/255000;
  2619. y_row[x+1]=OC_CLAMP255(y);
  2620. u=(-16744*(rgb_row[4*x+2]+rgb_row[4*x+6])
  2621. -32872*(rgb_row[4*x+1]+rgb_row[4*x+5])
  2622. +49616*(rgb_row[4*x+0]+rgb_row[4*x+4])+29032005)/225930;
  2623. v=(78512*(rgb_row[4*x+2]+rgb_row[4*x+6])
  2624. -65744*(rgb_row[4*x+1]+rgb_row[4*x+5])
  2625. -12768*(rgb_row[4*x+0]+rgb_row[4*x+4])+45940035)/357510;
  2626. u_row[x>>1]=OC_CLAMP255(u);
  2627. v_row[x>>1]=OC_CLAMP255(v);
  2628. }
  2629. y_row+=_ycbcr[0].stride;
  2630. u_row+=_ycbcr[1].stride;
  2631. v_row+=_ycbcr[2].stride;
  2632. rgb_row+=cstride;
  2633. }
  2634. }break;
  2635. /*case TH_PF_444:*/
  2636. default:{
  2637. for(y=0;y<h;y++){
  2638. for(x=0;x<w;x++){
  2639. int y;
  2640. int u;
  2641. int v;
  2642. y=(65481*rgb_row[4*x+2]+128553*rgb_row[4*x+1]
  2643. +24966*rgb_row[4*x+0]+4207500)/255000;
  2644. u=(-33488*rgb_row[4*x+2]-65744*rgb_row[4*x+1]
  2645. +99232*rgb_row[4*x+0]+29032005)/225930;
  2646. v=(157024*rgb_row[4*x+2]-131488*rgb_row[4*x+1]
  2647. -25536*rgb_row[4*x+0]+45940035)/357510;
  2648. y_row[x]=OC_CLAMP255(y);
  2649. u_row[x]=OC_CLAMP255(u);
  2650. v_row[x]=OC_CLAMP255(v);
  2651. }
  2652. y_row+=_ycbcr[0].stride;
  2653. u_row+=_ycbcr[1].stride;
  2654. v_row+=_ycbcr[2].stride;
  2655. rgb_row+=cstride;
  2656. }
  2657. }break;
  2658. }
  2659. /*Finished.
  2660. Destroy the surface.*/
  2661. cairo_surface_destroy(cs);
  2662. }
  2663. #endif
  2664. int th_decode_packetin(th_dec_ctx *_dec,const ogg_packet *_op,
  2665. ogg_int64_t *_granpos){
  2666. int ret;
  2667. if(_dec==NULL||_op==NULL)return TH_EFAULT;
  2668. /*A completely empty packet indicates a dropped frame and is treated exactly
  2669. like an inter frame with no coded blocks.*/
  2670. if(_op->bytes==0){
  2671. _dec->state.frame_type=OC_INTER_FRAME;
  2672. _dec->state.ntotal_coded_fragis=0;
  2673. }
  2674. else{
  2675. oc_pack_readinit(&_dec->opb,_op->packet,_op->bytes);
  2676. ret=oc_dec_frame_header_unpack(_dec);
  2677. if(ret<0)return ret;
  2678. if(_dec->state.frame_type==OC_INTRA_FRAME)oc_dec_mark_all_intra(_dec);
  2679. else oc_dec_coded_flags_unpack(_dec);
  2680. }
  2681. /*If there have been no reference frames, and we need one, initialize one.*/
  2682. if(_dec->state.frame_type!=OC_INTRA_FRAME&&
  2683. (_dec->state.ref_frame_idx[OC_FRAME_GOLD]<0||
  2684. _dec->state.ref_frame_idx[OC_FRAME_PREV]<0)){
  2685. oc_dec_init_dummy_frame(_dec);
  2686. }
  2687. /*If this was an inter frame with no coded blocks...*/
  2688. if(_dec->state.ntotal_coded_fragis<=0){
  2689. /*Just update the granule position and return.*/
  2690. _dec->state.granpos=(_dec->state.keyframe_num+_dec->state.granpos_bias<<
  2691. _dec->state.info.keyframe_granule_shift)
  2692. +(_dec->state.curframe_num-_dec->state.keyframe_num);
  2693. _dec->state.curframe_num++;
  2694. if(_granpos!=NULL)*_granpos=_dec->state.granpos;
  2695. return TH_DUPFRAME;
  2696. }
  2697. else{
  2698. th_ycbcr_buffer stripe_buf;
  2699. int stripe_fragy;
  2700. int refi;
  2701. int pli;
  2702. int notstart;
  2703. int notdone;
  2704. #ifdef HAVE_CAIRO
  2705. int telemetry;
  2706. /*Save the current telemetry state.
  2707. This prevents it from being modified in the middle of decoding this
  2708. frame, which could cause us to skip calls to the striped decoding
  2709. callback.*/
  2710. telemetry=_dec->telemetry_mbmode||_dec->telemetry_mv||
  2711. _dec->telemetry_qi||_dec->telemetry_bits;
  2712. #endif
  2713. /*Select a free buffer to use for the reconstructed version of this frame.*/
  2714. for(refi=0;refi==_dec->state.ref_frame_idx[OC_FRAME_GOLD]||
  2715. refi==_dec->state.ref_frame_idx[OC_FRAME_PREV];refi++);
  2716. _dec->state.ref_frame_idx[OC_FRAME_SELF]=refi;
  2717. _dec->state.ref_frame_data[OC_FRAME_SELF]=
  2718. _dec->state.ref_frame_bufs[refi][0].data;
  2719. #if defined(HAVE_CAIRO)
  2720. _dec->telemetry_frame_bytes=_op->bytes;
  2721. #endif
  2722. if(_dec->state.frame_type==OC_INTRA_FRAME){
  2723. _dec->state.keyframe_num=_dec->state.curframe_num;
  2724. #if defined(HAVE_CAIRO)
  2725. _dec->telemetry_coding_bytes=
  2726. _dec->telemetry_mode_bytes=
  2727. _dec->telemetry_mv_bytes=oc_pack_bytes_left(&_dec->opb);
  2728. #endif
  2729. }
  2730. else{
  2731. #if defined(HAVE_CAIRO)
  2732. _dec->telemetry_coding_bytes=oc_pack_bytes_left(&_dec->opb);
  2733. #endif
  2734. oc_dec_mb_modes_unpack(_dec);
  2735. #if defined(HAVE_CAIRO)
  2736. _dec->telemetry_mode_bytes=oc_pack_bytes_left(&_dec->opb);
  2737. #endif
  2738. oc_dec_mv_unpack_and_frag_modes_fill(_dec);
  2739. #if defined(HAVE_CAIRO)
  2740. _dec->telemetry_mv_bytes=oc_pack_bytes_left(&_dec->opb);
  2741. #endif
  2742. }
  2743. oc_dec_block_qis_unpack(_dec);
  2744. #if defined(HAVE_CAIRO)
  2745. _dec->telemetry_qi_bytes=oc_pack_bytes_left(&_dec->opb);
  2746. #endif
  2747. oc_dec_residual_tokens_unpack(_dec);
  2748. /*Update granule position.
  2749. This must be done before the striped decode callbacks so that the
  2750. application knows what to do with the frame data.*/
  2751. _dec->state.granpos=(_dec->state.keyframe_num+_dec->state.granpos_bias<<
  2752. _dec->state.info.keyframe_granule_shift)
  2753. +(_dec->state.curframe_num-_dec->state.keyframe_num);
  2754. _dec->state.curframe_num++;
  2755. if(_granpos!=NULL)*_granpos=_dec->state.granpos;
  2756. /*All of the rest of the operations -- DC prediction reversal,
  2757. reconstructing coded fragments, copying uncoded fragments, loop
  2758. filtering, extending borders, and out-of-loop post-processing -- should
  2759. be pipelined.
  2760. I.e., DC prediction reversal, reconstruction, and uncoded fragment
  2761. copying are done for one or two super block rows, then loop filtering is
  2762. run as far as it can, then bordering copying, then post-processing.
  2763. For 4:2:0 video a Minimum Codable Unit or MCU contains two luma super
  2764. block rows, and one chroma.
  2765. Otherwise, an MCU consists of one super block row from each plane.
  2766. Inside each MCU, we perform all of the steps on one color plane before
  2767. moving on to the next.
  2768. After reconstruction, the additional filtering stages introduce a delay
  2769. since they need some pixels from the next fragment row.
  2770. Thus the actual number of decoded rows available is slightly smaller for
  2771. the first MCU, and slightly larger for the last.
  2772. This entire process allows us to operate on the data while it is still in
  2773. cache, resulting in big performance improvements.
  2774. An application callback allows further application processing (blitting
  2775. to video memory, color conversion, etc.) to also use the data while it's
  2776. in cache.*/
  2777. oc_dec_pipeline_init(_dec,&_dec->pipe);
  2778. oc_ycbcr_buffer_flip(stripe_buf,_dec->pp_frame_buf);
  2779. notstart=0;
  2780. notdone=1;
  2781. for(stripe_fragy=0;notdone;stripe_fragy+=_dec->pipe.mcu_nvfrags){
  2782. int avail_fragy0;
  2783. int avail_fragy_end;
  2784. avail_fragy0=avail_fragy_end=_dec->state.fplanes[0].nvfrags;
  2785. notdone=stripe_fragy+_dec->pipe.mcu_nvfrags<avail_fragy_end;
  2786. for(pli=0;pli<3;pli++){
  2787. oc_fragment_plane *fplane;
  2788. int frag_shift;
  2789. int pp_offset;
  2790. int sdelay;
  2791. int edelay;
  2792. fplane=_dec->state.fplanes+pli;
  2793. /*Compute the first and last fragment row of the current MCU for this
  2794. plane.*/
  2795. frag_shift=pli!=0&&!(_dec->state.info.pixel_fmt&2);
  2796. _dec->pipe.fragy0[pli]=stripe_fragy>>frag_shift;
  2797. _dec->pipe.fragy_end[pli]=OC_MINI(fplane->nvfrags,
  2798. _dec->pipe.fragy0[pli]+(_dec->pipe.mcu_nvfrags>>frag_shift));
  2799. oc_dec_dc_unpredict_mcu_plane(_dec,&_dec->pipe,pli);
  2800. oc_dec_frags_recon_mcu_plane(_dec,&_dec->pipe,pli);
  2801. sdelay=edelay=0;
  2802. if(_dec->pipe.loop_filter){
  2803. sdelay+=notstart;
  2804. edelay+=notdone;
  2805. oc_state_loop_filter_frag_rows(&_dec->state,
  2806. _dec->pipe.bounding_values,OC_FRAME_SELF,pli,
  2807. _dec->pipe.fragy0[pli]-sdelay,_dec->pipe.fragy_end[pli]-edelay);
  2808. }
  2809. /*To fill the borders, we have an additional two pixel delay, since a
  2810. fragment in the next row could filter its top edge, using two pixels
  2811. from a fragment in this row.
  2812. But there's no reason to delay a full fragment between the two.*/
  2813. oc_state_borders_fill_rows(&_dec->state,refi,pli,
  2814. (_dec->pipe.fragy0[pli]-sdelay<<3)-(sdelay<<1),
  2815. (_dec->pipe.fragy_end[pli]-edelay<<3)-(edelay<<1));
  2816. /*Out-of-loop post-processing.*/
  2817. pp_offset=3*(pli!=0);
  2818. if(_dec->pipe.pp_level>=OC_PP_LEVEL_DEBLOCKY+pp_offset){
  2819. /*Perform de-blocking in one plane.*/
  2820. sdelay+=notstart;
  2821. edelay+=notdone;
  2822. oc_dec_deblock_frag_rows(_dec,_dec->pp_frame_buf,
  2823. _dec->state.ref_frame_bufs[refi],pli,
  2824. _dec->pipe.fragy0[pli]-sdelay,_dec->pipe.fragy_end[pli]-edelay);
  2825. if(_dec->pipe.pp_level>=OC_PP_LEVEL_DERINGY+pp_offset){
  2826. /*Perform de-ringing in one plane.*/
  2827. sdelay+=notstart;
  2828. edelay+=notdone;
  2829. oc_dec_dering_frag_rows(_dec,_dec->pp_frame_buf,pli,
  2830. _dec->pipe.fragy0[pli]-sdelay,_dec->pipe.fragy_end[pli]-edelay);
  2831. }
  2832. }
  2833. /*If no post-processing is done, we still need to delay a row for the
  2834. loop filter, thanks to the strange filtering order VP3 chose.*/
  2835. else if(_dec->pipe.loop_filter){
  2836. sdelay+=notstart;
  2837. edelay+=notdone;
  2838. }
  2839. /*Compute the intersection of the available rows in all planes.
  2840. If chroma is sub-sampled, the effect of each of its delays is
  2841. doubled, but luma might have more post-processing filters enabled
  2842. than chroma, so we don't know up front which one is the limiting
  2843. factor.*/
  2844. avail_fragy0=OC_MINI(avail_fragy0,
  2845. _dec->pipe.fragy0[pli]-sdelay<<frag_shift);
  2846. avail_fragy_end=OC_MINI(avail_fragy_end,
  2847. _dec->pipe.fragy_end[pli]-edelay<<frag_shift);
  2848. }
  2849. #ifdef HAVE_CAIRO
  2850. if(_dec->stripe_cb.stripe_decoded!=NULL&&!telemetry){
  2851. #else
  2852. if(_dec->stripe_cb.stripe_decoded!=NULL){
  2853. #endif
  2854. /*The callback might want to use the FPU, so let's make sure they can.
  2855. We violate all kinds of ABI restrictions by not doing this until
  2856. now, but none of them actually matter since we don't use floating
  2857. point ourselves.*/
  2858. oc_restore_fpu(&_dec->state);
  2859. /*Make the callback, ensuring we flip the sense of the "start" and
  2860. "end" of the available region upside down.*/
  2861. (*_dec->stripe_cb.stripe_decoded)(_dec->stripe_cb.ctx,stripe_buf,
  2862. _dec->state.fplanes[0].nvfrags-avail_fragy_end,
  2863. _dec->state.fplanes[0].nvfrags-avail_fragy0);
  2864. }
  2865. notstart=1;
  2866. }
  2867. /*Finish filling in the reference frame borders.*/
  2868. for(pli=0;pli<3;pli++)oc_state_borders_fill_caps(&_dec->state,refi,pli);
  2869. /*Update the reference frame indices.*/
  2870. if(_dec->state.frame_type==OC_INTRA_FRAME){
  2871. /*The new frame becomes both the previous and gold reference frames.*/
  2872. _dec->state.ref_frame_idx[OC_FRAME_GOLD]=
  2873. _dec->state.ref_frame_idx[OC_FRAME_PREV]=
  2874. _dec->state.ref_frame_idx[OC_FRAME_SELF];
  2875. _dec->state.ref_frame_data[OC_FRAME_GOLD]=
  2876. _dec->state.ref_frame_data[OC_FRAME_PREV]=
  2877. _dec->state.ref_frame_data[OC_FRAME_SELF];
  2878. }
  2879. else{
  2880. /*Otherwise, just replace the previous reference frame.*/
  2881. _dec->state.ref_frame_idx[OC_FRAME_PREV]=
  2882. _dec->state.ref_frame_idx[OC_FRAME_SELF];
  2883. _dec->state.ref_frame_data[OC_FRAME_PREV]=
  2884. _dec->state.ref_frame_data[OC_FRAME_SELF];
  2885. }
  2886. /*Restore the FPU before dump_frame, since that _does_ use the FPU (for PNG
  2887. gamma values, if nothing else).*/
  2888. oc_restore_fpu(&_dec->state);
  2889. #ifdef HAVE_CAIRO
  2890. /*If telemetry ioctls are active, we need to draw to the output buffer.*/
  2891. if(telemetry){
  2892. oc_render_telemetry(_dec,stripe_buf,telemetry);
  2893. oc_ycbcr_buffer_flip(_dec->pp_frame_buf,stripe_buf);
  2894. /*If we had a striped decoding callback, we skipped calling it above
  2895. (because the telemetry wasn't rendered yet).
  2896. Call it now with the whole frame.*/
  2897. if(_dec->stripe_cb.stripe_decoded!=NULL){
  2898. (*_dec->stripe_cb.stripe_decoded)(_dec->stripe_cb.ctx,
  2899. stripe_buf,0,_dec->state.fplanes[0].nvfrags);
  2900. }
  2901. }
  2902. #endif
  2903. #if defined(OC_DUMP_IMAGES)
  2904. /*We only dump images if there were some coded blocks.*/
  2905. oc_state_dump_frame(&_dec->state,OC_FRAME_SELF,"dec");
  2906. #endif
  2907. return 0;
  2908. }
  2909. }
  2910. int th_decode_ycbcr_out(th_dec_ctx *_dec,th_ycbcr_buffer _ycbcr){
  2911. if(_dec==NULL||_ycbcr==NULL)return TH_EFAULT;
  2912. oc_ycbcr_buffer_flip(_ycbcr,_dec->pp_frame_buf);
  2913. return 0;
  2914. }