row_common.cc 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758
  1. /*
  2. * Copyright 2011 The LibYuv Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "libyuv/row.h"
  11. #include <string.h> // For memcpy and memset.
  12. #include "libyuv/basic_types.h"
  13. #ifdef __cplusplus
  14. namespace libyuv {
  15. extern "C" {
  16. #endif
  17. // llvm x86 is poor at ternary operator, so use branchless min/max.
  18. #define USE_BRANCHLESS 1
  19. #if USE_BRANCHLESS
  20. static __inline int32 clamp0(int32 v) {
  21. return ((-(v) >> 31) & (v));
  22. }
  23. static __inline int32 clamp255(int32 v) {
  24. return (((255 - (v)) >> 31) | (v)) & 255;
  25. }
  26. static __inline uint32 Clamp(int32 val) {
  27. int v = clamp0(val);
  28. return (uint32)(clamp255(v));
  29. }
  30. static __inline uint32 Abs(int32 v) {
  31. int m = v >> 31;
  32. return (v + m) ^ m;
  33. }
  34. #else // USE_BRANCHLESS
  35. static __inline int32 clamp0(int32 v) {
  36. return (v < 0) ? 0 : v;
  37. }
  38. static __inline int32 clamp255(int32 v) {
  39. return (v > 255) ? 255 : v;
  40. }
  41. static __inline uint32 Clamp(int32 val) {
  42. int v = clamp0(val);
  43. return (uint32)(clamp255(v));
  44. }
  45. static __inline uint32 Abs(int32 v) {
  46. return (v < 0) ? -v : v;
  47. }
  48. #endif // USE_BRANCHLESS
  49. #ifdef LIBYUV_LITTLE_ENDIAN
  50. #define WRITEWORD(p, v) *(uint32*)(p) = v
  51. #else
  52. static inline void WRITEWORD(uint8* p, uint32 v) {
  53. p[0] = (uint8)(v & 255);
  54. p[1] = (uint8)((v >> 8) & 255);
  55. p[2] = (uint8)((v >> 16) & 255);
  56. p[3] = (uint8)((v >> 24) & 255);
  57. }
  58. #endif
  59. void RGB24ToARGBRow_C(const uint8* src_rgb24, uint8* dst_argb, int width) {
  60. int x;
  61. for (x = 0; x < width; ++x) {
  62. uint8 b = src_rgb24[0];
  63. uint8 g = src_rgb24[1];
  64. uint8 r = src_rgb24[2];
  65. dst_argb[0] = b;
  66. dst_argb[1] = g;
  67. dst_argb[2] = r;
  68. dst_argb[3] = 255u;
  69. dst_argb += 4;
  70. src_rgb24 += 3;
  71. }
  72. }
  73. void RAWToARGBRow_C(const uint8* src_raw, uint8* dst_argb, int width) {
  74. int x;
  75. for (x = 0; x < width; ++x) {
  76. uint8 r = src_raw[0];
  77. uint8 g = src_raw[1];
  78. uint8 b = src_raw[2];
  79. dst_argb[0] = b;
  80. dst_argb[1] = g;
  81. dst_argb[2] = r;
  82. dst_argb[3] = 255u;
  83. dst_argb += 4;
  84. src_raw += 3;
  85. }
  86. }
  87. void RAWToRGB24Row_C(const uint8* src_raw, uint8* dst_rgb24, int width) {
  88. int x;
  89. for (x = 0; x < width; ++x) {
  90. uint8 r = src_raw[0];
  91. uint8 g = src_raw[1];
  92. uint8 b = src_raw[2];
  93. dst_rgb24[0] = b;
  94. dst_rgb24[1] = g;
  95. dst_rgb24[2] = r;
  96. dst_rgb24 += 3;
  97. src_raw += 3;
  98. }
  99. }
  100. void RGB565ToARGBRow_C(const uint8* src_rgb565, uint8* dst_argb, int width) {
  101. int x;
  102. for (x = 0; x < width; ++x) {
  103. uint8 b = src_rgb565[0] & 0x1f;
  104. uint8 g = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
  105. uint8 r = src_rgb565[1] >> 3;
  106. dst_argb[0] = (b << 3) | (b >> 2);
  107. dst_argb[1] = (g << 2) | (g >> 4);
  108. dst_argb[2] = (r << 3) | (r >> 2);
  109. dst_argb[3] = 255u;
  110. dst_argb += 4;
  111. src_rgb565 += 2;
  112. }
  113. }
  114. void ARGB1555ToARGBRow_C(const uint8* src_argb1555,
  115. uint8* dst_argb,
  116. int width) {
  117. int x;
  118. for (x = 0; x < width; ++x) {
  119. uint8 b = src_argb1555[0] & 0x1f;
  120. uint8 g = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
  121. uint8 r = (src_argb1555[1] & 0x7c) >> 2;
  122. uint8 a = src_argb1555[1] >> 7;
  123. dst_argb[0] = (b << 3) | (b >> 2);
  124. dst_argb[1] = (g << 3) | (g >> 2);
  125. dst_argb[2] = (r << 3) | (r >> 2);
  126. dst_argb[3] = -a;
  127. dst_argb += 4;
  128. src_argb1555 += 2;
  129. }
  130. }
  131. void ARGB4444ToARGBRow_C(const uint8* src_argb4444,
  132. uint8* dst_argb,
  133. int width) {
  134. int x;
  135. for (x = 0; x < width; ++x) {
  136. uint8 b = src_argb4444[0] & 0x0f;
  137. uint8 g = src_argb4444[0] >> 4;
  138. uint8 r = src_argb4444[1] & 0x0f;
  139. uint8 a = src_argb4444[1] >> 4;
  140. dst_argb[0] = (b << 4) | b;
  141. dst_argb[1] = (g << 4) | g;
  142. dst_argb[2] = (r << 4) | r;
  143. dst_argb[3] = (a << 4) | a;
  144. dst_argb += 4;
  145. src_argb4444 += 2;
  146. }
  147. }
  148. void ARGBToRGB24Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
  149. int x;
  150. for (x = 0; x < width; ++x) {
  151. uint8 b = src_argb[0];
  152. uint8 g = src_argb[1];
  153. uint8 r = src_argb[2];
  154. dst_rgb[0] = b;
  155. dst_rgb[1] = g;
  156. dst_rgb[2] = r;
  157. dst_rgb += 3;
  158. src_argb += 4;
  159. }
  160. }
  161. void ARGBToRAWRow_C(const uint8* src_argb, uint8* dst_rgb, int width) {
  162. int x;
  163. for (x = 0; x < width; ++x) {
  164. uint8 b = src_argb[0];
  165. uint8 g = src_argb[1];
  166. uint8 r = src_argb[2];
  167. dst_rgb[0] = r;
  168. dst_rgb[1] = g;
  169. dst_rgb[2] = b;
  170. dst_rgb += 3;
  171. src_argb += 4;
  172. }
  173. }
  174. void ARGBToRGB565Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
  175. int x;
  176. for (x = 0; x < width - 1; x += 2) {
  177. uint8 b0 = src_argb[0] >> 3;
  178. uint8 g0 = src_argb[1] >> 2;
  179. uint8 r0 = src_argb[2] >> 3;
  180. uint8 b1 = src_argb[4] >> 3;
  181. uint8 g1 = src_argb[5] >> 2;
  182. uint8 r1 = src_argb[6] >> 3;
  183. WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) |
  184. (r1 << 27));
  185. dst_rgb += 4;
  186. src_argb += 8;
  187. }
  188. if (width & 1) {
  189. uint8 b0 = src_argb[0] >> 3;
  190. uint8 g0 = src_argb[1] >> 2;
  191. uint8 r0 = src_argb[2] >> 3;
  192. *(uint16*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 11);
  193. }
  194. }
  195. // dither4 is a row of 4 values from 4x4 dither matrix.
  196. // The 4x4 matrix contains values to increase RGB. When converting to
  197. // fewer bits (565) this provides an ordered dither.
  198. // The order in the 4x4 matrix in first byte is upper left.
  199. // The 4 values are passed as an int, then referenced as an array, so
  200. // endian will not affect order of the original matrix. But the dither4
  201. // will containing the first pixel in the lower byte for little endian
  202. // or the upper byte for big endian.
  203. void ARGBToRGB565DitherRow_C(const uint8* src_argb,
  204. uint8* dst_rgb,
  205. const uint32 dither4,
  206. int width) {
  207. int x;
  208. for (x = 0; x < width - 1; x += 2) {
  209. int dither0 = ((const unsigned char*)(&dither4))[x & 3];
  210. int dither1 = ((const unsigned char*)(&dither4))[(x + 1) & 3];
  211. uint8 b0 = clamp255(src_argb[0] + dither0) >> 3;
  212. uint8 g0 = clamp255(src_argb[1] + dither0) >> 2;
  213. uint8 r0 = clamp255(src_argb[2] + dither0) >> 3;
  214. uint8 b1 = clamp255(src_argb[4] + dither1) >> 3;
  215. uint8 g1 = clamp255(src_argb[5] + dither1) >> 2;
  216. uint8 r1 = clamp255(src_argb[6] + dither1) >> 3;
  217. WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) |
  218. (r1 << 27));
  219. dst_rgb += 4;
  220. src_argb += 8;
  221. }
  222. if (width & 1) {
  223. int dither0 = ((const unsigned char*)(&dither4))[(width - 1) & 3];
  224. uint8 b0 = clamp255(src_argb[0] + dither0) >> 3;
  225. uint8 g0 = clamp255(src_argb[1] + dither0) >> 2;
  226. uint8 r0 = clamp255(src_argb[2] + dither0) >> 3;
  227. *(uint16*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 11);
  228. }
  229. }
  230. void ARGBToARGB1555Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
  231. int x;
  232. for (x = 0; x < width - 1; x += 2) {
  233. uint8 b0 = src_argb[0] >> 3;
  234. uint8 g0 = src_argb[1] >> 3;
  235. uint8 r0 = src_argb[2] >> 3;
  236. uint8 a0 = src_argb[3] >> 7;
  237. uint8 b1 = src_argb[4] >> 3;
  238. uint8 g1 = src_argb[5] >> 3;
  239. uint8 r1 = src_argb[6] >> 3;
  240. uint8 a1 = src_argb[7] >> 7;
  241. *(uint32*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 10) | (a0 << 15) |
  242. (b1 << 16) | (g1 << 21) | (r1 << 26) | (a1 << 31);
  243. dst_rgb += 4;
  244. src_argb += 8;
  245. }
  246. if (width & 1) {
  247. uint8 b0 = src_argb[0] >> 3;
  248. uint8 g0 = src_argb[1] >> 3;
  249. uint8 r0 = src_argb[2] >> 3;
  250. uint8 a0 = src_argb[3] >> 7;
  251. *(uint16*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 10) | (a0 << 15);
  252. }
  253. }
  254. void ARGBToARGB4444Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
  255. int x;
  256. for (x = 0; x < width - 1; x += 2) {
  257. uint8 b0 = src_argb[0] >> 4;
  258. uint8 g0 = src_argb[1] >> 4;
  259. uint8 r0 = src_argb[2] >> 4;
  260. uint8 a0 = src_argb[3] >> 4;
  261. uint8 b1 = src_argb[4] >> 4;
  262. uint8 g1 = src_argb[5] >> 4;
  263. uint8 r1 = src_argb[6] >> 4;
  264. uint8 a1 = src_argb[7] >> 4;
  265. *(uint32*)(dst_rgb) = b0 | (g0 << 4) | (r0 << 8) | (a0 << 12) | (b1 << 16) |
  266. (g1 << 20) | (r1 << 24) | (a1 << 28);
  267. dst_rgb += 4;
  268. src_argb += 8;
  269. }
  270. if (width & 1) {
  271. uint8 b0 = src_argb[0] >> 4;
  272. uint8 g0 = src_argb[1] >> 4;
  273. uint8 r0 = src_argb[2] >> 4;
  274. uint8 a0 = src_argb[3] >> 4;
  275. *(uint16*)(dst_rgb) = b0 | (g0 << 4) | (r0 << 8) | (a0 << 12);
  276. }
  277. }
  278. static __inline int RGBToY(uint8 r, uint8 g, uint8 b) {
  279. return (66 * r + 129 * g + 25 * b + 0x1080) >> 8;
  280. }
  281. static __inline int RGBToU(uint8 r, uint8 g, uint8 b) {
  282. return (112 * b - 74 * g - 38 * r + 0x8080) >> 8;
  283. }
  284. static __inline int RGBToV(uint8 r, uint8 g, uint8 b) {
  285. return (112 * r - 94 * g - 18 * b + 0x8080) >> 8;
  286. }
  287. // ARGBToY_C and ARGBToUV_C
  288. #define MAKEROWY(NAME, R, G, B, BPP) \
  289. void NAME##ToYRow_C(const uint8* src_argb0, uint8* dst_y, int width) { \
  290. int x; \
  291. for (x = 0; x < width; ++x) { \
  292. dst_y[0] = RGBToY(src_argb0[R], src_argb0[G], src_argb0[B]); \
  293. src_argb0 += BPP; \
  294. dst_y += 1; \
  295. } \
  296. } \
  297. void NAME##ToUVRow_C(const uint8* src_rgb0, int src_stride_rgb, \
  298. uint8* dst_u, uint8* dst_v, int width) { \
  299. const uint8* src_rgb1 = src_rgb0 + src_stride_rgb; \
  300. int x; \
  301. for (x = 0; x < width - 1; x += 2) { \
  302. uint8 ab = (src_rgb0[B] + src_rgb0[B + BPP] + src_rgb1[B] + \
  303. src_rgb1[B + BPP]) >> \
  304. 2; \
  305. uint8 ag = (src_rgb0[G] + src_rgb0[G + BPP] + src_rgb1[G] + \
  306. src_rgb1[G + BPP]) >> \
  307. 2; \
  308. uint8 ar = (src_rgb0[R] + src_rgb0[R + BPP] + src_rgb1[R] + \
  309. src_rgb1[R + BPP]) >> \
  310. 2; \
  311. dst_u[0] = RGBToU(ar, ag, ab); \
  312. dst_v[0] = RGBToV(ar, ag, ab); \
  313. src_rgb0 += BPP * 2; \
  314. src_rgb1 += BPP * 2; \
  315. dst_u += 1; \
  316. dst_v += 1; \
  317. } \
  318. if (width & 1) { \
  319. uint8 ab = (src_rgb0[B] + src_rgb1[B]) >> 1; \
  320. uint8 ag = (src_rgb0[G] + src_rgb1[G]) >> 1; \
  321. uint8 ar = (src_rgb0[R] + src_rgb1[R]) >> 1; \
  322. dst_u[0] = RGBToU(ar, ag, ab); \
  323. dst_v[0] = RGBToV(ar, ag, ab); \
  324. } \
  325. }
  326. MAKEROWY(ARGB, 2, 1, 0, 4)
  327. MAKEROWY(BGRA, 1, 2, 3, 4)
  328. MAKEROWY(ABGR, 0, 1, 2, 4)
  329. MAKEROWY(RGBA, 3, 2, 1, 4)
  330. MAKEROWY(RGB24, 2, 1, 0, 3)
  331. MAKEROWY(RAW, 0, 1, 2, 3)
  332. #undef MAKEROWY
  333. // JPeg uses a variation on BT.601-1 full range
  334. // y = 0.29900 * r + 0.58700 * g + 0.11400 * b
  335. // u = -0.16874 * r - 0.33126 * g + 0.50000 * b + center
  336. // v = 0.50000 * r - 0.41869 * g - 0.08131 * b + center
  337. // BT.601 Mpeg range uses:
  338. // b 0.1016 * 255 = 25.908 = 25
  339. // g 0.5078 * 255 = 129.489 = 129
  340. // r 0.2578 * 255 = 65.739 = 66
  341. // JPeg 8 bit Y (not used):
  342. // b 0.11400 * 256 = 29.184 = 29
  343. // g 0.58700 * 256 = 150.272 = 150
  344. // r 0.29900 * 256 = 76.544 = 77
  345. // JPeg 7 bit Y:
  346. // b 0.11400 * 128 = 14.592 = 15
  347. // g 0.58700 * 128 = 75.136 = 75
  348. // r 0.29900 * 128 = 38.272 = 38
  349. // JPeg 8 bit U:
  350. // b 0.50000 * 255 = 127.5 = 127
  351. // g -0.33126 * 255 = -84.4713 = -84
  352. // r -0.16874 * 255 = -43.0287 = -43
  353. // JPeg 8 bit V:
  354. // b -0.08131 * 255 = -20.73405 = -20
  355. // g -0.41869 * 255 = -106.76595 = -107
  356. // r 0.50000 * 255 = 127.5 = 127
  357. static __inline int RGBToYJ(uint8 r, uint8 g, uint8 b) {
  358. return (38 * r + 75 * g + 15 * b + 64) >> 7;
  359. }
  360. static __inline int RGBToUJ(uint8 r, uint8 g, uint8 b) {
  361. return (127 * b - 84 * g - 43 * r + 0x8080) >> 8;
  362. }
  363. static __inline int RGBToVJ(uint8 r, uint8 g, uint8 b) {
  364. return (127 * r - 107 * g - 20 * b + 0x8080) >> 8;
  365. }
  366. #define AVGB(a, b) (((a) + (b) + 1) >> 1)
  367. // ARGBToYJ_C and ARGBToUVJ_C
  368. #define MAKEROWYJ(NAME, R, G, B, BPP) \
  369. void NAME##ToYJRow_C(const uint8* src_argb0, uint8* dst_y, int width) { \
  370. int x; \
  371. for (x = 0; x < width; ++x) { \
  372. dst_y[0] = RGBToYJ(src_argb0[R], src_argb0[G], src_argb0[B]); \
  373. src_argb0 += BPP; \
  374. dst_y += 1; \
  375. } \
  376. } \
  377. void NAME##ToUVJRow_C(const uint8* src_rgb0, int src_stride_rgb, \
  378. uint8* dst_u, uint8* dst_v, int width) { \
  379. const uint8* src_rgb1 = src_rgb0 + src_stride_rgb; \
  380. int x; \
  381. for (x = 0; x < width - 1; x += 2) { \
  382. uint8 ab = AVGB(AVGB(src_rgb0[B], src_rgb1[B]), \
  383. AVGB(src_rgb0[B + BPP], src_rgb1[B + BPP])); \
  384. uint8 ag = AVGB(AVGB(src_rgb0[G], src_rgb1[G]), \
  385. AVGB(src_rgb0[G + BPP], src_rgb1[G + BPP])); \
  386. uint8 ar = AVGB(AVGB(src_rgb0[R], src_rgb1[R]), \
  387. AVGB(src_rgb0[R + BPP], src_rgb1[R + BPP])); \
  388. dst_u[0] = RGBToUJ(ar, ag, ab); \
  389. dst_v[0] = RGBToVJ(ar, ag, ab); \
  390. src_rgb0 += BPP * 2; \
  391. src_rgb1 += BPP * 2; \
  392. dst_u += 1; \
  393. dst_v += 1; \
  394. } \
  395. if (width & 1) { \
  396. uint8 ab = AVGB(src_rgb0[B], src_rgb1[B]); \
  397. uint8 ag = AVGB(src_rgb0[G], src_rgb1[G]); \
  398. uint8 ar = AVGB(src_rgb0[R], src_rgb1[R]); \
  399. dst_u[0] = RGBToUJ(ar, ag, ab); \
  400. dst_v[0] = RGBToVJ(ar, ag, ab); \
  401. } \
  402. }
  403. MAKEROWYJ(ARGB, 2, 1, 0, 4)
  404. #undef MAKEROWYJ
  405. void RGB565ToYRow_C(const uint8* src_rgb565, uint8* dst_y, int width) {
  406. int x;
  407. for (x = 0; x < width; ++x) {
  408. uint8 b = src_rgb565[0] & 0x1f;
  409. uint8 g = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
  410. uint8 r = src_rgb565[1] >> 3;
  411. b = (b << 3) | (b >> 2);
  412. g = (g << 2) | (g >> 4);
  413. r = (r << 3) | (r >> 2);
  414. dst_y[0] = RGBToY(r, g, b);
  415. src_rgb565 += 2;
  416. dst_y += 1;
  417. }
  418. }
  419. void ARGB1555ToYRow_C(const uint8* src_argb1555, uint8* dst_y, int width) {
  420. int x;
  421. for (x = 0; x < width; ++x) {
  422. uint8 b = src_argb1555[0] & 0x1f;
  423. uint8 g = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
  424. uint8 r = (src_argb1555[1] & 0x7c) >> 2;
  425. b = (b << 3) | (b >> 2);
  426. g = (g << 3) | (g >> 2);
  427. r = (r << 3) | (r >> 2);
  428. dst_y[0] = RGBToY(r, g, b);
  429. src_argb1555 += 2;
  430. dst_y += 1;
  431. }
  432. }
  433. void ARGB4444ToYRow_C(const uint8* src_argb4444, uint8* dst_y, int width) {
  434. int x;
  435. for (x = 0; x < width; ++x) {
  436. uint8 b = src_argb4444[0] & 0x0f;
  437. uint8 g = src_argb4444[0] >> 4;
  438. uint8 r = src_argb4444[1] & 0x0f;
  439. b = (b << 4) | b;
  440. g = (g << 4) | g;
  441. r = (r << 4) | r;
  442. dst_y[0] = RGBToY(r, g, b);
  443. src_argb4444 += 2;
  444. dst_y += 1;
  445. }
  446. }
  447. void RGB565ToUVRow_C(const uint8* src_rgb565,
  448. int src_stride_rgb565,
  449. uint8* dst_u,
  450. uint8* dst_v,
  451. int width) {
  452. const uint8* next_rgb565 = src_rgb565 + src_stride_rgb565;
  453. int x;
  454. for (x = 0; x < width - 1; x += 2) {
  455. uint8 b0 = src_rgb565[0] & 0x1f;
  456. uint8 g0 = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
  457. uint8 r0 = src_rgb565[1] >> 3;
  458. uint8 b1 = src_rgb565[2] & 0x1f;
  459. uint8 g1 = (src_rgb565[2] >> 5) | ((src_rgb565[3] & 0x07) << 3);
  460. uint8 r1 = src_rgb565[3] >> 3;
  461. uint8 b2 = next_rgb565[0] & 0x1f;
  462. uint8 g2 = (next_rgb565[0] >> 5) | ((next_rgb565[1] & 0x07) << 3);
  463. uint8 r2 = next_rgb565[1] >> 3;
  464. uint8 b3 = next_rgb565[2] & 0x1f;
  465. uint8 g3 = (next_rgb565[2] >> 5) | ((next_rgb565[3] & 0x07) << 3);
  466. uint8 r3 = next_rgb565[3] >> 3;
  467. uint8 b = (b0 + b1 + b2 + b3); // 565 * 4 = 787.
  468. uint8 g = (g0 + g1 + g2 + g3);
  469. uint8 r = (r0 + r1 + r2 + r3);
  470. b = (b << 1) | (b >> 6); // 787 -> 888.
  471. r = (r << 1) | (r >> 6);
  472. dst_u[0] = RGBToU(r, g, b);
  473. dst_v[0] = RGBToV(r, g, b);
  474. src_rgb565 += 4;
  475. next_rgb565 += 4;
  476. dst_u += 1;
  477. dst_v += 1;
  478. }
  479. if (width & 1) {
  480. uint8 b0 = src_rgb565[0] & 0x1f;
  481. uint8 g0 = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
  482. uint8 r0 = src_rgb565[1] >> 3;
  483. uint8 b2 = next_rgb565[0] & 0x1f;
  484. uint8 g2 = (next_rgb565[0] >> 5) | ((next_rgb565[1] & 0x07) << 3);
  485. uint8 r2 = next_rgb565[1] >> 3;
  486. uint8 b = (b0 + b2); // 565 * 2 = 676.
  487. uint8 g = (g0 + g2);
  488. uint8 r = (r0 + r2);
  489. b = (b << 2) | (b >> 4); // 676 -> 888
  490. g = (g << 1) | (g >> 6);
  491. r = (r << 2) | (r >> 4);
  492. dst_u[0] = RGBToU(r, g, b);
  493. dst_v[0] = RGBToV(r, g, b);
  494. }
  495. }
  496. void ARGB1555ToUVRow_C(const uint8* src_argb1555,
  497. int src_stride_argb1555,
  498. uint8* dst_u,
  499. uint8* dst_v,
  500. int width) {
  501. const uint8* next_argb1555 = src_argb1555 + src_stride_argb1555;
  502. int x;
  503. for (x = 0; x < width - 1; x += 2) {
  504. uint8 b0 = src_argb1555[0] & 0x1f;
  505. uint8 g0 = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
  506. uint8 r0 = (src_argb1555[1] & 0x7c) >> 2;
  507. uint8 b1 = src_argb1555[2] & 0x1f;
  508. uint8 g1 = (src_argb1555[2] >> 5) | ((src_argb1555[3] & 0x03) << 3);
  509. uint8 r1 = (src_argb1555[3] & 0x7c) >> 2;
  510. uint8 b2 = next_argb1555[0] & 0x1f;
  511. uint8 g2 = (next_argb1555[0] >> 5) | ((next_argb1555[1] & 0x03) << 3);
  512. uint8 r2 = (next_argb1555[1] & 0x7c) >> 2;
  513. uint8 b3 = next_argb1555[2] & 0x1f;
  514. uint8 g3 = (next_argb1555[2] >> 5) | ((next_argb1555[3] & 0x03) << 3);
  515. uint8 r3 = (next_argb1555[3] & 0x7c) >> 2;
  516. uint8 b = (b0 + b1 + b2 + b3); // 555 * 4 = 777.
  517. uint8 g = (g0 + g1 + g2 + g3);
  518. uint8 r = (r0 + r1 + r2 + r3);
  519. b = (b << 1) | (b >> 6); // 777 -> 888.
  520. g = (g << 1) | (g >> 6);
  521. r = (r << 1) | (r >> 6);
  522. dst_u[0] = RGBToU(r, g, b);
  523. dst_v[0] = RGBToV(r, g, b);
  524. src_argb1555 += 4;
  525. next_argb1555 += 4;
  526. dst_u += 1;
  527. dst_v += 1;
  528. }
  529. if (width & 1) {
  530. uint8 b0 = src_argb1555[0] & 0x1f;
  531. uint8 g0 = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
  532. uint8 r0 = (src_argb1555[1] & 0x7c) >> 2;
  533. uint8 b2 = next_argb1555[0] & 0x1f;
  534. uint8 g2 = (next_argb1555[0] >> 5) | ((next_argb1555[1] & 0x03) << 3);
  535. uint8 r2 = next_argb1555[1] >> 3;
  536. uint8 b = (b0 + b2); // 555 * 2 = 666.
  537. uint8 g = (g0 + g2);
  538. uint8 r = (r0 + r2);
  539. b = (b << 2) | (b >> 4); // 666 -> 888.
  540. g = (g << 2) | (g >> 4);
  541. r = (r << 2) | (r >> 4);
  542. dst_u[0] = RGBToU(r, g, b);
  543. dst_v[0] = RGBToV(r, g, b);
  544. }
  545. }
  546. void ARGB4444ToUVRow_C(const uint8* src_argb4444,
  547. int src_stride_argb4444,
  548. uint8* dst_u,
  549. uint8* dst_v,
  550. int width) {
  551. const uint8* next_argb4444 = src_argb4444 + src_stride_argb4444;
  552. int x;
  553. for (x = 0; x < width - 1; x += 2) {
  554. uint8 b0 = src_argb4444[0] & 0x0f;
  555. uint8 g0 = src_argb4444[0] >> 4;
  556. uint8 r0 = src_argb4444[1] & 0x0f;
  557. uint8 b1 = src_argb4444[2] & 0x0f;
  558. uint8 g1 = src_argb4444[2] >> 4;
  559. uint8 r1 = src_argb4444[3] & 0x0f;
  560. uint8 b2 = next_argb4444[0] & 0x0f;
  561. uint8 g2 = next_argb4444[0] >> 4;
  562. uint8 r2 = next_argb4444[1] & 0x0f;
  563. uint8 b3 = next_argb4444[2] & 0x0f;
  564. uint8 g3 = next_argb4444[2] >> 4;
  565. uint8 r3 = next_argb4444[3] & 0x0f;
  566. uint8 b = (b0 + b1 + b2 + b3); // 444 * 4 = 666.
  567. uint8 g = (g0 + g1 + g2 + g3);
  568. uint8 r = (r0 + r1 + r2 + r3);
  569. b = (b << 2) | (b >> 4); // 666 -> 888.
  570. g = (g << 2) | (g >> 4);
  571. r = (r << 2) | (r >> 4);
  572. dst_u[0] = RGBToU(r, g, b);
  573. dst_v[0] = RGBToV(r, g, b);
  574. src_argb4444 += 4;
  575. next_argb4444 += 4;
  576. dst_u += 1;
  577. dst_v += 1;
  578. }
  579. if (width & 1) {
  580. uint8 b0 = src_argb4444[0] & 0x0f;
  581. uint8 g0 = src_argb4444[0] >> 4;
  582. uint8 r0 = src_argb4444[1] & 0x0f;
  583. uint8 b2 = next_argb4444[0] & 0x0f;
  584. uint8 g2 = next_argb4444[0] >> 4;
  585. uint8 r2 = next_argb4444[1] & 0x0f;
  586. uint8 b = (b0 + b2); // 444 * 2 = 555.
  587. uint8 g = (g0 + g2);
  588. uint8 r = (r0 + r2);
  589. b = (b << 3) | (b >> 2); // 555 -> 888.
  590. g = (g << 3) | (g >> 2);
  591. r = (r << 3) | (r >> 2);
  592. dst_u[0] = RGBToU(r, g, b);
  593. dst_v[0] = RGBToV(r, g, b);
  594. }
  595. }
  596. void ARGBToUV444Row_C(const uint8* src_argb,
  597. uint8* dst_u,
  598. uint8* dst_v,
  599. int width) {
  600. int x;
  601. for (x = 0; x < width; ++x) {
  602. uint8 ab = src_argb[0];
  603. uint8 ag = src_argb[1];
  604. uint8 ar = src_argb[2];
  605. dst_u[0] = RGBToU(ar, ag, ab);
  606. dst_v[0] = RGBToV(ar, ag, ab);
  607. src_argb += 4;
  608. dst_u += 1;
  609. dst_v += 1;
  610. }
  611. }
  612. void ARGBGrayRow_C(const uint8* src_argb, uint8* dst_argb, int width) {
  613. int x;
  614. for (x = 0; x < width; ++x) {
  615. uint8 y = RGBToYJ(src_argb[2], src_argb[1], src_argb[0]);
  616. dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
  617. dst_argb[3] = src_argb[3];
  618. dst_argb += 4;
  619. src_argb += 4;
  620. }
  621. }
  622. // Convert a row of image to Sepia tone.
  623. void ARGBSepiaRow_C(uint8* dst_argb, int width) {
  624. int x;
  625. for (x = 0; x < width; ++x) {
  626. int b = dst_argb[0];
  627. int g = dst_argb[1];
  628. int r = dst_argb[2];
  629. int sb = (b * 17 + g * 68 + r * 35) >> 7;
  630. int sg = (b * 22 + g * 88 + r * 45) >> 7;
  631. int sr = (b * 24 + g * 98 + r * 50) >> 7;
  632. // b does not over flow. a is preserved from original.
  633. dst_argb[0] = sb;
  634. dst_argb[1] = clamp255(sg);
  635. dst_argb[2] = clamp255(sr);
  636. dst_argb += 4;
  637. }
  638. }
  639. // Apply color matrix to a row of image. Matrix is signed.
  640. // TODO(fbarchard): Consider adding rounding (+32).
  641. void ARGBColorMatrixRow_C(const uint8* src_argb,
  642. uint8* dst_argb,
  643. const int8* matrix_argb,
  644. int width) {
  645. int x;
  646. for (x = 0; x < width; ++x) {
  647. int b = src_argb[0];
  648. int g = src_argb[1];
  649. int r = src_argb[2];
  650. int a = src_argb[3];
  651. int sb = (b * matrix_argb[0] + g * matrix_argb[1] + r * matrix_argb[2] +
  652. a * matrix_argb[3]) >>
  653. 6;
  654. int sg = (b * matrix_argb[4] + g * matrix_argb[5] + r * matrix_argb[6] +
  655. a * matrix_argb[7]) >>
  656. 6;
  657. int sr = (b * matrix_argb[8] + g * matrix_argb[9] + r * matrix_argb[10] +
  658. a * matrix_argb[11]) >>
  659. 6;
  660. int sa = (b * matrix_argb[12] + g * matrix_argb[13] + r * matrix_argb[14] +
  661. a * matrix_argb[15]) >>
  662. 6;
  663. dst_argb[0] = Clamp(sb);
  664. dst_argb[1] = Clamp(sg);
  665. dst_argb[2] = Clamp(sr);
  666. dst_argb[3] = Clamp(sa);
  667. src_argb += 4;
  668. dst_argb += 4;
  669. }
  670. }
  671. // Apply color table to a row of image.
  672. void ARGBColorTableRow_C(uint8* dst_argb, const uint8* table_argb, int width) {
  673. int x;
  674. for (x = 0; x < width; ++x) {
  675. int b = dst_argb[0];
  676. int g = dst_argb[1];
  677. int r = dst_argb[2];
  678. int a = dst_argb[3];
  679. dst_argb[0] = table_argb[b * 4 + 0];
  680. dst_argb[1] = table_argb[g * 4 + 1];
  681. dst_argb[2] = table_argb[r * 4 + 2];
  682. dst_argb[3] = table_argb[a * 4 + 3];
  683. dst_argb += 4;
  684. }
  685. }
  686. // Apply color table to a row of image.
  687. void RGBColorTableRow_C(uint8* dst_argb, const uint8* table_argb, int width) {
  688. int x;
  689. for (x = 0; x < width; ++x) {
  690. int b = dst_argb[0];
  691. int g = dst_argb[1];
  692. int r = dst_argb[2];
  693. dst_argb[0] = table_argb[b * 4 + 0];
  694. dst_argb[1] = table_argb[g * 4 + 1];
  695. dst_argb[2] = table_argb[r * 4 + 2];
  696. dst_argb += 4;
  697. }
  698. }
  699. void ARGBQuantizeRow_C(uint8* dst_argb,
  700. int scale,
  701. int interval_size,
  702. int interval_offset,
  703. int width) {
  704. int x;
  705. for (x = 0; x < width; ++x) {
  706. int b = dst_argb[0];
  707. int g = dst_argb[1];
  708. int r = dst_argb[2];
  709. dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset;
  710. dst_argb[1] = (g * scale >> 16) * interval_size + interval_offset;
  711. dst_argb[2] = (r * scale >> 16) * interval_size + interval_offset;
  712. dst_argb += 4;
  713. }
  714. }
  715. #define REPEAT8(v) (v) | ((v) << 8)
  716. #define SHADE(f, v) v* f >> 24
  717. void ARGBShadeRow_C(const uint8* src_argb,
  718. uint8* dst_argb,
  719. int width,
  720. uint32 value) {
  721. const uint32 b_scale = REPEAT8(value & 0xff);
  722. const uint32 g_scale = REPEAT8((value >> 8) & 0xff);
  723. const uint32 r_scale = REPEAT8((value >> 16) & 0xff);
  724. const uint32 a_scale = REPEAT8(value >> 24);
  725. int i;
  726. for (i = 0; i < width; ++i) {
  727. const uint32 b = REPEAT8(src_argb[0]);
  728. const uint32 g = REPEAT8(src_argb[1]);
  729. const uint32 r = REPEAT8(src_argb[2]);
  730. const uint32 a = REPEAT8(src_argb[3]);
  731. dst_argb[0] = SHADE(b, b_scale);
  732. dst_argb[1] = SHADE(g, g_scale);
  733. dst_argb[2] = SHADE(r, r_scale);
  734. dst_argb[3] = SHADE(a, a_scale);
  735. src_argb += 4;
  736. dst_argb += 4;
  737. }
  738. }
  739. #undef REPEAT8
  740. #undef SHADE
  741. #define REPEAT8(v) (v) | ((v) << 8)
  742. #define SHADE(f, v) v* f >> 16
  743. void ARGBMultiplyRow_C(const uint8* src_argb0,
  744. const uint8* src_argb1,
  745. uint8* dst_argb,
  746. int width) {
  747. int i;
  748. for (i = 0; i < width; ++i) {
  749. const uint32 b = REPEAT8(src_argb0[0]);
  750. const uint32 g = REPEAT8(src_argb0[1]);
  751. const uint32 r = REPEAT8(src_argb0[2]);
  752. const uint32 a = REPEAT8(src_argb0[3]);
  753. const uint32 b_scale = src_argb1[0];
  754. const uint32 g_scale = src_argb1[1];
  755. const uint32 r_scale = src_argb1[2];
  756. const uint32 a_scale = src_argb1[3];
  757. dst_argb[0] = SHADE(b, b_scale);
  758. dst_argb[1] = SHADE(g, g_scale);
  759. dst_argb[2] = SHADE(r, r_scale);
  760. dst_argb[3] = SHADE(a, a_scale);
  761. src_argb0 += 4;
  762. src_argb1 += 4;
  763. dst_argb += 4;
  764. }
  765. }
  766. #undef REPEAT8
  767. #undef SHADE
  768. #define SHADE(f, v) clamp255(v + f)
  769. void ARGBAddRow_C(const uint8* src_argb0,
  770. const uint8* src_argb1,
  771. uint8* dst_argb,
  772. int width) {
  773. int i;
  774. for (i = 0; i < width; ++i) {
  775. const int b = src_argb0[0];
  776. const int g = src_argb0[1];
  777. const int r = src_argb0[2];
  778. const int a = src_argb0[3];
  779. const int b_add = src_argb1[0];
  780. const int g_add = src_argb1[1];
  781. const int r_add = src_argb1[2];
  782. const int a_add = src_argb1[3];
  783. dst_argb[0] = SHADE(b, b_add);
  784. dst_argb[1] = SHADE(g, g_add);
  785. dst_argb[2] = SHADE(r, r_add);
  786. dst_argb[3] = SHADE(a, a_add);
  787. src_argb0 += 4;
  788. src_argb1 += 4;
  789. dst_argb += 4;
  790. }
  791. }
  792. #undef SHADE
  793. #define SHADE(f, v) clamp0(f - v)
  794. void ARGBSubtractRow_C(const uint8* src_argb0,
  795. const uint8* src_argb1,
  796. uint8* dst_argb,
  797. int width) {
  798. int i;
  799. for (i = 0; i < width; ++i) {
  800. const int b = src_argb0[0];
  801. const int g = src_argb0[1];
  802. const int r = src_argb0[2];
  803. const int a = src_argb0[3];
  804. const int b_sub = src_argb1[0];
  805. const int g_sub = src_argb1[1];
  806. const int r_sub = src_argb1[2];
  807. const int a_sub = src_argb1[3];
  808. dst_argb[0] = SHADE(b, b_sub);
  809. dst_argb[1] = SHADE(g, g_sub);
  810. dst_argb[2] = SHADE(r, r_sub);
  811. dst_argb[3] = SHADE(a, a_sub);
  812. src_argb0 += 4;
  813. src_argb1 += 4;
  814. dst_argb += 4;
  815. }
  816. }
  817. #undef SHADE
  818. // Sobel functions which mimics SSSE3.
  819. void SobelXRow_C(const uint8* src_y0,
  820. const uint8* src_y1,
  821. const uint8* src_y2,
  822. uint8* dst_sobelx,
  823. int width) {
  824. int i;
  825. for (i = 0; i < width; ++i) {
  826. int a = src_y0[i];
  827. int b = src_y1[i];
  828. int c = src_y2[i];
  829. int a_sub = src_y0[i + 2];
  830. int b_sub = src_y1[i + 2];
  831. int c_sub = src_y2[i + 2];
  832. int a_diff = a - a_sub;
  833. int b_diff = b - b_sub;
  834. int c_diff = c - c_sub;
  835. int sobel = Abs(a_diff + b_diff * 2 + c_diff);
  836. dst_sobelx[i] = (uint8)(clamp255(sobel));
  837. }
  838. }
  839. void SobelYRow_C(const uint8* src_y0,
  840. const uint8* src_y1,
  841. uint8* dst_sobely,
  842. int width) {
  843. int i;
  844. for (i = 0; i < width; ++i) {
  845. int a = src_y0[i + 0];
  846. int b = src_y0[i + 1];
  847. int c = src_y0[i + 2];
  848. int a_sub = src_y1[i + 0];
  849. int b_sub = src_y1[i + 1];
  850. int c_sub = src_y1[i + 2];
  851. int a_diff = a - a_sub;
  852. int b_diff = b - b_sub;
  853. int c_diff = c - c_sub;
  854. int sobel = Abs(a_diff + b_diff * 2 + c_diff);
  855. dst_sobely[i] = (uint8)(clamp255(sobel));
  856. }
  857. }
  858. void SobelRow_C(const uint8* src_sobelx,
  859. const uint8* src_sobely,
  860. uint8* dst_argb,
  861. int width) {
  862. int i;
  863. for (i = 0; i < width; ++i) {
  864. int r = src_sobelx[i];
  865. int b = src_sobely[i];
  866. int s = clamp255(r + b);
  867. dst_argb[0] = (uint8)(s);
  868. dst_argb[1] = (uint8)(s);
  869. dst_argb[2] = (uint8)(s);
  870. dst_argb[3] = (uint8)(255u);
  871. dst_argb += 4;
  872. }
  873. }
  874. void SobelToPlaneRow_C(const uint8* src_sobelx,
  875. const uint8* src_sobely,
  876. uint8* dst_y,
  877. int width) {
  878. int i;
  879. for (i = 0; i < width; ++i) {
  880. int r = src_sobelx[i];
  881. int b = src_sobely[i];
  882. int s = clamp255(r + b);
  883. dst_y[i] = (uint8)(s);
  884. }
  885. }
  886. void SobelXYRow_C(const uint8* src_sobelx,
  887. const uint8* src_sobely,
  888. uint8* dst_argb,
  889. int width) {
  890. int i;
  891. for (i = 0; i < width; ++i) {
  892. int r = src_sobelx[i];
  893. int b = src_sobely[i];
  894. int g = clamp255(r + b);
  895. dst_argb[0] = (uint8)(b);
  896. dst_argb[1] = (uint8)(g);
  897. dst_argb[2] = (uint8)(r);
  898. dst_argb[3] = (uint8)(255u);
  899. dst_argb += 4;
  900. }
  901. }
  902. void J400ToARGBRow_C(const uint8* src_y, uint8* dst_argb, int width) {
  903. // Copy a Y to RGB.
  904. int x;
  905. for (x = 0; x < width; ++x) {
  906. uint8 y = src_y[0];
  907. dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
  908. dst_argb[3] = 255u;
  909. dst_argb += 4;
  910. ++src_y;
  911. }
  912. }
  913. // TODO(fbarchard): Unify these structures to be platform independent.
  914. // TODO(fbarchard): Generate SIMD structures from float matrix.
  915. // BT.601 YUV to RGB reference
  916. // R = (Y - 16) * 1.164 - V * -1.596
  917. // G = (Y - 16) * 1.164 - U * 0.391 - V * 0.813
  918. // B = (Y - 16) * 1.164 - U * -2.018
  919. // Y contribution to R,G,B. Scale and bias.
  920. #define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */
  921. #define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */
  922. // U and V contributions to R,G,B.
  923. #define UB -128 /* max(-128, round(-2.018 * 64)) */
  924. #define UG 25 /* round(0.391 * 64) */
  925. #define VG 52 /* round(0.813 * 64) */
  926. #define VR -102 /* round(-1.596 * 64) */
  927. // Bias values to subtract 16 from Y and 128 from U and V.
  928. #define BB (UB * 128 + YGB)
  929. #define BG (UG * 128 + VG * 128 + YGB)
  930. #define BR (VR * 128 + YGB)
  931. #if defined(__aarch64__) // 64 bit arm
  932. const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
  933. {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR},
  934. {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR},
  935. {UG, VG, UG, VG, UG, VG, UG, VG},
  936. {UG, VG, UG, VG, UG, VG, UG, VG},
  937. {BB, BG, BR, 0, 0, 0, 0, 0},
  938. {0x0101 * YG, 0, 0, 0}};
  939. const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
  940. {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB},
  941. {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB},
  942. {VG, UG, VG, UG, VG, UG, VG, UG},
  943. {VG, UG, VG, UG, VG, UG, VG, UG},
  944. {BR, BG, BB, 0, 0, 0, 0, 0},
  945. {0x0101 * YG, 0, 0, 0}};
  946. #elif defined(__arm__) // 32 bit arm
  947. const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
  948. {-UB, -UB, -UB, -UB, -VR, -VR, -VR, -VR, 0, 0, 0, 0, 0, 0, 0, 0},
  949. {UG, UG, UG, UG, VG, VG, VG, VG, 0, 0, 0, 0, 0, 0, 0, 0},
  950. {BB, BG, BR, 0, 0, 0, 0, 0},
  951. {0x0101 * YG, 0, 0, 0}};
  952. const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
  953. {-VR, -VR, -VR, -VR, -UB, -UB, -UB, -UB, 0, 0, 0, 0, 0, 0, 0, 0},
  954. {VG, VG, VG, VG, UG, UG, UG, UG, 0, 0, 0, 0, 0, 0, 0, 0},
  955. {BR, BG, BB, 0, 0, 0, 0, 0},
  956. {0x0101 * YG, 0, 0, 0}};
  957. #else
  958. const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
  959. {UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0,
  960. UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0},
  961. {UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG,
  962. UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG},
  963. {0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR,
  964. 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR},
  965. {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB},
  966. {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
  967. {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR},
  968. {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}};
  969. const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
  970. {VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0,
  971. VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0},
  972. {VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG,
  973. VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG},
  974. {0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB,
  975. 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB},
  976. {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR},
  977. {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
  978. {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB},
  979. {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}};
  980. #endif
  981. #undef BB
  982. #undef BG
  983. #undef BR
  984. #undef YGB
  985. #undef UB
  986. #undef UG
  987. #undef VG
  988. #undef VR
  989. #undef YG
  990. // JPEG YUV to RGB reference
  991. // * R = Y - V * -1.40200
  992. // * G = Y - U * 0.34414 - V * 0.71414
  993. // * B = Y - U * -1.77200
  994. // Y contribution to R,G,B. Scale and bias.
  995. #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
  996. #define YGB 32 /* 64 / 2 */
  997. // U and V contributions to R,G,B.
  998. #define UB -113 /* round(-1.77200 * 64) */
  999. #define UG 22 /* round(0.34414 * 64) */
  1000. #define VG 46 /* round(0.71414 * 64) */
  1001. #define VR -90 /* round(-1.40200 * 64) */
  1002. // Bias values to round, and subtract 128 from U and V.
  1003. #define BB (UB * 128 + YGB)
  1004. #define BG (UG * 128 + VG * 128 + YGB)
  1005. #define BR (VR * 128 + YGB)
  1006. #if defined(__aarch64__)
  1007. const struct YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = {
  1008. {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR},
  1009. {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR},
  1010. {UG, VG, UG, VG, UG, VG, UG, VG},
  1011. {UG, VG, UG, VG, UG, VG, UG, VG},
  1012. {BB, BG, BR, 0, 0, 0, 0, 0},
  1013. {0x0101 * YG, 0, 0, 0}};
  1014. const struct YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = {
  1015. {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB},
  1016. {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB},
  1017. {VG, UG, VG, UG, VG, UG, VG, UG},
  1018. {VG, UG, VG, UG, VG, UG, VG, UG},
  1019. {BR, BG, BB, 0, 0, 0, 0, 0},
  1020. {0x0101 * YG, 0, 0, 0}};
  1021. #elif defined(__arm__)
  1022. const struct YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = {
  1023. {-UB, -UB, -UB, -UB, -VR, -VR, -VR, -VR, 0, 0, 0, 0, 0, 0, 0, 0},
  1024. {UG, UG, UG, UG, VG, VG, VG, VG, 0, 0, 0, 0, 0, 0, 0, 0},
  1025. {BB, BG, BR, 0, 0, 0, 0, 0},
  1026. {0x0101 * YG, 0, 0, 0}};
  1027. const struct YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = {
  1028. {-VR, -VR, -VR, -VR, -UB, -UB, -UB, -UB, 0, 0, 0, 0, 0, 0, 0, 0},
  1029. {VG, VG, VG, VG, UG, UG, UG, UG, 0, 0, 0, 0, 0, 0, 0, 0},
  1030. {BR, BG, BB, 0, 0, 0, 0, 0},
  1031. {0x0101 * YG, 0, 0, 0}};
  1032. #else
  1033. const struct YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = {
  1034. {UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0,
  1035. UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0},
  1036. {UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG,
  1037. UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG},
  1038. {0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR,
  1039. 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR},
  1040. {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB},
  1041. {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
  1042. {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR},
  1043. {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}};
  1044. const struct YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = {
  1045. {VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0,
  1046. VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0},
  1047. {VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG,
  1048. VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG},
  1049. {0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB,
  1050. 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB},
  1051. {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR},
  1052. {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
  1053. {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB},
  1054. {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}};
  1055. #endif
  1056. #undef BB
  1057. #undef BG
  1058. #undef BR
  1059. #undef YGB
  1060. #undef UB
  1061. #undef UG
  1062. #undef VG
  1063. #undef VR
  1064. #undef YG
  1065. // BT.709 YUV to RGB reference
  1066. // R = (Y - 16) * 1.164 - V * -1.793
  1067. // G = (Y - 16) * 1.164 - U * 0.213 - V * 0.533
  1068. // B = (Y - 16) * 1.164 - U * -2.112
  1069. // See also http://www.equasys.de/colorconversion.html
  1070. // Y contribution to R,G,B. Scale and bias.
  1071. #define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */
  1072. #define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */
  1073. // TODO(fbarchard): Find way to express 2.112 instead of 2.0.
  1074. // U and V contributions to R,G,B.
  1075. #define UB -128 /* max(-128, round(-2.112 * 64)) */
  1076. #define UG 14 /* round(0.213 * 64) */
  1077. #define VG 34 /* round(0.533 * 64) */
  1078. #define VR -115 /* round(-1.793 * 64) */
  1079. // Bias values to round, and subtract 128 from U and V.
  1080. #define BB (UB * 128 + YGB)
  1081. #define BG (UG * 128 + VG * 128 + YGB)
  1082. #define BR (VR * 128 + YGB)
  1083. #if defined(__aarch64__)
  1084. const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
  1085. {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR},
  1086. {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR},
  1087. {UG, VG, UG, VG, UG, VG, UG, VG},
  1088. {UG, VG, UG, VG, UG, VG, UG, VG},
  1089. {BB, BG, BR, 0, 0, 0, 0, 0},
  1090. {0x0101 * YG, 0, 0, 0}};
  1091. const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
  1092. {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB},
  1093. {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB},
  1094. {VG, UG, VG, UG, VG, UG, VG, UG},
  1095. {VG, UG, VG, UG, VG, UG, VG, UG},
  1096. {BR, BG, BB, 0, 0, 0, 0, 0},
  1097. {0x0101 * YG, 0, 0, 0}};
  1098. #elif defined(__arm__)
  1099. const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
  1100. {-UB, -UB, -UB, -UB, -VR, -VR, -VR, -VR, 0, 0, 0, 0, 0, 0, 0, 0},
  1101. {UG, UG, UG, UG, VG, VG, VG, VG, 0, 0, 0, 0, 0, 0, 0, 0},
  1102. {BB, BG, BR, 0, 0, 0, 0, 0},
  1103. {0x0101 * YG, 0, 0, 0}};
  1104. const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
  1105. {-VR, -VR, -VR, -VR, -UB, -UB, -UB, -UB, 0, 0, 0, 0, 0, 0, 0, 0},
  1106. {VG, VG, VG, VG, UG, UG, UG, UG, 0, 0, 0, 0, 0, 0, 0, 0},
  1107. {BR, BG, BB, 0, 0, 0, 0, 0},
  1108. {0x0101 * YG, 0, 0, 0}};
  1109. #else
  1110. const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
  1111. {UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0,
  1112. UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0},
  1113. {UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG,
  1114. UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG},
  1115. {0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR,
  1116. 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR},
  1117. {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB},
  1118. {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
  1119. {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR},
  1120. {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}};
  1121. const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
  1122. {VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0,
  1123. VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0},
  1124. {VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG,
  1125. VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG},
  1126. {0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB,
  1127. 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB},
  1128. {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR},
  1129. {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
  1130. {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB},
  1131. {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}};
  1132. #endif
  1133. #undef BB
  1134. #undef BG
  1135. #undef BR
  1136. #undef YGB
  1137. #undef UB
  1138. #undef UG
  1139. #undef VG
  1140. #undef VR
  1141. #undef YG
  1142. // C reference code that mimics the YUV assembly.
  1143. static __inline void YuvPixel(uint8 y,
  1144. uint8 u,
  1145. uint8 v,
  1146. uint8* b,
  1147. uint8* g,
  1148. uint8* r,
  1149. const struct YuvConstants* yuvconstants) {
  1150. #if defined(__aarch64__)
  1151. int ub = -yuvconstants->kUVToRB[0];
  1152. int ug = yuvconstants->kUVToG[0];
  1153. int vg = yuvconstants->kUVToG[1];
  1154. int vr = -yuvconstants->kUVToRB[1];
  1155. int bb = yuvconstants->kUVBiasBGR[0];
  1156. int bg = yuvconstants->kUVBiasBGR[1];
  1157. int br = yuvconstants->kUVBiasBGR[2];
  1158. int yg = yuvconstants->kYToRgb[0] / 0x0101;
  1159. #elif defined(__arm__)
  1160. int ub = -yuvconstants->kUVToRB[0];
  1161. int ug = yuvconstants->kUVToG[0];
  1162. int vg = yuvconstants->kUVToG[4];
  1163. int vr = -yuvconstants->kUVToRB[4];
  1164. int bb = yuvconstants->kUVBiasBGR[0];
  1165. int bg = yuvconstants->kUVBiasBGR[1];
  1166. int br = yuvconstants->kUVBiasBGR[2];
  1167. int yg = yuvconstants->kYToRgb[0] / 0x0101;
  1168. #else
  1169. int ub = yuvconstants->kUVToB[0];
  1170. int ug = yuvconstants->kUVToG[0];
  1171. int vg = yuvconstants->kUVToG[1];
  1172. int vr = yuvconstants->kUVToR[1];
  1173. int bb = yuvconstants->kUVBiasB[0];
  1174. int bg = yuvconstants->kUVBiasG[0];
  1175. int br = yuvconstants->kUVBiasR[0];
  1176. int yg = yuvconstants->kYToRgb[0];
  1177. #endif
  1178. uint32 y1 = (uint32)(y * 0x0101 * yg) >> 16;
  1179. *b = Clamp((int32)(-(u * ub) + y1 + bb) >> 6);
  1180. *g = Clamp((int32)(-(u * ug + v * vg) + y1 + bg) >> 6);
  1181. *r = Clamp((int32)(-(v * vr) + y1 + br) >> 6);
  1182. }
  1183. // Y contribution to R,G,B. Scale and bias.
  1184. #define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */
  1185. #define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */
  1186. // C reference code that mimics the YUV assembly.
  1187. static __inline void YPixel(uint8 y, uint8* b, uint8* g, uint8* r) {
  1188. uint32 y1 = (uint32)(y * 0x0101 * YG) >> 16;
  1189. *b = Clamp((int32)(y1 + YGB) >> 6);
  1190. *g = Clamp((int32)(y1 + YGB) >> 6);
  1191. *r = Clamp((int32)(y1 + YGB) >> 6);
  1192. }
  1193. #undef YG
  1194. #undef YGB
  1195. #if !defined(LIBYUV_DISABLE_NEON) && \
  1196. (defined(__ARM_NEON__) || defined(__aarch64__) || defined(LIBYUV_NEON))
  1197. // C mimic assembly.
  1198. // TODO(fbarchard): Remove subsampling from Neon.
  1199. void I444ToARGBRow_C(const uint8* src_y,
  1200. const uint8* src_u,
  1201. const uint8* src_v,
  1202. uint8* rgb_buf,
  1203. const struct YuvConstants* yuvconstants,
  1204. int width) {
  1205. int x;
  1206. for (x = 0; x < width - 1; x += 2) {
  1207. uint8 u = (src_u[0] + src_u[1] + 1) >> 1;
  1208. uint8 v = (src_v[0] + src_v[1] + 1) >> 1;
  1209. YuvPixel(src_y[0], u, v, rgb_buf + 0, rgb_buf + 1, rgb_buf + 2,
  1210. yuvconstants);
  1211. rgb_buf[3] = 255;
  1212. YuvPixel(src_y[1], u, v, rgb_buf + 4, rgb_buf + 5, rgb_buf + 6,
  1213. yuvconstants);
  1214. rgb_buf[7] = 255;
  1215. src_y += 2;
  1216. src_u += 2;
  1217. src_v += 2;
  1218. rgb_buf += 8; // Advance 2 pixels.
  1219. }
  1220. if (width & 1) {
  1221. YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
  1222. rgb_buf + 2, yuvconstants);
  1223. rgb_buf[3] = 255;
  1224. }
  1225. }
  1226. #else
  1227. void I444ToARGBRow_C(const uint8* src_y,
  1228. const uint8* src_u,
  1229. const uint8* src_v,
  1230. uint8* rgb_buf,
  1231. const struct YuvConstants* yuvconstants,
  1232. int width) {
  1233. int x;
  1234. for (x = 0; x < width; ++x) {
  1235. YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
  1236. rgb_buf + 2, yuvconstants);
  1237. rgb_buf[3] = 255;
  1238. src_y += 1;
  1239. src_u += 1;
  1240. src_v += 1;
  1241. rgb_buf += 4; // Advance 1 pixel.
  1242. }
  1243. }
  1244. #endif
  1245. // Also used for 420
  1246. void I422ToARGBRow_C(const uint8* src_y,
  1247. const uint8* src_u,
  1248. const uint8* src_v,
  1249. uint8* rgb_buf,
  1250. const struct YuvConstants* yuvconstants,
  1251. int width) {
  1252. int x;
  1253. for (x = 0; x < width - 1; x += 2) {
  1254. YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
  1255. rgb_buf + 2, yuvconstants);
  1256. rgb_buf[3] = 255;
  1257. YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
  1258. rgb_buf + 6, yuvconstants);
  1259. rgb_buf[7] = 255;
  1260. src_y += 2;
  1261. src_u += 1;
  1262. src_v += 1;
  1263. rgb_buf += 8; // Advance 2 pixels.
  1264. }
  1265. if (width & 1) {
  1266. YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
  1267. rgb_buf + 2, yuvconstants);
  1268. rgb_buf[3] = 255;
  1269. }
  1270. }
  1271. void I422AlphaToARGBRow_C(const uint8* src_y,
  1272. const uint8* src_u,
  1273. const uint8* src_v,
  1274. const uint8* src_a,
  1275. uint8* rgb_buf,
  1276. const struct YuvConstants* yuvconstants,
  1277. int width) {
  1278. int x;
  1279. for (x = 0; x < width - 1; x += 2) {
  1280. YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
  1281. rgb_buf + 2, yuvconstants);
  1282. rgb_buf[3] = src_a[0];
  1283. YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
  1284. rgb_buf + 6, yuvconstants);
  1285. rgb_buf[7] = src_a[1];
  1286. src_y += 2;
  1287. src_u += 1;
  1288. src_v += 1;
  1289. src_a += 2;
  1290. rgb_buf += 8; // Advance 2 pixels.
  1291. }
  1292. if (width & 1) {
  1293. YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
  1294. rgb_buf + 2, yuvconstants);
  1295. rgb_buf[3] = src_a[0];
  1296. }
  1297. }
  1298. void I422ToRGB24Row_C(const uint8* src_y,
  1299. const uint8* src_u,
  1300. const uint8* src_v,
  1301. uint8* rgb_buf,
  1302. const struct YuvConstants* yuvconstants,
  1303. int width) {
  1304. int x;
  1305. for (x = 0; x < width - 1; x += 2) {
  1306. YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
  1307. rgb_buf + 2, yuvconstants);
  1308. YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 3, rgb_buf + 4,
  1309. rgb_buf + 5, yuvconstants);
  1310. src_y += 2;
  1311. src_u += 1;
  1312. src_v += 1;
  1313. rgb_buf += 6; // Advance 2 pixels.
  1314. }
  1315. if (width & 1) {
  1316. YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
  1317. rgb_buf + 2, yuvconstants);
  1318. }
  1319. }
  1320. void I422ToARGB4444Row_C(const uint8* src_y,
  1321. const uint8* src_u,
  1322. const uint8* src_v,
  1323. uint8* dst_argb4444,
  1324. const struct YuvConstants* yuvconstants,
  1325. int width) {
  1326. uint8 b0;
  1327. uint8 g0;
  1328. uint8 r0;
  1329. uint8 b1;
  1330. uint8 g1;
  1331. uint8 r1;
  1332. int x;
  1333. for (x = 0; x < width - 1; x += 2) {
  1334. YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
  1335. YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants);
  1336. b0 = b0 >> 4;
  1337. g0 = g0 >> 4;
  1338. r0 = r0 >> 4;
  1339. b1 = b1 >> 4;
  1340. g1 = g1 >> 4;
  1341. r1 = r1 >> 4;
  1342. *(uint32*)(dst_argb4444) = b0 | (g0 << 4) | (r0 << 8) | (b1 << 16) |
  1343. (g1 << 20) | (r1 << 24) | 0xf000f000;
  1344. src_y += 2;
  1345. src_u += 1;
  1346. src_v += 1;
  1347. dst_argb4444 += 4; // Advance 2 pixels.
  1348. }
  1349. if (width & 1) {
  1350. YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
  1351. b0 = b0 >> 4;
  1352. g0 = g0 >> 4;
  1353. r0 = r0 >> 4;
  1354. *(uint16*)(dst_argb4444) = b0 | (g0 << 4) | (r0 << 8) | 0xf000;
  1355. }
  1356. }
  1357. void I422ToARGB1555Row_C(const uint8* src_y,
  1358. const uint8* src_u,
  1359. const uint8* src_v,
  1360. uint8* dst_argb1555,
  1361. const struct YuvConstants* yuvconstants,
  1362. int width) {
  1363. uint8 b0;
  1364. uint8 g0;
  1365. uint8 r0;
  1366. uint8 b1;
  1367. uint8 g1;
  1368. uint8 r1;
  1369. int x;
  1370. for (x = 0; x < width - 1; x += 2) {
  1371. YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
  1372. YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants);
  1373. b0 = b0 >> 3;
  1374. g0 = g0 >> 3;
  1375. r0 = r0 >> 3;
  1376. b1 = b1 >> 3;
  1377. g1 = g1 >> 3;
  1378. r1 = r1 >> 3;
  1379. *(uint32*)(dst_argb1555) = b0 | (g0 << 5) | (r0 << 10) | (b1 << 16) |
  1380. (g1 << 21) | (r1 << 26) | 0x80008000;
  1381. src_y += 2;
  1382. src_u += 1;
  1383. src_v += 1;
  1384. dst_argb1555 += 4; // Advance 2 pixels.
  1385. }
  1386. if (width & 1) {
  1387. YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
  1388. b0 = b0 >> 3;
  1389. g0 = g0 >> 3;
  1390. r0 = r0 >> 3;
  1391. *(uint16*)(dst_argb1555) = b0 | (g0 << 5) | (r0 << 10) | 0x8000;
  1392. }
  1393. }
  1394. void I422ToRGB565Row_C(const uint8* src_y,
  1395. const uint8* src_u,
  1396. const uint8* src_v,
  1397. uint8* dst_rgb565,
  1398. const struct YuvConstants* yuvconstants,
  1399. int width) {
  1400. uint8 b0;
  1401. uint8 g0;
  1402. uint8 r0;
  1403. uint8 b1;
  1404. uint8 g1;
  1405. uint8 r1;
  1406. int x;
  1407. for (x = 0; x < width - 1; x += 2) {
  1408. YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
  1409. YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants);
  1410. b0 = b0 >> 3;
  1411. g0 = g0 >> 2;
  1412. r0 = r0 >> 3;
  1413. b1 = b1 >> 3;
  1414. g1 = g1 >> 2;
  1415. r1 = r1 >> 3;
  1416. *(uint32*)(dst_rgb565) =
  1417. b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) | (r1 << 27);
  1418. src_y += 2;
  1419. src_u += 1;
  1420. src_v += 1;
  1421. dst_rgb565 += 4; // Advance 2 pixels.
  1422. }
  1423. if (width & 1) {
  1424. YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
  1425. b0 = b0 >> 3;
  1426. g0 = g0 >> 2;
  1427. r0 = r0 >> 3;
  1428. *(uint16*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11);
  1429. }
  1430. }
  1431. void NV12ToARGBRow_C(const uint8* src_y,
  1432. const uint8* src_uv,
  1433. uint8* rgb_buf,
  1434. const struct YuvConstants* yuvconstants,
  1435. int width) {
  1436. int x;
  1437. for (x = 0; x < width - 1; x += 2) {
  1438. YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1,
  1439. rgb_buf + 2, yuvconstants);
  1440. rgb_buf[3] = 255;
  1441. YuvPixel(src_y[1], src_uv[0], src_uv[1], rgb_buf + 4, rgb_buf + 5,
  1442. rgb_buf + 6, yuvconstants);
  1443. rgb_buf[7] = 255;
  1444. src_y += 2;
  1445. src_uv += 2;
  1446. rgb_buf += 8; // Advance 2 pixels.
  1447. }
  1448. if (width & 1) {
  1449. YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1,
  1450. rgb_buf + 2, yuvconstants);
  1451. rgb_buf[3] = 255;
  1452. }
  1453. }
  1454. void NV21ToARGBRow_C(const uint8* src_y,
  1455. const uint8* src_vu,
  1456. uint8* rgb_buf,
  1457. const struct YuvConstants* yuvconstants,
  1458. int width) {
  1459. int x;
  1460. for (x = 0; x < width - 1; x += 2) {
  1461. YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1,
  1462. rgb_buf + 2, yuvconstants);
  1463. rgb_buf[3] = 255;
  1464. YuvPixel(src_y[1], src_vu[1], src_vu[0], rgb_buf + 4, rgb_buf + 5,
  1465. rgb_buf + 6, yuvconstants);
  1466. rgb_buf[7] = 255;
  1467. src_y += 2;
  1468. src_vu += 2;
  1469. rgb_buf += 8; // Advance 2 pixels.
  1470. }
  1471. if (width & 1) {
  1472. YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1,
  1473. rgb_buf + 2, yuvconstants);
  1474. rgb_buf[3] = 255;
  1475. }
  1476. }
  1477. void NV12ToRGB565Row_C(const uint8* src_y,
  1478. const uint8* src_uv,
  1479. uint8* dst_rgb565,
  1480. const struct YuvConstants* yuvconstants,
  1481. int width) {
  1482. uint8 b0;
  1483. uint8 g0;
  1484. uint8 r0;
  1485. uint8 b1;
  1486. uint8 g1;
  1487. uint8 r1;
  1488. int x;
  1489. for (x = 0; x < width - 1; x += 2) {
  1490. YuvPixel(src_y[0], src_uv[0], src_uv[1], &b0, &g0, &r0, yuvconstants);
  1491. YuvPixel(src_y[1], src_uv[0], src_uv[1], &b1, &g1, &r1, yuvconstants);
  1492. b0 = b0 >> 3;
  1493. g0 = g0 >> 2;
  1494. r0 = r0 >> 3;
  1495. b1 = b1 >> 3;
  1496. g1 = g1 >> 2;
  1497. r1 = r1 >> 3;
  1498. *(uint32*)(dst_rgb565) =
  1499. b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) | (r1 << 27);
  1500. src_y += 2;
  1501. src_uv += 2;
  1502. dst_rgb565 += 4; // Advance 2 pixels.
  1503. }
  1504. if (width & 1) {
  1505. YuvPixel(src_y[0], src_uv[0], src_uv[1], &b0, &g0, &r0, yuvconstants);
  1506. b0 = b0 >> 3;
  1507. g0 = g0 >> 2;
  1508. r0 = r0 >> 3;
  1509. *(uint16*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11);
  1510. }
  1511. }
  1512. void YUY2ToARGBRow_C(const uint8* src_yuy2,
  1513. uint8* rgb_buf,
  1514. const struct YuvConstants* yuvconstants,
  1515. int width) {
  1516. int x;
  1517. for (x = 0; x < width - 1; x += 2) {
  1518. YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3], rgb_buf + 0, rgb_buf + 1,
  1519. rgb_buf + 2, yuvconstants);
  1520. rgb_buf[3] = 255;
  1521. YuvPixel(src_yuy2[2], src_yuy2[1], src_yuy2[3], rgb_buf + 4, rgb_buf + 5,
  1522. rgb_buf + 6, yuvconstants);
  1523. rgb_buf[7] = 255;
  1524. src_yuy2 += 4;
  1525. rgb_buf += 8; // Advance 2 pixels.
  1526. }
  1527. if (width & 1) {
  1528. YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3], rgb_buf + 0, rgb_buf + 1,
  1529. rgb_buf + 2, yuvconstants);
  1530. rgb_buf[3] = 255;
  1531. }
  1532. }
  1533. void UYVYToARGBRow_C(const uint8* src_uyvy,
  1534. uint8* rgb_buf,
  1535. const struct YuvConstants* yuvconstants,
  1536. int width) {
  1537. int x;
  1538. for (x = 0; x < width - 1; x += 2) {
  1539. YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2], rgb_buf + 0, rgb_buf + 1,
  1540. rgb_buf + 2, yuvconstants);
  1541. rgb_buf[3] = 255;
  1542. YuvPixel(src_uyvy[3], src_uyvy[0], src_uyvy[2], rgb_buf + 4, rgb_buf + 5,
  1543. rgb_buf + 6, yuvconstants);
  1544. rgb_buf[7] = 255;
  1545. src_uyvy += 4;
  1546. rgb_buf += 8; // Advance 2 pixels.
  1547. }
  1548. if (width & 1) {
  1549. YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2], rgb_buf + 0, rgb_buf + 1,
  1550. rgb_buf + 2, yuvconstants);
  1551. rgb_buf[3] = 255;
  1552. }
  1553. }
  1554. void I422ToRGBARow_C(const uint8* src_y,
  1555. const uint8* src_u,
  1556. const uint8* src_v,
  1557. uint8* rgb_buf,
  1558. const struct YuvConstants* yuvconstants,
  1559. int width) {
  1560. int x;
  1561. for (x = 0; x < width - 1; x += 2) {
  1562. YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 1, rgb_buf + 2,
  1563. rgb_buf + 3, yuvconstants);
  1564. rgb_buf[0] = 255;
  1565. YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 5, rgb_buf + 6,
  1566. rgb_buf + 7, yuvconstants);
  1567. rgb_buf[4] = 255;
  1568. src_y += 2;
  1569. src_u += 1;
  1570. src_v += 1;
  1571. rgb_buf += 8; // Advance 2 pixels.
  1572. }
  1573. if (width & 1) {
  1574. YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 1, rgb_buf + 2,
  1575. rgb_buf + 3, yuvconstants);
  1576. rgb_buf[0] = 255;
  1577. }
  1578. }
  1579. void I400ToARGBRow_C(const uint8* src_y, uint8* rgb_buf, int width) {
  1580. int x;
  1581. for (x = 0; x < width - 1; x += 2) {
  1582. YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2);
  1583. rgb_buf[3] = 255;
  1584. YPixel(src_y[1], rgb_buf + 4, rgb_buf + 5, rgb_buf + 6);
  1585. rgb_buf[7] = 255;
  1586. src_y += 2;
  1587. rgb_buf += 8; // Advance 2 pixels.
  1588. }
  1589. if (width & 1) {
  1590. YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2);
  1591. rgb_buf[3] = 255;
  1592. }
  1593. }
  1594. void MirrorRow_C(const uint8* src, uint8* dst, int width) {
  1595. int x;
  1596. src += width - 1;
  1597. for (x = 0; x < width - 1; x += 2) {
  1598. dst[x] = src[0];
  1599. dst[x + 1] = src[-1];
  1600. src -= 2;
  1601. }
  1602. if (width & 1) {
  1603. dst[width - 1] = src[0];
  1604. }
  1605. }
  1606. void MirrorUVRow_C(const uint8* src_uv, uint8* dst_u, uint8* dst_v, int width) {
  1607. int x;
  1608. src_uv += (width - 1) << 1;
  1609. for (x = 0; x < width - 1; x += 2) {
  1610. dst_u[x] = src_uv[0];
  1611. dst_u[x + 1] = src_uv[-2];
  1612. dst_v[x] = src_uv[1];
  1613. dst_v[x + 1] = src_uv[-2 + 1];
  1614. src_uv -= 4;
  1615. }
  1616. if (width & 1) {
  1617. dst_u[width - 1] = src_uv[0];
  1618. dst_v[width - 1] = src_uv[1];
  1619. }
  1620. }
  1621. void ARGBMirrorRow_C(const uint8* src, uint8* dst, int width) {
  1622. int x;
  1623. const uint32* src32 = (const uint32*)(src);
  1624. uint32* dst32 = (uint32*)(dst);
  1625. src32 += width - 1;
  1626. for (x = 0; x < width - 1; x += 2) {
  1627. dst32[x] = src32[0];
  1628. dst32[x + 1] = src32[-1];
  1629. src32 -= 2;
  1630. }
  1631. if (width & 1) {
  1632. dst32[width - 1] = src32[0];
  1633. }
  1634. }
  1635. void SplitUVRow_C(const uint8* src_uv, uint8* dst_u, uint8* dst_v, int width) {
  1636. int x;
  1637. for (x = 0; x < width - 1; x += 2) {
  1638. dst_u[x] = src_uv[0];
  1639. dst_u[x + 1] = src_uv[2];
  1640. dst_v[x] = src_uv[1];
  1641. dst_v[x + 1] = src_uv[3];
  1642. src_uv += 4;
  1643. }
  1644. if (width & 1) {
  1645. dst_u[width - 1] = src_uv[0];
  1646. dst_v[width - 1] = src_uv[1];
  1647. }
  1648. }
  1649. void MergeUVRow_C(const uint8* src_u,
  1650. const uint8* src_v,
  1651. uint8* dst_uv,
  1652. int width) {
  1653. int x;
  1654. for (x = 0; x < width - 1; x += 2) {
  1655. dst_uv[0] = src_u[x];
  1656. dst_uv[1] = src_v[x];
  1657. dst_uv[2] = src_u[x + 1];
  1658. dst_uv[3] = src_v[x + 1];
  1659. dst_uv += 4;
  1660. }
  1661. if (width & 1) {
  1662. dst_uv[0] = src_u[width - 1];
  1663. dst_uv[1] = src_v[width - 1];
  1664. }
  1665. }
  1666. void SplitRGBRow_C(const uint8* src_rgb,
  1667. uint8* dst_r,
  1668. uint8* dst_g,
  1669. uint8* dst_b,
  1670. int width) {
  1671. int x;
  1672. for (x = 0; x < width; ++x) {
  1673. dst_r[x] = src_rgb[0];
  1674. dst_g[x] = src_rgb[1];
  1675. dst_b[x] = src_rgb[2];
  1676. src_rgb += 3;
  1677. }
  1678. }
  1679. void MergeRGBRow_C(const uint8* src_r,
  1680. const uint8* src_g,
  1681. const uint8* src_b,
  1682. uint8* dst_rgb,
  1683. int width) {
  1684. int x;
  1685. for (x = 0; x < width; ++x) {
  1686. dst_rgb[0] = src_r[x];
  1687. dst_rgb[1] = src_g[x];
  1688. dst_rgb[2] = src_b[x];
  1689. dst_rgb += 3;
  1690. }
  1691. }
  1692. void MergeUVRow_16_C(const uint16* src_u,
  1693. const uint16* src_v,
  1694. uint16* dst_uv,
  1695. int scale,
  1696. int width) {
  1697. int x;
  1698. for (x = 0; x < width - 1; x += 2) {
  1699. dst_uv[0] = src_u[x] * scale;
  1700. dst_uv[1] = src_v[x] * scale;
  1701. dst_uv[2] = src_u[x + 1] * scale;
  1702. dst_uv[3] = src_v[x + 1] * scale;
  1703. dst_uv += 4;
  1704. }
  1705. if (width & 1) {
  1706. dst_uv[0] = src_u[width - 1] * scale;
  1707. dst_uv[1] = src_v[width - 1] * scale;
  1708. }
  1709. }
  1710. void MultiplyRow_16_C(const uint16* src_y,
  1711. uint16* dst_y,
  1712. int scale,
  1713. int width) {
  1714. int x;
  1715. for (x = 0; x < width; ++x) {
  1716. dst_y[x] = src_y[x] * scale;
  1717. }
  1718. }
  1719. void CopyRow_C(const uint8* src, uint8* dst, int count) {
  1720. memcpy(dst, src, count);
  1721. }
  1722. void CopyRow_16_C(const uint16* src, uint16* dst, int count) {
  1723. memcpy(dst, src, count * 2);
  1724. }
  1725. void SetRow_C(uint8* dst, uint8 v8, int width) {
  1726. memset(dst, v8, width);
  1727. }
  1728. void ARGBSetRow_C(uint8* dst_argb, uint32 v32, int width) {
  1729. uint32* d = (uint32*)(dst_argb);
  1730. int x;
  1731. for (x = 0; x < width; ++x) {
  1732. d[x] = v32;
  1733. }
  1734. }
  1735. // Filter 2 rows of YUY2 UV's (422) into U and V (420).
  1736. void YUY2ToUVRow_C(const uint8* src_yuy2,
  1737. int src_stride_yuy2,
  1738. uint8* dst_u,
  1739. uint8* dst_v,
  1740. int width) {
  1741. // Output a row of UV values, filtering 2 rows of YUY2.
  1742. int x;
  1743. for (x = 0; x < width; x += 2) {
  1744. dst_u[0] = (src_yuy2[1] + src_yuy2[src_stride_yuy2 + 1] + 1) >> 1;
  1745. dst_v[0] = (src_yuy2[3] + src_yuy2[src_stride_yuy2 + 3] + 1) >> 1;
  1746. src_yuy2 += 4;
  1747. dst_u += 1;
  1748. dst_v += 1;
  1749. }
  1750. }
  1751. // Copy row of YUY2 UV's (422) into U and V (422).
  1752. void YUY2ToUV422Row_C(const uint8* src_yuy2,
  1753. uint8* dst_u,
  1754. uint8* dst_v,
  1755. int width) {
  1756. // Output a row of UV values.
  1757. int x;
  1758. for (x = 0; x < width; x += 2) {
  1759. dst_u[0] = src_yuy2[1];
  1760. dst_v[0] = src_yuy2[3];
  1761. src_yuy2 += 4;
  1762. dst_u += 1;
  1763. dst_v += 1;
  1764. }
  1765. }
  1766. // Copy row of YUY2 Y's (422) into Y (420/422).
  1767. void YUY2ToYRow_C(const uint8* src_yuy2, uint8* dst_y, int width) {
  1768. // Output a row of Y values.
  1769. int x;
  1770. for (x = 0; x < width - 1; x += 2) {
  1771. dst_y[x] = src_yuy2[0];
  1772. dst_y[x + 1] = src_yuy2[2];
  1773. src_yuy2 += 4;
  1774. }
  1775. if (width & 1) {
  1776. dst_y[width - 1] = src_yuy2[0];
  1777. }
  1778. }
  1779. // Filter 2 rows of UYVY UV's (422) into U and V (420).
  1780. void UYVYToUVRow_C(const uint8* src_uyvy,
  1781. int src_stride_uyvy,
  1782. uint8* dst_u,
  1783. uint8* dst_v,
  1784. int width) {
  1785. // Output a row of UV values.
  1786. int x;
  1787. for (x = 0; x < width; x += 2) {
  1788. dst_u[0] = (src_uyvy[0] + src_uyvy[src_stride_uyvy + 0] + 1) >> 1;
  1789. dst_v[0] = (src_uyvy[2] + src_uyvy[src_stride_uyvy + 2] + 1) >> 1;
  1790. src_uyvy += 4;
  1791. dst_u += 1;
  1792. dst_v += 1;
  1793. }
  1794. }
  1795. // Copy row of UYVY UV's (422) into U and V (422).
  1796. void UYVYToUV422Row_C(const uint8* src_uyvy,
  1797. uint8* dst_u,
  1798. uint8* dst_v,
  1799. int width) {
  1800. // Output a row of UV values.
  1801. int x;
  1802. for (x = 0; x < width; x += 2) {
  1803. dst_u[0] = src_uyvy[0];
  1804. dst_v[0] = src_uyvy[2];
  1805. src_uyvy += 4;
  1806. dst_u += 1;
  1807. dst_v += 1;
  1808. }
  1809. }
  1810. // Copy row of UYVY Y's (422) into Y (420/422).
  1811. void UYVYToYRow_C(const uint8* src_uyvy, uint8* dst_y, int width) {
  1812. // Output a row of Y values.
  1813. int x;
  1814. for (x = 0; x < width - 1; x += 2) {
  1815. dst_y[x] = src_uyvy[1];
  1816. dst_y[x + 1] = src_uyvy[3];
  1817. src_uyvy += 4;
  1818. }
  1819. if (width & 1) {
  1820. dst_y[width - 1] = src_uyvy[1];
  1821. }
  1822. }
  1823. #define BLEND(f, b, a) (((256 - a) * b) >> 8) + f
  1824. // Blend src_argb0 over src_argb1 and store to dst_argb.
  1825. // dst_argb may be src_argb0 or src_argb1.
  1826. // This code mimics the SSSE3 version for better testability.
  1827. void ARGBBlendRow_C(const uint8* src_argb0,
  1828. const uint8* src_argb1,
  1829. uint8* dst_argb,
  1830. int width) {
  1831. int x;
  1832. for (x = 0; x < width - 1; x += 2) {
  1833. uint32 fb = src_argb0[0];
  1834. uint32 fg = src_argb0[1];
  1835. uint32 fr = src_argb0[2];
  1836. uint32 a = src_argb0[3];
  1837. uint32 bb = src_argb1[0];
  1838. uint32 bg = src_argb1[1];
  1839. uint32 br = src_argb1[2];
  1840. dst_argb[0] = BLEND(fb, bb, a);
  1841. dst_argb[1] = BLEND(fg, bg, a);
  1842. dst_argb[2] = BLEND(fr, br, a);
  1843. dst_argb[3] = 255u;
  1844. fb = src_argb0[4 + 0];
  1845. fg = src_argb0[4 + 1];
  1846. fr = src_argb0[4 + 2];
  1847. a = src_argb0[4 + 3];
  1848. bb = src_argb1[4 + 0];
  1849. bg = src_argb1[4 + 1];
  1850. br = src_argb1[4 + 2];
  1851. dst_argb[4 + 0] = BLEND(fb, bb, a);
  1852. dst_argb[4 + 1] = BLEND(fg, bg, a);
  1853. dst_argb[4 + 2] = BLEND(fr, br, a);
  1854. dst_argb[4 + 3] = 255u;
  1855. src_argb0 += 8;
  1856. src_argb1 += 8;
  1857. dst_argb += 8;
  1858. }
  1859. if (width & 1) {
  1860. uint32 fb = src_argb0[0];
  1861. uint32 fg = src_argb0[1];
  1862. uint32 fr = src_argb0[2];
  1863. uint32 a = src_argb0[3];
  1864. uint32 bb = src_argb1[0];
  1865. uint32 bg = src_argb1[1];
  1866. uint32 br = src_argb1[2];
  1867. dst_argb[0] = BLEND(fb, bb, a);
  1868. dst_argb[1] = BLEND(fg, bg, a);
  1869. dst_argb[2] = BLEND(fr, br, a);
  1870. dst_argb[3] = 255u;
  1871. }
  1872. }
  1873. #undef BLEND
  1874. #define UBLEND(f, b, a) (((a)*f) + ((255 - a) * b) + 255) >> 8
  1875. void BlendPlaneRow_C(const uint8* src0,
  1876. const uint8* src1,
  1877. const uint8* alpha,
  1878. uint8* dst,
  1879. int width) {
  1880. int x;
  1881. for (x = 0; x < width - 1; x += 2) {
  1882. dst[0] = UBLEND(src0[0], src1[0], alpha[0]);
  1883. dst[1] = UBLEND(src0[1], src1[1], alpha[1]);
  1884. src0 += 2;
  1885. src1 += 2;
  1886. alpha += 2;
  1887. dst += 2;
  1888. }
  1889. if (width & 1) {
  1890. dst[0] = UBLEND(src0[0], src1[0], alpha[0]);
  1891. }
  1892. }
  1893. #undef UBLEND
  1894. #define ATTENUATE(f, a) (a | (a << 8)) * (f | (f << 8)) >> 24
  1895. // Multiply source RGB by alpha and store to destination.
  1896. // This code mimics the SSSE3 version for better testability.
  1897. void ARGBAttenuateRow_C(const uint8* src_argb, uint8* dst_argb, int width) {
  1898. int i;
  1899. for (i = 0; i < width - 1; i += 2) {
  1900. uint32 b = src_argb[0];
  1901. uint32 g = src_argb[1];
  1902. uint32 r = src_argb[2];
  1903. uint32 a = src_argb[3];
  1904. dst_argb[0] = ATTENUATE(b, a);
  1905. dst_argb[1] = ATTENUATE(g, a);
  1906. dst_argb[2] = ATTENUATE(r, a);
  1907. dst_argb[3] = a;
  1908. b = src_argb[4];
  1909. g = src_argb[5];
  1910. r = src_argb[6];
  1911. a = src_argb[7];
  1912. dst_argb[4] = ATTENUATE(b, a);
  1913. dst_argb[5] = ATTENUATE(g, a);
  1914. dst_argb[6] = ATTENUATE(r, a);
  1915. dst_argb[7] = a;
  1916. src_argb += 8;
  1917. dst_argb += 8;
  1918. }
  1919. if (width & 1) {
  1920. const uint32 b = src_argb[0];
  1921. const uint32 g = src_argb[1];
  1922. const uint32 r = src_argb[2];
  1923. const uint32 a = src_argb[3];
  1924. dst_argb[0] = ATTENUATE(b, a);
  1925. dst_argb[1] = ATTENUATE(g, a);
  1926. dst_argb[2] = ATTENUATE(r, a);
  1927. dst_argb[3] = a;
  1928. }
  1929. }
  1930. #undef ATTENUATE
  1931. // Divide source RGB by alpha and store to destination.
  1932. // b = (b * 255 + (a / 2)) / a;
  1933. // g = (g * 255 + (a / 2)) / a;
  1934. // r = (r * 255 + (a / 2)) / a;
  1935. // Reciprocal method is off by 1 on some values. ie 125
  1936. // 8.8 fixed point inverse table with 1.0 in upper short and 1 / a in lower.
  1937. #define T(a) 0x01000000 + (0x10000 / a)
  1938. const uint32 fixed_invtbl8[256] = {
  1939. 0x01000000, 0x0100ffff, T(0x02), T(0x03), T(0x04), T(0x05), T(0x06),
  1940. T(0x07), T(0x08), T(0x09), T(0x0a), T(0x0b), T(0x0c), T(0x0d),
  1941. T(0x0e), T(0x0f), T(0x10), T(0x11), T(0x12), T(0x13), T(0x14),
  1942. T(0x15), T(0x16), T(0x17), T(0x18), T(0x19), T(0x1a), T(0x1b),
  1943. T(0x1c), T(0x1d), T(0x1e), T(0x1f), T(0x20), T(0x21), T(0x22),
  1944. T(0x23), T(0x24), T(0x25), T(0x26), T(0x27), T(0x28), T(0x29),
  1945. T(0x2a), T(0x2b), T(0x2c), T(0x2d), T(0x2e), T(0x2f), T(0x30),
  1946. T(0x31), T(0x32), T(0x33), T(0x34), T(0x35), T(0x36), T(0x37),
  1947. T(0x38), T(0x39), T(0x3a), T(0x3b), T(0x3c), T(0x3d), T(0x3e),
  1948. T(0x3f), T(0x40), T(0x41), T(0x42), T(0x43), T(0x44), T(0x45),
  1949. T(0x46), T(0x47), T(0x48), T(0x49), T(0x4a), T(0x4b), T(0x4c),
  1950. T(0x4d), T(0x4e), T(0x4f), T(0x50), T(0x51), T(0x52), T(0x53),
  1951. T(0x54), T(0x55), T(0x56), T(0x57), T(0x58), T(0x59), T(0x5a),
  1952. T(0x5b), T(0x5c), T(0x5d), T(0x5e), T(0x5f), T(0x60), T(0x61),
  1953. T(0x62), T(0x63), T(0x64), T(0x65), T(0x66), T(0x67), T(0x68),
  1954. T(0x69), T(0x6a), T(0x6b), T(0x6c), T(0x6d), T(0x6e), T(0x6f),
  1955. T(0x70), T(0x71), T(0x72), T(0x73), T(0x74), T(0x75), T(0x76),
  1956. T(0x77), T(0x78), T(0x79), T(0x7a), T(0x7b), T(0x7c), T(0x7d),
  1957. T(0x7e), T(0x7f), T(0x80), T(0x81), T(0x82), T(0x83), T(0x84),
  1958. T(0x85), T(0x86), T(0x87), T(0x88), T(0x89), T(0x8a), T(0x8b),
  1959. T(0x8c), T(0x8d), T(0x8e), T(0x8f), T(0x90), T(0x91), T(0x92),
  1960. T(0x93), T(0x94), T(0x95), T(0x96), T(0x97), T(0x98), T(0x99),
  1961. T(0x9a), T(0x9b), T(0x9c), T(0x9d), T(0x9e), T(0x9f), T(0xa0),
  1962. T(0xa1), T(0xa2), T(0xa3), T(0xa4), T(0xa5), T(0xa6), T(0xa7),
  1963. T(0xa8), T(0xa9), T(0xaa), T(0xab), T(0xac), T(0xad), T(0xae),
  1964. T(0xaf), T(0xb0), T(0xb1), T(0xb2), T(0xb3), T(0xb4), T(0xb5),
  1965. T(0xb6), T(0xb7), T(0xb8), T(0xb9), T(0xba), T(0xbb), T(0xbc),
  1966. T(0xbd), T(0xbe), T(0xbf), T(0xc0), T(0xc1), T(0xc2), T(0xc3),
  1967. T(0xc4), T(0xc5), T(0xc6), T(0xc7), T(0xc8), T(0xc9), T(0xca),
  1968. T(0xcb), T(0xcc), T(0xcd), T(0xce), T(0xcf), T(0xd0), T(0xd1),
  1969. T(0xd2), T(0xd3), T(0xd4), T(0xd5), T(0xd6), T(0xd7), T(0xd8),
  1970. T(0xd9), T(0xda), T(0xdb), T(0xdc), T(0xdd), T(0xde), T(0xdf),
  1971. T(0xe0), T(0xe1), T(0xe2), T(0xe3), T(0xe4), T(0xe5), T(0xe6),
  1972. T(0xe7), T(0xe8), T(0xe9), T(0xea), T(0xeb), T(0xec), T(0xed),
  1973. T(0xee), T(0xef), T(0xf0), T(0xf1), T(0xf2), T(0xf3), T(0xf4),
  1974. T(0xf5), T(0xf6), T(0xf7), T(0xf8), T(0xf9), T(0xfa), T(0xfb),
  1975. T(0xfc), T(0xfd), T(0xfe), 0x01000100};
  1976. #undef T
  1977. void ARGBUnattenuateRow_C(const uint8* src_argb, uint8* dst_argb, int width) {
  1978. int i;
  1979. for (i = 0; i < width; ++i) {
  1980. uint32 b = src_argb[0];
  1981. uint32 g = src_argb[1];
  1982. uint32 r = src_argb[2];
  1983. const uint32 a = src_argb[3];
  1984. const uint32 ia = fixed_invtbl8[a] & 0xffff; // 8.8 fixed point
  1985. b = (b * ia) >> 8;
  1986. g = (g * ia) >> 8;
  1987. r = (r * ia) >> 8;
  1988. // Clamping should not be necessary but is free in assembly.
  1989. dst_argb[0] = clamp255(b);
  1990. dst_argb[1] = clamp255(g);
  1991. dst_argb[2] = clamp255(r);
  1992. dst_argb[3] = a;
  1993. src_argb += 4;
  1994. dst_argb += 4;
  1995. }
  1996. }
  1997. void ComputeCumulativeSumRow_C(const uint8* row,
  1998. int32* cumsum,
  1999. const int32* previous_cumsum,
  2000. int width) {
  2001. int32 row_sum[4] = {0, 0, 0, 0};
  2002. int x;
  2003. for (x = 0; x < width; ++x) {
  2004. row_sum[0] += row[x * 4 + 0];
  2005. row_sum[1] += row[x * 4 + 1];
  2006. row_sum[2] += row[x * 4 + 2];
  2007. row_sum[3] += row[x * 4 + 3];
  2008. cumsum[x * 4 + 0] = row_sum[0] + previous_cumsum[x * 4 + 0];
  2009. cumsum[x * 4 + 1] = row_sum[1] + previous_cumsum[x * 4 + 1];
  2010. cumsum[x * 4 + 2] = row_sum[2] + previous_cumsum[x * 4 + 2];
  2011. cumsum[x * 4 + 3] = row_sum[3] + previous_cumsum[x * 4 + 3];
  2012. }
  2013. }
  2014. void CumulativeSumToAverageRow_C(const int32* tl,
  2015. const int32* bl,
  2016. int w,
  2017. int area,
  2018. uint8* dst,
  2019. int count) {
  2020. float ooa = 1.0f / area;
  2021. int i;
  2022. for (i = 0; i < count; ++i) {
  2023. dst[0] = (uint8)((bl[w + 0] + tl[0] - bl[0] - tl[w + 0]) * ooa);
  2024. dst[1] = (uint8)((bl[w + 1] + tl[1] - bl[1] - tl[w + 1]) * ooa);
  2025. dst[2] = (uint8)((bl[w + 2] + tl[2] - bl[2] - tl[w + 2]) * ooa);
  2026. dst[3] = (uint8)((bl[w + 3] + tl[3] - bl[3] - tl[w + 3]) * ooa);
  2027. dst += 4;
  2028. tl += 4;
  2029. bl += 4;
  2030. }
  2031. }
  2032. // Copy pixels from rotated source to destination row with a slope.
  2033. LIBYUV_API
  2034. void ARGBAffineRow_C(const uint8* src_argb,
  2035. int src_argb_stride,
  2036. uint8* dst_argb,
  2037. const float* uv_dudv,
  2038. int width) {
  2039. int i;
  2040. // Render a row of pixels from source into a buffer.
  2041. float uv[2];
  2042. uv[0] = uv_dudv[0];
  2043. uv[1] = uv_dudv[1];
  2044. for (i = 0; i < width; ++i) {
  2045. int x = (int)(uv[0]);
  2046. int y = (int)(uv[1]);
  2047. *(uint32*)(dst_argb) =
  2048. *(const uint32*)(src_argb + y * src_argb_stride + x * 4);
  2049. dst_argb += 4;
  2050. uv[0] += uv_dudv[2];
  2051. uv[1] += uv_dudv[3];
  2052. }
  2053. }
  2054. // Blend 2 rows into 1.
  2055. static void HalfRow_C(const uint8* src_uv,
  2056. ptrdiff_t src_uv_stride,
  2057. uint8* dst_uv,
  2058. int width) {
  2059. int x;
  2060. for (x = 0; x < width; ++x) {
  2061. dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
  2062. }
  2063. }
  2064. static void HalfRow_16_C(const uint16* src_uv,
  2065. ptrdiff_t src_uv_stride,
  2066. uint16* dst_uv,
  2067. int width) {
  2068. int x;
  2069. for (x = 0; x < width; ++x) {
  2070. dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
  2071. }
  2072. }
  2073. // C version 2x2 -> 2x1.
  2074. void InterpolateRow_C(uint8* dst_ptr,
  2075. const uint8* src_ptr,
  2076. ptrdiff_t src_stride,
  2077. int width,
  2078. int source_y_fraction) {
  2079. int y1_fraction = source_y_fraction;
  2080. int y0_fraction = 256 - y1_fraction;
  2081. const uint8* src_ptr1 = src_ptr + src_stride;
  2082. int x;
  2083. if (y1_fraction == 0) {
  2084. memcpy(dst_ptr, src_ptr, width);
  2085. return;
  2086. }
  2087. if (y1_fraction == 128) {
  2088. HalfRow_C(src_ptr, src_stride, dst_ptr, width);
  2089. return;
  2090. }
  2091. for (x = 0; x < width - 1; x += 2) {
  2092. dst_ptr[0] =
  2093. (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction + 128) >> 8;
  2094. dst_ptr[1] =
  2095. (src_ptr[1] * y0_fraction + src_ptr1[1] * y1_fraction + 128) >> 8;
  2096. src_ptr += 2;
  2097. src_ptr1 += 2;
  2098. dst_ptr += 2;
  2099. }
  2100. if (width & 1) {
  2101. dst_ptr[0] =
  2102. (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction + 128) >> 8;
  2103. }
  2104. }
  2105. void InterpolateRow_16_C(uint16* dst_ptr,
  2106. const uint16* src_ptr,
  2107. ptrdiff_t src_stride,
  2108. int width,
  2109. int source_y_fraction) {
  2110. int y1_fraction = source_y_fraction;
  2111. int y0_fraction = 256 - y1_fraction;
  2112. const uint16* src_ptr1 = src_ptr + src_stride;
  2113. int x;
  2114. if (source_y_fraction == 0) {
  2115. memcpy(dst_ptr, src_ptr, width * 2);
  2116. return;
  2117. }
  2118. if (source_y_fraction == 128) {
  2119. HalfRow_16_C(src_ptr, src_stride, dst_ptr, width);
  2120. return;
  2121. }
  2122. for (x = 0; x < width - 1; x += 2) {
  2123. dst_ptr[0] = (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction) >> 8;
  2124. dst_ptr[1] = (src_ptr[1] * y0_fraction + src_ptr1[1] * y1_fraction) >> 8;
  2125. src_ptr += 2;
  2126. src_ptr1 += 2;
  2127. dst_ptr += 2;
  2128. }
  2129. if (width & 1) {
  2130. dst_ptr[0] = (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction) >> 8;
  2131. }
  2132. }
  2133. // Use first 4 shuffler values to reorder ARGB channels.
  2134. void ARGBShuffleRow_C(const uint8* src_argb,
  2135. uint8* dst_argb,
  2136. const uint8* shuffler,
  2137. int width) {
  2138. int index0 = shuffler[0];
  2139. int index1 = shuffler[1];
  2140. int index2 = shuffler[2];
  2141. int index3 = shuffler[3];
  2142. // Shuffle a row of ARGB.
  2143. int x;
  2144. for (x = 0; x < width; ++x) {
  2145. // To support in-place conversion.
  2146. uint8 b = src_argb[index0];
  2147. uint8 g = src_argb[index1];
  2148. uint8 r = src_argb[index2];
  2149. uint8 a = src_argb[index3];
  2150. dst_argb[0] = b;
  2151. dst_argb[1] = g;
  2152. dst_argb[2] = r;
  2153. dst_argb[3] = a;
  2154. src_argb += 4;
  2155. dst_argb += 4;
  2156. }
  2157. }
  2158. void I422ToYUY2Row_C(const uint8* src_y,
  2159. const uint8* src_u,
  2160. const uint8* src_v,
  2161. uint8* dst_frame,
  2162. int width) {
  2163. int x;
  2164. for (x = 0; x < width - 1; x += 2) {
  2165. dst_frame[0] = src_y[0];
  2166. dst_frame[1] = src_u[0];
  2167. dst_frame[2] = src_y[1];
  2168. dst_frame[3] = src_v[0];
  2169. dst_frame += 4;
  2170. src_y += 2;
  2171. src_u += 1;
  2172. src_v += 1;
  2173. }
  2174. if (width & 1) {
  2175. dst_frame[0] = src_y[0];
  2176. dst_frame[1] = src_u[0];
  2177. dst_frame[2] = 0;
  2178. dst_frame[3] = src_v[0];
  2179. }
  2180. }
  2181. void I422ToUYVYRow_C(const uint8* src_y,
  2182. const uint8* src_u,
  2183. const uint8* src_v,
  2184. uint8* dst_frame,
  2185. int width) {
  2186. int x;
  2187. for (x = 0; x < width - 1; x += 2) {
  2188. dst_frame[0] = src_u[0];
  2189. dst_frame[1] = src_y[0];
  2190. dst_frame[2] = src_v[0];
  2191. dst_frame[3] = src_y[1];
  2192. dst_frame += 4;
  2193. src_y += 2;
  2194. src_u += 1;
  2195. src_v += 1;
  2196. }
  2197. if (width & 1) {
  2198. dst_frame[0] = src_u[0];
  2199. dst_frame[1] = src_y[0];
  2200. dst_frame[2] = src_v[0];
  2201. dst_frame[3] = 0;
  2202. }
  2203. }
  2204. void ARGBPolynomialRow_C(const uint8* src_argb,
  2205. uint8* dst_argb,
  2206. const float* poly,
  2207. int width) {
  2208. int i;
  2209. for (i = 0; i < width; ++i) {
  2210. float b = (float)(src_argb[0]);
  2211. float g = (float)(src_argb[1]);
  2212. float r = (float)(src_argb[2]);
  2213. float a = (float)(src_argb[3]);
  2214. float b2 = b * b;
  2215. float g2 = g * g;
  2216. float r2 = r * r;
  2217. float a2 = a * a;
  2218. float db = poly[0] + poly[4] * b;
  2219. float dg = poly[1] + poly[5] * g;
  2220. float dr = poly[2] + poly[6] * r;
  2221. float da = poly[3] + poly[7] * a;
  2222. float b3 = b2 * b;
  2223. float g3 = g2 * g;
  2224. float r3 = r2 * r;
  2225. float a3 = a2 * a;
  2226. db += poly[8] * b2;
  2227. dg += poly[9] * g2;
  2228. dr += poly[10] * r2;
  2229. da += poly[11] * a2;
  2230. db += poly[12] * b3;
  2231. dg += poly[13] * g3;
  2232. dr += poly[14] * r3;
  2233. da += poly[15] * a3;
  2234. dst_argb[0] = Clamp((int32)(db));
  2235. dst_argb[1] = Clamp((int32)(dg));
  2236. dst_argb[2] = Clamp((int32)(dr));
  2237. dst_argb[3] = Clamp((int32)(da));
  2238. src_argb += 4;
  2239. dst_argb += 4;
  2240. }
  2241. }
  2242. // Samples assumed to be unsigned in low 9, 10 or 12 bits. Scale factor
  2243. // adjust the source integer range to the half float range desired.
  2244. // This magic constant is 2^-112. Multiplying by this
  2245. // is the same as subtracting 112 from the exponent, which
  2246. // is the difference in exponent bias between 32-bit and
  2247. // 16-bit floats. Once we've done this subtraction, we can
  2248. // simply extract the low bits of the exponent and the high
  2249. // bits of the mantissa from our float and we're done.
  2250. void HalfFloatRow_C(const uint16* src, uint16* dst, float scale, int width) {
  2251. int i;
  2252. float mult = 1.9259299444e-34f * scale;
  2253. for (i = 0; i < width; ++i) {
  2254. float value = src[i] * mult;
  2255. dst[i] = (uint16)((*(uint32_t*)&value) >> 13);
  2256. }
  2257. }
  2258. void ARGBLumaColorTableRow_C(const uint8* src_argb,
  2259. uint8* dst_argb,
  2260. int width,
  2261. const uint8* luma,
  2262. uint32 lumacoeff) {
  2263. uint32 bc = lumacoeff & 0xff;
  2264. uint32 gc = (lumacoeff >> 8) & 0xff;
  2265. uint32 rc = (lumacoeff >> 16) & 0xff;
  2266. int i;
  2267. for (i = 0; i < width - 1; i += 2) {
  2268. // Luminance in rows, color values in columns.
  2269. const uint8* luma0 =
  2270. ((src_argb[0] * bc + src_argb[1] * gc + src_argb[2] * rc) & 0x7F00u) +
  2271. luma;
  2272. const uint8* luma1;
  2273. dst_argb[0] = luma0[src_argb[0]];
  2274. dst_argb[1] = luma0[src_argb[1]];
  2275. dst_argb[2] = luma0[src_argb[2]];
  2276. dst_argb[3] = src_argb[3];
  2277. luma1 =
  2278. ((src_argb[4] * bc + src_argb[5] * gc + src_argb[6] * rc) & 0x7F00u) +
  2279. luma;
  2280. dst_argb[4] = luma1[src_argb[4]];
  2281. dst_argb[5] = luma1[src_argb[5]];
  2282. dst_argb[6] = luma1[src_argb[6]];
  2283. dst_argb[7] = src_argb[7];
  2284. src_argb += 8;
  2285. dst_argb += 8;
  2286. }
  2287. if (width & 1) {
  2288. // Luminance in rows, color values in columns.
  2289. const uint8* luma0 =
  2290. ((src_argb[0] * bc + src_argb[1] * gc + src_argb[2] * rc) & 0x7F00u) +
  2291. luma;
  2292. dst_argb[0] = luma0[src_argb[0]];
  2293. dst_argb[1] = luma0[src_argb[1]];
  2294. dst_argb[2] = luma0[src_argb[2]];
  2295. dst_argb[3] = src_argb[3];
  2296. }
  2297. }
  2298. void ARGBCopyAlphaRow_C(const uint8* src, uint8* dst, int width) {
  2299. int i;
  2300. for (i = 0; i < width - 1; i += 2) {
  2301. dst[3] = src[3];
  2302. dst[7] = src[7];
  2303. dst += 8;
  2304. src += 8;
  2305. }
  2306. if (width & 1) {
  2307. dst[3] = src[3];
  2308. }
  2309. }
  2310. void ARGBExtractAlphaRow_C(const uint8* src_argb, uint8* dst_a, int width) {
  2311. int i;
  2312. for (i = 0; i < width - 1; i += 2) {
  2313. dst_a[0] = src_argb[3];
  2314. dst_a[1] = src_argb[7];
  2315. dst_a += 2;
  2316. src_argb += 8;
  2317. }
  2318. if (width & 1) {
  2319. dst_a[0] = src_argb[3];
  2320. }
  2321. }
  2322. void ARGBCopyYToAlphaRow_C(const uint8* src, uint8* dst, int width) {
  2323. int i;
  2324. for (i = 0; i < width - 1; i += 2) {
  2325. dst[3] = src[0];
  2326. dst[7] = src[1];
  2327. dst += 8;
  2328. src += 2;
  2329. }
  2330. if (width & 1) {
  2331. dst[3] = src[0];
  2332. }
  2333. }
  2334. // Maximum temporary width for wrappers to process at a time, in pixels.
  2335. #define MAXTWIDTH 2048
  2336. #if !(defined(_MSC_VER) && defined(_M_IX86)) && \
  2337. defined(HAS_I422TORGB565ROW_SSSE3)
  2338. // row_win.cc has asm version, but GCC uses 2 step wrapper.
  2339. void I422ToRGB565Row_SSSE3(const uint8* src_y,
  2340. const uint8* src_u,
  2341. const uint8* src_v,
  2342. uint8* dst_rgb565,
  2343. const struct YuvConstants* yuvconstants,
  2344. int width) {
  2345. SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  2346. while (width > 0) {
  2347. int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
  2348. I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
  2349. ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
  2350. src_y += twidth;
  2351. src_u += twidth / 2;
  2352. src_v += twidth / 2;
  2353. dst_rgb565 += twidth * 2;
  2354. width -= twidth;
  2355. }
  2356. }
  2357. #endif
  2358. #if defined(HAS_I422TOARGB1555ROW_SSSE3)
  2359. void I422ToARGB1555Row_SSSE3(const uint8* src_y,
  2360. const uint8* src_u,
  2361. const uint8* src_v,
  2362. uint8* dst_argb1555,
  2363. const struct YuvConstants* yuvconstants,
  2364. int width) {
  2365. // Row buffer for intermediate ARGB pixels.
  2366. SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  2367. while (width > 0) {
  2368. int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
  2369. I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
  2370. ARGBToARGB1555Row_SSE2(row, dst_argb1555, twidth);
  2371. src_y += twidth;
  2372. src_u += twidth / 2;
  2373. src_v += twidth / 2;
  2374. dst_argb1555 += twidth * 2;
  2375. width -= twidth;
  2376. }
  2377. }
  2378. #endif
  2379. #if defined(HAS_I422TOARGB4444ROW_SSSE3)
  2380. void I422ToARGB4444Row_SSSE3(const uint8* src_y,
  2381. const uint8* src_u,
  2382. const uint8* src_v,
  2383. uint8* dst_argb4444,
  2384. const struct YuvConstants* yuvconstants,
  2385. int width) {
  2386. // Row buffer for intermediate ARGB pixels.
  2387. SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  2388. while (width > 0) {
  2389. int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
  2390. I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
  2391. ARGBToARGB4444Row_SSE2(row, dst_argb4444, twidth);
  2392. src_y += twidth;
  2393. src_u += twidth / 2;
  2394. src_v += twidth / 2;
  2395. dst_argb4444 += twidth * 2;
  2396. width -= twidth;
  2397. }
  2398. }
  2399. #endif
  2400. #if defined(HAS_NV12TORGB565ROW_SSSE3)
  2401. void NV12ToRGB565Row_SSSE3(const uint8* src_y,
  2402. const uint8* src_uv,
  2403. uint8* dst_rgb565,
  2404. const struct YuvConstants* yuvconstants,
  2405. int width) {
  2406. // Row buffer for intermediate ARGB pixels.
  2407. SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  2408. while (width > 0) {
  2409. int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
  2410. NV12ToARGBRow_SSSE3(src_y, src_uv, row, yuvconstants, twidth);
  2411. ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
  2412. src_y += twidth;
  2413. src_uv += twidth;
  2414. dst_rgb565 += twidth * 2;
  2415. width -= twidth;
  2416. }
  2417. }
  2418. #endif
  2419. #if defined(HAS_I422TORGB565ROW_AVX2)
  2420. void I422ToRGB565Row_AVX2(const uint8* src_y,
  2421. const uint8* src_u,
  2422. const uint8* src_v,
  2423. uint8* dst_rgb565,
  2424. const struct YuvConstants* yuvconstants,
  2425. int width) {
  2426. SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  2427. while (width > 0) {
  2428. int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
  2429. I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
  2430. #if defined(HAS_ARGBTORGB565ROW_AVX2)
  2431. ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth);
  2432. #else
  2433. ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
  2434. #endif
  2435. src_y += twidth;
  2436. src_u += twidth / 2;
  2437. src_v += twidth / 2;
  2438. dst_rgb565 += twidth * 2;
  2439. width -= twidth;
  2440. }
  2441. }
  2442. #endif
  2443. #if defined(HAS_I422TOARGB1555ROW_AVX2)
  2444. void I422ToARGB1555Row_AVX2(const uint8* src_y,
  2445. const uint8* src_u,
  2446. const uint8* src_v,
  2447. uint8* dst_argb1555,
  2448. const struct YuvConstants* yuvconstants,
  2449. int width) {
  2450. // Row buffer for intermediate ARGB pixels.
  2451. SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  2452. while (width > 0) {
  2453. int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
  2454. I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
  2455. #if defined(HAS_ARGBTOARGB1555ROW_AVX2)
  2456. ARGBToARGB1555Row_AVX2(row, dst_argb1555, twidth);
  2457. #else
  2458. ARGBToARGB1555Row_SSE2(row, dst_argb1555, twidth);
  2459. #endif
  2460. src_y += twidth;
  2461. src_u += twidth / 2;
  2462. src_v += twidth / 2;
  2463. dst_argb1555 += twidth * 2;
  2464. width -= twidth;
  2465. }
  2466. }
  2467. #endif
  2468. #if defined(HAS_I422TOARGB4444ROW_AVX2)
  2469. void I422ToARGB4444Row_AVX2(const uint8* src_y,
  2470. const uint8* src_u,
  2471. const uint8* src_v,
  2472. uint8* dst_argb4444,
  2473. const struct YuvConstants* yuvconstants,
  2474. int width) {
  2475. // Row buffer for intermediate ARGB pixels.
  2476. SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  2477. while (width > 0) {
  2478. int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
  2479. I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
  2480. #if defined(HAS_ARGBTOARGB4444ROW_AVX2)
  2481. ARGBToARGB4444Row_AVX2(row, dst_argb4444, twidth);
  2482. #else
  2483. ARGBToARGB4444Row_SSE2(row, dst_argb4444, twidth);
  2484. #endif
  2485. src_y += twidth;
  2486. src_u += twidth / 2;
  2487. src_v += twidth / 2;
  2488. dst_argb4444 += twidth * 2;
  2489. width -= twidth;
  2490. }
  2491. }
  2492. #endif
  2493. #if defined(HAS_I422TORGB24ROW_AVX2)
  2494. void I422ToRGB24Row_AVX2(const uint8* src_y,
  2495. const uint8* src_u,
  2496. const uint8* src_v,
  2497. uint8* dst_rgb24,
  2498. const struct YuvConstants* yuvconstants,
  2499. int width) {
  2500. // Row buffer for intermediate ARGB pixels.
  2501. SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  2502. while (width > 0) {
  2503. int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
  2504. I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
  2505. // TODO(fbarchard): ARGBToRGB24Row_AVX2
  2506. ARGBToRGB24Row_SSSE3(row, dst_rgb24, twidth);
  2507. src_y += twidth;
  2508. src_u += twidth / 2;
  2509. src_v += twidth / 2;
  2510. dst_rgb24 += twidth * 3;
  2511. width -= twidth;
  2512. }
  2513. }
  2514. #endif
  2515. #if defined(HAS_NV12TORGB565ROW_AVX2)
  2516. void NV12ToRGB565Row_AVX2(const uint8* src_y,
  2517. const uint8* src_uv,
  2518. uint8* dst_rgb565,
  2519. const struct YuvConstants* yuvconstants,
  2520. int width) {
  2521. // Row buffer for intermediate ARGB pixels.
  2522. SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  2523. while (width > 0) {
  2524. int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
  2525. NV12ToARGBRow_AVX2(src_y, src_uv, row, yuvconstants, twidth);
  2526. #if defined(HAS_ARGBTORGB565ROW_AVX2)
  2527. ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth);
  2528. #else
  2529. ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
  2530. #endif
  2531. src_y += twidth;
  2532. src_uv += twidth;
  2533. dst_rgb565 += twidth * 2;
  2534. width -= twidth;
  2535. }
  2536. }
  2537. #endif
  2538. float ScaleSumSamples_C(const float* src, float* dst, float scale, int width) {
  2539. float fsum = 0.f;
  2540. int i;
  2541. #if defined(__clang__)
  2542. #pragma clang loop vectorize_width(4)
  2543. #endif
  2544. for (i = 0; i < width; ++i) {
  2545. float v = *src++;
  2546. fsum += v * v;
  2547. *dst++ = v * scale;
  2548. }
  2549. return fsum;
  2550. }
  2551. float ScaleMaxSamples_C(const float* src, float* dst, float scale, int width) {
  2552. float fmax = 0.f;
  2553. int i;
  2554. for (i = 0; i < width; ++i) {
  2555. float v = *src++;
  2556. float vs = v * scale;
  2557. fmax = (v > fmax) ? v : fmax;
  2558. *dst++ = vs;
  2559. }
  2560. return fmax;
  2561. }
  2562. void ScaleSamples_C(const float* src, float* dst, float scale, int width) {
  2563. int i;
  2564. for (i = 0; i < width; ++i) {
  2565. *dst++ = *src++ * scale;
  2566. }
  2567. }
  2568. void GaussRow_C(const uint32* src, uint16* dst, int width) {
  2569. int i;
  2570. for (i = 0; i < width; ++i) {
  2571. *dst++ =
  2572. (src[0] + src[1] * 4 + src[2] * 6 + src[3] * 4 + src[4] + 128) >> 8;
  2573. ++src;
  2574. }
  2575. }
  2576. // filter 5 rows with 1, 4, 6, 4, 1 coefficients to produce 1 row.
  2577. void GaussCol_C(const uint16* src0,
  2578. const uint16* src1,
  2579. const uint16* src2,
  2580. const uint16* src3,
  2581. const uint16* src4,
  2582. uint32* dst,
  2583. int width) {
  2584. int i;
  2585. for (i = 0; i < width; ++i) {
  2586. *dst++ = *src0++ + *src1++ * 4 + *src2++ * 6 + *src3++ * 4 + *src4++;
  2587. }
  2588. }
  2589. #ifdef __cplusplus
  2590. } // extern "C"
  2591. } // namespace libyuv
  2592. #endif