lossless.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Image transforms and color space conversion methods for lossless decoder.
  11. //
  12. // Authors: Vikas Arora (vikaas.arora@gmail.com)
  13. // Jyrki Alakuijala (jyrki@google.com)
  14. // Urvang Joshi (urvang@google.com)
  15. #include "./dsp.h"
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include "../dec/vp8li.h"
  19. #include "../utils/endian_inl.h"
  20. #include "./lossless.h"
  21. #include "./yuv.h"
  22. #define MAX_DIFF_COST (1e30f)
  23. // lookup table for small values of log2(int)
  24. const float kLog2Table[LOG_LOOKUP_IDX_MAX] = {
  25. 0.0000000000000000f, 0.0000000000000000f,
  26. 1.0000000000000000f, 1.5849625007211560f,
  27. 2.0000000000000000f, 2.3219280948873621f,
  28. 2.5849625007211560f, 2.8073549220576041f,
  29. 3.0000000000000000f, 3.1699250014423121f,
  30. 3.3219280948873621f, 3.4594316186372973f,
  31. 3.5849625007211560f, 3.7004397181410921f,
  32. 3.8073549220576041f, 3.9068905956085187f,
  33. 4.0000000000000000f, 4.0874628412503390f,
  34. 4.1699250014423121f, 4.2479275134435852f,
  35. 4.3219280948873626f, 4.3923174227787606f,
  36. 4.4594316186372973f, 4.5235619560570130f,
  37. 4.5849625007211560f, 4.6438561897747243f,
  38. 4.7004397181410917f, 4.7548875021634682f,
  39. 4.8073549220576037f, 4.8579809951275718f,
  40. 4.9068905956085187f, 4.9541963103868749f,
  41. 5.0000000000000000f, 5.0443941193584533f,
  42. 5.0874628412503390f, 5.1292830169449663f,
  43. 5.1699250014423121f, 5.2094533656289501f,
  44. 5.2479275134435852f, 5.2854022188622487f,
  45. 5.3219280948873626f, 5.3575520046180837f,
  46. 5.3923174227787606f, 5.4262647547020979f,
  47. 5.4594316186372973f, 5.4918530963296747f,
  48. 5.5235619560570130f, 5.5545888516776376f,
  49. 5.5849625007211560f, 5.6147098441152083f,
  50. 5.6438561897747243f, 5.6724253419714951f,
  51. 5.7004397181410917f, 5.7279204545631987f,
  52. 5.7548875021634682f, 5.7813597135246599f,
  53. 5.8073549220576037f, 5.8328900141647412f,
  54. 5.8579809951275718f, 5.8826430493618415f,
  55. 5.9068905956085187f, 5.9307373375628866f,
  56. 5.9541963103868749f, 5.9772799234999167f,
  57. 6.0000000000000000f, 6.0223678130284543f,
  58. 6.0443941193584533f, 6.0660891904577720f,
  59. 6.0874628412503390f, 6.1085244567781691f,
  60. 6.1292830169449663f, 6.1497471195046822f,
  61. 6.1699250014423121f, 6.1898245588800175f,
  62. 6.2094533656289501f, 6.2288186904958804f,
  63. 6.2479275134435852f, 6.2667865406949010f,
  64. 6.2854022188622487f, 6.3037807481771030f,
  65. 6.3219280948873626f, 6.3398500028846243f,
  66. 6.3575520046180837f, 6.3750394313469245f,
  67. 6.3923174227787606f, 6.4093909361377017f,
  68. 6.4262647547020979f, 6.4429434958487279f,
  69. 6.4594316186372973f, 6.4757334309663976f,
  70. 6.4918530963296747f, 6.5077946401986963f,
  71. 6.5235619560570130f, 6.5391588111080309f,
  72. 6.5545888516776376f, 6.5698556083309478f,
  73. 6.5849625007211560f, 6.5999128421871278f,
  74. 6.6147098441152083f, 6.6293566200796094f,
  75. 6.6438561897747243f, 6.6582114827517946f,
  76. 6.6724253419714951f, 6.6865005271832185f,
  77. 6.7004397181410917f, 6.7142455176661224f,
  78. 6.7279204545631987f, 6.7414669864011464f,
  79. 6.7548875021634682f, 6.7681843247769259f,
  80. 6.7813597135246599f, 6.7944158663501061f,
  81. 6.8073549220576037f, 6.8201789624151878f,
  82. 6.8328900141647412f, 6.8454900509443747f,
  83. 6.8579809951275718f, 6.8703647195834047f,
  84. 6.8826430493618415f, 6.8948177633079437f,
  85. 6.9068905956085187f, 6.9188632372745946f,
  86. 6.9307373375628866f, 6.9425145053392398f,
  87. 6.9541963103868749f, 6.9657842846620869f,
  88. 6.9772799234999167f, 6.9886846867721654f,
  89. 7.0000000000000000f, 7.0112272554232539f,
  90. 7.0223678130284543f, 7.0334230015374501f,
  91. 7.0443941193584533f, 7.0552824355011898f,
  92. 7.0660891904577720f, 7.0768155970508308f,
  93. 7.0874628412503390f, 7.0980320829605263f,
  94. 7.1085244567781691f, 7.1189410727235076f,
  95. 7.1292830169449663f, 7.1395513523987936f,
  96. 7.1497471195046822f, 7.1598713367783890f,
  97. 7.1699250014423121f, 7.1799090900149344f,
  98. 7.1898245588800175f, 7.1996723448363644f,
  99. 7.2094533656289501f, 7.2191685204621611f,
  100. 7.2288186904958804f, 7.2384047393250785f,
  101. 7.2479275134435852f, 7.2573878426926521f,
  102. 7.2667865406949010f, 7.2761244052742375f,
  103. 7.2854022188622487f, 7.2946207488916270f,
  104. 7.3037807481771030f, 7.3128829552843557f,
  105. 7.3219280948873626f, 7.3309168781146167f,
  106. 7.3398500028846243f, 7.3487281542310771f,
  107. 7.3575520046180837f, 7.3663222142458160f,
  108. 7.3750394313469245f, 7.3837042924740519f,
  109. 7.3923174227787606f, 7.4008794362821843f,
  110. 7.4093909361377017f, 7.4178525148858982f,
  111. 7.4262647547020979f, 7.4346282276367245f,
  112. 7.4429434958487279f, 7.4512111118323289f,
  113. 7.4594316186372973f, 7.4676055500829976f,
  114. 7.4757334309663976f, 7.4838157772642563f,
  115. 7.4918530963296747f, 7.4998458870832056f,
  116. 7.5077946401986963f, 7.5156998382840427f,
  117. 7.5235619560570130f, 7.5313814605163118f,
  118. 7.5391588111080309f, 7.5468944598876364f,
  119. 7.5545888516776376f, 7.5622424242210728f,
  120. 7.5698556083309478f, 7.5774288280357486f,
  121. 7.5849625007211560f, 7.5924570372680806f,
  122. 7.5999128421871278f, 7.6073303137496104f,
  123. 7.6147098441152083f, 7.6220518194563764f,
  124. 7.6293566200796094f, 7.6366246205436487f,
  125. 7.6438561897747243f, 7.6510516911789281f,
  126. 7.6582114827517946f, 7.6653359171851764f,
  127. 7.6724253419714951f, 7.6794800995054464f,
  128. 7.6865005271832185f, 7.6934869574993252f,
  129. 7.7004397181410917f, 7.7073591320808825f,
  130. 7.7142455176661224f, 7.7210991887071855f,
  131. 7.7279204545631987f, 7.7347096202258383f,
  132. 7.7414669864011464f, 7.7481928495894605f,
  133. 7.7548875021634682f, 7.7615512324444795f,
  134. 7.7681843247769259f, 7.7747870596011736f,
  135. 7.7813597135246599f, 7.7879025593914317f,
  136. 7.7944158663501061f, 7.8008998999203047f,
  137. 7.8073549220576037f, 7.8137811912170374f,
  138. 7.8201789624151878f, 7.8265484872909150f,
  139. 7.8328900141647412f, 7.8392037880969436f,
  140. 7.8454900509443747f, 7.8517490414160571f,
  141. 7.8579809951275718f, 7.8641861446542797f,
  142. 7.8703647195834047f, 7.8765169465649993f,
  143. 7.8826430493618415f, 7.8887432488982591f,
  144. 7.8948177633079437f, 7.9008668079807486f,
  145. 7.9068905956085187f, 7.9128893362299619f,
  146. 7.9188632372745946f, 7.9248125036057812f,
  147. 7.9307373375628866f, 7.9366379390025709f,
  148. 7.9425145053392398f, 7.9483672315846778f,
  149. 7.9541963103868749f, 7.9600019320680805f,
  150. 7.9657842846620869f, 7.9715435539507719f,
  151. 7.9772799234999167f, 7.9829935746943103f,
  152. 7.9886846867721654f, 7.9943534368588577f
  153. };
  154. const float kSLog2Table[LOG_LOOKUP_IDX_MAX] = {
  155. 0.00000000f, 0.00000000f, 2.00000000f, 4.75488750f,
  156. 8.00000000f, 11.60964047f, 15.50977500f, 19.65148445f,
  157. 24.00000000f, 28.52932501f, 33.21928095f, 38.05374781f,
  158. 43.01955001f, 48.10571634f, 53.30296891f, 58.60335893f,
  159. 64.00000000f, 69.48686830f, 75.05865003f, 80.71062276f,
  160. 86.43856190f, 92.23866588f, 98.10749561f, 104.04192499f,
  161. 110.03910002f, 116.09640474f, 122.21143267f, 128.38196256f,
  162. 134.60593782f, 140.88144886f, 147.20671787f, 153.58008562f,
  163. 160.00000000f, 166.46500594f, 172.97373660f, 179.52490559f,
  164. 186.11730005f, 192.74977453f, 199.42124551f, 206.13068654f,
  165. 212.87712380f, 219.65963219f, 226.47733176f, 233.32938445f,
  166. 240.21499122f, 247.13338933f, 254.08384998f, 261.06567603f,
  167. 268.07820003f, 275.12078236f, 282.19280949f, 289.29369244f,
  168. 296.42286534f, 303.57978409f, 310.76392512f, 317.97478424f,
  169. 325.21187564f, 332.47473081f, 339.76289772f, 347.07593991f,
  170. 354.41343574f, 361.77497759f, 369.16017124f, 376.56863518f,
  171. 384.00000000f, 391.45390785f, 398.93001188f, 406.42797576f,
  172. 413.94747321f, 421.48818752f, 429.04981119f, 436.63204548f,
  173. 444.23460010f, 451.85719280f, 459.49954906f, 467.16140179f,
  174. 474.84249102f, 482.54256363f, 490.26137307f, 497.99867911f,
  175. 505.75424759f, 513.52785023f, 521.31926438f, 529.12827280f,
  176. 536.95466351f, 544.79822957f, 552.65876890f, 560.53608414f,
  177. 568.42998244f, 576.34027536f, 584.26677867f, 592.20931226f,
  178. 600.16769996f, 608.14176943f, 616.13135206f, 624.13628279f,
  179. 632.15640007f, 640.19154569f, 648.24156472f, 656.30630539f,
  180. 664.38561898f, 672.47935976f, 680.58738488f, 688.70955430f,
  181. 696.84573069f, 704.99577935f, 713.15956818f, 721.33696754f,
  182. 729.52785023f, 737.73209140f, 745.94956849f, 754.18016116f,
  183. 762.42375127f, 770.68022275f, 778.94946161f, 787.23135586f,
  184. 795.52579543f, 803.83267219f, 812.15187982f, 820.48331383f,
  185. 828.82687147f, 837.18245171f, 845.54995518f, 853.92928416f,
  186. 862.32034249f, 870.72303558f, 879.13727036f, 887.56295522f,
  187. 896.00000000f, 904.44831595f, 912.90781569f, 921.37841320f,
  188. 929.86002376f, 938.35256392f, 946.85595152f, 955.37010560f,
  189. 963.89494641f, 972.43039537f, 980.97637504f, 989.53280911f,
  190. 998.09962237f, 1006.67674069f, 1015.26409097f, 1023.86160116f,
  191. 1032.46920021f, 1041.08681805f, 1049.71438560f, 1058.35183469f,
  192. 1066.99909811f, 1075.65610955f, 1084.32280357f, 1092.99911564f,
  193. 1101.68498204f, 1110.38033993f, 1119.08512727f, 1127.79928282f,
  194. 1136.52274614f, 1145.25545758f, 1153.99735821f, 1162.74838989f,
  195. 1171.50849518f, 1180.27761738f, 1189.05570047f, 1197.84268914f,
  196. 1206.63852876f, 1215.44316535f, 1224.25654560f, 1233.07861684f,
  197. 1241.90932703f, 1250.74862473f, 1259.59645914f, 1268.45278005f,
  198. 1277.31753781f, 1286.19068338f, 1295.07216828f, 1303.96194457f,
  199. 1312.85996488f, 1321.76618236f, 1330.68055071f, 1339.60302413f,
  200. 1348.53355734f, 1357.47210556f, 1366.41862452f, 1375.37307041f,
  201. 1384.33539991f, 1393.30557020f, 1402.28353887f, 1411.26926400f,
  202. 1420.26270412f, 1429.26381818f, 1438.27256558f, 1447.28890615f,
  203. 1456.31280014f, 1465.34420819f, 1474.38309138f, 1483.42941118f,
  204. 1492.48312945f, 1501.54420843f, 1510.61261078f, 1519.68829949f,
  205. 1528.77123795f, 1537.86138993f, 1546.95871952f, 1556.06319119f,
  206. 1565.17476976f, 1574.29342040f, 1583.41910860f, 1592.55180020f,
  207. 1601.69146137f, 1610.83805860f, 1619.99155871f, 1629.15192882f,
  208. 1638.31913637f, 1647.49314911f, 1656.67393509f, 1665.86146266f,
  209. 1675.05570047f, 1684.25661744f, 1693.46418280f, 1702.67836605f,
  210. 1711.89913698f, 1721.12646563f, 1730.36032233f, 1739.60067768f,
  211. 1748.84750254f, 1758.10076802f, 1767.36044551f, 1776.62650662f,
  212. 1785.89892323f, 1795.17766747f, 1804.46271172f, 1813.75402857f,
  213. 1823.05159087f, 1832.35537170f, 1841.66534438f, 1850.98148244f,
  214. 1860.30375965f, 1869.63214999f, 1878.96662767f, 1888.30716711f,
  215. 1897.65374295f, 1907.00633003f, 1916.36490342f, 1925.72943838f,
  216. 1935.09991037f, 1944.47629506f, 1953.85856831f, 1963.24670620f,
  217. 1972.64068498f, 1982.04048108f, 1991.44607117f, 2000.85743204f,
  218. 2010.27454072f, 2019.69737440f, 2029.12591044f, 2038.56012640f
  219. };
  220. const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX] = {
  221. { 0, 0}, { 0, 0}, { 1, 0}, { 2, 0}, { 3, 0}, { 4, 1}, { 4, 1}, { 5, 1},
  222. { 5, 1}, { 6, 2}, { 6, 2}, { 6, 2}, { 6, 2}, { 7, 2}, { 7, 2}, { 7, 2},
  223. { 7, 2}, { 8, 3}, { 8, 3}, { 8, 3}, { 8, 3}, { 8, 3}, { 8, 3}, { 8, 3},
  224. { 8, 3}, { 9, 3}, { 9, 3}, { 9, 3}, { 9, 3}, { 9, 3}, { 9, 3}, { 9, 3},
  225. { 9, 3}, {10, 4}, {10, 4}, {10, 4}, {10, 4}, {10, 4}, {10, 4}, {10, 4},
  226. {10, 4}, {10, 4}, {10, 4}, {10, 4}, {10, 4}, {10, 4}, {10, 4}, {10, 4},
  227. {10, 4}, {11, 4}, {11, 4}, {11, 4}, {11, 4}, {11, 4}, {11, 4}, {11, 4},
  228. {11, 4}, {11, 4}, {11, 4}, {11, 4}, {11, 4}, {11, 4}, {11, 4}, {11, 4},
  229. {11, 4}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
  230. {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
  231. {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
  232. {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
  233. {12, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5},
  234. {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5},
  235. {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5},
  236. {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5}, {13, 5},
  237. {13, 5}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6},
  238. {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6},
  239. {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6},
  240. {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6},
  241. {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6},
  242. {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6},
  243. {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6},
  244. {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6}, {14, 6},
  245. {14, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6},
  246. {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6},
  247. {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6},
  248. {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6},
  249. {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6},
  250. {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6},
  251. {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6},
  252. {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6}, {15, 6},
  253. {15, 6}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  254. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  255. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  256. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  257. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  258. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  259. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  260. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  261. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  262. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  263. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  264. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  265. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  266. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  267. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  268. {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7}, {16, 7},
  269. {16, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  270. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  271. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  272. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  273. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  274. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  275. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  276. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  277. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  278. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  279. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  280. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  281. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  282. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  283. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  284. {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
  285. };
  286. const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX] = {
  287. 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 2, 3, 0, 1, 2, 3,
  288. 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
  289. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  290. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  291. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  292. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  293. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  294. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  295. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  296. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  297. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  298. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  299. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  300. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  301. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  302. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  303. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  304. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  305. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  306. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  307. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  308. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  309. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  310. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
  311. 127,
  312. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  313. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  314. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  315. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  316. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  317. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  318. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  319. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126
  320. };
  321. // The threshold till approximate version of log_2 can be used.
  322. // Practically, we can get rid of the call to log() as the two values match to
  323. // very high degree (the ratio of these two is 0.99999x).
  324. // Keeping a high threshold for now.
  325. #define APPROX_LOG_WITH_CORRECTION_MAX 65536
  326. #define APPROX_LOG_MAX 4096
  327. #define LOG_2_RECIPROCAL 1.44269504088896338700465094007086
  328. static float FastSLog2Slow(uint32_t v) {
  329. assert(v >= LOG_LOOKUP_IDX_MAX);
  330. if (v < APPROX_LOG_WITH_CORRECTION_MAX) {
  331. int log_cnt = 0;
  332. uint32_t y = 1;
  333. int correction = 0;
  334. const float v_f = (float)v;
  335. const uint32_t orig_v = v;
  336. do {
  337. ++log_cnt;
  338. v = v >> 1;
  339. y = y << 1;
  340. } while (v >= LOG_LOOKUP_IDX_MAX);
  341. // vf = (2^log_cnt) * Xf; where y = 2^log_cnt and Xf < 256
  342. // Xf = floor(Xf) * (1 + (v % y) / v)
  343. // log2(Xf) = log2(floor(Xf)) + log2(1 + (v % y) / v)
  344. // The correction factor: log(1 + d) ~ d; for very small d values, so
  345. // log2(1 + (v % y) / v) ~ LOG_2_RECIPROCAL * (v % y)/v
  346. // LOG_2_RECIPROCAL ~ 23/16
  347. correction = (23 * (orig_v & (y - 1))) >> 4;
  348. return v_f * (kLog2Table[v] + log_cnt) + correction;
  349. } else {
  350. return (float)(LOG_2_RECIPROCAL * v * log((double)v));
  351. }
  352. }
  353. static float FastLog2Slow(uint32_t v) {
  354. assert(v >= LOG_LOOKUP_IDX_MAX);
  355. if (v < APPROX_LOG_WITH_CORRECTION_MAX) {
  356. int log_cnt = 0;
  357. uint32_t y = 1;
  358. const uint32_t orig_v = v;
  359. double log_2;
  360. do {
  361. ++log_cnt;
  362. v = v >> 1;
  363. y = y << 1;
  364. } while (v >= LOG_LOOKUP_IDX_MAX);
  365. log_2 = kLog2Table[v] + log_cnt;
  366. if (orig_v >= APPROX_LOG_MAX) {
  367. // Since the division is still expensive, add this correction factor only
  368. // for large values of 'v'.
  369. const int correction = (23 * (orig_v & (y - 1))) >> 4;
  370. log_2 += (double)correction / orig_v;
  371. }
  372. return (float)log_2;
  373. } else {
  374. return (float)(LOG_2_RECIPROCAL * log((double)v));
  375. }
  376. }
  377. //------------------------------------------------------------------------------
  378. // Image transforms.
  379. // Mostly used to reduce code size + readability
  380. static WEBP_INLINE int GetMin(int a, int b) { return (a > b) ? b : a; }
  381. // In-place sum of each component with mod 256.
  382. static WEBP_INLINE void AddPixelsEq(uint32_t* a, uint32_t b) {
  383. const uint32_t alpha_and_green = (*a & 0xff00ff00u) + (b & 0xff00ff00u);
  384. const uint32_t red_and_blue = (*a & 0x00ff00ffu) + (b & 0x00ff00ffu);
  385. *a = (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
  386. }
  387. static WEBP_INLINE uint32_t Average2(uint32_t a0, uint32_t a1) {
  388. return (((a0 ^ a1) & 0xfefefefeL) >> 1) + (a0 & a1);
  389. }
  390. static WEBP_INLINE uint32_t Average3(uint32_t a0, uint32_t a1, uint32_t a2) {
  391. return Average2(Average2(a0, a2), a1);
  392. }
  393. static WEBP_INLINE uint32_t Average4(uint32_t a0, uint32_t a1,
  394. uint32_t a2, uint32_t a3) {
  395. return Average2(Average2(a0, a1), Average2(a2, a3));
  396. }
  397. static WEBP_INLINE uint32_t Clip255(uint32_t a) {
  398. if (a < 256) {
  399. return a;
  400. }
  401. // return 0, when a is a negative integer.
  402. // return 255, when a is positive.
  403. return ~a >> 24;
  404. }
  405. static WEBP_INLINE int AddSubtractComponentFull(int a, int b, int c) {
  406. return Clip255(a + b - c);
  407. }
  408. static WEBP_INLINE uint32_t ClampedAddSubtractFull(uint32_t c0, uint32_t c1,
  409. uint32_t c2) {
  410. const int a = AddSubtractComponentFull(c0 >> 24, c1 >> 24, c2 >> 24);
  411. const int r = AddSubtractComponentFull((c0 >> 16) & 0xff,
  412. (c1 >> 16) & 0xff,
  413. (c2 >> 16) & 0xff);
  414. const int g = AddSubtractComponentFull((c0 >> 8) & 0xff,
  415. (c1 >> 8) & 0xff,
  416. (c2 >> 8) & 0xff);
  417. const int b = AddSubtractComponentFull(c0 & 0xff, c1 & 0xff, c2 & 0xff);
  418. return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
  419. }
  420. static WEBP_INLINE int AddSubtractComponentHalf(int a, int b) {
  421. return Clip255(a + (a - b) / 2);
  422. }
  423. static WEBP_INLINE uint32_t ClampedAddSubtractHalf(uint32_t c0, uint32_t c1,
  424. uint32_t c2) {
  425. const uint32_t ave = Average2(c0, c1);
  426. const int a = AddSubtractComponentHalf(ave >> 24, c2 >> 24);
  427. const int r = AddSubtractComponentHalf((ave >> 16) & 0xff, (c2 >> 16) & 0xff);
  428. const int g = AddSubtractComponentHalf((ave >> 8) & 0xff, (c2 >> 8) & 0xff);
  429. const int b = AddSubtractComponentHalf((ave >> 0) & 0xff, (c2 >> 0) & 0xff);
  430. return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
  431. }
  432. // gcc-4.9 on ARM generates incorrect code in Select() when Sub3() is inlined.
  433. #if defined(__arm__) && LOCAL_GCC_VERSION == 0x409
  434. # define LOCAL_INLINE __attribute__ ((noinline))
  435. #else
  436. # define LOCAL_INLINE WEBP_INLINE
  437. #endif
  438. static LOCAL_INLINE int Sub3(int a, int b, int c) {
  439. const int pb = b - c;
  440. const int pa = a - c;
  441. return abs(pb) - abs(pa);
  442. }
  443. #undef LOCAL_INLINE
  444. static WEBP_INLINE uint32_t Select(uint32_t a, uint32_t b, uint32_t c) {
  445. const int pa_minus_pb =
  446. Sub3((a >> 24) , (b >> 24) , (c >> 24) ) +
  447. Sub3((a >> 16) & 0xff, (b >> 16) & 0xff, (c >> 16) & 0xff) +
  448. Sub3((a >> 8) & 0xff, (b >> 8) & 0xff, (c >> 8) & 0xff) +
  449. Sub3((a ) & 0xff, (b ) & 0xff, (c ) & 0xff);
  450. return (pa_minus_pb <= 0) ? a : b;
  451. }
  452. //------------------------------------------------------------------------------
  453. // Predictors
  454. static uint32_t Predictor0(uint32_t left, const uint32_t* const top) {
  455. (void)top;
  456. (void)left;
  457. return ARGB_BLACK;
  458. }
  459. static uint32_t Predictor1(uint32_t left, const uint32_t* const top) {
  460. (void)top;
  461. return left;
  462. }
  463. static uint32_t Predictor2(uint32_t left, const uint32_t* const top) {
  464. (void)left;
  465. return top[0];
  466. }
  467. static uint32_t Predictor3(uint32_t left, const uint32_t* const top) {
  468. (void)left;
  469. return top[1];
  470. }
  471. static uint32_t Predictor4(uint32_t left, const uint32_t* const top) {
  472. (void)left;
  473. return top[-1];
  474. }
  475. static uint32_t Predictor5(uint32_t left, const uint32_t* const top) {
  476. const uint32_t pred = Average3(left, top[0], top[1]);
  477. return pred;
  478. }
  479. static uint32_t Predictor6(uint32_t left, const uint32_t* const top) {
  480. const uint32_t pred = Average2(left, top[-1]);
  481. return pred;
  482. }
  483. static uint32_t Predictor7(uint32_t left, const uint32_t* const top) {
  484. const uint32_t pred = Average2(left, top[0]);
  485. return pred;
  486. }
  487. static uint32_t Predictor8(uint32_t left, const uint32_t* const top) {
  488. const uint32_t pred = Average2(top[-1], top[0]);
  489. (void)left;
  490. return pred;
  491. }
  492. static uint32_t Predictor9(uint32_t left, const uint32_t* const top) {
  493. const uint32_t pred = Average2(top[0], top[1]);
  494. (void)left;
  495. return pred;
  496. }
  497. static uint32_t Predictor10(uint32_t left, const uint32_t* const top) {
  498. const uint32_t pred = Average4(left, top[-1], top[0], top[1]);
  499. return pred;
  500. }
  501. static uint32_t Predictor11(uint32_t left, const uint32_t* const top) {
  502. const uint32_t pred = Select(top[0], left, top[-1]);
  503. return pred;
  504. }
  505. static uint32_t Predictor12(uint32_t left, const uint32_t* const top) {
  506. const uint32_t pred = ClampedAddSubtractFull(left, top[0], top[-1]);
  507. return pred;
  508. }
  509. static uint32_t Predictor13(uint32_t left, const uint32_t* const top) {
  510. const uint32_t pred = ClampedAddSubtractHalf(left, top[0], top[-1]);
  511. return pred;
  512. }
  513. static const VP8LPredictorFunc kPredictorsC[16] = {
  514. Predictor0, Predictor1, Predictor2, Predictor3,
  515. Predictor4, Predictor5, Predictor6, Predictor7,
  516. Predictor8, Predictor9, Predictor10, Predictor11,
  517. Predictor12, Predictor13,
  518. Predictor0, Predictor0 // <- padding security sentinels
  519. };
  520. static float PredictionCostSpatial(const int counts[256], int weight_0,
  521. double exp_val) {
  522. const int significant_symbols = 256 >> 4;
  523. const double exp_decay_factor = 0.6;
  524. double bits = weight_0 * counts[0];
  525. int i;
  526. for (i = 1; i < significant_symbols; ++i) {
  527. bits += exp_val * (counts[i] + counts[256 - i]);
  528. exp_val *= exp_decay_factor;
  529. }
  530. return (float)(-0.1 * bits);
  531. }
  532. // Compute the combined Shanon's entropy for distribution {X} and {X+Y}
  533. static float CombinedShannonEntropy(const int X[256], const int Y[256]) {
  534. int i;
  535. double retval = 0.;
  536. int sumX = 0, sumXY = 0;
  537. for (i = 0; i < 256; ++i) {
  538. const int x = X[i];
  539. const int xy = x + Y[i];
  540. if (x != 0) {
  541. sumX += x;
  542. retval -= VP8LFastSLog2(x);
  543. sumXY += xy;
  544. retval -= VP8LFastSLog2(xy);
  545. } else if (xy != 0) {
  546. sumXY += xy;
  547. retval -= VP8LFastSLog2(xy);
  548. }
  549. }
  550. retval += VP8LFastSLog2(sumX) + VP8LFastSLog2(sumXY);
  551. return (float)retval;
  552. }
  553. static float PredictionCostSpatialHistogram(const int accumulated[4][256],
  554. const int tile[4][256]) {
  555. int i;
  556. double retval = 0;
  557. for (i = 0; i < 4; ++i) {
  558. const double kExpValue = 0.94;
  559. retval += PredictionCostSpatial(tile[i], 1, kExpValue);
  560. retval += CombinedShannonEntropy(tile[i], accumulated[i]);
  561. }
  562. return (float)retval;
  563. }
  564. static WEBP_INLINE void UpdateHisto(int histo_argb[4][256], uint32_t argb) {
  565. ++histo_argb[0][argb >> 24];
  566. ++histo_argb[1][(argb >> 16) & 0xff];
  567. ++histo_argb[2][(argb >> 8) & 0xff];
  568. ++histo_argb[3][argb & 0xff];
  569. }
  570. static int GetBestPredictorForTile(int width, int height,
  571. int tile_x, int tile_y, int bits,
  572. const int accumulated[4][256],
  573. const uint32_t* const argb_scratch) {
  574. const int kNumPredModes = 14;
  575. const int col_start = tile_x << bits;
  576. const int row_start = tile_y << bits;
  577. const int tile_size = 1 << bits;
  578. const int max_y = GetMin(tile_size, height - row_start);
  579. const int max_x = GetMin(tile_size, width - col_start);
  580. float best_diff = MAX_DIFF_COST;
  581. int best_mode = 0;
  582. int mode;
  583. for (mode = 0; mode < kNumPredModes; ++mode) {
  584. const uint32_t* current_row = argb_scratch;
  585. const VP8LPredictorFunc pred_func = VP8LPredictors[mode];
  586. float cur_diff;
  587. int y;
  588. int histo_argb[4][256];
  589. memset(histo_argb, 0, sizeof(histo_argb));
  590. for (y = 0; y < max_y; ++y) {
  591. int x;
  592. const int row = row_start + y;
  593. const uint32_t* const upper_row = current_row;
  594. current_row = upper_row + width;
  595. for (x = 0; x < max_x; ++x) {
  596. const int col = col_start + x;
  597. uint32_t predict;
  598. if (row == 0) {
  599. predict = (col == 0) ? ARGB_BLACK : current_row[col - 1]; // Left.
  600. } else if (col == 0) {
  601. predict = upper_row[col]; // Top.
  602. } else {
  603. predict = pred_func(current_row[col - 1], upper_row + col);
  604. }
  605. UpdateHisto(histo_argb, VP8LSubPixels(current_row[col], predict));
  606. }
  607. }
  608. cur_diff = PredictionCostSpatialHistogram(
  609. accumulated, (const int (*)[256])histo_argb);
  610. if (cur_diff < best_diff) {
  611. best_diff = cur_diff;
  612. best_mode = mode;
  613. }
  614. }
  615. return best_mode;
  616. }
  617. static void CopyTileWithPrediction(int width, int height,
  618. int tile_x, int tile_y, int bits, int mode,
  619. const uint32_t* const argb_scratch,
  620. uint32_t* const argb) {
  621. const int col_start = tile_x << bits;
  622. const int row_start = tile_y << bits;
  623. const int tile_size = 1 << bits;
  624. const int max_y = GetMin(tile_size, height - row_start);
  625. const int max_x = GetMin(tile_size, width - col_start);
  626. const VP8LPredictorFunc pred_func = VP8LPredictors[mode];
  627. const uint32_t* current_row = argb_scratch;
  628. int y;
  629. for (y = 0; y < max_y; ++y) {
  630. int x;
  631. const int row = row_start + y;
  632. const uint32_t* const upper_row = current_row;
  633. current_row = upper_row + width;
  634. for (x = 0; x < max_x; ++x) {
  635. const int col = col_start + x;
  636. const int pix = row * width + col;
  637. uint32_t predict;
  638. if (row == 0) {
  639. predict = (col == 0) ? ARGB_BLACK : current_row[col - 1]; // Left.
  640. } else if (col == 0) {
  641. predict = upper_row[col]; // Top.
  642. } else {
  643. predict = pred_func(current_row[col - 1], upper_row + col);
  644. }
  645. argb[pix] = VP8LSubPixels(current_row[col], predict);
  646. }
  647. }
  648. }
  649. void VP8LResidualImage(int width, int height, int bits,
  650. uint32_t* const argb, uint32_t* const argb_scratch,
  651. uint32_t* const image) {
  652. const int max_tile_size = 1 << bits;
  653. const int tiles_per_row = VP8LSubSampleSize(width, bits);
  654. const int tiles_per_col = VP8LSubSampleSize(height, bits);
  655. uint32_t* const upper_row = argb_scratch;
  656. uint32_t* const current_tile_rows = argb_scratch + width;
  657. int tile_y;
  658. int histo[4][256];
  659. memset(histo, 0, sizeof(histo));
  660. for (tile_y = 0; tile_y < tiles_per_col; ++tile_y) {
  661. const int tile_y_offset = tile_y * max_tile_size;
  662. const int this_tile_height =
  663. (tile_y < tiles_per_col - 1) ? max_tile_size : height - tile_y_offset;
  664. int tile_x;
  665. if (tile_y > 0) {
  666. memcpy(upper_row, current_tile_rows + (max_tile_size - 1) * width,
  667. width * sizeof(*upper_row));
  668. }
  669. memcpy(current_tile_rows, &argb[tile_y_offset * width],
  670. this_tile_height * width * sizeof(*current_tile_rows));
  671. for (tile_x = 0; tile_x < tiles_per_row; ++tile_x) {
  672. int pred;
  673. int y;
  674. const int tile_x_offset = tile_x * max_tile_size;
  675. int all_x_max = tile_x_offset + max_tile_size;
  676. if (all_x_max > width) {
  677. all_x_max = width;
  678. }
  679. pred = GetBestPredictorForTile(width, height, tile_x, tile_y, bits,
  680. (const int (*)[256])histo,
  681. argb_scratch);
  682. image[tile_y * tiles_per_row + tile_x] = 0xff000000u | (pred << 8);
  683. CopyTileWithPrediction(width, height, tile_x, tile_y, bits, pred,
  684. argb_scratch, argb);
  685. for (y = 0; y < max_tile_size; ++y) {
  686. int ix;
  687. int all_x;
  688. int all_y = tile_y_offset + y;
  689. if (all_y >= height) {
  690. break;
  691. }
  692. ix = all_y * width + tile_x_offset;
  693. for (all_x = tile_x_offset; all_x < all_x_max; ++all_x, ++ix) {
  694. UpdateHisto(histo, argb[ix]);
  695. }
  696. }
  697. }
  698. }
  699. }
  700. // Inverse prediction.
  701. static void PredictorInverseTransform(const VP8LTransform* const transform,
  702. int y_start, int y_end, uint32_t* data) {
  703. const int width = transform->xsize_;
  704. if (y_start == 0) { // First Row follows the L (mode=1) mode.
  705. int x;
  706. const uint32_t pred0 = Predictor0(data[-1], NULL);
  707. AddPixelsEq(data, pred0);
  708. for (x = 1; x < width; ++x) {
  709. const uint32_t pred1 = Predictor1(data[x - 1], NULL);
  710. AddPixelsEq(data + x, pred1);
  711. }
  712. data += width;
  713. ++y_start;
  714. }
  715. {
  716. int y = y_start;
  717. const int tile_width = 1 << transform->bits_;
  718. const int mask = tile_width - 1;
  719. const int safe_width = width & ~mask;
  720. const int tiles_per_row = VP8LSubSampleSize(width, transform->bits_);
  721. const uint32_t* pred_mode_base =
  722. transform->data_ + (y >> transform->bits_) * tiles_per_row;
  723. while (y < y_end) {
  724. const uint32_t pred2 = Predictor2(data[-1], data - width);
  725. const uint32_t* pred_mode_src = pred_mode_base;
  726. VP8LPredictorFunc pred_func;
  727. int x = 1;
  728. int t = 1;
  729. // First pixel follows the T (mode=2) mode.
  730. AddPixelsEq(data, pred2);
  731. // .. the rest:
  732. while (x < safe_width) {
  733. pred_func = VP8LPredictors[((*pred_mode_src++) >> 8) & 0xf];
  734. for (; t < tile_width; ++t, ++x) {
  735. const uint32_t pred = pred_func(data[x - 1], data + x - width);
  736. AddPixelsEq(data + x, pred);
  737. }
  738. t = 0;
  739. }
  740. if (x < width) {
  741. pred_func = VP8LPredictors[((*pred_mode_src++) >> 8) & 0xf];
  742. for (; x < width; ++x) {
  743. const uint32_t pred = pred_func(data[x - 1], data + x - width);
  744. AddPixelsEq(data + x, pred);
  745. }
  746. }
  747. data += width;
  748. ++y;
  749. if ((y & mask) == 0) { // Use the same mask, since tiles are squares.
  750. pred_mode_base += tiles_per_row;
  751. }
  752. }
  753. }
  754. }
  755. void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data, int num_pixels) {
  756. int i;
  757. for (i = 0; i < num_pixels; ++i) {
  758. const uint32_t argb = argb_data[i];
  759. const uint32_t green = (argb >> 8) & 0xff;
  760. const uint32_t new_r = (((argb >> 16) & 0xff) - green) & 0xff;
  761. const uint32_t new_b = ((argb & 0xff) - green) & 0xff;
  762. argb_data[i] = (argb & 0xff00ff00) | (new_r << 16) | new_b;
  763. }
  764. }
  765. // Add green to blue and red channels (i.e. perform the inverse transform of
  766. // 'subtract green').
  767. void VP8LAddGreenToBlueAndRed_C(uint32_t* data, int num_pixels) {
  768. int i;
  769. for (i = 0; i < num_pixels; ++i) {
  770. const uint32_t argb = data[i];
  771. const uint32_t green = ((argb >> 8) & 0xff);
  772. uint32_t red_blue = (argb & 0x00ff00ffu);
  773. red_blue += (green << 16) | green;
  774. red_blue &= 0x00ff00ffu;
  775. data[i] = (argb & 0xff00ff00u) | red_blue;
  776. }
  777. }
  778. static WEBP_INLINE void MultipliersClear(VP8LMultipliers* const m) {
  779. m->green_to_red_ = 0;
  780. m->green_to_blue_ = 0;
  781. m->red_to_blue_ = 0;
  782. }
  783. static WEBP_INLINE uint32_t ColorTransformDelta(int8_t color_pred,
  784. int8_t color) {
  785. return (uint32_t)((int)(color_pred) * color) >> 5;
  786. }
  787. static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
  788. VP8LMultipliers* const m) {
  789. m->green_to_red_ = (color_code >> 0) & 0xff;
  790. m->green_to_blue_ = (color_code >> 8) & 0xff;
  791. m->red_to_blue_ = (color_code >> 16) & 0xff;
  792. }
  793. static WEBP_INLINE uint32_t MultipliersToColorCode(
  794. const VP8LMultipliers* const m) {
  795. return 0xff000000u |
  796. ((uint32_t)(m->red_to_blue_) << 16) |
  797. ((uint32_t)(m->green_to_blue_) << 8) |
  798. m->green_to_red_;
  799. }
  800. void VP8LTransformColor_C(const VP8LMultipliers* const m, uint32_t* data,
  801. int num_pixels) {
  802. int i;
  803. for (i = 0; i < num_pixels; ++i) {
  804. const uint32_t argb = data[i];
  805. const uint32_t green = argb >> 8;
  806. const uint32_t red = argb >> 16;
  807. uint32_t new_red = red;
  808. uint32_t new_blue = argb;
  809. new_red -= ColorTransformDelta(m->green_to_red_, green);
  810. new_red &= 0xff;
  811. new_blue -= ColorTransformDelta(m->green_to_blue_, green);
  812. new_blue -= ColorTransformDelta(m->red_to_blue_, red);
  813. new_blue &= 0xff;
  814. data[i] = (argb & 0xff00ff00u) | (new_red << 16) | (new_blue);
  815. }
  816. }
  817. void VP8LTransformColorInverse_C(const VP8LMultipliers* const m, uint32_t* data,
  818. int num_pixels) {
  819. int i;
  820. for (i = 0; i < num_pixels; ++i) {
  821. const uint32_t argb = data[i];
  822. const uint32_t green = argb >> 8;
  823. const uint32_t red = argb >> 16;
  824. uint32_t new_red = red;
  825. uint32_t new_blue = argb;
  826. new_red += ColorTransformDelta(m->green_to_red_, green);
  827. new_red &= 0xff;
  828. new_blue += ColorTransformDelta(m->green_to_blue_, green);
  829. new_blue += ColorTransformDelta(m->red_to_blue_, new_red);
  830. new_blue &= 0xff;
  831. data[i] = (argb & 0xff00ff00u) | (new_red << 16) | (new_blue);
  832. }
  833. }
  834. static WEBP_INLINE uint8_t TransformColorRed(uint8_t green_to_red,
  835. uint32_t argb) {
  836. const uint32_t green = argb >> 8;
  837. uint32_t new_red = argb >> 16;
  838. new_red -= ColorTransformDelta(green_to_red, green);
  839. return (new_red & 0xff);
  840. }
  841. static WEBP_INLINE uint8_t TransformColorBlue(uint8_t green_to_blue,
  842. uint8_t red_to_blue,
  843. uint32_t argb) {
  844. const uint32_t green = argb >> 8;
  845. const uint32_t red = argb >> 16;
  846. uint8_t new_blue = argb;
  847. new_blue -= ColorTransformDelta(green_to_blue, green);
  848. new_blue -= ColorTransformDelta(red_to_blue, red);
  849. return (new_blue & 0xff);
  850. }
  851. static float PredictionCostCrossColor(const int accumulated[256],
  852. const int counts[256]) {
  853. // Favor low entropy, locally and globally.
  854. // Favor small absolute values for PredictionCostSpatial
  855. static const double kExpValue = 2.4;
  856. return CombinedShannonEntropy(counts, accumulated) +
  857. PredictionCostSpatial(counts, 3, kExpValue);
  858. }
  859. static float GetPredictionCostCrossColorRed(
  860. int tile_x_offset, int tile_y_offset, int all_x_max, int all_y_max,
  861. int xsize, VP8LMultipliers prev_x, VP8LMultipliers prev_y, int green_to_red,
  862. const int accumulated_red_histo[256], const uint32_t* const argb) {
  863. int all_y;
  864. int histo[256] = { 0 };
  865. float cur_diff;
  866. for (all_y = tile_y_offset; all_y < all_y_max; ++all_y) {
  867. int ix = all_y * xsize + tile_x_offset;
  868. int all_x;
  869. for (all_x = tile_x_offset; all_x < all_x_max; ++all_x, ++ix) {
  870. ++histo[TransformColorRed(green_to_red, argb[ix])]; // red.
  871. }
  872. }
  873. cur_diff = PredictionCostCrossColor(accumulated_red_histo, histo);
  874. if ((uint8_t)green_to_red == prev_x.green_to_red_) {
  875. cur_diff -= 3; // favor keeping the areas locally similar
  876. }
  877. if ((uint8_t)green_to_red == prev_y.green_to_red_) {
  878. cur_diff -= 3; // favor keeping the areas locally similar
  879. }
  880. if (green_to_red == 0) {
  881. cur_diff -= 3;
  882. }
  883. return cur_diff;
  884. }
  885. static void GetBestGreenToRed(
  886. int tile_x_offset, int tile_y_offset, int all_x_max, int all_y_max,
  887. int xsize, VP8LMultipliers prev_x, VP8LMultipliers prev_y,
  888. const int accumulated_red_histo[256], const uint32_t* const argb,
  889. VP8LMultipliers* const best_tx) {
  890. int min_green_to_red = -64;
  891. int max_green_to_red = 64;
  892. int green_to_red = 0;
  893. int eval_min = 1;
  894. int eval_max = 1;
  895. float cur_diff_min = MAX_DIFF_COST;
  896. float cur_diff_max = MAX_DIFF_COST;
  897. // Do a binary search to find the optimal green_to_red color transform.
  898. while (max_green_to_red - min_green_to_red > 2) {
  899. if (eval_min) {
  900. cur_diff_min = GetPredictionCostCrossColorRed(
  901. tile_x_offset, tile_y_offset, all_x_max, all_y_max, xsize,
  902. prev_x, prev_y, min_green_to_red, accumulated_red_histo, argb);
  903. eval_min = 0;
  904. }
  905. if (eval_max) {
  906. cur_diff_max = GetPredictionCostCrossColorRed(
  907. tile_x_offset, tile_y_offset, all_x_max, all_y_max, xsize,
  908. prev_x, prev_y, max_green_to_red, accumulated_red_histo, argb);
  909. eval_max = 0;
  910. }
  911. if (cur_diff_min < cur_diff_max) {
  912. green_to_red = min_green_to_red;
  913. max_green_to_red = (max_green_to_red + min_green_to_red) / 2;
  914. eval_max = 1;
  915. } else {
  916. green_to_red = max_green_to_red;
  917. min_green_to_red = (max_green_to_red + min_green_to_red) / 2;
  918. eval_min = 1;
  919. }
  920. }
  921. best_tx->green_to_red_ = green_to_red;
  922. }
  923. static float GetPredictionCostCrossColorBlue(
  924. int tile_x_offset, int tile_y_offset, int all_x_max, int all_y_max,
  925. int xsize, VP8LMultipliers prev_x, VP8LMultipliers prev_y,
  926. int green_to_blue, int red_to_blue, const int accumulated_blue_histo[256],
  927. const uint32_t* const argb) {
  928. int all_y;
  929. int histo[256] = { 0 };
  930. float cur_diff;
  931. for (all_y = tile_y_offset; all_y < all_y_max; ++all_y) {
  932. int all_x;
  933. int ix = all_y * xsize + tile_x_offset;
  934. for (all_x = tile_x_offset; all_x < all_x_max; ++all_x, ++ix) {
  935. ++histo[TransformColorBlue(green_to_blue, red_to_blue, argb[ix])];
  936. }
  937. }
  938. cur_diff = PredictionCostCrossColor(accumulated_blue_histo, histo);
  939. if ((uint8_t)green_to_blue == prev_x.green_to_blue_) {
  940. cur_diff -= 3; // favor keeping the areas locally similar
  941. }
  942. if ((uint8_t)green_to_blue == prev_y.green_to_blue_) {
  943. cur_diff -= 3; // favor keeping the areas locally similar
  944. }
  945. if ((uint8_t)red_to_blue == prev_x.red_to_blue_) {
  946. cur_diff -= 3; // favor keeping the areas locally similar
  947. }
  948. if ((uint8_t)red_to_blue == prev_y.red_to_blue_) {
  949. cur_diff -= 3; // favor keeping the areas locally similar
  950. }
  951. if (green_to_blue == 0) {
  952. cur_diff -= 3;
  953. }
  954. if (red_to_blue == 0) {
  955. cur_diff -= 3;
  956. }
  957. return cur_diff;
  958. }
  959. static void GetBestGreenRedToBlue(
  960. int tile_x_offset, int tile_y_offset, int all_x_max, int all_y_max,
  961. int xsize, VP8LMultipliers prev_x, VP8LMultipliers prev_y, int quality,
  962. const int accumulated_blue_histo[256], const uint32_t* const argb,
  963. VP8LMultipliers* const best_tx) {
  964. float best_diff = MAX_DIFF_COST;
  965. float cur_diff;
  966. const int step = (quality < 25) ? 32 : (quality > 50) ? 8 : 16;
  967. const int min_green_to_blue = -32;
  968. const int max_green_to_blue = 32;
  969. const int min_red_to_blue = -32;
  970. const int max_red_to_blue = 32;
  971. const int num_iters =
  972. (1 + (max_green_to_blue - min_green_to_blue) / step) *
  973. (1 + (max_red_to_blue - min_red_to_blue) / step);
  974. // Number of tries to get optimal green_to_blue & red_to_blue color transforms
  975. // after finding a local minima.
  976. const int max_tries_after_min = 4 + (num_iters >> 2);
  977. int num_tries_after_min = 0;
  978. int green_to_blue;
  979. for (green_to_blue = min_green_to_blue;
  980. green_to_blue <= max_green_to_blue &&
  981. num_tries_after_min < max_tries_after_min;
  982. green_to_blue += step) {
  983. int red_to_blue;
  984. for (red_to_blue = min_red_to_blue;
  985. red_to_blue <= max_red_to_blue &&
  986. num_tries_after_min < max_tries_after_min;
  987. red_to_blue += step) {
  988. cur_diff = GetPredictionCostCrossColorBlue(
  989. tile_x_offset, tile_y_offset, all_x_max, all_y_max, xsize, prev_x,
  990. prev_y, green_to_blue, red_to_blue, accumulated_blue_histo, argb);
  991. if (cur_diff < best_diff) {
  992. best_diff = cur_diff;
  993. best_tx->green_to_blue_ = green_to_blue;
  994. best_tx->red_to_blue_ = red_to_blue;
  995. num_tries_after_min = 0;
  996. } else {
  997. ++num_tries_after_min;
  998. }
  999. }
  1000. }
  1001. }
  1002. static VP8LMultipliers GetBestColorTransformForTile(
  1003. int tile_x, int tile_y, int bits,
  1004. VP8LMultipliers prev_x,
  1005. VP8LMultipliers prev_y,
  1006. int quality, int xsize, int ysize,
  1007. const int accumulated_red_histo[256],
  1008. const int accumulated_blue_histo[256],
  1009. const uint32_t* const argb) {
  1010. const int max_tile_size = 1 << bits;
  1011. const int tile_y_offset = tile_y * max_tile_size;
  1012. const int tile_x_offset = tile_x * max_tile_size;
  1013. const int all_x_max = GetMin(tile_x_offset + max_tile_size, xsize);
  1014. const int all_y_max = GetMin(tile_y_offset + max_tile_size, ysize);
  1015. VP8LMultipliers best_tx;
  1016. MultipliersClear(&best_tx);
  1017. GetBestGreenToRed(tile_x_offset, tile_y_offset, all_x_max, all_y_max, xsize,
  1018. prev_x, prev_y, accumulated_red_histo, argb, &best_tx);
  1019. GetBestGreenRedToBlue(tile_x_offset, tile_y_offset, all_x_max, all_y_max,
  1020. xsize, prev_x, prev_y, quality, accumulated_blue_histo,
  1021. argb, &best_tx);
  1022. return best_tx;
  1023. }
  1024. static void CopyTileWithColorTransform(int xsize, int ysize,
  1025. int tile_x, int tile_y,
  1026. int max_tile_size,
  1027. VP8LMultipliers color_transform,
  1028. uint32_t* argb) {
  1029. const int xscan = GetMin(max_tile_size, xsize - tile_x);
  1030. int yscan = GetMin(max_tile_size, ysize - tile_y);
  1031. argb += tile_y * xsize + tile_x;
  1032. while (yscan-- > 0) {
  1033. VP8LTransformColor(&color_transform, argb, xscan);
  1034. argb += xsize;
  1035. }
  1036. }
  1037. void VP8LColorSpaceTransform(int width, int height, int bits, int quality,
  1038. uint32_t* const argb, uint32_t* image) {
  1039. const int max_tile_size = 1 << bits;
  1040. const int tile_xsize = VP8LSubSampleSize(width, bits);
  1041. const int tile_ysize = VP8LSubSampleSize(height, bits);
  1042. int accumulated_red_histo[256] = { 0 };
  1043. int accumulated_blue_histo[256] = { 0 };
  1044. int tile_x, tile_y;
  1045. VP8LMultipliers prev_x, prev_y;
  1046. MultipliersClear(&prev_y);
  1047. MultipliersClear(&prev_x);
  1048. for (tile_y = 0; tile_y < tile_ysize; ++tile_y) {
  1049. for (tile_x = 0; tile_x < tile_xsize; ++tile_x) {
  1050. int y;
  1051. const int tile_x_offset = tile_x * max_tile_size;
  1052. const int tile_y_offset = tile_y * max_tile_size;
  1053. const int all_x_max = GetMin(tile_x_offset + max_tile_size, width);
  1054. const int all_y_max = GetMin(tile_y_offset + max_tile_size, height);
  1055. const int offset = tile_y * tile_xsize + tile_x;
  1056. if (tile_y != 0) {
  1057. ColorCodeToMultipliers(image[offset - tile_xsize], &prev_y);
  1058. }
  1059. prev_x = GetBestColorTransformForTile(tile_x, tile_y, bits,
  1060. prev_x, prev_y,
  1061. quality, width, height,
  1062. accumulated_red_histo,
  1063. accumulated_blue_histo,
  1064. argb);
  1065. image[offset] = MultipliersToColorCode(&prev_x);
  1066. CopyTileWithColorTransform(width, height, tile_x_offset, tile_y_offset,
  1067. max_tile_size, prev_x, argb);
  1068. // Gather accumulated histogram data.
  1069. for (y = tile_y_offset; y < all_y_max; ++y) {
  1070. int ix = y * width + tile_x_offset;
  1071. const int ix_end = ix + all_x_max - tile_x_offset;
  1072. for (; ix < ix_end; ++ix) {
  1073. const uint32_t pix = argb[ix];
  1074. if (ix >= 2 &&
  1075. pix == argb[ix - 2] &&
  1076. pix == argb[ix - 1]) {
  1077. continue; // repeated pixels are handled by backward references
  1078. }
  1079. if (ix >= width + 2 &&
  1080. argb[ix - 2] == argb[ix - width - 2] &&
  1081. argb[ix - 1] == argb[ix - width - 1] &&
  1082. pix == argb[ix - width]) {
  1083. continue; // repeated pixels are handled by backward references
  1084. }
  1085. ++accumulated_red_histo[(pix >> 16) & 0xff];
  1086. ++accumulated_blue_histo[(pix >> 0) & 0xff];
  1087. }
  1088. }
  1089. }
  1090. }
  1091. }
  1092. // Color space inverse transform.
  1093. static void ColorSpaceInverseTransform(const VP8LTransform* const transform,
  1094. int y_start, int y_end, uint32_t* data) {
  1095. const int width = transform->xsize_;
  1096. const int tile_width = 1 << transform->bits_;
  1097. const int mask = tile_width - 1;
  1098. const int safe_width = width & ~mask;
  1099. const int remaining_width = width - safe_width;
  1100. const int tiles_per_row = VP8LSubSampleSize(width, transform->bits_);
  1101. int y = y_start;
  1102. const uint32_t* pred_row =
  1103. transform->data_ + (y >> transform->bits_) * tiles_per_row;
  1104. while (y < y_end) {
  1105. const uint32_t* pred = pred_row;
  1106. VP8LMultipliers m = { 0, 0, 0 };
  1107. const uint32_t* const data_safe_end = data + safe_width;
  1108. const uint32_t* const data_end = data + width;
  1109. while (data < data_safe_end) {
  1110. ColorCodeToMultipliers(*pred++, &m);
  1111. VP8LTransformColorInverse(&m, data, tile_width);
  1112. data += tile_width;
  1113. }
  1114. if (data < data_end) { // Left-overs using C-version.
  1115. ColorCodeToMultipliers(*pred++, &m);
  1116. VP8LTransformColorInverse(&m, data, remaining_width);
  1117. data += remaining_width;
  1118. }
  1119. ++y;
  1120. if ((y & mask) == 0) pred_row += tiles_per_row;
  1121. }
  1122. }
  1123. // Separate out pixels packed together using pixel-bundling.
  1124. // We define two methods for ARGB data (uint32_t) and alpha-only data (uint8_t).
  1125. #define COLOR_INDEX_INVERSE(FUNC_NAME, TYPE, GET_INDEX, GET_VALUE) \
  1126. void FUNC_NAME(const VP8LTransform* const transform, \
  1127. int y_start, int y_end, const TYPE* src, TYPE* dst) { \
  1128. int y; \
  1129. const int bits_per_pixel = 8 >> transform->bits_; \
  1130. const int width = transform->xsize_; \
  1131. const uint32_t* const color_map = transform->data_; \
  1132. if (bits_per_pixel < 8) { \
  1133. const int pixels_per_byte = 1 << transform->bits_; \
  1134. const int count_mask = pixels_per_byte - 1; \
  1135. const uint32_t bit_mask = (1 << bits_per_pixel) - 1; \
  1136. for (y = y_start; y < y_end; ++y) { \
  1137. uint32_t packed_pixels = 0; \
  1138. int x; \
  1139. for (x = 0; x < width; ++x) { \
  1140. /* We need to load fresh 'packed_pixels' once every */ \
  1141. /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */ \
  1142. /* is a power of 2, so can just use a mask for that, instead of */ \
  1143. /* decrementing a counter. */ \
  1144. if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++); \
  1145. *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]); \
  1146. packed_pixels >>= bits_per_pixel; \
  1147. } \
  1148. } \
  1149. } else { \
  1150. for (y = y_start; y < y_end; ++y) { \
  1151. int x; \
  1152. for (x = 0; x < width; ++x) { \
  1153. *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]); \
  1154. } \
  1155. } \
  1156. } \
  1157. }
  1158. static WEBP_INLINE uint32_t GetARGBIndex(uint32_t idx) {
  1159. return (idx >> 8) & 0xff;
  1160. }
  1161. static WEBP_INLINE uint8_t GetAlphaIndex(uint8_t idx) {
  1162. return idx;
  1163. }
  1164. static WEBP_INLINE uint32_t GetARGBValue(uint32_t val) {
  1165. return val;
  1166. }
  1167. static WEBP_INLINE uint8_t GetAlphaValue(uint32_t val) {
  1168. return (val >> 8) & 0xff;
  1169. }
  1170. static COLOR_INDEX_INVERSE(ColorIndexInverseTransform, uint32_t, GetARGBIndex,
  1171. GetARGBValue)
  1172. COLOR_INDEX_INVERSE(VP8LColorIndexInverseTransformAlpha, uint8_t, GetAlphaIndex,
  1173. GetAlphaValue)
  1174. #undef COLOR_INDEX_INVERSE
  1175. void VP8LInverseTransform(const VP8LTransform* const transform,
  1176. int row_start, int row_end,
  1177. const uint32_t* const in, uint32_t* const out) {
  1178. const int width = transform->xsize_;
  1179. assert(row_start < row_end);
  1180. assert(row_end <= transform->ysize_);
  1181. switch (transform->type_) {
  1182. case SUBTRACT_GREEN:
  1183. VP8LAddGreenToBlueAndRed(out, (row_end - row_start) * width);
  1184. break;
  1185. case PREDICTOR_TRANSFORM:
  1186. PredictorInverseTransform(transform, row_start, row_end, out);
  1187. if (row_end != transform->ysize_) {
  1188. // The last predicted row in this iteration will be the top-pred row
  1189. // for the first row in next iteration.
  1190. memcpy(out - width, out + (row_end - row_start - 1) * width,
  1191. width * sizeof(*out));
  1192. }
  1193. break;
  1194. case CROSS_COLOR_TRANSFORM:
  1195. ColorSpaceInverseTransform(transform, row_start, row_end, out);
  1196. break;
  1197. case COLOR_INDEXING_TRANSFORM:
  1198. if (in == out && transform->bits_ > 0) {
  1199. // Move packed pixels to the end of unpacked region, so that unpacking
  1200. // can occur seamlessly.
  1201. // Also, note that this is the only transform that applies on
  1202. // the effective width of VP8LSubSampleSize(xsize_, bits_). All other
  1203. // transforms work on effective width of xsize_.
  1204. const int out_stride = (row_end - row_start) * width;
  1205. const int in_stride = (row_end - row_start) *
  1206. VP8LSubSampleSize(transform->xsize_, transform->bits_);
  1207. uint32_t* const src = out + out_stride - in_stride;
  1208. memmove(src, out, in_stride * sizeof(*src));
  1209. ColorIndexInverseTransform(transform, row_start, row_end, src, out);
  1210. } else {
  1211. ColorIndexInverseTransform(transform, row_start, row_end, in, out);
  1212. }
  1213. break;
  1214. }
  1215. }
  1216. //------------------------------------------------------------------------------
  1217. // Color space conversion.
  1218. static int is_big_endian(void) {
  1219. static const union {
  1220. uint16_t w;
  1221. uint8_t b[2];
  1222. } tmp = { 1 };
  1223. return (tmp.b[0] != 1);
  1224. }
  1225. void VP8LConvertBGRAToRGB_C(const uint32_t* src,
  1226. int num_pixels, uint8_t* dst) {
  1227. const uint32_t* const src_end = src + num_pixels;
  1228. while (src < src_end) {
  1229. const uint32_t argb = *src++;
  1230. *dst++ = (argb >> 16) & 0xff;
  1231. *dst++ = (argb >> 8) & 0xff;
  1232. *dst++ = (argb >> 0) & 0xff;
  1233. }
  1234. }
  1235. void VP8LConvertBGRAToRGBA_C(const uint32_t* src,
  1236. int num_pixels, uint8_t* dst) {
  1237. const uint32_t* const src_end = src + num_pixels;
  1238. while (src < src_end) {
  1239. const uint32_t argb = *src++;
  1240. *dst++ = (argb >> 16) & 0xff;
  1241. *dst++ = (argb >> 8) & 0xff;
  1242. *dst++ = (argb >> 0) & 0xff;
  1243. *dst++ = (argb >> 24) & 0xff;
  1244. }
  1245. }
  1246. void VP8LConvertBGRAToRGBA4444_C(const uint32_t* src,
  1247. int num_pixels, uint8_t* dst) {
  1248. const uint32_t* const src_end = src + num_pixels;
  1249. while (src < src_end) {
  1250. const uint32_t argb = *src++;
  1251. const uint8_t rg = ((argb >> 16) & 0xf0) | ((argb >> 12) & 0xf);
  1252. const uint8_t ba = ((argb >> 0) & 0xf0) | ((argb >> 28) & 0xf);
  1253. #ifdef WEBP_SWAP_16BIT_CSP
  1254. *dst++ = ba;
  1255. *dst++ = rg;
  1256. #else
  1257. *dst++ = rg;
  1258. *dst++ = ba;
  1259. #endif
  1260. }
  1261. }
  1262. void VP8LConvertBGRAToRGB565_C(const uint32_t* src,
  1263. int num_pixels, uint8_t* dst) {
  1264. const uint32_t* const src_end = src + num_pixels;
  1265. while (src < src_end) {
  1266. const uint32_t argb = *src++;
  1267. const uint8_t rg = ((argb >> 16) & 0xf8) | ((argb >> 13) & 0x7);
  1268. const uint8_t gb = ((argb >> 5) & 0xe0) | ((argb >> 3) & 0x1f);
  1269. #ifdef WEBP_SWAP_16BIT_CSP
  1270. *dst++ = gb;
  1271. *dst++ = rg;
  1272. #else
  1273. *dst++ = rg;
  1274. *dst++ = gb;
  1275. #endif
  1276. }
  1277. }
  1278. void VP8LConvertBGRAToBGR_C(const uint32_t* src,
  1279. int num_pixels, uint8_t* dst) {
  1280. const uint32_t* const src_end = src + num_pixels;
  1281. while (src < src_end) {
  1282. const uint32_t argb = *src++;
  1283. *dst++ = (argb >> 0) & 0xff;
  1284. *dst++ = (argb >> 8) & 0xff;
  1285. *dst++ = (argb >> 16) & 0xff;
  1286. }
  1287. }
  1288. static void CopyOrSwap(const uint32_t* src, int num_pixels, uint8_t* dst,
  1289. int swap_on_big_endian) {
  1290. if (is_big_endian() == swap_on_big_endian) {
  1291. const uint32_t* const src_end = src + num_pixels;
  1292. while (src < src_end) {
  1293. const uint32_t argb = *src++;
  1294. #if !defined(WORDS_BIGENDIAN)
  1295. #if !defined(WEBP_REFERENCE_IMPLEMENTATION)
  1296. *(uint32_t*)dst = BSwap32(argb);
  1297. #else // WEBP_REFERENCE_IMPLEMENTATION
  1298. dst[0] = (argb >> 24) & 0xff;
  1299. dst[1] = (argb >> 16) & 0xff;
  1300. dst[2] = (argb >> 8) & 0xff;
  1301. dst[3] = (argb >> 0) & 0xff;
  1302. #endif
  1303. #else // WORDS_BIGENDIAN
  1304. dst[0] = (argb >> 0) & 0xff;
  1305. dst[1] = (argb >> 8) & 0xff;
  1306. dst[2] = (argb >> 16) & 0xff;
  1307. dst[3] = (argb >> 24) & 0xff;
  1308. #endif
  1309. dst += sizeof(argb);
  1310. }
  1311. } else {
  1312. memcpy(dst, src, num_pixels * sizeof(*src));
  1313. }
  1314. }
  1315. void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels,
  1316. WEBP_CSP_MODE out_colorspace, uint8_t* const rgba) {
  1317. switch (out_colorspace) {
  1318. case MODE_RGB:
  1319. VP8LConvertBGRAToRGB(in_data, num_pixels, rgba);
  1320. break;
  1321. case MODE_RGBA:
  1322. VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba);
  1323. break;
  1324. case MODE_rgbA:
  1325. VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba);
  1326. WebPApplyAlphaMultiply(rgba, 0, num_pixels, 1, 0);
  1327. break;
  1328. case MODE_BGR:
  1329. VP8LConvertBGRAToBGR(in_data, num_pixels, rgba);
  1330. break;
  1331. case MODE_BGRA:
  1332. CopyOrSwap(in_data, num_pixels, rgba, 1);
  1333. break;
  1334. case MODE_bgrA:
  1335. CopyOrSwap(in_data, num_pixels, rgba, 1);
  1336. WebPApplyAlphaMultiply(rgba, 0, num_pixels, 1, 0);
  1337. break;
  1338. case MODE_ARGB:
  1339. CopyOrSwap(in_data, num_pixels, rgba, 0);
  1340. break;
  1341. case MODE_Argb:
  1342. CopyOrSwap(in_data, num_pixels, rgba, 0);
  1343. WebPApplyAlphaMultiply(rgba, 1, num_pixels, 1, 0);
  1344. break;
  1345. case MODE_RGBA_4444:
  1346. VP8LConvertBGRAToRGBA4444(in_data, num_pixels, rgba);
  1347. break;
  1348. case MODE_rgbA_4444:
  1349. VP8LConvertBGRAToRGBA4444(in_data, num_pixels, rgba);
  1350. WebPApplyAlphaMultiply4444(rgba, num_pixels, 1, 0);
  1351. break;
  1352. case MODE_RGB_565:
  1353. VP8LConvertBGRAToRGB565(in_data, num_pixels, rgba);
  1354. break;
  1355. default:
  1356. assert(0); // Code flow should not reach here.
  1357. }
  1358. }
  1359. //------------------------------------------------------------------------------
  1360. // Bundles multiple (1, 2, 4 or 8) pixels into a single pixel.
  1361. void VP8LBundleColorMap(const uint8_t* const row, int width,
  1362. int xbits, uint32_t* const dst) {
  1363. int x;
  1364. if (xbits > 0) {
  1365. const int bit_depth = 1 << (3 - xbits);
  1366. const int mask = (1 << xbits) - 1;
  1367. uint32_t code = 0xff000000;
  1368. for (x = 0; x < width; ++x) {
  1369. const int xsub = x & mask;
  1370. if (xsub == 0) {
  1371. code = 0xff000000;
  1372. }
  1373. code |= row[x] << (8 + bit_depth * xsub);
  1374. dst[x >> xbits] = code;
  1375. }
  1376. } else {
  1377. for (x = 0; x < width; ++x) dst[x] = 0xff000000 | (row[x] << 8);
  1378. }
  1379. }
  1380. //------------------------------------------------------------------------------
  1381. static double ExtraCost(const uint32_t* population, int length) {
  1382. int i;
  1383. double cost = 0.;
  1384. for (i = 2; i < length - 2; ++i) cost += (i >> 1) * population[i + 2];
  1385. return cost;
  1386. }
  1387. static double ExtraCostCombined(const uint32_t* X, const uint32_t* Y,
  1388. int length) {
  1389. int i;
  1390. double cost = 0.;
  1391. for (i = 2; i < length - 2; ++i) {
  1392. const int xy = X[i + 2] + Y[i + 2];
  1393. cost += (i >> 1) * xy;
  1394. }
  1395. return cost;
  1396. }
  1397. // Returns the various RLE counts
  1398. static VP8LStreaks HuffmanCostCount(const uint32_t* population, int length) {
  1399. int i;
  1400. int streak = 0;
  1401. VP8LStreaks stats;
  1402. memset(&stats, 0, sizeof(stats));
  1403. for (i = 0; i < length - 1; ++i) {
  1404. ++streak;
  1405. if (population[i] == population[i + 1]) {
  1406. continue;
  1407. }
  1408. stats.counts[population[i] != 0] += (streak > 3);
  1409. stats.streaks[population[i] != 0][(streak > 3)] += streak;
  1410. streak = 0;
  1411. }
  1412. ++streak;
  1413. stats.counts[population[i] != 0] += (streak > 3);
  1414. stats.streaks[population[i] != 0][(streak > 3)] += streak;
  1415. return stats;
  1416. }
  1417. static VP8LStreaks HuffmanCostCombinedCount(const uint32_t* X,
  1418. const uint32_t* Y, int length) {
  1419. int i;
  1420. int streak = 0;
  1421. VP8LStreaks stats;
  1422. memset(&stats, 0, sizeof(stats));
  1423. for (i = 0; i < length - 1; ++i) {
  1424. const int xy = X[i] + Y[i];
  1425. const int xy_next = X[i + 1] + Y[i + 1];
  1426. ++streak;
  1427. if (xy == xy_next) {
  1428. continue;
  1429. }
  1430. stats.counts[xy != 0] += (streak > 3);
  1431. stats.streaks[xy != 0][(streak > 3)] += streak;
  1432. streak = 0;
  1433. }
  1434. {
  1435. const int xy = X[i] + Y[i];
  1436. ++streak;
  1437. stats.counts[xy != 0] += (streak > 3);
  1438. stats.streaks[xy != 0][(streak > 3)] += streak;
  1439. }
  1440. return stats;
  1441. }
  1442. //------------------------------------------------------------------------------
  1443. static void HistogramAdd(const VP8LHistogram* const a,
  1444. const VP8LHistogram* const b,
  1445. VP8LHistogram* const out) {
  1446. int i;
  1447. const int literal_size = VP8LHistogramNumCodes(a->palette_code_bits_);
  1448. assert(a->palette_code_bits_ == b->palette_code_bits_);
  1449. if (b != out) {
  1450. for (i = 0; i < literal_size; ++i) {
  1451. out->literal_[i] = a->literal_[i] + b->literal_[i];
  1452. }
  1453. for (i = 0; i < NUM_DISTANCE_CODES; ++i) {
  1454. out->distance_[i] = a->distance_[i] + b->distance_[i];
  1455. }
  1456. for (i = 0; i < NUM_LITERAL_CODES; ++i) {
  1457. out->red_[i] = a->red_[i] + b->red_[i];
  1458. out->blue_[i] = a->blue_[i] + b->blue_[i];
  1459. out->alpha_[i] = a->alpha_[i] + b->alpha_[i];
  1460. }
  1461. } else {
  1462. for (i = 0; i < literal_size; ++i) {
  1463. out->literal_[i] += a->literal_[i];
  1464. }
  1465. for (i = 0; i < NUM_DISTANCE_CODES; ++i) {
  1466. out->distance_[i] += a->distance_[i];
  1467. }
  1468. for (i = 0; i < NUM_LITERAL_CODES; ++i) {
  1469. out->red_[i] += a->red_[i];
  1470. out->blue_[i] += a->blue_[i];
  1471. out->alpha_[i] += a->alpha_[i];
  1472. }
  1473. }
  1474. }
  1475. //------------------------------------------------------------------------------
  1476. VP8LProcessBlueAndRedFunc VP8LSubtractGreenFromBlueAndRed;
  1477. VP8LProcessBlueAndRedFunc VP8LAddGreenToBlueAndRed;
  1478. VP8LPredictorFunc VP8LPredictors[16];
  1479. VP8LTransformColorFunc VP8LTransformColor;
  1480. VP8LTransformColorFunc VP8LTransformColorInverse;
  1481. VP8LConvertFunc VP8LConvertBGRAToRGB;
  1482. VP8LConvertFunc VP8LConvertBGRAToRGBA;
  1483. VP8LConvertFunc VP8LConvertBGRAToRGBA4444;
  1484. VP8LConvertFunc VP8LConvertBGRAToRGB565;
  1485. VP8LConvertFunc VP8LConvertBGRAToBGR;
  1486. VP8LFastLog2SlowFunc VP8LFastLog2Slow;
  1487. VP8LFastLog2SlowFunc VP8LFastSLog2Slow;
  1488. VP8LCostFunc VP8LExtraCost;
  1489. VP8LCostCombinedFunc VP8LExtraCostCombined;
  1490. VP8LCostCountFunc VP8LHuffmanCostCount;
  1491. VP8LCostCombinedCountFunc VP8LHuffmanCostCombinedCount;
  1492. VP8LHistogramAddFunc VP8LHistogramAdd;
  1493. extern void VP8LDspInitSSE2(void);
  1494. extern void VP8LDspInitNEON(void);
  1495. extern void VP8LDspInitMIPS32(void);
  1496. void VP8LDspInit(void) {
  1497. memcpy(VP8LPredictors, kPredictorsC, sizeof(VP8LPredictors));
  1498. VP8LSubtractGreenFromBlueAndRed = VP8LSubtractGreenFromBlueAndRed_C;
  1499. VP8LAddGreenToBlueAndRed = VP8LAddGreenToBlueAndRed_C;
  1500. VP8LTransformColor = VP8LTransformColor_C;
  1501. VP8LTransformColorInverse = VP8LTransformColorInverse_C;
  1502. VP8LConvertBGRAToRGB = VP8LConvertBGRAToRGB_C;
  1503. VP8LConvertBGRAToRGBA = VP8LConvertBGRAToRGBA_C;
  1504. VP8LConvertBGRAToRGBA4444 = VP8LConvertBGRAToRGBA4444_C;
  1505. VP8LConvertBGRAToRGB565 = VP8LConvertBGRAToRGB565_C;
  1506. VP8LConvertBGRAToBGR = VP8LConvertBGRAToBGR_C;
  1507. VP8LFastLog2Slow = FastLog2Slow;
  1508. VP8LFastSLog2Slow = FastSLog2Slow;
  1509. VP8LExtraCost = ExtraCost;
  1510. VP8LExtraCostCombined = ExtraCostCombined;
  1511. VP8LHuffmanCostCount = HuffmanCostCount;
  1512. VP8LHuffmanCostCombinedCount = HuffmanCostCombinedCount;
  1513. VP8LHistogramAdd = HistogramAdd;
  1514. // If defined, use CPUInfo() to overwrite some pointers with faster versions.
  1515. if (VP8GetCPUInfo != NULL) {
  1516. #if defined(WEBP_USE_SSE2)
  1517. if (VP8GetCPUInfo(kSSE2)) {
  1518. VP8LDspInitSSE2();
  1519. }
  1520. #endif
  1521. #if defined(WEBP_USE_NEON)
  1522. if (VP8GetCPUInfo(kNEON)) {
  1523. VP8LDspInitNEON();
  1524. }
  1525. #endif
  1526. #if defined(WEBP_USE_MIPS32)
  1527. if (VP8GetCPUInfo(kMIPS32)) {
  1528. VP8LDspInitMIPS32();
  1529. }
  1530. #endif
  1531. }
  1532. }
  1533. //------------------------------------------------------------------------------