row_common.cc 84 KB

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