row_common.cc 104 KB

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